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
)
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 error_at (location_of (expr1
),
4968 "conditional expression in operand of fold-expression");
4971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4972 return finish_right_unary_fold_expr (expr1
, op
);
4974 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4976 cp_parser_error (parser
, "mismatched operator in fold-expression");
4977 return error_mark_node
;
4979 cp_lexer_consume_token (parser
->lexer
);
4981 // Binary left or right fold.
4982 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4983 if (expr2
== error_mark_node
)
4984 return error_mark_node
;
4985 return finish_binary_fold_expr (expr1
, expr2
, op
);
4988 /* Parse a primary-expression.
4995 lambda-expression (C++11)
5000 ( compound-statement )
5001 __builtin_va_arg ( assignment-expression , type-id )
5002 __builtin_offsetof ( type-id , offsetof-expression )
5005 __has_nothrow_assign ( type-id )
5006 __has_nothrow_constructor ( type-id )
5007 __has_nothrow_copy ( type-id )
5008 __has_trivial_assign ( type-id )
5009 __has_trivial_constructor ( type-id )
5010 __has_trivial_copy ( type-id )
5011 __has_trivial_destructor ( type-id )
5012 __has_virtual_destructor ( type-id )
5013 __is_abstract ( type-id )
5014 __is_base_of ( type-id , type-id )
5015 __is_class ( type-id )
5016 __is_empty ( type-id )
5017 __is_enum ( type-id )
5018 __is_final ( type-id )
5019 __is_literal_type ( type-id )
5020 __is_pod ( type-id )
5021 __is_polymorphic ( type-id )
5022 __is_std_layout ( type-id )
5023 __is_trivial ( type-id )
5024 __is_union ( type-id )
5026 Objective-C++ Extension:
5034 ADDRESS_P is true iff this expression was immediately preceded by
5035 "&" and therefore might denote a pointer-to-member. CAST_P is true
5036 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5037 true iff this expression is a template argument.
5039 Returns a representation of the expression. Upon return, *IDK
5040 indicates what kind of id-expression (if any) was present. */
5043 cp_parser_primary_expression (cp_parser
*parser
,
5046 bool template_arg_p
,
5050 cp_token
*token
= NULL
;
5052 /* Assume the primary expression is not an id-expression. */
5053 *idk
= CP_ID_KIND_NONE
;
5055 /* Peek at the next token. */
5056 token
= cp_lexer_peek_token (parser
->lexer
);
5057 switch ((int) token
->type
)
5066 user-defined-literal */
5073 case CPP_PREPARSED_EXPR
:
5074 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5075 return cp_parser_userdef_numeric_literal (parser
);
5076 token
= cp_lexer_consume_token (parser
->lexer
);
5077 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5079 error_at (token
->location
,
5080 "fixed-point types not supported in C++");
5081 return error_mark_node
;
5083 /* Floating-point literals are only allowed in an integral
5084 constant expression if they are cast to an integral or
5085 enumeration type. */
5086 if (TREE_CODE (token
->u
.value
) == REAL_CST
5087 && parser
->integral_constant_expression_p
5090 /* CAST_P will be set even in invalid code like "int(2.7 +
5091 ...)". Therefore, we have to check that the next token
5092 is sure to end the cast. */
5095 cp_token
*next_token
;
5097 next_token
= cp_lexer_peek_token (parser
->lexer
);
5098 if (/* The comma at the end of an
5099 enumerator-definition. */
5100 next_token
->type
!= CPP_COMMA
5101 /* The curly brace at the end of an enum-specifier. */
5102 && next_token
->type
!= CPP_CLOSE_BRACE
5103 /* The end of a statement. */
5104 && next_token
->type
!= CPP_SEMICOLON
5105 /* The end of the cast-expression. */
5106 && next_token
->type
!= CPP_CLOSE_PAREN
5107 /* The end of an array bound. */
5108 && next_token
->type
!= CPP_CLOSE_SQUARE
5109 /* The closing ">" in a template-argument-list. */
5110 && (next_token
->type
!= CPP_GREATER
5111 || parser
->greater_than_is_operator_p
)
5112 /* C++0x only: A ">>" treated like two ">" tokens,
5113 in a template-argument-list. */
5114 && (next_token
->type
!= CPP_RSHIFT
5115 || (cxx_dialect
== cxx98
)
5116 || parser
->greater_than_is_operator_p
))
5120 /* If we are within a cast, then the constraint that the
5121 cast is to an integral or enumeration type will be
5122 checked at that point. If we are not within a cast, then
5123 this code is invalid. */
5125 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5127 return cp_expr (token
->u
.value
, token
->location
);
5129 case CPP_CHAR_USERDEF
:
5130 case CPP_CHAR16_USERDEF
:
5131 case CPP_CHAR32_USERDEF
:
5132 case CPP_WCHAR_USERDEF
:
5133 case CPP_UTF8CHAR_USERDEF
:
5134 return cp_parser_userdef_char_literal (parser
);
5140 case CPP_UTF8STRING
:
5141 case CPP_STRING_USERDEF
:
5142 case CPP_STRING16_USERDEF
:
5143 case CPP_STRING32_USERDEF
:
5144 case CPP_WSTRING_USERDEF
:
5145 case CPP_UTF8STRING_USERDEF
:
5146 /* ??? Should wide strings be allowed when parser->translate_strings_p
5147 is false (i.e. in attributes)? If not, we can kill the third
5148 argument to cp_parser_string_literal. */
5149 return cp_parser_string_literal (parser
,
5150 parser
->translate_strings_p
,
5153 case CPP_OPEN_PAREN
:
5154 /* If we see `( { ' then we are looking at the beginning of
5155 a GNU statement-expression. */
5156 if (cp_parser_allow_gnu_extensions_p (parser
)
5157 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5159 /* Statement-expressions are not allowed by the standard. */
5160 pedwarn (token
->location
, OPT_Wpedantic
,
5161 "ISO C++ forbids braced-groups within expressions");
5163 /* And they're not allowed outside of a function-body; you
5164 cannot, for example, write:
5166 int i = ({ int j = 3; j + 1; });
5168 at class or namespace scope. */
5169 if (!parser
->in_function_body
5170 || parser
->in_template_argument_list_p
)
5172 error_at (token
->location
,
5173 "statement-expressions are not allowed outside "
5174 "functions nor in template-argument lists");
5175 cp_parser_skip_to_end_of_block_or_statement (parser
);
5176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5177 cp_lexer_consume_token (parser
->lexer
);
5178 return error_mark_node
;
5181 return cp_parser_statement_expr (parser
);
5183 /* Otherwise it's a normal parenthesized expression. */
5186 bool saved_greater_than_is_operator_p
;
5188 location_t open_paren_loc
= token
->location
;
5190 /* Consume the `('. */
5191 matching_parens parens
;
5192 parens
.consume_open (parser
);
5193 /* Within a parenthesized expression, a `>' token is always
5194 the greater-than operator. */
5195 saved_greater_than_is_operator_p
5196 = parser
->greater_than_is_operator_p
;
5197 parser
->greater_than_is_operator_p
= true;
5199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5200 /* Left fold expression. */
5203 /* Parse the parenthesized expression. */
5204 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5206 token
= cp_lexer_peek_token (parser
->lexer
);
5207 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5209 expr
= cp_parser_fold_expression (parser
, expr
);
5210 if (expr
!= error_mark_node
5211 && cxx_dialect
< cxx17
5212 && !in_system_header_at (input_location
))
5213 pedwarn (input_location
, 0, "fold-expressions only available "
5214 "with -std=c++17 or -std=gnu++17");
5217 /* Let the front end know that this expression was
5218 enclosed in parentheses. This matters in case, for
5219 example, the expression is of the form `A::B', since
5220 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5222 expr
= finish_parenthesized_expr (expr
);
5224 /* DR 705: Wrapping an unqualified name in parentheses
5225 suppresses arg-dependent lookup. We want to pass back
5226 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5227 (c++/37862), but none of the others. */
5228 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5229 *idk
= CP_ID_KIND_NONE
;
5231 /* The `>' token might be the end of a template-id or
5232 template-parameter-list now. */
5233 parser
->greater_than_is_operator_p
5234 = saved_greater_than_is_operator_p
;
5236 /* Consume the `)'. */
5237 token
= cp_lexer_peek_token (parser
->lexer
);
5238 location_t close_paren_loc
= token
->location
;
5239 expr
.set_range (open_paren_loc
, close_paren_loc
);
5240 if (!parens
.require_close (parser
)
5241 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5242 cp_parser_skip_to_end_of_statement (parser
);
5247 case CPP_OPEN_SQUARE
:
5249 if (c_dialect_objc ())
5251 /* We might have an Objective-C++ message. */
5252 cp_parser_parse_tentatively (parser
);
5253 tree msg
= cp_parser_objc_message_expression (parser
);
5254 /* If that works out, we're done ... */
5255 if (cp_parser_parse_definitely (parser
))
5257 /* ... else, fall though to see if it's a lambda. */
5259 cp_expr lam
= cp_parser_lambda_expression (parser
);
5260 /* Don't warn about a failed tentative parse. */
5261 if (cp_parser_error_occurred (parser
))
5262 return error_mark_node
;
5263 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5267 case CPP_OBJC_STRING
:
5268 if (c_dialect_objc ())
5269 /* We have an Objective-C++ string literal. */
5270 return cp_parser_objc_expression (parser
);
5271 cp_parser_error (parser
, "expected primary-expression");
5272 return error_mark_node
;
5275 switch (token
->keyword
)
5277 /* These two are the boolean literals. */
5279 cp_lexer_consume_token (parser
->lexer
);
5280 return cp_expr (boolean_true_node
, token
->location
);
5282 cp_lexer_consume_token (parser
->lexer
);
5283 return cp_expr (boolean_false_node
, token
->location
);
5285 /* The `__null' literal. */
5287 cp_lexer_consume_token (parser
->lexer
);
5288 return cp_expr (null_node
, token
->location
);
5290 /* The `nullptr' literal. */
5292 cp_lexer_consume_token (parser
->lexer
);
5293 return cp_expr (nullptr_node
, token
->location
);
5295 /* Recognize the `this' keyword. */
5297 cp_lexer_consume_token (parser
->lexer
);
5298 if (parser
->local_variables_forbidden_p
)
5300 error_at (token
->location
,
5301 "%<this%> may not be used in this context");
5302 return error_mark_node
;
5304 /* Pointers cannot appear in constant-expressions. */
5305 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5306 return error_mark_node
;
5307 return cp_expr (finish_this_expr (), token
->location
);
5309 /* The `operator' keyword can be the beginning of an
5314 case RID_FUNCTION_NAME
:
5315 case RID_PRETTY_FUNCTION_NAME
:
5316 case RID_C99_FUNCTION_NAME
:
5318 non_integral_constant name
;
5320 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5321 __func__ are the names of variables -- but they are
5322 treated specially. Therefore, they are handled here,
5323 rather than relying on the generic id-expression logic
5324 below. Grammatically, these names are id-expressions.
5326 Consume the token. */
5327 token
= cp_lexer_consume_token (parser
->lexer
);
5329 switch (token
->keyword
)
5331 case RID_FUNCTION_NAME
:
5332 name
= NIC_FUNC_NAME
;
5334 case RID_PRETTY_FUNCTION_NAME
:
5335 name
= NIC_PRETTY_FUNC
;
5337 case RID_C99_FUNCTION_NAME
:
5338 name
= NIC_C99_FUNC
;
5344 if (cp_parser_non_integral_constant_expression (parser
, name
))
5345 return error_mark_node
;
5347 /* Look up the name. */
5348 return finish_fname (token
->u
.value
);
5355 source_location type_location
;
5356 location_t start_loc
5357 = cp_lexer_peek_token (parser
->lexer
)->location
;
5358 /* The `__builtin_va_arg' construct is used to handle
5359 `va_arg'. Consume the `__builtin_va_arg' token. */
5360 cp_lexer_consume_token (parser
->lexer
);
5361 /* Look for the opening `('. */
5362 matching_parens parens
;
5363 parens
.require_open (parser
);
5364 /* Now, parse the assignment-expression. */
5365 expression
= cp_parser_assignment_expression (parser
);
5366 /* Look for the `,'. */
5367 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5368 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5369 /* Parse the type-id. */
5371 type_id_in_expr_sentinel
s (parser
);
5372 type
= cp_parser_type_id (parser
);
5374 /* Look for the closing `)'. */
5375 location_t finish_loc
5376 = cp_lexer_peek_token (parser
->lexer
)->location
;
5377 parens
.require_close (parser
);
5378 /* Using `va_arg' in a constant-expression is not
5380 if (cp_parser_non_integral_constant_expression (parser
,
5382 return error_mark_node
;
5383 /* Construct a location of the form:
5384 __builtin_va_arg (v, int)
5385 ~~~~~~~~~~~~~~~~~~~~~^~~~
5386 with the caret at the type, ranging from the start of the
5387 "__builtin_va_arg" token to the close paren. */
5388 location_t combined_loc
5389 = make_location (type_location
, start_loc
, finish_loc
);
5390 return build_x_va_arg (combined_loc
, expression
, type
);
5394 return cp_parser_builtin_offsetof (parser
);
5396 case RID_HAS_NOTHROW_ASSIGN
:
5397 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5398 case RID_HAS_NOTHROW_COPY
:
5399 case RID_HAS_TRIVIAL_ASSIGN
:
5400 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5401 case RID_HAS_TRIVIAL_COPY
:
5402 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5403 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5404 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5405 case RID_IS_ABSTRACT
:
5406 case RID_IS_AGGREGATE
:
5407 case RID_IS_BASE_OF
:
5412 case RID_IS_LITERAL_TYPE
:
5414 case RID_IS_POLYMORPHIC
:
5415 case RID_IS_SAME_AS
:
5416 case RID_IS_STD_LAYOUT
:
5417 case RID_IS_TRIVIAL
:
5418 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5419 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5420 case RID_IS_TRIVIALLY_COPYABLE
:
5422 case RID_IS_ASSIGNABLE
:
5423 case RID_IS_CONSTRUCTIBLE
:
5424 return cp_parser_trait_expr (parser
, token
->keyword
);
5428 return cp_parser_requires_expression (parser
);
5430 /* Objective-C++ expressions. */
5432 case RID_AT_PROTOCOL
:
5433 case RID_AT_SELECTOR
:
5434 return cp_parser_objc_expression (parser
);
5437 if (parser
->in_function_body
5438 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5441 error_at (token
->location
,
5442 "a template declaration cannot appear at block scope");
5443 cp_parser_skip_to_end_of_block_or_statement (parser
);
5444 return error_mark_node
;
5448 cp_parser_error (parser
, "expected primary-expression");
5449 return error_mark_node
;
5452 /* An id-expression can start with either an identifier, a
5453 `::' as the beginning of a qualified-id, or the "operator"
5457 case CPP_TEMPLATE_ID
:
5458 case CPP_NESTED_NAME_SPECIFIER
:
5461 cp_expr id_expression
;
5463 const char *error_msg
;
5466 cp_token
*id_expr_token
;
5468 /* Parse the id-expression. */
5470 = cp_parser_id_expression (parser
,
5471 /*template_keyword_p=*/false,
5472 /*check_dependency_p=*/true,
5474 /*declarator_p=*/false,
5475 /*optional_p=*/false);
5476 if (id_expression
== error_mark_node
)
5477 return error_mark_node
;
5478 id_expr_token
= token
;
5479 token
= cp_lexer_peek_token (parser
->lexer
);
5480 done
= (token
->type
!= CPP_OPEN_SQUARE
5481 && token
->type
!= CPP_OPEN_PAREN
5482 && token
->type
!= CPP_DOT
5483 && token
->type
!= CPP_DEREF
5484 && token
->type
!= CPP_PLUS_PLUS
5485 && token
->type
!= CPP_MINUS_MINUS
);
5486 /* If we have a template-id, then no further lookup is
5487 required. If the template-id was for a template-class, we
5488 will sometimes have a TYPE_DECL at this point. */
5489 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5490 || TREE_CODE (id_expression
) == TYPE_DECL
)
5491 decl
= id_expression
;
5492 /* Look up the name. */
5495 tree ambiguous_decls
;
5497 /* If we already know that this lookup is ambiguous, then
5498 we've already issued an error message; there's no reason
5500 if (id_expr_token
->type
== CPP_NAME
5501 && id_expr_token
->error_reported
)
5503 cp_parser_simulate_error (parser
);
5504 return error_mark_node
;
5507 decl
= cp_parser_lookup_name (parser
, id_expression
,
5510 /*is_namespace=*/false,
5511 /*check_dependency=*/true,
5513 id_expr_token
->location
);
5514 /* If the lookup was ambiguous, an error will already have
5516 if (ambiguous_decls
)
5517 return error_mark_node
;
5519 /* In Objective-C++, we may have an Objective-C 2.0
5520 dot-syntax for classes here. */
5521 if (c_dialect_objc ()
5522 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5523 && TREE_CODE (decl
) == TYPE_DECL
5524 && objc_is_class_name (decl
))
5527 cp_lexer_consume_token (parser
->lexer
);
5528 component
= cp_parser_identifier (parser
);
5529 if (component
== error_mark_node
)
5530 return error_mark_node
;
5532 tree result
= objc_build_class_component_ref (id_expression
,
5534 /* Build a location of the form:
5537 with caret at the start of the component name (at
5538 input_location), ranging from the start of the id_expression
5539 to the end of the component name. */
5540 location_t combined_loc
5541 = make_location (input_location
, id_expression
.get_start (),
5542 get_finish (input_location
));
5543 protected_set_expr_location (result
, combined_loc
);
5547 /* In Objective-C++, an instance variable (ivar) may be preferred
5548 to whatever cp_parser_lookup_name() found.
5549 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5550 rest of c-family, we have to do a little extra work to preserve
5551 any location information in cp_expr "decl". Given that
5552 objc_lookup_ivar is implemented in "c-family" and "objc", we
5553 have a trip through the pure "tree" type, rather than cp_expr.
5554 Naively copying it back to "decl" would implicitly give the
5555 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5556 store an EXPR_LOCATION. Hence we only update "decl" (and
5557 hence its location_t) if we get back a different tree node. */
5558 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5560 if (decl_tree
!= decl
.get_value ())
5561 decl
= cp_expr (decl_tree
);
5563 /* If name lookup gives us a SCOPE_REF, then the
5564 qualifying scope was dependent. */
5565 if (TREE_CODE (decl
) == SCOPE_REF
)
5567 /* At this point, we do not know if DECL is a valid
5568 integral constant expression. We assume that it is
5569 in fact such an expression, so that code like:
5571 template <int N> struct A {
5575 is accepted. At template-instantiation time, we
5576 will check that B<N>::i is actually a constant. */
5579 /* Check to see if DECL is a local variable in a context
5580 where that is forbidden. */
5581 if (parser
->local_variables_forbidden_p
5582 && local_variable_p (decl
))
5584 /* It might be that we only found DECL because we are
5585 trying to be generous with pre-ISO scoping rules.
5586 For example, consider:
5590 for (int i = 0; i < 10; ++i) {}
5591 extern void f(int j = i);
5594 Here, name look up will originally find the out
5595 of scope `i'. We need to issue a warning message,
5596 but then use the global `i'. */
5597 decl
= check_for_out_of_scope_variable (decl
);
5598 if (local_variable_p (decl
))
5600 error_at (id_expr_token
->location
,
5601 "local variable %qD may not appear in this context",
5603 return error_mark_node
;
5608 decl
= (finish_id_expression
5609 (id_expression
, decl
, parser
->scope
,
5611 parser
->integral_constant_expression_p
,
5612 parser
->allow_non_integral_constant_expression_p
,
5613 &parser
->non_integral_constant_expression_p
,
5614 template_p
, done
, address_p
,
5617 id_expression
.get_location ()));
5619 cp_parser_error (parser
, error_msg
);
5620 decl
.set_location (id_expr_token
->location
);
5624 /* Anything else is an error. */
5626 cp_parser_error (parser
, "expected primary-expression");
5627 return error_mark_node
;
5631 static inline cp_expr
5632 cp_parser_primary_expression (cp_parser
*parser
,
5635 bool template_arg_p
,
5638 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5639 /*decltype*/false, idk
);
5642 /* Parse an id-expression.
5649 :: [opt] nested-name-specifier template [opt] unqualified-id
5651 :: operator-function-id
5654 Return a representation of the unqualified portion of the
5655 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5656 a `::' or nested-name-specifier.
5658 Often, if the id-expression was a qualified-id, the caller will
5659 want to make a SCOPE_REF to represent the qualified-id. This
5660 function does not do this in order to avoid wastefully creating
5661 SCOPE_REFs when they are not required.
5663 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5666 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5667 uninstantiated templates.
5669 If *TEMPLATE_P is non-NULL, it is set to true iff the
5670 `template' keyword is used to explicitly indicate that the entity
5671 named is a template.
5673 If DECLARATOR_P is true, the id-expression is appearing as part of
5674 a declarator, rather than as part of an expression. */
5677 cp_parser_id_expression (cp_parser
*parser
,
5678 bool template_keyword_p
,
5679 bool check_dependency_p
,
5684 bool global_scope_p
;
5685 bool nested_name_specifier_p
;
5687 /* Assume the `template' keyword was not used. */
5689 *template_p
= template_keyword_p
;
5691 /* Look for the optional `::' operator. */
5693 = (!template_keyword_p
5694 && (cp_parser_global_scope_opt (parser
,
5695 /*current_scope_valid_p=*/false)
5698 /* Look for the optional nested-name-specifier. */
5699 nested_name_specifier_p
5700 = (cp_parser_nested_name_specifier_opt (parser
,
5701 /*typename_keyword_p=*/false,
5708 /* If there is a nested-name-specifier, then we are looking at
5709 the first qualified-id production. */
5710 if (nested_name_specifier_p
)
5713 tree saved_object_scope
;
5714 tree saved_qualifying_scope
;
5715 cp_expr unqualified_id
;
5718 /* See if the next token is the `template' keyword. */
5720 template_p
= &is_template
;
5721 *template_p
= cp_parser_optional_template_keyword (parser
);
5722 /* Name lookup we do during the processing of the
5723 unqualified-id might obliterate SCOPE. */
5724 saved_scope
= parser
->scope
;
5725 saved_object_scope
= parser
->object_scope
;
5726 saved_qualifying_scope
= parser
->qualifying_scope
;
5727 /* Process the final unqualified-id. */
5728 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5731 /*optional_p=*/false);
5732 /* Restore the SAVED_SCOPE for our caller. */
5733 parser
->scope
= saved_scope
;
5734 parser
->object_scope
= saved_object_scope
;
5735 parser
->qualifying_scope
= saved_qualifying_scope
;
5737 return unqualified_id
;
5739 /* Otherwise, if we are in global scope, then we are looking at one
5740 of the other qualified-id productions. */
5741 else if (global_scope_p
)
5746 /* Peek at the next token. */
5747 token
= cp_lexer_peek_token (parser
->lexer
);
5749 /* If it's an identifier, and the next token is not a "<", then
5750 we can avoid the template-id case. This is an optimization
5751 for this common case. */
5752 if (token
->type
== CPP_NAME
5753 && !cp_parser_nth_token_starts_template_argument_list_p
5755 return cp_parser_identifier (parser
);
5757 cp_parser_parse_tentatively (parser
);
5758 /* Try a template-id. */
5759 id
= cp_parser_template_id (parser
,
5760 /*template_keyword_p=*/false,
5761 /*check_dependency_p=*/true,
5764 /* If that worked, we're done. */
5765 if (cp_parser_parse_definitely (parser
))
5768 /* Peek at the next token. (Changes in the token buffer may
5769 have invalidated the pointer obtained above.) */
5770 token
= cp_lexer_peek_token (parser
->lexer
);
5772 switch (token
->type
)
5775 return cp_parser_identifier (parser
);
5778 if (token
->keyword
== RID_OPERATOR
)
5779 return cp_parser_operator_function_id (parser
);
5783 cp_parser_error (parser
, "expected id-expression");
5784 return error_mark_node
;
5788 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5789 /*check_dependency_p=*/true,
5794 /* Parse an unqualified-id.
5798 operator-function-id
5799 conversion-function-id
5803 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5804 keyword, in a construct like `A::template ...'.
5806 Returns a representation of unqualified-id. For the `identifier'
5807 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5808 production a BIT_NOT_EXPR is returned; the operand of the
5809 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5810 other productions, see the documentation accompanying the
5811 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5812 names are looked up in uninstantiated templates. If DECLARATOR_P
5813 is true, the unqualified-id is appearing as part of a declarator,
5814 rather than as part of an expression. */
5817 cp_parser_unqualified_id (cp_parser
* parser
,
5818 bool template_keyword_p
,
5819 bool check_dependency_p
,
5825 /* Peek at the next token. */
5826 token
= cp_lexer_peek_token (parser
->lexer
);
5828 switch ((int) token
->type
)
5834 /* We don't know yet whether or not this will be a
5836 cp_parser_parse_tentatively (parser
);
5837 /* Try a template-id. */
5838 id
= cp_parser_template_id (parser
, template_keyword_p
,
5842 /* If it worked, we're done. */
5843 if (cp_parser_parse_definitely (parser
))
5845 /* Otherwise, it's an ordinary identifier. */
5846 return cp_parser_identifier (parser
);
5849 case CPP_TEMPLATE_ID
:
5850 return cp_parser_template_id (parser
, template_keyword_p
,
5858 tree qualifying_scope
;
5863 /* Consume the `~' token. */
5864 cp_lexer_consume_token (parser
->lexer
);
5865 /* Parse the class-name. The standard, as written, seems to
5868 template <typename T> struct S { ~S (); };
5869 template <typename T> S<T>::~S() {}
5871 is invalid, since `~' must be followed by a class-name, but
5872 `S<T>' is dependent, and so not known to be a class.
5873 That's not right; we need to look in uninstantiated
5874 templates. A further complication arises from:
5876 template <typename T> void f(T t) {
5880 Here, it is not possible to look up `T' in the scope of `T'
5881 itself. We must look in both the current scope, and the
5882 scope of the containing complete expression.
5884 Yet another issue is:
5893 The standard does not seem to say that the `S' in `~S'
5894 should refer to the type `S' and not the data member
5897 /* DR 244 says that we look up the name after the "~" in the
5898 same scope as we looked up the qualifying name. That idea
5899 isn't fully worked out; it's more complicated than that. */
5900 scope
= parser
->scope
;
5901 object_scope
= parser
->object_scope
;
5902 qualifying_scope
= parser
->qualifying_scope
;
5904 /* Check for invalid scopes. */
5905 if (scope
== error_mark_node
)
5907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5908 cp_lexer_consume_token (parser
->lexer
);
5909 return error_mark_node
;
5911 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5913 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5914 error_at (token
->location
,
5915 "scope %qT before %<~%> is not a class-name",
5917 cp_parser_simulate_error (parser
);
5918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5919 cp_lexer_consume_token (parser
->lexer
);
5920 return error_mark_node
;
5922 gcc_assert (!scope
|| TYPE_P (scope
));
5924 /* If the name is of the form "X::~X" it's OK even if X is a
5926 token
= cp_lexer_peek_token (parser
->lexer
);
5928 && token
->type
== CPP_NAME
5929 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5931 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5932 || (CLASS_TYPE_P (scope
)
5933 && constructor_name_p (token
->u
.value
, scope
))))
5935 cp_lexer_consume_token (parser
->lexer
);
5936 return build_nt (BIT_NOT_EXPR
, scope
);
5939 /* ~auto means the destructor of whatever the object is. */
5940 if (cp_parser_is_keyword (token
, RID_AUTO
))
5942 if (cxx_dialect
< cxx14
)
5943 pedwarn (input_location
, 0,
5944 "%<~auto%> only available with "
5945 "-std=c++14 or -std=gnu++14");
5946 cp_lexer_consume_token (parser
->lexer
);
5947 return build_nt (BIT_NOT_EXPR
, make_auto ());
5950 /* If there was an explicit qualification (S::~T), first look
5951 in the scope given by the qualification (i.e., S).
5953 Note: in the calls to cp_parser_class_name below we pass
5954 typename_type so that lookup finds the injected-class-name
5955 rather than the constructor. */
5957 type_decl
= NULL_TREE
;
5960 cp_parser_parse_tentatively (parser
);
5961 type_decl
= cp_parser_class_name (parser
,
5962 /*typename_keyword_p=*/false,
5963 /*template_keyword_p=*/false,
5965 /*check_dependency=*/false,
5966 /*class_head_p=*/false,
5968 if (cp_parser_parse_definitely (parser
))
5971 /* In "N::S::~S", look in "N" as well. */
5972 if (!done
&& scope
&& qualifying_scope
)
5974 cp_parser_parse_tentatively (parser
);
5975 parser
->scope
= qualifying_scope
;
5976 parser
->object_scope
= NULL_TREE
;
5977 parser
->qualifying_scope
= NULL_TREE
;
5979 = cp_parser_class_name (parser
,
5980 /*typename_keyword_p=*/false,
5981 /*template_keyword_p=*/false,
5983 /*check_dependency=*/false,
5984 /*class_head_p=*/false,
5986 if (cp_parser_parse_definitely (parser
))
5989 /* In "p->S::~T", look in the scope given by "*p" as well. */
5990 else if (!done
&& object_scope
)
5992 cp_parser_parse_tentatively (parser
);
5993 parser
->scope
= object_scope
;
5994 parser
->object_scope
= NULL_TREE
;
5995 parser
->qualifying_scope
= NULL_TREE
;
5997 = cp_parser_class_name (parser
,
5998 /*typename_keyword_p=*/false,
5999 /*template_keyword_p=*/false,
6001 /*check_dependency=*/false,
6002 /*class_head_p=*/false,
6004 if (cp_parser_parse_definitely (parser
))
6007 /* Look in the surrounding context. */
6010 parser
->scope
= NULL_TREE
;
6011 parser
->object_scope
= NULL_TREE
;
6012 parser
->qualifying_scope
= NULL_TREE
;
6013 if (processing_template_decl
)
6014 cp_parser_parse_tentatively (parser
);
6016 = cp_parser_class_name (parser
,
6017 /*typename_keyword_p=*/false,
6018 /*template_keyword_p=*/false,
6020 /*check_dependency=*/false,
6021 /*class_head_p=*/false,
6023 if (processing_template_decl
6024 && ! cp_parser_parse_definitely (parser
))
6026 /* We couldn't find a type with this name. If we're parsing
6027 tentatively, fail and try something else. */
6028 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6030 cp_parser_simulate_error (parser
);
6031 return error_mark_node
;
6033 /* Otherwise, accept it and check for a match at instantiation
6035 type_decl
= cp_parser_identifier (parser
);
6036 if (type_decl
!= error_mark_node
)
6037 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6041 /* If an error occurred, assume that the name of the
6042 destructor is the same as the name of the qualifying
6043 class. That allows us to keep parsing after running
6044 into ill-formed destructor names. */
6045 if (type_decl
== error_mark_node
&& scope
)
6046 return build_nt (BIT_NOT_EXPR
, scope
);
6047 else if (type_decl
== error_mark_node
)
6048 return error_mark_node
;
6050 /* Check that destructor name and scope match. */
6051 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6054 error_at (token
->location
,
6055 "declaration of %<~%T%> as member of %qT",
6057 cp_parser_simulate_error (parser
);
6058 return error_mark_node
;
6063 A typedef-name that names a class shall not be used as the
6064 identifier in the declarator for a destructor declaration. */
6066 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6067 && !DECL_SELF_REFERENCE_P (type_decl
)
6068 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6069 error_at (token
->location
,
6070 "typedef-name %qD used as destructor declarator",
6073 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6077 if (token
->keyword
== RID_OPERATOR
)
6081 /* This could be a template-id, so we try that first. */
6082 cp_parser_parse_tentatively (parser
);
6083 /* Try a template-id. */
6084 id
= cp_parser_template_id (parser
, template_keyword_p
,
6085 /*check_dependency_p=*/true,
6088 /* If that worked, we're done. */
6089 if (cp_parser_parse_definitely (parser
))
6091 /* We still don't know whether we're looking at an
6092 operator-function-id or a conversion-function-id. */
6093 cp_parser_parse_tentatively (parser
);
6094 /* Try an operator-function-id. */
6095 id
= cp_parser_operator_function_id (parser
);
6096 /* If that didn't work, try a conversion-function-id. */
6097 if (!cp_parser_parse_definitely (parser
))
6098 id
= cp_parser_conversion_function_id (parser
);
6099 else if (UDLIT_OPER_P (id
))
6102 const char *name
= UDLIT_OP_SUFFIX (id
);
6103 if (name
[0] != '_' && !in_system_header_at (input_location
)
6105 warning (OPT_Wliteral_suffix
,
6106 "literal operator suffixes not preceded by %<_%>"
6107 " are reserved for future standardization");
6117 cp_parser_error (parser
, "expected unqualified-id");
6118 return error_mark_node
;
6122 /* Parse an (optional) nested-name-specifier.
6124 nested-name-specifier: [C++98]
6125 class-or-namespace-name :: nested-name-specifier [opt]
6126 class-or-namespace-name :: template nested-name-specifier [opt]
6128 nested-name-specifier: [C++0x]
6131 nested-name-specifier identifier ::
6132 nested-name-specifier template [opt] simple-template-id ::
6134 PARSER->SCOPE should be set appropriately before this function is
6135 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6136 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6139 Sets PARSER->SCOPE to the class (TYPE) or namespace
6140 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6141 it unchanged if there is no nested-name-specifier. Returns the new
6142 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6144 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6145 part of a declaration and/or decl-specifier. */
6148 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6149 bool typename_keyword_p
,
6150 bool check_dependency_p
,
6152 bool is_declaration
,
6153 bool template_keyword_p
/* = false */)
6155 bool success
= false;
6156 cp_token_position start
= 0;
6159 /* Remember where the nested-name-specifier starts. */
6160 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6162 start
= cp_lexer_token_position (parser
->lexer
, false);
6163 push_deferring_access_checks (dk_deferred
);
6170 tree saved_qualifying_scope
;
6172 /* Spot cases that cannot be the beginning of a
6173 nested-name-specifier. */
6174 token
= cp_lexer_peek_token (parser
->lexer
);
6176 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6177 the already parsed nested-name-specifier. */
6178 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6180 /* Grab the nested-name-specifier and continue the loop. */
6181 cp_parser_pre_parsed_nested_name_specifier (parser
);
6182 /* If we originally encountered this nested-name-specifier
6183 with IS_DECLARATION set to false, we will not have
6184 resolved TYPENAME_TYPEs, so we must do so here. */
6186 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6188 new_scope
= resolve_typename_type (parser
->scope
,
6189 /*only_current_p=*/false);
6190 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6191 parser
->scope
= new_scope
;
6197 /* Spot cases that cannot be the beginning of a
6198 nested-name-specifier. On the second and subsequent times
6199 through the loop, we look for the `template' keyword. */
6200 if (success
&& token
->keyword
== RID_TEMPLATE
)
6202 /* A template-id can start a nested-name-specifier. */
6203 else if (token
->type
== CPP_TEMPLATE_ID
)
6205 /* DR 743: decltype can be used in a nested-name-specifier. */
6206 else if (token_is_decltype (token
))
6210 /* If the next token is not an identifier, then it is
6211 definitely not a type-name or namespace-name. */
6212 if (token
->type
!= CPP_NAME
)
6214 /* If the following token is neither a `<' (to begin a
6215 template-id), nor a `::', then we are not looking at a
6216 nested-name-specifier. */
6217 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6219 if (token
->type
== CPP_COLON
6220 && parser
->colon_corrects_to_scope_p
6221 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6223 gcc_rich_location
richloc (token
->location
);
6224 richloc
.add_fixit_replace ("::");
6226 "found %<:%> in nested-name-specifier, "
6228 token
->type
= CPP_SCOPE
;
6231 if (token
->type
!= CPP_SCOPE
6232 && !cp_parser_nth_token_starts_template_argument_list_p
6237 /* The nested-name-specifier is optional, so we parse
6239 cp_parser_parse_tentatively (parser
);
6241 /* Look for the optional `template' keyword, if this isn't the
6242 first time through the loop. */
6244 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6246 /* Save the old scope since the name lookup we are about to do
6247 might destroy it. */
6248 old_scope
= parser
->scope
;
6249 saved_qualifying_scope
= parser
->qualifying_scope
;
6250 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6251 look up names in "X<T>::I" in order to determine that "Y" is
6252 a template. So, if we have a typename at this point, we make
6253 an effort to look through it. */
6255 && !typename_keyword_p
6257 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6258 parser
->scope
= resolve_typename_type (parser
->scope
,
6259 /*only_current_p=*/false);
6260 /* Parse the qualifying entity. */
6262 = cp_parser_qualifying_entity (parser
,
6268 /* Look for the `::' token. */
6269 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6271 /* If we found what we wanted, we keep going; otherwise, we're
6273 if (!cp_parser_parse_definitely (parser
))
6275 bool error_p
= false;
6277 /* Restore the OLD_SCOPE since it was valid before the
6278 failed attempt at finding the last
6279 class-or-namespace-name. */
6280 parser
->scope
= old_scope
;
6281 parser
->qualifying_scope
= saved_qualifying_scope
;
6283 /* If the next token is a decltype, and the one after that is a
6284 `::', then the decltype has failed to resolve to a class or
6285 enumeration type. Give this error even when parsing
6286 tentatively since it can't possibly be valid--and we're going
6287 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6288 won't get another chance.*/
6289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6290 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6293 token
= cp_lexer_consume_token (parser
->lexer
);
6294 error_at (token
->location
, "decltype evaluates to %qT, "
6295 "which is not a class or enumeration type",
6296 token
->u
.tree_check_value
->value
);
6297 parser
->scope
= error_mark_node
;
6301 cp_lexer_consume_token (parser
->lexer
);
6304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6305 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6307 /* If we have a non-type template-id followed by ::, it can't
6308 possibly be valid. */
6309 token
= cp_lexer_peek_token (parser
->lexer
);
6310 tree tid
= token
->u
.tree_check_value
->value
;
6311 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6312 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6314 tree tmpl
= NULL_TREE
;
6315 if (is_overloaded_fn (tid
))
6317 tree fns
= get_fns (tid
);
6318 if (OVL_SINGLE_P (fns
))
6319 tmpl
= OVL_FIRST (fns
);
6320 error_at (token
->location
, "function template-id %qD "
6321 "in nested-name-specifier", tid
);
6325 /* Variable template. */
6326 tmpl
= TREE_OPERAND (tid
, 0);
6327 gcc_assert (variable_template_p (tmpl
));
6328 error_at (token
->location
, "variable template-id %qD "
6329 "in nested-name-specifier", tid
);
6332 inform (DECL_SOURCE_LOCATION (tmpl
),
6333 "%qD declared here", tmpl
);
6335 parser
->scope
= error_mark_node
;
6339 cp_lexer_consume_token (parser
->lexer
);
6340 cp_lexer_consume_token (parser
->lexer
);
6344 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6346 /* If the next token is an identifier, and the one after
6347 that is a `::', then any valid interpretation would have
6348 found a class-or-namespace-name. */
6349 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6350 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6352 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6355 token
= cp_lexer_consume_token (parser
->lexer
);
6358 if (!token
->error_reported
)
6361 tree ambiguous_decls
;
6363 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6365 /*is_template=*/false,
6366 /*is_namespace=*/false,
6367 /*check_dependency=*/true,
6370 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6371 error_at (token
->location
,
6372 "%qD used without template parameters",
6374 else if (ambiguous_decls
)
6376 // cp_parser_lookup_name has the same diagnostic,
6377 // thus make sure to emit it at most once.
6378 if (cp_parser_uncommitted_to_tentative_parse_p
6381 error_at (token
->location
,
6382 "reference to %qD is ambiguous",
6384 print_candidates (ambiguous_decls
);
6386 decl
= error_mark_node
;
6390 if (cxx_dialect
!= cxx98
)
6391 cp_parser_name_lookup_error
6392 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6395 cp_parser_name_lookup_error
6396 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6400 parser
->scope
= error_mark_node
;
6402 /* Treat this as a successful nested-name-specifier
6407 If the name found is not a class-name (clause
6408 _class_) or namespace-name (_namespace.def_), the
6409 program is ill-formed. */
6412 cp_lexer_consume_token (parser
->lexer
);
6416 /* We've found one valid nested-name-specifier. */
6418 /* Name lookup always gives us a DECL. */
6419 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6420 new_scope
= TREE_TYPE (new_scope
);
6421 /* Uses of "template" must be followed by actual templates. */
6422 if (template_keyword_p
6423 && !(CLASS_TYPE_P (new_scope
)
6424 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6425 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6426 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6427 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6428 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6429 == TEMPLATE_ID_EXPR
)))
6430 permerror (input_location
, TYPE_P (new_scope
)
6431 ? G_("%qT is not a template")
6432 : G_("%qD is not a template"),
6434 /* If it is a class scope, try to complete it; we are about to
6435 be looking up names inside the class. */
6436 if (TYPE_P (new_scope
)
6437 /* Since checking types for dependency can be expensive,
6438 avoid doing it if the type is already complete. */
6439 && !COMPLETE_TYPE_P (new_scope
)
6440 /* Do not try to complete dependent types. */
6441 && !dependent_type_p (new_scope
))
6443 new_scope
= complete_type (new_scope
);
6444 /* If it is a typedef to current class, use the current
6445 class instead, as the typedef won't have any names inside
6447 if (!COMPLETE_TYPE_P (new_scope
)
6448 && currently_open_class (new_scope
))
6449 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6451 /* Make sure we look in the right scope the next time through
6453 parser
->scope
= new_scope
;
6456 /* If parsing tentatively, replace the sequence of tokens that makes
6457 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6458 token. That way, should we re-parse the token stream, we will
6459 not have to repeat the effort required to do the parse, nor will
6460 we issue duplicate error messages. */
6461 if (success
&& start
)
6465 token
= cp_lexer_token_at (parser
->lexer
, start
);
6466 /* Reset the contents of the START token. */
6467 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6468 /* Retrieve any deferred checks. Do not pop this access checks yet
6469 so the memory will not be reclaimed during token replacing below. */
6470 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6471 token
->u
.tree_check_value
->value
= parser
->scope
;
6472 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6473 token
->u
.tree_check_value
->qualifying_scope
=
6474 parser
->qualifying_scope
;
6475 token
->keyword
= RID_MAX
;
6477 /* Purge all subsequent tokens. */
6478 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6482 pop_to_parent_deferring_access_checks ();
6484 return success
? parser
->scope
: NULL_TREE
;
6487 /* Parse a nested-name-specifier. See
6488 cp_parser_nested_name_specifier_opt for details. This function
6489 behaves identically, except that it will an issue an error if no
6490 nested-name-specifier is present. */
6493 cp_parser_nested_name_specifier (cp_parser
*parser
,
6494 bool typename_keyword_p
,
6495 bool check_dependency_p
,
6497 bool is_declaration
)
6501 /* Look for the nested-name-specifier. */
6502 scope
= cp_parser_nested_name_specifier_opt (parser
,
6507 /* If it was not present, issue an error message. */
6510 cp_parser_error (parser
, "expected nested-name-specifier");
6511 parser
->scope
= NULL_TREE
;
6517 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6518 this is either a class-name or a namespace-name (which corresponds
6519 to the class-or-namespace-name production in the grammar). For
6520 C++0x, it can also be a type-name that refers to an enumeration
6521 type or a simple-template-id.
6523 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6524 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6525 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6526 TYPE_P is TRUE iff the next name should be taken as a class-name,
6527 even the same name is declared to be another entity in the same
6530 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6531 specified by the class-or-namespace-name. If neither is found the
6532 ERROR_MARK_NODE is returned. */
6535 cp_parser_qualifying_entity (cp_parser
*parser
,
6536 bool typename_keyword_p
,
6537 bool template_keyword_p
,
6538 bool check_dependency_p
,
6540 bool is_declaration
)
6543 tree saved_qualifying_scope
;
6544 tree saved_object_scope
;
6547 bool successful_parse_p
;
6549 /* DR 743: decltype can appear in a nested-name-specifier. */
6550 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6552 scope
= cp_parser_decltype (parser
);
6553 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6554 && !MAYBE_CLASS_TYPE_P (scope
))
6556 cp_parser_simulate_error (parser
);
6557 return error_mark_node
;
6559 if (TYPE_NAME (scope
))
6560 scope
= TYPE_NAME (scope
);
6564 /* Before we try to parse the class-name, we must save away the
6565 current PARSER->SCOPE since cp_parser_class_name will destroy
6567 saved_scope
= parser
->scope
;
6568 saved_qualifying_scope
= parser
->qualifying_scope
;
6569 saved_object_scope
= parser
->object_scope
;
6570 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6571 there is no need to look for a namespace-name. */
6572 only_class_p
= template_keyword_p
6573 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6575 cp_parser_parse_tentatively (parser
);
6576 scope
= cp_parser_class_name (parser
,
6579 type_p
? class_type
: none_type
,
6581 /*class_head_p=*/false,
6583 /*enum_ok=*/cxx_dialect
> cxx98
);
6584 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6585 /* If that didn't work, try for a namespace-name. */
6586 if (!only_class_p
&& !successful_parse_p
)
6588 /* Restore the saved scope. */
6589 parser
->scope
= saved_scope
;
6590 parser
->qualifying_scope
= saved_qualifying_scope
;
6591 parser
->object_scope
= saved_object_scope
;
6592 /* If we are not looking at an identifier followed by the scope
6593 resolution operator, then this is not part of a
6594 nested-name-specifier. (Note that this function is only used
6595 to parse the components of a nested-name-specifier.) */
6596 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6597 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6598 return error_mark_node
;
6599 scope
= cp_parser_namespace_name (parser
);
6605 /* Return true if we are looking at a compound-literal, false otherwise. */
6608 cp_parser_compound_literal_p (cp_parser
*parser
)
6610 cp_lexer_save_tokens (parser
->lexer
);
6612 /* Skip tokens until the next token is a closing parenthesis.
6613 If we find the closing `)', and the next token is a `{', then
6614 we are looking at a compound-literal. */
6615 bool compound_literal_p
6616 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6617 /*consume_paren=*/true)
6618 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6620 /* Roll back the tokens we skipped. */
6621 cp_lexer_rollback_tokens (parser
->lexer
);
6623 return compound_literal_p
;
6626 /* Return true if EXPR is the integer constant zero or a complex constant
6627 of zero, without any folding, but ignoring location wrappers. */
6630 literal_integer_zerop (const_tree expr
)
6632 STRIP_ANY_LOCATION_WRAPPER (expr
);
6633 return integer_zerop (expr
);
6636 /* Parse a postfix-expression.
6640 postfix-expression [ expression ]
6641 postfix-expression ( expression-list [opt] )
6642 simple-type-specifier ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier identifier
6644 ( expression-list [opt] )
6645 typename :: [opt] nested-name-specifier template [opt] template-id
6646 ( expression-list [opt] )
6647 postfix-expression . template [opt] id-expression
6648 postfix-expression -> template [opt] id-expression
6649 postfix-expression . pseudo-destructor-name
6650 postfix-expression -> pseudo-destructor-name
6651 postfix-expression ++
6652 postfix-expression --
6653 dynamic_cast < type-id > ( expression )
6654 static_cast < type-id > ( expression )
6655 reinterpret_cast < type-id > ( expression )
6656 const_cast < type-id > ( expression )
6657 typeid ( expression )
6663 ( type-id ) { initializer-list , [opt] }
6665 This extension is a GNU version of the C99 compound-literal
6666 construct. (The C99 grammar uses `type-name' instead of `type-id',
6667 but they are essentially the same concept.)
6669 If ADDRESS_P is true, the postfix expression is the operand of the
6670 `&' operator. CAST_P is true if this expression is the target of a
6673 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6674 class member access expressions [expr.ref].
6676 Returns a representation of the expression. */
6679 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6680 bool member_access_only_p
, bool decltype_p
,
6681 cp_id_kind
* pidk_return
)
6686 cp_id_kind idk
= CP_ID_KIND_NONE
;
6687 cp_expr postfix_expression
= NULL_TREE
;
6688 bool is_member_access
= false;
6690 /* Peek at the next token. */
6691 token
= cp_lexer_peek_token (parser
->lexer
);
6692 loc
= token
->location
;
6693 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6695 /* Some of the productions are determined by keywords. */
6696 keyword
= token
->keyword
;
6706 const char *saved_message
;
6707 bool saved_in_type_id_in_expr_p
;
6709 /* All of these can be handled in the same way from the point
6710 of view of parsing. Begin by consuming the token
6711 identifying the cast. */
6712 cp_lexer_consume_token (parser
->lexer
);
6714 /* New types cannot be defined in the cast. */
6715 saved_message
= parser
->type_definition_forbidden_message
;
6716 parser
->type_definition_forbidden_message
6717 = G_("types may not be defined in casts");
6719 /* Look for the opening `<'. */
6720 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6721 /* Parse the type to which we are casting. */
6722 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6723 parser
->in_type_id_in_expr_p
= true;
6724 type
= cp_parser_type_id (parser
);
6725 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6726 /* Look for the closing `>'. */
6727 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6728 /* Restore the old message. */
6729 parser
->type_definition_forbidden_message
= saved_message
;
6731 bool saved_greater_than_is_operator_p
6732 = parser
->greater_than_is_operator_p
;
6733 parser
->greater_than_is_operator_p
= true;
6735 /* And the expression which is being cast. */
6736 matching_parens parens
;
6737 parens
.require_open (parser
);
6738 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6739 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6741 location_t end_loc
= close_paren
?
6742 close_paren
->location
: UNKNOWN_LOCATION
;
6744 parser
->greater_than_is_operator_p
6745 = saved_greater_than_is_operator_p
;
6747 /* Only type conversions to integral or enumeration types
6748 can be used in constant-expressions. */
6749 if (!cast_valid_in_integral_constant_expression_p (type
)
6750 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6752 postfix_expression
= error_mark_node
;
6760 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6764 = build_static_cast (type
, expression
, tf_warning_or_error
);
6768 = build_reinterpret_cast (type
, expression
,
6769 tf_warning_or_error
);
6773 = build_const_cast (type
, expression
, tf_warning_or_error
);
6779 /* Construct a location e.g. :
6780 reinterpret_cast <int *> (expr)
6781 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6782 ranging from the start of the "*_cast" token to the final closing
6783 paren, with the caret at the start. */
6784 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6785 postfix_expression
.set_location (cp_cast_loc
);
6792 const char *saved_message
;
6793 bool saved_in_type_id_in_expr_p
;
6795 /* Consume the `typeid' token. */
6796 cp_lexer_consume_token (parser
->lexer
);
6797 /* Look for the `(' token. */
6798 matching_parens parens
;
6799 parens
.require_open (parser
);
6800 /* Types cannot be defined in a `typeid' expression. */
6801 saved_message
= parser
->type_definition_forbidden_message
;
6802 parser
->type_definition_forbidden_message
6803 = G_("types may not be defined in a %<typeid%> expression");
6804 /* We can't be sure yet whether we're looking at a type-id or an
6806 cp_parser_parse_tentatively (parser
);
6807 /* Try a type-id first. */
6808 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6809 parser
->in_type_id_in_expr_p
= true;
6810 type
= cp_parser_type_id (parser
);
6811 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6812 /* Look for the `)' token. Otherwise, we can't be sure that
6813 we're not looking at an expression: consider `typeid (int
6814 (3))', for example. */
6815 cp_token
*close_paren
= parens
.require_close (parser
);
6816 /* If all went well, simply lookup the type-id. */
6817 if (cp_parser_parse_definitely (parser
))
6818 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6819 /* Otherwise, fall back to the expression variant. */
6824 /* Look for an expression. */
6825 expression
= cp_parser_expression (parser
, & idk
);
6826 /* Compute its typeid. */
6827 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6828 /* Look for the `)' token. */
6829 close_paren
= parens
.require_close (parser
);
6831 /* Restore the saved message. */
6832 parser
->type_definition_forbidden_message
= saved_message
;
6833 /* `typeid' may not appear in an integral constant expression. */
6834 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6835 postfix_expression
= error_mark_node
;
6837 /* Construct a location e.g. :
6840 ranging from the start of the "typeid" token to the final closing
6841 paren, with the caret at the start. */
6844 location_t typeid_loc
6845 = make_location (start_loc
, start_loc
, close_paren
->location
);
6846 postfix_expression
.set_location (typeid_loc
);
6847 postfix_expression
.maybe_add_location_wrapper ();
6855 /* The syntax permitted here is the same permitted for an
6856 elaborated-type-specifier. */
6857 ++parser
->prevent_constrained_type_specifiers
;
6858 type
= cp_parser_elaborated_type_specifier (parser
,
6859 /*is_friend=*/false,
6860 /*is_declaration=*/false);
6861 --parser
->prevent_constrained_type_specifiers
;
6862 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6867 case RID_BUILTIN_SHUFFLE
:
6868 case RID_BUILTIN_LAUNDER
:
6870 vec
<tree
, va_gc
> *vec
;
6874 cp_lexer_consume_token (parser
->lexer
);
6875 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6876 /*cast_p=*/false, /*allow_expansion_p=*/true,
6877 /*non_constant_p=*/NULL
);
6880 postfix_expression
= error_mark_node
;
6884 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6890 if (vec
->length () == 1)
6892 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6895 error_at (loc
, "wrong number of arguments to "
6896 "%<__builtin_addressof%>");
6897 postfix_expression
= error_mark_node
;
6901 case RID_BUILTIN_LAUNDER
:
6902 if (vec
->length () == 1)
6903 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6904 tf_warning_or_error
);
6907 error_at (loc
, "wrong number of arguments to "
6908 "%<__builtin_launder%>");
6909 postfix_expression
= error_mark_node
;
6913 case RID_BUILTIN_SHUFFLE
:
6914 if (vec
->length () == 2)
6916 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6917 (*vec
)[1], tf_warning_or_error
);
6918 else if (vec
->length () == 3)
6920 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6921 (*vec
)[2], tf_warning_or_error
);
6924 error_at (loc
, "wrong number of arguments to "
6925 "%<__builtin_shuffle%>");
6926 postfix_expression
= error_mark_node
;
6940 /* If the next thing is a simple-type-specifier, we may be
6941 looking at a functional cast. We could also be looking at
6942 an id-expression. So, we try the functional cast, and if
6943 that doesn't work we fall back to the primary-expression. */
6944 cp_parser_parse_tentatively (parser
);
6945 /* Look for the simple-type-specifier. */
6946 ++parser
->prevent_constrained_type_specifiers
;
6947 type
= cp_parser_simple_type_specifier (parser
,
6948 /*decl_specs=*/NULL
,
6949 CP_PARSER_FLAGS_NONE
);
6950 --parser
->prevent_constrained_type_specifiers
;
6951 /* Parse the cast itself. */
6952 if (!cp_parser_error_occurred (parser
))
6954 = cp_parser_functional_cast (parser
, type
);
6955 /* If that worked, we're done. */
6956 if (cp_parser_parse_definitely (parser
))
6959 /* If the functional-cast didn't work out, try a
6960 compound-literal. */
6961 if (cp_parser_allow_gnu_extensions_p (parser
)
6962 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6964 cp_expr initializer
= NULL_TREE
;
6966 cp_parser_parse_tentatively (parser
);
6968 matching_parens parens
;
6969 parens
.consume_open (parser
);
6971 /* Avoid calling cp_parser_type_id pointlessly, see comment
6972 in cp_parser_cast_expression about c++/29234. */
6973 if (!cp_parser_compound_literal_p (parser
))
6974 cp_parser_simulate_error (parser
);
6977 /* Parse the type. */
6978 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6979 parser
->in_type_id_in_expr_p
= true;
6980 type
= cp_parser_type_id (parser
);
6981 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6982 parens
.require_close (parser
);
6985 /* If things aren't going well, there's no need to
6987 if (!cp_parser_error_occurred (parser
))
6989 bool non_constant_p
;
6990 /* Parse the brace-enclosed initializer list. */
6991 initializer
= cp_parser_braced_list (parser
,
6994 /* If that worked, we're definitely looking at a
6995 compound-literal expression. */
6996 if (cp_parser_parse_definitely (parser
))
6998 /* Warn the user that a compound literal is not
6999 allowed in standard C++. */
7000 pedwarn (input_location
, OPT_Wpedantic
,
7001 "ISO C++ forbids compound-literals");
7002 /* For simplicity, we disallow compound literals in
7003 constant-expressions. We could
7004 allow compound literals of integer type, whose
7005 initializer was a constant, in constant
7006 expressions. Permitting that usage, as a further
7007 extension, would not change the meaning of any
7008 currently accepted programs. (Of course, as
7009 compound literals are not part of ISO C++, the
7010 standard has nothing to say.) */
7011 if (cp_parser_non_integral_constant_expression (parser
,
7014 postfix_expression
= error_mark_node
;
7017 /* Form the representation of the compound-literal. */
7019 = finish_compound_literal (type
, initializer
,
7020 tf_warning_or_error
, fcl_c99
);
7021 postfix_expression
.set_location (initializer
.get_location ());
7026 /* It must be a primary-expression. */
7028 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7029 /*template_arg_p=*/false,
7036 /* Note that we don't need to worry about calling build_cplus_new on a
7037 class-valued CALL_EXPR in decltype when it isn't the end of the
7038 postfix-expression; unary_complex_lvalue will take care of that for
7041 /* Keep looping until the postfix-expression is complete. */
7044 if (idk
== CP_ID_KIND_UNQUALIFIED
7045 && identifier_p (postfix_expression
)
7046 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7047 /* It is not a Koenig lookup function call. */
7049 = unqualified_name_lookup_error (postfix_expression
);
7051 /* Peek at the next token. */
7052 token
= cp_lexer_peek_token (parser
->lexer
);
7054 switch (token
->type
)
7056 case CPP_OPEN_SQUARE
:
7057 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7059 cp_parser_error (parser
,
7060 "two consecutive %<[%> shall "
7061 "only introduce an attribute");
7062 return error_mark_node
;
7065 = cp_parser_postfix_open_square_expression (parser
,
7069 postfix_expression
.set_range (start_loc
,
7070 postfix_expression
.get_location ());
7072 idk
= CP_ID_KIND_NONE
;
7073 is_member_access
= false;
7076 case CPP_OPEN_PAREN
:
7077 /* postfix-expression ( expression-list [opt] ) */
7080 bool is_builtin_constant_p
;
7081 bool saved_integral_constant_expression_p
= false;
7082 bool saved_non_integral_constant_expression_p
= false;
7083 tsubst_flags_t complain
= complain_flags (decltype_p
);
7084 vec
<tree
, va_gc
> *args
;
7085 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7087 is_member_access
= false;
7089 is_builtin_constant_p
7090 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7091 if (is_builtin_constant_p
)
7093 /* The whole point of __builtin_constant_p is to allow
7094 non-constant expressions to appear as arguments. */
7095 saved_integral_constant_expression_p
7096 = parser
->integral_constant_expression_p
;
7097 saved_non_integral_constant_expression_p
7098 = parser
->non_integral_constant_expression_p
;
7099 parser
->integral_constant_expression_p
= false;
7101 args
= (cp_parser_parenthesized_expression_list
7103 /*cast_p=*/false, /*allow_expansion_p=*/true,
7104 /*non_constant_p=*/NULL
,
7105 /*close_paren_loc=*/&close_paren_loc
,
7106 /*wrap_locations_p=*/true));
7107 if (is_builtin_constant_p
)
7109 parser
->integral_constant_expression_p
7110 = saved_integral_constant_expression_p
;
7111 parser
->non_integral_constant_expression_p
7112 = saved_non_integral_constant_expression_p
;
7117 postfix_expression
= error_mark_node
;
7121 /* Function calls are not permitted in
7122 constant-expressions. */
7123 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7124 && cp_parser_non_integral_constant_expression (parser
,
7127 postfix_expression
= error_mark_node
;
7128 release_tree_vector (args
);
7133 if (idk
== CP_ID_KIND_UNQUALIFIED
7134 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7136 if (identifier_p (postfix_expression
))
7138 if (!args
->is_empty ())
7141 if (!any_type_dependent_arguments_p (args
))
7143 = perform_koenig_lookup (postfix_expression
, args
,
7148 = unqualified_fn_lookup_error (postfix_expression
);
7150 /* We do not perform argument-dependent lookup if
7151 normal lookup finds a non-function, in accordance
7152 with the expected resolution of DR 218. */
7153 else if (!args
->is_empty ()
7154 && is_overloaded_fn (postfix_expression
))
7156 tree fn
= get_first_fn (postfix_expression
);
7157 fn
= STRIP_TEMPLATE (fn
);
7159 /* Do not do argument dependent lookup if regular
7160 lookup finds a member function or a block-scope
7161 function declaration. [basic.lookup.argdep]/3 */
7162 if (!DECL_FUNCTION_MEMBER_P (fn
)
7163 && !DECL_LOCAL_FUNCTION_P (fn
))
7166 if (!any_type_dependent_arguments_p (args
))
7168 = perform_koenig_lookup (postfix_expression
, args
,
7174 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7175 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7176 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7177 && vec_safe_length (args
) == 3)
7179 tree arg0
= (*args
)[0];
7180 tree arg1
= (*args
)[1];
7181 tree arg2
= (*args
)[2];
7182 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7183 | (literal_integer_zerop (arg2
) << 2));
7184 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7187 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7189 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7190 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7192 if (processing_template_decl
7193 && (type_dependent_object_expression_p (instance
)
7194 || (!BASELINK_P (fn
)
7195 && TREE_CODE (fn
) != FIELD_DECL
)
7196 || type_dependent_expression_p (fn
)
7197 || any_type_dependent_arguments_p (args
)))
7199 maybe_generic_this_capture (instance
, fn
);
7201 = build_min_nt_call_vec (postfix_expression
, args
);
7202 release_tree_vector (args
);
7206 if (BASELINK_P (fn
))
7209 = (build_new_method_call
7210 (instance
, fn
, &args
, NULL_TREE
,
7211 (idk
== CP_ID_KIND_QUALIFIED
7212 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7219 = finish_call_expr (postfix_expression
, &args
,
7220 /*disallow_virtual=*/false,
7224 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7225 || TREE_CODE (postfix_expression
) == MEMBER_REF
7226 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7227 postfix_expression
= (build_offset_ref_call_from_tree
7228 (postfix_expression
, &args
,
7230 else if (idk
== CP_ID_KIND_QUALIFIED
)
7231 /* A call to a static class member, or a namespace-scope
7234 = finish_call_expr (postfix_expression
, &args
,
7235 /*disallow_virtual=*/true,
7239 /* All other function calls. */
7241 = finish_call_expr (postfix_expression
, &args
,
7242 /*disallow_virtual=*/false,
7246 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7248 location_t combined_loc
= make_location (token
->location
,
7251 postfix_expression
.set_location (combined_loc
);
7254 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7255 idk
= CP_ID_KIND_NONE
;
7257 release_tree_vector (args
);
7263 /* postfix-expression . template [opt] id-expression
7264 postfix-expression . pseudo-destructor-name
7265 postfix-expression -> template [opt] id-expression
7266 postfix-expression -> pseudo-destructor-name */
7268 /* Consume the `.' or `->' operator. */
7269 cp_lexer_consume_token (parser
->lexer
);
7272 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7276 is_member_access
= true;
7280 /* postfix-expression ++ */
7281 /* Consume the `++' token. */
7282 cp_lexer_consume_token (parser
->lexer
);
7283 /* Generate a representation for the complete expression. */
7285 = finish_increment_expr (postfix_expression
,
7286 POSTINCREMENT_EXPR
);
7287 /* Increments may not appear in constant-expressions. */
7288 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7289 postfix_expression
= error_mark_node
;
7290 idk
= CP_ID_KIND_NONE
;
7291 is_member_access
= false;
7294 case CPP_MINUS_MINUS
:
7295 /* postfix-expression -- */
7296 /* Consume the `--' token. */
7297 cp_lexer_consume_token (parser
->lexer
);
7298 /* Generate a representation for the complete expression. */
7300 = finish_increment_expr (postfix_expression
,
7301 POSTDECREMENT_EXPR
);
7302 /* Decrements may not appear in constant-expressions. */
7303 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7304 postfix_expression
= error_mark_node
;
7305 idk
= CP_ID_KIND_NONE
;
7306 is_member_access
= false;
7310 if (pidk_return
!= NULL
)
7311 * pidk_return
= idk
;
7312 if (member_access_only_p
)
7313 return is_member_access
7314 ? postfix_expression
7315 : cp_expr (error_mark_node
);
7317 return postfix_expression
;
7321 /* We should never get here. */
7323 return error_mark_node
;
7326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7327 by cp_parser_builtin_offsetof. We're looking for
7329 postfix-expression [ expression ]
7330 postfix-expression [ braced-init-list ] (C++11)
7332 FOR_OFFSETOF is set if we're being called in that context, which
7333 changes how we deal with integer constant expressions. */
7336 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7337 tree postfix_expression
,
7341 tree index
= NULL_TREE
;
7342 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7343 bool saved_greater_than_is_operator_p
;
7345 /* Consume the `[' token. */
7346 cp_lexer_consume_token (parser
->lexer
);
7348 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7349 parser
->greater_than_is_operator_p
= true;
7351 /* Parse the index expression. */
7352 /* ??? For offsetof, there is a question of what to allow here. If
7353 offsetof is not being used in an integral constant expression context,
7354 then we *could* get the right answer by computing the value at runtime.
7355 If we are in an integral constant expression context, then we might
7356 could accept any constant expression; hard to say without analysis.
7357 Rather than open the barn door too wide right away, allow only integer
7358 constant expressions here. */
7360 index
= cp_parser_constant_expression (parser
);
7363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7365 bool expr_nonconst_p
;
7366 cp_lexer_set_source_position (parser
->lexer
);
7367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7368 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7371 index
= cp_parser_expression (parser
);
7374 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7376 /* Look for the closing `]'. */
7377 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7379 /* Build the ARRAY_REF. */
7380 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7383 /* When not doing offsetof, array references are not permitted in
7384 constant-expressions. */
7386 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7387 postfix_expression
= error_mark_node
;
7389 return postfix_expression
;
7392 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7393 dereference of incomplete type, returns true if error_mark_node should
7394 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7395 and *DEPENDENT_P. */
7398 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7401 /* In a template, be permissive by treating an object expression
7402 of incomplete type as dependent (after a pedwarn). */
7403 diagnostic_t kind
= (processing_template_decl
7404 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7406 switch (TREE_CODE (*postfix_expression
))
7409 case REINTERPRET_CAST_EXPR
:
7410 case CONST_CAST_EXPR
:
7411 case STATIC_CAST_EXPR
:
7412 case DYNAMIC_CAST_EXPR
:
7413 case IMPLICIT_CONV_EXPR
:
7414 case VIEW_CONVERT_EXPR
:
7415 case NON_LVALUE_EXPR
:
7419 /* Don't emit any diagnostic for OVERLOADs. */
7423 /* Avoid clobbering e.g. DECLs. */
7424 if (!EXPR_P (*postfix_expression
))
7429 if (kind
== DK_IGNORED
)
7432 location_t exploc
= location_of (*postfix_expression
);
7433 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7434 if (!MAYBE_CLASS_TYPE_P (*scope
))
7436 if (kind
== DK_ERROR
)
7437 *scope
= *postfix_expression
= error_mark_node
;
7438 else if (processing_template_decl
)
7440 *dependent_p
= true;
7441 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7446 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7447 by cp_parser_builtin_offsetof. We're looking for
7449 postfix-expression . template [opt] id-expression
7450 postfix-expression . pseudo-destructor-name
7451 postfix-expression -> template [opt] id-expression
7452 postfix-expression -> pseudo-destructor-name
7454 FOR_OFFSETOF is set if we're being called in that context. That sorta
7455 limits what of the above we'll actually accept, but nevermind.
7456 TOKEN_TYPE is the "." or "->" token, which will already have been
7457 removed from the stream. */
7460 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7461 enum cpp_ttype token_type
,
7462 cp_expr postfix_expression
,
7463 bool for_offsetof
, cp_id_kind
*idk
,
7464 location_t location
)
7468 bool pseudo_destructor_p
;
7469 tree scope
= NULL_TREE
;
7470 location_t start_loc
= postfix_expression
.get_start ();
7472 /* If this is a `->' operator, dereference the pointer. */
7473 if (token_type
== CPP_DEREF
)
7474 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7475 tf_warning_or_error
);
7476 /* Check to see whether or not the expression is type-dependent and
7477 not the current instantiation. */
7478 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7479 /* The identifier following the `->' or `.' is not qualified. */
7480 parser
->scope
= NULL_TREE
;
7481 parser
->qualifying_scope
= NULL_TREE
;
7482 parser
->object_scope
= NULL_TREE
;
7483 *idk
= CP_ID_KIND_NONE
;
7485 /* Enter the scope corresponding to the type of the object
7486 given by the POSTFIX_EXPRESSION. */
7489 scope
= TREE_TYPE (postfix_expression
);
7490 /* According to the standard, no expression should ever have
7491 reference type. Unfortunately, we do not currently match
7492 the standard in this respect in that our internal representation
7493 of an expression may have reference type even when the standard
7494 says it does not. Therefore, we have to manually obtain the
7495 underlying type here. */
7496 scope
= non_reference (scope
);
7497 /* The type of the POSTFIX_EXPRESSION must be complete. */
7498 /* Unlike the object expression in other contexts, *this is not
7499 required to be of complete type for purposes of class member
7500 access (5.2.5) outside the member function body. */
7501 if (postfix_expression
!= current_class_ref
7502 && scope
!= error_mark_node
7503 && !(processing_template_decl
7504 && current_class_type
7505 && (same_type_ignoring_top_level_qualifiers_p
7506 (scope
, current_class_type
))))
7508 scope
= complete_type (scope
);
7509 if (!COMPLETE_TYPE_P (scope
)
7510 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7512 return error_mark_node
;
7517 /* Let the name lookup machinery know that we are processing a
7518 class member access expression. */
7519 parser
->context
->object_type
= scope
;
7520 /* If something went wrong, we want to be able to discern that case,
7521 as opposed to the case where there was no SCOPE due to the type
7522 of expression being dependent. */
7524 scope
= error_mark_node
;
7525 /* If the SCOPE was erroneous, make the various semantic analysis
7526 functions exit quickly -- and without issuing additional error
7528 if (scope
== error_mark_node
)
7529 postfix_expression
= error_mark_node
;
7534 /* Tell cp_parser_lookup_name that there was an object, even though it's
7536 parser
->context
->object_type
= unknown_type_node
;
7538 /* Assume this expression is not a pseudo-destructor access. */
7539 pseudo_destructor_p
= false;
7541 /* If the SCOPE is a scalar type, then, if this is a valid program,
7542 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7543 is type dependent, it can be pseudo-destructor-name or something else.
7544 Try to parse it as pseudo-destructor-name first. */
7545 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7550 cp_parser_parse_tentatively (parser
);
7551 /* Parse the pseudo-destructor-name. */
7553 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7556 && (cp_parser_error_occurred (parser
)
7557 || !SCALAR_TYPE_P (type
)))
7558 cp_parser_abort_tentative_parse (parser
);
7559 else if (cp_parser_parse_definitely (parser
))
7561 pseudo_destructor_p
= true;
7563 = finish_pseudo_destructor_expr (postfix_expression
,
7568 if (!pseudo_destructor_p
)
7570 /* If the SCOPE is not a scalar type, we are looking at an
7571 ordinary class member access expression, rather than a
7572 pseudo-destructor-name. */
7574 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7575 /* Parse the id-expression. */
7576 name
= (cp_parser_id_expression
7578 cp_parser_optional_template_keyword (parser
),
7579 /*check_dependency_p=*/true,
7581 /*declarator_p=*/false,
7582 /*optional_p=*/false));
7583 /* In general, build a SCOPE_REF if the member name is qualified.
7584 However, if the name was not dependent and has already been
7585 resolved; there is no need to build the SCOPE_REF. For example;
7587 struct X { void f(); };
7588 template <typename T> void f(T* t) { t->X::f(); }
7590 Even though "t" is dependent, "X::f" is not and has been resolved
7591 to a BASELINK; there is no need to include scope information. */
7593 /* But we do need to remember that there was an explicit scope for
7594 virtual function calls. */
7596 *idk
= CP_ID_KIND_QUALIFIED
;
7598 /* If the name is a template-id that names a type, we will get a
7599 TYPE_DECL here. That is invalid code. */
7600 if (TREE_CODE (name
) == TYPE_DECL
)
7602 error_at (token
->location
, "invalid use of %qD", name
);
7603 postfix_expression
= error_mark_node
;
7607 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7609 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7611 error_at (token
->location
, "%<%D::%D%> is not a class member",
7612 parser
->scope
, name
);
7613 postfix_expression
= error_mark_node
;
7616 name
= build_qualified_name (/*type=*/NULL_TREE
,
7620 parser
->scope
= NULL_TREE
;
7621 parser
->qualifying_scope
= NULL_TREE
;
7622 parser
->object_scope
= NULL_TREE
;
7624 if (parser
->scope
&& name
&& BASELINK_P (name
))
7625 adjust_result_of_qualified_name_lookup
7626 (name
, parser
->scope
, scope
);
7628 = finish_class_member_access_expr (postfix_expression
, name
,
7630 tf_warning_or_error
);
7631 /* Build a location e.g.:
7634 where the caret is at the deref token, ranging from
7635 the start of postfix_expression to the end of the access expr. */
7637 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7638 location_t combined_loc
7639 = make_location (input_location
, start_loc
, end_loc
);
7640 protected_set_expr_location (postfix_expression
, combined_loc
);
7644 /* We no longer need to look up names in the scope of the object on
7645 the left-hand side of the `.' or `->' operator. */
7646 parser
->context
->object_type
= NULL_TREE
;
7648 /* Outside of offsetof, these operators may not appear in
7649 constant-expressions. */
7651 && (cp_parser_non_integral_constant_expression
7652 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7653 postfix_expression
= error_mark_node
;
7655 return postfix_expression
;
7658 /* Parse a parenthesized expression-list.
7661 assignment-expression
7662 expression-list, assignment-expression
7667 identifier, expression-list
7669 CAST_P is true if this expression is the target of a cast.
7671 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7674 WRAP_LOCATIONS_P is true if expressions within this list for which
7675 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7676 their source locations.
7678 Returns a vector of trees. Each element is a representation of an
7679 assignment-expression. NULL is returned if the ( and or ) are
7680 missing. An empty, but allocated, vector is returned on no
7681 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7682 if we are parsing an attribute list for an attribute that wants a
7683 plain identifier argument, normal_attr for an attribute that wants
7684 an expression, or non_attr if we aren't parsing an attribute list. If
7685 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7686 not all of the expressions in the list were constant.
7687 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7688 will be written to with the location of the closing parenthesis. If
7689 an error occurs, it may or may not be written to. */
7691 static vec
<tree
, va_gc
> *
7692 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7693 int is_attribute_list
,
7695 bool allow_expansion_p
,
7696 bool *non_constant_p
,
7697 location_t
*close_paren_loc
,
7698 bool wrap_locations_p
)
7700 vec
<tree
, va_gc
> *expression_list
;
7701 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7702 tree identifier
= NULL_TREE
;
7703 bool saved_greater_than_is_operator_p
;
7705 /* Assume all the expressions will be constant. */
7707 *non_constant_p
= false;
7709 matching_parens parens
;
7710 if (!parens
.require_open (parser
))
7713 expression_list
= make_tree_vector ();
7715 /* Within a parenthesized expression, a `>' token is always
7716 the greater-than operator. */
7717 saved_greater_than_is_operator_p
7718 = parser
->greater_than_is_operator_p
;
7719 parser
->greater_than_is_operator_p
= true;
7721 cp_expr
expr (NULL_TREE
);
7723 /* Consume expressions until there are no more. */
7724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7727 /* At the beginning of attribute lists, check to see if the
7728 next token is an identifier. */
7729 if (is_attribute_list
== id_attr
7730 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7734 /* Consume the identifier. */
7735 token
= cp_lexer_consume_token (parser
->lexer
);
7736 /* Save the identifier. */
7737 identifier
= token
->u
.value
;
7741 bool expr_non_constant_p
;
7743 /* Parse the next assignment-expression. */
7744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7746 /* A braced-init-list. */
7747 cp_lexer_set_source_position (parser
->lexer
);
7748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7749 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7750 if (non_constant_p
&& expr_non_constant_p
)
7751 *non_constant_p
= true;
7753 else if (non_constant_p
)
7755 expr
= (cp_parser_constant_expression
7756 (parser
, /*allow_non_constant_p=*/true,
7757 &expr_non_constant_p
));
7758 if (expr_non_constant_p
)
7759 *non_constant_p
= true;
7762 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7766 expr
= instantiate_non_dependent_expr (expr
);
7768 /* If we have an ellipsis, then this is an expression
7770 if (allow_expansion_p
7771 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7773 /* Consume the `...'. */
7774 cp_lexer_consume_token (parser
->lexer
);
7776 /* Build the argument pack. */
7777 expr
= make_pack_expansion (expr
);
7780 if (wrap_locations_p
)
7781 expr
.maybe_add_location_wrapper ();
7783 /* Add it to the list. We add error_mark_node
7784 expressions to the list, so that we can still tell if
7785 the correct form for a parenthesized expression-list
7786 is found. That gives better errors. */
7787 vec_safe_push (expression_list
, expr
.get_value ());
7789 if (expr
== error_mark_node
)
7793 /* After the first item, attribute lists look the same as
7794 expression lists. */
7795 is_attribute_list
= non_attr
;
7798 /* If the next token isn't a `,', then we are done. */
7799 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7802 /* Otherwise, consume the `,' and keep going. */
7803 cp_lexer_consume_token (parser
->lexer
);
7806 if (close_paren_loc
)
7807 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7809 if (!parens
.require_close (parser
))
7814 /* We try and resync to an unnested comma, as that will give the
7815 user better diagnostics. */
7816 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7817 /*recovering=*/true,
7819 /*consume_paren=*/true);
7824 parser
->greater_than_is_operator_p
7825 = saved_greater_than_is_operator_p
;
7830 parser
->greater_than_is_operator_p
7831 = saved_greater_than_is_operator_p
;
7834 vec_safe_insert (expression_list
, 0, identifier
);
7836 return expression_list
;
7839 /* Parse a pseudo-destructor-name.
7841 pseudo-destructor-name:
7842 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7843 :: [opt] nested-name-specifier template template-id :: ~ type-name
7844 :: [opt] nested-name-specifier [opt] ~ type-name
7846 If either of the first two productions is used, sets *SCOPE to the
7847 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7848 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7849 or ERROR_MARK_NODE if the parse fails. */
7852 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7857 bool nested_name_specifier_p
;
7860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7861 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7862 && !type_dependent_expression_p (object
))
7864 if (cxx_dialect
< cxx14
)
7865 pedwarn (input_location
, 0,
7866 "%<~auto%> only available with "
7867 "-std=c++14 or -std=gnu++14");
7868 cp_lexer_consume_token (parser
->lexer
);
7869 cp_lexer_consume_token (parser
->lexer
);
7871 *type
= TREE_TYPE (object
);
7875 /* Assume that things will not work out. */
7876 *type
= error_mark_node
;
7878 /* Look for the optional `::' operator. */
7879 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7880 /* Look for the optional nested-name-specifier. */
7881 nested_name_specifier_p
7882 = (cp_parser_nested_name_specifier_opt (parser
,
7883 /*typename_keyword_p=*/false,
7884 /*check_dependency_p=*/true,
7886 /*is_declaration=*/false)
7888 /* Now, if we saw a nested-name-specifier, we might be doing the
7889 second production. */
7890 if (nested_name_specifier_p
7891 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7893 /* Consume the `template' keyword. */
7894 cp_lexer_consume_token (parser
->lexer
);
7895 /* Parse the template-id. */
7896 cp_parser_template_id (parser
,
7897 /*template_keyword_p=*/true,
7898 /*check_dependency_p=*/false,
7900 /*is_declaration=*/true);
7901 /* Look for the `::' token. */
7902 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7904 /* If the next token is not a `~', then there might be some
7905 additional qualification. */
7906 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7908 /* At this point, we're looking for "type-name :: ~". The type-name
7909 must not be a class-name, since this is a pseudo-destructor. So,
7910 it must be either an enum-name, or a typedef-name -- both of which
7911 are just identifiers. So, we peek ahead to check that the "::"
7912 and "~" tokens are present; if they are not, then we can avoid
7913 calling type_name. */
7914 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7915 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7916 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7918 cp_parser_error (parser
, "non-scalar type");
7922 /* Look for the type-name. */
7923 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7924 if (*scope
== error_mark_node
)
7927 /* Look for the `::' token. */
7928 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7933 /* Look for the `~'. */
7934 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7936 /* Once we see the ~, this has to be a pseudo-destructor. */
7937 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7938 cp_parser_commit_to_topmost_tentative_parse (parser
);
7940 /* Look for the type-name again. We are not responsible for
7941 checking that it matches the first type-name. */
7942 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7945 /* Parse a unary-expression.
7951 unary-operator cast-expression
7952 sizeof unary-expression
7954 alignof ( type-id ) [C++0x]
7961 __extension__ cast-expression
7962 __alignof__ unary-expression
7963 __alignof__ ( type-id )
7964 alignof unary-expression [C++0x]
7965 __real__ cast-expression
7966 __imag__ cast-expression
7968 sizeof ( type-id ) { initializer-list , [opt] }
7969 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7970 __alignof__ ( type-id ) { initializer-list , [opt] }
7972 ADDRESS_P is true iff the unary-expression is appearing as the
7973 operand of the `&' operator. CAST_P is true if this expression is
7974 the target of a cast.
7976 Returns a representation of the expression. */
7979 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7980 bool address_p
, bool cast_p
, bool decltype_p
)
7983 enum tree_code unary_operator
;
7985 /* Peek at the next token. */
7986 token
= cp_lexer_peek_token (parser
->lexer
);
7987 /* Some keywords give away the kind of expression. */
7988 if (token
->type
== CPP_KEYWORD
)
7990 enum rid keyword
= token
->keyword
;
7999 location_t start_loc
= token
->location
;
8001 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8002 /* Consume the token. */
8003 cp_lexer_consume_token (parser
->lexer
);
8004 /* Parse the operand. */
8005 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8007 if (TYPE_P (operand
))
8008 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
8011 /* ISO C++ defines alignof only with types, not with
8012 expressions. So pedwarn if alignof is used with a non-
8013 type expression. However, __alignof__ is ok. */
8014 if (id_equal (token
->u
.value
, "alignof"))
8015 pedwarn (token
->location
, OPT_Wpedantic
,
8016 "ISO C++ does not allow %<alignof%> "
8019 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8021 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8022 SIZEOF_EXPR with the original operand. */
8023 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8025 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8027 if (!processing_template_decl
&& TYPE_P (operand
))
8029 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8030 build1 (NOP_EXPR
, operand
,
8032 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8035 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8036 TREE_SIDE_EFFECTS (ret
) = 0;
8037 TREE_READONLY (ret
) = 1;
8041 /* Construct a location e.g. :
8044 with start == caret at the start of the "alignof"/"sizeof"
8045 token, with the endpoint at the final closing paren. */
8046 location_t finish_loc
8047 = cp_lexer_previous_token (parser
->lexer
)->location
;
8048 location_t compound_loc
8049 = make_location (start_loc
, start_loc
, finish_loc
);
8051 cp_expr
ret_expr (ret
);
8052 ret_expr
.set_location (compound_loc
);
8053 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8058 return cp_parser_new_expression (parser
);
8061 return cp_parser_delete_expression (parser
);
8065 /* The saved value of the PEDANTIC flag. */
8069 /* Save away the PEDANTIC flag. */
8070 cp_parser_extension_opt (parser
, &saved_pedantic
);
8071 /* Parse the cast-expression. */
8072 expr
= cp_parser_simple_cast_expression (parser
);
8073 /* Restore the PEDANTIC flag. */
8074 pedantic
= saved_pedantic
;
8084 /* Consume the `__real__' or `__imag__' token. */
8085 cp_lexer_consume_token (parser
->lexer
);
8086 /* Parse the cast-expression. */
8087 expression
= cp_parser_simple_cast_expression (parser
);
8088 /* Create the complete representation. */
8089 return build_x_unary_op (token
->location
,
8090 (keyword
== RID_REALPART
8091 ? REALPART_EXPR
: IMAGPART_EXPR
),
8093 tf_warning_or_error
);
8097 case RID_TRANSACTION_ATOMIC
:
8098 case RID_TRANSACTION_RELAXED
:
8099 return cp_parser_transaction_expression (parser
, keyword
);
8104 const char *saved_message
;
8105 bool saved_integral_constant_expression_p
;
8106 bool saved_non_integral_constant_expression_p
;
8107 bool saved_greater_than_is_operator_p
;
8109 location_t start_loc
= token
->location
;
8111 cp_lexer_consume_token (parser
->lexer
);
8112 matching_parens parens
;
8113 parens
.require_open (parser
);
8115 saved_message
= parser
->type_definition_forbidden_message
;
8116 parser
->type_definition_forbidden_message
8117 = G_("types may not be defined in %<noexcept%> expressions");
8119 saved_integral_constant_expression_p
8120 = parser
->integral_constant_expression_p
;
8121 saved_non_integral_constant_expression_p
8122 = parser
->non_integral_constant_expression_p
;
8123 parser
->integral_constant_expression_p
= false;
8125 saved_greater_than_is_operator_p
8126 = parser
->greater_than_is_operator_p
;
8127 parser
->greater_than_is_operator_p
= true;
8129 ++cp_unevaluated_operand
;
8130 ++c_inhibit_evaluation_warnings
;
8131 ++cp_noexcept_operand
;
8132 expr
= cp_parser_expression (parser
);
8133 --cp_noexcept_operand
;
8134 --c_inhibit_evaluation_warnings
;
8135 --cp_unevaluated_operand
;
8137 parser
->greater_than_is_operator_p
8138 = saved_greater_than_is_operator_p
;
8140 parser
->integral_constant_expression_p
8141 = saved_integral_constant_expression_p
;
8142 parser
->non_integral_constant_expression_p
8143 = saved_non_integral_constant_expression_p
;
8145 parser
->type_definition_forbidden_message
= saved_message
;
8147 location_t finish_loc
8148 = cp_lexer_peek_token (parser
->lexer
)->location
;
8149 parens
.require_close (parser
);
8151 /* Construct a location of the form:
8154 with start == caret, finishing at the close-paren. */
8155 location_t noexcept_loc
8156 = make_location (start_loc
, start_loc
, finish_loc
);
8158 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8167 /* Look for the `:: new' and `:: delete', which also signal the
8168 beginning of a new-expression, or delete-expression,
8169 respectively. If the next token is `::', then it might be one of
8171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8175 /* See if the token after the `::' is one of the keywords in
8176 which we're interested. */
8177 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8178 /* If it's `new', we have a new-expression. */
8179 if (keyword
== RID_NEW
)
8180 return cp_parser_new_expression (parser
);
8181 /* Similarly, for `delete'. */
8182 else if (keyword
== RID_DELETE
)
8183 return cp_parser_delete_expression (parser
);
8186 /* Look for a unary operator. */
8187 unary_operator
= cp_parser_unary_operator (token
);
8188 /* The `++' and `--' operators can be handled similarly, even though
8189 they are not technically unary-operators in the grammar. */
8190 if (unary_operator
== ERROR_MARK
)
8192 if (token
->type
== CPP_PLUS_PLUS
)
8193 unary_operator
= PREINCREMENT_EXPR
;
8194 else if (token
->type
== CPP_MINUS_MINUS
)
8195 unary_operator
= PREDECREMENT_EXPR
;
8196 /* Handle the GNU address-of-label extension. */
8197 else if (cp_parser_allow_gnu_extensions_p (parser
)
8198 && token
->type
== CPP_AND_AND
)
8202 location_t start_loc
= token
->location
;
8204 /* Consume the '&&' token. */
8205 cp_lexer_consume_token (parser
->lexer
);
8206 /* Look for the identifier. */
8207 location_t finish_loc
8208 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8209 identifier
= cp_parser_identifier (parser
);
8210 /* Construct a location of the form:
8213 with caret==start at the "&&", finish at the end of the label. */
8214 location_t combined_loc
8215 = make_location (start_loc
, start_loc
, finish_loc
);
8216 /* Create an expression representing the address. */
8217 expression
= finish_label_address_expr (identifier
, combined_loc
);
8218 if (cp_parser_non_integral_constant_expression (parser
,
8220 expression
= error_mark_node
;
8224 if (unary_operator
!= ERROR_MARK
)
8226 cp_expr cast_expression
;
8227 cp_expr expression
= error_mark_node
;
8228 non_integral_constant non_constant_p
= NIC_NONE
;
8229 location_t loc
= token
->location
;
8230 tsubst_flags_t complain
= complain_flags (decltype_p
);
8232 /* Consume the operator token. */
8233 token
= cp_lexer_consume_token (parser
->lexer
);
8234 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8236 /* Parse the cast-expression. */
8238 = cp_parser_cast_expression (parser
,
8239 unary_operator
== ADDR_EXPR
,
8245 OP_TOKEN CAST_EXPRESSION
8246 ^~~~~~~~~~~~~~~~~~~~~~~~~
8247 with start==caret at the operator token, and
8248 extending to the end of the cast_expression. */
8249 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8251 /* Now, build an appropriate representation. */
8252 switch (unary_operator
)
8255 non_constant_p
= NIC_STAR
;
8256 expression
= build_x_indirect_ref (loc
, cast_expression
,
8259 /* TODO: build_x_indirect_ref does not always honor the
8260 location, so ensure it is set. */
8261 expression
.set_location (loc
);
8265 non_constant_p
= NIC_ADDR
;
8268 expression
= build_x_unary_op (loc
, unary_operator
,
8271 /* TODO: build_x_unary_op does not always honor the location,
8272 so ensure it is set. */
8273 expression
.set_location (loc
);
8276 case PREINCREMENT_EXPR
:
8277 case PREDECREMENT_EXPR
:
8278 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8279 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8282 /* Immediately fold negation of a constant, unless the constant is 0
8283 (since -0 == 0) or it would overflow. */
8284 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8285 && CONSTANT_CLASS_P (cast_expression
)
8286 && !integer_zerop (cast_expression
)
8287 && !TREE_OVERFLOW (cast_expression
))
8289 tree folded
= fold_build1 (unary_operator
,
8290 TREE_TYPE (cast_expression
),
8292 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8294 expression
= cp_expr (folded
, loc
);
8299 case UNARY_PLUS_EXPR
:
8300 case TRUTH_NOT_EXPR
:
8301 expression
= finish_unary_op_expr (loc
, unary_operator
,
8302 cast_expression
, complain
);
8309 if (non_constant_p
!= NIC_NONE
8310 && cp_parser_non_integral_constant_expression (parser
,
8312 expression
= error_mark_node
;
8317 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8318 /*member_access_only_p=*/false,
8323 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8324 unary-operator, the corresponding tree code is returned. */
8326 static enum tree_code
8327 cp_parser_unary_operator (cp_token
* token
)
8329 switch (token
->type
)
8332 return INDIRECT_REF
;
8338 return UNARY_PLUS_EXPR
;
8344 return TRUTH_NOT_EXPR
;
8347 return BIT_NOT_EXPR
;
8354 /* Parse a new-expression.
8357 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8358 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8360 Returns a representation of the expression. */
8363 cp_parser_new_expression (cp_parser
* parser
)
8365 bool global_scope_p
;
8366 vec
<tree
, va_gc
> *placement
;
8368 vec
<tree
, va_gc
> *initializer
;
8369 tree nelts
= NULL_TREE
;
8372 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8374 /* Look for the optional `::' operator. */
8376 = (cp_parser_global_scope_opt (parser
,
8377 /*current_scope_valid_p=*/false)
8379 /* Look for the `new' operator. */
8380 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8381 /* There's no easy way to tell a new-placement from the
8382 `( type-id )' construct. */
8383 cp_parser_parse_tentatively (parser
);
8384 /* Look for a new-placement. */
8385 placement
= cp_parser_new_placement (parser
);
8386 /* If that didn't work out, there's no new-placement. */
8387 if (!cp_parser_parse_definitely (parser
))
8389 if (placement
!= NULL
)
8390 release_tree_vector (placement
);
8394 /* If the next token is a `(', then we have a parenthesized
8396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8399 const char *saved_message
= parser
->type_definition_forbidden_message
;
8401 /* Consume the `('. */
8402 matching_parens parens
;
8403 parens
.consume_open (parser
);
8405 /* Parse the type-id. */
8406 parser
->type_definition_forbidden_message
8407 = G_("types may not be defined in a new-expression");
8409 type_id_in_expr_sentinel
s (parser
);
8410 type
= cp_parser_type_id (parser
);
8412 parser
->type_definition_forbidden_message
= saved_message
;
8414 /* Look for the closing `)'. */
8415 parens
.require_close (parser
);
8416 token
= cp_lexer_peek_token (parser
->lexer
);
8417 /* There should not be a direct-new-declarator in this production,
8418 but GCC used to allowed this, so we check and emit a sensible error
8419 message for this case. */
8420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8422 error_at (token
->location
,
8423 "array bound forbidden after parenthesized type-id");
8424 inform (token
->location
,
8425 "try removing the parentheses around the type-id");
8426 cp_parser_direct_new_declarator (parser
);
8429 /* Otherwise, there must be a new-type-id. */
8431 type
= cp_parser_new_type_id (parser
, &nelts
);
8433 /* If the next token is a `(' or '{', then we have a new-initializer. */
8434 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8435 if (token
->type
== CPP_OPEN_PAREN
8436 || token
->type
== CPP_OPEN_BRACE
)
8437 initializer
= cp_parser_new_initializer (parser
);
8441 /* A new-expression may not appear in an integral constant
8443 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8444 ret
= error_mark_node
;
8445 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8446 of a new-type-id or type-id of a new-expression, the new-expression shall
8447 contain a new-initializer of the form ( assignment-expression )".
8448 Additionally, consistently with the spirit of DR 1467, we want to accept
8449 'new auto { 2 }' too. */
8450 else if ((ret
= type_uses_auto (type
))
8451 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8452 && (vec_safe_length (initializer
) != 1
8453 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8454 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8456 error_at (token
->location
,
8457 "initialization of new-expression for type %<auto%> "
8458 "requires exactly one element");
8459 ret
= error_mark_node
;
8463 /* Construct a location e.g.:
8466 with caret == start at the start of the "new" token, and the end
8467 at the end of the final token we consumed. */
8468 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8469 location_t end_loc
= get_finish (end_tok
->location
);
8470 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8472 /* Create a representation of the new-expression. */
8473 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8474 tf_warning_or_error
);
8475 protected_set_expr_location (ret
, combined_loc
);
8478 if (placement
!= NULL
)
8479 release_tree_vector (placement
);
8480 if (initializer
!= NULL
)
8481 release_tree_vector (initializer
);
8486 /* Parse a new-placement.
8491 Returns the same representation as for an expression-list. */
8493 static vec
<tree
, va_gc
> *
8494 cp_parser_new_placement (cp_parser
* parser
)
8496 vec
<tree
, va_gc
> *expression_list
;
8498 /* Parse the expression-list. */
8499 expression_list
= (cp_parser_parenthesized_expression_list
8500 (parser
, non_attr
, /*cast_p=*/false,
8501 /*allow_expansion_p=*/true,
8502 /*non_constant_p=*/NULL
));
8504 if (expression_list
&& expression_list
->is_empty ())
8505 error ("expected expression-list or type-id");
8507 return expression_list
;
8510 /* Parse a new-type-id.
8513 type-specifier-seq new-declarator [opt]
8515 Returns the TYPE allocated. If the new-type-id indicates an array
8516 type, *NELTS is set to the number of elements in the last array
8517 bound; the TYPE will not include the last array bound. */
8520 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8522 cp_decl_specifier_seq type_specifier_seq
;
8523 cp_declarator
*new_declarator
;
8524 cp_declarator
*declarator
;
8525 cp_declarator
*outer_declarator
;
8526 const char *saved_message
;
8528 /* The type-specifier sequence must not contain type definitions.
8529 (It cannot contain declarations of new types either, but if they
8530 are not definitions we will catch that because they are not
8532 saved_message
= parser
->type_definition_forbidden_message
;
8533 parser
->type_definition_forbidden_message
8534 = G_("types may not be defined in a new-type-id");
8535 /* Parse the type-specifier-seq. */
8536 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8537 /*is_trailing_return=*/false,
8538 &type_specifier_seq
);
8539 /* Restore the old message. */
8540 parser
->type_definition_forbidden_message
= saved_message
;
8542 if (type_specifier_seq
.type
== error_mark_node
)
8543 return error_mark_node
;
8545 /* Parse the new-declarator. */
8546 new_declarator
= cp_parser_new_declarator_opt (parser
);
8548 /* Determine the number of elements in the last array dimension, if
8551 /* Skip down to the last array dimension. */
8552 declarator
= new_declarator
;
8553 outer_declarator
= NULL
;
8554 while (declarator
&& (declarator
->kind
== cdk_pointer
8555 || declarator
->kind
== cdk_ptrmem
))
8557 outer_declarator
= declarator
;
8558 declarator
= declarator
->declarator
;
8561 && declarator
->kind
== cdk_array
8562 && declarator
->declarator
8563 && declarator
->declarator
->kind
== cdk_array
)
8565 outer_declarator
= declarator
;
8566 declarator
= declarator
->declarator
;
8569 if (declarator
&& declarator
->kind
== cdk_array
)
8571 *nelts
= declarator
->u
.array
.bounds
;
8572 if (*nelts
== error_mark_node
)
8573 *nelts
= integer_one_node
;
8575 if (outer_declarator
)
8576 outer_declarator
->declarator
= declarator
->declarator
;
8578 new_declarator
= NULL
;
8581 return groktypename (&type_specifier_seq
, new_declarator
, false);
8584 /* Parse an (optional) new-declarator.
8587 ptr-operator new-declarator [opt]
8588 direct-new-declarator
8590 Returns the declarator. */
8592 static cp_declarator
*
8593 cp_parser_new_declarator_opt (cp_parser
* parser
)
8595 enum tree_code code
;
8596 tree type
, std_attributes
= NULL_TREE
;
8597 cp_cv_quals cv_quals
;
8599 /* We don't know if there's a ptr-operator next, or not. */
8600 cp_parser_parse_tentatively (parser
);
8601 /* Look for a ptr-operator. */
8602 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8603 /* If that worked, look for more new-declarators. */
8604 if (cp_parser_parse_definitely (parser
))
8606 cp_declarator
*declarator
;
8608 /* Parse another optional declarator. */
8609 declarator
= cp_parser_new_declarator_opt (parser
);
8611 declarator
= cp_parser_make_indirect_declarator
8612 (code
, type
, cv_quals
, declarator
, std_attributes
);
8617 /* If the next token is a `[', there is a direct-new-declarator. */
8618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8619 return cp_parser_direct_new_declarator (parser
);
8624 /* Parse a direct-new-declarator.
8626 direct-new-declarator:
8628 direct-new-declarator [constant-expression]
8632 static cp_declarator
*
8633 cp_parser_direct_new_declarator (cp_parser
* parser
)
8635 cp_declarator
*declarator
= NULL
;
8642 /* Look for the opening `['. */
8643 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8645 token
= cp_lexer_peek_token (parser
->lexer
);
8646 expression
= cp_parser_expression (parser
);
8647 /* The standard requires that the expression have integral
8648 type. DR 74 adds enumeration types. We believe that the
8649 real intent is that these expressions be handled like the
8650 expression in a `switch' condition, which also allows
8651 classes with a single conversion to integral or
8652 enumeration type. */
8653 if (!processing_template_decl
)
8656 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8661 error_at (token
->location
,
8662 "expression in new-declarator must have integral "
8663 "or enumeration type");
8664 expression
= error_mark_node
;
8668 /* Look for the closing `]'. */
8669 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8671 /* Add this bound to the declarator. */
8672 declarator
= make_array_declarator (declarator
, expression
);
8674 /* If the next token is not a `[', then there are no more
8676 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8683 /* Parse a new-initializer.
8686 ( expression-list [opt] )
8689 Returns a representation of the expression-list. */
8691 static vec
<tree
, va_gc
> *
8692 cp_parser_new_initializer (cp_parser
* parser
)
8694 vec
<tree
, va_gc
> *expression_list
;
8696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8699 bool expr_non_constant_p
;
8700 cp_lexer_set_source_position (parser
->lexer
);
8701 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8702 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8703 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8704 expression_list
= make_tree_vector_single (t
);
8707 expression_list
= (cp_parser_parenthesized_expression_list
8708 (parser
, non_attr
, /*cast_p=*/false,
8709 /*allow_expansion_p=*/true,
8710 /*non_constant_p=*/NULL
));
8712 return expression_list
;
8715 /* Parse a delete-expression.
8718 :: [opt] delete cast-expression
8719 :: [opt] delete [ ] cast-expression
8721 Returns a representation of the expression. */
8724 cp_parser_delete_expression (cp_parser
* parser
)
8726 bool global_scope_p
;
8730 /* Look for the optional `::' operator. */
8732 = (cp_parser_global_scope_opt (parser
,
8733 /*current_scope_valid_p=*/false)
8735 /* Look for the `delete' keyword. */
8736 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8737 /* See if the array syntax is in use. */
8738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8740 /* Consume the `[' token. */
8741 cp_lexer_consume_token (parser
->lexer
);
8742 /* Look for the `]' token. */
8743 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8744 /* Remember that this is the `[]' construct. */
8750 /* Parse the cast-expression. */
8751 expression
= cp_parser_simple_cast_expression (parser
);
8753 /* A delete-expression may not appear in an integral constant
8755 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8756 return error_mark_node
;
8758 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8759 tf_warning_or_error
);
8762 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8763 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8767 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8769 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8770 switch (token
->type
)
8776 case CPP_CLOSE_SQUARE
:
8777 case CPP_CLOSE_PAREN
:
8778 case CPP_CLOSE_BRACE
:
8779 case CPP_OPEN_BRACE
:
8783 case CPP_DEREF_STAR
:
8791 case CPP_GREATER_EQ
:
8812 case CPP_OPEN_PAREN
:
8813 /* In ((type ()) () the last () isn't a valid cast-expression,
8814 so the whole must be parsed as postfix-expression. */
8815 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8818 case CPP_OPEN_SQUARE
:
8819 /* '[' may start a primary-expression in obj-c++ and in C++11,
8820 as a lambda-expression, eg, '(void)[]{}'. */
8821 if (cxx_dialect
>= cxx11
)
8823 return c_dialect_objc ();
8826 case CPP_MINUS_MINUS
:
8827 /* '++' and '--' may or may not start a cast-expression:
8829 struct T { void operator++(int); };
8830 void f() { (T())++; }
8843 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8844 in the order: const_cast, static_cast, reinterpret_cast.
8846 Don't suggest dynamic_cast.
8848 Return the first legal cast kind found, or NULL otherwise. */
8851 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8855 /* Reuse the parser logic by attempting to build the various kinds of
8856 cast, with "complain" disabled.
8857 Identify the first such cast that is valid. */
8859 /* Don't attempt to run such logic within template processing. */
8860 if (processing_template_decl
)
8863 /* First try const_cast. */
8864 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8865 if (trial
!= error_mark_node
)
8866 return "const_cast";
8868 /* If that fails, try static_cast. */
8869 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8870 if (trial
!= error_mark_node
)
8871 return "static_cast";
8873 /* Finally, try reinterpret_cast. */
8874 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8875 if (trial
!= error_mark_node
)
8876 return "reinterpret_cast";
8878 /* No such cast possible. */
8882 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8883 suggesting how to convert a C-style cast of the form:
8887 to a C++-style cast.
8889 The primary range of RICHLOC is asssumed to be that of the original
8890 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8891 of the parens in the C-style cast. */
8894 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8895 location_t close_paren_loc
, tree orig_expr
,
8898 /* This function is non-trivial, so bail out now if the warning isn't
8899 going to be emitted. */
8900 if (!warn_old_style_cast
)
8903 /* Try to find a legal C++ cast, trying them in order:
8904 const_cast, static_cast, reinterpret_cast. */
8905 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8906 if (!cast_suggestion
)
8909 /* Replace the open paren with "CAST_SUGGESTION<". */
8911 pp_printf (&pp
, "%s<", cast_suggestion
);
8912 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8914 /* Replace the close paren with "> (". */
8915 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8917 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8918 rich_loc
->add_fixit_insert_after (")");
8922 /* Parse a cast-expression.
8926 ( type-id ) cast-expression
8928 ADDRESS_P is true iff the unary-expression is appearing as the
8929 operand of the `&' operator. CAST_P is true if this expression is
8930 the target of a cast.
8932 Returns a representation of the expression. */
8935 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8936 bool decltype_p
, cp_id_kind
* pidk
)
8938 /* If it's a `(', then we might be looking at a cast. */
8939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8941 tree type
= NULL_TREE
;
8942 cp_expr
expr (NULL_TREE
);
8943 int cast_expression
= 0;
8944 const char *saved_message
;
8946 /* There's no way to know yet whether or not this is a cast.
8947 For example, `(int (3))' is a unary-expression, while `(int)
8948 3' is a cast. So, we resort to parsing tentatively. */
8949 cp_parser_parse_tentatively (parser
);
8950 /* Types may not be defined in a cast. */
8951 saved_message
= parser
->type_definition_forbidden_message
;
8952 parser
->type_definition_forbidden_message
8953 = G_("types may not be defined in casts");
8954 /* Consume the `('. */
8955 matching_parens parens
;
8956 cp_token
*open_paren
= parens
.consume_open (parser
);
8957 location_t open_paren_loc
= open_paren
->location
;
8958 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8960 /* A very tricky bit is that `(struct S) { 3 }' is a
8961 compound-literal (which we permit in C++ as an extension).
8962 But, that construct is not a cast-expression -- it is a
8963 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8964 is legal; if the compound-literal were a cast-expression,
8965 you'd need an extra set of parentheses.) But, if we parse
8966 the type-id, and it happens to be a class-specifier, then we
8967 will commit to the parse at that point, because we cannot
8968 undo the action that is done when creating a new class. So,
8969 then we cannot back up and do a postfix-expression.
8971 Another tricky case is the following (c++/29234):
8973 struct S { void operator () (); };
8980 As a type-id we parse the parenthesized S()() as a function
8981 returning a function, groktypename complains and we cannot
8982 back up in this case either.
8984 Therefore, we scan ahead to the closing `)', and check to see
8985 if the tokens after the `)' can start a cast-expression. Otherwise
8986 we are dealing with an unary-expression, a postfix-expression
8989 Yet another tricky case, in C++11, is the following (c++/54891):
8993 The issue is that usually, besides the case of lambda-expressions,
8994 the parenthesized type-id cannot be followed by '[', and, eg, we
8995 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8996 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8997 we don't commit, we try a cast-expression, then an unary-expression.
8999 Save tokens so that we can put them back. */
9000 cp_lexer_save_tokens (parser
->lexer
);
9002 /* We may be looking at a cast-expression. */
9003 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9004 /*consume_paren=*/true))
9006 = cp_parser_tokens_start_cast_expression (parser
);
9008 /* Roll back the tokens we skipped. */
9009 cp_lexer_rollback_tokens (parser
->lexer
);
9010 /* If we aren't looking at a cast-expression, simulate an error so
9011 that the call to cp_parser_error_occurred below returns true. */
9012 if (!cast_expression
)
9013 cp_parser_simulate_error (parser
);
9016 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9017 parser
->in_type_id_in_expr_p
= true;
9018 /* Look for the type-id. */
9019 type
= cp_parser_type_id (parser
);
9020 /* Look for the closing `)'. */
9021 cp_token
*close_paren
= parens
.require_close (parser
);
9023 close_paren_loc
= close_paren
->location
;
9024 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9027 /* Restore the saved message. */
9028 parser
->type_definition_forbidden_message
= saved_message
;
9030 /* At this point this can only be either a cast or a
9031 parenthesized ctor such as `(T ())' that looks like a cast to
9032 function returning T. */
9033 if (!cp_parser_error_occurred (parser
))
9035 /* Only commit if the cast-expression doesn't start with
9036 '++', '--', or '[' in C++11. */
9037 if (cast_expression
> 0)
9038 cp_parser_commit_to_topmost_tentative_parse (parser
);
9040 expr
= cp_parser_cast_expression (parser
,
9041 /*address_p=*/false,
9043 /*decltype_p=*/false,
9046 if (cp_parser_parse_definitely (parser
))
9048 /* Warn about old-style casts, if so requested. */
9049 if (warn_old_style_cast
9050 && !in_system_header_at (input_location
)
9051 && !VOID_TYPE_P (type
)
9052 && current_lang_name
!= lang_name_c
)
9054 gcc_rich_location
rich_loc (input_location
);
9055 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9057 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9058 "use of old-style cast to %q#T", type
);
9061 /* Only type conversions to integral or enumeration types
9062 can be used in constant-expressions. */
9063 if (!cast_valid_in_integral_constant_expression_p (type
)
9064 && cp_parser_non_integral_constant_expression (parser
,
9066 return error_mark_node
;
9068 /* Perform the cast. */
9072 with start==caret at the open paren, extending to the
9074 location_t cast_loc
= make_location (open_paren_loc
,
9076 expr
.get_finish ());
9077 expr
= build_c_cast (cast_loc
, type
, expr
);
9082 cp_parser_abort_tentative_parse (parser
);
9085 /* If we get here, then it's not a cast, so it must be a
9086 unary-expression. */
9087 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9088 cast_p
, decltype_p
);
9091 /* Parse a binary expression of the general form:
9095 pm-expression .* cast-expression
9096 pm-expression ->* cast-expression
9098 multiplicative-expression:
9100 multiplicative-expression * pm-expression
9101 multiplicative-expression / pm-expression
9102 multiplicative-expression % pm-expression
9104 additive-expression:
9105 multiplicative-expression
9106 additive-expression + multiplicative-expression
9107 additive-expression - multiplicative-expression
9111 shift-expression << additive-expression
9112 shift-expression >> additive-expression
9114 relational-expression:
9116 relational-expression < shift-expression
9117 relational-expression > shift-expression
9118 relational-expression <= shift-expression
9119 relational-expression >= shift-expression
9123 relational-expression:
9124 relational-expression <? shift-expression
9125 relational-expression >? shift-expression
9127 equality-expression:
9128 relational-expression
9129 equality-expression == relational-expression
9130 equality-expression != relational-expression
9134 and-expression & equality-expression
9136 exclusive-or-expression:
9138 exclusive-or-expression ^ and-expression
9140 inclusive-or-expression:
9141 exclusive-or-expression
9142 inclusive-or-expression | exclusive-or-expression
9144 logical-and-expression:
9145 inclusive-or-expression
9146 logical-and-expression && inclusive-or-expression
9148 logical-or-expression:
9149 logical-and-expression
9150 logical-or-expression || logical-and-expression
9152 All these are implemented with a single function like:
9155 simple-cast-expression
9156 binary-expression <token> binary-expression
9158 CAST_P is true if this expression is the target of a cast.
9160 The binops_by_token map is used to get the tree codes for each <token> type.
9161 binary-expressions are associated according to a precedence table. */
9163 #define TOKEN_PRECEDENCE(token) \
9164 (((token->type == CPP_GREATER \
9165 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9166 && !parser->greater_than_is_operator_p) \
9167 ? PREC_NOT_OPERATOR \
9168 : binops_by_token[token->type].prec)
9171 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9172 bool no_toplevel_fold_p
,
9174 enum cp_parser_prec prec
,
9177 cp_parser_expression_stack stack
;
9178 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9179 cp_parser_expression_stack_entry current
;
9182 enum tree_code rhs_type
;
9183 enum cp_parser_prec new_prec
, lookahead_prec
;
9186 /* Parse the first expression. */
9187 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9188 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9189 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9190 cast_p
, decltype_p
, pidk
);
9191 current
.prec
= prec
;
9193 if (cp_parser_error_occurred (parser
))
9194 return error_mark_node
;
9198 /* Get an operator token. */
9199 token
= cp_lexer_peek_token (parser
->lexer
);
9201 if (warn_cxx11_compat
9202 && token
->type
== CPP_RSHIFT
9203 && !parser
->greater_than_is_operator_p
)
9205 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9206 "%<>>%> operator is treated"
9207 " as two right angle brackets in C++11"))
9208 inform (token
->location
,
9209 "suggest parentheses around %<>>%> expression");
9212 new_prec
= TOKEN_PRECEDENCE (token
);
9213 if (new_prec
!= PREC_NOT_OPERATOR
9214 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9215 /* This is a fold-expression; handle it later. */
9216 new_prec
= PREC_NOT_OPERATOR
;
9218 /* Popping an entry off the stack means we completed a subexpression:
9219 - either we found a token which is not an operator (`>' where it is not
9220 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9221 will happen repeatedly;
9222 - or, we found an operator which has lower priority. This is the case
9223 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9225 if (new_prec
<= current
.prec
)
9234 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9235 current
.loc
= token
->location
;
9237 /* We used the operator token. */
9238 cp_lexer_consume_token (parser
->lexer
);
9240 /* For "false && x" or "true || x", x will never be executed;
9241 disable warnings while evaluating it. */
9242 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9243 c_inhibit_evaluation_warnings
+=
9244 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9245 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9246 c_inhibit_evaluation_warnings
+=
9247 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9249 /* Extract another operand. It may be the RHS of this expression
9250 or the LHS of a new, higher priority expression. */
9251 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9252 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9253 rhs
= cp_parser_simple_cast_expression (parser
);
9255 /* Get another operator token. Look up its precedence to avoid
9256 building a useless (immediately popped) stack entry for common
9257 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9258 token
= cp_lexer_peek_token (parser
->lexer
);
9259 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9260 if (lookahead_prec
!= PREC_NOT_OPERATOR
9261 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9262 lookahead_prec
= PREC_NOT_OPERATOR
;
9263 if (lookahead_prec
> new_prec
)
9265 /* ... and prepare to parse the RHS of the new, higher priority
9266 expression. Since precedence levels on the stack are
9267 monotonically increasing, we do not have to care about
9272 current
.lhs_type
= rhs_type
;
9273 current
.prec
= new_prec
;
9274 new_prec
= lookahead_prec
;
9278 lookahead_prec
= new_prec
;
9279 /* If the stack is not empty, we have parsed into LHS the right side
9280 (`4' in the example above) of an expression we had suspended.
9281 We can use the information on the stack to recover the LHS (`3')
9282 from the stack together with the tree code (`MULT_EXPR'), and
9283 the precedence of the higher level subexpression
9284 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9285 which will be used to actually build the additive expression. */
9287 rhs_type
= current
.lhs_type
;
9292 /* Undo the disabling of warnings done above. */
9293 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9294 c_inhibit_evaluation_warnings
-=
9295 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9296 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9297 c_inhibit_evaluation_warnings
-=
9298 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9300 if (warn_logical_not_paren
9301 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9302 && current
.lhs_type
== TRUTH_NOT_EXPR
9303 /* Avoid warning for !!x == y. */
9304 && (TREE_CODE (current
.lhs
) != NE_EXPR
9305 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9306 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9307 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9308 /* Avoid warning for !b == y where b is boolean. */
9309 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9310 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9312 /* Avoid warning for !!b == y where b is boolean. */
9313 && (!DECL_P (current
.lhs
)
9314 || TREE_TYPE (current
.lhs
) == NULL_TREE
9315 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9316 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9317 current
.lhs
, maybe_constant_value (rhs
));
9321 location_t combined_loc
= make_location (current
.loc
,
9322 current
.lhs
.get_start (),
9325 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9326 ERROR_MARK for everything that is not a binary expression.
9327 This makes warn_about_parentheses miss some warnings that
9328 involve unary operators. For unary expressions we should
9329 pass the correct tree_code unless the unary expression was
9330 surrounded by parentheses.
9332 if (no_toplevel_fold_p
9333 && lookahead_prec
<= current
.prec
9336 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9337 current
.lhs
= error_mark_node
;
9341 = build_min (current
.tree_type
,
9342 TREE_CODE_CLASS (current
.tree_type
)
9344 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9345 current
.lhs
.get_value (), rhs
.get_value ());
9346 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9351 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9352 current
.lhs
, current
.lhs_type
,
9353 rhs
, rhs_type
, &overload
,
9354 complain_flags (decltype_p
));
9355 /* TODO: build_x_binary_op doesn't always honor the location. */
9356 current
.lhs
.set_location (combined_loc
);
9358 current
.lhs_type
= current
.tree_type
;
9360 /* If the binary operator required the use of an overloaded operator,
9361 then this expression cannot be an integral constant-expression.
9362 An overloaded operator can be used even if both operands are
9363 otherwise permissible in an integral constant-expression if at
9364 least one of the operands is of enumeration type. */
9367 && cp_parser_non_integral_constant_expression (parser
,
9369 return error_mark_node
;
9376 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9377 bool no_toplevel_fold_p
,
9378 enum cp_parser_prec prec
,
9381 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9382 /*decltype*/false, prec
, pidk
);
9385 /* Parse the `? expression : assignment-expression' part of a
9386 conditional-expression. The LOGICAL_OR_EXPR is the
9387 logical-or-expression that started the conditional-expression.
9388 Returns a representation of the entire conditional-expression.
9390 This routine is used by cp_parser_assignment_expression.
9392 ? expression : assignment-expression
9396 ? : assignment-expression */
9399 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9401 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9402 cp_expr assignment_expr
;
9403 struct cp_token
*token
;
9404 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9406 /* Consume the `?' token. */
9407 cp_lexer_consume_token (parser
->lexer
);
9408 token
= cp_lexer_peek_token (parser
->lexer
);
9409 if (cp_parser_allow_gnu_extensions_p (parser
)
9410 && token
->type
== CPP_COLON
)
9412 pedwarn (token
->location
, OPT_Wpedantic
,
9413 "ISO C++ does not allow ?: with omitted middle operand");
9414 /* Implicit true clause. */
9416 c_inhibit_evaluation_warnings
+=
9417 folded_logical_or_expr
== truthvalue_true_node
;
9418 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9422 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9423 parser
->colon_corrects_to_scope_p
= false;
9424 /* Parse the expression. */
9425 c_inhibit_evaluation_warnings
+=
9426 folded_logical_or_expr
== truthvalue_false_node
;
9427 expr
= cp_parser_expression (parser
);
9428 c_inhibit_evaluation_warnings
+=
9429 ((folded_logical_or_expr
== truthvalue_true_node
)
9430 - (folded_logical_or_expr
== truthvalue_false_node
));
9431 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9434 /* The next token should be a `:'. */
9435 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9436 /* Parse the assignment-expression. */
9437 assignment_expr
= cp_parser_assignment_expression (parser
);
9438 c_inhibit_evaluation_warnings
-=
9439 folded_logical_or_expr
== truthvalue_true_node
;
9442 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9443 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9444 with the caret at the "?", ranging from the start of
9445 the logical_or_expr to the end of the assignment_expr. */
9446 loc
= make_location (loc
,
9447 logical_or_expr
.get_start (),
9448 assignment_expr
.get_finish ());
9450 /* Build the conditional-expression. */
9451 return build_x_conditional_expr (loc
, logical_or_expr
,
9454 tf_warning_or_error
);
9457 /* Parse an assignment-expression.
9459 assignment-expression:
9460 conditional-expression
9461 logical-or-expression assignment-operator assignment_expression
9464 CAST_P is true if this expression is the target of a cast.
9465 DECLTYPE_P is true if this expression is the operand of decltype.
9467 Returns a representation for the expression. */
9470 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9471 bool cast_p
, bool decltype_p
)
9475 /* If the next token is the `throw' keyword, then we're looking at
9476 a throw-expression. */
9477 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9478 expr
= cp_parser_throw_expression (parser
);
9479 /* Otherwise, it must be that we are looking at a
9480 logical-or-expression. */
9483 /* Parse the binary expressions (logical-or-expression). */
9484 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9486 PREC_NOT_OPERATOR
, pidk
);
9487 /* If the next token is a `?' then we're actually looking at a
9488 conditional-expression. */
9489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9490 return cp_parser_question_colon_clause (parser
, expr
);
9493 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9495 /* If it's an assignment-operator, we're using the second
9497 enum tree_code assignment_operator
9498 = cp_parser_assignment_operator_opt (parser
);
9499 if (assignment_operator
!= ERROR_MARK
)
9501 bool non_constant_p
;
9503 /* Parse the right-hand side of the assignment. */
9504 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9507 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9508 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9510 /* An assignment may not appear in a
9511 constant-expression. */
9512 if (cp_parser_non_integral_constant_expression (parser
,
9514 return error_mark_node
;
9515 /* Build the assignment expression. Its default
9519 is the location of the '=' token as the
9520 caret, ranging from the start of the lhs to the
9522 loc
= make_location (loc
,
9525 expr
= build_x_modify_expr (loc
, expr
,
9526 assignment_operator
,
9528 complain_flags (decltype_p
));
9529 /* TODO: build_x_modify_expr doesn't honor the location,
9530 so we must set it here. */
9531 expr
.set_location (loc
);
9539 /* Parse an (optional) assignment-operator.
9541 assignment-operator: one of
9542 = *= /= %= += -= >>= <<= &= ^= |=
9546 assignment-operator: one of
9549 If the next token is an assignment operator, the corresponding tree
9550 code is returned, and the token is consumed. For example, for
9551 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9552 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9553 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9554 operator, ERROR_MARK is returned. */
9556 static enum tree_code
9557 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9562 /* Peek at the next token. */
9563 token
= cp_lexer_peek_token (parser
->lexer
);
9565 switch (token
->type
)
9576 op
= TRUNC_DIV_EXPR
;
9580 op
= TRUNC_MOD_EXPR
;
9612 /* Nothing else is an assignment operator. */
9616 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9617 if (op
!= ERROR_MARK
9618 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9621 /* If it was an assignment operator, consume it. */
9622 if (op
!= ERROR_MARK
)
9623 cp_lexer_consume_token (parser
->lexer
);
9628 /* Parse an expression.
9631 assignment-expression
9632 expression , assignment-expression
9634 CAST_P is true if this expression is the target of a cast.
9635 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9636 except possibly parenthesized or on the RHS of a comma (N3276).
9638 Returns a representation of the expression. */
9641 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9642 bool cast_p
, bool decltype_p
)
9644 cp_expr expression
= NULL_TREE
;
9645 location_t loc
= UNKNOWN_LOCATION
;
9649 cp_expr assignment_expression
;
9651 /* Parse the next assignment-expression. */
9652 assignment_expression
9653 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9655 /* We don't create a temporary for a call that is the immediate operand
9656 of decltype or on the RHS of a comma. But when we see a comma, we
9657 need to create a temporary for a call on the LHS. */
9658 if (decltype_p
&& !processing_template_decl
9659 && TREE_CODE (assignment_expression
) == CALL_EXPR
9660 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9661 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9662 assignment_expression
9663 = build_cplus_new (TREE_TYPE (assignment_expression
),
9664 assignment_expression
, tf_warning_or_error
);
9666 /* If this is the first assignment-expression, we can just
9669 expression
= assignment_expression
;
9672 /* Create a location with caret at the comma, ranging
9673 from the start of the LHS to the end of the RHS. */
9674 loc
= make_location (loc
,
9675 expression
.get_start (),
9676 assignment_expression
.get_finish ());
9677 expression
= build_x_compound_expr (loc
, expression
,
9678 assignment_expression
,
9679 complain_flags (decltype_p
));
9680 expression
.set_location (loc
);
9682 /* If the next token is not a comma, or we're in a fold-expression, then
9683 we are done with the expression. */
9684 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9685 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9687 /* Consume the `,'. */
9688 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9689 cp_lexer_consume_token (parser
->lexer
);
9690 /* A comma operator cannot appear in a constant-expression. */
9691 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9692 expression
= error_mark_node
;
9698 /* Parse a constant-expression.
9700 constant-expression:
9701 conditional-expression
9703 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9704 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9705 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9706 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9707 only parse a conditional-expression, otherwise parse an
9708 assignment-expression. See below for rationale. */
9711 cp_parser_constant_expression (cp_parser
* parser
,
9712 bool allow_non_constant_p
,
9713 bool *non_constant_p
,
9716 bool saved_integral_constant_expression_p
;
9717 bool saved_allow_non_integral_constant_expression_p
;
9718 bool saved_non_integral_constant_expression_p
;
9721 /* It might seem that we could simply parse the
9722 conditional-expression, and then check to see if it were
9723 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9724 one that the compiler can figure out is constant, possibly after
9725 doing some simplifications or optimizations. The standard has a
9726 precise definition of constant-expression, and we must honor
9727 that, even though it is somewhat more restrictive.
9733 is not a legal declaration, because `(2, 3)' is not a
9734 constant-expression. The `,' operator is forbidden in a
9735 constant-expression. However, GCC's constant-folding machinery
9736 will fold this operation to an INTEGER_CST for `3'. */
9738 /* Save the old settings. */
9739 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9740 saved_allow_non_integral_constant_expression_p
9741 = parser
->allow_non_integral_constant_expression_p
;
9742 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9743 /* We are now parsing a constant-expression. */
9744 parser
->integral_constant_expression_p
= true;
9745 parser
->allow_non_integral_constant_expression_p
9746 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9747 parser
->non_integral_constant_expression_p
= false;
9748 /* Although the grammar says "conditional-expression", when not STRICT_P,
9749 we parse an "assignment-expression", which also permits
9750 "throw-expression" and the use of assignment operators. In the case
9751 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9752 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9753 actually essential that we look for an assignment-expression.
9754 For example, cp_parser_initializer_clauses uses this function to
9755 determine whether a particular assignment-expression is in fact
9759 /* Parse the binary expressions (logical-or-expression). */
9760 expression
= cp_parser_binary_expression (parser
, false, false, false,
9761 PREC_NOT_OPERATOR
, NULL
);
9762 /* If the next token is a `?' then we're actually looking at
9763 a conditional-expression; otherwise we're done. */
9764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9765 expression
= cp_parser_question_colon_clause (parser
, expression
);
9768 expression
= cp_parser_assignment_expression (parser
);
9769 /* Restore the old settings. */
9770 parser
->integral_constant_expression_p
9771 = saved_integral_constant_expression_p
;
9772 parser
->allow_non_integral_constant_expression_p
9773 = saved_allow_non_integral_constant_expression_p
;
9774 if (cxx_dialect
>= cxx11
)
9776 /* Require an rvalue constant expression here; that's what our
9777 callers expect. Reference constant expressions are handled
9778 separately in e.g. cp_parser_template_argument. */
9779 tree decay
= expression
;
9780 if (TREE_TYPE (expression
)
9781 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9782 decay
= build_address (expression
);
9783 bool is_const
= potential_rvalue_constant_expression (decay
);
9784 parser
->non_integral_constant_expression_p
= !is_const
;
9785 if (!is_const
&& !allow_non_constant_p
)
9786 require_potential_rvalue_constant_expression (decay
);
9788 if (allow_non_constant_p
)
9789 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9790 parser
->non_integral_constant_expression_p
9791 = saved_non_integral_constant_expression_p
;
9796 /* Parse __builtin_offsetof.
9798 offsetof-expression:
9799 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9801 offsetof-member-designator:
9803 | offsetof-member-designator "." id-expression
9804 | offsetof-member-designator "[" expression "]"
9805 | offsetof-member-designator "->" id-expression */
9808 cp_parser_builtin_offsetof (cp_parser
*parser
)
9810 int save_ice_p
, save_non_ice_p
;
9815 location_t finish_loc
;
9817 /* We're about to accept non-integral-constant things, but will
9818 definitely yield an integral constant expression. Save and
9819 restore these values around our local parsing. */
9820 save_ice_p
= parser
->integral_constant_expression_p
;
9821 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9823 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9825 /* Consume the "__builtin_offsetof" token. */
9826 cp_lexer_consume_token (parser
->lexer
);
9827 /* Consume the opening `('. */
9828 matching_parens parens
;
9829 parens
.require_open (parser
);
9830 /* Parse the type-id. */
9831 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9832 type
= cp_parser_type_id (parser
);
9833 /* Look for the `,'. */
9834 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9835 token
= cp_lexer_peek_token (parser
->lexer
);
9837 /* Build the (type *)null that begins the traditional offsetof macro. */
9839 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9840 tf_warning_or_error
);
9842 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9843 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9844 true, &dummy
, token
->location
);
9847 token
= cp_lexer_peek_token (parser
->lexer
);
9848 switch (token
->type
)
9850 case CPP_OPEN_SQUARE
:
9851 /* offsetof-member-designator "[" expression "]" */
9852 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9857 /* offsetof-member-designator "->" identifier */
9858 expr
= grok_array_decl (token
->location
, expr
,
9859 integer_zero_node
, false);
9863 /* offsetof-member-designator "." identifier */
9864 cp_lexer_consume_token (parser
->lexer
);
9865 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9870 case CPP_CLOSE_PAREN
:
9871 /* Consume the ")" token. */
9872 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9873 cp_lexer_consume_token (parser
->lexer
);
9877 /* Error. We know the following require will fail, but
9878 that gives the proper error message. */
9879 parens
.require_close (parser
);
9880 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9881 expr
= error_mark_node
;
9887 /* Make a location of the form:
9888 __builtin_offsetof (struct s, f)
9889 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9890 with caret at the type-id, ranging from the start of the
9891 "_builtin_offsetof" token to the close paren. */
9892 loc
= make_location (loc
, start_loc
, finish_loc
);
9893 /* The result will be an INTEGER_CST, so we need to explicitly
9894 preserve the location. */
9895 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9898 parser
->integral_constant_expression_p
= save_ice_p
;
9899 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9901 expr
= expr
.maybe_add_location_wrapper ();
9905 /* Parse a trait expression.
9907 Returns a representation of the expression, the underlying type
9908 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9911 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9914 tree type1
, type2
= NULL_TREE
;
9915 bool binary
= false;
9916 bool variadic
= false;
9920 case RID_HAS_NOTHROW_ASSIGN
:
9921 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9923 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9924 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9926 case RID_HAS_NOTHROW_COPY
:
9927 kind
= CPTK_HAS_NOTHROW_COPY
;
9929 case RID_HAS_TRIVIAL_ASSIGN
:
9930 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9932 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9933 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9935 case RID_HAS_TRIVIAL_COPY
:
9936 kind
= CPTK_HAS_TRIVIAL_COPY
;
9938 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9939 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9941 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9942 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9944 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9945 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9947 case RID_IS_ABSTRACT
:
9948 kind
= CPTK_IS_ABSTRACT
;
9950 case RID_IS_AGGREGATE
:
9951 kind
= CPTK_IS_AGGREGATE
;
9953 case RID_IS_BASE_OF
:
9954 kind
= CPTK_IS_BASE_OF
;
9958 kind
= CPTK_IS_CLASS
;
9961 kind
= CPTK_IS_EMPTY
;
9964 kind
= CPTK_IS_ENUM
;
9967 kind
= CPTK_IS_FINAL
;
9969 case RID_IS_LITERAL_TYPE
:
9970 kind
= CPTK_IS_LITERAL_TYPE
;
9975 case RID_IS_POLYMORPHIC
:
9976 kind
= CPTK_IS_POLYMORPHIC
;
9978 case RID_IS_SAME_AS
:
9979 kind
= CPTK_IS_SAME_AS
;
9982 case RID_IS_STD_LAYOUT
:
9983 kind
= CPTK_IS_STD_LAYOUT
;
9985 case RID_IS_TRIVIAL
:
9986 kind
= CPTK_IS_TRIVIAL
;
9988 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9989 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9992 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9993 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9996 case RID_IS_TRIVIALLY_COPYABLE
:
9997 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10000 kind
= CPTK_IS_UNION
;
10002 case RID_UNDERLYING_TYPE
:
10003 kind
= CPTK_UNDERLYING_TYPE
;
10008 case RID_DIRECT_BASES
:
10009 kind
= CPTK_DIRECT_BASES
;
10011 case RID_IS_ASSIGNABLE
:
10012 kind
= CPTK_IS_ASSIGNABLE
;
10015 case RID_IS_CONSTRUCTIBLE
:
10016 kind
= CPTK_IS_CONSTRUCTIBLE
;
10020 gcc_unreachable ();
10023 /* Get location of initial token. */
10024 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10026 /* Consume the token. */
10027 cp_lexer_consume_token (parser
->lexer
);
10029 matching_parens parens
;
10030 parens
.require_open (parser
);
10033 type_id_in_expr_sentinel
s (parser
);
10034 type1
= cp_parser_type_id (parser
);
10037 if (type1
== error_mark_node
)
10038 return error_mark_node
;
10042 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10045 type_id_in_expr_sentinel
s (parser
);
10046 type2
= cp_parser_type_id (parser
);
10049 if (type2
== error_mark_node
)
10050 return error_mark_node
;
10054 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10056 cp_lexer_consume_token (parser
->lexer
);
10057 tree elt
= cp_parser_type_id (parser
);
10058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10060 cp_lexer_consume_token (parser
->lexer
);
10061 elt
= make_pack_expansion (elt
);
10063 if (elt
== error_mark_node
)
10064 return error_mark_node
;
10065 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10069 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10070 parens
.require_close (parser
);
10072 /* Construct a location of the form:
10073 __is_trivially_copyable(_Tp)
10074 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10075 with start == caret, finishing at the close-paren. */
10076 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10078 /* Complete the trait expression, which may mean either processing
10079 the trait expr now or saving it for template instantiation. */
10082 case CPTK_UNDERLYING_TYPE
:
10083 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10085 return cp_expr (finish_bases (type1
, false), trait_loc
);
10086 case CPTK_DIRECT_BASES
:
10087 return cp_expr (finish_bases (type1
, true), trait_loc
);
10089 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10093 /* Parse a lambda expression.
10096 lambda-introducer lambda-declarator [opt] compound-statement
10098 Returns a representation of the expression. */
10101 cp_parser_lambda_expression (cp_parser
* parser
)
10103 tree lambda_expr
= build_lambda_expr ();
10106 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10107 cp_token_position start
= 0;
10109 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10111 if (cp_unevaluated_operand
)
10113 if (!token
->error_reported
)
10115 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10116 "lambda-expression in unevaluated context");
10117 token
->error_reported
= true;
10121 else if (parser
->in_template_argument_list_p
)
10123 if (!token
->error_reported
)
10125 error_at (token
->location
, "lambda-expression in template-argument");
10126 token
->error_reported
= true;
10131 /* We may be in the middle of deferred access check. Disable
10133 push_deferring_access_checks (dk_no_deferred
);
10135 cp_parser_lambda_introducer (parser
, lambda_expr
);
10137 type
= begin_lambda_type (lambda_expr
);
10138 if (type
== error_mark_node
)
10139 return error_mark_node
;
10141 record_lambda_scope (lambda_expr
);
10143 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10144 determine_visibility (TYPE_NAME (type
));
10146 /* Now that we've started the type, add the capture fields for any
10147 explicit captures. */
10148 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10151 /* Inside the class, surrounding template-parameter-lists do not apply. */
10152 unsigned int saved_num_template_parameter_lists
10153 = parser
->num_template_parameter_lists
;
10154 unsigned char in_statement
= parser
->in_statement
;
10155 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10156 bool fully_implicit_function_template_p
10157 = parser
->fully_implicit_function_template_p
;
10158 tree implicit_template_parms
= parser
->implicit_template_parms
;
10159 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10160 bool auto_is_implicit_function_template_parm_p
10161 = parser
->auto_is_implicit_function_template_parm_p
;
10163 parser
->num_template_parameter_lists
= 0;
10164 parser
->in_statement
= 0;
10165 parser
->in_switch_statement_p
= false;
10166 parser
->fully_implicit_function_template_p
= false;
10167 parser
->implicit_template_parms
= 0;
10168 parser
->implicit_template_scope
= 0;
10169 parser
->auto_is_implicit_function_template_parm_p
= false;
10171 /* By virtue of defining a local class, a lambda expression has access to
10172 the private variables of enclosing classes. */
10174 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10176 if (ok
&& cp_parser_error_occurred (parser
))
10181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10182 && cp_parser_start_tentative_firewall (parser
))
10184 cp_parser_lambda_body (parser
, lambda_expr
);
10186 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10188 if (cp_parser_skip_to_closing_brace (parser
))
10189 cp_lexer_consume_token (parser
->lexer
);
10192 /* The capture list was built up in reverse order; fix that now. */
10193 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10194 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10197 maybe_add_lambda_conv_op (type
);
10199 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10201 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10202 parser
->in_statement
= in_statement
;
10203 parser
->in_switch_statement_p
= in_switch_statement_p
;
10204 parser
->fully_implicit_function_template_p
10205 = fully_implicit_function_template_p
;
10206 parser
->implicit_template_parms
= implicit_template_parms
;
10207 parser
->implicit_template_scope
= implicit_template_scope
;
10208 parser
->auto_is_implicit_function_template_parm_p
10209 = auto_is_implicit_function_template_parm_p
;
10212 /* This field is only used during parsing of the lambda. */
10213 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10215 /* This lambda shouldn't have any proxies left at this point. */
10216 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10217 /* And now that we're done, push proxies for an enclosing lambda. */
10218 insert_pending_capture_proxies ();
10221 lambda_expr
= build_lambda_object (lambda_expr
);
10223 lambda_expr
= error_mark_node
;
10225 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10227 pop_deferring_access_checks ();
10229 return lambda_expr
;
10232 /* Parse the beginning of a lambda expression.
10235 [ lambda-capture [opt] ]
10237 LAMBDA_EXPR is the current representation of the lambda expression. */
10240 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10242 /* Need commas after the first capture. */
10245 /* Eat the leading `['. */
10246 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10248 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10250 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10251 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10252 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10257 cp_lexer_consume_token (parser
->lexer
);
10261 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10263 cp_token
* capture_token
;
10265 tree capture_init_expr
;
10266 cp_id_kind idk
= CP_ID_KIND_NONE
;
10267 bool explicit_init_p
= false;
10269 enum capture_kind_type
10274 enum capture_kind_type capture_kind
= BY_COPY
;
10276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10278 error ("expected end of capture-list");
10285 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10287 /* Possibly capture `this'. */
10288 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10290 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10291 if (cxx_dialect
< cxx2a
10292 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10293 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10294 "with by-copy capture default");
10295 cp_lexer_consume_token (parser
->lexer
);
10296 add_capture (lambda_expr
,
10297 /*id=*/this_identifier
,
10298 /*initializer=*/finish_this_expr (),
10299 /*by_reference_p=*/true,
10304 /* Possibly capture `*this'. */
10305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10306 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10308 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10309 if (cxx_dialect
< cxx17
)
10310 pedwarn (loc
, 0, "%<*this%> capture only available with "
10311 "-std=c++17 or -std=gnu++17");
10312 cp_lexer_consume_token (parser
->lexer
);
10313 cp_lexer_consume_token (parser
->lexer
);
10314 add_capture (lambda_expr
,
10315 /*id=*/this_identifier
,
10316 /*initializer=*/finish_this_expr (),
10317 /*by_reference_p=*/false,
10322 /* Remember whether we want to capture as a reference or not. */
10323 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10325 capture_kind
= BY_REFERENCE
;
10326 cp_lexer_consume_token (parser
->lexer
);
10329 /* Get the identifier. */
10330 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10331 capture_id
= cp_parser_identifier (parser
);
10333 if (capture_id
== error_mark_node
)
10334 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10335 delimiters, but I modified this to stop on unnested ']' as well. It
10336 was already changed to stop on unnested '}', so the
10337 "closing_parenthesis" name is no more misleading with my change. */
10339 cp_parser_skip_to_closing_parenthesis (parser
,
10340 /*recovering=*/true,
10342 /*consume_paren=*/true);
10346 /* Find the initializer for this capture. */
10347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10348 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10349 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10351 bool direct
, non_constant
;
10352 /* An explicit initializer exists. */
10353 if (cxx_dialect
< cxx14
)
10354 pedwarn (input_location
, 0,
10355 "lambda capture initializers "
10356 "only available with -std=c++14 or -std=gnu++14");
10357 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10359 explicit_init_p
= true;
10360 if (capture_init_expr
== NULL_TREE
)
10362 error ("empty initializer for lambda init-capture");
10363 capture_init_expr
= error_mark_node
;
10368 const char* error_msg
;
10370 /* Turn the identifier into an id-expression. */
10372 = cp_parser_lookup_name_simple (parser
, capture_id
,
10373 capture_token
->location
);
10375 if (capture_init_expr
== error_mark_node
)
10377 unqualified_name_lookup_error (capture_id
);
10380 else if (DECL_P (capture_init_expr
)
10381 && (!VAR_P (capture_init_expr
)
10382 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10384 error_at (capture_token
->location
,
10385 "capture of non-variable %qD ",
10386 capture_init_expr
);
10387 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10388 "%q#D declared here", capture_init_expr
);
10391 if (VAR_P (capture_init_expr
)
10392 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10394 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10395 "%qD with non-automatic storage duration",
10396 capture_init_expr
))
10397 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10398 "%q#D declared here", capture_init_expr
);
10403 = finish_id_expression
10408 /*integral_constant_expression_p=*/false,
10409 /*allow_non_integral_constant_expression_p=*/false,
10410 /*non_integral_constant_expression_p=*/NULL
,
10411 /*template_p=*/false,
10413 /*address_p=*/false,
10414 /*template_arg_p=*/false,
10416 capture_token
->location
);
10418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10420 cp_lexer_consume_token (parser
->lexer
);
10421 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10425 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10426 && !explicit_init_p
)
10428 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10429 && capture_kind
== BY_COPY
)
10430 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10431 "of %qD redundant with by-copy capture default",
10433 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10434 && capture_kind
== BY_REFERENCE
)
10435 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10436 "capture of %qD redundant with by-reference capture "
10437 "default", capture_id
);
10440 add_capture (lambda_expr
,
10443 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10446 /* If there is any qualification still in effect, clear it
10447 now; we will be starting fresh with the next capture. */
10448 parser
->scope
= NULL_TREE
;
10449 parser
->qualifying_scope
= NULL_TREE
;
10450 parser
->object_scope
= NULL_TREE
;
10453 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10456 /* Parse the (optional) middle of a lambda expression.
10459 < template-parameter-list [opt] >
10460 ( parameter-declaration-clause [opt] )
10461 attribute-specifier [opt]
10462 decl-specifier-seq [opt]
10463 exception-specification [opt]
10464 lambda-return-type-clause [opt]
10466 LAMBDA_EXPR is the current representation of the lambda expression. */
10469 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10471 /* 5.1.1.4 of the standard says:
10472 If a lambda-expression does not include a lambda-declarator, it is as if
10473 the lambda-declarator were ().
10474 This means an empty parameter list, no attributes, and no exception
10476 tree param_list
= void_list_node
;
10477 tree attributes
= NULL_TREE
;
10478 tree exception_spec
= NULL_TREE
;
10479 tree template_param_list
= NULL_TREE
;
10480 tree tx_qual
= NULL_TREE
;
10481 tree return_type
= NULL_TREE
;
10482 cp_decl_specifier_seq lambda_specs
;
10483 clear_decl_specs (&lambda_specs
);
10485 /* The template-parameter-list is optional, but must begin with
10486 an opening angle if present. */
10487 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10489 if (cxx_dialect
< cxx14
)
10490 pedwarn (parser
->lexer
->next_token
->location
, 0,
10491 "lambda templates are only available with "
10492 "-std=c++14 or -std=gnu++14");
10493 else if (cxx_dialect
< cxx2a
)
10494 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10495 "lambda templates are only available with "
10496 "-std=c++2a or -std=gnu++2a");
10498 cp_lexer_consume_token (parser
->lexer
);
10500 template_param_list
= cp_parser_template_parameter_list (parser
);
10502 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10504 /* We just processed one more parameter list. */
10505 ++parser
->num_template_parameter_lists
;
10508 /* The parameter-declaration-clause is optional (unless
10509 template-parameter-list was given), but must begin with an
10510 opening parenthesis if present. */
10511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10513 matching_parens parens
;
10514 parens
.consume_open (parser
);
10516 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10518 /* Parse parameters. */
10519 param_list
= cp_parser_parameter_declaration_clause (parser
);
10521 /* Default arguments shall not be specified in the
10522 parameter-declaration-clause of a lambda-declarator. */
10523 if (cxx_dialect
< cxx14
)
10524 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10525 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10526 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10527 "default argument specified for lambda parameter");
10529 parens
.require_close (parser
);
10531 attributes
= cp_parser_attributes_opt (parser
);
10533 /* In the decl-specifier-seq of the lambda-declarator, each
10534 decl-specifier shall either be mutable or constexpr. */
10535 int declares_class_or_enum
;
10536 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10537 cp_parser_decl_specifier_seq (parser
,
10538 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10539 &lambda_specs
, &declares_class_or_enum
);
10540 if (lambda_specs
.storage_class
== sc_mutable
)
10542 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10543 if (lambda_specs
.conflicting_specifiers_p
)
10544 error_at (lambda_specs
.locations
[ds_storage_class
],
10545 "duplicate %<mutable%>");
10548 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10550 /* Parse optional exception specification. */
10551 exception_spec
= cp_parser_exception_specification_opt (parser
);
10553 /* Parse optional trailing return type. */
10554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10556 cp_lexer_consume_token (parser
->lexer
);
10557 return_type
= cp_parser_trailing_type_id (parser
);
10560 /* The function parameters must be in scope all the way until after the
10561 trailing-return-type in case of decltype. */
10562 pop_bindings_and_leave_scope ();
10564 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10565 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10567 /* Create the function call operator.
10569 Messing with declarators like this is no uglier than building up the
10570 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10573 cp_decl_specifier_seq return_type_specs
;
10574 cp_declarator
* declarator
;
10579 clear_decl_specs (&return_type_specs
);
10581 return_type_specs
.type
= return_type
;
10583 /* Maybe we will deduce the return type later. */
10584 return_type_specs
.type
= make_auto ();
10586 if (lambda_specs
.locations
[ds_constexpr
])
10588 if (cxx_dialect
>= cxx17
)
10589 return_type_specs
.locations
[ds_constexpr
]
10590 = lambda_specs
.locations
[ds_constexpr
];
10592 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10593 "lambda only available with -std=c++17 or -std=gnu++17");
10596 p
= obstack_alloc (&declarator_obstack
, 0);
10598 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10600 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10601 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10602 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10603 VIRT_SPEC_UNSPECIFIED
,
10607 /*late_return_type=*/NULL_TREE
,
10608 /*requires_clause*/NULL_TREE
);
10609 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10611 fco
= grokmethod (&return_type_specs
,
10614 if (fco
!= error_mark_node
)
10616 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10617 DECL_ARTIFICIAL (fco
) = 1;
10618 /* Give the object parameter a different name. */
10619 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10621 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10623 if (template_param_list
)
10625 fco
= finish_member_template_decl (fco
);
10626 finish_template_decl (template_param_list
);
10627 --parser
->num_template_parameter_lists
;
10629 else if (parser
->fully_implicit_function_template_p
)
10630 fco
= finish_fully_implicit_template (parser
, fco
);
10632 finish_member_declaration (fco
);
10634 obstack_free (&declarator_obstack
, p
);
10636 return (fco
!= error_mark_node
);
10640 /* Parse the body of a lambda expression, which is simply
10644 but which requires special handling.
10645 LAMBDA_EXPR is the current representation of the lambda expression. */
10648 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10650 bool nested
= (current_function_decl
!= NULL_TREE
);
10651 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10652 bool in_function_body
= parser
->in_function_body
;
10655 push_function_context ();
10657 /* Still increment function_depth so that we don't GC in the
10658 middle of an expression. */
10661 vec
<tree
> omp_privatization_save
;
10662 save_omp_privatization_clauses (omp_privatization_save
);
10663 /* Clear this in case we're in the middle of a default argument. */
10664 parser
->local_variables_forbidden_p
= false;
10665 parser
->in_function_body
= true;
10668 local_specialization_stack
s (lss_copy
);
10669 tree fco
= lambda_function (lambda_expr
);
10670 tree body
= start_lambda_function (fco
, lambda_expr
);
10671 matching_braces braces
;
10673 if (braces
.require_open (parser
))
10675 tree compound_stmt
= begin_compound_stmt (0);
10677 /* Originally C++11 required us to peek for 'return expr'; and
10678 process it specially here to deduce the return type. N3638
10679 removed the need for that. */
10681 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10682 cp_parser_label_declaration (parser
);
10683 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10684 braces
.require_close (parser
);
10686 finish_compound_stmt (compound_stmt
);
10689 finish_lambda_function (body
);
10692 restore_omp_privatization_clauses (omp_privatization_save
);
10693 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10694 parser
->in_function_body
= in_function_body
;
10696 pop_function_context();
10701 /* Statements [gram.stmt.stmt] */
10703 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10706 add_debug_begin_stmt (location_t loc
)
10708 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10710 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10711 /* A concept is never expanded normally. */
10714 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10715 SET_EXPR_LOCATION (stmt
, loc
);
10719 /* Parse a statement.
10723 expression-statement
10725 selection-statement
10726 iteration-statement
10728 declaration-statement
10735 attribute-specifier-seq (opt) expression-statement
10736 attribute-specifier-seq (opt) compound-statement
10737 attribute-specifier-seq (opt) selection-statement
10738 attribute-specifier-seq (opt) iteration-statement
10739 attribute-specifier-seq (opt) jump-statement
10740 declaration-statement
10741 attribute-specifier-seq (opt) try-block
10744 expression-statement
10752 IN_COMPOUND is true when the statement is nested inside a
10753 cp_parser_compound_statement; this matters for certain pragmas.
10755 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10756 is a (possibly labeled) if statement which is not enclosed in braces
10757 and has an else clause. This is used to implement -Wparentheses.
10759 CHAIN is a vector of if-else-if conditions. */
10762 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10763 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10764 location_t
*loc_after_labels
)
10766 tree statement
, std_attrs
= NULL_TREE
;
10768 location_t statement_location
, attrs_location
;
10773 /* There is no statement yet. */
10774 statement
= NULL_TREE
;
10776 saved_token_sentinel
saved_tokens (parser
->lexer
);
10777 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10778 if (c_dialect_objc ())
10779 /* In obj-c++, seeing '[[' might be the either the beginning of
10780 c++11 attributes, or a nested objc-message-expression. So
10781 let's parse the c++11 attributes tentatively. */
10782 cp_parser_parse_tentatively (parser
);
10783 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10784 if (c_dialect_objc ())
10786 if (!cp_parser_parse_definitely (parser
))
10787 std_attrs
= NULL_TREE
;
10790 /* Peek at the next token. */
10791 token
= cp_lexer_peek_token (parser
->lexer
);
10792 /* Remember the location of the first token in the statement. */
10793 statement_location
= token
->location
;
10794 add_debug_begin_stmt (statement_location
);
10795 /* If this is a keyword, then that will often determine what kind of
10796 statement we have. */
10797 if (token
->type
== CPP_KEYWORD
)
10799 enum rid keyword
= token
->keyword
;
10805 /* Looks like a labeled-statement with a case label.
10806 Parse the label, and then use tail recursion to parse
10808 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10809 in_compound
= false;
10814 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10820 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10827 statement
= cp_parser_jump_statement (parser
);
10830 /* Objective-C++ exception-handling constructs. */
10833 case RID_AT_FINALLY
:
10834 case RID_AT_SYNCHRONIZED
:
10836 statement
= cp_parser_objc_statement (parser
);
10840 statement
= cp_parser_try_block (parser
);
10843 case RID_NAMESPACE
:
10844 /* This must be a namespace alias definition. */
10845 cp_parser_declaration_statement (parser
);
10848 case RID_TRANSACTION_ATOMIC
:
10849 case RID_TRANSACTION_RELAXED
:
10850 case RID_SYNCHRONIZED
:
10851 case RID_ATOMIC_NOEXCEPT
:
10852 case RID_ATOMIC_CANCEL
:
10853 statement
= cp_parser_transaction (parser
, token
);
10855 case RID_TRANSACTION_CANCEL
:
10856 statement
= cp_parser_transaction_cancel (parser
);
10860 /* It might be a keyword like `int' that can start a
10861 declaration-statement. */
10865 else if (token
->type
== CPP_NAME
)
10867 /* If the next token is a `:', then we are looking at a
10868 labeled-statement. */
10869 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10870 if (token
->type
== CPP_COLON
)
10872 /* Looks like a labeled-statement with an ordinary label.
10873 Parse the label, and then use tail recursion to parse
10876 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10877 in_compound
= false;
10881 /* Anything that starts with a `{' must be a compound-statement. */
10882 else if (token
->type
== CPP_OPEN_BRACE
)
10883 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10884 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10885 a statement all its own. */
10886 else if (token
->type
== CPP_PRAGMA
)
10888 /* Only certain OpenMP pragmas are attached to statements, and thus
10889 are considered statements themselves. All others are not. In
10890 the context of a compound, accept the pragma as a "statement" and
10891 return so that we can check for a close brace. Otherwise we
10892 require a real statement and must go back and read one. */
10894 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10895 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10899 else if (token
->type
== CPP_EOF
)
10901 cp_parser_error (parser
, "expected statement");
10905 /* Everything else must be a declaration-statement or an
10906 expression-statement. Try for the declaration-statement
10907 first, unless we are looking at a `;', in which case we know that
10908 we have an expression-statement. */
10911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10913 if (std_attrs
!= NULL_TREE
)
10915 /* Attributes should be parsed as part of the the
10916 declaration, so let's un-parse them. */
10917 saved_tokens
.rollback();
10918 std_attrs
= NULL_TREE
;
10921 cp_parser_parse_tentatively (parser
);
10922 /* Try to parse the declaration-statement. */
10923 cp_parser_declaration_statement (parser
);
10924 /* If that worked, we're done. */
10925 if (cp_parser_parse_definitely (parser
))
10928 /* All preceding labels have been parsed at this point. */
10929 if (loc_after_labels
!= NULL
)
10930 *loc_after_labels
= statement_location
;
10932 /* Look for an expression-statement instead. */
10933 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10935 /* Handle [[fallthrough]];. */
10936 if (attribute_fallthrough_p (std_attrs
))
10938 /* The next token after the fallthrough attribute is ';'. */
10939 if (statement
== NULL_TREE
)
10941 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10942 statement
= build_call_expr_internal_loc (statement_location
,
10944 void_type_node
, 0);
10945 finish_expr_stmt (statement
);
10948 warning_at (statement_location
, OPT_Wattributes
,
10949 "%<fallthrough%> attribute not followed by %<;%>");
10950 std_attrs
= NULL_TREE
;
10954 /* Set the line number for the statement. */
10955 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10956 SET_EXPR_LOCATION (statement
, statement_location
);
10958 /* Allow "[[fallthrough]];", but warn otherwise. */
10959 if (std_attrs
!= NULL_TREE
)
10960 warning_at (attrs_location
,
10962 "attributes at the beginning of statement are ignored");
10965 /* Append ATTR to attribute list ATTRS. */
10968 attr_chainon (tree attrs
, tree attr
)
10970 if (attrs
== error_mark_node
)
10971 return error_mark_node
;
10972 if (attr
== error_mark_node
)
10973 return error_mark_node
;
10974 return chainon (attrs
, attr
);
10977 /* Parse the label for a labeled-statement, i.e.
10980 case constant-expression :
10984 case constant-expression ... constant-expression : statement
10986 When a label is parsed without errors, the label is added to the
10987 parse tree by the finish_* functions, so this function doesn't
10988 have to return the label. */
10991 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10994 tree label
= NULL_TREE
;
10995 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10997 /* The next token should be an identifier. */
10998 token
= cp_lexer_peek_token (parser
->lexer
);
10999 if (token
->type
!= CPP_NAME
11000 && token
->type
!= CPP_KEYWORD
)
11002 cp_parser_error (parser
, "expected labeled-statement");
11006 /* Remember whether this case or a user-defined label is allowed to fall
11008 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11010 parser
->colon_corrects_to_scope_p
= false;
11011 switch (token
->keyword
)
11015 tree expr
, expr_hi
;
11016 cp_token
*ellipsis
;
11018 /* Consume the `case' token. */
11019 cp_lexer_consume_token (parser
->lexer
);
11020 /* Parse the constant-expression. */
11021 expr
= cp_parser_constant_expression (parser
);
11022 if (check_for_bare_parameter_packs (expr
))
11023 expr
= error_mark_node
;
11025 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11026 if (ellipsis
->type
== CPP_ELLIPSIS
)
11028 /* Consume the `...' token. */
11029 cp_lexer_consume_token (parser
->lexer
);
11030 expr_hi
= cp_parser_constant_expression (parser
);
11031 if (check_for_bare_parameter_packs (expr_hi
))
11032 expr_hi
= error_mark_node
;
11034 /* We don't need to emit warnings here, as the common code
11035 will do this for us. */
11038 expr_hi
= NULL_TREE
;
11040 if (parser
->in_switch_statement_p
)
11042 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11043 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11044 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11047 error_at (token
->location
,
11048 "case label %qE not within a switch statement",
11054 /* Consume the `default' token. */
11055 cp_lexer_consume_token (parser
->lexer
);
11057 if (parser
->in_switch_statement_p
)
11059 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11060 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11061 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11064 error_at (token
->location
, "case label not within a switch statement");
11068 /* Anything else must be an ordinary label. */
11069 label
= finish_label_stmt (cp_parser_identifier (parser
));
11070 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11071 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11075 /* Require the `:' token. */
11076 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11078 /* An ordinary label may optionally be followed by attributes.
11079 However, this is only permitted if the attributes are then
11080 followed by a semicolon. This is because, for backward
11081 compatibility, when parsing
11082 lab: __attribute__ ((unused)) int i;
11083 we want the attribute to attach to "i", not "lab". */
11084 if (label
!= NULL_TREE
11085 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11088 cp_parser_parse_tentatively (parser
);
11089 attrs
= cp_parser_gnu_attributes_opt (parser
);
11090 if (attrs
== NULL_TREE
11091 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11092 cp_parser_abort_tentative_parse (parser
);
11093 else if (!cp_parser_parse_definitely (parser
))
11096 attributes
= attr_chainon (attributes
, attrs
);
11099 if (attributes
!= NULL_TREE
)
11100 cplus_decl_attributes (&label
, attributes
, 0);
11102 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11105 /* Parse an expression-statement.
11107 expression-statement:
11110 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11111 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11112 indicates whether this expression-statement is part of an
11113 expression statement. */
11116 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11118 tree statement
= NULL_TREE
;
11119 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11120 location_t loc
= token
->location
;
11122 /* There might be attribute fallthrough. */
11123 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11125 /* If the next token is a ';', then there is no expression
11127 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11129 statement
= cp_parser_expression (parser
);
11130 if (statement
== error_mark_node
11131 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11133 cp_parser_skip_to_end_of_block_or_statement (parser
);
11134 return error_mark_node
;
11138 /* Handle [[fallthrough]];. */
11139 if (attribute_fallthrough_p (attr
))
11141 /* The next token after the fallthrough attribute is ';'. */
11142 if (statement
== NULL_TREE
)
11143 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11144 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11145 void_type_node
, 0);
11147 warning_at (loc
, OPT_Wattributes
,
11148 "%<fallthrough%> attribute not followed by %<;%>");
11152 /* Allow "[[fallthrough]];", but warn otherwise. */
11153 if (attr
!= NULL_TREE
)
11154 warning_at (loc
, OPT_Wattributes
,
11155 "attributes at the beginning of statement are ignored");
11157 /* Give a helpful message for "A<T>::type t;" and the like. */
11158 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11159 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11161 if (TREE_CODE (statement
) == SCOPE_REF
)
11162 error_at (token
->location
, "need %<typename%> before %qE because "
11163 "%qT is a dependent scope",
11164 statement
, TREE_OPERAND (statement
, 0));
11165 else if (is_overloaded_fn (statement
)
11166 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11169 tree fn
= get_first_fn (statement
);
11170 error_at (token
->location
,
11171 "%<%T::%D%> names the constructor, not the type",
11172 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11176 /* Consume the final `;'. */
11177 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11179 if (in_statement_expr
11180 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11181 /* This is the final expression statement of a statement
11183 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11184 else if (statement
)
11185 statement
= finish_expr_stmt (statement
);
11190 /* Parse a compound-statement.
11192 compound-statement:
11193 { statement-seq [opt] }
11197 compound-statement:
11198 { label-declaration-seq [opt] statement-seq [opt] }
11200 label-declaration-seq:
11202 label-declaration-seq label-declaration
11204 Returns a tree representing the statement. */
11207 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11208 int bcs_flags
, bool function_body
)
11210 tree compound_stmt
;
11211 matching_braces braces
;
11213 /* Consume the `{'. */
11214 if (!braces
.require_open (parser
))
11215 return error_mark_node
;
11216 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11217 && !function_body
&& cxx_dialect
< cxx14
)
11218 pedwarn (input_location
, OPT_Wpedantic
,
11219 "compound-statement in %<constexpr%> function");
11220 /* Begin the compound-statement. */
11221 compound_stmt
= begin_compound_stmt (bcs_flags
);
11222 /* If the next keyword is `__label__' we have a label declaration. */
11223 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11224 cp_parser_label_declaration (parser
);
11225 /* Parse an (optional) statement-seq. */
11226 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11227 /* Finish the compound-statement. */
11228 finish_compound_stmt (compound_stmt
);
11229 /* Consume the `}'. */
11230 braces
.require_close (parser
);
11232 return compound_stmt
;
11235 /* Parse an (optional) statement-seq.
11239 statement-seq [opt] statement */
11242 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11244 /* Scan statements until there aren't any more. */
11247 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11249 /* If we are looking at a `}', then we have run out of
11250 statements; the same is true if we have reached the end
11251 of file, or have stumbled upon a stray '@end'. */
11252 if (token
->type
== CPP_CLOSE_BRACE
11253 || token
->type
== CPP_EOF
11254 || token
->type
== CPP_PRAGMA_EOL
11255 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11258 /* If we are in a compound statement and find 'else' then
11259 something went wrong. */
11260 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11262 if (parser
->in_statement
& IN_IF_STMT
)
11266 token
= cp_lexer_consume_token (parser
->lexer
);
11267 error_at (token
->location
, "%<else%> without a previous %<if%>");
11271 /* Parse the statement. */
11272 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11276 /* Return true if we're looking at (init; cond), false otherwise. */
11279 cp_parser_init_statement_p (cp_parser
*parser
)
11281 /* Save tokens so that we can put them back. */
11282 cp_lexer_save_tokens (parser
->lexer
);
11284 /* Look for ';' that is not nested in () or {}. */
11285 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11286 /*recovering=*/false,
11288 /*consume_paren=*/false);
11290 /* Roll back the tokens we skipped. */
11291 cp_lexer_rollback_tokens (parser
->lexer
);
11296 /* Parse a selection-statement.
11298 selection-statement:
11299 if ( init-statement [opt] condition ) statement
11300 if ( init-statement [opt] condition ) statement else statement
11301 switch ( init-statement [opt] condition ) statement
11303 Returns the new IF_STMT or SWITCH_STMT.
11305 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11306 is a (possibly labeled) if statement which is not enclosed in
11307 braces and has an else clause. This is used to implement
11310 CHAIN is a vector of if-else-if conditions. This is used to implement
11311 -Wduplicated-cond. */
11314 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11319 token_indent_info guard_tinfo
;
11324 /* Peek at the next token. */
11325 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11326 guard_tinfo
= get_token_indent_info (token
);
11328 /* See what kind of keyword it is. */
11329 keyword
= token
->keyword
;
11339 if (keyword
== RID_IF
11340 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11344 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11345 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11346 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11347 "with -std=c++17 or -std=gnu++17");
11350 /* Look for the `('. */
11351 matching_parens parens
;
11352 if (!parens
.require_open (parser
))
11354 cp_parser_skip_to_end_of_statement (parser
);
11355 return error_mark_node
;
11358 /* Begin the selection-statement. */
11359 if (keyword
== RID_IF
)
11361 statement
= begin_if_stmt ();
11362 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11365 statement
= begin_switch_stmt ();
11367 /* Parse the optional init-statement. */
11368 if (cp_parser_init_statement_p (parser
))
11371 if (cxx_dialect
< cxx17
)
11372 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11373 "init-statement in selection statements only available "
11374 "with -std=c++17 or -std=gnu++17");
11375 cp_parser_init_statement (parser
, &decl
);
11378 /* Parse the condition. */
11379 condition
= cp_parser_condition (parser
);
11380 /* Look for the `)'. */
11381 if (!parens
.require_close (parser
))
11382 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11383 /*consume_paren=*/true);
11385 if (keyword
== RID_IF
)
11388 unsigned char in_statement
;
11390 /* Add the condition. */
11391 condition
= finish_if_stmt_cond (condition
, statement
);
11393 if (warn_duplicated_cond
)
11394 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11397 /* Parse the then-clause. */
11398 in_statement
= parser
->in_statement
;
11399 parser
->in_statement
|= IN_IF_STMT
;
11401 /* Outside a template, the non-selected branch of a constexpr
11402 if is a 'discarded statement', i.e. unevaluated. */
11403 bool was_discarded
= in_discarded_stmt
;
11404 bool discard_then
= (cx
&& !processing_template_decl
11405 && integer_zerop (condition
));
11408 in_discarded_stmt
= true;
11409 ++c_inhibit_evaluation_warnings
;
11412 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11415 parser
->in_statement
= in_statement
;
11417 finish_then_clause (statement
);
11421 THEN_CLAUSE (statement
) = NULL_TREE
;
11422 in_discarded_stmt
= was_discarded
;
11423 --c_inhibit_evaluation_warnings
;
11426 /* If the next token is `else', parse the else-clause. */
11427 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11430 bool discard_else
= (cx
&& !processing_template_decl
11431 && integer_nonzerop (condition
));
11434 in_discarded_stmt
= true;
11435 ++c_inhibit_evaluation_warnings
;
11439 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11440 /* Consume the `else' keyword. */
11441 cp_lexer_consume_token (parser
->lexer
);
11442 if (warn_duplicated_cond
)
11444 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11448 /* We've got "if (COND) else if (COND2)". Start
11449 the condition chain and add COND as the first
11451 chain
= new vec
<tree
> ();
11452 if (!CONSTANT_CLASS_P (condition
)
11453 && !TREE_SIDE_EFFECTS (condition
))
11455 /* Wrap it in a NOP_EXPR so that we can set the
11456 location of the condition. */
11457 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11459 SET_EXPR_LOCATION (e
, token
->location
);
11460 chain
->safe_push (e
);
11463 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11466 /* This is if-else without subsequent if. Zap the
11467 condition chain; we would have already warned at
11473 begin_else_clause (statement
);
11474 /* Parse the else-clause. */
11475 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11476 guard_tinfo
, chain
);
11478 finish_else_clause (statement
);
11480 /* If we are currently parsing a then-clause, then
11481 IF_P will not be NULL. We set it to true to
11482 indicate that this if statement has an else clause.
11483 This may trigger the Wparentheses warning below
11484 when we get back up to the parent if statement. */
11490 ELSE_CLAUSE (statement
) = NULL_TREE
;
11491 in_discarded_stmt
= was_discarded
;
11492 --c_inhibit_evaluation_warnings
;
11497 /* This if statement does not have an else clause. If
11498 NESTED_IF is true, then the then-clause has an if
11499 statement which does have an else clause. We warn
11500 about the potential ambiguity. */
11502 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11503 "suggest explicit braces to avoid ambiguous"
11505 if (warn_duplicated_cond
)
11507 /* We don't need the condition chain anymore. */
11513 /* Now we're all done with the if-statement. */
11514 finish_if_stmt (statement
);
11518 bool in_switch_statement_p
;
11519 unsigned char in_statement
;
11521 /* Add the condition. */
11522 finish_switch_cond (condition
, statement
);
11524 /* Parse the body of the switch-statement. */
11525 in_switch_statement_p
= parser
->in_switch_statement_p
;
11526 in_statement
= parser
->in_statement
;
11527 parser
->in_switch_statement_p
= true;
11528 parser
->in_statement
|= IN_SWITCH_STMT
;
11529 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11531 parser
->in_switch_statement_p
= in_switch_statement_p
;
11532 parser
->in_statement
= in_statement
;
11534 /* Now we're all done with the switch-statement. */
11535 finish_switch_stmt (statement
);
11543 cp_parser_error (parser
, "expected selection-statement");
11544 return error_mark_node
;
11548 /* Parse a condition.
11552 type-specifier-seq declarator = initializer-clause
11553 type-specifier-seq declarator braced-init-list
11558 type-specifier-seq declarator asm-specification [opt]
11559 attributes [opt] = assignment-expression
11561 Returns the expression that should be tested. */
11564 cp_parser_condition (cp_parser
* parser
)
11566 cp_decl_specifier_seq type_specifiers
;
11567 const char *saved_message
;
11568 int declares_class_or_enum
;
11570 /* Try the declaration first. */
11571 cp_parser_parse_tentatively (parser
);
11572 /* New types are not allowed in the type-specifier-seq for a
11574 saved_message
= parser
->type_definition_forbidden_message
;
11575 parser
->type_definition_forbidden_message
11576 = G_("types may not be defined in conditions");
11577 /* Parse the type-specifier-seq. */
11578 cp_parser_decl_specifier_seq (parser
,
11579 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11581 &declares_class_or_enum
);
11582 /* Restore the saved message. */
11583 parser
->type_definition_forbidden_message
= saved_message
;
11584 /* If all is well, we might be looking at a declaration. */
11585 if (!cp_parser_error_occurred (parser
))
11588 tree asm_specification
;
11590 cp_declarator
*declarator
;
11591 tree initializer
= NULL_TREE
;
11593 /* Parse the declarator. */
11594 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11595 /*ctor_dtor_or_conv_p=*/NULL
,
11596 /*parenthesized_p=*/NULL
,
11597 /*member_p=*/false,
11598 /*friend_p=*/false);
11599 /* Parse the attributes. */
11600 attributes
= cp_parser_attributes_opt (parser
);
11601 /* Parse the asm-specification. */
11602 asm_specification
= cp_parser_asm_specification_opt (parser
);
11603 /* If the next token is not an `=' or '{', then we might still be
11604 looking at an expression. For example:
11608 looks like a decl-specifier-seq and a declarator -- but then
11609 there is no `=', so this is an expression. */
11610 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11611 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11612 cp_parser_simulate_error (parser
);
11614 /* If we did see an `=' or '{', then we are looking at a declaration
11616 if (cp_parser_parse_definitely (parser
))
11619 bool non_constant_p
;
11620 int flags
= LOOKUP_ONLYCONVERTING
;
11622 /* Create the declaration. */
11623 decl
= start_decl (declarator
, &type_specifiers
,
11624 /*initialized_p=*/true,
11625 attributes
, /*prefix_attributes=*/NULL_TREE
,
11628 /* Parse the initializer. */
11629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11631 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11632 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11637 /* Consume the `='. */
11638 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11639 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11641 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11644 /* Process the initializer. */
11645 cp_finish_decl (decl
,
11646 initializer
, !non_constant_p
,
11651 pop_scope (pushed_scope
);
11653 return convert_from_reference (decl
);
11656 /* If we didn't even get past the declarator successfully, we are
11657 definitely not looking at a declaration. */
11659 cp_parser_abort_tentative_parse (parser
);
11661 /* Otherwise, we are looking at an expression. */
11662 return cp_parser_expression (parser
);
11665 /* Parses a for-statement or range-for-statement until the closing ')',
11669 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11671 tree init
, scope
, decl
;
11674 /* Begin the for-statement. */
11675 scope
= begin_for_scope (&init
);
11677 /* Parse the initialization. */
11678 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11681 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11683 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11687 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11688 unsigned short unroll
)
11690 /* Normal for loop */
11691 tree condition
= NULL_TREE
;
11692 tree expression
= NULL_TREE
;
11695 stmt
= begin_for_stmt (scope
, init
);
11696 /* The init-statement has already been parsed in
11697 cp_parser_init_statement, so no work is needed here. */
11698 finish_init_stmt (stmt
);
11700 /* If there's a condition, process it. */
11701 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11702 condition
= cp_parser_condition (parser
);
11705 cp_parser_error (parser
, "missing loop condition in loop with "
11706 "%<GCC ivdep%> pragma");
11707 condition
= error_mark_node
;
11711 cp_parser_error (parser
, "missing loop condition in loop with "
11712 "%<GCC unroll%> pragma");
11713 condition
= error_mark_node
;
11715 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11716 /* Look for the `;'. */
11717 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11719 /* If there's an expression, process it. */
11720 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11721 expression
= cp_parser_expression (parser
);
11722 finish_for_expr (expression
, stmt
);
11727 /* Tries to parse a range-based for-statement:
11730 decl-specifier-seq declarator : expression
11732 The decl-specifier-seq declarator and the `:' are already parsed by
11733 cp_parser_init_statement. If processing_template_decl it returns a
11734 newly created RANGE_FOR_STMT; if not, it is converted to a
11735 regular FOR_STMT. */
11738 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11739 bool ivdep
, unsigned short unroll
)
11741 tree stmt
, range_expr
;
11742 auto_vec
<cxx_binding
*, 16> bindings
;
11743 auto_vec
<tree
, 16> names
;
11744 tree decomp_first_name
= NULL_TREE
;
11745 unsigned int decomp_cnt
= 0;
11747 /* Get the range declaration momentarily out of the way so that
11748 the range expression doesn't clash with it. */
11749 if (range_decl
!= error_mark_node
)
11751 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11753 tree v
= DECL_VALUE_EXPR (range_decl
);
11754 /* For decomposition declaration get all of the corresponding
11755 declarations out of the way. */
11756 if (TREE_CODE (v
) == ARRAY_REF
11757 && VAR_P (TREE_OPERAND (v
, 0))
11758 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11760 tree d
= range_decl
;
11761 range_decl
= TREE_OPERAND (v
, 0);
11762 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11763 decomp_first_name
= d
;
11764 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11766 tree name
= DECL_NAME (d
);
11767 names
.safe_push (name
);
11768 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11769 IDENTIFIER_BINDING (name
)
11770 = IDENTIFIER_BINDING (name
)->previous
;
11774 if (names
.is_empty ())
11776 tree name
= DECL_NAME (range_decl
);
11777 names
.safe_push (name
);
11778 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11779 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11785 bool expr_non_constant_p
;
11786 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11789 range_expr
= cp_parser_expression (parser
);
11791 /* Put the range declaration(s) back into scope. */
11792 for (unsigned int i
= 0; i
< names
.length (); i
++)
11794 cxx_binding
*binding
= bindings
[i
];
11795 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11796 IDENTIFIER_BINDING (names
[i
]) = binding
;
11799 /* If in template, STMT is converted to a normal for-statement
11800 at instantiation. If not, it is done just ahead. */
11801 if (processing_template_decl
)
11803 if (check_for_bare_parameter_packs (range_expr
))
11804 range_expr
= error_mark_node
;
11805 stmt
= begin_range_for_stmt (scope
, init
);
11807 RANGE_FOR_IVDEP (stmt
) = 1;
11809 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11810 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11811 if (!type_dependent_expression_p (range_expr
)
11812 /* do_auto_deduction doesn't mess with template init-lists. */
11813 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11814 do_range_for_auto_deduction (range_decl
, range_expr
);
11818 stmt
= begin_for_stmt (scope
, init
);
11819 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11820 decomp_first_name
, decomp_cnt
, ivdep
,
11826 /* Subroutine of cp_convert_range_for: given the initializer expression,
11827 builds up the range temporary. */
11830 build_range_temp (tree range_expr
)
11832 tree range_type
, range_temp
;
11834 /* Find out the type deduced by the declaration
11835 `auto &&__range = range_expr'. */
11836 range_type
= cp_build_reference_type (make_auto (), true);
11837 range_type
= do_auto_deduction (range_type
, range_expr
,
11838 type_uses_auto (range_type
));
11840 /* Create the __range variable. */
11841 range_temp
= build_decl (input_location
, VAR_DECL
,
11842 get_identifier ("__for_range"), range_type
);
11843 TREE_USED (range_temp
) = 1;
11844 DECL_ARTIFICIAL (range_temp
) = 1;
11849 /* Used by cp_parser_range_for in template context: we aren't going to
11850 do a full conversion yet, but we still need to resolve auto in the
11851 type of the for-range-declaration if present. This is basically
11852 a shortcut version of cp_convert_range_for. */
11855 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11857 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11860 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11861 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11862 iter_type
= (cp_parser_perform_range_for_lookup
11863 (range_temp
, &begin_dummy
, &end_dummy
));
11866 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11868 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11870 tf_warning_or_error
);
11871 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11872 iter_decl
, auto_node
);
11877 /* Converts a range-based for-statement into a normal
11878 for-statement, as per the definition.
11880 for (RANGE_DECL : RANGE_EXPR)
11883 should be equivalent to:
11886 auto &&__range = RANGE_EXPR;
11887 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11891 RANGE_DECL = *__begin;
11896 If RANGE_EXPR is an array:
11897 BEGIN_EXPR = __range
11898 END_EXPR = __range + ARRAY_SIZE(__range)
11899 Else if RANGE_EXPR has a member 'begin' or 'end':
11900 BEGIN_EXPR = __range.begin()
11901 END_EXPR = __range.end()
11903 BEGIN_EXPR = begin(__range)
11904 END_EXPR = end(__range);
11906 If __range has a member 'begin' but not 'end', or vice versa, we must
11907 still use the second alternative (it will surely fail, however).
11908 When calling begin()/end() in the third alternative we must use
11909 argument dependent lookup, but always considering 'std' as an associated
11913 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11914 tree decomp_first_name
, unsigned int decomp_cnt
,
11915 bool ivdep
, unsigned short unroll
)
11918 tree iter_type
, begin_expr
, end_expr
;
11919 tree condition
, expression
;
11921 range_expr
= mark_lvalue_use (range_expr
);
11923 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11924 /* If an error happened previously do nothing or else a lot of
11925 unhelpful errors would be issued. */
11926 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11931 if (VAR_P (range_expr
)
11932 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11933 /* Can't bind a reference to an array of runtime bound. */
11934 range_temp
= range_expr
;
11937 range_temp
= build_range_temp (range_expr
);
11938 pushdecl (range_temp
);
11939 cp_finish_decl (range_temp
, range_expr
,
11940 /*is_constant_init*/false, NULL_TREE
,
11941 LOOKUP_ONLYCONVERTING
);
11942 range_temp
= convert_from_reference (range_temp
);
11944 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11945 &begin_expr
, &end_expr
);
11948 /* The new for initialization statement. */
11949 begin
= build_decl (input_location
, VAR_DECL
,
11950 get_identifier ("__for_begin"), iter_type
);
11951 TREE_USED (begin
) = 1;
11952 DECL_ARTIFICIAL (begin
) = 1;
11954 cp_finish_decl (begin
, begin_expr
,
11955 /*is_constant_init*/false, NULL_TREE
,
11956 LOOKUP_ONLYCONVERTING
);
11958 if (cxx_dialect
>= cxx17
)
11959 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11960 end
= build_decl (input_location
, VAR_DECL
,
11961 get_identifier ("__for_end"), iter_type
);
11962 TREE_USED (end
) = 1;
11963 DECL_ARTIFICIAL (end
) = 1;
11965 cp_finish_decl (end
, end_expr
,
11966 /*is_constant_init*/false, NULL_TREE
,
11967 LOOKUP_ONLYCONVERTING
);
11969 finish_init_stmt (statement
);
11971 /* The new for condition. */
11972 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11975 NULL
, tf_warning_or_error
);
11976 finish_for_cond (condition
, statement
, ivdep
, unroll
);
11978 /* The new increment expression. */
11979 expression
= finish_unary_op_expr (input_location
,
11980 PREINCREMENT_EXPR
, begin
,
11981 tf_warning_or_error
);
11982 finish_for_expr (expression
, statement
);
11984 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11985 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11987 /* The declaration is initialized with *__begin inside the loop body. */
11988 cp_finish_decl (range_decl
,
11989 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11990 tf_warning_or_error
),
11991 /*is_constant_init*/false, NULL_TREE
,
11992 LOOKUP_ONLYCONVERTING
);
11993 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11994 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11999 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12000 We need to solve both at the same time because the method used
12001 depends on the existence of members begin or end.
12002 Returns the type deduced for the iterator expression. */
12005 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12007 if (error_operand_p (range
))
12009 *begin
= *end
= error_mark_node
;
12010 return error_mark_node
;
12013 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12015 error ("range-based %<for%> expression of type %qT "
12016 "has incomplete type", TREE_TYPE (range
));
12017 *begin
= *end
= error_mark_node
;
12018 return error_mark_node
;
12020 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12022 /* If RANGE is an array, we will use pointer arithmetic. */
12023 *begin
= decay_conversion (range
, tf_warning_or_error
);
12024 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12026 array_type_nelts_top (TREE_TYPE (range
)),
12028 return TREE_TYPE (*begin
);
12032 /* If it is not an array, we must do a bit of magic. */
12033 tree id_begin
, id_end
;
12034 tree member_begin
, member_end
;
12036 *begin
= *end
= error_mark_node
;
12038 id_begin
= get_identifier ("begin");
12039 id_end
= get_identifier ("end");
12040 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12041 /*protect=*/2, /*want_type=*/false,
12042 tf_warning_or_error
);
12043 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12044 /*protect=*/2, /*want_type=*/false,
12045 tf_warning_or_error
);
12047 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
12049 /* Use the member functions. */
12050 if (member_begin
!= NULL_TREE
)
12051 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12053 error ("range-based %<for%> expression of type %qT has an "
12054 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
12056 if (member_end
!= NULL_TREE
)
12057 *end
= cp_parser_range_for_member_function (range
, id_end
);
12059 error ("range-based %<for%> expression of type %qT has a "
12060 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
12064 /* Use global functions with ADL. */
12065 vec
<tree
, va_gc
> *vec
;
12066 vec
= make_tree_vector ();
12068 vec_safe_push (vec
, range
);
12070 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12071 tf_warning_or_error
);
12072 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12073 tf_warning_or_error
);
12074 member_end
= perform_koenig_lookup (id_end
, vec
,
12075 tf_warning_or_error
);
12076 *end
= finish_call_expr (member_end
, &vec
, false, true,
12077 tf_warning_or_error
);
12079 release_tree_vector (vec
);
12082 /* Last common checks. */
12083 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12085 /* If one of the expressions is an error do no more checks. */
12086 *begin
= *end
= error_mark_node
;
12087 return error_mark_node
;
12089 else if (type_dependent_expression_p (*begin
)
12090 || type_dependent_expression_p (*end
))
12091 /* Can happen, when, eg, in a template context, Koenig lookup
12092 can't resolve begin/end (c++/58503). */
12096 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12097 /* The unqualified type of the __begin and __end temporaries should
12098 be the same, as required by the multiple auto declaration. */
12099 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12101 if (cxx_dialect
>= cxx17
12102 && (build_x_binary_op (input_location
, NE_EXPR
,
12103 *begin
, ERROR_MARK
,
12106 != error_mark_node
))
12107 /* P0184R0 allows __begin and __end to have different types,
12108 but make sure they are comparable so we can give a better
12111 error ("inconsistent begin/end types in range-based %<for%> "
12112 "statement: %qT and %qT",
12113 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12120 /* Helper function for cp_parser_perform_range_for_lookup.
12121 Builds a tree for RANGE.IDENTIFIER(). */
12124 cp_parser_range_for_member_function (tree range
, tree identifier
)
12127 vec
<tree
, va_gc
> *vec
;
12129 member
= finish_class_member_access_expr (range
, identifier
,
12130 false, tf_warning_or_error
);
12131 if (member
== error_mark_node
)
12132 return error_mark_node
;
12134 vec
= make_tree_vector ();
12135 res
= finish_call_expr (member
, &vec
,
12136 /*disallow_virtual=*/false,
12137 /*koenig_p=*/false,
12138 tf_warning_or_error
);
12139 release_tree_vector (vec
);
12143 /* Parse an iteration-statement.
12145 iteration-statement:
12146 while ( condition ) statement
12147 do statement while ( expression ) ;
12148 for ( init-statement condition [opt] ; expression [opt] )
12151 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12154 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12155 unsigned short unroll
)
12160 unsigned char in_statement
;
12161 token_indent_info guard_tinfo
;
12163 /* Peek at the next token. */
12164 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12166 return error_mark_node
;
12168 guard_tinfo
= get_token_indent_info (token
);
12170 /* Remember whether or not we are already within an iteration
12172 in_statement
= parser
->in_statement
;
12174 /* See what kind of keyword it is. */
12175 keyword
= token
->keyword
;
12182 /* Begin the while-statement. */
12183 statement
= begin_while_stmt ();
12184 /* Look for the `('. */
12185 matching_parens parens
;
12186 parens
.require_open (parser
);
12187 /* Parse the condition. */
12188 condition
= cp_parser_condition (parser
);
12189 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12190 /* Look for the `)'. */
12191 parens
.require_close (parser
);
12192 /* Parse the dependent statement. */
12193 parser
->in_statement
= IN_ITERATION_STMT
;
12194 bool prev
= note_iteration_stmt_body_start ();
12195 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12196 note_iteration_stmt_body_end (prev
);
12197 parser
->in_statement
= in_statement
;
12198 /* We're done with the while-statement. */
12199 finish_while_stmt (statement
);
12207 /* Begin the do-statement. */
12208 statement
= begin_do_stmt ();
12209 /* Parse the body of the do-statement. */
12210 parser
->in_statement
= IN_ITERATION_STMT
;
12211 bool prev
= note_iteration_stmt_body_start ();
12212 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12213 note_iteration_stmt_body_end (prev
);
12214 parser
->in_statement
= in_statement
;
12215 finish_do_body (statement
);
12216 /* Look for the `while' keyword. */
12217 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12218 /* Look for the `('. */
12219 matching_parens parens
;
12220 parens
.require_open (parser
);
12221 /* Parse the expression. */
12222 expression
= cp_parser_expression (parser
);
12223 /* We're done with the do-statement. */
12224 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12225 /* Look for the `)'. */
12226 parens
.require_close (parser
);
12227 /* Look for the `;'. */
12228 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12234 /* Look for the `('. */
12235 matching_parens parens
;
12236 parens
.require_open (parser
);
12238 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12240 /* Look for the `)'. */
12241 parens
.require_close (parser
);
12243 /* Parse the body of the for-statement. */
12244 parser
->in_statement
= IN_ITERATION_STMT
;
12245 bool prev
= note_iteration_stmt_body_start ();
12246 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12247 note_iteration_stmt_body_end (prev
);
12248 parser
->in_statement
= in_statement
;
12250 /* We're done with the for-statement. */
12251 finish_for_stmt (statement
);
12256 cp_parser_error (parser
, "expected iteration-statement");
12257 statement
= error_mark_node
;
12264 /* Parse a init-statement or the declarator of a range-based-for.
12265 Returns true if a range-based-for declaration is seen.
12268 expression-statement
12269 simple-declaration */
12272 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12274 /* If the next token is a `;', then we have an empty
12275 expression-statement. Grammatically, this is also a
12276 simple-declaration, but an invalid one, because it does not
12277 declare anything. Therefore, if we did not handle this case
12278 specially, we would issue an error message about an invalid
12280 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12282 bool is_range_for
= false;
12283 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12285 /* A colon is used in range-based for. */
12286 parser
->colon_corrects_to_scope_p
= false;
12288 /* We're going to speculatively look for a declaration, falling back
12289 to an expression, if necessary. */
12290 cp_parser_parse_tentatively (parser
);
12291 /* Parse the declaration. */
12292 cp_parser_simple_declaration (parser
,
12293 /*function_definition_allowed_p=*/false,
12295 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12298 /* It is a range-for, consume the ':' */
12299 cp_lexer_consume_token (parser
->lexer
);
12300 is_range_for
= true;
12301 if (cxx_dialect
< cxx11
)
12303 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12304 "range-based %<for%> loops only available with "
12305 "-std=c++11 or -std=gnu++11");
12306 *decl
= error_mark_node
;
12310 /* The ';' is not consumed yet because we told
12311 cp_parser_simple_declaration not to. */
12312 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12314 if (cp_parser_parse_definitely (parser
))
12315 return is_range_for
;
12316 /* If the tentative parse failed, then we shall need to look for an
12317 expression-statement. */
12319 /* If we are here, it is an expression-statement. */
12320 cp_parser_expression_statement (parser
, NULL_TREE
);
12324 /* Parse a jump-statement.
12329 return expression [opt] ;
12330 return braced-init-list ;
12336 goto * expression ;
12338 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12341 cp_parser_jump_statement (cp_parser
* parser
)
12343 tree statement
= error_mark_node
;
12346 unsigned char in_statement
;
12348 /* Peek at the next token. */
12349 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12351 return error_mark_node
;
12353 /* See what kind of keyword it is. */
12354 keyword
= token
->keyword
;
12358 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12359 switch (in_statement
)
12362 error_at (token
->location
, "break statement not within loop or switch");
12365 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12366 || in_statement
== IN_ITERATION_STMT
);
12367 statement
= finish_break_stmt ();
12368 if (in_statement
== IN_ITERATION_STMT
)
12369 break_maybe_infinite_loop ();
12372 error_at (token
->location
, "invalid exit from OpenMP structured block");
12375 error_at (token
->location
, "break statement used with OpenMP for loop");
12378 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12382 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12385 error_at (token
->location
, "continue statement not within a loop");
12387 /* Fall through. */
12388 case IN_ITERATION_STMT
:
12390 statement
= finish_continue_stmt ();
12393 error_at (token
->location
, "invalid exit from OpenMP structured block");
12396 gcc_unreachable ();
12398 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12404 bool expr_non_constant_p
;
12406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12408 cp_lexer_set_source_position (parser
->lexer
);
12409 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12410 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12412 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12413 expr
= cp_parser_expression (parser
);
12415 /* If the next token is a `;', then there is no
12418 /* Build the return-statement. */
12419 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12420 /* Don't deduce from a discarded return statement. */;
12422 statement
= finish_return_stmt (expr
);
12423 /* Look for the final `;'. */
12424 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12429 if (parser
->in_function_body
12430 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12432 error ("%<goto%> in %<constexpr%> function");
12433 cp_function_chain
->invalid_constexpr
= true;
12436 /* Create the goto-statement. */
12437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12439 /* Issue a warning about this use of a GNU extension. */
12440 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12441 /* Consume the '*' token. */
12442 cp_lexer_consume_token (parser
->lexer
);
12443 /* Parse the dependent expression. */
12444 finish_goto_stmt (cp_parser_expression (parser
));
12447 finish_goto_stmt (cp_parser_identifier (parser
));
12448 /* Look for the final `;'. */
12449 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12453 cp_parser_error (parser
, "expected jump-statement");
12460 /* Parse a declaration-statement.
12462 declaration-statement:
12463 block-declaration */
12466 cp_parser_declaration_statement (cp_parser
* parser
)
12470 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12471 p
= obstack_alloc (&declarator_obstack
, 0);
12473 /* Parse the block-declaration. */
12474 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12476 /* Free any declarators allocated. */
12477 obstack_free (&declarator_obstack
, p
);
12480 /* Some dependent statements (like `if (cond) statement'), are
12481 implicitly in their own scope. In other words, if the statement is
12482 a single statement (as opposed to a compound-statement), it is
12483 none-the-less treated as if it were enclosed in braces. Any
12484 declarations appearing in the dependent statement are out of scope
12485 after control passes that point. This function parses a statement,
12486 but ensures that is in its own scope, even if it is not a
12487 compound-statement.
12489 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12490 is a (possibly labeled) if statement which is not enclosed in
12491 braces and has an else clause. This is used to implement
12494 CHAIN is a vector of if-else-if conditions. This is used to implement
12497 Returns the new statement. */
12500 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12501 const token_indent_info
&guard_tinfo
,
12505 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12506 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12507 token_indent_info body_tinfo
12508 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12513 /* Mark if () ; with a special NOP_EXPR. */
12514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12516 cp_lexer_consume_token (parser
->lexer
);
12517 statement
= add_stmt (build_empty_stmt (body_loc
));
12519 if (guard_tinfo
.keyword
== RID_IF
12520 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12521 warning_at (body_loc
, OPT_Wempty_body
,
12522 "suggest braces around empty body in an %<if%> statement");
12523 else if (guard_tinfo
.keyword
== RID_ELSE
)
12524 warning_at (body_loc
, OPT_Wempty_body
,
12525 "suggest braces around empty body in an %<else%> statement");
12527 /* if a compound is opened, we simply parse the statement directly. */
12528 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12529 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12530 /* If the token is not a `{', then we must take special action. */
12533 /* Create a compound-statement. */
12534 statement
= begin_compound_stmt (0);
12535 /* Parse the dependent-statement. */
12536 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12537 &body_loc_after_labels
);
12538 /* Finish the dummy compound-statement. */
12539 finish_compound_stmt (statement
);
12542 token_indent_info next_tinfo
12543 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12544 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12546 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12547 && next_tinfo
.type
!= CPP_SEMICOLON
)
12548 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12549 guard_tinfo
.location
, guard_tinfo
.keyword
);
12551 /* Return the statement. */
12555 /* For some dependent statements (like `while (cond) statement'), we
12556 have already created a scope. Therefore, even if the dependent
12557 statement is a compound-statement, we do not want to create another
12561 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12562 const token_indent_info
&guard_tinfo
)
12564 /* If the token is a `{', then we must take special action. */
12565 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12567 token_indent_info body_tinfo
12568 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12569 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12571 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12572 &loc_after_labels
);
12573 token_indent_info next_tinfo
12574 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12575 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12577 if (loc_after_labels
!= UNKNOWN_LOCATION
12578 && next_tinfo
.type
!= CPP_SEMICOLON
)
12579 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12580 guard_tinfo
.location
,
12581 guard_tinfo
.keyword
);
12585 /* Avoid calling cp_parser_compound_statement, so that we
12586 don't create a new scope. Do everything else by hand. */
12587 matching_braces braces
;
12588 braces
.require_open (parser
);
12589 /* If the next keyword is `__label__' we have a label declaration. */
12590 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12591 cp_parser_label_declaration (parser
);
12592 /* Parse an (optional) statement-seq. */
12593 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12594 braces
.require_close (parser
);
12598 /* Declarations [gram.dcl.dcl] */
12600 /* Parse an optional declaration-sequence.
12604 declaration-seq declaration */
12607 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12613 token
= cp_lexer_peek_token (parser
->lexer
);
12615 if (token
->type
== CPP_CLOSE_BRACE
12616 || token
->type
== CPP_EOF
12617 || token
->type
== CPP_PRAGMA_EOL
)
12620 if (token
->type
== CPP_SEMICOLON
)
12622 /* A declaration consisting of a single semicolon is
12623 invalid. Allow it unless we're being pedantic. */
12624 cp_lexer_consume_token (parser
->lexer
);
12625 if (!in_system_header_at (input_location
))
12626 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12630 /* If we're entering or exiting a region that's implicitly
12631 extern "C", modify the lang context appropriately. */
12632 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12634 push_lang_context (lang_name_c
);
12635 parser
->implicit_extern_c
= true;
12637 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12639 pop_lang_context ();
12640 parser
->implicit_extern_c
= false;
12643 if (token
->type
== CPP_PRAGMA
)
12645 /* A top-level declaration can consist solely of a #pragma.
12646 A nested declaration cannot, so this is done here and not
12647 in cp_parser_declaration. (A #pragma at block scope is
12648 handled in cp_parser_statement.) */
12649 cp_parser_pragma (parser
, pragma_external
, NULL
);
12653 /* Parse the declaration itself. */
12654 cp_parser_declaration (parser
);
12658 /* Parse a declaration.
12662 function-definition
12663 template-declaration
12664 explicit-instantiation
12665 explicit-specialization
12666 linkage-specification
12667 namespace-definition
12675 __extension__ declaration */
12678 cp_parser_declaration (cp_parser
* parser
)
12682 int saved_pedantic
;
12684 tree attributes
= NULL_TREE
;
12686 /* Check for the `__extension__' keyword. */
12687 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12689 /* Parse the qualified declaration. */
12690 cp_parser_declaration (parser
);
12691 /* Restore the PEDANTIC flag. */
12692 pedantic
= saved_pedantic
;
12697 /* Try to figure out what kind of declaration is present. */
12698 token1
= *cp_lexer_peek_token (parser
->lexer
);
12700 if (token1
.type
!= CPP_EOF
)
12701 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12704 token2
.type
= CPP_EOF
;
12705 token2
.keyword
= RID_MAX
;
12708 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12709 p
= obstack_alloc (&declarator_obstack
, 0);
12711 /* If the next token is `extern' and the following token is a string
12712 literal, then we have a linkage specification. */
12713 if (token1
.keyword
== RID_EXTERN
12714 && cp_parser_is_pure_string_literal (&token2
))
12715 cp_parser_linkage_specification (parser
);
12716 /* If the next token is `template', then we have either a template
12717 declaration, an explicit instantiation, or an explicit
12719 else if (token1
.keyword
== RID_TEMPLATE
)
12721 /* `template <>' indicates a template specialization. */
12722 if (token2
.type
== CPP_LESS
12723 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12724 cp_parser_explicit_specialization (parser
);
12725 /* `template <' indicates a template declaration. */
12726 else if (token2
.type
== CPP_LESS
)
12727 cp_parser_template_declaration (parser
, /*member_p=*/false);
12728 /* Anything else must be an explicit instantiation. */
12730 cp_parser_explicit_instantiation (parser
);
12732 /* If the next token is `export', then we have a template
12734 else if (token1
.keyword
== RID_EXPORT
)
12735 cp_parser_template_declaration (parser
, /*member_p=*/false);
12736 /* If the next token is `extern', 'static' or 'inline' and the one
12737 after that is `template', we have a GNU extended explicit
12738 instantiation directive. */
12739 else if (cp_parser_allow_gnu_extensions_p (parser
)
12740 && (token1
.keyword
== RID_EXTERN
12741 || token1
.keyword
== RID_STATIC
12742 || token1
.keyword
== RID_INLINE
)
12743 && token2
.keyword
== RID_TEMPLATE
)
12744 cp_parser_explicit_instantiation (parser
);
12745 /* If the next token is `namespace', check for a named or unnamed
12746 namespace definition. */
12747 else if (token1
.keyword
== RID_NAMESPACE
12748 && (/* A named namespace definition. */
12749 (token2
.type
== CPP_NAME
12750 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12752 || (token2
.type
== CPP_OPEN_SQUARE
12753 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12754 == CPP_OPEN_SQUARE
)
12755 /* An unnamed namespace definition. */
12756 || token2
.type
== CPP_OPEN_BRACE
12757 || token2
.keyword
== RID_ATTRIBUTE
))
12758 cp_parser_namespace_definition (parser
);
12759 /* An inline (associated) namespace definition. */
12760 else if (token1
.keyword
== RID_INLINE
12761 && token2
.keyword
== RID_NAMESPACE
)
12762 cp_parser_namespace_definition (parser
);
12763 /* Objective-C++ declaration/definition. */
12764 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12765 cp_parser_objc_declaration (parser
, NULL_TREE
);
12766 else if (c_dialect_objc ()
12767 && token1
.keyword
== RID_ATTRIBUTE
12768 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12769 cp_parser_objc_declaration (parser
, attributes
);
12770 /* At this point we may have a template declared by a concept
12772 else if (flag_concepts
12773 && cp_parser_template_declaration_after_export (parser
,
12774 /*member_p=*/false))
12777 /* Try to parse a block-declaration, or a function-definition. */
12778 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12780 /* Free any declarators allocated. */
12781 obstack_free (&declarator_obstack
, p
);
12784 /* Parse a block-declaration.
12789 namespace-alias-definition
12796 __extension__ block-declaration
12801 static_assert-declaration
12803 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12804 part of a declaration-statement. */
12807 cp_parser_block_declaration (cp_parser
*parser
,
12811 int saved_pedantic
;
12813 /* Check for the `__extension__' keyword. */
12814 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12816 /* Parse the qualified declaration. */
12817 cp_parser_block_declaration (parser
, statement_p
);
12818 /* Restore the PEDANTIC flag. */
12819 pedantic
= saved_pedantic
;
12824 /* Peek at the next token to figure out which kind of declaration is
12826 token1
= cp_lexer_peek_token (parser
->lexer
);
12828 /* If the next keyword is `asm', we have an asm-definition. */
12829 if (token1
->keyword
== RID_ASM
)
12832 cp_parser_commit_to_tentative_parse (parser
);
12833 cp_parser_asm_definition (parser
);
12835 /* If the next keyword is `namespace', we have a
12836 namespace-alias-definition. */
12837 else if (token1
->keyword
== RID_NAMESPACE
)
12838 cp_parser_namespace_alias_definition (parser
);
12839 /* If the next keyword is `using', we have a
12840 using-declaration, a using-directive, or an alias-declaration. */
12841 else if (token1
->keyword
== RID_USING
)
12846 cp_parser_commit_to_tentative_parse (parser
);
12847 /* If the token after `using' is `namespace', then we have a
12848 using-directive. */
12849 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12850 if (token2
->keyword
== RID_NAMESPACE
)
12851 cp_parser_using_directive (parser
);
12852 /* If the second token after 'using' is '=', then we have an
12853 alias-declaration. */
12854 else if (cxx_dialect
>= cxx11
12855 && token2
->type
== CPP_NAME
12856 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12857 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12858 cp_parser_alias_declaration (parser
);
12859 /* Otherwise, it's a using-declaration. */
12861 cp_parser_using_declaration (parser
,
12862 /*access_declaration_p=*/false);
12864 /* If the next keyword is `__label__' we have a misplaced label
12866 else if (token1
->keyword
== RID_LABEL
)
12868 cp_lexer_consume_token (parser
->lexer
);
12869 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12870 cp_parser_skip_to_end_of_statement (parser
);
12871 /* If the next token is now a `;', consume it. */
12872 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12873 cp_lexer_consume_token (parser
->lexer
);
12875 /* If the next token is `static_assert' we have a static assertion. */
12876 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12877 cp_parser_static_assert (parser
, /*member_p=*/false);
12878 /* Anything else must be a simple-declaration. */
12880 cp_parser_simple_declaration (parser
, !statement_p
,
12881 /*maybe_range_for_decl*/NULL
);
12884 /* Parse a simple-declaration.
12886 simple-declaration:
12887 decl-specifier-seq [opt] init-declarator-list [opt] ;
12888 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12889 brace-or-equal-initializer ;
12891 init-declarator-list:
12893 init-declarator-list , init-declarator
12895 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12896 function-definition as a simple-declaration.
12898 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12899 parsed declaration if it is an uninitialized single declarator not followed
12900 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12901 if present, will not be consumed. */
12904 cp_parser_simple_declaration (cp_parser
* parser
,
12905 bool function_definition_allowed_p
,
12906 tree
*maybe_range_for_decl
)
12908 cp_decl_specifier_seq decl_specifiers
;
12909 int declares_class_or_enum
;
12910 bool saw_declarator
;
12911 location_t comma_loc
= UNKNOWN_LOCATION
;
12912 location_t init_loc
= UNKNOWN_LOCATION
;
12914 if (maybe_range_for_decl
)
12915 *maybe_range_for_decl
= NULL_TREE
;
12917 /* Defer access checks until we know what is being declared; the
12918 checks for names appearing in the decl-specifier-seq should be
12919 done as if we were in the scope of the thing being declared. */
12920 push_deferring_access_checks (dk_deferred
);
12922 /* Parse the decl-specifier-seq. We have to keep track of whether
12923 or not the decl-specifier-seq declares a named class or
12924 enumeration type, since that is the only case in which the
12925 init-declarator-list is allowed to be empty.
12929 In a simple-declaration, the optional init-declarator-list can be
12930 omitted only when declaring a class or enumeration, that is when
12931 the decl-specifier-seq contains either a class-specifier, an
12932 elaborated-type-specifier, or an enum-specifier. */
12933 cp_parser_decl_specifier_seq (parser
,
12934 CP_PARSER_FLAGS_OPTIONAL
,
12936 &declares_class_or_enum
);
12937 /* We no longer need to defer access checks. */
12938 stop_deferring_access_checks ();
12940 /* In a block scope, a valid declaration must always have a
12941 decl-specifier-seq. By not trying to parse declarators, we can
12942 resolve the declaration/expression ambiguity more quickly. */
12943 if (!function_definition_allowed_p
12944 && !decl_specifiers
.any_specifiers_p
)
12946 cp_parser_error (parser
, "expected declaration");
12950 /* If the next two tokens are both identifiers, the code is
12951 erroneous. The usual cause of this situation is code like:
12955 where "T" should name a type -- but does not. */
12956 if (!decl_specifiers
.any_type_specifiers_p
12957 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12959 /* If parsing tentatively, we should commit; we really are
12960 looking at a declaration. */
12961 cp_parser_commit_to_tentative_parse (parser
);
12966 /* If we have seen at least one decl-specifier, and the next token
12967 is not a parenthesis, then we must be looking at a declaration.
12968 (After "int (" we might be looking at a functional cast.) */
12969 if (decl_specifiers
.any_specifiers_p
12970 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12971 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12972 && !cp_parser_error_occurred (parser
))
12973 cp_parser_commit_to_tentative_parse (parser
);
12975 /* Look for C++17 decomposition declaration. */
12976 for (size_t n
= 1; ; n
++)
12977 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12978 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12980 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12981 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12982 && decl_specifiers
.any_specifiers_p
)
12985 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12986 maybe_range_for_decl
,
12989 /* The next token should be either a `,' or a `;'. */
12990 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12991 /* If it's a `;', we are done. */
12992 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12994 /* Anything else is an error. */
12997 /* If we have already issued an error message we don't need
12998 to issue another one. */
12999 if ((decl
!= error_mark_node
13000 && DECL_INITIAL (decl
) != error_mark_node
)
13001 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13002 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13003 /* Skip tokens until we reach the end of the statement. */
13004 cp_parser_skip_to_end_of_statement (parser
);
13005 /* If the next token is now a `;', consume it. */
13006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13007 cp_lexer_consume_token (parser
->lexer
);
13015 bool auto_specifier_p
;
13016 /* NULL_TREE if both variable and function declaration are allowed,
13017 error_mark_node if function declaration are not allowed and
13018 a FUNCTION_DECL that should be diagnosed if it is followed by
13019 variable declarations. */
13020 tree auto_function_declaration
;
13022 last_type
= NULL_TREE
;
13024 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13025 auto_function_declaration
= NULL_TREE
;
13027 /* Keep going until we hit the `;' at the end of the simple
13029 saw_declarator
= false;
13030 while (cp_lexer_next_token_is_not (parser
->lexer
,
13034 bool function_definition_p
;
13036 tree auto_result
= NULL_TREE
;
13038 if (saw_declarator
)
13040 /* If we are processing next declarator, comma is expected */
13041 token
= cp_lexer_peek_token (parser
->lexer
);
13042 gcc_assert (token
->type
== CPP_COMMA
);
13043 cp_lexer_consume_token (parser
->lexer
);
13044 if (maybe_range_for_decl
)
13046 *maybe_range_for_decl
= error_mark_node
;
13047 if (comma_loc
== UNKNOWN_LOCATION
)
13048 comma_loc
= token
->location
;
13052 saw_declarator
= true;
13054 /* Parse the init-declarator. */
13055 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13057 function_definition_allowed_p
,
13058 /*member_p=*/false,
13059 declares_class_or_enum
,
13060 &function_definition_p
,
13061 maybe_range_for_decl
,
13064 /* If an error occurred while parsing tentatively, exit quickly.
13065 (That usually happens when in the body of a function; each
13066 statement is treated as a declaration-statement until proven
13068 if (cp_parser_error_occurred (parser
))
13071 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13073 /* If the init-declarator-list contains more than one
13074 init-declarator, they shall all form declarations of
13076 if (auto_function_declaration
== NULL_TREE
)
13077 auto_function_declaration
13078 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13079 else if (TREE_CODE (decl
) == FUNCTION_DECL
13080 || auto_function_declaration
!= error_mark_node
)
13082 error_at (decl_specifiers
.locations
[ds_type_spec
],
13083 "non-variable %qD in declaration with more than one "
13084 "declarator with placeholder type",
13085 TREE_CODE (decl
) == FUNCTION_DECL
13086 ? decl
: auto_function_declaration
);
13087 auto_function_declaration
= error_mark_node
;
13092 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13095 && last_type
!= error_mark_node
13096 && !same_type_p (auto_result
, last_type
))
13098 /* If the list of declarators contains more than one declarator,
13099 the type of each declared variable is determined as described
13100 above. If the type deduced for the template parameter U is not
13101 the same in each deduction, the program is ill-formed. */
13102 error_at (decl_specifiers
.locations
[ds_type_spec
],
13103 "inconsistent deduction for %qT: %qT and then %qT",
13104 decl_specifiers
.type
, last_type
, auto_result
);
13105 last_type
= error_mark_node
;
13108 last_type
= auto_result
;
13111 /* Handle function definitions specially. */
13112 if (function_definition_p
)
13114 /* If the next token is a `,', then we are probably
13115 processing something like:
13119 which is erroneous. */
13120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13122 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13123 error_at (token
->location
,
13125 " declarations and function-definitions is forbidden");
13127 /* Otherwise, we're done with the list of declarators. */
13130 pop_deferring_access_checks ();
13134 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13135 *maybe_range_for_decl
= decl
;
13136 /* The next token should be either a `,' or a `;'. */
13137 token
= cp_lexer_peek_token (parser
->lexer
);
13138 /* If it's a `,', there are more declarators to come. */
13139 if (token
->type
== CPP_COMMA
)
13140 /* will be consumed next time around */;
13141 /* If it's a `;', we are done. */
13142 else if (token
->type
== CPP_SEMICOLON
)
13144 else if (maybe_range_for_decl
)
13146 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13147 permerror (decl_specifiers
.locations
[ds_type_spec
],
13148 "types may not be defined in a for-range-declaration");
13151 /* Anything else is an error. */
13154 /* If we have already issued an error message we don't need
13155 to issue another one. */
13156 if ((decl
!= error_mark_node
13157 && DECL_INITIAL (decl
) != error_mark_node
)
13158 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13159 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13160 /* Skip tokens until we reach the end of the statement. */
13161 cp_parser_skip_to_end_of_statement (parser
);
13162 /* If the next token is now a `;', consume it. */
13163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13164 cp_lexer_consume_token (parser
->lexer
);
13167 /* After the first time around, a function-definition is not
13168 allowed -- even if it was OK at first. For example:
13173 function_definition_allowed_p
= false;
13176 /* Issue an error message if no declarators are present, and the
13177 decl-specifier-seq does not itself declare a class or
13178 enumeration: [dcl.dcl]/3. */
13179 if (!saw_declarator
)
13181 if (cp_parser_declares_only_class_p (parser
))
13183 if (!declares_class_or_enum
13184 && decl_specifiers
.type
13185 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13186 /* Ensure an error is issued anyway when finish_decltype_type,
13187 called via cp_parser_decl_specifier_seq, returns a class or
13188 an enumeration (c++/51786). */
13189 decl_specifiers
.type
= NULL_TREE
;
13190 shadow_tag (&decl_specifiers
);
13192 /* Perform any deferred access checks. */
13193 perform_deferred_access_checks (tf_warning_or_error
);
13196 /* Consume the `;'. */
13198 if (!maybe_range_for_decl
)
13199 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13200 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13202 if (init_loc
!= UNKNOWN_LOCATION
)
13203 error_at (init_loc
, "initializer in range-based %<for%> loop");
13204 if (comma_loc
!= UNKNOWN_LOCATION
)
13205 error_at (comma_loc
,
13206 "multiple declarations in range-based %<for%> loop");
13210 pop_deferring_access_checks ();
13213 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13214 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13218 cp_parser_decomposition_declaration (cp_parser
*parser
,
13219 cp_decl_specifier_seq
*decl_specifiers
,
13220 tree
*maybe_range_for_decl
,
13221 location_t
*init_loc
)
13223 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13224 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13225 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13227 /* Parse the identifier-list. */
13228 auto_vec
<cp_expr
, 10> v
;
13229 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13232 cp_expr e
= cp_parser_identifier (parser
);
13233 if (e
.get_value () == error_mark_node
)
13236 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13238 cp_lexer_consume_token (parser
->lexer
);
13241 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13242 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13244 end_loc
= UNKNOWN_LOCATION
;
13245 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13248 cp_lexer_consume_token (parser
->lexer
);
13251 cp_parser_skip_to_end_of_statement (parser
);
13252 return error_mark_node
;
13256 if (cxx_dialect
< cxx17
)
13257 pedwarn (loc
, 0, "structured bindings only available with "
13258 "-std=c++17 or -std=gnu++17");
13261 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13262 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13263 declarator
->id_loc
= loc
;
13264 if (ref_qual
!= REF_QUAL_NONE
)
13265 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13266 ref_qual
== REF_QUAL_RVALUE
,
13268 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13269 NULL_TREE
, decl_specifiers
->attributes
,
13271 tree orig_decl
= decl
;
13275 cp_decl_specifier_seq decl_specs
;
13276 clear_decl_specs (&decl_specs
);
13277 decl_specs
.type
= make_auto ();
13279 FOR_EACH_VEC_ELT (v
, i
, e
)
13282 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13284 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13285 declarator
->id_loc
= e
.get_location ();
13286 tree elt_pushed_scope
;
13287 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13288 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13289 if (decl2
== error_mark_node
)
13290 decl
= error_mark_node
;
13291 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13293 /* Ensure we've diagnosed redeclaration if we aren't creating
13295 gcc_assert (errorcount
);
13296 decl
= error_mark_node
;
13300 if (elt_pushed_scope
)
13301 pop_scope (elt_pushed_scope
);
13306 error_at (loc
, "empty structured binding declaration");
13307 decl
= error_mark_node
;
13310 if (maybe_range_for_decl
== NULL
13311 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13313 bool non_constant_p
= false, is_direct_init
= false;
13314 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13315 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13317 if (initializer
== NULL_TREE
13318 || (TREE_CODE (initializer
) == TREE_LIST
13319 && TREE_CHAIN (initializer
))
13321 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13322 && CONSTRUCTOR_NELTS (initializer
) != 1))
13324 error_at (loc
, "invalid initializer for structured binding "
13326 initializer
= error_mark_node
;
13329 if (decl
!= error_mark_node
)
13331 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13332 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13333 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13334 cp_finish_decomp (decl
, prev
, v
.length ());
13337 else if (decl
!= error_mark_node
)
13339 *maybe_range_for_decl
= prev
;
13340 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13341 the underlying DECL. */
13342 cp_finish_decomp (decl
, prev
, v
.length ());
13346 pop_scope (pushed_scope
);
13348 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13350 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13351 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13357 /* Parse a decl-specifier-seq.
13359 decl-specifier-seq:
13360 decl-specifier-seq [opt] decl-specifier
13361 decl-specifier attribute-specifier-seq [opt] (C++11)
13364 storage-class-specifier
13375 Concepts Extension:
13380 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13382 The parser flags FLAGS is used to control type-specifier parsing.
13384 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13387 1: one of the decl-specifiers is an elaborated-type-specifier
13388 (i.e., a type declaration)
13389 2: one of the decl-specifiers is an enum-specifier or a
13390 class-specifier (i.e., a type definition)
13395 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13396 cp_parser_flags flags
,
13397 cp_decl_specifier_seq
*decl_specs
,
13398 int* declares_class_or_enum
)
13400 bool constructor_possible_p
= !parser
->in_declarator_p
;
13401 bool found_decl_spec
= false;
13402 cp_token
*start_token
= NULL
;
13405 /* Clear DECL_SPECS. */
13406 clear_decl_specs (decl_specs
);
13408 /* Assume no class or enumeration type is declared. */
13409 *declares_class_or_enum
= 0;
13411 /* Keep reading specifiers until there are no more to read. */
13414 bool constructor_p
;
13418 /* Peek at the next token. */
13419 token
= cp_lexer_peek_token (parser
->lexer
);
13421 /* Save the first token of the decl spec list for error
13424 start_token
= token
;
13425 /* Handle attributes. */
13426 if (cp_next_tokens_can_be_attribute_p (parser
))
13428 /* Parse the attributes. */
13429 tree attrs
= cp_parser_attributes_opt (parser
);
13431 /* In a sequence of declaration specifiers, c++11 attributes
13432 appertain to the type that precede them. In that case
13435 The attribute-specifier-seq affects the type only for
13436 the declaration it appears in, not other declarations
13437 involving the same type.
13439 But for now let's force the user to position the
13440 attribute either at the beginning of the declaration or
13441 after the declarator-id, which would clearly mean that it
13442 applies to the declarator. */
13443 if (cxx11_attribute_p (attrs
))
13445 if (!found_decl_spec
)
13446 /* The c++11 attribute is at the beginning of the
13447 declaration. It appertains to the entity being
13451 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13453 /* This is an attribute following a
13454 class-specifier. */
13455 if (decl_specs
->type_definition_p
)
13456 warn_misplaced_attr_for_class_type (token
->location
,
13462 decl_specs
->std_attributes
13463 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13464 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13465 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13471 decl_specs
->attributes
13472 = attr_chainon (decl_specs
->attributes
, attrs
);
13473 if (decl_specs
->locations
[ds_attribute
] == 0)
13474 decl_specs
->locations
[ds_attribute
] = token
->location
;
13477 /* Assume we will find a decl-specifier keyword. */
13478 found_decl_spec
= true;
13479 /* If the next token is an appropriate keyword, we can simply
13480 add it to the list. */
13481 switch (token
->keyword
)
13487 if (!at_class_scope_p ())
13489 gcc_rich_location
richloc (token
->location
);
13490 richloc
.add_fixit_remove ();
13491 error_at (&richloc
, "%<friend%> used outside of class");
13492 cp_lexer_purge_token (parser
->lexer
);
13497 /* Consume the token. */
13498 cp_lexer_consume_token (parser
->lexer
);
13502 case RID_CONSTEXPR
:
13504 cp_lexer_consume_token (parser
->lexer
);
13509 cp_lexer_consume_token (parser
->lexer
);
13512 /* function-specifier:
13519 cp_parser_function_specifier_opt (parser
, decl_specs
);
13526 /* Consume the token. */
13527 cp_lexer_consume_token (parser
->lexer
);
13528 /* A constructor declarator cannot appear in a typedef. */
13529 constructor_possible_p
= false;
13530 /* The "typedef" keyword can only occur in a declaration; we
13531 may as well commit at this point. */
13532 cp_parser_commit_to_tentative_parse (parser
);
13534 if (decl_specs
->storage_class
!= sc_none
)
13535 decl_specs
->conflicting_specifiers_p
= true;
13538 /* storage-class-specifier:
13548 if (cxx_dialect
== cxx98
)
13550 /* Consume the token. */
13551 cp_lexer_consume_token (parser
->lexer
);
13553 /* Complain about `auto' as a storage specifier, if
13554 we're complaining about C++0x compatibility. */
13555 gcc_rich_location
richloc (token
->location
);
13556 richloc
.add_fixit_remove ();
13557 warning_at (&richloc
, OPT_Wc__11_compat
,
13558 "%<auto%> changes meaning in C++11; "
13559 "please remove it");
13561 /* Set the storage class anyway. */
13562 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13566 /* C++0x auto type-specifier. */
13567 found_decl_spec
= false;
13574 /* Consume the token. */
13575 cp_lexer_consume_token (parser
->lexer
);
13576 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13580 /* Consume the token. */
13582 cp_lexer_consume_token (parser
->lexer
);
13586 /* We did not yet find a decl-specifier yet. */
13587 found_decl_spec
= false;
13591 if (found_decl_spec
13592 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13593 && token
->keyword
!= RID_CONSTEXPR
)
13594 error ("decl-specifier invalid in condition");
13596 if (found_decl_spec
13597 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13598 && token
->keyword
!= RID_MUTABLE
13599 && token
->keyword
!= RID_CONSTEXPR
)
13600 error_at (token
->location
, "%qD invalid in lambda",
13601 ridpointers
[token
->keyword
]);
13604 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13606 /* Constructors are a special case. The `S' in `S()' is not a
13607 decl-specifier; it is the beginning of the declarator. */
13609 = (!found_decl_spec
13610 && constructor_possible_p
13611 && (cp_parser_constructor_declarator_p
13612 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13614 /* If we don't have a DECL_SPEC yet, then we must be looking at
13615 a type-specifier. */
13616 if (!found_decl_spec
&& !constructor_p
)
13618 int decl_spec_declares_class_or_enum
;
13619 bool is_cv_qualifier
;
13623 = cp_parser_type_specifier (parser
, flags
,
13625 /*is_declaration=*/true,
13626 &decl_spec_declares_class_or_enum
,
13628 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13630 /* If this type-specifier referenced a user-defined type
13631 (a typedef, class-name, etc.), then we can't allow any
13632 more such type-specifiers henceforth.
13636 The longest sequence of decl-specifiers that could
13637 possibly be a type name is taken as the
13638 decl-specifier-seq of a declaration. The sequence shall
13639 be self-consistent as described below.
13643 As a general rule, at most one type-specifier is allowed
13644 in the complete decl-specifier-seq of a declaration. The
13645 only exceptions are the following:
13647 -- const or volatile can be combined with any other
13650 -- signed or unsigned can be combined with char, long,
13658 void g (const int Pc);
13660 Here, Pc is *not* part of the decl-specifier seq; it's
13661 the declarator. Therefore, once we see a type-specifier
13662 (other than a cv-qualifier), we forbid any additional
13663 user-defined types. We *do* still allow things like `int
13664 int' to be considered a decl-specifier-seq, and issue the
13665 error message later. */
13666 if (type_spec
&& !is_cv_qualifier
)
13667 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13668 /* A constructor declarator cannot follow a type-specifier. */
13671 constructor_possible_p
= false;
13672 found_decl_spec
= true;
13673 if (!is_cv_qualifier
)
13674 decl_specs
->any_type_specifiers_p
= true;
13678 /* If we still do not have a DECL_SPEC, then there are no more
13679 decl-specifiers. */
13680 if (!found_decl_spec
)
13683 decl_specs
->any_specifiers_p
= true;
13684 /* After we see one decl-specifier, further decl-specifiers are
13685 always optional. */
13686 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13689 /* Don't allow a friend specifier with a class definition. */
13690 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13691 && (*declares_class_or_enum
& 2))
13692 error_at (decl_specs
->locations
[ds_friend
],
13693 "class definition may not be declared a friend");
13696 /* Parse an (optional) storage-class-specifier.
13698 storage-class-specifier:
13707 storage-class-specifier:
13710 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13713 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13715 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13718 if (cxx_dialect
!= cxx98
)
13720 /* Fall through for C++98. */
13721 gcc_fallthrough ();
13728 /* Consume the token. */
13729 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13736 /* Parse an (optional) function-specifier.
13738 function-specifier:
13743 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13744 Updates DECL_SPECS, if it is non-NULL. */
13747 cp_parser_function_specifier_opt (cp_parser
* parser
,
13748 cp_decl_specifier_seq
*decl_specs
)
13750 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13751 switch (token
->keyword
)
13754 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13758 /* 14.5.2.3 [temp.mem]
13760 A member function template shall not be virtual. */
13761 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13762 && current_class_type
)
13763 error_at (token
->location
, "templates may not be %<virtual%>");
13765 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13769 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13776 /* Consume the token. */
13777 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13780 /* Parse a linkage-specification.
13782 linkage-specification:
13783 extern string-literal { declaration-seq [opt] }
13784 extern string-literal declaration */
13787 cp_parser_linkage_specification (cp_parser
* parser
)
13791 /* Look for the `extern' keyword. */
13792 cp_token
*extern_token
13793 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13795 /* Look for the string-literal. */
13796 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13797 linkage
= cp_parser_string_literal (parser
, false, false);
13799 /* Transform the literal into an identifier. If the literal is a
13800 wide-character string, or contains embedded NULs, then we can't
13801 handle it as the user wants. */
13802 if (strlen (TREE_STRING_POINTER (linkage
))
13803 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13805 cp_parser_error (parser
, "invalid linkage-specification");
13806 /* Assume C++ linkage. */
13807 linkage
= lang_name_cplusplus
;
13810 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13812 /* We're now using the new linkage. */
13813 push_lang_context (linkage
);
13815 /* Preserve the location of the the innermost linkage specification,
13816 tracking the locations of nested specifications via a local. */
13817 location_t saved_location
13818 = parser
->innermost_linkage_specification_location
;
13819 /* Construct a location ranging from the start of the "extern" to
13820 the end of the string-literal, with the caret at the start, e.g.:
13824 parser
->innermost_linkage_specification_location
13825 = make_location (extern_token
->location
,
13826 extern_token
->location
,
13827 get_finish (string_token
->location
));
13829 /* If the next token is a `{', then we're using the first
13831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13833 cp_ensure_no_omp_declare_simd (parser
);
13834 cp_ensure_no_oacc_routine (parser
);
13836 /* Consume the `{' token. */
13837 matching_braces braces
;
13838 braces
.consume_open (parser
)->location
;
13839 /* Parse the declarations. */
13840 cp_parser_declaration_seq_opt (parser
);
13841 /* Look for the closing `}'. */
13842 braces
.require_close (parser
);
13844 /* Otherwise, there's just one declaration. */
13847 bool saved_in_unbraced_linkage_specification_p
;
13849 saved_in_unbraced_linkage_specification_p
13850 = parser
->in_unbraced_linkage_specification_p
;
13851 parser
->in_unbraced_linkage_specification_p
= true;
13852 cp_parser_declaration (parser
);
13853 parser
->in_unbraced_linkage_specification_p
13854 = saved_in_unbraced_linkage_specification_p
;
13857 /* We're done with the linkage-specification. */
13858 pop_lang_context ();
13860 /* Restore location of parent linkage specification, if any. */
13861 parser
->innermost_linkage_specification_location
= saved_location
;
13864 /* Parse a static_assert-declaration.
13866 static_assert-declaration:
13867 static_assert ( constant-expression , string-literal ) ;
13868 static_assert ( constant-expression ) ; (C++17)
13870 If MEMBER_P, this static_assert is a class member. */
13873 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13876 location_t token_loc
;
13880 /* Peek at the `static_assert' token so we can keep track of exactly
13881 where the static assertion started. */
13882 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13884 /* Look for the `static_assert' keyword. */
13885 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13889 /* We know we are in a static assertion; commit to any tentative
13891 if (cp_parser_parsing_tentatively (parser
))
13892 cp_parser_commit_to_tentative_parse (parser
);
13894 /* Parse the `(' starting the static assertion condition. */
13895 matching_parens parens
;
13896 parens
.require_open (parser
);
13898 /* Parse the constant-expression. Allow a non-constant expression
13899 here in order to give better diagnostics in finish_static_assert. */
13901 cp_parser_constant_expression (parser
,
13902 /*allow_non_constant_p=*/true,
13903 /*non_constant_p=*/&dummy
);
13905 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13907 if (cxx_dialect
< cxx17
)
13908 pedwarn (input_location
, OPT_Wpedantic
,
13909 "static_assert without a message "
13910 "only available with -std=c++17 or -std=gnu++17");
13912 cp_lexer_consume_token (parser
->lexer
);
13913 message
= build_string (1, "");
13914 TREE_TYPE (message
) = char_array_type_node
;
13915 fix_string_type (message
);
13919 /* Parse the separating `,'. */
13920 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13922 /* Parse the string-literal message. */
13923 message
= cp_parser_string_literal (parser
,
13924 /*translate=*/false,
13927 /* A `)' completes the static assertion. */
13928 if (!parens
.require_close (parser
))
13929 cp_parser_skip_to_closing_parenthesis (parser
,
13930 /*recovering=*/true,
13931 /*or_comma=*/false,
13932 /*consume_paren=*/true);
13935 /* A semicolon terminates the declaration. */
13936 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13938 /* Get the location for the static assertion. Use that of the
13939 condition if available, otherwise, use that of the "static_assert"
13941 location_t assert_loc
= condition
.get_location ();
13942 if (assert_loc
== UNKNOWN_LOCATION
)
13943 assert_loc
= token_loc
;
13945 /* Complete the static assertion, which may mean either processing
13946 the static assert now or saving it for template instantiation. */
13947 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13950 /* Parse the expression in decltype ( expression ). */
13953 cp_parser_decltype_expr (cp_parser
*parser
,
13954 bool &id_expression_or_member_access_p
)
13956 cp_token
*id_expr_start_token
;
13959 /* Since we're going to preserve any side-effects from this parse, set up a
13960 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13961 in the expression. */
13962 tentative_firewall
firewall (parser
);
13964 /* First, try parsing an id-expression. */
13965 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13966 cp_parser_parse_tentatively (parser
);
13967 expr
= cp_parser_id_expression (parser
,
13968 /*template_keyword_p=*/false,
13969 /*check_dependency_p=*/true,
13970 /*template_p=*/NULL
,
13971 /*declarator_p=*/false,
13972 /*optional_p=*/false);
13974 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13976 bool non_integral_constant_expression_p
= false;
13977 tree id_expression
= expr
;
13979 const char *error_msg
;
13981 if (identifier_p (expr
))
13982 /* Lookup the name we got back from the id-expression. */
13983 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13984 id_expr_start_token
->location
);
13986 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
13987 /* A template without args is not a complete id-expression. */
13988 expr
= error_mark_node
;
13991 && expr
!= error_mark_node
13992 && TREE_CODE (expr
) != TYPE_DECL
13993 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13994 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13995 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13997 /* Complete lookup of the id-expression. */
13998 expr
= (finish_id_expression
13999 (id_expression
, expr
, parser
->scope
, &idk
,
14000 /*integral_constant_expression_p=*/false,
14001 /*allow_non_integral_constant_expression_p=*/true,
14002 &non_integral_constant_expression_p
,
14003 /*template_p=*/false,
14005 /*address_p=*/false,
14006 /*template_arg_p=*/false,
14008 id_expr_start_token
->location
));
14010 if (expr
== error_mark_node
)
14011 /* We found an id-expression, but it was something that we
14012 should not have found. This is an error, not something
14013 we can recover from, so note that we found an
14014 id-expression and we'll recover as gracefully as
14016 id_expression_or_member_access_p
= true;
14020 && expr
!= error_mark_node
14021 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14022 /* We have an id-expression. */
14023 id_expression_or_member_access_p
= true;
14026 if (!id_expression_or_member_access_p
)
14028 /* Abort the id-expression parse. */
14029 cp_parser_abort_tentative_parse (parser
);
14031 /* Parsing tentatively, again. */
14032 cp_parser_parse_tentatively (parser
);
14034 /* Parse a class member access. */
14035 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14036 /*cast_p=*/false, /*decltype*/true,
14037 /*member_access_only_p=*/true, NULL
);
14040 && expr
!= error_mark_node
14041 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14042 /* We have an id-expression. */
14043 id_expression_or_member_access_p
= true;
14046 if (id_expression_or_member_access_p
)
14047 /* We have parsed the complete id-expression or member access. */
14048 cp_parser_parse_definitely (parser
);
14051 /* Abort our attempt to parse an id-expression or member access
14053 cp_parser_abort_tentative_parse (parser
);
14055 /* Commit to the tentative_firewall so we get syntax errors. */
14056 cp_parser_commit_to_tentative_parse (parser
);
14058 /* Parse a full expression. */
14059 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14060 /*decltype_p=*/true);
14066 /* Parse a `decltype' type. Returns the type.
14068 simple-type-specifier:
14069 decltype ( expression )
14071 decltype ( auto ) */
14074 cp_parser_decltype (cp_parser
*parser
)
14076 bool id_expression_or_member_access_p
= false;
14077 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14079 if (start_token
->type
== CPP_DECLTYPE
)
14081 /* Already parsed. */
14082 cp_lexer_consume_token (parser
->lexer
);
14083 return saved_checks_value (start_token
->u
.tree_check_value
);
14086 /* Look for the `decltype' token. */
14087 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14088 return error_mark_node
;
14090 /* Parse the opening `('. */
14091 matching_parens parens
;
14092 if (!parens
.require_open (parser
))
14093 return error_mark_node
;
14095 push_deferring_access_checks (dk_deferred
);
14097 tree expr
= NULL_TREE
;
14099 if (cxx_dialect
>= cxx14
14100 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14101 /* decltype (auto) */
14102 cp_lexer_consume_token (parser
->lexer
);
14105 /* decltype (expression) */
14107 /* Types cannot be defined in a `decltype' expression. Save away the
14108 old message and set the new one. */
14109 const char *saved_message
= parser
->type_definition_forbidden_message
;
14110 parser
->type_definition_forbidden_message
14111 = G_("types may not be defined in %<decltype%> expressions");
14113 /* The restrictions on constant-expressions do not apply inside
14114 decltype expressions. */
14115 bool saved_integral_constant_expression_p
14116 = parser
->integral_constant_expression_p
;
14117 bool saved_non_integral_constant_expression_p
14118 = parser
->non_integral_constant_expression_p
;
14119 parser
->integral_constant_expression_p
= false;
14121 /* Within a parenthesized expression, a `>' token is always
14122 the greater-than operator. */
14123 bool saved_greater_than_is_operator_p
14124 = parser
->greater_than_is_operator_p
;
14125 parser
->greater_than_is_operator_p
= true;
14127 /* Do not actually evaluate the expression. */
14128 ++cp_unevaluated_operand
;
14130 /* Do not warn about problems with the expression. */
14131 ++c_inhibit_evaluation_warnings
;
14133 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14135 /* Go back to evaluating expressions. */
14136 --cp_unevaluated_operand
;
14137 --c_inhibit_evaluation_warnings
;
14139 /* The `>' token might be the end of a template-id or
14140 template-parameter-list now. */
14141 parser
->greater_than_is_operator_p
14142 = saved_greater_than_is_operator_p
;
14144 /* Restore the old message and the integral constant expression
14146 parser
->type_definition_forbidden_message
= saved_message
;
14147 parser
->integral_constant_expression_p
14148 = saved_integral_constant_expression_p
;
14149 parser
->non_integral_constant_expression_p
14150 = saved_non_integral_constant_expression_p
;
14153 /* Parse to the closing `)'. */
14154 if (!parens
.require_close (parser
))
14156 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14157 /*consume_paren=*/true);
14158 pop_deferring_access_checks ();
14159 return error_mark_node
;
14165 expr
= make_decltype_auto ();
14166 AUTO_IS_DECLTYPE (expr
) = true;
14169 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14170 tf_warning_or_error
);
14172 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14174 start_token
->type
= CPP_DECLTYPE
;
14175 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14176 start_token
->u
.tree_check_value
->value
= expr
;
14177 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14178 start_token
->keyword
= RID_MAX
;
14179 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14181 pop_to_parent_deferring_access_checks ();
14186 /* Special member functions [gram.special] */
14188 /* Parse a conversion-function-id.
14190 conversion-function-id:
14191 operator conversion-type-id
14193 Returns an IDENTIFIER_NODE representing the operator. */
14196 cp_parser_conversion_function_id (cp_parser
* parser
)
14200 tree saved_qualifying_scope
;
14201 tree saved_object_scope
;
14202 tree pushed_scope
= NULL_TREE
;
14204 /* Look for the `operator' token. */
14205 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14206 return error_mark_node
;
14207 /* When we parse the conversion-type-id, the current scope will be
14208 reset. However, we need that information in able to look up the
14209 conversion function later, so we save it here. */
14210 saved_scope
= parser
->scope
;
14211 saved_qualifying_scope
= parser
->qualifying_scope
;
14212 saved_object_scope
= parser
->object_scope
;
14213 /* We must enter the scope of the class so that the names of
14214 entities declared within the class are available in the
14215 conversion-type-id. For example, consider:
14222 S::operator I() { ... }
14224 In order to see that `I' is a type-name in the definition, we
14225 must be in the scope of `S'. */
14227 pushed_scope
= push_scope (saved_scope
);
14228 /* Parse the conversion-type-id. */
14229 type
= cp_parser_conversion_type_id (parser
);
14230 /* Leave the scope of the class, if any. */
14232 pop_scope (pushed_scope
);
14233 /* Restore the saved scope. */
14234 parser
->scope
= saved_scope
;
14235 parser
->qualifying_scope
= saved_qualifying_scope
;
14236 parser
->object_scope
= saved_object_scope
;
14237 /* If the TYPE is invalid, indicate failure. */
14238 if (type
== error_mark_node
)
14239 return error_mark_node
;
14240 return make_conv_op_name (type
);
14243 /* Parse a conversion-type-id:
14245 conversion-type-id:
14246 type-specifier-seq conversion-declarator [opt]
14248 Returns the TYPE specified. */
14251 cp_parser_conversion_type_id (cp_parser
* parser
)
14254 cp_decl_specifier_seq type_specifiers
;
14255 cp_declarator
*declarator
;
14256 tree type_specified
;
14257 const char *saved_message
;
14259 /* Parse the attributes. */
14260 attributes
= cp_parser_attributes_opt (parser
);
14262 saved_message
= parser
->type_definition_forbidden_message
;
14263 parser
->type_definition_forbidden_message
14264 = G_("types may not be defined in a conversion-type-id");
14266 /* Parse the type-specifiers. */
14267 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14268 /*is_trailing_return=*/false,
14271 parser
->type_definition_forbidden_message
= saved_message
;
14273 /* If that didn't work, stop. */
14274 if (type_specifiers
.type
== error_mark_node
)
14275 return error_mark_node
;
14276 /* Parse the conversion-declarator. */
14277 declarator
= cp_parser_conversion_declarator_opt (parser
);
14279 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14280 /*initialized=*/0, &attributes
);
14282 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14284 /* Don't give this error when parsing tentatively. This happens to
14285 work because we always parse this definitively once. */
14286 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14287 && type_uses_auto (type_specified
))
14289 if (cxx_dialect
< cxx14
)
14291 error ("invalid use of %<auto%> in conversion operator");
14292 return error_mark_node
;
14294 else if (template_parm_scope_p ())
14295 warning (0, "use of %<auto%> in member template "
14296 "conversion operator can never be deduced");
14299 return type_specified
;
14302 /* Parse an (optional) conversion-declarator.
14304 conversion-declarator:
14305 ptr-operator conversion-declarator [opt]
14309 static cp_declarator
*
14310 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14312 enum tree_code code
;
14313 tree class_type
, std_attributes
= NULL_TREE
;
14314 cp_cv_quals cv_quals
;
14316 /* We don't know if there's a ptr-operator next, or not. */
14317 cp_parser_parse_tentatively (parser
);
14318 /* Try the ptr-operator. */
14319 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14321 /* If it worked, look for more conversion-declarators. */
14322 if (cp_parser_parse_definitely (parser
))
14324 cp_declarator
*declarator
;
14326 /* Parse another optional declarator. */
14327 declarator
= cp_parser_conversion_declarator_opt (parser
);
14329 declarator
= cp_parser_make_indirect_declarator
14330 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14338 /* Parse an (optional) ctor-initializer.
14341 : mem-initializer-list */
14344 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14346 /* If the next token is not a `:', then there is no
14347 ctor-initializer. */
14348 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14350 /* Do default initialization of any bases and members. */
14351 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14352 finish_mem_initializers (NULL_TREE
);
14356 /* Consume the `:' token. */
14357 cp_lexer_consume_token (parser
->lexer
);
14358 /* And the mem-initializer-list. */
14359 cp_parser_mem_initializer_list (parser
);
14362 /* Parse a mem-initializer-list.
14364 mem-initializer-list:
14365 mem-initializer ... [opt]
14366 mem-initializer ... [opt] , mem-initializer-list */
14369 cp_parser_mem_initializer_list (cp_parser
* parser
)
14371 tree mem_initializer_list
= NULL_TREE
;
14372 tree target_ctor
= error_mark_node
;
14373 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14375 /* Let the semantic analysis code know that we are starting the
14376 mem-initializer-list. */
14377 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14378 error_at (token
->location
,
14379 "only constructors take member initializers");
14381 /* Loop through the list. */
14384 tree mem_initializer
;
14386 token
= cp_lexer_peek_token (parser
->lexer
);
14387 /* Parse the mem-initializer. */
14388 mem_initializer
= cp_parser_mem_initializer (parser
);
14389 /* If the next token is a `...', we're expanding member initializers. */
14390 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14392 /* Consume the `...'. */
14393 cp_lexer_consume_token (parser
->lexer
);
14395 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14396 can be expanded but members cannot. */
14397 if (mem_initializer
!= error_mark_node
14398 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14400 error_at (token
->location
,
14401 "cannot expand initializer for member %qD",
14402 TREE_PURPOSE (mem_initializer
));
14403 mem_initializer
= error_mark_node
;
14406 /* Construct the pack expansion type. */
14407 if (mem_initializer
!= error_mark_node
)
14408 mem_initializer
= make_pack_expansion (mem_initializer
);
14410 if (target_ctor
!= error_mark_node
14411 && mem_initializer
!= error_mark_node
)
14413 error ("mem-initializer for %qD follows constructor delegation",
14414 TREE_PURPOSE (mem_initializer
));
14415 mem_initializer
= error_mark_node
;
14417 /* Look for a target constructor. */
14418 if (mem_initializer
!= error_mark_node
14419 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14420 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14422 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14423 if (mem_initializer_list
)
14425 error ("constructor delegation follows mem-initializer for %qD",
14426 TREE_PURPOSE (mem_initializer_list
));
14427 mem_initializer
= error_mark_node
;
14429 target_ctor
= mem_initializer
;
14431 /* Add it to the list, unless it was erroneous. */
14432 if (mem_initializer
!= error_mark_node
)
14434 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14435 mem_initializer_list
= mem_initializer
;
14437 /* If the next token is not a `,', we're done. */
14438 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14440 /* Consume the `,' token. */
14441 cp_lexer_consume_token (parser
->lexer
);
14444 /* Perform semantic analysis. */
14445 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14446 finish_mem_initializers (mem_initializer_list
);
14449 /* Parse a mem-initializer.
14452 mem-initializer-id ( expression-list [opt] )
14453 mem-initializer-id braced-init-list
14458 ( expression-list [opt] )
14460 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14461 class) or FIELD_DECL (for a non-static data member) to initialize;
14462 the TREE_VALUE is the expression-list. An empty initialization
14463 list is represented by void_list_node. */
14466 cp_parser_mem_initializer (cp_parser
* parser
)
14468 tree mem_initializer_id
;
14469 tree expression_list
;
14471 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14473 /* Find out what is being initialized. */
14474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14476 permerror (token
->location
,
14477 "anachronistic old-style base class initializer");
14478 mem_initializer_id
= NULL_TREE
;
14482 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14483 if (mem_initializer_id
== error_mark_node
)
14484 return mem_initializer_id
;
14486 member
= expand_member_init (mem_initializer_id
);
14487 if (member
&& !DECL_P (member
))
14488 in_base_initializer
= 1;
14490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14492 bool expr_non_constant_p
;
14493 cp_lexer_set_source_position (parser
->lexer
);
14494 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14495 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14496 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14497 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14501 vec
<tree
, va_gc
> *vec
;
14502 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14504 /*allow_expansion_p=*/true,
14505 /*non_constant_p=*/NULL
);
14507 return error_mark_node
;
14508 expression_list
= build_tree_list_vec (vec
);
14509 release_tree_vector (vec
);
14512 if (expression_list
== error_mark_node
)
14513 return error_mark_node
;
14514 if (!expression_list
)
14515 expression_list
= void_type_node
;
14517 in_base_initializer
= 0;
14519 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14522 /* Parse a mem-initializer-id.
14524 mem-initializer-id:
14525 :: [opt] nested-name-specifier [opt] class-name
14526 decltype-specifier (C++11)
14529 Returns a TYPE indicating the class to be initialized for the first
14530 production (and the second in C++11). Returns an IDENTIFIER_NODE
14531 indicating the data member to be initialized for the last production. */
14534 cp_parser_mem_initializer_id (cp_parser
* parser
)
14536 bool global_scope_p
;
14537 bool nested_name_specifier_p
;
14538 bool template_p
= false;
14541 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14543 /* `typename' is not allowed in this context ([temp.res]). */
14544 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14546 error_at (token
->location
,
14547 "keyword %<typename%> not allowed in this context (a qualified "
14548 "member initializer is implicitly a type)");
14549 cp_lexer_consume_token (parser
->lexer
);
14551 /* Look for the optional `::' operator. */
14553 = (cp_parser_global_scope_opt (parser
,
14554 /*current_scope_valid_p=*/false)
14556 /* Look for the optional nested-name-specifier. The simplest way to
14561 The keyword `typename' is not permitted in a base-specifier or
14562 mem-initializer; in these contexts a qualified name that
14563 depends on a template-parameter is implicitly assumed to be a
14566 is to assume that we have seen the `typename' keyword at this
14568 nested_name_specifier_p
14569 = (cp_parser_nested_name_specifier_opt (parser
,
14570 /*typename_keyword_p=*/true,
14571 /*check_dependency_p=*/true,
14573 /*is_declaration=*/true)
14575 if (nested_name_specifier_p
)
14576 template_p
= cp_parser_optional_template_keyword (parser
);
14577 /* If there is a `::' operator or a nested-name-specifier, then we
14578 are definitely looking for a class-name. */
14579 if (global_scope_p
|| nested_name_specifier_p
)
14580 return cp_parser_class_name (parser
,
14581 /*typename_keyword_p=*/true,
14582 /*template_keyword_p=*/template_p
,
14584 /*check_dependency_p=*/true,
14585 /*class_head_p=*/false,
14586 /*is_declaration=*/true);
14587 /* Otherwise, we could also be looking for an ordinary identifier. */
14588 cp_parser_parse_tentatively (parser
);
14589 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14590 /* Try a decltype-specifier. */
14591 id
= cp_parser_decltype (parser
);
14593 /* Otherwise, try a class-name. */
14594 id
= cp_parser_class_name (parser
,
14595 /*typename_keyword_p=*/true,
14596 /*template_keyword_p=*/false,
14598 /*check_dependency_p=*/true,
14599 /*class_head_p=*/false,
14600 /*is_declaration=*/true);
14601 /* If we found one, we're done. */
14602 if (cp_parser_parse_definitely (parser
))
14604 /* Otherwise, look for an ordinary identifier. */
14605 return cp_parser_identifier (parser
);
14608 /* Overloading [gram.over] */
14610 /* Parse an operator-function-id.
14612 operator-function-id:
14615 Returns an IDENTIFIER_NODE for the operator which is a
14616 human-readable spelling of the identifier, e.g., `operator +'. */
14619 cp_parser_operator_function_id (cp_parser
* parser
)
14621 /* Look for the `operator' keyword. */
14622 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14623 return error_mark_node
;
14624 /* And then the name of the operator itself. */
14625 return cp_parser_operator (parser
);
14628 /* Return an identifier node for a user-defined literal operator.
14629 The suffix identifier is chained to the operator name identifier. */
14632 cp_literal_operator_id (const char* name
)
14635 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14636 + strlen (name
) + 10);
14637 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14638 identifier
= get_identifier (buffer
);
14643 /* Parse an operator.
14646 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14647 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14648 || ++ -- , ->* -> () []
14655 Returns an IDENTIFIER_NODE for the operator which is a
14656 human-readable spelling of the identifier, e.g., `operator +'. */
14659 cp_parser_operator (cp_parser
* parser
)
14661 tree id
= NULL_TREE
;
14665 /* Peek at the next token. */
14666 token
= cp_lexer_peek_token (parser
->lexer
);
14668 location_t start_loc
= token
->location
;
14670 /* Figure out which operator we have. */
14671 enum tree_code op
= ERROR_MARK
;
14672 bool assop
= false;
14673 bool consumed
= false;
14674 switch (token
->type
)
14678 /* The keyword should be either `new' or `delete'. */
14679 if (token
->keyword
== RID_NEW
)
14681 else if (token
->keyword
== RID_DELETE
)
14686 /* Consume the `new' or `delete' token. */
14687 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14689 /* Peek at the next token. */
14690 token
= cp_lexer_peek_token (parser
->lexer
);
14691 /* If it's a `[' token then this is the array variant of the
14693 if (token
->type
== CPP_OPEN_SQUARE
)
14695 /* Consume the `[' token. */
14696 cp_lexer_consume_token (parser
->lexer
);
14697 /* Look for the `]' token. */
14698 if (cp_token
*close_token
14699 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14700 end_loc
= close_token
->location
;
14701 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14703 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14721 op
= TRUNC_DIV_EXPR
;
14725 op
= TRUNC_MOD_EXPR
;
14745 op
= TRUTH_NOT_EXPR
;
14778 op
= TRUNC_DIV_EXPR
;
14783 op
= TRUNC_MOD_EXPR
;
14809 case CPP_LSHIFT_EQ
:
14814 case CPP_RSHIFT_EQ
:
14831 case CPP_GREATER_EQ
:
14836 op
= TRUTH_ANDIF_EXPR
;
14840 op
= TRUTH_ORIF_EXPR
;
14843 case CPP_PLUS_PLUS
:
14844 op
= POSTINCREMENT_EXPR
;
14847 case CPP_MINUS_MINUS
:
14848 op
= PREDECREMENT_EXPR
;
14852 op
= COMPOUND_EXPR
;
14855 case CPP_DEREF_STAR
:
14860 op
= COMPONENT_REF
;
14863 case CPP_OPEN_PAREN
:
14865 /* Consume the `('. */
14866 matching_parens parens
;
14867 parens
.consume_open (parser
);
14868 /* Look for the matching `)'. */
14869 parens
.require_close (parser
);
14875 case CPP_OPEN_SQUARE
:
14876 /* Consume the `['. */
14877 cp_lexer_consume_token (parser
->lexer
);
14878 /* Look for the matching `]'. */
14879 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14884 case CPP_UTF8STRING
:
14885 case CPP_UTF8STRING_USERDEF
:
14892 case CPP_STRING_USERDEF
:
14893 case CPP_WSTRING_USERDEF
:
14894 case CPP_STRING16_USERDEF
:
14895 case CPP_STRING32_USERDEF
:
14897 tree str
, string_tree
;
14900 if (cxx_dialect
== cxx98
)
14901 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14903 /* Consume the string. */
14904 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14905 /*wide_ok=*/true, /*lookup_udlit=*/false);
14906 if (str
== error_mark_node
)
14907 return error_mark_node
;
14908 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14910 string_tree
= USERDEF_LITERAL_VALUE (str
);
14911 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14916 /* Look for the suffix identifier. */
14917 token
= cp_lexer_peek_token (parser
->lexer
);
14918 if (token
->type
== CPP_NAME
)
14919 id
= cp_parser_identifier (parser
);
14920 else if (token
->type
== CPP_KEYWORD
)
14922 error ("unexpected keyword;"
14923 " remove space between quotes and suffix identifier");
14924 return error_mark_node
;
14928 error ("expected suffix identifier");
14929 return error_mark_node
;
14932 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14933 (TREE_TYPE (TREE_TYPE (string_tree
))));
14934 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14937 error ("expected empty string after %<operator%> keyword");
14938 return error_mark_node
;
14940 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14943 error ("invalid encoding prefix in literal operator");
14944 return error_mark_node
;
14946 if (id
!= error_mark_node
)
14948 const char *name
= IDENTIFIER_POINTER (id
);
14949 id
= cp_literal_operator_id (name
);
14955 /* Anything else is an error. */
14959 /* If we have selected an identifier, we need to consume the
14961 if (op
!= ERROR_MARK
)
14963 id
= ovl_op_identifier (assop
, op
);
14965 cp_lexer_consume_token (parser
->lexer
);
14967 /* Otherwise, no valid operator name was present. */
14970 cp_parser_error (parser
, "expected operator");
14971 id
= error_mark_node
;
14974 return cp_expr (id
, start_loc
);
14977 /* Parse a template-declaration.
14979 template-declaration:
14980 export [opt] template < template-parameter-list > declaration
14982 If MEMBER_P is TRUE, this template-declaration occurs within a
14985 The grammar rule given by the standard isn't correct. What
14986 is really meant is:
14988 template-declaration:
14989 export [opt] template-parameter-list-seq
14990 decl-specifier-seq [opt] init-declarator [opt] ;
14991 export [opt] template-parameter-list-seq
14992 function-definition
14994 template-parameter-list-seq:
14995 template-parameter-list-seq [opt]
14996 template < template-parameter-list >
14998 Concept Extensions:
15000 template-parameter-list-seq:
15001 template < template-parameter-list > requires-clause [opt]
15004 requires logical-or-expression */
15007 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15009 /* Check for `export'. */
15010 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15012 /* Consume the `export' token. */
15013 cp_lexer_consume_token (parser
->lexer
);
15014 /* Warn that we do not support `export'. */
15015 warning (0, "keyword %<export%> not implemented, and will be ignored");
15018 cp_parser_template_declaration_after_export (parser
, member_p
);
15021 /* Parse a template-parameter-list.
15023 template-parameter-list:
15025 template-parameter-list , template-parameter
15027 Returns a TREE_LIST. Each node represents a template parameter.
15028 The nodes are connected via their TREE_CHAINs. */
15031 cp_parser_template_parameter_list (cp_parser
* parser
)
15033 tree parameter_list
= NULL_TREE
;
15035 begin_template_parm_list ();
15037 /* The loop below parses the template parms. We first need to know
15038 the total number of template parms to be able to compute proper
15039 canonical types of each dependent type. So after the loop, when
15040 we know the total number of template parms,
15041 end_template_parm_list computes the proper canonical types and
15042 fixes up the dependent types accordingly. */
15047 bool is_parameter_pack
;
15048 location_t parm_loc
;
15050 /* Parse the template-parameter. */
15051 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15052 parameter
= cp_parser_template_parameter (parser
,
15054 &is_parameter_pack
);
15055 /* Add it to the list. */
15056 if (parameter
!= error_mark_node
)
15057 parameter_list
= process_template_parm (parameter_list
,
15061 is_parameter_pack
);
15064 tree err_parm
= build_tree_list (parameter
, parameter
);
15065 parameter_list
= chainon (parameter_list
, err_parm
);
15068 /* If the next token is not a `,', we're done. */
15069 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15071 /* Otherwise, consume the `,' token. */
15072 cp_lexer_consume_token (parser
->lexer
);
15075 return end_template_parm_list (parameter_list
);
15078 /* Parse a introduction-list.
15081 introduced-parameter
15082 introduction-list , introduced-parameter
15084 introduced-parameter:
15085 ...[opt] identifier
15087 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15088 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15089 WILDCARD_DECL will also have DECL_NAME set and token location in
15090 DECL_SOURCE_LOCATION. */
15093 cp_parser_introduction_list (cp_parser
*parser
)
15095 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15099 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15101 cp_lexer_consume_token (parser
->lexer
);
15103 /* Build placeholder. */
15104 tree parm
= build_nt (WILDCARD_DECL
);
15105 DECL_SOURCE_LOCATION (parm
)
15106 = cp_lexer_peek_token (parser
->lexer
)->location
;
15107 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15108 WILDCARD_PACK_P (parm
) = is_pack
;
15109 vec_safe_push (introduction_vec
, parm
);
15111 /* If the next token is not a `,', we're done. */
15112 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15114 /* Otherwise, consume the `,' token. */
15115 cp_lexer_consume_token (parser
->lexer
);
15118 /* Convert the vec into a TREE_VEC. */
15119 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15122 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15123 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15125 release_tree_vector (introduction_vec
);
15126 return introduction_list
;
15129 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15130 is an abstract declarator. */
15132 static inline cp_declarator
*
15133 get_id_declarator (cp_declarator
*declarator
)
15135 cp_declarator
*d
= declarator
;
15136 while (d
&& d
->kind
!= cdk_id
)
15141 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15142 is an abstract declarator. */
15145 get_unqualified_id (cp_declarator
*declarator
)
15147 declarator
= get_id_declarator (declarator
);
15149 return declarator
->u
.id
.unqualified_name
;
15154 /* Returns true if DECL represents a constrained-parameter. */
15157 is_constrained_parameter (tree decl
)
15160 && TREE_CODE (decl
) == TYPE_DECL
15161 && CONSTRAINED_PARM_CONCEPT (decl
)
15162 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15165 /* Returns true if PARM declares a constrained-parameter. */
15168 is_constrained_parameter (cp_parameter_declarator
*parm
)
15170 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15173 /* Check that the type parameter is only a declarator-id, and that its
15174 type is not cv-qualified. */
15177 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15178 cp_parameter_declarator
*parm
)
15180 if (!parm
->declarator
)
15183 if (parm
->declarator
->kind
!= cdk_id
)
15185 cp_parser_error (parser
, "invalid constrained type parameter");
15189 /* Don't allow cv-qualified type parameters. */
15190 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15191 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15193 cp_parser_error (parser
, "cv-qualified type parameter");
15200 /* Finish parsing/processing a template type parameter and checking
15201 various restrictions. */
15204 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15206 cp_parameter_declarator
* parmdecl
)
15208 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15209 return finish_template_type_parm (class_type_node
, id
);
15211 return error_mark_node
;
15215 finish_constrained_template_template_parm (tree proto
, tree id
)
15217 /* FIXME: This should probably be copied, and we may need to adjust
15218 the template parameter depths. */
15219 tree saved_parms
= current_template_parms
;
15220 begin_template_parm_list ();
15221 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15222 end_template_parm_list ();
15224 tree parm
= finish_template_template_parm (class_type_node
, id
);
15225 current_template_parms
= saved_parms
;
15230 /* Finish parsing/processing a template template parameter by borrowing
15231 the template parameter list from the prototype parameter. */
15234 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15237 cp_parameter_declarator
*parmdecl
)
15239 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15240 return error_mark_node
;
15241 return finish_constrained_template_template_parm (proto
, id
);
15244 /* Create a new non-type template parameter from the given PARM
15248 constrained_non_type_template_parm (bool *is_non_type
,
15249 cp_parameter_declarator
*parm
)
15251 *is_non_type
= true;
15252 cp_declarator
*decl
= parm
->declarator
;
15253 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15254 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15255 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15258 /* Build a constrained template parameter based on the PARMDECL
15259 declarator. The type of PARMDECL is the constrained type, which
15260 refers to the prototype template parameter that ultimately
15261 specifies the type of the declared parameter. */
15264 finish_constrained_parameter (cp_parser
*parser
,
15265 cp_parameter_declarator
*parmdecl
,
15267 bool *is_parameter_pack
)
15269 tree decl
= parmdecl
->decl_specifiers
.type
;
15270 tree id
= get_unqualified_id (parmdecl
->declarator
);
15271 tree def
= parmdecl
->default_argument
;
15272 tree proto
= DECL_INITIAL (decl
);
15274 /* A template parameter constrained by a variadic concept shall also
15275 be declared as a template parameter pack. */
15276 bool is_variadic
= template_parameter_pack_p (proto
);
15277 if (is_variadic
&& !*is_parameter_pack
)
15278 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15280 /* Build the parameter. Return an error if the declarator was invalid. */
15282 if (TREE_CODE (proto
) == TYPE_DECL
)
15283 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15284 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15285 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15288 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15289 if (parm
== error_mark_node
)
15290 return error_mark_node
;
15292 /* Finish the parameter decl and create a node attaching the
15293 default argument and constraint. */
15294 parm
= build_tree_list (def
, parm
);
15295 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15300 /* Returns true if the parsed type actually represents the declaration
15301 of a type template-parameter. */
15304 declares_constrained_type_template_parameter (tree type
)
15306 return (is_constrained_parameter (type
)
15307 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15311 /* Returns true if the parsed type actually represents the declaration of
15312 a template template-parameter. */
15315 declares_constrained_template_template_parameter (tree type
)
15317 return (is_constrained_parameter (type
)
15318 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15321 /* Parse a default argument for a type template-parameter.
15322 Note that diagnostics are handled in cp_parser_template_parameter. */
15325 cp_parser_default_type_template_argument (cp_parser
*parser
)
15327 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15329 /* Consume the `=' token. */
15330 cp_lexer_consume_token (parser
->lexer
);
15332 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15334 /* Parse the default-argument. */
15335 push_deferring_access_checks (dk_no_deferred
);
15336 tree default_argument
= cp_parser_type_id (parser
);
15337 pop_deferring_access_checks ();
15339 if (flag_concepts
&& type_uses_auto (default_argument
))
15341 error_at (token
->location
,
15342 "invalid use of %<auto%> in default template argument");
15343 return error_mark_node
;
15346 return default_argument
;
15349 /* Parse a default argument for a template template-parameter. */
15352 cp_parser_default_template_template_argument (cp_parser
*parser
)
15354 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15358 /* Consume the `='. */
15359 cp_lexer_consume_token (parser
->lexer
);
15360 /* Parse the id-expression. */
15361 push_deferring_access_checks (dk_no_deferred
);
15362 /* save token before parsing the id-expression, for error
15364 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15365 tree default_argument
15366 = cp_parser_id_expression (parser
,
15367 /*template_keyword_p=*/false,
15368 /*check_dependency_p=*/true,
15369 /*template_p=*/&is_template
,
15370 /*declarator_p=*/false,
15371 /*optional_p=*/false);
15372 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15373 /* If the id-expression was a template-id that refers to
15374 a template-class, we already have the declaration here,
15375 so no further lookup is needed. */
15378 /* Look up the name. */
15380 = cp_parser_lookup_name (parser
, default_argument
,
15382 /*is_template=*/is_template
,
15383 /*is_namespace=*/false,
15384 /*check_dependency=*/true,
15385 /*ambiguous_decls=*/NULL
,
15387 /* See if the default argument is valid. */
15388 default_argument
= check_template_template_default_arg (default_argument
);
15389 pop_deferring_access_checks ();
15390 return default_argument
;
15393 /* Parse a template-parameter.
15395 template-parameter:
15397 parameter-declaration
15399 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15400 the parameter. The TREE_PURPOSE is the default value, if any.
15401 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15402 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15403 set to true iff this parameter is a parameter pack. */
15406 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15407 bool *is_parameter_pack
)
15410 cp_parameter_declarator
*parameter_declarator
;
15413 /* Assume it is a type parameter or a template parameter. */
15414 *is_non_type
= false;
15415 /* Assume it not a parameter pack. */
15416 *is_parameter_pack
= false;
15417 /* Peek at the next token. */
15418 token
= cp_lexer_peek_token (parser
->lexer
);
15419 /* If it is `template', we have a type-parameter. */
15420 if (token
->keyword
== RID_TEMPLATE
)
15421 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15422 /* If it is `class' or `typename' we do not know yet whether it is a
15423 type parameter or a non-type parameter. Consider:
15425 template <typename T, typename T::X X> ...
15429 template <class C, class D*> ...
15431 Here, the first parameter is a type parameter, and the second is
15432 a non-type parameter. We can tell by looking at the token after
15433 the identifier -- if it is a `,', `=', or `>' then we have a type
15435 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15437 /* Peek at the token after `class' or `typename'. */
15438 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15439 /* If it's an ellipsis, we have a template type parameter
15441 if (token
->type
== CPP_ELLIPSIS
)
15442 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15443 /* If it's an identifier, skip it. */
15444 if (token
->type
== CPP_NAME
)
15445 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15446 /* Now, see if the token looks like the end of a template
15448 if (token
->type
== CPP_COMMA
15449 || token
->type
== CPP_EQ
15450 || token
->type
== CPP_GREATER
)
15451 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15454 /* Otherwise, it is a non-type parameter or a constrained parameter.
15458 When parsing a default template-argument for a non-type
15459 template-parameter, the first non-nested `>' is taken as the end
15460 of the template parameter-list rather than a greater-than
15462 parameter_declarator
15463 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15464 /*parenthesized_p=*/NULL
);
15466 if (!parameter_declarator
)
15467 return error_mark_node
;
15469 /* If the parameter declaration is marked as a parameter pack, set
15470 *IS_PARAMETER_PACK to notify the caller. */
15471 if (parameter_declarator
->template_parameter_pack_p
)
15472 *is_parameter_pack
= true;
15474 if (parameter_declarator
->default_argument
)
15476 /* Can happen in some cases of erroneous input (c++/34892). */
15477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15478 /* Consume the `...' for better error recovery. */
15479 cp_lexer_consume_token (parser
->lexer
);
15482 // The parameter may have been constrained.
15483 if (is_constrained_parameter (parameter_declarator
))
15484 return finish_constrained_parameter (parser
,
15485 parameter_declarator
,
15487 is_parameter_pack
);
15489 // Now we're sure that the parameter is a non-type parameter.
15490 *is_non_type
= true;
15492 parm
= grokdeclarator (parameter_declarator
->declarator
,
15493 ¶meter_declarator
->decl_specifiers
,
15494 TPARM
, /*initialized=*/0,
15495 /*attrlist=*/NULL
);
15496 if (parm
== error_mark_node
)
15497 return error_mark_node
;
15499 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15502 /* Parse a type-parameter.
15505 class identifier [opt]
15506 class identifier [opt] = type-id
15507 typename identifier [opt]
15508 typename identifier [opt] = type-id
15509 template < template-parameter-list > class identifier [opt]
15510 template < template-parameter-list > class identifier [opt]
15513 GNU Extension (variadic templates):
15516 class ... identifier [opt]
15517 typename ... identifier [opt]
15519 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15520 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15521 the declaration of the parameter.
15523 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15526 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15531 /* Look for a keyword to tell us what kind of parameter this is. */
15532 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15534 return error_mark_node
;
15536 switch (token
->keyword
)
15542 tree default_argument
;
15544 /* If the next token is an ellipsis, we have a template
15546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15548 /* Consume the `...' token. */
15549 cp_lexer_consume_token (parser
->lexer
);
15550 maybe_warn_variadic_templates ();
15552 *is_parameter_pack
= true;
15555 /* If the next token is an identifier, then it names the
15557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15558 identifier
= cp_parser_identifier (parser
);
15560 identifier
= NULL_TREE
;
15562 /* Create the parameter. */
15563 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15565 /* If the next token is an `=', we have a default argument. */
15566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15569 = cp_parser_default_type_template_argument (parser
);
15571 /* Template parameter packs cannot have default
15573 if (*is_parameter_pack
)
15576 error_at (token
->location
,
15577 "template parameter pack %qD cannot have a "
15578 "default argument", identifier
);
15580 error_at (token
->location
,
15581 "template parameter packs cannot have "
15582 "default arguments");
15583 default_argument
= NULL_TREE
;
15585 else if (check_for_bare_parameter_packs (default_argument
))
15586 default_argument
= error_mark_node
;
15589 default_argument
= NULL_TREE
;
15591 /* Create the combined representation of the parameter and the
15592 default argument. */
15593 parameter
= build_tree_list (default_argument
, parameter
);
15600 tree default_argument
;
15602 /* Look for the `<'. */
15603 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15604 /* Parse the template-parameter-list. */
15605 cp_parser_template_parameter_list (parser
);
15606 /* Look for the `>'. */
15607 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15609 // If template requirements are present, parse them.
15612 tree reqs
= get_shorthand_constraints (current_template_parms
);
15613 if (tree r
= cp_parser_requires_clause_opt (parser
))
15614 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15615 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15618 /* Look for the `class' or 'typename' keywords. */
15619 cp_parser_type_parameter_key (parser
);
15620 /* If the next token is an ellipsis, we have a template
15622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15624 /* Consume the `...' token. */
15625 cp_lexer_consume_token (parser
->lexer
);
15626 maybe_warn_variadic_templates ();
15628 *is_parameter_pack
= true;
15630 /* If the next token is an `=', then there is a
15631 default-argument. If the next token is a `>', we are at
15632 the end of the parameter-list. If the next token is a `,',
15633 then we are at the end of this parameter. */
15634 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15635 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15636 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15638 identifier
= cp_parser_identifier (parser
);
15639 /* Treat invalid names as if the parameter were nameless. */
15640 if (identifier
== error_mark_node
)
15641 identifier
= NULL_TREE
;
15644 identifier
= NULL_TREE
;
15646 /* Create the template parameter. */
15647 parameter
= finish_template_template_parm (class_type_node
,
15650 /* If the next token is an `=', then there is a
15651 default-argument. */
15652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15655 = cp_parser_default_template_template_argument (parser
);
15657 /* Template parameter packs cannot have default
15659 if (*is_parameter_pack
)
15662 error_at (token
->location
,
15663 "template parameter pack %qD cannot "
15664 "have a default argument",
15667 error_at (token
->location
, "template parameter packs cannot "
15668 "have default arguments");
15669 default_argument
= NULL_TREE
;
15673 default_argument
= NULL_TREE
;
15675 /* Create the combined representation of the parameter and the
15676 default argument. */
15677 parameter
= build_tree_list (default_argument
, parameter
);
15682 gcc_unreachable ();
15689 /* Parse a template-id.
15692 template-name < template-argument-list [opt] >
15694 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15695 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15696 returned. Otherwise, if the template-name names a function, or set
15697 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15698 names a class, returns a TYPE_DECL for the specialization.
15700 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15701 uninstantiated templates. */
15704 cp_parser_template_id (cp_parser
*parser
,
15705 bool template_keyword_p
,
15706 bool check_dependency_p
,
15707 enum tag_types tag_type
,
15708 bool is_declaration
)
15713 cp_token_position start_of_id
= 0;
15714 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15715 bool is_identifier
;
15717 /* If the next token corresponds to a template-id, there is no need
15719 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15720 if (token
->type
== CPP_TEMPLATE_ID
)
15722 cp_lexer_consume_token (parser
->lexer
);
15723 return saved_checks_value (token
->u
.tree_check_value
);
15726 /* Avoid performing name lookup if there is no possibility of
15727 finding a template-id. */
15728 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15729 || (token
->type
== CPP_NAME
15730 && !cp_parser_nth_token_starts_template_argument_list_p
15733 cp_parser_error (parser
, "expected template-id");
15734 return error_mark_node
;
15737 /* Remember where the template-id starts. */
15738 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15739 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15741 push_deferring_access_checks (dk_deferred
);
15743 /* Parse the template-name. */
15744 is_identifier
= false;
15745 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15746 check_dependency_p
,
15750 if (templ
== error_mark_node
|| is_identifier
)
15752 pop_deferring_access_checks ();
15756 /* Since we're going to preserve any side-effects from this parse, set up a
15757 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15758 in the template arguments. */
15759 tentative_firewall
firewall (parser
);
15761 /* If we find the sequence `[:' after a template-name, it's probably
15762 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15763 parse correctly the argument list. */
15764 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15765 == CPP_OPEN_SQUARE
)
15766 && next_token
->flags
& DIGRAPH
15767 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15769 && !(next_token_2
->flags
& PREV_WHITE
))
15771 cp_parser_parse_tentatively (parser
);
15772 /* Change `:' into `::'. */
15773 next_token_2
->type
= CPP_SCOPE
;
15774 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15776 cp_lexer_consume_token (parser
->lexer
);
15778 /* Parse the arguments. */
15779 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15780 if (!cp_parser_parse_definitely (parser
))
15782 /* If we couldn't parse an argument list, then we revert our changes
15783 and return simply an error. Maybe this is not a template-id
15785 next_token_2
->type
= CPP_COLON
;
15786 cp_parser_error (parser
, "expected %<<%>");
15787 pop_deferring_access_checks ();
15788 return error_mark_node
;
15790 /* Otherwise, emit an error about the invalid digraph, but continue
15791 parsing because we got our argument list. */
15792 if (permerror (next_token
->location
,
15793 "%<<::%> cannot begin a template-argument list"))
15795 static bool hint
= false;
15796 inform (next_token
->location
,
15797 "%<<:%> is an alternate spelling for %<[%>."
15798 " Insert whitespace between %<<%> and %<::%>");
15799 if (!hint
&& !flag_permissive
)
15801 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15802 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15803 "accept your code)");
15810 /* Look for the `<' that starts the template-argument-list. */
15811 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15813 pop_deferring_access_checks ();
15814 return error_mark_node
;
15816 /* Parse the arguments. */
15817 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15820 /* Set the location to be of the form:
15821 template-name < template-argument-list [opt] >
15822 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15823 with caret == start at the start of the template-name,
15824 ranging until the closing '>'. */
15825 location_t finish_loc
15826 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15827 location_t combined_loc
15828 = make_location (token
->location
, token
->location
, finish_loc
);
15830 /* Build a representation of the specialization. */
15831 if (identifier_p (templ
))
15832 template_id
= build_min_nt_loc (combined_loc
,
15835 else if (DECL_TYPE_TEMPLATE_P (templ
)
15836 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15838 bool entering_scope
;
15839 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15840 template (rather than some instantiation thereof) only if
15841 is not nested within some other construct. For example, in
15842 "template <typename T> void f(T) { A<T>::", A<T> is just an
15843 instantiation of A. */
15844 entering_scope
= (template_parm_scope_p ()
15845 && cp_lexer_next_token_is (parser
->lexer
,
15848 = finish_template_type (templ
, arguments
, entering_scope
);
15850 /* A template-like identifier may be a partial concept id. */
15851 else if (flag_concepts
15852 && (template_id
= (cp_parser_maybe_partial_concept_id
15853 (parser
, templ
, arguments
))))
15854 return template_id
;
15855 else if (variable_template_p (templ
))
15857 template_id
= lookup_template_variable (templ
, arguments
);
15858 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15859 SET_EXPR_LOCATION (template_id
, combined_loc
);
15863 /* If it's not a class-template or a template-template, it should be
15864 a function-template. */
15865 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15866 || TREE_CODE (templ
) == OVERLOAD
15867 || BASELINK_P (templ
)));
15869 template_id
= lookup_template_function (templ
, arguments
);
15870 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15871 SET_EXPR_LOCATION (template_id
, combined_loc
);
15874 /* If parsing tentatively, replace the sequence of tokens that makes
15875 up the template-id with a CPP_TEMPLATE_ID token. That way,
15876 should we re-parse the token stream, we will not have to repeat
15877 the effort required to do the parse, nor will we issue duplicate
15878 error messages about problems during instantiation of the
15881 /* Don't do this if we had a parse error in a declarator; re-parsing
15882 might succeed if a name changes meaning (60361). */
15883 && !(cp_parser_error_occurred (parser
)
15884 && cp_parser_parsing_tentatively (parser
)
15885 && parser
->in_declarator_p
))
15887 /* Reset the contents of the START_OF_ID token. */
15888 token
->type
= CPP_TEMPLATE_ID
;
15889 token
->location
= combined_loc
;
15891 /* We must mark the lookup as kept, so we don't throw it away on
15892 the first parse. */
15893 if (is_overloaded_fn (template_id
))
15894 lookup_keep (get_fns (template_id
), true);
15896 /* Retrieve any deferred checks. Do not pop this access checks yet
15897 so the memory will not be reclaimed during token replacing below. */
15898 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15899 token
->u
.tree_check_value
->value
= template_id
;
15900 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15901 token
->keyword
= RID_MAX
;
15903 /* Purge all subsequent tokens. */
15904 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15906 /* ??? Can we actually assume that, if template_id ==
15907 error_mark_node, we will have issued a diagnostic to the
15908 user, as opposed to simply marking the tentative parse as
15910 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15911 error_at (token
->location
, "parse error in template argument list");
15914 pop_to_parent_deferring_access_checks ();
15915 return template_id
;
15918 /* Parse a template-name.
15923 The standard should actually say:
15927 operator-function-id
15929 A defect report has been filed about this issue.
15931 A conversion-function-id cannot be a template name because they cannot
15932 be part of a template-id. In fact, looking at this code:
15934 a.operator K<int>()
15936 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15937 It is impossible to call a templated conversion-function-id with an
15938 explicit argument list, since the only allowed template parameter is
15939 the type to which it is converting.
15941 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15942 `template' keyword, in a construction like:
15946 In that case `f' is taken to be a template-name, even though there
15947 is no way of knowing for sure.
15949 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15950 name refers to a set of overloaded functions, at least one of which
15951 is a template, or an IDENTIFIER_NODE with the name of the template,
15952 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15953 names are looked up inside uninstantiated templates. */
15956 cp_parser_template_name (cp_parser
* parser
,
15957 bool template_keyword_p
,
15958 bool check_dependency_p
,
15959 bool is_declaration
,
15960 enum tag_types tag_type
,
15961 bool *is_identifier
)
15965 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15967 /* If the next token is `operator', then we have either an
15968 operator-function-id or a conversion-function-id. */
15969 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15971 /* We don't know whether we're looking at an
15972 operator-function-id or a conversion-function-id. */
15973 cp_parser_parse_tentatively (parser
);
15974 /* Try an operator-function-id. */
15975 identifier
= cp_parser_operator_function_id (parser
);
15976 /* If that didn't work, try a conversion-function-id. */
15977 if (!cp_parser_parse_definitely (parser
))
15979 cp_parser_error (parser
, "expected template-name");
15980 return error_mark_node
;
15983 /* Look for the identifier. */
15985 identifier
= cp_parser_identifier (parser
);
15987 /* If we didn't find an identifier, we don't have a template-id. */
15988 if (identifier
== error_mark_node
)
15989 return error_mark_node
;
15991 /* If the name immediately followed the `template' keyword, then it
15992 is a template-name. However, if the next token is not `<', then
15993 we do not treat it as a template-name, since it is not being used
15994 as part of a template-id. This enables us to handle constructs
15997 template <typename T> struct S { S(); };
15998 template <typename T> S<T>::S();
16000 correctly. We would treat `S' as a template -- if it were `S<T>'
16001 -- but we do not if there is no `<'. */
16003 if (processing_template_decl
16004 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16006 /* In a declaration, in a dependent context, we pretend that the
16007 "template" keyword was present in order to improve error
16008 recovery. For example, given:
16010 template <typename T> void f(T::X<int>);
16012 we want to treat "X<int>" as a template-id. */
16014 && !template_keyword_p
16015 && parser
->scope
&& TYPE_P (parser
->scope
)
16016 && check_dependency_p
16017 && dependent_scope_p (parser
->scope
)
16018 /* Do not do this for dtors (or ctors), since they never
16019 need the template keyword before their name. */
16020 && !constructor_name_p (identifier
, parser
->scope
))
16022 cp_token_position start
= 0;
16024 /* Explain what went wrong. */
16025 error_at (token
->location
, "non-template %qD used as template",
16027 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16028 parser
->scope
, identifier
);
16029 /* If parsing tentatively, find the location of the "<" token. */
16030 if (cp_parser_simulate_error (parser
))
16031 start
= cp_lexer_token_position (parser
->lexer
, true);
16032 /* Parse the template arguments so that we can issue error
16033 messages about them. */
16034 cp_lexer_consume_token (parser
->lexer
);
16035 cp_parser_enclosed_template_argument_list (parser
);
16036 /* Skip tokens until we find a good place from which to
16037 continue parsing. */
16038 cp_parser_skip_to_closing_parenthesis (parser
,
16039 /*recovering=*/true,
16041 /*consume_paren=*/false);
16042 /* If parsing tentatively, permanently remove the
16043 template argument list. That will prevent duplicate
16044 error messages from being issued about the missing
16045 "template" keyword. */
16047 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16049 *is_identifier
= true;
16050 parser
->context
->object_type
= NULL_TREE
;
16054 /* If the "template" keyword is present, then there is generally
16055 no point in doing name-lookup, so we just return IDENTIFIER.
16056 But, if the qualifying scope is non-dependent then we can
16057 (and must) do name-lookup normally. */
16058 if (template_keyword_p
)
16060 tree scope
= (parser
->scope
? parser
->scope
16061 : parser
->context
->object_type
);
16062 if (scope
&& TYPE_P (scope
)
16063 && (!CLASS_TYPE_P (scope
)
16064 || (check_dependency_p
&& dependent_type_p (scope
))))
16066 /* We're optimizing away the call to cp_parser_lookup_name, but
16067 we still need to do this. */
16068 parser
->context
->object_type
= NULL_TREE
;
16074 /* Look up the name. */
16075 decl
= cp_parser_lookup_name (parser
, identifier
,
16077 /*is_template=*/true,
16078 /*is_namespace=*/false,
16079 check_dependency_p
,
16080 /*ambiguous_decls=*/NULL
,
16083 decl
= strip_using_decl (decl
);
16085 /* If DECL is a template, then the name was a template-name. */
16086 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16088 if (TREE_DEPRECATED (decl
)
16089 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16090 warn_deprecated_use (decl
, NULL_TREE
);
16094 /* The standard does not explicitly indicate whether a name that
16095 names a set of overloaded declarations, some of which are
16096 templates, is a template-name. However, such a name should
16097 be a template-name; otherwise, there is no way to form a
16098 template-id for the overloaded templates. */
16099 bool found
= false;
16101 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16102 !found
&& iter
; ++iter
)
16103 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16108 /* The name does not name a template. */
16109 cp_parser_error (parser
, "expected template-name");
16110 return error_mark_node
;
16114 /* If DECL is dependent, and refers to a function, then just return
16115 its name; we will look it up again during template instantiation. */
16116 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16118 tree scope
= ovl_scope (decl
);
16119 if (TYPE_P (scope
) && dependent_type_p (scope
))
16126 /* Parse a template-argument-list.
16128 template-argument-list:
16129 template-argument ... [opt]
16130 template-argument-list , template-argument ... [opt]
16132 Returns a TREE_VEC containing the arguments. */
16135 cp_parser_template_argument_list (cp_parser
* parser
)
16137 tree fixed_args
[10];
16138 unsigned n_args
= 0;
16139 unsigned alloced
= 10;
16140 tree
*arg_ary
= fixed_args
;
16142 bool saved_in_template_argument_list_p
;
16144 bool saved_non_ice_p
;
16146 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16147 parser
->in_template_argument_list_p
= true;
16148 /* Even if the template-id appears in an integral
16149 constant-expression, the contents of the argument list do
16151 saved_ice_p
= parser
->integral_constant_expression_p
;
16152 parser
->integral_constant_expression_p
= false;
16153 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16154 parser
->non_integral_constant_expression_p
= false;
16156 /* Parse the arguments. */
16162 /* Consume the comma. */
16163 cp_lexer_consume_token (parser
->lexer
);
16165 /* Parse the template-argument. */
16166 argument
= cp_parser_template_argument (parser
);
16168 /* If the next token is an ellipsis, we're expanding a template
16170 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16172 if (argument
== error_mark_node
)
16174 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16175 error_at (token
->location
,
16176 "expected parameter pack before %<...%>");
16178 /* Consume the `...' token. */
16179 cp_lexer_consume_token (parser
->lexer
);
16181 /* Make the argument into a TYPE_PACK_EXPANSION or
16182 EXPR_PACK_EXPANSION. */
16183 argument
= make_pack_expansion (argument
);
16186 if (n_args
== alloced
)
16190 if (arg_ary
== fixed_args
)
16192 arg_ary
= XNEWVEC (tree
, alloced
);
16193 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16196 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16198 arg_ary
[n_args
++] = argument
;
16200 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16202 vec
= make_tree_vec (n_args
);
16205 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16207 if (arg_ary
!= fixed_args
)
16209 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16210 parser
->integral_constant_expression_p
= saved_ice_p
;
16211 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16213 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16217 /* Parse a template-argument.
16220 assignment-expression
16224 The representation is that of an assignment-expression, type-id, or
16225 id-expression -- except that the qualified id-expression is
16226 evaluated, so that the value returned is either a DECL or an
16229 Although the standard says "assignment-expression", it forbids
16230 throw-expressions or assignments in the template argument.
16231 Therefore, we use "conditional-expression" instead. */
16234 cp_parser_template_argument (cp_parser
* parser
)
16239 bool maybe_type_id
= false;
16240 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16241 location_t loc
= 0;
16244 /* There's really no way to know what we're looking at, so we just
16245 try each alternative in order.
16249 In a template-argument, an ambiguity between a type-id and an
16250 expression is resolved to a type-id, regardless of the form of
16251 the corresponding template-parameter.
16253 Therefore, we try a type-id first. */
16254 cp_parser_parse_tentatively (parser
);
16255 argument
= cp_parser_template_type_arg (parser
);
16256 /* If there was no error parsing the type-id but the next token is a
16257 '>>', our behavior depends on which dialect of C++ we're
16258 parsing. In C++98, we probably found a typo for '> >'. But there
16259 are type-id which are also valid expressions. For instance:
16261 struct X { int operator >> (int); };
16262 template <int V> struct Foo {};
16265 Here 'X()' is a valid type-id of a function type, but the user just
16266 wanted to write the expression "X() >> 5". Thus, we remember that we
16267 found a valid type-id, but we still try to parse the argument as an
16268 expression to see what happens.
16270 In C++0x, the '>>' will be considered two separate '>'
16272 if (!cp_parser_error_occurred (parser
)
16273 && cxx_dialect
== cxx98
16274 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16276 maybe_type_id
= true;
16277 cp_parser_abort_tentative_parse (parser
);
16281 /* If the next token isn't a `,' or a `>', then this argument wasn't
16282 really finished. This means that the argument is not a valid
16284 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16285 cp_parser_error (parser
, "expected template-argument");
16286 /* If that worked, we're done. */
16287 if (cp_parser_parse_definitely (parser
))
16290 /* We're still not sure what the argument will be. */
16291 cp_parser_parse_tentatively (parser
);
16292 /* Try a template. */
16293 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16294 argument
= cp_parser_id_expression (parser
,
16295 /*template_keyword_p=*/false,
16296 /*check_dependency_p=*/true,
16298 /*declarator_p=*/false,
16299 /*optional_p=*/false);
16300 /* If the next token isn't a `,' or a `>', then this argument wasn't
16301 really finished. */
16302 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16303 cp_parser_error (parser
, "expected template-argument");
16304 if (!cp_parser_error_occurred (parser
))
16306 /* Figure out what is being referred to. If the id-expression
16307 was for a class template specialization, then we will have a
16308 TYPE_DECL at this point. There is no need to do name lookup
16309 at this point in that case. */
16310 if (TREE_CODE (argument
) != TYPE_DECL
)
16311 argument
= cp_parser_lookup_name (parser
, argument
,
16313 /*is_template=*/template_p
,
16314 /*is_namespace=*/false,
16315 /*check_dependency=*/true,
16316 /*ambiguous_decls=*/NULL
,
16317 argument_start_token
->location
);
16318 /* Handle a constrained-type-specifier for a non-type template
16320 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16322 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16323 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16324 cp_parser_error (parser
, "expected template-name");
16326 if (cp_parser_parse_definitely (parser
))
16328 if (TREE_DEPRECATED (argument
))
16329 warn_deprecated_use (argument
, NULL_TREE
);
16332 /* It must be a non-type argument. In C++17 any constant-expression is
16334 if (cxx_dialect
> cxx14
)
16337 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16339 -- an integral constant-expression of integral or enumeration
16342 -- the name of a non-type template-parameter; or
16344 -- the name of an object or function with external linkage...
16346 -- the address of an object or function with external linkage...
16348 -- a pointer to member... */
16349 /* Look for a non-type template parameter. */
16350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16352 cp_parser_parse_tentatively (parser
);
16353 argument
= cp_parser_primary_expression (parser
,
16354 /*address_p=*/false,
16356 /*template_arg_p=*/true,
16358 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16359 || !cp_parser_next_token_ends_template_argument_p (parser
))
16360 cp_parser_simulate_error (parser
);
16361 if (cp_parser_parse_definitely (parser
))
16365 /* If the next token is "&", the argument must be the address of an
16366 object or function with external linkage. */
16367 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16370 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16371 cp_lexer_consume_token (parser
->lexer
);
16373 /* See if we might have an id-expression. */
16374 token
= cp_lexer_peek_token (parser
->lexer
);
16375 if (token
->type
== CPP_NAME
16376 || token
->keyword
== RID_OPERATOR
16377 || token
->type
== CPP_SCOPE
16378 || token
->type
== CPP_TEMPLATE_ID
16379 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16381 cp_parser_parse_tentatively (parser
);
16382 argument
= cp_parser_primary_expression (parser
,
16385 /*template_arg_p=*/true,
16387 if (cp_parser_error_occurred (parser
)
16388 || !cp_parser_next_token_ends_template_argument_p (parser
))
16389 cp_parser_abort_tentative_parse (parser
);
16394 if (INDIRECT_REF_P (argument
))
16396 /* Strip the dereference temporarily. */
16397 gcc_assert (REFERENCE_REF_P (argument
));
16398 argument
= TREE_OPERAND (argument
, 0);
16401 /* If we're in a template, we represent a qualified-id referring
16402 to a static data member as a SCOPE_REF even if the scope isn't
16403 dependent so that we can check access control later. */
16405 if (TREE_CODE (probe
) == SCOPE_REF
)
16406 probe
= TREE_OPERAND (probe
, 1);
16409 /* A variable without external linkage might still be a
16410 valid constant-expression, so no error is issued here
16411 if the external-linkage check fails. */
16412 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16413 cp_parser_simulate_error (parser
);
16415 else if (is_overloaded_fn (argument
))
16416 /* All overloaded functions are allowed; if the external
16417 linkage test does not pass, an error will be issued
16421 && (TREE_CODE (argument
) == OFFSET_REF
16422 || TREE_CODE (argument
) == SCOPE_REF
))
16423 /* A pointer-to-member. */
16425 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16428 cp_parser_simulate_error (parser
);
16430 if (cp_parser_parse_definitely (parser
))
16433 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16434 tf_warning_or_error
);
16436 argument
= convert_from_reference (argument
);
16441 /* If the argument started with "&", there are no other valid
16442 alternatives at this point. */
16445 cp_parser_error (parser
, "invalid non-type template argument");
16446 return error_mark_node
;
16450 /* If the argument wasn't successfully parsed as a type-id followed
16451 by '>>', the argument can only be a constant expression now.
16452 Otherwise, we try parsing the constant-expression tentatively,
16453 because the argument could really be a type-id. */
16455 cp_parser_parse_tentatively (parser
);
16457 if (cxx_dialect
<= cxx14
)
16458 argument
= cp_parser_constant_expression (parser
);
16461 /* With C++17 generalized non-type template arguments we need to handle
16462 lvalue constant expressions, too. */
16463 argument
= cp_parser_assignment_expression (parser
);
16464 require_potential_constant_expression (argument
);
16467 if (!maybe_type_id
)
16469 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16470 cp_parser_error (parser
, "expected template-argument");
16471 if (cp_parser_parse_definitely (parser
))
16473 /* We did our best to parse the argument as a non type-id, but that
16474 was the only alternative that matched (albeit with a '>' after
16475 it). We can assume it's just a typo from the user, and a
16476 diagnostic will then be issued. */
16477 return cp_parser_template_type_arg (parser
);
16480 /* Parse an explicit-instantiation.
16482 explicit-instantiation:
16483 template declaration
16485 Although the standard says `declaration', what it really means is:
16487 explicit-instantiation:
16488 template decl-specifier-seq [opt] declarator [opt] ;
16490 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16491 supposed to be allowed. A defect report has been filed about this
16496 explicit-instantiation:
16497 storage-class-specifier template
16498 decl-specifier-seq [opt] declarator [opt] ;
16499 function-specifier template
16500 decl-specifier-seq [opt] declarator [opt] ; */
16503 cp_parser_explicit_instantiation (cp_parser
* parser
)
16505 int declares_class_or_enum
;
16506 cp_decl_specifier_seq decl_specifiers
;
16507 tree extension_specifier
= NULL_TREE
;
16509 timevar_push (TV_TEMPLATE_INST
);
16511 /* Look for an (optional) storage-class-specifier or
16512 function-specifier. */
16513 if (cp_parser_allow_gnu_extensions_p (parser
))
16515 extension_specifier
16516 = cp_parser_storage_class_specifier_opt (parser
);
16517 if (!extension_specifier
)
16518 extension_specifier
16519 = cp_parser_function_specifier_opt (parser
,
16520 /*decl_specs=*/NULL
);
16523 /* Look for the `template' keyword. */
16524 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16525 /* Let the front end know that we are processing an explicit
16527 begin_explicit_instantiation ();
16528 /* [temp.explicit] says that we are supposed to ignore access
16529 control while processing explicit instantiation directives. */
16530 push_deferring_access_checks (dk_no_check
);
16531 /* Parse a decl-specifier-seq. */
16532 cp_parser_decl_specifier_seq (parser
,
16533 CP_PARSER_FLAGS_OPTIONAL
,
16535 &declares_class_or_enum
);
16536 /* If there was exactly one decl-specifier, and it declared a class,
16537 and there's no declarator, then we have an explicit type
16539 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16543 type
= check_tag_decl (&decl_specifiers
,
16544 /*explicit_type_instantiation_p=*/true);
16545 /* Turn access control back on for names used during
16546 template instantiation. */
16547 pop_deferring_access_checks ();
16549 do_type_instantiation (type
, extension_specifier
,
16550 /*complain=*/tf_error
);
16554 cp_declarator
*declarator
;
16557 /* Parse the declarator. */
16559 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16560 /*ctor_dtor_or_conv_p=*/NULL
,
16561 /*parenthesized_p=*/NULL
,
16562 /*member_p=*/false,
16563 /*friend_p=*/false);
16564 if (declares_class_or_enum
& 2)
16565 cp_parser_check_for_definition_in_return_type (declarator
,
16566 decl_specifiers
.type
,
16567 decl_specifiers
.locations
[ds_type_spec
]);
16568 if (declarator
!= cp_error_declarator
)
16570 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16571 permerror (decl_specifiers
.locations
[ds_inline
],
16572 "explicit instantiation shall not use"
16573 " %<inline%> specifier");
16574 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16575 permerror (decl_specifiers
.locations
[ds_constexpr
],
16576 "explicit instantiation shall not use"
16577 " %<constexpr%> specifier");
16579 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16580 NORMAL
, 0, &decl_specifiers
.attributes
);
16581 /* Turn access control back on for names used during
16582 template instantiation. */
16583 pop_deferring_access_checks ();
16584 /* Do the explicit instantiation. */
16585 do_decl_instantiation (decl
, extension_specifier
);
16589 pop_deferring_access_checks ();
16590 /* Skip the body of the explicit instantiation. */
16591 cp_parser_skip_to_end_of_statement (parser
);
16594 /* We're done with the instantiation. */
16595 end_explicit_instantiation ();
16597 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16599 timevar_pop (TV_TEMPLATE_INST
);
16602 /* Parse an explicit-specialization.
16604 explicit-specialization:
16605 template < > declaration
16607 Although the standard says `declaration', what it really means is:
16609 explicit-specialization:
16610 template <> decl-specifier [opt] init-declarator [opt] ;
16611 template <> function-definition
16612 template <> explicit-specialization
16613 template <> template-declaration */
16616 cp_parser_explicit_specialization (cp_parser
* parser
)
16618 bool need_lang_pop
;
16619 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16621 /* Look for the `template' keyword. */
16622 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16623 /* Look for the `<'. */
16624 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16625 /* Look for the `>'. */
16626 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16627 /* We have processed another parameter list. */
16628 ++parser
->num_template_parameter_lists
;
16631 A template ... explicit specialization ... shall not have C
16633 if (current_lang_name
== lang_name_c
)
16635 error_at (token
->location
, "template specialization with C linkage");
16636 maybe_show_extern_c_location ();
16637 /* Give it C++ linkage to avoid confusing other parts of the
16639 push_lang_context (lang_name_cplusplus
);
16640 need_lang_pop
= true;
16643 need_lang_pop
= false;
16644 /* Let the front end know that we are beginning a specialization. */
16645 if (!begin_specialization ())
16647 end_specialization ();
16651 /* If the next keyword is `template', we need to figure out whether
16652 or not we're looking a template-declaration. */
16653 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16655 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16656 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16657 cp_parser_template_declaration_after_export (parser
,
16658 /*member_p=*/false);
16660 cp_parser_explicit_specialization (parser
);
16663 /* Parse the dependent declaration. */
16664 cp_parser_single_declaration (parser
,
16666 /*member_p=*/false,
16667 /*explicit_specialization_p=*/true,
16668 /*friend_p=*/NULL
);
16669 /* We're done with the specialization. */
16670 end_specialization ();
16671 /* For the erroneous case of a template with C linkage, we pushed an
16672 implicit C++ linkage scope; exit that scope now. */
16674 pop_lang_context ();
16675 /* We're done with this parameter list. */
16676 --parser
->num_template_parameter_lists
;
16679 /* Parse a type-specifier.
16682 simple-type-specifier
16685 elaborated-type-specifier
16693 Returns a representation of the type-specifier. For a
16694 class-specifier, enum-specifier, or elaborated-type-specifier, a
16695 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16697 The parser flags FLAGS is used to control type-specifier parsing.
16699 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16700 in a decl-specifier-seq.
16702 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16703 class-specifier, enum-specifier, or elaborated-type-specifier, then
16704 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16705 if a type is declared; 2 if it is defined. Otherwise, it is set to
16708 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16709 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16710 is set to FALSE. */
16713 cp_parser_type_specifier (cp_parser
* parser
,
16714 cp_parser_flags flags
,
16715 cp_decl_specifier_seq
*decl_specs
,
16716 bool is_declaration
,
16717 int* declares_class_or_enum
,
16718 bool* is_cv_qualifier
)
16720 tree type_spec
= NULL_TREE
;
16723 cp_decl_spec ds
= ds_last
;
16725 /* Assume this type-specifier does not declare a new type. */
16726 if (declares_class_or_enum
)
16727 *declares_class_or_enum
= 0;
16728 /* And that it does not specify a cv-qualifier. */
16729 if (is_cv_qualifier
)
16730 *is_cv_qualifier
= false;
16731 /* Peek at the next token. */
16732 token
= cp_lexer_peek_token (parser
->lexer
);
16734 /* If we're looking at a keyword, we can use that to guide the
16735 production we choose. */
16736 keyword
= token
->keyword
;
16740 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16741 goto elaborated_type_specifier
;
16743 /* Look for the enum-specifier. */
16744 type_spec
= cp_parser_enum_specifier (parser
);
16745 /* If that worked, we're done. */
16748 if (declares_class_or_enum
)
16749 *declares_class_or_enum
= 2;
16751 cp_parser_set_decl_spec_type (decl_specs
,
16754 /*type_definition_p=*/true);
16758 goto elaborated_type_specifier
;
16760 /* Any of these indicate either a class-specifier, or an
16761 elaborated-type-specifier. */
16765 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16766 goto elaborated_type_specifier
;
16768 /* Parse tentatively so that we can back up if we don't find a
16769 class-specifier. */
16770 cp_parser_parse_tentatively (parser
);
16771 /* Look for the class-specifier. */
16772 type_spec
= cp_parser_class_specifier (parser
);
16773 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16774 /* If that worked, we're done. */
16775 if (cp_parser_parse_definitely (parser
))
16777 if (declares_class_or_enum
)
16778 *declares_class_or_enum
= 2;
16780 cp_parser_set_decl_spec_type (decl_specs
,
16783 /*type_definition_p=*/true);
16787 /* Fall through. */
16788 elaborated_type_specifier
:
16789 /* We're declaring (not defining) a class or enum. */
16790 if (declares_class_or_enum
)
16791 *declares_class_or_enum
= 1;
16793 /* Fall through. */
16795 /* Look for an elaborated-type-specifier. */
16797 = (cp_parser_elaborated_type_specifier
16799 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16802 cp_parser_set_decl_spec_type (decl_specs
,
16805 /*type_definition_p=*/false);
16810 if (is_cv_qualifier
)
16811 *is_cv_qualifier
= true;
16816 if (is_cv_qualifier
)
16817 *is_cv_qualifier
= true;
16822 if (is_cv_qualifier
)
16823 *is_cv_qualifier
= true;
16827 /* The `__complex__' keyword is a GNU extension. */
16835 /* Handle simple keywords. */
16840 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16841 decl_specs
->any_specifiers_p
= true;
16843 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16846 /* If we do not already have a type-specifier, assume we are looking
16847 at a simple-type-specifier. */
16848 type_spec
= cp_parser_simple_type_specifier (parser
,
16852 /* If we didn't find a type-specifier, and a type-specifier was not
16853 optional in this context, issue an error message. */
16854 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16856 cp_parser_error (parser
, "expected type specifier");
16857 return error_mark_node
;
16863 /* Parse a simple-type-specifier.
16865 simple-type-specifier:
16866 :: [opt] nested-name-specifier [opt] type-name
16867 :: [opt] nested-name-specifier template template-id
16882 simple-type-specifier:
16884 decltype ( expression )
16887 __underlying_type ( type-id )
16891 nested-name-specifier(opt) template-name
16895 simple-type-specifier:
16897 __typeof__ unary-expression
16898 __typeof__ ( type-id )
16899 __typeof__ ( type-id ) { initializer-list , [opt] }
16901 Concepts Extension:
16903 simple-type-specifier:
16904 constrained-type-specifier
16906 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16907 appropriately updated. */
16910 cp_parser_simple_type_specifier (cp_parser
* parser
,
16911 cp_decl_specifier_seq
*decl_specs
,
16912 cp_parser_flags flags
)
16914 tree type
= NULL_TREE
;
16918 /* Peek at the next token. */
16919 token
= cp_lexer_peek_token (parser
->lexer
);
16921 /* If we're looking at a keyword, things are easy. */
16922 switch (token
->keyword
)
16926 decl_specs
->explicit_char_p
= true;
16927 type
= char_type_node
;
16930 type
= char16_type_node
;
16933 type
= char32_type_node
;
16936 type
= wchar_type_node
;
16939 type
= boolean_type_node
;
16942 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16943 type
= short_integer_type_node
;
16947 decl_specs
->explicit_int_p
= true;
16948 type
= integer_type_node
;
16954 idx
= token
->keyword
- RID_INT_N_0
;
16955 if (! int_n_enabled_p
[idx
])
16959 decl_specs
->explicit_intN_p
= true;
16960 decl_specs
->int_n_idx
= idx
;
16962 type
= int_n_trees
[idx
].signed_type
;
16966 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16967 type
= long_integer_type_node
;
16970 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16971 type
= integer_type_node
;
16974 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16975 type
= unsigned_type_node
;
16978 type
= float_type_node
;
16981 type
= double_type_node
;
16984 type
= void_type_node
;
16988 maybe_warn_cpp0x (CPP0X_AUTO
);
16989 if (parser
->auto_is_implicit_function_template_parm_p
)
16991 /* The 'auto' might be the placeholder return type for a function decl
16992 with trailing return type. */
16993 bool have_trailing_return_fn_decl
= false;
16995 cp_parser_parse_tentatively (parser
);
16996 cp_lexer_consume_token (parser
->lexer
);
16997 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16998 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16999 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17000 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17004 cp_lexer_consume_token (parser
->lexer
);
17005 cp_parser_skip_to_closing_parenthesis (parser
,
17006 /*recovering*/false,
17008 /*consume_paren*/true);
17012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17014 have_trailing_return_fn_decl
= true;
17018 cp_lexer_consume_token (parser
->lexer
);
17020 cp_parser_abort_tentative_parse (parser
);
17022 if (have_trailing_return_fn_decl
)
17024 type
= make_auto ();
17028 if (cxx_dialect
>= cxx14
)
17030 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17031 type
= TREE_TYPE (type
);
17034 type
= error_mark_node
;
17036 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17038 if (cxx_dialect
< cxx14
)
17039 error_at (token
->location
,
17040 "use of %<auto%> in lambda parameter declaration "
17041 "only available with "
17042 "-std=c++14 or -std=gnu++14");
17044 else if (cxx_dialect
< cxx14
)
17045 error_at (token
->location
,
17046 "use of %<auto%> in parameter declaration "
17047 "only available with "
17048 "-std=c++14 or -std=gnu++14");
17049 else if (!flag_concepts
)
17050 pedwarn (token
->location
, OPT_Wpedantic
,
17051 "ISO C++ forbids use of %<auto%> in parameter "
17055 type
= make_auto ();
17059 /* Since DR 743, decltype can either be a simple-type-specifier by
17060 itself or begin a nested-name-specifier. Parsing it will replace
17061 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17062 handling below decide what to do. */
17063 cp_parser_decltype (parser
);
17064 cp_lexer_set_token_position (parser
->lexer
, token
);
17068 /* Consume the `typeof' token. */
17069 cp_lexer_consume_token (parser
->lexer
);
17070 /* Parse the operand to `typeof'. */
17071 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17072 /* If it is not already a TYPE, take its type. */
17073 if (!TYPE_P (type
))
17074 type
= finish_typeof (type
);
17077 cp_parser_set_decl_spec_type (decl_specs
, type
,
17079 /*type_definition_p=*/false);
17083 case RID_UNDERLYING_TYPE
:
17084 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17086 cp_parser_set_decl_spec_type (decl_specs
, type
,
17088 /*type_definition_p=*/false);
17093 case RID_DIRECT_BASES
:
17094 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17096 cp_parser_set_decl_spec_type (decl_specs
, type
,
17098 /*type_definition_p=*/false);
17104 /* If token is an already-parsed decltype not followed by ::,
17105 it's a simple-type-specifier. */
17106 if (token
->type
== CPP_DECLTYPE
17107 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17109 type
= saved_checks_value (token
->u
.tree_check_value
);
17112 cp_parser_set_decl_spec_type (decl_specs
, type
,
17114 /*type_definition_p=*/false);
17115 /* Remember that we are handling a decltype in order to
17116 implement the resolution of DR 1510 when the argument
17117 isn't instantiation dependent. */
17118 decl_specs
->decltype_p
= true;
17120 cp_lexer_consume_token (parser
->lexer
);
17124 /* If the type-specifier was for a built-in type, we're done. */
17127 /* Record the type. */
17129 && (token
->keyword
!= RID_SIGNED
17130 && token
->keyword
!= RID_UNSIGNED
17131 && token
->keyword
!= RID_SHORT
17132 && token
->keyword
!= RID_LONG
))
17133 cp_parser_set_decl_spec_type (decl_specs
,
17136 /*type_definition_p=*/false);
17138 decl_specs
->any_specifiers_p
= true;
17140 /* Consume the token. */
17141 cp_lexer_consume_token (parser
->lexer
);
17143 if (type
== error_mark_node
)
17144 return error_mark_node
;
17146 /* There is no valid C++ program where a non-template type is
17147 followed by a "<". That usually indicates that the user thought
17148 that the type was a template. */
17149 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17152 return TYPE_NAME (type
);
17155 /* The type-specifier must be a user-defined type. */
17156 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17161 /* Don't gobble tokens or issue error messages if this is an
17162 optional type-specifier. */
17163 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17164 cp_parser_parse_tentatively (parser
);
17166 token
= cp_lexer_peek_token (parser
->lexer
);
17168 /* Look for the optional `::' operator. */
17170 = (cp_parser_global_scope_opt (parser
,
17171 /*current_scope_valid_p=*/false)
17173 /* Look for the nested-name specifier. */
17175 = (cp_parser_nested_name_specifier_opt (parser
,
17176 /*typename_keyword_p=*/false,
17177 /*check_dependency_p=*/true,
17179 /*is_declaration=*/false)
17181 /* If we have seen a nested-name-specifier, and the next token
17182 is `template', then we are using the template-id production. */
17184 && cp_parser_optional_template_keyword (parser
))
17186 /* Look for the template-id. */
17187 type
= cp_parser_template_id (parser
,
17188 /*template_keyword_p=*/true,
17189 /*check_dependency_p=*/true,
17191 /*is_declaration=*/false);
17192 /* If the template-id did not name a type, we are out of
17194 if (TREE_CODE (type
) != TYPE_DECL
)
17196 cp_parser_error (parser
, "expected template-id for type");
17200 /* Otherwise, look for a type-name. */
17202 type
= cp_parser_type_name (parser
);
17203 /* Keep track of all name-lookups performed in class scopes. */
17207 && TREE_CODE (type
) == TYPE_DECL
17208 && identifier_p (DECL_NAME (type
)))
17209 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17210 /* If it didn't work out, we don't have a TYPE. */
17211 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17212 && !cp_parser_parse_definitely (parser
))
17214 if (!type
&& cxx_dialect
>= cxx17
)
17216 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17217 cp_parser_parse_tentatively (parser
);
17219 cp_parser_global_scope_opt (parser
,
17220 /*current_scope_valid_p=*/false);
17221 cp_parser_nested_name_specifier_opt (parser
,
17222 /*typename_keyword_p=*/false,
17223 /*check_dependency_p=*/true,
17225 /*is_declaration=*/false);
17226 tree name
= cp_parser_identifier (parser
);
17227 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17228 && parser
->scope
!= error_mark_node
)
17230 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17232 /*is_template=*/false,
17233 /*is_namespace=*/false,
17234 /*check_dependency=*/true,
17235 /*ambiguous_decls=*/NULL
,
17237 if (tmpl
&& tmpl
!= error_mark_node
17238 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17239 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17240 type
= make_template_placeholder (tmpl
);
17243 type
= error_mark_node
;
17244 if (!cp_parser_simulate_error (parser
))
17245 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17246 NLE_TYPE
, token
->location
);
17250 type
= error_mark_node
;
17252 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17253 && !cp_parser_parse_definitely (parser
))
17256 if (type
&& decl_specs
)
17257 cp_parser_set_decl_spec_type (decl_specs
, type
,
17259 /*type_definition_p=*/false);
17262 /* If we didn't get a type-name, issue an error message. */
17263 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17265 cp_parser_error (parser
, "expected type-name");
17266 return error_mark_node
;
17269 if (type
&& type
!= error_mark_node
)
17271 /* See if TYPE is an Objective-C type, and if so, parse and
17272 accept any protocol references following it. Do this before
17273 the cp_parser_check_for_invalid_template_id() call, because
17274 Objective-C types can be followed by '<...>' which would
17275 enclose protocol names rather than template arguments, and so
17276 everything is fine. */
17277 if (c_dialect_objc () && !parser
->scope
17278 && (objc_is_id (type
) || objc_is_class_name (type
)))
17280 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17281 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17283 /* Clobber the "unqualified" type previously entered into
17284 DECL_SPECS with the new, improved protocol-qualified version. */
17286 decl_specs
->type
= qual_type
;
17291 /* There is no valid C++ program where a non-template type is
17292 followed by a "<". That usually indicates that the user
17293 thought that the type was a template. */
17294 cp_parser_check_for_invalid_template_id (parser
, type
,
17302 /* Parse a type-name.
17308 simple-template-id [in c++0x]
17325 Returns a TYPE_DECL for the type. */
17328 cp_parser_type_name (cp_parser
* parser
)
17330 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17335 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17339 /* We can't know yet whether it is a class-name or not. */
17340 cp_parser_parse_tentatively (parser
);
17341 /* Try a class-name. */
17342 type_decl
= cp_parser_class_name (parser
,
17343 typename_keyword_p
,
17344 /*template_keyword_p=*/false,
17346 /*check_dependency_p=*/true,
17347 /*class_head_p=*/false,
17348 /*is_declaration=*/false);
17349 /* If it's not a class-name, keep looking. */
17350 if (!cp_parser_parse_definitely (parser
))
17352 if (cxx_dialect
< cxx11
)
17353 /* It must be a typedef-name or an enum-name. */
17354 return cp_parser_nonclass_name (parser
);
17356 cp_parser_parse_tentatively (parser
);
17357 /* It is either a simple-template-id representing an
17358 instantiation of an alias template... */
17359 type_decl
= cp_parser_template_id (parser
,
17360 /*template_keyword_p=*/false,
17361 /*check_dependency_p=*/true,
17363 /*is_declaration=*/false);
17364 /* Note that this must be an instantiation of an alias template
17365 because [temp.names]/6 says:
17367 A template-id that names an alias template specialization
17370 Whereas [temp.names]/7 says:
17372 A simple-template-id that names a class template
17373 specialization is a class-name.
17375 With concepts, this could also be a partial-concept-id that
17376 declares a non-type template parameter. */
17377 if (type_decl
!= NULL_TREE
17378 && TREE_CODE (type_decl
) == TYPE_DECL
17379 && TYPE_DECL_ALIAS_P (type_decl
))
17380 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17381 else if (is_constrained_parameter (type_decl
))
17382 /* Don't do anything. */ ;
17384 cp_parser_simulate_error (parser
);
17386 if (!cp_parser_parse_definitely (parser
))
17387 /* ... Or a typedef-name or an enum-name. */
17388 return cp_parser_nonclass_name (parser
);
17394 /* Check if DECL and ARGS can form a constrained-type-specifier.
17395 If ARGS is non-null, we try to form a concept check of the
17396 form DECL<?, ARGS> where ? is a wildcard that matches any
17397 kind of template argument. If ARGS is NULL, then we try to
17398 form a concept check of the form DECL<?>. */
17401 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17402 tree decl
, tree args
)
17404 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17406 /* If we a constrained-type-specifier cannot be deduced. */
17407 if (parser
->prevent_constrained_type_specifiers
)
17410 /* A constrained type specifier can only be found in an
17411 overload set or as a reference to a template declaration.
17413 FIXME: This might be masking a bug. It's possible that
17414 that the deduction below is causing template specializations
17415 to be formed with the wildcard as an argument. */
17416 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17419 /* Try to build a call expression that evaluates the
17420 concept. This can fail if the overload set refers
17421 only to non-templates. */
17422 tree placeholder
= build_nt (WILDCARD_DECL
);
17423 tree check
= build_concept_check (decl
, placeholder
, args
);
17424 if (check
== error_mark_node
)
17427 /* Deduce the checked constraint and the prototype parameter.
17429 FIXME: In certain cases, failure to deduce should be a
17430 diagnosable error. */
17433 if (!deduce_constrained_parameter (check
, conc
, proto
))
17436 /* In template parameter scope, this results in a constrained
17437 parameter. Return a descriptor of that parm. */
17438 if (processing_template_parmlist
)
17439 return build_constrained_parameter (conc
, proto
, args
);
17441 /* In a parameter-declaration-clause, constrained-type
17442 specifiers result in invented template parameters. */
17443 if (parser
->auto_is_implicit_function_template_parm_p
)
17445 tree x
= build_constrained_parameter (conc
, proto
, args
);
17446 return synthesize_implicit_template_parm (parser
, x
);
17450 /* Otherwise, we're in a context where the constrained
17451 type name is deduced and the constraint applies
17452 after deduction. */
17453 return make_constrained_auto (conc
, args
);
17459 /* If DECL refers to a concept, return a TYPE_DECL representing
17460 the result of using the constrained type specifier in the
17461 current context. DECL refers to a concept if
17463 - it is an overload set containing a function concept taking a single
17466 - it is a variable concept taking a single type argument. */
17469 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17472 && (TREE_CODE (decl
) == OVERLOAD
17473 || BASELINK_P (decl
)
17474 || variable_concept_p (decl
)))
17475 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17480 /* Check if DECL and ARGS form a partial-concept-id. If so,
17481 assign ID to the resulting constrained placeholder.
17483 Returns true if the partial-concept-id designates a placeholder
17484 and false otherwise. Note that *id is set to NULL_TREE in
17488 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17490 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17493 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17505 Returns a TYPE_DECL for the type. */
17508 cp_parser_nonclass_name (cp_parser
* parser
)
17513 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17514 identifier
= cp_parser_identifier (parser
);
17515 if (identifier
== error_mark_node
)
17516 return error_mark_node
;
17518 /* Look up the type-name. */
17519 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17521 type_decl
= strip_using_decl (type_decl
);
17523 /* If we found an overload set, then it may refer to a concept-name. */
17524 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17527 if (TREE_CODE (type_decl
) != TYPE_DECL
17528 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17530 /* See if this is an Objective-C type. */
17531 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17532 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17534 type_decl
= TYPE_NAME (type
);
17537 /* Issue an error if we did not find a type-name. */
17538 if (TREE_CODE (type_decl
) != TYPE_DECL
17539 /* In Objective-C, we have the complication that class names are
17540 normally type names and start declarations (eg, the
17541 "NSObject" in "NSObject *object;"), but can be used in an
17542 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17543 is an expression. So, a classname followed by a dot is not a
17544 valid type-name. */
17545 || (objc_is_class_name (TREE_TYPE (type_decl
))
17546 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17548 if (!cp_parser_simulate_error (parser
))
17549 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17550 NLE_TYPE
, token
->location
);
17551 return error_mark_node
;
17553 /* Remember that the name was used in the definition of the
17554 current class so that we can check later to see if the
17555 meaning would have been different after the class was
17556 entirely defined. */
17557 else if (type_decl
!= error_mark_node
17559 maybe_note_name_used_in_class (identifier
, type_decl
);
17564 /* Parse an elaborated-type-specifier. Note that the grammar given
17565 here incorporates the resolution to DR68.
17567 elaborated-type-specifier:
17568 class-key :: [opt] nested-name-specifier [opt] identifier
17569 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17570 enum-key :: [opt] nested-name-specifier [opt] identifier
17571 typename :: [opt] nested-name-specifier identifier
17572 typename :: [opt] nested-name-specifier template [opt]
17577 elaborated-type-specifier:
17578 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17579 class-key attributes :: [opt] nested-name-specifier [opt]
17580 template [opt] template-id
17581 enum attributes :: [opt] nested-name-specifier [opt] identifier
17583 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17584 declared `friend'. If IS_DECLARATION is TRUE, then this
17585 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17586 something is being declared.
17588 Returns the TYPE specified. */
17591 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17593 bool is_declaration
)
17595 enum tag_types tag_type
;
17597 tree type
= NULL_TREE
;
17598 tree attributes
= NULL_TREE
;
17600 cp_token
*token
= NULL
;
17602 /* See if we're looking at the `enum' keyword. */
17603 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17605 /* Consume the `enum' token. */
17606 cp_lexer_consume_token (parser
->lexer
);
17607 /* Remember that it's an enumeration type. */
17608 tag_type
= enum_type
;
17609 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17610 enums) is used here. */
17611 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17612 if (cp_parser_is_keyword (token
, RID_CLASS
)
17613 || cp_parser_is_keyword (token
, RID_STRUCT
))
17615 gcc_rich_location
richloc (token
->location
);
17616 richloc
.add_range (input_location
, false);
17617 richloc
.add_fixit_remove ();
17618 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17619 "a scoped enum must not use the %qD keyword",
17621 /* Consume the `struct' or `class' and parse it anyway. */
17622 cp_lexer_consume_token (parser
->lexer
);
17624 /* Parse the attributes. */
17625 attributes
= cp_parser_attributes_opt (parser
);
17627 /* Or, it might be `typename'. */
17628 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17631 /* Consume the `typename' token. */
17632 cp_lexer_consume_token (parser
->lexer
);
17633 /* Remember that it's a `typename' type. */
17634 tag_type
= typename_type
;
17636 /* Otherwise it must be a class-key. */
17639 tag_type
= cp_parser_class_key (parser
);
17640 if (tag_type
== none_type
)
17641 return error_mark_node
;
17642 /* Parse the attributes. */
17643 attributes
= cp_parser_attributes_opt (parser
);
17646 /* Look for the `::' operator. */
17647 globalscope
= cp_parser_global_scope_opt (parser
,
17648 /*current_scope_valid_p=*/false);
17649 /* Look for the nested-name-specifier. */
17650 tree nested_name_specifier
;
17651 if (tag_type
== typename_type
&& !globalscope
)
17653 nested_name_specifier
17654 = cp_parser_nested_name_specifier (parser
,
17655 /*typename_keyword_p=*/true,
17656 /*check_dependency_p=*/true,
17659 if (!nested_name_specifier
)
17660 return error_mark_node
;
17663 /* Even though `typename' is not present, the proposed resolution
17664 to Core Issue 180 says that in `class A<T>::B', `B' should be
17665 considered a type-name, even if `A<T>' is dependent. */
17666 nested_name_specifier
17667 = cp_parser_nested_name_specifier_opt (parser
,
17668 /*typename_keyword_p=*/true,
17669 /*check_dependency_p=*/true,
17672 /* For everything but enumeration types, consider a template-id.
17673 For an enumeration type, consider only a plain identifier. */
17674 if (tag_type
!= enum_type
)
17676 bool template_p
= false;
17679 /* Allow the `template' keyword. */
17680 template_p
= cp_parser_optional_template_keyword (parser
);
17681 /* If we didn't see `template', we don't know if there's a
17682 template-id or not. */
17684 cp_parser_parse_tentatively (parser
);
17685 /* Parse the template-id. */
17686 token
= cp_lexer_peek_token (parser
->lexer
);
17687 decl
= cp_parser_template_id (parser
, template_p
,
17688 /*check_dependency_p=*/true,
17691 /* If we didn't find a template-id, look for an ordinary
17693 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17695 /* We can get here when cp_parser_template_id, called by
17696 cp_parser_class_name with tag_type == none_type, succeeds
17697 and caches a BASELINK. Then, when called again here,
17698 instead of failing and returning an error_mark_node
17699 returns it (see template/typename17.C in C++11).
17700 ??? Could we diagnose this earlier? */
17701 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17703 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17704 type
= error_mark_node
;
17706 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17707 in effect, then we must assume that, upon instantiation, the
17708 template will correspond to a class. */
17709 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17710 && tag_type
== typename_type
)
17711 type
= make_typename_type (parser
->scope
, decl
,
17713 /*complain=*/tf_error
);
17714 /* If the `typename' keyword is in effect and DECL is not a type
17715 decl, then type is non existent. */
17716 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17718 else if (TREE_CODE (decl
) == TYPE_DECL
)
17720 type
= check_elaborated_type_specifier (tag_type
, decl
,
17721 /*allow_template_p=*/true);
17723 /* If the next token is a semicolon, this must be a specialization,
17724 instantiation, or friend declaration. Check the scope while we
17725 still know whether or not we had a nested-name-specifier. */
17726 if (type
!= error_mark_node
17727 && !nested_name_specifier
&& !is_friend
17728 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17729 check_unqualified_spec_or_inst (type
, token
->location
);
17731 else if (decl
== error_mark_node
)
17732 type
= error_mark_node
;
17737 token
= cp_lexer_peek_token (parser
->lexer
);
17738 identifier
= cp_parser_identifier (parser
);
17740 if (identifier
== error_mark_node
)
17742 parser
->scope
= NULL_TREE
;
17743 return error_mark_node
;
17746 /* For a `typename', we needn't call xref_tag. */
17747 if (tag_type
== typename_type
17748 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17749 return cp_parser_make_typename_type (parser
, identifier
,
17752 /* Template parameter lists apply only if we are not within a
17753 function parameter list. */
17754 bool template_parm_lists_apply
17755 = parser
->num_template_parameter_lists
;
17756 if (template_parm_lists_apply
)
17757 for (cp_binding_level
*s
= current_binding_level
;
17758 s
&& s
->kind
!= sk_template_parms
;
17759 s
= s
->level_chain
)
17760 if (s
->kind
== sk_function_parms
)
17761 template_parm_lists_apply
= false;
17763 /* Look up a qualified name in the usual way. */
17767 tree ambiguous_decls
;
17769 decl
= cp_parser_lookup_name (parser
, identifier
,
17771 /*is_template=*/false,
17772 /*is_namespace=*/false,
17773 /*check_dependency=*/true,
17777 /* If the lookup was ambiguous, an error will already have been
17779 if (ambiguous_decls
)
17780 return error_mark_node
;
17782 /* If we are parsing friend declaration, DECL may be a
17783 TEMPLATE_DECL tree node here. However, we need to check
17784 whether this TEMPLATE_DECL results in valid code. Consider
17785 the following example:
17788 template <class T> class C {};
17791 template <class T> friend class N::C; // #1, valid code
17793 template <class T> class Y {
17794 friend class N::C; // #2, invalid code
17797 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17798 name lookup of `N::C'. We see that friend declaration must
17799 be template for the code to be valid. Note that
17800 processing_template_decl does not work here since it is
17801 always 1 for the above two cases. */
17803 decl
= (cp_parser_maybe_treat_template_as_class
17804 (decl
, /*tag_name_p=*/is_friend
17805 && template_parm_lists_apply
));
17807 if (TREE_CODE (decl
) != TYPE_DECL
)
17809 cp_parser_diagnose_invalid_type_name (parser
,
17812 return error_mark_node
;
17815 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17817 bool allow_template
= (template_parm_lists_apply
17818 || DECL_SELF_REFERENCE_P (decl
));
17819 type
= check_elaborated_type_specifier (tag_type
, decl
,
17822 if (type
== error_mark_node
)
17823 return error_mark_node
;
17826 /* Forward declarations of nested types, such as
17831 are invalid unless all components preceding the final '::'
17832 are complete. If all enclosing types are complete, these
17833 declarations become merely pointless.
17835 Invalid forward declarations of nested types are errors
17836 caught elsewhere in parsing. Those that are pointless arrive
17839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17840 && !is_friend
&& !processing_explicit_instantiation
)
17841 warning (0, "declaration %qD does not declare anything", decl
);
17843 type
= TREE_TYPE (decl
);
17847 /* An elaborated-type-specifier sometimes introduces a new type and
17848 sometimes names an existing type. Normally, the rule is that it
17849 introduces a new type only if there is not an existing type of
17850 the same name already in scope. For example, given:
17853 void f() { struct S s; }
17855 the `struct S' in the body of `f' is the same `struct S' as in
17856 the global scope; the existing definition is used. However, if
17857 there were no global declaration, this would introduce a new
17858 local class named `S'.
17860 An exception to this rule applies to the following code:
17862 namespace N { struct S; }
17864 Here, the elaborated-type-specifier names a new type
17865 unconditionally; even if there is already an `S' in the
17866 containing scope this declaration names a new type.
17867 This exception only applies if the elaborated-type-specifier
17868 forms the complete declaration:
17872 A declaration consisting solely of `class-key identifier ;' is
17873 either a redeclaration of the name in the current scope or a
17874 forward declaration of the identifier as a class name. It
17875 introduces the name into the current scope.
17877 We are in this situation precisely when the next token is a `;'.
17879 An exception to the exception is that a `friend' declaration does
17880 *not* name a new type; i.e., given:
17882 struct S { friend struct T; };
17884 `T' is not a new type in the scope of `S'.
17886 Also, `new struct S' or `sizeof (struct S)' never results in the
17887 definition of a new type; a new type can only be declared in a
17888 declaration context. */
17894 /* Friends have special name lookup rules. */
17895 ts
= ts_within_enclosing_non_class
;
17896 else if (is_declaration
17897 && cp_lexer_next_token_is (parser
->lexer
,
17899 /* This is a `class-key identifier ;' */
17905 (template_parm_lists_apply
17906 && (cp_parser_next_token_starts_class_definition_p (parser
)
17907 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17908 /* An unqualified name was used to reference this type, so
17909 there were no qualifying templates. */
17910 if (template_parm_lists_apply
17911 && !cp_parser_check_template_parameters (parser
,
17912 /*num_templates=*/0,
17914 /*declarator=*/NULL
))
17915 return error_mark_node
;
17916 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17920 if (type
== error_mark_node
)
17921 return error_mark_node
;
17923 /* Allow attributes on forward declarations of classes. */
17926 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17927 warning (OPT_Wattributes
,
17928 "attributes ignored on uninstantiated type");
17929 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17930 && ! processing_explicit_instantiation
)
17931 warning (OPT_Wattributes
,
17932 "attributes ignored on template instantiation");
17933 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17934 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17936 warning (OPT_Wattributes
,
17937 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17940 if (tag_type
!= enum_type
)
17942 /* Indicate whether this class was declared as a `class' or as a
17944 if (CLASS_TYPE_P (type
))
17945 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17946 cp_parser_check_class_key (tag_type
, type
);
17949 /* A "<" cannot follow an elaborated type specifier. If that
17950 happens, the user was probably trying to form a template-id. */
17951 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17957 /* Parse an enum-specifier.
17960 enum-head { enumerator-list [opt] }
17961 enum-head { enumerator-list , } [C++0x]
17964 enum-key identifier [opt] enum-base [opt]
17965 enum-key nested-name-specifier identifier enum-base [opt]
17970 enum struct [C++0x]
17973 : type-specifier-seq
17975 opaque-enum-specifier:
17976 enum-key identifier enum-base [opt] ;
17979 enum-key attributes[opt] identifier [opt] enum-base [opt]
17980 { enumerator-list [opt] }attributes[opt]
17981 enum-key attributes[opt] identifier [opt] enum-base [opt]
17982 { enumerator-list, }attributes[opt] [C++0x]
17984 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17985 if the token stream isn't an enum-specifier after all. */
17988 cp_parser_enum_specifier (cp_parser
* parser
)
17991 tree type
= NULL_TREE
;
17993 tree nested_name_specifier
= NULL_TREE
;
17995 bool scoped_enum_p
= false;
17996 bool has_underlying_type
= false;
17997 bool nested_being_defined
= false;
17998 bool new_value_list
= false;
17999 bool is_new_type
= false;
18000 bool is_unnamed
= false;
18001 tree underlying_type
= NULL_TREE
;
18002 cp_token
*type_start_token
= NULL
;
18003 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18005 parser
->colon_corrects_to_scope_p
= false;
18007 /* Parse tentatively so that we can back up if we don't find a
18009 cp_parser_parse_tentatively (parser
);
18011 /* Caller guarantees that the current token is 'enum', an identifier
18012 possibly follows, and the token after that is an opening brace.
18013 If we don't have an identifier, fabricate an anonymous name for
18014 the enumeration being defined. */
18015 cp_lexer_consume_token (parser
->lexer
);
18017 /* Parse the "class" or "struct", which indicates a scoped
18018 enumeration type in C++0x. */
18019 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18020 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18022 if (cxx_dialect
< cxx11
)
18023 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18025 /* Consume the `struct' or `class' token. */
18026 cp_lexer_consume_token (parser
->lexer
);
18028 scoped_enum_p
= true;
18031 attributes
= cp_parser_attributes_opt (parser
);
18033 /* Clear the qualification. */
18034 parser
->scope
= NULL_TREE
;
18035 parser
->qualifying_scope
= NULL_TREE
;
18036 parser
->object_scope
= NULL_TREE
;
18038 /* Figure out in what scope the declaration is being placed. */
18039 prev_scope
= current_scope ();
18041 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18043 push_deferring_access_checks (dk_no_check
);
18044 nested_name_specifier
18045 = cp_parser_nested_name_specifier_opt (parser
,
18046 /*typename_keyword_p=*/true,
18047 /*check_dependency_p=*/false,
18049 /*is_declaration=*/false);
18051 if (nested_name_specifier
)
18055 identifier
= cp_parser_identifier (parser
);
18056 name
= cp_parser_lookup_name (parser
, identifier
,
18058 /*is_template=*/false,
18059 /*is_namespace=*/false,
18060 /*check_dependency=*/true,
18061 /*ambiguous_decls=*/NULL
,
18063 if (name
&& name
!= error_mark_node
)
18065 type
= TREE_TYPE (name
);
18066 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18068 /* Are template enums allowed in ISO? */
18069 if (template_parm_scope_p ())
18070 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18071 "%qD is an enumeration template", name
);
18072 /* ignore a typename reference, for it will be solved by name
18077 else if (nested_name_specifier
== error_mark_node
)
18078 /* We already issued an error. */;
18081 error_at (type_start_token
->location
,
18082 "%qD does not name an enumeration in %qT",
18083 identifier
, nested_name_specifier
);
18084 nested_name_specifier
= error_mark_node
;
18089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18090 identifier
= cp_parser_identifier (parser
);
18093 identifier
= make_anon_name ();
18096 error_at (type_start_token
->location
,
18097 "unnamed scoped enum is not allowed");
18100 pop_deferring_access_checks ();
18102 /* Check for the `:' that denotes a specified underlying type in C++0x.
18103 Note that a ':' could also indicate a bitfield width, however. */
18104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18106 cp_decl_specifier_seq type_specifiers
;
18108 /* Consume the `:'. */
18109 cp_lexer_consume_token (parser
->lexer
);
18111 /* Parse the type-specifier-seq. */
18112 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18113 /*is_trailing_return=*/false,
18116 /* At this point this is surely not elaborated type specifier. */
18117 if (!cp_parser_parse_definitely (parser
))
18120 if (cxx_dialect
< cxx11
)
18121 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18123 has_underlying_type
= true;
18125 /* If that didn't work, stop. */
18126 if (type_specifiers
.type
!= error_mark_node
)
18128 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18129 /*initialized=*/0, NULL
);
18130 if (underlying_type
== error_mark_node
18131 || check_for_bare_parameter_packs (underlying_type
))
18132 underlying_type
= NULL_TREE
;
18136 /* Look for the `{' but don't consume it yet. */
18137 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18139 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18141 cp_parser_error (parser
, "expected %<{%>");
18142 if (has_underlying_type
)
18148 /* An opaque-enum-specifier must have a ';' here. */
18149 if ((scoped_enum_p
|| underlying_type
)
18150 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18152 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18153 if (has_underlying_type
)
18161 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18164 if (nested_name_specifier
)
18166 if (CLASS_TYPE_P (nested_name_specifier
))
18168 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18169 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18170 push_scope (nested_name_specifier
);
18172 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18174 push_nested_namespace (nested_name_specifier
);
18178 /* Issue an error message if type-definitions are forbidden here. */
18179 if (!cp_parser_check_type_definition (parser
))
18180 type
= error_mark_node
;
18182 /* Create the new type. We do this before consuming the opening
18183 brace so the enum will be recorded as being on the line of its
18184 tag (or the 'enum' keyword, if there is no tag). */
18185 type
= start_enum (identifier
, type
, underlying_type
,
18186 attributes
, scoped_enum_p
, &is_new_type
);
18188 /* If the next token is not '{' it is an opaque-enum-specifier or an
18189 elaborated-type-specifier. */
18190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18192 timevar_push (TV_PARSE_ENUM
);
18193 if (nested_name_specifier
18194 && nested_name_specifier
!= error_mark_node
)
18196 /* The following catches invalid code such as:
18197 enum class S<int>::E { A, B, C }; */
18198 if (!processing_specialization
18199 && CLASS_TYPE_P (nested_name_specifier
)
18200 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18201 error_at (type_start_token
->location
, "cannot add an enumerator "
18202 "list to a template instantiation");
18204 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18206 error_at (type_start_token
->location
,
18207 "%<%T::%E%> has not been declared",
18208 TYPE_CONTEXT (nested_name_specifier
),
18209 nested_name_specifier
);
18210 type
= error_mark_node
;
18212 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18213 && !CLASS_TYPE_P (nested_name_specifier
))
18215 error_at (type_start_token
->location
, "nested name specifier "
18216 "%qT for enum declaration does not name a class "
18217 "or namespace", nested_name_specifier
);
18218 type
= error_mark_node
;
18220 /* If that scope does not contain the scope in which the
18221 class was originally declared, the program is invalid. */
18222 else if (prev_scope
&& !is_ancestor (prev_scope
,
18223 nested_name_specifier
))
18225 if (at_namespace_scope_p ())
18226 error_at (type_start_token
->location
,
18227 "declaration of %qD in namespace %qD which does not "
18229 type
, prev_scope
, nested_name_specifier
);
18231 error_at (type_start_token
->location
,
18232 "declaration of %qD in %qD which does not "
18234 type
, prev_scope
, nested_name_specifier
);
18235 type
= error_mark_node
;
18237 /* If that scope is the scope where the declaration is being placed
18238 the program is invalid. */
18239 else if (CLASS_TYPE_P (nested_name_specifier
)
18240 && CLASS_TYPE_P (prev_scope
)
18241 && same_type_p (nested_name_specifier
, prev_scope
))
18243 permerror (type_start_token
->location
,
18244 "extra qualification not allowed");
18245 nested_name_specifier
= NULL_TREE
;
18250 begin_scope (sk_scoped_enum
, type
);
18252 /* Consume the opening brace. */
18253 matching_braces braces
;
18254 braces
.consume_open (parser
);
18256 if (type
== error_mark_node
)
18257 ; /* Nothing to add */
18258 else if (OPAQUE_ENUM_P (type
)
18259 || (cxx_dialect
> cxx98
&& processing_specialization
))
18261 new_value_list
= true;
18262 SET_OPAQUE_ENUM_P (type
, false);
18263 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18267 error_at (type_start_token
->location
,
18268 "multiple definition of %q#T", type
);
18269 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18270 "previous definition here");
18271 type
= error_mark_node
;
18274 if (type
== error_mark_node
)
18275 cp_parser_skip_to_end_of_block_or_statement (parser
);
18276 /* If the next token is not '}', then there are some enumerators. */
18277 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18279 if (is_unnamed
&& !scoped_enum_p
)
18280 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18281 "ISO C++ forbids empty unnamed enum");
18284 cp_parser_enumerator_list (parser
, type
);
18286 /* Consume the final '}'. */
18287 braces
.require_close (parser
);
18291 timevar_pop (TV_PARSE_ENUM
);
18295 /* If a ';' follows, then it is an opaque-enum-specifier
18296 and additional restrictions apply. */
18297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18300 error_at (type_start_token
->location
,
18301 "opaque-enum-specifier without name");
18302 else if (nested_name_specifier
)
18303 error_at (type_start_token
->location
,
18304 "opaque-enum-specifier must use a simple identifier");
18308 /* Look for trailing attributes to apply to this enumeration, and
18309 apply them if appropriate. */
18310 if (cp_parser_allow_gnu_extensions_p (parser
))
18312 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18313 cplus_decl_attributes (&type
,
18315 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18318 /* Finish up the enumeration. */
18319 if (type
!= error_mark_node
)
18321 if (new_value_list
)
18322 finish_enum_value_list (type
);
18324 finish_enum (type
);
18327 if (nested_name_specifier
)
18329 if (CLASS_TYPE_P (nested_name_specifier
))
18331 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18332 pop_scope (nested_name_specifier
);
18334 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18336 pop_nested_namespace (nested_name_specifier
);
18340 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18344 /* Parse an enumerator-list. The enumerators all have the indicated
18348 enumerator-definition
18349 enumerator-list , enumerator-definition */
18352 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18356 /* Parse an enumerator-definition. */
18357 cp_parser_enumerator_definition (parser
, type
);
18359 /* If the next token is not a ',', we've reached the end of
18361 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18363 /* Otherwise, consume the `,' and keep going. */
18364 cp_lexer_consume_token (parser
->lexer
);
18365 /* If the next token is a `}', there is a trailing comma. */
18366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18368 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18369 pedwarn (input_location
, OPT_Wpedantic
,
18370 "comma at end of enumerator list");
18376 /* Parse an enumerator-definition. The enumerator has the indicated
18379 enumerator-definition:
18381 enumerator = constant-expression
18388 enumerator-definition:
18389 enumerator attributes [opt]
18390 enumerator attributes [opt] = constant-expression */
18393 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18399 /* Save the input location because we are interested in the location
18400 of the identifier and not the location of the explicit value. */
18401 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18403 /* Look for the identifier. */
18404 identifier
= cp_parser_identifier (parser
);
18405 if (identifier
== error_mark_node
)
18408 /* Parse any specified attributes. */
18409 tree attrs
= cp_parser_attributes_opt (parser
);
18411 /* If the next token is an '=', then there is an explicit value. */
18412 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18414 /* Consume the `=' token. */
18415 cp_lexer_consume_token (parser
->lexer
);
18416 /* Parse the value. */
18417 value
= cp_parser_constant_expression (parser
);
18422 /* If we are processing a template, make sure the initializer of the
18423 enumerator doesn't contain any bare template parameter pack. */
18424 if (check_for_bare_parameter_packs (value
))
18425 value
= error_mark_node
;
18427 /* Create the enumerator. */
18428 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18431 /* Parse a namespace-name.
18434 original-namespace-name
18437 Returns the NAMESPACE_DECL for the namespace. */
18440 cp_parser_namespace_name (cp_parser
* parser
)
18443 tree namespace_decl
;
18445 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18447 /* Get the name of the namespace. */
18448 identifier
= cp_parser_identifier (parser
);
18449 if (identifier
== error_mark_node
)
18450 return error_mark_node
;
18452 /* Look up the identifier in the currently active scope. Look only
18453 for namespaces, due to:
18455 [basic.lookup.udir]
18457 When looking up a namespace-name in a using-directive or alias
18458 definition, only namespace names are considered.
18462 [basic.lookup.qual]
18464 During the lookup of a name preceding the :: scope resolution
18465 operator, object, function, and enumerator names are ignored.
18467 (Note that cp_parser_qualifying_entity only calls this
18468 function if the token after the name is the scope resolution
18470 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18472 /*is_template=*/false,
18473 /*is_namespace=*/true,
18474 /*check_dependency=*/true,
18475 /*ambiguous_decls=*/NULL
,
18477 /* If it's not a namespace, issue an error. */
18478 if (namespace_decl
== error_mark_node
18479 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18481 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18483 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18484 if (namespace_decl
== error_mark_node
18485 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18486 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18489 cp_parser_error (parser
, "expected namespace-name");
18490 namespace_decl
= error_mark_node
;
18493 return namespace_decl
;
18496 /* Parse a namespace-definition.
18498 namespace-definition:
18499 named-namespace-definition
18500 unnamed-namespace-definition
18502 named-namespace-definition:
18503 original-namespace-definition
18504 extension-namespace-definition
18506 original-namespace-definition:
18507 namespace identifier { namespace-body }
18509 extension-namespace-definition:
18510 namespace original-namespace-name { namespace-body }
18512 unnamed-namespace-definition:
18513 namespace { namespace-body } */
18516 cp_parser_namespace_definition (cp_parser
* parser
)
18519 int nested_definition_count
= 0;
18521 cp_ensure_no_omp_declare_simd (parser
);
18522 cp_ensure_no_oacc_routine (parser
);
18524 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18528 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18529 cp_lexer_consume_token (parser
->lexer
);
18532 /* Look for the `namespace' keyword. */
18534 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18536 /* Parse any specified attributes before the identifier. */
18537 tree attribs
= cp_parser_attributes_opt (parser
);
18541 identifier
= NULL_TREE
;
18543 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18545 identifier
= cp_parser_identifier (parser
);
18547 /* Parse any attributes specified after the identifier. */
18548 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18551 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18554 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18555 pedwarn (input_location
, OPT_Wpedantic
,
18556 "nested namespace definitions only available with "
18557 "-std=c++17 or -std=gnu++17");
18559 /* Nested namespace names can create new namespaces (unlike
18560 other qualified-ids). */
18561 if (int count
= identifier
? push_namespace (identifier
) : 0)
18562 nested_definition_count
+= count
;
18564 cp_parser_error (parser
, "nested namespace name required");
18565 cp_lexer_consume_token (parser
->lexer
);
18568 if (nested_definition_count
&& !identifier
)
18569 cp_parser_error (parser
, "namespace name required");
18571 if (nested_definition_count
&& attribs
)
18572 error_at (token
->location
,
18573 "a nested namespace definition cannot have attributes");
18574 if (nested_definition_count
&& is_inline
)
18575 error_at (token
->location
,
18576 "a nested namespace definition cannot be inline");
18578 /* Start the namespace. */
18579 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18581 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18583 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18585 /* Look for the `{' to validate starting the namespace. */
18586 matching_braces braces
;
18587 if (braces
.require_open (parser
))
18589 /* Parse the body of the namespace. */
18590 cp_parser_namespace_body (parser
);
18592 /* Look for the final `}'. */
18593 braces
.require_close (parser
);
18596 if (has_visibility
)
18597 pop_visibility (1);
18599 /* Pop the nested namespace definitions. */
18600 while (nested_definition_count
--)
18604 /* Parse a namespace-body.
18607 declaration-seq [opt] */
18610 cp_parser_namespace_body (cp_parser
* parser
)
18612 cp_parser_declaration_seq_opt (parser
);
18615 /* Parse a namespace-alias-definition.
18617 namespace-alias-definition:
18618 namespace identifier = qualified-namespace-specifier ; */
18621 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18624 tree namespace_specifier
;
18626 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18628 /* Look for the `namespace' keyword. */
18629 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18630 /* Look for the identifier. */
18631 identifier
= cp_parser_identifier (parser
);
18632 if (identifier
== error_mark_node
)
18634 /* Look for the `=' token. */
18635 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18636 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18638 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18639 /* Skip the definition. */
18640 cp_lexer_consume_token (parser
->lexer
);
18641 if (cp_parser_skip_to_closing_brace (parser
))
18642 cp_lexer_consume_token (parser
->lexer
);
18645 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18646 /* Look for the qualified-namespace-specifier. */
18647 namespace_specifier
18648 = cp_parser_qualified_namespace_specifier (parser
);
18649 /* Look for the `;' token. */
18650 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18652 /* Register the alias in the symbol table. */
18653 do_namespace_alias (identifier
, namespace_specifier
);
18656 /* Parse a qualified-namespace-specifier.
18658 qualified-namespace-specifier:
18659 :: [opt] nested-name-specifier [opt] namespace-name
18661 Returns a NAMESPACE_DECL corresponding to the specified
18665 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18667 /* Look for the optional `::'. */
18668 cp_parser_global_scope_opt (parser
,
18669 /*current_scope_valid_p=*/false);
18671 /* Look for the optional nested-name-specifier. */
18672 cp_parser_nested_name_specifier_opt (parser
,
18673 /*typename_keyword_p=*/false,
18674 /*check_dependency_p=*/true,
18676 /*is_declaration=*/true);
18678 return cp_parser_namespace_name (parser
);
18681 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18682 access declaration.
18685 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18686 using :: unqualified-id ;
18688 access-declaration:
18694 cp_parser_using_declaration (cp_parser
* parser
,
18695 bool access_declaration_p
)
18698 bool typename_p
= false;
18699 bool global_scope_p
;
18703 int oldcount
= errorcount
;
18704 cp_token
*diag_token
= NULL
;
18706 if (access_declaration_p
)
18708 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18709 cp_parser_parse_tentatively (parser
);
18713 /* Look for the `using' keyword. */
18714 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18717 /* Peek at the next token. */
18718 token
= cp_lexer_peek_token (parser
->lexer
);
18719 /* See if it's `typename'. */
18720 if (token
->keyword
== RID_TYPENAME
)
18722 /* Remember that we've seen it. */
18724 /* Consume the `typename' token. */
18725 cp_lexer_consume_token (parser
->lexer
);
18729 /* Look for the optional global scope qualification. */
18731 = (cp_parser_global_scope_opt (parser
,
18732 /*current_scope_valid_p=*/false)
18735 /* If we saw `typename', or didn't see `::', then there must be a
18736 nested-name-specifier present. */
18737 if (typename_p
|| !global_scope_p
)
18739 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18740 /*check_dependency_p=*/true,
18742 /*is_declaration=*/true);
18743 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18745 cp_parser_skip_to_end_of_block_or_statement (parser
);
18749 /* Otherwise, we could be in either of the two productions. In that
18750 case, treat the nested-name-specifier as optional. */
18752 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18753 /*typename_keyword_p=*/false,
18754 /*check_dependency_p=*/true,
18756 /*is_declaration=*/true);
18758 qscope
= global_namespace
;
18759 else if (UNSCOPED_ENUM_P (qscope
))
18760 qscope
= CP_TYPE_CONTEXT (qscope
);
18762 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18763 /* Something has already gone wrong; there's no need to parse
18764 further. Since an error has occurred, the return value of
18765 cp_parser_parse_definitely will be false, as required. */
18766 return cp_parser_parse_definitely (parser
);
18768 token
= cp_lexer_peek_token (parser
->lexer
);
18769 /* Parse the unqualified-id. */
18770 identifier
= cp_parser_unqualified_id (parser
,
18771 /*template_keyword_p=*/false,
18772 /*check_dependency_p=*/true,
18773 /*declarator_p=*/true,
18774 /*optional_p=*/false);
18776 if (access_declaration_p
)
18778 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18779 cp_parser_simulate_error (parser
);
18780 if (!cp_parser_parse_definitely (parser
))
18783 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18785 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18786 if (cxx_dialect
< cxx17
18787 && !in_system_header_at (ell
->location
))
18788 pedwarn (ell
->location
, 0,
18789 "pack expansion in using-declaration only available "
18790 "with -std=c++17 or -std=gnu++17");
18791 qscope
= make_pack_expansion (qscope
);
18794 /* The function we call to handle a using-declaration is different
18795 depending on what scope we are in. */
18796 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18798 else if (!identifier_p (identifier
)
18799 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18800 /* [namespace.udecl]
18802 A using declaration shall not name a template-id. */
18803 error_at (token
->location
,
18804 "a template-id may not appear in a using-declaration");
18807 if (at_class_scope_p ())
18809 /* Create the USING_DECL. */
18810 decl
= do_class_using_decl (qscope
, identifier
);
18812 if (decl
&& typename_p
)
18813 USING_DECL_TYPENAME_P (decl
) = 1;
18815 if (check_for_bare_parameter_packs (decl
))
18817 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18821 /* Add it to the list of members in this class. */
18822 finish_member_declaration (decl
);
18826 decl
= cp_parser_lookup_name_simple (parser
,
18829 if (decl
== error_mark_node
)
18830 cp_parser_name_lookup_error (parser
, identifier
,
18833 else if (check_for_bare_parameter_packs (decl
))
18835 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18838 else if (!at_namespace_scope_p ())
18839 finish_local_using_decl (decl
, qscope
, identifier
);
18841 finish_namespace_using_decl (decl
, qscope
, identifier
);
18845 if (!access_declaration_p
18846 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18848 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18849 if (cxx_dialect
< cxx17
)
18850 pedwarn (comma
->location
, 0,
18851 "comma-separated list in using-declaration only available "
18852 "with -std=c++17 or -std=gnu++17");
18856 /* Look for the final `;'. */
18857 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18859 if (access_declaration_p
&& errorcount
== oldcount
)
18860 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18861 "access declarations are deprecated "
18862 "in favour of using-declarations; "
18863 "suggestion: add the %<using%> keyword");
18868 /* Parse an alias-declaration.
18871 using identifier attribute-specifier-seq [opt] = type-id */
18874 cp_parser_alias_declaration (cp_parser
* parser
)
18876 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18877 location_t id_location
;
18878 cp_declarator
*declarator
;
18879 cp_decl_specifier_seq decl_specs
;
18881 const char *saved_message
= NULL
;
18883 /* Look for the `using' keyword. */
18884 cp_token
*using_token
18885 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18886 if (using_token
== NULL
)
18887 return error_mark_node
;
18889 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18890 id
= cp_parser_identifier (parser
);
18891 if (id
== error_mark_node
)
18892 return error_mark_node
;
18894 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18895 attributes
= cp_parser_attributes_opt (parser
);
18896 if (attributes
== error_mark_node
)
18897 return error_mark_node
;
18899 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18901 if (cp_parser_error_occurred (parser
))
18902 return error_mark_node
;
18904 cp_parser_commit_to_tentative_parse (parser
);
18906 /* Now we are going to parse the type-id of the declaration. */
18911 "A type-specifier-seq shall not define a class or enumeration
18912 unless it appears in the type-id of an alias-declaration (7.1.3) that
18913 is not the declaration of a template-declaration."
18915 In other words, if we currently are in an alias template, the
18916 type-id should not define a type.
18918 So let's set parser->type_definition_forbidden_message in that
18919 case; cp_parser_check_type_definition (called by
18920 cp_parser_class_specifier) will then emit an error if a type is
18921 defined in the type-id. */
18922 if (parser
->num_template_parameter_lists
)
18924 saved_message
= parser
->type_definition_forbidden_message
;
18925 parser
->type_definition_forbidden_message
=
18926 G_("types may not be defined in alias template declarations");
18929 type
= cp_parser_type_id (parser
);
18931 /* Restore the error message if need be. */
18932 if (parser
->num_template_parameter_lists
)
18933 parser
->type_definition_forbidden_message
= saved_message
;
18935 if (type
== error_mark_node
18936 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18938 cp_parser_skip_to_end_of_block_or_statement (parser
);
18939 return error_mark_node
;
18942 /* A typedef-name can also be introduced by an alias-declaration. The
18943 identifier following the using keyword becomes a typedef-name. It has
18944 the same semantics as if it were introduced by the typedef
18945 specifier. In particular, it does not define a new type and it shall
18946 not appear in the type-id. */
18948 clear_decl_specs (&decl_specs
);
18949 decl_specs
.type
= type
;
18950 if (attributes
!= NULL_TREE
)
18952 decl_specs
.attributes
= attributes
;
18953 set_and_check_decl_spec_loc (&decl_specs
,
18957 set_and_check_decl_spec_loc (&decl_specs
,
18960 set_and_check_decl_spec_loc (&decl_specs
,
18964 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18965 declarator
->id_loc
= id_location
;
18967 member_p
= at_class_scope_p ();
18969 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18970 NULL_TREE
, attributes
);
18972 decl
= start_decl (declarator
, &decl_specs
, 0,
18973 attributes
, NULL_TREE
, &pushed_scope
);
18974 if (decl
== error_mark_node
)
18977 // Attach constraints to the alias declaration.
18978 if (flag_concepts
&& current_template_parms
)
18980 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18981 tree constr
= build_constraints (reqs
, NULL_TREE
);
18982 set_constraints (decl
, constr
);
18985 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18988 pop_scope (pushed_scope
);
18990 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18991 added into the symbol table; otherwise, return the TYPE_DECL. */
18992 if (DECL_LANG_SPECIFIC (decl
)
18993 && DECL_TEMPLATE_INFO (decl
)
18994 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18996 decl
= DECL_TI_TEMPLATE (decl
);
18998 check_member_template (decl
);
19004 /* Parse a using-directive.
19007 using namespace :: [opt] nested-name-specifier [opt]
19008 namespace-name ; */
19011 cp_parser_using_directive (cp_parser
* parser
)
19013 tree namespace_decl
;
19016 /* Look for the `using' keyword. */
19017 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19018 /* And the `namespace' keyword. */
19019 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19020 /* Look for the optional `::' operator. */
19021 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19022 /* And the optional nested-name-specifier. */
19023 cp_parser_nested_name_specifier_opt (parser
,
19024 /*typename_keyword_p=*/false,
19025 /*check_dependency_p=*/true,
19027 /*is_declaration=*/true);
19028 /* Get the namespace being used. */
19029 namespace_decl
= cp_parser_namespace_name (parser
);
19030 /* And any specified attributes. */
19031 attribs
= cp_parser_attributes_opt (parser
);
19033 /* Update the symbol table. */
19034 if (namespace_bindings_p ())
19035 finish_namespace_using_directive (namespace_decl
, attribs
);
19037 finish_local_using_directive (namespace_decl
, attribs
);
19039 /* Look for the final `;'. */
19040 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19043 /* Parse an asm-definition.
19046 asm ( string-literal ) ;
19051 asm volatile [opt] ( string-literal ) ;
19052 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19053 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19054 : asm-operand-list [opt] ) ;
19055 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19056 : asm-operand-list [opt]
19057 : asm-clobber-list [opt] ) ;
19058 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19059 : asm-clobber-list [opt]
19060 : asm-goto-list ) ; */
19063 cp_parser_asm_definition (cp_parser
* parser
)
19066 tree outputs
= NULL_TREE
;
19067 tree inputs
= NULL_TREE
;
19068 tree clobbers
= NULL_TREE
;
19069 tree labels
= NULL_TREE
;
19071 bool volatile_p
= false;
19072 bool extended_p
= false;
19073 bool invalid_inputs_p
= false;
19074 bool invalid_outputs_p
= false;
19075 bool goto_p
= false;
19076 required_token missing
= RT_NONE
;
19078 /* Look for the `asm' keyword. */
19079 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19081 if (parser
->in_function_body
19082 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19084 error ("%<asm%> in %<constexpr%> function");
19085 cp_function_chain
->invalid_constexpr
= true;
19088 /* See if the next token is `volatile'. */
19089 if (cp_parser_allow_gnu_extensions_p (parser
)
19090 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19092 /* Remember that we saw the `volatile' keyword. */
19094 /* Consume the token. */
19095 cp_lexer_consume_token (parser
->lexer
);
19097 if (cp_parser_allow_gnu_extensions_p (parser
)
19098 && parser
->in_function_body
19099 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19101 /* Remember that we saw the `goto' keyword. */
19103 /* Consume the token. */
19104 cp_lexer_consume_token (parser
->lexer
);
19106 /* Look for the opening `('. */
19107 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19109 /* Look for the string. */
19110 string
= cp_parser_string_literal (parser
, false, false);
19111 if (string
== error_mark_node
)
19113 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19114 /*consume_paren=*/true);
19118 /* If we're allowing GNU extensions, check for the extended assembly
19119 syntax. Unfortunately, the `:' tokens need not be separated by
19120 a space in C, and so, for compatibility, we tolerate that here
19121 too. Doing that means that we have to treat the `::' operator as
19123 if (cp_parser_allow_gnu_extensions_p (parser
)
19124 && parser
->in_function_body
19125 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19126 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19128 bool inputs_p
= false;
19129 bool clobbers_p
= false;
19130 bool labels_p
= false;
19132 /* The extended syntax was used. */
19135 /* Look for outputs. */
19136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19138 /* Consume the `:'. */
19139 cp_lexer_consume_token (parser
->lexer
);
19140 /* Parse the output-operands. */
19141 if (cp_lexer_next_token_is_not (parser
->lexer
,
19143 && cp_lexer_next_token_is_not (parser
->lexer
,
19145 && cp_lexer_next_token_is_not (parser
->lexer
,
19149 outputs
= cp_parser_asm_operand_list (parser
);
19150 if (outputs
== error_mark_node
)
19151 invalid_outputs_p
= true;
19154 /* If the next token is `::', there are no outputs, and the
19155 next token is the beginning of the inputs. */
19156 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19157 /* The inputs are coming next. */
19160 /* Look for inputs. */
19162 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19164 /* Consume the `:' or `::'. */
19165 cp_lexer_consume_token (parser
->lexer
);
19166 /* Parse the output-operands. */
19167 if (cp_lexer_next_token_is_not (parser
->lexer
,
19169 && cp_lexer_next_token_is_not (parser
->lexer
,
19171 && cp_lexer_next_token_is_not (parser
->lexer
,
19174 inputs
= cp_parser_asm_operand_list (parser
);
19175 if (inputs
== error_mark_node
)
19176 invalid_inputs_p
= true;
19179 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19180 /* The clobbers are coming next. */
19183 /* Look for clobbers. */
19185 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19188 /* Consume the `:' or `::'. */
19189 cp_lexer_consume_token (parser
->lexer
);
19190 /* Parse the clobbers. */
19191 if (cp_lexer_next_token_is_not (parser
->lexer
,
19193 && cp_lexer_next_token_is_not (parser
->lexer
,
19195 clobbers
= cp_parser_asm_clobber_list (parser
);
19198 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19199 /* The labels are coming next. */
19202 /* Look for labels. */
19204 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19207 /* Consume the `:' or `::'. */
19208 cp_lexer_consume_token (parser
->lexer
);
19209 /* Parse the labels. */
19210 labels
= cp_parser_asm_label_list (parser
);
19213 if (goto_p
&& !labels_p
)
19214 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19217 missing
= RT_COLON_SCOPE
;
19219 /* Look for the closing `)'. */
19220 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19221 missing
? missing
: RT_CLOSE_PAREN
))
19222 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19223 /*consume_paren=*/true);
19224 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19226 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19228 /* Create the ASM_EXPR. */
19229 if (parser
->in_function_body
)
19231 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19232 inputs
, clobbers
, labels
);
19233 /* If the extended syntax was not used, mark the ASM_EXPR. */
19236 tree temp
= asm_stmt
;
19237 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19238 temp
= TREE_OPERAND (temp
, 0);
19240 ASM_INPUT_P (temp
) = 1;
19244 symtab
->finalize_toplevel_asm (string
);
19248 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19249 type that comes from the decl-specifier-seq. */
19252 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19254 for (cp_declarator
*d
= declarator
; d
;)
19264 if (TYPE_PTRMEMFUNC_P (type
))
19265 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19266 type
= TREE_TYPE (type
);
19274 /* Declarators [gram.dcl.decl] */
19276 /* Parse an init-declarator.
19279 declarator initializer [opt]
19284 declarator asm-specification [opt] attributes [opt] initializer [opt]
19286 function-definition:
19287 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19289 decl-specifier-seq [opt] declarator function-try-block
19293 function-definition:
19294 __extension__ function-definition
19298 function-definition:
19299 decl-specifier-seq [opt] declarator function-transaction-block
19301 The DECL_SPECIFIERS apply to this declarator. Returns a
19302 representation of the entity declared. If MEMBER_P is TRUE, then
19303 this declarator appears in a class scope. The new DECL created by
19304 this declarator is returned.
19306 The CHECKS are access checks that should be performed once we know
19307 what entity is being declared (and, therefore, what classes have
19310 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19311 for a function-definition here as well. If the declarator is a
19312 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19313 be TRUE upon return. By that point, the function-definition will
19314 have been completely parsed.
19316 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19319 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19320 parsed declaration if it is an uninitialized single declarator not followed
19321 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19322 if present, will not be consumed. If returned, this declarator will be
19323 created with SD_INITIALIZED but will not call cp_finish_decl.
19325 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19326 and there is an initializer, the pointed location_t is set to the
19327 location of the '=' or `(', or '{' in C++11 token introducing the
19331 cp_parser_init_declarator (cp_parser
* parser
,
19332 cp_decl_specifier_seq
*decl_specifiers
,
19333 vec
<deferred_access_check
, va_gc
> *checks
,
19334 bool function_definition_allowed_p
,
19336 int declares_class_or_enum
,
19337 bool* function_definition_p
,
19338 tree
* maybe_range_for_decl
,
19339 location_t
* init_loc
,
19342 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19343 *attributes_start_token
= NULL
;
19344 cp_declarator
*declarator
;
19345 tree prefix_attributes
;
19346 tree attributes
= NULL
;
19347 tree asm_specification
;
19349 tree decl
= NULL_TREE
;
19351 int is_initialized
;
19352 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19353 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19355 enum cpp_ttype initialization_kind
;
19356 bool is_direct_init
= false;
19357 bool is_non_constant_init
;
19358 int ctor_dtor_or_conv_p
;
19359 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19360 tree pushed_scope
= NULL_TREE
;
19361 bool range_for_decl_p
= false;
19362 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19363 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19365 /* Gather the attributes that were provided with the
19366 decl-specifiers. */
19367 prefix_attributes
= decl_specifiers
->attributes
;
19369 /* Assume that this is not the declarator for a function
19371 if (function_definition_p
)
19372 *function_definition_p
= false;
19374 /* Default arguments are only permitted for function parameters. */
19375 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19376 parser
->default_arg_ok_p
= false;
19378 /* Defer access checks while parsing the declarator; we cannot know
19379 what names are accessible until we know what is being
19381 resume_deferring_access_checks ();
19383 token
= cp_lexer_peek_token (parser
->lexer
);
19385 /* Parse the declarator. */
19387 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19388 &ctor_dtor_or_conv_p
,
19389 /*parenthesized_p=*/NULL
,
19390 member_p
, friend_p
);
19391 /* Gather up the deferred checks. */
19392 stop_deferring_access_checks ();
19394 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19396 /* If the DECLARATOR was erroneous, there's no need to go
19398 if (declarator
== cp_error_declarator
)
19399 return error_mark_node
;
19401 /* Check that the number of template-parameter-lists is OK. */
19402 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19404 return error_mark_node
;
19406 if (declares_class_or_enum
& 2)
19407 cp_parser_check_for_definition_in_return_type (declarator
,
19408 decl_specifiers
->type
,
19409 decl_specifiers
->locations
[ds_type_spec
]);
19411 /* Figure out what scope the entity declared by the DECLARATOR is
19412 located in. `grokdeclarator' sometimes changes the scope, so
19413 we compute it now. */
19414 scope
= get_scope_of_declarator (declarator
);
19416 /* Perform any lookups in the declared type which were thought to be
19417 dependent, but are not in the scope of the declarator. */
19418 decl_specifiers
->type
19419 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19421 /* If we're allowing GNU extensions, look for an
19422 asm-specification. */
19423 if (cp_parser_allow_gnu_extensions_p (parser
))
19425 /* Look for an asm-specification. */
19426 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19427 asm_specification
= cp_parser_asm_specification_opt (parser
);
19430 asm_specification
= NULL_TREE
;
19432 /* Look for attributes. */
19433 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19434 attributes
= cp_parser_attributes_opt (parser
);
19436 /* Peek at the next token. */
19437 token
= cp_lexer_peek_token (parser
->lexer
);
19439 bool bogus_implicit_tmpl
= false;
19441 if (function_declarator_p (declarator
))
19443 /* Handle C++17 deduction guides. */
19444 if (!decl_specifiers
->type
19445 && ctor_dtor_or_conv_p
<= 0
19446 && cxx_dialect
>= cxx17
)
19448 cp_declarator
*id
= get_id_declarator (declarator
);
19449 tree name
= id
->u
.id
.unqualified_name
;
19450 parser
->scope
= id
->u
.id
.qualifying_scope
;
19451 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19453 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19454 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19456 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19457 id
->u
.id
.sfk
= sfk_deduction_guide
;
19458 ctor_dtor_or_conv_p
= 1;
19462 /* Check to see if the token indicates the start of a
19463 function-definition. */
19464 if (cp_parser_token_starts_function_definition_p (token
))
19466 if (!function_definition_allowed_p
)
19468 /* If a function-definition should not appear here, issue an
19470 cp_parser_error (parser
,
19471 "a function-definition is not allowed here");
19472 return error_mark_node
;
19475 location_t func_brace_location
19476 = cp_lexer_peek_token (parser
->lexer
)->location
;
19478 /* Neither attributes nor an asm-specification are allowed
19479 on a function-definition. */
19480 if (asm_specification
)
19481 error_at (asm_spec_start_token
->location
,
19482 "an asm-specification is not allowed "
19483 "on a function-definition");
19485 error_at (attributes_start_token
->location
,
19486 "attributes are not allowed "
19487 "on a function-definition");
19488 /* This is a function-definition. */
19489 *function_definition_p
= true;
19491 /* Parse the function definition. */
19493 decl
= cp_parser_save_member_function_body (parser
,
19496 prefix_attributes
);
19499 (cp_parser_function_definition_from_specifiers_and_declarator
19500 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19502 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19504 /* This is where the prologue starts... */
19505 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19506 = func_brace_location
;
19512 else if (parser
->fully_implicit_function_template_p
)
19514 /* A non-template declaration involving a function parameter list
19515 containing an implicit template parameter will be made into a
19516 template. If the resulting declaration is not going to be an
19517 actual function then finish the template scope here to prevent it.
19518 An error message will be issued once we have a decl to talk about.
19520 FIXME probably we should do type deduction rather than create an
19521 implicit template, but the standard currently doesn't allow it. */
19522 bogus_implicit_tmpl
= true;
19523 finish_fully_implicit_template (parser
, NULL_TREE
);
19528 Only in function declarations for constructors, destructors, type
19529 conversions, and deduction guides can the decl-specifier-seq be omitted.
19531 We explicitly postpone this check past the point where we handle
19532 function-definitions because we tolerate function-definitions
19533 that are missing their return types in some modes. */
19534 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19536 cp_parser_error (parser
,
19537 "expected constructor, destructor, or type conversion");
19538 return error_mark_node
;
19541 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19542 if (token
->type
== CPP_EQ
19543 || token
->type
== CPP_OPEN_PAREN
19544 || token
->type
== CPP_OPEN_BRACE
)
19546 is_initialized
= SD_INITIALIZED
;
19547 initialization_kind
= token
->type
;
19548 if (maybe_range_for_decl
)
19549 *maybe_range_for_decl
= error_mark_node
;
19550 tmp_init_loc
= token
->location
;
19551 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19552 *init_loc
= tmp_init_loc
;
19554 if (token
->type
== CPP_EQ
19555 && function_declarator_p (declarator
))
19557 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19558 if (t2
->keyword
== RID_DEFAULT
)
19559 is_initialized
= SD_DEFAULTED
;
19560 else if (t2
->keyword
== RID_DELETE
)
19561 is_initialized
= SD_DELETED
;
19566 /* If the init-declarator isn't initialized and isn't followed by a
19567 `,' or `;', it's not a valid init-declarator. */
19568 if (token
->type
!= CPP_COMMA
19569 && token
->type
!= CPP_SEMICOLON
)
19571 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19572 range_for_decl_p
= true;
19575 if (!maybe_range_for_decl
)
19576 cp_parser_error (parser
, "expected initializer");
19577 return error_mark_node
;
19580 is_initialized
= SD_UNINITIALIZED
;
19581 initialization_kind
= CPP_EOF
;
19584 /* Because start_decl has side-effects, we should only call it if we
19585 know we're going ahead. By this point, we know that we cannot
19586 possibly be looking at any other construct. */
19587 cp_parser_commit_to_tentative_parse (parser
);
19589 /* Enter the newly declared entry in the symbol table. If we're
19590 processing a declaration in a class-specifier, we wait until
19591 after processing the initializer. */
19594 if (parser
->in_unbraced_linkage_specification_p
)
19595 decl_specifiers
->storage_class
= sc_extern
;
19596 decl
= start_decl (declarator
, decl_specifiers
,
19597 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19598 attributes
, prefix_attributes
, &pushed_scope
);
19599 cp_finalize_omp_declare_simd (parser
, decl
);
19600 cp_finalize_oacc_routine (parser
, decl
, false);
19601 /* Adjust location of decl if declarator->id_loc is more appropriate:
19602 set, and decl wasn't merged with another decl, in which case its
19603 location would be different from input_location, and more accurate. */
19605 && declarator
->id_loc
!= UNKNOWN_LOCATION
19606 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19607 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19610 /* Enter the SCOPE. That way unqualified names appearing in the
19611 initializer will be looked up in SCOPE. */
19612 pushed_scope
= push_scope (scope
);
19614 /* Perform deferred access control checks, now that we know in which
19615 SCOPE the declared entity resides. */
19616 if (!member_p
&& decl
)
19618 tree saved_current_function_decl
= NULL_TREE
;
19620 /* If the entity being declared is a function, pretend that we
19621 are in its scope. If it is a `friend', it may have access to
19622 things that would not otherwise be accessible. */
19623 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19625 saved_current_function_decl
= current_function_decl
;
19626 current_function_decl
= decl
;
19629 /* Perform access checks for template parameters. */
19630 cp_parser_perform_template_parameter_access_checks (checks
);
19632 /* Perform the access control checks for the declarator and the
19633 decl-specifiers. */
19634 perform_deferred_access_checks (tf_warning_or_error
);
19636 /* Restore the saved value. */
19637 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19638 current_function_decl
= saved_current_function_decl
;
19641 /* Parse the initializer. */
19642 initializer
= NULL_TREE
;
19643 is_direct_init
= false;
19644 is_non_constant_init
= true;
19645 if (is_initialized
)
19647 if (function_declarator_p (declarator
))
19649 if (initialization_kind
== CPP_EQ
)
19650 initializer
= cp_parser_pure_specifier (parser
);
19653 /* If the declaration was erroneous, we don't really
19654 know what the user intended, so just silently
19655 consume the initializer. */
19656 if (decl
!= error_mark_node
)
19657 error_at (tmp_init_loc
, "initializer provided for function");
19658 cp_parser_skip_to_closing_parenthesis (parser
,
19659 /*recovering=*/true,
19660 /*or_comma=*/false,
19661 /*consume_paren=*/true);
19666 /* We want to record the extra mangling scope for in-class
19667 initializers of class members and initializers of static data
19668 member templates. The former involves deferring
19669 parsing of the initializer until end of class as with default
19670 arguments. So right here we only handle the latter. */
19671 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19672 start_lambda_scope (decl
);
19673 initializer
= cp_parser_initializer (parser
,
19675 &is_non_constant_init
);
19676 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19677 finish_lambda_scope ();
19678 if (initializer
== error_mark_node
)
19679 cp_parser_skip_to_end_of_statement (parser
);
19683 /* The old parser allows attributes to appear after a parenthesized
19684 initializer. Mark Mitchell proposed removing this functionality
19685 on the GCC mailing lists on 2002-08-13. This parser accepts the
19686 attributes -- but ignores them. */
19687 if (cp_parser_allow_gnu_extensions_p (parser
)
19688 && initialization_kind
== CPP_OPEN_PAREN
)
19689 if (cp_parser_attributes_opt (parser
))
19690 warning (OPT_Wattributes
,
19691 "attributes after parenthesized initializer ignored");
19693 /* And now complain about a non-function implicit template. */
19694 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19695 error_at (DECL_SOURCE_LOCATION (decl
),
19696 "non-function %qD declared as implicit template", decl
);
19698 /* For an in-class declaration, use `grokfield' to create the
19704 pop_scope (pushed_scope
);
19705 pushed_scope
= NULL_TREE
;
19707 decl
= grokfield (declarator
, decl_specifiers
,
19708 initializer
, !is_non_constant_init
,
19709 /*asmspec=*/NULL_TREE
,
19710 attr_chainon (attributes
, prefix_attributes
));
19711 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19712 cp_parser_save_default_args (parser
, decl
);
19713 cp_finalize_omp_declare_simd (parser
, decl
);
19714 cp_finalize_oacc_routine (parser
, decl
, false);
19717 /* Finish processing the declaration. But, skip member
19719 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19721 cp_finish_decl (decl
,
19722 initializer
, !is_non_constant_init
,
19724 /* If the initializer is in parentheses, then this is
19725 a direct-initialization, which means that an
19726 `explicit' constructor is OK. Otherwise, an
19727 `explicit' constructor cannot be used. */
19728 ((is_direct_init
|| !is_initialized
)
19729 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19731 else if ((cxx_dialect
!= cxx98
) && friend_p
19732 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19733 /* Core issue #226 (C++0x only): A default template-argument
19734 shall not be specified in a friend class template
19736 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19737 /*is_partial=*/false, /*is_friend_decl=*/1);
19739 if (!friend_p
&& pushed_scope
)
19740 pop_scope (pushed_scope
);
19742 if (function_declarator_p (declarator
)
19743 && parser
->fully_implicit_function_template_p
)
19746 decl
= finish_fully_implicit_template (parser
, decl
);
19748 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19751 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19752 && type_uses_auto (decl_specifiers
->type
))
19753 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19758 /* Parse a declarator.
19762 ptr-operator declarator
19764 abstract-declarator:
19765 ptr-operator abstract-declarator [opt]
19766 direct-abstract-declarator
19771 attributes [opt] direct-declarator
19772 attributes [opt] ptr-operator declarator
19774 abstract-declarator:
19775 attributes [opt] ptr-operator abstract-declarator [opt]
19776 attributes [opt] direct-abstract-declarator
19778 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19779 detect constructors, destructors, deduction guides, or conversion operators.
19780 It is set to -1 if the declarator is a name, and +1 if it is a
19781 function. Otherwise it is set to zero. Usually you just want to
19782 test for >0, but internally the negative value is used.
19784 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19785 a decl-specifier-seq unless it declares a constructor, destructor,
19786 or conversion. It might seem that we could check this condition in
19787 semantic analysis, rather than parsing, but that makes it difficult
19788 to handle something like `f()'. We want to notice that there are
19789 no decl-specifiers, and therefore realize that this is an
19790 expression, not a declaration.)
19792 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19793 the declarator is a direct-declarator of the form "(...)".
19795 MEMBER_P is true iff this declarator is a member-declarator.
19797 FRIEND_P is true iff this declarator is a friend. */
19799 static cp_declarator
*
19800 cp_parser_declarator (cp_parser
* parser
,
19801 cp_parser_declarator_kind dcl_kind
,
19802 int* ctor_dtor_or_conv_p
,
19803 bool* parenthesized_p
,
19804 bool member_p
, bool friend_p
)
19806 cp_declarator
*declarator
;
19807 enum tree_code code
;
19808 cp_cv_quals cv_quals
;
19810 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19812 /* Assume this is not a constructor, destructor, or type-conversion
19814 if (ctor_dtor_or_conv_p
)
19815 *ctor_dtor_or_conv_p
= 0;
19817 if (cp_parser_allow_gnu_extensions_p (parser
))
19818 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19820 /* Check for the ptr-operator production. */
19821 cp_parser_parse_tentatively (parser
);
19822 /* Parse the ptr-operator. */
19823 code
= cp_parser_ptr_operator (parser
,
19828 /* If that worked, then we have a ptr-operator. */
19829 if (cp_parser_parse_definitely (parser
))
19831 /* If a ptr-operator was found, then this declarator was not
19833 if (parenthesized_p
)
19834 *parenthesized_p
= true;
19835 /* The dependent declarator is optional if we are parsing an
19836 abstract-declarator. */
19837 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19838 cp_parser_parse_tentatively (parser
);
19840 /* Parse the dependent declarator. */
19841 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19842 /*ctor_dtor_or_conv_p=*/NULL
,
19843 /*parenthesized_p=*/NULL
,
19844 /*member_p=*/false,
19847 /* If we are parsing an abstract-declarator, we must handle the
19848 case where the dependent declarator is absent. */
19849 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19850 && !cp_parser_parse_definitely (parser
))
19853 declarator
= cp_parser_make_indirect_declarator
19854 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19856 /* Everything else is a direct-declarator. */
19859 if (parenthesized_p
)
19860 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19862 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19863 ctor_dtor_or_conv_p
,
19864 member_p
, friend_p
);
19867 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19868 declarator
->attributes
= gnu_attributes
;
19872 /* Parse a direct-declarator or direct-abstract-declarator.
19876 direct-declarator ( parameter-declaration-clause )
19877 cv-qualifier-seq [opt]
19878 ref-qualifier [opt]
19879 exception-specification [opt]
19880 direct-declarator [ constant-expression [opt] ]
19883 direct-abstract-declarator:
19884 direct-abstract-declarator [opt]
19885 ( parameter-declaration-clause )
19886 cv-qualifier-seq [opt]
19887 ref-qualifier [opt]
19888 exception-specification [opt]
19889 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19890 ( abstract-declarator )
19892 Returns a representation of the declarator. DCL_KIND is
19893 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19894 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19895 we are parsing a direct-declarator. It is
19896 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19897 of ambiguity we prefer an abstract declarator, as per
19898 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19899 as for cp_parser_declarator. */
19901 static cp_declarator
*
19902 cp_parser_direct_declarator (cp_parser
* parser
,
19903 cp_parser_declarator_kind dcl_kind
,
19904 int* ctor_dtor_or_conv_p
,
19905 bool member_p
, bool friend_p
)
19908 cp_declarator
*declarator
= NULL
;
19909 tree scope
= NULL_TREE
;
19910 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19911 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19913 tree pushed_scope
= NULL_TREE
;
19914 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19918 /* Peek at the next token. */
19919 token
= cp_lexer_peek_token (parser
->lexer
);
19920 if (token
->type
== CPP_OPEN_PAREN
)
19922 /* This is either a parameter-declaration-clause, or a
19923 parenthesized declarator. When we know we are parsing a
19924 named declarator, it must be a parenthesized declarator
19925 if FIRST is true. For instance, `(int)' is a
19926 parameter-declaration-clause, with an omitted
19927 direct-abstract-declarator. But `((*))', is a
19928 parenthesized abstract declarator. Finally, when T is a
19929 template parameter `(T)' is a
19930 parameter-declaration-clause, and not a parenthesized
19933 We first try and parse a parameter-declaration-clause,
19934 and then try a nested declarator (if FIRST is true).
19936 It is not an error for it not to be a
19937 parameter-declaration-clause, even when FIRST is
19943 The first is the declaration of a function while the
19944 second is the definition of a variable, including its
19947 Having seen only the parenthesis, we cannot know which of
19948 these two alternatives should be selected. Even more
19949 complex are examples like:
19954 The former is a function-declaration; the latter is a
19955 variable initialization.
19957 Thus again, we try a parameter-declaration-clause, and if
19958 that fails, we back out and return. */
19960 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19963 bool is_declarator
= false;
19967 /* In a member-declarator, the only valid interpretation
19968 of a parenthesis is the start of a
19969 parameter-declaration-clause. (It is invalid to
19970 initialize a static data member with a parenthesized
19971 initializer; only the "=" form of initialization is
19974 cp_parser_parse_tentatively (parser
);
19976 /* Consume the `('. */
19977 matching_parens parens
;
19978 parens
.consume_open (parser
);
19981 /* If this is going to be an abstract declarator, we're
19982 in a declarator and we can't have default args. */
19983 parser
->default_arg_ok_p
= false;
19984 parser
->in_declarator_p
= true;
19987 begin_scope (sk_function_parms
, NULL_TREE
);
19989 /* Parse the parameter-declaration-clause. */
19990 params
= cp_parser_parameter_declaration_clause (parser
);
19992 /* Consume the `)'. */
19993 parens
.require_close (parser
);
19995 /* If all went well, parse the cv-qualifier-seq,
19996 ref-qualifier and the exception-specification. */
19997 if (member_p
|| cp_parser_parse_definitely (parser
))
19999 cp_cv_quals cv_quals
;
20000 cp_virt_specifiers virt_specifiers
;
20001 cp_ref_qualifier ref_qual
;
20002 tree exception_specification
;
20005 bool memfn
= (member_p
|| (pushed_scope
20006 && CLASS_TYPE_P (pushed_scope
)));
20008 is_declarator
= true;
20010 if (ctor_dtor_or_conv_p
)
20011 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20014 /* Parse the cv-qualifier-seq. */
20015 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20016 /* Parse the ref-qualifier. */
20017 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20018 /* Parse the tx-qualifier. */
20019 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20020 /* And the exception-specification. */
20021 exception_specification
20022 = cp_parser_exception_specification_opt (parser
);
20024 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20026 /* In here, we handle cases where attribute is used after
20027 the function declaration. For example:
20028 void func (int x) __attribute__((vector(..))); */
20029 tree gnu_attrs
= NULL_TREE
;
20030 tree requires_clause
= NULL_TREE
;
20031 late_return
= (cp_parser_late_return_type_opt
20032 (parser
, declarator
, requires_clause
,
20033 memfn
? cv_quals
: -1));
20035 /* Parse the virt-specifier-seq. */
20036 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20038 /* Create the function-declarator. */
20039 declarator
= make_call_declarator (declarator
,
20045 exception_specification
,
20048 declarator
->std_attributes
= attrs
;
20049 declarator
->attributes
= gnu_attrs
;
20050 /* Any subsequent parameter lists are to do with
20051 return type, so are not those of the declared
20053 parser
->default_arg_ok_p
= false;
20056 /* Remove the function parms from scope. */
20057 pop_bindings_and_leave_scope ();
20060 /* Repeat the main loop. */
20064 /* If this is the first, we can try a parenthesized
20068 bool saved_in_type_id_in_expr_p
;
20070 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20071 parser
->in_declarator_p
= saved_in_declarator_p
;
20073 open_paren
= token
;
20074 /* Consume the `('. */
20075 matching_parens parens
;
20076 parens
.consume_open (parser
);
20077 /* Parse the nested declarator. */
20078 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20079 parser
->in_type_id_in_expr_p
= true;
20081 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20082 /*parenthesized_p=*/NULL
,
20083 member_p
, friend_p
);
20084 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20086 /* Expect a `)'. */
20087 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20088 if (!parens
.require_close (parser
))
20089 declarator
= cp_error_declarator
;
20090 if (declarator
== cp_error_declarator
)
20093 goto handle_declarator
;
20095 /* Otherwise, we must be done. */
20099 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20100 && token
->type
== CPP_OPEN_SQUARE
20101 && !cp_next_tokens_can_be_attribute_p (parser
))
20103 /* Parse an array-declarator. */
20104 tree bounds
, attrs
;
20106 if (ctor_dtor_or_conv_p
)
20107 *ctor_dtor_or_conv_p
= 0;
20111 parser
->default_arg_ok_p
= false;
20112 parser
->in_declarator_p
= true;
20113 /* Consume the `['. */
20114 cp_lexer_consume_token (parser
->lexer
);
20115 /* Peek at the next token. */
20116 token
= cp_lexer_peek_token (parser
->lexer
);
20117 /* If the next token is `]', then there is no
20118 constant-expression. */
20119 if (token
->type
!= CPP_CLOSE_SQUARE
)
20121 bool non_constant_p
;
20123 = cp_parser_constant_expression (parser
,
20124 /*allow_non_constant=*/true,
20126 if (!non_constant_p
)
20128 else if (error_operand_p (bounds
))
20129 /* Already gave an error. */;
20130 else if (!parser
->in_function_body
20131 || current_binding_level
->kind
== sk_function_parms
)
20133 /* Normally, the array bound must be an integral constant
20134 expression. However, as an extension, we allow VLAs
20135 in function scopes as long as they aren't part of a
20136 parameter declaration. */
20137 cp_parser_error (parser
,
20138 "array bound is not an integer constant");
20139 bounds
= error_mark_node
;
20141 else if (processing_template_decl
20142 && !type_dependent_expression_p (bounds
))
20144 /* Remember this wasn't a constant-expression. */
20145 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20146 TREE_SIDE_EFFECTS (bounds
) = 1;
20150 bounds
= NULL_TREE
;
20151 /* Look for the closing `]'. */
20152 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20154 declarator
= cp_error_declarator
;
20158 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20159 declarator
= make_array_declarator (declarator
, bounds
);
20160 declarator
->std_attributes
= attrs
;
20162 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20165 tree qualifying_scope
;
20166 tree unqualified_name
;
20168 special_function_kind sfk
;
20170 bool pack_expansion_p
= false;
20171 cp_token
*declarator_id_start_token
;
20173 /* Parse a declarator-id */
20174 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20177 cp_parser_parse_tentatively (parser
);
20179 /* If we see an ellipsis, we should be looking at a
20181 if (token
->type
== CPP_ELLIPSIS
)
20183 /* Consume the `...' */
20184 cp_lexer_consume_token (parser
->lexer
);
20186 pack_expansion_p
= true;
20190 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20192 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20193 qualifying_scope
= parser
->scope
;
20198 if (!unqualified_name
&& pack_expansion_p
)
20200 /* Check whether an error occurred. */
20201 okay
= !cp_parser_error_occurred (parser
);
20203 /* We already consumed the ellipsis to mark a
20204 parameter pack, but we have no way to report it,
20205 so abort the tentative parse. We will be exiting
20206 immediately anyway. */
20207 cp_parser_abort_tentative_parse (parser
);
20210 okay
= cp_parser_parse_definitely (parser
);
20213 unqualified_name
= error_mark_node
;
20214 else if (unqualified_name
20215 && (qualifying_scope
20216 || (!identifier_p (unqualified_name
))))
20218 cp_parser_error (parser
, "expected unqualified-id");
20219 unqualified_name
= error_mark_node
;
20223 if (!unqualified_name
)
20225 if (unqualified_name
== error_mark_node
)
20227 declarator
= cp_error_declarator
;
20228 pack_expansion_p
= false;
20229 declarator
->parameter_pack_p
= false;
20233 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20235 if (qualifying_scope
&& at_namespace_scope_p ()
20236 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20238 /* In the declaration of a member of a template class
20239 outside of the class itself, the SCOPE will sometimes
20240 be a TYPENAME_TYPE. For example, given:
20242 template <typename T>
20243 int S<T>::R::i = 3;
20245 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20246 this context, we must resolve S<T>::R to an ordinary
20247 type, rather than a typename type.
20249 The reason we normally avoid resolving TYPENAME_TYPEs
20250 is that a specialization of `S' might render
20251 `S<T>::R' not a type. However, if `S' is
20252 specialized, then this `i' will not be used, so there
20253 is no harm in resolving the types here. */
20256 /* Resolve the TYPENAME_TYPE. */
20257 type
= resolve_typename_type (qualifying_scope
,
20258 /*only_current_p=*/false);
20259 /* If that failed, the declarator is invalid. */
20260 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20262 if (typedef_variant_p (type
))
20263 error_at (declarator_id_start_token
->location
,
20264 "cannot define member of dependent typedef "
20267 error_at (declarator_id_start_token
->location
,
20268 "%<%T::%E%> is not a type",
20269 TYPE_CONTEXT (qualifying_scope
),
20270 TYPE_IDENTIFIER (qualifying_scope
));
20272 qualifying_scope
= type
;
20277 if (unqualified_name
)
20281 if (qualifying_scope
20282 && CLASS_TYPE_P (qualifying_scope
))
20283 class_type
= qualifying_scope
;
20285 class_type
= current_class_type
;
20287 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20289 tree name_type
= TREE_TYPE (unqualified_name
);
20291 if (!class_type
|| !same_type_p (name_type
, class_type
))
20293 /* We do not attempt to print the declarator
20294 here because we do not have enough
20295 information about its original syntactic
20297 cp_parser_error (parser
, "invalid declarator");
20298 declarator
= cp_error_declarator
;
20301 else if (qualifying_scope
20302 && CLASSTYPE_USE_TEMPLATE (name_type
))
20304 error_at (declarator_id_start_token
->location
,
20305 "invalid use of constructor as a template");
20306 inform (declarator_id_start_token
->location
,
20307 "use %<%T::%D%> instead of %<%T::%D%> to "
20308 "name the constructor in a qualified name",
20310 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20311 class_type
, name_type
);
20312 declarator
= cp_error_declarator
;
20315 unqualified_name
= constructor_name (class_type
);
20320 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20321 sfk
= sfk_destructor
;
20322 else if (identifier_p (unqualified_name
)
20323 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20324 sfk
= sfk_conversion
;
20325 else if (/* There's no way to declare a constructor
20326 for an unnamed type, even if the type
20327 got a name for linkage purposes. */
20328 !TYPE_WAS_UNNAMED (class_type
)
20329 /* Handle correctly (c++/19200):
20343 friend void N::S();
20345 && (!friend_p
|| class_type
== qualifying_scope
)
20346 && constructor_name_p (unqualified_name
,
20348 sfk
= sfk_constructor
;
20349 else if (is_overloaded_fn (unqualified_name
)
20350 && DECL_CONSTRUCTOR_P (get_first_fn
20351 (unqualified_name
)))
20352 sfk
= sfk_constructor
;
20354 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20355 *ctor_dtor_or_conv_p
= -1;
20358 declarator
= make_id_declarator (qualifying_scope
,
20361 declarator
->std_attributes
= attrs
;
20362 declarator
->id_loc
= token
->location
;
20363 declarator
->parameter_pack_p
= pack_expansion_p
;
20365 if (pack_expansion_p
)
20366 maybe_warn_variadic_templates ();
20369 handle_declarator
:;
20370 scope
= get_scope_of_declarator (declarator
);
20373 /* Any names that appear after the declarator-id for a
20374 member are looked up in the containing scope. */
20375 if (at_function_scope_p ())
20377 /* But declarations with qualified-ids can't appear in a
20379 cp_parser_error (parser
, "qualified-id in declaration");
20380 declarator
= cp_error_declarator
;
20383 pushed_scope
= push_scope (scope
);
20385 parser
->in_declarator_p
= true;
20386 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20387 || (declarator
&& declarator
->kind
== cdk_id
))
20388 /* Default args are only allowed on function
20390 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20392 parser
->default_arg_ok_p
= false;
20401 /* For an abstract declarator, we might wind up with nothing at this
20402 point. That's an error; the declarator is not optional. */
20404 cp_parser_error (parser
, "expected declarator");
20405 else if (open_paren
)
20407 /* Record overly parenthesized declarator so we can give a
20408 diagnostic about confusing decl/expr disambiguation. */
20409 if (declarator
->kind
== cdk_array
)
20411 /* If the open and close parens are on different lines, this
20412 is probably a formatting thing, so ignore. */
20413 expanded_location open
= expand_location (open_paren
->location
);
20414 expanded_location close
= expand_location (close_paren
->location
);
20415 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20419 declarator
->parenthesized
= open_paren
->location
;
20422 /* If we entered a scope, we must exit it now. */
20424 pop_scope (pushed_scope
);
20426 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20427 parser
->in_declarator_p
= saved_in_declarator_p
;
20432 /* Parse a ptr-operator.
20435 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20436 * cv-qualifier-seq [opt]
20438 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20439 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20444 & cv-qualifier-seq [opt]
20446 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20447 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20448 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20449 filled in with the TYPE containing the member. *CV_QUALS is
20450 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20451 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20452 Note that the tree codes returned by this function have nothing
20453 to do with the types of trees that will be eventually be created
20454 to represent the pointer or reference type being parsed. They are
20455 just constants with suggestive names. */
20456 static enum tree_code
20457 cp_parser_ptr_operator (cp_parser
* parser
,
20459 cp_cv_quals
*cv_quals
,
20462 enum tree_code code
= ERROR_MARK
;
20464 tree attrs
= NULL_TREE
;
20466 /* Assume that it's not a pointer-to-member. */
20468 /* And that there are no cv-qualifiers. */
20469 *cv_quals
= TYPE_UNQUALIFIED
;
20471 /* Peek at the next token. */
20472 token
= cp_lexer_peek_token (parser
->lexer
);
20474 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20475 if (token
->type
== CPP_MULT
)
20476 code
= INDIRECT_REF
;
20477 else if (token
->type
== CPP_AND
)
20479 else if ((cxx_dialect
!= cxx98
) &&
20480 token
->type
== CPP_AND_AND
) /* C++0x only */
20481 code
= NON_LVALUE_EXPR
;
20483 if (code
!= ERROR_MARK
)
20485 /* Consume the `*', `&' or `&&'. */
20486 cp_lexer_consume_token (parser
->lexer
);
20488 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20489 `&', if we are allowing GNU extensions. (The only qualifier
20490 that can legally appear after `&' is `restrict', but that is
20491 enforced during semantic analysis. */
20492 if (code
== INDIRECT_REF
20493 || cp_parser_allow_gnu_extensions_p (parser
))
20494 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20496 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20497 if (attributes
!= NULL
)
20498 *attributes
= attrs
;
20502 /* Try the pointer-to-member case. */
20503 cp_parser_parse_tentatively (parser
);
20504 /* Look for the optional `::' operator. */
20505 cp_parser_global_scope_opt (parser
,
20506 /*current_scope_valid_p=*/false);
20507 /* Look for the nested-name specifier. */
20508 token
= cp_lexer_peek_token (parser
->lexer
);
20509 cp_parser_nested_name_specifier (parser
,
20510 /*typename_keyword_p=*/false,
20511 /*check_dependency_p=*/true,
20513 /*is_declaration=*/false);
20514 /* If we found it, and the next token is a `*', then we are
20515 indeed looking at a pointer-to-member operator. */
20516 if (!cp_parser_error_occurred (parser
)
20517 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20519 /* Indicate that the `*' operator was used. */
20520 code
= INDIRECT_REF
;
20522 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20523 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20524 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20525 error_at (token
->location
, "cannot form pointer to member of "
20526 "non-class %q#T", parser
->scope
);
20529 /* The type of which the member is a member is given by the
20531 *type
= parser
->scope
;
20532 /* The next name will not be qualified. */
20533 parser
->scope
= NULL_TREE
;
20534 parser
->qualifying_scope
= NULL_TREE
;
20535 parser
->object_scope
= NULL_TREE
;
20536 /* Look for optional c++11 attributes. */
20537 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20538 if (attributes
!= NULL
)
20539 *attributes
= attrs
;
20540 /* Look for the optional cv-qualifier-seq. */
20541 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20544 /* If that didn't work we don't have a ptr-operator. */
20545 if (!cp_parser_parse_definitely (parser
))
20546 cp_parser_error (parser
, "expected ptr-operator");
20552 /* Parse an (optional) cv-qualifier-seq.
20555 cv-qualifier cv-qualifier-seq [opt]
20566 Returns a bitmask representing the cv-qualifiers. */
20569 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20571 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20576 cp_cv_quals cv_qualifier
;
20578 /* Peek at the next token. */
20579 token
= cp_lexer_peek_token (parser
->lexer
);
20580 /* See if it's a cv-qualifier. */
20581 switch (token
->keyword
)
20584 cv_qualifier
= TYPE_QUAL_CONST
;
20588 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20592 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20596 cv_qualifier
= TYPE_UNQUALIFIED
;
20603 if (cv_quals
& cv_qualifier
)
20605 gcc_rich_location
richloc (token
->location
);
20606 richloc
.add_fixit_remove ();
20607 error_at (&richloc
, "duplicate cv-qualifier");
20608 cp_lexer_purge_token (parser
->lexer
);
20612 cp_lexer_consume_token (parser
->lexer
);
20613 cv_quals
|= cv_qualifier
;
20620 /* Parse an (optional) ref-qualifier
20626 Returns cp_ref_qualifier representing ref-qualifier. */
20628 static cp_ref_qualifier
20629 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20631 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20633 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20634 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20639 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20640 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20642 switch (token
->type
)
20645 curr_ref_qual
= REF_QUAL_LVALUE
;
20649 curr_ref_qual
= REF_QUAL_RVALUE
;
20653 curr_ref_qual
= REF_QUAL_NONE
;
20657 if (!curr_ref_qual
)
20661 error_at (token
->location
, "multiple ref-qualifiers");
20662 cp_lexer_purge_token (parser
->lexer
);
20666 ref_qual
= curr_ref_qual
;
20667 cp_lexer_consume_token (parser
->lexer
);
20674 /* Parse an optional tx-qualifier.
20678 transaction_safe_dynamic */
20681 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20683 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20684 if (token
->type
== CPP_NAME
)
20686 tree name
= token
->u
.value
;
20687 const char *p
= IDENTIFIER_POINTER (name
);
20688 const int len
= strlen ("transaction_safe");
20689 if (!strncmp (p
, "transaction_safe", len
))
20693 || !strcmp (p
, "_dynamic"))
20695 cp_lexer_consume_token (parser
->lexer
);
20698 error ("%qE requires %<-fgnu-tm%>", name
);
20709 /* Parse an (optional) virt-specifier-seq.
20711 virt-specifier-seq:
20712 virt-specifier virt-specifier-seq [opt]
20718 Returns a bitmask representing the virt-specifiers. */
20720 static cp_virt_specifiers
20721 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20723 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20728 cp_virt_specifiers virt_specifier
;
20730 /* Peek at the next token. */
20731 token
= cp_lexer_peek_token (parser
->lexer
);
20732 /* See if it's a virt-specifier-qualifier. */
20733 if (token
->type
!= CPP_NAME
)
20735 if (id_equal (token
->u
.value
, "override"))
20737 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20738 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20740 else if (id_equal (token
->u
.value
, "final"))
20742 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20743 virt_specifier
= VIRT_SPEC_FINAL
;
20745 else if (id_equal (token
->u
.value
, "__final"))
20747 virt_specifier
= VIRT_SPEC_FINAL
;
20752 if (virt_specifiers
& virt_specifier
)
20754 gcc_rich_location
richloc (token
->location
);
20755 richloc
.add_fixit_remove ();
20756 error_at (&richloc
, "duplicate virt-specifier");
20757 cp_lexer_purge_token (parser
->lexer
);
20761 cp_lexer_consume_token (parser
->lexer
);
20762 virt_specifiers
|= virt_specifier
;
20765 return virt_specifiers
;
20768 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20769 is in scope even though it isn't real. */
20772 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20776 if (current_class_ptr
)
20778 /* We don't clear this between NSDMIs. Is it already what we want? */
20779 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20780 if (DECL_P (current_class_ptr
)
20781 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20782 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20783 && cp_type_quals (type
) == quals
)
20787 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20788 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20789 current_class_ptr
= NULL_TREE
;
20791 = cp_build_fold_indirect_ref (this_parm
);
20792 current_class_ptr
= this_parm
;
20795 /* Return true iff our current scope is a non-static data member
20799 parsing_nsdmi (void)
20801 /* We recognize NSDMI context by the context-less 'this' pointer set up
20802 by the function above. */
20803 if (current_class_ptr
20804 && TREE_CODE (current_class_ptr
) == PARM_DECL
20805 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20810 /* Parse a late-specified return type, if any. This is not a separate
20811 non-terminal, but part of a function declarator, which looks like
20813 -> trailing-type-specifier-seq abstract-declarator(opt)
20815 Returns the type indicated by the type-id.
20817 In addition to this, parse any queued up #pragma omp declare simd
20818 clauses, and #pragma acc routine clauses.
20820 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20824 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20825 tree
& requires_clause
, cp_cv_quals quals
)
20828 tree type
= NULL_TREE
;
20829 bool declare_simd_p
= (parser
->omp_declare_simd
20831 && declarator
->kind
== cdk_id
);
20833 bool oacc_routine_p
= (parser
->oacc_routine
20835 && declarator
->kind
== cdk_id
);
20837 /* Peek at the next token. */
20838 token
= cp_lexer_peek_token (parser
->lexer
);
20839 /* A late-specified return type is indicated by an initial '->'. */
20840 if (token
->type
!= CPP_DEREF
20841 && token
->keyword
!= RID_REQUIRES
20842 && !(token
->type
== CPP_NAME
20843 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20844 && !(declare_simd_p
|| oacc_routine_p
))
20847 tree save_ccp
= current_class_ptr
;
20848 tree save_ccr
= current_class_ref
;
20851 /* DR 1207: 'this' is in scope in the trailing return type. */
20852 inject_this_parameter (current_class_type
, quals
);
20855 if (token
->type
== CPP_DEREF
)
20857 /* Consume the ->. */
20858 cp_lexer_consume_token (parser
->lexer
);
20860 type
= cp_parser_trailing_type_id (parser
);
20863 /* Function declarations may be followed by a trailing
20864 requires-clause. */
20865 requires_clause
= cp_parser_requires_clause_opt (parser
);
20867 if (declare_simd_p
)
20868 declarator
->attributes
20869 = cp_parser_late_parsing_omp_declare_simd (parser
,
20870 declarator
->attributes
);
20871 if (oacc_routine_p
)
20872 declarator
->attributes
20873 = cp_parser_late_parsing_oacc_routine (parser
,
20874 declarator
->attributes
);
20878 current_class_ptr
= save_ccp
;
20879 current_class_ref
= save_ccr
;
20885 /* Parse a declarator-id.
20889 :: [opt] nested-name-specifier [opt] type-name
20891 In the `id-expression' case, the value returned is as for
20892 cp_parser_id_expression if the id-expression was an unqualified-id.
20893 If the id-expression was a qualified-id, then a SCOPE_REF is
20894 returned. The first operand is the scope (either a NAMESPACE_DECL
20895 or TREE_TYPE), but the second is still just a representation of an
20899 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20902 /* The expression must be an id-expression. Assume that qualified
20903 names are the names of types so that:
20906 int S<T>::R::i = 3;
20908 will work; we must treat `S<T>::R' as the name of a type.
20909 Similarly, assume that qualified names are templates, where
20913 int S<T>::R<T>::i = 3;
20916 id
= cp_parser_id_expression (parser
,
20917 /*template_keyword_p=*/false,
20918 /*check_dependency_p=*/false,
20919 /*template_p=*/NULL
,
20920 /*declarator_p=*/true,
20922 if (id
&& BASELINK_P (id
))
20923 id
= BASELINK_FUNCTIONS (id
);
20927 /* Parse a type-id.
20930 type-specifier-seq abstract-declarator [opt]
20932 Returns the TYPE specified. */
20935 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20936 bool is_trailing_return
)
20938 cp_decl_specifier_seq type_specifier_seq
;
20939 cp_declarator
*abstract_declarator
;
20941 /* Parse the type-specifier-seq. */
20942 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20943 is_trailing_return
,
20944 &type_specifier_seq
);
20945 if (is_template_arg
&& type_specifier_seq
.type
20946 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20947 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20948 /* A bare template name as a template argument is a template template
20949 argument, not a placeholder, so fail parsing it as a type argument. */
20951 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20952 cp_parser_simulate_error (parser
);
20953 return error_mark_node
;
20955 if (type_specifier_seq
.type
== error_mark_node
)
20956 return error_mark_node
;
20958 /* There might or might not be an abstract declarator. */
20959 cp_parser_parse_tentatively (parser
);
20960 /* Look for the declarator. */
20961 abstract_declarator
20962 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20963 /*parenthesized_p=*/NULL
,
20964 /*member_p=*/false,
20965 /*friend_p=*/false);
20966 /* Check to see if there really was a declarator. */
20967 if (!cp_parser_parse_definitely (parser
))
20968 abstract_declarator
= NULL
;
20970 if (type_specifier_seq
.type
20971 /* The concepts TS allows 'auto' as a type-id. */
20972 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20973 /* None of the valid uses of 'auto' in C++14 involve the type-id
20974 nonterminal, but it is valid in a trailing-return-type. */
20975 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20976 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20978 /* A type-id with type 'auto' is only ok if the abstract declarator
20979 is a function declarator with a late-specified return type.
20981 A type-id with 'auto' is also valid in a trailing-return-type
20982 in a compound-requirement. */
20983 if (abstract_declarator
20984 && abstract_declarator
->kind
== cdk_function
20985 && abstract_declarator
->u
.function
.late_return_type
)
20987 else if (parser
->in_result_type_constraint_p
)
20991 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20992 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20994 error_at (loc
, "missing template arguments after %qT",
20996 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21000 error_at (loc
, "invalid use of %qT", auto_node
);
21001 return error_mark_node
;
21005 return groktypename (&type_specifier_seq
, abstract_declarator
,
21010 cp_parser_type_id (cp_parser
*parser
)
21012 return cp_parser_type_id_1 (parser
, false, false);
21016 cp_parser_template_type_arg (cp_parser
*parser
)
21019 const char *saved_message
= parser
->type_definition_forbidden_message
;
21020 parser
->type_definition_forbidden_message
21021 = G_("types may not be defined in template arguments");
21022 r
= cp_parser_type_id_1 (parser
, true, false);
21023 parser
->type_definition_forbidden_message
= saved_message
;
21024 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21026 error ("invalid use of %<auto%> in template argument");
21027 r
= error_mark_node
;
21033 cp_parser_trailing_type_id (cp_parser
*parser
)
21035 return cp_parser_type_id_1 (parser
, false, true);
21038 /* Parse a type-specifier-seq.
21040 type-specifier-seq:
21041 type-specifier type-specifier-seq [opt]
21045 type-specifier-seq:
21046 attributes type-specifier-seq [opt]
21048 If IS_DECLARATION is true, we are at the start of a "condition" or
21049 exception-declaration, so we might be followed by a declarator-id.
21051 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21052 i.e. we've just seen "->".
21054 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21057 cp_parser_type_specifier_seq (cp_parser
* parser
,
21058 bool is_declaration
,
21059 bool is_trailing_return
,
21060 cp_decl_specifier_seq
*type_specifier_seq
)
21062 bool seen_type_specifier
= false;
21063 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21064 cp_token
*start_token
= NULL
;
21066 /* Clear the TYPE_SPECIFIER_SEQ. */
21067 clear_decl_specs (type_specifier_seq
);
21069 /* In the context of a trailing return type, enum E { } is an
21070 elaborated-type-specifier followed by a function-body, not an
21072 if (is_trailing_return
)
21073 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21075 /* Parse the type-specifiers and attributes. */
21078 tree type_specifier
;
21079 bool is_cv_qualifier
;
21081 /* Check for attributes first. */
21082 if (cp_next_tokens_can_be_attribute_p (parser
))
21084 type_specifier_seq
->attributes
21085 = attr_chainon (type_specifier_seq
->attributes
,
21086 cp_parser_attributes_opt (parser
));
21090 /* record the token of the beginning of the type specifier seq,
21091 for error reporting purposes*/
21093 start_token
= cp_lexer_peek_token (parser
->lexer
);
21095 /* Look for the type-specifier. */
21096 type_specifier
= cp_parser_type_specifier (parser
,
21098 type_specifier_seq
,
21099 /*is_declaration=*/false,
21102 if (!type_specifier
)
21104 /* If the first type-specifier could not be found, this is not a
21105 type-specifier-seq at all. */
21106 if (!seen_type_specifier
)
21108 /* Set in_declarator_p to avoid skipping to the semicolon. */
21109 int in_decl
= parser
->in_declarator_p
;
21110 parser
->in_declarator_p
= true;
21112 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21113 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21114 cp_parser_error (parser
, "expected type-specifier");
21116 parser
->in_declarator_p
= in_decl
;
21118 type_specifier_seq
->type
= error_mark_node
;
21121 /* If subsequent type-specifiers could not be found, the
21122 type-specifier-seq is complete. */
21126 seen_type_specifier
= true;
21127 /* The standard says that a condition can be:
21129 type-specifier-seq declarator = assignment-expression
21136 we should treat the "S" as a declarator, not as a
21137 type-specifier. The standard doesn't say that explicitly for
21138 type-specifier-seq, but it does say that for
21139 decl-specifier-seq in an ordinary declaration. Perhaps it
21140 would be clearer just to allow a decl-specifier-seq here, and
21141 then add a semantic restriction that if any decl-specifiers
21142 that are not type-specifiers appear, the program is invalid. */
21143 if (is_declaration
&& !is_cv_qualifier
)
21144 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21148 /* Return whether the function currently being declared has an associated
21149 template parameter list. */
21152 function_being_declared_is_template_p (cp_parser
* parser
)
21154 if (!current_template_parms
|| processing_template_parmlist
)
21157 if (parser
->implicit_template_scope
)
21160 if (at_class_scope_p ()
21161 && TYPE_BEING_DEFINED (current_class_type
))
21162 return parser
->num_template_parameter_lists
!= 0;
21164 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21165 (current_class_type
));
21168 /* Parse a parameter-declaration-clause.
21170 parameter-declaration-clause:
21171 parameter-declaration-list [opt] ... [opt]
21172 parameter-declaration-list , ...
21174 Returns a representation for the parameter declarations. A return
21175 value of NULL indicates a parameter-declaration-clause consisting
21176 only of an ellipsis. */
21179 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21188 int auto_is_implicit_function_template_parm_p
;
21190 parser
->auto_is_implicit_function_template_parm_p
21191 = auto_is_implicit_function_template_parm_p
;
21193 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
21197 if (!processing_specialization
21198 && !processing_template_parmlist
21199 && !processing_explicit_instantiation
)
21200 if (!current_function_decl
21201 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21202 parser
->auto_is_implicit_function_template_parm_p
= true;
21204 /* Peek at the next token. */
21205 token
= cp_lexer_peek_token (parser
->lexer
);
21206 /* Check for trivial parameter-declaration-clauses. */
21207 if (token
->type
== CPP_ELLIPSIS
)
21209 /* Consume the `...' token. */
21210 cp_lexer_consume_token (parser
->lexer
);
21213 else if (token
->type
== CPP_CLOSE_PAREN
)
21214 /* There are no parameters. */
21216 #ifndef NO_IMPLICIT_EXTERN_C
21217 if (in_system_header_at (input_location
)
21218 && current_class_type
== NULL
21219 && current_lang_name
== lang_name_c
)
21223 return void_list_node
;
21225 /* Check for `(void)', too, which is a special case. */
21226 else if (token
->keyword
== RID_VOID
21227 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21228 == CPP_CLOSE_PAREN
))
21230 /* Consume the `void' token. */
21231 cp_lexer_consume_token (parser
->lexer
);
21232 /* There are no parameters. */
21233 return void_list_node
;
21236 /* Parse the parameter-declaration-list. */
21237 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21238 /* If a parse error occurred while parsing the
21239 parameter-declaration-list, then the entire
21240 parameter-declaration-clause is erroneous. */
21244 /* Peek at the next token. */
21245 token
= cp_lexer_peek_token (parser
->lexer
);
21246 /* If it's a `,', the clause should terminate with an ellipsis. */
21247 if (token
->type
== CPP_COMMA
)
21249 /* Consume the `,'. */
21250 cp_lexer_consume_token (parser
->lexer
);
21251 /* Expect an ellipsis. */
21253 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21255 /* It might also be `...' if the optional trailing `,' was
21257 else if (token
->type
== CPP_ELLIPSIS
)
21259 /* Consume the `...' token. */
21260 cp_lexer_consume_token (parser
->lexer
);
21261 /* And remember that we saw it. */
21265 ellipsis_p
= false;
21267 /* Finish the parameter list. */
21269 parameters
= chainon (parameters
, void_list_node
);
21274 /* Parse a parameter-declaration-list.
21276 parameter-declaration-list:
21277 parameter-declaration
21278 parameter-declaration-list , parameter-declaration
21280 Returns a representation of the parameter-declaration-list, as for
21281 cp_parser_parameter_declaration_clause. However, the
21282 `void_list_node' is never appended to the list. Upon return,
21283 *IS_ERROR will be true iff an error occurred. */
21286 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21288 tree parameters
= NULL_TREE
;
21289 tree
*tail
= ¶meters
;
21290 bool saved_in_unbraced_linkage_specification_p
;
21293 /* Assume all will go well. */
21295 /* The special considerations that apply to a function within an
21296 unbraced linkage specifications do not apply to the parameters
21297 to the function. */
21298 saved_in_unbraced_linkage_specification_p
21299 = parser
->in_unbraced_linkage_specification_p
;
21300 parser
->in_unbraced_linkage_specification_p
= false;
21302 /* Look for more parameters. */
21305 cp_parameter_declarator
*parameter
;
21306 tree decl
= error_mark_node
;
21307 bool parenthesized_p
= false;
21308 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21309 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21310 (current_template_parms
)) : 0);
21312 /* Parse the parameter. */
21314 = cp_parser_parameter_declaration (parser
,
21315 /*template_parm_p=*/false,
21318 /* We don't know yet if the enclosing context is deprecated, so wait
21319 and warn in grokparms if appropriate. */
21320 deprecated_state
= DEPRECATED_SUPPRESS
;
21324 /* If a function parameter pack was specified and an implicit template
21325 parameter was introduced during cp_parser_parameter_declaration,
21326 change any implicit parameters introduced into packs. */
21327 if (parser
->implicit_template_parms
21328 && parameter
->declarator
21329 && parameter
->declarator
->parameter_pack_p
)
21331 int latest_template_parm_idx
= TREE_VEC_LENGTH
21332 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21334 if (latest_template_parm_idx
!= template_parm_idx
)
21335 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21336 (parameter
->decl_specifiers
.type
,
21337 template_parm_idx
, latest_template_parm_idx
);
21340 decl
= grokdeclarator (parameter
->declarator
,
21341 ¶meter
->decl_specifiers
,
21343 parameter
->default_argument
!= NULL_TREE
,
21344 ¶meter
->decl_specifiers
.attributes
);
21345 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21346 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21349 deprecated_state
= DEPRECATED_NORMAL
;
21351 /* If a parse error occurred parsing the parameter declaration,
21352 then the entire parameter-declaration-list is erroneous. */
21353 if (decl
== error_mark_node
)
21356 parameters
= error_mark_node
;
21360 if (parameter
->decl_specifiers
.attributes
)
21361 cplus_decl_attributes (&decl
,
21362 parameter
->decl_specifiers
.attributes
,
21364 if (DECL_NAME (decl
))
21365 decl
= pushdecl (decl
);
21367 if (decl
!= error_mark_node
)
21369 retrofit_lang_decl (decl
);
21370 DECL_PARM_INDEX (decl
) = ++index
;
21371 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21374 /* Add the new parameter to the list. */
21375 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21376 tail
= &TREE_CHAIN (*tail
);
21378 /* Peek at the next token. */
21379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21380 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21381 /* These are for Objective-C++ */
21382 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21383 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21384 /* The parameter-declaration-list is complete. */
21386 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21390 /* Peek at the next token. */
21391 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21392 /* If it's an ellipsis, then the list is complete. */
21393 if (token
->type
== CPP_ELLIPSIS
)
21395 /* Otherwise, there must be more parameters. Consume the
21397 cp_lexer_consume_token (parser
->lexer
);
21398 /* When parsing something like:
21400 int i(float f, double d)
21402 we can tell after seeing the declaration for "f" that we
21403 are not looking at an initialization of a variable "i",
21404 but rather at the declaration of a function "i".
21406 Due to the fact that the parsing of template arguments
21407 (as specified to a template-id) requires backtracking we
21408 cannot use this technique when inside a template argument
21410 if (!parser
->in_template_argument_list_p
21411 && !parser
->in_type_id_in_expr_p
21412 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21413 /* However, a parameter-declaration of the form
21414 "float(f)" (which is a valid declaration of a
21415 parameter "f") can also be interpreted as an
21416 expression (the conversion of "f" to "float"). */
21417 && !parenthesized_p
)
21418 cp_parser_commit_to_tentative_parse (parser
);
21422 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21423 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21424 cp_parser_skip_to_closing_parenthesis (parser
,
21425 /*recovering=*/true,
21426 /*or_comma=*/false,
21427 /*consume_paren=*/false);
21432 parser
->in_unbraced_linkage_specification_p
21433 = saved_in_unbraced_linkage_specification_p
;
21435 /* Reset implicit_template_scope if we are about to leave the function
21436 parameter list that introduced it. Note that for out-of-line member
21437 definitions, there will be one or more class scopes before we get to
21438 the template parameter scope. */
21440 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21441 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21443 while (maybe_its
->kind
== sk_class
)
21444 maybe_its
= maybe_its
->level_chain
;
21445 if (maybe_its
== its
)
21447 parser
->implicit_template_parms
= 0;
21448 parser
->implicit_template_scope
= 0;
21455 /* Parse a parameter declaration.
21457 parameter-declaration:
21458 decl-specifier-seq ... [opt] declarator
21459 decl-specifier-seq declarator = assignment-expression
21460 decl-specifier-seq ... [opt] abstract-declarator [opt]
21461 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21463 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21464 declares a template parameter. (In that case, a non-nested `>'
21465 token encountered during the parsing of the assignment-expression
21466 is not interpreted as a greater-than operator.)
21468 Returns a representation of the parameter, or NULL if an error
21469 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21470 true iff the declarator is of the form "(p)". */
21472 static cp_parameter_declarator
*
21473 cp_parser_parameter_declaration (cp_parser
*parser
,
21474 bool template_parm_p
,
21475 bool *parenthesized_p
)
21477 int declares_class_or_enum
;
21478 cp_decl_specifier_seq decl_specifiers
;
21479 cp_declarator
*declarator
;
21480 tree default_argument
;
21481 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21482 const char *saved_message
;
21483 bool template_parameter_pack_p
= false;
21485 /* In a template parameter, `>' is not an operator.
21489 When parsing a default template-argument for a non-type
21490 template-parameter, the first non-nested `>' is taken as the end
21491 of the template parameter-list rather than a greater-than
21494 /* Type definitions may not appear in parameter types. */
21495 saved_message
= parser
->type_definition_forbidden_message
;
21496 parser
->type_definition_forbidden_message
21497 = G_("types may not be defined in parameter types");
21499 /* Parse the declaration-specifiers. */
21500 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21501 cp_parser_decl_specifier_seq (parser
,
21502 CP_PARSER_FLAGS_NONE
,
21504 &declares_class_or_enum
);
21506 /* Complain about missing 'typename' or other invalid type names. */
21507 if (!decl_specifiers
.any_type_specifiers_p
21508 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21509 decl_specifiers
.type
= error_mark_node
;
21511 /* If an error occurred, there's no reason to attempt to parse the
21512 rest of the declaration. */
21513 if (cp_parser_error_occurred (parser
))
21515 parser
->type_definition_forbidden_message
= saved_message
;
21519 /* Peek at the next token. */
21520 token
= cp_lexer_peek_token (parser
->lexer
);
21522 /* If the next token is a `)', `,', `=', `>', or `...', then there
21523 is no declarator. However, when variadic templates are enabled,
21524 there may be a declarator following `...'. */
21525 if (token
->type
== CPP_CLOSE_PAREN
21526 || token
->type
== CPP_COMMA
21527 || token
->type
== CPP_EQ
21528 || token
->type
== CPP_GREATER
)
21531 if (parenthesized_p
)
21532 *parenthesized_p
= false;
21534 /* Otherwise, there should be a declarator. */
21537 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21538 parser
->default_arg_ok_p
= false;
21540 /* After seeing a decl-specifier-seq, if the next token is not a
21541 "(", there is no possibility that the code is a valid
21542 expression. Therefore, if parsing tentatively, we commit at
21544 if (!parser
->in_template_argument_list_p
21545 /* In an expression context, having seen:
21549 we cannot be sure whether we are looking at a
21550 function-type (taking a "char" as a parameter) or a cast
21551 of some object of type "char" to "int". */
21552 && !parser
->in_type_id_in_expr_p
21553 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21554 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21555 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21556 cp_parser_commit_to_tentative_parse (parser
);
21557 /* Parse the declarator. */
21558 declarator_token_start
= token
;
21559 declarator
= cp_parser_declarator (parser
,
21560 CP_PARSER_DECLARATOR_EITHER
,
21561 /*ctor_dtor_or_conv_p=*/NULL
,
21563 /*member_p=*/false,
21564 /*friend_p=*/false);
21565 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21566 /* After the declarator, allow more attributes. */
21567 decl_specifiers
.attributes
21568 = attr_chainon (decl_specifiers
.attributes
,
21569 cp_parser_attributes_opt (parser
));
21571 /* If the declarator is a template parameter pack, remember that and
21572 clear the flag in the declarator itself so we don't get errors
21573 from grokdeclarator. */
21574 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21576 declarator
->parameter_pack_p
= false;
21577 template_parameter_pack_p
= true;
21581 /* If the next token is an ellipsis, and we have not seen a declarator
21582 name, and if either the type of the declarator contains parameter
21583 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21584 for, eg, abbreviated integral type names), then we actually have a
21585 parameter pack expansion expression. Otherwise, leave the ellipsis
21586 for a C-style variadic function. */
21587 token
= cp_lexer_peek_token (parser
->lexer
);
21588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21590 tree type
= decl_specifiers
.type
;
21592 if (type
&& DECL_P (type
))
21593 type
= TREE_TYPE (type
);
21596 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21597 && (template_parm_p
|| uses_parameter_packs (type
)))
21598 || (!type
&& template_parm_p
))
21599 && declarator_can_be_parameter_pack (declarator
))
21601 /* Consume the `...'. */
21602 cp_lexer_consume_token (parser
->lexer
);
21603 maybe_warn_variadic_templates ();
21605 /* Build a pack expansion type */
21606 if (template_parm_p
)
21607 template_parameter_pack_p
= true;
21608 else if (declarator
)
21609 declarator
->parameter_pack_p
= true;
21611 decl_specifiers
.type
= make_pack_expansion (type
);
21615 /* The restriction on defining new types applies only to the type
21616 of the parameter, not to the default argument. */
21617 parser
->type_definition_forbidden_message
= saved_message
;
21619 /* If the next token is `=', then process a default argument. */
21620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21622 tree type
= decl_specifiers
.type
;
21623 token
= cp_lexer_peek_token (parser
->lexer
);
21624 /* If we are defining a class, then the tokens that make up the
21625 default argument must be saved and processed later. */
21626 if (!template_parm_p
&& at_class_scope_p ()
21627 && TYPE_BEING_DEFINED (current_class_type
)
21628 && !LAMBDA_TYPE_P (current_class_type
))
21629 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21631 // A constrained-type-specifier may declare a type template-parameter.
21632 else if (declares_constrained_type_template_parameter (type
))
21634 = cp_parser_default_type_template_argument (parser
);
21636 // A constrained-type-specifier may declare a template-template-parameter.
21637 else if (declares_constrained_template_template_parameter (type
))
21639 = cp_parser_default_template_template_argument (parser
);
21641 /* Outside of a class definition, we can just parse the
21642 assignment-expression. */
21645 = cp_parser_default_argument (parser
, template_parm_p
);
21647 if (!parser
->default_arg_ok_p
)
21649 permerror (token
->location
,
21650 "default arguments are only "
21651 "permitted for function parameters");
21653 else if ((declarator
&& declarator
->parameter_pack_p
)
21654 || template_parameter_pack_p
21655 || (decl_specifiers
.type
21656 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21658 /* Find the name of the parameter pack. */
21659 cp_declarator
*id_declarator
= declarator
;
21660 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21661 id_declarator
= id_declarator
->declarator
;
21663 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21664 error_at (declarator_token_start
->location
,
21666 ? G_("template parameter pack %qD "
21667 "cannot have a default argument")
21668 : G_("parameter pack %qD cannot have "
21669 "a default argument"),
21670 id_declarator
->u
.id
.unqualified_name
);
21672 error_at (declarator_token_start
->location
,
21674 ? G_("template parameter pack cannot have "
21675 "a default argument")
21676 : G_("parameter pack cannot have a "
21677 "default argument"));
21679 default_argument
= NULL_TREE
;
21683 default_argument
= NULL_TREE
;
21685 /* Generate a location for the parameter, ranging from the start of the
21686 initial token to the end of the final token (using input_location for
21687 the latter, set up by cp_lexer_set_source_position_from_token when
21690 If we have a identifier, then use it for the caret location, e.g.
21692 extern int callee (int one, int (*two)(int, int), float three);
21693 ~~~~~~^~~~~~~~~~~~~~
21695 otherwise, reuse the start location for the caret location e.g.:
21697 extern int callee (int one, int (*)(int, int), float three);
21701 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21702 ? declarator
->id_loc
21703 : decl_spec_token_start
->location
);
21704 location_t param_loc
= make_location (caret_loc
,
21705 decl_spec_token_start
->location
,
21708 return make_parameter_declarator (&decl_specifiers
,
21712 template_parameter_pack_p
);
21715 /* Parse a default argument and return it.
21717 TEMPLATE_PARM_P is true if this is a default argument for a
21718 non-type template parameter. */
21720 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21722 tree default_argument
= NULL_TREE
;
21723 bool saved_greater_than_is_operator_p
;
21724 bool saved_local_variables_forbidden_p
;
21725 bool non_constant_p
, is_direct_init
;
21727 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21729 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21730 parser
->greater_than_is_operator_p
= !template_parm_p
;
21731 /* Local variable names (and the `this' keyword) may not
21732 appear in a default argument. */
21733 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21734 parser
->local_variables_forbidden_p
= true;
21735 /* Parse the assignment-expression. */
21736 if (template_parm_p
)
21737 push_deferring_access_checks (dk_no_deferred
);
21738 tree saved_class_ptr
= NULL_TREE
;
21739 tree saved_class_ref
= NULL_TREE
;
21740 /* The "this" pointer is not valid in a default argument. */
21743 saved_class_ptr
= current_class_ptr
;
21744 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21745 saved_class_ref
= current_class_ref
;
21746 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21749 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21750 /* Restore the "this" pointer. */
21753 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21754 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21756 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21758 if (template_parm_p
)
21759 pop_deferring_access_checks ();
21760 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21761 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21763 return default_argument
;
21766 /* Parse a function-body.
21769 compound_statement */
21772 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21774 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21775 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21779 /* Parse a ctor-initializer-opt followed by a function-body. Return
21780 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21781 is true we are parsing a function-try-block. */
21784 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21785 bool in_function_try_block
)
21788 const bool check_body_p
=
21789 DECL_CONSTRUCTOR_P (current_function_decl
)
21790 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21793 /* Begin the function body. */
21794 body
= begin_function_body ();
21795 /* Parse the optional ctor-initializer. */
21796 cp_parser_ctor_initializer_opt (parser
);
21798 /* If we're parsing a constexpr constructor definition, we need
21799 to check that the constructor body is indeed empty. However,
21800 before we get to cp_parser_function_body lot of junk has been
21801 generated, so we can't just check that we have an empty block.
21802 Rather we take a snapshot of the outermost block, and check whether
21803 cp_parser_function_body changed its state. */
21806 list
= cur_stmt_list
;
21807 if (STATEMENT_LIST_TAIL (list
))
21808 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21810 /* Parse the function-body. */
21811 cp_parser_function_body (parser
, in_function_try_block
);
21813 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21814 /* Finish the function body. */
21815 finish_function_body (body
);
21818 /* Parse an initializer.
21821 = initializer-clause
21822 ( expression-list )
21824 Returns an expression representing the initializer. If no
21825 initializer is present, NULL_TREE is returned.
21827 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21828 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21829 set to TRUE if there is no initializer present. If there is an
21830 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21831 is set to true; otherwise it is set to false. */
21834 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21835 bool* non_constant_p
)
21840 /* Peek at the next token. */
21841 token
= cp_lexer_peek_token (parser
->lexer
);
21843 /* Let our caller know whether or not this initializer was
21845 *is_direct_init
= (token
->type
!= CPP_EQ
);
21846 /* Assume that the initializer is constant. */
21847 *non_constant_p
= false;
21849 if (token
->type
== CPP_EQ
)
21851 /* Consume the `='. */
21852 cp_lexer_consume_token (parser
->lexer
);
21853 /* Parse the initializer-clause. */
21854 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21856 else if (token
->type
== CPP_OPEN_PAREN
)
21858 vec
<tree
, va_gc
> *vec
;
21859 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21861 /*allow_expansion_p=*/true,
21864 return error_mark_node
;
21865 init
= build_tree_list_vec (vec
);
21866 release_tree_vector (vec
);
21868 else if (token
->type
== CPP_OPEN_BRACE
)
21870 cp_lexer_set_source_position (parser
->lexer
);
21871 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21872 init
= cp_parser_braced_list (parser
, non_constant_p
);
21873 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21877 /* Anything else is an error. */
21878 cp_parser_error (parser
, "expected initializer");
21879 init
= error_mark_node
;
21882 if (check_for_bare_parameter_packs (init
))
21883 init
= error_mark_node
;
21888 /* Parse an initializer-clause.
21890 initializer-clause:
21891 assignment-expression
21894 Returns an expression representing the initializer.
21896 If the `assignment-expression' production is used the value
21897 returned is simply a representation for the expression.
21899 Otherwise, calls cp_parser_braced_list. */
21902 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21904 cp_expr initializer
;
21906 /* Assume the expression is constant. */
21907 *non_constant_p
= false;
21909 /* If it is not a `{', then we are looking at an
21910 assignment-expression. */
21911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21914 = cp_parser_constant_expression (parser
,
21915 /*allow_non_constant_p=*/true,
21919 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21921 return initializer
;
21924 /* Parse a brace-enclosed initializer list.
21927 { initializer-list , [opt] }
21928 { designated-initializer-list , [opt] }
21931 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21932 the elements of the initializer-list (or NULL, if the last
21933 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21934 NULL_TREE. There is no way to detect whether or not the optional
21935 trailing `,' was provided. NON_CONSTANT_P is as for
21936 cp_parser_initializer. */
21939 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21942 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21944 /* Consume the `{' token. */
21945 matching_braces braces
;
21946 braces
.require_open (parser
);
21947 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21948 initializer
= make_node (CONSTRUCTOR
);
21949 /* If it's not a `}', then there is a non-trivial initializer. */
21950 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21952 /* Parse the initializer list. */
21953 CONSTRUCTOR_ELTS (initializer
)
21954 = cp_parser_initializer_list (parser
, non_constant_p
);
21955 /* A trailing `,' token is allowed. */
21956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21957 cp_lexer_consume_token (parser
->lexer
);
21960 *non_constant_p
= false;
21961 /* Now, there should be a trailing `}'. */
21962 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21963 braces
.require_close (parser
);
21964 TREE_TYPE (initializer
) = init_list_type_node
;
21966 cp_expr
result (initializer
);
21967 /* Build a location of the form:
21970 with caret==start at the open brace, finish at the close brace. */
21971 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21972 result
.set_location (combined_loc
);
21976 /* Consume tokens up to, and including, the next non-nested closing `]'.
21977 Returns true iff we found a closing `]'. */
21980 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21982 unsigned square_depth
= 0;
21986 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21988 switch (token
->type
)
21991 case CPP_PRAGMA_EOL
:
21992 /* If we've run out of tokens, then there is no closing `]'. */
21995 case CPP_OPEN_SQUARE
:
21999 case CPP_CLOSE_SQUARE
:
22000 if (!square_depth
--)
22002 cp_lexer_consume_token (parser
->lexer
);
22011 /* Consume the token. */
22012 cp_lexer_consume_token (parser
->lexer
);
22016 /* Return true if we are looking at an array-designator, false otherwise. */
22019 cp_parser_array_designator_p (cp_parser
*parser
)
22021 /* Consume the `['. */
22022 cp_lexer_consume_token (parser
->lexer
);
22024 cp_lexer_save_tokens (parser
->lexer
);
22026 /* Skip tokens until the next token is a closing square bracket.
22027 If we find the closing `]', and the next token is a `=', then
22028 we are looking at an array designator. */
22029 bool array_designator_p
22030 = (cp_parser_skip_to_closing_square_bracket (parser
)
22031 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22033 /* Roll back the tokens we skipped. */
22034 cp_lexer_rollback_tokens (parser
->lexer
);
22036 return array_designator_p
;
22039 /* Parse an initializer-list.
22042 initializer-clause ... [opt]
22043 initializer-list , initializer-clause ... [opt]
22047 designated-initializer-list:
22048 designated-initializer-clause
22049 designated-initializer-list , designated-initializer-clause
22051 designated-initializer-clause:
22052 designator brace-or-equal-initializer
22060 designation initializer-clause ...[opt]
22061 initializer-list , designation initializer-clause ...[opt]
22066 [ constant-expression ] =
22068 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22069 for the initializer. If the INDEX of the elt is non-NULL, it is the
22070 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22071 as for cp_parser_initializer. */
22073 static vec
<constructor_elt
, va_gc
> *
22074 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22076 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22077 bool first_p
= true;
22078 tree first_designator
= NULL_TREE
;
22080 /* Assume all of the expressions are constant. */
22081 *non_constant_p
= false;
22083 /* Parse the rest of the list. */
22089 bool clause_non_constant_p
;
22090 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22092 /* Handle the C++2A syntax, '. id ='. */
22093 if ((cxx_dialect
>= cxx2a
22094 || cp_parser_allow_gnu_extensions_p (parser
))
22095 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22096 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22097 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22098 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22099 == CPP_OPEN_BRACE
)))
22101 if (cxx_dialect
< cxx2a
)
22102 pedwarn (loc
, OPT_Wpedantic
,
22103 "C++ designated initializers only available with "
22104 "-std=c++2a or -std=gnu++2a");
22105 /* Consume the `.'. */
22106 cp_lexer_consume_token (parser
->lexer
);
22107 /* Consume the identifier. */
22108 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22110 /* Consume the `='. */
22111 cp_lexer_consume_token (parser
->lexer
);
22113 /* Also, if the next token is an identifier and the following one is a
22114 colon, we are looking at the GNU designated-initializer
22116 else if (cp_parser_allow_gnu_extensions_p (parser
)
22117 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22118 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22121 /* Warn the user that they are using an extension. */
22122 pedwarn (loc
, OPT_Wpedantic
,
22123 "ISO C++ does not allow GNU designated initializers");
22124 /* Consume the identifier. */
22125 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22126 /* Consume the `:'. */
22127 cp_lexer_consume_token (parser
->lexer
);
22129 /* Also handle C99 array designators, '[ const ] ='. */
22130 else if (cp_parser_allow_gnu_extensions_p (parser
)
22131 && !c_dialect_objc ()
22132 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22134 /* In C++11, [ could start a lambda-introducer. */
22135 bool non_const
= false;
22137 cp_parser_parse_tentatively (parser
);
22139 if (!cp_parser_array_designator_p (parser
))
22141 cp_parser_simulate_error (parser
);
22142 designator
= NULL_TREE
;
22146 designator
= cp_parser_constant_expression (parser
, true,
22148 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22149 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22152 if (!cp_parser_parse_definitely (parser
))
22153 designator
= NULL_TREE
;
22155 && (!require_potential_rvalue_constant_expression
22157 designator
= NULL_TREE
;
22159 /* Warn the user that they are using an extension. */
22160 pedwarn (loc
, OPT_Wpedantic
,
22161 "ISO C++ does not allow C99 designated initializers");
22164 designator
= NULL_TREE
;
22168 first_designator
= designator
;
22171 else if (cxx_dialect
>= cxx2a
22172 && first_designator
!= error_mark_node
22173 && (!first_designator
!= !designator
))
22175 error_at (loc
, "either all initializer clauses should be designated "
22176 "or none of them should be");
22177 first_designator
= error_mark_node
;
22179 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22180 first_designator
= designator
;
22182 /* Parse the initializer. */
22183 initializer
= cp_parser_initializer_clause (parser
,
22184 &clause_non_constant_p
);
22185 /* If any clause is non-constant, so is the entire initializer. */
22186 if (clause_non_constant_p
)
22187 *non_constant_p
= true;
22189 /* If we have an ellipsis, this is an initializer pack
22191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22193 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22195 /* Consume the `...'. */
22196 cp_lexer_consume_token (parser
->lexer
);
22198 if (designator
&& cxx_dialect
>= cxx2a
)
22200 "%<...%> not allowed in designated initializer list");
22202 /* Turn the initializer into an initializer expansion. */
22203 initializer
= make_pack_expansion (initializer
);
22206 /* Add it to the vector. */
22207 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22209 /* If the next token is not a comma, we have reached the end of
22211 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22214 /* Peek at the next token. */
22215 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22216 /* If the next token is a `}', then we're still done. An
22217 initializer-clause can have a trailing `,' after the
22218 initializer-list and before the closing `}'. */
22219 if (token
->type
== CPP_CLOSE_BRACE
)
22222 /* Consume the `,' token. */
22223 cp_lexer_consume_token (parser
->lexer
);
22226 /* The same identifier shall not appear in multiple designators
22227 of a designated-initializer-list. */
22228 if (first_designator
)
22231 tree designator
, val
;
22232 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22233 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22235 if (IDENTIFIER_MARKED (designator
))
22237 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22238 "%<.%s%> designator used multiple times in "
22239 "the same initializer list",
22240 IDENTIFIER_POINTER (designator
));
22241 (*v
)[i
].index
= NULL_TREE
;
22244 IDENTIFIER_MARKED (designator
) = 1;
22246 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22247 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22248 IDENTIFIER_MARKED (designator
) = 0;
22254 /* Classes [gram.class] */
22256 /* Parse a class-name.
22262 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22263 to indicate that names looked up in dependent types should be
22264 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22265 keyword has been used to indicate that the name that appears next
22266 is a template. TAG_TYPE indicates the explicit tag given before
22267 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22268 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22269 is the class being defined in a class-head. If ENUM_OK is TRUE,
22270 enum-names are also accepted.
22272 Returns the TYPE_DECL representing the class. */
22275 cp_parser_class_name (cp_parser
*parser
,
22276 bool typename_keyword_p
,
22277 bool template_keyword_p
,
22278 enum tag_types tag_type
,
22279 bool check_dependency_p
,
22281 bool is_declaration
,
22288 tree identifier
= NULL_TREE
;
22290 /* All class-names start with an identifier. */
22291 token
= cp_lexer_peek_token (parser
->lexer
);
22292 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22294 cp_parser_error (parser
, "expected class-name");
22295 return error_mark_node
;
22298 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22299 to a template-id, so we save it here. */
22300 scope
= parser
->scope
;
22301 if (scope
== error_mark_node
)
22302 return error_mark_node
;
22304 /* Any name names a type if we're following the `typename' keyword
22305 in a qualified name where the enclosing scope is type-dependent. */
22306 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22307 && dependent_type_p (scope
));
22308 /* Handle the common case (an identifier, but not a template-id)
22310 if (token
->type
== CPP_NAME
22311 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22313 cp_token
*identifier_token
;
22316 /* Look for the identifier. */
22317 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22318 ambiguous_p
= identifier_token
->error_reported
;
22319 identifier
= cp_parser_identifier (parser
);
22320 /* If the next token isn't an identifier, we are certainly not
22321 looking at a class-name. */
22322 if (identifier
== error_mark_node
)
22323 decl
= error_mark_node
;
22324 /* If we know this is a type-name, there's no need to look it
22326 else if (typename_p
)
22330 tree ambiguous_decls
;
22331 /* If we already know that this lookup is ambiguous, then
22332 we've already issued an error message; there's no reason
22336 cp_parser_simulate_error (parser
);
22337 return error_mark_node
;
22339 /* If the next token is a `::', then the name must be a type
22342 [basic.lookup.qual]
22344 During the lookup for a name preceding the :: scope
22345 resolution operator, object, function, and enumerator
22346 names are ignored. */
22347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22348 tag_type
= scope_type
;
22349 /* Look up the name. */
22350 decl
= cp_parser_lookup_name (parser
, identifier
,
22352 /*is_template=*/false,
22353 /*is_namespace=*/false,
22354 check_dependency_p
,
22356 identifier_token
->location
);
22357 if (ambiguous_decls
)
22359 if (cp_parser_parsing_tentatively (parser
))
22360 cp_parser_simulate_error (parser
);
22361 return error_mark_node
;
22367 /* Try a template-id. */
22368 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22369 check_dependency_p
,
22372 if (decl
== error_mark_node
)
22373 return error_mark_node
;
22376 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22378 /* If this is a typename, create a TYPENAME_TYPE. */
22379 if (typename_p
&& decl
!= error_mark_node
)
22381 decl
= make_typename_type (scope
, decl
, typename_type
,
22382 /*complain=*/tf_error
);
22383 if (decl
!= error_mark_node
)
22384 decl
= TYPE_NAME (decl
);
22387 decl
= strip_using_decl (decl
);
22389 /* Check to see that it is really the name of a class. */
22390 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22391 && identifier_p (TREE_OPERAND (decl
, 0))
22392 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22393 /* Situations like this:
22395 template <typename T> struct A {
22396 typename T::template X<int>::I i;
22399 are problematic. Is `T::template X<int>' a class-name? The
22400 standard does not seem to be definitive, but there is no other
22401 valid interpretation of the following `::'. Therefore, those
22402 names are considered class-names. */
22404 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22405 if (decl
!= error_mark_node
)
22406 decl
= TYPE_NAME (decl
);
22408 else if (TREE_CODE (decl
) != TYPE_DECL
22409 || TREE_TYPE (decl
) == error_mark_node
22410 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22411 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22412 /* In Objective-C 2.0, a classname followed by '.' starts a
22413 dot-syntax expression, and it's not a type-name. */
22414 || (c_dialect_objc ()
22415 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22416 && objc_is_class_name (decl
)))
22417 decl
= error_mark_node
;
22419 if (decl
== error_mark_node
)
22420 cp_parser_error (parser
, "expected class-name");
22421 else if (identifier
&& !parser
->scope
)
22422 maybe_note_name_used_in_class (identifier
, decl
);
22427 /* Parse a class-specifier.
22430 class-head { member-specification [opt] }
22432 Returns the TREE_TYPE representing the class. */
22435 cp_parser_class_specifier_1 (cp_parser
* parser
)
22438 tree attributes
= NULL_TREE
;
22439 bool nested_name_specifier_p
;
22440 unsigned saved_num_template_parameter_lists
;
22441 bool saved_in_function_body
;
22442 unsigned char in_statement
;
22443 bool in_switch_statement_p
;
22444 bool saved_in_unbraced_linkage_specification_p
;
22445 tree old_scope
= NULL_TREE
;
22446 tree scope
= NULL_TREE
;
22447 cp_token
*closing_brace
;
22449 push_deferring_access_checks (dk_no_deferred
);
22451 /* Parse the class-head. */
22452 type
= cp_parser_class_head (parser
,
22453 &nested_name_specifier_p
);
22454 /* If the class-head was a semantic disaster, skip the entire body
22458 cp_parser_skip_to_end_of_block_or_statement (parser
);
22459 pop_deferring_access_checks ();
22460 return error_mark_node
;
22463 /* Look for the `{'. */
22464 matching_braces braces
;
22465 if (!braces
.require_open (parser
))
22467 pop_deferring_access_checks ();
22468 return error_mark_node
;
22471 cp_ensure_no_omp_declare_simd (parser
);
22472 cp_ensure_no_oacc_routine (parser
);
22474 /* Issue an error message if type-definitions are forbidden here. */
22475 cp_parser_check_type_definition (parser
);
22476 /* Remember that we are defining one more class. */
22477 ++parser
->num_classes_being_defined
;
22478 /* Inside the class, surrounding template-parameter-lists do not
22480 saved_num_template_parameter_lists
22481 = parser
->num_template_parameter_lists
;
22482 parser
->num_template_parameter_lists
= 0;
22483 /* We are not in a function body. */
22484 saved_in_function_body
= parser
->in_function_body
;
22485 parser
->in_function_body
= false;
22486 /* Or in a loop. */
22487 in_statement
= parser
->in_statement
;
22488 parser
->in_statement
= 0;
22489 /* Or in a switch. */
22490 in_switch_statement_p
= parser
->in_switch_statement_p
;
22491 parser
->in_switch_statement_p
= false;
22492 /* We are not immediately inside an extern "lang" block. */
22493 saved_in_unbraced_linkage_specification_p
22494 = parser
->in_unbraced_linkage_specification_p
;
22495 parser
->in_unbraced_linkage_specification_p
= false;
22497 // Associate constraints with the type.
22499 type
= associate_classtype_constraints (type
);
22501 /* Start the class. */
22502 if (nested_name_specifier_p
)
22504 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22505 old_scope
= push_inner_scope (scope
);
22507 type
= begin_class_definition (type
);
22509 if (type
== error_mark_node
)
22510 /* If the type is erroneous, skip the entire body of the class. */
22511 cp_parser_skip_to_closing_brace (parser
);
22513 /* Parse the member-specification. */
22514 cp_parser_member_specification_opt (parser
);
22516 /* Look for the trailing `}'. */
22517 closing_brace
= braces
.require_close (parser
);
22518 /* Look for trailing attributes to apply to this class. */
22519 if (cp_parser_allow_gnu_extensions_p (parser
))
22520 attributes
= cp_parser_gnu_attributes_opt (parser
);
22521 if (type
!= error_mark_node
)
22522 type
= finish_struct (type
, attributes
);
22523 if (nested_name_specifier_p
)
22524 pop_inner_scope (old_scope
, scope
);
22526 /* We've finished a type definition. Check for the common syntax
22527 error of forgetting a semicolon after the definition. We need to
22528 be careful, as we can't just check for not-a-semicolon and be done
22529 with it; the user might have typed:
22531 class X { } c = ...;
22532 class X { } *p = ...;
22534 and so forth. Instead, enumerate all the possible tokens that
22535 might follow this production; if we don't see one of them, then
22536 complain and silently insert the semicolon. */
22538 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22539 bool want_semicolon
= true;
22541 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22542 /* Don't try to parse c++11 attributes here. As per the
22543 grammar, that should be a task for
22544 cp_parser_decl_specifier_seq. */
22545 want_semicolon
= false;
22547 switch (token
->type
)
22550 case CPP_SEMICOLON
:
22553 case CPP_OPEN_PAREN
:
22554 case CPP_CLOSE_PAREN
:
22556 want_semicolon
= false;
22559 /* While it's legal for type qualifiers and storage class
22560 specifiers to follow type definitions in the grammar, only
22561 compiler testsuites contain code like that. Assume that if
22562 we see such code, then what we're really seeing is a case
22566 const <type> var = ...;
22571 static <type> func (...) ...
22573 i.e. the qualifier or specifier applies to the next
22574 declaration. To do so, however, we need to look ahead one
22575 more token to see if *that* token is a type specifier.
22577 This code could be improved to handle:
22580 static const <type> var = ...; */
22582 if (keyword_is_decl_specifier (token
->keyword
))
22584 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22586 /* Handling user-defined types here would be nice, but very
22589 = (lookahead
->type
== CPP_KEYWORD
22590 && keyword_begins_type_specifier (lookahead
->keyword
));
22597 /* If we don't have a type, then something is very wrong and we
22598 shouldn't try to do anything clever. Likewise for not seeing the
22600 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22602 /* Locate the closing brace. */
22603 cp_token_position prev
22604 = cp_lexer_previous_token_position (parser
->lexer
);
22605 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22606 location_t loc
= prev_token
->location
;
22608 /* We want to suggest insertion of a ';' immediately *after* the
22609 closing brace, so, if we can, offset the location by 1 column. */
22610 location_t next_loc
= loc
;
22611 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22612 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22614 rich_location
richloc (line_table
, next_loc
);
22616 /* If we successfully offset the location, suggest the fix-it. */
22617 if (next_loc
!= loc
)
22618 richloc
.add_fixit_insert_before (next_loc
, ";");
22620 if (CLASSTYPE_DECLARED_CLASS (type
))
22621 error_at (&richloc
,
22622 "expected %<;%> after class definition");
22623 else if (TREE_CODE (type
) == RECORD_TYPE
)
22624 error_at (&richloc
,
22625 "expected %<;%> after struct definition");
22626 else if (TREE_CODE (type
) == UNION_TYPE
)
22627 error_at (&richloc
,
22628 "expected %<;%> after union definition");
22630 gcc_unreachable ();
22632 /* Unget one token and smash it to look as though we encountered
22633 a semicolon in the input stream. */
22634 cp_lexer_set_token_position (parser
->lexer
, prev
);
22635 token
= cp_lexer_peek_token (parser
->lexer
);
22636 token
->type
= CPP_SEMICOLON
;
22637 token
->keyword
= RID_MAX
;
22641 /* If this class is not itself within the scope of another class,
22642 then we need to parse the bodies of all of the queued function
22643 definitions. Note that the queued functions defined in a class
22644 are not always processed immediately following the
22645 class-specifier for that class. Consider:
22648 struct B { void f() { sizeof (A); } };
22651 If `f' were processed before the processing of `A' were
22652 completed, there would be no way to compute the size of `A'.
22653 Note that the nesting we are interested in here is lexical --
22654 not the semantic nesting given by TYPE_CONTEXT. In particular,
22657 struct A { struct B; };
22658 struct A::B { void f() { } };
22660 there is no need to delay the parsing of `A::B::f'. */
22661 if (--parser
->num_classes_being_defined
== 0)
22664 tree class_type
= NULL_TREE
;
22665 tree pushed_scope
= NULL_TREE
;
22667 cp_default_arg_entry
*e
;
22668 tree save_ccp
, save_ccr
;
22670 /* In a first pass, parse default arguments to the functions.
22671 Then, in a second pass, parse the bodies of the functions.
22672 This two-phased approach handles cases like:
22680 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22683 /* If there are default arguments that have not yet been processed,
22684 take care of them now. */
22685 if (class_type
!= e
->class_type
)
22688 pop_scope (pushed_scope
);
22689 class_type
= e
->class_type
;
22690 pushed_scope
= push_scope (class_type
);
22692 /* Make sure that any template parameters are in scope. */
22693 maybe_begin_member_template_processing (decl
);
22694 /* Parse the default argument expressions. */
22695 cp_parser_late_parsing_default_args (parser
, decl
);
22696 /* Remove any template parameters from the symbol table. */
22697 maybe_end_member_template_processing ();
22699 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22700 /* Now parse any NSDMIs. */
22701 save_ccp
= current_class_ptr
;
22702 save_ccr
= current_class_ref
;
22703 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22705 if (class_type
!= DECL_CONTEXT (decl
))
22708 pop_scope (pushed_scope
);
22709 class_type
= DECL_CONTEXT (decl
);
22710 pushed_scope
= push_scope (class_type
);
22712 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22713 cp_parser_late_parsing_nsdmi (parser
, decl
);
22715 vec_safe_truncate (unparsed_nsdmis
, 0);
22716 current_class_ptr
= save_ccp
;
22717 current_class_ref
= save_ccr
;
22719 pop_scope (pushed_scope
);
22721 /* Now do some post-NSDMI bookkeeping. */
22722 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22723 after_nsdmi_defaulted_late_checks (class_type
);
22724 vec_safe_truncate (unparsed_classes
, 0);
22725 after_nsdmi_defaulted_late_checks (type
);
22727 /* Now parse the body of the functions. */
22730 /* OpenMP UDRs need to be parsed before all other functions. */
22731 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22732 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22733 cp_parser_late_parsing_for_member (parser
, decl
);
22734 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22735 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22736 cp_parser_late_parsing_for_member (parser
, decl
);
22739 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22740 cp_parser_late_parsing_for_member (parser
, decl
);
22741 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22744 vec_safe_push (unparsed_classes
, type
);
22746 /* Put back any saved access checks. */
22747 pop_deferring_access_checks ();
22749 /* Restore saved state. */
22750 parser
->in_switch_statement_p
= in_switch_statement_p
;
22751 parser
->in_statement
= in_statement
;
22752 parser
->in_function_body
= saved_in_function_body
;
22753 parser
->num_template_parameter_lists
22754 = saved_num_template_parameter_lists
;
22755 parser
->in_unbraced_linkage_specification_p
22756 = saved_in_unbraced_linkage_specification_p
;
22762 cp_parser_class_specifier (cp_parser
* parser
)
22765 timevar_push (TV_PARSE_STRUCT
);
22766 ret
= cp_parser_class_specifier_1 (parser
);
22767 timevar_pop (TV_PARSE_STRUCT
);
22771 /* Parse a class-head.
22774 class-key identifier [opt] base-clause [opt]
22775 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22776 class-key nested-name-specifier [opt] template-id
22779 class-virt-specifier:
22783 class-key attributes identifier [opt] base-clause [opt]
22784 class-key attributes nested-name-specifier identifier base-clause [opt]
22785 class-key attributes nested-name-specifier [opt] template-id
22788 Upon return BASES is initialized to the list of base classes (or
22789 NULL, if there are none) in the same form returned by
22790 cp_parser_base_clause.
22792 Returns the TYPE of the indicated class. Sets
22793 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22794 involving a nested-name-specifier was used, and FALSE otherwise.
22796 Returns error_mark_node if this is not a class-head.
22798 Returns NULL_TREE if the class-head is syntactically valid, but
22799 semantically invalid in a way that means we should skip the entire
22800 body of the class. */
22803 cp_parser_class_head (cp_parser
* parser
,
22804 bool* nested_name_specifier_p
)
22806 tree nested_name_specifier
;
22807 enum tag_types class_key
;
22808 tree id
= NULL_TREE
;
22809 tree type
= NULL_TREE
;
22812 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22813 bool template_id_p
= false;
22814 bool qualified_p
= false;
22815 bool invalid_nested_name_p
= false;
22816 bool invalid_explicit_specialization_p
= false;
22817 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22818 tree pushed_scope
= NULL_TREE
;
22819 unsigned num_templates
;
22820 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22821 /* Assume no nested-name-specifier will be present. */
22822 *nested_name_specifier_p
= false;
22823 /* Assume no template parameter lists will be used in defining the
22826 parser
->colon_corrects_to_scope_p
= false;
22828 /* Look for the class-key. */
22829 class_key
= cp_parser_class_key (parser
);
22830 if (class_key
== none_type
)
22831 return error_mark_node
;
22833 location_t class_head_start_location
= input_location
;
22835 /* Parse the attributes. */
22836 attributes
= cp_parser_attributes_opt (parser
);
22838 /* If the next token is `::', that is invalid -- but sometimes
22839 people do try to write:
22843 Handle this gracefully by accepting the extra qualifier, and then
22844 issuing an error about it later if this really is a
22845 class-head. If it turns out just to be an elaborated type
22846 specifier, remain silent. */
22847 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22848 qualified_p
= true;
22850 push_deferring_access_checks (dk_no_check
);
22852 /* Determine the name of the class. Begin by looking for an
22853 optional nested-name-specifier. */
22854 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22855 nested_name_specifier
22856 = cp_parser_nested_name_specifier_opt (parser
,
22857 /*typename_keyword_p=*/false,
22858 /*check_dependency_p=*/false,
22860 /*is_declaration=*/false);
22861 /* If there was a nested-name-specifier, then there *must* be an
22864 cp_token
*bad_template_keyword
= NULL
;
22866 if (nested_name_specifier
)
22868 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22869 /* Although the grammar says `identifier', it really means
22870 `class-name' or `template-name'. You are only allowed to
22871 define a class that has already been declared with this
22874 The proposed resolution for Core Issue 180 says that wherever
22875 you see `class T::X' you should treat `X' as a type-name.
22877 It is OK to define an inaccessible class; for example:
22879 class A { class B; };
22882 We do not know if we will see a class-name, or a
22883 template-name. We look for a class-name first, in case the
22884 class-name is a template-id; if we looked for the
22885 template-name first we would stop after the template-name. */
22886 cp_parser_parse_tentatively (parser
);
22887 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22888 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22889 type
= cp_parser_class_name (parser
,
22890 /*typename_keyword_p=*/false,
22891 /*template_keyword_p=*/false,
22893 /*check_dependency_p=*/false,
22894 /*class_head_p=*/true,
22895 /*is_declaration=*/false);
22896 /* If that didn't work, ignore the nested-name-specifier. */
22897 if (!cp_parser_parse_definitely (parser
))
22899 invalid_nested_name_p
= true;
22900 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22901 id
= cp_parser_identifier (parser
);
22902 if (id
== error_mark_node
)
22905 /* If we could not find a corresponding TYPE, treat this
22906 declaration like an unqualified declaration. */
22907 if (type
== error_mark_node
)
22908 nested_name_specifier
= NULL_TREE
;
22909 /* Otherwise, count the number of templates used in TYPE and its
22910 containing scopes. */
22915 for (scope
= TREE_TYPE (type
);
22916 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22917 scope
= get_containing_scope (scope
))
22919 && CLASS_TYPE_P (scope
)
22920 && CLASSTYPE_TEMPLATE_INFO (scope
)
22921 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22922 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22923 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22927 /* Otherwise, the identifier is optional. */
22930 /* We don't know whether what comes next is a template-id,
22931 an identifier, or nothing at all. */
22932 cp_parser_parse_tentatively (parser
);
22933 /* Check for a template-id. */
22934 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22935 id
= cp_parser_template_id (parser
,
22936 /*template_keyword_p=*/false,
22937 /*check_dependency_p=*/true,
22939 /*is_declaration=*/true);
22940 /* If that didn't work, it could still be an identifier. */
22941 if (!cp_parser_parse_definitely (parser
))
22943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22945 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22946 id
= cp_parser_identifier (parser
);
22953 template_id_p
= true;
22958 pop_deferring_access_checks ();
22962 cp_parser_check_for_invalid_template_id (parser
, id
,
22964 type_start_token
->location
);
22966 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22968 /* If it's not a `:' or a `{' then we can't really be looking at a
22969 class-head, since a class-head only appears as part of a
22970 class-specifier. We have to detect this situation before calling
22971 xref_tag, since that has irreversible side-effects. */
22972 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22974 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22975 type
= error_mark_node
;
22979 /* At this point, we're going ahead with the class-specifier, even
22980 if some other problem occurs. */
22981 cp_parser_commit_to_tentative_parse (parser
);
22982 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22984 cp_parser_error (parser
,
22985 "cannot specify %<override%> for a class");
22986 type
= error_mark_node
;
22989 /* Issue the error about the overly-qualified name now. */
22992 cp_parser_error (parser
,
22993 "global qualification of class name is invalid");
22994 type
= error_mark_node
;
22997 else if (invalid_nested_name_p
)
22999 cp_parser_error (parser
,
23000 "qualified name does not name a class");
23001 type
= error_mark_node
;
23004 else if (nested_name_specifier
)
23008 if (bad_template_keyword
)
23009 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23010 keyword template shall not appear at the top level. */
23011 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23012 "keyword %<template%> not allowed in class-head-name");
23014 /* Reject typedef-names in class heads. */
23015 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23017 error_at (type_start_token
->location
,
23018 "invalid class name in declaration of %qD",
23024 /* Figure out in what scope the declaration is being placed. */
23025 scope
= current_scope ();
23026 /* If that scope does not contain the scope in which the
23027 class was originally declared, the program is invalid. */
23028 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23030 if (at_namespace_scope_p ())
23031 error_at (type_start_token
->location
,
23032 "declaration of %qD in namespace %qD which does not "
23034 type
, scope
, nested_name_specifier
);
23036 error_at (type_start_token
->location
,
23037 "declaration of %qD in %qD which does not enclose %qD",
23038 type
, scope
, nested_name_specifier
);
23044 A declarator-id shall not be qualified except for the
23045 definition of a ... nested class outside of its class
23046 ... [or] the definition or explicit instantiation of a
23047 class member of a namespace outside of its namespace. */
23048 if (scope
== nested_name_specifier
)
23050 permerror (nested_name_specifier_token_start
->location
,
23051 "extra qualification not allowed");
23052 nested_name_specifier
= NULL_TREE
;
23056 /* An explicit-specialization must be preceded by "template <>". If
23057 it is not, try to recover gracefully. */
23058 if (at_namespace_scope_p ()
23059 && parser
->num_template_parameter_lists
== 0
23060 && !processing_template_parmlist
23063 /* Build a location of this form:
23064 struct typename <ARGS>
23065 ^~~~~~~~~~~~~~~~~~~~~~
23066 with caret==start at the start token, and
23067 finishing at the end of the type. */
23068 location_t reported_loc
23069 = make_location (class_head_start_location
,
23070 class_head_start_location
,
23071 get_finish (type_start_token
->location
));
23072 rich_location
richloc (line_table
, reported_loc
);
23073 richloc
.add_fixit_insert_before (class_head_start_location
,
23075 error_at (&richloc
,
23076 "an explicit specialization must be preceded by"
23077 " %<template <>%>");
23078 invalid_explicit_specialization_p
= true;
23079 /* Take the same action that would have been taken by
23080 cp_parser_explicit_specialization. */
23081 ++parser
->num_template_parameter_lists
;
23082 begin_specialization ();
23084 /* There must be no "return" statements between this point and the
23085 end of this function; set "type "to the correct return value and
23086 use "goto done;" to return. */
23087 /* Make sure that the right number of template parameters were
23089 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23090 type_start_token
->location
,
23091 /*declarator=*/NULL
))
23093 /* If something went wrong, there is no point in even trying to
23094 process the class-definition. */
23099 /* Look up the type. */
23102 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23103 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23104 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23106 error_at (type_start_token
->location
,
23107 "function template %qD redeclared as a class template", id
);
23108 type
= error_mark_node
;
23112 type
= TREE_TYPE (id
);
23113 type
= maybe_process_partial_specialization (type
);
23115 /* Check the scope while we still know whether or not we had a
23116 nested-name-specifier. */
23117 if (type
!= error_mark_node
)
23118 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23120 if (nested_name_specifier
)
23121 pushed_scope
= push_scope (nested_name_specifier
);
23123 else if (nested_name_specifier
)
23129 template <typename T> struct S { struct T };
23130 template <typename T> struct S<T>::T { };
23132 we will get a TYPENAME_TYPE when processing the definition of
23133 `S::T'. We need to resolve it to the actual type before we
23134 try to define it. */
23135 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23137 class_type
= resolve_typename_type (TREE_TYPE (type
),
23138 /*only_current_p=*/false);
23139 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23140 type
= TYPE_NAME (class_type
);
23143 cp_parser_error (parser
, "could not resolve typename type");
23144 type
= error_mark_node
;
23148 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23149 == error_mark_node
)
23155 class_type
= current_class_type
;
23156 /* Enter the scope indicated by the nested-name-specifier. */
23157 pushed_scope
= push_scope (nested_name_specifier
);
23158 /* Get the canonical version of this type. */
23159 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23160 /* Call push_template_decl if it seems like we should be defining a
23161 template either from the template headers or the type we're
23162 defining, so that we diagnose both extra and missing headers. */
23163 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23164 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23165 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23167 type
= push_template_decl (type
);
23168 if (type
== error_mark_node
)
23175 type
= TREE_TYPE (type
);
23176 *nested_name_specifier_p
= true;
23178 else /* The name is not a nested name. */
23180 /* If the class was unnamed, create a dummy name. */
23182 id
= make_anon_name ();
23183 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23184 ? ts_within_enclosing_non_class
23186 type
= xref_tag (class_key
, id
, tag_scope
,
23187 parser
->num_template_parameter_lists
);
23190 /* Indicate whether this class was declared as a `class' or as a
23192 if (TREE_CODE (type
) == RECORD_TYPE
)
23193 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23194 cp_parser_check_class_key (class_key
, type
);
23196 /* If this type was already complete, and we see another definition,
23197 that's an error. */
23198 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23200 error_at (type_start_token
->location
, "redefinition of %q#T",
23202 inform (location_of (type
), "previous definition of %q#T",
23207 else if (type
== error_mark_node
)
23212 /* Apply attributes now, before any use of the class as a template
23213 argument in its base list. */
23214 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23215 fixup_attribute_variants (type
);
23218 /* We will have entered the scope containing the class; the names of
23219 base classes should be looked up in that context. For example:
23221 struct A { struct B {}; struct C; };
23222 struct A::C : B {};
23226 /* Get the list of base-classes, if there is one. */
23227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23229 /* PR59482: enter the class scope so that base-specifiers are looked
23233 bases
= cp_parser_base_clause (parser
);
23234 /* PR59482: get out of the previously pushed class scope so that the
23235 subsequent pops pop the right thing. */
23242 /* If we're really defining a class, process the base classes.
23243 If they're invalid, fail. */
23244 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23245 xref_basetypes (type
, bases
);
23248 /* Leave the scope given by the nested-name-specifier. We will
23249 enter the class scope itself while processing the members. */
23251 pop_scope (pushed_scope
);
23253 if (invalid_explicit_specialization_p
)
23255 end_specialization ();
23256 --parser
->num_template_parameter_lists
;
23260 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23261 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23262 CLASSTYPE_FINAL (type
) = 1;
23264 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23268 /* Parse a class-key.
23275 Returns the kind of class-key specified, or none_type to indicate
23278 static enum tag_types
23279 cp_parser_class_key (cp_parser
* parser
)
23282 enum tag_types tag_type
;
23284 /* Look for the class-key. */
23285 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23289 /* Check to see if the TOKEN is a class-key. */
23290 tag_type
= cp_parser_token_is_class_key (token
);
23292 cp_parser_error (parser
, "expected class-key");
23296 /* Parse a type-parameter-key.
23298 type-parameter-key:
23304 cp_parser_type_parameter_key (cp_parser
* parser
)
23306 /* Look for the type-parameter-key. */
23307 enum tag_types tag_type
= none_type
;
23308 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23309 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23311 cp_lexer_consume_token (parser
->lexer
);
23312 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23313 /* typename is not allowed in a template template parameter
23314 by the standard until C++17. */
23315 pedwarn (token
->location
, OPT_Wpedantic
,
23316 "ISO C++ forbids typename key in template template parameter;"
23317 " use -std=c++17 or -std=gnu++17");
23320 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23325 /* Parse an (optional) member-specification.
23327 member-specification:
23328 member-declaration member-specification [opt]
23329 access-specifier : member-specification [opt] */
23332 cp_parser_member_specification_opt (cp_parser
* parser
)
23339 /* Peek at the next token. */
23340 token
= cp_lexer_peek_token (parser
->lexer
);
23341 /* If it's a `}', or EOF then we've seen all the members. */
23342 if (token
->type
== CPP_CLOSE_BRACE
23343 || token
->type
== CPP_EOF
23344 || token
->type
== CPP_PRAGMA_EOL
)
23347 /* See if this token is a keyword. */
23348 keyword
= token
->keyword
;
23352 case RID_PROTECTED
:
23354 /* Consume the access-specifier. */
23355 cp_lexer_consume_token (parser
->lexer
);
23356 /* Remember which access-specifier is active. */
23357 current_access_specifier
= token
->u
.value
;
23358 /* Look for the `:'. */
23359 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23363 /* Accept #pragmas at class scope. */
23364 if (token
->type
== CPP_PRAGMA
)
23366 cp_parser_pragma (parser
, pragma_member
, NULL
);
23370 /* Otherwise, the next construction must be a
23371 member-declaration. */
23372 cp_parser_member_declaration (parser
);
23377 /* Parse a member-declaration.
23379 member-declaration:
23380 decl-specifier-seq [opt] member-declarator-list [opt] ;
23381 function-definition ; [opt]
23382 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23384 template-declaration
23387 member-declarator-list:
23389 member-declarator-list , member-declarator
23392 declarator pure-specifier [opt]
23393 declarator constant-initializer [opt]
23394 identifier [opt] : constant-expression
23398 member-declaration:
23399 __extension__ member-declaration
23402 declarator attributes [opt] pure-specifier [opt]
23403 declarator attributes [opt] constant-initializer [opt]
23404 identifier [opt] attributes [opt] : constant-expression
23408 member-declaration:
23409 static_assert-declaration */
23412 cp_parser_member_declaration (cp_parser
* parser
)
23414 cp_decl_specifier_seq decl_specifiers
;
23415 tree prefix_attributes
;
23417 int declares_class_or_enum
;
23419 cp_token
*token
= NULL
;
23420 cp_token
*decl_spec_token_start
= NULL
;
23421 cp_token
*initializer_token_start
= NULL
;
23422 int saved_pedantic
;
23423 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23425 /* Check for the `__extension__' keyword. */
23426 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23429 cp_parser_member_declaration (parser
);
23430 /* Restore the old value of the PEDANTIC flag. */
23431 pedantic
= saved_pedantic
;
23436 /* Check for a template-declaration. */
23437 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23439 /* An explicit specialization here is an error condition, and we
23440 expect the specialization handler to detect and report this. */
23441 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23442 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23443 cp_parser_explicit_specialization (parser
);
23445 cp_parser_template_declaration (parser
, /*member_p=*/true);
23449 /* Check for a template introduction. */
23450 else if (cp_parser_template_declaration_after_export (parser
, true))
23453 /* Check for a using-declaration. */
23454 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23456 if (cxx_dialect
< cxx11
)
23458 /* Parse the using-declaration. */
23459 cp_parser_using_declaration (parser
,
23460 /*access_declaration_p=*/false);
23466 bool alias_decl_expected
;
23467 cp_parser_parse_tentatively (parser
);
23468 decl
= cp_parser_alias_declaration (parser
);
23469 /* Note that if we actually see the '=' token after the
23470 identifier, cp_parser_alias_declaration commits the
23471 tentative parse. In that case, we really expect an
23472 alias-declaration. Otherwise, we expect a using
23474 alias_decl_expected
=
23475 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23476 cp_parser_parse_definitely (parser
);
23478 if (alias_decl_expected
)
23479 finish_member_declaration (decl
);
23481 cp_parser_using_declaration (parser
,
23482 /*access_declaration_p=*/false);
23487 /* Check for @defs. */
23488 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23491 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23492 ivar
= ivar_chains
;
23496 ivar
= TREE_CHAIN (member
);
23497 TREE_CHAIN (member
) = NULL_TREE
;
23498 finish_member_declaration (member
);
23503 /* If the next token is `static_assert' we have a static assertion. */
23504 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23506 cp_parser_static_assert (parser
, /*member_p=*/true);
23510 parser
->colon_corrects_to_scope_p
= false;
23512 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23515 /* Parse the decl-specifier-seq. */
23516 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23517 cp_parser_decl_specifier_seq (parser
,
23518 CP_PARSER_FLAGS_OPTIONAL
,
23520 &declares_class_or_enum
);
23521 /* Check for an invalid type-name. */
23522 if (!decl_specifiers
.any_type_specifiers_p
23523 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23525 /* If there is no declarator, then the decl-specifier-seq should
23527 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23529 /* If there was no decl-specifier-seq, and the next token is a
23530 `;', then we have something like:
23536 Each member-declaration shall declare at least one member
23537 name of the class. */
23538 if (!decl_specifiers
.any_specifiers_p
)
23540 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23541 if (!in_system_header_at (token
->location
))
23543 gcc_rich_location
richloc (token
->location
);
23544 richloc
.add_fixit_remove ();
23545 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23552 /* See if this declaration is a friend. */
23553 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23554 /* If there were decl-specifiers, check to see if there was
23555 a class-declaration. */
23556 type
= check_tag_decl (&decl_specifiers
,
23557 /*explicit_type_instantiation_p=*/false);
23558 /* Nested classes have already been added to the class, but
23559 a `friend' needs to be explicitly registered. */
23562 /* If the `friend' keyword was present, the friend must
23563 be introduced with a class-key. */
23564 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23565 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23566 "in C++03 a class-key must be used "
23567 "when declaring a friend");
23570 template <typename T> struct A {
23571 friend struct A<T>::B;
23574 A<T>::B will be represented by a TYPENAME_TYPE, and
23575 therefore not recognized by check_tag_decl. */
23578 type
= decl_specifiers
.type
;
23579 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23580 type
= TREE_TYPE (type
);
23582 if (!type
|| !TYPE_P (type
))
23583 error_at (decl_spec_token_start
->location
,
23584 "friend declaration does not name a class or "
23587 make_friend_class (current_class_type
, type
,
23588 /*complain=*/true);
23590 /* If there is no TYPE, an error message will already have
23592 else if (!type
|| type
== error_mark_node
)
23594 /* An anonymous aggregate has to be handled specially; such
23595 a declaration really declares a data member (with a
23596 particular type), as opposed to a nested class. */
23597 else if (ANON_AGGR_TYPE_P (type
))
23600 if (decl_specifiers
.storage_class
!= sc_none
)
23601 error_at (decl_spec_token_start
->location
,
23602 "a storage class on an anonymous aggregate "
23603 "in class scope is not allowed");
23605 /* Remove constructors and such from TYPE, now that we
23606 know it is an anonymous aggregate. */
23607 fixup_anonymous_aggr (type
);
23608 /* And make the corresponding data member. */
23609 decl
= build_decl (decl_spec_token_start
->location
,
23610 FIELD_DECL
, NULL_TREE
, type
);
23611 /* Add it to the class. */
23612 finish_member_declaration (decl
);
23615 cp_parser_check_access_in_redeclaration
23617 decl_spec_token_start
->location
);
23622 bool assume_semicolon
= false;
23624 /* Clear attributes from the decl_specifiers but keep them
23625 around as prefix attributes that apply them to the entity
23627 prefix_attributes
= decl_specifiers
.attributes
;
23628 decl_specifiers
.attributes
= NULL_TREE
;
23630 /* See if these declarations will be friends. */
23631 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23633 /* Keep going until we hit the `;' at the end of the
23635 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23637 tree attributes
= NULL_TREE
;
23638 tree first_attribute
;
23640 bool named_bitfld
= false;
23642 /* Peek at the next token. */
23643 token
= cp_lexer_peek_token (parser
->lexer
);
23645 /* The following code wants to know early if it is a bit-field
23646 or some other declaration. Attributes can appear before
23647 the `:' token. Skip over them without consuming any tokens
23648 to peek if they are followed by `:'. */
23649 if (cp_next_tokens_can_be_attribute_p (parser
)
23650 || (token
->type
== CPP_NAME
23651 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23652 && (named_bitfld
= true)))
23655 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23656 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23659 /* Check for a bitfield declaration. */
23660 if (token
->type
== CPP_COLON
23661 || (token
->type
== CPP_NAME
23662 && token
== cp_lexer_peek_token (parser
->lexer
)
23663 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23664 && (named_bitfld
= true)))
23668 tree late_attributes
= NULL_TREE
;
23671 identifier
= cp_parser_identifier (parser
);
23673 identifier
= NULL_TREE
;
23675 /* Look for attributes that apply to the bitfield. */
23676 attributes
= cp_parser_attributes_opt (parser
);
23678 /* Consume the `:' token. */
23679 cp_lexer_consume_token (parser
->lexer
);
23681 /* Get the width of the bitfield. */
23682 width
= cp_parser_constant_expression (parser
, false, NULL
,
23683 cxx_dialect
>= cxx11
);
23685 /* In C++2A and as extension for C++11 and above we allow
23686 default member initializers for bit-fields. */
23687 initializer
= NULL_TREE
;
23688 if (cxx_dialect
>= cxx11
23689 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23690 || cp_lexer_next_token_is (parser
->lexer
,
23694 = cp_lexer_peek_token (parser
->lexer
)->location
;
23695 if (cxx_dialect
< cxx2a
23696 && !in_system_header_at (loc
)
23697 && identifier
!= NULL_TREE
)
23699 "default member initializers for bit-fields "
23700 "only available with -std=c++2a or "
23703 initializer
= cp_parser_save_nsdmi (parser
);
23704 if (identifier
== NULL_TREE
)
23706 error_at (loc
, "default member initializer for "
23707 "unnamed bit-field");
23708 initializer
= NULL_TREE
;
23713 /* Look for attributes that apply to the bitfield after
23714 the `:' token and width. This is where GCC used to
23715 parse attributes in the past, pedwarn if there is
23716 a std attribute. */
23717 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23718 pedwarn (input_location
, OPT_Wpedantic
,
23719 "ISO C++ allows bit-field attributes only "
23720 "before the %<:%> token");
23722 late_attributes
= cp_parser_attributes_opt (parser
);
23725 attributes
= attr_chainon (attributes
, late_attributes
);
23727 /* Remember which attributes are prefix attributes and
23729 first_attribute
= attributes
;
23730 /* Combine the attributes. */
23731 attributes
= attr_chainon (prefix_attributes
, attributes
);
23733 /* Create the bitfield declaration. */
23734 decl
= grokbitfield (identifier
23735 ? make_id_declarator (NULL_TREE
,
23740 width
, initializer
,
23745 cp_declarator
*declarator
;
23746 tree asm_specification
;
23747 int ctor_dtor_or_conv_p
;
23749 /* Parse the declarator. */
23751 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23752 &ctor_dtor_or_conv_p
,
23753 /*parenthesized_p=*/NULL
,
23757 /* If something went wrong parsing the declarator, make sure
23758 that we at least consume some tokens. */
23759 if (declarator
== cp_error_declarator
)
23761 /* Skip to the end of the statement. */
23762 cp_parser_skip_to_end_of_statement (parser
);
23763 /* If the next token is not a semicolon, that is
23764 probably because we just skipped over the body of
23765 a function. So, we consume a semicolon if
23766 present, but do not issue an error message if it
23768 if (cp_lexer_next_token_is (parser
->lexer
,
23770 cp_lexer_consume_token (parser
->lexer
);
23774 if (declares_class_or_enum
& 2)
23775 cp_parser_check_for_definition_in_return_type
23776 (declarator
, decl_specifiers
.type
,
23777 decl_specifiers
.locations
[ds_type_spec
]);
23779 /* Look for an asm-specification. */
23780 asm_specification
= cp_parser_asm_specification_opt (parser
);
23781 /* Look for attributes that apply to the declaration. */
23782 attributes
= cp_parser_attributes_opt (parser
);
23783 /* Remember which attributes are prefix attributes and
23785 first_attribute
= attributes
;
23786 /* Combine the attributes. */
23787 attributes
= attr_chainon (prefix_attributes
, attributes
);
23789 /* If it's an `=', then we have a constant-initializer or a
23790 pure-specifier. It is not correct to parse the
23791 initializer before registering the member declaration
23792 since the member declaration should be in scope while
23793 its initializer is processed. However, the rest of the
23794 front end does not yet provide an interface that allows
23795 us to handle this correctly. */
23796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23800 A pure-specifier shall be used only in the declaration of
23801 a virtual function.
23803 A member-declarator can contain a constant-initializer
23804 only if it declares a static member of integral or
23807 Therefore, if the DECLARATOR is for a function, we look
23808 for a pure-specifier; otherwise, we look for a
23809 constant-initializer. When we call `grokfield', it will
23810 perform more stringent semantics checks. */
23811 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23812 if (function_declarator_p (declarator
)
23813 || (decl_specifiers
.type
23814 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23815 && declarator
->kind
== cdk_id
23816 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23817 == FUNCTION_TYPE
)))
23818 initializer
= cp_parser_pure_specifier (parser
);
23819 else if (decl_specifiers
.storage_class
!= sc_static
)
23820 initializer
= cp_parser_save_nsdmi (parser
);
23821 else if (cxx_dialect
>= cxx11
)
23824 /* Don't require a constant rvalue in C++11, since we
23825 might want a reference constant. We'll enforce
23826 constancy later. */
23827 cp_lexer_consume_token (parser
->lexer
);
23828 /* Parse the initializer. */
23829 initializer
= cp_parser_initializer_clause (parser
,
23833 /* Parse the initializer. */
23834 initializer
= cp_parser_constant_initializer (parser
);
23836 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23837 && !function_declarator_p (declarator
))
23840 if (decl_specifiers
.storage_class
!= sc_static
)
23841 initializer
= cp_parser_save_nsdmi (parser
);
23843 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23845 /* Otherwise, there is no initializer. */
23847 initializer
= NULL_TREE
;
23849 /* See if we are probably looking at a function
23850 definition. We are certainly not looking at a
23851 member-declarator. Calling `grokfield' has
23852 side-effects, so we must not do it unless we are sure
23853 that we are looking at a member-declarator. */
23854 if (cp_parser_token_starts_function_definition_p
23855 (cp_lexer_peek_token (parser
->lexer
)))
23857 /* The grammar does not allow a pure-specifier to be
23858 used when a member function is defined. (It is
23859 possible that this fact is an oversight in the
23860 standard, since a pure function may be defined
23861 outside of the class-specifier. */
23862 if (initializer
&& initializer_token_start
)
23863 error_at (initializer_token_start
->location
,
23864 "pure-specifier on function-definition");
23865 decl
= cp_parser_save_member_function_body (parser
,
23869 if (parser
->fully_implicit_function_template_p
)
23870 decl
= finish_fully_implicit_template (parser
, decl
);
23871 /* If the member was not a friend, declare it here. */
23873 finish_member_declaration (decl
);
23874 /* Peek at the next token. */
23875 token
= cp_lexer_peek_token (parser
->lexer
);
23876 /* If the next token is a semicolon, consume it. */
23877 if (token
->type
== CPP_SEMICOLON
)
23879 location_t semicolon_loc
23880 = cp_lexer_consume_token (parser
->lexer
)->location
;
23881 gcc_rich_location
richloc (semicolon_loc
);
23882 richloc
.add_fixit_remove ();
23883 warning_at (&richloc
, OPT_Wextra_semi
,
23884 "extra %<;%> after in-class "
23885 "function definition");
23890 if (declarator
->kind
== cdk_function
)
23891 declarator
->id_loc
= token
->location
;
23892 /* Create the declaration. */
23893 decl
= grokfield (declarator
, &decl_specifiers
,
23894 initializer
, /*init_const_expr_p=*/true,
23895 asm_specification
, attributes
);
23896 if (parser
->fully_implicit_function_template_p
)
23899 finish_fully_implicit_template (parser
, 0);
23901 decl
= finish_fully_implicit_template (parser
, decl
);
23905 cp_finalize_omp_declare_simd (parser
, decl
);
23906 cp_finalize_oacc_routine (parser
, decl
, false);
23908 /* Reset PREFIX_ATTRIBUTES. */
23909 if (attributes
!= error_mark_node
)
23911 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23912 attributes
= TREE_CHAIN (attributes
);
23914 TREE_CHAIN (attributes
) = NULL_TREE
;
23917 /* If there is any qualification still in effect, clear it
23918 now; we will be starting fresh with the next declarator. */
23919 parser
->scope
= NULL_TREE
;
23920 parser
->qualifying_scope
= NULL_TREE
;
23921 parser
->object_scope
= NULL_TREE
;
23922 /* If it's a `,', then there are more declarators. */
23923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23925 cp_lexer_consume_token (parser
->lexer
);
23926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23928 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23929 gcc_rich_location
richloc (token
->location
);
23930 richloc
.add_fixit_remove ();
23931 error_at (&richloc
, "stray %<,%> at end of "
23932 "member declaration");
23935 /* If the next token isn't a `;', then we have a parse error. */
23936 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23939 /* The next token might be a ways away from where the
23940 actual semicolon is missing. Find the previous token
23941 and use that for our error position. */
23942 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23943 gcc_rich_location
richloc (token
->location
);
23944 richloc
.add_fixit_insert_after (";");
23945 error_at (&richloc
, "expected %<;%> at end of "
23946 "member declaration");
23948 /* Assume that the user meant to provide a semicolon. If
23949 we were to cp_parser_skip_to_end_of_statement, we might
23950 skip to a semicolon inside a member function definition
23951 and issue nonsensical error messages. */
23952 assume_semicolon
= true;
23957 /* Add DECL to the list of members. */
23959 /* Explicitly include, eg, NSDMIs, for better error
23960 recovery (c++/58650). */
23961 || !DECL_DECLARES_FUNCTION_P (decl
))
23962 finish_member_declaration (decl
);
23964 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23965 cp_parser_save_default_args (parser
, decl
);
23966 else if (TREE_CODE (decl
) == FIELD_DECL
23967 && DECL_INITIAL (decl
))
23968 /* Add DECL to the queue of NSDMI to be parsed later. */
23969 vec_safe_push (unparsed_nsdmis
, decl
);
23972 if (assume_semicolon
)
23977 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23979 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23982 /* Parse a pure-specifier.
23987 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23988 Otherwise, ERROR_MARK_NODE is returned. */
23991 cp_parser_pure_specifier (cp_parser
* parser
)
23995 /* Look for the `=' token. */
23996 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23997 return error_mark_node
;
23998 /* Look for the `0' token. */
23999 token
= cp_lexer_peek_token (parser
->lexer
);
24001 if (token
->type
== CPP_EOF
24002 || token
->type
== CPP_PRAGMA_EOL
)
24003 return error_mark_node
;
24005 cp_lexer_consume_token (parser
->lexer
);
24007 /* Accept = default or = delete in c++0x mode. */
24008 if (token
->keyword
== RID_DEFAULT
24009 || token
->keyword
== RID_DELETE
)
24011 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24012 return token
->u
.value
;
24015 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24016 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24018 cp_parser_error (parser
,
24019 "invalid pure specifier (only %<= 0%> is allowed)");
24020 cp_parser_skip_to_end_of_statement (parser
);
24021 return error_mark_node
;
24023 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24025 error_at (token
->location
, "templates may not be %<virtual%>");
24026 return error_mark_node
;
24029 return integer_zero_node
;
24032 /* Parse a constant-initializer.
24034 constant-initializer:
24035 = constant-expression
24037 Returns a representation of the constant-expression. */
24040 cp_parser_constant_initializer (cp_parser
* parser
)
24042 /* Look for the `=' token. */
24043 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24044 return error_mark_node
;
24046 /* It is invalid to write:
24048 struct S { static const int i = { 7 }; };
24051 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24053 cp_parser_error (parser
,
24054 "a brace-enclosed initializer is not allowed here");
24055 /* Consume the opening brace. */
24056 matching_braces braces
;
24057 braces
.consume_open (parser
);
24058 /* Skip the initializer. */
24059 cp_parser_skip_to_closing_brace (parser
);
24060 /* Look for the trailing `}'. */
24061 braces
.require_close (parser
);
24063 return error_mark_node
;
24066 return cp_parser_constant_expression (parser
);
24069 /* Derived classes [gram.class.derived] */
24071 /* Parse a base-clause.
24074 : base-specifier-list
24076 base-specifier-list:
24077 base-specifier ... [opt]
24078 base-specifier-list , base-specifier ... [opt]
24080 Returns a TREE_LIST representing the base-classes, in the order in
24081 which they were declared. The representation of each node is as
24082 described by cp_parser_base_specifier.
24084 In the case that no bases are specified, this function will return
24085 NULL_TREE, not ERROR_MARK_NODE. */
24088 cp_parser_base_clause (cp_parser
* parser
)
24090 tree bases
= NULL_TREE
;
24092 /* Look for the `:' that begins the list. */
24093 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24095 /* Scan the base-specifier-list. */
24100 bool pack_expansion_p
= false;
24102 /* Look for the base-specifier. */
24103 base
= cp_parser_base_specifier (parser
);
24104 /* Look for the (optional) ellipsis. */
24105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24107 /* Consume the `...'. */
24108 cp_lexer_consume_token (parser
->lexer
);
24110 pack_expansion_p
= true;
24113 /* Add BASE to the front of the list. */
24114 if (base
&& base
!= error_mark_node
)
24116 if (pack_expansion_p
)
24117 /* Make this a pack expansion type. */
24118 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24120 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24122 TREE_CHAIN (base
) = bases
;
24126 /* Peek at the next token. */
24127 token
= cp_lexer_peek_token (parser
->lexer
);
24128 /* If it's not a comma, then the list is complete. */
24129 if (token
->type
!= CPP_COMMA
)
24131 /* Consume the `,'. */
24132 cp_lexer_consume_token (parser
->lexer
);
24135 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24136 base class had a qualified name. However, the next name that
24137 appears is certainly not qualified. */
24138 parser
->scope
= NULL_TREE
;
24139 parser
->qualifying_scope
= NULL_TREE
;
24140 parser
->object_scope
= NULL_TREE
;
24142 return nreverse (bases
);
24145 /* Parse a base-specifier.
24148 :: [opt] nested-name-specifier [opt] class-name
24149 virtual access-specifier [opt] :: [opt] nested-name-specifier
24151 access-specifier virtual [opt] :: [opt] nested-name-specifier
24154 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24155 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24156 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24157 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24160 cp_parser_base_specifier (cp_parser
* parser
)
24164 bool virtual_p
= false;
24165 bool duplicate_virtual_error_issued_p
= false;
24166 bool duplicate_access_error_issued_p
= false;
24167 bool class_scope_p
, template_p
;
24168 tree access
= access_default_node
;
24171 /* Process the optional `virtual' and `access-specifier'. */
24174 /* Peek at the next token. */
24175 token
= cp_lexer_peek_token (parser
->lexer
);
24176 /* Process `virtual'. */
24177 switch (token
->keyword
)
24180 /* If `virtual' appears more than once, issue an error. */
24181 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24183 cp_parser_error (parser
,
24184 "%<virtual%> specified more than once in base-specifier");
24185 duplicate_virtual_error_issued_p
= true;
24190 /* Consume the `virtual' token. */
24191 cp_lexer_consume_token (parser
->lexer
);
24196 case RID_PROTECTED
:
24198 /* If more than one access specifier appears, issue an
24200 if (access
!= access_default_node
24201 && !duplicate_access_error_issued_p
)
24203 cp_parser_error (parser
,
24204 "more than one access specifier in base-specifier");
24205 duplicate_access_error_issued_p
= true;
24208 access
= ridpointers
[(int) token
->keyword
];
24210 /* Consume the access-specifier. */
24211 cp_lexer_consume_token (parser
->lexer
);
24220 /* It is not uncommon to see programs mechanically, erroneously, use
24221 the 'typename' keyword to denote (dependent) qualified types
24222 as base classes. */
24223 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24225 token
= cp_lexer_peek_token (parser
->lexer
);
24226 if (!processing_template_decl
)
24227 error_at (token
->location
,
24228 "keyword %<typename%> not allowed outside of templates");
24230 error_at (token
->location
,
24231 "keyword %<typename%> not allowed in this context "
24232 "(the base class is implicitly a type)");
24233 cp_lexer_consume_token (parser
->lexer
);
24236 /* Look for the optional `::' operator. */
24237 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24238 /* Look for the nested-name-specifier. The simplest way to
24243 The keyword `typename' is not permitted in a base-specifier or
24244 mem-initializer; in these contexts a qualified name that
24245 depends on a template-parameter is implicitly assumed to be a
24248 is to pretend that we have seen the `typename' keyword at this
24250 cp_parser_nested_name_specifier_opt (parser
,
24251 /*typename_keyword_p=*/true,
24252 /*check_dependency_p=*/true,
24254 /*is_declaration=*/true);
24255 /* If the base class is given by a qualified name, assume that names
24256 we see are type names or templates, as appropriate. */
24257 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24258 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24261 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24262 /* DR 950 allows decltype as a base-specifier. */
24263 type
= cp_parser_decltype (parser
);
24266 /* Otherwise, look for the class-name. */
24267 type
= cp_parser_class_name (parser
,
24271 /*check_dependency_p=*/true,
24272 /*class_head_p=*/false,
24273 /*is_declaration=*/true);
24274 type
= TREE_TYPE (type
);
24277 if (type
== error_mark_node
)
24278 return error_mark_node
;
24280 return finish_base_specifier (type
, access
, virtual_p
);
24283 /* Exception handling [gram.exception] */
24285 /* Parse an (optional) noexcept-specification.
24287 noexcept-specification:
24288 noexcept ( constant-expression ) [opt]
24290 If no noexcept-specification is present, returns NULL_TREE.
24291 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24292 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24293 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24294 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24295 in which case a boolean condition is returned instead. */
24298 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24299 bool require_constexpr
,
24300 bool* consumed_expr
,
24304 const char *saved_message
;
24306 /* Peek at the next token. */
24307 token
= cp_lexer_peek_token (parser
->lexer
);
24309 /* Is it a noexcept-specification? */
24310 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24313 cp_lexer_consume_token (parser
->lexer
);
24315 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24317 matching_parens parens
;
24318 parens
.consume_open (parser
);
24320 if (require_constexpr
)
24322 /* Types may not be defined in an exception-specification. */
24323 saved_message
= parser
->type_definition_forbidden_message
;
24324 parser
->type_definition_forbidden_message
24325 = G_("types may not be defined in an exception-specification");
24327 expr
= cp_parser_constant_expression (parser
);
24329 /* Restore the saved message. */
24330 parser
->type_definition_forbidden_message
= saved_message
;
24334 expr
= cp_parser_expression (parser
);
24335 *consumed_expr
= true;
24338 parens
.require_close (parser
);
24342 expr
= boolean_true_node
;
24343 if (!require_constexpr
)
24344 *consumed_expr
= false;
24347 /* We cannot build a noexcept-spec right away because this will check
24348 that expr is a constexpr. */
24350 return build_noexcept_spec (expr
, tf_warning_or_error
);
24358 /* Parse an (optional) exception-specification.
24360 exception-specification:
24361 throw ( type-id-list [opt] )
24363 Returns a TREE_LIST representing the exception-specification. The
24364 TREE_VALUE of each node is a type. */
24367 cp_parser_exception_specification_opt (cp_parser
* parser
)
24371 const char *saved_message
;
24373 /* Peek at the next token. */
24374 token
= cp_lexer_peek_token (parser
->lexer
);
24376 /* Is it a noexcept-specification? */
24377 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24379 if (type_id_list
!= NULL_TREE
)
24380 return type_id_list
;
24382 /* If it's not `throw', then there's no exception-specification. */
24383 if (!cp_parser_is_keyword (token
, RID_THROW
))
24386 location_t loc
= token
->location
;
24388 /* Consume the `throw'. */
24389 cp_lexer_consume_token (parser
->lexer
);
24391 /* Look for the `('. */
24392 matching_parens parens
;
24393 parens
.require_open (parser
);
24395 /* Peek at the next token. */
24396 token
= cp_lexer_peek_token (parser
->lexer
);
24397 /* If it's not a `)', then there is a type-id-list. */
24398 if (token
->type
!= CPP_CLOSE_PAREN
)
24400 /* Types may not be defined in an exception-specification. */
24401 saved_message
= parser
->type_definition_forbidden_message
;
24402 parser
->type_definition_forbidden_message
24403 = G_("types may not be defined in an exception-specification");
24404 /* Parse the type-id-list. */
24405 type_id_list
= cp_parser_type_id_list (parser
);
24406 /* Restore the saved message. */
24407 parser
->type_definition_forbidden_message
= saved_message
;
24409 if (cxx_dialect
>= cxx17
)
24411 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24413 type_id_list
= NULL_TREE
;
24415 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24416 warning_at (loc
, OPT_Wdeprecated
,
24417 "dynamic exception specifications are deprecated in "
24420 /* In C++17, throw() is equivalent to noexcept (true). throw()
24421 is deprecated in C++11 and above as well, but is still widely used,
24422 so don't warn about it yet. */
24423 else if (cxx_dialect
>= cxx17
)
24424 type_id_list
= noexcept_true_spec
;
24426 type_id_list
= empty_except_spec
;
24428 /* Look for the `)'. */
24429 parens
.require_close (parser
);
24431 return type_id_list
;
24434 /* Parse an (optional) type-id-list.
24438 type-id-list , type-id ... [opt]
24440 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24441 in the order that the types were presented. */
24444 cp_parser_type_id_list (cp_parser
* parser
)
24446 tree types
= NULL_TREE
;
24453 token
= cp_lexer_peek_token (parser
->lexer
);
24455 /* Get the next type-id. */
24456 type
= cp_parser_type_id (parser
);
24457 /* Check for invalid 'auto'. */
24458 if (flag_concepts
&& type_uses_auto (type
))
24460 error_at (token
->location
,
24461 "invalid use of %<auto%> in exception-specification");
24462 type
= error_mark_node
;
24464 /* Parse the optional ellipsis. */
24465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24467 /* Consume the `...'. */
24468 cp_lexer_consume_token (parser
->lexer
);
24470 /* Turn the type into a pack expansion expression. */
24471 type
= make_pack_expansion (type
);
24473 /* Add it to the list. */
24474 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24475 /* Peek at the next token. */
24476 token
= cp_lexer_peek_token (parser
->lexer
);
24477 /* If it is not a `,', we are done. */
24478 if (token
->type
!= CPP_COMMA
)
24480 /* Consume the `,'. */
24481 cp_lexer_consume_token (parser
->lexer
);
24484 return nreverse (types
);
24487 /* Parse a try-block.
24490 try compound-statement handler-seq */
24493 cp_parser_try_block (cp_parser
* parser
)
24497 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24498 if (parser
->in_function_body
24499 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24500 error ("%<try%> in %<constexpr%> function");
24502 try_block
= begin_try_block ();
24503 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24504 finish_try_block (try_block
);
24505 cp_parser_handler_seq (parser
);
24506 finish_handler_sequence (try_block
);
24511 /* Parse a function-try-block.
24513 function-try-block:
24514 try ctor-initializer [opt] function-body handler-seq */
24517 cp_parser_function_try_block (cp_parser
* parser
)
24519 tree compound_stmt
;
24522 /* Look for the `try' keyword. */
24523 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24525 /* Let the rest of the front end know where we are. */
24526 try_block
= begin_function_try_block (&compound_stmt
);
24527 /* Parse the function-body. */
24528 cp_parser_ctor_initializer_opt_and_function_body
24529 (parser
, /*in_function_try_block=*/true);
24530 /* We're done with the `try' part. */
24531 finish_function_try_block (try_block
);
24532 /* Parse the handlers. */
24533 cp_parser_handler_seq (parser
);
24534 /* We're done with the handlers. */
24535 finish_function_handler_sequence (try_block
, compound_stmt
);
24538 /* Parse a handler-seq.
24541 handler handler-seq [opt] */
24544 cp_parser_handler_seq (cp_parser
* parser
)
24550 /* Parse the handler. */
24551 cp_parser_handler (parser
);
24552 /* Peek at the next token. */
24553 token
= cp_lexer_peek_token (parser
->lexer
);
24554 /* If it's not `catch' then there are no more handlers. */
24555 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24560 /* Parse a handler.
24563 catch ( exception-declaration ) compound-statement */
24566 cp_parser_handler (cp_parser
* parser
)
24571 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24572 handler
= begin_handler ();
24573 matching_parens parens
;
24574 parens
.require_open (parser
);
24575 declaration
= cp_parser_exception_declaration (parser
);
24576 finish_handler_parms (declaration
, handler
);
24577 parens
.require_close (parser
);
24578 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24579 finish_handler (handler
);
24582 /* Parse an exception-declaration.
24584 exception-declaration:
24585 type-specifier-seq declarator
24586 type-specifier-seq abstract-declarator
24590 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24591 ellipsis variant is used. */
24594 cp_parser_exception_declaration (cp_parser
* parser
)
24596 cp_decl_specifier_seq type_specifiers
;
24597 cp_declarator
*declarator
;
24598 const char *saved_message
;
24600 /* If it's an ellipsis, it's easy to handle. */
24601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24603 /* Consume the `...' token. */
24604 cp_lexer_consume_token (parser
->lexer
);
24608 /* Types may not be defined in exception-declarations. */
24609 saved_message
= parser
->type_definition_forbidden_message
;
24610 parser
->type_definition_forbidden_message
24611 = G_("types may not be defined in exception-declarations");
24613 /* Parse the type-specifier-seq. */
24614 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24615 /*is_trailing_return=*/false,
24617 /* If it's a `)', then there is no declarator. */
24618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24621 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24622 /*ctor_dtor_or_conv_p=*/NULL
,
24623 /*parenthesized_p=*/NULL
,
24624 /*member_p=*/false,
24625 /*friend_p=*/false);
24627 /* Restore the saved message. */
24628 parser
->type_definition_forbidden_message
= saved_message
;
24630 if (!type_specifiers
.any_specifiers_p
)
24631 return error_mark_node
;
24633 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24636 /* Parse a throw-expression.
24639 throw assignment-expression [opt]
24641 Returns a THROW_EXPR representing the throw-expression. */
24644 cp_parser_throw_expression (cp_parser
* parser
)
24649 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24650 token
= cp_lexer_peek_token (parser
->lexer
);
24651 /* Figure out whether or not there is an assignment-expression
24652 following the "throw" keyword. */
24653 if (token
->type
== CPP_COMMA
24654 || token
->type
== CPP_SEMICOLON
24655 || token
->type
== CPP_CLOSE_PAREN
24656 || token
->type
== CPP_CLOSE_SQUARE
24657 || token
->type
== CPP_CLOSE_BRACE
24658 || token
->type
== CPP_COLON
)
24659 expression
= NULL_TREE
;
24661 expression
= cp_parser_assignment_expression (parser
);
24663 return build_throw (expression
);
24666 /* GNU Extensions */
24668 /* Parse an (optional) asm-specification.
24671 asm ( string-literal )
24673 If the asm-specification is present, returns a STRING_CST
24674 corresponding to the string-literal. Otherwise, returns
24678 cp_parser_asm_specification_opt (cp_parser
* parser
)
24681 tree asm_specification
;
24683 /* Peek at the next token. */
24684 token
= cp_lexer_peek_token (parser
->lexer
);
24685 /* If the next token isn't the `asm' keyword, then there's no
24686 asm-specification. */
24687 if (!cp_parser_is_keyword (token
, RID_ASM
))
24690 /* Consume the `asm' token. */
24691 cp_lexer_consume_token (parser
->lexer
);
24692 /* Look for the `('. */
24693 matching_parens parens
;
24694 parens
.require_open (parser
);
24696 /* Look for the string-literal. */
24697 asm_specification
= cp_parser_string_literal (parser
, false, false);
24699 /* Look for the `)'. */
24700 parens
.require_close (parser
);
24702 return asm_specification
;
24705 /* Parse an asm-operand-list.
24709 asm-operand-list , asm-operand
24712 string-literal ( expression )
24713 [ string-literal ] string-literal ( expression )
24715 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24716 each node is the expression. The TREE_PURPOSE is itself a
24717 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24718 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24719 is a STRING_CST for the string literal before the parenthesis. Returns
24720 ERROR_MARK_NODE if any of the operands are invalid. */
24723 cp_parser_asm_operand_list (cp_parser
* parser
)
24725 tree asm_operands
= NULL_TREE
;
24726 bool invalid_operands
= false;
24730 tree string_literal
;
24734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24736 /* Consume the `[' token. */
24737 cp_lexer_consume_token (parser
->lexer
);
24738 /* Read the operand name. */
24739 name
= cp_parser_identifier (parser
);
24740 if (name
!= error_mark_node
)
24741 name
= build_string (IDENTIFIER_LENGTH (name
),
24742 IDENTIFIER_POINTER (name
));
24743 /* Look for the closing `]'. */
24744 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24748 /* Look for the string-literal. */
24749 string_literal
= cp_parser_string_literal (parser
, false, false);
24751 /* Look for the `('. */
24752 matching_parens parens
;
24753 parens
.require_open (parser
);
24754 /* Parse the expression. */
24755 expression
= cp_parser_expression (parser
);
24756 /* Look for the `)'. */
24757 parens
.require_close (parser
);
24759 if (name
== error_mark_node
24760 || string_literal
== error_mark_node
24761 || expression
== error_mark_node
)
24762 invalid_operands
= true;
24764 /* Add this operand to the list. */
24765 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24768 /* If the next token is not a `,', there are no more
24770 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24772 /* Consume the `,'. */
24773 cp_lexer_consume_token (parser
->lexer
);
24776 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24779 /* Parse an asm-clobber-list.
24783 asm-clobber-list , string-literal
24785 Returns a TREE_LIST, indicating the clobbers in the order that they
24786 appeared. The TREE_VALUE of each node is a STRING_CST. */
24789 cp_parser_asm_clobber_list (cp_parser
* parser
)
24791 tree clobbers
= NULL_TREE
;
24795 tree string_literal
;
24797 /* Look for the string literal. */
24798 string_literal
= cp_parser_string_literal (parser
, false, false);
24799 /* Add it to the list. */
24800 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24801 /* If the next token is not a `,', then the list is
24803 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24805 /* Consume the `,' token. */
24806 cp_lexer_consume_token (parser
->lexer
);
24812 /* Parse an asm-label-list.
24816 asm-label-list , identifier
24818 Returns a TREE_LIST, indicating the labels in the order that they
24819 appeared. The TREE_VALUE of each node is a label. */
24822 cp_parser_asm_label_list (cp_parser
* parser
)
24824 tree labels
= NULL_TREE
;
24828 tree identifier
, label
, name
;
24830 /* Look for the identifier. */
24831 identifier
= cp_parser_identifier (parser
);
24832 if (!error_operand_p (identifier
))
24834 label
= lookup_label (identifier
);
24835 if (TREE_CODE (label
) == LABEL_DECL
)
24837 TREE_USED (label
) = 1;
24838 check_goto (label
);
24839 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24840 IDENTIFIER_POINTER (identifier
));
24841 labels
= tree_cons (name
, label
, labels
);
24844 /* If the next token is not a `,', then the list is
24846 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24848 /* Consume the `,' token. */
24849 cp_lexer_consume_token (parser
->lexer
);
24852 return nreverse (labels
);
24855 /* Return TRUE iff the next tokens in the stream are possibly the
24856 beginning of a GNU extension attribute. */
24859 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24861 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24864 /* Return TRUE iff the next tokens in the stream are possibly the
24865 beginning of a standard C++-11 attribute specifier. */
24868 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24870 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24873 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24874 beginning of a standard C++-11 attribute specifier. */
24877 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24879 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24881 return (cxx_dialect
>= cxx11
24882 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24883 || (token
->type
== CPP_OPEN_SQUARE
24884 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24885 && token
->type
== CPP_OPEN_SQUARE
)));
24888 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24889 beginning of a GNU extension attribute. */
24892 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24894 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24896 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24899 /* Return true iff the next tokens can be the beginning of either a
24900 GNU attribute list, or a standard C++11 attribute sequence. */
24903 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24905 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24906 || cp_next_tokens_can_be_std_attribute_p (parser
));
24909 /* Return true iff the next Nth tokens can be the beginning of either
24910 a GNU attribute list, or a standard C++11 attribute sequence. */
24913 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24915 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24916 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24919 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24920 of GNU attributes, or return NULL. */
24923 cp_parser_attributes_opt (cp_parser
*parser
)
24925 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24926 return cp_parser_gnu_attributes_opt (parser
);
24927 return cp_parser_std_attribute_spec_seq (parser
);
24930 /* Parse an (optional) series of attributes.
24933 attributes attribute
24936 __attribute__ (( attribute-list [opt] ))
24938 The return value is as for cp_parser_gnu_attribute_list. */
24941 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24943 tree attributes
= NULL_TREE
;
24948 tree attribute_list
;
24951 /* Peek at the next token. */
24952 token
= cp_lexer_peek_token (parser
->lexer
);
24953 /* If it's not `__attribute__', then we're done. */
24954 if (token
->keyword
!= RID_ATTRIBUTE
)
24957 /* Consume the `__attribute__' keyword. */
24958 cp_lexer_consume_token (parser
->lexer
);
24959 /* Look for the two `(' tokens. */
24960 matching_parens outer_parens
;
24961 outer_parens
.require_open (parser
);
24962 matching_parens inner_parens
;
24963 inner_parens
.require_open (parser
);
24965 /* Peek at the next token. */
24966 token
= cp_lexer_peek_token (parser
->lexer
);
24967 if (token
->type
!= CPP_CLOSE_PAREN
)
24968 /* Parse the attribute-list. */
24969 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24971 /* If the next token is a `)', then there is no attribute
24973 attribute_list
= NULL
;
24975 /* Look for the two `)' tokens. */
24976 if (!inner_parens
.require_close (parser
))
24978 if (!outer_parens
.require_close (parser
))
24981 cp_parser_skip_to_end_of_statement (parser
);
24983 /* Add these new attributes to the list. */
24984 attributes
= attr_chainon (attributes
, attribute_list
);
24990 /* Parse a GNU attribute-list.
24994 attribute-list , attribute
24998 identifier ( identifier )
24999 identifier ( identifier , expression-list )
25000 identifier ( expression-list )
25002 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25003 to an attribute. The TREE_PURPOSE of each node is the identifier
25004 indicating which attribute is in use. The TREE_VALUE represents
25005 the arguments, if any. */
25008 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25010 tree attribute_list
= NULL_TREE
;
25011 bool save_translate_strings_p
= parser
->translate_strings_p
;
25013 parser
->translate_strings_p
= false;
25020 /* Look for the identifier. We also allow keywords here; for
25021 example `__attribute__ ((const))' is legal. */
25022 token
= cp_lexer_peek_token (parser
->lexer
);
25023 if (token
->type
== CPP_NAME
25024 || token
->type
== CPP_KEYWORD
)
25026 tree arguments
= NULL_TREE
;
25028 /* Consume the token, but save it since we need it for the
25029 SIMD enabled function parsing. */
25030 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25032 /* Save away the identifier that indicates which attribute
25034 identifier
= (token
->type
== CPP_KEYWORD
)
25035 /* For keywords, use the canonical spelling, not the
25036 parsed identifier. */
25037 ? ridpointers
[(int) token
->keyword
]
25038 : id_token
->u
.value
;
25040 identifier
= canonicalize_attr_name (identifier
);
25041 attribute
= build_tree_list (identifier
, NULL_TREE
);
25043 /* Peek at the next token. */
25044 token
= cp_lexer_peek_token (parser
->lexer
);
25045 /* If it's an `(', then parse the attribute arguments. */
25046 if (token
->type
== CPP_OPEN_PAREN
)
25048 vec
<tree
, va_gc
> *vec
;
25049 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25050 ? id_attr
: normal_attr
);
25051 vec
= cp_parser_parenthesized_expression_list
25052 (parser
, attr_flag
, /*cast_p=*/false,
25053 /*allow_expansion_p=*/false,
25054 /*non_constant_p=*/NULL
);
25056 arguments
= error_mark_node
;
25059 arguments
= build_tree_list_vec (vec
);
25060 release_tree_vector (vec
);
25062 /* Save the arguments away. */
25063 TREE_VALUE (attribute
) = arguments
;
25066 if (arguments
!= error_mark_node
)
25068 /* Add this attribute to the list. */
25069 TREE_CHAIN (attribute
) = attribute_list
;
25070 attribute_list
= attribute
;
25073 token
= cp_lexer_peek_token (parser
->lexer
);
25075 /* Now, look for more attributes. If the next token isn't a
25076 `,', we're done. */
25077 if (token
->type
!= CPP_COMMA
)
25080 /* Consume the comma and keep going. */
25081 cp_lexer_consume_token (parser
->lexer
);
25083 parser
->translate_strings_p
= save_translate_strings_p
;
25085 /* We built up the list in reverse order. */
25086 return nreverse (attribute_list
);
25089 /* Parse a standard C++11 attribute.
25091 The returned representation is a TREE_LIST which TREE_PURPOSE is
25092 the scoped name of the attribute, and the TREE_VALUE is its
25095 Note that the scoped name of the attribute is itself a TREE_LIST
25096 which TREE_PURPOSE is the namespace of the attribute, and
25097 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25098 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25099 and which TREE_PURPOSE is directly the attribute name.
25101 Clients of the attribute code should use get_attribute_namespace
25102 and get_attribute_name to get the actual namespace and name of
25103 attributes, regardless of their being GNU or C++11 attributes.
25106 attribute-token attribute-argument-clause [opt]
25110 attribute-scoped-token
25112 attribute-scoped-token:
25113 attribute-namespace :: identifier
25115 attribute-namespace:
25118 attribute-argument-clause:
25119 ( balanced-token-seq )
25121 balanced-token-seq:
25122 balanced-token [opt]
25123 balanced-token-seq balanced-token
25126 ( balanced-token-seq )
25127 [ balanced-token-seq ]
25128 { balanced-token-seq }. */
25131 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25133 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25136 /* First, parse name of the attribute, a.k.a attribute-token. */
25138 token
= cp_lexer_peek_token (parser
->lexer
);
25139 if (token
->type
== CPP_NAME
)
25140 attr_id
= token
->u
.value
;
25141 else if (token
->type
== CPP_KEYWORD
)
25142 attr_id
= ridpointers
[(int) token
->keyword
];
25143 else if (token
->flags
& NAMED_OP
)
25144 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25146 if (attr_id
== NULL_TREE
)
25149 cp_lexer_consume_token (parser
->lexer
);
25151 token
= cp_lexer_peek_token (parser
->lexer
);
25152 if (token
->type
== CPP_SCOPE
)
25154 /* We are seeing a scoped attribute token. */
25156 cp_lexer_consume_token (parser
->lexer
);
25158 error_at (token
->location
, "attribute using prefix used together "
25159 "with scoped attribute token");
25162 token
= cp_lexer_consume_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
));
25171 error_at (token
->location
,
25172 "expected an identifier for the attribute name");
25173 return error_mark_node
;
25176 attr_id
= canonicalize_attr_name (attr_id
);
25177 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25179 token
= cp_lexer_peek_token (parser
->lexer
);
25182 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25186 attr_id
= canonicalize_attr_name (attr_id
);
25187 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25189 /* C++11 noreturn attribute is equivalent to GNU's. */
25190 if (is_attribute_p ("noreturn", attr_id
))
25191 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25192 /* C++14 deprecated attribute is equivalent to GNU's. */
25193 else if (is_attribute_p ("deprecated", attr_id
))
25194 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25195 /* C++17 fallthrough attribute is equivalent to GNU's. */
25196 else if (is_attribute_p ("fallthrough", attr_id
))
25197 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25198 /* Transactional Memory TS optimize_for_synchronized attribute is
25199 equivalent to GNU transaction_callable. */
25200 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25201 TREE_PURPOSE (attribute
)
25202 = get_identifier ("transaction_callable");
25203 /* Transactional Memory attributes are GNU attributes. */
25204 else if (tm_attr_to_mask (attr_id
))
25205 TREE_PURPOSE (attribute
) = attr_id
;
25208 /* Now parse the optional argument clause of the attribute. */
25210 if (token
->type
!= CPP_OPEN_PAREN
)
25214 vec
<tree
, va_gc
> *vec
;
25215 int attr_flag
= normal_attr
;
25217 if (attr_ns
== get_identifier ("gnu")
25218 && attribute_takes_identifier_p (attr_id
))
25219 /* A GNU attribute that takes an identifier in parameter. */
25220 attr_flag
= id_attr
;
25222 vec
= cp_parser_parenthesized_expression_list
25223 (parser
, attr_flag
, /*cast_p=*/false,
25224 /*allow_expansion_p=*/true,
25225 /*non_constant_p=*/NULL
);
25227 arguments
= error_mark_node
;
25230 arguments
= build_tree_list_vec (vec
);
25231 release_tree_vector (vec
);
25234 if (arguments
== error_mark_node
)
25235 attribute
= error_mark_node
;
25237 TREE_VALUE (attribute
) = arguments
;
25243 /* Check that the attribute ATTRIBUTE appears at most once in the
25244 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25245 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25246 isn't implemented yet in GCC. */
25249 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25253 tree name
= get_attribute_name (attribute
);
25254 if (is_attribute_p ("noreturn", name
)
25255 && lookup_attribute ("noreturn", attributes
))
25256 error ("attribute %<noreturn%> can appear at most once "
25257 "in an attribute-list");
25258 else if (is_attribute_p ("deprecated", name
)
25259 && lookup_attribute ("deprecated", attributes
))
25260 error ("attribute %<deprecated%> can appear at most once "
25261 "in an attribute-list");
25265 /* Parse a list of standard C++-11 attributes.
25269 attribute-list , attribute[opt]
25271 attribute-list , attribute ...
25275 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25277 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25278 cp_token
*token
= NULL
;
25282 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25283 if (attribute
== error_mark_node
)
25285 if (attribute
!= NULL_TREE
)
25287 cp_parser_check_std_attribute (attributes
, attribute
);
25288 TREE_CHAIN (attribute
) = attributes
;
25289 attributes
= attribute
;
25291 token
= cp_lexer_peek_token (parser
->lexer
);
25292 if (token
->type
== CPP_ELLIPSIS
)
25294 cp_lexer_consume_token (parser
->lexer
);
25295 if (attribute
== NULL_TREE
)
25296 error_at (token
->location
,
25297 "expected attribute before %<...%>");
25300 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25301 if (pack
== error_mark_node
)
25302 return error_mark_node
;
25303 TREE_VALUE (attribute
) = pack
;
25305 token
= cp_lexer_peek_token (parser
->lexer
);
25307 if (token
->type
!= CPP_COMMA
)
25309 cp_lexer_consume_token (parser
->lexer
);
25311 attributes
= nreverse (attributes
);
25315 /* Parse a standard C++-11 attribute specifier.
25317 attribute-specifier:
25318 [ [ attribute-using-prefix [opt] attribute-list ] ]
25319 alignment-specifier
25321 attribute-using-prefix:
25322 using attribute-namespace :
25324 alignment-specifier:
25325 alignas ( type-id ... [opt] )
25326 alignas ( alignment-expression ... [opt] ). */
25329 cp_parser_std_attribute_spec (cp_parser
*parser
)
25331 tree attributes
= NULL_TREE
;
25332 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25334 if (token
->type
== CPP_OPEN_SQUARE
25335 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25337 tree attr_ns
= NULL_TREE
;
25339 cp_lexer_consume_token (parser
->lexer
);
25340 cp_lexer_consume_token (parser
->lexer
);
25342 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25344 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25345 if (token
->type
== CPP_NAME
)
25346 attr_ns
= token
->u
.value
;
25347 else if (token
->type
== CPP_KEYWORD
)
25348 attr_ns
= ridpointers
[(int) token
->keyword
];
25349 else if (token
->flags
& NAMED_OP
)
25350 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25353 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25355 if (cxx_dialect
< cxx17
25356 && !in_system_header_at (input_location
))
25357 pedwarn (input_location
, 0,
25358 "attribute using prefix only available "
25359 "with -std=c++17 or -std=gnu++17");
25361 cp_lexer_consume_token (parser
->lexer
);
25362 cp_lexer_consume_token (parser
->lexer
);
25363 cp_lexer_consume_token (parser
->lexer
);
25366 attr_ns
= NULL_TREE
;
25369 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25371 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25372 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25373 cp_parser_skip_to_end_of_statement (parser
);
25375 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25376 when we are sure that we have actually parsed them. */
25377 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25383 /* Look for an alignment-specifier. */
25385 token
= cp_lexer_peek_token (parser
->lexer
);
25387 if (token
->type
!= CPP_KEYWORD
25388 || token
->keyword
!= RID_ALIGNAS
)
25391 cp_lexer_consume_token (parser
->lexer
);
25392 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25394 matching_parens parens
;
25395 if (!parens
.require_open (parser
))
25396 return error_mark_node
;
25398 cp_parser_parse_tentatively (parser
);
25399 alignas_expr
= cp_parser_type_id (parser
);
25401 if (!cp_parser_parse_definitely (parser
))
25403 alignas_expr
= cp_parser_assignment_expression (parser
);
25404 if (alignas_expr
== error_mark_node
)
25405 cp_parser_skip_to_end_of_statement (parser
);
25406 if (alignas_expr
== NULL_TREE
25407 || alignas_expr
== error_mark_node
)
25408 return alignas_expr
;
25411 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25412 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25414 /* Handle alignas (pack...). */
25415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25417 cp_lexer_consume_token (parser
->lexer
);
25418 alignas_expr
= make_pack_expansion (alignas_expr
);
25421 /* Something went wrong, so don't build the attribute. */
25422 if (alignas_expr
== error_mark_node
)
25423 return error_mark_node
;
25425 if (!parens
.require_close (parser
))
25426 return error_mark_node
;
25428 /* Build the C++-11 representation of an 'aligned'
25431 build_tree_list (build_tree_list (get_identifier ("gnu"),
25432 get_identifier ("aligned")),
25439 /* Parse a standard C++-11 attribute-specifier-seq.
25441 attribute-specifier-seq:
25442 attribute-specifier-seq [opt] attribute-specifier
25446 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25448 tree attr_specs
= NULL_TREE
;
25449 tree attr_last
= NULL_TREE
;
25453 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25454 if (attr_spec
== NULL_TREE
)
25456 if (attr_spec
== error_mark_node
)
25457 return error_mark_node
;
25460 TREE_CHAIN (attr_last
) = attr_spec
;
25462 attr_specs
= attr_last
= attr_spec
;
25463 attr_last
= tree_last (attr_last
);
25469 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25470 return index of the first token after balanced-token, or N on failure. */
25473 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25476 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25478 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25481 case CPP_PRAGMA_EOL
:
25482 /* Ran out of tokens. */
25484 case CPP_OPEN_PAREN
:
25487 case CPP_OPEN_BRACE
:
25490 case CPP_OPEN_SQUARE
:
25493 case CPP_CLOSE_PAREN
:
25496 case CPP_CLOSE_BRACE
:
25499 case CPP_CLOSE_SQUARE
:
25505 while (nparens
|| nbraces
|| nsquares
);
25509 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25510 return index of the first token after the GNU attribute tokens, or N on
25514 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25518 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25519 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25520 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25523 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25526 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25533 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25534 next token), return index of the first token after the standard C++11
25535 attribute tokens, or N on failure. */
25538 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25542 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25543 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25545 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25548 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25552 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25553 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25555 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25566 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25567 as the next token), return index of the first token after the attribute
25568 tokens, or N on failure. */
25571 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25573 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25574 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25575 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25578 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25579 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25580 current value of the PEDANTIC flag, regardless of whether or not
25581 the `__extension__' keyword is present. The caller is responsible
25582 for restoring the value of the PEDANTIC flag. */
25585 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25587 /* Save the old value of the PEDANTIC flag. */
25588 *saved_pedantic
= pedantic
;
25590 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25592 /* Consume the `__extension__' token. */
25593 cp_lexer_consume_token (parser
->lexer
);
25594 /* We're not being pedantic while the `__extension__' keyword is
25604 /* Parse a label declaration.
25607 __label__ label-declarator-seq ;
25609 label-declarator-seq:
25610 identifier , label-declarator-seq
25614 cp_parser_label_declaration (cp_parser
* parser
)
25616 /* Look for the `__label__' keyword. */
25617 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25623 /* Look for an identifier. */
25624 identifier
= cp_parser_identifier (parser
);
25625 /* If we failed, stop. */
25626 if (identifier
== error_mark_node
)
25628 /* Declare it as a label. */
25629 finish_label_decl (identifier
);
25630 /* If the next token is a `;', stop. */
25631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25633 /* Look for the `,' separating the label declarations. */
25634 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25637 /* Look for the final `;'. */
25638 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25641 // -------------------------------------------------------------------------- //
25644 // Parse a requires clause.
25646 // requires-clause:
25647 // 'requires' logical-or-expression
25649 // The required logical-or-expression must be a constant expression. Note
25650 // that we don't check that the expression is constepxr here. We defer until
25651 // we analyze constraints and then, we only check atomic constraints.
25653 cp_parser_requires_clause (cp_parser
*parser
)
25655 // Parse the requires clause so that it is not automatically folded.
25656 ++processing_template_decl
;
25657 tree expr
= cp_parser_binary_expression (parser
, false, false,
25658 PREC_NOT_OPERATOR
, NULL
);
25659 if (check_for_bare_parameter_packs (expr
))
25660 expr
= error_mark_node
;
25661 --processing_template_decl
;
25665 // Optionally parse a requires clause:
25667 cp_parser_requires_clause_opt (cp_parser
*parser
)
25669 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25670 if (tok
->keyword
!= RID_REQUIRES
)
25672 if (!flag_concepts
&& tok
->type
== CPP_NAME
25673 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25675 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25676 "%<requires%> only available with -fconcepts");
25677 /* Parse and discard the requires-clause. */
25678 cp_lexer_consume_token (parser
->lexer
);
25679 cp_parser_requires_clause (parser
);
25683 cp_lexer_consume_token (parser
->lexer
);
25684 return cp_parser_requires_clause (parser
);
25688 /*---------------------------------------------------------------------------
25689 Requires expressions
25690 ---------------------------------------------------------------------------*/
25692 /* Parse a requires expression
25694 requirement-expression:
25695 'requires' requirement-parameter-list [opt] requirement-body */
25697 cp_parser_requires_expression (cp_parser
*parser
)
25699 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25700 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25702 /* A requires-expression shall appear only within a concept
25703 definition or a requires-clause.
25705 TODO: Implement this diagnostic correctly. */
25706 if (!processing_template_decl
)
25708 error_at (loc
, "a requires expression cannot appear outside a template");
25709 cp_parser_skip_to_end_of_statement (parser
);
25710 return error_mark_node
;
25715 /* Local parameters are delared as variables within the scope
25716 of the expression. They are not visible past the end of
25717 the expression. Expressions within the requires-expression
25718 are unevaluated. */
25719 struct scope_sentinel
25723 ++cp_unevaluated_operand
;
25724 begin_scope (sk_block
, NULL_TREE
);
25729 pop_bindings_and_leave_scope ();
25730 --cp_unevaluated_operand
;
25734 /* Parse the optional parameter list. */
25735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25737 parms
= cp_parser_requirement_parameter_list (parser
);
25738 if (parms
== error_mark_node
)
25739 return error_mark_node
;
25744 /* Parse the requirement body. */
25745 reqs
= cp_parser_requirement_body (parser
);
25746 if (reqs
== error_mark_node
)
25747 return error_mark_node
;
25750 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25752 grokparms (parms
, &parms
);
25753 return finish_requires_expr (parms
, reqs
);
25756 /* Parse a parameterized requirement.
25758 requirement-parameter-list:
25759 '(' parameter-declaration-clause ')' */
25761 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25763 matching_parens parens
;
25764 if (!parens
.require_open (parser
))
25765 return error_mark_node
;
25767 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25769 if (!parens
.require_close (parser
))
25770 return error_mark_node
;
25775 /* Parse the body of a requirement.
25778 '{' requirement-list '}' */
25780 cp_parser_requirement_body (cp_parser
*parser
)
25782 matching_braces braces
;
25783 if (!braces
.require_open (parser
))
25784 return error_mark_node
;
25786 tree reqs
= cp_parser_requirement_list (parser
);
25788 if (!braces
.require_close (parser
))
25789 return error_mark_node
;
25794 /* Parse a list of requirements.
25798 requirement-list ';' requirement[opt] */
25800 cp_parser_requirement_list (cp_parser
*parser
)
25802 tree result
= NULL_TREE
;
25805 tree req
= cp_parser_requirement (parser
);
25806 if (req
== error_mark_node
)
25807 return error_mark_node
;
25809 result
= tree_cons (NULL_TREE
, req
, result
);
25811 /* If we see a semi-colon, consume it. */
25812 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25813 cp_lexer_consume_token (parser
->lexer
);
25815 /* Stop processing at the end of the list. */
25816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25820 /* Reverse the order of requirements so they are analyzed in
25821 declaration order. */
25822 return nreverse (result
);
25825 /* Parse a syntactic requirement or type requirement.
25829 compound-requirement
25831 nested-requirement */
25833 cp_parser_requirement (cp_parser
*parser
)
25835 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25836 return cp_parser_compound_requirement (parser
);
25837 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25838 return cp_parser_type_requirement (parser
);
25839 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25840 return cp_parser_nested_requirement (parser
);
25842 return cp_parser_simple_requirement (parser
);
25845 /* Parse a simple requirement.
25847 simple-requirement:
25850 cp_parser_simple_requirement (cp_parser
*parser
)
25852 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25853 if (!expr
|| expr
== error_mark_node
)
25854 return error_mark_node
;
25856 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25857 return error_mark_node
;
25859 return finish_simple_requirement (expr
);
25862 /* Parse a type requirement
25865 nested-name-specifier [opt] required-type-name ';'
25867 required-type-name:
25869 'template' [opt] simple-template-id */
25871 cp_parser_type_requirement (cp_parser
*parser
)
25873 cp_lexer_consume_token (parser
->lexer
);
25875 // Save the scope before parsing name specifiers.
25876 tree saved_scope
= parser
->scope
;
25877 tree saved_object_scope
= parser
->object_scope
;
25878 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25879 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25880 cp_parser_nested_name_specifier_opt (parser
,
25881 /*typename_keyword_p=*/true,
25882 /*check_dependency_p=*/false,
25884 /*is_declaration=*/false);
25887 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25889 cp_lexer_consume_token (parser
->lexer
);
25890 type
= cp_parser_template_id (parser
,
25891 /*template_keyword_p=*/true,
25892 /*check_dependency=*/false,
25893 /*tag_type=*/none_type
,
25894 /*is_declaration=*/false);
25895 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25896 /*complain=*/tf_error
);
25899 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25901 if (TREE_CODE (type
) == TYPE_DECL
)
25902 type
= TREE_TYPE (type
);
25904 parser
->scope
= saved_scope
;
25905 parser
->object_scope
= saved_object_scope
;
25906 parser
->qualifying_scope
= saved_qualifying_scope
;
25908 if (type
== error_mark_node
)
25909 cp_parser_skip_to_end_of_statement (parser
);
25911 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25912 return error_mark_node
;
25913 if (type
== error_mark_node
)
25914 return error_mark_node
;
25916 return finish_type_requirement (type
);
25919 /* Parse a compound requirement
25921 compound-requirement:
25922 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25924 cp_parser_compound_requirement (cp_parser
*parser
)
25926 /* Parse an expression enclosed in '{ }'s. */
25927 matching_braces braces
;
25928 if (!braces
.require_open (parser
))
25929 return error_mark_node
;
25931 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25932 if (!expr
|| expr
== error_mark_node
)
25933 return error_mark_node
;
25935 if (!braces
.require_close (parser
))
25936 return error_mark_node
;
25938 /* Parse the optional noexcept. */
25939 bool noexcept_p
= false;
25940 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25942 cp_lexer_consume_token (parser
->lexer
);
25946 /* Parse the optional trailing return type. */
25947 tree type
= NULL_TREE
;
25948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25950 cp_lexer_consume_token (parser
->lexer
);
25951 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25952 parser
->in_result_type_constraint_p
= true;
25953 type
= cp_parser_trailing_type_id (parser
);
25954 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25955 if (type
== error_mark_node
)
25956 return error_mark_node
;
25959 return finish_compound_requirement (expr
, type
, noexcept_p
);
25962 /* Parse a nested requirement. This is the same as a requires clause.
25964 nested-requirement:
25967 cp_parser_nested_requirement (cp_parser
*parser
)
25969 cp_lexer_consume_token (parser
->lexer
);
25970 tree req
= cp_parser_requires_clause (parser
);
25971 if (req
== error_mark_node
)
25972 return error_mark_node
;
25973 return finish_nested_requirement (req
);
25976 /* Support Functions */
25978 /* Return the appropriate prefer_type argument for lookup_name_real based on
25979 tag_type and template_mem_access. */
25982 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25984 /* DR 141: When looking in the current enclosing context for a template-name
25985 after -> or ., only consider class templates. */
25986 if (template_mem_access
)
25990 case none_type
: return 0; // No preference.
25991 case scope_type
: return 1; // Type or namespace.
25992 default: return 2; // Type only.
25996 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25997 NAME should have one of the representations used for an
25998 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25999 is returned. If PARSER->SCOPE is a dependent type, then a
26000 SCOPE_REF is returned.
26002 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26003 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26004 was formed. Abstractly, such entities should not be passed to this
26005 function, because they do not need to be looked up, but it is
26006 simpler to check for this special case here, rather than at the
26009 In cases not explicitly covered above, this function returns a
26010 DECL, OVERLOAD, or baselink representing the result of the lookup.
26011 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26014 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26015 (e.g., "struct") that was used. In that case bindings that do not
26016 refer to types are ignored.
26018 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26021 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26024 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26027 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26028 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26029 NULL_TREE otherwise. */
26032 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26033 enum tag_types tag_type
,
26036 bool check_dependency
,
26037 tree
*ambiguous_decls
,
26038 location_t name_location
)
26041 tree object_type
= parser
->context
->object_type
;
26043 /* Assume that the lookup will be unambiguous. */
26044 if (ambiguous_decls
)
26045 *ambiguous_decls
= NULL_TREE
;
26047 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26048 no longer valid. Note that if we are parsing tentatively, and
26049 the parse fails, OBJECT_TYPE will be automatically restored. */
26050 parser
->context
->object_type
= NULL_TREE
;
26052 if (name
== error_mark_node
)
26053 return error_mark_node
;
26055 /* A template-id has already been resolved; there is no lookup to
26057 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26059 if (BASELINK_P (name
))
26061 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26062 == TEMPLATE_ID_EXPR
);
26066 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26067 it should already have been checked to make sure that the name
26068 used matches the type being destroyed. */
26069 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26073 /* Figure out to which type this destructor applies. */
26075 type
= parser
->scope
;
26076 else if (object_type
)
26077 type
= object_type
;
26079 type
= current_class_type
;
26080 /* If that's not a class type, there is no destructor. */
26081 if (!type
|| !CLASS_TYPE_P (type
))
26082 return error_mark_node
;
26084 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26085 lazily_declare_fn (sfk_destructor
, type
);
26087 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26090 return error_mark_node
;
26093 /* By this point, the NAME should be an ordinary identifier. If
26094 the id-expression was a qualified name, the qualifying scope is
26095 stored in PARSER->SCOPE at this point. */
26096 gcc_assert (identifier_p (name
));
26098 /* Perform the lookup. */
26103 if (parser
->scope
== error_mark_node
)
26104 return error_mark_node
;
26106 /* If the SCOPE is dependent, the lookup must be deferred until
26107 the template is instantiated -- unless we are explicitly
26108 looking up names in uninstantiated templates. Even then, we
26109 cannot look up the name if the scope is not a class type; it
26110 might, for example, be a template type parameter. */
26111 dependent_p
= (TYPE_P (parser
->scope
)
26112 && dependent_scope_p (parser
->scope
));
26113 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26115 /* Defer lookup. */
26116 decl
= error_mark_node
;
26119 tree pushed_scope
= NULL_TREE
;
26121 /* If PARSER->SCOPE is a dependent type, then it must be a
26122 class type, and we must not be checking dependencies;
26123 otherwise, we would have processed this lookup above. So
26124 that PARSER->SCOPE is not considered a dependent base by
26125 lookup_member, we must enter the scope here. */
26127 pushed_scope
= push_scope (parser
->scope
);
26129 /* If the PARSER->SCOPE is a template specialization, it
26130 may be instantiated during name lookup. In that case,
26131 errors may be issued. Even if we rollback the current
26132 tentative parse, those errors are valid. */
26133 decl
= lookup_qualified_name (parser
->scope
, name
,
26134 prefer_type_arg (tag_type
),
26135 /*complain=*/true);
26137 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26138 lookup result and the nested-name-specifier nominates a class C:
26139 * if the name specified after the nested-name-specifier, when
26140 looked up in C, is the injected-class-name of C (Clause 9), or
26141 * if the name specified after the nested-name-specifier is the
26142 same as the identifier or the simple-template-id's template-
26143 name in the last component of the nested-name-specifier,
26144 the name is instead considered to name the constructor of
26145 class C. [ Note: for example, the constructor is not an
26146 acceptable lookup result in an elaborated-type-specifier so
26147 the constructor would not be used in place of the
26148 injected-class-name. --end note ] Such a constructor name
26149 shall be used only in the declarator-id of a declaration that
26150 names a constructor or in a using-declaration. */
26151 if (tag_type
== none_type
26152 && DECL_SELF_REFERENCE_P (decl
)
26153 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26154 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26155 prefer_type_arg (tag_type
),
26156 /*complain=*/true);
26158 /* If we have a single function from a using decl, pull it out. */
26159 if (TREE_CODE (decl
) == OVERLOAD
26160 && !really_overloaded_fn (decl
))
26161 decl
= OVL_FUNCTION (decl
);
26164 pop_scope (pushed_scope
);
26167 /* If the scope is a dependent type and either we deferred lookup or
26168 we did lookup but didn't find the name, rememeber the name. */
26169 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26170 && dependent_type_p (parser
->scope
))
26176 /* The resolution to Core Issue 180 says that `struct
26177 A::B' should be considered a type-name, even if `A'
26179 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26180 /*complain=*/tf_error
);
26181 if (type
!= error_mark_node
)
26182 decl
= TYPE_NAME (type
);
26184 else if (is_template
26185 && (cp_parser_next_token_ends_template_argument_p (parser
)
26186 || cp_lexer_next_token_is (parser
->lexer
,
26188 decl
= make_unbound_class_template (parser
->scope
,
26190 /*complain=*/tf_error
);
26192 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26193 parser
->scope
, name
,
26196 parser
->qualifying_scope
= parser
->scope
;
26197 parser
->object_scope
= NULL_TREE
;
26199 else if (object_type
)
26201 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26202 OBJECT_TYPE is not a class. */
26203 if (CLASS_TYPE_P (object_type
))
26204 /* If the OBJECT_TYPE is a template specialization, it may
26205 be instantiated during name lookup. In that case, errors
26206 may be issued. Even if we rollback the current tentative
26207 parse, those errors are valid. */
26208 decl
= lookup_member (object_type
,
26211 prefer_type_arg (tag_type
),
26212 tf_warning_or_error
);
26217 /* Look it up in the enclosing context. DR 141: When looking for a
26218 template-name after -> or ., only consider class templates. */
26219 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26221 /*block_p=*/true, is_namespace
, 0);
26222 if (object_type
== unknown_type_node
)
26223 /* The object is type-dependent, so we can't look anything up; we used
26224 this to get the DR 141 behavior. */
26225 object_type
= NULL_TREE
;
26226 parser
->object_scope
= object_type
;
26227 parser
->qualifying_scope
= NULL_TREE
;
26231 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26233 /*block_p=*/true, is_namespace
, 0);
26234 parser
->qualifying_scope
= NULL_TREE
;
26235 parser
->object_scope
= NULL_TREE
;
26238 /* If the lookup failed, let our caller know. */
26239 if (!decl
|| decl
== error_mark_node
)
26240 return error_mark_node
;
26242 /* Pull out the template from an injected-class-name (or multiple). */
26244 decl
= maybe_get_template_decl_from_type_decl (decl
);
26246 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26247 if (TREE_CODE (decl
) == TREE_LIST
)
26249 if (ambiguous_decls
)
26250 *ambiguous_decls
= decl
;
26251 /* The error message we have to print is too complicated for
26252 cp_parser_error, so we incorporate its actions directly. */
26253 if (!cp_parser_simulate_error (parser
))
26255 error_at (name_location
, "reference to %qD is ambiguous",
26257 print_candidates (decl
);
26259 return error_mark_node
;
26262 gcc_assert (DECL_P (decl
)
26263 || TREE_CODE (decl
) == OVERLOAD
26264 || TREE_CODE (decl
) == SCOPE_REF
26265 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26266 || BASELINK_P (decl
));
26268 /* If we have resolved the name of a member declaration, check to
26269 see if the declaration is accessible. When the name resolves to
26270 set of overloaded functions, accessibility is checked when
26271 overload resolution is done.
26273 During an explicit instantiation, access is not checked at all,
26274 as per [temp.explicit]. */
26276 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26278 maybe_record_typedef_use (decl
);
26280 return cp_expr (decl
, name_location
);
26283 /* Like cp_parser_lookup_name, but for use in the typical case where
26284 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26285 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26288 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26290 return cp_parser_lookup_name (parser
, name
,
26292 /*is_template=*/false,
26293 /*is_namespace=*/false,
26294 /*check_dependency=*/true,
26295 /*ambiguous_decls=*/NULL
,
26299 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26300 the current context, return the TYPE_DECL. If TAG_NAME_P is
26301 true, the DECL indicates the class being defined in a class-head,
26302 or declared in an elaborated-type-specifier.
26304 Otherwise, return DECL. */
26307 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26309 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26310 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26313 template <typename T> struct B;
26316 template <typename T> struct A::B {};
26318 Similarly, in an elaborated-type-specifier:
26320 namespace N { struct X{}; }
26323 template <typename T> friend struct N::X;
26326 However, if the DECL refers to a class type, and we are in
26327 the scope of the class, then the name lookup automatically
26328 finds the TYPE_DECL created by build_self_reference rather
26329 than a TEMPLATE_DECL. For example, in:
26331 template <class T> struct S {
26335 there is no need to handle such case. */
26337 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26338 return DECL_TEMPLATE_RESULT (decl
);
26343 /* If too many, or too few, template-parameter lists apply to the
26344 declarator, issue an error message. Returns TRUE if all went well,
26345 and FALSE otherwise. */
26348 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26349 cp_declarator
*declarator
,
26350 location_t declarator_location
)
26352 switch (declarator
->kind
)
26356 unsigned num_templates
= 0;
26357 tree scope
= declarator
->u
.id
.qualifying_scope
;
26360 num_templates
= num_template_headers_for_class (scope
);
26361 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26362 == TEMPLATE_ID_EXPR
)
26363 /* If the DECLARATOR has the form `X<y>' then it uses one
26364 additional level of template parameters. */
26367 return cp_parser_check_template_parameters
26368 (parser
, num_templates
, declarator_location
, declarator
);
26374 case cdk_reference
:
26376 return (cp_parser_check_declarator_template_parameters
26377 (parser
, declarator
->declarator
, declarator_location
));
26384 gcc_unreachable ();
26389 /* NUM_TEMPLATES were used in the current declaration. If that is
26390 invalid, return FALSE and issue an error messages. Otherwise,
26391 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26392 declarator and we can print more accurate diagnostics. */
26395 cp_parser_check_template_parameters (cp_parser
* parser
,
26396 unsigned num_templates
,
26397 location_t location
,
26398 cp_declarator
*declarator
)
26400 /* If there are the same number of template classes and parameter
26401 lists, that's OK. */
26402 if (parser
->num_template_parameter_lists
== num_templates
)
26404 /* If there are more, but only one more, then we are referring to a
26405 member template. That's OK too. */
26406 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26408 /* If there are more template classes than parameter lists, we have
26411 template <class T> void S<T>::R<T>::f (); */
26412 if (parser
->num_template_parameter_lists
< num_templates
)
26414 if (declarator
&& !current_function_decl
)
26415 error_at (location
, "specializing member %<%T::%E%> "
26416 "requires %<template<>%> syntax",
26417 declarator
->u
.id
.qualifying_scope
,
26418 declarator
->u
.id
.unqualified_name
);
26419 else if (declarator
)
26420 error_at (location
, "invalid declaration of %<%T::%E%>",
26421 declarator
->u
.id
.qualifying_scope
,
26422 declarator
->u
.id
.unqualified_name
);
26424 error_at (location
, "too few template-parameter-lists");
26427 /* Otherwise, there are too many template parameter lists. We have
26430 template <class T> template <class U> void S::f(); */
26431 error_at (location
, "too many template-parameter-lists");
26435 /* Parse an optional `::' token indicating that the following name is
26436 from the global namespace. If so, PARSER->SCOPE is set to the
26437 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26438 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26439 Returns the new value of PARSER->SCOPE, if the `::' token is
26440 present, and NULL_TREE otherwise. */
26443 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26447 /* Peek at the next token. */
26448 token
= cp_lexer_peek_token (parser
->lexer
);
26449 /* If we're looking at a `::' token then we're starting from the
26450 global namespace, not our current location. */
26451 if (token
->type
== CPP_SCOPE
)
26453 /* Consume the `::' token. */
26454 cp_lexer_consume_token (parser
->lexer
);
26455 /* Set the SCOPE so that we know where to start the lookup. */
26456 parser
->scope
= global_namespace
;
26457 parser
->qualifying_scope
= global_namespace
;
26458 parser
->object_scope
= NULL_TREE
;
26460 return parser
->scope
;
26462 else if (!current_scope_valid_p
)
26464 parser
->scope
= NULL_TREE
;
26465 parser
->qualifying_scope
= NULL_TREE
;
26466 parser
->object_scope
= NULL_TREE
;
26472 /* Returns TRUE if the upcoming token sequence is the start of a
26473 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26474 declarator is preceded by the `friend' specifier. */
26477 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26479 bool constructor_p
;
26480 bool outside_class_specifier_p
;
26481 tree nested_name_specifier
;
26482 cp_token
*next_token
;
26484 /* The common case is that this is not a constructor declarator, so
26485 try to avoid doing lots of work if at all possible. It's not
26486 valid declare a constructor at function scope. */
26487 if (parser
->in_function_body
)
26489 /* And only certain tokens can begin a constructor declarator. */
26490 next_token
= cp_lexer_peek_token (parser
->lexer
);
26491 if (next_token
->type
!= CPP_NAME
26492 && next_token
->type
!= CPP_SCOPE
26493 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26494 && next_token
->type
!= CPP_TEMPLATE_ID
)
26497 /* Parse tentatively; we are going to roll back all of the tokens
26499 cp_parser_parse_tentatively (parser
);
26500 /* Assume that we are looking at a constructor declarator. */
26501 constructor_p
= true;
26503 /* Look for the optional `::' operator. */
26504 cp_parser_global_scope_opt (parser
,
26505 /*current_scope_valid_p=*/false);
26506 /* Look for the nested-name-specifier. */
26507 nested_name_specifier
26508 = (cp_parser_nested_name_specifier_opt (parser
,
26509 /*typename_keyword_p=*/false,
26510 /*check_dependency_p=*/false,
26512 /*is_declaration=*/false));
26514 outside_class_specifier_p
= (!at_class_scope_p ()
26515 || !TYPE_BEING_DEFINED (current_class_type
)
26518 /* Outside of a class-specifier, there must be a
26519 nested-name-specifier. Except in C++17 mode, where we
26520 might be declaring a guiding declaration. */
26521 if (!nested_name_specifier
&& outside_class_specifier_p
26522 && cxx_dialect
< cxx17
)
26523 constructor_p
= false;
26524 else if (nested_name_specifier
== error_mark_node
)
26525 constructor_p
= false;
26527 /* If we have a class scope, this is easy; DR 147 says that S::S always
26528 names the constructor, and no other qualified name could. */
26529 if (constructor_p
&& nested_name_specifier
26530 && CLASS_TYPE_P (nested_name_specifier
))
26532 tree id
= cp_parser_unqualified_id (parser
,
26533 /*template_keyword_p=*/false,
26534 /*check_dependency_p=*/false,
26535 /*declarator_p=*/true,
26536 /*optional_p=*/false);
26537 if (is_overloaded_fn (id
))
26538 id
= DECL_NAME (get_first_fn (id
));
26539 if (!constructor_name_p (id
, nested_name_specifier
))
26540 constructor_p
= false;
26542 /* If we still think that this might be a constructor-declarator,
26543 look for a class-name. */
26544 else if (constructor_p
)
26548 template <typename T> struct S {
26552 we must recognize that the nested `S' names a class. */
26553 if (cxx_dialect
>= cxx17
)
26554 cp_parser_parse_tentatively (parser
);
26557 type_decl
= cp_parser_class_name (parser
,
26558 /*typename_keyword_p=*/false,
26559 /*template_keyword_p=*/false,
26561 /*check_dependency_p=*/false,
26562 /*class_head_p=*/false,
26563 /*is_declaration=*/false);
26565 if (cxx_dialect
>= cxx17
26566 && !cp_parser_parse_definitely (parser
))
26568 type_decl
= NULL_TREE
;
26569 tree tmpl
= cp_parser_template_name (parser
,
26570 /*template_keyword*/false,
26571 /*check_dependency_p*/false,
26572 /*is_declaration*/false,
26574 /*is_identifier*/NULL
);
26575 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26576 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26577 /* It's a deduction guide, return true. */;
26579 cp_parser_simulate_error (parser
);
26582 /* If there was no class-name, then this is not a constructor.
26583 Otherwise, if we are in a class-specifier and we aren't
26584 handling a friend declaration, check that its type matches
26585 current_class_type (c++/38313). Note: error_mark_node
26586 is left alone for error recovery purposes. */
26587 constructor_p
= (!cp_parser_error_occurred (parser
)
26588 && (outside_class_specifier_p
26589 || type_decl
== NULL_TREE
26590 || type_decl
== error_mark_node
26591 || same_type_p (current_class_type
,
26592 TREE_TYPE (type_decl
))));
26594 /* If we're still considering a constructor, we have to see a `(',
26595 to begin the parameter-declaration-clause, followed by either a
26596 `)', an `...', or a decl-specifier. We need to check for a
26597 type-specifier to avoid being fooled into thinking that:
26601 is a constructor. (It is actually a function named `f' that
26602 takes one parameter (of type `int') and returns a value of type
26605 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26606 constructor_p
= false;
26609 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26610 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26611 /* A parameter declaration begins with a decl-specifier,
26612 which is either the "attribute" keyword, a storage class
26613 specifier, or (usually) a type-specifier. */
26614 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26617 tree pushed_scope
= NULL_TREE
;
26618 unsigned saved_num_template_parameter_lists
;
26620 /* Names appearing in the type-specifier should be looked up
26621 in the scope of the class. */
26622 if (current_class_type
)
26624 else if (type_decl
)
26626 type
= TREE_TYPE (type_decl
);
26627 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26629 type
= resolve_typename_type (type
,
26630 /*only_current_p=*/false);
26631 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26633 cp_parser_abort_tentative_parse (parser
);
26637 pushed_scope
= push_scope (type
);
26640 /* Inside the constructor parameter list, surrounding
26641 template-parameter-lists do not apply. */
26642 saved_num_template_parameter_lists
26643 = parser
->num_template_parameter_lists
;
26644 parser
->num_template_parameter_lists
= 0;
26646 /* Look for the type-specifier. */
26647 cp_parser_type_specifier (parser
,
26648 CP_PARSER_FLAGS_NONE
,
26649 /*decl_specs=*/NULL
,
26650 /*is_declarator=*/true,
26651 /*declares_class_or_enum=*/NULL
,
26652 /*is_cv_qualifier=*/NULL
);
26654 parser
->num_template_parameter_lists
26655 = saved_num_template_parameter_lists
;
26657 /* Leave the scope of the class. */
26659 pop_scope (pushed_scope
);
26661 constructor_p
= !cp_parser_error_occurred (parser
);
26665 /* We did not really want to consume any tokens. */
26666 cp_parser_abort_tentative_parse (parser
);
26668 return constructor_p
;
26671 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26672 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26673 they must be performed once we are in the scope of the function.
26675 Returns the function defined. */
26678 cp_parser_function_definition_from_specifiers_and_declarator
26679 (cp_parser
* parser
,
26680 cp_decl_specifier_seq
*decl_specifiers
,
26682 const cp_declarator
*declarator
)
26687 /* Begin the function-definition. */
26688 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26690 /* The things we're about to see are not directly qualified by any
26691 template headers we've seen thus far. */
26692 reset_specialization ();
26694 /* If there were names looked up in the decl-specifier-seq that we
26695 did not check, check them now. We must wait until we are in the
26696 scope of the function to perform the checks, since the function
26697 might be a friend. */
26698 perform_deferred_access_checks (tf_warning_or_error
);
26702 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26703 parser
->omp_declare_simd
= NULL
;
26704 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26705 parser
->oacc_routine
= NULL
;
26710 /* Skip the entire function. */
26711 cp_parser_skip_to_end_of_block_or_statement (parser
);
26712 fn
= error_mark_node
;
26714 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26716 /* Seen already, skip it. An error message has already been output. */
26717 cp_parser_skip_to_end_of_block_or_statement (parser
);
26718 fn
= current_function_decl
;
26719 current_function_decl
= NULL_TREE
;
26720 /* If this is a function from a class, pop the nested class. */
26721 if (current_class_name
)
26722 pop_nested_class ();
26727 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26728 tv
= TV_PARSE_INLINE
;
26730 tv
= TV_PARSE_FUNC
;
26732 fn
= cp_parser_function_definition_after_declarator (parser
,
26733 /*inline_p=*/false);
26740 /* Parse the part of a function-definition that follows the
26741 declarator. INLINE_P is TRUE iff this function is an inline
26742 function defined within a class-specifier.
26744 Returns the function defined. */
26747 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26751 bool saved_in_unbraced_linkage_specification_p
;
26752 bool saved_in_function_body
;
26753 unsigned saved_num_template_parameter_lists
;
26755 bool fully_implicit_function_template_p
26756 = parser
->fully_implicit_function_template_p
;
26757 parser
->fully_implicit_function_template_p
= false;
26758 tree implicit_template_parms
26759 = parser
->implicit_template_parms
;
26760 parser
->implicit_template_parms
= 0;
26761 cp_binding_level
* implicit_template_scope
26762 = parser
->implicit_template_scope
;
26763 parser
->implicit_template_scope
= 0;
26765 saved_in_function_body
= parser
->in_function_body
;
26766 parser
->in_function_body
= true;
26767 /* If the next token is `return', then the code may be trying to
26768 make use of the "named return value" extension that G++ used to
26770 token
= cp_lexer_peek_token (parser
->lexer
);
26771 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26773 /* Consume the `return' keyword. */
26774 cp_lexer_consume_token (parser
->lexer
);
26775 /* Look for the identifier that indicates what value is to be
26777 cp_parser_identifier (parser
);
26778 /* Issue an error message. */
26779 error_at (token
->location
,
26780 "named return values are no longer supported");
26781 /* Skip tokens until we reach the start of the function body. */
26784 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26785 if (token
->type
== CPP_OPEN_BRACE
26786 || token
->type
== CPP_EOF
26787 || token
->type
== CPP_PRAGMA_EOL
)
26789 cp_lexer_consume_token (parser
->lexer
);
26792 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26793 anything declared inside `f'. */
26794 saved_in_unbraced_linkage_specification_p
26795 = parser
->in_unbraced_linkage_specification_p
;
26796 parser
->in_unbraced_linkage_specification_p
= false;
26797 /* Inside the function, surrounding template-parameter-lists do not
26799 saved_num_template_parameter_lists
26800 = parser
->num_template_parameter_lists
;
26801 parser
->num_template_parameter_lists
= 0;
26803 /* If the next token is `try', `__transaction_atomic', or
26804 `__transaction_relaxed`, then we are looking at either function-try-block
26805 or function-transaction-block. Note that all of these include the
26807 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26808 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26809 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26810 RID_TRANSACTION_RELAXED
))
26811 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26812 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26813 cp_parser_function_try_block (parser
);
26815 cp_parser_ctor_initializer_opt_and_function_body
26816 (parser
, /*in_function_try_block=*/false);
26818 /* Finish the function. */
26819 fn
= finish_function (inline_p
);
26820 /* Generate code for it, if necessary. */
26821 expand_or_defer_fn (fn
);
26822 /* Restore the saved values. */
26823 parser
->in_unbraced_linkage_specification_p
26824 = saved_in_unbraced_linkage_specification_p
;
26825 parser
->num_template_parameter_lists
26826 = saved_num_template_parameter_lists
;
26827 parser
->in_function_body
= saved_in_function_body
;
26829 parser
->fully_implicit_function_template_p
26830 = fully_implicit_function_template_p
;
26831 parser
->implicit_template_parms
26832 = implicit_template_parms
;
26833 parser
->implicit_template_scope
26834 = implicit_template_scope
;
26836 if (parser
->fully_implicit_function_template_p
)
26837 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26842 /* Parse a template-declaration body (following argument list). */
26845 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26846 tree parameter_list
,
26849 tree decl
= NULL_TREE
;
26850 bool friend_p
= false;
26852 /* We just processed one more parameter list. */
26853 ++parser
->num_template_parameter_lists
;
26855 /* Get the deferred access checks from the parameter list. These
26856 will be checked once we know what is being declared, as for a
26857 member template the checks must be performed in the scope of the
26858 class containing the member. */
26859 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26861 /* Tentatively parse for a new template parameter list, which can either be
26862 the template keyword or a template introduction. */
26863 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26865 else if (cxx_dialect
>= cxx11
26866 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26867 decl
= cp_parser_alias_declaration (parser
);
26870 /* There are no access checks when parsing a template, as we do not
26871 know if a specialization will be a friend. */
26872 push_deferring_access_checks (dk_no_check
);
26873 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26874 decl
= cp_parser_single_declaration (parser
,
26877 /*explicit_specialization_p=*/false,
26879 pop_deferring_access_checks ();
26881 /* If this is a member template declaration, let the front
26883 if (member_p
&& !friend_p
&& decl
)
26885 if (TREE_CODE (decl
) == TYPE_DECL
)
26886 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26888 decl
= finish_member_template_decl (decl
);
26890 else if (friend_p
&& decl
26891 && DECL_DECLARES_TYPE_P (decl
))
26892 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26893 /*complain=*/true);
26895 /* We are done with the current parameter list. */
26896 --parser
->num_template_parameter_lists
;
26898 pop_deferring_access_checks ();
26901 finish_template_decl (parameter_list
);
26903 /* Check the template arguments for a literal operator template. */
26905 && DECL_DECLARES_FUNCTION_P (decl
)
26906 && UDLIT_OPER_P (DECL_NAME (decl
)))
26909 if (parameter_list
== NULL_TREE
)
26913 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26914 if (num_parms
== 1)
26916 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26917 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26918 if (TREE_TYPE (parm
) != char_type_node
26919 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26922 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26924 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26925 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26926 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26927 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26928 if (parm
== error_mark_node
26929 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26930 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26938 if (cxx_dialect
>= cxx14
)
26939 error ("literal operator template %qD has invalid parameter list."
26940 " Expected non-type template argument pack <char...>"
26941 " or <typename CharT, CharT...>",
26944 error ("literal operator template %qD has invalid parameter list."
26945 " Expected non-type template argument pack <char...>",
26950 /* Register member declarations. */
26951 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26952 finish_member_declaration (decl
);
26953 /* If DECL is a function template, we must return to parse it later.
26954 (Even though there is no definition, there might be default
26955 arguments that need handling.) */
26956 if (member_p
&& decl
26957 && DECL_DECLARES_FUNCTION_P (decl
))
26958 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26961 /* Parse a template introduction header for a template-declaration. Returns
26962 false if tentative parse fails. */
26965 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26967 cp_parser_parse_tentatively (parser
);
26969 tree saved_scope
= parser
->scope
;
26970 tree saved_object_scope
= parser
->object_scope
;
26971 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26973 /* Look for the optional `::' operator. */
26974 cp_parser_global_scope_opt (parser
,
26975 /*current_scope_valid_p=*/false);
26976 /* Look for the nested-name-specifier. */
26977 cp_parser_nested_name_specifier_opt (parser
,
26978 /*typename_keyword_p=*/false,
26979 /*check_dependency_p=*/true,
26981 /*is_declaration=*/false);
26983 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26984 tree concept_name
= cp_parser_identifier (parser
);
26986 /* Look up the concept for which we will be matching
26987 template parameters. */
26988 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26990 parser
->scope
= saved_scope
;
26991 parser
->object_scope
= saved_object_scope
;
26992 parser
->qualifying_scope
= saved_qualifying_scope
;
26994 if (concept_name
== error_mark_node
)
26995 cp_parser_simulate_error (parser
);
26997 /* Look for opening brace for introduction. */
26998 matching_braces braces
;
26999 braces
.require_open (parser
);
27001 if (!cp_parser_parse_definitely (parser
))
27004 push_deferring_access_checks (dk_deferred
);
27006 /* Build vector of placeholder parameters and grab
27007 matching identifiers. */
27008 tree introduction_list
= cp_parser_introduction_list (parser
);
27010 /* The introduction-list shall not be empty. */
27011 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27014 error ("empty introduction-list");
27018 /* Look for closing brace for introduction. */
27019 if (!braces
.require_close (parser
))
27022 if (tmpl_decl
== error_mark_node
)
27024 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27029 /* Build and associate the constraint. */
27030 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27031 if (parms
&& parms
!= error_mark_node
)
27033 cp_parser_template_declaration_after_parameters (parser
, parms
,
27038 error_at (token
->location
, "no matching concept for template-introduction");
27042 /* Parse a normal template-declaration following the template keyword. */
27045 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27047 tree parameter_list
;
27048 bool need_lang_pop
;
27049 location_t location
= input_location
;
27051 /* Look for the `<' token. */
27052 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27054 if (at_class_scope_p () && current_function_decl
)
27056 /* 14.5.2.2 [temp.mem]
27058 A local class shall not have member templates. */
27059 error_at (location
,
27060 "invalid declaration of member template in local class");
27061 cp_parser_skip_to_end_of_block_or_statement (parser
);
27066 A template ... shall not have C linkage. */
27067 if (current_lang_name
== lang_name_c
)
27069 error_at (location
, "template with C linkage");
27070 maybe_show_extern_c_location ();
27071 /* Give it C++ linkage to avoid confusing other parts of the
27073 push_lang_context (lang_name_cplusplus
);
27074 need_lang_pop
= true;
27077 need_lang_pop
= false;
27079 /* We cannot perform access checks on the template parameter
27080 declarations until we know what is being declared, just as we
27081 cannot check the decl-specifier list. */
27082 push_deferring_access_checks (dk_deferred
);
27084 /* If the next token is `>', then we have an invalid
27085 specialization. Rather than complain about an invalid template
27086 parameter, issue an error message here. */
27087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27089 cp_parser_error (parser
, "invalid explicit specialization");
27090 begin_specialization ();
27091 parameter_list
= NULL_TREE
;
27095 /* Parse the template parameters. */
27096 parameter_list
= cp_parser_template_parameter_list (parser
);
27099 /* Look for the `>'. */
27100 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27102 /* Manage template requirements */
27105 tree reqs
= get_shorthand_constraints (current_template_parms
);
27106 if (tree r
= cp_parser_requires_clause_opt (parser
))
27107 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27108 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27111 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27114 /* For the erroneous case of a template with C linkage, we pushed an
27115 implicit C++ linkage scope; exit that scope now. */
27117 pop_lang_context ();
27120 /* Parse a template-declaration, assuming that the `export' (and
27121 `extern') keywords, if present, has already been scanned. MEMBER_P
27122 is as for cp_parser_template_declaration. */
27125 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27127 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27129 cp_lexer_consume_token (parser
->lexer
);
27130 cp_parser_explicit_template_declaration (parser
, member_p
);
27133 else if (flag_concepts
)
27134 return cp_parser_template_introduction (parser
, member_p
);
27139 /* Perform the deferred access checks from a template-parameter-list.
27140 CHECKS is a TREE_LIST of access checks, as returned by
27141 get_deferred_access_checks. */
27144 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27146 ++processing_template_parmlist
;
27147 perform_access_checks (checks
, tf_warning_or_error
);
27148 --processing_template_parmlist
;
27151 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27152 `function-definition' sequence that follows a template header.
27153 If MEMBER_P is true, this declaration appears in a class scope.
27155 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27156 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27159 cp_parser_single_declaration (cp_parser
* parser
,
27160 vec
<deferred_access_check
, va_gc
> *checks
,
27162 bool explicit_specialization_p
,
27165 int declares_class_or_enum
;
27166 tree decl
= NULL_TREE
;
27167 cp_decl_specifier_seq decl_specifiers
;
27168 bool function_definition_p
= false;
27169 cp_token
*decl_spec_token_start
;
27171 /* This function is only used when processing a template
27173 gcc_assert (innermost_scope_kind () == sk_template_parms
27174 || innermost_scope_kind () == sk_template_spec
);
27176 /* Defer access checks until we know what is being declared. */
27177 push_deferring_access_checks (dk_deferred
);
27179 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27181 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27182 cp_parser_decl_specifier_seq (parser
,
27183 CP_PARSER_FLAGS_OPTIONAL
,
27185 &declares_class_or_enum
);
27187 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27189 /* There are no template typedefs. */
27190 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27192 error_at (decl_spec_token_start
->location
,
27193 "template declaration of %<typedef%>");
27194 decl
= error_mark_node
;
27197 /* Gather up the access checks that occurred the
27198 decl-specifier-seq. */
27199 stop_deferring_access_checks ();
27201 /* Check for the declaration of a template class. */
27202 if (declares_class_or_enum
)
27204 if (cp_parser_declares_only_class_p (parser
)
27205 || (declares_class_or_enum
& 2))
27207 // If this is a declaration, but not a definition, associate
27208 // any constraints with the type declaration. Constraints
27209 // are associated with definitions in cp_parser_class_specifier.
27210 if (declares_class_or_enum
== 1)
27211 associate_classtype_constraints (decl_specifiers
.type
);
27213 decl
= shadow_tag (&decl_specifiers
);
27218 friend template <typename T> struct A<T>::B;
27221 A<T>::B will be represented by a TYPENAME_TYPE, and
27222 therefore not recognized by shadow_tag. */
27223 if (friend_p
&& *friend_p
27225 && decl_specifiers
.type
27226 && TYPE_P (decl_specifiers
.type
))
27227 decl
= decl_specifiers
.type
;
27229 if (decl
&& decl
!= error_mark_node
)
27230 decl
= TYPE_NAME (decl
);
27232 decl
= error_mark_node
;
27234 /* Perform access checks for template parameters. */
27235 cp_parser_perform_template_parameter_access_checks (checks
);
27237 /* Give a helpful diagnostic for
27238 template <class T> struct A { } a;
27239 if we aren't already recovering from an error. */
27240 if (!cp_parser_declares_only_class_p (parser
)
27243 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27244 "a class template declaration must not declare "
27246 cp_parser_skip_to_end_of_block_or_statement (parser
);
27252 /* Complain about missing 'typename' or other invalid type names. */
27253 if (!decl_specifiers
.any_type_specifiers_p
27254 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27256 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27257 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27258 the rest of this declaration. */
27259 decl
= error_mark_node
;
27263 /* If it's not a template class, try for a template function. If
27264 the next token is a `;', then this declaration does not declare
27265 anything. But, if there were errors in the decl-specifiers, then
27266 the error might well have come from an attempted class-specifier.
27267 In that case, there's no need to warn about a missing declarator. */
27269 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27270 || decl_specifiers
.type
!= error_mark_node
))
27272 decl
= cp_parser_init_declarator (parser
,
27275 /*function_definition_allowed_p=*/true,
27277 declares_class_or_enum
,
27278 &function_definition_p
,
27281 /* 7.1.1-1 [dcl.stc]
27283 A storage-class-specifier shall not be specified in an explicit
27284 specialization... */
27286 && explicit_specialization_p
27287 && decl_specifiers
.storage_class
!= sc_none
)
27289 error_at (decl_spec_token_start
->location
,
27290 "explicit template specialization cannot have a storage class");
27291 decl
= error_mark_node
;
27294 if (decl
&& VAR_P (decl
))
27295 check_template_variable (decl
);
27298 /* Look for a trailing `;' after the declaration. */
27299 if (!function_definition_p
27300 && (decl
== error_mark_node
27301 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27302 cp_parser_skip_to_end_of_block_or_statement (parser
);
27305 pop_deferring_access_checks ();
27307 /* Clear any current qualification; whatever comes next is the start
27308 of something new. */
27309 parser
->scope
= NULL_TREE
;
27310 parser
->qualifying_scope
= NULL_TREE
;
27311 parser
->object_scope
= NULL_TREE
;
27316 /* Parse a cast-expression that is not the operand of a unary "&". */
27319 cp_parser_simple_cast_expression (cp_parser
*parser
)
27321 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27322 /*cast_p=*/false, /*decltype*/false, NULL
);
27325 /* Parse a functional cast to TYPE. Returns an expression
27326 representing the cast. */
27329 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27331 vec
<tree
, va_gc
> *vec
;
27332 tree expression_list
;
27336 location_t start_loc
= input_location
;
27339 type
= error_mark_node
;
27341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27343 cp_lexer_set_source_position (parser
->lexer
);
27344 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27345 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27346 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27347 if (TREE_CODE (type
) == TYPE_DECL
)
27348 type
= TREE_TYPE (type
);
27350 cast
= finish_compound_literal (type
, expression_list
,
27351 tf_warning_or_error
, fcl_functional
);
27352 /* Create a location of the form:
27355 with caret == start at the start of the type name,
27356 finishing at the closing brace. */
27357 location_t finish_loc
27358 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27359 location_t combined_loc
= make_location (start_loc
, start_loc
,
27361 cast
.set_location (combined_loc
);
27366 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27368 /*allow_expansion_p=*/true,
27369 /*non_constant_p=*/NULL
);
27371 expression_list
= error_mark_node
;
27374 expression_list
= build_tree_list_vec (vec
);
27375 release_tree_vector (vec
);
27378 cast
= build_functional_cast (type
, expression_list
,
27379 tf_warning_or_error
);
27380 /* [expr.const]/1: In an integral constant expression "only type
27381 conversions to integral or enumeration type can be used". */
27382 if (TREE_CODE (type
) == TYPE_DECL
)
27383 type
= TREE_TYPE (type
);
27384 if (cast
!= error_mark_node
27385 && !cast_valid_in_integral_constant_expression_p (type
)
27386 && cp_parser_non_integral_constant_expression (parser
,
27388 return error_mark_node
;
27390 /* Create a location of the form:
27393 with caret == start at the start of the type name,
27394 finishing at the closing paren. */
27395 location_t finish_loc
27396 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27397 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27398 cast
.set_location (combined_loc
);
27402 /* Save the tokens that make up the body of a member function defined
27403 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27404 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27405 specifiers applied to the declaration. Returns the FUNCTION_DECL
27406 for the member function. */
27409 cp_parser_save_member_function_body (cp_parser
* parser
,
27410 cp_decl_specifier_seq
*decl_specifiers
,
27411 cp_declarator
*declarator
,
27417 bool function_try_block
= false;
27419 /* Create the FUNCTION_DECL. */
27420 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27421 cp_finalize_omp_declare_simd (parser
, fn
);
27422 cp_finalize_oacc_routine (parser
, fn
, true);
27423 /* If something went badly wrong, bail out now. */
27424 if (fn
== error_mark_node
)
27426 /* If there's a function-body, skip it. */
27427 if (cp_parser_token_starts_function_definition_p
27428 (cp_lexer_peek_token (parser
->lexer
)))
27429 cp_parser_skip_to_end_of_block_or_statement (parser
);
27430 return error_mark_node
;
27433 /* Remember it, if there default args to post process. */
27434 cp_parser_save_default_args (parser
, fn
);
27436 /* Save away the tokens that make up the body of the
27438 first
= parser
->lexer
->next_token
;
27440 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27441 cp_lexer_consume_token (parser
->lexer
);
27442 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27443 RID_TRANSACTION_ATOMIC
))
27445 cp_lexer_consume_token (parser
->lexer
);
27446 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27448 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27449 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27450 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27451 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27452 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27454 cp_lexer_consume_token (parser
->lexer
);
27455 cp_lexer_consume_token (parser
->lexer
);
27456 cp_lexer_consume_token (parser
->lexer
);
27457 cp_lexer_consume_token (parser
->lexer
);
27458 cp_lexer_consume_token (parser
->lexer
);
27461 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27462 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27464 cp_lexer_consume_token (parser
->lexer
);
27465 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27470 /* Handle function try blocks. */
27471 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27473 cp_lexer_consume_token (parser
->lexer
);
27474 function_try_block
= true;
27476 /* We can have braced-init-list mem-initializers before the fn body. */
27477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27479 cp_lexer_consume_token (parser
->lexer
);
27480 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27482 /* cache_group will stop after an un-nested { } pair, too. */
27483 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27486 /* variadic mem-inits have ... after the ')'. */
27487 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27488 cp_lexer_consume_token (parser
->lexer
);
27491 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27492 /* Handle function try blocks. */
27493 if (function_try_block
)
27494 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27495 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27496 last
= parser
->lexer
->next_token
;
27498 /* Save away the inline definition; we will process it when the
27499 class is complete. */
27500 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27501 DECL_PENDING_INLINE_P (fn
) = 1;
27503 /* We need to know that this was defined in the class, so that
27504 friend templates are handled correctly. */
27505 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27507 /* Add FN to the queue of functions to be parsed later. */
27508 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27513 /* Save the tokens that make up the in-class initializer for a non-static
27514 data member. Returns a DEFAULT_ARG. */
27517 cp_parser_save_nsdmi (cp_parser
* parser
)
27519 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27522 /* Parse a template-argument-list, as well as the trailing ">" (but
27523 not the opening "<"). See cp_parser_template_argument_list for the
27527 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27531 tree saved_qualifying_scope
;
27532 tree saved_object_scope
;
27533 bool saved_greater_than_is_operator_p
;
27534 int saved_unevaluated_operand
;
27535 int saved_inhibit_evaluation_warnings
;
27539 When parsing a template-id, the first non-nested `>' is taken as
27540 the end of the template-argument-list rather than a greater-than
27542 saved_greater_than_is_operator_p
27543 = parser
->greater_than_is_operator_p
;
27544 parser
->greater_than_is_operator_p
= false;
27545 /* Parsing the argument list may modify SCOPE, so we save it
27547 saved_scope
= parser
->scope
;
27548 saved_qualifying_scope
= parser
->qualifying_scope
;
27549 saved_object_scope
= parser
->object_scope
;
27550 /* We need to evaluate the template arguments, even though this
27551 template-id may be nested within a "sizeof". */
27552 saved_unevaluated_operand
= cp_unevaluated_operand
;
27553 cp_unevaluated_operand
= 0;
27554 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27555 c_inhibit_evaluation_warnings
= 0;
27556 /* Parse the template-argument-list itself. */
27557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27558 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27559 arguments
= NULL_TREE
;
27561 arguments
= cp_parser_template_argument_list (parser
);
27562 /* Look for the `>' that ends the template-argument-list. If we find
27563 a '>>' instead, it's probably just a typo. */
27564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27566 if (cxx_dialect
!= cxx98
)
27568 /* In C++0x, a `>>' in a template argument list or cast
27569 expression is considered to be two separate `>'
27570 tokens. So, change the current token to a `>', but don't
27571 consume it: it will be consumed later when the outer
27572 template argument list (or cast expression) is parsed.
27573 Note that this replacement of `>' for `>>' is necessary
27574 even if we are parsing tentatively: in the tentative
27575 case, after calling
27576 cp_parser_enclosed_template_argument_list we will always
27577 throw away all of the template arguments and the first
27578 closing `>', either because the template argument list
27579 was erroneous or because we are replacing those tokens
27580 with a CPP_TEMPLATE_ID token. The second `>' (which will
27581 not have been thrown away) is needed either to close an
27582 outer template argument list or to complete a new-style
27584 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27585 token
->type
= CPP_GREATER
;
27587 else if (!saved_greater_than_is_operator_p
)
27589 /* If we're in a nested template argument list, the '>>' has
27590 to be a typo for '> >'. We emit the error message, but we
27591 continue parsing and we push a '>' as next token, so that
27592 the argument list will be parsed correctly. Note that the
27593 global source location is still on the token before the
27594 '>>', so we need to say explicitly where we want it. */
27595 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27596 gcc_rich_location
richloc (token
->location
);
27597 richloc
.add_fixit_replace ("> >");
27598 error_at (&richloc
, "%<>>%> should be %<> >%> "
27599 "within a nested template argument list");
27601 token
->type
= CPP_GREATER
;
27605 /* If this is not a nested template argument list, the '>>'
27606 is a typo for '>'. Emit an error message and continue.
27607 Same deal about the token location, but here we can get it
27608 right by consuming the '>>' before issuing the diagnostic. */
27609 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27610 error_at (token
->location
,
27611 "spurious %<>>%>, use %<>%> to terminate "
27612 "a template argument list");
27616 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27617 /* The `>' token might be a greater-than operator again now. */
27618 parser
->greater_than_is_operator_p
27619 = saved_greater_than_is_operator_p
;
27620 /* Restore the SAVED_SCOPE. */
27621 parser
->scope
= saved_scope
;
27622 parser
->qualifying_scope
= saved_qualifying_scope
;
27623 parser
->object_scope
= saved_object_scope
;
27624 cp_unevaluated_operand
= saved_unevaluated_operand
;
27625 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27630 /* MEMBER_FUNCTION is a member function, or a friend. If default
27631 arguments, or the body of the function have not yet been parsed,
27635 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27637 timevar_push (TV_PARSE_INMETH
);
27638 /* If this member is a template, get the underlying
27640 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27641 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27643 /* There should not be any class definitions in progress at this
27644 point; the bodies of members are only parsed outside of all class
27646 gcc_assert (parser
->num_classes_being_defined
== 0);
27647 /* While we're parsing the member functions we might encounter more
27648 classes. We want to handle them right away, but we don't want
27649 them getting mixed up with functions that are currently in the
27651 push_unparsed_function_queues (parser
);
27653 /* Make sure that any template parameters are in scope. */
27654 maybe_begin_member_template_processing (member_function
);
27656 /* If the body of the function has not yet been parsed, parse it
27658 if (DECL_PENDING_INLINE_P (member_function
))
27660 tree function_scope
;
27661 cp_token_cache
*tokens
;
27663 /* The function is no longer pending; we are processing it. */
27664 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27665 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27666 DECL_PENDING_INLINE_P (member_function
) = 0;
27668 /* If this is a local class, enter the scope of the containing
27670 function_scope
= current_function_decl
;
27671 if (function_scope
)
27672 push_function_context ();
27674 /* Push the body of the function onto the lexer stack. */
27675 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27677 /* Let the front end know that we going to be defining this
27679 start_preparsed_function (member_function
, NULL_TREE
,
27680 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27682 /* Don't do access checking if it is a templated function. */
27683 if (processing_template_decl
)
27684 push_deferring_access_checks (dk_no_check
);
27686 /* #pragma omp declare reduction needs special parsing. */
27687 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27689 parser
->lexer
->in_pragma
= true;
27690 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27691 finish_function (/*inline_p=*/true);
27692 cp_check_omp_declare_reduction (member_function
);
27695 /* Now, parse the body of the function. */
27696 cp_parser_function_definition_after_declarator (parser
,
27697 /*inline_p=*/true);
27699 if (processing_template_decl
)
27700 pop_deferring_access_checks ();
27702 /* Leave the scope of the containing function. */
27703 if (function_scope
)
27704 pop_function_context ();
27705 cp_parser_pop_lexer (parser
);
27708 /* Remove any template parameters from the symbol table. */
27709 maybe_end_member_template_processing ();
27711 /* Restore the queue. */
27712 pop_unparsed_function_queues (parser
);
27713 timevar_pop (TV_PARSE_INMETH
);
27716 /* If DECL contains any default args, remember it on the unparsed
27717 functions queue. */
27720 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27724 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27726 probe
= TREE_CHAIN (probe
))
27727 if (TREE_PURPOSE (probe
))
27729 cp_default_arg_entry entry
= {current_class_type
, decl
};
27730 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27735 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27736 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27737 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27738 from the parameter-type-list. */
27741 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27742 tree default_arg
, tree parmtype
)
27744 cp_token_cache
*tokens
;
27748 if (default_arg
== error_mark_node
)
27749 return error_mark_node
;
27751 /* Push the saved tokens for the default argument onto the parser's
27753 tokens
= DEFARG_TOKENS (default_arg
);
27754 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27756 start_lambda_scope (decl
);
27758 /* Parse the default argument. */
27759 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27760 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27761 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27763 finish_lambda_scope ();
27765 if (parsed_arg
== error_mark_node
)
27766 cp_parser_skip_to_end_of_statement (parser
);
27768 if (!processing_template_decl
)
27770 /* In a non-template class, check conversions now. In a template,
27771 we'll wait and instantiate these as needed. */
27772 if (TREE_CODE (decl
) == PARM_DECL
)
27773 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27774 tf_warning_or_error
);
27775 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27776 parsed_arg
= error_mark_node
;
27778 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27781 /* If the token stream has not been completely used up, then
27782 there was extra junk after the end of the default
27784 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27786 if (TREE_CODE (decl
) == PARM_DECL
)
27787 cp_parser_error (parser
, "expected %<,%>");
27789 cp_parser_error (parser
, "expected %<;%>");
27792 /* Revert to the main lexer. */
27793 cp_parser_pop_lexer (parser
);
27798 /* FIELD is a non-static data member with an initializer which we saved for
27799 later; parse it now. */
27802 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27806 maybe_begin_member_template_processing (field
);
27808 push_unparsed_function_queues (parser
);
27809 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27810 DECL_INITIAL (field
),
27812 pop_unparsed_function_queues (parser
);
27814 maybe_end_member_template_processing ();
27816 DECL_INITIAL (field
) = def
;
27819 /* FN is a FUNCTION_DECL which may contains a parameter with an
27820 unparsed DEFAULT_ARG. Parse the default args now. This function
27821 assumes that the current scope is the scope in which the default
27822 argument should be processed. */
27825 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27827 bool saved_local_variables_forbidden_p
;
27828 tree parm
, parmdecl
;
27830 /* While we're parsing the default args, we might (due to the
27831 statement expression extension) encounter more classes. We want
27832 to handle them right away, but we don't want them getting mixed
27833 up with default args that are currently in the queue. */
27834 push_unparsed_function_queues (parser
);
27836 /* Local variable names (and the `this' keyword) may not appear
27837 in a default argument. */
27838 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27839 parser
->local_variables_forbidden_p
= true;
27841 push_defarg_context (fn
);
27843 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27844 parmdecl
= DECL_ARGUMENTS (fn
);
27845 parm
&& parm
!= void_list_node
;
27846 parm
= TREE_CHAIN (parm
),
27847 parmdecl
= DECL_CHAIN (parmdecl
))
27849 tree default_arg
= TREE_PURPOSE (parm
);
27851 vec
<tree
, va_gc
> *insts
;
27858 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27859 /* This can happen for a friend declaration for a function
27860 already declared with default arguments. */
27864 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27866 TREE_VALUE (parm
));
27867 TREE_PURPOSE (parm
) = parsed_arg
;
27869 /* Update any instantiations we've already created. */
27870 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27871 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27872 TREE_PURPOSE (copy
) = parsed_arg
;
27875 pop_defarg_context ();
27877 /* Make sure no default arg is missing. */
27878 check_default_args (fn
);
27880 /* Restore the state of local_variables_forbidden_p. */
27881 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27883 /* Restore the queue. */
27884 pop_unparsed_function_queues (parser
);
27887 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27889 sizeof ... ( identifier )
27891 where the 'sizeof' token has already been consumed. */
27894 cp_parser_sizeof_pack (cp_parser
*parser
)
27896 /* Consume the `...'. */
27897 cp_lexer_consume_token (parser
->lexer
);
27898 maybe_warn_variadic_templates ();
27900 matching_parens parens
;
27901 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27903 parens
.consume_open (parser
);
27905 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27906 "%<sizeof...%> argument must be surrounded by parentheses");
27908 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27909 tree name
= cp_parser_identifier (parser
);
27910 if (name
== error_mark_node
)
27911 return error_mark_node
;
27912 /* The name is not qualified. */
27913 parser
->scope
= NULL_TREE
;
27914 parser
->qualifying_scope
= NULL_TREE
;
27915 parser
->object_scope
= NULL_TREE
;
27916 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27917 if (expr
== error_mark_node
)
27918 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27920 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27921 expr
= TREE_TYPE (expr
);
27922 else if (TREE_CODE (expr
) == CONST_DECL
)
27923 expr
= DECL_INITIAL (expr
);
27924 expr
= make_pack_expansion (expr
);
27925 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27928 parens
.require_close (parser
);
27933 /* Parse the operand of `sizeof' (or a similar operator). Returns
27934 either a TYPE or an expression, depending on the form of the
27935 input. The KEYWORD indicates which kind of expression we have
27939 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27941 tree expr
= NULL_TREE
;
27942 const char *saved_message
;
27944 bool saved_integral_constant_expression_p
;
27945 bool saved_non_integral_constant_expression_p
;
27947 /* If it's a `...', then we are computing the length of a parameter
27949 if (keyword
== RID_SIZEOF
27950 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27951 return cp_parser_sizeof_pack (parser
);
27953 /* Types cannot be defined in a `sizeof' expression. Save away the
27955 saved_message
= parser
->type_definition_forbidden_message
;
27956 /* And create the new one. */
27957 tmp
= concat ("types may not be defined in %<",
27958 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27959 "%> expressions", NULL
);
27960 parser
->type_definition_forbidden_message
= tmp
;
27962 /* The restrictions on constant-expressions do not apply inside
27963 sizeof expressions. */
27964 saved_integral_constant_expression_p
27965 = parser
->integral_constant_expression_p
;
27966 saved_non_integral_constant_expression_p
27967 = parser
->non_integral_constant_expression_p
;
27968 parser
->integral_constant_expression_p
= false;
27970 /* Do not actually evaluate the expression. */
27971 ++cp_unevaluated_operand
;
27972 ++c_inhibit_evaluation_warnings
;
27973 /* If it's a `(', then we might be looking at the type-id
27975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27977 tree type
= NULL_TREE
;
27979 /* We can't be sure yet whether we're looking at a type-id or an
27981 cp_parser_parse_tentatively (parser
);
27983 matching_parens parens
;
27984 parens
.consume_open (parser
);
27986 /* Note: as a GNU Extension, compound literals are considered
27987 postfix-expressions as they are in C99, so they are valid
27988 arguments to sizeof. See comment in cp_parser_cast_expression
27990 if (cp_parser_compound_literal_p (parser
))
27991 cp_parser_simulate_error (parser
);
27994 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27995 parser
->in_type_id_in_expr_p
= true;
27996 /* Look for the type-id. */
27997 type
= cp_parser_type_id (parser
);
27998 /* Look for the closing `)'. */
27999 parens
.require_close (parser
);
28000 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28003 /* If all went well, then we're done. */
28004 if (cp_parser_parse_definitely (parser
))
28006 cp_decl_specifier_seq decl_specs
;
28008 /* Build a trivial decl-specifier-seq. */
28009 clear_decl_specs (&decl_specs
);
28010 decl_specs
.type
= type
;
28012 /* Call grokdeclarator to figure out what type this is. */
28013 expr
= grokdeclarator (NULL
,
28017 /*attrlist=*/NULL
);
28021 /* If the type-id production did not work out, then we must be
28022 looking at the unary-expression production. */
28024 expr
= cp_parser_unary_expression (parser
);
28026 /* Go back to evaluating expressions. */
28027 --cp_unevaluated_operand
;
28028 --c_inhibit_evaluation_warnings
;
28030 /* Free the message we created. */
28032 /* And restore the old one. */
28033 parser
->type_definition_forbidden_message
= saved_message
;
28034 parser
->integral_constant_expression_p
28035 = saved_integral_constant_expression_p
;
28036 parser
->non_integral_constant_expression_p
28037 = saved_non_integral_constant_expression_p
;
28042 /* If the current declaration has no declarator, return true. */
28045 cp_parser_declares_only_class_p (cp_parser
*parser
)
28047 /* If the next token is a `;' or a `,' then there is no
28049 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28050 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28053 /* Update the DECL_SPECS to reflect the storage class indicated by
28057 cp_parser_set_storage_class (cp_parser
*parser
,
28058 cp_decl_specifier_seq
*decl_specs
,
28062 cp_storage_class storage_class
;
28064 if (parser
->in_unbraced_linkage_specification_p
)
28066 error_at (token
->location
, "invalid use of %qD in linkage specification",
28067 ridpointers
[keyword
]);
28070 else if (decl_specs
->storage_class
!= sc_none
)
28072 decl_specs
->conflicting_specifiers_p
= true;
28076 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28077 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28078 && decl_specs
->gnu_thread_keyword_p
)
28080 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28081 "%<__thread%> before %qD", ridpointers
[keyword
]);
28087 storage_class
= sc_auto
;
28090 storage_class
= sc_register
;
28093 storage_class
= sc_static
;
28096 storage_class
= sc_extern
;
28099 storage_class
= sc_mutable
;
28102 gcc_unreachable ();
28104 decl_specs
->storage_class
= storage_class
;
28105 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28107 /* A storage class specifier cannot be applied alongside a typedef
28108 specifier. If there is a typedef specifier present then set
28109 conflicting_specifiers_p which will trigger an error later
28110 on in grokdeclarator. */
28111 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28112 decl_specs
->conflicting_specifiers_p
= true;
28115 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28116 is true, the type is a class or enum definition. */
28119 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28122 bool type_definition_p
)
28124 decl_specs
->any_specifiers_p
= true;
28126 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28127 (with, for example, in "typedef int wchar_t;") we remember that
28128 this is what happened. In system headers, we ignore these
28129 declarations so that G++ can work with system headers that are not
28131 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28132 && !type_definition_p
28133 && (type_spec
== boolean_type_node
28134 || type_spec
== char16_type_node
28135 || type_spec
== char32_type_node
28136 || type_spec
== wchar_type_node
)
28137 && (decl_specs
->type
28138 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28139 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28140 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28141 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28143 decl_specs
->redefined_builtin_type
= type_spec
;
28144 set_and_check_decl_spec_loc (decl_specs
,
28145 ds_redefined_builtin_type_spec
,
28147 if (!decl_specs
->type
)
28149 decl_specs
->type
= type_spec
;
28150 decl_specs
->type_definition_p
= false;
28151 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28154 else if (decl_specs
->type
)
28155 decl_specs
->multiple_types_p
= true;
28158 decl_specs
->type
= type_spec
;
28159 decl_specs
->type_definition_p
= type_definition_p
;
28160 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28161 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28165 /* True iff TOKEN is the GNU keyword __thread. */
28168 token_is__thread (cp_token
*token
)
28170 gcc_assert (token
->keyword
== RID_THREAD
);
28171 return id_equal (token
->u
.value
, "__thread");
28174 /* Set the location for a declarator specifier and check if it is
28177 DECL_SPECS is the sequence of declarator specifiers onto which to
28180 DS is the single declarator specifier to set which location is to
28181 be set onto the existing sequence of declarators.
28183 LOCATION is the location for the declarator specifier to
28187 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28188 cp_decl_spec ds
, cp_token
*token
)
28190 gcc_assert (ds
< ds_last
);
28192 if (decl_specs
== NULL
)
28195 source_location location
= token
->location
;
28197 if (decl_specs
->locations
[ds
] == 0)
28199 decl_specs
->locations
[ds
] = location
;
28200 if (ds
== ds_thread
)
28201 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28207 if (decl_specs
->locations
[ds_long_long
] != 0)
28208 error_at (location
,
28209 "%<long long long%> is too long for GCC");
28212 decl_specs
->locations
[ds_long_long
] = location
;
28213 pedwarn_cxx98 (location
,
28215 "ISO C++ 1998 does not support %<long long%>");
28218 else if (ds
== ds_thread
)
28220 bool gnu
= token_is__thread (token
);
28221 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28222 error_at (location
,
28223 "both %<__thread%> and %<thread_local%> specified");
28226 gcc_rich_location
richloc (location
);
28227 richloc
.add_fixit_remove ();
28228 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28233 static const char *const decl_spec_names
[] = {
28250 gcc_rich_location
richloc (location
);
28251 richloc
.add_fixit_remove ();
28252 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28257 /* Return true iff the declarator specifier DS is present in the
28258 sequence of declarator specifiers DECL_SPECS. */
28261 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28264 gcc_assert (ds
< ds_last
);
28266 if (decl_specs
== NULL
)
28269 return decl_specs
->locations
[ds
] != 0;
28272 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28273 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28276 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28278 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28281 /* Issue an error message indicating that TOKEN_DESC was expected.
28282 If KEYWORD is true, it indicated this function is called by
28283 cp_parser_require_keword and the required token can only be
28284 a indicated keyword.
28286 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28287 within any error as the location of an "opening" token matching
28288 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28289 RT_CLOSE_PAREN). */
28292 cp_parser_required_error (cp_parser
*parser
,
28293 required_token token_desc
,
28295 location_t matching_location
)
28297 if (cp_parser_simulate_error (parser
))
28300 const char *gmsgid
= NULL
;
28301 switch (token_desc
)
28304 gmsgid
= G_("expected %<new%>");
28307 gmsgid
= G_("expected %<delete%>");
28310 gmsgid
= G_("expected %<return%>");
28313 gmsgid
= G_("expected %<while%>");
28316 gmsgid
= G_("expected %<extern%>");
28318 case RT_STATIC_ASSERT
:
28319 gmsgid
= G_("expected %<static_assert%>");
28322 gmsgid
= G_("expected %<decltype%>");
28325 gmsgid
= G_("expected %<operator%>");
28328 gmsgid
= G_("expected %<class%>");
28331 gmsgid
= G_("expected %<template%>");
28334 gmsgid
= G_("expected %<namespace%>");
28337 gmsgid
= G_("expected %<using%>");
28340 gmsgid
= G_("expected %<asm%>");
28343 gmsgid
= G_("expected %<try%>");
28346 gmsgid
= G_("expected %<catch%>");
28349 gmsgid
= G_("expected %<throw%>");
28352 gmsgid
= G_("expected %<__label__%>");
28355 gmsgid
= G_("expected %<@try%>");
28357 case RT_AT_SYNCHRONIZED
:
28358 gmsgid
= G_("expected %<@synchronized%>");
28361 gmsgid
= G_("expected %<@throw%>");
28363 case RT_TRANSACTION_ATOMIC
:
28364 gmsgid
= G_("expected %<__transaction_atomic%>");
28366 case RT_TRANSACTION_RELAXED
:
28367 gmsgid
= G_("expected %<__transaction_relaxed%>");
28373 if (!gmsgid
&& !keyword
)
28375 switch (token_desc
)
28378 gmsgid
= G_("expected %<;%>");
28380 case RT_OPEN_PAREN
:
28381 gmsgid
= G_("expected %<(%>");
28383 case RT_CLOSE_BRACE
:
28384 gmsgid
= G_("expected %<}%>");
28386 case RT_OPEN_BRACE
:
28387 gmsgid
= G_("expected %<{%>");
28389 case RT_CLOSE_SQUARE
:
28390 gmsgid
= G_("expected %<]%>");
28392 case RT_OPEN_SQUARE
:
28393 gmsgid
= G_("expected %<[%>");
28396 gmsgid
= G_("expected %<,%>");
28399 gmsgid
= G_("expected %<::%>");
28402 gmsgid
= G_("expected %<<%>");
28405 gmsgid
= G_("expected %<>%>");
28408 gmsgid
= G_("expected %<=%>");
28411 gmsgid
= G_("expected %<...%>");
28414 gmsgid
= G_("expected %<*%>");
28417 gmsgid
= G_("expected %<~%>");
28420 gmsgid
= G_("expected %<:%>");
28422 case RT_COLON_SCOPE
:
28423 gmsgid
= G_("expected %<:%> or %<::%>");
28425 case RT_CLOSE_PAREN
:
28426 gmsgid
= G_("expected %<)%>");
28428 case RT_COMMA_CLOSE_PAREN
:
28429 gmsgid
= G_("expected %<,%> or %<)%>");
28431 case RT_PRAGMA_EOL
:
28432 gmsgid
= G_("expected end of line");
28435 gmsgid
= G_("expected identifier");
28438 gmsgid
= G_("expected selection-statement");
28441 gmsgid
= G_("expected iteration-statement");
28444 gmsgid
= G_("expected jump-statement");
28447 gmsgid
= G_("expected class-key");
28449 case RT_CLASS_TYPENAME_TEMPLATE
:
28450 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28453 gcc_unreachable ();
28458 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28462 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28463 issue an error message indicating that TOKEN_DESC was expected.
28465 Returns the token consumed, if the token had the appropriate type.
28466 Otherwise, returns NULL.
28468 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28469 within any error as the location of an "opening" token matching
28470 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28471 RT_CLOSE_PAREN). */
28474 cp_parser_require (cp_parser
* parser
,
28475 enum cpp_ttype type
,
28476 required_token token_desc
,
28477 location_t matching_location
)
28479 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28480 return cp_lexer_consume_token (parser
->lexer
);
28483 /* Output the MESSAGE -- unless we're parsing tentatively. */
28484 if (!cp_parser_simulate_error (parser
))
28485 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28486 matching_location
);
28491 /* An error message is produced if the next token is not '>'.
28492 All further tokens are skipped until the desired token is
28493 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28496 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28498 /* Current level of '< ... >'. */
28499 unsigned level
= 0;
28500 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28501 unsigned nesting_depth
= 0;
28503 /* Are we ready, yet? If not, issue error message. */
28504 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28507 /* Skip tokens until the desired token is found. */
28510 /* Peek at the next token. */
28511 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28514 if (!nesting_depth
)
28519 if (cxx_dialect
== cxx98
)
28520 /* C++0x views the `>>' operator as two `>' tokens, but
28523 else if (!nesting_depth
&& level
-- == 0)
28525 /* We've hit a `>>' where the first `>' closes the
28526 template argument list, and the second `>' is
28527 spurious. Just consume the `>>' and stop; we've
28528 already produced at least one error. */
28529 cp_lexer_consume_token (parser
->lexer
);
28532 /* Fall through for C++0x, so we handle the second `>' in
28534 gcc_fallthrough ();
28537 if (!nesting_depth
&& level
-- == 0)
28539 /* We've reached the token we want, consume it and stop. */
28540 cp_lexer_consume_token (parser
->lexer
);
28545 case CPP_OPEN_PAREN
:
28546 case CPP_OPEN_SQUARE
:
28550 case CPP_CLOSE_PAREN
:
28551 case CPP_CLOSE_SQUARE
:
28552 if (nesting_depth
-- == 0)
28557 case CPP_PRAGMA_EOL
:
28558 case CPP_SEMICOLON
:
28559 case CPP_OPEN_BRACE
:
28560 case CPP_CLOSE_BRACE
:
28561 /* The '>' was probably forgotten, don't look further. */
28568 /* Consume this token. */
28569 cp_lexer_consume_token (parser
->lexer
);
28573 /* If the next token is the indicated keyword, consume it. Otherwise,
28574 issue an error message indicating that TOKEN_DESC was expected.
28576 Returns the token consumed, if the token had the appropriate type.
28577 Otherwise, returns NULL. */
28580 cp_parser_require_keyword (cp_parser
* parser
,
28582 required_token token_desc
)
28584 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28586 if (token
&& token
->keyword
!= keyword
)
28588 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28596 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28597 function-definition. */
28600 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28602 return (/* An ordinary function-body begins with an `{'. */
28603 token
->type
== CPP_OPEN_BRACE
28604 /* A ctor-initializer begins with a `:'. */
28605 || token
->type
== CPP_COLON
28606 /* A function-try-block begins with `try'. */
28607 || token
->keyword
== RID_TRY
28608 /* A function-transaction-block begins with `__transaction_atomic'
28609 or `__transaction_relaxed'. */
28610 || token
->keyword
== RID_TRANSACTION_ATOMIC
28611 || token
->keyword
== RID_TRANSACTION_RELAXED
28612 /* The named return value extension begins with `return'. */
28613 || token
->keyword
== RID_RETURN
);
28616 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28620 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28624 token
= cp_lexer_peek_token (parser
->lexer
);
28625 return (token
->type
== CPP_OPEN_BRACE
28626 || (token
->type
== CPP_COLON
28627 && !parser
->colon_doesnt_start_class_def_p
));
28630 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28631 C++0x) ending a template-argument. */
28634 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28638 token
= cp_lexer_peek_token (parser
->lexer
);
28639 return (token
->type
== CPP_COMMA
28640 || token
->type
== CPP_GREATER
28641 || token
->type
== CPP_ELLIPSIS
28642 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28645 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28646 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28649 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28654 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28655 if (token
->type
== CPP_LESS
)
28657 /* Check for the sequence `<::' in the original code. It would be lexed as
28658 `[:', where `[' is a digraph, and there is no whitespace before
28660 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28663 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28664 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28670 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28671 or none_type otherwise. */
28673 static enum tag_types
28674 cp_parser_token_is_class_key (cp_token
* token
)
28676 switch (token
->keyword
)
28681 return record_type
;
28690 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28691 or none_type otherwise or if the token is null. */
28693 static enum tag_types
28694 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28699 switch (token
->keyword
)
28704 return typename_type
;
28711 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28714 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28716 if (type
== error_mark_node
)
28718 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28720 if (permerror (input_location
, "%qs tag used in naming %q#T",
28721 class_key
== union_type
? "union"
28722 : class_key
== record_type
? "struct" : "class",
28724 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28725 "%q#T was previously declared here", type
);
28729 /* Issue an error message if DECL is redeclared with different
28730 access than its original declaration [class.access.spec/3].
28731 This applies to nested classes, nested class templates and
28732 enumerations [class.mem/1]. */
28735 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28738 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28739 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28742 if ((TREE_PRIVATE (decl
)
28743 != (current_access_specifier
== access_private_node
))
28744 || (TREE_PROTECTED (decl
)
28745 != (current_access_specifier
== access_protected_node
)))
28746 error_at (location
, "%qD redeclared with different access", decl
);
28749 /* Look for the `template' keyword, as a syntactic disambiguator.
28750 Return TRUE iff it is present, in which case it will be
28754 cp_parser_optional_template_keyword (cp_parser
*parser
)
28756 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28758 /* In C++98 the `template' keyword can only be used within templates;
28759 outside templates the parser can always figure out what is a
28760 template and what is not. In C++11, per the resolution of DR 468,
28761 `template' is allowed in cases where it is not strictly necessary. */
28762 if (!processing_template_decl
28763 && pedantic
&& cxx_dialect
== cxx98
)
28765 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28766 pedwarn (token
->location
, OPT_Wpedantic
,
28767 "in C++98 %<template%> (as a disambiguator) is only "
28768 "allowed within templates");
28769 /* If this part of the token stream is rescanned, the same
28770 error message would be generated. So, we purge the token
28771 from the stream. */
28772 cp_lexer_purge_token (parser
->lexer
);
28777 /* Consume the `template' keyword. */
28778 cp_lexer_consume_token (parser
->lexer
);
28785 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28786 set PARSER->SCOPE, and perform other related actions. */
28789 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28791 struct tree_check
*check_value
;
28793 /* Get the stored value. */
28794 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28795 /* Set the scope from the stored value. */
28796 parser
->scope
= saved_checks_value (check_value
);
28797 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28798 parser
->object_scope
= NULL_TREE
;
28801 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28802 encounter the end of a block before what we were looking for. */
28805 cp_parser_cache_group (cp_parser
*parser
,
28806 enum cpp_ttype end
,
28811 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28813 /* Abort a parenthesized expression if we encounter a semicolon. */
28814 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28815 && token
->type
== CPP_SEMICOLON
)
28817 /* If we've reached the end of the file, stop. */
28818 if (token
->type
== CPP_EOF
28819 || (end
!= CPP_PRAGMA_EOL
28820 && token
->type
== CPP_PRAGMA_EOL
))
28822 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28823 /* We've hit the end of an enclosing block, so there's been some
28824 kind of syntax error. */
28827 /* Consume the token. */
28828 cp_lexer_consume_token (parser
->lexer
);
28829 /* See if it starts a new group. */
28830 if (token
->type
== CPP_OPEN_BRACE
)
28832 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28833 /* In theory this should probably check end == '}', but
28834 cp_parser_save_member_function_body needs it to exit
28835 after either '}' or ')' when called with ')'. */
28839 else if (token
->type
== CPP_OPEN_PAREN
)
28841 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28842 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28845 else if (token
->type
== CPP_PRAGMA
)
28846 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28847 else if (token
->type
== end
)
28852 /* Like above, for caching a default argument or NSDMI. Both of these are
28853 terminated by a non-nested comma, but it can be unclear whether or not a
28854 comma is nested in a template argument list unless we do more parsing.
28855 In order to handle this ambiguity, when we encounter a ',' after a '<'
28856 we try to parse what follows as a parameter-declaration-list (in the
28857 case of a default argument) or a member-declarator (in the case of an
28858 NSDMI). If that succeeds, then we stop caching. */
28861 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28863 unsigned depth
= 0;
28864 int maybe_template_id
= 0;
28865 cp_token
*first_token
;
28867 tree default_argument
;
28869 /* Add tokens until we have processed the entire default
28870 argument. We add the range [first_token, token). */
28871 first_token
= cp_lexer_peek_token (parser
->lexer
);
28872 if (first_token
->type
== CPP_OPEN_BRACE
)
28874 /* For list-initialization, this is straightforward. */
28875 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28876 token
= cp_lexer_peek_token (parser
->lexer
);
28882 /* Peek at the next token. */
28883 token
= cp_lexer_peek_token (parser
->lexer
);
28884 /* What we do depends on what token we have. */
28885 switch (token
->type
)
28887 /* In valid code, a default argument must be
28888 immediately followed by a `,' `)', or `...'. */
28890 if (depth
== 0 && maybe_template_id
)
28892 /* If we've seen a '<', we might be in a
28893 template-argument-list. Until Core issue 325 is
28894 resolved, we don't know how this situation ought
28895 to be handled, so try to DTRT. We check whether
28896 what comes after the comma is a valid parameter
28897 declaration list. If it is, then the comma ends
28898 the default argument; otherwise the default
28899 argument continues. */
28900 bool error
= false;
28903 /* Set ITALP so cp_parser_parameter_declaration_list
28904 doesn't decide to commit to this parse. */
28905 bool saved_italp
= parser
->in_template_argument_list_p
;
28906 parser
->in_template_argument_list_p
= true;
28908 cp_parser_parse_tentatively (parser
);
28912 /* Parse declarators until we reach a non-comma or
28913 somthing that cannot be an initializer.
28914 Just checking whether we're looking at a single
28915 declarator is insufficient. Consider:
28916 int var = tuple<T,U>::x;
28917 The template parameter 'U' looks exactly like a
28921 int ctor_dtor_or_conv_p
;
28922 cp_lexer_consume_token (parser
->lexer
);
28923 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28924 &ctor_dtor_or_conv_p
,
28925 /*parenthesized_p=*/NULL
,
28927 /*friend_p=*/false);
28928 peek
= cp_lexer_peek_token (parser
->lexer
);
28929 if (cp_parser_error_occurred (parser
))
28932 while (peek
->type
== CPP_COMMA
);
28933 /* If we met an '=' or ';' then the original comma
28934 was the end of the NSDMI. Otherwise assume
28935 we're still in the NSDMI. */
28936 error
= (peek
->type
!= CPP_EQ
28937 && peek
->type
!= CPP_SEMICOLON
);
28941 cp_lexer_consume_token (parser
->lexer
);
28942 begin_scope (sk_function_parms
, NULL_TREE
);
28943 cp_parser_parameter_declaration_list (parser
, &error
);
28944 pop_bindings_and_leave_scope ();
28946 if (!cp_parser_error_occurred (parser
) && !error
)
28948 cp_parser_abort_tentative_parse (parser
);
28950 parser
->in_template_argument_list_p
= saved_italp
;
28954 case CPP_CLOSE_PAREN
:
28956 /* If we run into a non-nested `;', `}', or `]',
28957 then the code is invalid -- but the default
28958 argument is certainly over. */
28959 case CPP_SEMICOLON
:
28960 case CPP_CLOSE_BRACE
:
28961 case CPP_CLOSE_SQUARE
:
28963 /* Handle correctly int n = sizeof ... ( p ); */
28964 && token
->type
!= CPP_ELLIPSIS
)
28966 /* Update DEPTH, if necessary. */
28967 else if (token
->type
== CPP_CLOSE_PAREN
28968 || token
->type
== CPP_CLOSE_BRACE
28969 || token
->type
== CPP_CLOSE_SQUARE
)
28973 case CPP_OPEN_PAREN
:
28974 case CPP_OPEN_SQUARE
:
28975 case CPP_OPEN_BRACE
:
28981 /* This might be the comparison operator, or it might
28982 start a template argument list. */
28983 ++maybe_template_id
;
28987 if (cxx_dialect
== cxx98
)
28989 /* Fall through for C++0x, which treats the `>>'
28990 operator like two `>' tokens in certain
28992 gcc_fallthrough ();
28997 /* This might be an operator, or it might close a
28998 template argument list. But if a previous '<'
28999 started a template argument list, this will have
29000 closed it, so we can't be in one anymore. */
29001 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29002 if (maybe_template_id
< 0)
29003 maybe_template_id
= 0;
29007 /* If we run out of tokens, issue an error message. */
29009 case CPP_PRAGMA_EOL
:
29010 error_at (token
->location
, "file ends in default argument");
29011 return error_mark_node
;
29015 /* In these cases, we should look for template-ids.
29016 For example, if the default argument is
29017 `X<int, double>()', we need to do name lookup to
29018 figure out whether or not `X' is a template; if
29019 so, the `,' does not end the default argument.
29021 That is not yet done. */
29028 /* If we've reached the end, stop. */
29032 /* Add the token to the token block. */
29033 token
= cp_lexer_consume_token (parser
->lexer
);
29036 /* Create a DEFAULT_ARG to represent the unparsed default
29038 default_argument
= make_node (DEFAULT_ARG
);
29039 DEFARG_TOKENS (default_argument
)
29040 = cp_token_cache_new (first_token
, token
);
29041 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29043 return default_argument
;
29046 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29049 defarg_location (tree default_argument
)
29051 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29052 location_t start
= tokens
->first
->location
;
29053 location_t end
= tokens
->last
->location
;
29054 return make_location (start
, start
, end
);
29057 /* Begin parsing tentatively. We always save tokens while parsing
29058 tentatively so that if the tentative parsing fails we can restore the
29062 cp_parser_parse_tentatively (cp_parser
* parser
)
29064 /* Enter a new parsing context. */
29065 parser
->context
= cp_parser_context_new (parser
->context
);
29066 /* Begin saving tokens. */
29067 cp_lexer_save_tokens (parser
->lexer
);
29068 /* In order to avoid repetitive access control error messages,
29069 access checks are queued up until we are no longer parsing
29071 push_deferring_access_checks (dk_deferred
);
29074 /* Commit to the currently active tentative parse. */
29077 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29079 cp_parser_context
*context
;
29082 /* Mark all of the levels as committed. */
29083 lexer
= parser
->lexer
;
29084 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29086 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29088 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29089 while (!cp_lexer_saving_tokens (lexer
))
29090 lexer
= lexer
->next
;
29091 cp_lexer_commit_tokens (lexer
);
29095 /* Commit to the topmost currently active tentative parse.
29097 Note that this function shouldn't be called when there are
29098 irreversible side-effects while in a tentative state. For
29099 example, we shouldn't create a permanent entry in the symbol
29100 table, or issue an error message that might not apply if the
29101 tentative parse is aborted. */
29104 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29106 cp_parser_context
*context
= parser
->context
;
29107 cp_lexer
*lexer
= parser
->lexer
;
29111 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29113 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29115 while (!cp_lexer_saving_tokens (lexer
))
29116 lexer
= lexer
->next
;
29117 cp_lexer_commit_tokens (lexer
);
29121 /* Abort the currently active tentative parse. All consumed tokens
29122 will be rolled back, and no diagnostics will be issued. */
29125 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29127 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29128 || errorcount
> 0);
29129 cp_parser_simulate_error (parser
);
29130 /* Now, pretend that we want to see if the construct was
29131 successfully parsed. */
29132 cp_parser_parse_definitely (parser
);
29135 /* Stop parsing tentatively. If a parse error has occurred, restore the
29136 token stream. Otherwise, commit to the tokens we have consumed.
29137 Returns true if no error occurred; false otherwise. */
29140 cp_parser_parse_definitely (cp_parser
* parser
)
29142 bool error_occurred
;
29143 cp_parser_context
*context
;
29145 /* Remember whether or not an error occurred, since we are about to
29146 destroy that information. */
29147 error_occurred
= cp_parser_error_occurred (parser
);
29148 /* Remove the topmost context from the stack. */
29149 context
= parser
->context
;
29150 parser
->context
= context
->next
;
29151 /* If no parse errors occurred, commit to the tentative parse. */
29152 if (!error_occurred
)
29154 /* Commit to the tokens read tentatively, unless that was
29156 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29157 cp_lexer_commit_tokens (parser
->lexer
);
29159 pop_to_parent_deferring_access_checks ();
29161 /* Otherwise, if errors occurred, roll back our state so that things
29162 are just as they were before we began the tentative parse. */
29165 cp_lexer_rollback_tokens (parser
->lexer
);
29166 pop_deferring_access_checks ();
29168 /* Add the context to the front of the free list. */
29169 context
->next
= cp_parser_context_free_list
;
29170 cp_parser_context_free_list
= context
;
29172 return !error_occurred
;
29175 /* Returns true if we are parsing tentatively and are not committed to
29176 this tentative parse. */
29179 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29181 return (cp_parser_parsing_tentatively (parser
)
29182 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29185 /* Returns nonzero iff an error has occurred during the most recent
29186 tentative parse. */
29189 cp_parser_error_occurred (cp_parser
* parser
)
29191 return (cp_parser_parsing_tentatively (parser
)
29192 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29195 /* Returns nonzero if GNU extensions are allowed. */
29198 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29200 return parser
->allow_gnu_extensions_p
;
29203 /* Objective-C++ Productions */
29206 /* Parse an Objective-C expression, which feeds into a primary-expression
29210 objc-message-expression
29211 objc-string-literal
29212 objc-encode-expression
29213 objc-protocol-expression
29214 objc-selector-expression
29216 Returns a tree representation of the expression. */
29219 cp_parser_objc_expression (cp_parser
* parser
)
29221 /* Try to figure out what kind of declaration is present. */
29222 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29226 case CPP_OPEN_SQUARE
:
29227 return cp_parser_objc_message_expression (parser
);
29229 case CPP_OBJC_STRING
:
29230 kwd
= cp_lexer_consume_token (parser
->lexer
);
29231 return objc_build_string_object (kwd
->u
.value
);
29234 switch (kwd
->keyword
)
29236 case RID_AT_ENCODE
:
29237 return cp_parser_objc_encode_expression (parser
);
29239 case RID_AT_PROTOCOL
:
29240 return cp_parser_objc_protocol_expression (parser
);
29242 case RID_AT_SELECTOR
:
29243 return cp_parser_objc_selector_expression (parser
);
29250 error_at (kwd
->location
,
29251 "misplaced %<@%D%> Objective-C++ construct",
29253 cp_parser_skip_to_end_of_block_or_statement (parser
);
29256 return error_mark_node
;
29259 /* Parse an Objective-C message expression.
29261 objc-message-expression:
29262 [ objc-message-receiver objc-message-args ]
29264 Returns a representation of an Objective-C message. */
29267 cp_parser_objc_message_expression (cp_parser
* parser
)
29269 tree receiver
, messageargs
;
29271 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29272 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29273 receiver
= cp_parser_objc_message_receiver (parser
);
29274 messageargs
= cp_parser_objc_message_args (parser
);
29275 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29276 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29278 tree result
= objc_build_message_expr (receiver
, messageargs
);
29280 /* Construct a location e.g.
29283 ranging from the '[' to the ']', with the caret at the start. */
29284 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29285 protected_set_expr_location (result
, combined_loc
);
29290 /* Parse an objc-message-receiver.
29292 objc-message-receiver:
29294 simple-type-specifier
29296 Returns a representation of the type or expression. */
29299 cp_parser_objc_message_receiver (cp_parser
* parser
)
29303 /* An Objective-C message receiver may be either (1) a type
29304 or (2) an expression. */
29305 cp_parser_parse_tentatively (parser
);
29306 rcv
= cp_parser_expression (parser
);
29308 /* If that worked out, fine. */
29309 if (cp_parser_parse_definitely (parser
))
29312 cp_parser_parse_tentatively (parser
);
29313 rcv
= cp_parser_simple_type_specifier (parser
,
29314 /*decl_specs=*/NULL
,
29315 CP_PARSER_FLAGS_NONE
);
29317 if (cp_parser_parse_definitely (parser
))
29318 return objc_get_class_reference (rcv
);
29320 cp_parser_error (parser
, "objective-c++ message receiver expected");
29321 return error_mark_node
;
29324 /* Parse the arguments and selectors comprising an Objective-C message.
29329 objc-selector-args , objc-comma-args
29331 objc-selector-args:
29332 objc-selector [opt] : assignment-expression
29333 objc-selector-args objc-selector [opt] : assignment-expression
29336 assignment-expression
29337 objc-comma-args , assignment-expression
29339 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29340 selector arguments and TREE_VALUE containing a list of comma
29344 cp_parser_objc_message_args (cp_parser
* parser
)
29346 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29347 bool maybe_unary_selector_p
= true;
29348 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29350 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29352 tree selector
= NULL_TREE
, arg
;
29354 if (token
->type
!= CPP_COLON
)
29355 selector
= cp_parser_objc_selector (parser
);
29357 /* Detect if we have a unary selector. */
29358 if (maybe_unary_selector_p
29359 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29360 return build_tree_list (selector
, NULL_TREE
);
29362 maybe_unary_selector_p
= false;
29363 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29364 arg
= cp_parser_assignment_expression (parser
);
29367 = chainon (sel_args
,
29368 build_tree_list (selector
, arg
));
29370 token
= cp_lexer_peek_token (parser
->lexer
);
29373 /* Handle non-selector arguments, if any. */
29374 while (token
->type
== CPP_COMMA
)
29378 cp_lexer_consume_token (parser
->lexer
);
29379 arg
= cp_parser_assignment_expression (parser
);
29382 = chainon (addl_args
,
29383 build_tree_list (NULL_TREE
, arg
));
29385 token
= cp_lexer_peek_token (parser
->lexer
);
29388 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29390 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29391 return build_tree_list (error_mark_node
, error_mark_node
);
29394 return build_tree_list (sel_args
, addl_args
);
29397 /* Parse an Objective-C encode expression.
29399 objc-encode-expression:
29400 @encode objc-typename
29402 Returns an encoded representation of the type argument. */
29405 cp_parser_objc_encode_expression (cp_parser
* parser
)
29409 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29411 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29412 matching_parens parens
;
29413 parens
.require_open (parser
);
29414 token
= cp_lexer_peek_token (parser
->lexer
);
29415 type
= complete_type (cp_parser_type_id (parser
));
29416 parens
.require_close (parser
);
29420 error_at (token
->location
,
29421 "%<@encode%> must specify a type as an argument");
29422 return error_mark_node
;
29425 /* This happens if we find @encode(T) (where T is a template
29426 typename or something dependent on a template typename) when
29427 parsing a template. In that case, we can't compile it
29428 immediately, but we rather create an AT_ENCODE_EXPR which will
29429 need to be instantiated when the template is used.
29431 if (dependent_type_p (type
))
29433 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29434 TREE_READONLY (value
) = 1;
29439 /* Build a location of the form:
29442 with caret==start at the @ token, finishing at the close paren. */
29443 location_t combined_loc
29444 = make_location (start_loc
, start_loc
,
29445 cp_lexer_previous_token (parser
->lexer
)->location
);
29447 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29450 /* Parse an Objective-C @defs expression. */
29453 cp_parser_objc_defs_expression (cp_parser
*parser
)
29457 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29458 matching_parens parens
;
29459 parens
.require_open (parser
);
29460 name
= cp_parser_identifier (parser
);
29461 parens
.require_close (parser
);
29463 return objc_get_class_ivars (name
);
29466 /* Parse an Objective-C protocol expression.
29468 objc-protocol-expression:
29469 @protocol ( identifier )
29471 Returns a representation of the protocol expression. */
29474 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29477 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29479 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29480 matching_parens parens
;
29481 parens
.require_open (parser
);
29482 proto
= cp_parser_identifier (parser
);
29483 parens
.require_close (parser
);
29485 /* Build a location of the form:
29488 with caret==start at the @ token, finishing at the close paren. */
29489 location_t combined_loc
29490 = make_location (start_loc
, start_loc
,
29491 cp_lexer_previous_token (parser
->lexer
)->location
);
29492 tree result
= objc_build_protocol_expr (proto
);
29493 protected_set_expr_location (result
, combined_loc
);
29497 /* Parse an Objective-C selector expression.
29499 objc-selector-expression:
29500 @selector ( objc-method-signature )
29502 objc-method-signature:
29508 objc-selector-seq objc-selector :
29510 Returns a representation of the method selector. */
29513 cp_parser_objc_selector_expression (cp_parser
* parser
)
29515 tree sel_seq
= NULL_TREE
;
29516 bool maybe_unary_selector_p
= true;
29518 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29520 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29521 matching_parens parens
;
29522 parens
.require_open (parser
);
29523 token
= cp_lexer_peek_token (parser
->lexer
);
29525 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29526 || token
->type
== CPP_SCOPE
)
29528 tree selector
= NULL_TREE
;
29530 if (token
->type
!= CPP_COLON
29531 || token
->type
== CPP_SCOPE
)
29532 selector
= cp_parser_objc_selector (parser
);
29534 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29535 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29537 /* Detect if we have a unary selector. */
29538 if (maybe_unary_selector_p
)
29540 sel_seq
= selector
;
29541 goto finish_selector
;
29545 cp_parser_error (parser
, "expected %<:%>");
29548 maybe_unary_selector_p
= false;
29549 token
= cp_lexer_consume_token (parser
->lexer
);
29551 if (token
->type
== CPP_SCOPE
)
29554 = chainon (sel_seq
,
29555 build_tree_list (selector
, NULL_TREE
));
29557 = chainon (sel_seq
,
29558 build_tree_list (NULL_TREE
, NULL_TREE
));
29562 = chainon (sel_seq
,
29563 build_tree_list (selector
, NULL_TREE
));
29565 token
= cp_lexer_peek_token (parser
->lexer
);
29569 parens
.require_close (parser
);
29572 /* Build a location of the form:
29575 with caret==start at the @ token, finishing at the close paren. */
29576 location_t combined_loc
29577 = make_location (loc
, loc
,
29578 cp_lexer_previous_token (parser
->lexer
)->location
);
29579 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29580 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29581 protected_set_expr_location (result
, combined_loc
);
29585 /* Parse a list of identifiers.
29587 objc-identifier-list:
29589 objc-identifier-list , identifier
29591 Returns a TREE_LIST of identifier nodes. */
29594 cp_parser_objc_identifier_list (cp_parser
* parser
)
29600 identifier
= cp_parser_identifier (parser
);
29601 if (identifier
== error_mark_node
)
29602 return error_mark_node
;
29604 list
= build_tree_list (NULL_TREE
, identifier
);
29605 sep
= cp_lexer_peek_token (parser
->lexer
);
29607 while (sep
->type
== CPP_COMMA
)
29609 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29610 identifier
= cp_parser_identifier (parser
);
29611 if (identifier
== error_mark_node
)
29614 list
= chainon (list
, build_tree_list (NULL_TREE
,
29616 sep
= cp_lexer_peek_token (parser
->lexer
);
29622 /* Parse an Objective-C alias declaration.
29624 objc-alias-declaration:
29625 @compatibility_alias identifier identifier ;
29627 This function registers the alias mapping with the Objective-C front end.
29628 It returns nothing. */
29631 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29635 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29636 alias
= cp_parser_identifier (parser
);
29637 orig
= cp_parser_identifier (parser
);
29638 objc_declare_alias (alias
, orig
);
29639 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29642 /* Parse an Objective-C class forward-declaration.
29644 objc-class-declaration:
29645 @class objc-identifier-list ;
29647 The function registers the forward declarations with the Objective-C
29648 front end. It returns nothing. */
29651 cp_parser_objc_class_declaration (cp_parser
* parser
)
29653 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29658 id
= cp_parser_identifier (parser
);
29659 if (id
== error_mark_node
)
29662 objc_declare_class (id
);
29664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29665 cp_lexer_consume_token (parser
->lexer
);
29669 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29672 /* Parse a list of Objective-C protocol references.
29674 objc-protocol-refs-opt:
29675 objc-protocol-refs [opt]
29677 objc-protocol-refs:
29678 < objc-identifier-list >
29680 Returns a TREE_LIST of identifiers, if any. */
29683 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29685 tree protorefs
= NULL_TREE
;
29687 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29689 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29690 protorefs
= cp_parser_objc_identifier_list (parser
);
29691 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29697 /* Parse a Objective-C visibility specification. */
29700 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29702 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29704 switch (vis
->keyword
)
29706 case RID_AT_PRIVATE
:
29707 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29709 case RID_AT_PROTECTED
:
29710 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29712 case RID_AT_PUBLIC
:
29713 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29715 case RID_AT_PACKAGE
:
29716 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29722 /* Eat '@private'/'@protected'/'@public'. */
29723 cp_lexer_consume_token (parser
->lexer
);
29726 /* Parse an Objective-C method type. Return 'true' if it is a class
29727 (+) method, and 'false' if it is an instance (-) method. */
29730 cp_parser_objc_method_type (cp_parser
* parser
)
29732 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29738 /* Parse an Objective-C protocol qualifier. */
29741 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29743 tree quals
= NULL_TREE
, node
;
29744 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29746 node
= token
->u
.value
;
29748 while (node
&& identifier_p (node
)
29749 && (node
== ridpointers
[(int) RID_IN
]
29750 || node
== ridpointers
[(int) RID_OUT
]
29751 || node
== ridpointers
[(int) RID_INOUT
]
29752 || node
== ridpointers
[(int) RID_BYCOPY
]
29753 || node
== ridpointers
[(int) RID_BYREF
]
29754 || node
== ridpointers
[(int) RID_ONEWAY
]))
29756 quals
= tree_cons (NULL_TREE
, node
, quals
);
29757 cp_lexer_consume_token (parser
->lexer
);
29758 token
= cp_lexer_peek_token (parser
->lexer
);
29759 node
= token
->u
.value
;
29765 /* Parse an Objective-C typename. */
29768 cp_parser_objc_typename (cp_parser
* parser
)
29770 tree type_name
= NULL_TREE
;
29772 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29774 tree proto_quals
, cp_type
= NULL_TREE
;
29776 matching_parens parens
;
29777 parens
.consume_open (parser
); /* Eat '('. */
29778 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29780 /* An ObjC type name may consist of just protocol qualifiers, in which
29781 case the type shall default to 'id'. */
29782 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29784 cp_type
= cp_parser_type_id (parser
);
29786 /* If the type could not be parsed, an error has already
29787 been produced. For error recovery, behave as if it had
29788 not been specified, which will use the default type
29790 if (cp_type
== error_mark_node
)
29792 cp_type
= NULL_TREE
;
29793 /* We need to skip to the closing parenthesis as
29794 cp_parser_type_id() does not seem to do it for
29796 cp_parser_skip_to_closing_parenthesis (parser
,
29797 /*recovering=*/true,
29798 /*or_comma=*/false,
29799 /*consume_paren=*/false);
29803 parens
.require_close (parser
);
29804 type_name
= build_tree_list (proto_quals
, cp_type
);
29810 /* Check to see if TYPE refers to an Objective-C selector name. */
29813 cp_parser_objc_selector_p (enum cpp_ttype type
)
29815 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29816 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29817 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29818 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29819 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29822 /* Parse an Objective-C selector. */
29825 cp_parser_objc_selector (cp_parser
* parser
)
29827 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29829 if (!cp_parser_objc_selector_p (token
->type
))
29831 error_at (token
->location
, "invalid Objective-C++ selector name");
29832 return error_mark_node
;
29835 /* C++ operator names are allowed to appear in ObjC selectors. */
29836 switch (token
->type
)
29838 case CPP_AND_AND
: return get_identifier ("and");
29839 case CPP_AND_EQ
: return get_identifier ("and_eq");
29840 case CPP_AND
: return get_identifier ("bitand");
29841 case CPP_OR
: return get_identifier ("bitor");
29842 case CPP_COMPL
: return get_identifier ("compl");
29843 case CPP_NOT
: return get_identifier ("not");
29844 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29845 case CPP_OR_OR
: return get_identifier ("or");
29846 case CPP_OR_EQ
: return get_identifier ("or_eq");
29847 case CPP_XOR
: return get_identifier ("xor");
29848 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29849 default: return token
->u
.value
;
29853 /* Parse an Objective-C params list. */
29856 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29858 tree params
= NULL_TREE
;
29859 bool maybe_unary_selector_p
= true;
29860 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29862 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29864 tree selector
= NULL_TREE
, type_name
, identifier
;
29865 tree parm_attr
= NULL_TREE
;
29867 if (token
->keyword
== RID_ATTRIBUTE
)
29870 if (token
->type
!= CPP_COLON
)
29871 selector
= cp_parser_objc_selector (parser
);
29873 /* Detect if we have a unary selector. */
29874 if (maybe_unary_selector_p
29875 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29877 params
= selector
; /* Might be followed by attributes. */
29881 maybe_unary_selector_p
= false;
29882 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29884 /* Something went quite wrong. There should be a colon
29885 here, but there is not. Stop parsing parameters. */
29888 type_name
= cp_parser_objc_typename (parser
);
29889 /* New ObjC allows attributes on parameters too. */
29890 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29891 parm_attr
= cp_parser_attributes_opt (parser
);
29892 identifier
= cp_parser_identifier (parser
);
29896 objc_build_keyword_decl (selector
,
29901 token
= cp_lexer_peek_token (parser
->lexer
);
29904 if (params
== NULL_TREE
)
29906 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29907 return error_mark_node
;
29910 /* We allow tail attributes for the method. */
29911 if (token
->keyword
== RID_ATTRIBUTE
)
29913 *attributes
= cp_parser_attributes_opt (parser
);
29914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29915 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29917 cp_parser_error (parser
,
29918 "method attributes must be specified at the end");
29919 return error_mark_node
;
29922 if (params
== NULL_TREE
)
29924 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29925 return error_mark_node
;
29930 /* Parse the non-keyword Objective-C params. */
29933 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29936 tree params
= make_node (TREE_LIST
);
29937 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29938 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29940 while (token
->type
== CPP_COMMA
)
29942 cp_parameter_declarator
*parmdecl
;
29945 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29946 token
= cp_lexer_peek_token (parser
->lexer
);
29948 if (token
->type
== CPP_ELLIPSIS
)
29950 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29952 token
= cp_lexer_peek_token (parser
->lexer
);
29956 /* TODO: parse attributes for tail parameters. */
29957 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29958 parm
= grokdeclarator (parmdecl
->declarator
,
29959 &parmdecl
->decl_specifiers
,
29960 PARM
, /*initialized=*/0,
29961 /*attrlist=*/NULL
);
29963 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29964 token
= cp_lexer_peek_token (parser
->lexer
);
29967 /* We allow tail attributes for the method. */
29968 if (token
->keyword
== RID_ATTRIBUTE
)
29970 if (*attributes
== NULL_TREE
)
29972 *attributes
= cp_parser_attributes_opt (parser
);
29973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29974 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29978 /* We have an error, but parse the attributes, so that we can
29980 *attributes
= cp_parser_attributes_opt (parser
);
29982 cp_parser_error (parser
,
29983 "method attributes must be specified at the end");
29984 return error_mark_node
;
29990 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29993 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29995 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29997 /* If the next token is `extern' and the following token is a string
29998 literal, then we have a linkage specification. */
29999 if (token
->keyword
== RID_EXTERN
30000 && cp_parser_is_pure_string_literal
30001 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30002 cp_parser_linkage_specification (parser
);
30003 /* Handle #pragma, if any. */
30004 else if (token
->type
== CPP_PRAGMA
)
30005 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30006 /* Allow stray semicolons. */
30007 else if (token
->type
== CPP_SEMICOLON
)
30008 cp_lexer_consume_token (parser
->lexer
);
30009 /* Mark methods as optional or required, when building protocols. */
30010 else if (token
->keyword
== RID_AT_OPTIONAL
)
30012 cp_lexer_consume_token (parser
->lexer
);
30013 objc_set_method_opt (true);
30015 else if (token
->keyword
== RID_AT_REQUIRED
)
30017 cp_lexer_consume_token (parser
->lexer
);
30018 objc_set_method_opt (false);
30020 else if (token
->keyword
== RID_NAMESPACE
)
30021 cp_parser_namespace_definition (parser
);
30022 /* Other stray characters must generate errors. */
30023 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30025 cp_lexer_consume_token (parser
->lexer
);
30026 error ("stray %qs between Objective-C++ methods",
30027 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30029 /* Finally, try to parse a block-declaration, or a function-definition. */
30031 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30034 /* Parse a method signature. */
30037 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30039 tree rettype
, kwdparms
, optparms
;
30040 bool ellipsis
= false;
30041 bool is_class_method
;
30043 is_class_method
= cp_parser_objc_method_type (parser
);
30044 rettype
= cp_parser_objc_typename (parser
);
30045 *attributes
= NULL_TREE
;
30046 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30047 if (kwdparms
== error_mark_node
)
30048 return error_mark_node
;
30049 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30050 if (optparms
== error_mark_node
)
30051 return error_mark_node
;
30053 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30057 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30060 cp_lexer_save_tokens (parser
->lexer
);
30061 tattr
= cp_parser_attributes_opt (parser
);
30062 gcc_assert (tattr
) ;
30064 /* If the attributes are followed by a method introducer, this is not allowed.
30065 Dump the attributes and flag the situation. */
30066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30067 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30070 /* Otherwise, the attributes introduce some interstitial code, possibly so
30071 rewind to allow that check. */
30072 cp_lexer_rollback_tokens (parser
->lexer
);
30076 /* Parse an Objective-C method prototype list. */
30079 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30081 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30083 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30085 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30087 tree attributes
, sig
;
30088 bool is_class_method
;
30089 if (token
->type
== CPP_PLUS
)
30090 is_class_method
= true;
30092 is_class_method
= false;
30093 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30094 if (sig
== error_mark_node
)
30096 cp_parser_skip_to_end_of_block_or_statement (parser
);
30097 token
= cp_lexer_peek_token (parser
->lexer
);
30100 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30101 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30103 else if (token
->keyword
== RID_AT_PROPERTY
)
30104 cp_parser_objc_at_property_declaration (parser
);
30105 else if (token
->keyword
== RID_ATTRIBUTE
30106 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30107 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30109 "prefix attributes are ignored for methods");
30111 /* Allow for interspersed non-ObjC++ code. */
30112 cp_parser_objc_interstitial_code (parser
);
30114 token
= cp_lexer_peek_token (parser
->lexer
);
30117 if (token
->type
!= CPP_EOF
)
30118 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30120 cp_parser_error (parser
, "expected %<@end%>");
30122 objc_finish_interface ();
30125 /* Parse an Objective-C method definition list. */
30128 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30130 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30132 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30136 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30139 tree sig
, attribute
;
30140 bool is_class_method
;
30141 if (token
->type
== CPP_PLUS
)
30142 is_class_method
= true;
30144 is_class_method
= false;
30145 push_deferring_access_checks (dk_deferred
);
30146 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30147 if (sig
== error_mark_node
)
30149 cp_parser_skip_to_end_of_block_or_statement (parser
);
30150 token
= cp_lexer_peek_token (parser
->lexer
);
30153 objc_start_method_definition (is_class_method
, sig
, attribute
,
30156 /* For historical reasons, we accept an optional semicolon. */
30157 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30158 cp_lexer_consume_token (parser
->lexer
);
30160 ptk
= cp_lexer_peek_token (parser
->lexer
);
30161 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30162 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30164 perform_deferred_access_checks (tf_warning_or_error
);
30165 stop_deferring_access_checks ();
30166 meth
= cp_parser_function_definition_after_declarator (parser
,
30168 pop_deferring_access_checks ();
30169 objc_finish_method_definition (meth
);
30172 /* The following case will be removed once @synthesize is
30173 completely implemented. */
30174 else if (token
->keyword
== RID_AT_PROPERTY
)
30175 cp_parser_objc_at_property_declaration (parser
);
30176 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30177 cp_parser_objc_at_synthesize_declaration (parser
);
30178 else if (token
->keyword
== RID_AT_DYNAMIC
)
30179 cp_parser_objc_at_dynamic_declaration (parser
);
30180 else if (token
->keyword
== RID_ATTRIBUTE
30181 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30182 warning_at (token
->location
, OPT_Wattributes
,
30183 "prefix attributes are ignored for methods");
30185 /* Allow for interspersed non-ObjC++ code. */
30186 cp_parser_objc_interstitial_code (parser
);
30188 token
= cp_lexer_peek_token (parser
->lexer
);
30191 if (token
->type
!= CPP_EOF
)
30192 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30194 cp_parser_error (parser
, "expected %<@end%>");
30196 objc_finish_implementation ();
30199 /* Parse Objective-C ivars. */
30202 cp_parser_objc_class_ivars (cp_parser
* parser
)
30204 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30206 if (token
->type
!= CPP_OPEN_BRACE
)
30207 return; /* No ivars specified. */
30209 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30210 token
= cp_lexer_peek_token (parser
->lexer
);
30212 while (token
->type
!= CPP_CLOSE_BRACE
30213 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30215 cp_decl_specifier_seq declspecs
;
30216 int decl_class_or_enum_p
;
30217 tree prefix_attributes
;
30219 cp_parser_objc_visibility_spec (parser
);
30221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30224 cp_parser_decl_specifier_seq (parser
,
30225 CP_PARSER_FLAGS_OPTIONAL
,
30227 &decl_class_or_enum_p
);
30229 /* auto, register, static, extern, mutable. */
30230 if (declspecs
.storage_class
!= sc_none
)
30232 cp_parser_error (parser
, "invalid type for instance variable");
30233 declspecs
.storage_class
= sc_none
;
30236 /* thread_local. */
30237 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30239 cp_parser_error (parser
, "invalid type for instance variable");
30240 declspecs
.locations
[ds_thread
] = 0;
30244 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30246 cp_parser_error (parser
, "invalid type for instance variable");
30247 declspecs
.locations
[ds_typedef
] = 0;
30250 prefix_attributes
= declspecs
.attributes
;
30251 declspecs
.attributes
= NULL_TREE
;
30253 /* Keep going until we hit the `;' at the end of the
30255 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30257 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30258 cp_declarator
*declarator
= NULL
;
30259 int ctor_dtor_or_conv_p
;
30261 /* Check for a (possibly unnamed) bitfield declaration. */
30262 token
= cp_lexer_peek_token (parser
->lexer
);
30263 if (token
->type
== CPP_COLON
)
30266 if (token
->type
== CPP_NAME
30267 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30270 /* Get the name of the bitfield. */
30271 declarator
= make_id_declarator (NULL_TREE
,
30272 cp_parser_identifier (parser
),
30276 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30277 /* Get the width of the bitfield. */
30279 = cp_parser_constant_expression (parser
);
30283 /* Parse the declarator. */
30285 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30286 &ctor_dtor_or_conv_p
,
30287 /*parenthesized_p=*/NULL
,
30288 /*member_p=*/false,
30289 /*friend_p=*/false);
30292 /* Look for attributes that apply to the ivar. */
30293 attributes
= cp_parser_attributes_opt (parser
);
30294 /* Remember which attributes are prefix attributes and
30296 first_attribute
= attributes
;
30297 /* Combine the attributes. */
30298 attributes
= attr_chainon (prefix_attributes
, attributes
);
30301 /* Create the bitfield declaration. */
30302 decl
= grokbitfield (declarator
, &declspecs
,
30303 width
, NULL_TREE
, attributes
);
30305 decl
= grokfield (declarator
, &declspecs
,
30306 NULL_TREE
, /*init_const_expr_p=*/false,
30307 NULL_TREE
, attributes
);
30309 /* Add the instance variable. */
30310 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30311 objc_add_instance_variable (decl
);
30313 /* Reset PREFIX_ATTRIBUTES. */
30314 if (attributes
!= error_mark_node
)
30316 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30317 attributes
= TREE_CHAIN (attributes
);
30319 TREE_CHAIN (attributes
) = NULL_TREE
;
30322 token
= cp_lexer_peek_token (parser
->lexer
);
30324 if (token
->type
== CPP_COMMA
)
30326 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30332 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30333 token
= cp_lexer_peek_token (parser
->lexer
);
30336 if (token
->keyword
== RID_AT_END
)
30337 cp_parser_error (parser
, "expected %<}%>");
30339 /* Do not consume the RID_AT_END, so it will be read again as terminating
30340 the @interface of @implementation. */
30341 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30342 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30344 /* For historical reasons, we accept an optional semicolon. */
30345 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30346 cp_lexer_consume_token (parser
->lexer
);
30349 /* Parse an Objective-C protocol declaration. */
30352 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30354 tree proto
, protorefs
;
30357 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30358 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30360 tok
= cp_lexer_peek_token (parser
->lexer
);
30361 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30362 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30366 /* See if we have a forward declaration or a definition. */
30367 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30369 /* Try a forward declaration first. */
30370 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30376 id
= cp_parser_identifier (parser
);
30377 if (id
== error_mark_node
)
30380 objc_declare_protocol (id
, attributes
);
30382 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30383 cp_lexer_consume_token (parser
->lexer
);
30387 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30390 /* Ok, we got a full-fledged definition (or at least should). */
30393 proto
= cp_parser_identifier (parser
);
30394 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30395 objc_start_protocol (proto
, protorefs
, attributes
);
30396 cp_parser_objc_method_prototype_list (parser
);
30400 /* Parse an Objective-C superclass or category. */
30403 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30406 tree
*categ
, bool *is_class_extension
)
30408 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30410 *super
= *categ
= NULL_TREE
;
30411 *is_class_extension
= false;
30412 if (next
->type
== CPP_COLON
)
30414 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30415 *super
= cp_parser_identifier (parser
);
30417 else if (next
->type
== CPP_OPEN_PAREN
)
30419 matching_parens parens
;
30420 parens
.consume_open (parser
); /* Eat '('. */
30422 /* If there is no category name, and this is an @interface, we
30423 have a class extension. */
30424 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30426 *categ
= NULL_TREE
;
30427 *is_class_extension
= true;
30430 *categ
= cp_parser_identifier (parser
);
30432 parens
.require_close (parser
);
30436 /* Parse an Objective-C class interface. */
30439 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30441 tree name
, super
, categ
, protos
;
30442 bool is_class_extension
;
30444 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30445 name
= cp_parser_identifier (parser
);
30446 if (name
== error_mark_node
)
30448 /* It's hard to recover because even if valid @interface stuff
30449 is to follow, we can't compile it (or validate it) if we
30450 don't even know which class it refers to. Let's assume this
30451 was a stray '@interface' token in the stream and skip it.
30455 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30456 &is_class_extension
);
30457 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30459 /* We have either a class or a category on our hands. */
30460 if (categ
|| is_class_extension
)
30461 objc_start_category_interface (name
, categ
, protos
, attributes
);
30464 objc_start_class_interface (name
, super
, protos
, attributes
);
30465 /* Handle instance variable declarations, if any. */
30466 cp_parser_objc_class_ivars (parser
);
30467 objc_continue_interface ();
30470 cp_parser_objc_method_prototype_list (parser
);
30473 /* Parse an Objective-C class implementation. */
30476 cp_parser_objc_class_implementation (cp_parser
* parser
)
30478 tree name
, super
, categ
;
30479 bool is_class_extension
;
30481 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30482 name
= cp_parser_identifier (parser
);
30483 if (name
== error_mark_node
)
30485 /* It's hard to recover because even if valid @implementation
30486 stuff is to follow, we can't compile it (or validate it) if
30487 we don't even know which class it refers to. Let's assume
30488 this was a stray '@implementation' token in the stream and
30493 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30494 &is_class_extension
);
30496 /* We have either a class or a category on our hands. */
30498 objc_start_category_implementation (name
, categ
);
30501 objc_start_class_implementation (name
, super
);
30502 /* Handle instance variable declarations, if any. */
30503 cp_parser_objc_class_ivars (parser
);
30504 objc_continue_implementation ();
30507 cp_parser_objc_method_definition_list (parser
);
30510 /* Consume the @end token and finish off the implementation. */
30513 cp_parser_objc_end_implementation (cp_parser
* parser
)
30515 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30516 objc_finish_implementation ();
30519 /* Parse an Objective-C declaration. */
30522 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30524 /* Try to figure out what kind of declaration is present. */
30525 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30528 switch (kwd
->keyword
)
30533 error_at (kwd
->location
, "attributes may not be specified before"
30534 " the %<@%D%> Objective-C++ keyword",
30538 case RID_AT_IMPLEMENTATION
:
30539 warning_at (kwd
->location
, OPT_Wattributes
,
30540 "prefix attributes are ignored before %<@%D%>",
30547 switch (kwd
->keyword
)
30550 cp_parser_objc_alias_declaration (parser
);
30553 cp_parser_objc_class_declaration (parser
);
30555 case RID_AT_PROTOCOL
:
30556 cp_parser_objc_protocol_declaration (parser
, attributes
);
30558 case RID_AT_INTERFACE
:
30559 cp_parser_objc_class_interface (parser
, attributes
);
30561 case RID_AT_IMPLEMENTATION
:
30562 cp_parser_objc_class_implementation (parser
);
30565 cp_parser_objc_end_implementation (parser
);
30568 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30570 cp_parser_skip_to_end_of_block_or_statement (parser
);
30574 /* Parse an Objective-C try-catch-finally statement.
30576 objc-try-catch-finally-stmt:
30577 @try compound-statement objc-catch-clause-seq [opt]
30578 objc-finally-clause [opt]
30580 objc-catch-clause-seq:
30581 objc-catch-clause objc-catch-clause-seq [opt]
30584 @catch ( objc-exception-declaration ) compound-statement
30586 objc-finally-clause:
30587 @finally compound-statement
30589 objc-exception-declaration:
30590 parameter-declaration
30593 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30597 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30598 for C. Keep them in sync. */
30601 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30603 location_t location
;
30606 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30607 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30608 objc_maybe_warn_exceptions (location
);
30609 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30610 node, lest it get absorbed into the surrounding block. */
30611 stmt
= push_stmt_list ();
30612 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30613 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30615 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30617 cp_parameter_declarator
*parm
;
30618 tree parameter_declaration
= error_mark_node
;
30619 bool seen_open_paren
= false;
30620 matching_parens parens
;
30622 cp_lexer_consume_token (parser
->lexer
);
30623 if (parens
.require_open (parser
))
30624 seen_open_paren
= true;
30625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30627 /* We have "@catch (...)" (where the '...' are literally
30628 what is in the code). Skip the '...'.
30629 parameter_declaration is set to NULL_TREE, and
30630 objc_being_catch_clauses() knows that that means
30632 cp_lexer_consume_token (parser
->lexer
);
30633 parameter_declaration
= NULL_TREE
;
30637 /* We have "@catch (NSException *exception)" or something
30638 like that. Parse the parameter declaration. */
30639 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30641 parameter_declaration
= error_mark_node
;
30643 parameter_declaration
= grokdeclarator (parm
->declarator
,
30644 &parm
->decl_specifiers
,
30645 PARM
, /*initialized=*/0,
30646 /*attrlist=*/NULL
);
30648 if (seen_open_paren
)
30649 parens
.require_close (parser
);
30652 /* If there was no open parenthesis, we are recovering from
30653 an error, and we are trying to figure out what mistake
30654 the user has made. */
30656 /* If there is an immediate closing parenthesis, the user
30657 probably forgot the opening one (ie, they typed "@catch
30658 NSException *e)". Parse the closing parenthesis and keep
30660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30661 cp_lexer_consume_token (parser
->lexer
);
30663 /* If these is no immediate closing parenthesis, the user
30664 probably doesn't know that parenthesis are required at
30665 all (ie, they typed "@catch NSException *e"). So, just
30666 forget about the closing parenthesis and keep going. */
30668 objc_begin_catch_clause (parameter_declaration
);
30669 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30670 objc_finish_catch_clause ();
30672 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30674 cp_lexer_consume_token (parser
->lexer
);
30675 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30676 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30677 node, lest it get absorbed into the surrounding block. */
30678 stmt
= push_stmt_list ();
30679 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30680 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30683 return objc_finish_try_stmt ();
30686 /* Parse an Objective-C synchronized statement.
30688 objc-synchronized-stmt:
30689 @synchronized ( expression ) compound-statement
30691 Returns NULL_TREE. */
30694 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30696 location_t location
;
30699 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30701 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30702 objc_maybe_warn_exceptions (location
);
30703 matching_parens parens
;
30704 parens
.require_open (parser
);
30705 lock
= cp_parser_expression (parser
);
30706 parens
.require_close (parser
);
30708 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30709 node, lest it get absorbed into the surrounding block. */
30710 stmt
= push_stmt_list ();
30711 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30713 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30716 /* Parse an Objective-C throw statement.
30719 @throw assignment-expression [opt] ;
30721 Returns a constructed '@throw' statement. */
30724 cp_parser_objc_throw_statement (cp_parser
*parser
)
30726 tree expr
= NULL_TREE
;
30727 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30729 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30731 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30732 expr
= cp_parser_expression (parser
);
30734 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30736 return objc_build_throw_stmt (loc
, expr
);
30739 /* Parse an Objective-C statement. */
30742 cp_parser_objc_statement (cp_parser
* parser
)
30744 /* Try to figure out what kind of declaration is present. */
30745 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30747 switch (kwd
->keyword
)
30750 return cp_parser_objc_try_catch_finally_statement (parser
);
30751 case RID_AT_SYNCHRONIZED
:
30752 return cp_parser_objc_synchronized_statement (parser
);
30754 return cp_parser_objc_throw_statement (parser
);
30756 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30758 cp_parser_skip_to_end_of_block_or_statement (parser
);
30761 return error_mark_node
;
30764 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30765 look ahead to see if an objc keyword follows the attributes. This
30766 is to detect the use of prefix attributes on ObjC @interface and
30770 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30772 cp_lexer_save_tokens (parser
->lexer
);
30773 *attrib
= cp_parser_attributes_opt (parser
);
30774 gcc_assert (*attrib
);
30775 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30777 cp_lexer_commit_tokens (parser
->lexer
);
30780 cp_lexer_rollback_tokens (parser
->lexer
);
30784 /* This routine is a minimal replacement for
30785 c_parser_struct_declaration () used when parsing the list of
30786 types/names or ObjC++ properties. For example, when parsing the
30789 @property (readonly) int a, b, c;
30791 this function is responsible for parsing "int a, int b, int c" and
30792 returning the declarations as CHAIN of DECLs.
30794 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30795 similar parsing. */
30797 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30799 tree decls
= NULL_TREE
;
30800 cp_decl_specifier_seq declspecs
;
30801 int decl_class_or_enum_p
;
30802 tree prefix_attributes
;
30804 cp_parser_decl_specifier_seq (parser
,
30805 CP_PARSER_FLAGS_NONE
,
30807 &decl_class_or_enum_p
);
30809 if (declspecs
.type
== error_mark_node
)
30810 return error_mark_node
;
30812 /* auto, register, static, extern, mutable. */
30813 if (declspecs
.storage_class
!= sc_none
)
30815 cp_parser_error (parser
, "invalid type for property");
30816 declspecs
.storage_class
= sc_none
;
30819 /* thread_local. */
30820 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30822 cp_parser_error (parser
, "invalid type for property");
30823 declspecs
.locations
[ds_thread
] = 0;
30827 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30829 cp_parser_error (parser
, "invalid type for property");
30830 declspecs
.locations
[ds_typedef
] = 0;
30833 prefix_attributes
= declspecs
.attributes
;
30834 declspecs
.attributes
= NULL_TREE
;
30836 /* Keep going until we hit the `;' at the end of the declaration. */
30837 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30839 tree attributes
, first_attribute
, decl
;
30840 cp_declarator
*declarator
;
30843 /* Parse the declarator. */
30844 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30845 NULL
, NULL
, false, false);
30847 /* Look for attributes that apply to the ivar. */
30848 attributes
= cp_parser_attributes_opt (parser
);
30849 /* Remember which attributes are prefix attributes and
30851 first_attribute
= attributes
;
30852 /* Combine the attributes. */
30853 attributes
= attr_chainon (prefix_attributes
, attributes
);
30855 decl
= grokfield (declarator
, &declspecs
,
30856 NULL_TREE
, /*init_const_expr_p=*/false,
30857 NULL_TREE
, attributes
);
30859 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30860 return error_mark_node
;
30862 /* Reset PREFIX_ATTRIBUTES. */
30863 if (attributes
!= error_mark_node
)
30865 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30866 attributes
= TREE_CHAIN (attributes
);
30868 TREE_CHAIN (attributes
) = NULL_TREE
;
30871 DECL_CHAIN (decl
) = decls
;
30874 token
= cp_lexer_peek_token (parser
->lexer
);
30875 if (token
->type
== CPP_COMMA
)
30877 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30886 /* Parse an Objective-C @property declaration. The syntax is:
30888 objc-property-declaration:
30889 '@property' objc-property-attributes[opt] struct-declaration ;
30891 objc-property-attributes:
30892 '(' objc-property-attribute-list ')'
30894 objc-property-attribute-list:
30895 objc-property-attribute
30896 objc-property-attribute-list, objc-property-attribute
30898 objc-property-attribute
30899 'getter' = identifier
30900 'setter' = identifier
30909 @property NSString *name;
30910 @property (readonly) id object;
30911 @property (retain, nonatomic, getter=getTheName) id name;
30912 @property int a, b, c;
30914 PS: This function is identical to
30915 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30917 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30919 /* The following variables hold the attributes of the properties as
30920 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30921 seen. When we see an attribute, we set them to 'true' (if they
30922 are boolean properties) or to the identifier (if they have an
30923 argument, ie, for getter and setter). Note that here we only
30924 parse the list of attributes, check the syntax and accumulate the
30925 attributes that we find. objc_add_property_declaration() will
30926 then process the information. */
30927 bool property_assign
= false;
30928 bool property_copy
= false;
30929 tree property_getter_ident
= NULL_TREE
;
30930 bool property_nonatomic
= false;
30931 bool property_readonly
= false;
30932 bool property_readwrite
= false;
30933 bool property_retain
= false;
30934 tree property_setter_ident
= NULL_TREE
;
30936 /* 'properties' is the list of properties that we read. Usually a
30937 single one, but maybe more (eg, in "@property int a, b, c;" there
30942 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30944 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30946 /* Parse the optional attribute list... */
30947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30950 matching_parens parens
;
30951 parens
.consume_open (parser
);
30955 bool syntax_error
= false;
30956 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30959 if (token
->type
!= CPP_NAME
)
30961 cp_parser_error (parser
, "expected identifier");
30964 keyword
= C_RID_CODE (token
->u
.value
);
30965 cp_lexer_consume_token (parser
->lexer
);
30968 case RID_ASSIGN
: property_assign
= true; break;
30969 case RID_COPY
: property_copy
= true; break;
30970 case RID_NONATOMIC
: property_nonatomic
= true; break;
30971 case RID_READONLY
: property_readonly
= true; break;
30972 case RID_READWRITE
: property_readwrite
= true; break;
30973 case RID_RETAIN
: property_retain
= true; break;
30977 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30979 if (keyword
== RID_GETTER
)
30980 cp_parser_error (parser
,
30981 "missing %<=%> (after %<getter%> attribute)");
30983 cp_parser_error (parser
,
30984 "missing %<=%> (after %<setter%> attribute)");
30985 syntax_error
= true;
30988 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30989 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30991 cp_parser_error (parser
, "expected identifier");
30992 syntax_error
= true;
30995 if (keyword
== RID_SETTER
)
30997 if (property_setter_ident
!= NULL_TREE
)
30999 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31000 cp_lexer_consume_token (parser
->lexer
);
31003 property_setter_ident
= cp_parser_objc_selector (parser
);
31004 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31005 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31007 cp_lexer_consume_token (parser
->lexer
);
31011 if (property_getter_ident
!= NULL_TREE
)
31013 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31014 cp_lexer_consume_token (parser
->lexer
);
31017 property_getter_ident
= cp_parser_objc_selector (parser
);
31021 cp_parser_error (parser
, "unknown property attribute");
31022 syntax_error
= true;
31029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31030 cp_lexer_consume_token (parser
->lexer
);
31035 /* FIXME: "@property (setter, assign);" will generate a spurious
31036 "error: expected ‘)’ before ‘,’ token". This is because
31037 cp_parser_require, unlike the C counterpart, will produce an
31038 error even if we are in error recovery. */
31039 if (!parens
.require_close (parser
))
31041 cp_parser_skip_to_closing_parenthesis (parser
,
31042 /*recovering=*/true,
31043 /*or_comma=*/false,
31044 /*consume_paren=*/true);
31048 /* ... and the property declaration(s). */
31049 properties
= cp_parser_objc_struct_declaration (parser
);
31051 if (properties
== error_mark_node
)
31053 cp_parser_skip_to_end_of_statement (parser
);
31054 /* If the next token is now a `;', consume it. */
31055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31056 cp_lexer_consume_token (parser
->lexer
);
31060 if (properties
== NULL_TREE
)
31061 cp_parser_error (parser
, "expected identifier");
31064 /* Comma-separated properties are chained together in
31065 reverse order; add them one by one. */
31066 properties
= nreverse (properties
);
31068 for (; properties
; properties
= TREE_CHAIN (properties
))
31069 objc_add_property_declaration (loc
, copy_node (properties
),
31070 property_readonly
, property_readwrite
,
31071 property_assign
, property_retain
,
31072 property_copy
, property_nonatomic
,
31073 property_getter_ident
, property_setter_ident
);
31076 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31079 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31081 objc-synthesize-declaration:
31082 @synthesize objc-synthesize-identifier-list ;
31084 objc-synthesize-identifier-list:
31085 objc-synthesize-identifier
31086 objc-synthesize-identifier-list, objc-synthesize-identifier
31088 objc-synthesize-identifier
31090 identifier = identifier
31093 @synthesize MyProperty;
31094 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31096 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31097 for C. Keep them in sync.
31100 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31102 tree list
= NULL_TREE
;
31104 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31106 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31109 tree property
, ivar
;
31110 property
= cp_parser_identifier (parser
);
31111 if (property
== error_mark_node
)
31113 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31118 cp_lexer_consume_token (parser
->lexer
);
31119 ivar
= cp_parser_identifier (parser
);
31120 if (ivar
== error_mark_node
)
31122 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31128 list
= chainon (list
, build_tree_list (ivar
, property
));
31129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31130 cp_lexer_consume_token (parser
->lexer
);
31134 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31135 objc_add_synthesize_declaration (loc
, list
);
31138 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31140 objc-dynamic-declaration:
31141 @dynamic identifier-list ;
31144 @dynamic MyProperty;
31145 @dynamic MyProperty, AnotherProperty;
31147 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31148 for C. Keep them in sync.
31151 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31153 tree list
= NULL_TREE
;
31155 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31157 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31161 property
= cp_parser_identifier (parser
);
31162 if (property
== error_mark_node
)
31164 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31167 list
= chainon (list
, build_tree_list (NULL
, property
));
31168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31169 cp_lexer_consume_token (parser
->lexer
);
31173 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31174 objc_add_dynamic_declaration (loc
, list
);
31178 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31180 /* Returns name of the next clause.
31181 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31182 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31183 returned and the token is consumed. */
31185 static pragma_omp_clause
31186 cp_parser_omp_clause_name (cp_parser
*parser
)
31188 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31190 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31191 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31192 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31193 result
= PRAGMA_OMP_CLAUSE_IF
;
31194 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31195 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31196 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31197 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31198 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31199 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31200 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31201 result
= PRAGMA_OMP_CLAUSE_FOR
;
31202 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31204 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31205 const char *p
= IDENTIFIER_POINTER (id
);
31210 if (!strcmp ("aligned", p
))
31211 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31212 else if (!strcmp ("async", p
))
31213 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31216 if (!strcmp ("collapse", p
))
31217 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31218 else if (!strcmp ("copy", p
))
31219 result
= PRAGMA_OACC_CLAUSE_COPY
;
31220 else if (!strcmp ("copyin", p
))
31221 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31222 else if (!strcmp ("copyout", p
))
31223 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31224 else if (!strcmp ("copyprivate", p
))
31225 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31226 else if (!strcmp ("create", p
))
31227 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31230 if (!strcmp ("defaultmap", p
))
31231 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31232 else if (!strcmp ("depend", p
))
31233 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31234 else if (!strcmp ("device", p
))
31235 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31236 else if (!strcmp ("deviceptr", p
))
31237 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31238 else if (!strcmp ("device_resident", p
))
31239 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31240 else if (!strcmp ("dist_schedule", p
))
31241 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31244 if (!strcmp ("final", p
))
31245 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31246 else if (!strcmp ("firstprivate", p
))
31247 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31248 else if (!strcmp ("from", p
))
31249 result
= PRAGMA_OMP_CLAUSE_FROM
;
31252 if (!strcmp ("gang", p
))
31253 result
= PRAGMA_OACC_CLAUSE_GANG
;
31254 else if (!strcmp ("grainsize", p
))
31255 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31258 if (!strcmp ("hint", p
))
31259 result
= PRAGMA_OMP_CLAUSE_HINT
;
31260 else if (!strcmp ("host", p
))
31261 result
= PRAGMA_OACC_CLAUSE_HOST
;
31264 if (!strcmp ("inbranch", p
))
31265 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31266 else if (!strcmp ("independent", p
))
31267 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31268 else if (!strcmp ("is_device_ptr", p
))
31269 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31272 if (!strcmp ("lastprivate", p
))
31273 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31274 else if (!strcmp ("linear", p
))
31275 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31276 else if (!strcmp ("link", p
))
31277 result
= PRAGMA_OMP_CLAUSE_LINK
;
31280 if (!strcmp ("map", p
))
31281 result
= PRAGMA_OMP_CLAUSE_MAP
;
31282 else if (!strcmp ("mergeable", p
))
31283 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31286 if (!strcmp ("nogroup", p
))
31287 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31288 else if (!strcmp ("notinbranch", p
))
31289 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31290 else if (!strcmp ("nowait", p
))
31291 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31292 else if (!strcmp ("num_gangs", p
))
31293 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31294 else if (!strcmp ("num_tasks", p
))
31295 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31296 else if (!strcmp ("num_teams", p
))
31297 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31298 else if (!strcmp ("num_threads", p
))
31299 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31300 else if (!strcmp ("num_workers", p
))
31301 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31304 if (!strcmp ("ordered", p
))
31305 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31308 if (!strcmp ("parallel", p
))
31309 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31310 else if (!strcmp ("present", p
))
31311 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31312 else if (!strcmp ("present_or_copy", p
)
31313 || !strcmp ("pcopy", p
))
31314 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31315 else if (!strcmp ("present_or_copyin", p
)
31316 || !strcmp ("pcopyin", p
))
31317 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31318 else if (!strcmp ("present_or_copyout", p
)
31319 || !strcmp ("pcopyout", p
))
31320 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31321 else if (!strcmp ("present_or_create", p
)
31322 || !strcmp ("pcreate", p
))
31323 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31324 else if (!strcmp ("priority", p
))
31325 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31326 else if (!strcmp ("proc_bind", p
))
31327 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31330 if (!strcmp ("reduction", p
))
31331 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31334 if (!strcmp ("safelen", p
))
31335 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31336 else if (!strcmp ("schedule", p
))
31337 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31338 else if (!strcmp ("sections", p
))
31339 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31340 else if (!strcmp ("self", p
))
31341 result
= PRAGMA_OACC_CLAUSE_SELF
;
31342 else if (!strcmp ("seq", p
))
31343 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31344 else if (!strcmp ("shared", p
))
31345 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31346 else if (!strcmp ("simd", p
))
31347 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31348 else if (!strcmp ("simdlen", p
))
31349 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31352 if (!strcmp ("taskgroup", p
))
31353 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31354 else if (!strcmp ("thread_limit", p
))
31355 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31356 else if (!strcmp ("threads", p
))
31357 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31358 else if (!strcmp ("tile", p
))
31359 result
= PRAGMA_OACC_CLAUSE_TILE
;
31360 else if (!strcmp ("to", p
))
31361 result
= PRAGMA_OMP_CLAUSE_TO
;
31364 if (!strcmp ("uniform", p
))
31365 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31366 else if (!strcmp ("untied", p
))
31367 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31368 else if (!strcmp ("use_device", p
))
31369 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31370 else if (!strcmp ("use_device_ptr", p
))
31371 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31374 if (!strcmp ("vector", p
))
31375 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31376 else if (!strcmp ("vector_length", p
))
31377 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31380 if (!strcmp ("wait", p
))
31381 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31382 else if (!strcmp ("worker", p
))
31383 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31388 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31389 cp_lexer_consume_token (parser
->lexer
);
31394 /* Validate that a clause of the given type does not already exist. */
31397 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31398 const char *name
, location_t location
)
31402 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31403 if (OMP_CLAUSE_CODE (c
) == code
)
31405 error_at (location
, "too many %qs clauses", name
);
31413 variable-list , identifier
31415 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31416 colon). An opening parenthesis will have been consumed by the caller.
31418 If KIND is nonzero, create the appropriate node and install the decl
31419 in OMP_CLAUSE_DECL and add the node to the head of the list.
31421 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31422 return the list created.
31424 COLON can be NULL if only closing parenthesis should end the list,
31425 or pointer to bool which will receive false if the list is terminated
31426 by closing parenthesis or true if the list is terminated by colon. */
31429 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31430 tree list
, bool *colon
)
31433 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31436 parser
->colon_corrects_to_scope_p
= false;
31443 token
= cp_lexer_peek_token (parser
->lexer
);
31445 && current_class_ptr
31446 && cp_parser_is_keyword (token
, RID_THIS
))
31448 decl
= finish_this_expr ();
31449 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31450 || CONVERT_EXPR_P (decl
))
31451 decl
= TREE_OPERAND (decl
, 0);
31452 cp_lexer_consume_token (parser
->lexer
);
31456 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31457 /*check_dependency_p=*/true,
31458 /*template_p=*/NULL
,
31459 /*declarator_p=*/false,
31460 /*optional_p=*/false);
31461 if (name
== error_mark_node
)
31464 if (identifier_p (name
))
31465 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31468 if (decl
== error_mark_node
)
31469 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31472 if (decl
== error_mark_node
)
31474 else if (kind
!= 0)
31478 case OMP_CLAUSE__CACHE_
:
31479 /* The OpenACC cache directive explicitly only allows "array
31480 elements or subarrays". */
31481 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31483 error_at (token
->location
, "expected %<[%>");
31484 decl
= error_mark_node
;
31488 case OMP_CLAUSE_MAP
:
31489 case OMP_CLAUSE_FROM
:
31490 case OMP_CLAUSE_TO
:
31491 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31494 = cp_lexer_peek_token (parser
->lexer
)->location
;
31495 cp_id_kind idk
= CP_ID_KIND_NONE
;
31496 cp_lexer_consume_token (parser
->lexer
);
31497 decl
= convert_from_reference (decl
);
31499 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31504 case OMP_CLAUSE_DEPEND
:
31505 case OMP_CLAUSE_REDUCTION
:
31506 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31508 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31510 parser
->colon_corrects_to_scope_p
= false;
31511 cp_lexer_consume_token (parser
->lexer
);
31512 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31513 low_bound
= cp_parser_expression (parser
);
31515 parser
->colon_corrects_to_scope_p
31516 = saved_colon_corrects_to_scope_p
;
31517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31518 length
= integer_one_node
;
31521 /* Look for `:'. */
31522 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31524 if (!cp_lexer_next_token_is (parser
->lexer
,
31526 length
= cp_parser_expression (parser
);
31528 /* Look for the closing `]'. */
31529 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31533 decl
= tree_cons (low_bound
, length
, decl
);
31540 tree u
= build_omp_clause (token
->location
, kind
);
31541 OMP_CLAUSE_DECL (u
) = decl
;
31542 OMP_CLAUSE_CHAIN (u
) = list
;
31546 list
= tree_cons (decl
, NULL_TREE
, list
);
31549 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31551 cp_lexer_consume_token (parser
->lexer
);
31555 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31557 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31560 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31564 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31568 /* Try to resync to an unnested comma. Copied from
31569 cp_parser_parenthesized_expression_list. */
31572 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31573 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31574 /*recovering=*/true,
31576 /*consume_paren=*/true);
31584 /* Similarly, but expect leading and trailing parenthesis. This is a very
31585 common case for omp clauses. */
31588 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31590 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31591 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31596 copy ( variable-list )
31597 copyin ( variable-list )
31598 copyout ( variable-list )
31599 create ( variable-list )
31600 delete ( variable-list )
31601 present ( variable-list )
31602 present_or_copy ( variable-list )
31603 pcopy ( variable-list )
31604 present_or_copyin ( variable-list )
31605 pcopyin ( variable-list )
31606 present_or_copyout ( variable-list )
31607 pcopyout ( variable-list )
31608 present_or_create ( variable-list )
31609 pcreate ( variable-list ) */
31612 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31615 enum gomp_map_kind kind
;
31618 case PRAGMA_OACC_CLAUSE_COPY
:
31619 kind
= GOMP_MAP_FORCE_TOFROM
;
31621 case PRAGMA_OACC_CLAUSE_COPYIN
:
31622 kind
= GOMP_MAP_FORCE_TO
;
31624 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31625 kind
= GOMP_MAP_FORCE_FROM
;
31627 case PRAGMA_OACC_CLAUSE_CREATE
:
31628 kind
= GOMP_MAP_FORCE_ALLOC
;
31630 case PRAGMA_OACC_CLAUSE_DELETE
:
31631 kind
= GOMP_MAP_DELETE
;
31633 case PRAGMA_OACC_CLAUSE_DEVICE
:
31634 kind
= GOMP_MAP_FORCE_TO
;
31636 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31637 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31639 case PRAGMA_OACC_CLAUSE_HOST
:
31640 case PRAGMA_OACC_CLAUSE_SELF
:
31641 kind
= GOMP_MAP_FORCE_FROM
;
31643 case PRAGMA_OACC_CLAUSE_LINK
:
31644 kind
= GOMP_MAP_LINK
;
31646 case PRAGMA_OACC_CLAUSE_PRESENT
:
31647 kind
= GOMP_MAP_FORCE_PRESENT
;
31649 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31650 kind
= GOMP_MAP_TOFROM
;
31652 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31653 kind
= GOMP_MAP_TO
;
31655 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31656 kind
= GOMP_MAP_FROM
;
31658 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31659 kind
= GOMP_MAP_ALLOC
;
31662 gcc_unreachable ();
31665 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31667 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31668 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31674 deviceptr ( variable-list ) */
31677 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31679 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31682 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31683 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31684 variable-list must only allow for pointer variables. */
31685 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31686 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31688 tree v
= TREE_PURPOSE (t
);
31689 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31690 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31691 OMP_CLAUSE_DECL (u
) = v
;
31692 OMP_CLAUSE_CHAIN (u
) = list
;
31706 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31707 enum omp_clause_code code
,
31708 tree list
, location_t location
)
31710 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31711 tree c
= build_omp_clause (location
, code
);
31712 OMP_CLAUSE_CHAIN (c
) = list
;
31717 num_gangs ( expression )
31718 num_workers ( expression )
31719 vector_length ( expression ) */
31722 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31723 const char *str
, tree list
)
31725 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31727 matching_parens parens
;
31728 if (!parens
.require_open (parser
))
31731 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31733 if (t
== error_mark_node
31734 || !parens
.require_close (parser
))
31736 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31737 /*or_comma=*/false,
31738 /*consume_paren=*/true);
31742 check_no_duplicate_clause (list
, code
, str
, loc
);
31744 tree c
= build_omp_clause (loc
, code
);
31745 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31746 OMP_CLAUSE_CHAIN (c
) = list
;
31752 gang [( gang-arg-list )]
31753 worker [( [num:] int-expr )]
31754 vector [( [length:] int-expr )]
31756 where gang-arg is one of:
31761 and size-expr may be:
31768 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31769 const char *str
, tree list
)
31771 const char *id
= "num";
31772 cp_lexer
*lexer
= parser
->lexer
;
31773 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31774 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31776 if (kind
== OMP_CLAUSE_VECTOR
)
31779 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31781 matching_parens parens
;
31782 parens
.consume_open (parser
);
31786 cp_token
*next
= cp_lexer_peek_token (lexer
);
31789 /* Gang static argument. */
31790 if (kind
== OMP_CLAUSE_GANG
31791 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31793 cp_lexer_consume_token (lexer
);
31795 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31796 goto cleanup_error
;
31799 if (ops
[idx
] != NULL
)
31801 cp_parser_error (parser
, "too many %<static%> arguments");
31802 goto cleanup_error
;
31805 /* Check for the '*' argument. */
31806 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31807 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31808 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31811 cp_lexer_consume_token (lexer
);
31812 ops
[idx
] = integer_minus_one_node
;
31814 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31816 cp_lexer_consume_token (lexer
);
31822 /* Worker num: argument and vector length: arguments. */
31823 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31824 && id_equal (next
->u
.value
, id
)
31825 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31827 cp_lexer_consume_token (lexer
); /* id */
31828 cp_lexer_consume_token (lexer
); /* ':' */
31831 /* Now collect the actual argument. */
31832 if (ops
[idx
] != NULL_TREE
)
31834 cp_parser_error (parser
, "unexpected argument");
31835 goto cleanup_error
;
31838 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31840 if (expr
== error_mark_node
)
31841 goto cleanup_error
;
31843 mark_exp_read (expr
);
31846 if (kind
== OMP_CLAUSE_GANG
31847 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31849 cp_lexer_consume_token (lexer
);
31856 if (!parens
.require_close (parser
))
31857 goto cleanup_error
;
31860 check_no_duplicate_clause (list
, kind
, str
, loc
);
31862 c
= build_omp_clause (loc
, kind
);
31865 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31867 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31868 OMP_CLAUSE_CHAIN (c
) = list
;
31873 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31878 tile ( size-expr-list ) */
31881 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31883 tree c
, expr
= error_mark_node
;
31884 tree tile
= NULL_TREE
;
31886 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31887 so, but the spec authors never considered such a case and have
31888 differing opinions on what it might mean, including 'not
31890 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31891 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31894 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31899 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31902 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31903 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31904 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31906 cp_lexer_consume_token (parser
->lexer
);
31907 expr
= integer_zero_node
;
31910 expr
= cp_parser_constant_expression (parser
);
31912 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31914 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31916 /* Consume the trailing ')'. */
31917 cp_lexer_consume_token (parser
->lexer
);
31919 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31920 tile
= nreverse (tile
);
31921 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31922 OMP_CLAUSE_CHAIN (c
) = list
;
31927 Parse wait clause or directive parameters. */
31930 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31932 vec
<tree
, va_gc
> *args
;
31935 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31937 /*allow_expansion_p=*/true,
31938 /*non_constant_p=*/NULL
);
31940 if (args
== NULL
|| args
->length () == 0)
31942 cp_parser_error (parser
, "expected integer expression before ')'");
31944 release_tree_vector (args
);
31948 args_tree
= build_tree_list_vec (args
);
31950 release_tree_vector (args
);
31952 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31954 tree targ
= TREE_VALUE (t
);
31956 if (targ
!= error_mark_node
)
31958 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31959 error ("%<wait%> expression must be integral");
31962 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31964 targ
= mark_rvalue_use (targ
);
31965 OMP_CLAUSE_DECL (c
) = targ
;
31966 OMP_CLAUSE_CHAIN (c
) = list
;
31976 wait ( int-expr-list ) */
31979 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31981 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31983 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31986 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31992 collapse ( constant-expression ) */
31995 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32001 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32002 matching_parens parens
;
32003 if (!parens
.require_open (parser
))
32006 num
= cp_parser_constant_expression (parser
);
32008 if (!parens
.require_close (parser
))
32009 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32010 /*or_comma=*/false,
32011 /*consume_paren=*/true);
32013 if (num
== error_mark_node
)
32015 num
= fold_non_dependent_expr (num
);
32016 if (!tree_fits_shwi_p (num
)
32017 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32018 || (n
= tree_to_shwi (num
)) <= 0
32021 error_at (loc
, "collapse argument needs positive constant integer expression");
32025 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32026 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32027 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32028 OMP_CLAUSE_CHAIN (c
) = list
;
32029 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32035 default ( none | shared )
32038 default ( none | present ) */
32041 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32042 location_t location
, bool is_oacc
)
32044 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32047 matching_parens parens
;
32048 if (!parens
.require_open (parser
))
32050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32052 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32053 const char *p
= IDENTIFIER_POINTER (id
);
32058 if (strcmp ("none", p
) != 0)
32060 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32064 if (strcmp ("present", p
) != 0 || !is_oacc
)
32066 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32070 if (strcmp ("shared", p
) != 0 || is_oacc
)
32072 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32079 cp_lexer_consume_token (parser
->lexer
);
32085 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32087 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32090 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32091 || !parens
.require_close (parser
))
32092 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32093 /*or_comma=*/false,
32094 /*consume_paren=*/true);
32096 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32099 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32100 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32101 OMP_CLAUSE_CHAIN (c
) = list
;
32102 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32108 final ( expression ) */
32111 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32115 matching_parens parens
;
32116 if (!parens
.require_open (parser
))
32119 t
= cp_parser_condition (parser
);
32121 if (t
== error_mark_node
32122 || !parens
.require_close (parser
))
32123 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32124 /*or_comma=*/false,
32125 /*consume_paren=*/true);
32127 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32129 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32130 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32131 OMP_CLAUSE_CHAIN (c
) = list
;
32140 if ( directive-name-modifier : expression )
32142 directive-name-modifier:
32143 parallel | task | taskloop | target data | target | target update
32144 | target enter data | target exit data */
32147 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32151 enum tree_code if_modifier
= ERROR_MARK
;
32153 matching_parens parens
;
32154 if (!parens
.require_open (parser
))
32157 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32159 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32160 const char *p
= IDENTIFIER_POINTER (id
);
32163 if (strcmp ("parallel", p
) == 0)
32164 if_modifier
= OMP_PARALLEL
;
32165 else if (strcmp ("task", p
) == 0)
32166 if_modifier
= OMP_TASK
;
32167 else if (strcmp ("taskloop", p
) == 0)
32168 if_modifier
= OMP_TASKLOOP
;
32169 else if (strcmp ("target", p
) == 0)
32171 if_modifier
= OMP_TARGET
;
32172 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32174 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32175 p
= IDENTIFIER_POINTER (id
);
32176 if (strcmp ("data", p
) == 0)
32177 if_modifier
= OMP_TARGET_DATA
;
32178 else if (strcmp ("update", p
) == 0)
32179 if_modifier
= OMP_TARGET_UPDATE
;
32180 else if (strcmp ("enter", p
) == 0)
32181 if_modifier
= OMP_TARGET_ENTER_DATA
;
32182 else if (strcmp ("exit", p
) == 0)
32183 if_modifier
= OMP_TARGET_EXIT_DATA
;
32184 if (if_modifier
!= OMP_TARGET
)
32189 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32190 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32192 if_modifier
= ERROR_MARK
;
32194 if (if_modifier
== OMP_TARGET_ENTER_DATA
32195 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32197 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32199 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32200 p
= IDENTIFIER_POINTER (id
);
32201 if (strcmp ("data", p
) == 0)
32207 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32208 error_at (loc
, "expected %<data%>");
32209 if_modifier
= ERROR_MARK
;
32214 if (if_modifier
!= ERROR_MARK
)
32216 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32219 cp_lexer_consume_token (parser
->lexer
);
32226 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32227 error_at (loc
, "expected %<:%>");
32229 if_modifier
= ERROR_MARK
;
32234 t
= cp_parser_condition (parser
);
32236 if (t
== error_mark_node
32237 || !parens
.require_close (parser
))
32238 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32239 /*or_comma=*/false,
32240 /*consume_paren=*/true);
32242 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32243 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32245 if (if_modifier
!= ERROR_MARK
32246 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32248 const char *p
= NULL
;
32249 switch (if_modifier
)
32251 case OMP_PARALLEL
: p
= "parallel"; break;
32252 case OMP_TASK
: p
= "task"; break;
32253 case OMP_TASKLOOP
: p
= "taskloop"; break;
32254 case OMP_TARGET_DATA
: p
= "target data"; break;
32255 case OMP_TARGET
: p
= "target"; break;
32256 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32257 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32258 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32259 default: gcc_unreachable ();
32261 error_at (location
, "too many %<if%> clauses with %qs modifier",
32265 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32268 error_at (location
, "too many %<if%> clauses");
32270 error_at (location
, "too many %<if%> clauses without modifier");
32273 else if (if_modifier
== ERROR_MARK
32274 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32276 error_at (location
, "if any %<if%> clause has modifier, then all "
32277 "%<if%> clauses have to use modifier");
32282 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32283 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32284 OMP_CLAUSE_IF_EXPR (c
) = t
;
32285 OMP_CLAUSE_CHAIN (c
) = list
;
32294 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32295 tree list
, location_t location
)
32299 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32302 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32303 OMP_CLAUSE_CHAIN (c
) = list
;
32311 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32312 tree list
, location_t location
)
32316 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32318 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32319 OMP_CLAUSE_CHAIN (c
) = list
;
32324 num_threads ( expression ) */
32327 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32328 location_t location
)
32332 matching_parens parens
;
32333 if (!parens
.require_open (parser
))
32336 t
= cp_parser_expression (parser
);
32338 if (t
== error_mark_node
32339 || !parens
.require_close (parser
))
32340 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32341 /*or_comma=*/false,
32342 /*consume_paren=*/true);
32344 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32345 "num_threads", location
);
32347 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32348 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32349 OMP_CLAUSE_CHAIN (c
) = list
;
32355 num_tasks ( expression ) */
32358 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32359 location_t location
)
32363 matching_parens parens
;
32364 if (!parens
.require_open (parser
))
32367 t
= cp_parser_expression (parser
);
32369 if (t
== error_mark_node
32370 || !parens
.require_close (parser
))
32371 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32372 /*or_comma=*/false,
32373 /*consume_paren=*/true);
32375 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32376 "num_tasks", location
);
32378 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32379 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32380 OMP_CLAUSE_CHAIN (c
) = list
;
32386 grainsize ( expression ) */
32389 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32390 location_t location
)
32394 matching_parens parens
;
32395 if (!parens
.require_open (parser
))
32398 t
= cp_parser_expression (parser
);
32400 if (t
== error_mark_node
32401 || !parens
.require_close (parser
))
32402 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32403 /*or_comma=*/false,
32404 /*consume_paren=*/true);
32406 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32407 "grainsize", location
);
32409 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32410 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32411 OMP_CLAUSE_CHAIN (c
) = list
;
32417 priority ( expression ) */
32420 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32421 location_t location
)
32425 matching_parens parens
;
32426 if (!parens
.require_open (parser
))
32429 t
= cp_parser_expression (parser
);
32431 if (t
== error_mark_node
32432 || !parens
.require_close (parser
))
32433 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32434 /*or_comma=*/false,
32435 /*consume_paren=*/true);
32437 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32438 "priority", location
);
32440 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32441 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32442 OMP_CLAUSE_CHAIN (c
) = list
;
32448 hint ( expression ) */
32451 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32452 location_t location
)
32456 matching_parens parens
;
32457 if (!parens
.require_open (parser
))
32460 t
= cp_parser_expression (parser
);
32462 if (t
== error_mark_node
32463 || !parens
.require_close (parser
))
32464 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32465 /*or_comma=*/false,
32466 /*consume_paren=*/true);
32468 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32470 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32471 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32472 OMP_CLAUSE_CHAIN (c
) = list
;
32478 defaultmap ( tofrom : scalar ) */
32481 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32482 location_t location
)
32487 matching_parens parens
;
32488 if (!parens
.require_open (parser
))
32491 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32493 cp_parser_error (parser
, "expected %<tofrom%>");
32496 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32497 p
= IDENTIFIER_POINTER (id
);
32498 if (strcmp (p
, "tofrom") != 0)
32500 cp_parser_error (parser
, "expected %<tofrom%>");
32503 cp_lexer_consume_token (parser
->lexer
);
32504 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32507 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32509 cp_parser_error (parser
, "expected %<scalar%>");
32512 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32513 p
= IDENTIFIER_POINTER (id
);
32514 if (strcmp (p
, "scalar") != 0)
32516 cp_parser_error (parser
, "expected %<scalar%>");
32519 cp_lexer_consume_token (parser
->lexer
);
32520 if (!parens
.require_close (parser
))
32523 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32526 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32527 OMP_CLAUSE_CHAIN (c
) = list
;
32531 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32532 /*or_comma=*/false,
32533 /*consume_paren=*/true);
32541 ordered ( constant-expression ) */
32544 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32545 tree list
, location_t location
)
32547 tree c
, num
= NULL_TREE
;
32550 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32551 "ordered", location
);
32553 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32555 matching_parens parens
;
32556 parens
.consume_open (parser
);
32558 num
= cp_parser_constant_expression (parser
);
32560 if (!parens
.require_close (parser
))
32561 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32562 /*or_comma=*/false,
32563 /*consume_paren=*/true);
32565 if (num
== error_mark_node
)
32567 num
= fold_non_dependent_expr (num
);
32568 if (!tree_fits_shwi_p (num
)
32569 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32570 || (n
= tree_to_shwi (num
)) <= 0
32573 error_at (location
,
32574 "ordered argument needs positive constant integer "
32580 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32581 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32582 OMP_CLAUSE_CHAIN (c
) = list
;
32587 reduction ( reduction-operator : variable-list )
32589 reduction-operator:
32590 One of: + * - & ^ | && ||
32594 reduction-operator:
32595 One of: + * - & ^ | && || min max
32599 reduction-operator:
32600 One of: + * - & ^ | && ||
32604 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32606 enum tree_code code
= ERROR_MARK
;
32607 tree nlist
, c
, id
= NULL_TREE
;
32609 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32612 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32614 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32615 case CPP_MULT
: code
= MULT_EXPR
; break;
32616 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32617 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32618 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32619 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32620 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32621 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32625 if (code
!= ERROR_MARK
)
32626 cp_lexer_consume_token (parser
->lexer
);
32629 bool saved_colon_corrects_to_scope_p
;
32630 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32631 parser
->colon_corrects_to_scope_p
= false;
32632 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32633 /*check_dependency_p=*/true,
32634 /*template_p=*/NULL
,
32635 /*declarator_p=*/false,
32636 /*optional_p=*/false);
32637 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32638 if (identifier_p (id
))
32640 const char *p
= IDENTIFIER_POINTER (id
);
32642 if (strcmp (p
, "min") == 0)
32644 else if (strcmp (p
, "max") == 0)
32646 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32648 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32650 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32652 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32653 code
= BIT_AND_EXPR
;
32654 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32655 code
= BIT_IOR_EXPR
;
32656 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32657 code
= BIT_XOR_EXPR
;
32658 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32659 code
= TRUTH_ANDIF_EXPR
;
32660 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32661 code
= TRUTH_ORIF_EXPR
;
32662 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32663 tree scope
= parser
->scope
;
32665 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32666 parser
->scope
= NULL_TREE
;
32667 parser
->qualifying_scope
= NULL_TREE
;
32668 parser
->object_scope
= NULL_TREE
;
32672 error ("invalid reduction-identifier");
32674 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32675 /*or_comma=*/false,
32676 /*consume_paren=*/true);
32681 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32684 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32686 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32688 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32689 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32696 schedule ( schedule-kind )
32697 schedule ( schedule-kind , expression )
32700 static | dynamic | guided | runtime | auto
32703 schedule ( schedule-modifier : schedule-kind )
32704 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32712 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32715 int modifiers
= 0, nmodifiers
= 0;
32717 matching_parens parens
;
32718 if (!parens
.require_open (parser
))
32721 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32723 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32725 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32726 const char *p
= IDENTIFIER_POINTER (id
);
32727 if (strcmp ("simd", p
) == 0)
32728 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32729 else if (strcmp ("monotonic", p
) == 0)
32730 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32731 else if (strcmp ("nonmonotonic", p
) == 0)
32732 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32735 cp_lexer_consume_token (parser
->lexer
);
32736 if (nmodifiers
++ == 0
32737 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32738 cp_lexer_consume_token (parser
->lexer
);
32741 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32748 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32749 const char *p
= IDENTIFIER_POINTER (id
);
32754 if (strcmp ("dynamic", p
) != 0)
32756 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32760 if (strcmp ("guided", p
) != 0)
32762 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32766 if (strcmp ("runtime", p
) != 0)
32768 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32775 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32776 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32777 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32778 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32781 cp_lexer_consume_token (parser
->lexer
);
32783 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32784 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32785 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32786 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32788 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32793 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32796 cp_lexer_consume_token (parser
->lexer
);
32798 token
= cp_lexer_peek_token (parser
->lexer
);
32799 t
= cp_parser_assignment_expression (parser
);
32801 if (t
== error_mark_node
)
32803 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32804 error_at (token
->location
, "schedule %<runtime%> does not take "
32805 "a %<chunk_size%> parameter");
32806 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32807 error_at (token
->location
, "schedule %<auto%> does not take "
32808 "a %<chunk_size%> parameter");
32810 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32812 if (!parens
.require_close (parser
))
32815 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32818 OMP_CLAUSE_SCHEDULE_KIND (c
)
32819 = (enum omp_clause_schedule_kind
)
32820 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32822 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32823 OMP_CLAUSE_CHAIN (c
) = list
;
32827 cp_parser_error (parser
, "invalid schedule kind");
32829 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32830 /*or_comma=*/false,
32831 /*consume_paren=*/true);
32839 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32840 tree list
, location_t location
)
32844 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32846 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32847 OMP_CLAUSE_CHAIN (c
) = list
;
32856 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32857 tree list
, location_t location
)
32859 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32860 tree c
= build_omp_clause (location
, code
);
32861 OMP_CLAUSE_CHAIN (c
) = list
;
32872 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32873 enum omp_clause_code code
,
32874 tree list
, location_t location
)
32876 tree c
= build_omp_clause (location
, code
);
32877 OMP_CLAUSE_CHAIN (c
) = list
;
32885 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32886 tree list
, location_t location
)
32888 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32889 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32890 OMP_CLAUSE_CHAIN (c
) = list
;
32899 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32900 enum omp_clause_code code
,
32901 tree list
, location_t location
)
32903 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32904 tree c
= build_omp_clause (location
, code
);
32905 OMP_CLAUSE_CHAIN (c
) = list
;
32910 num_teams ( expression ) */
32913 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32914 location_t location
)
32918 matching_parens parens
;
32919 if (!parens
.require_open (parser
))
32922 t
= cp_parser_expression (parser
);
32924 if (t
== error_mark_node
32925 || !parens
.require_close (parser
))
32926 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32927 /*or_comma=*/false,
32928 /*consume_paren=*/true);
32930 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32931 "num_teams", location
);
32933 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32934 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32935 OMP_CLAUSE_CHAIN (c
) = list
;
32941 thread_limit ( expression ) */
32944 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32945 location_t location
)
32949 matching_parens parens
;
32950 if (!parens
.require_open (parser
))
32953 t
= cp_parser_expression (parser
);
32955 if (t
== error_mark_node
32956 || !parens
.require_close (parser
))
32957 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32958 /*or_comma=*/false,
32959 /*consume_paren=*/true);
32961 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32962 "thread_limit", location
);
32964 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32965 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32966 OMP_CLAUSE_CHAIN (c
) = list
;
32972 aligned ( variable-list )
32973 aligned ( variable-list : constant-expression ) */
32976 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32978 tree nlist
, c
, alignment
= NULL_TREE
;
32981 matching_parens parens
;
32982 if (!parens
.require_open (parser
))
32985 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32990 alignment
= cp_parser_constant_expression (parser
);
32992 if (!parens
.require_close (parser
))
32993 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32994 /*or_comma=*/false,
32995 /*consume_paren=*/true);
32997 if (alignment
== error_mark_node
)
32998 alignment
= NULL_TREE
;
33001 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33002 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33008 linear ( variable-list )
33009 linear ( variable-list : expression )
33012 linear ( modifier ( variable-list ) )
33013 linear ( modifier ( variable-list ) : expression ) */
33016 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33019 tree nlist
, c
, step
= integer_one_node
;
33021 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33023 matching_parens parens
;
33024 if (!parens
.require_open (parser
))
33027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33029 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33030 const char *p
= IDENTIFIER_POINTER (id
);
33032 if (strcmp ("ref", p
) == 0)
33033 kind
= OMP_CLAUSE_LINEAR_REF
;
33034 else if (strcmp ("val", p
) == 0)
33035 kind
= OMP_CLAUSE_LINEAR_VAL
;
33036 else if (strcmp ("uval", p
) == 0)
33037 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33038 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33039 cp_lexer_consume_token (parser
->lexer
);
33041 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33044 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33045 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33049 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33050 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33052 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33053 else if (!parens
.require_close (parser
))
33054 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33055 /*or_comma=*/false,
33056 /*consume_paren=*/true);
33063 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33064 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33066 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33067 cp_parser_parse_tentatively (parser
);
33068 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33069 /*check_dependency_p=*/true,
33070 /*template_p=*/NULL
,
33071 /*declarator_p=*/false,
33072 /*optional_p=*/false);
33073 if (step
!= error_mark_node
)
33074 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33075 if (step
== error_mark_node
)
33078 cp_parser_abort_tentative_parse (parser
);
33080 else if (!cp_parser_parse_definitely (parser
))
33084 step
= cp_parser_expression (parser
);
33086 if (!parens
.require_close (parser
))
33087 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33088 /*or_comma=*/false,
33089 /*consume_paren=*/true);
33091 if (step
== error_mark_node
)
33095 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33097 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33098 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33105 safelen ( constant-expression ) */
33108 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33109 location_t location
)
33113 matching_parens parens
;
33114 if (!parens
.require_open (parser
))
33117 t
= cp_parser_constant_expression (parser
);
33119 if (t
== error_mark_node
33120 || !parens
.require_close (parser
))
33121 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33122 /*or_comma=*/false,
33123 /*consume_paren=*/true);
33125 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33127 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33128 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33129 OMP_CLAUSE_CHAIN (c
) = list
;
33135 simdlen ( constant-expression ) */
33138 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33139 location_t location
)
33143 matching_parens parens
;
33144 if (!parens
.require_open (parser
))
33147 t
= cp_parser_constant_expression (parser
);
33149 if (t
== error_mark_node
33150 || !parens
.require_close (parser
))
33151 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33152 /*or_comma=*/false,
33153 /*consume_paren=*/true);
33155 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33157 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33158 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33159 OMP_CLAUSE_CHAIN (c
) = list
;
33166 identifier [+/- integer]
33167 vec , identifier [+/- integer]
33171 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33176 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33178 cp_parser_error (parser
, "expected identifier");
33182 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33184 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33185 tree t
, identifier
= cp_parser_identifier (parser
);
33186 tree addend
= NULL
;
33188 if (identifier
== error_mark_node
)
33189 t
= error_mark_node
;
33192 t
= cp_parser_lookup_name_simple
33193 (parser
, identifier
,
33194 cp_lexer_peek_token (parser
->lexer
)->location
);
33195 if (t
== error_mark_node
)
33196 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33203 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33205 addend
= integer_zero_node
;
33206 goto add_to_vector
;
33208 cp_lexer_consume_token (parser
->lexer
);
33210 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33212 cp_parser_error (parser
, "expected integer");
33216 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33217 if (TREE_CODE (addend
) != INTEGER_CST
)
33219 cp_parser_error (parser
, "expected integer");
33222 cp_lexer_consume_token (parser
->lexer
);
33225 if (t
!= error_mark_node
)
33227 vec
= tree_cons (addend
, t
, vec
);
33229 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33232 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33235 cp_lexer_consume_token (parser
->lexer
);
33238 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33240 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33241 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33242 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33243 OMP_CLAUSE_CHAIN (u
) = list
;
33250 depend ( depend-kind : variable-list )
33258 depend ( sink : vec ) */
33261 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33264 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33266 matching_parens parens
;
33267 if (!parens
.require_open (parser
))
33270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33272 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33273 const char *p
= IDENTIFIER_POINTER (id
);
33275 if (strcmp ("in", p
) == 0)
33276 kind
= OMP_CLAUSE_DEPEND_IN
;
33277 else if (strcmp ("inout", p
) == 0)
33278 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33279 else if (strcmp ("out", p
) == 0)
33280 kind
= OMP_CLAUSE_DEPEND_OUT
;
33281 else if (strcmp ("source", p
) == 0)
33282 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33283 else if (strcmp ("sink", p
) == 0)
33284 kind
= OMP_CLAUSE_DEPEND_SINK
;
33291 cp_lexer_consume_token (parser
->lexer
);
33293 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33295 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33296 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33297 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33298 OMP_CLAUSE_CHAIN (c
) = list
;
33299 if (!parens
.require_close (parser
))
33300 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33301 /*or_comma=*/false,
33302 /*consume_paren=*/true);
33306 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33309 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33310 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33313 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33316 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33317 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33322 cp_parser_error (parser
, "invalid depend kind");
33324 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33325 /*or_comma=*/false,
33326 /*consume_paren=*/true);
33331 map ( map-kind : variable-list )
33332 map ( variable-list )
33335 alloc | to | from | tofrom
33339 alloc | to | from | tofrom | release | delete
33341 map ( always [,] map-kind: variable-list ) */
33344 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33347 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33348 bool always
= false;
33350 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33355 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33356 const char *p
= IDENTIFIER_POINTER (id
);
33358 if (strcmp ("always", p
) == 0)
33361 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33363 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33364 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33366 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33370 cp_lexer_consume_token (parser
->lexer
);
33372 cp_lexer_consume_token (parser
->lexer
);
33377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33378 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33380 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33381 const char *p
= IDENTIFIER_POINTER (id
);
33383 if (strcmp ("alloc", p
) == 0)
33384 kind
= GOMP_MAP_ALLOC
;
33385 else if (strcmp ("to", p
) == 0)
33386 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33387 else if (strcmp ("from", p
) == 0)
33388 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33389 else if (strcmp ("tofrom", p
) == 0)
33390 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33391 else if (strcmp ("release", p
) == 0)
33392 kind
= GOMP_MAP_RELEASE
;
33395 cp_parser_error (parser
, "invalid map kind");
33396 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33397 /*or_comma=*/false,
33398 /*consume_paren=*/true);
33401 cp_lexer_consume_token (parser
->lexer
);
33402 cp_lexer_consume_token (parser
->lexer
);
33404 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33405 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33407 kind
= GOMP_MAP_DELETE
;
33408 cp_lexer_consume_token (parser
->lexer
);
33409 cp_lexer_consume_token (parser
->lexer
);
33412 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33415 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33416 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33422 device ( expression ) */
33425 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33426 location_t location
)
33430 matching_parens parens
;
33431 if (!parens
.require_open (parser
))
33434 t
= cp_parser_expression (parser
);
33436 if (t
== error_mark_node
33437 || !parens
.require_close (parser
))
33438 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33439 /*or_comma=*/false,
33440 /*consume_paren=*/true);
33442 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33443 "device", location
);
33445 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33446 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33447 OMP_CLAUSE_CHAIN (c
) = list
;
33453 dist_schedule ( static )
33454 dist_schedule ( static , expression ) */
33457 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33458 location_t location
)
33462 matching_parens parens
;
33463 if (!parens
.require_open (parser
))
33466 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33468 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33470 cp_lexer_consume_token (parser
->lexer
);
33472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33474 cp_lexer_consume_token (parser
->lexer
);
33476 t
= cp_parser_assignment_expression (parser
);
33478 if (t
== error_mark_node
)
33480 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33482 if (!parens
.require_close (parser
))
33485 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33488 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33490 OMP_CLAUSE_CHAIN (c
) = list
;
33494 cp_parser_error (parser
, "invalid dist_schedule kind");
33496 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33497 /*or_comma=*/false,
33498 /*consume_paren=*/true);
33503 proc_bind ( proc-bind-kind )
33506 master | close | spread */
33509 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33510 location_t location
)
33513 enum omp_clause_proc_bind_kind kind
;
33515 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33520 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33521 const char *p
= IDENTIFIER_POINTER (id
);
33523 if (strcmp ("master", p
) == 0)
33524 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33525 else if (strcmp ("close", p
) == 0)
33526 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33527 else if (strcmp ("spread", p
) == 0)
33528 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33535 cp_lexer_consume_token (parser
->lexer
);
33536 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33539 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33540 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33542 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33543 OMP_CLAUSE_CHAIN (c
) = list
;
33547 cp_parser_error (parser
, "invalid depend kind");
33549 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33550 /*or_comma=*/false,
33551 /*consume_paren=*/true);
33556 async [( int-expr )] */
33559 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33562 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33564 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33566 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33568 matching_parens parens
;
33569 parens
.consume_open (parser
);
33571 t
= cp_parser_expression (parser
);
33572 if (t
== error_mark_node
33573 || !parens
.require_close (parser
))
33574 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33575 /*or_comma=*/false,
33576 /*consume_paren=*/true);
33579 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33581 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33582 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33583 OMP_CLAUSE_CHAIN (c
) = list
;
33589 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33590 is a bitmask in MASK. Return the list of clauses found. */
33593 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33594 const char *where
, cp_token
*pragma_tok
,
33595 bool finish_p
= true)
33597 tree clauses
= NULL
;
33600 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33603 pragma_omp_clause c_kind
;
33604 omp_clause_code code
;
33605 const char *c_name
;
33606 tree prev
= clauses
;
33608 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33609 cp_lexer_consume_token (parser
->lexer
);
33611 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33612 c_kind
= cp_parser_omp_clause_name (parser
);
33616 case PRAGMA_OACC_CLAUSE_ASYNC
:
33617 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33620 case PRAGMA_OACC_CLAUSE_AUTO
:
33621 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33625 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33626 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33627 c_name
= "collapse";
33629 case PRAGMA_OACC_CLAUSE_COPY
:
33630 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33633 case PRAGMA_OACC_CLAUSE_COPYIN
:
33634 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33637 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33638 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33639 c_name
= "copyout";
33641 case PRAGMA_OACC_CLAUSE_CREATE
:
33642 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33645 case PRAGMA_OACC_CLAUSE_DELETE
:
33646 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33649 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33650 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33651 c_name
= "default";
33653 case PRAGMA_OACC_CLAUSE_DEVICE
:
33654 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33657 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33658 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33659 c_name
= "deviceptr";
33661 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33662 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33663 c_name
= "device_resident";
33665 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33666 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33668 c_name
= "firstprivate";
33670 case PRAGMA_OACC_CLAUSE_GANG
:
33672 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33675 case PRAGMA_OACC_CLAUSE_HOST
:
33676 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33679 case PRAGMA_OACC_CLAUSE_IF
:
33680 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33683 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33684 clauses
= cp_parser_oacc_simple_clause (parser
,
33685 OMP_CLAUSE_INDEPENDENT
,
33687 c_name
= "independent";
33689 case PRAGMA_OACC_CLAUSE_LINK
:
33690 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33693 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33694 code
= OMP_CLAUSE_NUM_GANGS
;
33695 c_name
= "num_gangs";
33696 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33699 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33700 c_name
= "num_workers";
33701 code
= OMP_CLAUSE_NUM_WORKERS
;
33702 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33705 case PRAGMA_OACC_CLAUSE_PRESENT
:
33706 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33707 c_name
= "present";
33709 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33710 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33711 c_name
= "present_or_copy";
33713 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33714 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33715 c_name
= "present_or_copyin";
33717 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33718 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33719 c_name
= "present_or_copyout";
33721 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33722 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33723 c_name
= "present_or_create";
33725 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33726 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33728 c_name
= "private";
33730 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33731 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33732 c_name
= "reduction";
33734 case PRAGMA_OACC_CLAUSE_SELF
:
33735 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33738 case PRAGMA_OACC_CLAUSE_SEQ
:
33739 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33743 case PRAGMA_OACC_CLAUSE_TILE
:
33744 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33747 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33748 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33750 c_name
= "use_device";
33752 case PRAGMA_OACC_CLAUSE_VECTOR
:
33754 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33757 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33758 c_name
= "vector_length";
33759 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33760 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33763 case PRAGMA_OACC_CLAUSE_WAIT
:
33764 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33767 case PRAGMA_OACC_CLAUSE_WORKER
:
33769 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33773 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33779 if (((mask
>> c_kind
) & 1) == 0)
33781 /* Remove the invalid clause(s) from the list to avoid
33782 confusing the rest of the compiler. */
33784 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33789 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33792 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33797 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33798 is a bitmask in MASK. Return the list of clauses found; the result
33799 of clause default goes in *pdefault. */
33802 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33803 const char *where
, cp_token
*pragma_tok
,
33804 bool finish_p
= true)
33806 tree clauses
= NULL
;
33808 cp_token
*token
= NULL
;
33810 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33812 pragma_omp_clause c_kind
;
33813 const char *c_name
;
33814 tree prev
= clauses
;
33816 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33817 cp_lexer_consume_token (parser
->lexer
);
33819 token
= cp_lexer_peek_token (parser
->lexer
);
33820 c_kind
= cp_parser_omp_clause_name (parser
);
33824 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33825 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33827 c_name
= "collapse";
33829 case PRAGMA_OMP_CLAUSE_COPYIN
:
33830 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33833 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33834 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33836 c_name
= "copyprivate";
33838 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33839 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33840 token
->location
, false);
33841 c_name
= "default";
33843 case PRAGMA_OMP_CLAUSE_FINAL
:
33844 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33847 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33848 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33850 c_name
= "firstprivate";
33852 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33853 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33855 c_name
= "grainsize";
33857 case PRAGMA_OMP_CLAUSE_HINT
:
33858 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33862 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33863 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33865 c_name
= "defaultmap";
33867 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33868 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33870 c_name
= "use_device_ptr";
33872 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33873 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33875 c_name
= "is_device_ptr";
33877 case PRAGMA_OMP_CLAUSE_IF
:
33878 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33882 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33883 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33885 c_name
= "lastprivate";
33887 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33888 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33890 c_name
= "mergeable";
33892 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33893 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33896 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33897 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33899 c_name
= "num_tasks";
33901 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33902 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33904 c_name
= "num_threads";
33906 case PRAGMA_OMP_CLAUSE_ORDERED
:
33907 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33909 c_name
= "ordered";
33911 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33912 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33914 c_name
= "priority";
33916 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33917 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33919 c_name
= "private";
33921 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33922 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33923 c_name
= "reduction";
33925 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33926 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33928 c_name
= "schedule";
33930 case PRAGMA_OMP_CLAUSE_SHARED
:
33931 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33935 case PRAGMA_OMP_CLAUSE_UNTIED
:
33936 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33940 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33941 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33942 clauses
, token
->location
);
33943 c_name
= "inbranch";
33945 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33946 clauses
= cp_parser_omp_clause_branch (parser
,
33947 OMP_CLAUSE_NOTINBRANCH
,
33948 clauses
, token
->location
);
33949 c_name
= "notinbranch";
33951 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33952 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33953 clauses
, token
->location
);
33954 c_name
= "parallel";
33958 error_at (token
->location
, "%qs must be the first clause of %qs",
33963 case PRAGMA_OMP_CLAUSE_FOR
:
33964 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33965 clauses
, token
->location
);
33968 goto clause_not_first
;
33970 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33971 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33972 clauses
, token
->location
);
33973 c_name
= "sections";
33975 goto clause_not_first
;
33977 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33978 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33979 clauses
, token
->location
);
33980 c_name
= "taskgroup";
33982 goto clause_not_first
;
33984 case PRAGMA_OMP_CLAUSE_LINK
:
33985 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33988 case PRAGMA_OMP_CLAUSE_TO
:
33989 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33990 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33993 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33996 case PRAGMA_OMP_CLAUSE_FROM
:
33997 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34000 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34001 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34003 c_name
= "uniform";
34005 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34006 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34008 c_name
= "num_teams";
34010 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34011 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34013 c_name
= "thread_limit";
34015 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34016 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34017 c_name
= "aligned";
34019 case PRAGMA_OMP_CLAUSE_LINEAR
:
34021 bool declare_simd
= false;
34022 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34023 declare_simd
= true;
34024 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34028 case PRAGMA_OMP_CLAUSE_DEPEND
:
34029 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34033 case PRAGMA_OMP_CLAUSE_MAP
:
34034 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34037 case PRAGMA_OMP_CLAUSE_DEVICE
:
34038 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34042 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34043 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34045 c_name
= "dist_schedule";
34047 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34048 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34050 c_name
= "proc_bind";
34052 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34053 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34055 c_name
= "safelen";
34057 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34058 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34060 c_name
= "simdlen";
34062 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34063 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34065 c_name
= "nogroup";
34067 case PRAGMA_OMP_CLAUSE_THREADS
:
34069 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34070 clauses
, token
->location
);
34071 c_name
= "threads";
34073 case PRAGMA_OMP_CLAUSE_SIMD
:
34075 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34076 clauses
, token
->location
);
34080 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34086 if (((mask
>> c_kind
) & 1) == 0)
34088 /* Remove the invalid clause(s) from the list to avoid
34089 confusing the rest of the compiler. */
34091 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34095 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34098 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34099 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34101 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34110 In practice, we're also interested in adding the statement to an
34111 outer node. So it is convenient if we work around the fact that
34112 cp_parser_statement calls add_stmt. */
34115 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34117 unsigned save
= parser
->in_statement
;
34119 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34120 This preserves the "not within loop or switch" style error messages
34121 for nonsense cases like
34127 if (parser
->in_statement
)
34128 parser
->in_statement
= IN_OMP_BLOCK
;
34134 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34136 parser
->in_statement
= save
;
34140 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34142 tree stmt
= begin_omp_structured_block ();
34143 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34145 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34147 cp_parser_end_omp_structured_block (parser
, save
);
34148 return finish_omp_structured_block (stmt
);
34152 # pragma omp atomic new-line
34156 x binop= expr | x++ | ++x | x-- | --x
34158 +, *, -, /, &, ^, |, <<, >>
34160 where x is an lvalue expression with scalar type.
34163 # pragma omp atomic new-line
34166 # pragma omp atomic read new-line
34169 # pragma omp atomic write new-line
34172 # pragma omp atomic update new-line
34175 # pragma omp atomic capture new-line
34178 # pragma omp atomic capture new-line
34186 expression-stmt | x = x binop expr
34188 v = expression-stmt
34190 { v = x; update-stmt; } | { update-stmt; v = x; }
34194 expression-stmt | x = x binop expr | x = expr binop x
34198 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34200 where x and v are lvalue expressions with scalar type. */
34203 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34205 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34206 tree rhs1
= NULL_TREE
, orig_lhs
;
34207 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34208 bool structured_block
= false;
34209 bool seq_cst
= false;
34211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34213 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34214 const char *p
= IDENTIFIER_POINTER (id
);
34216 if (!strcmp (p
, "seq_cst"))
34219 cp_lexer_consume_token (parser
->lexer
);
34220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34221 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34222 cp_lexer_consume_token (parser
->lexer
);
34225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34227 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34228 const char *p
= IDENTIFIER_POINTER (id
);
34230 if (!strcmp (p
, "read"))
34231 code
= OMP_ATOMIC_READ
;
34232 else if (!strcmp (p
, "write"))
34234 else if (!strcmp (p
, "update"))
34236 else if (!strcmp (p
, "capture"))
34237 code
= OMP_ATOMIC_CAPTURE_NEW
;
34241 cp_lexer_consume_token (parser
->lexer
);
34245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34246 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34247 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
, "seq_cst"))
34257 cp_lexer_consume_token (parser
->lexer
);
34261 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34265 case OMP_ATOMIC_READ
:
34266 case NOP_EXPR
: /* atomic write */
34267 v
= cp_parser_unary_expression (parser
);
34268 if (v
== error_mark_node
)
34270 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34272 if (code
== NOP_EXPR
)
34273 lhs
= cp_parser_expression (parser
);
34275 lhs
= cp_parser_unary_expression (parser
);
34276 if (lhs
== error_mark_node
)
34278 if (code
== NOP_EXPR
)
34280 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34288 case OMP_ATOMIC_CAPTURE_NEW
:
34289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34291 cp_lexer_consume_token (parser
->lexer
);
34292 structured_block
= true;
34296 v
= cp_parser_unary_expression (parser
);
34297 if (v
== error_mark_node
)
34299 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34307 lhs
= cp_parser_unary_expression (parser
);
34309 switch (TREE_CODE (lhs
))
34314 case POSTINCREMENT_EXPR
:
34315 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34316 code
= OMP_ATOMIC_CAPTURE_OLD
;
34318 case PREINCREMENT_EXPR
:
34319 lhs
= TREE_OPERAND (lhs
, 0);
34320 opcode
= PLUS_EXPR
;
34321 rhs
= integer_one_node
;
34324 case POSTDECREMENT_EXPR
:
34325 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34326 code
= OMP_ATOMIC_CAPTURE_OLD
;
34328 case PREDECREMENT_EXPR
:
34329 lhs
= TREE_OPERAND (lhs
, 0);
34330 opcode
= MINUS_EXPR
;
34331 rhs
= integer_one_node
;
34334 case COMPOUND_EXPR
:
34335 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34336 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34337 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34338 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34339 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34340 (TREE_OPERAND (lhs
, 1), 0), 0)))
34342 /* Undo effects of boolean_increment for post {in,de}crement. */
34343 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34346 if (TREE_CODE (lhs
) == MODIFY_EXPR
34347 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34349 /* Undo effects of boolean_increment. */
34350 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34352 /* This is pre or post increment. */
34353 rhs
= TREE_OPERAND (lhs
, 1);
34354 lhs
= TREE_OPERAND (lhs
, 0);
34356 if (code
== OMP_ATOMIC_CAPTURE_NEW
34357 && !structured_block
34358 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34359 code
= OMP_ATOMIC_CAPTURE_OLD
;
34365 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34368 opcode
= MULT_EXPR
;
34371 opcode
= TRUNC_DIV_EXPR
;
34374 opcode
= PLUS_EXPR
;
34377 opcode
= MINUS_EXPR
;
34379 case CPP_LSHIFT_EQ
:
34380 opcode
= LSHIFT_EXPR
;
34382 case CPP_RSHIFT_EQ
:
34383 opcode
= RSHIFT_EXPR
;
34386 opcode
= BIT_AND_EXPR
;
34389 opcode
= BIT_IOR_EXPR
;
34392 opcode
= BIT_XOR_EXPR
;
34395 enum cp_parser_prec oprec
;
34397 cp_lexer_consume_token (parser
->lexer
);
34398 cp_parser_parse_tentatively (parser
);
34399 rhs1
= cp_parser_simple_cast_expression (parser
);
34400 if (rhs1
== error_mark_node
)
34402 cp_parser_abort_tentative_parse (parser
);
34403 cp_parser_simple_cast_expression (parser
);
34406 token
= cp_lexer_peek_token (parser
->lexer
);
34407 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34409 cp_parser_abort_tentative_parse (parser
);
34410 cp_parser_parse_tentatively (parser
);
34411 rhs
= cp_parser_binary_expression (parser
, false, true,
34412 PREC_NOT_OPERATOR
, NULL
);
34413 if (rhs
== error_mark_node
)
34415 cp_parser_abort_tentative_parse (parser
);
34416 cp_parser_binary_expression (parser
, false, true,
34417 PREC_NOT_OPERATOR
, NULL
);
34420 switch (TREE_CODE (rhs
))
34423 case TRUNC_DIV_EXPR
:
34432 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34434 if (cp_parser_parse_definitely (parser
))
34436 opcode
= TREE_CODE (rhs
);
34437 rhs1
= TREE_OPERAND (rhs
, 0);
34438 rhs
= TREE_OPERAND (rhs
, 1);
34448 cp_parser_abort_tentative_parse (parser
);
34449 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34451 rhs
= cp_parser_expression (parser
);
34452 if (rhs
== error_mark_node
)
34458 cp_parser_error (parser
,
34459 "invalid form of %<#pragma omp atomic%>");
34462 if (!cp_parser_parse_definitely (parser
))
34464 switch (token
->type
)
34466 case CPP_SEMICOLON
:
34467 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34469 code
= OMP_ATOMIC_CAPTURE_OLD
;
34474 cp_lexer_consume_token (parser
->lexer
);
34477 else if (structured_block
)
34484 cp_parser_error (parser
,
34485 "invalid form of %<#pragma omp atomic%>");
34488 opcode
= MULT_EXPR
;
34491 opcode
= TRUNC_DIV_EXPR
;
34494 opcode
= PLUS_EXPR
;
34497 opcode
= MINUS_EXPR
;
34500 opcode
= LSHIFT_EXPR
;
34503 opcode
= RSHIFT_EXPR
;
34506 opcode
= BIT_AND_EXPR
;
34509 opcode
= BIT_IOR_EXPR
;
34512 opcode
= BIT_XOR_EXPR
;
34515 cp_parser_error (parser
,
34516 "invalid operator for %<#pragma omp atomic%>");
34519 oprec
= TOKEN_PRECEDENCE (token
);
34520 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34521 if (commutative_tree_code (opcode
))
34522 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34523 cp_lexer_consume_token (parser
->lexer
);
34524 rhs
= cp_parser_binary_expression (parser
, false, false,
34526 if (rhs
== error_mark_node
)
34531 cp_parser_error (parser
,
34532 "invalid operator for %<#pragma omp atomic%>");
34535 cp_lexer_consume_token (parser
->lexer
);
34537 rhs
= cp_parser_expression (parser
);
34538 if (rhs
== error_mark_node
)
34543 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34545 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34547 v
= cp_parser_unary_expression (parser
);
34548 if (v
== error_mark_node
)
34550 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34552 lhs1
= cp_parser_unary_expression (parser
);
34553 if (lhs1
== error_mark_node
)
34556 if (structured_block
)
34558 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34559 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34562 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34563 if (!structured_block
)
34564 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34568 cp_parser_skip_to_end_of_block_or_statement (parser
);
34569 if (structured_block
)
34571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34572 cp_lexer_consume_token (parser
->lexer
);
34573 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34575 cp_parser_skip_to_end_of_block_or_statement (parser
);
34576 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34577 cp_lexer_consume_token (parser
->lexer
);
34584 # pragma omp barrier new-line */
34587 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34589 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34590 finish_omp_barrier ();
34594 # pragma omp critical [(name)] new-line
34598 # pragma omp critical [(name) [hint(expression)]] new-line
34599 structured-block */
34601 #define OMP_CRITICAL_CLAUSE_MASK \
34602 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34605 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34607 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34611 matching_parens parens
;
34612 parens
.consume_open (parser
);
34614 name
= cp_parser_identifier (parser
);
34616 if (name
== error_mark_node
34617 || !parens
.require_close (parser
))
34618 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34619 /*or_comma=*/false,
34620 /*consume_paren=*/true);
34621 if (name
== error_mark_node
)
34624 clauses
= cp_parser_omp_all_clauses (parser
,
34625 OMP_CRITICAL_CLAUSE_MASK
,
34626 "#pragma omp critical", pragma_tok
);
34629 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34631 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34632 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34636 # pragma omp flush flush-vars[opt] new-line
34639 ( variable-list ) */
34642 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34645 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34646 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34648 finish_omp_flush ();
34651 /* Helper function, to parse omp for increment expression. */
34654 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34656 tree cond
= cp_parser_binary_expression (parser
, false, true,
34657 PREC_NOT_OPERATOR
, NULL
);
34658 if (cond
== error_mark_node
34659 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34661 cp_parser_skip_to_end_of_statement (parser
);
34662 return error_mark_node
;
34665 switch (TREE_CODE (cond
))
34673 /* Fall through: OpenMP disallows NE_EXPR. */
34674 gcc_fallthrough ();
34676 return error_mark_node
;
34679 /* If decl is an iterator, preserve LHS and RHS of the relational
34680 expr until finish_omp_for. */
34682 && (type_dependent_expression_p (decl
)
34683 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34686 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34688 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34689 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34690 /*overload=*/NULL
, tf_warning_or_error
);
34693 /* Helper function, to parse omp for increment expression. */
34696 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34698 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34704 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34706 op
= (token
->type
== CPP_PLUS_PLUS
34707 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34708 cp_lexer_consume_token (parser
->lexer
);
34709 lhs
= cp_parser_simple_cast_expression (parser
);
34711 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34712 return error_mark_node
;
34713 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34716 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34718 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34719 return error_mark_node
;
34721 token
= cp_lexer_peek_token (parser
->lexer
);
34722 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34724 op
= (token
->type
== CPP_PLUS_PLUS
34725 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34726 cp_lexer_consume_token (parser
->lexer
);
34727 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34730 op
= cp_parser_assignment_operator_opt (parser
);
34731 if (op
== ERROR_MARK
)
34732 return error_mark_node
;
34734 if (op
!= NOP_EXPR
)
34736 rhs
= cp_parser_assignment_expression (parser
);
34737 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34738 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34741 lhs
= cp_parser_binary_expression (parser
, false, false,
34742 PREC_ADDITIVE_EXPRESSION
, NULL
);
34743 token
= cp_lexer_peek_token (parser
->lexer
);
34744 decl_first
= (lhs
== decl
34745 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34748 if (token
->type
!= CPP_PLUS
34749 && token
->type
!= CPP_MINUS
)
34750 return error_mark_node
;
34754 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34755 cp_lexer_consume_token (parser
->lexer
);
34756 rhs
= cp_parser_binary_expression (parser
, false, false,
34757 PREC_ADDITIVE_EXPRESSION
, NULL
);
34758 token
= cp_lexer_peek_token (parser
->lexer
);
34759 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34761 if (lhs
== NULL_TREE
)
34763 if (op
== PLUS_EXPR
)
34766 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34767 tf_warning_or_error
);
34770 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34771 ERROR_MARK
, NULL
, tf_warning_or_error
);
34774 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34779 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34780 || op
== MINUS_EXPR
)
34781 return error_mark_node
;
34782 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34785 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34787 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34790 /* Parse the initialization statement of an OpenMP for loop.
34792 Return true if the resulting construct should have an
34793 OMP_CLAUSE_PRIVATE added to it. */
34796 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34797 tree
&this_pre_body
,
34798 vec
<tree
, va_gc
> *for_block
,
34804 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34807 tree add_private_clause
= NULL_TREE
;
34809 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34813 integer-type var = lb
34814 random-access-iterator-type var = lb
34815 pointer-type var = lb
34817 cp_decl_specifier_seq type_specifiers
;
34819 /* First, try to parse as an initialized declaration. See
34820 cp_parser_condition, from whence the bulk of this is copied. */
34822 cp_parser_parse_tentatively (parser
);
34823 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34824 /*is_trailing_return=*/false,
34826 if (cp_parser_parse_definitely (parser
))
34828 /* If parsing a type specifier seq succeeded, then this
34829 MUST be a initialized declaration. */
34830 tree asm_specification
, attributes
;
34831 cp_declarator
*declarator
;
34833 declarator
= cp_parser_declarator (parser
,
34834 CP_PARSER_DECLARATOR_NAMED
,
34835 /*ctor_dtor_or_conv_p=*/NULL
,
34836 /*parenthesized_p=*/NULL
,
34837 /*member_p=*/false,
34838 /*friend_p=*/false);
34839 attributes
= cp_parser_attributes_opt (parser
);
34840 asm_specification
= cp_parser_asm_specification_opt (parser
);
34842 if (declarator
== cp_error_declarator
)
34843 cp_parser_skip_to_end_of_statement (parser
);
34847 tree pushed_scope
, auto_node
;
34849 decl
= start_decl (declarator
, &type_specifiers
,
34850 SD_INITIALIZED
, attributes
,
34851 /*prefix_attributes=*/NULL_TREE
,
34854 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34855 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34857 if (cp_lexer_next_token_is (parser
->lexer
,
34859 error ("parenthesized initialization is not allowed in "
34860 "OpenMP %<for%> loop");
34862 /* Trigger an error. */
34863 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34865 init
= error_mark_node
;
34866 cp_parser_skip_to_end_of_statement (parser
);
34868 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34869 || type_dependent_expression_p (decl
)
34872 bool is_direct_init
, is_non_constant_init
;
34874 init
= cp_parser_initializer (parser
,
34876 &is_non_constant_init
);
34881 = do_auto_deduction (TREE_TYPE (decl
), init
,
34884 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34885 && !type_dependent_expression_p (decl
))
34889 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34891 LOOKUP_ONLYCONVERTING
);
34893 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34895 vec_safe_push (for_block
, this_pre_body
);
34900 init
= pop_stmt_list (this_pre_body
);
34901 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34903 tree_stmt_iterator i
= tsi_start (init
);
34904 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34905 while (!tsi_end_p (i
))
34907 tree t
= tsi_stmt (i
);
34908 if (TREE_CODE (t
) == DECL_EXPR
34909 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34912 vec_safe_push (for_block
, t
);
34917 if (tsi_one_before_end_p (i
))
34919 tree t
= tsi_stmt (i
);
34921 free_stmt_list (init
);
34926 this_pre_body
= NULL_TREE
;
34931 cp_lexer_consume_token (parser
->lexer
);
34932 init
= cp_parser_assignment_expression (parser
);
34935 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34936 init
= error_mark_node
;
34938 cp_finish_decl (decl
, NULL_TREE
,
34939 /*init_const_expr_p=*/false,
34941 LOOKUP_ONLYCONVERTING
);
34945 pop_scope (pushed_scope
);
34951 /* If parsing a type specifier sequence failed, then
34952 this MUST be a simple expression. */
34953 cp_parser_parse_tentatively (parser
);
34954 decl
= cp_parser_primary_expression (parser
, false, false,
34956 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34957 if (!cp_parser_error_occurred (parser
)
34959 && (TREE_CODE (decl
) == COMPONENT_REF
34960 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34962 cp_parser_abort_tentative_parse (parser
);
34963 cp_parser_parse_tentatively (parser
);
34964 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34965 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34966 /*check_dependency_p=*/true,
34967 /*template_p=*/NULL
,
34968 /*declarator_p=*/false,
34969 /*optional_p=*/false);
34970 if (name
!= error_mark_node
34971 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34973 decl
= cp_parser_lookup_name_simple (parser
, name
,
34975 if (TREE_CODE (decl
) == FIELD_DECL
)
34976 add_private_clause
= omp_privatize_field (decl
, false);
34978 cp_parser_abort_tentative_parse (parser
);
34979 cp_parser_parse_tentatively (parser
);
34980 decl
= cp_parser_primary_expression (parser
, false, false,
34983 if (!cp_parser_error_occurred (parser
)
34986 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34990 cp_parser_parse_definitely (parser
);
34991 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34992 rhs
= cp_parser_assignment_expression (parser
);
34994 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34997 tf_warning_or_error
));
34998 if (!add_private_clause
)
34999 add_private_clause
= decl
;
35004 cp_parser_abort_tentative_parse (parser
);
35005 init
= cp_parser_expression (parser
);
35008 if (TREE_CODE (init
) == MODIFY_EXPR
35009 || TREE_CODE (init
) == MODOP_EXPR
)
35010 real_decl
= TREE_OPERAND (init
, 0);
35014 return add_private_clause
;
35017 /* Parse the restricted form of the for statement allowed by OpenMP. */
35020 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35021 tree
*cclauses
, bool *if_p
)
35023 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35024 tree real_decl
, initv
, condv
, incrv
, declv
;
35025 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35026 location_t loc_first
;
35027 bool collapse_err
= false;
35028 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35029 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35030 auto_vec
<tree
, 4> orig_inits
;
35031 bool tiling
= false;
35033 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35034 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35035 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35036 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35039 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35041 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35042 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35045 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35048 if (ordered
&& ordered
< collapse
)
35050 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35051 "%<ordered%> clause parameter is less than %<collapse%>");
35052 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35053 = build_int_cst (NULL_TREE
, collapse
);
35054 ordered
= collapse
;
35058 for (tree
*pc
= &clauses
; *pc
; )
35059 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35061 error_at (OMP_CLAUSE_LOCATION (*pc
),
35062 "%<linear%> clause may not be specified together "
35063 "with %<ordered%> clause with a parameter");
35064 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35067 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35070 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35071 count
= ordered
? ordered
: collapse
;
35073 declv
= make_tree_vec (count
);
35074 initv
= make_tree_vec (count
);
35075 condv
= make_tree_vec (count
);
35076 incrv
= make_tree_vec (count
);
35078 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35080 for (i
= 0; i
< count
; i
++)
35082 int bracecount
= 0;
35083 tree add_private_clause
= NULL_TREE
;
35086 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35089 cp_parser_error (parser
, "for statement expected");
35092 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35094 matching_parens parens
;
35095 if (!parens
.require_open (parser
))
35098 init
= orig_init
= decl
= real_decl
= NULL
;
35099 this_pre_body
= push_stmt_list ();
35102 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35103 init
, orig_init
, decl
, real_decl
);
35105 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35108 this_pre_body
= pop_stmt_list (this_pre_body
);
35112 pre_body
= push_stmt_list ();
35114 add_stmt (this_pre_body
);
35115 pre_body
= pop_stmt_list (pre_body
);
35118 pre_body
= this_pre_body
;
35123 if (cclauses
!= NULL
35124 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35125 && real_decl
!= NULL_TREE
)
35128 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35129 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35130 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35132 error_at (loc
, "iteration variable %qD"
35133 " should not be firstprivate", real_decl
);
35134 *c
= OMP_CLAUSE_CHAIN (*c
);
35136 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35137 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35139 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35141 *c
= OMP_CLAUSE_CHAIN (*c
);
35142 if (code
== OMP_SIMD
)
35144 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35145 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35149 OMP_CLAUSE_CHAIN (l
) = clauses
;
35152 add_private_clause
= NULL_TREE
;
35156 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35157 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35158 add_private_clause
= NULL_TREE
;
35159 c
= &OMP_CLAUSE_CHAIN (*c
);
35163 if (add_private_clause
)
35166 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35168 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35169 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35170 && OMP_CLAUSE_DECL (c
) == decl
)
35172 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35173 && OMP_CLAUSE_DECL (c
) == decl
)
35174 error_at (loc
, "iteration variable %qD "
35175 "should not be firstprivate",
35177 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35178 && OMP_CLAUSE_DECL (c
) == decl
)
35179 error_at (loc
, "iteration variable %qD should not be reduction",
35184 if (code
!= OMP_SIMD
)
35185 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35186 else if (collapse
== 1)
35187 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35189 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35190 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35191 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35194 OMP_CLAUSE_CHAIN (c
) = clauses
;
35196 /* For linear, signal that we need to fill up
35197 the so far unknown linear step. */
35198 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35199 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35205 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35206 cond
= cp_parser_omp_for_cond (parser
, decl
);
35207 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35210 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35212 /* If decl is an iterator, preserve the operator on decl
35213 until finish_omp_for. */
35215 && ((processing_template_decl
35216 && (TREE_TYPE (real_decl
) == NULL_TREE
35217 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35218 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35219 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35221 incr
= cp_parser_expression (parser
);
35222 if (!EXPR_HAS_LOCATION (incr
))
35223 protected_set_expr_location (incr
, input_location
);
35226 if (!parens
.require_close (parser
))
35227 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35228 /*or_comma=*/false,
35229 /*consume_paren=*/true);
35231 TREE_VEC_ELT (declv
, i
) = decl
;
35232 TREE_VEC_ELT (initv
, i
) = init
;
35233 TREE_VEC_ELT (condv
, i
) = cond
;
35234 TREE_VEC_ELT (incrv
, i
) = incr
;
35237 orig_inits
.safe_grow_cleared (i
+ 1);
35238 orig_inits
[i
] = orig_init
;
35241 if (i
== count
- 1)
35244 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35245 in between the collapsed for loops to be still considered perfectly
35246 nested. Hopefully the final version clarifies this.
35247 For now handle (multiple) {'s and empty statements. */
35248 cp_parser_parse_tentatively (parser
);
35251 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35253 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35255 cp_lexer_consume_token (parser
->lexer
);
35258 else if (bracecount
35259 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35260 cp_lexer_consume_token (parser
->lexer
);
35263 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35264 error_at (loc
, "not enough for loops to collapse");
35265 collapse_err
= true;
35266 cp_parser_abort_tentative_parse (parser
);
35274 cp_parser_parse_definitely (parser
);
35275 nbraces
+= bracecount
;
35282 /* Note that we saved the original contents of this flag when we entered
35283 the structured block, and so we don't need to re-save it here. */
35284 parser
->in_statement
= IN_OMP_FOR
;
35286 /* Note that the grammar doesn't call for a structured block here,
35287 though the loop as a whole is a structured block. */
35288 body
= push_stmt_list ();
35289 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35290 body
= pop_stmt_list (body
);
35292 if (declv
== NULL_TREE
)
35295 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35296 body
, pre_body
, &orig_inits
, clauses
);
35300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35302 cp_lexer_consume_token (parser
->lexer
);
35305 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35306 cp_lexer_consume_token (parser
->lexer
);
35311 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35312 "collapsed loops not perfectly nested");
35314 collapse_err
= true;
35315 cp_parser_statement_seq_opt (parser
, NULL
);
35316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35321 while (!for_block
->is_empty ())
35323 tree t
= for_block
->pop ();
35324 if (TREE_CODE (t
) == STATEMENT_LIST
)
35325 add_stmt (pop_stmt_list (t
));
35329 release_tree_vector (for_block
);
35334 /* Helper function for OpenMP parsing, split clauses and call
35335 finish_omp_clauses on each of the set of clauses afterwards. */
35338 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35339 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35342 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35343 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35345 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35349 #pragma omp simd simd-clause[optseq] new-line
35352 #define OMP_SIMD_CLAUSE_MASK \
35353 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35363 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35364 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35367 tree clauses
, sb
, ret
;
35369 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35371 strcat (p_name
, " simd");
35372 mask
|= OMP_SIMD_CLAUSE_MASK
;
35374 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35378 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35379 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35380 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35381 OMP_CLAUSE_ORDERED
);
35382 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35384 error_at (OMP_CLAUSE_LOCATION (c
),
35385 "%<ordered%> clause with parameter may not be specified "
35386 "on %qs construct", p_name
);
35387 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35391 sb
= begin_omp_structured_block ();
35392 save
= cp_parser_begin_omp_structured_block (parser
);
35394 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35396 cp_parser_end_omp_structured_block (parser
, save
);
35397 add_stmt (finish_omp_structured_block (sb
));
35403 #pragma omp for for-clause[optseq] new-line
35407 #pragma omp for simd for-simd-clause[optseq] new-line
35410 #define OMP_FOR_CLAUSE_MASK \
35411 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35422 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35423 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35426 tree clauses
, sb
, ret
;
35428 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35430 strcat (p_name
, " for");
35431 mask
|= OMP_FOR_CLAUSE_MASK
;
35432 /* parallel for{, simd} disallows nowait clause, but for
35433 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35434 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35435 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35436 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35437 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35438 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35442 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35443 const char *p
= IDENTIFIER_POINTER (id
);
35445 if (strcmp (p
, "simd") == 0)
35447 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35448 if (cclauses
== NULL
)
35449 cclauses
= cclauses_buf
;
35451 cp_lexer_consume_token (parser
->lexer
);
35452 if (!flag_openmp
) /* flag_openmp_simd */
35453 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35455 sb
= begin_omp_structured_block ();
35456 save
= cp_parser_begin_omp_structured_block (parser
);
35457 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35459 cp_parser_end_omp_structured_block (parser
, save
);
35460 tree body
= finish_omp_structured_block (sb
);
35463 ret
= make_node (OMP_FOR
);
35464 TREE_TYPE (ret
) = void_type_node
;
35465 OMP_FOR_BODY (ret
) = body
;
35466 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35467 SET_EXPR_LOCATION (ret
, loc
);
35472 if (!flag_openmp
) /* flag_openmp_simd */
35474 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35478 /* Composite distribute parallel for disallows linear clause. */
35479 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35480 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35482 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35486 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35487 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35490 sb
= begin_omp_structured_block ();
35491 save
= cp_parser_begin_omp_structured_block (parser
);
35493 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35495 cp_parser_end_omp_structured_block (parser
, save
);
35496 add_stmt (finish_omp_structured_block (sb
));
35502 # pragma omp master new-line
35503 structured-block */
35506 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35508 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35509 return c_finish_omp_master (input_location
,
35510 cp_parser_omp_structured_block (parser
, if_p
));
35514 # pragma omp ordered new-line
35518 # pragma omp ordered ordered-clauses new-line
35519 structured-block */
35521 #define OMP_ORDERED_CLAUSE_MASK \
35522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35525 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35526 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35529 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35530 enum pragma_context context
, bool *if_p
)
35532 location_t loc
= pragma_tok
->location
;
35534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35536 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35537 const char *p
= IDENTIFIER_POINTER (id
);
35539 if (strcmp (p
, "depend") == 0)
35541 if (!flag_openmp
) /* flag_openmp_simd */
35543 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35546 if (context
== pragma_stmt
)
35548 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35549 "%<depend%> clause may only be used in compound "
35551 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35555 = cp_parser_omp_all_clauses (parser
,
35556 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35557 "#pragma omp ordered", pragma_tok
);
35558 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35564 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35565 "#pragma omp ordered", pragma_tok
);
35567 if (!flag_openmp
/* flag_openmp_simd */
35568 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35571 c_finish_omp_ordered (loc
, clauses
,
35572 cp_parser_omp_structured_block (parser
, if_p
));
35579 { section-sequence }
35582 section-directive[opt] structured-block
35583 section-sequence section-directive structured-block */
35586 cp_parser_omp_sections_scope (cp_parser
*parser
)
35588 tree stmt
, substmt
;
35589 bool error_suppress
= false;
35592 matching_braces braces
;
35593 if (!braces
.require_open (parser
))
35596 stmt
= push_stmt_list ();
35598 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35599 != PRAGMA_OMP_SECTION
)
35601 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35602 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35603 add_stmt (substmt
);
35608 tok
= cp_lexer_peek_token (parser
->lexer
);
35609 if (tok
->type
== CPP_CLOSE_BRACE
)
35611 if (tok
->type
== CPP_EOF
)
35614 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35616 cp_lexer_consume_token (parser
->lexer
);
35617 cp_parser_require_pragma_eol (parser
, tok
);
35618 error_suppress
= false;
35620 else if (!error_suppress
)
35622 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35623 error_suppress
= true;
35626 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35627 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35628 add_stmt (substmt
);
35630 braces
.require_close (parser
);
35632 substmt
= pop_stmt_list (stmt
);
35634 stmt
= make_node (OMP_SECTIONS
);
35635 TREE_TYPE (stmt
) = void_type_node
;
35636 OMP_SECTIONS_BODY (stmt
) = substmt
;
35643 # pragma omp sections sections-clause[optseq] newline
35646 #define OMP_SECTIONS_CLAUSE_MASK \
35647 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35654 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35655 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35658 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35660 strcat (p_name
, " sections");
35661 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35663 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35665 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35669 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35670 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35673 ret
= cp_parser_omp_sections_scope (parser
);
35675 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35681 # pragma omp parallel parallel-clause[optseq] new-line
35683 # pragma omp parallel for parallel-for-clause[optseq] new-line
35685 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35689 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35690 structured-block */
35692 #define OMP_PARALLEL_CLAUSE_MASK \
35693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35704 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35705 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35708 tree stmt
, clauses
, block
;
35710 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35712 strcat (p_name
, " parallel");
35713 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35714 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35715 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35716 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35717 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35719 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35721 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35722 if (cclauses
== NULL
)
35723 cclauses
= cclauses_buf
;
35725 cp_lexer_consume_token (parser
->lexer
);
35726 if (!flag_openmp
) /* flag_openmp_simd */
35727 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35729 block
= begin_omp_parallel ();
35730 save
= cp_parser_begin_omp_structured_block (parser
);
35731 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35733 cp_parser_end_omp_structured_block (parser
, save
);
35734 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35736 if (ret
== NULL_TREE
)
35738 OMP_PARALLEL_COMBINED (stmt
) = 1;
35741 /* When combined with distribute, parallel has to be followed by for.
35742 #pragma omp target parallel is allowed though. */
35744 && (mask
& (OMP_CLAUSE_MASK_1
35745 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35747 error_at (loc
, "expected %<for%> after %qs", p_name
);
35748 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35751 else if (!flag_openmp
) /* flag_openmp_simd */
35753 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35756 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35758 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35759 const char *p
= IDENTIFIER_POINTER (id
);
35760 if (strcmp (p
, "sections") == 0)
35762 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35763 cclauses
= cclauses_buf
;
35765 cp_lexer_consume_token (parser
->lexer
);
35766 block
= begin_omp_parallel ();
35767 save
= cp_parser_begin_omp_structured_block (parser
);
35768 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35769 cp_parser_end_omp_structured_block (parser
, save
);
35770 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35772 OMP_PARALLEL_COMBINED (stmt
) = 1;
35777 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35781 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35782 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35785 block
= begin_omp_parallel ();
35786 save
= cp_parser_begin_omp_structured_block (parser
);
35787 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35788 cp_parser_end_omp_structured_block (parser
, save
);
35789 stmt
= finish_omp_parallel (clauses
, block
);
35794 # pragma omp single single-clause[optseq] new-line
35795 structured-block */
35797 #define OMP_SINGLE_CLAUSE_MASK \
35798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35804 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35806 tree stmt
= make_node (OMP_SINGLE
);
35807 TREE_TYPE (stmt
) = void_type_node
;
35809 OMP_SINGLE_CLAUSES (stmt
)
35810 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35811 "#pragma omp single", pragma_tok
);
35812 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35814 return add_stmt (stmt
);
35818 # pragma omp task task-clause[optseq] new-line
35819 structured-block */
35821 #define OMP_TASK_CLAUSE_MASK \
35822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35834 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35836 tree clauses
, block
;
35839 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35840 "#pragma omp task", pragma_tok
);
35841 block
= begin_omp_task ();
35842 save
= cp_parser_begin_omp_structured_block (parser
);
35843 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35844 cp_parser_end_omp_structured_block (parser
, save
);
35845 return finish_omp_task (clauses
, block
);
35849 # pragma omp taskwait new-line */
35852 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35854 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35855 finish_omp_taskwait ();
35859 # pragma omp taskyield new-line */
35862 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35864 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35865 finish_omp_taskyield ();
35869 # pragma omp taskgroup new-line
35870 structured-block */
35873 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35875 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35876 return c_finish_omp_taskgroup (input_location
,
35877 cp_parser_omp_structured_block (parser
,
35883 # pragma omp threadprivate (variable-list) */
35886 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35890 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35891 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35893 finish_omp_threadprivate (vars
);
35897 # pragma omp cancel cancel-clause[optseq] new-line */
35899 #define OMP_CANCEL_CLAUSE_MASK \
35900 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35907 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35909 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35910 "#pragma omp cancel", pragma_tok
);
35911 finish_omp_cancel (clauses
);
35915 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35917 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35918 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35924 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35925 enum pragma_context context
)
35928 bool point_seen
= false;
35930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35932 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35933 const char *p
= IDENTIFIER_POINTER (id
);
35935 if (strcmp (p
, "point") == 0)
35937 cp_lexer_consume_token (parser
->lexer
);
35943 cp_parser_error (parser
, "expected %<point%>");
35944 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35948 if (context
!= pragma_compound
)
35950 if (context
== pragma_stmt
)
35951 error_at (pragma_tok
->location
,
35952 "%<#pragma %s%> may only be used in compound statements",
35953 "omp cancellation point");
35955 cp_parser_error (parser
, "expected declaration specifiers");
35956 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35960 clauses
= cp_parser_omp_all_clauses (parser
,
35961 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35962 "#pragma omp cancellation point",
35964 finish_omp_cancellation_point (clauses
);
35968 #pragma omp distribute distribute-clause[optseq] new-line
35971 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35979 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35980 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35983 tree clauses
, sb
, ret
;
35985 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35987 strcat (p_name
, " distribute");
35988 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
35990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35992 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35993 const char *p
= IDENTIFIER_POINTER (id
);
35995 bool parallel
= false;
35997 if (strcmp (p
, "simd") == 0)
36000 parallel
= strcmp (p
, "parallel") == 0;
36001 if (parallel
|| simd
)
36003 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36004 if (cclauses
== NULL
)
36005 cclauses
= cclauses_buf
;
36006 cp_lexer_consume_token (parser
->lexer
);
36007 if (!flag_openmp
) /* flag_openmp_simd */
36010 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36013 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36016 sb
= begin_omp_structured_block ();
36017 save
= cp_parser_begin_omp_structured_block (parser
);
36019 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36022 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36024 cp_parser_end_omp_structured_block (parser
, save
);
36025 tree body
= finish_omp_structured_block (sb
);
36028 ret
= make_node (OMP_DISTRIBUTE
);
36029 TREE_TYPE (ret
) = void_type_node
;
36030 OMP_FOR_BODY (ret
) = body
;
36031 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36032 SET_EXPR_LOCATION (ret
, loc
);
36037 if (!flag_openmp
) /* flag_openmp_simd */
36039 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36043 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36047 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36048 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36051 sb
= begin_omp_structured_block ();
36052 save
= cp_parser_begin_omp_structured_block (parser
);
36054 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36056 cp_parser_end_omp_structured_block (parser
, save
);
36057 add_stmt (finish_omp_structured_block (sb
));
36063 # pragma omp teams teams-clause[optseq] new-line
36064 structured-block */
36066 #define OMP_TEAMS_CLAUSE_MASK \
36067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36076 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36077 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36080 tree clauses
, sb
, ret
;
36082 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36084 strcat (p_name
, " teams");
36085 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36089 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36090 const char *p
= IDENTIFIER_POINTER (id
);
36091 if (strcmp (p
, "distribute") == 0)
36093 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36094 if (cclauses
== NULL
)
36095 cclauses
= cclauses_buf
;
36097 cp_lexer_consume_token (parser
->lexer
);
36098 if (!flag_openmp
) /* flag_openmp_simd */
36099 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36101 sb
= begin_omp_structured_block ();
36102 save
= cp_parser_begin_omp_structured_block (parser
);
36103 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36105 cp_parser_end_omp_structured_block (parser
, save
);
36106 tree body
= finish_omp_structured_block (sb
);
36109 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36110 ret
= make_node (OMP_TEAMS
);
36111 TREE_TYPE (ret
) = void_type_node
;
36112 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36113 OMP_TEAMS_BODY (ret
) = body
;
36114 OMP_TEAMS_COMBINED (ret
) = 1;
36115 SET_EXPR_LOCATION (ret
, loc
);
36116 return add_stmt (ret
);
36119 if (!flag_openmp
) /* flag_openmp_simd */
36121 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36125 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36129 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36130 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36133 tree stmt
= make_node (OMP_TEAMS
);
36134 TREE_TYPE (stmt
) = void_type_node
;
36135 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36136 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36137 SET_EXPR_LOCATION (stmt
, loc
);
36139 return add_stmt (stmt
);
36143 # pragma omp target data target-data-clause[optseq] new-line
36144 structured-block */
36146 #define OMP_TARGET_DATA_CLAUSE_MASK \
36147 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36153 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36156 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36157 "#pragma omp target data", pragma_tok
);
36159 for (tree
*pc
= &clauses
; *pc
;)
36161 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36162 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36165 case GOMP_MAP_ALWAYS_TO
:
36166 case GOMP_MAP_FROM
:
36167 case GOMP_MAP_ALWAYS_FROM
:
36168 case GOMP_MAP_TOFROM
:
36169 case GOMP_MAP_ALWAYS_TOFROM
:
36170 case GOMP_MAP_ALLOC
:
36173 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36174 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36175 case GOMP_MAP_ALWAYS_POINTER
:
36179 error_at (OMP_CLAUSE_LOCATION (*pc
),
36180 "%<#pragma omp target data%> with map-type other "
36181 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36182 "on %<map%> clause");
36183 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36186 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36192 error_at (pragma_tok
->location
,
36193 "%<#pragma omp target data%> must contain at least "
36194 "one %<map%> clause");
36198 tree stmt
= make_node (OMP_TARGET_DATA
);
36199 TREE_TYPE (stmt
) = void_type_node
;
36200 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36202 keep_next_level (true);
36203 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36205 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36206 return add_stmt (stmt
);
36210 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36211 structured-block */
36213 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36221 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36222 enum pragma_context context
)
36224 bool data_seen
= false;
36225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36227 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36228 const char *p
= IDENTIFIER_POINTER (id
);
36230 if (strcmp (p
, "data") == 0)
36232 cp_lexer_consume_token (parser
->lexer
);
36238 cp_parser_error (parser
, "expected %<data%>");
36239 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36243 if (context
== pragma_stmt
)
36245 error_at (pragma_tok
->location
,
36246 "%<#pragma %s%> may only be used in compound statements",
36247 "omp target enter data");
36248 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36253 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36254 "#pragma omp target enter data", pragma_tok
);
36256 for (tree
*pc
= &clauses
; *pc
;)
36258 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36259 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36262 case GOMP_MAP_ALWAYS_TO
:
36263 case GOMP_MAP_ALLOC
:
36266 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36267 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36268 case GOMP_MAP_ALWAYS_POINTER
:
36272 error_at (OMP_CLAUSE_LOCATION (*pc
),
36273 "%<#pragma omp target enter data%> with map-type other "
36274 "than %<to%> or %<alloc%> on %<map%> clause");
36275 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36278 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36284 error_at (pragma_tok
->location
,
36285 "%<#pragma omp target enter data%> must contain at least "
36286 "one %<map%> clause");
36290 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36291 TREE_TYPE (stmt
) = void_type_node
;
36292 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36293 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36294 return add_stmt (stmt
);
36298 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36299 structured-block */
36301 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36302 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36309 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36310 enum pragma_context context
)
36312 bool data_seen
= false;
36313 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36315 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36316 const char *p
= IDENTIFIER_POINTER (id
);
36318 if (strcmp (p
, "data") == 0)
36320 cp_lexer_consume_token (parser
->lexer
);
36326 cp_parser_error (parser
, "expected %<data%>");
36327 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36331 if (context
== pragma_stmt
)
36333 error_at (pragma_tok
->location
,
36334 "%<#pragma %s%> may only be used in compound statements",
36335 "omp target exit data");
36336 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36341 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36342 "#pragma omp target exit data", pragma_tok
);
36344 for (tree
*pc
= &clauses
; *pc
;)
36346 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36347 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36349 case GOMP_MAP_FROM
:
36350 case GOMP_MAP_ALWAYS_FROM
:
36351 case GOMP_MAP_RELEASE
:
36352 case GOMP_MAP_DELETE
:
36355 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36356 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36357 case GOMP_MAP_ALWAYS_POINTER
:
36361 error_at (OMP_CLAUSE_LOCATION (*pc
),
36362 "%<#pragma omp target exit data%> with map-type other "
36363 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36365 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36368 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36374 error_at (pragma_tok
->location
,
36375 "%<#pragma omp target exit data%> must contain at least "
36376 "one %<map%> clause");
36380 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36381 TREE_TYPE (stmt
) = void_type_node
;
36382 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36383 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36384 return add_stmt (stmt
);
36388 # pragma omp target update target-update-clause[optseq] new-line */
36390 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36399 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36400 enum pragma_context context
)
36402 if (context
== pragma_stmt
)
36404 error_at (pragma_tok
->location
,
36405 "%<#pragma %s%> may only be used in compound statements",
36406 "omp target update");
36407 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36412 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36413 "#pragma omp target update", pragma_tok
);
36414 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36415 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36417 error_at (pragma_tok
->location
,
36418 "%<#pragma omp target update%> must contain at least one "
36419 "%<from%> or %<to%> clauses");
36423 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36424 TREE_TYPE (stmt
) = void_type_node
;
36425 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36426 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36432 # pragma omp target target-clause[optseq] new-line
36433 structured-block */
36435 #define OMP_TARGET_CLAUSE_MASK \
36436 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36447 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36448 enum pragma_context context
, bool *if_p
)
36450 tree
*pc
= NULL
, stmt
;
36452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36454 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36455 const char *p
= IDENTIFIER_POINTER (id
);
36456 enum tree_code ccode
= ERROR_MARK
;
36458 if (strcmp (p
, "teams") == 0)
36460 else if (strcmp (p
, "parallel") == 0)
36461 ccode
= OMP_PARALLEL
;
36462 else if (strcmp (p
, "simd") == 0)
36464 if (ccode
!= ERROR_MARK
)
36466 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36467 char p_name
[sizeof ("#pragma omp target teams distribute "
36468 "parallel for simd")];
36470 cp_lexer_consume_token (parser
->lexer
);
36471 strcpy (p_name
, "#pragma omp target");
36472 if (!flag_openmp
) /* flag_openmp_simd */
36478 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36479 OMP_TARGET_CLAUSE_MASK
,
36483 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36484 OMP_TARGET_CLAUSE_MASK
,
36488 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36489 OMP_TARGET_CLAUSE_MASK
,
36493 gcc_unreachable ();
36495 return stmt
!= NULL_TREE
;
36497 keep_next_level (true);
36498 tree sb
= begin_omp_structured_block (), ret
;
36499 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36503 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36504 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36508 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36509 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36513 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36514 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36518 gcc_unreachable ();
36520 cp_parser_end_omp_structured_block (parser
, save
);
36521 tree body
= finish_omp_structured_block (sb
);
36522 if (ret
== NULL_TREE
)
36524 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36526 /* For combined target teams, ensure the num_teams and
36527 thread_limit clause expressions are evaluated on the host,
36528 before entering the target construct. */
36530 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36531 c
; c
= OMP_CLAUSE_CHAIN (c
))
36532 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36533 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36534 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36536 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36537 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36538 if (expr
== error_mark_node
)
36540 tree tmp
= TARGET_EXPR_SLOT (expr
);
36542 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36543 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36544 OMP_CLAUSE_FIRSTPRIVATE
);
36545 OMP_CLAUSE_DECL (tc
) = tmp
;
36546 OMP_CLAUSE_CHAIN (tc
)
36547 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36548 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36551 tree stmt
= make_node (OMP_TARGET
);
36552 TREE_TYPE (stmt
) = void_type_node
;
36553 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36554 OMP_TARGET_BODY (stmt
) = body
;
36555 OMP_TARGET_COMBINED (stmt
) = 1;
36556 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36558 pc
= &OMP_TARGET_CLAUSES (stmt
);
36559 goto check_clauses
;
36561 else if (!flag_openmp
) /* flag_openmp_simd */
36563 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36566 else if (strcmp (p
, "data") == 0)
36568 cp_lexer_consume_token (parser
->lexer
);
36569 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36572 else if (strcmp (p
, "enter") == 0)
36574 cp_lexer_consume_token (parser
->lexer
);
36575 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36578 else if (strcmp (p
, "exit") == 0)
36580 cp_lexer_consume_token (parser
->lexer
);
36581 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36584 else if (strcmp (p
, "update") == 0)
36586 cp_lexer_consume_token (parser
->lexer
);
36587 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36590 if (!flag_openmp
) /* flag_openmp_simd */
36592 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36596 stmt
= make_node (OMP_TARGET
);
36597 TREE_TYPE (stmt
) = void_type_node
;
36599 OMP_TARGET_CLAUSES (stmt
)
36600 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36601 "#pragma omp target", pragma_tok
);
36602 pc
= &OMP_TARGET_CLAUSES (stmt
);
36603 keep_next_level (true);
36604 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36606 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36612 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36613 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36616 case GOMP_MAP_ALWAYS_TO
:
36617 case GOMP_MAP_FROM
:
36618 case GOMP_MAP_ALWAYS_FROM
:
36619 case GOMP_MAP_TOFROM
:
36620 case GOMP_MAP_ALWAYS_TOFROM
:
36621 case GOMP_MAP_ALLOC
:
36622 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36623 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36624 case GOMP_MAP_ALWAYS_POINTER
:
36627 error_at (OMP_CLAUSE_LOCATION (*pc
),
36628 "%<#pragma omp target%> with map-type other "
36629 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36630 "on %<map%> clause");
36631 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36634 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36640 # pragma acc cache (variable-list) new-line
36644 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36646 tree stmt
, clauses
;
36648 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36649 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36651 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36653 stmt
= make_node (OACC_CACHE
);
36654 TREE_TYPE (stmt
) = void_type_node
;
36655 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36656 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36663 # pragma acc data oacc-data-clause[optseq] new-line
36664 structured-block */
36666 #define OACC_DATA_CLAUSE_MASK \
36667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36680 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36682 tree stmt
, clauses
, block
;
36685 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36686 "#pragma acc data", pragma_tok
);
36688 block
= begin_omp_parallel ();
36689 save
= cp_parser_begin_omp_structured_block (parser
);
36690 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36691 cp_parser_end_omp_structured_block (parser
, save
);
36692 stmt
= finish_oacc_data (clauses
, block
);
36697 # pragma acc host_data <clauses> new-line
36698 structured-block */
36700 #define OACC_HOST_DATA_CLAUSE_MASK \
36701 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36704 cp_parser_oacc_host_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_HOST_DATA_CLAUSE_MASK
,
36710 "#pragma acc host_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_host_data (clauses
, block
);
36721 # pragma acc declare oacc-data-clause[optseq] new-line
36724 #define OACC_DECLARE_CLAUSE_MASK \
36725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36739 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36741 tree clauses
, stmt
;
36742 bool error
= false;
36744 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36745 "#pragma acc declare", pragma_tok
, true);
36748 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36750 error_at (pragma_tok
->location
,
36751 "no valid clauses specified in %<#pragma acc declare%>");
36755 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36757 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36758 tree decl
= OMP_CLAUSE_DECL (t
);
36759 if (!DECL_P (decl
))
36761 error_at (loc
, "array section in %<#pragma acc declare%>");
36765 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36766 switch (OMP_CLAUSE_MAP_KIND (t
))
36768 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36769 case GOMP_MAP_FORCE_ALLOC
:
36770 case GOMP_MAP_FORCE_TO
:
36771 case GOMP_MAP_FORCE_DEVICEPTR
:
36772 case GOMP_MAP_DEVICE_RESIDENT
:
36775 case GOMP_MAP_LINK
:
36776 if (!global_bindings_p ()
36777 && (TREE_STATIC (decl
)
36778 || !DECL_EXTERNAL (decl
)))
36781 "%qD must be a global variable in "
36782 "%<#pragma acc declare link%>",
36790 if (global_bindings_p ())
36792 error_at (loc
, "invalid OpenACC clause at file scope");
36796 if (DECL_EXTERNAL (decl
))
36799 "invalid use of %<extern%> variable %qD "
36800 "in %<#pragma acc declare%>", decl
);
36804 else if (TREE_PUBLIC (decl
))
36807 "invalid use of %<global%> variable %qD "
36808 "in %<#pragma acc declare%>", decl
);
36815 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36816 || lookup_attribute ("omp declare target link",
36817 DECL_ATTRIBUTES (decl
)))
36819 error_at (loc
, "variable %qD used more than once with "
36820 "%<#pragma acc declare%>", decl
);
36829 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36830 id
= get_identifier ("omp declare target link");
36832 id
= get_identifier ("omp declare target");
36834 DECL_ATTRIBUTES (decl
)
36835 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36836 if (global_bindings_p ())
36838 symtab_node
*node
= symtab_node::get (decl
);
36841 node
->offloadable
= 1;
36842 if (ENABLE_OFFLOADING
)
36844 g
->have_offload
= true;
36845 if (is_a
<varpool_node
*> (node
))
36846 vec_safe_push (offload_vars
, decl
);
36853 if (error
|| global_bindings_p ())
36856 stmt
= make_node (OACC_DECLARE
);
36857 TREE_TYPE (stmt
) = void_type_node
;
36858 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36859 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36867 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36871 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36873 LOC is the location of the #pragma token.
36876 #define OACC_ENTER_DATA_CLAUSE_MASK \
36877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36885 #define OACC_EXIT_DATA_CLAUSE_MASK \
36886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36893 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36896 location_t loc
= pragma_tok
->location
;
36897 tree stmt
, clauses
;
36898 const char *p
= "";
36900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36901 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36903 if (strcmp (p
, "data") != 0)
36905 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36906 enter
? "enter" : "exit");
36907 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36911 cp_lexer_consume_token (parser
->lexer
);
36914 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36915 "#pragma acc enter data", pragma_tok
);
36917 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36918 "#pragma acc exit data", pragma_tok
);
36920 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36922 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36923 enter
? "enter" : "exit");
36927 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36928 TREE_TYPE (stmt
) = void_type_node
;
36929 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36930 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36936 # pragma acc loop oacc-loop-clause[optseq] new-line
36937 structured-block */
36939 #define OACC_LOOP_CLAUSE_MASK \
36940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36952 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36953 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36955 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36957 strcat (p_name
, " loop");
36958 mask
|= OACC_LOOP_CLAUSE_MASK
;
36960 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36964 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36966 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36968 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36971 tree block
= begin_omp_structured_block ();
36972 int save
= cp_parser_begin_omp_structured_block (parser
);
36973 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36974 cp_parser_end_omp_structured_block (parser
, save
);
36975 add_stmt (finish_omp_structured_block (block
));
36981 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36986 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36990 #define OACC_KERNELS_CLAUSE_MASK \
36991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37009 #define OACC_PARALLEL_CLAUSE_MASK \
37010 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37032 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37033 char *p_name
, bool *if_p
)
37035 omp_clause_mask mask
;
37036 enum tree_code code
;
37037 switch (cp_parser_pragma_kind (pragma_tok
))
37039 case PRAGMA_OACC_KERNELS
:
37040 strcat (p_name
, " kernels");
37041 mask
= OACC_KERNELS_CLAUSE_MASK
;
37042 code
= OACC_KERNELS
;
37044 case PRAGMA_OACC_PARALLEL
:
37045 strcat (p_name
, " parallel");
37046 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37047 code
= OACC_PARALLEL
;
37050 gcc_unreachable ();
37053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37056 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37057 if (strcmp (p
, "loop") == 0)
37059 cp_lexer_consume_token (parser
->lexer
);
37060 tree block
= begin_omp_parallel ();
37062 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37064 return finish_omp_construct (code
, block
, clauses
);
37068 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37070 tree block
= begin_omp_parallel ();
37071 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37072 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37073 cp_parser_end_omp_structured_block (parser
, save
);
37074 return finish_omp_construct (code
, block
, clauses
);
37078 # pragma acc update oacc-update-clause[optseq] new-line
37081 #define OACC_UPDATE_CLAUSE_MASK \
37082 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37090 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37092 tree stmt
, clauses
;
37094 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37095 "#pragma acc update", pragma_tok
);
37097 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37099 error_at (pragma_tok
->location
,
37100 "%<#pragma acc update%> must contain at least one "
37101 "%<device%> or %<host%> or %<self%> clause");
37105 stmt
= make_node (OACC_UPDATE
);
37106 TREE_TYPE (stmt
) = void_type_node
;
37107 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37108 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37114 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37116 LOC is the location of the #pragma token.
37119 #define OACC_WAIT_CLAUSE_MASK \
37120 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37123 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37125 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37126 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37128 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37129 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37131 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37132 "#pragma acc wait", pragma_tok
);
37134 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37135 stmt
= finish_expr_stmt (stmt
);
37141 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37143 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37152 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37153 enum pragma_context context
)
37155 bool first_p
= parser
->omp_declare_simd
== NULL
;
37156 cp_omp_declare_simd_data data
;
37159 data
.error_seen
= false;
37160 data
.fndecl_seen
= false;
37161 data
.tokens
= vNULL
;
37162 data
.clauses
= NULL_TREE
;
37163 /* It is safe to take the address of a local variable; it will only be
37164 used while this scope is live. */
37165 parser
->omp_declare_simd
= &data
;
37168 /* Store away all pragma tokens. */
37169 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37170 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37171 cp_lexer_consume_token (parser
->lexer
);
37172 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37173 parser
->omp_declare_simd
->error_seen
= true;
37174 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37175 struct cp_token_cache
*cp
37176 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37177 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37181 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37182 cp_parser_pragma (parser
, context
, NULL
);
37185 case pragma_external
:
37186 cp_parser_declaration (parser
);
37188 case pragma_member
:
37189 cp_parser_member_declaration (parser
);
37191 case pragma_objc_icode
:
37192 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37195 cp_parser_declaration_statement (parser
);
37198 if (parser
->omp_declare_simd
37199 && !parser
->omp_declare_simd
->error_seen
37200 && !parser
->omp_declare_simd
->fndecl_seen
)
37201 error_at (pragma_tok
->location
,
37202 "%<#pragma omp declare simd%> not immediately followed by "
37203 "function declaration or definition");
37204 data
.tokens
.release ();
37205 parser
->omp_declare_simd
= NULL
;
37209 /* Finalize #pragma omp declare simd clauses after direct declarator has
37210 been parsed, and put that into "omp declare simd" attribute. */
37213 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37215 struct cp_token_cache
*ce
;
37216 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37219 if (!data
->error_seen
&& data
->fndecl_seen
)
37221 error ("%<#pragma omp declare simd%> not immediately followed by "
37222 "a single function declaration or definition");
37223 data
->error_seen
= true;
37225 if (data
->error_seen
)
37228 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37232 cp_parser_push_lexer_for_tokens (parser
, ce
);
37233 parser
->lexer
->in_pragma
= true;
37234 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37235 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37236 cp_lexer_consume_token (parser
->lexer
);
37237 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37238 "#pragma omp declare simd", pragma_tok
);
37239 cp_parser_pop_lexer (parser
);
37241 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37242 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37243 TREE_CHAIN (c
) = attrs
;
37244 if (processing_template_decl
)
37245 ATTR_IS_DEPENDENT (c
) = 1;
37249 data
->fndecl_seen
= true;
37255 # pragma omp declare target new-line
37256 declarations and definitions
37257 # pragma omp end declare target new-line
37260 # pragma omp declare target ( extended-list ) new-line
37262 # pragma omp declare target declare-target-clauses[seq] new-line */
37264 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37265 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37269 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37271 tree clauses
= NULL_TREE
;
37272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37274 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37275 "#pragma omp declare target", pragma_tok
);
37276 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37278 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37280 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37281 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37285 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37286 scope_chain
->omp_declare_target_attribute
++;
37289 if (scope_chain
->omp_declare_target_attribute
)
37290 error_at (pragma_tok
->location
,
37291 "%<#pragma omp declare target%> with clauses in between "
37292 "%<#pragma omp declare target%> without clauses and "
37293 "%<#pragma omp end declare target%>");
37294 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37296 tree t
= OMP_CLAUSE_DECL (c
), id
;
37297 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37298 tree at2
= lookup_attribute ("omp declare target link",
37299 DECL_ATTRIBUTES (t
));
37300 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37302 id
= get_identifier ("omp declare target link");
37303 std::swap (at1
, at2
);
37306 id
= get_identifier ("omp declare target");
37309 error_at (OMP_CLAUSE_LOCATION (c
),
37310 "%qD specified both in declare target %<link%> and %<to%>"
37316 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37317 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37320 symtab_node
*node
= symtab_node::get (t
);
37323 node
->offloadable
= 1;
37324 if (ENABLE_OFFLOADING
)
37326 g
->have_offload
= true;
37327 if (is_a
<varpool_node
*> (node
))
37328 vec_safe_push (offload_vars
, t
);
37336 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37338 const char *p
= "";
37339 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37341 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37342 p
= IDENTIFIER_POINTER (id
);
37344 if (strcmp (p
, "declare") == 0)
37346 cp_lexer_consume_token (parser
->lexer
);
37348 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37350 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37351 p
= IDENTIFIER_POINTER (id
);
37353 if (strcmp (p
, "target") == 0)
37354 cp_lexer_consume_token (parser
->lexer
);
37357 cp_parser_error (parser
, "expected %<target%>");
37358 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37364 cp_parser_error (parser
, "expected %<declare%>");
37365 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37368 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37369 if (!scope_chain
->omp_declare_target_attribute
)
37370 error_at (pragma_tok
->location
,
37371 "%<#pragma omp end declare target%> without corresponding "
37372 "%<#pragma omp declare target%>");
37374 scope_chain
->omp_declare_target_attribute
--;
37377 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37378 expression and optional initializer clause of
37379 #pragma omp declare reduction. We store the expression(s) as
37380 either 3, 6 or 7 special statements inside of the artificial function's
37381 body. The first two statements are DECL_EXPRs for the artificial
37382 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37383 expression that uses those variables.
37384 If there was any INITIALIZER clause, this is followed by further statements,
37385 the fourth and fifth statements are DECL_EXPRs for the artificial
37386 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37387 constructor variant (first token after open paren is not omp_priv),
37388 then the sixth statement is a statement with the function call expression
37389 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37390 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37391 to initialize the OMP_PRIV artificial variable and there is seventh
37392 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37395 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37397 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37398 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37399 type
= TREE_TYPE (type
);
37400 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37401 DECL_ARTIFICIAL (omp_out
) = 1;
37402 pushdecl (omp_out
);
37403 add_decl_expr (omp_out
);
37404 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37405 DECL_ARTIFICIAL (omp_in
) = 1;
37407 add_decl_expr (omp_in
);
37409 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37411 keep_next_level (true);
37412 tree block
= begin_omp_structured_block ();
37413 combiner
= cp_parser_expression (parser
);
37414 finish_expr_stmt (combiner
);
37415 block
= finish_omp_structured_block (block
);
37418 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37421 const char *p
= "";
37422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37424 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37425 p
= IDENTIFIER_POINTER (id
);
37428 if (strcmp (p
, "initializer") == 0)
37430 cp_lexer_consume_token (parser
->lexer
);
37431 matching_parens parens
;
37432 if (!parens
.require_open (parser
))
37436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37438 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37439 p
= IDENTIFIER_POINTER (id
);
37442 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37443 DECL_ARTIFICIAL (omp_priv
) = 1;
37444 pushdecl (omp_priv
);
37445 add_decl_expr (omp_priv
);
37446 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37447 DECL_ARTIFICIAL (omp_orig
) = 1;
37448 pushdecl (omp_orig
);
37449 add_decl_expr (omp_orig
);
37451 keep_next_level (true);
37452 block
= begin_omp_structured_block ();
37455 if (strcmp (p
, "omp_priv") == 0)
37457 bool is_direct_init
, is_non_constant_init
;
37459 cp_lexer_consume_token (parser
->lexer
);
37460 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37462 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37463 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37465 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37466 == CPP_CLOSE_PAREN
))
37468 finish_omp_structured_block (block
);
37469 error ("invalid initializer clause");
37472 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37473 &is_non_constant_init
);
37474 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37475 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37479 cp_parser_parse_tentatively (parser
);
37480 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37481 /*check_dependency_p=*/true,
37482 /*template_p=*/NULL
,
37483 /*declarator_p=*/false,
37484 /*optional_p=*/false);
37485 vec
<tree
, va_gc
> *args
;
37486 if (fn_name
== error_mark_node
37487 || cp_parser_error_occurred (parser
)
37488 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37489 || ((args
= cp_parser_parenthesized_expression_list
37490 (parser
, non_attr
, /*cast_p=*/false,
37491 /*allow_expansion_p=*/true,
37492 /*non_constant_p=*/NULL
)),
37493 cp_parser_error_occurred (parser
)))
37495 finish_omp_structured_block (block
);
37496 cp_parser_abort_tentative_parse (parser
);
37497 cp_parser_error (parser
, "expected id-expression (arguments)");
37502 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37503 if (arg
== omp_priv
37504 || (TREE_CODE (arg
) == ADDR_EXPR
37505 && TREE_OPERAND (arg
, 0) == omp_priv
))
37507 cp_parser_abort_tentative_parse (parser
);
37508 if (arg
== NULL_TREE
)
37509 error ("one of the initializer call arguments should be %<omp_priv%>"
37510 " or %<&omp_priv%>");
37511 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37513 finish_expr_stmt (initializer
);
37516 block
= finish_omp_structured_block (block
);
37517 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37521 add_decl_expr (omp_orig
);
37523 if (!parens
.require_close (parser
))
37527 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37528 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37535 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37536 initializer-clause[opt] new-line
37538 initializer-clause:
37539 initializer (omp_priv initializer)
37540 initializer (function-name (argument-list)) */
37543 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37544 enum pragma_context
)
37546 auto_vec
<tree
> types
;
37547 enum tree_code reduc_code
= ERROR_MARK
;
37548 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37550 cp_token
*first_token
;
37551 cp_token_cache
*cp
;
37555 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37556 p
= obstack_alloc (&declarator_obstack
, 0);
37558 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37561 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37564 reduc_code
= PLUS_EXPR
;
37567 reduc_code
= MULT_EXPR
;
37570 reduc_code
= MINUS_EXPR
;
37573 reduc_code
= BIT_AND_EXPR
;
37576 reduc_code
= BIT_XOR_EXPR
;
37579 reduc_code
= BIT_IOR_EXPR
;
37582 reduc_code
= TRUTH_ANDIF_EXPR
;
37585 reduc_code
= TRUTH_ORIF_EXPR
;
37588 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37591 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37592 "%<|%>, %<&&%>, %<||%> or identifier");
37596 if (reduc_code
!= ERROR_MARK
)
37597 cp_lexer_consume_token (parser
->lexer
);
37599 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37600 if (reduc_id
== error_mark_node
)
37603 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37606 /* Types may not be defined in declare reduction type list. */
37607 const char *saved_message
;
37608 saved_message
= parser
->type_definition_forbidden_message
;
37609 parser
->type_definition_forbidden_message
37610 = G_("types may not be defined in declare reduction type list");
37611 bool saved_colon_corrects_to_scope_p
;
37612 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37613 parser
->colon_corrects_to_scope_p
= false;
37614 bool saved_colon_doesnt_start_class_def_p
;
37615 saved_colon_doesnt_start_class_def_p
37616 = parser
->colon_doesnt_start_class_def_p
;
37617 parser
->colon_doesnt_start_class_def_p
= true;
37621 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37622 type
= cp_parser_type_id (parser
);
37623 if (type
== error_mark_node
)
37625 else if (ARITHMETIC_TYPE_P (type
)
37626 && (orig_reduc_id
== NULL_TREE
37627 || (TREE_CODE (type
) != COMPLEX_TYPE
37628 && (id_equal (orig_reduc_id
, "min")
37629 || id_equal (orig_reduc_id
, "max")))))
37630 error_at (loc
, "predeclared arithmetic type %qT in "
37631 "%<#pragma omp declare reduction%>", type
);
37632 else if (TREE_CODE (type
) == FUNCTION_TYPE
37633 || TREE_CODE (type
) == METHOD_TYPE
37634 || TREE_CODE (type
) == ARRAY_TYPE
)
37635 error_at (loc
, "function or array type %qT in "
37636 "%<#pragma omp declare reduction%>", type
);
37637 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37638 error_at (loc
, "reference type %qT in "
37639 "%<#pragma omp declare reduction%>", type
);
37640 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37641 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37642 "%<#pragma omp declare reduction%>", type
);
37644 types
.safe_push (type
);
37646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37647 cp_lexer_consume_token (parser
->lexer
);
37652 /* Restore the saved message. */
37653 parser
->type_definition_forbidden_message
= saved_message
;
37654 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37655 parser
->colon_doesnt_start_class_def_p
37656 = saved_colon_doesnt_start_class_def_p
;
37658 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37659 || types
.is_empty ())
37662 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37666 first_token
= cp_lexer_peek_token (parser
->lexer
);
37669 FOR_EACH_VEC_ELT (types
, i
, type
)
37672 = build_function_type_list (void_type_node
,
37673 cp_build_reference_type (type
, false),
37675 tree this_reduc_id
= reduc_id
;
37676 if (!dependent_type_p (type
))
37677 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37678 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37679 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37680 DECL_ARTIFICIAL (fndecl
) = 1;
37681 DECL_EXTERNAL (fndecl
) = 1;
37682 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37683 DECL_IGNORED_P (fndecl
) = 1;
37684 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37685 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37686 DECL_ATTRIBUTES (fndecl
)
37687 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37688 DECL_ATTRIBUTES (fndecl
));
37689 if (processing_template_decl
)
37690 fndecl
= push_template_decl (fndecl
);
37691 bool block_scope
= false;
37692 tree block
= NULL_TREE
;
37693 if (current_function_decl
)
37695 block_scope
= true;
37696 DECL_CONTEXT (fndecl
) = global_namespace
;
37697 if (!processing_template_decl
)
37700 else if (current_class_type
)
37704 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37705 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37706 cp_lexer_consume_token (parser
->lexer
);
37707 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37709 cp
= cp_token_cache_new (first_token
,
37710 cp_lexer_peek_nth_token (parser
->lexer
,
37713 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37714 finish_member_declaration (fndecl
);
37715 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37716 DECL_PENDING_INLINE_P (fndecl
) = 1;
37717 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37722 DECL_CONTEXT (fndecl
) = current_namespace
;
37726 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37728 block
= begin_omp_structured_block ();
37731 cp_parser_push_lexer_for_tokens (parser
, cp
);
37732 parser
->lexer
->in_pragma
= true;
37734 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37737 finish_function (/*inline_p=*/false);
37739 DECL_CONTEXT (fndecl
) = current_function_decl
;
37741 cp_parser_pop_lexer (parser
);
37745 cp_parser_pop_lexer (parser
);
37747 finish_function (/*inline_p=*/false);
37750 DECL_CONTEXT (fndecl
) = current_function_decl
;
37751 block
= finish_omp_structured_block (block
);
37752 if (TREE_CODE (block
) == BIND_EXPR
)
37753 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37754 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37755 DECL_SAVED_TREE (fndecl
) = block
;
37756 if (processing_template_decl
)
37757 add_decl_expr (fndecl
);
37759 cp_check_omp_declare_reduction (fndecl
);
37760 if (cp
== NULL
&& types
.length () > 1)
37761 cp
= cp_token_cache_new (first_token
,
37762 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37763 if (errs
!= errorcount
)
37767 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37770 /* Free any declarators allocated. */
37771 obstack_free (&declarator_obstack
, p
);
37775 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37776 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37777 initializer-clause[opt] new-line
37778 #pragma omp declare target new-line */
37781 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37782 enum pragma_context context
)
37784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37786 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37787 const char *p
= IDENTIFIER_POINTER (id
);
37789 if (strcmp (p
, "simd") == 0)
37791 cp_lexer_consume_token (parser
->lexer
);
37792 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37796 cp_ensure_no_omp_declare_simd (parser
);
37797 if (strcmp (p
, "reduction") == 0)
37799 cp_lexer_consume_token (parser
->lexer
);
37800 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37804 if (!flag_openmp
) /* flag_openmp_simd */
37806 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37809 if (strcmp (p
, "target") == 0)
37811 cp_lexer_consume_token (parser
->lexer
);
37812 cp_parser_omp_declare_target (parser
, pragma_tok
);
37816 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37818 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37823 #pragma omp taskloop taskloop-clause[optseq] new-line
37826 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37829 #define OMP_TASKLOOP_CLAUSE_MASK \
37830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37846 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37847 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37850 tree clauses
, sb
, ret
;
37852 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37854 strcat (p_name
, " taskloop");
37855 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37857 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37859 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37860 const char *p
= IDENTIFIER_POINTER (id
);
37862 if (strcmp (p
, "simd") == 0)
37864 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37865 if (cclauses
== NULL
)
37866 cclauses
= cclauses_buf
;
37868 cp_lexer_consume_token (parser
->lexer
);
37869 if (!flag_openmp
) /* flag_openmp_simd */
37870 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37872 sb
= begin_omp_structured_block ();
37873 save
= cp_parser_begin_omp_structured_block (parser
);
37874 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37876 cp_parser_end_omp_structured_block (parser
, save
);
37877 tree body
= finish_omp_structured_block (sb
);
37880 ret
= make_node (OMP_TASKLOOP
);
37881 TREE_TYPE (ret
) = void_type_node
;
37882 OMP_FOR_BODY (ret
) = body
;
37883 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37884 SET_EXPR_LOCATION (ret
, loc
);
37889 if (!flag_openmp
) /* flag_openmp_simd */
37891 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37895 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37899 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37900 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37903 sb
= begin_omp_structured_block ();
37904 save
= cp_parser_begin_omp_structured_block (parser
);
37906 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37909 cp_parser_end_omp_structured_block (parser
, save
);
37910 add_stmt (finish_omp_structured_block (sb
));
37917 # pragma acc routine oacc-routine-clause[optseq] new-line
37918 function-definition
37920 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37923 #define OACC_ROUTINE_CLAUSE_MASK \
37924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37930 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37931 component, which must resolve to a declared namespace-scope
37932 function. The clauses are either processed directly (for a named
37933 function), or defered until the immediatley following declaration
37937 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37938 enum pragma_context context
)
37940 gcc_checking_assert (context
== pragma_external
);
37941 /* The checking for "another pragma following this one" in the "no optional
37942 '( name )'" case makes sure that we dont re-enter. */
37943 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37945 cp_oacc_routine_data data
;
37946 data
.error_seen
= false;
37947 data
.fndecl_seen
= false;
37948 data
.tokens
= vNULL
;
37949 data
.clauses
= NULL_TREE
;
37950 data
.loc
= pragma_tok
->location
;
37951 /* It is safe to take the address of a local variable; it will only be
37952 used while this scope is live. */
37953 parser
->oacc_routine
= &data
;
37955 /* Look for optional '( name )'. */
37956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37958 matching_parens parens
;
37959 parens
.consume_open (parser
); /* '(' */
37961 /* We parse the name as an id-expression. If it resolves to
37962 anything other than a non-overloaded function at namespace
37963 scope, it's an error. */
37964 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37965 tree name
= cp_parser_id_expression (parser
,
37966 /*template_keyword_p=*/false,
37967 /*check_dependency_p=*/false,
37968 /*template_p=*/NULL
,
37969 /*declarator_p=*/false,
37970 /*optional_p=*/false);
37971 tree decl
= (identifier_p (name
)
37972 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
37974 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37975 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37977 if (decl
== error_mark_node
37978 || !parens
.require_close (parser
))
37980 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37981 parser
->oacc_routine
= NULL
;
37986 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37987 "#pragma acc routine",
37988 cp_lexer_peek_token (parser
->lexer
));
37990 if (decl
&& is_overloaded_fn (decl
)
37991 && (TREE_CODE (decl
) != FUNCTION_DECL
37992 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37994 error_at (name_loc
,
37995 "%<#pragma acc routine%> names a set of overloads");
37996 parser
->oacc_routine
= NULL
;
38000 /* Perhaps we should use the same rule as declarations in different
38002 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38004 error_at (name_loc
,
38005 "%qD does not refer to a namespace scope function", decl
);
38006 parser
->oacc_routine
= NULL
;
38010 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38012 error_at (name_loc
, "%qD does not refer to a function", decl
);
38013 parser
->oacc_routine
= NULL
;
38017 cp_finalize_oacc_routine (parser
, decl
, false);
38018 parser
->oacc_routine
= NULL
;
38020 else /* No optional '( name )'. */
38022 /* Store away all pragma tokens. */
38023 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38024 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38025 cp_lexer_consume_token (parser
->lexer
);
38026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38027 parser
->oacc_routine
->error_seen
= true;
38028 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38029 struct cp_token_cache
*cp
38030 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38031 parser
->oacc_routine
->tokens
.safe_push (cp
);
38033 /* Emit a helpful diagnostic if there's another pragma following this
38035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38037 cp_ensure_no_oacc_routine (parser
);
38038 data
.tokens
.release ();
38039 /* ..., and then just keep going. */
38043 /* We only have to consider the pragma_external case here. */
38044 cp_parser_declaration (parser
);
38045 if (parser
->oacc_routine
38046 && !parser
->oacc_routine
->fndecl_seen
)
38047 cp_ensure_no_oacc_routine (parser
);
38049 parser
->oacc_routine
= NULL
;
38050 data
.tokens
.release ();
38054 /* Finalize #pragma acc routine clauses after direct declarator has
38058 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38060 struct cp_token_cache
*ce
;
38061 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38063 if (!data
->error_seen
&& data
->fndecl_seen
)
38065 error_at (data
->loc
,
38066 "%<#pragma acc routine%> not immediately followed by "
38067 "a single function declaration or definition");
38068 data
->error_seen
= true;
38070 if (data
->error_seen
)
38073 gcc_checking_assert (data
->tokens
.length () == 1);
38074 ce
= data
->tokens
[0];
38076 cp_parser_push_lexer_for_tokens (parser
, ce
);
38077 parser
->lexer
->in_pragma
= true;
38078 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38080 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38081 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38082 parser
->oacc_routine
->clauses
38083 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38084 "#pragma acc routine", pragma_tok
);
38085 cp_parser_pop_lexer (parser
);
38086 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38092 /* Apply any saved OpenACC routine clauses to a just-parsed
38096 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38098 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38100 /* Keep going if we're in error reporting mode. */
38101 if (parser
->oacc_routine
->error_seen
38102 || fndecl
== error_mark_node
)
38105 if (parser
->oacc_routine
->fndecl_seen
)
38107 error_at (parser
->oacc_routine
->loc
,
38108 "%<#pragma acc routine%> not immediately followed by"
38109 " a single function declaration or definition");
38110 parser
->oacc_routine
= NULL
;
38113 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38115 cp_ensure_no_oacc_routine (parser
);
38119 if (oacc_get_fn_attrib (fndecl
))
38121 error_at (parser
->oacc_routine
->loc
,
38122 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38123 parser
->oacc_routine
= NULL
;
38127 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38129 error_at (parser
->oacc_routine
->loc
,
38131 ? G_("%<#pragma acc routine%> must be applied before use")
38132 : G_("%<#pragma acc routine%> must be applied before "
38134 parser
->oacc_routine
= NULL
;
38138 /* Process the routine's dimension clauses. */
38139 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38140 oacc_replace_fn_attrib (fndecl
, dims
);
38142 /* Add an "omp declare target" attribute. */
38143 DECL_ATTRIBUTES (fndecl
)
38144 = tree_cons (get_identifier ("omp declare target"),
38145 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38147 /* Don't unset parser->oacc_routine here: we may still need it to
38148 diagnose wrong usage. But, remember that we've used this "#pragma acc
38150 parser
->oacc_routine
->fndecl_seen
= true;
38154 /* Main entry point to OpenMP statement pragmas. */
38157 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38160 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38161 omp_clause_mask
mask (0);
38163 switch (cp_parser_pragma_kind (pragma_tok
))
38165 case PRAGMA_OACC_ATOMIC
:
38166 cp_parser_omp_atomic (parser
, pragma_tok
);
38168 case PRAGMA_OACC_CACHE
:
38169 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38171 case PRAGMA_OACC_DATA
:
38172 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38174 case PRAGMA_OACC_ENTER_DATA
:
38175 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38177 case PRAGMA_OACC_EXIT_DATA
:
38178 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38180 case PRAGMA_OACC_HOST_DATA
:
38181 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38183 case PRAGMA_OACC_KERNELS
:
38184 case PRAGMA_OACC_PARALLEL
:
38185 strcpy (p_name
, "#pragma acc");
38186 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38189 case PRAGMA_OACC_LOOP
:
38190 strcpy (p_name
, "#pragma acc");
38191 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38194 case PRAGMA_OACC_UPDATE
:
38195 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38197 case PRAGMA_OACC_WAIT
:
38198 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38200 case PRAGMA_OMP_ATOMIC
:
38201 cp_parser_omp_atomic (parser
, pragma_tok
);
38203 case PRAGMA_OMP_CRITICAL
:
38204 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38206 case PRAGMA_OMP_DISTRIBUTE
:
38207 strcpy (p_name
, "#pragma omp");
38208 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38211 case PRAGMA_OMP_FOR
:
38212 strcpy (p_name
, "#pragma omp");
38213 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38216 case PRAGMA_OMP_MASTER
:
38217 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38219 case PRAGMA_OMP_PARALLEL
:
38220 strcpy (p_name
, "#pragma omp");
38221 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38224 case PRAGMA_OMP_SECTIONS
:
38225 strcpy (p_name
, "#pragma omp");
38226 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38228 case PRAGMA_OMP_SIMD
:
38229 strcpy (p_name
, "#pragma omp");
38230 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38233 case PRAGMA_OMP_SINGLE
:
38234 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38236 case PRAGMA_OMP_TASK
:
38237 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38239 case PRAGMA_OMP_TASKGROUP
:
38240 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38242 case PRAGMA_OMP_TASKLOOP
:
38243 strcpy (p_name
, "#pragma omp");
38244 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38247 case PRAGMA_OMP_TEAMS
:
38248 strcpy (p_name
, "#pragma omp");
38249 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38253 gcc_unreachable ();
38256 protected_set_expr_location (stmt
, pragma_tok
->location
);
38259 /* Transactional Memory parsing routines. */
38261 /* Parse a transaction attribute.
38267 We use this instead of cp_parser_attributes_opt for transactions to avoid
38268 the pedwarn in C++98 mode. */
38271 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38274 tree attr_name
, attr
= NULL
;
38276 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38277 return cp_parser_attributes_opt (parser
);
38279 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38281 cp_lexer_consume_token (parser
->lexer
);
38282 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38285 token
= cp_lexer_peek_token (parser
->lexer
);
38286 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38288 token
= cp_lexer_consume_token (parser
->lexer
);
38290 attr_name
= (token
->type
== CPP_KEYWORD
38291 /* For keywords, use the canonical spelling,
38292 not the parsed identifier. */
38293 ? ridpointers
[(int) token
->keyword
]
38295 attr
= build_tree_list (attr_name
, NULL_TREE
);
38298 cp_parser_error (parser
, "expected identifier");
38300 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38302 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38306 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38308 transaction-statement:
38309 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38311 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38315 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38317 unsigned char old_in
= parser
->in_transaction
;
38318 unsigned char this_in
= 1, new_in
;
38319 enum rid keyword
= token
->keyword
;
38320 tree stmt
, attrs
, noex
;
38322 cp_lexer_consume_token (parser
->lexer
);
38324 if (keyword
== RID_TRANSACTION_RELAXED
38325 || keyword
== RID_SYNCHRONIZED
)
38326 this_in
|= TM_STMT_ATTR_RELAXED
;
38329 attrs
= cp_parser_txn_attribute_opt (parser
);
38331 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38334 /* Parse a noexcept specification. */
38335 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38336 noex
= boolean_true_node
;
38337 else if (keyword
== RID_ATOMIC_CANCEL
)
38339 /* cancel-and-throw is unimplemented. */
38340 sorry ("atomic_cancel");
38344 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38346 /* Keep track if we're in the lexical scope of an outer transaction. */
38347 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38349 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38351 parser
->in_transaction
= new_in
;
38352 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38353 parser
->in_transaction
= old_in
;
38355 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38360 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38362 transaction-expression:
38363 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38364 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38368 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38370 unsigned char old_in
= parser
->in_transaction
;
38371 unsigned char this_in
= 1;
38375 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38377 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38378 || keyword
== RID_TRANSACTION_RELAXED
);
38382 keyword
== RID_TRANSACTION_RELAXED
38383 ? G_("%<__transaction_relaxed%> without transactional memory "
38385 : G_("%<__transaction_atomic%> without transactional memory "
38386 "support enabled"));
38388 token
= cp_parser_require_keyword (parser
, keyword
,
38389 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38390 : RT_TRANSACTION_RELAXED
));
38391 gcc_assert (token
!= NULL
);
38393 if (keyword
== RID_TRANSACTION_RELAXED
)
38394 this_in
|= TM_STMT_ATTR_RELAXED
;
38396 /* Set this early. This might mean that we allow transaction_cancel in
38397 an expression that we find out later actually has to be a constexpr.
38398 However, we expect that cxx_constant_value will be able to deal with
38399 this; also, if the noexcept has no constexpr, then what we parse next
38400 really is a transaction's body. */
38401 parser
->in_transaction
= this_in
;
38403 /* Parse a noexcept specification. */
38404 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38407 if (!noex
|| !noex_expr
38408 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38410 matching_parens parens
;
38411 parens
.require_open (parser
);
38413 expr
= cp_parser_expression (parser
);
38414 expr
= finish_parenthesized_expr (expr
);
38416 parens
.require_close (parser
);
38420 /* The only expression that is available got parsed for the noexcept
38421 already. noexcept is true then. */
38423 noex
= boolean_true_node
;
38426 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38427 parser
->in_transaction
= old_in
;
38429 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38430 return error_mark_node
;
38432 return (flag_tm
? expr
: error_mark_node
);
38435 /* Parse a function-transaction-block.
38437 function-transaction-block:
38438 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38440 __transaction_atomic txn-attribute[opt] function-try-block
38441 __transaction_relaxed ctor-initializer[opt] function-body
38442 __transaction_relaxed function-try-block
38446 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38448 unsigned char old_in
= parser
->in_transaction
;
38449 unsigned char new_in
= 1;
38450 tree compound_stmt
, stmt
, attrs
;
38453 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38454 || keyword
== RID_TRANSACTION_RELAXED
);
38455 token
= cp_parser_require_keyword (parser
, keyword
,
38456 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38457 : RT_TRANSACTION_RELAXED
));
38458 gcc_assert (token
!= NULL
);
38460 if (keyword
== RID_TRANSACTION_RELAXED
)
38461 new_in
|= TM_STMT_ATTR_RELAXED
;
38464 attrs
= cp_parser_txn_attribute_opt (parser
);
38466 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38469 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38471 parser
->in_transaction
= new_in
;
38473 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38474 cp_parser_function_try_block (parser
);
38476 cp_parser_ctor_initializer_opt_and_function_body
38477 (parser
, /*in_function_try_block=*/false);
38479 parser
->in_transaction
= old_in
;
38481 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38484 /* Parse a __transaction_cancel statement.
38487 __transaction_cancel txn-attribute[opt] ;
38488 __transaction_cancel txn-attribute[opt] throw-expression ;
38490 ??? Cancel and throw is not yet implemented. */
38493 cp_parser_transaction_cancel (cp_parser
*parser
)
38496 bool is_outer
= false;
38499 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38500 RT_TRANSACTION_CANCEL
);
38501 gcc_assert (token
!= NULL
);
38503 attrs
= cp_parser_txn_attribute_opt (parser
);
38505 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38507 /* ??? Parse cancel-and-throw here. */
38509 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38513 error_at (token
->location
, "%<__transaction_cancel%> without "
38514 "transactional memory support enabled");
38515 return error_mark_node
;
38517 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38519 error_at (token
->location
, "%<__transaction_cancel%> within a "
38520 "%<__transaction_relaxed%>");
38521 return error_mark_node
;
38525 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38526 && !is_tm_may_cancel_outer (current_function_decl
))
38528 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38529 "within outer %<__transaction_atomic%>");
38530 error_at (token
->location
,
38531 " or a %<transaction_may_cancel_outer%> function");
38532 return error_mark_node
;
38535 else if (parser
->in_transaction
== 0)
38537 error_at (token
->location
, "%<__transaction_cancel%> not within "
38538 "%<__transaction_atomic%>");
38539 return error_mark_node
;
38542 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38550 static GTY (()) cp_parser
*the_parser
;
38553 /* Special handling for the first token or line in the file. The first
38554 thing in the file might be #pragma GCC pch_preprocess, which loads a
38555 PCH file, which is a GC collection point. So we need to handle this
38556 first pragma without benefit of an existing lexer structure.
38558 Always returns one token to the caller in *FIRST_TOKEN. This is
38559 either the true first token of the file, or the first token after
38560 the initial pragma. */
38563 cp_parser_initial_pragma (cp_token
*first_token
)
38567 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38568 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38571 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38572 if (first_token
->type
== CPP_STRING
)
38574 name
= first_token
->u
.value
;
38576 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38577 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38578 error_at (first_token
->location
,
38579 "junk at end of %<#pragma GCC pch_preprocess%>");
38582 error_at (first_token
->location
, "expected string literal");
38584 /* Skip to the end of the pragma. */
38585 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38586 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38588 /* Now actually load the PCH file. */
38590 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38592 /* Read one more token to return to our caller. We have to do this
38593 after reading the PCH file in, since its pointers have to be
38595 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38598 /* Parse a pragma GCC ivdep. */
38601 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38603 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38607 /* Parse a pragma GCC unroll. */
38609 static unsigned short
38610 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38612 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38613 tree expr
= cp_parser_constant_expression (parser
);
38614 unsigned short unroll
;
38615 expr
= maybe_constant_value (expr
);
38616 HOST_WIDE_INT lunroll
= 0;
38617 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38618 || TREE_CODE (expr
) != INTEGER_CST
38619 || (lunroll
= tree_to_shwi (expr
)) < 0
38620 || lunroll
>= USHRT_MAX
)
38622 error_at (location
, "%<#pragma GCC unroll%> requires an"
38623 " assignment-expression that evaluates to a non-negative"
38624 " integral constant less than %u", USHRT_MAX
);
38629 unroll
= (unsigned short)lunroll
;
38633 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38637 /* Normal parsing of a pragma token. Here we can (and must) use the
38641 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38643 cp_token
*pragma_tok
;
38648 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38649 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38650 parser
->lexer
->in_pragma
= true;
38652 id
= cp_parser_pragma_kind (pragma_tok
);
38653 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38654 cp_ensure_no_omp_declare_simd (parser
);
38657 case PRAGMA_GCC_PCH_PREPROCESS
:
38658 error_at (pragma_tok
->location
,
38659 "%<#pragma GCC pch_preprocess%> must be first");
38662 case PRAGMA_OMP_BARRIER
:
38665 case pragma_compound
:
38666 cp_parser_omp_barrier (parser
, pragma_tok
);
38669 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38670 "used in compound statements", "omp barrier");
38677 case PRAGMA_OMP_FLUSH
:
38680 case pragma_compound
:
38681 cp_parser_omp_flush (parser
, pragma_tok
);
38684 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38685 "used in compound statements", "omp flush");
38692 case PRAGMA_OMP_TASKWAIT
:
38695 case pragma_compound
:
38696 cp_parser_omp_taskwait (parser
, pragma_tok
);
38699 error_at (pragma_tok
->location
,
38700 "%<#pragma %s%> may only be used in compound statements",
38708 case PRAGMA_OMP_TASKYIELD
:
38711 case pragma_compound
:
38712 cp_parser_omp_taskyield (parser
, pragma_tok
);
38715 error_at (pragma_tok
->location
,
38716 "%<#pragma %s%> may only be used in compound statements",
38724 case PRAGMA_OMP_CANCEL
:
38727 case pragma_compound
:
38728 cp_parser_omp_cancel (parser
, pragma_tok
);
38731 error_at (pragma_tok
->location
,
38732 "%<#pragma %s%> may only be used in compound statements",
38740 case PRAGMA_OMP_CANCELLATION_POINT
:
38741 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38744 case PRAGMA_OMP_THREADPRIVATE
:
38745 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38748 case PRAGMA_OMP_DECLARE
:
38749 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38751 case PRAGMA_OACC_DECLARE
:
38752 cp_parser_oacc_declare (parser
, pragma_tok
);
38755 case PRAGMA_OACC_ENTER_DATA
:
38756 if (context
== pragma_stmt
)
38758 error_at (pragma_tok
->location
,
38759 "%<#pragma %s%> may only be used in compound statements",
38763 else if (context
!= pragma_compound
)
38765 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38768 case PRAGMA_OACC_EXIT_DATA
:
38769 if (context
== pragma_stmt
)
38771 error_at (pragma_tok
->location
,
38772 "%<#pragma %s%> may only be used in compound statements",
38776 else if (context
!= pragma_compound
)
38778 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38781 case PRAGMA_OACC_ROUTINE
:
38782 if (context
!= pragma_external
)
38784 error_at (pragma_tok
->location
,
38785 "%<#pragma acc routine%> must be at file scope");
38788 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38791 case PRAGMA_OACC_UPDATE
:
38792 if (context
== pragma_stmt
)
38794 error_at (pragma_tok
->location
,
38795 "%<#pragma %s%> may only be used in compound statements",
38799 else if (context
!= pragma_compound
)
38801 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38804 case PRAGMA_OACC_WAIT
:
38805 if (context
== pragma_stmt
)
38807 error_at (pragma_tok
->location
,
38808 "%<#pragma %s%> may only be used in compound statements",
38812 else if (context
!= pragma_compound
)
38814 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38817 case PRAGMA_OACC_ATOMIC
:
38818 case PRAGMA_OACC_CACHE
:
38819 case PRAGMA_OACC_DATA
:
38820 case PRAGMA_OACC_HOST_DATA
:
38821 case PRAGMA_OACC_KERNELS
:
38822 case PRAGMA_OACC_PARALLEL
:
38823 case PRAGMA_OACC_LOOP
:
38824 case PRAGMA_OMP_ATOMIC
:
38825 case PRAGMA_OMP_CRITICAL
:
38826 case PRAGMA_OMP_DISTRIBUTE
:
38827 case PRAGMA_OMP_FOR
:
38828 case PRAGMA_OMP_MASTER
:
38829 case PRAGMA_OMP_PARALLEL
:
38830 case PRAGMA_OMP_SECTIONS
:
38831 case PRAGMA_OMP_SIMD
:
38832 case PRAGMA_OMP_SINGLE
:
38833 case PRAGMA_OMP_TASK
:
38834 case PRAGMA_OMP_TASKGROUP
:
38835 case PRAGMA_OMP_TASKLOOP
:
38836 case PRAGMA_OMP_TEAMS
:
38837 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38839 stmt
= push_omp_privatization_clauses (false);
38840 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38841 pop_omp_privatization_clauses (stmt
);
38844 case PRAGMA_OMP_ORDERED
:
38845 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38847 stmt
= push_omp_privatization_clauses (false);
38848 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38849 pop_omp_privatization_clauses (stmt
);
38852 case PRAGMA_OMP_TARGET
:
38853 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38855 stmt
= push_omp_privatization_clauses (false);
38856 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38857 pop_omp_privatization_clauses (stmt
);
38860 case PRAGMA_OMP_END_DECLARE_TARGET
:
38861 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38864 case PRAGMA_OMP_SECTION
:
38865 error_at (pragma_tok
->location
,
38866 "%<#pragma omp section%> may only be used in "
38867 "%<#pragma omp sections%> construct");
38872 if (context
== pragma_external
)
38874 error_at (pragma_tok
->location
,
38875 "%<#pragma GCC ivdep%> must be inside a function");
38878 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38879 unsigned short unroll
;
38880 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38881 if (tok
->type
== CPP_PRAGMA
38882 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38884 tok
= cp_lexer_consume_token (parser
->lexer
);
38885 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38886 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38890 if (tok
->type
!= CPP_KEYWORD
38891 || (tok
->keyword
!= RID_FOR
38892 && tok
->keyword
!= RID_WHILE
38893 && tok
->keyword
!= RID_DO
))
38895 cp_parser_error (parser
, "for, while or do statement expected");
38898 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38902 case PRAGMA_UNROLL
:
38904 if (context
== pragma_external
)
38906 error_at (pragma_tok
->location
,
38907 "%<#pragma GCC unroll%> must be inside a function");
38910 const unsigned short unroll
38911 = cp_parser_pragma_unroll (parser
, pragma_tok
);
38913 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38914 if (tok
->type
== CPP_PRAGMA
38915 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
38917 tok
= cp_lexer_consume_token (parser
->lexer
);
38918 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
38919 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38923 if (tok
->type
!= CPP_KEYWORD
38924 || (tok
->keyword
!= RID_FOR
38925 && tok
->keyword
!= RID_WHILE
38926 && tok
->keyword
!= RID_DO
))
38928 cp_parser_error (parser
, "for, while or do statement expected");
38931 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38936 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38937 c_invoke_pragma_handler (id
);
38941 cp_parser_error (parser
, "expected declaration specifiers");
38945 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38949 /* The interface the pragma parsers have to the lexer. */
38952 pragma_lex (tree
*value
, location_t
*loc
)
38954 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38955 enum cpp_ttype ret
= tok
->type
;
38957 *value
= tok
->u
.value
;
38959 *loc
= tok
->location
;
38961 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38963 else if (ret
== CPP_STRING
)
38964 *value
= cp_parser_string_literal (the_parser
, false, false);
38967 if (ret
== CPP_KEYWORD
)
38969 cp_lexer_consume_token (the_parser
->lexer
);
38976 /* External interface. */
38978 /* Parse one entire translation unit. */
38981 c_parse_file (void)
38983 static bool already_called
= false;
38985 if (already_called
)
38986 fatal_error (input_location
,
38987 "inter-module optimizations not implemented for C++");
38988 already_called
= true;
38990 the_parser
= cp_parser_new ();
38991 push_deferring_access_checks (flag_access_control
38992 ? dk_no_deferred
: dk_no_check
);
38993 cp_parser_translation_unit (the_parser
);
38997 /* Create an identifier for a generic parameter type (a synthesized
38998 template parameter implied by `auto' or a concept identifier). */
39000 static GTY(()) int generic_parm_count
;
39002 make_generic_type_name ()
39005 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39006 return get_identifier (buf
);
39009 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39010 (creating a new template parameter list if necessary). Returns the newly
39011 created template type parm. */
39014 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39016 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39018 /* Before committing to modifying any scope, if we're in an
39019 implicit template scope, and we're trying to synthesize a
39020 constrained parameter, try to find a previous parameter with
39021 the same name. This is the same-type rule for abbreviated
39022 function templates.
39024 NOTE: We can generate implicit parameters when tentatively
39025 parsing a nested name specifier, only to reject that parse
39026 later. However, matching the same template-id as part of a
39027 direct-declarator should generate an identical template
39028 parameter, so this rule will merge them. */
39029 if (parser
->implicit_template_scope
&& constr
)
39031 tree t
= parser
->implicit_template_parms
;
39034 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39036 tree d
= TREE_VALUE (t
);
39037 if (TREE_CODE (d
) == PARM_DECL
)
39038 /* Return the TEMPLATE_PARM_INDEX. */
39039 d
= DECL_INITIAL (d
);
39042 t
= TREE_CHAIN (t
);
39046 /* We are either continuing a function template that already contains implicit
39047 template parameters, creating a new fully-implicit function template, or
39048 extending an existing explicit function template with implicit template
39051 cp_binding_level
*const entry_scope
= current_binding_level
;
39053 bool become_template
= false;
39054 cp_binding_level
*parent_scope
= 0;
39056 if (parser
->implicit_template_scope
)
39058 gcc_assert (parser
->implicit_template_parms
);
39060 current_binding_level
= parser
->implicit_template_scope
;
39064 /* Roll back to the existing template parameter scope (in the case of
39065 extending an explicit function template) or introduce a new template
39066 parameter scope ahead of the function parameter scope (or class scope
39067 in the case of out-of-line member definitions). The function scope is
39068 added back after template parameter synthesis below. */
39070 cp_binding_level
*scope
= entry_scope
;
39072 while (scope
->kind
== sk_function_parms
)
39074 parent_scope
= scope
;
39075 scope
= scope
->level_chain
;
39077 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39079 /* If not defining a class, then any class scope is a scope level in
39080 an out-of-line member definition. In this case simply wind back
39081 beyond the first such scope to inject the template parameter list.
39082 Otherwise wind back to the class being defined. The latter can
39083 occur in class member friend declarations such as:
39089 friend void A::foo (auto);
39092 The template parameter list synthesized for the friend declaration
39093 must be injected in the scope of 'B'. This can also occur in
39094 erroneous cases such as:
39100 void B::foo (auto) {}
39103 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39104 but, nevertheless, the template parameter list synthesized for the
39105 declarator should be injected into the scope of 'A' as if the
39106 ill-formed template was specified explicitly. */
39108 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39110 parent_scope
= scope
;
39111 scope
= scope
->level_chain
;
39115 current_binding_level
= scope
;
39117 if (scope
->kind
!= sk_template_parms
39118 || !function_being_declared_is_template_p (parser
))
39120 /* Introduce a new template parameter list for implicit template
39123 become_template
= true;
39125 parser
->implicit_template_scope
39126 = begin_scope (sk_template_parms
, NULL
);
39128 ++processing_template_decl
;
39130 parser
->fully_implicit_function_template_p
= true;
39131 ++parser
->num_template_parameter_lists
;
39135 /* Synthesize implicit template parameters at the end of the explicit
39136 template parameter list. */
39138 gcc_assert (current_template_parms
);
39140 parser
->implicit_template_scope
= scope
;
39142 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39143 parser
->implicit_template_parms
39144 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39148 /* Synthesize a new template parameter and track the current template
39149 parameter chain with implicit_template_parms. */
39151 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39152 tree synth_id
= make_generic_type_name ();
39153 tree synth_tmpl_parm
;
39154 bool non_type
= false;
39156 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39158 = finish_template_type_parm (class_type_node
, synth_id
);
39159 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39161 = finish_constrained_template_template_parm (proto
, synth_id
);
39164 synth_tmpl_parm
= copy_decl (proto
);
39165 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39169 // Attach the constraint to the parm before processing.
39170 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39171 TREE_TYPE (node
) = constr
;
39173 = process_template_parm (parser
->implicit_template_parms
,
39176 /*non_type=*/non_type
,
39177 /*param_pack=*/false);
39179 // Chain the new parameter to the list of implicit parameters.
39180 if (parser
->implicit_template_parms
)
39181 parser
->implicit_template_parms
39182 = TREE_CHAIN (parser
->implicit_template_parms
);
39184 parser
->implicit_template_parms
= new_parm
;
39186 tree new_decl
= get_local_decls ();
39188 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39189 new_decl
= DECL_INITIAL (new_decl
);
39191 /* If creating a fully implicit function template, start the new implicit
39192 template parameter list with this synthesized type, otherwise grow the
39193 current template parameter list. */
39195 if (become_template
)
39197 parent_scope
->level_chain
= current_binding_level
;
39199 tree new_parms
= make_tree_vec (1);
39200 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39201 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39202 new_parms
, current_template_parms
);
39206 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39207 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39208 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39209 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39212 // If the new parameter was constrained, we need to add that to the
39213 // constraints in the template parameter list.
39214 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39216 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39217 reqs
= conjoin_constraints (reqs
, req
);
39218 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39221 current_binding_level
= entry_scope
;
39226 /* Finish the declaration of a fully implicit function template. Such a
39227 template has no explicit template parameter list so has not been through the
39228 normal template head and tail processing. synthesize_implicit_template_parm
39229 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39230 provided if the declaration is a class member such that its template
39231 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39232 form is returned. Otherwise NULL_TREE is returned. */
39235 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39237 gcc_assert (parser
->fully_implicit_function_template_p
);
39239 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39240 && DECL_VIRTUAL_P (member_decl_opt
))
39242 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39243 "implicit templates may not be %<virtual%>");
39244 DECL_VIRTUAL_P (member_decl_opt
) = false;
39247 if (member_decl_opt
)
39248 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39249 end_template_decl ();
39251 parser
->fully_implicit_function_template_p
= false;
39252 --parser
->num_template_parameter_lists
;
39254 return member_decl_opt
;
39257 /* Helper function for diagnostics that have complained about things
39258 being used with 'extern "C"' linkage.
39260 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39263 maybe_show_extern_c_location (void)
39265 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39266 inform (the_parser
->innermost_linkage_specification_location
,
39267 "%<extern \"C\"%> linkage started here");
39270 #include "gt-cp-parser.h"