2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
560 parser
->implicit_extern_c
);
561 cp_debug_print_flag (file
, "String expressions should be translated "
562 "to execution character set",
563 parser
->translate_strings_p
);
564 cp_debug_print_flag (file
, "Parsing function body outside of a "
565 "local class", parser
->in_function_body
);
566 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
567 parser
->colon_corrects_to_scope_p
);
568 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
569 parser
->colon_doesnt_start_class_def_p
);
570 if (parser
->type_definition_forbidden_message
)
571 fprintf (file
, "Error message for forbidden type definitions: %s\n",
572 parser
->type_definition_forbidden_message
);
573 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
574 fprintf (file
, "Number of class definitions in progress: %u\n",
575 parser
->num_classes_being_defined
);
576 fprintf (file
, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser
->num_template_parameter_lists
);
578 cp_debug_parser_tokens (file
, parser
, window_size
);
579 token
= parser
->lexer
->next_token
;
580 fprintf (file
, "Next token to parse:\n");
581 fprintf (file
, "\tToken: ");
582 cp_lexer_print_token (file
, token
);
583 eloc
= expand_location (token
->location
);
584 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
585 fprintf (file
, "\tLine: %d\n", eloc
.line
);
586 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
590 debug (cp_parser
&ref
)
592 cp_debug_parser (stderr
, &ref
);
596 debug (cp_parser
*ptr
)
601 fprintf (stderr
, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
616 /* Initially we are not debugging. */
617 lexer
->debugging_p
= false;
619 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
621 /* Create the buffer. */
622 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token
);
642 lexer
= cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer
->buffer
->quick_push (token
);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token
.type
!= CPP_EOF
)
650 cp_lexer_get_preprocessor_token (lexer
, &token
);
651 vec_safe_push (lexer
->buffer
, token
);
654 lexer
->last_token
= lexer
->buffer
->address ()
655 + lexer
->buffer
->length ()
657 lexer
->next_token
= lexer
->buffer
->length ()
658 ? lexer
->buffer
->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
681 lexer
->next_token
= first
== last
? &eof_token
: first
;
682 lexer
->last_token
= last
;
684 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
686 /* Initially we are not debugging. */
687 lexer
->debugging_p
= false;
689 gcc_assert (!lexer
->next_token
->purged_p
);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer
*lexer
)
698 vec_free (lexer
->buffer
);
699 lexer
->saved_tokens
.release ();
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
713 cp_lexer_debugging_p (cp_lexer
*lexer
)
715 if (!LEXER_DEBUGGING_ENABLED_P
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 /* Skip past purged tokens. */
759 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
763 return cp_lexer_token_at (lexer
, tp
);
766 /* nonzero if we are presently saving tokens. */
769 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
771 return lexer
->saved_tokens
.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
780 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
782 static int is_extern_c
= 0;
784 /* Get a new token from the preprocessor. */
786 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
787 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
788 token
->keyword
= RID_MAX
;
789 token
->purged_p
= false;
790 token
->error_reported
= false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c
+= pending_lang_change
;
796 pending_lang_change
= 0;
797 token
->implicit_extern_c
= is_extern_c
> 0;
799 /* Check to see if this token is a keyword. */
800 if (token
->type
== CPP_NAME
)
802 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
804 /* Mark this token as a keyword. */
805 token
->type
= CPP_KEYWORD
;
806 /* Record which keyword. */
807 token
->keyword
= C_RID_CODE (token
->u
.value
);
811 if (warn_cxx11_compat
812 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
813 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
815 /* Warn about the C++0x keyword (but still treat it as
817 warning (OPT_Wc__11_compat
,
818 "identifier %qE is a keyword in C++11",
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
826 token
->keyword
= RID_MAX
;
829 else if (token
->type
== CPP_AT_NAME
)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token
->type
= CPP_KEYWORD
;
833 switch (C_RID_CODE (token
->u
.value
))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
843 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
844 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
845 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
846 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
847 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
848 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
849 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
850 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
851 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if KEYWORD can start a decl-specifier. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE
:
979 if (keyword
>= RID_FIRST_INT_N
980 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
981 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
994 token
= cp_lexer_peek_token (lexer
);
995 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
998 /* Returns TRUE iff the token T begins a decltype type. */
1001 token_is_decltype (cp_token
*t
)
1003 return (t
->keyword
== RID_DECLTYPE
1004 || t
->type
== CPP_DECLTYPE
);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1010 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1012 cp_token
*t
= cp_lexer_peek_token (lexer
);
1013 return token_is_decltype (t
);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1020 saved_checks_value (struct tree_check
*check_value
)
1022 /* Perform any access checks that were deferred. */
1023 vec
<deferred_access_check
, va_gc
> *checks
;
1024 deferred_access_check
*chk
;
1025 checks
= check_value
->checks
;
1029 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1030 perform_or_defer_access_check (chk
->binfo
,
1032 chk
->diag_decl
, tf_warning_or_error
);
1034 /* Return the stored value. */
1035 return check_value
->value
;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1045 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer
))
1053 fprintf (cp_lexer_debug_stream
,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1057 token
= lexer
->next_token
;
1058 gcc_assert (!n
|| token
!= &eof_token
);
1062 if (token
== lexer
->last_token
)
1068 if (!token
->purged_p
)
1072 if (cp_lexer_debugging_p (lexer
))
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1085 cp_lexer_consume_token (cp_lexer
* lexer
)
1087 cp_token
*token
= lexer
->next_token
;
1089 gcc_assert (token
!= &eof_token
);
1090 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1094 lexer
->next_token
++;
1095 if (lexer
->next_token
== lexer
->last_token
)
1097 lexer
->next_token
= &eof_token
;
1102 while (lexer
->next_token
->purged_p
);
1104 cp_lexer_set_source_position_from_token (token
);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer
))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1110 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1111 putc ('\n', cp_lexer_debug_stream
);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1122 cp_lexer_purge_token (cp_lexer
*lexer
)
1124 cp_token
*tok
= lexer
->next_token
;
1126 gcc_assert (tok
!= &eof_token
);
1127 tok
->purged_p
= true;
1128 tok
->location
= UNKNOWN_LOCATION
;
1129 tok
->u
.value
= NULL_TREE
;
1130 tok
->keyword
= RID_MAX
;
1135 if (tok
== lexer
->last_token
)
1141 while (tok
->purged_p
);
1142 lexer
->next_token
= tok
;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1150 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1152 cp_token
*peek
= lexer
->next_token
;
1154 if (peek
== &eof_token
)
1155 peek
= lexer
->last_token
;
1157 gcc_assert (tok
< peek
);
1159 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1161 tok
->purged_p
= true;
1162 tok
->location
= UNKNOWN_LOCATION
;
1163 tok
->u
.value
= NULL_TREE
;
1164 tok
->keyword
= RID_MAX
;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1172 cp_lexer_save_tokens (cp_lexer
* lexer
)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer
))
1176 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1178 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1181 /* Commit to the portion of the token stream most recently saved. */
1184 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer
))
1188 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1190 lexer
->saved_tokens
.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1197 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer
))
1201 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1203 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1218 len
= lexer
->saved_tokens
.length ();
1219 cp_lexer_save_tokens (lexer
);
1223 cp_lexer_rollback_tokens (lexer
);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer
);
1230 gcc_assert (lexer
->saved_tokens
.length () == len
);
1234 /* Print a representation of the TOKEN on the STREAM. */
1237 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names
[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token
->type
)
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token
->u
.value
))
1264 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1271 case CPP_UTF8STRING
:
1272 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1276 print_generic_expr (stream
, token
->u
.value
);
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token
->type
< ARRAY_SIZE(token_names
))
1283 fputs (token_names
[token
->type
], stream
);
1285 fprintf (stream
, "[%d]", token
->type
);
1291 debug (cp_token
&ref
)
1293 cp_lexer_print_token (stderr
, &ref
);
1294 fprintf (stderr
, "\n");
1298 debug (cp_token
*ptr
)
1303 fprintf (stderr
, "<nil>\n");
1307 /* Start emitting debugging information. */
1310 cp_lexer_start_debugging (cp_lexer
* lexer
)
1312 if (!LEXER_DEBUGGING_ENABLED_P
)
1313 fatal_error (input_location
,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer
->debugging_p
= true;
1317 cp_lexer_debug_stream
= stderr
;
1320 /* Stop emitting debugging information. */
1323 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1325 if (!LEXER_DEBUGGING_ENABLED_P
)
1326 fatal_error (input_location
,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer
->debugging_p
= false;
1330 cp_lexer_debug_stream
= NULL
;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache
*
1336 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1338 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1339 cache
->first
= first
;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1348 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1350 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser
->omp_declare_simd
= NULL
;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1362 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1364 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1366 if (fndecl
== error_mark_node
)
1368 parser
->omp_declare_simd
= NULL
;
1371 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1373 cp_ensure_no_omp_declare_simd (parser
);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1383 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1385 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1387 error_at (parser
->oacc_routine
->loc
,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser
->oacc_routine
= NULL
;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1399 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1401 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator
*make_call_declarator
1412 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1413 static cp_declarator
*make_array_declarator
1414 (cp_declarator
*, tree
);
1415 static cp_declarator
*make_pointer_declarator
1416 (cp_cv_quals
, cp_declarator
*, tree
);
1417 static cp_declarator
*make_reference_declarator
1418 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1419 static cp_declarator
*make_ptrmem_declarator
1420 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1422 /* An erroneous declarator. */
1423 static cp_declarator
*cp_error_declarator
;
1425 /* The obstack on which declarators and related data structures are
1427 static struct obstack declarator_obstack
;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes
)
1434 return obstack_alloc (&declarator_obstack
, bytes
);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator
*
1441 make_declarator (cp_declarator_kind kind
)
1443 cp_declarator
*declarator
;
1445 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1446 declarator
->kind
= kind
;
1447 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1448 declarator
->attributes
= NULL_TREE
;
1449 declarator
->std_attributes
= NULL_TREE
;
1450 declarator
->declarator
= NULL
;
1451 declarator
->parameter_pack_p
= false;
1452 declarator
->id_loc
= UNKNOWN_LOCATION
;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1463 static cp_declarator
*
1464 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1465 special_function_kind sfk
)
1467 cp_declarator
*declarator
;
1469 /* It is valid to write:
1471 class C { void f(); };
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1479 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1480 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1482 gcc_assert (identifier_p (unqualified_name
)
1483 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1486 declarator
= make_declarator (cdk_id
);
1487 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1488 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1489 declarator
->u
.id
.sfk
= sfk
;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_pointer
);
1506 declarator
->declarator
= target
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1511 declarator
->id_loc
= target
->id_loc
;
1512 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1513 target
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1528 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1529 bool rvalue_ref
, tree attributes
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_reference
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1536 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1539 declarator
->id_loc
= target
->id_loc
;
1540 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1541 target
->parameter_pack_p
= false;
1544 declarator
->parameter_pack_p
= false;
1546 declarator
->std_attributes
= attributes
;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1557 cp_declarator
*pointee
,
1560 cp_declarator
*declarator
;
1562 declarator
= make_declarator (cdk_ptrmem
);
1563 declarator
->declarator
= pointee
;
1564 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1565 declarator
->u
.pointer
.class_type
= class_type
;
1569 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1570 pointee
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1575 declarator
->std_attributes
= attributes
;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1586 make_call_declarator (cp_declarator
*target
,
1588 cp_cv_quals cv_qualifiers
,
1589 cp_virt_specifiers virt_specifiers
,
1590 cp_ref_qualifier ref_qualifier
,
1592 tree exception_specification
,
1593 tree late_return_type
,
1594 tree requires_clause
)
1596 cp_declarator
*declarator
;
1598 declarator
= make_declarator (cdk_function
);
1599 declarator
->declarator
= target
;
1600 declarator
->u
.function
.parameters
= parms
;
1601 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1603 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1604 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1605 declarator
->u
.function
.exception_specification
= exception_specification
;
1606 declarator
->u
.function
.late_return_type
= late_return_type
;
1607 declarator
->u
.function
.requires_clause
= requires_clause
;
1610 declarator
->id_loc
= target
->id_loc
;
1611 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1612 target
->parameter_pack_p
= false;
1615 declarator
->parameter_pack_p
= false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1624 make_array_declarator (cp_declarator
*element
, tree bounds
)
1626 cp_declarator
*declarator
;
1628 declarator
= make_declarator (cdk_array
);
1629 declarator
->declarator
= element
;
1630 declarator
->u
.array
.bounds
= bounds
;
1633 declarator
->id_loc
= element
->id_loc
;
1634 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1635 element
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1648 if (declarator
&& declarator
->parameter_pack_p
)
1649 /* We already saw an ellipsis. */
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1657 while (declarator
&& !found
)
1659 switch ((int)declarator
->kind
)
1671 declarator
= declarator
->declarator
;
1679 cp_parameter_declarator
*no_parameters
;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator
*
1685 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1686 cp_declarator
*declarator
,
1687 tree default_argument
,
1689 bool template_parameter_pack_p
= false)
1691 cp_parameter_declarator
*parameter
;
1693 parameter
= ((cp_parameter_declarator
*)
1694 alloc_declarator (sizeof (cp_parameter_declarator
)));
1695 parameter
->next
= NULL
;
1696 if (decl_specifiers
)
1697 parameter
->decl_specifiers
= *decl_specifiers
;
1699 clear_decl_specs (¶meter
->decl_specifiers
);
1700 parameter
->declarator
= declarator
;
1701 parameter
->default_argument
= default_argument
;
1702 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1703 parameter
->loc
= loc
;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator
*declarator
)
1715 if (declarator
->kind
== cdk_function
1716 && declarator
->declarator
->kind
== cdk_id
)
1718 if (declarator
->kind
== cdk_id
1719 || declarator
->kind
== cdk_decomp
1720 || declarator
->kind
== cdk_error
)
1722 declarator
= declarator
->declarator
;
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1788 CP_PARSER_FLAGS_NONE
= 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser
*, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser
*, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser
*, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser
*, tree
, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2049 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2050 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser
*, tree
, tree
*, tree
*);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2060 static tree cp_parser_new_expression
2062 static vec
<tree
, va_gc
> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser
*, tree
*);
2066 static cp_declarator
*cp_parser_new_declarator_opt
2068 static cp_declarator
*cp_parser_direct_new_declarator
2070 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser
*, cp_expr
);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2084 static cp_expr cp_parser_expression
2085 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2090 static cp_expr cp_parser_lambda_expression
2092 static void cp_parser_lambda_introducer
2093 (cp_parser
*, tree
);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser
*, tree
);
2096 static void cp_parser_lambda_body
2097 (cp_parser
*, tree
);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_expression_statement
2106 (cp_parser
*, tree
);
2107 static tree cp_parser_compound_statement
2108 (cp_parser
*, tree
, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_selection_statement
2112 (cp_parser
*, bool *, vec
<tree
> *);
2113 static tree cp_parser_condition
2115 static tree cp_parser_iteration_statement
2116 (cp_parser
*, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser
*, tree
*decl
);
2119 static tree cp_parser_for
2120 (cp_parser
*, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser
*, tree
, tree
, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree
, tree
*, tree
*);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser
*, bool *, const token_indent_info
&);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2145 static void cp_parser_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_type_name
2166 static tree cp_parser_nonclass_name
2167 (cp_parser
* parser
);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser
*, bool, bool);
2170 static tree cp_parser_enum_specifier
2172 static void cp_parser_enumerator_list
2173 (cp_parser
*, tree
);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser
*, tree
);
2176 static tree cp_parser_namespace_name
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser
*, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser
*, bool);
2198 static tree cp_parser_decltype
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2207 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2224 static tree cp_parser_declarator_id
2225 (cp_parser
*, bool);
2226 static tree cp_parser_type_id
2228 static tree cp_parser_template_type_arg
2230 static tree
cp_parser_trailing_type_id (cp_parser
*);
2231 static tree cp_parser_type_id_1
2232 (cp_parser
*, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2235 static tree cp_parser_parameter_declaration_clause
2237 static tree cp_parser_parameter_declaration_list
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2267 static void abort_fully_implicit_template
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2276 static tree cp_parser_class_head
2277 (cp_parser
*, bool *);
2278 static enum tag_types cp_parser_class_key
2280 static void cp_parser_type_parameter_key
2281 (cp_parser
* parser
);
2282 static void cp_parser_member_specification_opt
2284 static void cp_parser_member_declaration
2286 static tree cp_parser_pure_specifier
2288 static tree cp_parser_constant_initializer
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2295 static tree cp_parser_base_specifier
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2302 static tree cp_parser_conversion_type_id
2304 static cp_declarator
*cp_parser_conversion_declarator_opt
2306 static void cp_parser_ctor_initializer_opt
2308 static void cp_parser_mem_initializer_list
2310 static tree cp_parser_mem_initializer
2312 static tree cp_parser_mem_initializer_id
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2319 static cp_expr cp_parser_operator
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser
*, bool);
2326 static tree cp_parser_template_parameter_list
2328 static tree cp_parser_template_parameter
2329 (cp_parser
*, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser
*, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser
*, bool, bool, enum tag_types
, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2336 static tree cp_parser_template_argument_list
2338 static tree cp_parser_template_argument
2340 static void cp_parser_explicit_instantiation
2342 static void cp_parser_explicit_specialization
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2349 static void cp_parser_function_try_block
2351 static void cp_parser_handler_seq
2353 static void cp_parser_handler
2355 static tree cp_parser_exception_declaration
2357 static tree cp_parser_throw_expression
2359 static tree cp_parser_exception_specification_opt
2361 static tree cp_parser_type_id_list
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2368 static tree cp_parser_asm_operand_list
2370 static tree cp_parser_asm_clobber_list
2372 static tree cp_parser_asm_label_list
2374 static bool cp_next_tokens_can_be_attribute_p
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 static bool cp_next_tokens_can_be_std_attribute_p
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser
*, size_t);
2386 static tree cp_parser_attributes_opt
2388 static tree cp_parser_gnu_attributes_opt
2390 static tree cp_parser_gnu_attribute_list
2392 static tree cp_parser_std_attribute
2393 (cp_parser
*, tree
);
2394 static tree cp_parser_std_attribute_spec
2396 static tree cp_parser_std_attribute_spec_seq
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser
*, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser
*, int *);
2402 static void cp_parser_label_declaration
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2409 static tree cp_parser_requires_clause_opt
2411 static tree cp_parser_requires_expression
2413 static tree cp_parser_requirement_parameter_list
2415 static tree cp_parser_requirement_body
2417 static tree cp_parser_requirement_list
2419 static tree cp_parser_requirement
2421 static tree cp_parser_simple_requirement
2423 static tree cp_parser_compound_requirement
2425 static tree cp_parser_type_requirement
2427 static tree cp_parser_nested_requirement
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser
*, cp_token
*);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser
*, enum rid
);
2436 static void cp_parser_function_transaction
2437 (cp_parser
*, enum rid
);
2438 static tree cp_parser_transaction_cancel
2441 enum pragma_context
{
2448 static bool cp_parser_pragma
2449 (cp_parser
*, enum pragma_context
, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2455 static tree cp_parser_objc_message_args
2457 static tree cp_parser_objc_message_expression
2459 static cp_expr cp_parser_objc_encode_expression
2461 static tree cp_parser_objc_defs_expression
2463 static tree cp_parser_objc_protocol_expression
2465 static tree cp_parser_objc_selector_expression
2467 static cp_expr cp_parser_objc_expression
2469 static bool cp_parser_objc_selector_p
2471 static tree cp_parser_objc_selector
2473 static tree cp_parser_objc_protocol_refs_opt
2475 static void cp_parser_objc_declaration
2476 (cp_parser
*, tree
);
2477 static tree cp_parser_objc_statement
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser
*, tree
*);
2481 static void cp_parser_objc_at_property_declaration
2483 static void cp_parser_objc_at_synthesize_declaration
2485 static void cp_parser_objc_at_dynamic_declaration
2487 static tree cp_parser_objc_struct_declaration
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser
*, tree
, location_t
);
2496 static tree cp_parser_maybe_treat_template_as_class
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser
*, cp_declarator
*, location_t
);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2502 static cp_expr cp_parser_simple_cast_expression
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser
*, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser
*, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser
*, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser
*, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec
<deferred_access_check
, va_gc
> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser
*, tree
);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2522 static tree cp_parser_save_nsdmi
2524 static tree cp_parser_enclosed_template_argument_list
2526 static void cp_parser_save_default_args
2527 (cp_parser
*, tree
);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser
*, tree
);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser
*, tree
, tree
, tree
);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser
*, tree
);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser
*, tree
);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser
*, enum rid
);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser
*, enum rid
);
2540 static bool cp_parser_declares_only_class_p
2542 static void cp_parser_set_storage_class
2543 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq
*decl_specs
,
2548 cp_decl_spec ds
, cp_token
*);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq
*);
2551 static void cp_parser_required_error
2552 (cp_parser
*, required_token
, bool, location_t
);
2553 static cp_token
*cp_parser_require
2554 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2555 static cp_token
*cp_parser_require_keyword
2556 (cp_parser
*, enum rid
, required_token
);
2557 static bool cp_parser_token_starts_function_definition_p
2559 static bool cp_parser_next_token_starts_class_definition_p
2561 static bool cp_parser_next_token_ends_template_argument_p
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser
*, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2569 static void cp_parser_check_class_key
2570 (enum tag_types
, tree type
);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type
, location_t location
);
2573 static bool cp_parser_optional_template_keyword
2575 static void cp_parser_pre_parsed_nested_name_specifier
2577 static bool cp_parser_cache_group
2578 (cp_parser
*, enum cpp_ttype
, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser
*parser
, bool nsdmi
);
2581 static void cp_parser_parse_tentatively
2583 static void cp_parser_commit_to_tentative_parse
2585 static void cp_parser_commit_to_topmost_tentative_parse
2587 static void cp_parser_abort_tentative_parse
2589 static bool cp_parser_parse_definitely
2591 static inline bool cp_parser_parsing_tentatively
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2595 static void cp_parser_error
2596 (cp_parser
*, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2599 static bool cp_parser_simulate_error
2601 static bool cp_parser_check_type_definition
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator
*, tree
, location_t type_location
);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser
*, non_integral_constant
);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser
*, tree
, location_t
);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser
*, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2619 static void cp_parser_skip_to_end_of_block_or_statement
2621 static bool cp_parser_skip_to_closing_brace
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser
*, cp_token
*);
2627 static bool cp_parser_error_occurred
2629 static bool cp_parser_allow_gnu_extensions_p
2631 static bool cp_parser_is_pure_string_literal
2633 static bool cp_parser_is_string_literal
2635 static bool cp_parser_is_keyword
2636 (cp_token
*, enum rid
);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser
*, tree
, location_t location
);
2639 static cp_declarator
* cp_parser_make_indirect_declarator
2640 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2641 static bool cp_parser_compound_literal_p
2643 static bool cp_parser_array_designator_p
2645 static bool cp_parser_init_statement_p
2647 static bool cp_parser_skip_to_closing_square_bracket
2650 /* Concept-related syntactic transformations */
2652 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2653 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand
;
2662 ++c_inhibit_evaluation_warnings
;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings
;
2668 --cp_unevaluated_operand
;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2677 cp_parser_parsing_tentatively (cp_parser
* parser
)
2679 return parser
->context
->next
!= NULL
;
2682 /* Returns nonzero if TOKEN is a string literal. */
2685 cp_parser_is_pure_string_literal (cp_token
* token
)
2687 return (token
->type
== CPP_STRING
||
2688 token
->type
== CPP_STRING16
||
2689 token
->type
== CPP_STRING32
||
2690 token
->type
== CPP_WSTRING
||
2691 token
->type
== CPP_UTF8STRING
);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2698 cp_parser_is_string_literal (cp_token
* token
)
2700 return (cp_parser_is_pure_string_literal (token
) ||
2701 token
->type
== CPP_STRING_USERDEF
||
2702 token
->type
== CPP_STRING16_USERDEF
||
2703 token
->type
== CPP_STRING32_USERDEF
||
2704 token
->type
== CPP_WSTRING_USERDEF
||
2705 token
->type
== CPP_UTF8STRING_USERDEF
);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2713 return token
->keyword
== keyword
;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token
*token
)
2722 if (token
->type
!= CPP_PRAGMA
)
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2740 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2741 location_t
*out_loc
)
2743 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2744 if (token2
->type
!= tok1_kind
)
2746 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2747 if (token3
->type
!= tok1_kind
)
2749 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2750 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2753 /* It must be at the start of the line. */
2754 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2755 if (LOCATION_COLUMN (start_loc
) != 1)
2758 /* We have a conflict marker. Construct a location of the form:
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc
= get_finish (token4
->location
);
2763 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2772 get_matching_symbol (required_token token_desc
)
2779 case RT_CLOSE_BRACE
:
2781 case RT_CLOSE_PAREN
:
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc
)
2795 return CPP_SEMICOLON
;
2797 return CPP_OPEN_PAREN
;
2798 case RT_CLOSE_BRACE
:
2799 return CPP_CLOSE_BRACE
;
2801 return CPP_OPEN_BRACE
;
2802 case RT_CLOSE_SQUARE
:
2803 return CPP_CLOSE_SQUARE
;
2804 case RT_OPEN_SQUARE
:
2805 return CPP_OPEN_SQUARE
;
2810 case RT_CLOSE_PAREN
:
2811 return CPP_CLOSE_PAREN
;
2814 /* Use CPP_EOF as a "no completions possible" code. */
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2839 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2840 required_token missing_token_desc
,
2841 location_t matching_location
)
2843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token
);
2848 if (token
->type
== CPP_PRAGMA
)
2850 error_at (token
->location
,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser
, token
);
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token
->type
== CPP_LSHIFT
2858 || token
->type
== CPP_RSHIFT
2859 || token
->type
== CPP_EQ_EQ
)
2862 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2864 error_at (loc
, "version control conflict marker in file");
2869 gcc_rich_location
richloc (input_location
);
2871 bool added_matching_location
= false;
2873 if (missing_token_desc
!= RT_NONE
)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser
->lexer
)->location
;
2881 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location
!= UNKNOWN_LOCATION
)
2887 added_matching_location
2888 = richloc
.add_location_if_nearby (matching_location
);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid
,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2896 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2897 token
->u
.value
, token
->flags
, &richloc
);
2899 if (missing_token_desc
!= RT_NONE
)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location
!= UNKNOWN_LOCATION
2904 && !added_matching_location
)
2905 inform (matching_location
, "to match this %qs",
2906 get_matching_symbol (missing_token_desc
));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2917 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2919 if (!cp_parser_simulate_error (parser
))
2920 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2929 cp_parser_name_lookup_error (cp_parser
* parser
,
2932 name_lookup_error desired
,
2933 location_t location
)
2935 /* If name lookup completely failed, tell the user that NAME was not
2937 if (decl
== error_mark_node
)
2939 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2940 error_at (location
, "%<%E::%E%> has not been declared",
2941 parser
->scope
, name
);
2942 else if (parser
->scope
== global_namespace
)
2943 error_at (location
, "%<::%E%> has not been declared", name
);
2944 else if (parser
->object_scope
2945 && !CLASS_TYPE_P (parser
->object_scope
))
2946 error_at (location
, "request for member %qE in non-class type %qT",
2947 name
, parser
->object_scope
);
2948 else if (parser
->object_scope
)
2949 error_at (location
, "%<%T::%E%> has not been declared",
2950 parser
->object_scope
, name
);
2952 error_at (location
, "%qE has not been declared", name
);
2954 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2959 error_at (location
, "%<%E::%E%> is not a type",
2960 parser
->scope
, name
);
2963 error_at (location
, "%<%E::%E%> is not a class or namespace",
2964 parser
->scope
, name
);
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser
->scope
, name
);
2976 else if (parser
->scope
== global_namespace
)
2981 error_at (location
, "%<::%E%> is not a type", name
);
2984 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2988 "%<::%E%> is not a class, namespace, or enumeration",
3000 error_at (location
, "%qE is not a type", name
);
3003 error_at (location
, "%qE is not a class or namespace", name
);
3007 "%qE is not a class, namespace, or enumeration", name
);
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3020 cp_parser_simulate_error (cp_parser
* parser
)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3024 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3035 cp_parser_check_type_definition (cp_parser
* parser
)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser
->type_definition_forbidden_message
)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser
->type_definition_forbidden_message
);
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3055 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3056 tree type
, location_t type_location
)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3062 && (declarator
->kind
== cdk_pointer
3063 || declarator
->kind
== cdk_reference
3064 || declarator
->kind
== cdk_ptrmem
))
3065 declarator
= declarator
->declarator
;
3067 && declarator
->kind
== cdk_function
)
3069 error_at (type_location
,
3070 "new types may not be defined in a return type");
3071 inform (type_location
,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3084 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3086 enum tag_types tag_type
,
3087 location_t location
)
3089 cp_token_position start
= 0;
3091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3093 if (TREE_CODE (type
) == TYPE_DECL
)
3094 type
= TREE_TYPE (type
);
3095 if (TYPE_P (type
) && !template_placeholder_p (type
))
3096 error_at (location
, "%qT is not a template", type
);
3097 else if (identifier_p (type
))
3099 if (tag_type
!= none_type
)
3100 error_at (location
, "%qE is not a class template", type
);
3102 error_at (location
, "%qE is not a template", type
);
3105 error_at (location
, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3108 start
= cp_lexer_token_position (parser
->lexer
, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser
->lexer
);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser
);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3116 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3126 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3127 non_integral_constant thing
)
3129 parser
->non_integral_constant_expression_p
= true;
3130 if (parser
->integral_constant_expression_p
)
3132 if (!parser
->allow_non_integral_constant_expression_p
)
3134 const char *msg
= NULL
;
3138 pedwarn (input_location
, OPT_Wpedantic
,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3171 case NIC_ADDR_LABEL
:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3175 case NIC_OVERLOADED
:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3179 case NIC_ASSIGNMENT
:
3180 error ("an assignment cannot appear in a constant-expression");
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3186 case NIC_CONSTRUCTOR
:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3190 case NIC_TRANSACTION
:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3198 msg
= "__FUNCTION__";
3200 case NIC_PRETTY_FUNC
:
3201 msg
= "__PRETTY_FUNCTION__";
3221 case NIC_PREINCREMENT
:
3224 case NIC_PREDECREMENT
:
3237 error ("%qs cannot appear in a constant-expression", msg
);
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3250 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3251 location_t location
)
3253 tree decl
, ambiguous_decls
;
3254 cp_parser_commit_to_tentative_parse (parser
);
3255 /* Try to lookup the identifier. */
3256 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls
, location
);
3261 if (ambiguous_decls
)
3262 /* If the lookup was ambiguous, an error will already have
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl
))
3270 "invalid use of template-name %qE without an argument list",
3272 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3273 inform (location
, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3277 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3278 error_at (location
, "invalid use of destructor %qD as a type", id
);
3279 else if (TREE_CODE (decl
) == TYPE_DECL
)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location
, "invalid combination of multiple type-specifiers");
3282 else if (!parser
->scope
)
3284 /* Issue an error message. */
3286 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3287 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3290 gcc_rich_location
richloc (location
);
3291 richloc
.add_fixit_replace (hint
.suggestion ());
3293 "%qE does not name a type; did you mean %qs?",
3294 id
, hint
.suggestion ());
3297 error_at (location
, "%qE does not name a type", id
);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3306 inform (location
, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3309 inform (location
, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect
< cxx11
3312 && TREE_CODE (id
) == IDENTIFIER_NODE
3313 && id_equal (id
, "thread_local"))
3314 inform (location
, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3317 inform (location
, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl
&& current_class_type
3319 && TYPE_BINFO (current_class_type
))
3323 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3327 tree base_type
= BINFO_TYPE (b
);
3328 if (CLASS_TYPE_P (base_type
)
3329 && dependent_type_p (base_type
))
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3336 for (field
= TYPE_FIELDS (base_type
);
3338 field
= DECL_CHAIN (field
))
3339 if (TREE_CODE (field
) == TYPE_DECL
3340 && DECL_NAME (field
) == id
)
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b
), id
);
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser
->scope
!= error_mark_node
)
3357 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3360 error_at (location_of (id
),
3361 "%qE in namespace %qE does not name a template type",
3363 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3364 error_at (location_of (id
),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id
, 0), parser
->scope
);
3368 error_at (location_of (id
),
3369 "%qE in namespace %qE does not name a type",
3372 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3373 else if (decl
== error_mark_node
)
3374 suggest_alternative_in_explicit_scope (location
, id
,
3377 else if (CLASS_TYPE_P (parser
->scope
)
3378 && constructor_name_p (id
, parser
->scope
))
3381 error_at (location
, "%<%T::%E%> names the constructor, not"
3382 " the type", parser
->scope
, id
);
3383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3384 error_at (location
, "and %qT has no template constructors",
3387 else if (TYPE_P (parser
->scope
)
3388 && dependent_scope_p (parser
->scope
))
3390 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser
->scope
),
3395 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3397 TYPE_CONTEXT (parser
->scope
),
3398 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3400 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser
->scope
, id
, parser
->scope
);
3404 else if (TYPE_P (parser
->scope
))
3406 if (!COMPLETE_TYPE_P (parser
->scope
))
3407 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3409 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3410 error_at (location_of (id
),
3411 "%qE in %q#T does not name a template type",
3413 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id
, 0), parser
->scope
);
3418 error_at (location_of (id
),
3419 "%qE in %q#T does not name a type",
3422 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3448 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3449 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3453 cp_parser_parse_tentatively (parser
);
3454 id
= cp_parser_id_expression (parser
,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL
,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3463 || TREE_CODE (id
) == TYPE_DECL
)
3465 cp_parser_abort_tentative_parse (parser
);
3468 if (!cp_parser_parse_definitely (parser
))
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser
->in_declarator_p
)
3478 cp_parser_skip_to_end_of_block_or_statement (parser
);
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3493 unsigned paren_depth
= 0;
3494 unsigned brace_depth
= 0;
3495 unsigned square_depth
= 0;
3496 unsigned condop_depth
= 0;
3498 if (recovering
&& or_ttype
== CPP_EOF
3499 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3504 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3506 /* Have we found what we're looking for before the closing paren? */
3507 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3508 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3511 switch (token
->type
)
3514 case CPP_PRAGMA_EOL
:
3515 /* If we've run out of tokens, then there is no closing `)'. */
3518 /* This is good for lambda expression capture-lists. */
3519 case CPP_OPEN_SQUARE
:
3522 case CPP_CLOSE_SQUARE
:
3523 if (!square_depth
--)
3528 /* This matches the processing in skip_to_end_of_statement. */
3533 case CPP_OPEN_BRACE
:
3536 case CPP_CLOSE_BRACE
:
3541 case CPP_OPEN_PAREN
:
3546 case CPP_CLOSE_PAREN
:
3547 if (!brace_depth
&& !paren_depth
--)
3550 cp_lexer_consume_token (parser
->lexer
);
3556 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3561 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3569 /* Consume the token. */
3570 cp_lexer_consume_token (parser
->lexer
);
3574 /* Consume tokens up to, and including, the next non-nested closing `)'.
3575 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3576 are doing error recovery. Returns -1 if OR_COMMA is true and we
3577 found an unnested token of that type. */
3580 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3585 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3586 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3587 ttype
, consume_paren
);
3590 /* Consume tokens until we reach the end of the current statement.
3591 Normally, that will be just before consuming a `;'. However, if a
3592 non-nested `}' comes first, then we stop before consuming that. */
3595 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3597 unsigned nesting_depth
= 0;
3599 /* Unwind generic function template scope if necessary. */
3600 if (parser
->fully_implicit_function_template_p
)
3601 abort_fully_implicit_template (parser
);
3605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3607 switch (token
->type
)
3610 case CPP_PRAGMA_EOL
:
3611 /* If we've run out of tokens, stop. */
3615 /* If the next token is a `;', we have reached the end of the
3621 case CPP_CLOSE_BRACE
:
3622 /* If this is a non-nested '}', stop before consuming it.
3623 That way, when confronted with something like:
3627 we stop before consuming the closing '}', even though we
3628 have not yet reached a `;'. */
3629 if (nesting_depth
== 0)
3632 /* If it is the closing '}' for a block that we have
3633 scanned, stop -- but only after consuming the token.
3639 we will stop after the body of the erroneously declared
3640 function, but before consuming the following `typedef'
3642 if (--nesting_depth
== 0)
3644 cp_lexer_consume_token (parser
->lexer
);
3649 case CPP_OPEN_BRACE
:
3657 /* Consume the token. */
3658 cp_lexer_consume_token (parser
->lexer
);
3662 /* This function is called at the end of a statement or declaration.
3663 If the next token is a semicolon, it is consumed; otherwise, error
3664 recovery is attempted. */
3667 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3669 /* Look for the trailing `;'. */
3670 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3672 /* If there is additional (erroneous) input, skip to the end of
3674 cp_parser_skip_to_end_of_statement (parser
);
3675 /* If the next token is now a `;', consume it. */
3676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3677 cp_lexer_consume_token (parser
->lexer
);
3681 /* Skip tokens until we have consumed an entire block, or until we
3682 have consumed a non-nested `;'. */
3685 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3687 int nesting_depth
= 0;
3689 /* Unwind generic function template scope if necessary. */
3690 if (parser
->fully_implicit_function_template_p
)
3691 abort_fully_implicit_template (parser
);
3693 while (nesting_depth
>= 0)
3695 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3697 switch (token
->type
)
3700 case CPP_PRAGMA_EOL
:
3701 /* If we've run out of tokens, stop. */
3705 /* Stop if this is an unnested ';'. */
3710 case CPP_CLOSE_BRACE
:
3711 /* Stop if this is an unnested '}', or closes the outermost
3714 if (nesting_depth
< 0)
3720 case CPP_OPEN_BRACE
:
3729 /* Consume the token. */
3730 cp_lexer_consume_token (parser
->lexer
);
3734 /* Skip tokens until a non-nested closing curly brace is the next
3735 token, or there are no more tokens. Return true in the first case,
3739 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3741 unsigned nesting_depth
= 0;
3745 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3747 switch (token
->type
)
3750 case CPP_PRAGMA_EOL
:
3751 /* If we've run out of tokens, stop. */
3754 case CPP_CLOSE_BRACE
:
3755 /* If the next token is a non-nested `}', then we have reached
3756 the end of the current block. */
3757 if (nesting_depth
-- == 0)
3761 case CPP_OPEN_BRACE
:
3762 /* If it the next token is a `{', then we are entering a new
3763 block. Consume the entire block. */
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser
->lexer
);
3776 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3777 parameter is the PRAGMA token, allowing us to purge the entire pragma
3781 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3785 parser
->lexer
->in_pragma
= false;
3788 token
= cp_lexer_consume_token (parser
->lexer
);
3789 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3791 /* Ensure that the pragma is not parsed again. */
3792 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3795 /* Require pragma end of line, resyncing with it as necessary. The
3796 arguments are as for cp_parser_skip_to_pragma_eol. */
3799 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3801 parser
->lexer
->in_pragma
= false;
3802 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3803 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3806 /* This is a simple wrapper around make_typename_type. When the id is
3807 an unresolved identifier node, we can provide a superior diagnostic
3808 using cp_parser_diagnose_invalid_type_name. */
3811 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3812 location_t id_location
)
3815 if (identifier_p (id
))
3817 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3818 /*complain=*/tf_none
);
3819 if (result
== error_mark_node
)
3820 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3823 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3826 /* This is a wrapper around the
3827 make_{pointer,ptrmem,reference}_declarator functions that decides
3828 which one to call based on the CODE and CLASS_TYPE arguments. The
3829 CODE argument should be one of the values returned by
3830 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3831 appertain to the pointer or reference. */
3833 static cp_declarator
*
3834 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3835 cp_cv_quals cv_qualifiers
,
3836 cp_declarator
*target
,
3839 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3840 return cp_error_declarator
;
3842 if (code
== INDIRECT_REF
)
3843 if (class_type
== NULL_TREE
)
3844 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3846 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3847 target
, attributes
);
3848 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3849 return make_reference_declarator (cv_qualifiers
, target
,
3851 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3852 return make_reference_declarator (cv_qualifiers
, target
,
3857 /* Create a new C++ parser. */
3860 cp_parser_new (void)
3866 /* cp_lexer_new_main is called before doing GC allocation because
3867 cp_lexer_new_main might load a PCH file. */
3868 lexer
= cp_lexer_new_main ();
3870 /* Initialize the binops_by_token so that we can get the tree
3871 directly from the token. */
3872 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3873 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3875 parser
= ggc_cleared_alloc
<cp_parser
> ();
3876 parser
->lexer
= lexer
;
3877 parser
->context
= cp_parser_context_new (NULL
);
3879 /* For now, we always accept GNU extensions. */
3880 parser
->allow_gnu_extensions_p
= 1;
3882 /* The `>' token is a greater-than operator, not the end of a
3884 parser
->greater_than_is_operator_p
= true;
3886 parser
->default_arg_ok_p
= true;
3888 /* We are not parsing a constant-expression. */
3889 parser
->integral_constant_expression_p
= false;
3890 parser
->allow_non_integral_constant_expression_p
= false;
3891 parser
->non_integral_constant_expression_p
= false;
3893 /* Local variable names are not forbidden. */
3894 parser
->local_variables_forbidden_p
= false;
3896 /* We are not processing an `extern "C"' declaration. */
3897 parser
->in_unbraced_linkage_specification_p
= false;
3899 /* We are not processing a declarator. */
3900 parser
->in_declarator_p
= false;
3902 /* We are not processing a template-argument-list. */
3903 parser
->in_template_argument_list_p
= false;
3905 /* We are not in an iteration statement. */
3906 parser
->in_statement
= 0;
3908 /* We are not in a switch statement. */
3909 parser
->in_switch_statement_p
= false;
3911 /* We are not parsing a type-id inside an expression. */
3912 parser
->in_type_id_in_expr_p
= false;
3914 /* Declarations aren't implicitly extern "C". */
3915 parser
->implicit_extern_c
= false;
3917 /* String literals should be translated to the execution character set. */
3918 parser
->translate_strings_p
= true;
3920 /* We are not parsing a function body. */
3921 parser
->in_function_body
= false;
3923 /* We can correct until told otherwise. */
3924 parser
->colon_corrects_to_scope_p
= true;
3926 /* The unparsed function queue is empty. */
3927 push_unparsed_function_queues (parser
);
3929 /* There are no classes being defined. */
3930 parser
->num_classes_being_defined
= 0;
3932 /* No template parameters apply. */
3933 parser
->num_template_parameter_lists
= 0;
3935 /* Special parsing data structures. */
3936 parser
->omp_declare_simd
= NULL
;
3937 parser
->oacc_routine
= NULL
;
3939 /* Not declaring an implicit function template. */
3940 parser
->auto_is_implicit_function_template_parm_p
= false;
3941 parser
->fully_implicit_function_template_p
= false;
3942 parser
->implicit_template_parms
= 0;
3943 parser
->implicit_template_scope
= 0;
3945 /* Allow constrained-type-specifiers. */
3946 parser
->prevent_constrained_type_specifiers
= 0;
3948 /* We haven't yet seen an 'extern "C"'. */
3949 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3954 /* Create a cp_lexer structure which will emit the tokens in CACHE
3955 and push it onto the parser's lexer stack. This is used for delayed
3956 parsing of in-class method bodies and default arguments, and should
3957 not be confused with tentative parsing. */
3959 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3961 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3962 lexer
->next
= parser
->lexer
;
3963 parser
->lexer
= lexer
;
3965 /* Move the current source position to that of the first token in the
3967 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3970 /* Pop the top lexer off the parser stack. This is never used for the
3971 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3973 cp_parser_pop_lexer (cp_parser
*parser
)
3975 cp_lexer
*lexer
= parser
->lexer
;
3976 parser
->lexer
= lexer
->next
;
3977 cp_lexer_destroy (lexer
);
3979 /* Put the current source position back where it was before this
3980 lexer was pushed. */
3981 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3984 /* Lexical conventions [gram.lex] */
3986 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3990 cp_parser_identifier (cp_parser
* parser
)
3994 /* Look for the identifier. */
3995 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3996 /* Return the value. */
3998 return cp_expr (token
->u
.value
, token
->location
);
4000 return error_mark_node
;
4003 /* Parse a sequence of adjacent string constants. Returns a
4004 TREE_STRING representing the combined, nul-terminated string
4005 constant. If TRANSLATE is true, translate the string to the
4006 execution character set. If WIDE_OK is true, a wide string is
4009 C++98 [lex.string] says that if a narrow string literal token is
4010 adjacent to a wide string literal token, the behavior is undefined.
4011 However, C99 6.4.5p4 says that this results in a wide string literal.
4012 We follow C99 here, for consistency with the C front end.
4014 This code is largely lifted from lex_string() in c-lex.c.
4016 FUTURE: ObjC++ will need to handle @-strings here. */
4018 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4019 bool lookup_udlit
= true)
4023 struct obstack str_ob
;
4024 cpp_string str
, istr
, *strs
;
4026 enum cpp_ttype type
, curr_type
;
4027 int have_suffix_p
= 0;
4029 tree suffix_id
= NULL_TREE
;
4030 bool curr_tok_is_userdef_p
= false;
4032 tok
= cp_lexer_peek_token (parser
->lexer
);
4033 if (!cp_parser_is_string_literal (tok
))
4035 cp_parser_error (parser
, "expected string-literal");
4036 return error_mark_node
;
4039 location_t loc
= tok
->location
;
4041 if (cpp_userdef_string_p (tok
->type
))
4043 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4044 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4045 curr_tok_is_userdef_p
= true;
4049 string_tree
= tok
->u
.value
;
4050 curr_type
= tok
->type
;
4054 /* Try to avoid the overhead of creating and destroying an obstack
4055 for the common case of just one string. */
4056 if (!cp_parser_is_string_literal
4057 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4059 cp_lexer_consume_token (parser
->lexer
);
4061 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4062 str
.len
= TREE_STRING_LENGTH (string_tree
);
4065 if (curr_tok_is_userdef_p
)
4067 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4069 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4072 curr_type
= tok
->type
;
4078 location_t last_tok_loc
= tok
->location
;
4079 gcc_obstack_init (&str_ob
);
4084 cp_lexer_consume_token (parser
->lexer
);
4086 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4087 str
.len
= TREE_STRING_LENGTH (string_tree
);
4089 if (curr_tok_is_userdef_p
)
4091 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4092 if (have_suffix_p
== 0)
4094 suffix_id
= curr_suffix_id
;
4097 else if (have_suffix_p
== 1
4098 && curr_suffix_id
!= suffix_id
)
4100 error ("inconsistent user-defined literal suffixes"
4101 " %qD and %qD in string literal",
4102 suffix_id
, curr_suffix_id
);
4105 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4108 curr_type
= tok
->type
;
4110 if (type
!= curr_type
)
4112 if (type
== CPP_STRING
)
4114 else if (curr_type
!= CPP_STRING
)
4116 rich_location
rich_loc (line_table
, tok
->location
);
4117 rich_loc
.add_range (last_tok_loc
, false);
4118 error_at (&rich_loc
,
4119 "unsupported non-standard concatenation "
4120 "of string literals");
4124 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4126 last_tok_loc
= tok
->location
;
4128 tok
= cp_lexer_peek_token (parser
->lexer
);
4129 if (cpp_userdef_string_p (tok
->type
))
4131 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4132 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4133 curr_tok_is_userdef_p
= true;
4137 string_tree
= tok
->u
.value
;
4138 curr_type
= tok
->type
;
4139 curr_tok_is_userdef_p
= false;
4142 while (cp_parser_is_string_literal (tok
));
4144 /* A string literal built by concatenation has its caret=start at
4145 the start of the initial string, and its finish at the finish of
4146 the final string literal. */
4147 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4149 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4152 if (type
!= CPP_STRING
&& !wide_ok
)
4154 cp_parser_error (parser
, "a wide string is invalid in this context");
4158 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4159 (parse_in
, strs
, count
, &istr
, type
))
4161 value
= build_string (istr
.len
, (const char *)istr
.text
);
4162 free (CONST_CAST (unsigned char *, istr
.text
));
4168 case CPP_UTF8STRING
:
4169 TREE_TYPE (value
) = char_array_type_node
;
4172 TREE_TYPE (value
) = char16_array_type_node
;
4175 TREE_TYPE (value
) = char32_array_type_node
;
4178 TREE_TYPE (value
) = wchar_array_type_node
;
4182 value
= fix_string_type (value
);
4186 tree literal
= build_userdef_literal (suffix_id
, value
,
4187 OT_NONE
, NULL_TREE
);
4189 value
= cp_parser_userdef_string_literal (literal
);
4195 /* cpp_interpret_string has issued an error. */
4196 value
= error_mark_node
;
4199 obstack_free (&str_ob
, 0);
4201 return cp_expr (value
, loc
);
4204 /* Look up a literal operator with the name and the exact arguments. */
4207 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4210 decl
= lookup_name (name
);
4211 if (!decl
|| !is_overloaded_fn (decl
))
4212 return error_mark_node
;
4214 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4219 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4220 if (parmtypes
!= NULL_TREE
)
4222 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4223 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4225 tree tparm
= TREE_VALUE (parmtypes
);
4226 tree targ
= TREE_TYPE ((*args
)[ix
]);
4227 bool ptr
= TYPE_PTR_P (tparm
);
4228 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4229 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4231 || !same_type_p (TREE_TYPE (tparm
),
4236 && ix
== vec_safe_length (args
)
4237 /* May be this should be sufficient_parms_p instead,
4238 depending on how exactly should user-defined literals
4239 work in presence of default arguments on the literal
4240 operator parameters. */
4241 && parmtypes
== void_list_node
)
4246 return error_mark_node
;
4249 /* Parse a user-defined char constant. Returns a call to a user-defined
4250 literal operator taking the character as an argument. */
4253 cp_parser_userdef_char_literal (cp_parser
*parser
)
4255 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4256 tree literal
= token
->u
.value
;
4257 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4258 tree value
= USERDEF_LITERAL_VALUE (literal
);
4259 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4262 /* Build up a call to the user-defined operator */
4263 /* Lookup the name we got back from the id-expression. */
4264 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4265 vec_safe_push (args
, value
);
4266 decl
= lookup_literal_operator (name
, args
);
4267 if (!decl
|| decl
== error_mark_node
)
4269 error ("unable to find character literal operator %qD with %qT argument",
4270 name
, TREE_TYPE (value
));
4271 release_tree_vector (args
);
4272 return error_mark_node
;
4274 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4275 release_tree_vector (args
);
4279 /* A subroutine of cp_parser_userdef_numeric_literal to
4280 create a char... template parameter pack from a string node. */
4283 make_char_string_pack (tree value
)
4286 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4287 const char *str
= TREE_STRING_POINTER (value
);
4288 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4289 tree argvec
= make_tree_vec (1);
4291 /* Fill in CHARVEC with all of the parameters. */
4292 charvec
= make_tree_vec (len
);
4293 for (i
= 0; i
< len
; ++i
)
4294 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4296 /* Build the argument packs. */
4297 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4299 TREE_VEC_ELT (argvec
, 0) = argpack
;
4304 /* A subroutine of cp_parser_userdef_numeric_literal to
4305 create a char... template parameter pack from a string node. */
4308 make_string_pack (tree value
)
4311 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4312 const unsigned char *str
4313 = (const unsigned char *) TREE_STRING_POINTER (value
);
4314 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4315 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4316 tree argvec
= make_tree_vec (2);
4318 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4319 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4321 /* First template parm is character type. */
4322 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4324 /* Fill in CHARVEC with all of the parameters. */
4325 charvec
= make_tree_vec (len
);
4326 for (int i
= 0; i
< len
; ++i
)
4327 TREE_VEC_ELT (charvec
, i
)
4328 = double_int_to_tree (str_char_type_node
,
4329 double_int::from_buffer (str
+ i
* sz
, sz
));
4331 /* Build the argument packs. */
4332 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4334 TREE_VEC_ELT (argvec
, 1) = argpack
;
4339 /* Parse a user-defined numeric constant. returns a call to a user-defined
4340 literal operator. */
4343 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4345 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4346 tree literal
= token
->u
.value
;
4347 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4348 tree value
= USERDEF_LITERAL_VALUE (literal
);
4349 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4350 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4351 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4353 vec
<tree
, va_gc
> *args
;
4355 /* Look for a literal operator taking the exact type of numeric argument
4356 as the literal value. */
4357 args
= make_tree_vector ();
4358 vec_safe_push (args
, value
);
4359 decl
= lookup_literal_operator (name
, args
);
4360 if (decl
&& decl
!= error_mark_node
)
4362 result
= finish_call_expr (decl
, &args
, false, true,
4363 tf_warning_or_error
);
4365 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4367 warning_at (token
->location
, OPT_Woverflow
,
4368 "integer literal exceeds range of %qT type",
4369 long_long_unsigned_type_node
);
4374 warning_at (token
->location
, OPT_Woverflow
,
4375 "floating literal exceeds range of %qT type",
4376 long_double_type_node
);
4377 else if (overflow
< 0)
4378 warning_at (token
->location
, OPT_Woverflow
,
4379 "floating literal truncated to zero");
4382 release_tree_vector (args
);
4385 release_tree_vector (args
);
4387 /* If the numeric argument didn't work, look for a raw literal
4388 operator taking a const char* argument consisting of the number
4389 in string format. */
4390 args
= make_tree_vector ();
4391 vec_safe_push (args
, num_string
);
4392 decl
= lookup_literal_operator (name
, args
);
4393 if (decl
&& decl
!= error_mark_node
)
4395 result
= finish_call_expr (decl
, &args
, false, true,
4396 tf_warning_or_error
);
4397 release_tree_vector (args
);
4400 release_tree_vector (args
);
4402 /* If the raw literal didn't work, look for a non-type template
4403 function with parameter pack char.... Call the function with
4404 template parameter characters representing the number. */
4405 args
= make_tree_vector ();
4406 decl
= lookup_literal_operator (name
, args
);
4407 if (decl
&& decl
!= error_mark_node
)
4409 tree tmpl_args
= make_char_string_pack (num_string
);
4410 decl
= lookup_template_function (decl
, tmpl_args
);
4411 result
= finish_call_expr (decl
, &args
, false, true,
4412 tf_warning_or_error
);
4413 release_tree_vector (args
);
4417 release_tree_vector (args
);
4419 /* In C++14 the standard library defines complex number suffixes that
4420 conflict with GNU extensions. Prefer them if <complex> is #included. */
4421 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4422 bool i14
= (cxx_dialect
> cxx11
4423 && (id_equal (suffix_id
, "i")
4424 || id_equal (suffix_id
, "if")
4425 || id_equal (suffix_id
, "il")));
4426 diagnostic_t kind
= DK_ERROR
;
4431 tree cxlit
= lookup_qualified_name (std_node
,
4432 get_identifier ("complex_literals"),
4434 if (cxlit
== error_mark_node
)
4436 /* No <complex>, so pedwarn and use GNU semantics. */
4438 opt
= OPT_Wpedantic
;
4443 = emit_diagnostic (kind
, input_location
, opt
,
4444 "unable to find numeric literal operator %qD", name
);
4447 /* Don't inform either. */;
4450 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4451 "(from <complex>) to enable the C++14 user-defined literal "
4454 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4455 "GNU built-in suffix");
4458 inform (token
->location
, "use -fext-numeric-literals "
4459 "to enable more built-in suffixes");
4461 if (kind
== DK_ERROR
)
4462 value
= error_mark_node
;
4465 /* Use the built-in semantics. */
4467 if (id_equal (suffix_id
, "i"))
4469 if (TREE_CODE (value
) == INTEGER_CST
)
4470 type
= integer_type_node
;
4472 type
= double_type_node
;
4474 else if (id_equal (suffix_id
, "if"))
4475 type
= float_type_node
;
4476 else /* if (id_equal (suffix_id, "il")) */
4477 type
= long_double_type_node
;
4479 value
= build_complex (build_complex_type (type
),
4480 fold_convert (type
, integer_zero_node
),
4481 fold_convert (type
, value
));
4484 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4485 /* Avoid repeated diagnostics. */
4486 token
->u
.value
= value
;
4490 /* Parse a user-defined string constant. Returns a call to a user-defined
4491 literal operator taking a character pointer and the length of the string
4495 cp_parser_userdef_string_literal (tree literal
)
4497 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4498 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4499 tree value
= USERDEF_LITERAL_VALUE (literal
);
4500 int len
= TREE_STRING_LENGTH (value
)
4501 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4503 vec
<tree
, va_gc
> *args
;
4505 /* Build up a call to the user-defined operator. */
4506 /* Lookup the name we got back from the id-expression. */
4507 args
= make_tree_vector ();
4508 vec_safe_push (args
, value
);
4509 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4510 decl
= lookup_literal_operator (name
, args
);
4512 if (decl
&& decl
!= error_mark_node
)
4514 result
= finish_call_expr (decl
, &args
, false, true,
4515 tf_warning_or_error
);
4516 release_tree_vector (args
);
4519 release_tree_vector (args
);
4521 /* Look for a template function with typename parameter CharT
4522 and parameter pack CharT... Call the function with
4523 template parameter characters representing the string. */
4524 args
= make_tree_vector ();
4525 decl
= lookup_literal_operator (name
, args
);
4526 if (decl
&& decl
!= error_mark_node
)
4528 tree tmpl_args
= make_string_pack (value
);
4529 decl
= lookup_template_function (decl
, tmpl_args
);
4530 result
= finish_call_expr (decl
, &args
, false, true,
4531 tf_warning_or_error
);
4532 release_tree_vector (args
);
4535 release_tree_vector (args
);
4537 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4538 name
, TREE_TYPE (value
), size_type_node
);
4539 return error_mark_node
;
4543 /* Basic concepts [gram.basic] */
4545 /* Parse a translation-unit.
4548 declaration-seq [opt]
4550 Returns TRUE if all went well. */
4553 cp_parser_translation_unit (cp_parser
* parser
)
4555 /* The address of the first non-permanent object on the declarator
4557 static void *declarator_obstack_base
;
4561 /* Create the declarator obstack, if necessary. */
4562 if (!cp_error_declarator
)
4564 gcc_obstack_init (&declarator_obstack
);
4565 /* Create the error declarator. */
4566 cp_error_declarator
= make_declarator (cdk_error
);
4567 /* Create the empty parameter list. */
4568 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4570 /* Remember where the base of the declarator obstack lies. */
4571 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4574 cp_parser_declaration_seq_opt (parser
);
4576 /* If there are no tokens left then all went well. */
4577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4579 /* Get rid of the token array; we don't need it any more. */
4580 cp_lexer_destroy (parser
->lexer
);
4581 parser
->lexer
= NULL
;
4583 /* This file might have been a context that's implicitly extern
4584 "C". If so, pop the lang context. (Only relevant for PCH.) */
4585 if (parser
->implicit_extern_c
)
4587 pop_lang_context ();
4588 parser
->implicit_extern_c
= false;
4592 finish_translation_unit ();
4598 cp_parser_error (parser
, "expected declaration");
4602 /* Make sure the declarator obstack was fully cleaned up. */
4603 gcc_assert (obstack_next_free (&declarator_obstack
)
4604 == declarator_obstack_base
);
4606 /* All went well. */
4610 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4611 decltype context. */
4613 static inline tsubst_flags_t
4614 complain_flags (bool decltype_p
)
4616 tsubst_flags_t complain
= tf_warning_or_error
;
4618 complain
|= tf_decltype
;
4622 /* We're about to parse a collection of statements. If we're currently
4623 parsing tentatively, set up a firewall so that any nested
4624 cp_parser_commit_to_tentative_parse won't affect the current context. */
4626 static cp_token_position
4627 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4629 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4632 cp_parser_parse_tentatively (parser
);
4633 cp_parser_commit_to_topmost_tentative_parse (parser
);
4634 return cp_lexer_token_position (parser
->lexer
, false);
4637 /* We've finished parsing the collection of statements. Wrap up the
4638 firewall and replace the relevant tokens with the parsed form. */
4641 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4647 /* Finish the firewall level. */
4648 cp_parser_parse_definitely (parser
);
4649 /* And remember the result of the parse for when we try again. */
4650 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4651 token
->type
= CPP_PREPARSED_EXPR
;
4652 token
->u
.value
= expr
;
4653 token
->keyword
= RID_MAX
;
4654 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4657 /* Like the above functions, but let the user modify the tokens. Used by
4658 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4659 later parses, so it makes sense to localize the effects of
4660 cp_parser_commit_to_tentative_parse. */
4662 struct tentative_firewall
4667 tentative_firewall (cp_parser
*p
): parser(p
)
4669 /* If we're currently parsing tentatively, start a committed level as a
4670 firewall and then an inner tentative parse. */
4671 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4673 cp_parser_parse_tentatively (parser
);
4674 cp_parser_commit_to_topmost_tentative_parse (parser
);
4675 cp_parser_parse_tentatively (parser
);
4679 ~tentative_firewall()
4683 /* Finish the inner tentative parse and the firewall, propagating any
4684 uncommitted error state to the outer tentative parse. */
4685 bool err
= cp_parser_error_occurred (parser
);
4686 cp_parser_parse_definitely (parser
);
4687 cp_parser_parse_definitely (parser
);
4689 cp_parser_simulate_error (parser
);
4694 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4695 This class is for tracking such a matching pair of symbols.
4696 In particular, it tracks the location of the first token,
4697 so that if the second token is missing, we can highlight the
4698 location of the first token when notifying the user about the
4701 template <typename traits_t
>
4705 /* token_pair's ctor. */
4706 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4708 /* If the next token is the opening symbol for this pair, consume it and
4710 Otherwise, issue an error and return false.
4711 In either case, record the location of the opening token. */
4713 bool require_open (cp_parser
*parser
)
4715 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4716 return cp_parser_require (parser
, traits_t::open_token_type
,
4717 traits_t::required_token_open
);
4720 /* Consume the next token from PARSER, recording its location as
4721 that of the opening token within the pair. */
4723 cp_token
* consume_open (cp_parser
*parser
)
4725 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4726 gcc_assert (tok
->type
== traits_t::open_token_type
);
4727 m_open_loc
= tok
->location
;
4731 /* If the next token is the closing symbol for this pair, consume it
4733 Otherwise, issue an error, highlighting the location of the
4734 corresponding opening token, and return NULL. */
4736 cp_token
*require_close (cp_parser
*parser
) const
4738 return cp_parser_require (parser
, traits_t::close_token_type
,
4739 traits_t::required_token_close
,
4744 location_t m_open_loc
;
4747 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4749 struct matching_paren_traits
4751 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4752 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4753 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4754 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4757 /* "matching_parens" is a token_pair<T> class for tracking matching
4758 pairs of parentheses. */
4760 typedef token_pair
<matching_paren_traits
> matching_parens
;
4762 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4764 struct matching_brace_traits
4766 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4767 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4768 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4769 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4772 /* "matching_braces" is a token_pair<T> class for tracking matching
4775 typedef token_pair
<matching_brace_traits
> matching_braces
;
4778 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4779 enclosing parentheses. */
4782 cp_parser_statement_expr (cp_parser
*parser
)
4784 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4786 /* Consume the '('. */
4787 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4788 matching_parens parens
;
4789 parens
.consume_open (parser
);
4790 /* Start the statement-expression. */
4791 tree expr
= begin_stmt_expr ();
4792 /* Parse the compound-statement. */
4793 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4795 expr
= finish_stmt_expr (expr
, false);
4796 /* Consume the ')'. */
4797 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4798 if (!parens
.require_close (parser
))
4799 cp_parser_skip_to_end_of_statement (parser
);
4801 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4802 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4803 return cp_expr (expr
, combined_loc
);
4806 /* Expressions [gram.expr] */
4808 /* Parse a fold-operator.
4811 - * / % ^ & | = < > << >>
4812 = -= *= /= %= ^= &= |= <<= >>=
4813 == != <= >= && || , .* ->*
4815 This returns the tree code corresponding to the matched operator
4816 as an int. When the current token matches a compound assignment
4817 opertor, the resulting tree code is the negative value of the
4818 non-assignment operator. */
4821 cp_parser_fold_operator (cp_token
*token
)
4823 switch (token
->type
)
4825 case CPP_PLUS
: return PLUS_EXPR
;
4826 case CPP_MINUS
: return MINUS_EXPR
;
4827 case CPP_MULT
: return MULT_EXPR
;
4828 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4829 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4830 case CPP_XOR
: return BIT_XOR_EXPR
;
4831 case CPP_AND
: return BIT_AND_EXPR
;
4832 case CPP_OR
: return BIT_IOR_EXPR
;
4833 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4834 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4836 case CPP_EQ
: return -NOP_EXPR
;
4837 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4838 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4839 case CPP_MULT_EQ
: return -MULT_EXPR
;
4840 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4841 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4842 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4843 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4844 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4845 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4846 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4848 case CPP_EQ_EQ
: return EQ_EXPR
;
4849 case CPP_NOT_EQ
: return NE_EXPR
;
4850 case CPP_LESS
: return LT_EXPR
;
4851 case CPP_GREATER
: return GT_EXPR
;
4852 case CPP_LESS_EQ
: return LE_EXPR
;
4853 case CPP_GREATER_EQ
: return GE_EXPR
;
4855 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4856 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4858 case CPP_COMMA
: return COMPOUND_EXPR
;
4860 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4861 case CPP_DEREF_STAR
: return MEMBER_REF
;
4863 default: return ERROR_MARK
;
4867 /* Returns true if CODE indicates a binary expression, which is not allowed in
4868 the LHS of a fold-expression. More codes will need to be added to use this
4869 function in other contexts. */
4872 is_binary_op (tree_code code
)
4877 case POINTER_PLUS_EXPR
:
4880 case TRUNC_DIV_EXPR
:
4881 case TRUNC_MOD_EXPR
:
4897 case TRUTH_ANDIF_EXPR
:
4898 case TRUTH_ORIF_EXPR
:
4911 /* If the next token is a suitable fold operator, consume it and return as
4912 the function above. */
4915 cp_parser_fold_operator (cp_parser
*parser
)
4917 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4918 int code
= cp_parser_fold_operator (token
);
4919 if (code
!= ERROR_MARK
)
4920 cp_lexer_consume_token (parser
->lexer
);
4924 /* Parse a fold-expression.
4927 ( ... folding-operator cast-expression)
4928 ( cast-expression folding-operator ... )
4929 ( cast-expression folding operator ... folding-operator cast-expression)
4931 Note that the '(' and ')' are matched in primary expression. */
4934 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4941 cp_lexer_consume_token (parser
->lexer
);
4942 int op
= cp_parser_fold_operator (parser
);
4943 if (op
== ERROR_MARK
)
4945 cp_parser_error (parser
, "expected binary operator");
4946 return error_mark_node
;
4949 tree expr
= cp_parser_cast_expression (parser
, false, false,
4951 if (expr
== error_mark_node
)
4952 return error_mark_node
;
4953 return finish_left_unary_fold_expr (expr
, op
);
4956 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4957 int op
= cp_parser_fold_operator (parser
);
4958 if (op
== ERROR_MARK
)
4960 cp_parser_error (parser
, "expected binary operator");
4961 return error_mark_node
;
4964 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4966 cp_parser_error (parser
, "expected ...");
4967 return error_mark_node
;
4969 cp_lexer_consume_token (parser
->lexer
);
4971 /* The operands of a fold-expression are cast-expressions, so binary or
4972 conditional expressions are not allowed. We check this here to avoid
4973 tentative parsing. */
4974 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4975 /* OK, the expression was parenthesized. */;
4976 else if (is_binary_op (TREE_CODE (expr1
)))
4977 error_at (location_of (expr1
),
4978 "binary expression in operand of fold-expression");
4979 else if (TREE_CODE (expr1
) == COND_EXPR
4980 || (REFERENCE_REF_P (expr1
)
4981 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
4982 error_at (location_of (expr1
),
4983 "conditional expression in operand of fold-expression");
4986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4987 return finish_right_unary_fold_expr (expr1
, op
);
4989 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4991 cp_parser_error (parser
, "mismatched operator in fold-expression");
4992 return error_mark_node
;
4994 cp_lexer_consume_token (parser
->lexer
);
4996 // Binary left or right fold.
4997 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4998 if (expr2
== error_mark_node
)
4999 return error_mark_node
;
5000 return finish_binary_fold_expr (expr1
, expr2
, op
);
5003 /* Parse a primary-expression.
5010 lambda-expression (C++11)
5015 ( compound-statement )
5016 __builtin_va_arg ( assignment-expression , type-id )
5017 __builtin_offsetof ( type-id , offsetof-expression )
5020 __has_nothrow_assign ( type-id )
5021 __has_nothrow_constructor ( type-id )
5022 __has_nothrow_copy ( type-id )
5023 __has_trivial_assign ( type-id )
5024 __has_trivial_constructor ( type-id )
5025 __has_trivial_copy ( type-id )
5026 __has_trivial_destructor ( type-id )
5027 __has_virtual_destructor ( type-id )
5028 __is_abstract ( type-id )
5029 __is_base_of ( type-id , type-id )
5030 __is_class ( type-id )
5031 __is_empty ( type-id )
5032 __is_enum ( type-id )
5033 __is_final ( type-id )
5034 __is_literal_type ( type-id )
5035 __is_pod ( type-id )
5036 __is_polymorphic ( type-id )
5037 __is_std_layout ( type-id )
5038 __is_trivial ( type-id )
5039 __is_union ( type-id )
5041 Objective-C++ Extension:
5049 ADDRESS_P is true iff this expression was immediately preceded by
5050 "&" and therefore might denote a pointer-to-member. CAST_P is true
5051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5052 true iff this expression is a template argument.
5054 Returns a representation of the expression. Upon return, *IDK
5055 indicates what kind of id-expression (if any) was present. */
5058 cp_parser_primary_expression (cp_parser
*parser
,
5061 bool template_arg_p
,
5065 cp_token
*token
= NULL
;
5067 /* Assume the primary expression is not an id-expression. */
5068 *idk
= CP_ID_KIND_NONE
;
5070 /* Peek at the next token. */
5071 token
= cp_lexer_peek_token (parser
->lexer
);
5072 switch ((int) token
->type
)
5081 user-defined-literal */
5088 case CPP_PREPARSED_EXPR
:
5089 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5090 return cp_parser_userdef_numeric_literal (parser
);
5091 token
= cp_lexer_consume_token (parser
->lexer
);
5092 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5094 error_at (token
->location
,
5095 "fixed-point types not supported in C++");
5096 return error_mark_node
;
5098 /* Floating-point literals are only allowed in an integral
5099 constant expression if they are cast to an integral or
5100 enumeration type. */
5101 if (TREE_CODE (token
->u
.value
) == REAL_CST
5102 && parser
->integral_constant_expression_p
5105 /* CAST_P will be set even in invalid code like "int(2.7 +
5106 ...)". Therefore, we have to check that the next token
5107 is sure to end the cast. */
5110 cp_token
*next_token
;
5112 next_token
= cp_lexer_peek_token (parser
->lexer
);
5113 if (/* The comma at the end of an
5114 enumerator-definition. */
5115 next_token
->type
!= CPP_COMMA
5116 /* The curly brace at the end of an enum-specifier. */
5117 && next_token
->type
!= CPP_CLOSE_BRACE
5118 /* The end of a statement. */
5119 && next_token
->type
!= CPP_SEMICOLON
5120 /* The end of the cast-expression. */
5121 && next_token
->type
!= CPP_CLOSE_PAREN
5122 /* The end of an array bound. */
5123 && next_token
->type
!= CPP_CLOSE_SQUARE
5124 /* The closing ">" in a template-argument-list. */
5125 && (next_token
->type
!= CPP_GREATER
5126 || parser
->greater_than_is_operator_p
)
5127 /* C++0x only: A ">>" treated like two ">" tokens,
5128 in a template-argument-list. */
5129 && (next_token
->type
!= CPP_RSHIFT
5130 || (cxx_dialect
== cxx98
)
5131 || parser
->greater_than_is_operator_p
))
5135 /* If we are within a cast, then the constraint that the
5136 cast is to an integral or enumeration type will be
5137 checked at that point. If we are not within a cast, then
5138 this code is invalid. */
5140 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5142 return cp_expr (token
->u
.value
, token
->location
);
5144 case CPP_CHAR_USERDEF
:
5145 case CPP_CHAR16_USERDEF
:
5146 case CPP_CHAR32_USERDEF
:
5147 case CPP_WCHAR_USERDEF
:
5148 case CPP_UTF8CHAR_USERDEF
:
5149 return cp_parser_userdef_char_literal (parser
);
5155 case CPP_UTF8STRING
:
5156 case CPP_STRING_USERDEF
:
5157 case CPP_STRING16_USERDEF
:
5158 case CPP_STRING32_USERDEF
:
5159 case CPP_WSTRING_USERDEF
:
5160 case CPP_UTF8STRING_USERDEF
:
5161 /* ??? Should wide strings be allowed when parser->translate_strings_p
5162 is false (i.e. in attributes)? If not, we can kill the third
5163 argument to cp_parser_string_literal. */
5164 return cp_parser_string_literal (parser
,
5165 parser
->translate_strings_p
,
5168 case CPP_OPEN_PAREN
:
5169 /* If we see `( { ' then we are looking at the beginning of
5170 a GNU statement-expression. */
5171 if (cp_parser_allow_gnu_extensions_p (parser
)
5172 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5174 /* Statement-expressions are not allowed by the standard. */
5175 pedwarn (token
->location
, OPT_Wpedantic
,
5176 "ISO C++ forbids braced-groups within expressions");
5178 /* And they're not allowed outside of a function-body; you
5179 cannot, for example, write:
5181 int i = ({ int j = 3; j + 1; });
5183 at class or namespace scope. */
5184 if (!parser
->in_function_body
5185 || parser
->in_template_argument_list_p
)
5187 error_at (token
->location
,
5188 "statement-expressions are not allowed outside "
5189 "functions nor in template-argument lists");
5190 cp_parser_skip_to_end_of_block_or_statement (parser
);
5191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5192 cp_lexer_consume_token (parser
->lexer
);
5193 return error_mark_node
;
5196 return cp_parser_statement_expr (parser
);
5198 /* Otherwise it's a normal parenthesized expression. */
5201 bool saved_greater_than_is_operator_p
;
5203 location_t open_paren_loc
= token
->location
;
5205 /* Consume the `('. */
5206 matching_parens parens
;
5207 parens
.consume_open (parser
);
5208 /* Within a parenthesized expression, a `>' token is always
5209 the greater-than operator. */
5210 saved_greater_than_is_operator_p
5211 = parser
->greater_than_is_operator_p
;
5212 parser
->greater_than_is_operator_p
= true;
5214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5215 /* Left fold expression. */
5218 /* Parse the parenthesized expression. */
5219 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5221 token
= cp_lexer_peek_token (parser
->lexer
);
5222 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5224 expr
= cp_parser_fold_expression (parser
, expr
);
5225 if (expr
!= error_mark_node
5226 && cxx_dialect
< cxx17
5227 && !in_system_header_at (input_location
))
5228 pedwarn (input_location
, 0, "fold-expressions only available "
5229 "with -std=c++17 or -std=gnu++17");
5232 /* Let the front end know that this expression was
5233 enclosed in parentheses. This matters in case, for
5234 example, the expression is of the form `A::B', since
5235 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5237 expr
= finish_parenthesized_expr (expr
);
5239 /* DR 705: Wrapping an unqualified name in parentheses
5240 suppresses arg-dependent lookup. We want to pass back
5241 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5242 (c++/37862), but none of the others. */
5243 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5244 *idk
= CP_ID_KIND_NONE
;
5246 /* The `>' token might be the end of a template-id or
5247 template-parameter-list now. */
5248 parser
->greater_than_is_operator_p
5249 = saved_greater_than_is_operator_p
;
5251 /* Consume the `)'. */
5252 token
= cp_lexer_peek_token (parser
->lexer
);
5253 location_t close_paren_loc
= token
->location
;
5254 expr
.set_range (open_paren_loc
, close_paren_loc
);
5255 if (!parens
.require_close (parser
)
5256 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5257 cp_parser_skip_to_end_of_statement (parser
);
5262 case CPP_OPEN_SQUARE
:
5264 if (c_dialect_objc ())
5266 /* We might have an Objective-C++ message. */
5267 cp_parser_parse_tentatively (parser
);
5268 tree msg
= cp_parser_objc_message_expression (parser
);
5269 /* If that works out, we're done ... */
5270 if (cp_parser_parse_definitely (parser
))
5272 /* ... else, fall though to see if it's a lambda. */
5274 cp_expr lam
= cp_parser_lambda_expression (parser
);
5275 /* Don't warn about a failed tentative parse. */
5276 if (cp_parser_error_occurred (parser
))
5277 return error_mark_node
;
5278 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5282 case CPP_OBJC_STRING
:
5283 if (c_dialect_objc ())
5284 /* We have an Objective-C++ string literal. */
5285 return cp_parser_objc_expression (parser
);
5286 cp_parser_error (parser
, "expected primary-expression");
5287 return error_mark_node
;
5290 switch (token
->keyword
)
5292 /* These two are the boolean literals. */
5294 cp_lexer_consume_token (parser
->lexer
);
5295 return cp_expr (boolean_true_node
, token
->location
);
5297 cp_lexer_consume_token (parser
->lexer
);
5298 return cp_expr (boolean_false_node
, token
->location
);
5300 /* The `__null' literal. */
5302 cp_lexer_consume_token (parser
->lexer
);
5303 return cp_expr (null_node
, token
->location
);
5305 /* The `nullptr' literal. */
5307 cp_lexer_consume_token (parser
->lexer
);
5308 return cp_expr (nullptr_node
, token
->location
);
5310 /* Recognize the `this' keyword. */
5312 cp_lexer_consume_token (parser
->lexer
);
5313 if (parser
->local_variables_forbidden_p
)
5315 error_at (token
->location
,
5316 "%<this%> may not be used in this context");
5317 return error_mark_node
;
5319 /* Pointers cannot appear in constant-expressions. */
5320 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5321 return error_mark_node
;
5322 return cp_expr (finish_this_expr (), token
->location
);
5324 /* The `operator' keyword can be the beginning of an
5329 case RID_FUNCTION_NAME
:
5330 case RID_PRETTY_FUNCTION_NAME
:
5331 case RID_C99_FUNCTION_NAME
:
5333 non_integral_constant name
;
5335 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5336 __func__ are the names of variables -- but they are
5337 treated specially. Therefore, they are handled here,
5338 rather than relying on the generic id-expression logic
5339 below. Grammatically, these names are id-expressions.
5341 Consume the token. */
5342 token
= cp_lexer_consume_token (parser
->lexer
);
5344 switch (token
->keyword
)
5346 case RID_FUNCTION_NAME
:
5347 name
= NIC_FUNC_NAME
;
5349 case RID_PRETTY_FUNCTION_NAME
:
5350 name
= NIC_PRETTY_FUNC
;
5352 case RID_C99_FUNCTION_NAME
:
5353 name
= NIC_C99_FUNC
;
5359 if (cp_parser_non_integral_constant_expression (parser
, name
))
5360 return error_mark_node
;
5362 /* Look up the name. */
5363 return finish_fname (token
->u
.value
);
5370 source_location type_location
;
5371 location_t start_loc
5372 = cp_lexer_peek_token (parser
->lexer
)->location
;
5373 /* The `__builtin_va_arg' construct is used to handle
5374 `va_arg'. Consume the `__builtin_va_arg' token. */
5375 cp_lexer_consume_token (parser
->lexer
);
5376 /* Look for the opening `('. */
5377 matching_parens parens
;
5378 parens
.require_open (parser
);
5379 /* Now, parse the assignment-expression. */
5380 expression
= cp_parser_assignment_expression (parser
);
5381 /* Look for the `,'. */
5382 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5383 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5384 /* Parse the type-id. */
5386 type_id_in_expr_sentinel
s (parser
);
5387 type
= cp_parser_type_id (parser
);
5389 /* Look for the closing `)'. */
5390 location_t finish_loc
5391 = cp_lexer_peek_token (parser
->lexer
)->location
;
5392 parens
.require_close (parser
);
5393 /* Using `va_arg' in a constant-expression is not
5395 if (cp_parser_non_integral_constant_expression (parser
,
5397 return error_mark_node
;
5398 /* Construct a location of the form:
5399 __builtin_va_arg (v, int)
5400 ~~~~~~~~~~~~~~~~~~~~~^~~~
5401 with the caret at the type, ranging from the start of the
5402 "__builtin_va_arg" token to the close paren. */
5403 location_t combined_loc
5404 = make_location (type_location
, start_loc
, finish_loc
);
5405 return build_x_va_arg (combined_loc
, expression
, type
);
5409 return cp_parser_builtin_offsetof (parser
);
5411 case RID_HAS_NOTHROW_ASSIGN
:
5412 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5413 case RID_HAS_NOTHROW_COPY
:
5414 case RID_HAS_TRIVIAL_ASSIGN
:
5415 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5416 case RID_HAS_TRIVIAL_COPY
:
5417 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5418 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5419 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5420 case RID_IS_ABSTRACT
:
5421 case RID_IS_AGGREGATE
:
5422 case RID_IS_BASE_OF
:
5427 case RID_IS_LITERAL_TYPE
:
5429 case RID_IS_POLYMORPHIC
:
5430 case RID_IS_SAME_AS
:
5431 case RID_IS_STD_LAYOUT
:
5432 case RID_IS_TRIVIAL
:
5433 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5434 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5435 case RID_IS_TRIVIALLY_COPYABLE
:
5437 case RID_IS_ASSIGNABLE
:
5438 case RID_IS_CONSTRUCTIBLE
:
5439 return cp_parser_trait_expr (parser
, token
->keyword
);
5443 return cp_parser_requires_expression (parser
);
5445 /* Objective-C++ expressions. */
5447 case RID_AT_PROTOCOL
:
5448 case RID_AT_SELECTOR
:
5449 return cp_parser_objc_expression (parser
);
5452 if (parser
->in_function_body
5453 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5456 error_at (token
->location
,
5457 "a template declaration cannot appear at block scope");
5458 cp_parser_skip_to_end_of_block_or_statement (parser
);
5459 return error_mark_node
;
5463 cp_parser_error (parser
, "expected primary-expression");
5464 return error_mark_node
;
5467 /* An id-expression can start with either an identifier, a
5468 `::' as the beginning of a qualified-id, or the "operator"
5472 case CPP_TEMPLATE_ID
:
5473 case CPP_NESTED_NAME_SPECIFIER
:
5476 cp_expr id_expression
;
5478 const char *error_msg
;
5481 cp_token
*id_expr_token
;
5483 /* Parse the id-expression. */
5485 = cp_parser_id_expression (parser
,
5486 /*template_keyword_p=*/false,
5487 /*check_dependency_p=*/true,
5489 /*declarator_p=*/false,
5490 /*optional_p=*/false);
5491 if (id_expression
== error_mark_node
)
5492 return error_mark_node
;
5493 id_expr_token
= token
;
5494 token
= cp_lexer_peek_token (parser
->lexer
);
5495 done
= (token
->type
!= CPP_OPEN_SQUARE
5496 && token
->type
!= CPP_OPEN_PAREN
5497 && token
->type
!= CPP_DOT
5498 && token
->type
!= CPP_DEREF
5499 && token
->type
!= CPP_PLUS_PLUS
5500 && token
->type
!= CPP_MINUS_MINUS
);
5501 /* If we have a template-id, then no further lookup is
5502 required. If the template-id was for a template-class, we
5503 will sometimes have a TYPE_DECL at this point. */
5504 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5505 || TREE_CODE (id_expression
) == TYPE_DECL
)
5506 decl
= id_expression
;
5507 /* Look up the name. */
5510 tree ambiguous_decls
;
5512 /* If we already know that this lookup is ambiguous, then
5513 we've already issued an error message; there's no reason
5515 if (id_expr_token
->type
== CPP_NAME
5516 && id_expr_token
->error_reported
)
5518 cp_parser_simulate_error (parser
);
5519 return error_mark_node
;
5522 decl
= cp_parser_lookup_name (parser
, id_expression
,
5525 /*is_namespace=*/false,
5526 /*check_dependency=*/true,
5528 id_expr_token
->location
);
5529 /* If the lookup was ambiguous, an error will already have
5531 if (ambiguous_decls
)
5532 return error_mark_node
;
5534 /* In Objective-C++, we may have an Objective-C 2.0
5535 dot-syntax for classes here. */
5536 if (c_dialect_objc ()
5537 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5538 && TREE_CODE (decl
) == TYPE_DECL
5539 && objc_is_class_name (decl
))
5542 cp_lexer_consume_token (parser
->lexer
);
5543 component
= cp_parser_identifier (parser
);
5544 if (component
== error_mark_node
)
5545 return error_mark_node
;
5547 tree result
= objc_build_class_component_ref (id_expression
,
5549 /* Build a location of the form:
5552 with caret at the start of the component name (at
5553 input_location), ranging from the start of the id_expression
5554 to the end of the component name. */
5555 location_t combined_loc
5556 = make_location (input_location
, id_expression
.get_start (),
5557 get_finish (input_location
));
5558 protected_set_expr_location (result
, combined_loc
);
5562 /* In Objective-C++, an instance variable (ivar) may be preferred
5563 to whatever cp_parser_lookup_name() found.
5564 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5565 rest of c-family, we have to do a little extra work to preserve
5566 any location information in cp_expr "decl". Given that
5567 objc_lookup_ivar is implemented in "c-family" and "objc", we
5568 have a trip through the pure "tree" type, rather than cp_expr.
5569 Naively copying it back to "decl" would implicitly give the
5570 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5571 store an EXPR_LOCATION. Hence we only update "decl" (and
5572 hence its location_t) if we get back a different tree node. */
5573 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5575 if (decl_tree
!= decl
.get_value ())
5576 decl
= cp_expr (decl_tree
);
5578 /* If name lookup gives us a SCOPE_REF, then the
5579 qualifying scope was dependent. */
5580 if (TREE_CODE (decl
) == SCOPE_REF
)
5582 /* At this point, we do not know if DECL is a valid
5583 integral constant expression. We assume that it is
5584 in fact such an expression, so that code like:
5586 template <int N> struct A {
5590 is accepted. At template-instantiation time, we
5591 will check that B<N>::i is actually a constant. */
5594 /* Check to see if DECL is a local variable in a context
5595 where that is forbidden. */
5596 if (parser
->local_variables_forbidden_p
5597 && local_variable_p (decl
))
5599 error_at (id_expr_token
->location
,
5600 "local variable %qD may not appear in this context",
5602 return error_mark_node
;
5606 decl
= (finish_id_expression
5607 (id_expression
, decl
, parser
->scope
,
5609 parser
->integral_constant_expression_p
,
5610 parser
->allow_non_integral_constant_expression_p
,
5611 &parser
->non_integral_constant_expression_p
,
5612 template_p
, done
, address_p
,
5615 id_expression
.get_location ()));
5617 cp_parser_error (parser
, error_msg
);
5618 decl
.set_location (id_expr_token
->location
);
5622 /* Anything else is an error. */
5624 cp_parser_error (parser
, "expected primary-expression");
5625 return error_mark_node
;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser
*parser
,
5633 bool template_arg_p
,
5636 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5637 /*decltype*/false, idk
);
5640 /* Parse an id-expression.
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5649 :: operator-function-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5675 cp_parser_id_expression (cp_parser
*parser
,
5676 bool template_keyword_p
,
5677 bool check_dependency_p
,
5682 bool global_scope_p
;
5683 bool nested_name_specifier_p
;
5685 /* Assume the `template' keyword was not used. */
5687 *template_p
= template_keyword_p
;
5689 /* Look for the optional `::' operator. */
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser
,
5693 /*current_scope_valid_p=*/false)
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser
,
5699 /*typename_keyword_p=*/false,
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p
)
5711 tree saved_object_scope
;
5712 tree saved_qualifying_scope
;
5713 cp_expr unqualified_id
;
5716 /* See if the next token is the `template' keyword. */
5718 template_p
= &is_template
;
5719 *template_p
= cp_parser_optional_template_keyword (parser
);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope
= parser
->scope
;
5723 saved_object_scope
= parser
->object_scope
;
5724 saved_qualifying_scope
= parser
->qualifying_scope
;
5725 /* Process the final unqualified-id. */
5726 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser
->scope
= saved_scope
;
5732 parser
->object_scope
= saved_object_scope
;
5733 parser
->qualifying_scope
= saved_qualifying_scope
;
5735 return unqualified_id
;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p
)
5744 /* Peek at the next token. */
5745 token
= cp_lexer_peek_token (parser
->lexer
);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token
->type
== CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5753 return cp_parser_identifier (parser
);
5755 cp_parser_parse_tentatively (parser
);
5756 /* Try a template-id. */
5757 id
= cp_parser_template_id (parser
,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser
))
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token
= cp_lexer_peek_token (parser
->lexer
);
5770 switch (token
->type
)
5773 return cp_parser_identifier (parser
);
5776 if (token
->keyword
== RID_OPERATOR
)
5777 return cp_parser_operator_function_id (parser
);
5781 cp_parser_error (parser
, "expected id-expression");
5782 return error_mark_node
;
5786 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5787 /*check_dependency_p=*/true,
5792 /* Parse an unqualified-id.
5796 operator-function-id
5797 conversion-function-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5815 cp_parser_unqualified_id (cp_parser
* parser
,
5816 bool template_keyword_p
,
5817 bool check_dependency_p
,
5823 /* Peek at the next token. */
5824 token
= cp_lexer_peek_token (parser
->lexer
);
5826 switch ((int) token
->type
)
5832 /* We don't know yet whether or not this will be a
5834 cp_parser_parse_tentatively (parser
);
5835 /* Try a template-id. */
5836 id
= cp_parser_template_id (parser
, template_keyword_p
,
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser
))
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser
);
5847 case CPP_TEMPLATE_ID
:
5848 return cp_parser_template_id (parser
, template_keyword_p
,
5856 tree qualifying_scope
;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser
->lexer
);
5863 /* Parse the class-name. The standard, as written, seems to
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope
= parser
->scope
;
5899 object_scope
= parser
->object_scope
;
5900 qualifying_scope
= parser
->qualifying_scope
;
5902 /* Check for invalid scopes. */
5903 if (scope
== error_mark_node
)
5905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5906 cp_lexer_consume_token (parser
->lexer
);
5907 return error_mark_node
;
5909 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5912 error_at (token
->location
,
5913 "scope %qT before %<~%> is not a class-name",
5915 cp_parser_simulate_error (parser
);
5916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5917 cp_lexer_consume_token (parser
->lexer
);
5918 return error_mark_node
;
5920 gcc_assert (!scope
|| TYPE_P (scope
));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5924 token
= cp_lexer_peek_token (parser
->lexer
);
5926 && token
->type
== CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5929 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5930 || (CLASS_TYPE_P (scope
)
5931 && constructor_name_p (token
->u
.value
, scope
))))
5933 cp_lexer_consume_token (parser
->lexer
);
5934 return build_nt (BIT_NOT_EXPR
, scope
);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token
, RID_AUTO
))
5940 if (cxx_dialect
< cxx14
)
5941 pedwarn (input_location
, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser
->lexer
);
5945 return build_nt (BIT_NOT_EXPR
, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5955 type_decl
= NULL_TREE
;
5958 cp_parser_parse_tentatively (parser
);
5959 type_decl
= cp_parser_class_name (parser
,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5966 if (cp_parser_parse_definitely (parser
))
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done
&& scope
&& qualifying_scope
)
5972 cp_parser_parse_tentatively (parser
);
5973 parser
->scope
= qualifying_scope
;
5974 parser
->object_scope
= NULL_TREE
;
5975 parser
->qualifying_scope
= NULL_TREE
;
5977 = cp_parser_class_name (parser
,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5984 if (cp_parser_parse_definitely (parser
))
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done
&& object_scope
)
5990 cp_parser_parse_tentatively (parser
);
5991 parser
->scope
= object_scope
;
5992 parser
->object_scope
= NULL_TREE
;
5993 parser
->qualifying_scope
= NULL_TREE
;
5995 = cp_parser_class_name (parser
,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6002 if (cp_parser_parse_definitely (parser
))
6005 /* Look in the surrounding context. */
6008 parser
->scope
= NULL_TREE
;
6009 parser
->object_scope
= NULL_TREE
;
6010 parser
->qualifying_scope
= NULL_TREE
;
6011 if (processing_template_decl
)
6012 cp_parser_parse_tentatively (parser
);
6014 = cp_parser_class_name (parser
,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser
))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6028 cp_parser_simulate_error (parser
);
6029 return error_mark_node
;
6031 /* Otherwise, accept it and check for a match at instantiation
6033 type_decl
= cp_parser_identifier (parser
);
6034 if (type_decl
!= error_mark_node
)
6035 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl
== error_mark_node
&& scope
)
6044 return build_nt (BIT_NOT_EXPR
, scope
);
6045 else if (type_decl
== error_mark_node
)
6046 return error_mark_node
;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6052 error_at (token
->location
,
6053 "declaration of %<~%T%> as member of %qT",
6055 cp_parser_simulate_error (parser
);
6056 return error_mark_node
;
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6065 && !DECL_SELF_REFERENCE_P (type_decl
)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6067 error_at (token
->location
,
6068 "typedef-name %qD used as destructor declarator",
6071 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6075 if (token
->keyword
== RID_OPERATOR
)
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser
);
6081 /* Try a template-id. */
6082 id
= cp_parser_template_id (parser
, template_keyword_p
,
6083 /*check_dependency_p=*/true,
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser
))
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser
);
6092 /* Try an operator-function-id. */
6093 id
= cp_parser_operator_function_id (parser
);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser
))
6096 id
= cp_parser_conversion_function_id (parser
);
6105 cp_parser_error (parser
, "expected unqualified-id");
6106 return error_mark_node
;
6110 /* Parse an (optional) nested-name-specifier.
6112 nested-name-specifier: [C++98]
6113 class-or-namespace-name :: nested-name-specifier [opt]
6114 class-or-namespace-name :: template nested-name-specifier [opt]
6116 nested-name-specifier: [C++0x]
6119 nested-name-specifier identifier ::
6120 nested-name-specifier template [opt] simple-template-id ::
6122 PARSER->SCOPE should be set appropriately before this function is
6123 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6124 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6127 Sets PARSER->SCOPE to the class (TYPE) or namespace
6128 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6129 it unchanged if there is no nested-name-specifier. Returns the new
6130 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6132 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6133 part of a declaration and/or decl-specifier. */
6136 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6137 bool typename_keyword_p
,
6138 bool check_dependency_p
,
6140 bool is_declaration
,
6141 bool template_keyword_p
/* = false */)
6143 bool success
= false;
6144 cp_token_position start
= 0;
6147 /* Remember where the nested-name-specifier starts. */
6148 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6150 start
= cp_lexer_token_position (parser
->lexer
, false);
6151 push_deferring_access_checks (dk_deferred
);
6158 tree saved_qualifying_scope
;
6160 /* Spot cases that cannot be the beginning of a
6161 nested-name-specifier. */
6162 token
= cp_lexer_peek_token (parser
->lexer
);
6164 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6165 the already parsed nested-name-specifier. */
6166 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6168 /* Grab the nested-name-specifier and continue the loop. */
6169 cp_parser_pre_parsed_nested_name_specifier (parser
);
6170 /* If we originally encountered this nested-name-specifier
6171 with IS_DECLARATION set to false, we will not have
6172 resolved TYPENAME_TYPEs, so we must do so here. */
6174 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6176 new_scope
= resolve_typename_type (parser
->scope
,
6177 /*only_current_p=*/false);
6178 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6179 parser
->scope
= new_scope
;
6185 /* Spot cases that cannot be the beginning of a
6186 nested-name-specifier. On the second and subsequent times
6187 through the loop, we look for the `template' keyword. */
6188 if (success
&& token
->keyword
== RID_TEMPLATE
)
6190 /* A template-id can start a nested-name-specifier. */
6191 else if (token
->type
== CPP_TEMPLATE_ID
)
6193 /* DR 743: decltype can be used in a nested-name-specifier. */
6194 else if (token_is_decltype (token
))
6198 /* If the next token is not an identifier, then it is
6199 definitely not a type-name or namespace-name. */
6200 if (token
->type
!= CPP_NAME
)
6202 /* If the following token is neither a `<' (to begin a
6203 template-id), nor a `::', then we are not looking at a
6204 nested-name-specifier. */
6205 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6207 if (token
->type
== CPP_COLON
6208 && parser
->colon_corrects_to_scope_p
6209 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6211 gcc_rich_location
richloc (token
->location
);
6212 richloc
.add_fixit_replace ("::");
6214 "found %<:%> in nested-name-specifier, "
6216 token
->type
= CPP_SCOPE
;
6219 if (token
->type
!= CPP_SCOPE
6220 && !cp_parser_nth_token_starts_template_argument_list_p
6225 /* The nested-name-specifier is optional, so we parse
6227 cp_parser_parse_tentatively (parser
);
6229 /* Look for the optional `template' keyword, if this isn't the
6230 first time through the loop. */
6232 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6234 /* Save the old scope since the name lookup we are about to do
6235 might destroy it. */
6236 old_scope
= parser
->scope
;
6237 saved_qualifying_scope
= parser
->qualifying_scope
;
6238 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6239 look up names in "X<T>::I" in order to determine that "Y" is
6240 a template. So, if we have a typename at this point, we make
6241 an effort to look through it. */
6243 && !typename_keyword_p
6245 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6246 parser
->scope
= resolve_typename_type (parser
->scope
,
6247 /*only_current_p=*/false);
6248 /* Parse the qualifying entity. */
6250 = cp_parser_qualifying_entity (parser
,
6256 /* Look for the `::' token. */
6257 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6259 /* If we found what we wanted, we keep going; otherwise, we're
6261 if (!cp_parser_parse_definitely (parser
))
6263 bool error_p
= false;
6265 /* Restore the OLD_SCOPE since it was valid before the
6266 failed attempt at finding the last
6267 class-or-namespace-name. */
6268 parser
->scope
= old_scope
;
6269 parser
->qualifying_scope
= saved_qualifying_scope
;
6271 /* If the next token is a decltype, and the one after that is a
6272 `::', then the decltype has failed to resolve to a class or
6273 enumeration type. Give this error even when parsing
6274 tentatively since it can't possibly be valid--and we're going
6275 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6276 won't get another chance.*/
6277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6278 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6281 token
= cp_lexer_consume_token (parser
->lexer
);
6282 error_at (token
->location
, "decltype evaluates to %qT, "
6283 "which is not a class or enumeration type",
6284 token
->u
.tree_check_value
->value
);
6285 parser
->scope
= error_mark_node
;
6289 cp_lexer_consume_token (parser
->lexer
);
6292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6293 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6295 /* If we have a non-type template-id followed by ::, it can't
6296 possibly be valid. */
6297 token
= cp_lexer_peek_token (parser
->lexer
);
6298 tree tid
= token
->u
.tree_check_value
->value
;
6299 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6300 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6302 tree tmpl
= NULL_TREE
;
6303 if (is_overloaded_fn (tid
))
6305 tree fns
= get_fns (tid
);
6306 if (OVL_SINGLE_P (fns
))
6307 tmpl
= OVL_FIRST (fns
);
6308 error_at (token
->location
, "function template-id %qD "
6309 "in nested-name-specifier", tid
);
6313 /* Variable template. */
6314 tmpl
= TREE_OPERAND (tid
, 0);
6315 gcc_assert (variable_template_p (tmpl
));
6316 error_at (token
->location
, "variable template-id %qD "
6317 "in nested-name-specifier", tid
);
6320 inform (DECL_SOURCE_LOCATION (tmpl
),
6321 "%qD declared here", tmpl
);
6323 parser
->scope
= error_mark_node
;
6327 cp_lexer_consume_token (parser
->lexer
);
6328 cp_lexer_consume_token (parser
->lexer
);
6332 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6334 /* If the next token is an identifier, and the one after
6335 that is a `::', then any valid interpretation would have
6336 found a class-or-namespace-name. */
6337 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6338 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6340 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6343 token
= cp_lexer_consume_token (parser
->lexer
);
6346 if (!token
->error_reported
)
6349 tree ambiguous_decls
;
6351 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6353 /*is_template=*/false,
6354 /*is_namespace=*/false,
6355 /*check_dependency=*/true,
6358 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6359 error_at (token
->location
,
6360 "%qD used without template arguments",
6362 else if (ambiguous_decls
)
6364 // cp_parser_lookup_name has the same diagnostic,
6365 // thus make sure to emit it at most once.
6366 if (cp_parser_uncommitted_to_tentative_parse_p
6369 error_at (token
->location
,
6370 "reference to %qD is ambiguous",
6372 print_candidates (ambiguous_decls
);
6374 decl
= error_mark_node
;
6378 if (cxx_dialect
!= cxx98
)
6379 cp_parser_name_lookup_error
6380 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6383 cp_parser_name_lookup_error
6384 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6388 parser
->scope
= error_mark_node
;
6390 /* Treat this as a successful nested-name-specifier
6395 If the name found is not a class-name (clause
6396 _class_) or namespace-name (_namespace.def_), the
6397 program is ill-formed. */
6400 cp_lexer_consume_token (parser
->lexer
);
6404 /* We've found one valid nested-name-specifier. */
6406 /* Name lookup always gives us a DECL. */
6407 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6408 new_scope
= TREE_TYPE (new_scope
);
6409 /* Uses of "template" must be followed by actual templates. */
6410 if (template_keyword_p
6411 && !(CLASS_TYPE_P (new_scope
)
6412 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6414 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6415 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6416 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6417 == TEMPLATE_ID_EXPR
)))
6418 permerror (input_location
, TYPE_P (new_scope
)
6419 ? G_("%qT is not a template")
6420 : G_("%qD is not a template"),
6422 /* If it is a class scope, try to complete it; we are about to
6423 be looking up names inside the class. */
6424 if (TYPE_P (new_scope
)
6425 /* Since checking types for dependency can be expensive,
6426 avoid doing it if the type is already complete. */
6427 && !COMPLETE_TYPE_P (new_scope
)
6428 /* Do not try to complete dependent types. */
6429 && !dependent_type_p (new_scope
))
6431 new_scope
= complete_type (new_scope
);
6432 /* If it is a typedef to current class, use the current
6433 class instead, as the typedef won't have any names inside
6435 if (!COMPLETE_TYPE_P (new_scope
)
6436 && currently_open_class (new_scope
))
6437 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6439 /* Make sure we look in the right scope the next time through
6441 parser
->scope
= new_scope
;
6444 /* If parsing tentatively, replace the sequence of tokens that makes
6445 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6446 token. That way, should we re-parse the token stream, we will
6447 not have to repeat the effort required to do the parse, nor will
6448 we issue duplicate error messages. */
6449 if (success
&& start
)
6453 token
= cp_lexer_token_at (parser
->lexer
, start
);
6454 /* Reset the contents of the START token. */
6455 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6456 /* Retrieve any deferred checks. Do not pop this access checks yet
6457 so the memory will not be reclaimed during token replacing below. */
6458 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6459 token
->u
.tree_check_value
->value
= parser
->scope
;
6460 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6461 token
->u
.tree_check_value
->qualifying_scope
=
6462 parser
->qualifying_scope
;
6463 token
->keyword
= RID_MAX
;
6465 /* Purge all subsequent tokens. */
6466 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6470 pop_to_parent_deferring_access_checks ();
6472 return success
? parser
->scope
: NULL_TREE
;
6475 /* Parse a nested-name-specifier. See
6476 cp_parser_nested_name_specifier_opt for details. This function
6477 behaves identically, except that it will an issue an error if no
6478 nested-name-specifier is present. */
6481 cp_parser_nested_name_specifier (cp_parser
*parser
,
6482 bool typename_keyword_p
,
6483 bool check_dependency_p
,
6485 bool is_declaration
)
6489 /* Look for the nested-name-specifier. */
6490 scope
= cp_parser_nested_name_specifier_opt (parser
,
6495 /* If it was not present, issue an error message. */
6498 cp_parser_error (parser
, "expected nested-name-specifier");
6499 parser
->scope
= NULL_TREE
;
6505 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6506 this is either a class-name or a namespace-name (which corresponds
6507 to the class-or-namespace-name production in the grammar). For
6508 C++0x, it can also be a type-name that refers to an enumeration
6509 type or a simple-template-id.
6511 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6512 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6513 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6514 TYPE_P is TRUE iff the next name should be taken as a class-name,
6515 even the same name is declared to be another entity in the same
6518 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6519 specified by the class-or-namespace-name. If neither is found the
6520 ERROR_MARK_NODE is returned. */
6523 cp_parser_qualifying_entity (cp_parser
*parser
,
6524 bool typename_keyword_p
,
6525 bool template_keyword_p
,
6526 bool check_dependency_p
,
6528 bool is_declaration
)
6531 tree saved_qualifying_scope
;
6532 tree saved_object_scope
;
6535 bool successful_parse_p
;
6537 /* DR 743: decltype can appear in a nested-name-specifier. */
6538 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6540 scope
= cp_parser_decltype (parser
);
6541 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6542 && !MAYBE_CLASS_TYPE_P (scope
))
6544 cp_parser_simulate_error (parser
);
6545 return error_mark_node
;
6547 if (TYPE_NAME (scope
))
6548 scope
= TYPE_NAME (scope
);
6552 /* Before we try to parse the class-name, we must save away the
6553 current PARSER->SCOPE since cp_parser_class_name will destroy
6555 saved_scope
= parser
->scope
;
6556 saved_qualifying_scope
= parser
->qualifying_scope
;
6557 saved_object_scope
= parser
->object_scope
;
6558 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6559 there is no need to look for a namespace-name. */
6560 only_class_p
= template_keyword_p
6561 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6563 cp_parser_parse_tentatively (parser
);
6564 scope
= cp_parser_class_name (parser
,
6567 type_p
? class_type
: none_type
,
6569 /*class_head_p=*/false,
6571 /*enum_ok=*/cxx_dialect
> cxx98
);
6572 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6573 /* If that didn't work, try for a namespace-name. */
6574 if (!only_class_p
&& !successful_parse_p
)
6576 /* Restore the saved scope. */
6577 parser
->scope
= saved_scope
;
6578 parser
->qualifying_scope
= saved_qualifying_scope
;
6579 parser
->object_scope
= saved_object_scope
;
6580 /* If we are not looking at an identifier followed by the scope
6581 resolution operator, then this is not part of a
6582 nested-name-specifier. (Note that this function is only used
6583 to parse the components of a nested-name-specifier.) */
6584 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6585 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6586 return error_mark_node
;
6587 scope
= cp_parser_namespace_name (parser
);
6593 /* Return true if we are looking at a compound-literal, false otherwise. */
6596 cp_parser_compound_literal_p (cp_parser
*parser
)
6598 cp_lexer_save_tokens (parser
->lexer
);
6600 /* Skip tokens until the next token is a closing parenthesis.
6601 If we find the closing `)', and the next token is a `{', then
6602 we are looking at a compound-literal. */
6603 bool compound_literal_p
6604 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6605 /*consume_paren=*/true)
6606 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6608 /* Roll back the tokens we skipped. */
6609 cp_lexer_rollback_tokens (parser
->lexer
);
6611 return compound_literal_p
;
6614 /* Return true if EXPR is the integer constant zero or a complex constant
6615 of zero, without any folding, but ignoring location wrappers. */
6618 literal_integer_zerop (const_tree expr
)
6620 return (location_wrapper_p (expr
)
6621 && integer_zerop (TREE_OPERAND (expr
, 0)));
6624 /* Parse a postfix-expression.
6628 postfix-expression [ expression ]
6629 postfix-expression ( expression-list [opt] )
6630 simple-type-specifier ( expression-list [opt] )
6631 typename :: [opt] nested-name-specifier identifier
6632 ( expression-list [opt] )
6633 typename :: [opt] nested-name-specifier template [opt] template-id
6634 ( expression-list [opt] )
6635 postfix-expression . template [opt] id-expression
6636 postfix-expression -> template [opt] id-expression
6637 postfix-expression . pseudo-destructor-name
6638 postfix-expression -> pseudo-destructor-name
6639 postfix-expression ++
6640 postfix-expression --
6641 dynamic_cast < type-id > ( expression )
6642 static_cast < type-id > ( expression )
6643 reinterpret_cast < type-id > ( expression )
6644 const_cast < type-id > ( expression )
6645 typeid ( expression )
6651 ( type-id ) { initializer-list , [opt] }
6653 This extension is a GNU version of the C99 compound-literal
6654 construct. (The C99 grammar uses `type-name' instead of `type-id',
6655 but they are essentially the same concept.)
6657 If ADDRESS_P is true, the postfix expression is the operand of the
6658 `&' operator. CAST_P is true if this expression is the target of a
6661 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6662 class member access expressions [expr.ref].
6664 Returns a representation of the expression. */
6667 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6668 bool member_access_only_p
, bool decltype_p
,
6669 cp_id_kind
* pidk_return
)
6674 cp_id_kind idk
= CP_ID_KIND_NONE
;
6675 cp_expr postfix_expression
= NULL_TREE
;
6676 bool is_member_access
= false;
6678 /* Peek at the next token. */
6679 token
= cp_lexer_peek_token (parser
->lexer
);
6680 loc
= token
->location
;
6681 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6683 /* Some of the productions are determined by keywords. */
6684 keyword
= token
->keyword
;
6694 const char *saved_message
;
6695 bool saved_in_type_id_in_expr_p
;
6697 /* All of these can be handled in the same way from the point
6698 of view of parsing. Begin by consuming the token
6699 identifying the cast. */
6700 cp_lexer_consume_token (parser
->lexer
);
6702 /* New types cannot be defined in the cast. */
6703 saved_message
= parser
->type_definition_forbidden_message
;
6704 parser
->type_definition_forbidden_message
6705 = G_("types may not be defined in casts");
6707 /* Look for the opening `<'. */
6708 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6709 /* Parse the type to which we are casting. */
6710 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6711 parser
->in_type_id_in_expr_p
= true;
6712 type
= cp_parser_type_id (parser
);
6713 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6714 /* Look for the closing `>'. */
6715 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6716 /* Restore the old message. */
6717 parser
->type_definition_forbidden_message
= saved_message
;
6719 bool saved_greater_than_is_operator_p
6720 = parser
->greater_than_is_operator_p
;
6721 parser
->greater_than_is_operator_p
= true;
6723 /* And the expression which is being cast. */
6724 matching_parens parens
;
6725 parens
.require_open (parser
);
6726 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6727 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6729 location_t end_loc
= close_paren
?
6730 close_paren
->location
: UNKNOWN_LOCATION
;
6732 parser
->greater_than_is_operator_p
6733 = saved_greater_than_is_operator_p
;
6735 /* Only type conversions to integral or enumeration types
6736 can be used in constant-expressions. */
6737 if (!cast_valid_in_integral_constant_expression_p (type
)
6738 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6740 postfix_expression
= error_mark_node
;
6748 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6752 = build_static_cast (type
, expression
, tf_warning_or_error
);
6756 = build_reinterpret_cast (type
, expression
,
6757 tf_warning_or_error
);
6761 = build_const_cast (type
, expression
, tf_warning_or_error
);
6767 /* Construct a location e.g. :
6768 reinterpret_cast <int *> (expr)
6769 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6770 ranging from the start of the "*_cast" token to the final closing
6771 paren, with the caret at the start. */
6772 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6773 postfix_expression
.set_location (cp_cast_loc
);
6780 const char *saved_message
;
6781 bool saved_in_type_id_in_expr_p
;
6783 /* Consume the `typeid' token. */
6784 cp_lexer_consume_token (parser
->lexer
);
6785 /* Look for the `(' token. */
6786 matching_parens parens
;
6787 parens
.require_open (parser
);
6788 /* Types cannot be defined in a `typeid' expression. */
6789 saved_message
= parser
->type_definition_forbidden_message
;
6790 parser
->type_definition_forbidden_message
6791 = G_("types may not be defined in a %<typeid%> expression");
6792 /* We can't be sure yet whether we're looking at a type-id or an
6794 cp_parser_parse_tentatively (parser
);
6795 /* Try a type-id first. */
6796 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6797 parser
->in_type_id_in_expr_p
= true;
6798 type
= cp_parser_type_id (parser
);
6799 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6800 /* Look for the `)' token. Otherwise, we can't be sure that
6801 we're not looking at an expression: consider `typeid (int
6802 (3))', for example. */
6803 cp_token
*close_paren
= parens
.require_close (parser
);
6804 /* If all went well, simply lookup the type-id. */
6805 if (cp_parser_parse_definitely (parser
))
6806 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6807 /* Otherwise, fall back to the expression variant. */
6812 /* Look for an expression. */
6813 expression
= cp_parser_expression (parser
, & idk
);
6814 /* Compute its typeid. */
6815 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6816 /* Look for the `)' token. */
6817 close_paren
= parens
.require_close (parser
);
6819 /* Restore the saved message. */
6820 parser
->type_definition_forbidden_message
= saved_message
;
6821 /* `typeid' may not appear in an integral constant expression. */
6822 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6823 postfix_expression
= error_mark_node
;
6825 /* Construct a location e.g. :
6828 ranging from the start of the "typeid" token to the final closing
6829 paren, with the caret at the start. */
6832 location_t typeid_loc
6833 = make_location (start_loc
, start_loc
, close_paren
->location
);
6834 postfix_expression
.set_location (typeid_loc
);
6835 postfix_expression
.maybe_add_location_wrapper ();
6843 /* The syntax permitted here is the same permitted for an
6844 elaborated-type-specifier. */
6845 ++parser
->prevent_constrained_type_specifiers
;
6846 type
= cp_parser_elaborated_type_specifier (parser
,
6847 /*is_friend=*/false,
6848 /*is_declaration=*/false);
6849 --parser
->prevent_constrained_type_specifiers
;
6850 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6855 case RID_BUILTIN_SHUFFLE
:
6856 case RID_BUILTIN_LAUNDER
:
6858 vec
<tree
, va_gc
> *vec
;
6862 cp_lexer_consume_token (parser
->lexer
);
6863 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6864 /*cast_p=*/false, /*allow_expansion_p=*/true,
6865 /*non_constant_p=*/NULL
);
6868 postfix_expression
= error_mark_node
;
6872 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6878 if (vec
->length () == 1)
6880 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6883 error_at (loc
, "wrong number of arguments to "
6884 "%<__builtin_addressof%>");
6885 postfix_expression
= error_mark_node
;
6889 case RID_BUILTIN_LAUNDER
:
6890 if (vec
->length () == 1)
6891 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6892 tf_warning_or_error
);
6895 error_at (loc
, "wrong number of arguments to "
6896 "%<__builtin_launder%>");
6897 postfix_expression
= error_mark_node
;
6901 case RID_BUILTIN_SHUFFLE
:
6902 if (vec
->length () == 2)
6904 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6905 (*vec
)[1], tf_warning_or_error
);
6906 else if (vec
->length () == 3)
6908 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6909 (*vec
)[2], tf_warning_or_error
);
6912 error_at (loc
, "wrong number of arguments to "
6913 "%<__builtin_shuffle%>");
6914 postfix_expression
= error_mark_node
;
6928 /* If the next thing is a simple-type-specifier, we may be
6929 looking at a functional cast. We could also be looking at
6930 an id-expression. So, we try the functional cast, and if
6931 that doesn't work we fall back to the primary-expression. */
6932 cp_parser_parse_tentatively (parser
);
6933 /* Look for the simple-type-specifier. */
6934 ++parser
->prevent_constrained_type_specifiers
;
6935 type
= cp_parser_simple_type_specifier (parser
,
6936 /*decl_specs=*/NULL
,
6937 CP_PARSER_FLAGS_NONE
);
6938 --parser
->prevent_constrained_type_specifiers
;
6939 /* Parse the cast itself. */
6940 if (!cp_parser_error_occurred (parser
))
6942 = cp_parser_functional_cast (parser
, type
);
6943 /* If that worked, we're done. */
6944 if (cp_parser_parse_definitely (parser
))
6947 /* If the functional-cast didn't work out, try a
6948 compound-literal. */
6949 if (cp_parser_allow_gnu_extensions_p (parser
)
6950 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6952 cp_expr initializer
= NULL_TREE
;
6954 cp_parser_parse_tentatively (parser
);
6956 matching_parens parens
;
6957 parens
.consume_open (parser
);
6959 /* Avoid calling cp_parser_type_id pointlessly, see comment
6960 in cp_parser_cast_expression about c++/29234. */
6961 if (!cp_parser_compound_literal_p (parser
))
6962 cp_parser_simulate_error (parser
);
6965 /* Parse the type. */
6966 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6967 parser
->in_type_id_in_expr_p
= true;
6968 type
= cp_parser_type_id (parser
);
6969 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6970 parens
.require_close (parser
);
6973 /* If things aren't going well, there's no need to
6975 if (!cp_parser_error_occurred (parser
))
6977 bool non_constant_p
;
6978 /* Parse the brace-enclosed initializer list. */
6979 initializer
= cp_parser_braced_list (parser
,
6982 /* If that worked, we're definitely looking at a
6983 compound-literal expression. */
6984 if (cp_parser_parse_definitely (parser
))
6986 /* Warn the user that a compound literal is not
6987 allowed in standard C++. */
6988 pedwarn (input_location
, OPT_Wpedantic
,
6989 "ISO C++ forbids compound-literals");
6990 /* For simplicity, we disallow compound literals in
6991 constant-expressions. We could
6992 allow compound literals of integer type, whose
6993 initializer was a constant, in constant
6994 expressions. Permitting that usage, as a further
6995 extension, would not change the meaning of any
6996 currently accepted programs. (Of course, as
6997 compound literals are not part of ISO C++, the
6998 standard has nothing to say.) */
6999 if (cp_parser_non_integral_constant_expression (parser
,
7002 postfix_expression
= error_mark_node
;
7005 /* Form the representation of the compound-literal. */
7007 = finish_compound_literal (type
, initializer
,
7008 tf_warning_or_error
, fcl_c99
);
7009 postfix_expression
.set_location (initializer
.get_location ());
7014 /* It must be a primary-expression. */
7016 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7017 /*template_arg_p=*/false,
7024 /* Note that we don't need to worry about calling build_cplus_new on a
7025 class-valued CALL_EXPR in decltype when it isn't the end of the
7026 postfix-expression; unary_complex_lvalue will take care of that for
7029 /* Keep looping until the postfix-expression is complete. */
7032 if (idk
== CP_ID_KIND_UNQUALIFIED
7033 && identifier_p (postfix_expression
)
7034 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7035 /* It is not a Koenig lookup function call. */
7037 = unqualified_name_lookup_error (postfix_expression
);
7039 /* Peek at the next token. */
7040 token
= cp_lexer_peek_token (parser
->lexer
);
7042 switch (token
->type
)
7044 case CPP_OPEN_SQUARE
:
7045 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7047 cp_parser_error (parser
,
7048 "two consecutive %<[%> shall "
7049 "only introduce an attribute");
7050 return error_mark_node
;
7053 = cp_parser_postfix_open_square_expression (parser
,
7057 postfix_expression
.set_range (start_loc
,
7058 postfix_expression
.get_location ());
7060 idk
= CP_ID_KIND_NONE
;
7061 is_member_access
= false;
7064 case CPP_OPEN_PAREN
:
7065 /* postfix-expression ( expression-list [opt] ) */
7068 bool is_builtin_constant_p
;
7069 bool saved_integral_constant_expression_p
= false;
7070 bool saved_non_integral_constant_expression_p
= false;
7071 tsubst_flags_t complain
= complain_flags (decltype_p
);
7072 vec
<tree
, va_gc
> *args
;
7073 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7075 is_member_access
= false;
7077 is_builtin_constant_p
7078 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7079 if (is_builtin_constant_p
)
7081 /* The whole point of __builtin_constant_p is to allow
7082 non-constant expressions to appear as arguments. */
7083 saved_integral_constant_expression_p
7084 = parser
->integral_constant_expression_p
;
7085 saved_non_integral_constant_expression_p
7086 = parser
->non_integral_constant_expression_p
;
7087 parser
->integral_constant_expression_p
= false;
7089 args
= (cp_parser_parenthesized_expression_list
7091 /*cast_p=*/false, /*allow_expansion_p=*/true,
7092 /*non_constant_p=*/NULL
,
7093 /*close_paren_loc=*/&close_paren_loc
,
7094 /*wrap_locations_p=*/true));
7095 if (is_builtin_constant_p
)
7097 parser
->integral_constant_expression_p
7098 = saved_integral_constant_expression_p
;
7099 parser
->non_integral_constant_expression_p
7100 = saved_non_integral_constant_expression_p
;
7105 postfix_expression
= error_mark_node
;
7109 /* Function calls are not permitted in
7110 constant-expressions. */
7111 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7112 && cp_parser_non_integral_constant_expression (parser
,
7115 postfix_expression
= error_mark_node
;
7116 release_tree_vector (args
);
7121 if (idk
== CP_ID_KIND_UNQUALIFIED
7122 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7124 if (identifier_p (postfix_expression
))
7126 if (!args
->is_empty ())
7129 if (!any_type_dependent_arguments_p (args
))
7131 = perform_koenig_lookup (postfix_expression
, args
,
7136 = unqualified_fn_lookup_error (postfix_expression
);
7138 /* We do not perform argument-dependent lookup if
7139 normal lookup finds a non-function, in accordance
7140 with the expected resolution of DR 218. */
7141 else if (!args
->is_empty ()
7142 && is_overloaded_fn (postfix_expression
))
7144 tree fn
= get_first_fn (postfix_expression
);
7145 fn
= STRIP_TEMPLATE (fn
);
7147 /* Do not do argument dependent lookup if regular
7148 lookup finds a member function or a block-scope
7149 function declaration. [basic.lookup.argdep]/3 */
7150 if (!DECL_FUNCTION_MEMBER_P (fn
)
7151 && !DECL_LOCAL_FUNCTION_P (fn
))
7154 if (!any_type_dependent_arguments_p (args
))
7156 = perform_koenig_lookup (postfix_expression
, args
,
7162 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7164 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7165 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7167 if (processing_template_decl
7168 && (type_dependent_object_expression_p (instance
)
7169 || (!BASELINK_P (fn
)
7170 && TREE_CODE (fn
) != FIELD_DECL
)
7171 || type_dependent_expression_p (fn
)
7172 || any_type_dependent_arguments_p (args
)))
7174 maybe_generic_this_capture (instance
, fn
);
7176 = build_min_nt_call_vec (postfix_expression
, args
);
7177 release_tree_vector (args
);
7181 if (BASELINK_P (fn
))
7184 = (build_new_method_call
7185 (instance
, fn
, &args
, NULL_TREE
,
7186 (idk
== CP_ID_KIND_QUALIFIED
7187 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7194 = finish_call_expr (postfix_expression
, &args
,
7195 /*disallow_virtual=*/false,
7199 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7200 || TREE_CODE (postfix_expression
) == MEMBER_REF
7201 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7202 postfix_expression
= (build_offset_ref_call_from_tree
7203 (postfix_expression
, &args
,
7205 else if (idk
== CP_ID_KIND_QUALIFIED
)
7206 /* A call to a static class member, or a namespace-scope
7209 = finish_call_expr (postfix_expression
, &args
,
7210 /*disallow_virtual=*/true,
7214 /* All other function calls. */
7216 = finish_call_expr (postfix_expression
, &args
,
7217 /*disallow_virtual=*/false,
7221 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7223 location_t combined_loc
= make_location (token
->location
,
7226 postfix_expression
.set_location (combined_loc
);
7229 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7230 idk
= CP_ID_KIND_NONE
;
7232 release_tree_vector (args
);
7238 /* postfix-expression . template [opt] id-expression
7239 postfix-expression . pseudo-destructor-name
7240 postfix-expression -> template [opt] id-expression
7241 postfix-expression -> pseudo-destructor-name */
7243 /* Consume the `.' or `->' operator. */
7244 cp_lexer_consume_token (parser
->lexer
);
7247 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7251 is_member_access
= true;
7255 /* postfix-expression ++ */
7256 /* Consume the `++' token. */
7257 cp_lexer_consume_token (parser
->lexer
);
7258 /* Generate a representation for the complete expression. */
7260 = finish_increment_expr (postfix_expression
,
7261 POSTINCREMENT_EXPR
);
7262 /* Increments may not appear in constant-expressions. */
7263 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7264 postfix_expression
= error_mark_node
;
7265 idk
= CP_ID_KIND_NONE
;
7266 is_member_access
= false;
7269 case CPP_MINUS_MINUS
:
7270 /* postfix-expression -- */
7271 /* Consume the `--' token. */
7272 cp_lexer_consume_token (parser
->lexer
);
7273 /* Generate a representation for the complete expression. */
7275 = finish_increment_expr (postfix_expression
,
7276 POSTDECREMENT_EXPR
);
7277 /* Decrements may not appear in constant-expressions. */
7278 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7279 postfix_expression
= error_mark_node
;
7280 idk
= CP_ID_KIND_NONE
;
7281 is_member_access
= false;
7285 if (pidk_return
!= NULL
)
7286 * pidk_return
= idk
;
7287 if (member_access_only_p
)
7288 return is_member_access
7289 ? postfix_expression
7290 : cp_expr (error_mark_node
);
7292 return postfix_expression
;
7296 /* We should never get here. */
7298 return error_mark_node
;
7301 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7302 by cp_parser_builtin_offsetof. We're looking for
7304 postfix-expression [ expression ]
7305 postfix-expression [ braced-init-list ] (C++11)
7307 FOR_OFFSETOF is set if we're being called in that context, which
7308 changes how we deal with integer constant expressions. */
7311 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7312 tree postfix_expression
,
7316 tree index
= NULL_TREE
;
7317 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7318 bool saved_greater_than_is_operator_p
;
7320 /* Consume the `[' token. */
7321 cp_lexer_consume_token (parser
->lexer
);
7323 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7324 parser
->greater_than_is_operator_p
= true;
7326 /* Parse the index expression. */
7327 /* ??? For offsetof, there is a question of what to allow here. If
7328 offsetof is not being used in an integral constant expression context,
7329 then we *could* get the right answer by computing the value at runtime.
7330 If we are in an integral constant expression context, then we might
7331 could accept any constant expression; hard to say without analysis.
7332 Rather than open the barn door too wide right away, allow only integer
7333 constant expressions here. */
7335 index
= cp_parser_constant_expression (parser
);
7338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7340 bool expr_nonconst_p
;
7341 cp_lexer_set_source_position (parser
->lexer
);
7342 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7343 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7346 index
= cp_parser_expression (parser
);
7349 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7351 /* Look for the closing `]'. */
7352 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7354 /* Build the ARRAY_REF. */
7355 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7358 /* When not doing offsetof, array references are not permitted in
7359 constant-expressions. */
7361 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7362 postfix_expression
= error_mark_node
;
7364 return postfix_expression
;
7367 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7368 dereference of incomplete type, returns true if error_mark_node should
7369 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7370 and *DEPENDENT_P. */
7373 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7376 /* In a template, be permissive by treating an object expression
7377 of incomplete type as dependent (after a pedwarn). */
7378 diagnostic_t kind
= (processing_template_decl
7379 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7381 switch (TREE_CODE (*postfix_expression
))
7384 case REINTERPRET_CAST_EXPR
:
7385 case CONST_CAST_EXPR
:
7386 case STATIC_CAST_EXPR
:
7387 case DYNAMIC_CAST_EXPR
:
7388 case IMPLICIT_CONV_EXPR
:
7389 case VIEW_CONVERT_EXPR
:
7390 case NON_LVALUE_EXPR
:
7394 /* Don't emit any diagnostic for OVERLOADs. */
7398 /* Avoid clobbering e.g. DECLs. */
7399 if (!EXPR_P (*postfix_expression
))
7404 if (kind
== DK_IGNORED
)
7407 location_t exploc
= location_of (*postfix_expression
);
7408 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7409 if (!MAYBE_CLASS_TYPE_P (*scope
))
7411 if (kind
== DK_ERROR
)
7412 *scope
= *postfix_expression
= error_mark_node
;
7413 else if (processing_template_decl
)
7415 *dependent_p
= true;
7416 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7421 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7422 by cp_parser_builtin_offsetof. We're looking for
7424 postfix-expression . template [opt] id-expression
7425 postfix-expression . pseudo-destructor-name
7426 postfix-expression -> template [opt] id-expression
7427 postfix-expression -> pseudo-destructor-name
7429 FOR_OFFSETOF is set if we're being called in that context. That sorta
7430 limits what of the above we'll actually accept, but nevermind.
7431 TOKEN_TYPE is the "." or "->" token, which will already have been
7432 removed from the stream. */
7435 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7436 enum cpp_ttype token_type
,
7437 cp_expr postfix_expression
,
7438 bool for_offsetof
, cp_id_kind
*idk
,
7439 location_t location
)
7443 bool pseudo_destructor_p
;
7444 tree scope
= NULL_TREE
;
7445 location_t start_loc
= postfix_expression
.get_start ();
7447 /* If this is a `->' operator, dereference the pointer. */
7448 if (token_type
== CPP_DEREF
)
7449 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7450 tf_warning_or_error
);
7451 /* Check to see whether or not the expression is type-dependent and
7452 not the current instantiation. */
7453 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7454 /* The identifier following the `->' or `.' is not qualified. */
7455 parser
->scope
= NULL_TREE
;
7456 parser
->qualifying_scope
= NULL_TREE
;
7457 parser
->object_scope
= NULL_TREE
;
7458 *idk
= CP_ID_KIND_NONE
;
7460 /* Enter the scope corresponding to the type of the object
7461 given by the POSTFIX_EXPRESSION. */
7464 scope
= TREE_TYPE (postfix_expression
);
7465 /* According to the standard, no expression should ever have
7466 reference type. Unfortunately, we do not currently match
7467 the standard in this respect in that our internal representation
7468 of an expression may have reference type even when the standard
7469 says it does not. Therefore, we have to manually obtain the
7470 underlying type here. */
7471 scope
= non_reference (scope
);
7472 /* The type of the POSTFIX_EXPRESSION must be complete. */
7473 /* Unlike the object expression in other contexts, *this is not
7474 required to be of complete type for purposes of class member
7475 access (5.2.5) outside the member function body. */
7476 if (postfix_expression
!= current_class_ref
7477 && scope
!= error_mark_node
7478 && !currently_open_class (scope
))
7480 scope
= complete_type (scope
);
7481 if (!COMPLETE_TYPE_P (scope
)
7482 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7484 return error_mark_node
;
7489 /* Let the name lookup machinery know that we are processing a
7490 class member access expression. */
7491 parser
->context
->object_type
= scope
;
7492 /* If something went wrong, we want to be able to discern that case,
7493 as opposed to the case where there was no SCOPE due to the type
7494 of expression being dependent. */
7496 scope
= error_mark_node
;
7497 /* If the SCOPE was erroneous, make the various semantic analysis
7498 functions exit quickly -- and without issuing additional error
7500 if (scope
== error_mark_node
)
7501 postfix_expression
= error_mark_node
;
7506 /* Tell cp_parser_lookup_name that there was an object, even though it's
7508 parser
->context
->object_type
= unknown_type_node
;
7510 /* Assume this expression is not a pseudo-destructor access. */
7511 pseudo_destructor_p
= false;
7513 /* If the SCOPE is a scalar type, then, if this is a valid program,
7514 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7515 is type dependent, it can be pseudo-destructor-name or something else.
7516 Try to parse it as pseudo-destructor-name first. */
7517 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7522 cp_parser_parse_tentatively (parser
);
7523 /* Parse the pseudo-destructor-name. */
7525 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7528 && (cp_parser_error_occurred (parser
)
7529 || !SCALAR_TYPE_P (type
)))
7530 cp_parser_abort_tentative_parse (parser
);
7531 else if (cp_parser_parse_definitely (parser
))
7533 pseudo_destructor_p
= true;
7535 = finish_pseudo_destructor_expr (postfix_expression
,
7540 if (!pseudo_destructor_p
)
7542 /* If the SCOPE is not a scalar type, we are looking at an
7543 ordinary class member access expression, rather than a
7544 pseudo-destructor-name. */
7546 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7547 /* Parse the id-expression. */
7548 name
= (cp_parser_id_expression
7550 cp_parser_optional_template_keyword (parser
),
7551 /*check_dependency_p=*/true,
7553 /*declarator_p=*/false,
7554 /*optional_p=*/false));
7555 /* In general, build a SCOPE_REF if the member name is qualified.
7556 However, if the name was not dependent and has already been
7557 resolved; there is no need to build the SCOPE_REF. For example;
7559 struct X { void f(); };
7560 template <typename T> void f(T* t) { t->X::f(); }
7562 Even though "t" is dependent, "X::f" is not and has been resolved
7563 to a BASELINK; there is no need to include scope information. */
7565 /* But we do need to remember that there was an explicit scope for
7566 virtual function calls. */
7568 *idk
= CP_ID_KIND_QUALIFIED
;
7570 /* If the name is a template-id that names a type, we will get a
7571 TYPE_DECL here. That is invalid code. */
7572 if (TREE_CODE (name
) == TYPE_DECL
)
7574 error_at (token
->location
, "invalid use of %qD", name
);
7575 postfix_expression
= error_mark_node
;
7579 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7581 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7583 error_at (token
->location
, "%<%D::%D%> is not a class member",
7584 parser
->scope
, name
);
7585 postfix_expression
= error_mark_node
;
7588 name
= build_qualified_name (/*type=*/NULL_TREE
,
7592 parser
->scope
= NULL_TREE
;
7593 parser
->qualifying_scope
= NULL_TREE
;
7594 parser
->object_scope
= NULL_TREE
;
7596 if (parser
->scope
&& name
&& BASELINK_P (name
))
7597 adjust_result_of_qualified_name_lookup
7598 (name
, parser
->scope
, scope
);
7600 = finish_class_member_access_expr (postfix_expression
, name
,
7602 tf_warning_or_error
);
7603 /* Build a location e.g.:
7606 where the caret is at the deref token, ranging from
7607 the start of postfix_expression to the end of the access expr. */
7609 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7610 location_t combined_loc
7611 = make_location (input_location
, start_loc
, end_loc
);
7612 protected_set_expr_location (postfix_expression
, combined_loc
);
7616 /* We no longer need to look up names in the scope of the object on
7617 the left-hand side of the `.' or `->' operator. */
7618 parser
->context
->object_type
= NULL_TREE
;
7620 /* Outside of offsetof, these operators may not appear in
7621 constant-expressions. */
7623 && (cp_parser_non_integral_constant_expression
7624 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7625 postfix_expression
= error_mark_node
;
7627 return postfix_expression
;
7630 /* Parse a parenthesized expression-list.
7633 assignment-expression
7634 expression-list, assignment-expression
7639 identifier, expression-list
7641 CAST_P is true if this expression is the target of a cast.
7643 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7646 WRAP_LOCATIONS_P is true if expressions within this list for which
7647 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7648 their source locations.
7650 Returns a vector of trees. Each element is a representation of an
7651 assignment-expression. NULL is returned if the ( and or ) are
7652 missing. An empty, but allocated, vector is returned on no
7653 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7654 if we are parsing an attribute list for an attribute that wants a
7655 plain identifier argument, normal_attr for an attribute that wants
7656 an expression, or non_attr if we aren't parsing an attribute list. If
7657 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7658 not all of the expressions in the list were constant.
7659 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7660 will be written to with the location of the closing parenthesis. If
7661 an error occurs, it may or may not be written to. */
7663 static vec
<tree
, va_gc
> *
7664 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7665 int is_attribute_list
,
7667 bool allow_expansion_p
,
7668 bool *non_constant_p
,
7669 location_t
*close_paren_loc
,
7670 bool wrap_locations_p
)
7672 vec
<tree
, va_gc
> *expression_list
;
7673 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7674 tree identifier
= NULL_TREE
;
7675 bool saved_greater_than_is_operator_p
;
7677 /* Assume all the expressions will be constant. */
7679 *non_constant_p
= false;
7681 matching_parens parens
;
7682 if (!parens
.require_open (parser
))
7685 expression_list
= make_tree_vector ();
7687 /* Within a parenthesized expression, a `>' token is always
7688 the greater-than operator. */
7689 saved_greater_than_is_operator_p
7690 = parser
->greater_than_is_operator_p
;
7691 parser
->greater_than_is_operator_p
= true;
7693 cp_expr
expr (NULL_TREE
);
7695 /* Consume expressions until there are no more. */
7696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7699 /* At the beginning of attribute lists, check to see if the
7700 next token is an identifier. */
7701 if (is_attribute_list
== id_attr
7702 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7706 /* Consume the identifier. */
7707 token
= cp_lexer_consume_token (parser
->lexer
);
7708 /* Save the identifier. */
7709 identifier
= token
->u
.value
;
7713 bool expr_non_constant_p
;
7715 /* Parse the next assignment-expression. */
7716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7718 /* A braced-init-list. */
7719 cp_lexer_set_source_position (parser
->lexer
);
7720 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7721 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7722 if (non_constant_p
&& expr_non_constant_p
)
7723 *non_constant_p
= true;
7725 else if (non_constant_p
)
7727 expr
= (cp_parser_constant_expression
7728 (parser
, /*allow_non_constant_p=*/true,
7729 &expr_non_constant_p
));
7730 if (expr_non_constant_p
)
7731 *non_constant_p
= true;
7734 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7738 expr
= instantiate_non_dependent_expr (expr
);
7740 /* If we have an ellipsis, then this is an expression
7742 if (allow_expansion_p
7743 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7745 /* Consume the `...'. */
7746 cp_lexer_consume_token (parser
->lexer
);
7748 /* Build the argument pack. */
7749 expr
= make_pack_expansion (expr
);
7752 if (wrap_locations_p
)
7753 expr
.maybe_add_location_wrapper ();
7755 /* Add it to the list. We add error_mark_node
7756 expressions to the list, so that we can still tell if
7757 the correct form for a parenthesized expression-list
7758 is found. That gives better errors. */
7759 vec_safe_push (expression_list
, expr
.get_value ());
7761 if (expr
== error_mark_node
)
7765 /* After the first item, attribute lists look the same as
7766 expression lists. */
7767 is_attribute_list
= non_attr
;
7770 /* If the next token isn't a `,', then we are done. */
7771 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7774 /* Otherwise, consume the `,' and keep going. */
7775 cp_lexer_consume_token (parser
->lexer
);
7778 if (close_paren_loc
)
7779 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7781 if (!parens
.require_close (parser
))
7786 /* We try and resync to an unnested comma, as that will give the
7787 user better diagnostics. */
7788 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7789 /*recovering=*/true,
7791 /*consume_paren=*/true);
7796 parser
->greater_than_is_operator_p
7797 = saved_greater_than_is_operator_p
;
7802 parser
->greater_than_is_operator_p
7803 = saved_greater_than_is_operator_p
;
7806 vec_safe_insert (expression_list
, 0, identifier
);
7808 return expression_list
;
7811 /* Parse a pseudo-destructor-name.
7813 pseudo-destructor-name:
7814 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7815 :: [opt] nested-name-specifier template template-id :: ~ type-name
7816 :: [opt] nested-name-specifier [opt] ~ type-name
7818 If either of the first two productions is used, sets *SCOPE to the
7819 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7820 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7821 or ERROR_MARK_NODE if the parse fails. */
7824 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7829 bool nested_name_specifier_p
;
7832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7833 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7834 && !type_dependent_expression_p (object
))
7836 if (cxx_dialect
< cxx14
)
7837 pedwarn (input_location
, 0,
7838 "%<~auto%> only available with "
7839 "-std=c++14 or -std=gnu++14");
7840 cp_lexer_consume_token (parser
->lexer
);
7841 cp_lexer_consume_token (parser
->lexer
);
7843 *type
= TREE_TYPE (object
);
7847 /* Assume that things will not work out. */
7848 *type
= error_mark_node
;
7850 /* Look for the optional `::' operator. */
7851 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7852 /* Look for the optional nested-name-specifier. */
7853 nested_name_specifier_p
7854 = (cp_parser_nested_name_specifier_opt (parser
,
7855 /*typename_keyword_p=*/false,
7856 /*check_dependency_p=*/true,
7858 /*is_declaration=*/false)
7860 /* Now, if we saw a nested-name-specifier, we might be doing the
7861 second production. */
7862 if (nested_name_specifier_p
7863 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7865 /* Consume the `template' keyword. */
7866 cp_lexer_consume_token (parser
->lexer
);
7867 /* Parse the template-id. */
7868 cp_parser_template_id (parser
,
7869 /*template_keyword_p=*/true,
7870 /*check_dependency_p=*/false,
7872 /*is_declaration=*/true);
7873 /* Look for the `::' token. */
7874 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7876 /* If the next token is not a `~', then there might be some
7877 additional qualification. */
7878 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7880 /* At this point, we're looking for "type-name :: ~". The type-name
7881 must not be a class-name, since this is a pseudo-destructor. So,
7882 it must be either an enum-name, or a typedef-name -- both of which
7883 are just identifiers. So, we peek ahead to check that the "::"
7884 and "~" tokens are present; if they are not, then we can avoid
7885 calling type_name. */
7886 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7887 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7888 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7890 cp_parser_error (parser
, "non-scalar type");
7894 /* Look for the type-name. */
7895 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7896 if (*scope
== error_mark_node
)
7899 /* Look for the `::' token. */
7900 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7905 /* Look for the `~'. */
7906 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7908 /* Once we see the ~, this has to be a pseudo-destructor. */
7909 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7910 cp_parser_commit_to_topmost_tentative_parse (parser
);
7912 /* Look for the type-name again. We are not responsible for
7913 checking that it matches the first type-name. */
7914 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7917 /* Parse a unary-expression.
7923 unary-operator cast-expression
7924 sizeof unary-expression
7926 alignof ( type-id ) [C++0x]
7933 __extension__ cast-expression
7934 __alignof__ unary-expression
7935 __alignof__ ( type-id )
7936 alignof unary-expression [C++0x]
7937 __real__ cast-expression
7938 __imag__ cast-expression
7940 sizeof ( type-id ) { initializer-list , [opt] }
7941 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7942 __alignof__ ( type-id ) { initializer-list , [opt] }
7944 ADDRESS_P is true iff the unary-expression is appearing as the
7945 operand of the `&' operator. CAST_P is true if this expression is
7946 the target of a cast.
7948 Returns a representation of the expression. */
7951 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7952 bool address_p
, bool cast_p
, bool decltype_p
)
7955 enum tree_code unary_operator
;
7957 /* Peek at the next token. */
7958 token
= cp_lexer_peek_token (parser
->lexer
);
7959 /* Some keywords give away the kind of expression. */
7960 if (token
->type
== CPP_KEYWORD
)
7962 enum rid keyword
= token
->keyword
;
7971 location_t start_loc
= token
->location
;
7973 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7974 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
7976 /* Consume the token. */
7977 cp_lexer_consume_token (parser
->lexer
);
7978 /* Parse the operand. */
7979 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7981 if (TYPE_P (operand
))
7982 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
7986 /* ISO C++ defines alignof only with types, not with
7987 expressions. So pedwarn if alignof is used with a non-
7988 type expression. However, __alignof__ is ok. */
7990 pedwarn (token
->location
, OPT_Wpedantic
,
7991 "ISO C++ does not allow %<alignof%> "
7994 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7996 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7997 SIZEOF_EXPR with the original operand. */
7998 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8000 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8002 if (!processing_template_decl
&& TYPE_P (operand
))
8004 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8005 build1 (NOP_EXPR
, operand
,
8007 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8010 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8011 TREE_SIDE_EFFECTS (ret
) = 0;
8012 TREE_READONLY (ret
) = 1;
8016 /* Construct a location e.g. :
8019 with start == caret at the start of the "alignof"/"sizeof"
8020 token, with the endpoint at the final closing paren. */
8021 location_t finish_loc
8022 = cp_lexer_previous_token (parser
->lexer
)->location
;
8023 location_t compound_loc
8024 = make_location (start_loc
, start_loc
, finish_loc
);
8026 cp_expr
ret_expr (ret
);
8027 ret_expr
.set_location (compound_loc
);
8028 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8033 return cp_parser_new_expression (parser
);
8036 return cp_parser_delete_expression (parser
);
8040 /* The saved value of the PEDANTIC flag. */
8044 /* Save away the PEDANTIC flag. */
8045 cp_parser_extension_opt (parser
, &saved_pedantic
);
8046 /* Parse the cast-expression. */
8047 expr
= cp_parser_simple_cast_expression (parser
);
8048 /* Restore the PEDANTIC flag. */
8049 pedantic
= saved_pedantic
;
8059 /* Consume the `__real__' or `__imag__' token. */
8060 cp_lexer_consume_token (parser
->lexer
);
8061 /* Parse the cast-expression. */
8062 expression
= cp_parser_simple_cast_expression (parser
);
8063 /* Create the complete representation. */
8064 return build_x_unary_op (token
->location
,
8065 (keyword
== RID_REALPART
8066 ? REALPART_EXPR
: IMAGPART_EXPR
),
8068 tf_warning_or_error
);
8072 case RID_TRANSACTION_ATOMIC
:
8073 case RID_TRANSACTION_RELAXED
:
8074 return cp_parser_transaction_expression (parser
, keyword
);
8079 const char *saved_message
;
8080 bool saved_integral_constant_expression_p
;
8081 bool saved_non_integral_constant_expression_p
;
8082 bool saved_greater_than_is_operator_p
;
8084 location_t start_loc
= token
->location
;
8086 cp_lexer_consume_token (parser
->lexer
);
8087 matching_parens parens
;
8088 parens
.require_open (parser
);
8090 saved_message
= parser
->type_definition_forbidden_message
;
8091 parser
->type_definition_forbidden_message
8092 = G_("types may not be defined in %<noexcept%> expressions");
8094 saved_integral_constant_expression_p
8095 = parser
->integral_constant_expression_p
;
8096 saved_non_integral_constant_expression_p
8097 = parser
->non_integral_constant_expression_p
;
8098 parser
->integral_constant_expression_p
= false;
8100 saved_greater_than_is_operator_p
8101 = parser
->greater_than_is_operator_p
;
8102 parser
->greater_than_is_operator_p
= true;
8104 ++cp_unevaluated_operand
;
8105 ++c_inhibit_evaluation_warnings
;
8106 ++cp_noexcept_operand
;
8107 expr
= cp_parser_expression (parser
);
8108 --cp_noexcept_operand
;
8109 --c_inhibit_evaluation_warnings
;
8110 --cp_unevaluated_operand
;
8112 parser
->greater_than_is_operator_p
8113 = saved_greater_than_is_operator_p
;
8115 parser
->integral_constant_expression_p
8116 = saved_integral_constant_expression_p
;
8117 parser
->non_integral_constant_expression_p
8118 = saved_non_integral_constant_expression_p
;
8120 parser
->type_definition_forbidden_message
= saved_message
;
8122 location_t finish_loc
8123 = cp_lexer_peek_token (parser
->lexer
)->location
;
8124 parens
.require_close (parser
);
8126 /* Construct a location of the form:
8129 with start == caret, finishing at the close-paren. */
8130 location_t noexcept_loc
8131 = make_location (start_loc
, start_loc
, finish_loc
);
8133 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8142 /* Look for the `:: new' and `:: delete', which also signal the
8143 beginning of a new-expression, or delete-expression,
8144 respectively. If the next token is `::', then it might be one of
8146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8150 /* See if the token after the `::' is one of the keywords in
8151 which we're interested. */
8152 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8153 /* If it's `new', we have a new-expression. */
8154 if (keyword
== RID_NEW
)
8155 return cp_parser_new_expression (parser
);
8156 /* Similarly, for `delete'. */
8157 else if (keyword
== RID_DELETE
)
8158 return cp_parser_delete_expression (parser
);
8161 /* Look for a unary operator. */
8162 unary_operator
= cp_parser_unary_operator (token
);
8163 /* The `++' and `--' operators can be handled similarly, even though
8164 they are not technically unary-operators in the grammar. */
8165 if (unary_operator
== ERROR_MARK
)
8167 if (token
->type
== CPP_PLUS_PLUS
)
8168 unary_operator
= PREINCREMENT_EXPR
;
8169 else if (token
->type
== CPP_MINUS_MINUS
)
8170 unary_operator
= PREDECREMENT_EXPR
;
8171 /* Handle the GNU address-of-label extension. */
8172 else if (cp_parser_allow_gnu_extensions_p (parser
)
8173 && token
->type
== CPP_AND_AND
)
8177 location_t start_loc
= token
->location
;
8179 /* Consume the '&&' token. */
8180 cp_lexer_consume_token (parser
->lexer
);
8181 /* Look for the identifier. */
8182 location_t finish_loc
8183 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8184 identifier
= cp_parser_identifier (parser
);
8185 /* Construct a location of the form:
8188 with caret==start at the "&&", finish at the end of the label. */
8189 location_t combined_loc
8190 = make_location (start_loc
, start_loc
, finish_loc
);
8191 /* Create an expression representing the address. */
8192 expression
= finish_label_address_expr (identifier
, combined_loc
);
8193 if (cp_parser_non_integral_constant_expression (parser
,
8195 expression
= error_mark_node
;
8199 if (unary_operator
!= ERROR_MARK
)
8201 cp_expr cast_expression
;
8202 cp_expr expression
= error_mark_node
;
8203 non_integral_constant non_constant_p
= NIC_NONE
;
8204 location_t loc
= token
->location
;
8205 tsubst_flags_t complain
= complain_flags (decltype_p
);
8207 /* Consume the operator token. */
8208 token
= cp_lexer_consume_token (parser
->lexer
);
8209 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8211 /* Parse the cast-expression. */
8213 = cp_parser_cast_expression (parser
,
8214 unary_operator
== ADDR_EXPR
,
8220 OP_TOKEN CAST_EXPRESSION
8221 ^~~~~~~~~~~~~~~~~~~~~~~~~
8222 with start==caret at the operator token, and
8223 extending to the end of the cast_expression. */
8224 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8226 /* Now, build an appropriate representation. */
8227 switch (unary_operator
)
8230 non_constant_p
= NIC_STAR
;
8231 expression
= build_x_indirect_ref (loc
, cast_expression
,
8234 /* TODO: build_x_indirect_ref does not always honor the
8235 location, so ensure it is set. */
8236 expression
.set_location (loc
);
8240 non_constant_p
= NIC_ADDR
;
8243 expression
= build_x_unary_op (loc
, unary_operator
,
8246 /* TODO: build_x_unary_op does not always honor the location,
8247 so ensure it is set. */
8248 expression
.set_location (loc
);
8251 case PREINCREMENT_EXPR
:
8252 case PREDECREMENT_EXPR
:
8253 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8254 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8257 /* Immediately fold negation of a constant, unless the constant is 0
8258 (since -0 == 0) or it would overflow. */
8259 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8260 && CONSTANT_CLASS_P (cast_expression
)
8261 && !integer_zerop (cast_expression
)
8262 && !TREE_OVERFLOW (cast_expression
))
8264 tree folded
= fold_build1 (unary_operator
,
8265 TREE_TYPE (cast_expression
),
8267 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8269 expression
= cp_expr (folded
, loc
);
8274 case UNARY_PLUS_EXPR
:
8275 case TRUTH_NOT_EXPR
:
8276 expression
= finish_unary_op_expr (loc
, unary_operator
,
8277 cast_expression
, complain
);
8284 if (non_constant_p
!= NIC_NONE
8285 && cp_parser_non_integral_constant_expression (parser
,
8287 expression
= error_mark_node
;
8292 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8293 /*member_access_only_p=*/false,
8298 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8299 unary-operator, the corresponding tree code is returned. */
8301 static enum tree_code
8302 cp_parser_unary_operator (cp_token
* token
)
8304 switch (token
->type
)
8307 return INDIRECT_REF
;
8313 return UNARY_PLUS_EXPR
;
8319 return TRUTH_NOT_EXPR
;
8322 return BIT_NOT_EXPR
;
8329 /* Parse a new-expression.
8332 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8333 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8335 Returns a representation of the expression. */
8338 cp_parser_new_expression (cp_parser
* parser
)
8340 bool global_scope_p
;
8341 vec
<tree
, va_gc
> *placement
;
8343 vec
<tree
, va_gc
> *initializer
;
8344 tree nelts
= NULL_TREE
;
8347 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8349 /* Look for the optional `::' operator. */
8351 = (cp_parser_global_scope_opt (parser
,
8352 /*current_scope_valid_p=*/false)
8354 /* Look for the `new' operator. */
8355 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8356 /* There's no easy way to tell a new-placement from the
8357 `( type-id )' construct. */
8358 cp_parser_parse_tentatively (parser
);
8359 /* Look for a new-placement. */
8360 placement
= cp_parser_new_placement (parser
);
8361 /* If that didn't work out, there's no new-placement. */
8362 if (!cp_parser_parse_definitely (parser
))
8364 if (placement
!= NULL
)
8365 release_tree_vector (placement
);
8369 /* If the next token is a `(', then we have a parenthesized
8371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8374 const char *saved_message
= parser
->type_definition_forbidden_message
;
8376 /* Consume the `('. */
8377 matching_parens parens
;
8378 parens
.consume_open (parser
);
8380 /* Parse the type-id. */
8381 parser
->type_definition_forbidden_message
8382 = G_("types may not be defined in a new-expression");
8384 type_id_in_expr_sentinel
s (parser
);
8385 type
= cp_parser_type_id (parser
);
8387 parser
->type_definition_forbidden_message
= saved_message
;
8389 /* Look for the closing `)'. */
8390 parens
.require_close (parser
);
8391 token
= cp_lexer_peek_token (parser
->lexer
);
8392 /* There should not be a direct-new-declarator in this production,
8393 but GCC used to allowed this, so we check and emit a sensible error
8394 message for this case. */
8395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8397 error_at (token
->location
,
8398 "array bound forbidden after parenthesized type-id");
8399 inform (token
->location
,
8400 "try removing the parentheses around the type-id");
8401 cp_parser_direct_new_declarator (parser
);
8404 /* Otherwise, there must be a new-type-id. */
8406 type
= cp_parser_new_type_id (parser
, &nelts
);
8408 /* If the next token is a `(' or '{', then we have a new-initializer. */
8409 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8410 if (token
->type
== CPP_OPEN_PAREN
8411 || token
->type
== CPP_OPEN_BRACE
)
8412 initializer
= cp_parser_new_initializer (parser
);
8416 /* A new-expression may not appear in an integral constant
8418 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8419 ret
= error_mark_node
;
8420 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8421 of a new-type-id or type-id of a new-expression, the new-expression shall
8422 contain a new-initializer of the form ( assignment-expression )".
8423 Additionally, consistently with the spirit of DR 1467, we want to accept
8424 'new auto { 2 }' too. */
8425 else if ((ret
= type_uses_auto (type
))
8426 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8427 && (vec_safe_length (initializer
) != 1
8428 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8429 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8431 error_at (token
->location
,
8432 "initialization of new-expression for type %<auto%> "
8433 "requires exactly one element");
8434 ret
= error_mark_node
;
8438 /* Construct a location e.g.:
8441 with caret == start at the start of the "new" token, and the end
8442 at the end of the final token we consumed. */
8443 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8444 location_t end_loc
= get_finish (end_tok
->location
);
8445 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8447 /* Create a representation of the new-expression. */
8448 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8449 tf_warning_or_error
);
8450 protected_set_expr_location (ret
, combined_loc
);
8453 if (placement
!= NULL
)
8454 release_tree_vector (placement
);
8455 if (initializer
!= NULL
)
8456 release_tree_vector (initializer
);
8461 /* Parse a new-placement.
8466 Returns the same representation as for an expression-list. */
8468 static vec
<tree
, va_gc
> *
8469 cp_parser_new_placement (cp_parser
* parser
)
8471 vec
<tree
, va_gc
> *expression_list
;
8473 /* Parse the expression-list. */
8474 expression_list
= (cp_parser_parenthesized_expression_list
8475 (parser
, non_attr
, /*cast_p=*/false,
8476 /*allow_expansion_p=*/true,
8477 /*non_constant_p=*/NULL
));
8479 if (expression_list
&& expression_list
->is_empty ())
8480 error ("expected expression-list or type-id");
8482 return expression_list
;
8485 /* Parse a new-type-id.
8488 type-specifier-seq new-declarator [opt]
8490 Returns the TYPE allocated. If the new-type-id indicates an array
8491 type, *NELTS is set to the number of elements in the last array
8492 bound; the TYPE will not include the last array bound. */
8495 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8497 cp_decl_specifier_seq type_specifier_seq
;
8498 cp_declarator
*new_declarator
;
8499 cp_declarator
*declarator
;
8500 cp_declarator
*outer_declarator
;
8501 const char *saved_message
;
8503 /* The type-specifier sequence must not contain type definitions.
8504 (It cannot contain declarations of new types either, but if they
8505 are not definitions we will catch that because they are not
8507 saved_message
= parser
->type_definition_forbidden_message
;
8508 parser
->type_definition_forbidden_message
8509 = G_("types may not be defined in a new-type-id");
8510 /* Parse the type-specifier-seq. */
8511 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8512 /*is_trailing_return=*/false,
8513 &type_specifier_seq
);
8514 /* Restore the old message. */
8515 parser
->type_definition_forbidden_message
= saved_message
;
8517 if (type_specifier_seq
.type
== error_mark_node
)
8518 return error_mark_node
;
8520 /* Parse the new-declarator. */
8521 new_declarator
= cp_parser_new_declarator_opt (parser
);
8523 /* Determine the number of elements in the last array dimension, if
8526 /* Skip down to the last array dimension. */
8527 declarator
= new_declarator
;
8528 outer_declarator
= NULL
;
8529 while (declarator
&& (declarator
->kind
== cdk_pointer
8530 || declarator
->kind
== cdk_ptrmem
))
8532 outer_declarator
= declarator
;
8533 declarator
= declarator
->declarator
;
8536 && declarator
->kind
== cdk_array
8537 && declarator
->declarator
8538 && declarator
->declarator
->kind
== cdk_array
)
8540 outer_declarator
= declarator
;
8541 declarator
= declarator
->declarator
;
8544 if (declarator
&& declarator
->kind
== cdk_array
)
8546 *nelts
= declarator
->u
.array
.bounds
;
8547 if (*nelts
== error_mark_node
)
8548 *nelts
= integer_one_node
;
8550 if (outer_declarator
)
8551 outer_declarator
->declarator
= declarator
->declarator
;
8553 new_declarator
= NULL
;
8556 return groktypename (&type_specifier_seq
, new_declarator
, false);
8559 /* Parse an (optional) new-declarator.
8562 ptr-operator new-declarator [opt]
8563 direct-new-declarator
8565 Returns the declarator. */
8567 static cp_declarator
*
8568 cp_parser_new_declarator_opt (cp_parser
* parser
)
8570 enum tree_code code
;
8571 tree type
, std_attributes
= NULL_TREE
;
8572 cp_cv_quals cv_quals
;
8574 /* We don't know if there's a ptr-operator next, or not. */
8575 cp_parser_parse_tentatively (parser
);
8576 /* Look for a ptr-operator. */
8577 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8578 /* If that worked, look for more new-declarators. */
8579 if (cp_parser_parse_definitely (parser
))
8581 cp_declarator
*declarator
;
8583 /* Parse another optional declarator. */
8584 declarator
= cp_parser_new_declarator_opt (parser
);
8586 declarator
= cp_parser_make_indirect_declarator
8587 (code
, type
, cv_quals
, declarator
, std_attributes
);
8592 /* If the next token is a `[', there is a direct-new-declarator. */
8593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8594 return cp_parser_direct_new_declarator (parser
);
8599 /* Parse a direct-new-declarator.
8601 direct-new-declarator:
8603 direct-new-declarator [constant-expression]
8607 static cp_declarator
*
8608 cp_parser_direct_new_declarator (cp_parser
* parser
)
8610 cp_declarator
*declarator
= NULL
;
8617 /* Look for the opening `['. */
8618 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8620 token
= cp_lexer_peek_token (parser
->lexer
);
8621 expression
= cp_parser_expression (parser
);
8622 /* The standard requires that the expression have integral
8623 type. DR 74 adds enumeration types. We believe that the
8624 real intent is that these expressions be handled like the
8625 expression in a `switch' condition, which also allows
8626 classes with a single conversion to integral or
8627 enumeration type. */
8628 if (!processing_template_decl
)
8631 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8636 error_at (token
->location
,
8637 "expression in new-declarator must have integral "
8638 "or enumeration type");
8639 expression
= error_mark_node
;
8643 /* Look for the closing `]'. */
8644 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8646 /* Add this bound to the declarator. */
8647 declarator
= make_array_declarator (declarator
, expression
);
8649 /* If the next token is not a `[', then there are no more
8651 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8658 /* Parse a new-initializer.
8661 ( expression-list [opt] )
8664 Returns a representation of the expression-list. */
8666 static vec
<tree
, va_gc
> *
8667 cp_parser_new_initializer (cp_parser
* parser
)
8669 vec
<tree
, va_gc
> *expression_list
;
8671 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8674 bool expr_non_constant_p
;
8675 cp_lexer_set_source_position (parser
->lexer
);
8676 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8677 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8678 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8679 expression_list
= make_tree_vector_single (t
);
8682 expression_list
= (cp_parser_parenthesized_expression_list
8683 (parser
, non_attr
, /*cast_p=*/false,
8684 /*allow_expansion_p=*/true,
8685 /*non_constant_p=*/NULL
));
8687 return expression_list
;
8690 /* Parse a delete-expression.
8693 :: [opt] delete cast-expression
8694 :: [opt] delete [ ] cast-expression
8696 Returns a representation of the expression. */
8699 cp_parser_delete_expression (cp_parser
* parser
)
8701 bool global_scope_p
;
8705 /* Look for the optional `::' operator. */
8707 = (cp_parser_global_scope_opt (parser
,
8708 /*current_scope_valid_p=*/false)
8710 /* Look for the `delete' keyword. */
8711 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8712 /* See if the array syntax is in use. */
8713 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8715 /* Consume the `[' token. */
8716 cp_lexer_consume_token (parser
->lexer
);
8717 /* Look for the `]' token. */
8718 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8719 /* Remember that this is the `[]' construct. */
8725 /* Parse the cast-expression. */
8726 expression
= cp_parser_simple_cast_expression (parser
);
8728 /* A delete-expression may not appear in an integral constant
8730 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8731 return error_mark_node
;
8733 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8734 tf_warning_or_error
);
8737 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8738 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8742 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8744 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8745 switch (token
->type
)
8751 case CPP_CLOSE_SQUARE
:
8752 case CPP_CLOSE_PAREN
:
8753 case CPP_CLOSE_BRACE
:
8754 case CPP_OPEN_BRACE
:
8758 case CPP_DEREF_STAR
:
8766 case CPP_GREATER_EQ
:
8787 case CPP_OPEN_PAREN
:
8788 /* In ((type ()) () the last () isn't a valid cast-expression,
8789 so the whole must be parsed as postfix-expression. */
8790 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8793 case CPP_OPEN_SQUARE
:
8794 /* '[' may start a primary-expression in obj-c++ and in C++11,
8795 as a lambda-expression, eg, '(void)[]{}'. */
8796 if (cxx_dialect
>= cxx11
)
8798 return c_dialect_objc ();
8801 case CPP_MINUS_MINUS
:
8802 /* '++' and '--' may or may not start a cast-expression:
8804 struct T { void operator++(int); };
8805 void f() { (T())++; }
8818 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8819 in the order: const_cast, static_cast, reinterpret_cast.
8821 Don't suggest dynamic_cast.
8823 Return the first legal cast kind found, or NULL otherwise. */
8826 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8830 /* Reuse the parser logic by attempting to build the various kinds of
8831 cast, with "complain" disabled.
8832 Identify the first such cast that is valid. */
8834 /* Don't attempt to run such logic within template processing. */
8835 if (processing_template_decl
)
8838 /* First try const_cast. */
8839 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8840 if (trial
!= error_mark_node
)
8841 return "const_cast";
8843 /* If that fails, try static_cast. */
8844 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8845 if (trial
!= error_mark_node
)
8846 return "static_cast";
8848 /* Finally, try reinterpret_cast. */
8849 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8850 if (trial
!= error_mark_node
)
8851 return "reinterpret_cast";
8853 /* No such cast possible. */
8857 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8858 suggesting how to convert a C-style cast of the form:
8862 to a C++-style cast.
8864 The primary range of RICHLOC is asssumed to be that of the original
8865 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8866 of the parens in the C-style cast. */
8869 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8870 location_t close_paren_loc
, tree orig_expr
,
8873 /* This function is non-trivial, so bail out now if the warning isn't
8874 going to be emitted. */
8875 if (!warn_old_style_cast
)
8878 /* Try to find a legal C++ cast, trying them in order:
8879 const_cast, static_cast, reinterpret_cast. */
8880 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8881 if (!cast_suggestion
)
8884 /* Replace the open paren with "CAST_SUGGESTION<". */
8886 pp_printf (&pp
, "%s<", cast_suggestion
);
8887 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8889 /* Replace the close paren with "> (". */
8890 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8892 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8893 rich_loc
->add_fixit_insert_after (")");
8897 /* Parse a cast-expression.
8901 ( type-id ) cast-expression
8903 ADDRESS_P is true iff the unary-expression is appearing as the
8904 operand of the `&' operator. CAST_P is true if this expression is
8905 the target of a cast.
8907 Returns a representation of the expression. */
8910 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8911 bool decltype_p
, cp_id_kind
* pidk
)
8913 /* If it's a `(', then we might be looking at a cast. */
8914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8916 tree type
= NULL_TREE
;
8917 cp_expr
expr (NULL_TREE
);
8918 int cast_expression
= 0;
8919 const char *saved_message
;
8921 /* There's no way to know yet whether or not this is a cast.
8922 For example, `(int (3))' is a unary-expression, while `(int)
8923 3' is a cast. So, we resort to parsing tentatively. */
8924 cp_parser_parse_tentatively (parser
);
8925 /* Types may not be defined in a cast. */
8926 saved_message
= parser
->type_definition_forbidden_message
;
8927 parser
->type_definition_forbidden_message
8928 = G_("types may not be defined in casts");
8929 /* Consume the `('. */
8930 matching_parens parens
;
8931 cp_token
*open_paren
= parens
.consume_open (parser
);
8932 location_t open_paren_loc
= open_paren
->location
;
8933 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8935 /* A very tricky bit is that `(struct S) { 3 }' is a
8936 compound-literal (which we permit in C++ as an extension).
8937 But, that construct is not a cast-expression -- it is a
8938 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8939 is legal; if the compound-literal were a cast-expression,
8940 you'd need an extra set of parentheses.) But, if we parse
8941 the type-id, and it happens to be a class-specifier, then we
8942 will commit to the parse at that point, because we cannot
8943 undo the action that is done when creating a new class. So,
8944 then we cannot back up and do a postfix-expression.
8946 Another tricky case is the following (c++/29234):
8948 struct S { void operator () (); };
8955 As a type-id we parse the parenthesized S()() as a function
8956 returning a function, groktypename complains and we cannot
8957 back up in this case either.
8959 Therefore, we scan ahead to the closing `)', and check to see
8960 if the tokens after the `)' can start a cast-expression. Otherwise
8961 we are dealing with an unary-expression, a postfix-expression
8964 Yet another tricky case, in C++11, is the following (c++/54891):
8968 The issue is that usually, besides the case of lambda-expressions,
8969 the parenthesized type-id cannot be followed by '[', and, eg, we
8970 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8971 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8972 we don't commit, we try a cast-expression, then an unary-expression.
8974 Save tokens so that we can put them back. */
8975 cp_lexer_save_tokens (parser
->lexer
);
8977 /* We may be looking at a cast-expression. */
8978 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8979 /*consume_paren=*/true))
8981 = cp_parser_tokens_start_cast_expression (parser
);
8983 /* Roll back the tokens we skipped. */
8984 cp_lexer_rollback_tokens (parser
->lexer
);
8985 /* If we aren't looking at a cast-expression, simulate an error so
8986 that the call to cp_parser_error_occurred below returns true. */
8987 if (!cast_expression
)
8988 cp_parser_simulate_error (parser
);
8991 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8992 parser
->in_type_id_in_expr_p
= true;
8993 /* Look for the type-id. */
8994 type
= cp_parser_type_id (parser
);
8995 /* Look for the closing `)'. */
8996 cp_token
*close_paren
= parens
.require_close (parser
);
8998 close_paren_loc
= close_paren
->location
;
8999 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9002 /* Restore the saved message. */
9003 parser
->type_definition_forbidden_message
= saved_message
;
9005 /* At this point this can only be either a cast or a
9006 parenthesized ctor such as `(T ())' that looks like a cast to
9007 function returning T. */
9008 if (!cp_parser_error_occurred (parser
))
9010 /* Only commit if the cast-expression doesn't start with
9011 '++', '--', or '[' in C++11. */
9012 if (cast_expression
> 0)
9013 cp_parser_commit_to_topmost_tentative_parse (parser
);
9015 expr
= cp_parser_cast_expression (parser
,
9016 /*address_p=*/false,
9018 /*decltype_p=*/false,
9021 if (cp_parser_parse_definitely (parser
))
9023 /* Warn about old-style casts, if so requested. */
9024 if (warn_old_style_cast
9025 && !in_system_header_at (input_location
)
9026 && !VOID_TYPE_P (type
)
9027 && current_lang_name
!= lang_name_c
)
9029 gcc_rich_location
rich_loc (input_location
);
9030 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9032 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9033 "use of old-style cast to %q#T", type
);
9036 /* Only type conversions to integral or enumeration types
9037 can be used in constant-expressions. */
9038 if (!cast_valid_in_integral_constant_expression_p (type
)
9039 && cp_parser_non_integral_constant_expression (parser
,
9041 return error_mark_node
;
9043 /* Perform the cast. */
9047 with start==caret at the open paren, extending to the
9049 location_t cast_loc
= make_location (open_paren_loc
,
9051 expr
.get_finish ());
9052 expr
= build_c_cast (cast_loc
, type
, expr
);
9057 cp_parser_abort_tentative_parse (parser
);
9060 /* If we get here, then it's not a cast, so it must be a
9061 unary-expression. */
9062 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9063 cast_p
, decltype_p
);
9066 /* Parse a binary expression of the general form:
9070 pm-expression .* cast-expression
9071 pm-expression ->* cast-expression
9073 multiplicative-expression:
9075 multiplicative-expression * pm-expression
9076 multiplicative-expression / pm-expression
9077 multiplicative-expression % pm-expression
9079 additive-expression:
9080 multiplicative-expression
9081 additive-expression + multiplicative-expression
9082 additive-expression - multiplicative-expression
9086 shift-expression << additive-expression
9087 shift-expression >> additive-expression
9089 relational-expression:
9091 relational-expression < shift-expression
9092 relational-expression > shift-expression
9093 relational-expression <= shift-expression
9094 relational-expression >= shift-expression
9098 relational-expression:
9099 relational-expression <? shift-expression
9100 relational-expression >? shift-expression
9102 equality-expression:
9103 relational-expression
9104 equality-expression == relational-expression
9105 equality-expression != relational-expression
9109 and-expression & equality-expression
9111 exclusive-or-expression:
9113 exclusive-or-expression ^ and-expression
9115 inclusive-or-expression:
9116 exclusive-or-expression
9117 inclusive-or-expression | exclusive-or-expression
9119 logical-and-expression:
9120 inclusive-or-expression
9121 logical-and-expression && inclusive-or-expression
9123 logical-or-expression:
9124 logical-and-expression
9125 logical-or-expression || logical-and-expression
9127 All these are implemented with a single function like:
9130 simple-cast-expression
9131 binary-expression <token> binary-expression
9133 CAST_P is true if this expression is the target of a cast.
9135 The binops_by_token map is used to get the tree codes for each <token> type.
9136 binary-expressions are associated according to a precedence table. */
9138 #define TOKEN_PRECEDENCE(token) \
9139 (((token->type == CPP_GREATER \
9140 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9141 && !parser->greater_than_is_operator_p) \
9142 ? PREC_NOT_OPERATOR \
9143 : binops_by_token[token->type].prec)
9146 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9147 bool no_toplevel_fold_p
,
9149 enum cp_parser_prec prec
,
9152 cp_parser_expression_stack stack
;
9153 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9154 cp_parser_expression_stack_entry current
;
9157 enum tree_code rhs_type
;
9158 enum cp_parser_prec new_prec
, lookahead_prec
;
9161 /* Parse the first expression. */
9162 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9163 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9164 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9165 cast_p
, decltype_p
, pidk
);
9166 current
.prec
= prec
;
9168 if (cp_parser_error_occurred (parser
))
9169 return error_mark_node
;
9173 /* Get an operator token. */
9174 token
= cp_lexer_peek_token (parser
->lexer
);
9176 if (warn_cxx11_compat
9177 && token
->type
== CPP_RSHIFT
9178 && !parser
->greater_than_is_operator_p
)
9180 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9181 "%<>>%> operator is treated"
9182 " as two right angle brackets in C++11"))
9183 inform (token
->location
,
9184 "suggest parentheses around %<>>%> expression");
9187 new_prec
= TOKEN_PRECEDENCE (token
);
9188 if (new_prec
!= PREC_NOT_OPERATOR
9189 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9190 /* This is a fold-expression; handle it later. */
9191 new_prec
= PREC_NOT_OPERATOR
;
9193 /* Popping an entry off the stack means we completed a subexpression:
9194 - either we found a token which is not an operator (`>' where it is not
9195 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9196 will happen repeatedly;
9197 - or, we found an operator which has lower priority. This is the case
9198 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9200 if (new_prec
<= current
.prec
)
9209 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9210 current
.loc
= token
->location
;
9212 /* We used the operator token. */
9213 cp_lexer_consume_token (parser
->lexer
);
9215 /* For "false && x" or "true || x", x will never be executed;
9216 disable warnings while evaluating it. */
9217 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9218 c_inhibit_evaluation_warnings
+=
9219 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9220 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9221 c_inhibit_evaluation_warnings
+=
9222 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9224 /* Extract another operand. It may be the RHS of this expression
9225 or the LHS of a new, higher priority expression. */
9226 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9227 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9228 rhs
= cp_parser_simple_cast_expression (parser
);
9230 /* Get another operator token. Look up its precedence to avoid
9231 building a useless (immediately popped) stack entry for common
9232 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9233 token
= cp_lexer_peek_token (parser
->lexer
);
9234 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9235 if (lookahead_prec
!= PREC_NOT_OPERATOR
9236 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9237 lookahead_prec
= PREC_NOT_OPERATOR
;
9238 if (lookahead_prec
> new_prec
)
9240 /* ... and prepare to parse the RHS of the new, higher priority
9241 expression. Since precedence levels on the stack are
9242 monotonically increasing, we do not have to care about
9247 current
.lhs_type
= rhs_type
;
9248 current
.prec
= new_prec
;
9249 new_prec
= lookahead_prec
;
9253 lookahead_prec
= new_prec
;
9254 /* If the stack is not empty, we have parsed into LHS the right side
9255 (`4' in the example above) of an expression we had suspended.
9256 We can use the information on the stack to recover the LHS (`3')
9257 from the stack together with the tree code (`MULT_EXPR'), and
9258 the precedence of the higher level subexpression
9259 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9260 which will be used to actually build the additive expression. */
9262 rhs_type
= current
.lhs_type
;
9267 /* Undo the disabling of warnings done above. */
9268 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9269 c_inhibit_evaluation_warnings
-=
9270 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9271 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9272 c_inhibit_evaluation_warnings
-=
9273 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9275 if (warn_logical_not_paren
9276 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9277 && current
.lhs_type
== TRUTH_NOT_EXPR
9278 /* Avoid warning for !!x == y. */
9279 && (TREE_CODE (current
.lhs
) != NE_EXPR
9280 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9281 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9282 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9283 /* Avoid warning for !b == y where b is boolean. */
9284 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9285 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9287 /* Avoid warning for !!b == y where b is boolean. */
9288 && (!DECL_P (current
.lhs
)
9289 || TREE_TYPE (current
.lhs
) == NULL_TREE
9290 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9291 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9292 current
.lhs
, maybe_constant_value (rhs
));
9296 location_t combined_loc
= make_location (current
.loc
,
9297 current
.lhs
.get_start (),
9300 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9301 ERROR_MARK for everything that is not a binary expression.
9302 This makes warn_about_parentheses miss some warnings that
9303 involve unary operators. For unary expressions we should
9304 pass the correct tree_code unless the unary expression was
9305 surrounded by parentheses.
9307 if (no_toplevel_fold_p
9308 && lookahead_prec
<= current
.prec
9311 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9312 current
.lhs
= error_mark_node
;
9316 = build_min (current
.tree_type
,
9317 TREE_CODE_CLASS (current
.tree_type
)
9319 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9320 current
.lhs
.get_value (), rhs
.get_value ());
9321 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9326 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9327 current
.lhs
, current
.lhs_type
,
9328 rhs
, rhs_type
, &overload
,
9329 complain_flags (decltype_p
));
9330 /* TODO: build_x_binary_op doesn't always honor the location. */
9331 current
.lhs
.set_location (combined_loc
);
9333 current
.lhs_type
= current
.tree_type
;
9335 /* If the binary operator required the use of an overloaded operator,
9336 then this expression cannot be an integral constant-expression.
9337 An overloaded operator can be used even if both operands are
9338 otherwise permissible in an integral constant-expression if at
9339 least one of the operands is of enumeration type. */
9342 && cp_parser_non_integral_constant_expression (parser
,
9344 return error_mark_node
;
9351 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9352 bool no_toplevel_fold_p
,
9353 enum cp_parser_prec prec
,
9356 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9357 /*decltype*/false, prec
, pidk
);
9360 /* Parse the `? expression : assignment-expression' part of a
9361 conditional-expression. The LOGICAL_OR_EXPR is the
9362 logical-or-expression that started the conditional-expression.
9363 Returns a representation of the entire conditional-expression.
9365 This routine is used by cp_parser_assignment_expression.
9367 ? expression : assignment-expression
9371 ? : assignment-expression */
9374 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9376 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9377 cp_expr assignment_expr
;
9378 struct cp_token
*token
;
9379 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9381 /* Consume the `?' token. */
9382 cp_lexer_consume_token (parser
->lexer
);
9383 token
= cp_lexer_peek_token (parser
->lexer
);
9384 if (cp_parser_allow_gnu_extensions_p (parser
)
9385 && token
->type
== CPP_COLON
)
9387 pedwarn (token
->location
, OPT_Wpedantic
,
9388 "ISO C++ does not allow ?: with omitted middle operand");
9389 /* Implicit true clause. */
9391 c_inhibit_evaluation_warnings
+=
9392 folded_logical_or_expr
== truthvalue_true_node
;
9393 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9397 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9398 parser
->colon_corrects_to_scope_p
= false;
9399 /* Parse the expression. */
9400 c_inhibit_evaluation_warnings
+=
9401 folded_logical_or_expr
== truthvalue_false_node
;
9402 expr
= cp_parser_expression (parser
);
9403 c_inhibit_evaluation_warnings
+=
9404 ((folded_logical_or_expr
== truthvalue_true_node
)
9405 - (folded_logical_or_expr
== truthvalue_false_node
));
9406 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9409 /* The next token should be a `:'. */
9410 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9411 /* Parse the assignment-expression. */
9412 assignment_expr
= cp_parser_assignment_expression (parser
);
9413 c_inhibit_evaluation_warnings
-=
9414 folded_logical_or_expr
== truthvalue_true_node
;
9417 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9418 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9419 with the caret at the "?", ranging from the start of
9420 the logical_or_expr to the end of the assignment_expr. */
9421 loc
= make_location (loc
,
9422 logical_or_expr
.get_start (),
9423 assignment_expr
.get_finish ());
9425 /* Build the conditional-expression. */
9426 return build_x_conditional_expr (loc
, logical_or_expr
,
9429 tf_warning_or_error
);
9432 /* Parse an assignment-expression.
9434 assignment-expression:
9435 conditional-expression
9436 logical-or-expression assignment-operator assignment_expression
9439 CAST_P is true if this expression is the target of a cast.
9440 DECLTYPE_P is true if this expression is the operand of decltype.
9442 Returns a representation for the expression. */
9445 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9446 bool cast_p
, bool decltype_p
)
9450 /* If the next token is the `throw' keyword, then we're looking at
9451 a throw-expression. */
9452 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9453 expr
= cp_parser_throw_expression (parser
);
9454 /* Otherwise, it must be that we are looking at a
9455 logical-or-expression. */
9458 /* Parse the binary expressions (logical-or-expression). */
9459 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9461 PREC_NOT_OPERATOR
, pidk
);
9462 /* If the next token is a `?' then we're actually looking at a
9463 conditional-expression. */
9464 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9465 return cp_parser_question_colon_clause (parser
, expr
);
9468 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9470 /* If it's an assignment-operator, we're using the second
9472 enum tree_code assignment_operator
9473 = cp_parser_assignment_operator_opt (parser
);
9474 if (assignment_operator
!= ERROR_MARK
)
9476 bool non_constant_p
;
9478 /* Parse the right-hand side of the assignment. */
9479 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9482 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9485 /* An assignment may not appear in a
9486 constant-expression. */
9487 if (cp_parser_non_integral_constant_expression (parser
,
9489 return error_mark_node
;
9490 /* Build the assignment expression. Its default
9494 is the location of the '=' token as the
9495 caret, ranging from the start of the lhs to the
9497 loc
= make_location (loc
,
9500 expr
= build_x_modify_expr (loc
, expr
,
9501 assignment_operator
,
9503 complain_flags (decltype_p
));
9504 /* TODO: build_x_modify_expr doesn't honor the location,
9505 so we must set it here. */
9506 expr
.set_location (loc
);
9514 /* Parse an (optional) assignment-operator.
9516 assignment-operator: one of
9517 = *= /= %= += -= >>= <<= &= ^= |=
9521 assignment-operator: one of
9524 If the next token is an assignment operator, the corresponding tree
9525 code is returned, and the token is consumed. For example, for
9526 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9527 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9528 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9529 operator, ERROR_MARK is returned. */
9531 static enum tree_code
9532 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9537 /* Peek at the next token. */
9538 token
= cp_lexer_peek_token (parser
->lexer
);
9540 switch (token
->type
)
9551 op
= TRUNC_DIV_EXPR
;
9555 op
= TRUNC_MOD_EXPR
;
9587 /* Nothing else is an assignment operator. */
9591 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9592 if (op
!= ERROR_MARK
9593 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9596 /* If it was an assignment operator, consume it. */
9597 if (op
!= ERROR_MARK
)
9598 cp_lexer_consume_token (parser
->lexer
);
9603 /* Parse an expression.
9606 assignment-expression
9607 expression , assignment-expression
9609 CAST_P is true if this expression is the target of a cast.
9610 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9611 except possibly parenthesized or on the RHS of a comma (N3276).
9613 Returns a representation of the expression. */
9616 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9617 bool cast_p
, bool decltype_p
)
9619 cp_expr expression
= NULL_TREE
;
9620 location_t loc
= UNKNOWN_LOCATION
;
9624 cp_expr assignment_expression
;
9626 /* Parse the next assignment-expression. */
9627 assignment_expression
9628 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9630 /* We don't create a temporary for a call that is the immediate operand
9631 of decltype or on the RHS of a comma. But when we see a comma, we
9632 need to create a temporary for a call on the LHS. */
9633 if (decltype_p
&& !processing_template_decl
9634 && TREE_CODE (assignment_expression
) == CALL_EXPR
9635 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9636 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9637 assignment_expression
9638 = build_cplus_new (TREE_TYPE (assignment_expression
),
9639 assignment_expression
, tf_warning_or_error
);
9641 /* If this is the first assignment-expression, we can just
9644 expression
= assignment_expression
;
9647 /* Create a location with caret at the comma, ranging
9648 from the start of the LHS to the end of the RHS. */
9649 loc
= make_location (loc
,
9650 expression
.get_start (),
9651 assignment_expression
.get_finish ());
9652 expression
= build_x_compound_expr (loc
, expression
,
9653 assignment_expression
,
9654 complain_flags (decltype_p
));
9655 expression
.set_location (loc
);
9657 /* If the next token is not a comma, or we're in a fold-expression, then
9658 we are done with the expression. */
9659 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9660 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9662 /* Consume the `,'. */
9663 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9664 cp_lexer_consume_token (parser
->lexer
);
9665 /* A comma operator cannot appear in a constant-expression. */
9666 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9667 expression
= error_mark_node
;
9673 /* Parse a constant-expression.
9675 constant-expression:
9676 conditional-expression
9678 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9679 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9680 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9681 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9682 only parse a conditional-expression, otherwise parse an
9683 assignment-expression. See below for rationale. */
9686 cp_parser_constant_expression (cp_parser
* parser
,
9687 bool allow_non_constant_p
,
9688 bool *non_constant_p
,
9691 bool saved_integral_constant_expression_p
;
9692 bool saved_allow_non_integral_constant_expression_p
;
9693 bool saved_non_integral_constant_expression_p
;
9696 /* It might seem that we could simply parse the
9697 conditional-expression, and then check to see if it were
9698 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9699 one that the compiler can figure out is constant, possibly after
9700 doing some simplifications or optimizations. The standard has a
9701 precise definition of constant-expression, and we must honor
9702 that, even though it is somewhat more restrictive.
9708 is not a legal declaration, because `(2, 3)' is not a
9709 constant-expression. The `,' operator is forbidden in a
9710 constant-expression. However, GCC's constant-folding machinery
9711 will fold this operation to an INTEGER_CST for `3'. */
9713 /* Save the old settings. */
9714 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9715 saved_allow_non_integral_constant_expression_p
9716 = parser
->allow_non_integral_constant_expression_p
;
9717 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9718 /* We are now parsing a constant-expression. */
9719 parser
->integral_constant_expression_p
= true;
9720 parser
->allow_non_integral_constant_expression_p
9721 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9722 parser
->non_integral_constant_expression_p
= false;
9723 /* Although the grammar says "conditional-expression", when not STRICT_P,
9724 we parse an "assignment-expression", which also permits
9725 "throw-expression" and the use of assignment operators. In the case
9726 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9727 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9728 actually essential that we look for an assignment-expression.
9729 For example, cp_parser_initializer_clauses uses this function to
9730 determine whether a particular assignment-expression is in fact
9734 /* Parse the binary expressions (logical-or-expression). */
9735 expression
= cp_parser_binary_expression (parser
, false, false, false,
9736 PREC_NOT_OPERATOR
, NULL
);
9737 /* If the next token is a `?' then we're actually looking at
9738 a conditional-expression; otherwise we're done. */
9739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9740 expression
= cp_parser_question_colon_clause (parser
, expression
);
9743 expression
= cp_parser_assignment_expression (parser
);
9744 /* Restore the old settings. */
9745 parser
->integral_constant_expression_p
9746 = saved_integral_constant_expression_p
;
9747 parser
->allow_non_integral_constant_expression_p
9748 = saved_allow_non_integral_constant_expression_p
;
9749 if (cxx_dialect
>= cxx11
)
9751 /* Require an rvalue constant expression here; that's what our
9752 callers expect. Reference constant expressions are handled
9753 separately in e.g. cp_parser_template_argument. */
9754 tree decay
= expression
;
9755 if (TREE_TYPE (expression
)
9756 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9757 decay
= build_address (expression
);
9758 bool is_const
= potential_rvalue_constant_expression (decay
);
9759 parser
->non_integral_constant_expression_p
= !is_const
;
9760 if (!is_const
&& !allow_non_constant_p
)
9761 require_potential_rvalue_constant_expression (decay
);
9763 if (allow_non_constant_p
)
9764 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9765 parser
->non_integral_constant_expression_p
9766 = saved_non_integral_constant_expression_p
;
9771 /* Parse __builtin_offsetof.
9773 offsetof-expression:
9774 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9776 offsetof-member-designator:
9778 | offsetof-member-designator "." id-expression
9779 | offsetof-member-designator "[" expression "]"
9780 | offsetof-member-designator "->" id-expression */
9783 cp_parser_builtin_offsetof (cp_parser
*parser
)
9785 int save_ice_p
, save_non_ice_p
;
9790 location_t finish_loc
;
9792 /* We're about to accept non-integral-constant things, but will
9793 definitely yield an integral constant expression. Save and
9794 restore these values around our local parsing. */
9795 save_ice_p
= parser
->integral_constant_expression_p
;
9796 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9798 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9800 /* Consume the "__builtin_offsetof" token. */
9801 cp_lexer_consume_token (parser
->lexer
);
9802 /* Consume the opening `('. */
9803 matching_parens parens
;
9804 parens
.require_open (parser
);
9805 /* Parse the type-id. */
9806 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9808 const char *saved_message
= parser
->type_definition_forbidden_message
;
9809 parser
->type_definition_forbidden_message
9810 = G_("types may not be defined within __builtin_offsetof");
9811 type
= cp_parser_type_id (parser
);
9812 parser
->type_definition_forbidden_message
= saved_message
;
9814 /* Look for the `,'. */
9815 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9816 token
= cp_lexer_peek_token (parser
->lexer
);
9818 /* Build the (type *)null that begins the traditional offsetof macro. */
9820 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9821 tf_warning_or_error
);
9823 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9824 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9825 true, &dummy
, token
->location
);
9828 token
= cp_lexer_peek_token (parser
->lexer
);
9829 switch (token
->type
)
9831 case CPP_OPEN_SQUARE
:
9832 /* offsetof-member-designator "[" expression "]" */
9833 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9838 /* offsetof-member-designator "->" identifier */
9839 expr
= grok_array_decl (token
->location
, expr
,
9840 integer_zero_node
, false);
9844 /* offsetof-member-designator "." identifier */
9845 cp_lexer_consume_token (parser
->lexer
);
9846 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9851 case CPP_CLOSE_PAREN
:
9852 /* Consume the ")" token. */
9853 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9854 cp_lexer_consume_token (parser
->lexer
);
9858 /* Error. We know the following require will fail, but
9859 that gives the proper error message. */
9860 parens
.require_close (parser
);
9861 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9862 expr
= error_mark_node
;
9868 /* Make a location of the form:
9869 __builtin_offsetof (struct s, f)
9870 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9871 with caret at the type-id, ranging from the start of the
9872 "_builtin_offsetof" token to the close paren. */
9873 loc
= make_location (loc
, start_loc
, finish_loc
);
9874 /* The result will be an INTEGER_CST, so we need to explicitly
9875 preserve the location. */
9876 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9879 parser
->integral_constant_expression_p
= save_ice_p
;
9880 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9882 expr
= expr
.maybe_add_location_wrapper ();
9886 /* Parse a trait expression.
9888 Returns a representation of the expression, the underlying type
9889 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9892 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9895 tree type1
, type2
= NULL_TREE
;
9896 bool binary
= false;
9897 bool variadic
= false;
9901 case RID_HAS_NOTHROW_ASSIGN
:
9902 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9904 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9905 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9907 case RID_HAS_NOTHROW_COPY
:
9908 kind
= CPTK_HAS_NOTHROW_COPY
;
9910 case RID_HAS_TRIVIAL_ASSIGN
:
9911 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9913 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9914 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9916 case RID_HAS_TRIVIAL_COPY
:
9917 kind
= CPTK_HAS_TRIVIAL_COPY
;
9919 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9920 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9922 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9923 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9925 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9926 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9928 case RID_IS_ABSTRACT
:
9929 kind
= CPTK_IS_ABSTRACT
;
9931 case RID_IS_AGGREGATE
:
9932 kind
= CPTK_IS_AGGREGATE
;
9934 case RID_IS_BASE_OF
:
9935 kind
= CPTK_IS_BASE_OF
;
9939 kind
= CPTK_IS_CLASS
;
9942 kind
= CPTK_IS_EMPTY
;
9945 kind
= CPTK_IS_ENUM
;
9948 kind
= CPTK_IS_FINAL
;
9950 case RID_IS_LITERAL_TYPE
:
9951 kind
= CPTK_IS_LITERAL_TYPE
;
9956 case RID_IS_POLYMORPHIC
:
9957 kind
= CPTK_IS_POLYMORPHIC
;
9959 case RID_IS_SAME_AS
:
9960 kind
= CPTK_IS_SAME_AS
;
9963 case RID_IS_STD_LAYOUT
:
9964 kind
= CPTK_IS_STD_LAYOUT
;
9966 case RID_IS_TRIVIAL
:
9967 kind
= CPTK_IS_TRIVIAL
;
9969 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9970 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9973 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9974 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9977 case RID_IS_TRIVIALLY_COPYABLE
:
9978 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9981 kind
= CPTK_IS_UNION
;
9983 case RID_UNDERLYING_TYPE
:
9984 kind
= CPTK_UNDERLYING_TYPE
;
9989 case RID_DIRECT_BASES
:
9990 kind
= CPTK_DIRECT_BASES
;
9992 case RID_IS_ASSIGNABLE
:
9993 kind
= CPTK_IS_ASSIGNABLE
;
9996 case RID_IS_CONSTRUCTIBLE
:
9997 kind
= CPTK_IS_CONSTRUCTIBLE
;
10001 gcc_unreachable ();
10004 /* Get location of initial token. */
10005 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10007 /* Consume the token. */
10008 cp_lexer_consume_token (parser
->lexer
);
10010 matching_parens parens
;
10011 parens
.require_open (parser
);
10014 type_id_in_expr_sentinel
s (parser
);
10015 type1
= cp_parser_type_id (parser
);
10018 if (type1
== error_mark_node
)
10019 return error_mark_node
;
10023 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10026 type_id_in_expr_sentinel
s (parser
);
10027 type2
= cp_parser_type_id (parser
);
10030 if (type2
== error_mark_node
)
10031 return error_mark_node
;
10035 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10037 cp_lexer_consume_token (parser
->lexer
);
10038 tree elt
= cp_parser_type_id (parser
);
10039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10041 cp_lexer_consume_token (parser
->lexer
);
10042 elt
= make_pack_expansion (elt
);
10044 if (elt
== error_mark_node
)
10045 return error_mark_node
;
10046 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10050 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10051 parens
.require_close (parser
);
10053 /* Construct a location of the form:
10054 __is_trivially_copyable(_Tp)
10055 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10056 with start == caret, finishing at the close-paren. */
10057 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10059 /* Complete the trait expression, which may mean either processing
10060 the trait expr now or saving it for template instantiation. */
10063 case CPTK_UNDERLYING_TYPE
:
10064 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10066 return cp_expr (finish_bases (type1
, false), trait_loc
);
10067 case CPTK_DIRECT_BASES
:
10068 return cp_expr (finish_bases (type1
, true), trait_loc
);
10070 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10074 /* Parse a lambda expression.
10077 lambda-introducer lambda-declarator [opt] compound-statement
10079 Returns a representation of the expression. */
10082 cp_parser_lambda_expression (cp_parser
* parser
)
10084 tree lambda_expr
= build_lambda_expr ();
10087 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10088 cp_token_position start
= 0;
10090 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10092 if (cp_unevaluated_operand
)
10094 if (!token
->error_reported
)
10096 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10097 "lambda-expression in unevaluated context");
10098 token
->error_reported
= true;
10102 else if (parser
->in_template_argument_list_p
)
10104 if (!token
->error_reported
)
10106 error_at (token
->location
, "lambda-expression in template-argument");
10107 token
->error_reported
= true;
10112 /* We may be in the middle of deferred access check. Disable
10114 push_deferring_access_checks (dk_no_deferred
);
10116 cp_parser_lambda_introducer (parser
, lambda_expr
);
10118 type
= begin_lambda_type (lambda_expr
);
10119 if (type
== error_mark_node
)
10120 return error_mark_node
;
10122 record_lambda_scope (lambda_expr
);
10124 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10125 determine_visibility (TYPE_NAME (type
));
10127 /* Now that we've started the type, add the capture fields for any
10128 explicit captures. */
10129 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10132 /* Inside the class, surrounding template-parameter-lists do not apply. */
10133 unsigned int saved_num_template_parameter_lists
10134 = parser
->num_template_parameter_lists
;
10135 unsigned char in_statement
= parser
->in_statement
;
10136 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10137 bool fully_implicit_function_template_p
10138 = parser
->fully_implicit_function_template_p
;
10139 tree implicit_template_parms
= parser
->implicit_template_parms
;
10140 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10141 bool auto_is_implicit_function_template_parm_p
10142 = parser
->auto_is_implicit_function_template_parm_p
;
10144 parser
->num_template_parameter_lists
= 0;
10145 parser
->in_statement
= 0;
10146 parser
->in_switch_statement_p
= false;
10147 parser
->fully_implicit_function_template_p
= false;
10148 parser
->implicit_template_parms
= 0;
10149 parser
->implicit_template_scope
= 0;
10150 parser
->auto_is_implicit_function_template_parm_p
= false;
10152 /* By virtue of defining a local class, a lambda expression has access to
10153 the private variables of enclosing classes. */
10155 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10157 if (ok
&& cp_parser_error_occurred (parser
))
10162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10163 && cp_parser_start_tentative_firewall (parser
))
10165 cp_parser_lambda_body (parser
, lambda_expr
);
10167 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10169 if (cp_parser_skip_to_closing_brace (parser
))
10170 cp_lexer_consume_token (parser
->lexer
);
10173 /* The capture list was built up in reverse order; fix that now. */
10174 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10175 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10178 maybe_add_lambda_conv_op (type
);
10180 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10182 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10183 parser
->in_statement
= in_statement
;
10184 parser
->in_switch_statement_p
= in_switch_statement_p
;
10185 parser
->fully_implicit_function_template_p
10186 = fully_implicit_function_template_p
;
10187 parser
->implicit_template_parms
= implicit_template_parms
;
10188 parser
->implicit_template_scope
= implicit_template_scope
;
10189 parser
->auto_is_implicit_function_template_parm_p
10190 = auto_is_implicit_function_template_parm_p
;
10193 /* This field is only used during parsing of the lambda. */
10194 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10196 /* This lambda shouldn't have any proxies left at this point. */
10197 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10198 /* And now that we're done, push proxies for an enclosing lambda. */
10199 insert_pending_capture_proxies ();
10201 /* Update the lambda expression to a range. */
10202 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10203 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10205 end_tok
->location
);
10208 lambda_expr
= build_lambda_object (lambda_expr
);
10210 lambda_expr
= error_mark_node
;
10212 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10214 pop_deferring_access_checks ();
10216 return lambda_expr
;
10219 /* Parse the beginning of a lambda expression.
10222 [ lambda-capture [opt] ]
10224 LAMBDA_EXPR is the current representation of the lambda expression. */
10227 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10229 /* Need commas after the first capture. */
10232 /* Eat the leading `['. */
10233 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10235 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10236 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10237 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10238 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10239 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10240 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10242 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10244 cp_lexer_consume_token (parser
->lexer
);
10248 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10250 cp_token
* capture_token
;
10252 tree capture_init_expr
;
10253 cp_id_kind idk
= CP_ID_KIND_NONE
;
10254 bool explicit_init_p
= false;
10256 enum capture_kind_type
10261 enum capture_kind_type capture_kind
= BY_COPY
;
10263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10265 error ("expected end of capture-list");
10272 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10274 /* Possibly capture `this'. */
10275 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10277 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10278 if (cxx_dialect
< cxx2a
10279 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10280 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10281 "with by-copy capture default");
10282 cp_lexer_consume_token (parser
->lexer
);
10283 add_capture (lambda_expr
,
10284 /*id=*/this_identifier
,
10285 /*initializer=*/finish_this_expr (),
10286 /*by_reference_p=*/true,
10291 /* Possibly capture `*this'. */
10292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10293 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10295 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10296 if (cxx_dialect
< cxx17
)
10297 pedwarn (loc
, 0, "%<*this%> capture only available with "
10298 "-std=c++17 or -std=gnu++17");
10299 cp_lexer_consume_token (parser
->lexer
);
10300 cp_lexer_consume_token (parser
->lexer
);
10301 add_capture (lambda_expr
,
10302 /*id=*/this_identifier
,
10303 /*initializer=*/finish_this_expr (),
10304 /*by_reference_p=*/false,
10309 /* Remember whether we want to capture as a reference or not. */
10310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10312 capture_kind
= BY_REFERENCE
;
10313 cp_lexer_consume_token (parser
->lexer
);
10316 /* Get the identifier. */
10317 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10318 capture_id
= cp_parser_identifier (parser
);
10320 if (capture_id
== error_mark_node
)
10321 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10322 delimiters, but I modified this to stop on unnested ']' as well. It
10323 was already changed to stop on unnested '}', so the
10324 "closing_parenthesis" name is no more misleading with my change. */
10326 cp_parser_skip_to_closing_parenthesis (parser
,
10327 /*recovering=*/true,
10329 /*consume_paren=*/true);
10333 /* Find the initializer for this capture. */
10334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10335 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10336 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10338 bool direct
, non_constant
;
10339 /* An explicit initializer exists. */
10340 if (cxx_dialect
< cxx14
)
10341 pedwarn (input_location
, 0,
10342 "lambda capture initializers "
10343 "only available with -std=c++14 or -std=gnu++14");
10344 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10345 &non_constant
, true);
10346 explicit_init_p
= true;
10347 if (capture_init_expr
== NULL_TREE
)
10349 error ("empty initializer for lambda init-capture");
10350 capture_init_expr
= error_mark_node
;
10355 const char* error_msg
;
10357 /* Turn the identifier into an id-expression. */
10359 = cp_parser_lookup_name_simple (parser
, capture_id
,
10360 capture_token
->location
);
10362 if (capture_init_expr
== error_mark_node
)
10364 unqualified_name_lookup_error (capture_id
);
10367 else if (!VAR_P (capture_init_expr
)
10368 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10370 error_at (capture_token
->location
,
10371 "capture of non-variable %qE",
10372 capture_init_expr
);
10373 if (DECL_P (capture_init_expr
))
10374 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10375 "%q#D declared here", capture_init_expr
);
10378 if (VAR_P (capture_init_expr
)
10379 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10381 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10382 "%qD with non-automatic storage duration",
10383 capture_init_expr
))
10384 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10385 "%q#D declared here", capture_init_expr
);
10390 = finish_id_expression
10395 /*integral_constant_expression_p=*/false,
10396 /*allow_non_integral_constant_expression_p=*/false,
10397 /*non_integral_constant_expression_p=*/NULL
,
10398 /*template_p=*/false,
10400 /*address_p=*/false,
10401 /*template_arg_p=*/false,
10403 capture_token
->location
);
10405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10407 cp_lexer_consume_token (parser
->lexer
);
10408 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10412 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10413 && !explicit_init_p
)
10415 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10416 && capture_kind
== BY_COPY
)
10417 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10418 "of %qD redundant with by-copy capture default",
10420 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10421 && capture_kind
== BY_REFERENCE
)
10422 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10423 "capture of %qD redundant with by-reference capture "
10424 "default", capture_id
);
10427 add_capture (lambda_expr
,
10430 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10433 /* If there is any qualification still in effect, clear it
10434 now; we will be starting fresh with the next capture. */
10435 parser
->scope
= NULL_TREE
;
10436 parser
->qualifying_scope
= NULL_TREE
;
10437 parser
->object_scope
= NULL_TREE
;
10440 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10443 /* Parse the (optional) middle of a lambda expression.
10446 < template-parameter-list [opt] >
10447 ( parameter-declaration-clause [opt] )
10448 attribute-specifier [opt]
10449 decl-specifier-seq [opt]
10450 exception-specification [opt]
10451 lambda-return-type-clause [opt]
10453 LAMBDA_EXPR is the current representation of the lambda expression. */
10456 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10458 /* 5.1.1.4 of the standard says:
10459 If a lambda-expression does not include a lambda-declarator, it is as if
10460 the lambda-declarator were ().
10461 This means an empty parameter list, no attributes, and no exception
10463 tree param_list
= void_list_node
;
10464 tree attributes
= NULL_TREE
;
10465 tree exception_spec
= NULL_TREE
;
10466 tree template_param_list
= NULL_TREE
;
10467 tree tx_qual
= NULL_TREE
;
10468 tree return_type
= NULL_TREE
;
10469 cp_decl_specifier_seq lambda_specs
;
10470 clear_decl_specs (&lambda_specs
);
10472 /* The template-parameter-list is optional, but must begin with
10473 an opening angle if present. */
10474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10476 if (cxx_dialect
< cxx14
)
10477 pedwarn (parser
->lexer
->next_token
->location
, 0,
10478 "lambda templates are only available with "
10479 "-std=c++14 or -std=gnu++14");
10480 else if (cxx_dialect
< cxx2a
)
10481 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10482 "lambda templates are only available with "
10483 "-std=c++2a or -std=gnu++2a");
10485 cp_lexer_consume_token (parser
->lexer
);
10487 template_param_list
= cp_parser_template_parameter_list (parser
);
10489 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10491 /* We just processed one more parameter list. */
10492 ++parser
->num_template_parameter_lists
;
10495 /* The parameter-declaration-clause is optional (unless
10496 template-parameter-list was given), but must begin with an
10497 opening parenthesis if present. */
10498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10500 matching_parens parens
;
10501 parens
.consume_open (parser
);
10503 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10505 /* Parse parameters. */
10506 param_list
= cp_parser_parameter_declaration_clause (parser
);
10508 /* Default arguments shall not be specified in the
10509 parameter-declaration-clause of a lambda-declarator. */
10510 if (cxx_dialect
< cxx14
)
10511 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10512 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10513 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10514 "default argument specified for lambda parameter");
10516 parens
.require_close (parser
);
10518 attributes
= cp_parser_attributes_opt (parser
);
10520 /* In the decl-specifier-seq of the lambda-declarator, each
10521 decl-specifier shall either be mutable or constexpr. */
10522 int declares_class_or_enum
;
10523 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10524 cp_parser_decl_specifier_seq (parser
,
10525 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10526 &lambda_specs
, &declares_class_or_enum
);
10527 if (lambda_specs
.storage_class
== sc_mutable
)
10529 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10530 if (lambda_specs
.conflicting_specifiers_p
)
10531 error_at (lambda_specs
.locations
[ds_storage_class
],
10532 "duplicate %<mutable%>");
10535 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10537 /* Parse optional exception specification. */
10538 exception_spec
= cp_parser_exception_specification_opt (parser
);
10540 /* Parse optional trailing return type. */
10541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10543 cp_lexer_consume_token (parser
->lexer
);
10544 return_type
= cp_parser_trailing_type_id (parser
);
10547 /* The function parameters must be in scope all the way until after the
10548 trailing-return-type in case of decltype. */
10549 pop_bindings_and_leave_scope ();
10551 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10552 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10554 /* Create the function call operator.
10556 Messing with declarators like this is no uglier than building up the
10557 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10560 cp_decl_specifier_seq return_type_specs
;
10561 cp_declarator
* declarator
;
10566 clear_decl_specs (&return_type_specs
);
10567 return_type_specs
.type
= make_auto ();
10569 if (lambda_specs
.locations
[ds_constexpr
])
10571 if (cxx_dialect
>= cxx17
)
10572 return_type_specs
.locations
[ds_constexpr
]
10573 = lambda_specs
.locations
[ds_constexpr
];
10575 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10576 "lambda only available with -std=c++17 or -std=gnu++17");
10579 p
= obstack_alloc (&declarator_obstack
, 0);
10581 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10583 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10584 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10585 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10586 VIRT_SPEC_UNSPECIFIED
,
10590 /*late_return_type=*/NULL_TREE
,
10591 /*requires_clause*/NULL_TREE
);
10592 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10594 declarator
->u
.function
.late_return_type
= return_type
;
10596 fco
= grokmethod (&return_type_specs
,
10599 if (fco
!= error_mark_node
)
10601 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10602 DECL_ARTIFICIAL (fco
) = 1;
10603 /* Give the object parameter a different name. */
10604 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10606 if (template_param_list
)
10608 fco
= finish_member_template_decl (fco
);
10609 finish_template_decl (template_param_list
);
10610 --parser
->num_template_parameter_lists
;
10612 else if (parser
->fully_implicit_function_template_p
)
10613 fco
= finish_fully_implicit_template (parser
, fco
);
10615 finish_member_declaration (fco
);
10617 obstack_free (&declarator_obstack
, p
);
10619 return (fco
!= error_mark_node
);
10623 /* Parse the body of a lambda expression, which is simply
10627 but which requires special handling.
10628 LAMBDA_EXPR is the current representation of the lambda expression. */
10631 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10633 bool nested
= (current_function_decl
!= NULL_TREE
);
10634 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10635 bool in_function_body
= parser
->in_function_body
;
10638 push_function_context ();
10640 /* Still increment function_depth so that we don't GC in the
10641 middle of an expression. */
10644 vec
<tree
> omp_privatization_save
;
10645 save_omp_privatization_clauses (omp_privatization_save
);
10646 /* Clear this in case we're in the middle of a default argument. */
10647 parser
->local_variables_forbidden_p
= false;
10648 parser
->in_function_body
= true;
10651 local_specialization_stack
s (lss_copy
);
10652 tree fco
= lambda_function (lambda_expr
);
10653 tree body
= start_lambda_function (fco
, lambda_expr
);
10654 matching_braces braces
;
10656 if (braces
.require_open (parser
))
10658 tree compound_stmt
= begin_compound_stmt (0);
10660 /* Originally C++11 required us to peek for 'return expr'; and
10661 process it specially here to deduce the return type. N3638
10662 removed the need for that. */
10664 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10665 cp_parser_label_declaration (parser
);
10666 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10667 braces
.require_close (parser
);
10669 finish_compound_stmt (compound_stmt
);
10672 finish_lambda_function (body
);
10675 restore_omp_privatization_clauses (omp_privatization_save
);
10676 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10677 parser
->in_function_body
= in_function_body
;
10679 pop_function_context();
10684 /* Statements [gram.stmt.stmt] */
10686 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10689 add_debug_begin_stmt (location_t loc
)
10691 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10693 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10694 /* A concept is never expanded normally. */
10697 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10698 SET_EXPR_LOCATION (stmt
, loc
);
10702 /* Parse a statement.
10706 expression-statement
10708 selection-statement
10709 iteration-statement
10711 declaration-statement
10718 attribute-specifier-seq (opt) expression-statement
10719 attribute-specifier-seq (opt) compound-statement
10720 attribute-specifier-seq (opt) selection-statement
10721 attribute-specifier-seq (opt) iteration-statement
10722 attribute-specifier-seq (opt) jump-statement
10723 declaration-statement
10724 attribute-specifier-seq (opt) try-block
10727 expression-statement
10735 IN_COMPOUND is true when the statement is nested inside a
10736 cp_parser_compound_statement; this matters for certain pragmas.
10738 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10739 is a (possibly labeled) if statement which is not enclosed in braces
10740 and has an else clause. This is used to implement -Wparentheses.
10742 CHAIN is a vector of if-else-if conditions. */
10745 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10746 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10747 location_t
*loc_after_labels
)
10749 tree statement
, std_attrs
= NULL_TREE
;
10751 location_t statement_location
, attrs_location
;
10756 /* There is no statement yet. */
10757 statement
= NULL_TREE
;
10759 saved_token_sentinel
saved_tokens (parser
->lexer
);
10760 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10761 if (c_dialect_objc ())
10762 /* In obj-c++, seeing '[[' might be the either the beginning of
10763 c++11 attributes, or a nested objc-message-expression. So
10764 let's parse the c++11 attributes tentatively. */
10765 cp_parser_parse_tentatively (parser
);
10766 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10767 if (c_dialect_objc ())
10769 if (!cp_parser_parse_definitely (parser
))
10770 std_attrs
= NULL_TREE
;
10773 /* Peek at the next token. */
10774 token
= cp_lexer_peek_token (parser
->lexer
);
10775 /* Remember the location of the first token in the statement. */
10776 statement_location
= token
->location
;
10777 add_debug_begin_stmt (statement_location
);
10778 /* If this is a keyword, then that will often determine what kind of
10779 statement we have. */
10780 if (token
->type
== CPP_KEYWORD
)
10782 enum rid keyword
= token
->keyword
;
10788 /* Looks like a labeled-statement with a case label.
10789 Parse the label, and then use tail recursion to parse
10791 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10792 in_compound
= false;
10797 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10803 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10810 statement
= cp_parser_jump_statement (parser
);
10813 /* Objective-C++ exception-handling constructs. */
10816 case RID_AT_FINALLY
:
10817 case RID_AT_SYNCHRONIZED
:
10819 statement
= cp_parser_objc_statement (parser
);
10823 statement
= cp_parser_try_block (parser
);
10826 case RID_NAMESPACE
:
10827 /* This must be a namespace alias definition. */
10828 cp_parser_declaration_statement (parser
);
10831 case RID_TRANSACTION_ATOMIC
:
10832 case RID_TRANSACTION_RELAXED
:
10833 case RID_SYNCHRONIZED
:
10834 case RID_ATOMIC_NOEXCEPT
:
10835 case RID_ATOMIC_CANCEL
:
10836 statement
= cp_parser_transaction (parser
, token
);
10838 case RID_TRANSACTION_CANCEL
:
10839 statement
= cp_parser_transaction_cancel (parser
);
10843 /* It might be a keyword like `int' that can start a
10844 declaration-statement. */
10848 else if (token
->type
== CPP_NAME
)
10850 /* If the next token is a `:', then we are looking at a
10851 labeled-statement. */
10852 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10853 if (token
->type
== CPP_COLON
)
10855 /* Looks like a labeled-statement with an ordinary label.
10856 Parse the label, and then use tail recursion to parse
10859 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10860 in_compound
= false;
10864 /* Anything that starts with a `{' must be a compound-statement. */
10865 else if (token
->type
== CPP_OPEN_BRACE
)
10866 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10867 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10868 a statement all its own. */
10869 else if (token
->type
== CPP_PRAGMA
)
10871 /* Only certain OpenMP pragmas are attached to statements, and thus
10872 are considered statements themselves. All others are not. In
10873 the context of a compound, accept the pragma as a "statement" and
10874 return so that we can check for a close brace. Otherwise we
10875 require a real statement and must go back and read one. */
10877 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10878 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10882 else if (token
->type
== CPP_EOF
)
10884 cp_parser_error (parser
, "expected statement");
10888 /* Everything else must be a declaration-statement or an
10889 expression-statement. Try for the declaration-statement
10890 first, unless we are looking at a `;', in which case we know that
10891 we have an expression-statement. */
10894 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10896 if (std_attrs
!= NULL_TREE
)
10898 /* Attributes should be parsed as part of the the
10899 declaration, so let's un-parse them. */
10900 saved_tokens
.rollback();
10901 std_attrs
= NULL_TREE
;
10904 cp_parser_parse_tentatively (parser
);
10905 /* Try to parse the declaration-statement. */
10906 cp_parser_declaration_statement (parser
);
10907 /* If that worked, we're done. */
10908 if (cp_parser_parse_definitely (parser
))
10911 /* All preceding labels have been parsed at this point. */
10912 if (loc_after_labels
!= NULL
)
10913 *loc_after_labels
= statement_location
;
10915 /* Look for an expression-statement instead. */
10916 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10918 /* Handle [[fallthrough]];. */
10919 if (attribute_fallthrough_p (std_attrs
))
10921 /* The next token after the fallthrough attribute is ';'. */
10922 if (statement
== NULL_TREE
)
10924 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10925 statement
= build_call_expr_internal_loc (statement_location
,
10927 void_type_node
, 0);
10928 finish_expr_stmt (statement
);
10931 warning_at (statement_location
, OPT_Wattributes
,
10932 "%<fallthrough%> attribute not followed by %<;%>");
10933 std_attrs
= NULL_TREE
;
10937 /* Set the line number for the statement. */
10938 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10939 SET_EXPR_LOCATION (statement
, statement_location
);
10941 /* Allow "[[fallthrough]];", but warn otherwise. */
10942 if (std_attrs
!= NULL_TREE
)
10943 warning_at (attrs_location
,
10945 "attributes at the beginning of statement are ignored");
10948 /* Append ATTR to attribute list ATTRS. */
10951 attr_chainon (tree attrs
, tree attr
)
10953 if (attrs
== error_mark_node
)
10954 return error_mark_node
;
10955 if (attr
== error_mark_node
)
10956 return error_mark_node
;
10957 return chainon (attrs
, attr
);
10960 /* Parse the label for a labeled-statement, i.e.
10963 case constant-expression :
10967 case constant-expression ... constant-expression : statement
10969 When a label is parsed without errors, the label is added to the
10970 parse tree by the finish_* functions, so this function doesn't
10971 have to return the label. */
10974 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10977 tree label
= NULL_TREE
;
10978 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10980 /* The next token should be an identifier. */
10981 token
= cp_lexer_peek_token (parser
->lexer
);
10982 if (token
->type
!= CPP_NAME
10983 && token
->type
!= CPP_KEYWORD
)
10985 cp_parser_error (parser
, "expected labeled-statement");
10989 /* Remember whether this case or a user-defined label is allowed to fall
10991 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10993 parser
->colon_corrects_to_scope_p
= false;
10994 switch (token
->keyword
)
10998 tree expr
, expr_hi
;
10999 cp_token
*ellipsis
;
11001 /* Consume the `case' token. */
11002 cp_lexer_consume_token (parser
->lexer
);
11003 /* Parse the constant-expression. */
11004 expr
= cp_parser_constant_expression (parser
);
11005 if (check_for_bare_parameter_packs (expr
))
11006 expr
= error_mark_node
;
11008 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11009 if (ellipsis
->type
== CPP_ELLIPSIS
)
11011 /* Consume the `...' token. */
11012 cp_lexer_consume_token (parser
->lexer
);
11013 expr_hi
= cp_parser_constant_expression (parser
);
11014 if (check_for_bare_parameter_packs (expr_hi
))
11015 expr_hi
= error_mark_node
;
11017 /* We don't need to emit warnings here, as the common code
11018 will do this for us. */
11021 expr_hi
= NULL_TREE
;
11023 if (parser
->in_switch_statement_p
)
11025 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11026 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11027 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11030 error_at (token
->location
,
11031 "case label %qE not within a switch statement",
11037 /* Consume the `default' token. */
11038 cp_lexer_consume_token (parser
->lexer
);
11040 if (parser
->in_switch_statement_p
)
11042 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11043 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11044 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11047 error_at (token
->location
, "case label not within a switch statement");
11051 /* Anything else must be an ordinary label. */
11052 label
= finish_label_stmt (cp_parser_identifier (parser
));
11053 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11054 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11058 /* Require the `:' token. */
11059 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11061 /* An ordinary label may optionally be followed by attributes.
11062 However, this is only permitted if the attributes are then
11063 followed by a semicolon. This is because, for backward
11064 compatibility, when parsing
11065 lab: __attribute__ ((unused)) int i;
11066 we want the attribute to attach to "i", not "lab". */
11067 if (label
!= NULL_TREE
11068 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11071 cp_parser_parse_tentatively (parser
);
11072 attrs
= cp_parser_gnu_attributes_opt (parser
);
11073 if (attrs
== NULL_TREE
11074 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11075 cp_parser_abort_tentative_parse (parser
);
11076 else if (!cp_parser_parse_definitely (parser
))
11079 attributes
= attr_chainon (attributes
, attrs
);
11082 if (attributes
!= NULL_TREE
)
11083 cplus_decl_attributes (&label
, attributes
, 0);
11085 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11088 /* Parse an expression-statement.
11090 expression-statement:
11093 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11094 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11095 indicates whether this expression-statement is part of an
11096 expression statement. */
11099 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11101 tree statement
= NULL_TREE
;
11102 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11103 location_t loc
= token
->location
;
11105 /* There might be attribute fallthrough. */
11106 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11108 /* If the next token is a ';', then there is no expression
11110 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11112 statement
= cp_parser_expression (parser
);
11113 if (statement
== error_mark_node
11114 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11116 cp_parser_skip_to_end_of_block_or_statement (parser
);
11117 return error_mark_node
;
11121 /* Handle [[fallthrough]];. */
11122 if (attribute_fallthrough_p (attr
))
11124 /* The next token after the fallthrough attribute is ';'. */
11125 if (statement
== NULL_TREE
)
11126 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11127 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11128 void_type_node
, 0);
11130 warning_at (loc
, OPT_Wattributes
,
11131 "%<fallthrough%> attribute not followed by %<;%>");
11135 /* Allow "[[fallthrough]];", but warn otherwise. */
11136 if (attr
!= NULL_TREE
)
11137 warning_at (loc
, OPT_Wattributes
,
11138 "attributes at the beginning of statement are ignored");
11140 /* Give a helpful message for "A<T>::type t;" and the like. */
11141 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11142 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11144 if (TREE_CODE (statement
) == SCOPE_REF
)
11145 error_at (token
->location
, "need %<typename%> before %qE because "
11146 "%qT is a dependent scope",
11147 statement
, TREE_OPERAND (statement
, 0));
11148 else if (is_overloaded_fn (statement
)
11149 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11152 tree fn
= get_first_fn (statement
);
11153 error_at (token
->location
,
11154 "%<%T::%D%> names the constructor, not the type",
11155 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11159 /* Consume the final `;'. */
11160 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11162 if (in_statement_expr
11163 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11164 /* This is the final expression statement of a statement
11166 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11167 else if (statement
)
11168 statement
= finish_expr_stmt (statement
);
11173 /* Parse a compound-statement.
11175 compound-statement:
11176 { statement-seq [opt] }
11180 compound-statement:
11181 { label-declaration-seq [opt] statement-seq [opt] }
11183 label-declaration-seq:
11185 label-declaration-seq label-declaration
11187 Returns a tree representing the statement. */
11190 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11191 int bcs_flags
, bool function_body
)
11193 tree compound_stmt
;
11194 matching_braces braces
;
11196 /* Consume the `{'. */
11197 if (!braces
.require_open (parser
))
11198 return error_mark_node
;
11199 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11200 && !function_body
&& cxx_dialect
< cxx14
)
11201 pedwarn (input_location
, OPT_Wpedantic
,
11202 "compound-statement in %<constexpr%> function");
11203 /* Begin the compound-statement. */
11204 compound_stmt
= begin_compound_stmt (bcs_flags
);
11205 /* If the next keyword is `__label__' we have a label declaration. */
11206 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11207 cp_parser_label_declaration (parser
);
11208 /* Parse an (optional) statement-seq. */
11209 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11210 /* Finish the compound-statement. */
11211 finish_compound_stmt (compound_stmt
);
11212 /* Consume the `}'. */
11213 braces
.require_close (parser
);
11215 return compound_stmt
;
11218 /* Parse an (optional) statement-seq.
11222 statement-seq [opt] statement */
11225 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11227 /* Scan statements until there aren't any more. */
11230 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11232 /* If we are looking at a `}', then we have run out of
11233 statements; the same is true if we have reached the end
11234 of file, or have stumbled upon a stray '@end'. */
11235 if (token
->type
== CPP_CLOSE_BRACE
11236 || token
->type
== CPP_EOF
11237 || token
->type
== CPP_PRAGMA_EOL
11238 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11241 /* If we are in a compound statement and find 'else' then
11242 something went wrong. */
11243 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11245 if (parser
->in_statement
& IN_IF_STMT
)
11249 token
= cp_lexer_consume_token (parser
->lexer
);
11250 error_at (token
->location
, "%<else%> without a previous %<if%>");
11254 /* Parse the statement. */
11255 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11259 /* Return true if this is the C++20 version of range-based-for with
11263 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11267 /* Save tokens so that we can put them back. */
11268 cp_lexer_save_tokens (parser
->lexer
);
11270 /* There has to be an unnested ; followed by an unnested :. */
11271 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11272 /*recovering=*/false,
11274 /*consume_paren=*/false) != -1)
11277 /* We found the semicolon, eat it now. */
11278 cp_lexer_consume_token (parser
->lexer
);
11280 /* Now look for ':' that is not nested in () or {}. */
11281 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11282 /*recovering=*/false,
11284 /*consume_paren=*/false) == -1);
11287 /* Roll back the tokens we skipped. */
11288 cp_lexer_rollback_tokens (parser
->lexer
);
11293 /* Return true if we're looking at (init; cond), false otherwise. */
11296 cp_parser_init_statement_p (cp_parser
*parser
)
11298 /* Save tokens so that we can put them back. */
11299 cp_lexer_save_tokens (parser
->lexer
);
11301 /* Look for ';' that is not nested in () or {}. */
11302 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11303 /*recovering=*/false,
11305 /*consume_paren=*/false);
11307 /* Roll back the tokens we skipped. */
11308 cp_lexer_rollback_tokens (parser
->lexer
);
11313 /* Parse a selection-statement.
11315 selection-statement:
11316 if ( init-statement [opt] condition ) statement
11317 if ( init-statement [opt] condition ) statement else statement
11318 switch ( init-statement [opt] condition ) statement
11320 Returns the new IF_STMT or SWITCH_STMT.
11322 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11323 is a (possibly labeled) if statement which is not enclosed in
11324 braces and has an else clause. This is used to implement
11327 CHAIN is a vector of if-else-if conditions. This is used to implement
11328 -Wduplicated-cond. */
11331 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11336 token_indent_info guard_tinfo
;
11341 /* Peek at the next token. */
11342 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11343 guard_tinfo
= get_token_indent_info (token
);
11345 /* See what kind of keyword it is. */
11346 keyword
= token
->keyword
;
11356 if (keyword
== RID_IF
11357 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11361 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11362 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11363 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11364 "with -std=c++17 or -std=gnu++17");
11367 /* Look for the `('. */
11368 matching_parens parens
;
11369 if (!parens
.require_open (parser
))
11371 cp_parser_skip_to_end_of_statement (parser
);
11372 return error_mark_node
;
11375 /* Begin the selection-statement. */
11376 if (keyword
== RID_IF
)
11378 statement
= begin_if_stmt ();
11379 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11382 statement
= begin_switch_stmt ();
11384 /* Parse the optional init-statement. */
11385 if (cp_parser_init_statement_p (parser
))
11388 if (cxx_dialect
< cxx17
)
11389 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11390 "init-statement in selection statements only available "
11391 "with -std=c++17 or -std=gnu++17");
11392 cp_parser_init_statement (parser
, &decl
);
11395 /* Parse the condition. */
11396 condition
= cp_parser_condition (parser
);
11397 /* Look for the `)'. */
11398 if (!parens
.require_close (parser
))
11399 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11400 /*consume_paren=*/true);
11402 if (keyword
== RID_IF
)
11405 unsigned char in_statement
;
11407 /* Add the condition. */
11408 condition
= finish_if_stmt_cond (condition
, statement
);
11410 if (warn_duplicated_cond
)
11411 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11414 /* Parse the then-clause. */
11415 in_statement
= parser
->in_statement
;
11416 parser
->in_statement
|= IN_IF_STMT
;
11418 /* Outside a template, the non-selected branch of a constexpr
11419 if is a 'discarded statement', i.e. unevaluated. */
11420 bool was_discarded
= in_discarded_stmt
;
11421 bool discard_then
= (cx
&& !processing_template_decl
11422 && integer_zerop (condition
));
11425 in_discarded_stmt
= true;
11426 ++c_inhibit_evaluation_warnings
;
11429 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11432 parser
->in_statement
= in_statement
;
11434 finish_then_clause (statement
);
11438 THEN_CLAUSE (statement
) = NULL_TREE
;
11439 in_discarded_stmt
= was_discarded
;
11440 --c_inhibit_evaluation_warnings
;
11443 /* If the next token is `else', parse the else-clause. */
11444 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11447 bool discard_else
= (cx
&& !processing_template_decl
11448 && integer_nonzerop (condition
));
11451 in_discarded_stmt
= true;
11452 ++c_inhibit_evaluation_warnings
;
11456 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11457 /* Consume the `else' keyword. */
11458 cp_lexer_consume_token (parser
->lexer
);
11459 if (warn_duplicated_cond
)
11461 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11465 /* We've got "if (COND) else if (COND2)". Start
11466 the condition chain and add COND as the first
11468 chain
= new vec
<tree
> ();
11469 if (!CONSTANT_CLASS_P (condition
)
11470 && !TREE_SIDE_EFFECTS (condition
))
11472 /* Wrap it in a NOP_EXPR so that we can set the
11473 location of the condition. */
11474 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11476 SET_EXPR_LOCATION (e
, token
->location
);
11477 chain
->safe_push (e
);
11480 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11483 /* This is if-else without subsequent if. Zap the
11484 condition chain; we would have already warned at
11490 begin_else_clause (statement
);
11491 /* Parse the else-clause. */
11492 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11493 guard_tinfo
, chain
);
11495 finish_else_clause (statement
);
11497 /* If we are currently parsing a then-clause, then
11498 IF_P will not be NULL. We set it to true to
11499 indicate that this if statement has an else clause.
11500 This may trigger the Wparentheses warning below
11501 when we get back up to the parent if statement. */
11507 ELSE_CLAUSE (statement
) = NULL_TREE
;
11508 in_discarded_stmt
= was_discarded
;
11509 --c_inhibit_evaluation_warnings
;
11514 /* This if statement does not have an else clause. If
11515 NESTED_IF is true, then the then-clause has an if
11516 statement which does have an else clause. We warn
11517 about the potential ambiguity. */
11519 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11520 "suggest explicit braces to avoid ambiguous"
11522 if (warn_duplicated_cond
)
11524 /* We don't need the condition chain anymore. */
11530 /* Now we're all done with the if-statement. */
11531 finish_if_stmt (statement
);
11535 bool in_switch_statement_p
;
11536 unsigned char in_statement
;
11538 /* Add the condition. */
11539 finish_switch_cond (condition
, statement
);
11541 /* Parse the body of the switch-statement. */
11542 in_switch_statement_p
= parser
->in_switch_statement_p
;
11543 in_statement
= parser
->in_statement
;
11544 parser
->in_switch_statement_p
= true;
11545 parser
->in_statement
|= IN_SWITCH_STMT
;
11546 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11548 parser
->in_switch_statement_p
= in_switch_statement_p
;
11549 parser
->in_statement
= in_statement
;
11551 /* Now we're all done with the switch-statement. */
11552 finish_switch_stmt (statement
);
11560 cp_parser_error (parser
, "expected selection-statement");
11561 return error_mark_node
;
11565 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11566 If we have seen at least one decl-specifier, and the next token
11567 is not a parenthesis, then we must be looking at a declaration.
11568 (After "int (" we might be looking at a functional cast.) */
11571 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11572 bool any_specifiers_p
)
11574 if (any_specifiers_p
11575 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11576 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11577 && !cp_parser_error_occurred (parser
))
11578 cp_parser_commit_to_tentative_parse (parser
);
11581 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11582 The declarator shall not specify a function or an array. Returns
11583 TRUE if the declarator is valid, FALSE otherwise. */
11586 cp_parser_check_condition_declarator (cp_parser
* parser
,
11587 cp_declarator
*declarator
,
11590 if (declarator
== cp_error_declarator
11591 || function_declarator_p (declarator
)
11592 || declarator
->kind
== cdk_array
)
11594 if (declarator
== cp_error_declarator
)
11595 /* Already complained. */;
11596 else if (declarator
->kind
== cdk_array
)
11597 error_at (loc
, "condition declares an array");
11599 error_at (loc
, "condition declares a function");
11600 if (parser
->fully_implicit_function_template_p
)
11601 abort_fully_implicit_template (parser
);
11602 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11603 /*or_comma=*/false,
11604 /*consume_paren=*/false);
11611 /* Parse a condition.
11615 type-specifier-seq declarator = initializer-clause
11616 type-specifier-seq declarator braced-init-list
11621 type-specifier-seq declarator asm-specification [opt]
11622 attributes [opt] = assignment-expression
11624 Returns the expression that should be tested. */
11627 cp_parser_condition (cp_parser
* parser
)
11629 cp_decl_specifier_seq type_specifiers
;
11630 const char *saved_message
;
11631 int declares_class_or_enum
;
11633 /* Try the declaration first. */
11634 cp_parser_parse_tentatively (parser
);
11635 /* New types are not allowed in the type-specifier-seq for a
11637 saved_message
= parser
->type_definition_forbidden_message
;
11638 parser
->type_definition_forbidden_message
11639 = G_("types may not be defined in conditions");
11640 /* Parse the type-specifier-seq. */
11641 cp_parser_decl_specifier_seq (parser
,
11642 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11644 &declares_class_or_enum
);
11645 /* Restore the saved message. */
11646 parser
->type_definition_forbidden_message
= saved_message
;
11648 cp_parser_maybe_commit_to_declaration (parser
,
11649 type_specifiers
.any_specifiers_p
);
11651 /* If all is well, we might be looking at a declaration. */
11652 if (!cp_parser_error_occurred (parser
))
11655 tree asm_specification
;
11657 cp_declarator
*declarator
;
11658 tree initializer
= NULL_TREE
;
11659 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11661 /* Parse the declarator. */
11662 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11663 /*ctor_dtor_or_conv_p=*/NULL
,
11664 /*parenthesized_p=*/NULL
,
11665 /*member_p=*/false,
11666 /*friend_p=*/false);
11667 /* Parse the attributes. */
11668 attributes
= cp_parser_attributes_opt (parser
);
11669 /* Parse the asm-specification. */
11670 asm_specification
= cp_parser_asm_specification_opt (parser
);
11671 /* If the next token is not an `=' or '{', then we might still be
11672 looking at an expression. For example:
11676 looks like a decl-specifier-seq and a declarator -- but then
11677 there is no `=', so this is an expression. */
11678 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11679 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11680 cp_parser_simulate_error (parser
);
11682 /* If we did see an `=' or '{', then we are looking at a declaration
11684 if (cp_parser_parse_definitely (parser
))
11687 bool non_constant_p
;
11688 int flags
= LOOKUP_ONLYCONVERTING
;
11690 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11691 return error_mark_node
;
11693 /* Create the declaration. */
11694 decl
= start_decl (declarator
, &type_specifiers
,
11695 /*initialized_p=*/true,
11696 attributes
, /*prefix_attributes=*/NULL_TREE
,
11699 /* Parse the initializer. */
11700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11702 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11703 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11706 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11708 /* Consume the `='. */
11709 cp_lexer_consume_token (parser
->lexer
);
11710 initializer
= cp_parser_initializer_clause (parser
,
11715 cp_parser_error (parser
, "expected initializer");
11716 initializer
= error_mark_node
;
11718 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11719 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11721 /* Process the initializer. */
11722 cp_finish_decl (decl
,
11723 initializer
, !non_constant_p
,
11728 pop_scope (pushed_scope
);
11730 return convert_from_reference (decl
);
11733 /* If we didn't even get past the declarator successfully, we are
11734 definitely not looking at a declaration. */
11736 cp_parser_abort_tentative_parse (parser
);
11738 /* Otherwise, we are looking at an expression. */
11739 return cp_parser_expression (parser
);
11742 /* Parses a for-statement or range-for-statement until the closing ')',
11746 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11748 tree init
, scope
, decl
;
11751 /* Begin the for-statement. */
11752 scope
= begin_for_scope (&init
);
11754 /* Parse the initialization. */
11755 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11758 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11760 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11764 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11765 unsigned short unroll
)
11767 /* Normal for loop */
11768 tree condition
= NULL_TREE
;
11769 tree expression
= NULL_TREE
;
11772 stmt
= begin_for_stmt (scope
, init
);
11773 /* The init-statement has already been parsed in
11774 cp_parser_init_statement, so no work is needed here. */
11775 finish_init_stmt (stmt
);
11777 /* If there's a condition, process it. */
11778 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11779 condition
= cp_parser_condition (parser
);
11782 cp_parser_error (parser
, "missing loop condition in loop with "
11783 "%<GCC ivdep%> pragma");
11784 condition
= error_mark_node
;
11788 cp_parser_error (parser
, "missing loop condition in loop with "
11789 "%<GCC unroll%> pragma");
11790 condition
= error_mark_node
;
11792 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11793 /* Look for the `;'. */
11794 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11796 /* If there's an expression, process it. */
11797 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11798 expression
= cp_parser_expression (parser
);
11799 finish_for_expr (expression
, stmt
);
11804 /* Tries to parse a range-based for-statement:
11807 decl-specifier-seq declarator : expression
11809 The decl-specifier-seq declarator and the `:' are already parsed by
11810 cp_parser_init_statement. If processing_template_decl it returns a
11811 newly created RANGE_FOR_STMT; if not, it is converted to a
11812 regular FOR_STMT. */
11815 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11816 bool ivdep
, unsigned short unroll
)
11818 tree stmt
, range_expr
;
11819 auto_vec
<cxx_binding
*, 16> bindings
;
11820 auto_vec
<tree
, 16> names
;
11821 tree decomp_first_name
= NULL_TREE
;
11822 unsigned int decomp_cnt
= 0;
11824 /* Get the range declaration momentarily out of the way so that
11825 the range expression doesn't clash with it. */
11826 if (range_decl
!= error_mark_node
)
11828 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11830 tree v
= DECL_VALUE_EXPR (range_decl
);
11831 /* For decomposition declaration get all of the corresponding
11832 declarations out of the way. */
11833 if (TREE_CODE (v
) == ARRAY_REF
11834 && VAR_P (TREE_OPERAND (v
, 0))
11835 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11837 tree d
= range_decl
;
11838 range_decl
= TREE_OPERAND (v
, 0);
11839 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11840 decomp_first_name
= d
;
11841 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11843 tree name
= DECL_NAME (d
);
11844 names
.safe_push (name
);
11845 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11846 IDENTIFIER_BINDING (name
)
11847 = IDENTIFIER_BINDING (name
)->previous
;
11851 if (names
.is_empty ())
11853 tree name
= DECL_NAME (range_decl
);
11854 names
.safe_push (name
);
11855 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11856 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11862 bool expr_non_constant_p
;
11863 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11866 range_expr
= cp_parser_expression (parser
);
11868 /* Put the range declaration(s) back into scope. */
11869 for (unsigned int i
= 0; i
< names
.length (); i
++)
11871 cxx_binding
*binding
= bindings
[i
];
11872 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11873 IDENTIFIER_BINDING (names
[i
]) = binding
;
11876 /* If in template, STMT is converted to a normal for-statement
11877 at instantiation. If not, it is done just ahead. */
11878 if (processing_template_decl
)
11880 if (check_for_bare_parameter_packs (range_expr
))
11881 range_expr
= error_mark_node
;
11882 stmt
= begin_range_for_stmt (scope
, init
);
11884 RANGE_FOR_IVDEP (stmt
) = 1;
11886 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11887 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11888 if (!type_dependent_expression_p (range_expr
)
11889 /* do_auto_deduction doesn't mess with template init-lists. */
11890 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11891 do_range_for_auto_deduction (range_decl
, range_expr
);
11895 stmt
= begin_for_stmt (scope
, init
);
11896 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11897 decomp_first_name
, decomp_cnt
, ivdep
,
11903 /* Subroutine of cp_convert_range_for: given the initializer expression,
11904 builds up the range temporary. */
11907 build_range_temp (tree range_expr
)
11909 tree range_type
, range_temp
;
11911 /* Find out the type deduced by the declaration
11912 `auto &&__range = range_expr'. */
11913 range_type
= cp_build_reference_type (make_auto (), true);
11914 range_type
= do_auto_deduction (range_type
, range_expr
,
11915 type_uses_auto (range_type
));
11917 /* Create the __range variable. */
11918 range_temp
= build_decl (input_location
, VAR_DECL
,
11919 get_identifier ("__for_range"), range_type
);
11920 TREE_USED (range_temp
) = 1;
11921 DECL_ARTIFICIAL (range_temp
) = 1;
11926 /* Used by cp_parser_range_for in template context: we aren't going to
11927 do a full conversion yet, but we still need to resolve auto in the
11928 type of the for-range-declaration if present. This is basically
11929 a shortcut version of cp_convert_range_for. */
11932 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11934 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11937 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11938 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11939 iter_type
= (cp_parser_perform_range_for_lookup
11940 (range_temp
, &begin_dummy
, &end_dummy
));
11943 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11945 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11947 tf_warning_or_error
);
11948 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11949 iter_decl
, auto_node
);
11954 /* Converts a range-based for-statement into a normal
11955 for-statement, as per the definition.
11957 for (RANGE_DECL : RANGE_EXPR)
11960 should be equivalent to:
11963 auto &&__range = RANGE_EXPR;
11964 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11968 RANGE_DECL = *__begin;
11973 If RANGE_EXPR is an array:
11974 BEGIN_EXPR = __range
11975 END_EXPR = __range + ARRAY_SIZE(__range)
11976 Else if RANGE_EXPR has a member 'begin' or 'end':
11977 BEGIN_EXPR = __range.begin()
11978 END_EXPR = __range.end()
11980 BEGIN_EXPR = begin(__range)
11981 END_EXPR = end(__range);
11983 If __range has a member 'begin' but not 'end', or vice versa, we must
11984 still use the second alternative (it will surely fail, however).
11985 When calling begin()/end() in the third alternative we must use
11986 argument dependent lookup, but always considering 'std' as an associated
11990 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11991 tree decomp_first_name
, unsigned int decomp_cnt
,
11992 bool ivdep
, unsigned short unroll
)
11995 tree iter_type
, begin_expr
, end_expr
;
11996 tree condition
, expression
;
11998 range_expr
= mark_lvalue_use (range_expr
);
12000 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12001 /* If an error happened previously do nothing or else a lot of
12002 unhelpful errors would be issued. */
12003 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12008 if (VAR_P (range_expr
)
12009 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12010 /* Can't bind a reference to an array of runtime bound. */
12011 range_temp
= range_expr
;
12014 range_temp
= build_range_temp (range_expr
);
12015 pushdecl (range_temp
);
12016 cp_finish_decl (range_temp
, range_expr
,
12017 /*is_constant_init*/false, NULL_TREE
,
12018 LOOKUP_ONLYCONVERTING
);
12019 range_temp
= convert_from_reference (range_temp
);
12021 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12022 &begin_expr
, &end_expr
);
12025 /* The new for initialization statement. */
12026 begin
= build_decl (input_location
, VAR_DECL
,
12027 get_identifier ("__for_begin"), iter_type
);
12028 TREE_USED (begin
) = 1;
12029 DECL_ARTIFICIAL (begin
) = 1;
12031 cp_finish_decl (begin
, begin_expr
,
12032 /*is_constant_init*/false, NULL_TREE
,
12033 LOOKUP_ONLYCONVERTING
);
12035 if (cxx_dialect
>= cxx17
)
12036 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12037 end
= build_decl (input_location
, VAR_DECL
,
12038 get_identifier ("__for_end"), iter_type
);
12039 TREE_USED (end
) = 1;
12040 DECL_ARTIFICIAL (end
) = 1;
12042 cp_finish_decl (end
, end_expr
,
12043 /*is_constant_init*/false, NULL_TREE
,
12044 LOOKUP_ONLYCONVERTING
);
12046 finish_init_stmt (statement
);
12048 /* The new for condition. */
12049 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12052 NULL
, tf_warning_or_error
);
12053 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12055 /* The new increment expression. */
12056 expression
= finish_unary_op_expr (input_location
,
12057 PREINCREMENT_EXPR
, begin
,
12058 tf_warning_or_error
);
12059 finish_for_expr (expression
, statement
);
12061 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12062 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12064 /* The declaration is initialized with *__begin inside the loop body. */
12065 cp_finish_decl (range_decl
,
12066 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12067 tf_warning_or_error
),
12068 /*is_constant_init*/false, NULL_TREE
,
12069 LOOKUP_ONLYCONVERTING
);
12070 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12071 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12076 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12077 We need to solve both at the same time because the method used
12078 depends on the existence of members begin or end.
12079 Returns the type deduced for the iterator expression. */
12082 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12084 if (error_operand_p (range
))
12086 *begin
= *end
= error_mark_node
;
12087 return error_mark_node
;
12090 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12092 error ("range-based %<for%> expression of type %qT "
12093 "has incomplete type", TREE_TYPE (range
));
12094 *begin
= *end
= error_mark_node
;
12095 return error_mark_node
;
12097 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12099 /* If RANGE is an array, we will use pointer arithmetic. */
12100 *begin
= decay_conversion (range
, tf_warning_or_error
);
12101 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12103 array_type_nelts_top (TREE_TYPE (range
)),
12105 return TREE_TYPE (*begin
);
12109 /* If it is not an array, we must do a bit of magic. */
12110 tree id_begin
, id_end
;
12111 tree member_begin
, member_end
;
12113 *begin
= *end
= error_mark_node
;
12115 id_begin
= get_identifier ("begin");
12116 id_end
= get_identifier ("end");
12117 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12118 /*protect=*/2, /*want_type=*/false,
12119 tf_warning_or_error
);
12120 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12121 /*protect=*/2, /*want_type=*/false,
12122 tf_warning_or_error
);
12124 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12126 /* Use the member functions. */
12127 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12128 *end
= cp_parser_range_for_member_function (range
, id_end
);
12132 /* Use global functions with ADL. */
12133 vec
<tree
, va_gc
> *vec
;
12134 vec
= make_tree_vector ();
12136 vec_safe_push (vec
, range
);
12138 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12139 tf_warning_or_error
);
12140 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12141 tf_warning_or_error
);
12142 member_end
= perform_koenig_lookup (id_end
, vec
,
12143 tf_warning_or_error
);
12144 *end
= finish_call_expr (member_end
, &vec
, false, true,
12145 tf_warning_or_error
);
12147 release_tree_vector (vec
);
12150 /* Last common checks. */
12151 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12153 /* If one of the expressions is an error do no more checks. */
12154 *begin
= *end
= error_mark_node
;
12155 return error_mark_node
;
12157 else if (type_dependent_expression_p (*begin
)
12158 || type_dependent_expression_p (*end
))
12159 /* Can happen, when, eg, in a template context, Koenig lookup
12160 can't resolve begin/end (c++/58503). */
12164 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12165 /* The unqualified type of the __begin and __end temporaries should
12166 be the same, as required by the multiple auto declaration. */
12167 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12169 if (cxx_dialect
>= cxx17
12170 && (build_x_binary_op (input_location
, NE_EXPR
,
12171 *begin
, ERROR_MARK
,
12174 != error_mark_node
))
12175 /* P0184R0 allows __begin and __end to have different types,
12176 but make sure they are comparable so we can give a better
12179 error ("inconsistent begin/end types in range-based %<for%> "
12180 "statement: %qT and %qT",
12181 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12188 /* Helper function for cp_parser_perform_range_for_lookup.
12189 Builds a tree for RANGE.IDENTIFIER(). */
12192 cp_parser_range_for_member_function (tree range
, tree identifier
)
12195 vec
<tree
, va_gc
> *vec
;
12197 member
= finish_class_member_access_expr (range
, identifier
,
12198 false, tf_warning_or_error
);
12199 if (member
== error_mark_node
)
12200 return error_mark_node
;
12202 vec
= make_tree_vector ();
12203 res
= finish_call_expr (member
, &vec
,
12204 /*disallow_virtual=*/false,
12205 /*koenig_p=*/false,
12206 tf_warning_or_error
);
12207 release_tree_vector (vec
);
12211 /* Parse an iteration-statement.
12213 iteration-statement:
12214 while ( condition ) statement
12215 do statement while ( expression ) ;
12216 for ( init-statement condition [opt] ; expression [opt] )
12219 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12222 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12223 unsigned short unroll
)
12228 unsigned char in_statement
;
12229 token_indent_info guard_tinfo
;
12231 /* Peek at the next token. */
12232 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12234 return error_mark_node
;
12236 guard_tinfo
= get_token_indent_info (token
);
12238 /* Remember whether or not we are already within an iteration
12240 in_statement
= parser
->in_statement
;
12242 /* See what kind of keyword it is. */
12243 keyword
= token
->keyword
;
12250 /* Begin the while-statement. */
12251 statement
= begin_while_stmt ();
12252 /* Look for the `('. */
12253 matching_parens parens
;
12254 parens
.require_open (parser
);
12255 /* Parse the condition. */
12256 condition
= cp_parser_condition (parser
);
12257 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12258 /* Look for the `)'. */
12259 parens
.require_close (parser
);
12260 /* Parse the dependent statement. */
12261 parser
->in_statement
= IN_ITERATION_STMT
;
12262 bool prev
= note_iteration_stmt_body_start ();
12263 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12264 note_iteration_stmt_body_end (prev
);
12265 parser
->in_statement
= in_statement
;
12266 /* We're done with the while-statement. */
12267 finish_while_stmt (statement
);
12275 /* Begin the do-statement. */
12276 statement
= begin_do_stmt ();
12277 /* Parse the body of the do-statement. */
12278 parser
->in_statement
= IN_ITERATION_STMT
;
12279 bool prev
= note_iteration_stmt_body_start ();
12280 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12281 note_iteration_stmt_body_end (prev
);
12282 parser
->in_statement
= in_statement
;
12283 finish_do_body (statement
);
12284 /* Look for the `while' keyword. */
12285 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12286 /* Look for the `('. */
12287 matching_parens parens
;
12288 parens
.require_open (parser
);
12289 /* Parse the expression. */
12290 expression
= cp_parser_expression (parser
);
12291 /* We're done with the do-statement. */
12292 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12293 /* Look for the `)'. */
12294 parens
.require_close (parser
);
12295 /* Look for the `;'. */
12296 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12302 /* Look for the `('. */
12303 matching_parens parens
;
12304 parens
.require_open (parser
);
12306 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12308 /* Look for the `)'. */
12309 parens
.require_close (parser
);
12311 /* Parse the body of the for-statement. */
12312 parser
->in_statement
= IN_ITERATION_STMT
;
12313 bool prev
= note_iteration_stmt_body_start ();
12314 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12315 note_iteration_stmt_body_end (prev
);
12316 parser
->in_statement
= in_statement
;
12318 /* We're done with the for-statement. */
12319 finish_for_stmt (statement
);
12324 cp_parser_error (parser
, "expected iteration-statement");
12325 statement
= error_mark_node
;
12332 /* Parse a init-statement or the declarator of a range-based-for.
12333 Returns true if a range-based-for declaration is seen.
12336 expression-statement
12337 simple-declaration */
12340 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12342 /* If the next token is a `;', then we have an empty
12343 expression-statement. Grammatically, this is also a
12344 simple-declaration, but an invalid one, because it does not
12345 declare anything. Therefore, if we did not handle this case
12346 specially, we would issue an error message about an invalid
12348 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12350 bool is_range_for
= false;
12351 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12353 /* Try to parse the init-statement. */
12354 if (cp_parser_range_based_for_with_init_p (parser
))
12357 cp_parser_parse_tentatively (parser
);
12358 /* Parse the declaration. */
12359 cp_parser_simple_declaration (parser
,
12360 /*function_definition_allowed_p=*/false,
12362 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12363 if (!cp_parser_parse_definitely (parser
))
12364 /* That didn't work, try to parse it as an expression-statement. */
12365 cp_parser_expression_statement (parser
, NULL_TREE
);
12367 if (cxx_dialect
< cxx2a
)
12369 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12370 "range-based %<for%> loops with initializer only "
12371 "available with -std=c++2a or -std=gnu++2a");
12372 *decl
= error_mark_node
;
12376 /* A colon is used in range-based for. */
12377 parser
->colon_corrects_to_scope_p
= false;
12379 /* We're going to speculatively look for a declaration, falling back
12380 to an expression, if necessary. */
12381 cp_parser_parse_tentatively (parser
);
12382 /* Parse the declaration. */
12383 cp_parser_simple_declaration (parser
,
12384 /*function_definition_allowed_p=*/false,
12386 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12389 /* It is a range-for, consume the ':'. */
12390 cp_lexer_consume_token (parser
->lexer
);
12391 is_range_for
= true;
12392 if (cxx_dialect
< cxx11
)
12393 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12394 "range-based %<for%> loops only available with "
12395 "-std=c++11 or -std=gnu++11");
12398 /* The ';' is not consumed yet because we told
12399 cp_parser_simple_declaration not to. */
12400 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12402 if (cp_parser_parse_definitely (parser
))
12403 return is_range_for
;
12404 /* If the tentative parse failed, then we shall need to look for an
12405 expression-statement. */
12407 /* If we are here, it is an expression-statement. */
12408 cp_parser_expression_statement (parser
, NULL_TREE
);
12412 /* Parse a jump-statement.
12417 return expression [opt] ;
12418 return braced-init-list ;
12424 goto * expression ;
12426 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12429 cp_parser_jump_statement (cp_parser
* parser
)
12431 tree statement
= error_mark_node
;
12434 unsigned char in_statement
;
12436 /* Peek at the next token. */
12437 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12439 return error_mark_node
;
12441 /* See what kind of keyword it is. */
12442 keyword
= token
->keyword
;
12446 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12447 switch (in_statement
)
12450 error_at (token
->location
, "break statement not within loop or switch");
12453 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12454 || in_statement
== IN_ITERATION_STMT
);
12455 statement
= finish_break_stmt ();
12456 if (in_statement
== IN_ITERATION_STMT
)
12457 break_maybe_infinite_loop ();
12460 error_at (token
->location
, "invalid exit from OpenMP structured block");
12463 error_at (token
->location
, "break statement used with OpenMP for loop");
12466 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12470 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12473 error_at (token
->location
, "continue statement not within a loop");
12475 /* Fall through. */
12476 case IN_ITERATION_STMT
:
12478 statement
= finish_continue_stmt ();
12481 error_at (token
->location
, "invalid exit from OpenMP structured block");
12484 gcc_unreachable ();
12486 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12492 bool expr_non_constant_p
;
12494 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12496 cp_lexer_set_source_position (parser
->lexer
);
12497 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12498 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12500 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12501 expr
= cp_parser_expression (parser
);
12503 /* If the next token is a `;', then there is no
12506 /* Build the return-statement. */
12507 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12508 /* Don't deduce from a discarded return statement. */;
12510 statement
= finish_return_stmt (expr
);
12511 /* Look for the final `;'. */
12512 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12517 if (parser
->in_function_body
12518 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12520 error ("%<goto%> in %<constexpr%> function");
12521 cp_function_chain
->invalid_constexpr
= true;
12524 /* Create the goto-statement. */
12525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12527 /* Issue a warning about this use of a GNU extension. */
12528 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12529 /* Consume the '*' token. */
12530 cp_lexer_consume_token (parser
->lexer
);
12531 /* Parse the dependent expression. */
12532 finish_goto_stmt (cp_parser_expression (parser
));
12535 finish_goto_stmt (cp_parser_identifier (parser
));
12536 /* Look for the final `;'. */
12537 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12541 cp_parser_error (parser
, "expected jump-statement");
12548 /* Parse a declaration-statement.
12550 declaration-statement:
12551 block-declaration */
12554 cp_parser_declaration_statement (cp_parser
* parser
)
12558 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12559 p
= obstack_alloc (&declarator_obstack
, 0);
12561 /* Parse the block-declaration. */
12562 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12564 /* Free any declarators allocated. */
12565 obstack_free (&declarator_obstack
, p
);
12568 /* Some dependent statements (like `if (cond) statement'), are
12569 implicitly in their own scope. In other words, if the statement is
12570 a single statement (as opposed to a compound-statement), it is
12571 none-the-less treated as if it were enclosed in braces. Any
12572 declarations appearing in the dependent statement are out of scope
12573 after control passes that point. This function parses a statement,
12574 but ensures that is in its own scope, even if it is not a
12575 compound-statement.
12577 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12578 is a (possibly labeled) if statement which is not enclosed in
12579 braces and has an else clause. This is used to implement
12582 CHAIN is a vector of if-else-if conditions. This is used to implement
12585 Returns the new statement. */
12588 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12589 const token_indent_info
&guard_tinfo
,
12593 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12594 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12595 token_indent_info body_tinfo
12596 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12601 /* Mark if () ; with a special NOP_EXPR. */
12602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12604 cp_lexer_consume_token (parser
->lexer
);
12605 statement
= add_stmt (build_empty_stmt (body_loc
));
12607 if (guard_tinfo
.keyword
== RID_IF
12608 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12609 warning_at (body_loc
, OPT_Wempty_body
,
12610 "suggest braces around empty body in an %<if%> statement");
12611 else if (guard_tinfo
.keyword
== RID_ELSE
)
12612 warning_at (body_loc
, OPT_Wempty_body
,
12613 "suggest braces around empty body in an %<else%> statement");
12615 /* if a compound is opened, we simply parse the statement directly. */
12616 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12617 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12618 /* If the token is not a `{', then we must take special action. */
12621 /* Create a compound-statement. */
12622 statement
= begin_compound_stmt (0);
12623 /* Parse the dependent-statement. */
12624 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12625 &body_loc_after_labels
);
12626 /* Finish the dummy compound-statement. */
12627 finish_compound_stmt (statement
);
12630 token_indent_info next_tinfo
12631 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12632 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12634 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12635 && next_tinfo
.type
!= CPP_SEMICOLON
)
12636 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12637 guard_tinfo
.location
, guard_tinfo
.keyword
);
12639 /* Return the statement. */
12643 /* For some dependent statements (like `while (cond) statement'), we
12644 have already created a scope. Therefore, even if the dependent
12645 statement is a compound-statement, we do not want to create another
12649 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12650 const token_indent_info
&guard_tinfo
)
12652 /* If the token is a `{', then we must take special action. */
12653 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12655 token_indent_info body_tinfo
12656 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12657 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12659 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12660 &loc_after_labels
);
12661 token_indent_info next_tinfo
12662 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12663 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12665 if (loc_after_labels
!= UNKNOWN_LOCATION
12666 && next_tinfo
.type
!= CPP_SEMICOLON
)
12667 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12668 guard_tinfo
.location
,
12669 guard_tinfo
.keyword
);
12673 /* Avoid calling cp_parser_compound_statement, so that we
12674 don't create a new scope. Do everything else by hand. */
12675 matching_braces braces
;
12676 braces
.require_open (parser
);
12677 /* If the next keyword is `__label__' we have a label declaration. */
12678 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12679 cp_parser_label_declaration (parser
);
12680 /* Parse an (optional) statement-seq. */
12681 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12682 braces
.require_close (parser
);
12686 /* Declarations [gram.dcl.dcl] */
12688 /* Parse an optional declaration-sequence.
12692 declaration-seq declaration */
12695 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12701 token
= cp_lexer_peek_token (parser
->lexer
);
12703 if (token
->type
== CPP_CLOSE_BRACE
12704 || token
->type
== CPP_EOF
12705 || token
->type
== CPP_PRAGMA_EOL
)
12708 if (token
->type
== CPP_SEMICOLON
)
12710 /* A declaration consisting of a single semicolon is
12711 invalid. Allow it unless we're being pedantic. */
12712 cp_lexer_consume_token (parser
->lexer
);
12713 if (!in_system_header_at (input_location
))
12714 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12718 /* If we're entering or exiting a region that's implicitly
12719 extern "C", modify the lang context appropriately. */
12720 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12722 push_lang_context (lang_name_c
);
12723 parser
->implicit_extern_c
= true;
12725 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12727 pop_lang_context ();
12728 parser
->implicit_extern_c
= false;
12731 if (token
->type
== CPP_PRAGMA
)
12733 /* A top-level declaration can consist solely of a #pragma.
12734 A nested declaration cannot, so this is done here and not
12735 in cp_parser_declaration. (A #pragma at block scope is
12736 handled in cp_parser_statement.) */
12737 cp_parser_pragma (parser
, pragma_external
, NULL
);
12741 /* Parse the declaration itself. */
12742 cp_parser_declaration (parser
);
12746 /* Parse a declaration.
12750 function-definition
12751 template-declaration
12752 explicit-instantiation
12753 explicit-specialization
12754 linkage-specification
12755 namespace-definition
12763 __extension__ declaration */
12766 cp_parser_declaration (cp_parser
* parser
)
12770 int saved_pedantic
;
12772 tree attributes
= NULL_TREE
;
12774 /* Check for the `__extension__' keyword. */
12775 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12777 /* Parse the qualified declaration. */
12778 cp_parser_declaration (parser
);
12779 /* Restore the PEDANTIC flag. */
12780 pedantic
= saved_pedantic
;
12785 /* Try to figure out what kind of declaration is present. */
12786 token1
= *cp_lexer_peek_token (parser
->lexer
);
12788 if (token1
.type
!= CPP_EOF
)
12789 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12792 token2
.type
= CPP_EOF
;
12793 token2
.keyword
= RID_MAX
;
12796 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12797 p
= obstack_alloc (&declarator_obstack
, 0);
12799 /* If the next token is `extern' and the following token is a string
12800 literal, then we have a linkage specification. */
12801 if (token1
.keyword
== RID_EXTERN
12802 && cp_parser_is_pure_string_literal (&token2
))
12803 cp_parser_linkage_specification (parser
);
12804 /* If the next token is `template', then we have either a template
12805 declaration, an explicit instantiation, or an explicit
12807 else if (token1
.keyword
== RID_TEMPLATE
)
12809 /* `template <>' indicates a template specialization. */
12810 if (token2
.type
== CPP_LESS
12811 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12812 cp_parser_explicit_specialization (parser
);
12813 /* `template <' indicates a template declaration. */
12814 else if (token2
.type
== CPP_LESS
)
12815 cp_parser_template_declaration (parser
, /*member_p=*/false);
12816 /* Anything else must be an explicit instantiation. */
12818 cp_parser_explicit_instantiation (parser
);
12820 /* If the next token is `export', then we have a template
12822 else if (token1
.keyword
== RID_EXPORT
)
12823 cp_parser_template_declaration (parser
, /*member_p=*/false);
12824 /* If the next token is `extern', 'static' or 'inline' and the one
12825 after that is `template', we have a GNU extended explicit
12826 instantiation directive. */
12827 else if (cp_parser_allow_gnu_extensions_p (parser
)
12828 && (token1
.keyword
== RID_EXTERN
12829 || token1
.keyword
== RID_STATIC
12830 || token1
.keyword
== RID_INLINE
)
12831 && token2
.keyword
== RID_TEMPLATE
)
12832 cp_parser_explicit_instantiation (parser
);
12833 /* If the next token is `namespace', check for a named or unnamed
12834 namespace definition. */
12835 else if (token1
.keyword
== RID_NAMESPACE
12836 && (/* A named namespace definition. */
12837 (token2
.type
== CPP_NAME
12838 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12840 || (token2
.type
== CPP_OPEN_SQUARE
12841 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12842 == CPP_OPEN_SQUARE
)
12843 /* An unnamed namespace definition. */
12844 || token2
.type
== CPP_OPEN_BRACE
12845 || token2
.keyword
== RID_ATTRIBUTE
))
12846 cp_parser_namespace_definition (parser
);
12847 /* An inline (associated) namespace definition. */
12848 else if (token1
.keyword
== RID_INLINE
12849 && token2
.keyword
== RID_NAMESPACE
)
12850 cp_parser_namespace_definition (parser
);
12851 /* Objective-C++ declaration/definition. */
12852 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12853 cp_parser_objc_declaration (parser
, NULL_TREE
);
12854 else if (c_dialect_objc ()
12855 && token1
.keyword
== RID_ATTRIBUTE
12856 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12857 cp_parser_objc_declaration (parser
, attributes
);
12858 /* At this point we may have a template declared by a concept
12860 else if (flag_concepts
12861 && cp_parser_template_declaration_after_export (parser
,
12862 /*member_p=*/false))
12865 /* Try to parse a block-declaration, or a function-definition. */
12866 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12868 /* Free any declarators allocated. */
12869 obstack_free (&declarator_obstack
, p
);
12872 /* Parse a block-declaration.
12877 namespace-alias-definition
12884 __extension__ block-declaration
12889 static_assert-declaration
12891 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12892 part of a declaration-statement. */
12895 cp_parser_block_declaration (cp_parser
*parser
,
12899 int saved_pedantic
;
12901 /* Check for the `__extension__' keyword. */
12902 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12904 /* Parse the qualified declaration. */
12905 cp_parser_block_declaration (parser
, statement_p
);
12906 /* Restore the PEDANTIC flag. */
12907 pedantic
= saved_pedantic
;
12912 /* Peek at the next token to figure out which kind of declaration is
12914 token1
= cp_lexer_peek_token (parser
->lexer
);
12916 /* If the next keyword is `asm', we have an asm-definition. */
12917 if (token1
->keyword
== RID_ASM
)
12920 cp_parser_commit_to_tentative_parse (parser
);
12921 cp_parser_asm_definition (parser
);
12923 /* If the next keyword is `namespace', we have a
12924 namespace-alias-definition. */
12925 else if (token1
->keyword
== RID_NAMESPACE
)
12926 cp_parser_namespace_alias_definition (parser
);
12927 /* If the next keyword is `using', we have a
12928 using-declaration, a using-directive, or an alias-declaration. */
12929 else if (token1
->keyword
== RID_USING
)
12934 cp_parser_commit_to_tentative_parse (parser
);
12935 /* If the token after `using' is `namespace', then we have a
12936 using-directive. */
12937 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12938 if (token2
->keyword
== RID_NAMESPACE
)
12939 cp_parser_using_directive (parser
);
12940 /* If the second token after 'using' is '=', then we have an
12941 alias-declaration. */
12942 else if (cxx_dialect
>= cxx11
12943 && token2
->type
== CPP_NAME
12944 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12945 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12946 cp_parser_alias_declaration (parser
);
12947 /* Otherwise, it's a using-declaration. */
12949 cp_parser_using_declaration (parser
,
12950 /*access_declaration_p=*/false);
12952 /* If the next keyword is `__label__' we have a misplaced label
12954 else if (token1
->keyword
== RID_LABEL
)
12956 cp_lexer_consume_token (parser
->lexer
);
12957 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12958 cp_parser_skip_to_end_of_statement (parser
);
12959 /* If the next token is now a `;', consume it. */
12960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12961 cp_lexer_consume_token (parser
->lexer
);
12963 /* If the next token is `static_assert' we have a static assertion. */
12964 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12965 cp_parser_static_assert (parser
, /*member_p=*/false);
12966 /* Anything else must be a simple-declaration. */
12968 cp_parser_simple_declaration (parser
, !statement_p
,
12969 /*maybe_range_for_decl*/NULL
);
12972 /* Parse a simple-declaration.
12974 simple-declaration:
12975 decl-specifier-seq [opt] init-declarator-list [opt] ;
12976 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12977 brace-or-equal-initializer ;
12979 init-declarator-list:
12981 init-declarator-list , init-declarator
12983 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12984 function-definition as a simple-declaration.
12986 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12987 parsed declaration if it is an uninitialized single declarator not followed
12988 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12989 if present, will not be consumed. */
12992 cp_parser_simple_declaration (cp_parser
* parser
,
12993 bool function_definition_allowed_p
,
12994 tree
*maybe_range_for_decl
)
12996 cp_decl_specifier_seq decl_specifiers
;
12997 int declares_class_or_enum
;
12998 bool saw_declarator
;
12999 location_t comma_loc
= UNKNOWN_LOCATION
;
13000 location_t init_loc
= UNKNOWN_LOCATION
;
13002 if (maybe_range_for_decl
)
13003 *maybe_range_for_decl
= NULL_TREE
;
13005 /* Defer access checks until we know what is being declared; the
13006 checks for names appearing in the decl-specifier-seq should be
13007 done as if we were in the scope of the thing being declared. */
13008 push_deferring_access_checks (dk_deferred
);
13010 /* Parse the decl-specifier-seq. We have to keep track of whether
13011 or not the decl-specifier-seq declares a named class or
13012 enumeration type, since that is the only case in which the
13013 init-declarator-list is allowed to be empty.
13017 In a simple-declaration, the optional init-declarator-list can be
13018 omitted only when declaring a class or enumeration, that is when
13019 the decl-specifier-seq contains either a class-specifier, an
13020 elaborated-type-specifier, or an enum-specifier. */
13021 cp_parser_decl_specifier_seq (parser
,
13022 CP_PARSER_FLAGS_OPTIONAL
,
13024 &declares_class_or_enum
);
13025 /* We no longer need to defer access checks. */
13026 stop_deferring_access_checks ();
13028 /* In a block scope, a valid declaration must always have a
13029 decl-specifier-seq. By not trying to parse declarators, we can
13030 resolve the declaration/expression ambiguity more quickly. */
13031 if (!function_definition_allowed_p
13032 && !decl_specifiers
.any_specifiers_p
)
13034 cp_parser_error (parser
, "expected declaration");
13038 /* If the next two tokens are both identifiers, the code is
13039 erroneous. The usual cause of this situation is code like:
13043 where "T" should name a type -- but does not. */
13044 if (!decl_specifiers
.any_type_specifiers_p
13045 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13047 /* If parsing tentatively, we should commit; we really are
13048 looking at a declaration. */
13049 cp_parser_commit_to_tentative_parse (parser
);
13054 cp_parser_maybe_commit_to_declaration (parser
,
13055 decl_specifiers
.any_specifiers_p
);
13057 /* Look for C++17 decomposition declaration. */
13058 for (size_t n
= 1; ; n
++)
13059 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13060 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13062 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13063 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13064 && decl_specifiers
.any_specifiers_p
)
13067 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13068 maybe_range_for_decl
,
13071 /* The next token should be either a `,' or a `;'. */
13072 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13073 /* If it's a `;', we are done. */
13074 if (token
->type
== CPP_SEMICOLON
)
13076 else if (maybe_range_for_decl
)
13078 if (*maybe_range_for_decl
== NULL_TREE
)
13079 *maybe_range_for_decl
= error_mark_node
;
13082 /* Anything else is an error. */
13085 /* If we have already issued an error message we don't need
13086 to issue another one. */
13087 if ((decl
!= error_mark_node
13088 && DECL_INITIAL (decl
) != error_mark_node
)
13089 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13090 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13091 /* Skip tokens until we reach the end of the statement. */
13092 cp_parser_skip_to_end_of_statement (parser
);
13093 /* If the next token is now a `;', consume it. */
13094 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13095 cp_lexer_consume_token (parser
->lexer
);
13103 bool auto_specifier_p
;
13104 /* NULL_TREE if both variable and function declaration are allowed,
13105 error_mark_node if function declaration are not allowed and
13106 a FUNCTION_DECL that should be diagnosed if it is followed by
13107 variable declarations. */
13108 tree auto_function_declaration
;
13110 last_type
= NULL_TREE
;
13112 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13113 auto_function_declaration
= NULL_TREE
;
13115 /* Keep going until we hit the `;' at the end of the simple
13117 saw_declarator
= false;
13118 while (cp_lexer_next_token_is_not (parser
->lexer
,
13122 bool function_definition_p
;
13124 tree auto_result
= NULL_TREE
;
13126 if (saw_declarator
)
13128 /* If we are processing next declarator, comma is expected */
13129 token
= cp_lexer_peek_token (parser
->lexer
);
13130 gcc_assert (token
->type
== CPP_COMMA
);
13131 cp_lexer_consume_token (parser
->lexer
);
13132 if (maybe_range_for_decl
)
13134 *maybe_range_for_decl
= error_mark_node
;
13135 if (comma_loc
== UNKNOWN_LOCATION
)
13136 comma_loc
= token
->location
;
13140 saw_declarator
= true;
13142 /* Parse the init-declarator. */
13143 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13145 function_definition_allowed_p
,
13146 /*member_p=*/false,
13147 declares_class_or_enum
,
13148 &function_definition_p
,
13149 maybe_range_for_decl
,
13152 /* If an error occurred while parsing tentatively, exit quickly.
13153 (That usually happens when in the body of a function; each
13154 statement is treated as a declaration-statement until proven
13156 if (cp_parser_error_occurred (parser
))
13159 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13161 /* If the init-declarator-list contains more than one
13162 init-declarator, they shall all form declarations of
13164 if (auto_function_declaration
== NULL_TREE
)
13165 auto_function_declaration
13166 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13167 else if (TREE_CODE (decl
) == FUNCTION_DECL
13168 || auto_function_declaration
!= error_mark_node
)
13170 error_at (decl_specifiers
.locations
[ds_type_spec
],
13171 "non-variable %qD in declaration with more than one "
13172 "declarator with placeholder type",
13173 TREE_CODE (decl
) == FUNCTION_DECL
13174 ? decl
: auto_function_declaration
);
13175 auto_function_declaration
= error_mark_node
;
13180 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13183 && last_type
!= error_mark_node
13184 && !same_type_p (auto_result
, last_type
))
13186 /* If the list of declarators contains more than one declarator,
13187 the type of each declared variable is determined as described
13188 above. If the type deduced for the template parameter U is not
13189 the same in each deduction, the program is ill-formed. */
13190 error_at (decl_specifiers
.locations
[ds_type_spec
],
13191 "inconsistent deduction for %qT: %qT and then %qT",
13192 decl_specifiers
.type
, last_type
, auto_result
);
13193 last_type
= error_mark_node
;
13196 last_type
= auto_result
;
13199 /* Handle function definitions specially. */
13200 if (function_definition_p
)
13202 /* If the next token is a `,', then we are probably
13203 processing something like:
13207 which is erroneous. */
13208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13210 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13211 error_at (token
->location
,
13213 " declarations and function-definitions is forbidden");
13215 /* Otherwise, we're done with the list of declarators. */
13218 pop_deferring_access_checks ();
13222 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13223 *maybe_range_for_decl
= decl
;
13224 /* The next token should be either a `,' or a `;'. */
13225 token
= cp_lexer_peek_token (parser
->lexer
);
13226 /* If it's a `,', there are more declarators to come. */
13227 if (token
->type
== CPP_COMMA
)
13228 /* will be consumed next time around */;
13229 /* If it's a `;', we are done. */
13230 else if (token
->type
== CPP_SEMICOLON
)
13232 else if (maybe_range_for_decl
)
13234 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13235 permerror (decl_specifiers
.locations
[ds_type_spec
],
13236 "types may not be defined in a for-range-declaration");
13239 /* Anything else is an error. */
13242 /* If we have already issued an error message we don't need
13243 to issue another one. */
13244 if ((decl
!= error_mark_node
13245 && DECL_INITIAL (decl
) != error_mark_node
)
13246 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13247 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13248 /* Skip tokens until we reach the end of the statement. */
13249 cp_parser_skip_to_end_of_statement (parser
);
13250 /* If the next token is now a `;', consume it. */
13251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13252 cp_lexer_consume_token (parser
->lexer
);
13255 /* After the first time around, a function-definition is not
13256 allowed -- even if it was OK at first. For example:
13261 function_definition_allowed_p
= false;
13264 /* Issue an error message if no declarators are present, and the
13265 decl-specifier-seq does not itself declare a class or
13266 enumeration: [dcl.dcl]/3. */
13267 if (!saw_declarator
)
13269 if (cp_parser_declares_only_class_p (parser
))
13271 if (!declares_class_or_enum
13272 && decl_specifiers
.type
13273 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13274 /* Ensure an error is issued anyway when finish_decltype_type,
13275 called via cp_parser_decl_specifier_seq, returns a class or
13276 an enumeration (c++/51786). */
13277 decl_specifiers
.type
= NULL_TREE
;
13278 shadow_tag (&decl_specifiers
);
13280 /* Perform any deferred access checks. */
13281 perform_deferred_access_checks (tf_warning_or_error
);
13284 /* Consume the `;'. */
13286 if (!maybe_range_for_decl
)
13287 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13288 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13290 if (init_loc
!= UNKNOWN_LOCATION
)
13291 error_at (init_loc
, "initializer in range-based %<for%> loop");
13292 if (comma_loc
!= UNKNOWN_LOCATION
)
13293 error_at (comma_loc
,
13294 "multiple declarations in range-based %<for%> loop");
13298 pop_deferring_access_checks ();
13301 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13302 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13306 cp_parser_decomposition_declaration (cp_parser
*parser
,
13307 cp_decl_specifier_seq
*decl_specifiers
,
13308 tree
*maybe_range_for_decl
,
13309 location_t
*init_loc
)
13311 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13312 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13313 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13315 /* Parse the identifier-list. */
13316 auto_vec
<cp_expr
, 10> v
;
13317 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13320 cp_expr e
= cp_parser_identifier (parser
);
13321 if (e
.get_value () == error_mark_node
)
13324 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13326 cp_lexer_consume_token (parser
->lexer
);
13329 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13330 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13332 end_loc
= UNKNOWN_LOCATION
;
13333 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13335 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13336 cp_lexer_consume_token (parser
->lexer
);
13339 cp_parser_skip_to_end_of_statement (parser
);
13340 return error_mark_node
;
13344 if (cxx_dialect
< cxx17
)
13345 pedwarn (loc
, 0, "structured bindings only available with "
13346 "-std=c++17 or -std=gnu++17");
13349 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13350 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13351 declarator
->id_loc
= loc
;
13352 if (ref_qual
!= REF_QUAL_NONE
)
13353 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13354 ref_qual
== REF_QUAL_RVALUE
,
13356 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13357 NULL_TREE
, decl_specifiers
->attributes
,
13359 tree orig_decl
= decl
;
13363 cp_decl_specifier_seq decl_specs
;
13364 clear_decl_specs (&decl_specs
);
13365 decl_specs
.type
= make_auto ();
13367 FOR_EACH_VEC_ELT (v
, i
, e
)
13370 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13372 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13373 declarator
->id_loc
= e
.get_location ();
13374 tree elt_pushed_scope
;
13375 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13376 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13377 if (decl2
== error_mark_node
)
13378 decl
= error_mark_node
;
13379 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13381 /* Ensure we've diagnosed redeclaration if we aren't creating
13383 gcc_assert (errorcount
);
13384 decl
= error_mark_node
;
13388 if (elt_pushed_scope
)
13389 pop_scope (elt_pushed_scope
);
13394 error_at (loc
, "empty structured binding declaration");
13395 decl
= error_mark_node
;
13398 if (maybe_range_for_decl
== NULL
13399 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13401 bool non_constant_p
= false, is_direct_init
= false;
13402 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13403 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13405 if (initializer
== NULL_TREE
13406 || (TREE_CODE (initializer
) == TREE_LIST
13407 && TREE_CHAIN (initializer
))
13409 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13410 && CONSTRUCTOR_NELTS (initializer
) != 1))
13412 error_at (loc
, "invalid initializer for structured binding "
13414 initializer
= error_mark_node
;
13417 if (decl
!= error_mark_node
)
13419 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13420 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13421 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13422 cp_finish_decomp (decl
, prev
, v
.length ());
13425 else if (decl
!= error_mark_node
)
13427 *maybe_range_for_decl
= prev
;
13428 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13429 the underlying DECL. */
13430 cp_finish_decomp (decl
, prev
, v
.length ());
13434 pop_scope (pushed_scope
);
13436 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13438 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13439 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13445 /* Parse a decl-specifier-seq.
13447 decl-specifier-seq:
13448 decl-specifier-seq [opt] decl-specifier
13449 decl-specifier attribute-specifier-seq [opt] (C++11)
13452 storage-class-specifier
13463 Concepts Extension:
13468 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13470 The parser flags FLAGS is used to control type-specifier parsing.
13472 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13475 1: one of the decl-specifiers is an elaborated-type-specifier
13476 (i.e., a type declaration)
13477 2: one of the decl-specifiers is an enum-specifier or a
13478 class-specifier (i.e., a type definition)
13483 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13484 cp_parser_flags flags
,
13485 cp_decl_specifier_seq
*decl_specs
,
13486 int* declares_class_or_enum
)
13488 bool constructor_possible_p
= !parser
->in_declarator_p
;
13489 bool found_decl_spec
= false;
13490 cp_token
*start_token
= NULL
;
13493 /* Clear DECL_SPECS. */
13494 clear_decl_specs (decl_specs
);
13496 /* Assume no class or enumeration type is declared. */
13497 *declares_class_or_enum
= 0;
13499 /* Keep reading specifiers until there are no more to read. */
13502 bool constructor_p
;
13506 /* Peek at the next token. */
13507 token
= cp_lexer_peek_token (parser
->lexer
);
13509 /* Save the first token of the decl spec list for error
13512 start_token
= token
;
13513 /* Handle attributes. */
13514 if (cp_next_tokens_can_be_attribute_p (parser
))
13516 /* Parse the attributes. */
13517 tree attrs
= cp_parser_attributes_opt (parser
);
13519 /* In a sequence of declaration specifiers, c++11 attributes
13520 appertain to the type that precede them. In that case
13523 The attribute-specifier-seq affects the type only for
13524 the declaration it appears in, not other declarations
13525 involving the same type.
13527 But for now let's force the user to position the
13528 attribute either at the beginning of the declaration or
13529 after the declarator-id, which would clearly mean that it
13530 applies to the declarator. */
13531 if (cxx11_attribute_p (attrs
))
13533 if (!found_decl_spec
)
13534 /* The c++11 attribute is at the beginning of the
13535 declaration. It appertains to the entity being
13539 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13541 /* This is an attribute following a
13542 class-specifier. */
13543 if (decl_specs
->type_definition_p
)
13544 warn_misplaced_attr_for_class_type (token
->location
,
13550 decl_specs
->std_attributes
13551 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13552 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13553 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13559 decl_specs
->attributes
13560 = attr_chainon (decl_specs
->attributes
, attrs
);
13561 if (decl_specs
->locations
[ds_attribute
] == 0)
13562 decl_specs
->locations
[ds_attribute
] = token
->location
;
13565 /* Assume we will find a decl-specifier keyword. */
13566 found_decl_spec
= true;
13567 /* If the next token is an appropriate keyword, we can simply
13568 add it to the list. */
13569 switch (token
->keyword
)
13575 if (!at_class_scope_p ())
13577 gcc_rich_location
richloc (token
->location
);
13578 richloc
.add_fixit_remove ();
13579 error_at (&richloc
, "%<friend%> used outside of class");
13580 cp_lexer_purge_token (parser
->lexer
);
13585 /* Consume the token. */
13586 cp_lexer_consume_token (parser
->lexer
);
13590 case RID_CONSTEXPR
:
13592 cp_lexer_consume_token (parser
->lexer
);
13597 cp_lexer_consume_token (parser
->lexer
);
13600 /* function-specifier:
13607 cp_parser_function_specifier_opt (parser
, decl_specs
);
13614 /* Consume the token. */
13615 cp_lexer_consume_token (parser
->lexer
);
13616 /* A constructor declarator cannot appear in a typedef. */
13617 constructor_possible_p
= false;
13618 /* The "typedef" keyword can only occur in a declaration; we
13619 may as well commit at this point. */
13620 cp_parser_commit_to_tentative_parse (parser
);
13622 if (decl_specs
->storage_class
!= sc_none
)
13623 decl_specs
->conflicting_specifiers_p
= true;
13626 /* storage-class-specifier:
13636 if (cxx_dialect
== cxx98
)
13638 /* Consume the token. */
13639 cp_lexer_consume_token (parser
->lexer
);
13641 /* Complain about `auto' as a storage specifier, if
13642 we're complaining about C++0x compatibility. */
13643 gcc_rich_location
richloc (token
->location
);
13644 richloc
.add_fixit_remove ();
13645 warning_at (&richloc
, OPT_Wc__11_compat
,
13646 "%<auto%> changes meaning in C++11; "
13647 "please remove it");
13649 /* Set the storage class anyway. */
13650 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13654 /* C++0x auto type-specifier. */
13655 found_decl_spec
= false;
13662 /* Consume the token. */
13663 cp_lexer_consume_token (parser
->lexer
);
13664 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13668 /* Consume the token. */
13670 cp_lexer_consume_token (parser
->lexer
);
13674 /* We did not yet find a decl-specifier yet. */
13675 found_decl_spec
= false;
13679 if (found_decl_spec
13680 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13681 && token
->keyword
!= RID_CONSTEXPR
)
13682 error ("decl-specifier invalid in condition");
13684 if (found_decl_spec
13685 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13686 && token
->keyword
!= RID_MUTABLE
13687 && token
->keyword
!= RID_CONSTEXPR
)
13688 error_at (token
->location
, "%qD invalid in lambda",
13689 ridpointers
[token
->keyword
]);
13692 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13694 /* Constructors are a special case. The `S' in `S()' is not a
13695 decl-specifier; it is the beginning of the declarator. */
13697 = (!found_decl_spec
13698 && constructor_possible_p
13699 && (cp_parser_constructor_declarator_p
13700 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13702 /* If we don't have a DECL_SPEC yet, then we must be looking at
13703 a type-specifier. */
13704 if (!found_decl_spec
&& !constructor_p
)
13706 int decl_spec_declares_class_or_enum
;
13707 bool is_cv_qualifier
;
13711 = cp_parser_type_specifier (parser
, flags
,
13713 /*is_declaration=*/true,
13714 &decl_spec_declares_class_or_enum
,
13716 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13718 /* If this type-specifier referenced a user-defined type
13719 (a typedef, class-name, etc.), then we can't allow any
13720 more such type-specifiers henceforth.
13724 The longest sequence of decl-specifiers that could
13725 possibly be a type name is taken as the
13726 decl-specifier-seq of a declaration. The sequence shall
13727 be self-consistent as described below.
13731 As a general rule, at most one type-specifier is allowed
13732 in the complete decl-specifier-seq of a declaration. The
13733 only exceptions are the following:
13735 -- const or volatile can be combined with any other
13738 -- signed or unsigned can be combined with char, long,
13746 void g (const int Pc);
13748 Here, Pc is *not* part of the decl-specifier seq; it's
13749 the declarator. Therefore, once we see a type-specifier
13750 (other than a cv-qualifier), we forbid any additional
13751 user-defined types. We *do* still allow things like `int
13752 int' to be considered a decl-specifier-seq, and issue the
13753 error message later. */
13754 if (type_spec
&& !is_cv_qualifier
)
13755 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13756 /* A constructor declarator cannot follow a type-specifier. */
13759 constructor_possible_p
= false;
13760 found_decl_spec
= true;
13761 if (!is_cv_qualifier
)
13762 decl_specs
->any_type_specifiers_p
= true;
13766 /* If we still do not have a DECL_SPEC, then there are no more
13767 decl-specifiers. */
13768 if (!found_decl_spec
)
13771 decl_specs
->any_specifiers_p
= true;
13772 /* After we see one decl-specifier, further decl-specifiers are
13773 always optional. */
13774 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13777 /* Don't allow a friend specifier with a class definition. */
13778 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13779 && (*declares_class_or_enum
& 2))
13780 error_at (decl_specs
->locations
[ds_friend
],
13781 "class definition may not be declared a friend");
13784 /* Parse an (optional) storage-class-specifier.
13786 storage-class-specifier:
13795 storage-class-specifier:
13798 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13801 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13803 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13806 if (cxx_dialect
!= cxx98
)
13808 /* Fall through for C++98. */
13809 gcc_fallthrough ();
13816 /* Consume the token. */
13817 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13824 /* Parse an (optional) function-specifier.
13826 function-specifier:
13831 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13832 Updates DECL_SPECS, if it is non-NULL. */
13835 cp_parser_function_specifier_opt (cp_parser
* parser
,
13836 cp_decl_specifier_seq
*decl_specs
)
13838 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13839 switch (token
->keyword
)
13842 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13846 /* 14.5.2.3 [temp.mem]
13848 A member function template shall not be virtual. */
13849 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13850 && current_class_type
)
13851 error_at (token
->location
, "templates may not be %<virtual%>");
13853 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13857 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13864 /* Consume the token. */
13865 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13868 /* Parse a linkage-specification.
13870 linkage-specification:
13871 extern string-literal { declaration-seq [opt] }
13872 extern string-literal declaration */
13875 cp_parser_linkage_specification (cp_parser
* parser
)
13879 /* Look for the `extern' keyword. */
13880 cp_token
*extern_token
13881 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13883 /* Look for the string-literal. */
13884 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13885 linkage
= cp_parser_string_literal (parser
, false, false);
13887 /* Transform the literal into an identifier. If the literal is a
13888 wide-character string, or contains embedded NULs, then we can't
13889 handle it as the user wants. */
13890 if (strlen (TREE_STRING_POINTER (linkage
))
13891 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13893 cp_parser_error (parser
, "invalid linkage-specification");
13894 /* Assume C++ linkage. */
13895 linkage
= lang_name_cplusplus
;
13898 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13900 /* We're now using the new linkage. */
13901 push_lang_context (linkage
);
13903 /* Preserve the location of the the innermost linkage specification,
13904 tracking the locations of nested specifications via a local. */
13905 location_t saved_location
13906 = parser
->innermost_linkage_specification_location
;
13907 /* Construct a location ranging from the start of the "extern" to
13908 the end of the string-literal, with the caret at the start, e.g.:
13912 parser
->innermost_linkage_specification_location
13913 = make_location (extern_token
->location
,
13914 extern_token
->location
,
13915 get_finish (string_token
->location
));
13917 /* If the next token is a `{', then we're using the first
13919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13921 cp_ensure_no_omp_declare_simd (parser
);
13922 cp_ensure_no_oacc_routine (parser
);
13924 /* Consume the `{' token. */
13925 matching_braces braces
;
13926 braces
.consume_open (parser
)->location
;
13927 /* Parse the declarations. */
13928 cp_parser_declaration_seq_opt (parser
);
13929 /* Look for the closing `}'. */
13930 braces
.require_close (parser
);
13932 /* Otherwise, there's just one declaration. */
13935 bool saved_in_unbraced_linkage_specification_p
;
13937 saved_in_unbraced_linkage_specification_p
13938 = parser
->in_unbraced_linkage_specification_p
;
13939 parser
->in_unbraced_linkage_specification_p
= true;
13940 cp_parser_declaration (parser
);
13941 parser
->in_unbraced_linkage_specification_p
13942 = saved_in_unbraced_linkage_specification_p
;
13945 /* We're done with the linkage-specification. */
13946 pop_lang_context ();
13948 /* Restore location of parent linkage specification, if any. */
13949 parser
->innermost_linkage_specification_location
= saved_location
;
13952 /* Parse a static_assert-declaration.
13954 static_assert-declaration:
13955 static_assert ( constant-expression , string-literal ) ;
13956 static_assert ( constant-expression ) ; (C++17)
13958 If MEMBER_P, this static_assert is a class member. */
13961 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13964 location_t token_loc
;
13968 /* Peek at the `static_assert' token so we can keep track of exactly
13969 where the static assertion started. */
13970 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13972 /* Look for the `static_assert' keyword. */
13973 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13977 /* We know we are in a static assertion; commit to any tentative
13979 if (cp_parser_parsing_tentatively (parser
))
13980 cp_parser_commit_to_tentative_parse (parser
);
13982 /* Parse the `(' starting the static assertion condition. */
13983 matching_parens parens
;
13984 parens
.require_open (parser
);
13986 /* Parse the constant-expression. Allow a non-constant expression
13987 here in order to give better diagnostics in finish_static_assert. */
13989 cp_parser_constant_expression (parser
,
13990 /*allow_non_constant_p=*/true,
13991 /*non_constant_p=*/&dummy
);
13993 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13995 if (cxx_dialect
< cxx17
)
13996 pedwarn (input_location
, OPT_Wpedantic
,
13997 "static_assert without a message "
13998 "only available with -std=c++17 or -std=gnu++17");
14000 cp_lexer_consume_token (parser
->lexer
);
14001 message
= build_string (1, "");
14002 TREE_TYPE (message
) = char_array_type_node
;
14003 fix_string_type (message
);
14007 /* Parse the separating `,'. */
14008 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14010 /* Parse the string-literal message. */
14011 message
= cp_parser_string_literal (parser
,
14012 /*translate=*/false,
14015 /* A `)' completes the static assertion. */
14016 if (!parens
.require_close (parser
))
14017 cp_parser_skip_to_closing_parenthesis (parser
,
14018 /*recovering=*/true,
14019 /*or_comma=*/false,
14020 /*consume_paren=*/true);
14023 /* A semicolon terminates the declaration. */
14024 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14026 /* Get the location for the static assertion. Use that of the
14027 condition if available, otherwise, use that of the "static_assert"
14029 location_t assert_loc
= condition
.get_location ();
14030 if (assert_loc
== UNKNOWN_LOCATION
)
14031 assert_loc
= token_loc
;
14033 /* Complete the static assertion, which may mean either processing
14034 the static assert now or saving it for template instantiation. */
14035 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14038 /* Parse the expression in decltype ( expression ). */
14041 cp_parser_decltype_expr (cp_parser
*parser
,
14042 bool &id_expression_or_member_access_p
)
14044 cp_token
*id_expr_start_token
;
14047 /* Since we're going to preserve any side-effects from this parse, set up a
14048 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14049 in the expression. */
14050 tentative_firewall
firewall (parser
);
14052 /* First, try parsing an id-expression. */
14053 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14054 cp_parser_parse_tentatively (parser
);
14055 expr
= cp_parser_id_expression (parser
,
14056 /*template_keyword_p=*/false,
14057 /*check_dependency_p=*/true,
14058 /*template_p=*/NULL
,
14059 /*declarator_p=*/false,
14060 /*optional_p=*/false);
14062 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14064 bool non_integral_constant_expression_p
= false;
14065 tree id_expression
= expr
;
14067 const char *error_msg
;
14069 if (identifier_p (expr
))
14070 /* Lookup the name we got back from the id-expression. */
14071 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14072 id_expr_start_token
->location
);
14074 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14075 /* A template without args is not a complete id-expression. */
14076 expr
= error_mark_node
;
14079 && expr
!= error_mark_node
14080 && TREE_CODE (expr
) != TYPE_DECL
14081 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14082 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14083 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14085 /* Complete lookup of the id-expression. */
14086 expr
= (finish_id_expression
14087 (id_expression
, expr
, parser
->scope
, &idk
,
14088 /*integral_constant_expression_p=*/false,
14089 /*allow_non_integral_constant_expression_p=*/true,
14090 &non_integral_constant_expression_p
,
14091 /*template_p=*/false,
14093 /*address_p=*/false,
14094 /*template_arg_p=*/false,
14096 id_expr_start_token
->location
));
14098 if (expr
== error_mark_node
)
14099 /* We found an id-expression, but it was something that we
14100 should not have found. This is an error, not something
14101 we can recover from, so note that we found an
14102 id-expression and we'll recover as gracefully as
14104 id_expression_or_member_access_p
= true;
14108 && expr
!= error_mark_node
14109 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14110 /* We have an id-expression. */
14111 id_expression_or_member_access_p
= true;
14114 if (!id_expression_or_member_access_p
)
14116 /* Abort the id-expression parse. */
14117 cp_parser_abort_tentative_parse (parser
);
14119 /* Parsing tentatively, again. */
14120 cp_parser_parse_tentatively (parser
);
14122 /* Parse a class member access. */
14123 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14124 /*cast_p=*/false, /*decltype*/true,
14125 /*member_access_only_p=*/true, NULL
);
14128 && expr
!= error_mark_node
14129 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14130 /* We have an id-expression. */
14131 id_expression_or_member_access_p
= true;
14134 if (id_expression_or_member_access_p
)
14135 /* We have parsed the complete id-expression or member access. */
14136 cp_parser_parse_definitely (parser
);
14139 /* Abort our attempt to parse an id-expression or member access
14141 cp_parser_abort_tentative_parse (parser
);
14143 /* Commit to the tentative_firewall so we get syntax errors. */
14144 cp_parser_commit_to_tentative_parse (parser
);
14146 /* Parse a full expression. */
14147 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14148 /*decltype_p=*/true);
14154 /* Parse a `decltype' type. Returns the type.
14156 simple-type-specifier:
14157 decltype ( expression )
14159 decltype ( auto ) */
14162 cp_parser_decltype (cp_parser
*parser
)
14164 bool id_expression_or_member_access_p
= false;
14165 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14167 if (start_token
->type
== CPP_DECLTYPE
)
14169 /* Already parsed. */
14170 cp_lexer_consume_token (parser
->lexer
);
14171 return saved_checks_value (start_token
->u
.tree_check_value
);
14174 /* Look for the `decltype' token. */
14175 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14176 return error_mark_node
;
14178 /* Parse the opening `('. */
14179 matching_parens parens
;
14180 if (!parens
.require_open (parser
))
14181 return error_mark_node
;
14183 push_deferring_access_checks (dk_deferred
);
14185 tree expr
= NULL_TREE
;
14187 if (cxx_dialect
>= cxx14
14188 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14189 /* decltype (auto) */
14190 cp_lexer_consume_token (parser
->lexer
);
14193 /* decltype (expression) */
14195 /* Types cannot be defined in a `decltype' expression. Save away the
14196 old message and set the new one. */
14197 const char *saved_message
= parser
->type_definition_forbidden_message
;
14198 parser
->type_definition_forbidden_message
14199 = G_("types may not be defined in %<decltype%> expressions");
14201 /* The restrictions on constant-expressions do not apply inside
14202 decltype expressions. */
14203 bool saved_integral_constant_expression_p
14204 = parser
->integral_constant_expression_p
;
14205 bool saved_non_integral_constant_expression_p
14206 = parser
->non_integral_constant_expression_p
;
14207 parser
->integral_constant_expression_p
= false;
14209 /* Within a parenthesized expression, a `>' token is always
14210 the greater-than operator. */
14211 bool saved_greater_than_is_operator_p
14212 = parser
->greater_than_is_operator_p
;
14213 parser
->greater_than_is_operator_p
= true;
14215 /* Do not actually evaluate the expression. */
14216 ++cp_unevaluated_operand
;
14218 /* Do not warn about problems with the expression. */
14219 ++c_inhibit_evaluation_warnings
;
14221 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14223 /* Go back to evaluating expressions. */
14224 --cp_unevaluated_operand
;
14225 --c_inhibit_evaluation_warnings
;
14227 /* The `>' token might be the end of a template-id or
14228 template-parameter-list now. */
14229 parser
->greater_than_is_operator_p
14230 = saved_greater_than_is_operator_p
;
14232 /* Restore the old message and the integral constant expression
14234 parser
->type_definition_forbidden_message
= saved_message
;
14235 parser
->integral_constant_expression_p
14236 = saved_integral_constant_expression_p
;
14237 parser
->non_integral_constant_expression_p
14238 = saved_non_integral_constant_expression_p
;
14241 /* Parse to the closing `)'. */
14242 if (!parens
.require_close (parser
))
14244 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14245 /*consume_paren=*/true);
14246 pop_deferring_access_checks ();
14247 return error_mark_node
;
14253 expr
= make_decltype_auto ();
14254 AUTO_IS_DECLTYPE (expr
) = true;
14257 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14258 tf_warning_or_error
);
14260 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14262 start_token
->type
= CPP_DECLTYPE
;
14263 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14264 start_token
->u
.tree_check_value
->value
= expr
;
14265 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14266 start_token
->keyword
= RID_MAX
;
14267 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14269 pop_to_parent_deferring_access_checks ();
14274 /* Special member functions [gram.special] */
14276 /* Parse a conversion-function-id.
14278 conversion-function-id:
14279 operator conversion-type-id
14281 Returns an IDENTIFIER_NODE representing the operator. */
14284 cp_parser_conversion_function_id (cp_parser
* parser
)
14288 tree saved_qualifying_scope
;
14289 tree saved_object_scope
;
14290 tree pushed_scope
= NULL_TREE
;
14292 /* Look for the `operator' token. */
14293 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14294 return error_mark_node
;
14295 /* When we parse the conversion-type-id, the current scope will be
14296 reset. However, we need that information in able to look up the
14297 conversion function later, so we save it here. */
14298 saved_scope
= parser
->scope
;
14299 saved_qualifying_scope
= parser
->qualifying_scope
;
14300 saved_object_scope
= parser
->object_scope
;
14301 /* We must enter the scope of the class so that the names of
14302 entities declared within the class are available in the
14303 conversion-type-id. For example, consider:
14310 S::operator I() { ... }
14312 In order to see that `I' is a type-name in the definition, we
14313 must be in the scope of `S'. */
14315 pushed_scope
= push_scope (saved_scope
);
14316 /* Parse the conversion-type-id. */
14317 type
= cp_parser_conversion_type_id (parser
);
14318 /* Leave the scope of the class, if any. */
14320 pop_scope (pushed_scope
);
14321 /* Restore the saved scope. */
14322 parser
->scope
= saved_scope
;
14323 parser
->qualifying_scope
= saved_qualifying_scope
;
14324 parser
->object_scope
= saved_object_scope
;
14325 /* If the TYPE is invalid, indicate failure. */
14326 if (type
== error_mark_node
)
14327 return error_mark_node
;
14328 return make_conv_op_name (type
);
14331 /* Parse a conversion-type-id:
14333 conversion-type-id:
14334 type-specifier-seq conversion-declarator [opt]
14336 Returns the TYPE specified. */
14339 cp_parser_conversion_type_id (cp_parser
* parser
)
14342 cp_decl_specifier_seq type_specifiers
;
14343 cp_declarator
*declarator
;
14344 tree type_specified
;
14345 const char *saved_message
;
14347 /* Parse the attributes. */
14348 attributes
= cp_parser_attributes_opt (parser
);
14350 saved_message
= parser
->type_definition_forbidden_message
;
14351 parser
->type_definition_forbidden_message
14352 = G_("types may not be defined in a conversion-type-id");
14354 /* Parse the type-specifiers. */
14355 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14356 /*is_trailing_return=*/false,
14359 parser
->type_definition_forbidden_message
= saved_message
;
14361 /* If that didn't work, stop. */
14362 if (type_specifiers
.type
== error_mark_node
)
14363 return error_mark_node
;
14364 /* Parse the conversion-declarator. */
14365 declarator
= cp_parser_conversion_declarator_opt (parser
);
14367 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14368 /*initialized=*/0, &attributes
);
14370 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14372 /* Don't give this error when parsing tentatively. This happens to
14373 work because we always parse this definitively once. */
14374 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14375 && type_uses_auto (type_specified
))
14377 if (cxx_dialect
< cxx14
)
14379 error ("invalid use of %<auto%> in conversion operator");
14380 return error_mark_node
;
14382 else if (template_parm_scope_p ())
14383 warning (0, "use of %<auto%> in member template "
14384 "conversion operator can never be deduced");
14387 return type_specified
;
14390 /* Parse an (optional) conversion-declarator.
14392 conversion-declarator:
14393 ptr-operator conversion-declarator [opt]
14397 static cp_declarator
*
14398 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14400 enum tree_code code
;
14401 tree class_type
, std_attributes
= NULL_TREE
;
14402 cp_cv_quals cv_quals
;
14404 /* We don't know if there's a ptr-operator next, or not. */
14405 cp_parser_parse_tentatively (parser
);
14406 /* Try the ptr-operator. */
14407 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14409 /* If it worked, look for more conversion-declarators. */
14410 if (cp_parser_parse_definitely (parser
))
14412 cp_declarator
*declarator
;
14414 /* Parse another optional declarator. */
14415 declarator
= cp_parser_conversion_declarator_opt (parser
);
14417 declarator
= cp_parser_make_indirect_declarator
14418 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14426 /* Parse an (optional) ctor-initializer.
14429 : mem-initializer-list */
14432 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14434 /* If the next token is not a `:', then there is no
14435 ctor-initializer. */
14436 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14438 /* Do default initialization of any bases and members. */
14439 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14440 finish_mem_initializers (NULL_TREE
);
14444 /* Consume the `:' token. */
14445 cp_lexer_consume_token (parser
->lexer
);
14446 /* And the mem-initializer-list. */
14447 cp_parser_mem_initializer_list (parser
);
14450 /* Parse a mem-initializer-list.
14452 mem-initializer-list:
14453 mem-initializer ... [opt]
14454 mem-initializer ... [opt] , mem-initializer-list */
14457 cp_parser_mem_initializer_list (cp_parser
* parser
)
14459 tree mem_initializer_list
= NULL_TREE
;
14460 tree target_ctor
= error_mark_node
;
14461 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14463 /* Let the semantic analysis code know that we are starting the
14464 mem-initializer-list. */
14465 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14466 error_at (token
->location
,
14467 "only constructors take member initializers");
14469 /* Loop through the list. */
14472 tree mem_initializer
;
14474 token
= cp_lexer_peek_token (parser
->lexer
);
14475 /* Parse the mem-initializer. */
14476 mem_initializer
= cp_parser_mem_initializer (parser
);
14477 /* If the next token is a `...', we're expanding member initializers. */
14478 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14480 || (mem_initializer
!= error_mark_node
14481 && check_for_bare_parameter_packs (TREE_PURPOSE
14482 (mem_initializer
))))
14484 /* Consume the `...'. */
14486 cp_lexer_consume_token (parser
->lexer
);
14488 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14489 can be expanded but members cannot. */
14490 if (mem_initializer
!= error_mark_node
14491 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14493 error_at (token
->location
,
14494 "cannot expand initializer for member %qD",
14495 TREE_PURPOSE (mem_initializer
));
14496 mem_initializer
= error_mark_node
;
14499 /* Construct the pack expansion type. */
14500 if (mem_initializer
!= error_mark_node
)
14501 mem_initializer
= make_pack_expansion (mem_initializer
);
14503 if (target_ctor
!= error_mark_node
14504 && mem_initializer
!= error_mark_node
)
14506 error ("mem-initializer for %qD follows constructor delegation",
14507 TREE_PURPOSE (mem_initializer
));
14508 mem_initializer
= error_mark_node
;
14510 /* Look for a target constructor. */
14511 if (mem_initializer
!= error_mark_node
14512 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14513 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14515 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14516 if (mem_initializer_list
)
14518 error ("constructor delegation follows mem-initializer for %qD",
14519 TREE_PURPOSE (mem_initializer_list
));
14520 mem_initializer
= error_mark_node
;
14522 target_ctor
= mem_initializer
;
14524 /* Add it to the list, unless it was erroneous. */
14525 if (mem_initializer
!= error_mark_node
)
14527 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14528 mem_initializer_list
= mem_initializer
;
14530 /* If the next token is not a `,', we're done. */
14531 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14533 /* Consume the `,' token. */
14534 cp_lexer_consume_token (parser
->lexer
);
14537 /* Perform semantic analysis. */
14538 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14539 finish_mem_initializers (mem_initializer_list
);
14542 /* Parse a mem-initializer.
14545 mem-initializer-id ( expression-list [opt] )
14546 mem-initializer-id braced-init-list
14551 ( expression-list [opt] )
14553 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14554 class) or FIELD_DECL (for a non-static data member) to initialize;
14555 the TREE_VALUE is the expression-list. An empty initialization
14556 list is represented by void_list_node. */
14559 cp_parser_mem_initializer (cp_parser
* parser
)
14561 tree mem_initializer_id
;
14562 tree expression_list
;
14564 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14566 /* Find out what is being initialized. */
14567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14569 permerror (token
->location
,
14570 "anachronistic old-style base class initializer");
14571 mem_initializer_id
= NULL_TREE
;
14575 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14576 if (mem_initializer_id
== error_mark_node
)
14577 return mem_initializer_id
;
14579 member
= expand_member_init (mem_initializer_id
);
14580 if (member
&& !DECL_P (member
))
14581 in_base_initializer
= 1;
14583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14585 bool expr_non_constant_p
;
14586 cp_lexer_set_source_position (parser
->lexer
);
14587 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14588 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14589 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14590 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14594 vec
<tree
, va_gc
> *vec
;
14595 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14597 /*allow_expansion_p=*/true,
14598 /*non_constant_p=*/NULL
);
14600 return error_mark_node
;
14601 expression_list
= build_tree_list_vec (vec
);
14602 release_tree_vector (vec
);
14605 if (expression_list
== error_mark_node
)
14606 return error_mark_node
;
14607 if (!expression_list
)
14608 expression_list
= void_type_node
;
14610 in_base_initializer
= 0;
14612 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14615 /* Parse a mem-initializer-id.
14617 mem-initializer-id:
14618 :: [opt] nested-name-specifier [opt] class-name
14619 decltype-specifier (C++11)
14622 Returns a TYPE indicating the class to be initialized for the first
14623 production (and the second in C++11). Returns an IDENTIFIER_NODE
14624 indicating the data member to be initialized for the last production. */
14627 cp_parser_mem_initializer_id (cp_parser
* parser
)
14629 bool global_scope_p
;
14630 bool nested_name_specifier_p
;
14631 bool template_p
= false;
14634 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14636 /* `typename' is not allowed in this context ([temp.res]). */
14637 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14639 error_at (token
->location
,
14640 "keyword %<typename%> not allowed in this context (a qualified "
14641 "member initializer is implicitly a type)");
14642 cp_lexer_consume_token (parser
->lexer
);
14644 /* Look for the optional `::' operator. */
14646 = (cp_parser_global_scope_opt (parser
,
14647 /*current_scope_valid_p=*/false)
14649 /* Look for the optional nested-name-specifier. The simplest way to
14654 The keyword `typename' is not permitted in a base-specifier or
14655 mem-initializer; in these contexts a qualified name that
14656 depends on a template-parameter is implicitly assumed to be a
14659 is to assume that we have seen the `typename' keyword at this
14661 nested_name_specifier_p
14662 = (cp_parser_nested_name_specifier_opt (parser
,
14663 /*typename_keyword_p=*/true,
14664 /*check_dependency_p=*/true,
14666 /*is_declaration=*/true)
14668 if (nested_name_specifier_p
)
14669 template_p
= cp_parser_optional_template_keyword (parser
);
14670 /* If there is a `::' operator or a nested-name-specifier, then we
14671 are definitely looking for a class-name. */
14672 if (global_scope_p
|| nested_name_specifier_p
)
14673 return cp_parser_class_name (parser
,
14674 /*typename_keyword_p=*/true,
14675 /*template_keyword_p=*/template_p
,
14677 /*check_dependency_p=*/true,
14678 /*class_head_p=*/false,
14679 /*is_declaration=*/true);
14680 /* Otherwise, we could also be looking for an ordinary identifier. */
14681 cp_parser_parse_tentatively (parser
);
14682 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14683 /* Try a decltype-specifier. */
14684 id
= cp_parser_decltype (parser
);
14686 /* Otherwise, try a class-name. */
14687 id
= cp_parser_class_name (parser
,
14688 /*typename_keyword_p=*/true,
14689 /*template_keyword_p=*/false,
14691 /*check_dependency_p=*/true,
14692 /*class_head_p=*/false,
14693 /*is_declaration=*/true);
14694 /* If we found one, we're done. */
14695 if (cp_parser_parse_definitely (parser
))
14697 /* Otherwise, look for an ordinary identifier. */
14698 return cp_parser_identifier (parser
);
14701 /* Overloading [gram.over] */
14703 /* Parse an operator-function-id.
14705 operator-function-id:
14708 Returns an IDENTIFIER_NODE for the operator which is a
14709 human-readable spelling of the identifier, e.g., `operator +'. */
14712 cp_parser_operator_function_id (cp_parser
* parser
)
14714 /* Look for the `operator' keyword. */
14715 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14716 return error_mark_node
;
14717 /* And then the name of the operator itself. */
14718 return cp_parser_operator (parser
);
14721 /* Return an identifier node for a user-defined literal operator.
14722 The suffix identifier is chained to the operator name identifier. */
14725 cp_literal_operator_id (const char* name
)
14728 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14729 + strlen (name
) + 10);
14730 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14731 identifier
= get_identifier (buffer
);
14736 /* Parse an operator.
14739 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14740 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14741 || ++ -- , ->* -> () []
14748 Returns an IDENTIFIER_NODE for the operator which is a
14749 human-readable spelling of the identifier, e.g., `operator +'. */
14752 cp_parser_operator (cp_parser
* parser
)
14754 tree id
= NULL_TREE
;
14758 /* Peek at the next token. */
14759 token
= cp_lexer_peek_token (parser
->lexer
);
14761 location_t start_loc
= token
->location
;
14763 /* Figure out which operator we have. */
14764 enum tree_code op
= ERROR_MARK
;
14765 bool assop
= false;
14766 bool consumed
= false;
14767 switch (token
->type
)
14771 /* The keyword should be either `new' or `delete'. */
14772 if (token
->keyword
== RID_NEW
)
14774 else if (token
->keyword
== RID_DELETE
)
14779 /* Consume the `new' or `delete' token. */
14780 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14782 /* Peek at the next token. */
14783 token
= cp_lexer_peek_token (parser
->lexer
);
14784 /* If it's a `[' token then this is the array variant of the
14786 if (token
->type
== CPP_OPEN_SQUARE
)
14788 /* Consume the `[' token. */
14789 cp_lexer_consume_token (parser
->lexer
);
14790 /* Look for the `]' token. */
14791 if (cp_token
*close_token
14792 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14793 end_loc
= close_token
->location
;
14794 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14796 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14814 op
= TRUNC_DIV_EXPR
;
14818 op
= TRUNC_MOD_EXPR
;
14838 op
= TRUTH_NOT_EXPR
;
14871 op
= TRUNC_DIV_EXPR
;
14876 op
= TRUNC_MOD_EXPR
;
14902 case CPP_LSHIFT_EQ
:
14907 case CPP_RSHIFT_EQ
:
14924 case CPP_GREATER_EQ
:
14929 op
= TRUTH_ANDIF_EXPR
;
14933 op
= TRUTH_ORIF_EXPR
;
14936 case CPP_PLUS_PLUS
:
14937 op
= POSTINCREMENT_EXPR
;
14940 case CPP_MINUS_MINUS
:
14941 op
= PREDECREMENT_EXPR
;
14945 op
= COMPOUND_EXPR
;
14948 case CPP_DEREF_STAR
:
14953 op
= COMPONENT_REF
;
14956 case CPP_OPEN_PAREN
:
14958 /* Consume the `('. */
14959 matching_parens parens
;
14960 parens
.consume_open (parser
);
14961 /* Look for the matching `)'. */
14962 parens
.require_close (parser
);
14968 case CPP_OPEN_SQUARE
:
14969 /* Consume the `['. */
14970 cp_lexer_consume_token (parser
->lexer
);
14971 /* Look for the matching `]'. */
14972 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14977 case CPP_UTF8STRING
:
14978 case CPP_UTF8STRING_USERDEF
:
14985 case CPP_STRING_USERDEF
:
14986 case CPP_WSTRING_USERDEF
:
14987 case CPP_STRING16_USERDEF
:
14988 case CPP_STRING32_USERDEF
:
14990 tree str
, string_tree
;
14993 if (cxx_dialect
== cxx98
)
14994 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14996 /* Consume the string. */
14997 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14998 /*wide_ok=*/true, /*lookup_udlit=*/false);
14999 if (str
== error_mark_node
)
15000 return error_mark_node
;
15001 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15003 string_tree
= USERDEF_LITERAL_VALUE (str
);
15004 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15009 /* Look for the suffix identifier. */
15010 token
= cp_lexer_peek_token (parser
->lexer
);
15011 if (token
->type
== CPP_NAME
)
15012 id
= cp_parser_identifier (parser
);
15013 else if (token
->type
== CPP_KEYWORD
)
15015 error ("unexpected keyword;"
15016 " remove space between quotes and suffix identifier");
15017 return error_mark_node
;
15021 error ("expected suffix identifier");
15022 return error_mark_node
;
15025 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15026 (TREE_TYPE (TREE_TYPE (string_tree
))));
15027 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15030 error ("expected empty string after %<operator%> keyword");
15031 return error_mark_node
;
15033 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15036 error ("invalid encoding prefix in literal operator");
15037 return error_mark_node
;
15039 if (id
!= error_mark_node
)
15041 const char *name
= IDENTIFIER_POINTER (id
);
15042 id
= cp_literal_operator_id (name
);
15048 /* Anything else is an error. */
15052 /* If we have selected an identifier, we need to consume the
15054 if (op
!= ERROR_MARK
)
15056 id
= ovl_op_identifier (assop
, op
);
15058 cp_lexer_consume_token (parser
->lexer
);
15060 /* Otherwise, no valid operator name was present. */
15063 cp_parser_error (parser
, "expected operator");
15064 id
= error_mark_node
;
15067 return cp_expr (id
, start_loc
);
15070 /* Parse a template-declaration.
15072 template-declaration:
15073 export [opt] template < template-parameter-list > declaration
15075 If MEMBER_P is TRUE, this template-declaration occurs within a
15078 The grammar rule given by the standard isn't correct. What
15079 is really meant is:
15081 template-declaration:
15082 export [opt] template-parameter-list-seq
15083 decl-specifier-seq [opt] init-declarator [opt] ;
15084 export [opt] template-parameter-list-seq
15085 function-definition
15087 template-parameter-list-seq:
15088 template-parameter-list-seq [opt]
15089 template < template-parameter-list >
15091 Concept Extensions:
15093 template-parameter-list-seq:
15094 template < template-parameter-list > requires-clause [opt]
15097 requires logical-or-expression */
15100 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15102 /* Check for `export'. */
15103 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15105 /* Consume the `export' token. */
15106 cp_lexer_consume_token (parser
->lexer
);
15107 /* Warn that we do not support `export'. */
15108 warning (0, "keyword %<export%> not implemented, and will be ignored");
15111 cp_parser_template_declaration_after_export (parser
, member_p
);
15114 /* Parse a template-parameter-list.
15116 template-parameter-list:
15118 template-parameter-list , template-parameter
15120 Returns a TREE_LIST. Each node represents a template parameter.
15121 The nodes are connected via their TREE_CHAINs. */
15124 cp_parser_template_parameter_list (cp_parser
* parser
)
15126 tree parameter_list
= NULL_TREE
;
15128 begin_template_parm_list ();
15130 /* The loop below parses the template parms. We first need to know
15131 the total number of template parms to be able to compute proper
15132 canonical types of each dependent type. So after the loop, when
15133 we know the total number of template parms,
15134 end_template_parm_list computes the proper canonical types and
15135 fixes up the dependent types accordingly. */
15140 bool is_parameter_pack
;
15141 location_t parm_loc
;
15143 /* Parse the template-parameter. */
15144 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15145 parameter
= cp_parser_template_parameter (parser
,
15147 &is_parameter_pack
);
15148 /* Add it to the list. */
15149 if (parameter
!= error_mark_node
)
15150 parameter_list
= process_template_parm (parameter_list
,
15154 is_parameter_pack
);
15157 tree err_parm
= build_tree_list (parameter
, parameter
);
15158 parameter_list
= chainon (parameter_list
, err_parm
);
15161 /* If the next token is not a `,', we're done. */
15162 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15164 /* Otherwise, consume the `,' token. */
15165 cp_lexer_consume_token (parser
->lexer
);
15168 return end_template_parm_list (parameter_list
);
15171 /* Parse a introduction-list.
15174 introduced-parameter
15175 introduction-list , introduced-parameter
15177 introduced-parameter:
15178 ...[opt] identifier
15180 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15181 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15182 WILDCARD_DECL will also have DECL_NAME set and token location in
15183 DECL_SOURCE_LOCATION. */
15186 cp_parser_introduction_list (cp_parser
*parser
)
15188 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15192 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15194 cp_lexer_consume_token (parser
->lexer
);
15196 /* Build placeholder. */
15197 tree parm
= build_nt (WILDCARD_DECL
);
15198 DECL_SOURCE_LOCATION (parm
)
15199 = cp_lexer_peek_token (parser
->lexer
)->location
;
15200 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15201 WILDCARD_PACK_P (parm
) = is_pack
;
15202 vec_safe_push (introduction_vec
, parm
);
15204 /* If the next token is not a `,', we're done. */
15205 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15207 /* Otherwise, consume the `,' token. */
15208 cp_lexer_consume_token (parser
->lexer
);
15211 /* Convert the vec into a TREE_VEC. */
15212 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15215 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15216 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15218 release_tree_vector (introduction_vec
);
15219 return introduction_list
;
15222 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15223 is an abstract declarator. */
15225 static inline cp_declarator
*
15226 get_id_declarator (cp_declarator
*declarator
)
15228 cp_declarator
*d
= declarator
;
15229 while (d
&& d
->kind
!= cdk_id
)
15234 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15235 is an abstract declarator. */
15238 get_unqualified_id (cp_declarator
*declarator
)
15240 declarator
= get_id_declarator (declarator
);
15242 return declarator
->u
.id
.unqualified_name
;
15247 /* Returns true if DECL represents a constrained-parameter. */
15250 is_constrained_parameter (tree decl
)
15253 && TREE_CODE (decl
) == TYPE_DECL
15254 && CONSTRAINED_PARM_CONCEPT (decl
)
15255 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15258 /* Returns true if PARM declares a constrained-parameter. */
15261 is_constrained_parameter (cp_parameter_declarator
*parm
)
15263 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15266 /* Check that the type parameter is only a declarator-id, and that its
15267 type is not cv-qualified. */
15270 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15271 cp_parameter_declarator
*parm
)
15273 if (!parm
->declarator
)
15276 if (parm
->declarator
->kind
!= cdk_id
)
15278 cp_parser_error (parser
, "invalid constrained type parameter");
15282 /* Don't allow cv-qualified type parameters. */
15283 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15284 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15286 cp_parser_error (parser
, "cv-qualified type parameter");
15293 /* Finish parsing/processing a template type parameter and checking
15294 various restrictions. */
15297 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15299 cp_parameter_declarator
* parmdecl
)
15301 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15302 return finish_template_type_parm (class_type_node
, id
);
15304 return error_mark_node
;
15308 finish_constrained_template_template_parm (tree proto
, tree id
)
15310 /* FIXME: This should probably be copied, and we may need to adjust
15311 the template parameter depths. */
15312 tree saved_parms
= current_template_parms
;
15313 begin_template_parm_list ();
15314 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15315 end_template_parm_list ();
15317 tree parm
= finish_template_template_parm (class_type_node
, id
);
15318 current_template_parms
= saved_parms
;
15323 /* Finish parsing/processing a template template parameter by borrowing
15324 the template parameter list from the prototype parameter. */
15327 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15330 cp_parameter_declarator
*parmdecl
)
15332 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15333 return error_mark_node
;
15334 return finish_constrained_template_template_parm (proto
, id
);
15337 /* Create a new non-type template parameter from the given PARM
15341 constrained_non_type_template_parm (bool *is_non_type
,
15342 cp_parameter_declarator
*parm
)
15344 *is_non_type
= true;
15345 cp_declarator
*decl
= parm
->declarator
;
15346 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15347 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15348 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15351 /* Build a constrained template parameter based on the PARMDECL
15352 declarator. The type of PARMDECL is the constrained type, which
15353 refers to the prototype template parameter that ultimately
15354 specifies the type of the declared parameter. */
15357 finish_constrained_parameter (cp_parser
*parser
,
15358 cp_parameter_declarator
*parmdecl
,
15360 bool *is_parameter_pack
)
15362 tree decl
= parmdecl
->decl_specifiers
.type
;
15363 tree id
= get_unqualified_id (parmdecl
->declarator
);
15364 tree def
= parmdecl
->default_argument
;
15365 tree proto
= DECL_INITIAL (decl
);
15367 /* A template parameter constrained by a variadic concept shall also
15368 be declared as a template parameter pack. */
15369 bool is_variadic
= template_parameter_pack_p (proto
);
15370 if (is_variadic
&& !*is_parameter_pack
)
15371 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15373 /* Build the parameter. Return an error if the declarator was invalid. */
15375 if (TREE_CODE (proto
) == TYPE_DECL
)
15376 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15377 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15378 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15381 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15382 if (parm
== error_mark_node
)
15383 return error_mark_node
;
15385 /* Finish the parameter decl and create a node attaching the
15386 default argument and constraint. */
15387 parm
= build_tree_list (def
, parm
);
15388 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15393 /* Returns true if the parsed type actually represents the declaration
15394 of a type template-parameter. */
15397 declares_constrained_type_template_parameter (tree type
)
15399 return (is_constrained_parameter (type
)
15400 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15404 /* Returns true if the parsed type actually represents the declaration of
15405 a template template-parameter. */
15408 declares_constrained_template_template_parameter (tree type
)
15410 return (is_constrained_parameter (type
)
15411 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15414 /* Parse a default argument for a type template-parameter.
15415 Note that diagnostics are handled in cp_parser_template_parameter. */
15418 cp_parser_default_type_template_argument (cp_parser
*parser
)
15420 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15422 /* Consume the `=' token. */
15423 cp_lexer_consume_token (parser
->lexer
);
15425 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15427 /* Parse the default-argument. */
15428 push_deferring_access_checks (dk_no_deferred
);
15429 tree default_argument
= cp_parser_type_id (parser
);
15430 pop_deferring_access_checks ();
15432 if (flag_concepts
&& type_uses_auto (default_argument
))
15434 error_at (token
->location
,
15435 "invalid use of %<auto%> in default template argument");
15436 return error_mark_node
;
15439 return default_argument
;
15442 /* Parse a default argument for a template template-parameter. */
15445 cp_parser_default_template_template_argument (cp_parser
*parser
)
15447 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15451 /* Consume the `='. */
15452 cp_lexer_consume_token (parser
->lexer
);
15453 /* Parse the id-expression. */
15454 push_deferring_access_checks (dk_no_deferred
);
15455 /* save token before parsing the id-expression, for error
15457 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15458 tree default_argument
15459 = cp_parser_id_expression (parser
,
15460 /*template_keyword_p=*/false,
15461 /*check_dependency_p=*/true,
15462 /*template_p=*/&is_template
,
15463 /*declarator_p=*/false,
15464 /*optional_p=*/false);
15465 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15466 /* If the id-expression was a template-id that refers to
15467 a template-class, we already have the declaration here,
15468 so no further lookup is needed. */
15471 /* Look up the name. */
15473 = cp_parser_lookup_name (parser
, default_argument
,
15475 /*is_template=*/is_template
,
15476 /*is_namespace=*/false,
15477 /*check_dependency=*/true,
15478 /*ambiguous_decls=*/NULL
,
15480 /* See if the default argument is valid. */
15481 default_argument
= check_template_template_default_arg (default_argument
);
15482 pop_deferring_access_checks ();
15483 return default_argument
;
15486 /* Parse a template-parameter.
15488 template-parameter:
15490 parameter-declaration
15492 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15493 the parameter. The TREE_PURPOSE is the default value, if any.
15494 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15495 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15496 set to true iff this parameter is a parameter pack. */
15499 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15500 bool *is_parameter_pack
)
15503 cp_parameter_declarator
*parameter_declarator
;
15506 /* Assume it is a type parameter or a template parameter. */
15507 *is_non_type
= false;
15508 /* Assume it not a parameter pack. */
15509 *is_parameter_pack
= false;
15510 /* Peek at the next token. */
15511 token
= cp_lexer_peek_token (parser
->lexer
);
15512 /* If it is `template', we have a type-parameter. */
15513 if (token
->keyword
== RID_TEMPLATE
)
15514 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15515 /* If it is `class' or `typename' we do not know yet whether it is a
15516 type parameter or a non-type parameter. Consider:
15518 template <typename T, typename T::X X> ...
15522 template <class C, class D*> ...
15524 Here, the first parameter is a type parameter, and the second is
15525 a non-type parameter. We can tell by looking at the token after
15526 the identifier -- if it is a `,', `=', or `>' then we have a type
15528 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15530 /* Peek at the token after `class' or `typename'. */
15531 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15532 /* If it's an ellipsis, we have a template type parameter
15534 if (token
->type
== CPP_ELLIPSIS
)
15535 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15536 /* If it's an identifier, skip it. */
15537 if (token
->type
== CPP_NAME
)
15538 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15539 /* Now, see if the token looks like the end of a template
15541 if (token
->type
== CPP_COMMA
15542 || token
->type
== CPP_EQ
15543 || token
->type
== CPP_GREATER
)
15544 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15547 /* Otherwise, it is a non-type parameter or a constrained parameter.
15551 When parsing a default template-argument for a non-type
15552 template-parameter, the first non-nested `>' is taken as the end
15553 of the template parameter-list rather than a greater-than
15555 parameter_declarator
15556 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15557 /*parenthesized_p=*/NULL
);
15559 if (!parameter_declarator
)
15560 return error_mark_node
;
15562 /* If the parameter declaration is marked as a parameter pack, set
15563 *IS_PARAMETER_PACK to notify the caller. */
15564 if (parameter_declarator
->template_parameter_pack_p
)
15565 *is_parameter_pack
= true;
15567 if (parameter_declarator
->default_argument
)
15569 /* Can happen in some cases of erroneous input (c++/34892). */
15570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15571 /* Consume the `...' for better error recovery. */
15572 cp_lexer_consume_token (parser
->lexer
);
15575 // The parameter may have been constrained.
15576 if (is_constrained_parameter (parameter_declarator
))
15577 return finish_constrained_parameter (parser
,
15578 parameter_declarator
,
15580 is_parameter_pack
);
15582 // Now we're sure that the parameter is a non-type parameter.
15583 *is_non_type
= true;
15585 parm
= grokdeclarator (parameter_declarator
->declarator
,
15586 ¶meter_declarator
->decl_specifiers
,
15587 TPARM
, /*initialized=*/0,
15588 /*attrlist=*/NULL
);
15589 if (parm
== error_mark_node
)
15590 return error_mark_node
;
15592 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15595 /* Parse a type-parameter.
15598 class identifier [opt]
15599 class identifier [opt] = type-id
15600 typename identifier [opt]
15601 typename identifier [opt] = type-id
15602 template < template-parameter-list > class identifier [opt]
15603 template < template-parameter-list > class identifier [opt]
15606 GNU Extension (variadic templates):
15609 class ... identifier [opt]
15610 typename ... identifier [opt]
15612 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15613 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15614 the declaration of the parameter.
15616 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15619 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15624 /* Look for a keyword to tell us what kind of parameter this is. */
15625 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15627 return error_mark_node
;
15629 switch (token
->keyword
)
15635 tree default_argument
;
15637 /* If the next token is an ellipsis, we have a template
15639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15641 /* Consume the `...' token. */
15642 cp_lexer_consume_token (parser
->lexer
);
15643 maybe_warn_variadic_templates ();
15645 *is_parameter_pack
= true;
15648 /* If the next token is an identifier, then it names the
15650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15651 identifier
= cp_parser_identifier (parser
);
15653 identifier
= NULL_TREE
;
15655 /* Create the parameter. */
15656 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15658 /* If the next token is an `=', we have a default argument. */
15659 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15662 = cp_parser_default_type_template_argument (parser
);
15664 /* Template parameter packs cannot have default
15666 if (*is_parameter_pack
)
15669 error_at (token
->location
,
15670 "template parameter pack %qD cannot have a "
15671 "default argument", identifier
);
15673 error_at (token
->location
,
15674 "template parameter packs cannot have "
15675 "default arguments");
15676 default_argument
= NULL_TREE
;
15678 else if (check_for_bare_parameter_packs (default_argument
))
15679 default_argument
= error_mark_node
;
15682 default_argument
= NULL_TREE
;
15684 /* Create the combined representation of the parameter and the
15685 default argument. */
15686 parameter
= build_tree_list (default_argument
, parameter
);
15693 tree default_argument
;
15695 /* Look for the `<'. */
15696 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15697 /* Parse the template-parameter-list. */
15698 cp_parser_template_parameter_list (parser
);
15699 /* Look for the `>'. */
15700 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15702 // If template requirements are present, parse them.
15705 tree reqs
= get_shorthand_constraints (current_template_parms
);
15706 if (tree r
= cp_parser_requires_clause_opt (parser
))
15707 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15708 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15711 /* Look for the `class' or 'typename' keywords. */
15712 cp_parser_type_parameter_key (parser
);
15713 /* If the next token is an ellipsis, we have a template
15715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15717 /* Consume the `...' token. */
15718 cp_lexer_consume_token (parser
->lexer
);
15719 maybe_warn_variadic_templates ();
15721 *is_parameter_pack
= true;
15723 /* If the next token is an `=', then there is a
15724 default-argument. If the next token is a `>', we are at
15725 the end of the parameter-list. If the next token is a `,',
15726 then we are at the end of this parameter. */
15727 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15728 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15729 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15731 identifier
= cp_parser_identifier (parser
);
15732 /* Treat invalid names as if the parameter were nameless. */
15733 if (identifier
== error_mark_node
)
15734 identifier
= NULL_TREE
;
15737 identifier
= NULL_TREE
;
15739 /* Create the template parameter. */
15740 parameter
= finish_template_template_parm (class_type_node
,
15743 /* If the next token is an `=', then there is a
15744 default-argument. */
15745 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15748 = cp_parser_default_template_template_argument (parser
);
15750 /* Template parameter packs cannot have default
15752 if (*is_parameter_pack
)
15755 error_at (token
->location
,
15756 "template parameter pack %qD cannot "
15757 "have a default argument",
15760 error_at (token
->location
, "template parameter packs cannot "
15761 "have default arguments");
15762 default_argument
= NULL_TREE
;
15766 default_argument
= NULL_TREE
;
15768 /* Create the combined representation of the parameter and the
15769 default argument. */
15770 parameter
= build_tree_list (default_argument
, parameter
);
15775 gcc_unreachable ();
15782 /* Parse a template-id.
15785 template-name < template-argument-list [opt] >
15787 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15788 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15789 returned. Otherwise, if the template-name names a function, or set
15790 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15791 names a class, returns a TYPE_DECL for the specialization.
15793 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15794 uninstantiated templates. */
15797 cp_parser_template_id (cp_parser
*parser
,
15798 bool template_keyword_p
,
15799 bool check_dependency_p
,
15800 enum tag_types tag_type
,
15801 bool is_declaration
)
15806 cp_token_position start_of_id
= 0;
15807 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15808 bool is_identifier
;
15810 /* If the next token corresponds to a template-id, there is no need
15812 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15813 if (token
->type
== CPP_TEMPLATE_ID
)
15815 cp_lexer_consume_token (parser
->lexer
);
15816 return saved_checks_value (token
->u
.tree_check_value
);
15819 /* Avoid performing name lookup if there is no possibility of
15820 finding a template-id. */
15821 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15822 || (token
->type
== CPP_NAME
15823 && !cp_parser_nth_token_starts_template_argument_list_p
15826 cp_parser_error (parser
, "expected template-id");
15827 return error_mark_node
;
15830 /* Remember where the template-id starts. */
15831 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15832 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15834 push_deferring_access_checks (dk_deferred
);
15836 /* Parse the template-name. */
15837 is_identifier
= false;
15838 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15839 check_dependency_p
,
15843 if (templ
== error_mark_node
|| is_identifier
)
15845 pop_deferring_access_checks ();
15849 /* Since we're going to preserve any side-effects from this parse, set up a
15850 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15851 in the template arguments. */
15852 tentative_firewall
firewall (parser
);
15854 /* If we find the sequence `[:' after a template-name, it's probably
15855 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15856 parse correctly the argument list. */
15857 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15858 == CPP_OPEN_SQUARE
)
15859 && next_token
->flags
& DIGRAPH
15860 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15862 && !(next_token_2
->flags
& PREV_WHITE
))
15864 cp_parser_parse_tentatively (parser
);
15865 /* Change `:' into `::'. */
15866 next_token_2
->type
= CPP_SCOPE
;
15867 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15869 cp_lexer_consume_token (parser
->lexer
);
15871 /* Parse the arguments. */
15872 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15873 if (!cp_parser_parse_definitely (parser
))
15875 /* If we couldn't parse an argument list, then we revert our changes
15876 and return simply an error. Maybe this is not a template-id
15878 next_token_2
->type
= CPP_COLON
;
15879 cp_parser_error (parser
, "expected %<<%>");
15880 pop_deferring_access_checks ();
15881 return error_mark_node
;
15883 /* Otherwise, emit an error about the invalid digraph, but continue
15884 parsing because we got our argument list. */
15885 if (permerror (next_token
->location
,
15886 "%<<::%> cannot begin a template-argument list"))
15888 static bool hint
= false;
15889 inform (next_token
->location
,
15890 "%<<:%> is an alternate spelling for %<[%>."
15891 " Insert whitespace between %<<%> and %<::%>");
15892 if (!hint
&& !flag_permissive
)
15894 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15895 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15896 "accept your code)");
15903 /* Look for the `<' that starts the template-argument-list. */
15904 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15906 pop_deferring_access_checks ();
15907 return error_mark_node
;
15909 /* Parse the arguments. */
15910 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15913 /* Set the location to be of the form:
15914 template-name < template-argument-list [opt] >
15915 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15916 with caret == start at the start of the template-name,
15917 ranging until the closing '>'. */
15918 location_t finish_loc
15919 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15920 location_t combined_loc
15921 = make_location (token
->location
, token
->location
, finish_loc
);
15923 /* Check for concepts autos where they don't belong. We could
15924 identify types in some cases of idnetifier TEMPL, looking ahead
15925 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15926 types. We reject them in functions, but if what we have is an
15927 identifier, even with none_type we can't conclude it's NOT a
15928 type, we have to wait for template substitution. */
15929 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15930 template_id
= error_mark_node
;
15931 /* Build a representation of the specialization. */
15932 else if (identifier_p (templ
))
15933 template_id
= build_min_nt_loc (combined_loc
,
15936 else if (DECL_TYPE_TEMPLATE_P (templ
)
15937 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15939 bool entering_scope
;
15940 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15941 template (rather than some instantiation thereof) only if
15942 is not nested within some other construct. For example, in
15943 "template <typename T> void f(T) { A<T>::", A<T> is just an
15944 instantiation of A. */
15945 entering_scope
= (template_parm_scope_p ()
15946 && cp_lexer_next_token_is (parser
->lexer
,
15949 = finish_template_type (templ
, arguments
, entering_scope
);
15951 /* A template-like identifier may be a partial concept id. */
15952 else if (flag_concepts
15953 && (template_id
= (cp_parser_maybe_partial_concept_id
15954 (parser
, templ
, arguments
))))
15955 return template_id
;
15956 else if (variable_template_p (templ
))
15958 template_id
= lookup_template_variable (templ
, arguments
);
15959 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15960 SET_EXPR_LOCATION (template_id
, combined_loc
);
15964 /* If it's not a class-template or a template-template, it should be
15965 a function-template. */
15966 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15967 || TREE_CODE (templ
) == OVERLOAD
15968 || BASELINK_P (templ
)));
15970 template_id
= lookup_template_function (templ
, arguments
);
15971 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15972 SET_EXPR_LOCATION (template_id
, combined_loc
);
15975 /* If parsing tentatively, replace the sequence of tokens that makes
15976 up the template-id with a CPP_TEMPLATE_ID token. That way,
15977 should we re-parse the token stream, we will not have to repeat
15978 the effort required to do the parse, nor will we issue duplicate
15979 error messages about problems during instantiation of the
15982 /* Don't do this if we had a parse error in a declarator; re-parsing
15983 might succeed if a name changes meaning (60361). */
15984 && !(cp_parser_error_occurred (parser
)
15985 && cp_parser_parsing_tentatively (parser
)
15986 && parser
->in_declarator_p
))
15988 /* Reset the contents of the START_OF_ID token. */
15989 token
->type
= CPP_TEMPLATE_ID
;
15990 token
->location
= combined_loc
;
15992 /* We must mark the lookup as kept, so we don't throw it away on
15993 the first parse. */
15994 if (is_overloaded_fn (template_id
))
15995 lookup_keep (get_fns (template_id
), true);
15997 /* Retrieve any deferred checks. Do not pop this access checks yet
15998 so the memory will not be reclaimed during token replacing below. */
15999 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16000 token
->u
.tree_check_value
->value
= template_id
;
16001 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16002 token
->keyword
= RID_MAX
;
16004 /* Purge all subsequent tokens. */
16005 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16007 /* ??? Can we actually assume that, if template_id ==
16008 error_mark_node, we will have issued a diagnostic to the
16009 user, as opposed to simply marking the tentative parse as
16011 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16012 error_at (token
->location
, "parse error in template argument list");
16015 pop_to_parent_deferring_access_checks ();
16016 return template_id
;
16019 /* Parse a template-name.
16024 The standard should actually say:
16028 operator-function-id
16030 A defect report has been filed about this issue.
16032 A conversion-function-id cannot be a template name because they cannot
16033 be part of a template-id. In fact, looking at this code:
16035 a.operator K<int>()
16037 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16038 It is impossible to call a templated conversion-function-id with an
16039 explicit argument list, since the only allowed template parameter is
16040 the type to which it is converting.
16042 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16043 `template' keyword, in a construction like:
16047 In that case `f' is taken to be a template-name, even though there
16048 is no way of knowing for sure.
16050 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16051 name refers to a set of overloaded functions, at least one of which
16052 is a template, or an IDENTIFIER_NODE with the name of the template,
16053 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16054 names are looked up inside uninstantiated templates. */
16057 cp_parser_template_name (cp_parser
* parser
,
16058 bool template_keyword_p
,
16059 bool check_dependency_p
,
16060 bool is_declaration
,
16061 enum tag_types tag_type
,
16062 bool *is_identifier
)
16066 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16068 /* If the next token is `operator', then we have either an
16069 operator-function-id or a conversion-function-id. */
16070 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16072 /* We don't know whether we're looking at an
16073 operator-function-id or a conversion-function-id. */
16074 cp_parser_parse_tentatively (parser
);
16075 /* Try an operator-function-id. */
16076 identifier
= cp_parser_operator_function_id (parser
);
16077 /* If that didn't work, try a conversion-function-id. */
16078 if (!cp_parser_parse_definitely (parser
))
16080 cp_parser_error (parser
, "expected template-name");
16081 return error_mark_node
;
16084 /* Look for the identifier. */
16086 identifier
= cp_parser_identifier (parser
);
16088 /* If we didn't find an identifier, we don't have a template-id. */
16089 if (identifier
== error_mark_node
)
16090 return error_mark_node
;
16092 /* If the name immediately followed the `template' keyword, then it
16093 is a template-name. However, if the next token is not `<', then
16094 we do not treat it as a template-name, since it is not being used
16095 as part of a template-id. This enables us to handle constructs
16098 template <typename T> struct S { S(); };
16099 template <typename T> S<T>::S();
16101 correctly. We would treat `S' as a template -- if it were `S<T>'
16102 -- but we do not if there is no `<'. */
16104 if (processing_template_decl
16105 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16107 /* In a declaration, in a dependent context, we pretend that the
16108 "template" keyword was present in order to improve error
16109 recovery. For example, given:
16111 template <typename T> void f(T::X<int>);
16113 we want to treat "X<int>" as a template-id. */
16115 && !template_keyword_p
16116 && parser
->scope
&& TYPE_P (parser
->scope
)
16117 && check_dependency_p
16118 && dependent_scope_p (parser
->scope
)
16119 /* Do not do this for dtors (or ctors), since they never
16120 need the template keyword before their name. */
16121 && !constructor_name_p (identifier
, parser
->scope
))
16123 cp_token_position start
= 0;
16125 /* Explain what went wrong. */
16126 error_at (token
->location
, "non-template %qD used as template",
16128 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16129 parser
->scope
, identifier
);
16130 /* If parsing tentatively, find the location of the "<" token. */
16131 if (cp_parser_simulate_error (parser
))
16132 start
= cp_lexer_token_position (parser
->lexer
, true);
16133 /* Parse the template arguments so that we can issue error
16134 messages about them. */
16135 cp_lexer_consume_token (parser
->lexer
);
16136 cp_parser_enclosed_template_argument_list (parser
);
16137 /* Skip tokens until we find a good place from which to
16138 continue parsing. */
16139 cp_parser_skip_to_closing_parenthesis (parser
,
16140 /*recovering=*/true,
16142 /*consume_paren=*/false);
16143 /* If parsing tentatively, permanently remove the
16144 template argument list. That will prevent duplicate
16145 error messages from being issued about the missing
16146 "template" keyword. */
16148 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16150 *is_identifier
= true;
16151 parser
->context
->object_type
= NULL_TREE
;
16155 /* If the "template" keyword is present, then there is generally
16156 no point in doing name-lookup, so we just return IDENTIFIER.
16157 But, if the qualifying scope is non-dependent then we can
16158 (and must) do name-lookup normally. */
16159 if (template_keyword_p
)
16161 tree scope
= (parser
->scope
? parser
->scope
16162 : parser
->context
->object_type
);
16163 if (scope
&& TYPE_P (scope
)
16164 && (!CLASS_TYPE_P (scope
)
16165 || (check_dependency_p
&& dependent_type_p (scope
))))
16167 /* We're optimizing away the call to cp_parser_lookup_name, but
16168 we still need to do this. */
16169 parser
->context
->object_type
= NULL_TREE
;
16175 /* Look up the name. */
16176 decl
= cp_parser_lookup_name (parser
, identifier
,
16178 /*is_template=*/true,
16179 /*is_namespace=*/false,
16180 check_dependency_p
,
16181 /*ambiguous_decls=*/NULL
,
16184 decl
= strip_using_decl (decl
);
16186 /* If DECL is a template, then the name was a template-name. */
16187 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16189 if (TREE_DEPRECATED (decl
)
16190 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16191 warn_deprecated_use (decl
, NULL_TREE
);
16195 /* The standard does not explicitly indicate whether a name that
16196 names a set of overloaded declarations, some of which are
16197 templates, is a template-name. However, such a name should
16198 be a template-name; otherwise, there is no way to form a
16199 template-id for the overloaded templates. */
16200 bool found
= false;
16202 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16203 !found
&& iter
; ++iter
)
16204 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16209 /* The name does not name a template. */
16210 cp_parser_error (parser
, "expected template-name");
16211 return error_mark_node
;
16215 /* If DECL is dependent, and refers to a function, then just return
16216 its name; we will look it up again during template instantiation. */
16217 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16219 tree scope
= ovl_scope (decl
);
16220 if (TYPE_P (scope
) && dependent_type_p (scope
))
16227 /* Parse a template-argument-list.
16229 template-argument-list:
16230 template-argument ... [opt]
16231 template-argument-list , template-argument ... [opt]
16233 Returns a TREE_VEC containing the arguments. */
16236 cp_parser_template_argument_list (cp_parser
* parser
)
16238 tree fixed_args
[10];
16239 unsigned n_args
= 0;
16240 unsigned alloced
= 10;
16241 tree
*arg_ary
= fixed_args
;
16243 bool saved_in_template_argument_list_p
;
16245 bool saved_non_ice_p
;
16247 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16248 parser
->in_template_argument_list_p
= true;
16249 /* Even if the template-id appears in an integral
16250 constant-expression, the contents of the argument list do
16252 saved_ice_p
= parser
->integral_constant_expression_p
;
16253 parser
->integral_constant_expression_p
= false;
16254 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16255 parser
->non_integral_constant_expression_p
= false;
16257 /* Parse the arguments. */
16263 /* Consume the comma. */
16264 cp_lexer_consume_token (parser
->lexer
);
16266 /* Parse the template-argument. */
16267 argument
= cp_parser_template_argument (parser
);
16269 /* If the next token is an ellipsis, we're expanding a template
16271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16273 if (argument
== error_mark_node
)
16275 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16276 error_at (token
->location
,
16277 "expected parameter pack before %<...%>");
16279 /* Consume the `...' token. */
16280 cp_lexer_consume_token (parser
->lexer
);
16282 /* Make the argument into a TYPE_PACK_EXPANSION or
16283 EXPR_PACK_EXPANSION. */
16284 argument
= make_pack_expansion (argument
);
16287 if (n_args
== alloced
)
16291 if (arg_ary
== fixed_args
)
16293 arg_ary
= XNEWVEC (tree
, alloced
);
16294 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16297 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16299 arg_ary
[n_args
++] = argument
;
16301 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16303 vec
= make_tree_vec (n_args
);
16306 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16308 if (arg_ary
!= fixed_args
)
16310 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16311 parser
->integral_constant_expression_p
= saved_ice_p
;
16312 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16314 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16318 /* Parse a template-argument.
16321 assignment-expression
16325 The representation is that of an assignment-expression, type-id, or
16326 id-expression -- except that the qualified id-expression is
16327 evaluated, so that the value returned is either a DECL or an
16330 Although the standard says "assignment-expression", it forbids
16331 throw-expressions or assignments in the template argument.
16332 Therefore, we use "conditional-expression" instead. */
16335 cp_parser_template_argument (cp_parser
* parser
)
16340 bool maybe_type_id
= false;
16341 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16342 location_t loc
= 0;
16345 /* There's really no way to know what we're looking at, so we just
16346 try each alternative in order.
16350 In a template-argument, an ambiguity between a type-id and an
16351 expression is resolved to a type-id, regardless of the form of
16352 the corresponding template-parameter.
16354 Therefore, we try a type-id first. */
16355 cp_parser_parse_tentatively (parser
);
16356 argument
= cp_parser_template_type_arg (parser
);
16357 /* If there was no error parsing the type-id but the next token is a
16358 '>>', our behavior depends on which dialect of C++ we're
16359 parsing. In C++98, we probably found a typo for '> >'. But there
16360 are type-id which are also valid expressions. For instance:
16362 struct X { int operator >> (int); };
16363 template <int V> struct Foo {};
16366 Here 'X()' is a valid type-id of a function type, but the user just
16367 wanted to write the expression "X() >> 5". Thus, we remember that we
16368 found a valid type-id, but we still try to parse the argument as an
16369 expression to see what happens.
16371 In C++0x, the '>>' will be considered two separate '>'
16373 if (!cp_parser_error_occurred (parser
)
16374 && cxx_dialect
== cxx98
16375 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16377 maybe_type_id
= true;
16378 cp_parser_abort_tentative_parse (parser
);
16382 /* If the next token isn't a `,' or a `>', then this argument wasn't
16383 really finished. This means that the argument is not a valid
16385 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16386 cp_parser_error (parser
, "expected template-argument");
16387 /* If that worked, we're done. */
16388 if (cp_parser_parse_definitely (parser
))
16391 /* We're still not sure what the argument will be. */
16392 cp_parser_parse_tentatively (parser
);
16393 /* Try a template. */
16394 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16395 argument
= cp_parser_id_expression (parser
,
16396 /*template_keyword_p=*/false,
16397 /*check_dependency_p=*/true,
16399 /*declarator_p=*/false,
16400 /*optional_p=*/false);
16401 /* If the next token isn't a `,' or a `>', then this argument wasn't
16402 really finished. */
16403 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16404 cp_parser_error (parser
, "expected template-argument");
16405 if (!cp_parser_error_occurred (parser
))
16407 /* Figure out what is being referred to. If the id-expression
16408 was for a class template specialization, then we will have a
16409 TYPE_DECL at this point. There is no need to do name lookup
16410 at this point in that case. */
16411 if (TREE_CODE (argument
) != TYPE_DECL
)
16412 argument
= cp_parser_lookup_name (parser
, argument
,
16414 /*is_template=*/template_p
,
16415 /*is_namespace=*/false,
16416 /*check_dependency=*/true,
16417 /*ambiguous_decls=*/NULL
,
16418 argument_start_token
->location
);
16419 /* Handle a constrained-type-specifier for a non-type template
16421 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16423 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16424 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16425 cp_parser_error (parser
, "expected template-name");
16427 if (cp_parser_parse_definitely (parser
))
16429 if (TREE_DEPRECATED (argument
))
16430 warn_deprecated_use (argument
, NULL_TREE
);
16433 /* It must be a non-type argument. In C++17 any constant-expression is
16435 if (cxx_dialect
> cxx14
)
16438 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16440 -- an integral constant-expression of integral or enumeration
16443 -- the name of a non-type template-parameter; or
16445 -- the name of an object or function with external linkage...
16447 -- the address of an object or function with external linkage...
16449 -- a pointer to member... */
16450 /* Look for a non-type template parameter. */
16451 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16453 cp_parser_parse_tentatively (parser
);
16454 argument
= cp_parser_primary_expression (parser
,
16455 /*address_p=*/false,
16457 /*template_arg_p=*/true,
16459 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16460 || !cp_parser_next_token_ends_template_argument_p (parser
))
16461 cp_parser_simulate_error (parser
);
16462 if (cp_parser_parse_definitely (parser
))
16466 /* If the next token is "&", the argument must be the address of an
16467 object or function with external linkage. */
16468 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16471 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16472 cp_lexer_consume_token (parser
->lexer
);
16474 /* See if we might have an id-expression. */
16475 token
= cp_lexer_peek_token (parser
->lexer
);
16476 if (token
->type
== CPP_NAME
16477 || token
->keyword
== RID_OPERATOR
16478 || token
->type
== CPP_SCOPE
16479 || token
->type
== CPP_TEMPLATE_ID
16480 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16482 cp_parser_parse_tentatively (parser
);
16483 argument
= cp_parser_primary_expression (parser
,
16486 /*template_arg_p=*/true,
16488 if (cp_parser_error_occurred (parser
)
16489 || !cp_parser_next_token_ends_template_argument_p (parser
))
16490 cp_parser_abort_tentative_parse (parser
);
16495 if (INDIRECT_REF_P (argument
))
16497 /* Strip the dereference temporarily. */
16498 gcc_assert (REFERENCE_REF_P (argument
));
16499 argument
= TREE_OPERAND (argument
, 0);
16502 /* If we're in a template, we represent a qualified-id referring
16503 to a static data member as a SCOPE_REF even if the scope isn't
16504 dependent so that we can check access control later. */
16506 if (TREE_CODE (probe
) == SCOPE_REF
)
16507 probe
= TREE_OPERAND (probe
, 1);
16510 /* A variable without external linkage might still be a
16511 valid constant-expression, so no error is issued here
16512 if the external-linkage check fails. */
16513 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16514 cp_parser_simulate_error (parser
);
16516 else if (is_overloaded_fn (argument
))
16517 /* All overloaded functions are allowed; if the external
16518 linkage test does not pass, an error will be issued
16522 && (TREE_CODE (argument
) == OFFSET_REF
16523 || TREE_CODE (argument
) == SCOPE_REF
))
16524 /* A pointer-to-member. */
16526 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16529 cp_parser_simulate_error (parser
);
16531 if (cp_parser_parse_definitely (parser
))
16534 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16535 tf_warning_or_error
);
16537 argument
= convert_from_reference (argument
);
16542 /* If the argument started with "&", there are no other valid
16543 alternatives at this point. */
16546 cp_parser_error (parser
, "invalid non-type template argument");
16547 return error_mark_node
;
16551 /* If the argument wasn't successfully parsed as a type-id followed
16552 by '>>', the argument can only be a constant expression now.
16553 Otherwise, we try parsing the constant-expression tentatively,
16554 because the argument could really be a type-id. */
16556 cp_parser_parse_tentatively (parser
);
16558 if (cxx_dialect
<= cxx14
)
16559 argument
= cp_parser_constant_expression (parser
);
16562 /* With C++17 generalized non-type template arguments we need to handle
16563 lvalue constant expressions, too. */
16564 argument
= cp_parser_assignment_expression (parser
);
16565 require_potential_constant_expression (argument
);
16568 if (!maybe_type_id
)
16570 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16571 cp_parser_error (parser
, "expected template-argument");
16572 if (cp_parser_parse_definitely (parser
))
16574 /* We did our best to parse the argument as a non type-id, but that
16575 was the only alternative that matched (albeit with a '>' after
16576 it). We can assume it's just a typo from the user, and a
16577 diagnostic will then be issued. */
16578 return cp_parser_template_type_arg (parser
);
16581 /* Parse an explicit-instantiation.
16583 explicit-instantiation:
16584 template declaration
16586 Although the standard says `declaration', what it really means is:
16588 explicit-instantiation:
16589 template decl-specifier-seq [opt] declarator [opt] ;
16591 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16592 supposed to be allowed. A defect report has been filed about this
16597 explicit-instantiation:
16598 storage-class-specifier template
16599 decl-specifier-seq [opt] declarator [opt] ;
16600 function-specifier template
16601 decl-specifier-seq [opt] declarator [opt] ; */
16604 cp_parser_explicit_instantiation (cp_parser
* parser
)
16606 int declares_class_or_enum
;
16607 cp_decl_specifier_seq decl_specifiers
;
16608 tree extension_specifier
= NULL_TREE
;
16610 timevar_push (TV_TEMPLATE_INST
);
16612 /* Look for an (optional) storage-class-specifier or
16613 function-specifier. */
16614 if (cp_parser_allow_gnu_extensions_p (parser
))
16616 extension_specifier
16617 = cp_parser_storage_class_specifier_opt (parser
);
16618 if (!extension_specifier
)
16619 extension_specifier
16620 = cp_parser_function_specifier_opt (parser
,
16621 /*decl_specs=*/NULL
);
16624 /* Look for the `template' keyword. */
16625 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16626 /* Let the front end know that we are processing an explicit
16628 begin_explicit_instantiation ();
16629 /* [temp.explicit] says that we are supposed to ignore access
16630 control while processing explicit instantiation directives. */
16631 push_deferring_access_checks (dk_no_check
);
16632 /* Parse a decl-specifier-seq. */
16633 cp_parser_decl_specifier_seq (parser
,
16634 CP_PARSER_FLAGS_OPTIONAL
,
16636 &declares_class_or_enum
);
16637 /* If there was exactly one decl-specifier, and it declared a class,
16638 and there's no declarator, then we have an explicit type
16640 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16644 type
= check_tag_decl (&decl_specifiers
,
16645 /*explicit_type_instantiation_p=*/true);
16646 /* Turn access control back on for names used during
16647 template instantiation. */
16648 pop_deferring_access_checks ();
16650 do_type_instantiation (type
, extension_specifier
,
16651 /*complain=*/tf_error
);
16655 cp_declarator
*declarator
;
16658 /* Parse the declarator. */
16660 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16661 /*ctor_dtor_or_conv_p=*/NULL
,
16662 /*parenthesized_p=*/NULL
,
16663 /*member_p=*/false,
16664 /*friend_p=*/false);
16665 if (declares_class_or_enum
& 2)
16666 cp_parser_check_for_definition_in_return_type (declarator
,
16667 decl_specifiers
.type
,
16668 decl_specifiers
.locations
[ds_type_spec
]);
16669 if (declarator
!= cp_error_declarator
)
16671 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16672 permerror (decl_specifiers
.locations
[ds_inline
],
16673 "explicit instantiation shall not use"
16674 " %<inline%> specifier");
16675 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16676 permerror (decl_specifiers
.locations
[ds_constexpr
],
16677 "explicit instantiation shall not use"
16678 " %<constexpr%> specifier");
16680 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16681 NORMAL
, 0, &decl_specifiers
.attributes
);
16682 /* Turn access control back on for names used during
16683 template instantiation. */
16684 pop_deferring_access_checks ();
16685 /* Do the explicit instantiation. */
16686 do_decl_instantiation (decl
, extension_specifier
);
16690 pop_deferring_access_checks ();
16691 /* Skip the body of the explicit instantiation. */
16692 cp_parser_skip_to_end_of_statement (parser
);
16695 /* We're done with the instantiation. */
16696 end_explicit_instantiation ();
16698 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16700 timevar_pop (TV_TEMPLATE_INST
);
16703 /* Parse an explicit-specialization.
16705 explicit-specialization:
16706 template < > declaration
16708 Although the standard says `declaration', what it really means is:
16710 explicit-specialization:
16711 template <> decl-specifier [opt] init-declarator [opt] ;
16712 template <> function-definition
16713 template <> explicit-specialization
16714 template <> template-declaration */
16717 cp_parser_explicit_specialization (cp_parser
* parser
)
16719 bool need_lang_pop
;
16720 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16722 /* Look for the `template' keyword. */
16723 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16724 /* Look for the `<'. */
16725 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16726 /* Look for the `>'. */
16727 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16728 /* We have processed another parameter list. */
16729 ++parser
->num_template_parameter_lists
;
16732 A template ... explicit specialization ... shall not have C
16734 if (current_lang_name
== lang_name_c
)
16736 error_at (token
->location
, "template specialization with C linkage");
16737 maybe_show_extern_c_location ();
16738 /* Give it C++ linkage to avoid confusing other parts of the
16740 push_lang_context (lang_name_cplusplus
);
16741 need_lang_pop
= true;
16744 need_lang_pop
= false;
16745 /* Let the front end know that we are beginning a specialization. */
16746 if (!begin_specialization ())
16748 end_specialization ();
16752 /* If the next keyword is `template', we need to figure out whether
16753 or not we're looking a template-declaration. */
16754 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16756 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16757 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16758 cp_parser_template_declaration_after_export (parser
,
16759 /*member_p=*/false);
16761 cp_parser_explicit_specialization (parser
);
16764 /* Parse the dependent declaration. */
16765 cp_parser_single_declaration (parser
,
16767 /*member_p=*/false,
16768 /*explicit_specialization_p=*/true,
16769 /*friend_p=*/NULL
);
16770 /* We're done with the specialization. */
16771 end_specialization ();
16772 /* For the erroneous case of a template with C linkage, we pushed an
16773 implicit C++ linkage scope; exit that scope now. */
16775 pop_lang_context ();
16776 /* We're done with this parameter list. */
16777 --parser
->num_template_parameter_lists
;
16780 /* Parse a type-specifier.
16783 simple-type-specifier
16786 elaborated-type-specifier
16794 Returns a representation of the type-specifier. For a
16795 class-specifier, enum-specifier, or elaborated-type-specifier, a
16796 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16798 The parser flags FLAGS is used to control type-specifier parsing.
16800 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16801 in a decl-specifier-seq.
16803 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16804 class-specifier, enum-specifier, or elaborated-type-specifier, then
16805 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16806 if a type is declared; 2 if it is defined. Otherwise, it is set to
16809 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16810 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16811 is set to FALSE. */
16814 cp_parser_type_specifier (cp_parser
* parser
,
16815 cp_parser_flags flags
,
16816 cp_decl_specifier_seq
*decl_specs
,
16817 bool is_declaration
,
16818 int* declares_class_or_enum
,
16819 bool* is_cv_qualifier
)
16821 tree type_spec
= NULL_TREE
;
16824 cp_decl_spec ds
= ds_last
;
16826 /* Assume this type-specifier does not declare a new type. */
16827 if (declares_class_or_enum
)
16828 *declares_class_or_enum
= 0;
16829 /* And that it does not specify a cv-qualifier. */
16830 if (is_cv_qualifier
)
16831 *is_cv_qualifier
= false;
16832 /* Peek at the next token. */
16833 token
= cp_lexer_peek_token (parser
->lexer
);
16835 /* If we're looking at a keyword, we can use that to guide the
16836 production we choose. */
16837 keyword
= token
->keyword
;
16841 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16842 goto elaborated_type_specifier
;
16844 /* Look for the enum-specifier. */
16845 type_spec
= cp_parser_enum_specifier (parser
);
16846 /* If that worked, we're done. */
16849 if (declares_class_or_enum
)
16850 *declares_class_or_enum
= 2;
16852 cp_parser_set_decl_spec_type (decl_specs
,
16855 /*type_definition_p=*/true);
16859 goto elaborated_type_specifier
;
16861 /* Any of these indicate either a class-specifier, or an
16862 elaborated-type-specifier. */
16866 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16867 goto elaborated_type_specifier
;
16869 /* Parse tentatively so that we can back up if we don't find a
16870 class-specifier. */
16871 cp_parser_parse_tentatively (parser
);
16872 /* Look for the class-specifier. */
16873 type_spec
= cp_parser_class_specifier (parser
);
16874 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16875 /* If that worked, we're done. */
16876 if (cp_parser_parse_definitely (parser
))
16878 if (declares_class_or_enum
)
16879 *declares_class_or_enum
= 2;
16881 cp_parser_set_decl_spec_type (decl_specs
,
16884 /*type_definition_p=*/true);
16888 /* Fall through. */
16889 elaborated_type_specifier
:
16890 /* We're declaring (not defining) a class or enum. */
16891 if (declares_class_or_enum
)
16892 *declares_class_or_enum
= 1;
16894 /* Fall through. */
16896 /* Look for an elaborated-type-specifier. */
16898 = (cp_parser_elaborated_type_specifier
16900 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16903 cp_parser_set_decl_spec_type (decl_specs
,
16906 /*type_definition_p=*/false);
16911 if (is_cv_qualifier
)
16912 *is_cv_qualifier
= true;
16917 if (is_cv_qualifier
)
16918 *is_cv_qualifier
= true;
16923 if (is_cv_qualifier
)
16924 *is_cv_qualifier
= true;
16928 /* The `__complex__' keyword is a GNU extension. */
16936 /* Handle simple keywords. */
16941 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16942 decl_specs
->any_specifiers_p
= true;
16944 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16947 /* If we do not already have a type-specifier, assume we are looking
16948 at a simple-type-specifier. */
16949 type_spec
= cp_parser_simple_type_specifier (parser
,
16953 /* If we didn't find a type-specifier, and a type-specifier was not
16954 optional in this context, issue an error message. */
16955 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16957 cp_parser_error (parser
, "expected type specifier");
16958 return error_mark_node
;
16964 /* Parse a simple-type-specifier.
16966 simple-type-specifier:
16967 :: [opt] nested-name-specifier [opt] type-name
16968 :: [opt] nested-name-specifier template template-id
16983 simple-type-specifier:
16985 decltype ( expression )
16988 __underlying_type ( type-id )
16992 nested-name-specifier(opt) template-name
16996 simple-type-specifier:
16998 __typeof__ unary-expression
16999 __typeof__ ( type-id )
17000 __typeof__ ( type-id ) { initializer-list , [opt] }
17002 Concepts Extension:
17004 simple-type-specifier:
17005 constrained-type-specifier
17007 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17008 appropriately updated. */
17011 cp_parser_simple_type_specifier (cp_parser
* parser
,
17012 cp_decl_specifier_seq
*decl_specs
,
17013 cp_parser_flags flags
)
17015 tree type
= NULL_TREE
;
17019 /* Peek at the next token. */
17020 token
= cp_lexer_peek_token (parser
->lexer
);
17022 /* If we're looking at a keyword, things are easy. */
17023 switch (token
->keyword
)
17027 decl_specs
->explicit_char_p
= true;
17028 type
= char_type_node
;
17031 type
= char16_type_node
;
17034 type
= char32_type_node
;
17037 type
= wchar_type_node
;
17040 type
= boolean_type_node
;
17043 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17044 type
= short_integer_type_node
;
17048 decl_specs
->explicit_int_p
= true;
17049 type
= integer_type_node
;
17055 idx
= token
->keyword
- RID_INT_N_0
;
17056 if (! int_n_enabled_p
[idx
])
17060 decl_specs
->explicit_intN_p
= true;
17061 decl_specs
->int_n_idx
= idx
;
17063 type
= int_n_trees
[idx
].signed_type
;
17067 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17068 type
= long_integer_type_node
;
17071 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17072 type
= integer_type_node
;
17075 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17076 type
= unsigned_type_node
;
17079 type
= float_type_node
;
17082 type
= double_type_node
;
17085 type
= void_type_node
;
17089 maybe_warn_cpp0x (CPP0X_AUTO
);
17090 if (parser
->auto_is_implicit_function_template_parm_p
)
17092 /* The 'auto' might be the placeholder return type for a function decl
17093 with trailing return type. */
17094 bool have_trailing_return_fn_decl
= false;
17096 cp_parser_parse_tentatively (parser
);
17097 cp_lexer_consume_token (parser
->lexer
);
17098 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17099 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17100 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17101 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17105 cp_lexer_consume_token (parser
->lexer
);
17106 cp_parser_skip_to_closing_parenthesis (parser
,
17107 /*recovering*/false,
17109 /*consume_paren*/true);
17113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17115 have_trailing_return_fn_decl
= true;
17119 cp_lexer_consume_token (parser
->lexer
);
17121 cp_parser_abort_tentative_parse (parser
);
17123 if (have_trailing_return_fn_decl
)
17125 type
= make_auto ();
17129 if (cxx_dialect
>= cxx14
)
17131 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17132 type
= TREE_TYPE (type
);
17135 type
= error_mark_node
;
17137 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17139 if (cxx_dialect
< cxx14
)
17140 error_at (token
->location
,
17141 "use of %<auto%> in lambda parameter declaration "
17142 "only available with "
17143 "-std=c++14 or -std=gnu++14");
17145 else if (cxx_dialect
< cxx14
)
17146 error_at (token
->location
,
17147 "use of %<auto%> in parameter declaration "
17148 "only available with "
17149 "-std=c++14 or -std=gnu++14");
17150 else if (!flag_concepts
)
17151 pedwarn (token
->location
, 0,
17152 "use of %<auto%> in parameter declaration "
17153 "only available with -fconcepts");
17156 type
= make_auto ();
17160 /* Since DR 743, decltype can either be a simple-type-specifier by
17161 itself or begin a nested-name-specifier. Parsing it will replace
17162 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17163 handling below decide what to do. */
17164 cp_parser_decltype (parser
);
17165 cp_lexer_set_token_position (parser
->lexer
, token
);
17169 /* Consume the `typeof' token. */
17170 cp_lexer_consume_token (parser
->lexer
);
17171 /* Parse the operand to `typeof'. */
17172 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17173 /* If it is not already a TYPE, take its type. */
17174 if (!TYPE_P (type
))
17175 type
= finish_typeof (type
);
17178 cp_parser_set_decl_spec_type (decl_specs
, type
,
17180 /*type_definition_p=*/false);
17184 case RID_UNDERLYING_TYPE
:
17185 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17187 cp_parser_set_decl_spec_type (decl_specs
, type
,
17189 /*type_definition_p=*/false);
17194 case RID_DIRECT_BASES
:
17195 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17197 cp_parser_set_decl_spec_type (decl_specs
, type
,
17199 /*type_definition_p=*/false);
17205 /* If token is an already-parsed decltype not followed by ::,
17206 it's a simple-type-specifier. */
17207 if (token
->type
== CPP_DECLTYPE
17208 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17210 type
= saved_checks_value (token
->u
.tree_check_value
);
17213 cp_parser_set_decl_spec_type (decl_specs
, type
,
17215 /*type_definition_p=*/false);
17216 /* Remember that we are handling a decltype in order to
17217 implement the resolution of DR 1510 when the argument
17218 isn't instantiation dependent. */
17219 decl_specs
->decltype_p
= true;
17221 cp_lexer_consume_token (parser
->lexer
);
17225 /* If the type-specifier was for a built-in type, we're done. */
17228 /* Record the type. */
17230 && (token
->keyword
!= RID_SIGNED
17231 && token
->keyword
!= RID_UNSIGNED
17232 && token
->keyword
!= RID_SHORT
17233 && token
->keyword
!= RID_LONG
))
17234 cp_parser_set_decl_spec_type (decl_specs
,
17237 /*type_definition_p=*/false);
17239 decl_specs
->any_specifiers_p
= true;
17241 /* Consume the token. */
17242 cp_lexer_consume_token (parser
->lexer
);
17244 if (type
== error_mark_node
)
17245 return error_mark_node
;
17247 /* There is no valid C++ program where a non-template type is
17248 followed by a "<". That usually indicates that the user thought
17249 that the type was a template. */
17250 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17253 return TYPE_NAME (type
);
17256 /* The type-specifier must be a user-defined type. */
17257 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17262 /* Don't gobble tokens or issue error messages if this is an
17263 optional type-specifier. */
17264 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17265 cp_parser_parse_tentatively (parser
);
17267 token
= cp_lexer_peek_token (parser
->lexer
);
17269 /* Look for the optional `::' operator. */
17271 = (cp_parser_global_scope_opt (parser
,
17272 /*current_scope_valid_p=*/false)
17274 /* Look for the nested-name specifier. */
17276 = (cp_parser_nested_name_specifier_opt (parser
,
17277 /*typename_keyword_p=*/false,
17278 /*check_dependency_p=*/true,
17280 /*is_declaration=*/false)
17282 /* If we have seen a nested-name-specifier, and the next token
17283 is `template', then we are using the template-id production. */
17285 && cp_parser_optional_template_keyword (parser
))
17287 /* Look for the template-id. */
17288 type
= cp_parser_template_id (parser
,
17289 /*template_keyword_p=*/true,
17290 /*check_dependency_p=*/true,
17292 /*is_declaration=*/false);
17293 /* If the template-id did not name a type, we are out of
17295 if (TREE_CODE (type
) != TYPE_DECL
)
17297 cp_parser_error (parser
, "expected template-id for type");
17301 /* Otherwise, look for a type-name. */
17303 type
= cp_parser_type_name (parser
);
17304 /* Keep track of all name-lookups performed in class scopes. */
17308 && TREE_CODE (type
) == TYPE_DECL
17309 && identifier_p (DECL_NAME (type
)))
17310 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17311 /* If it didn't work out, we don't have a TYPE. */
17312 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17313 && !cp_parser_parse_definitely (parser
))
17315 if (!type
&& cxx_dialect
>= cxx17
)
17317 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17318 cp_parser_parse_tentatively (parser
);
17320 cp_parser_global_scope_opt (parser
,
17321 /*current_scope_valid_p=*/false);
17322 cp_parser_nested_name_specifier_opt (parser
,
17323 /*typename_keyword_p=*/false,
17324 /*check_dependency_p=*/true,
17326 /*is_declaration=*/false);
17327 tree name
= cp_parser_identifier (parser
);
17328 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17329 && parser
->scope
!= error_mark_node
)
17331 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17333 /*is_template=*/false,
17334 /*is_namespace=*/false,
17335 /*check_dependency=*/true,
17336 /*ambiguous_decls=*/NULL
,
17338 if (tmpl
&& tmpl
!= error_mark_node
17339 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17340 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17341 type
= make_template_placeholder (tmpl
);
17344 type
= error_mark_node
;
17345 if (!cp_parser_simulate_error (parser
))
17346 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17347 NLE_TYPE
, token
->location
);
17351 type
= error_mark_node
;
17353 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17354 && !cp_parser_parse_definitely (parser
))
17357 if (type
&& decl_specs
)
17358 cp_parser_set_decl_spec_type (decl_specs
, type
,
17360 /*type_definition_p=*/false);
17363 /* If we didn't get a type-name, issue an error message. */
17364 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17366 cp_parser_error (parser
, "expected type-name");
17367 return error_mark_node
;
17370 if (type
&& type
!= error_mark_node
)
17372 /* See if TYPE is an Objective-C type, and if so, parse and
17373 accept any protocol references following it. Do this before
17374 the cp_parser_check_for_invalid_template_id() call, because
17375 Objective-C types can be followed by '<...>' which would
17376 enclose protocol names rather than template arguments, and so
17377 everything is fine. */
17378 if (c_dialect_objc () && !parser
->scope
17379 && (objc_is_id (type
) || objc_is_class_name (type
)))
17381 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17382 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17384 /* Clobber the "unqualified" type previously entered into
17385 DECL_SPECS with the new, improved protocol-qualified version. */
17387 decl_specs
->type
= qual_type
;
17392 /* There is no valid C++ program where a non-template type is
17393 followed by a "<". That usually indicates that the user
17394 thought that the type was a template. */
17395 cp_parser_check_for_invalid_template_id (parser
, type
,
17403 /* Parse a type-name.
17409 simple-template-id [in c++0x]
17426 Returns a TYPE_DECL for the type. */
17429 cp_parser_type_name (cp_parser
* parser
)
17431 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17436 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17440 /* We can't know yet whether it is a class-name or not. */
17441 cp_parser_parse_tentatively (parser
);
17442 /* Try a class-name. */
17443 type_decl
= cp_parser_class_name (parser
,
17444 typename_keyword_p
,
17445 /*template_keyword_p=*/false,
17447 /*check_dependency_p=*/true,
17448 /*class_head_p=*/false,
17449 /*is_declaration=*/false);
17450 /* If it's not a class-name, keep looking. */
17451 if (!cp_parser_parse_definitely (parser
))
17453 if (cxx_dialect
< cxx11
)
17454 /* It must be a typedef-name or an enum-name. */
17455 return cp_parser_nonclass_name (parser
);
17457 cp_parser_parse_tentatively (parser
);
17458 /* It is either a simple-template-id representing an
17459 instantiation of an alias template... */
17460 type_decl
= cp_parser_template_id (parser
,
17461 /*template_keyword_p=*/false,
17462 /*check_dependency_p=*/true,
17464 /*is_declaration=*/false);
17465 /* Note that this must be an instantiation of an alias template
17466 because [temp.names]/6 says:
17468 A template-id that names an alias template specialization
17471 Whereas [temp.names]/7 says:
17473 A simple-template-id that names a class template
17474 specialization is a class-name.
17476 With concepts, this could also be a partial-concept-id that
17477 declares a non-type template parameter. */
17478 if (type_decl
!= NULL_TREE
17479 && TREE_CODE (type_decl
) == TYPE_DECL
17480 && TYPE_DECL_ALIAS_P (type_decl
))
17481 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17482 else if (is_constrained_parameter (type_decl
))
17483 /* Don't do anything. */ ;
17485 cp_parser_simulate_error (parser
);
17487 if (!cp_parser_parse_definitely (parser
))
17488 /* ... Or a typedef-name or an enum-name. */
17489 return cp_parser_nonclass_name (parser
);
17495 /* Check if DECL and ARGS can form a constrained-type-specifier.
17496 If ARGS is non-null, we try to form a concept check of the
17497 form DECL<?, ARGS> where ? is a wildcard that matches any
17498 kind of template argument. If ARGS is NULL, then we try to
17499 form a concept check of the form DECL<?>. */
17502 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17503 tree decl
, tree args
)
17505 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17507 /* If we a constrained-type-specifier cannot be deduced. */
17508 if (parser
->prevent_constrained_type_specifiers
)
17511 /* A constrained type specifier can only be found in an
17512 overload set or as a reference to a template declaration.
17514 FIXME: This might be masking a bug. It's possible that
17515 that the deduction below is causing template specializations
17516 to be formed with the wildcard as an argument. */
17517 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17520 /* Try to build a call expression that evaluates the
17521 concept. This can fail if the overload set refers
17522 only to non-templates. */
17523 tree placeholder
= build_nt (WILDCARD_DECL
);
17524 tree check
= build_concept_check (decl
, placeholder
, args
);
17525 if (check
== error_mark_node
)
17528 /* Deduce the checked constraint and the prototype parameter.
17530 FIXME: In certain cases, failure to deduce should be a
17531 diagnosable error. */
17534 if (!deduce_constrained_parameter (check
, conc
, proto
))
17537 /* In template parameter scope, this results in a constrained
17538 parameter. Return a descriptor of that parm. */
17539 if (processing_template_parmlist
)
17540 return build_constrained_parameter (conc
, proto
, args
);
17542 /* In a parameter-declaration-clause, constrained-type
17543 specifiers result in invented template parameters. */
17544 if (parser
->auto_is_implicit_function_template_parm_p
)
17546 tree x
= build_constrained_parameter (conc
, proto
, args
);
17547 return synthesize_implicit_template_parm (parser
, x
);
17551 /* Otherwise, we're in a context where the constrained
17552 type name is deduced and the constraint applies
17553 after deduction. */
17554 return make_constrained_auto (conc
, args
);
17560 /* If DECL refers to a concept, return a TYPE_DECL representing
17561 the result of using the constrained type specifier in the
17562 current context. DECL refers to a concept if
17564 - it is an overload set containing a function concept taking a single
17567 - it is a variable concept taking a single type argument. */
17570 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17573 && (TREE_CODE (decl
) == OVERLOAD
17574 || BASELINK_P (decl
)
17575 || variable_concept_p (decl
)))
17576 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17581 /* Check if DECL and ARGS form a partial-concept-id. If so,
17582 assign ID to the resulting constrained placeholder.
17584 Returns true if the partial-concept-id designates a placeholder
17585 and false otherwise. Note that *id is set to NULL_TREE in
17589 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17591 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17594 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17606 Returns a TYPE_DECL for the type. */
17609 cp_parser_nonclass_name (cp_parser
* parser
)
17614 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17615 identifier
= cp_parser_identifier (parser
);
17616 if (identifier
== error_mark_node
)
17617 return error_mark_node
;
17619 /* Look up the type-name. */
17620 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17622 type_decl
= strip_using_decl (type_decl
);
17624 /* If we found an overload set, then it may refer to a concept-name. */
17625 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17628 if (TREE_CODE (type_decl
) != TYPE_DECL
17629 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17631 /* See if this is an Objective-C type. */
17632 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17633 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17635 type_decl
= TYPE_NAME (type
);
17638 /* Issue an error if we did not find a type-name. */
17639 if (TREE_CODE (type_decl
) != TYPE_DECL
17640 /* In Objective-C, we have the complication that class names are
17641 normally type names and start declarations (eg, the
17642 "NSObject" in "NSObject *object;"), but can be used in an
17643 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17644 is an expression. So, a classname followed by a dot is not a
17645 valid type-name. */
17646 || (objc_is_class_name (TREE_TYPE (type_decl
))
17647 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17649 if (!cp_parser_simulate_error (parser
))
17650 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17651 NLE_TYPE
, token
->location
);
17652 return error_mark_node
;
17654 /* Remember that the name was used in the definition of the
17655 current class so that we can check later to see if the
17656 meaning would have been different after the class was
17657 entirely defined. */
17658 else if (type_decl
!= error_mark_node
17660 maybe_note_name_used_in_class (identifier
, type_decl
);
17665 /* Parse an elaborated-type-specifier. Note that the grammar given
17666 here incorporates the resolution to DR68.
17668 elaborated-type-specifier:
17669 class-key :: [opt] nested-name-specifier [opt] identifier
17670 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17671 enum-key :: [opt] nested-name-specifier [opt] identifier
17672 typename :: [opt] nested-name-specifier identifier
17673 typename :: [opt] nested-name-specifier template [opt]
17678 elaborated-type-specifier:
17679 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17680 class-key attributes :: [opt] nested-name-specifier [opt]
17681 template [opt] template-id
17682 enum attributes :: [opt] nested-name-specifier [opt] identifier
17684 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17685 declared `friend'. If IS_DECLARATION is TRUE, then this
17686 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17687 something is being declared.
17689 Returns the TYPE specified. */
17692 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17694 bool is_declaration
)
17696 enum tag_types tag_type
;
17698 tree type
= NULL_TREE
;
17699 tree attributes
= NULL_TREE
;
17701 cp_token
*token
= NULL
;
17703 /* See if we're looking at the `enum' keyword. */
17704 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17706 /* Consume the `enum' token. */
17707 cp_lexer_consume_token (parser
->lexer
);
17708 /* Remember that it's an enumeration type. */
17709 tag_type
= enum_type
;
17710 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17711 enums) is used here. */
17712 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17713 if (cp_parser_is_keyword (token
, RID_CLASS
)
17714 || cp_parser_is_keyword (token
, RID_STRUCT
))
17716 gcc_rich_location
richloc (token
->location
);
17717 richloc
.add_range (input_location
, false);
17718 richloc
.add_fixit_remove ();
17719 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17720 "a scoped enum must not use the %qD keyword",
17722 /* Consume the `struct' or `class' and parse it anyway. */
17723 cp_lexer_consume_token (parser
->lexer
);
17725 /* Parse the attributes. */
17726 attributes
= cp_parser_attributes_opt (parser
);
17728 /* Or, it might be `typename'. */
17729 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17732 /* Consume the `typename' token. */
17733 cp_lexer_consume_token (parser
->lexer
);
17734 /* Remember that it's a `typename' type. */
17735 tag_type
= typename_type
;
17737 /* Otherwise it must be a class-key. */
17740 tag_type
= cp_parser_class_key (parser
);
17741 if (tag_type
== none_type
)
17742 return error_mark_node
;
17743 /* Parse the attributes. */
17744 attributes
= cp_parser_attributes_opt (parser
);
17747 /* Look for the `::' operator. */
17748 globalscope
= cp_parser_global_scope_opt (parser
,
17749 /*current_scope_valid_p=*/false);
17750 /* Look for the nested-name-specifier. */
17751 tree nested_name_specifier
;
17752 if (tag_type
== typename_type
&& !globalscope
)
17754 nested_name_specifier
17755 = cp_parser_nested_name_specifier (parser
,
17756 /*typename_keyword_p=*/true,
17757 /*check_dependency_p=*/true,
17760 if (!nested_name_specifier
)
17761 return error_mark_node
;
17764 /* Even though `typename' is not present, the proposed resolution
17765 to Core Issue 180 says that in `class A<T>::B', `B' should be
17766 considered a type-name, even if `A<T>' is dependent. */
17767 nested_name_specifier
17768 = cp_parser_nested_name_specifier_opt (parser
,
17769 /*typename_keyword_p=*/true,
17770 /*check_dependency_p=*/true,
17773 /* For everything but enumeration types, consider a template-id.
17774 For an enumeration type, consider only a plain identifier. */
17775 if (tag_type
!= enum_type
)
17777 bool template_p
= false;
17780 /* Allow the `template' keyword. */
17781 template_p
= cp_parser_optional_template_keyword (parser
);
17782 /* If we didn't see `template', we don't know if there's a
17783 template-id or not. */
17785 cp_parser_parse_tentatively (parser
);
17786 /* Parse the template-id. */
17787 token
= cp_lexer_peek_token (parser
->lexer
);
17788 decl
= cp_parser_template_id (parser
, template_p
,
17789 /*check_dependency_p=*/true,
17792 /* If we didn't find a template-id, look for an ordinary
17794 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17796 /* We can get here when cp_parser_template_id, called by
17797 cp_parser_class_name with tag_type == none_type, succeeds
17798 and caches a BASELINK. Then, when called again here,
17799 instead of failing and returning an error_mark_node
17800 returns it (see template/typename17.C in C++11).
17801 ??? Could we diagnose this earlier? */
17802 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17804 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17805 type
= error_mark_node
;
17807 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17808 in effect, then we must assume that, upon instantiation, the
17809 template will correspond to a class. */
17810 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17811 && tag_type
== typename_type
)
17812 type
= make_typename_type (parser
->scope
, decl
,
17814 /*complain=*/tf_error
);
17815 /* If the `typename' keyword is in effect and DECL is not a type
17816 decl, then type is non existent. */
17817 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17819 else if (TREE_CODE (decl
) == TYPE_DECL
)
17821 type
= check_elaborated_type_specifier (tag_type
, decl
,
17822 /*allow_template_p=*/true);
17824 /* If the next token is a semicolon, this must be a specialization,
17825 instantiation, or friend declaration. Check the scope while we
17826 still know whether or not we had a nested-name-specifier. */
17827 if (type
!= error_mark_node
17828 && !nested_name_specifier
&& !is_friend
17829 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17830 check_unqualified_spec_or_inst (type
, token
->location
);
17832 else if (decl
== error_mark_node
)
17833 type
= error_mark_node
;
17838 token
= cp_lexer_peek_token (parser
->lexer
);
17839 identifier
= cp_parser_identifier (parser
);
17841 if (identifier
== error_mark_node
)
17843 parser
->scope
= NULL_TREE
;
17844 return error_mark_node
;
17847 /* For a `typename', we needn't call xref_tag. */
17848 if (tag_type
== typename_type
17849 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17850 return cp_parser_make_typename_type (parser
, identifier
,
17853 /* Template parameter lists apply only if we are not within a
17854 function parameter list. */
17855 bool template_parm_lists_apply
17856 = parser
->num_template_parameter_lists
;
17857 if (template_parm_lists_apply
)
17858 for (cp_binding_level
*s
= current_binding_level
;
17859 s
&& s
->kind
!= sk_template_parms
;
17860 s
= s
->level_chain
)
17861 if (s
->kind
== sk_function_parms
)
17862 template_parm_lists_apply
= false;
17864 /* Look up a qualified name in the usual way. */
17868 tree ambiguous_decls
;
17870 decl
= cp_parser_lookup_name (parser
, identifier
,
17872 /*is_template=*/false,
17873 /*is_namespace=*/false,
17874 /*check_dependency=*/true,
17878 /* If the lookup was ambiguous, an error will already have been
17880 if (ambiguous_decls
)
17881 return error_mark_node
;
17883 /* If we are parsing friend declaration, DECL may be a
17884 TEMPLATE_DECL tree node here. However, we need to check
17885 whether this TEMPLATE_DECL results in valid code. Consider
17886 the following example:
17889 template <class T> class C {};
17892 template <class T> friend class N::C; // #1, valid code
17894 template <class T> class Y {
17895 friend class N::C; // #2, invalid code
17898 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17899 name lookup of `N::C'. We see that friend declaration must
17900 be template for the code to be valid. Note that
17901 processing_template_decl does not work here since it is
17902 always 1 for the above two cases. */
17904 decl
= (cp_parser_maybe_treat_template_as_class
17905 (decl
, /*tag_name_p=*/is_friend
17906 && template_parm_lists_apply
));
17908 if (TREE_CODE (decl
) != TYPE_DECL
)
17910 cp_parser_diagnose_invalid_type_name (parser
,
17913 return error_mark_node
;
17916 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17918 bool allow_template
= (template_parm_lists_apply
17919 || DECL_SELF_REFERENCE_P (decl
));
17920 type
= check_elaborated_type_specifier (tag_type
, decl
,
17923 if (type
== error_mark_node
)
17924 return error_mark_node
;
17927 /* Forward declarations of nested types, such as
17932 are invalid unless all components preceding the final '::'
17933 are complete. If all enclosing types are complete, these
17934 declarations become merely pointless.
17936 Invalid forward declarations of nested types are errors
17937 caught elsewhere in parsing. Those that are pointless arrive
17940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17941 && !is_friend
&& !processing_explicit_instantiation
)
17942 warning (0, "declaration %qD does not declare anything", decl
);
17944 type
= TREE_TYPE (decl
);
17948 /* An elaborated-type-specifier sometimes introduces a new type and
17949 sometimes names an existing type. Normally, the rule is that it
17950 introduces a new type only if there is not an existing type of
17951 the same name already in scope. For example, given:
17954 void f() { struct S s; }
17956 the `struct S' in the body of `f' is the same `struct S' as in
17957 the global scope; the existing definition is used. However, if
17958 there were no global declaration, this would introduce a new
17959 local class named `S'.
17961 An exception to this rule applies to the following code:
17963 namespace N { struct S; }
17965 Here, the elaborated-type-specifier names a new type
17966 unconditionally; even if there is already an `S' in the
17967 containing scope this declaration names a new type.
17968 This exception only applies if the elaborated-type-specifier
17969 forms the complete declaration:
17973 A declaration consisting solely of `class-key identifier ;' is
17974 either a redeclaration of the name in the current scope or a
17975 forward declaration of the identifier as a class name. It
17976 introduces the name into the current scope.
17978 We are in this situation precisely when the next token is a `;'.
17980 An exception to the exception is that a `friend' declaration does
17981 *not* name a new type; i.e., given:
17983 struct S { friend struct T; };
17985 `T' is not a new type in the scope of `S'.
17987 Also, `new struct S' or `sizeof (struct S)' never results in the
17988 definition of a new type; a new type can only be declared in a
17989 declaration context. */
17995 /* Friends have special name lookup rules. */
17996 ts
= ts_within_enclosing_non_class
;
17997 else if (is_declaration
17998 && cp_lexer_next_token_is (parser
->lexer
,
18000 /* This is a `class-key identifier ;' */
18006 (template_parm_lists_apply
18007 && (cp_parser_next_token_starts_class_definition_p (parser
)
18008 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18009 /* An unqualified name was used to reference this type, so
18010 there were no qualifying templates. */
18011 if (template_parm_lists_apply
18012 && !cp_parser_check_template_parameters (parser
,
18013 /*num_templates=*/0,
18014 /*template_id*/false,
18016 /*declarator=*/NULL
))
18017 return error_mark_node
;
18018 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18022 if (type
== error_mark_node
)
18023 return error_mark_node
;
18025 /* Allow attributes on forward declarations of classes. */
18028 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18029 warning (OPT_Wattributes
,
18030 "attributes ignored on uninstantiated type");
18031 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18032 && ! processing_explicit_instantiation
)
18033 warning (OPT_Wattributes
,
18034 "attributes ignored on template instantiation");
18035 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18036 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18038 warning (OPT_Wattributes
,
18039 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18042 if (tag_type
!= enum_type
)
18044 /* Indicate whether this class was declared as a `class' or as a
18046 if (CLASS_TYPE_P (type
))
18047 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18048 cp_parser_check_class_key (tag_type
, type
);
18051 /* A "<" cannot follow an elaborated type specifier. If that
18052 happens, the user was probably trying to form a template-id. */
18053 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18059 /* Parse an enum-specifier.
18062 enum-head { enumerator-list [opt] }
18063 enum-head { enumerator-list , } [C++0x]
18066 enum-key identifier [opt] enum-base [opt]
18067 enum-key nested-name-specifier identifier enum-base [opt]
18072 enum struct [C++0x]
18075 : type-specifier-seq
18077 opaque-enum-specifier:
18078 enum-key identifier enum-base [opt] ;
18081 enum-key attributes[opt] identifier [opt] enum-base [opt]
18082 { enumerator-list [opt] }attributes[opt]
18083 enum-key attributes[opt] identifier [opt] enum-base [opt]
18084 { enumerator-list, }attributes[opt] [C++0x]
18086 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18087 if the token stream isn't an enum-specifier after all. */
18090 cp_parser_enum_specifier (cp_parser
* parser
)
18093 tree type
= NULL_TREE
;
18095 tree nested_name_specifier
= NULL_TREE
;
18097 bool scoped_enum_p
= false;
18098 bool has_underlying_type
= false;
18099 bool nested_being_defined
= false;
18100 bool new_value_list
= false;
18101 bool is_new_type
= false;
18102 bool is_unnamed
= false;
18103 tree underlying_type
= NULL_TREE
;
18104 cp_token
*type_start_token
= NULL
;
18105 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18107 parser
->colon_corrects_to_scope_p
= false;
18109 /* Parse tentatively so that we can back up if we don't find a
18111 cp_parser_parse_tentatively (parser
);
18113 /* Caller guarantees that the current token is 'enum', an identifier
18114 possibly follows, and the token after that is an opening brace.
18115 If we don't have an identifier, fabricate an anonymous name for
18116 the enumeration being defined. */
18117 cp_lexer_consume_token (parser
->lexer
);
18119 /* Parse the "class" or "struct", which indicates a scoped
18120 enumeration type in C++0x. */
18121 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18122 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18124 if (cxx_dialect
< cxx11
)
18125 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18127 /* Consume the `struct' or `class' token. */
18128 cp_lexer_consume_token (parser
->lexer
);
18130 scoped_enum_p
= true;
18133 attributes
= cp_parser_attributes_opt (parser
);
18135 /* Clear the qualification. */
18136 parser
->scope
= NULL_TREE
;
18137 parser
->qualifying_scope
= NULL_TREE
;
18138 parser
->object_scope
= NULL_TREE
;
18140 /* Figure out in what scope the declaration is being placed. */
18141 prev_scope
= current_scope ();
18143 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18145 push_deferring_access_checks (dk_no_check
);
18146 nested_name_specifier
18147 = cp_parser_nested_name_specifier_opt (parser
,
18148 /*typename_keyword_p=*/true,
18149 /*check_dependency_p=*/false,
18151 /*is_declaration=*/false);
18153 if (nested_name_specifier
)
18157 identifier
= cp_parser_identifier (parser
);
18158 name
= cp_parser_lookup_name (parser
, identifier
,
18160 /*is_template=*/false,
18161 /*is_namespace=*/false,
18162 /*check_dependency=*/true,
18163 /*ambiguous_decls=*/NULL
,
18165 if (name
&& name
!= error_mark_node
)
18167 type
= TREE_TYPE (name
);
18168 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18170 /* Are template enums allowed in ISO? */
18171 if (template_parm_scope_p ())
18172 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18173 "%qD is an enumeration template", name
);
18174 /* ignore a typename reference, for it will be solved by name
18179 else if (nested_name_specifier
== error_mark_node
)
18180 /* We already issued an error. */;
18183 error_at (type_start_token
->location
,
18184 "%qD does not name an enumeration in %qT",
18185 identifier
, nested_name_specifier
);
18186 nested_name_specifier
= error_mark_node
;
18191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18192 identifier
= cp_parser_identifier (parser
);
18195 identifier
= make_anon_name ();
18198 error_at (type_start_token
->location
,
18199 "unnamed scoped enum is not allowed");
18202 pop_deferring_access_checks ();
18204 /* Check for the `:' that denotes a specified underlying type in C++0x.
18205 Note that a ':' could also indicate a bitfield width, however. */
18206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18208 cp_decl_specifier_seq type_specifiers
;
18210 /* Consume the `:'. */
18211 cp_lexer_consume_token (parser
->lexer
);
18213 /* Parse the type-specifier-seq. */
18214 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18215 /*is_trailing_return=*/false,
18218 /* At this point this is surely not elaborated type specifier. */
18219 if (!cp_parser_parse_definitely (parser
))
18222 if (cxx_dialect
< cxx11
)
18223 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18225 has_underlying_type
= true;
18227 /* If that didn't work, stop. */
18228 if (type_specifiers
.type
!= error_mark_node
)
18230 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18231 /*initialized=*/0, NULL
);
18232 if (underlying_type
== error_mark_node
18233 || check_for_bare_parameter_packs (underlying_type
))
18234 underlying_type
= NULL_TREE
;
18238 /* Look for the `{' but don't consume it yet. */
18239 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18241 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18243 cp_parser_error (parser
, "expected %<{%>");
18244 if (has_underlying_type
)
18250 /* An opaque-enum-specifier must have a ';' here. */
18251 if ((scoped_enum_p
|| underlying_type
)
18252 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18254 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18255 if (has_underlying_type
)
18263 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18266 if (nested_name_specifier
)
18268 if (CLASS_TYPE_P (nested_name_specifier
))
18270 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18271 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18272 push_scope (nested_name_specifier
);
18274 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18276 push_nested_namespace (nested_name_specifier
);
18280 /* Issue an error message if type-definitions are forbidden here. */
18281 if (!cp_parser_check_type_definition (parser
))
18282 type
= error_mark_node
;
18284 /* Create the new type. We do this before consuming the opening
18285 brace so the enum will be recorded as being on the line of its
18286 tag (or the 'enum' keyword, if there is no tag). */
18287 type
= start_enum (identifier
, type
, underlying_type
,
18288 attributes
, scoped_enum_p
, &is_new_type
);
18290 /* If the next token is not '{' it is an opaque-enum-specifier or an
18291 elaborated-type-specifier. */
18292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18294 timevar_push (TV_PARSE_ENUM
);
18295 if (nested_name_specifier
18296 && nested_name_specifier
!= error_mark_node
)
18298 /* The following catches invalid code such as:
18299 enum class S<int>::E { A, B, C }; */
18300 if (!processing_specialization
18301 && CLASS_TYPE_P (nested_name_specifier
)
18302 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18303 error_at (type_start_token
->location
, "cannot add an enumerator "
18304 "list to a template instantiation");
18306 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18308 error_at (type_start_token
->location
,
18309 "%<%T::%E%> has not been declared",
18310 TYPE_CONTEXT (nested_name_specifier
),
18311 nested_name_specifier
);
18312 type
= error_mark_node
;
18314 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18315 && !CLASS_TYPE_P (nested_name_specifier
))
18317 error_at (type_start_token
->location
, "nested name specifier "
18318 "%qT for enum declaration does not name a class "
18319 "or namespace", nested_name_specifier
);
18320 type
= error_mark_node
;
18322 /* If that scope does not contain the scope in which the
18323 class was originally declared, the program is invalid. */
18324 else if (prev_scope
&& !is_ancestor (prev_scope
,
18325 nested_name_specifier
))
18327 if (at_namespace_scope_p ())
18328 error_at (type_start_token
->location
,
18329 "declaration of %qD in namespace %qD which does not "
18331 type
, prev_scope
, nested_name_specifier
);
18333 error_at (type_start_token
->location
,
18334 "declaration of %qD in %qD which does not "
18336 type
, prev_scope
, nested_name_specifier
);
18337 type
= error_mark_node
;
18339 /* If that scope is the scope where the declaration is being placed
18340 the program is invalid. */
18341 else if (CLASS_TYPE_P (nested_name_specifier
)
18342 && CLASS_TYPE_P (prev_scope
)
18343 && same_type_p (nested_name_specifier
, prev_scope
))
18345 permerror (type_start_token
->location
,
18346 "extra qualification not allowed");
18347 nested_name_specifier
= NULL_TREE
;
18352 begin_scope (sk_scoped_enum
, type
);
18354 /* Consume the opening brace. */
18355 matching_braces braces
;
18356 braces
.consume_open (parser
);
18358 if (type
== error_mark_node
)
18359 ; /* Nothing to add */
18360 else if (OPAQUE_ENUM_P (type
)
18361 || (cxx_dialect
> cxx98
&& processing_specialization
))
18363 new_value_list
= true;
18364 SET_OPAQUE_ENUM_P (type
, false);
18365 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18369 error_at (type_start_token
->location
,
18370 "multiple definition of %q#T", type
);
18371 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18372 "previous definition here");
18373 type
= error_mark_node
;
18376 if (type
== error_mark_node
)
18377 cp_parser_skip_to_end_of_block_or_statement (parser
);
18378 /* If the next token is not '}', then there are some enumerators. */
18379 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18381 if (is_unnamed
&& !scoped_enum_p
)
18382 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18383 "ISO C++ forbids empty unnamed enum");
18386 cp_parser_enumerator_list (parser
, type
);
18388 /* Consume the final '}'. */
18389 braces
.require_close (parser
);
18393 timevar_pop (TV_PARSE_ENUM
);
18397 /* If a ';' follows, then it is an opaque-enum-specifier
18398 and additional restrictions apply. */
18399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18402 error_at (type_start_token
->location
,
18403 "opaque-enum-specifier without name");
18404 else if (nested_name_specifier
)
18405 error_at (type_start_token
->location
,
18406 "opaque-enum-specifier must use a simple identifier");
18410 /* Look for trailing attributes to apply to this enumeration, and
18411 apply them if appropriate. */
18412 if (cp_parser_allow_gnu_extensions_p (parser
))
18414 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18415 cplus_decl_attributes (&type
,
18417 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18420 /* Finish up the enumeration. */
18421 if (type
!= error_mark_node
)
18423 if (new_value_list
)
18424 finish_enum_value_list (type
);
18426 finish_enum (type
);
18429 if (nested_name_specifier
)
18431 if (CLASS_TYPE_P (nested_name_specifier
))
18433 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18434 pop_scope (nested_name_specifier
);
18436 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18438 pop_nested_namespace (nested_name_specifier
);
18442 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18446 /* Parse an enumerator-list. The enumerators all have the indicated
18450 enumerator-definition
18451 enumerator-list , enumerator-definition */
18454 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18458 /* Parse an enumerator-definition. */
18459 cp_parser_enumerator_definition (parser
, type
);
18461 /* If the next token is not a ',', we've reached the end of
18463 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18465 /* Otherwise, consume the `,' and keep going. */
18466 cp_lexer_consume_token (parser
->lexer
);
18467 /* If the next token is a `}', there is a trailing comma. */
18468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18470 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18471 pedwarn (input_location
, OPT_Wpedantic
,
18472 "comma at end of enumerator list");
18478 /* Parse an enumerator-definition. The enumerator has the indicated
18481 enumerator-definition:
18483 enumerator = constant-expression
18490 enumerator-definition:
18491 enumerator attributes [opt]
18492 enumerator attributes [opt] = constant-expression */
18495 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18501 /* Save the input location because we are interested in the location
18502 of the identifier and not the location of the explicit value. */
18503 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18505 /* Look for the identifier. */
18506 identifier
= cp_parser_identifier (parser
);
18507 if (identifier
== error_mark_node
)
18510 /* Parse any specified attributes. */
18511 tree attrs
= cp_parser_attributes_opt (parser
);
18513 /* If the next token is an '=', then there is an explicit value. */
18514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18516 /* Consume the `=' token. */
18517 cp_lexer_consume_token (parser
->lexer
);
18518 /* Parse the value. */
18519 value
= cp_parser_constant_expression (parser
);
18524 /* If we are processing a template, make sure the initializer of the
18525 enumerator doesn't contain any bare template parameter pack. */
18526 if (check_for_bare_parameter_packs (value
))
18527 value
= error_mark_node
;
18529 /* Create the enumerator. */
18530 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18533 /* Parse a namespace-name.
18536 original-namespace-name
18539 Returns the NAMESPACE_DECL for the namespace. */
18542 cp_parser_namespace_name (cp_parser
* parser
)
18545 tree namespace_decl
;
18547 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18549 /* Get the name of the namespace. */
18550 identifier
= cp_parser_identifier (parser
);
18551 if (identifier
== error_mark_node
)
18552 return error_mark_node
;
18554 /* Look up the identifier in the currently active scope. Look only
18555 for namespaces, due to:
18557 [basic.lookup.udir]
18559 When looking up a namespace-name in a using-directive or alias
18560 definition, only namespace names are considered.
18564 [basic.lookup.qual]
18566 During the lookup of a name preceding the :: scope resolution
18567 operator, object, function, and enumerator names are ignored.
18569 (Note that cp_parser_qualifying_entity only calls this
18570 function if the token after the name is the scope resolution
18572 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18574 /*is_template=*/false,
18575 /*is_namespace=*/true,
18576 /*check_dependency=*/true,
18577 /*ambiguous_decls=*/NULL
,
18579 /* If it's not a namespace, issue an error. */
18580 if (namespace_decl
== error_mark_node
18581 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18583 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18585 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18586 if (namespace_decl
== error_mark_node
18587 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18588 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18591 cp_parser_error (parser
, "expected namespace-name");
18592 namespace_decl
= error_mark_node
;
18595 return namespace_decl
;
18598 /* Parse a namespace-definition.
18600 namespace-definition:
18601 named-namespace-definition
18602 unnamed-namespace-definition
18604 named-namespace-definition:
18605 original-namespace-definition
18606 extension-namespace-definition
18608 original-namespace-definition:
18609 namespace identifier { namespace-body }
18611 extension-namespace-definition:
18612 namespace original-namespace-name { namespace-body }
18614 unnamed-namespace-definition:
18615 namespace { namespace-body } */
18618 cp_parser_namespace_definition (cp_parser
* parser
)
18621 int nested_definition_count
= 0;
18623 cp_ensure_no_omp_declare_simd (parser
);
18624 cp_ensure_no_oacc_routine (parser
);
18626 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18630 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18631 cp_lexer_consume_token (parser
->lexer
);
18634 /* Look for the `namespace' keyword. */
18636 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18638 /* Parse any specified attributes before the identifier. */
18639 tree attribs
= cp_parser_attributes_opt (parser
);
18643 identifier
= NULL_TREE
;
18645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18647 identifier
= cp_parser_identifier (parser
);
18649 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18650 pedwarn (input_location
, OPT_Wpedantic
,
18651 "standard attributes on namespaces must precede "
18652 "the namespace name");
18654 /* Parse any attributes specified after the identifier. */
18655 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18658 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18661 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18662 pedwarn (input_location
, OPT_Wpedantic
,
18663 "nested namespace definitions only available with "
18664 "-std=c++17 or -std=gnu++17");
18666 /* Nested namespace names can create new namespaces (unlike
18667 other qualified-ids). */
18668 if (int count
= identifier
? push_namespace (identifier
) : 0)
18669 nested_definition_count
+= count
;
18671 cp_parser_error (parser
, "nested namespace name required");
18672 cp_lexer_consume_token (parser
->lexer
);
18675 if (nested_definition_count
&& !identifier
)
18676 cp_parser_error (parser
, "namespace name required");
18678 if (nested_definition_count
&& attribs
)
18679 error_at (token
->location
,
18680 "a nested namespace definition cannot have attributes");
18681 if (nested_definition_count
&& is_inline
)
18682 error_at (token
->location
,
18683 "a nested namespace definition cannot be inline");
18685 /* Start the namespace. */
18686 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18688 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18690 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18692 /* Look for the `{' to validate starting the namespace. */
18693 matching_braces braces
;
18694 if (braces
.require_open (parser
))
18696 /* Parse the body of the namespace. */
18697 cp_parser_namespace_body (parser
);
18699 /* Look for the final `}'. */
18700 braces
.require_close (parser
);
18703 if (has_visibility
)
18704 pop_visibility (1);
18706 /* Pop the nested namespace definitions. */
18707 while (nested_definition_count
--)
18711 /* Parse a namespace-body.
18714 declaration-seq [opt] */
18717 cp_parser_namespace_body (cp_parser
* parser
)
18719 cp_parser_declaration_seq_opt (parser
);
18722 /* Parse a namespace-alias-definition.
18724 namespace-alias-definition:
18725 namespace identifier = qualified-namespace-specifier ; */
18728 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18731 tree namespace_specifier
;
18733 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18735 /* Look for the `namespace' keyword. */
18736 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18737 /* Look for the identifier. */
18738 identifier
= cp_parser_identifier (parser
);
18739 if (identifier
== error_mark_node
)
18741 /* Look for the `=' token. */
18742 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18743 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18745 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18746 /* Skip the definition. */
18747 cp_lexer_consume_token (parser
->lexer
);
18748 if (cp_parser_skip_to_closing_brace (parser
))
18749 cp_lexer_consume_token (parser
->lexer
);
18752 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18753 /* Look for the qualified-namespace-specifier. */
18754 namespace_specifier
18755 = cp_parser_qualified_namespace_specifier (parser
);
18756 /* Look for the `;' token. */
18757 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18759 /* Register the alias in the symbol table. */
18760 do_namespace_alias (identifier
, namespace_specifier
);
18763 /* Parse a qualified-namespace-specifier.
18765 qualified-namespace-specifier:
18766 :: [opt] nested-name-specifier [opt] namespace-name
18768 Returns a NAMESPACE_DECL corresponding to the specified
18772 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18774 /* Look for the optional `::'. */
18775 cp_parser_global_scope_opt (parser
,
18776 /*current_scope_valid_p=*/false);
18778 /* Look for the optional nested-name-specifier. */
18779 cp_parser_nested_name_specifier_opt (parser
,
18780 /*typename_keyword_p=*/false,
18781 /*check_dependency_p=*/true,
18783 /*is_declaration=*/true);
18785 return cp_parser_namespace_name (parser
);
18788 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18789 access declaration.
18792 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18793 using :: unqualified-id ;
18795 access-declaration:
18801 cp_parser_using_declaration (cp_parser
* parser
,
18802 bool access_declaration_p
)
18805 bool typename_p
= false;
18806 bool global_scope_p
;
18810 int oldcount
= errorcount
;
18811 cp_token
*diag_token
= NULL
;
18813 if (access_declaration_p
)
18815 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18816 cp_parser_parse_tentatively (parser
);
18820 /* Look for the `using' keyword. */
18821 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18824 /* Peek at the next token. */
18825 token
= cp_lexer_peek_token (parser
->lexer
);
18826 /* See if it's `typename'. */
18827 if (token
->keyword
== RID_TYPENAME
)
18829 /* Remember that we've seen it. */
18831 /* Consume the `typename' token. */
18832 cp_lexer_consume_token (parser
->lexer
);
18836 /* Look for the optional global scope qualification. */
18838 = (cp_parser_global_scope_opt (parser
,
18839 /*current_scope_valid_p=*/false)
18842 /* If we saw `typename', or didn't see `::', then there must be a
18843 nested-name-specifier present. */
18844 if (typename_p
|| !global_scope_p
)
18846 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18847 /*check_dependency_p=*/true,
18849 /*is_declaration=*/true);
18850 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18852 cp_parser_skip_to_end_of_block_or_statement (parser
);
18856 /* Otherwise, we could be in either of the two productions. In that
18857 case, treat the nested-name-specifier as optional. */
18859 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18860 /*typename_keyword_p=*/false,
18861 /*check_dependency_p=*/true,
18863 /*is_declaration=*/true);
18865 qscope
= global_namespace
;
18866 else if (UNSCOPED_ENUM_P (qscope
))
18867 qscope
= CP_TYPE_CONTEXT (qscope
);
18869 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18870 /* Something has already gone wrong; there's no need to parse
18871 further. Since an error has occurred, the return value of
18872 cp_parser_parse_definitely will be false, as required. */
18873 return cp_parser_parse_definitely (parser
);
18875 token
= cp_lexer_peek_token (parser
->lexer
);
18876 /* Parse the unqualified-id. */
18877 identifier
= cp_parser_unqualified_id (parser
,
18878 /*template_keyword_p=*/false,
18879 /*check_dependency_p=*/true,
18880 /*declarator_p=*/true,
18881 /*optional_p=*/false);
18883 if (access_declaration_p
)
18885 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18886 cp_parser_simulate_error (parser
);
18887 if (!cp_parser_parse_definitely (parser
))
18890 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18892 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18893 if (cxx_dialect
< cxx17
18894 && !in_system_header_at (ell
->location
))
18895 pedwarn (ell
->location
, 0,
18896 "pack expansion in using-declaration only available "
18897 "with -std=c++17 or -std=gnu++17");
18898 qscope
= make_pack_expansion (qscope
);
18901 /* The function we call to handle a using-declaration is different
18902 depending on what scope we are in. */
18903 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18905 else if (!identifier_p (identifier
)
18906 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18907 /* [namespace.udecl]
18909 A using declaration shall not name a template-id. */
18910 error_at (token
->location
,
18911 "a template-id may not appear in a using-declaration");
18914 if (at_class_scope_p ())
18916 /* Create the USING_DECL. */
18917 decl
= do_class_using_decl (qscope
, identifier
);
18919 if (decl
&& typename_p
)
18920 USING_DECL_TYPENAME_P (decl
) = 1;
18922 if (check_for_bare_parameter_packs (decl
))
18924 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18928 /* Add it to the list of members in this class. */
18929 finish_member_declaration (decl
);
18933 decl
= cp_parser_lookup_name_simple (parser
,
18936 if (decl
== error_mark_node
)
18937 cp_parser_name_lookup_error (parser
, identifier
,
18940 else if (check_for_bare_parameter_packs (decl
))
18942 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18945 else if (!at_namespace_scope_p ())
18946 finish_local_using_decl (decl
, qscope
, identifier
);
18948 finish_namespace_using_decl (decl
, qscope
, identifier
);
18952 if (!access_declaration_p
18953 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18955 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18956 if (cxx_dialect
< cxx17
)
18957 pedwarn (comma
->location
, 0,
18958 "comma-separated list in using-declaration only available "
18959 "with -std=c++17 or -std=gnu++17");
18963 /* Look for the final `;'. */
18964 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18966 if (access_declaration_p
&& errorcount
== oldcount
)
18967 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18968 "access declarations are deprecated "
18969 "in favour of using-declarations; "
18970 "suggestion: add the %<using%> keyword");
18975 /* Parse an alias-declaration.
18978 using identifier attribute-specifier-seq [opt] = type-id */
18981 cp_parser_alias_declaration (cp_parser
* parser
)
18983 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18984 location_t id_location
;
18985 cp_declarator
*declarator
;
18986 cp_decl_specifier_seq decl_specs
;
18988 const char *saved_message
= NULL
;
18990 /* Look for the `using' keyword. */
18991 cp_token
*using_token
18992 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18993 if (using_token
== NULL
)
18994 return error_mark_node
;
18996 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18997 id
= cp_parser_identifier (parser
);
18998 if (id
== error_mark_node
)
18999 return error_mark_node
;
19001 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19002 attributes
= cp_parser_attributes_opt (parser
);
19003 if (attributes
== error_mark_node
)
19004 return error_mark_node
;
19006 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19008 if (cp_parser_error_occurred (parser
))
19009 return error_mark_node
;
19011 cp_parser_commit_to_tentative_parse (parser
);
19013 /* Now we are going to parse the type-id of the declaration. */
19018 "A type-specifier-seq shall not define a class or enumeration
19019 unless it appears in the type-id of an alias-declaration (7.1.3) that
19020 is not the declaration of a template-declaration."
19022 In other words, if we currently are in an alias template, the
19023 type-id should not define a type.
19025 So let's set parser->type_definition_forbidden_message in that
19026 case; cp_parser_check_type_definition (called by
19027 cp_parser_class_specifier) will then emit an error if a type is
19028 defined in the type-id. */
19029 if (parser
->num_template_parameter_lists
)
19031 saved_message
= parser
->type_definition_forbidden_message
;
19032 parser
->type_definition_forbidden_message
=
19033 G_("types may not be defined in alias template declarations");
19036 type
= cp_parser_type_id (parser
);
19038 /* Restore the error message if need be. */
19039 if (parser
->num_template_parameter_lists
)
19040 parser
->type_definition_forbidden_message
= saved_message
;
19042 if (type
== error_mark_node
19043 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19045 cp_parser_skip_to_end_of_block_or_statement (parser
);
19046 return error_mark_node
;
19049 /* A typedef-name can also be introduced by an alias-declaration. The
19050 identifier following the using keyword becomes a typedef-name. It has
19051 the same semantics as if it were introduced by the typedef
19052 specifier. In particular, it does not define a new type and it shall
19053 not appear in the type-id. */
19055 clear_decl_specs (&decl_specs
);
19056 decl_specs
.type
= type
;
19057 if (attributes
!= NULL_TREE
)
19059 decl_specs
.attributes
= attributes
;
19060 set_and_check_decl_spec_loc (&decl_specs
,
19064 set_and_check_decl_spec_loc (&decl_specs
,
19067 set_and_check_decl_spec_loc (&decl_specs
,
19071 if (parser
->num_template_parameter_lists
19072 && !cp_parser_check_template_parameters (parser
,
19073 /*num_templates=*/0,
19074 /*template_id*/false,
19076 /*declarator=*/NULL
))
19077 return error_mark_node
;
19079 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19080 declarator
->id_loc
= id_location
;
19082 member_p
= at_class_scope_p ();
19084 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19085 NULL_TREE
, attributes
);
19087 decl
= start_decl (declarator
, &decl_specs
, 0,
19088 attributes
, NULL_TREE
, &pushed_scope
);
19089 if (decl
== error_mark_node
)
19092 // Attach constraints to the alias declaration.
19093 if (flag_concepts
&& current_template_parms
)
19095 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19096 tree constr
= build_constraints (reqs
, NULL_TREE
);
19097 set_constraints (decl
, constr
);
19100 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19103 pop_scope (pushed_scope
);
19105 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19106 added into the symbol table; otherwise, return the TYPE_DECL. */
19107 if (DECL_LANG_SPECIFIC (decl
)
19108 && DECL_TEMPLATE_INFO (decl
)
19109 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19111 decl
= DECL_TI_TEMPLATE (decl
);
19113 check_member_template (decl
);
19119 /* Parse a using-directive.
19122 using namespace :: [opt] nested-name-specifier [opt]
19123 namespace-name ; */
19126 cp_parser_using_directive (cp_parser
* parser
)
19128 tree namespace_decl
;
19131 /* Look for the `using' keyword. */
19132 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19133 /* And the `namespace' keyword. */
19134 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19135 /* Look for the optional `::' operator. */
19136 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19137 /* And the optional nested-name-specifier. */
19138 cp_parser_nested_name_specifier_opt (parser
,
19139 /*typename_keyword_p=*/false,
19140 /*check_dependency_p=*/true,
19142 /*is_declaration=*/true);
19143 /* Get the namespace being used. */
19144 namespace_decl
= cp_parser_namespace_name (parser
);
19145 /* And any specified attributes. */
19146 attribs
= cp_parser_attributes_opt (parser
);
19148 /* Update the symbol table. */
19149 if (namespace_bindings_p ())
19150 finish_namespace_using_directive (namespace_decl
, attribs
);
19152 finish_local_using_directive (namespace_decl
, attribs
);
19154 /* Look for the final `;'. */
19155 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19158 /* Parse an asm-definition.
19161 asm ( string-literal ) ;
19166 asm volatile [opt] ( string-literal ) ;
19167 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19168 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19169 : asm-operand-list [opt] ) ;
19170 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19171 : asm-operand-list [opt]
19172 : asm-clobber-list [opt] ) ;
19173 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19174 : asm-clobber-list [opt]
19175 : asm-goto-list ) ; */
19178 cp_parser_asm_definition (cp_parser
* parser
)
19181 tree outputs
= NULL_TREE
;
19182 tree inputs
= NULL_TREE
;
19183 tree clobbers
= NULL_TREE
;
19184 tree labels
= NULL_TREE
;
19186 bool volatile_p
= false;
19187 bool extended_p
= false;
19188 bool invalid_inputs_p
= false;
19189 bool invalid_outputs_p
= false;
19190 bool goto_p
= false;
19191 required_token missing
= RT_NONE
;
19193 /* Look for the `asm' keyword. */
19194 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19196 if (parser
->in_function_body
19197 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19199 error ("%<asm%> in %<constexpr%> function");
19200 cp_function_chain
->invalid_constexpr
= true;
19203 /* See if the next token is `volatile'. */
19204 if (cp_parser_allow_gnu_extensions_p (parser
)
19205 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19207 /* Remember that we saw the `volatile' keyword. */
19209 /* Consume the token. */
19210 cp_lexer_consume_token (parser
->lexer
);
19212 if (cp_parser_allow_gnu_extensions_p (parser
)
19213 && parser
->in_function_body
19214 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19216 /* Remember that we saw the `goto' keyword. */
19218 /* Consume the token. */
19219 cp_lexer_consume_token (parser
->lexer
);
19221 /* Look for the opening `('. */
19222 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19224 /* Look for the string. */
19225 string
= cp_parser_string_literal (parser
, false, false);
19226 if (string
== error_mark_node
)
19228 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19229 /*consume_paren=*/true);
19233 /* If we're allowing GNU extensions, check for the extended assembly
19234 syntax. Unfortunately, the `:' tokens need not be separated by
19235 a space in C, and so, for compatibility, we tolerate that here
19236 too. Doing that means that we have to treat the `::' operator as
19238 if (cp_parser_allow_gnu_extensions_p (parser
)
19239 && parser
->in_function_body
19240 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19241 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19243 bool inputs_p
= false;
19244 bool clobbers_p
= false;
19245 bool labels_p
= false;
19247 /* The extended syntax was used. */
19250 /* Look for outputs. */
19251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19253 /* Consume the `:'. */
19254 cp_lexer_consume_token (parser
->lexer
);
19255 /* Parse the output-operands. */
19256 if (cp_lexer_next_token_is_not (parser
->lexer
,
19258 && cp_lexer_next_token_is_not (parser
->lexer
,
19260 && cp_lexer_next_token_is_not (parser
->lexer
,
19264 outputs
= cp_parser_asm_operand_list (parser
);
19265 if (outputs
== error_mark_node
)
19266 invalid_outputs_p
= true;
19269 /* If the next token is `::', there are no outputs, and the
19270 next token is the beginning of the inputs. */
19271 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19272 /* The inputs are coming next. */
19275 /* Look for inputs. */
19277 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19279 /* Consume the `:' or `::'. */
19280 cp_lexer_consume_token (parser
->lexer
);
19281 /* Parse the output-operands. */
19282 if (cp_lexer_next_token_is_not (parser
->lexer
,
19284 && cp_lexer_next_token_is_not (parser
->lexer
,
19286 && cp_lexer_next_token_is_not (parser
->lexer
,
19289 inputs
= cp_parser_asm_operand_list (parser
);
19290 if (inputs
== error_mark_node
)
19291 invalid_inputs_p
= true;
19294 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19295 /* The clobbers are coming next. */
19298 /* Look for clobbers. */
19300 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19303 /* Consume the `:' or `::'. */
19304 cp_lexer_consume_token (parser
->lexer
);
19305 /* Parse the clobbers. */
19306 if (cp_lexer_next_token_is_not (parser
->lexer
,
19308 && cp_lexer_next_token_is_not (parser
->lexer
,
19310 clobbers
= cp_parser_asm_clobber_list (parser
);
19313 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19314 /* The labels are coming next. */
19317 /* Look for labels. */
19319 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19322 /* Consume the `:' or `::'. */
19323 cp_lexer_consume_token (parser
->lexer
);
19324 /* Parse the labels. */
19325 labels
= cp_parser_asm_label_list (parser
);
19328 if (goto_p
&& !labels_p
)
19329 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19332 missing
= RT_COLON_SCOPE
;
19334 /* Look for the closing `)'. */
19335 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19336 missing
? missing
: RT_CLOSE_PAREN
))
19337 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19338 /*consume_paren=*/true);
19339 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19341 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19343 /* Create the ASM_EXPR. */
19344 if (parser
->in_function_body
)
19346 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19347 inputs
, clobbers
, labels
);
19348 /* If the extended syntax was not used, mark the ASM_EXPR. */
19351 tree temp
= asm_stmt
;
19352 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19353 temp
= TREE_OPERAND (temp
, 0);
19355 ASM_INPUT_P (temp
) = 1;
19359 symtab
->finalize_toplevel_asm (string
);
19363 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19364 type that comes from the decl-specifier-seq. */
19367 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19369 for (cp_declarator
*d
= declarator
; d
;)
19379 if (TYPE_PTRMEMFUNC_P (type
))
19380 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19381 type
= TREE_TYPE (type
);
19389 /* Declarators [gram.dcl.decl] */
19391 /* Parse an init-declarator.
19394 declarator initializer [opt]
19399 declarator asm-specification [opt] attributes [opt] initializer [opt]
19401 function-definition:
19402 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19404 decl-specifier-seq [opt] declarator function-try-block
19408 function-definition:
19409 __extension__ function-definition
19413 function-definition:
19414 decl-specifier-seq [opt] declarator function-transaction-block
19416 The DECL_SPECIFIERS apply to this declarator. Returns a
19417 representation of the entity declared. If MEMBER_P is TRUE, then
19418 this declarator appears in a class scope. The new DECL created by
19419 this declarator is returned.
19421 The CHECKS are access checks that should be performed once we know
19422 what entity is being declared (and, therefore, what classes have
19425 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19426 for a function-definition here as well. If the declarator is a
19427 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19428 be TRUE upon return. By that point, the function-definition will
19429 have been completely parsed.
19431 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19434 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19435 parsed declaration if it is an uninitialized single declarator not followed
19436 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19437 if present, will not be consumed. If returned, this declarator will be
19438 created with SD_INITIALIZED but will not call cp_finish_decl.
19440 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19441 and there is an initializer, the pointed location_t is set to the
19442 location of the '=' or `(', or '{' in C++11 token introducing the
19446 cp_parser_init_declarator (cp_parser
* parser
,
19447 cp_decl_specifier_seq
*decl_specifiers
,
19448 vec
<deferred_access_check
, va_gc
> *checks
,
19449 bool function_definition_allowed_p
,
19451 int declares_class_or_enum
,
19452 bool* function_definition_p
,
19453 tree
* maybe_range_for_decl
,
19454 location_t
* init_loc
,
19457 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19458 *attributes_start_token
= NULL
;
19459 cp_declarator
*declarator
;
19460 tree prefix_attributes
;
19461 tree attributes
= NULL
;
19462 tree asm_specification
;
19464 tree decl
= NULL_TREE
;
19466 int is_initialized
;
19467 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19468 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19470 enum cpp_ttype initialization_kind
;
19471 bool is_direct_init
= false;
19472 bool is_non_constant_init
;
19473 int ctor_dtor_or_conv_p
;
19474 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19475 tree pushed_scope
= NULL_TREE
;
19476 bool range_for_decl_p
= false;
19477 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19478 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19480 /* Gather the attributes that were provided with the
19481 decl-specifiers. */
19482 prefix_attributes
= decl_specifiers
->attributes
;
19484 /* Assume that this is not the declarator for a function
19486 if (function_definition_p
)
19487 *function_definition_p
= false;
19489 /* Default arguments are only permitted for function parameters. */
19490 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19491 parser
->default_arg_ok_p
= false;
19493 /* Defer access checks while parsing the declarator; we cannot know
19494 what names are accessible until we know what is being
19496 resume_deferring_access_checks ();
19498 token
= cp_lexer_peek_token (parser
->lexer
);
19500 /* Parse the declarator. */
19502 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19503 &ctor_dtor_or_conv_p
,
19504 /*parenthesized_p=*/NULL
,
19505 member_p
, friend_p
);
19506 /* Gather up the deferred checks. */
19507 stop_deferring_access_checks ();
19509 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19511 /* If the DECLARATOR was erroneous, there's no need to go
19513 if (declarator
== cp_error_declarator
)
19514 return error_mark_node
;
19516 /* Check that the number of template-parameter-lists is OK. */
19517 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19519 return error_mark_node
;
19521 if (declares_class_or_enum
& 2)
19522 cp_parser_check_for_definition_in_return_type (declarator
,
19523 decl_specifiers
->type
,
19524 decl_specifiers
->locations
[ds_type_spec
]);
19526 /* Figure out what scope the entity declared by the DECLARATOR is
19527 located in. `grokdeclarator' sometimes changes the scope, so
19528 we compute it now. */
19529 scope
= get_scope_of_declarator (declarator
);
19531 /* Perform any lookups in the declared type which were thought to be
19532 dependent, but are not in the scope of the declarator. */
19533 decl_specifiers
->type
19534 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19536 /* If we're allowing GNU extensions, look for an
19537 asm-specification. */
19538 if (cp_parser_allow_gnu_extensions_p (parser
))
19540 /* Look for an asm-specification. */
19541 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19542 asm_specification
= cp_parser_asm_specification_opt (parser
);
19545 asm_specification
= NULL_TREE
;
19547 /* Look for attributes. */
19548 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19549 attributes
= cp_parser_attributes_opt (parser
);
19551 /* Peek at the next token. */
19552 token
= cp_lexer_peek_token (parser
->lexer
);
19554 bool bogus_implicit_tmpl
= false;
19556 if (function_declarator_p (declarator
))
19558 /* Handle C++17 deduction guides. */
19559 if (!decl_specifiers
->type
19560 && ctor_dtor_or_conv_p
<= 0
19561 && cxx_dialect
>= cxx17
)
19563 cp_declarator
*id
= get_id_declarator (declarator
);
19564 tree name
= id
->u
.id
.unqualified_name
;
19565 parser
->scope
= id
->u
.id
.qualifying_scope
;
19566 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19568 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19569 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19571 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19572 id
->u
.id
.sfk
= sfk_deduction_guide
;
19573 ctor_dtor_or_conv_p
= 1;
19577 /* Check to see if the token indicates the start of a
19578 function-definition. */
19579 if (cp_parser_token_starts_function_definition_p (token
))
19581 if (!function_definition_allowed_p
)
19583 /* If a function-definition should not appear here, issue an
19585 cp_parser_error (parser
,
19586 "a function-definition is not allowed here");
19587 return error_mark_node
;
19590 location_t func_brace_location
19591 = cp_lexer_peek_token (parser
->lexer
)->location
;
19593 /* Neither attributes nor an asm-specification are allowed
19594 on a function-definition. */
19595 if (asm_specification
)
19596 error_at (asm_spec_start_token
->location
,
19597 "an asm-specification is not allowed "
19598 "on a function-definition");
19600 error_at (attributes_start_token
->location
,
19601 "attributes are not allowed "
19602 "on a function-definition");
19603 /* This is a function-definition. */
19604 *function_definition_p
= true;
19606 /* Parse the function definition. */
19608 decl
= cp_parser_save_member_function_body (parser
,
19611 prefix_attributes
);
19614 (cp_parser_function_definition_from_specifiers_and_declarator
19615 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19617 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19619 /* This is where the prologue starts... */
19620 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19621 = func_brace_location
;
19627 else if (parser
->fully_implicit_function_template_p
)
19629 /* A non-template declaration involving a function parameter list
19630 containing an implicit template parameter will be made into a
19631 template. If the resulting declaration is not going to be an
19632 actual function then finish the template scope here to prevent it.
19633 An error message will be issued once we have a decl to talk about.
19635 FIXME probably we should do type deduction rather than create an
19636 implicit template, but the standard currently doesn't allow it. */
19637 bogus_implicit_tmpl
= true;
19638 finish_fully_implicit_template (parser
, NULL_TREE
);
19643 Only in function declarations for constructors, destructors, type
19644 conversions, and deduction guides can the decl-specifier-seq be omitted.
19646 We explicitly postpone this check past the point where we handle
19647 function-definitions because we tolerate function-definitions
19648 that are missing their return types in some modes. */
19649 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19651 cp_parser_error (parser
,
19652 "expected constructor, destructor, or type conversion");
19653 return error_mark_node
;
19656 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19657 if (token
->type
== CPP_EQ
19658 || token
->type
== CPP_OPEN_PAREN
19659 || token
->type
== CPP_OPEN_BRACE
)
19661 is_initialized
= SD_INITIALIZED
;
19662 initialization_kind
= token
->type
;
19663 if (maybe_range_for_decl
)
19664 *maybe_range_for_decl
= error_mark_node
;
19665 tmp_init_loc
= token
->location
;
19666 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19667 *init_loc
= tmp_init_loc
;
19669 if (token
->type
== CPP_EQ
19670 && function_declarator_p (declarator
))
19672 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19673 if (t2
->keyword
== RID_DEFAULT
)
19674 is_initialized
= SD_DEFAULTED
;
19675 else if (t2
->keyword
== RID_DELETE
)
19676 is_initialized
= SD_DELETED
;
19681 /* If the init-declarator isn't initialized and isn't followed by a
19682 `,' or `;', it's not a valid init-declarator. */
19683 if (token
->type
!= CPP_COMMA
19684 && token
->type
!= CPP_SEMICOLON
)
19686 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19687 range_for_decl_p
= true;
19690 if (!maybe_range_for_decl
)
19691 cp_parser_error (parser
, "expected initializer");
19692 return error_mark_node
;
19695 is_initialized
= SD_UNINITIALIZED
;
19696 initialization_kind
= CPP_EOF
;
19699 /* Because start_decl has side-effects, we should only call it if we
19700 know we're going ahead. By this point, we know that we cannot
19701 possibly be looking at any other construct. */
19702 cp_parser_commit_to_tentative_parse (parser
);
19704 /* Enter the newly declared entry in the symbol table. If we're
19705 processing a declaration in a class-specifier, we wait until
19706 after processing the initializer. */
19709 if (parser
->in_unbraced_linkage_specification_p
)
19710 decl_specifiers
->storage_class
= sc_extern
;
19711 decl
= start_decl (declarator
, decl_specifiers
,
19712 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19713 attributes
, prefix_attributes
, &pushed_scope
);
19714 cp_finalize_omp_declare_simd (parser
, decl
);
19715 cp_finalize_oacc_routine (parser
, decl
, false);
19716 /* Adjust location of decl if declarator->id_loc is more appropriate:
19717 set, and decl wasn't merged with another decl, in which case its
19718 location would be different from input_location, and more accurate. */
19720 && declarator
->id_loc
!= UNKNOWN_LOCATION
19721 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19722 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19725 /* Enter the SCOPE. That way unqualified names appearing in the
19726 initializer will be looked up in SCOPE. */
19727 pushed_scope
= push_scope (scope
);
19729 /* Perform deferred access control checks, now that we know in which
19730 SCOPE the declared entity resides. */
19731 if (!member_p
&& decl
)
19733 tree saved_current_function_decl
= NULL_TREE
;
19735 /* If the entity being declared is a function, pretend that we
19736 are in its scope. If it is a `friend', it may have access to
19737 things that would not otherwise be accessible. */
19738 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19740 saved_current_function_decl
= current_function_decl
;
19741 current_function_decl
= decl
;
19744 /* Perform access checks for template parameters. */
19745 cp_parser_perform_template_parameter_access_checks (checks
);
19747 /* Perform the access control checks for the declarator and the
19748 decl-specifiers. */
19749 perform_deferred_access_checks (tf_warning_or_error
);
19751 /* Restore the saved value. */
19752 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19753 current_function_decl
= saved_current_function_decl
;
19756 /* Parse the initializer. */
19757 initializer
= NULL_TREE
;
19758 is_direct_init
= false;
19759 is_non_constant_init
= true;
19760 if (is_initialized
)
19762 if (function_declarator_p (declarator
))
19764 if (initialization_kind
== CPP_EQ
)
19765 initializer
= cp_parser_pure_specifier (parser
);
19768 /* If the declaration was erroneous, we don't really
19769 know what the user intended, so just silently
19770 consume the initializer. */
19771 if (decl
!= error_mark_node
)
19772 error_at (tmp_init_loc
, "initializer provided for function");
19773 cp_parser_skip_to_closing_parenthesis (parser
,
19774 /*recovering=*/true,
19775 /*or_comma=*/false,
19776 /*consume_paren=*/true);
19781 /* We want to record the extra mangling scope for in-class
19782 initializers of class members and initializers of static data
19783 member templates. The former involves deferring
19784 parsing of the initializer until end of class as with default
19785 arguments. So right here we only handle the latter. */
19786 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19787 start_lambda_scope (decl
);
19788 initializer
= cp_parser_initializer (parser
,
19790 &is_non_constant_init
);
19791 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19792 finish_lambda_scope ();
19793 if (initializer
== error_mark_node
)
19794 cp_parser_skip_to_end_of_statement (parser
);
19798 /* The old parser allows attributes to appear after a parenthesized
19799 initializer. Mark Mitchell proposed removing this functionality
19800 on the GCC mailing lists on 2002-08-13. This parser accepts the
19801 attributes -- but ignores them. Made a permerror in GCC 8. */
19802 if (cp_parser_allow_gnu_extensions_p (parser
)
19803 && initialization_kind
== CPP_OPEN_PAREN
19804 && cp_parser_attributes_opt (parser
)
19805 && permerror (input_location
,
19806 "attributes after parenthesized initializer ignored"))
19809 if (flag_permissive
&& !hint
)
19812 inform (input_location
,
19813 "this flexibility is deprecated and will be removed");
19817 /* And now complain about a non-function implicit template. */
19818 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19819 error_at (DECL_SOURCE_LOCATION (decl
),
19820 "non-function %qD declared as implicit template", decl
);
19822 /* For an in-class declaration, use `grokfield' to create the
19828 pop_scope (pushed_scope
);
19829 pushed_scope
= NULL_TREE
;
19831 decl
= grokfield (declarator
, decl_specifiers
,
19832 initializer
, !is_non_constant_init
,
19833 /*asmspec=*/NULL_TREE
,
19834 attr_chainon (attributes
, prefix_attributes
));
19835 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19836 cp_parser_save_default_args (parser
, decl
);
19837 cp_finalize_omp_declare_simd (parser
, decl
);
19838 cp_finalize_oacc_routine (parser
, decl
, false);
19841 /* Finish processing the declaration. But, skip member
19843 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19845 cp_finish_decl (decl
,
19846 initializer
, !is_non_constant_init
,
19848 /* If the initializer is in parentheses, then this is
19849 a direct-initialization, which means that an
19850 `explicit' constructor is OK. Otherwise, an
19851 `explicit' constructor cannot be used. */
19852 ((is_direct_init
|| !is_initialized
)
19853 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19855 else if ((cxx_dialect
!= cxx98
) && friend_p
19856 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19857 /* Core issue #226 (C++0x only): A default template-argument
19858 shall not be specified in a friend class template
19860 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19861 /*is_partial=*/false, /*is_friend_decl=*/1);
19863 if (!friend_p
&& pushed_scope
)
19864 pop_scope (pushed_scope
);
19866 if (function_declarator_p (declarator
)
19867 && parser
->fully_implicit_function_template_p
)
19870 decl
= finish_fully_implicit_template (parser
, decl
);
19872 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19875 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19876 && type_uses_auto (decl_specifiers
->type
))
19877 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19882 /* Parse a declarator.
19886 ptr-operator declarator
19888 abstract-declarator:
19889 ptr-operator abstract-declarator [opt]
19890 direct-abstract-declarator
19895 attributes [opt] direct-declarator
19896 attributes [opt] ptr-operator declarator
19898 abstract-declarator:
19899 attributes [opt] ptr-operator abstract-declarator [opt]
19900 attributes [opt] direct-abstract-declarator
19902 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19903 detect constructors, destructors, deduction guides, or conversion operators.
19904 It is set to -1 if the declarator is a name, and +1 if it is a
19905 function. Otherwise it is set to zero. Usually you just want to
19906 test for >0, but internally the negative value is used.
19908 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19909 a decl-specifier-seq unless it declares a constructor, destructor,
19910 or conversion. It might seem that we could check this condition in
19911 semantic analysis, rather than parsing, but that makes it difficult
19912 to handle something like `f()'. We want to notice that there are
19913 no decl-specifiers, and therefore realize that this is an
19914 expression, not a declaration.)
19916 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19917 the declarator is a direct-declarator of the form "(...)".
19919 MEMBER_P is true iff this declarator is a member-declarator.
19921 FRIEND_P is true iff this declarator is a friend. */
19923 static cp_declarator
*
19924 cp_parser_declarator (cp_parser
* parser
,
19925 cp_parser_declarator_kind dcl_kind
,
19926 int* ctor_dtor_or_conv_p
,
19927 bool* parenthesized_p
,
19928 bool member_p
, bool friend_p
)
19930 cp_declarator
*declarator
;
19931 enum tree_code code
;
19932 cp_cv_quals cv_quals
;
19934 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19936 /* Assume this is not a constructor, destructor, or type-conversion
19938 if (ctor_dtor_or_conv_p
)
19939 *ctor_dtor_or_conv_p
= 0;
19941 if (cp_parser_allow_gnu_extensions_p (parser
))
19942 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19944 /* Check for the ptr-operator production. */
19945 cp_parser_parse_tentatively (parser
);
19946 /* Parse the ptr-operator. */
19947 code
= cp_parser_ptr_operator (parser
,
19952 /* If that worked, then we have a ptr-operator. */
19953 if (cp_parser_parse_definitely (parser
))
19955 /* If a ptr-operator was found, then this declarator was not
19957 if (parenthesized_p
)
19958 *parenthesized_p
= true;
19959 /* The dependent declarator is optional if we are parsing an
19960 abstract-declarator. */
19961 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19962 cp_parser_parse_tentatively (parser
);
19964 /* Parse the dependent declarator. */
19965 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19966 /*ctor_dtor_or_conv_p=*/NULL
,
19967 /*parenthesized_p=*/NULL
,
19968 /*member_p=*/false,
19971 /* If we are parsing an abstract-declarator, we must handle the
19972 case where the dependent declarator is absent. */
19973 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19974 && !cp_parser_parse_definitely (parser
))
19977 declarator
= cp_parser_make_indirect_declarator
19978 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19980 /* Everything else is a direct-declarator. */
19983 if (parenthesized_p
)
19984 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19986 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19987 ctor_dtor_or_conv_p
,
19988 member_p
, friend_p
);
19991 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19992 declarator
->attributes
= gnu_attributes
;
19996 /* Parse a direct-declarator or direct-abstract-declarator.
20000 direct-declarator ( parameter-declaration-clause )
20001 cv-qualifier-seq [opt]
20002 ref-qualifier [opt]
20003 exception-specification [opt]
20004 direct-declarator [ constant-expression [opt] ]
20007 direct-abstract-declarator:
20008 direct-abstract-declarator [opt]
20009 ( parameter-declaration-clause )
20010 cv-qualifier-seq [opt]
20011 ref-qualifier [opt]
20012 exception-specification [opt]
20013 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20014 ( abstract-declarator )
20016 Returns a representation of the declarator. DCL_KIND is
20017 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20018 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20019 we are parsing a direct-declarator. It is
20020 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20021 of ambiguity we prefer an abstract declarator, as per
20022 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20023 as for cp_parser_declarator. */
20025 static cp_declarator
*
20026 cp_parser_direct_declarator (cp_parser
* parser
,
20027 cp_parser_declarator_kind dcl_kind
,
20028 int* ctor_dtor_or_conv_p
,
20029 bool member_p
, bool friend_p
)
20032 cp_declarator
*declarator
= NULL
;
20033 tree scope
= NULL_TREE
;
20034 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20035 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20037 tree pushed_scope
= NULL_TREE
;
20038 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20042 /* Peek at the next token. */
20043 token
= cp_lexer_peek_token (parser
->lexer
);
20044 if (token
->type
== CPP_OPEN_PAREN
)
20046 /* This is either a parameter-declaration-clause, or a
20047 parenthesized declarator. When we know we are parsing a
20048 named declarator, it must be a parenthesized declarator
20049 if FIRST is true. For instance, `(int)' is a
20050 parameter-declaration-clause, with an omitted
20051 direct-abstract-declarator. But `((*))', is a
20052 parenthesized abstract declarator. Finally, when T is a
20053 template parameter `(T)' is a
20054 parameter-declaration-clause, and not a parenthesized
20057 We first try and parse a parameter-declaration-clause,
20058 and then try a nested declarator (if FIRST is true).
20060 It is not an error for it not to be a
20061 parameter-declaration-clause, even when FIRST is
20067 The first is the declaration of a function while the
20068 second is the definition of a variable, including its
20071 Having seen only the parenthesis, we cannot know which of
20072 these two alternatives should be selected. Even more
20073 complex are examples like:
20078 The former is a function-declaration; the latter is a
20079 variable initialization.
20081 Thus again, we try a parameter-declaration-clause, and if
20082 that fails, we back out and return. */
20084 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20087 bool is_declarator
= false;
20091 /* In a member-declarator, the only valid interpretation
20092 of a parenthesis is the start of a
20093 parameter-declaration-clause. (It is invalid to
20094 initialize a static data member with a parenthesized
20095 initializer; only the "=" form of initialization is
20098 cp_parser_parse_tentatively (parser
);
20100 /* Consume the `('. */
20101 matching_parens parens
;
20102 parens
.consume_open (parser
);
20105 /* If this is going to be an abstract declarator, we're
20106 in a declarator and we can't have default args. */
20107 parser
->default_arg_ok_p
= false;
20108 parser
->in_declarator_p
= true;
20111 begin_scope (sk_function_parms
, NULL_TREE
);
20113 /* Parse the parameter-declaration-clause. */
20114 params
= cp_parser_parameter_declaration_clause (parser
);
20116 /* Consume the `)'. */
20117 parens
.require_close (parser
);
20119 /* If all went well, parse the cv-qualifier-seq,
20120 ref-qualifier and the exception-specification. */
20121 if (member_p
|| cp_parser_parse_definitely (parser
))
20123 cp_cv_quals cv_quals
;
20124 cp_virt_specifiers virt_specifiers
;
20125 cp_ref_qualifier ref_qual
;
20126 tree exception_specification
;
20129 bool memfn
= (member_p
|| (pushed_scope
20130 && CLASS_TYPE_P (pushed_scope
)));
20132 is_declarator
= true;
20134 if (ctor_dtor_or_conv_p
)
20135 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20138 /* Parse the cv-qualifier-seq. */
20139 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20140 /* Parse the ref-qualifier. */
20141 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20142 /* Parse the tx-qualifier. */
20143 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20144 /* And the exception-specification. */
20145 exception_specification
20146 = cp_parser_exception_specification_opt (parser
);
20148 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20150 /* In here, we handle cases where attribute is used after
20151 the function declaration. For example:
20152 void func (int x) __attribute__((vector(..))); */
20153 tree gnu_attrs
= NULL_TREE
;
20154 tree requires_clause
= NULL_TREE
;
20155 late_return
= (cp_parser_late_return_type_opt
20156 (parser
, declarator
, requires_clause
,
20157 memfn
? cv_quals
: -1));
20159 /* Parse the virt-specifier-seq. */
20160 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20162 /* Create the function-declarator. */
20163 declarator
= make_call_declarator (declarator
,
20169 exception_specification
,
20172 declarator
->std_attributes
= attrs
;
20173 declarator
->attributes
= gnu_attrs
;
20174 /* Any subsequent parameter lists are to do with
20175 return type, so are not those of the declared
20177 parser
->default_arg_ok_p
= false;
20180 /* Remove the function parms from scope. */
20181 pop_bindings_and_leave_scope ();
20184 /* Repeat the main loop. */
20188 /* If this is the first, we can try a parenthesized
20192 bool saved_in_type_id_in_expr_p
;
20194 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20195 parser
->in_declarator_p
= saved_in_declarator_p
;
20197 open_paren
= token
;
20198 /* Consume the `('. */
20199 matching_parens parens
;
20200 parens
.consume_open (parser
);
20201 /* Parse the nested declarator. */
20202 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20203 parser
->in_type_id_in_expr_p
= true;
20205 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20206 /*parenthesized_p=*/NULL
,
20207 member_p
, friend_p
);
20208 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20210 /* Expect a `)'. */
20211 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20212 if (!parens
.require_close (parser
))
20213 declarator
= cp_error_declarator
;
20214 if (declarator
== cp_error_declarator
)
20217 goto handle_declarator
;
20219 /* Otherwise, we must be done. */
20223 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20224 && token
->type
== CPP_OPEN_SQUARE
20225 && !cp_next_tokens_can_be_attribute_p (parser
))
20227 /* Parse an array-declarator. */
20228 tree bounds
, attrs
;
20230 if (ctor_dtor_or_conv_p
)
20231 *ctor_dtor_or_conv_p
= 0;
20235 parser
->default_arg_ok_p
= false;
20236 parser
->in_declarator_p
= true;
20237 /* Consume the `['. */
20238 cp_lexer_consume_token (parser
->lexer
);
20239 /* Peek at the next token. */
20240 token
= cp_lexer_peek_token (parser
->lexer
);
20241 /* If the next token is `]', then there is no
20242 constant-expression. */
20243 if (token
->type
!= CPP_CLOSE_SQUARE
)
20245 bool non_constant_p
;
20247 = cp_parser_constant_expression (parser
,
20248 /*allow_non_constant=*/true,
20250 if (!non_constant_p
)
20252 else if (error_operand_p (bounds
))
20253 /* Already gave an error. */;
20254 else if (!parser
->in_function_body
20255 || current_binding_level
->kind
== sk_function_parms
)
20257 /* Normally, the array bound must be an integral constant
20258 expression. However, as an extension, we allow VLAs
20259 in function scopes as long as they aren't part of a
20260 parameter declaration. */
20261 cp_parser_error (parser
,
20262 "array bound is not an integer constant");
20263 bounds
= error_mark_node
;
20265 else if (processing_template_decl
20266 && !type_dependent_expression_p (bounds
))
20268 /* Remember this wasn't a constant-expression. */
20269 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20270 TREE_SIDE_EFFECTS (bounds
) = 1;
20274 bounds
= NULL_TREE
;
20275 /* Look for the closing `]'. */
20276 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20278 declarator
= cp_error_declarator
;
20282 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20283 declarator
= make_array_declarator (declarator
, bounds
);
20284 declarator
->std_attributes
= attrs
;
20286 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20289 tree qualifying_scope
;
20290 tree unqualified_name
;
20292 special_function_kind sfk
;
20294 bool pack_expansion_p
= false;
20295 cp_token
*declarator_id_start_token
;
20297 /* Parse a declarator-id */
20298 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20301 cp_parser_parse_tentatively (parser
);
20303 /* If we see an ellipsis, we should be looking at a
20305 if (token
->type
== CPP_ELLIPSIS
)
20307 /* Consume the `...' */
20308 cp_lexer_consume_token (parser
->lexer
);
20310 pack_expansion_p
= true;
20314 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20316 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20317 qualifying_scope
= parser
->scope
;
20322 if (!unqualified_name
&& pack_expansion_p
)
20324 /* Check whether an error occurred. */
20325 okay
= !cp_parser_error_occurred (parser
);
20327 /* We already consumed the ellipsis to mark a
20328 parameter pack, but we have no way to report it,
20329 so abort the tentative parse. We will be exiting
20330 immediately anyway. */
20331 cp_parser_abort_tentative_parse (parser
);
20334 okay
= cp_parser_parse_definitely (parser
);
20337 unqualified_name
= error_mark_node
;
20338 else if (unqualified_name
20339 && (qualifying_scope
20340 || (!identifier_p (unqualified_name
))))
20342 cp_parser_error (parser
, "expected unqualified-id");
20343 unqualified_name
= error_mark_node
;
20347 if (!unqualified_name
)
20349 if (unqualified_name
== error_mark_node
)
20351 declarator
= cp_error_declarator
;
20352 pack_expansion_p
= false;
20353 declarator
->parameter_pack_p
= false;
20357 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20359 if (qualifying_scope
&& at_namespace_scope_p ()
20360 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20362 /* In the declaration of a member of a template class
20363 outside of the class itself, the SCOPE will sometimes
20364 be a TYPENAME_TYPE. For example, given:
20366 template <typename T>
20367 int S<T>::R::i = 3;
20369 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20370 this context, we must resolve S<T>::R to an ordinary
20371 type, rather than a typename type.
20373 The reason we normally avoid resolving TYPENAME_TYPEs
20374 is that a specialization of `S' might render
20375 `S<T>::R' not a type. However, if `S' is
20376 specialized, then this `i' will not be used, so there
20377 is no harm in resolving the types here. */
20380 /* Resolve the TYPENAME_TYPE. */
20381 type
= resolve_typename_type (qualifying_scope
,
20382 /*only_current_p=*/false);
20383 /* If that failed, the declarator is invalid. */
20384 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20386 if (typedef_variant_p (type
))
20387 error_at (declarator_id_start_token
->location
,
20388 "cannot define member of dependent typedef "
20391 error_at (declarator_id_start_token
->location
,
20392 "%<%T::%E%> is not a type",
20393 TYPE_CONTEXT (qualifying_scope
),
20394 TYPE_IDENTIFIER (qualifying_scope
));
20396 qualifying_scope
= type
;
20401 if (unqualified_name
)
20405 if (qualifying_scope
20406 && CLASS_TYPE_P (qualifying_scope
))
20407 class_type
= qualifying_scope
;
20409 class_type
= current_class_type
;
20411 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20413 tree name_type
= TREE_TYPE (unqualified_name
);
20415 if (!class_type
|| !same_type_p (name_type
, class_type
))
20417 /* We do not attempt to print the declarator
20418 here because we do not have enough
20419 information about its original syntactic
20421 cp_parser_error (parser
, "invalid declarator");
20422 declarator
= cp_error_declarator
;
20425 else if (qualifying_scope
20426 && CLASSTYPE_USE_TEMPLATE (name_type
))
20428 error_at (declarator_id_start_token
->location
,
20429 "invalid use of constructor as a template");
20430 inform (declarator_id_start_token
->location
,
20431 "use %<%T::%D%> instead of %<%T::%D%> to "
20432 "name the constructor in a qualified name",
20434 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20435 class_type
, name_type
);
20436 declarator
= cp_error_declarator
;
20439 unqualified_name
= constructor_name (class_type
);
20444 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20445 sfk
= sfk_destructor
;
20446 else if (identifier_p (unqualified_name
)
20447 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20448 sfk
= sfk_conversion
;
20449 else if (/* There's no way to declare a constructor
20450 for an unnamed type, even if the type
20451 got a name for linkage purposes. */
20452 !TYPE_WAS_UNNAMED (class_type
)
20453 /* Handle correctly (c++/19200):
20467 friend void N::S();
20469 && (!friend_p
|| class_type
== qualifying_scope
)
20470 && constructor_name_p (unqualified_name
,
20472 sfk
= sfk_constructor
;
20473 else if (is_overloaded_fn (unqualified_name
)
20474 && DECL_CONSTRUCTOR_P (get_first_fn
20475 (unqualified_name
)))
20476 sfk
= sfk_constructor
;
20478 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20479 *ctor_dtor_or_conv_p
= -1;
20482 declarator
= make_id_declarator (qualifying_scope
,
20485 declarator
->std_attributes
= attrs
;
20486 declarator
->id_loc
= token
->location
;
20487 declarator
->parameter_pack_p
= pack_expansion_p
;
20489 if (pack_expansion_p
)
20490 maybe_warn_variadic_templates ();
20493 handle_declarator
:;
20494 scope
= get_scope_of_declarator (declarator
);
20497 /* Any names that appear after the declarator-id for a
20498 member are looked up in the containing scope. */
20499 if (at_function_scope_p ())
20501 /* But declarations with qualified-ids can't appear in a
20503 cp_parser_error (parser
, "qualified-id in declaration");
20504 declarator
= cp_error_declarator
;
20507 pushed_scope
= push_scope (scope
);
20509 parser
->in_declarator_p
= true;
20510 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20511 || (declarator
&& declarator
->kind
== cdk_id
))
20512 /* Default args are only allowed on function
20514 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20516 parser
->default_arg_ok_p
= false;
20525 /* For an abstract declarator, we might wind up with nothing at this
20526 point. That's an error; the declarator is not optional. */
20528 cp_parser_error (parser
, "expected declarator");
20529 else if (open_paren
)
20531 /* Record overly parenthesized declarator so we can give a
20532 diagnostic about confusing decl/expr disambiguation. */
20533 if (declarator
->kind
== cdk_array
)
20535 /* If the open and close parens are on different lines, this
20536 is probably a formatting thing, so ignore. */
20537 expanded_location open
= expand_location (open_paren
->location
);
20538 expanded_location close
= expand_location (close_paren
->location
);
20539 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20543 declarator
->parenthesized
= open_paren
->location
;
20546 /* If we entered a scope, we must exit it now. */
20548 pop_scope (pushed_scope
);
20550 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20551 parser
->in_declarator_p
= saved_in_declarator_p
;
20556 /* Parse a ptr-operator.
20559 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20560 * cv-qualifier-seq [opt]
20562 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20563 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20568 & cv-qualifier-seq [opt]
20570 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20571 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20572 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20573 filled in with the TYPE containing the member. *CV_QUALS is
20574 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20575 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20576 Note that the tree codes returned by this function have nothing
20577 to do with the types of trees that will be eventually be created
20578 to represent the pointer or reference type being parsed. They are
20579 just constants with suggestive names. */
20580 static enum tree_code
20581 cp_parser_ptr_operator (cp_parser
* parser
,
20583 cp_cv_quals
*cv_quals
,
20586 enum tree_code code
= ERROR_MARK
;
20588 tree attrs
= NULL_TREE
;
20590 /* Assume that it's not a pointer-to-member. */
20592 /* And that there are no cv-qualifiers. */
20593 *cv_quals
= TYPE_UNQUALIFIED
;
20595 /* Peek at the next token. */
20596 token
= cp_lexer_peek_token (parser
->lexer
);
20598 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20599 if (token
->type
== CPP_MULT
)
20600 code
= INDIRECT_REF
;
20601 else if (token
->type
== CPP_AND
)
20603 else if ((cxx_dialect
!= cxx98
) &&
20604 token
->type
== CPP_AND_AND
) /* C++0x only */
20605 code
= NON_LVALUE_EXPR
;
20607 if (code
!= ERROR_MARK
)
20609 /* Consume the `*', `&' or `&&'. */
20610 cp_lexer_consume_token (parser
->lexer
);
20612 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20613 `&', if we are allowing GNU extensions. (The only qualifier
20614 that can legally appear after `&' is `restrict', but that is
20615 enforced during semantic analysis. */
20616 if (code
== INDIRECT_REF
20617 || cp_parser_allow_gnu_extensions_p (parser
))
20618 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20620 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20621 if (attributes
!= NULL
)
20622 *attributes
= attrs
;
20626 /* Try the pointer-to-member case. */
20627 cp_parser_parse_tentatively (parser
);
20628 /* Look for the optional `::' operator. */
20629 cp_parser_global_scope_opt (parser
,
20630 /*current_scope_valid_p=*/false);
20631 /* Look for the nested-name specifier. */
20632 token
= cp_lexer_peek_token (parser
->lexer
);
20633 cp_parser_nested_name_specifier (parser
,
20634 /*typename_keyword_p=*/false,
20635 /*check_dependency_p=*/true,
20637 /*is_declaration=*/false);
20638 /* If we found it, and the next token is a `*', then we are
20639 indeed looking at a pointer-to-member operator. */
20640 if (!cp_parser_error_occurred (parser
)
20641 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20643 /* Indicate that the `*' operator was used. */
20644 code
= INDIRECT_REF
;
20646 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20647 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20648 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20649 error_at (token
->location
, "cannot form pointer to member of "
20650 "non-class %q#T", parser
->scope
);
20653 /* The type of which the member is a member is given by the
20655 *type
= parser
->scope
;
20656 /* The next name will not be qualified. */
20657 parser
->scope
= NULL_TREE
;
20658 parser
->qualifying_scope
= NULL_TREE
;
20659 parser
->object_scope
= NULL_TREE
;
20660 /* Look for optional c++11 attributes. */
20661 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20662 if (attributes
!= NULL
)
20663 *attributes
= attrs
;
20664 /* Look for the optional cv-qualifier-seq. */
20665 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20668 /* If that didn't work we don't have a ptr-operator. */
20669 if (!cp_parser_parse_definitely (parser
))
20670 cp_parser_error (parser
, "expected ptr-operator");
20676 /* Parse an (optional) cv-qualifier-seq.
20679 cv-qualifier cv-qualifier-seq [opt]
20690 Returns a bitmask representing the cv-qualifiers. */
20693 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20695 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20700 cp_cv_quals cv_qualifier
;
20702 /* Peek at the next token. */
20703 token
= cp_lexer_peek_token (parser
->lexer
);
20704 /* See if it's a cv-qualifier. */
20705 switch (token
->keyword
)
20708 cv_qualifier
= TYPE_QUAL_CONST
;
20712 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20716 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20720 cv_qualifier
= TYPE_UNQUALIFIED
;
20727 if (cv_quals
& cv_qualifier
)
20729 gcc_rich_location
richloc (token
->location
);
20730 richloc
.add_fixit_remove ();
20731 error_at (&richloc
, "duplicate cv-qualifier");
20732 cp_lexer_purge_token (parser
->lexer
);
20736 cp_lexer_consume_token (parser
->lexer
);
20737 cv_quals
|= cv_qualifier
;
20744 /* Parse an (optional) ref-qualifier
20750 Returns cp_ref_qualifier representing ref-qualifier. */
20752 static cp_ref_qualifier
20753 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20755 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20757 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20758 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20763 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20764 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20766 switch (token
->type
)
20769 curr_ref_qual
= REF_QUAL_LVALUE
;
20773 curr_ref_qual
= REF_QUAL_RVALUE
;
20777 curr_ref_qual
= REF_QUAL_NONE
;
20781 if (!curr_ref_qual
)
20785 error_at (token
->location
, "multiple ref-qualifiers");
20786 cp_lexer_purge_token (parser
->lexer
);
20790 ref_qual
= curr_ref_qual
;
20791 cp_lexer_consume_token (parser
->lexer
);
20798 /* Parse an optional tx-qualifier.
20802 transaction_safe_dynamic */
20805 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20807 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20808 if (token
->type
== CPP_NAME
)
20810 tree name
= token
->u
.value
;
20811 const char *p
= IDENTIFIER_POINTER (name
);
20812 const int len
= strlen ("transaction_safe");
20813 if (!strncmp (p
, "transaction_safe", len
))
20817 || !strcmp (p
, "_dynamic"))
20819 cp_lexer_consume_token (parser
->lexer
);
20822 error ("%qE requires %<-fgnu-tm%>", name
);
20833 /* Parse an (optional) virt-specifier-seq.
20835 virt-specifier-seq:
20836 virt-specifier virt-specifier-seq [opt]
20842 Returns a bitmask representing the virt-specifiers. */
20844 static cp_virt_specifiers
20845 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20847 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20852 cp_virt_specifiers virt_specifier
;
20854 /* Peek at the next token. */
20855 token
= cp_lexer_peek_token (parser
->lexer
);
20856 /* See if it's a virt-specifier-qualifier. */
20857 if (token
->type
!= CPP_NAME
)
20859 if (id_equal (token
->u
.value
, "override"))
20861 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20862 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20864 else if (id_equal (token
->u
.value
, "final"))
20866 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20867 virt_specifier
= VIRT_SPEC_FINAL
;
20869 else if (id_equal (token
->u
.value
, "__final"))
20871 virt_specifier
= VIRT_SPEC_FINAL
;
20876 if (virt_specifiers
& virt_specifier
)
20878 gcc_rich_location
richloc (token
->location
);
20879 richloc
.add_fixit_remove ();
20880 error_at (&richloc
, "duplicate virt-specifier");
20881 cp_lexer_purge_token (parser
->lexer
);
20885 cp_lexer_consume_token (parser
->lexer
);
20886 virt_specifiers
|= virt_specifier
;
20889 return virt_specifiers
;
20892 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20893 is in scope even though it isn't real. */
20896 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20900 if (current_class_ptr
)
20902 /* We don't clear this between NSDMIs. Is it already what we want? */
20903 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20904 if (DECL_P (current_class_ptr
)
20905 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20906 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20907 && cp_type_quals (type
) == quals
)
20911 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20912 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20913 current_class_ptr
= NULL_TREE
;
20915 = cp_build_fold_indirect_ref (this_parm
);
20916 current_class_ptr
= this_parm
;
20919 /* Return true iff our current scope is a non-static data member
20923 parsing_nsdmi (void)
20925 /* We recognize NSDMI context by the context-less 'this' pointer set up
20926 by the function above. */
20927 if (current_class_ptr
20928 && TREE_CODE (current_class_ptr
) == PARM_DECL
20929 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20934 /* Parse a late-specified return type, if any. This is not a separate
20935 non-terminal, but part of a function declarator, which looks like
20937 -> trailing-type-specifier-seq abstract-declarator(opt)
20939 Returns the type indicated by the type-id.
20941 In addition to this, parse any queued up #pragma omp declare simd
20942 clauses, and #pragma acc routine clauses.
20944 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20948 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20949 tree
& requires_clause
, cp_cv_quals quals
)
20952 tree type
= NULL_TREE
;
20953 bool declare_simd_p
= (parser
->omp_declare_simd
20955 && declarator
->kind
== cdk_id
);
20957 bool oacc_routine_p
= (parser
->oacc_routine
20959 && declarator
->kind
== cdk_id
);
20961 /* Peek at the next token. */
20962 token
= cp_lexer_peek_token (parser
->lexer
);
20963 /* A late-specified return type is indicated by an initial '->'. */
20964 if (token
->type
!= CPP_DEREF
20965 && token
->keyword
!= RID_REQUIRES
20966 && !(token
->type
== CPP_NAME
20967 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20968 && !(declare_simd_p
|| oacc_routine_p
))
20971 tree save_ccp
= current_class_ptr
;
20972 tree save_ccr
= current_class_ref
;
20975 /* DR 1207: 'this' is in scope in the trailing return type. */
20976 inject_this_parameter (current_class_type
, quals
);
20979 if (token
->type
== CPP_DEREF
)
20981 /* Consume the ->. */
20982 cp_lexer_consume_token (parser
->lexer
);
20984 type
= cp_parser_trailing_type_id (parser
);
20987 /* Function declarations may be followed by a trailing
20988 requires-clause. */
20989 requires_clause
= cp_parser_requires_clause_opt (parser
);
20991 if (declare_simd_p
)
20992 declarator
->attributes
20993 = cp_parser_late_parsing_omp_declare_simd (parser
,
20994 declarator
->attributes
);
20995 if (oacc_routine_p
)
20996 declarator
->attributes
20997 = cp_parser_late_parsing_oacc_routine (parser
,
20998 declarator
->attributes
);
21002 current_class_ptr
= save_ccp
;
21003 current_class_ref
= save_ccr
;
21009 /* Parse a declarator-id.
21013 :: [opt] nested-name-specifier [opt] type-name
21015 In the `id-expression' case, the value returned is as for
21016 cp_parser_id_expression if the id-expression was an unqualified-id.
21017 If the id-expression was a qualified-id, then a SCOPE_REF is
21018 returned. The first operand is the scope (either a NAMESPACE_DECL
21019 or TREE_TYPE), but the second is still just a representation of an
21023 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21026 /* The expression must be an id-expression. Assume that qualified
21027 names are the names of types so that:
21030 int S<T>::R::i = 3;
21032 will work; we must treat `S<T>::R' as the name of a type.
21033 Similarly, assume that qualified names are templates, where
21037 int S<T>::R<T>::i = 3;
21040 id
= cp_parser_id_expression (parser
,
21041 /*template_keyword_p=*/false,
21042 /*check_dependency_p=*/false,
21043 /*template_p=*/NULL
,
21044 /*declarator_p=*/true,
21046 if (id
&& BASELINK_P (id
))
21047 id
= BASELINK_FUNCTIONS (id
);
21051 /* Parse a type-id.
21054 type-specifier-seq abstract-declarator [opt]
21056 Returns the TYPE specified. */
21059 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21060 bool is_trailing_return
)
21062 cp_decl_specifier_seq type_specifier_seq
;
21063 cp_declarator
*abstract_declarator
;
21065 /* Parse the type-specifier-seq. */
21066 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21067 is_trailing_return
,
21068 &type_specifier_seq
);
21069 if (is_template_arg
&& type_specifier_seq
.type
21070 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21071 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21072 /* A bare template name as a template argument is a template template
21073 argument, not a placeholder, so fail parsing it as a type argument. */
21075 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21076 cp_parser_simulate_error (parser
);
21077 return error_mark_node
;
21079 if (type_specifier_seq
.type
== error_mark_node
)
21080 return error_mark_node
;
21082 /* There might or might not be an abstract declarator. */
21083 cp_parser_parse_tentatively (parser
);
21084 /* Look for the declarator. */
21085 abstract_declarator
21086 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21087 /*parenthesized_p=*/NULL
,
21088 /*member_p=*/false,
21089 /*friend_p=*/false);
21090 /* Check to see if there really was a declarator. */
21091 if (!cp_parser_parse_definitely (parser
))
21092 abstract_declarator
= NULL
;
21094 if (type_specifier_seq
.type
21095 /* The concepts TS allows 'auto' as a type-id. */
21096 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21097 /* None of the valid uses of 'auto' in C++14 involve the type-id
21098 nonterminal, but it is valid in a trailing-return-type. */
21099 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21100 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21102 /* A type-id with type 'auto' is only ok if the abstract declarator
21103 is a function declarator with a late-specified return type.
21105 A type-id with 'auto' is also valid in a trailing-return-type
21106 in a compound-requirement. */
21107 if (abstract_declarator
21108 && abstract_declarator
->kind
== cdk_function
21109 && abstract_declarator
->u
.function
.late_return_type
)
21111 else if (parser
->in_result_type_constraint_p
)
21115 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21116 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21118 error_at (loc
, "missing template arguments after %qT",
21120 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21124 error_at (loc
, "invalid use of %qT", auto_node
);
21125 return error_mark_node
;
21129 return groktypename (&type_specifier_seq
, abstract_declarator
,
21134 cp_parser_type_id (cp_parser
*parser
)
21136 return cp_parser_type_id_1 (parser
, false, false);
21140 cp_parser_template_type_arg (cp_parser
*parser
)
21143 const char *saved_message
= parser
->type_definition_forbidden_message
;
21144 parser
->type_definition_forbidden_message
21145 = G_("types may not be defined in template arguments");
21146 r
= cp_parser_type_id_1 (parser
, true, false);
21147 parser
->type_definition_forbidden_message
= saved_message
;
21148 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21150 error ("invalid use of %<auto%> in template argument");
21151 r
= error_mark_node
;
21157 cp_parser_trailing_type_id (cp_parser
*parser
)
21159 return cp_parser_type_id_1 (parser
, false, true);
21162 /* Parse a type-specifier-seq.
21164 type-specifier-seq:
21165 type-specifier type-specifier-seq [opt]
21169 type-specifier-seq:
21170 attributes type-specifier-seq [opt]
21172 If IS_DECLARATION is true, we are at the start of a "condition" or
21173 exception-declaration, so we might be followed by a declarator-id.
21175 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21176 i.e. we've just seen "->".
21178 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21181 cp_parser_type_specifier_seq (cp_parser
* parser
,
21182 bool is_declaration
,
21183 bool is_trailing_return
,
21184 cp_decl_specifier_seq
*type_specifier_seq
)
21186 bool seen_type_specifier
= false;
21187 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21188 cp_token
*start_token
= NULL
;
21190 /* Clear the TYPE_SPECIFIER_SEQ. */
21191 clear_decl_specs (type_specifier_seq
);
21193 /* In the context of a trailing return type, enum E { } is an
21194 elaborated-type-specifier followed by a function-body, not an
21196 if (is_trailing_return
)
21197 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21199 /* Parse the type-specifiers and attributes. */
21202 tree type_specifier
;
21203 bool is_cv_qualifier
;
21205 /* Check for attributes first. */
21206 if (cp_next_tokens_can_be_attribute_p (parser
))
21208 type_specifier_seq
->attributes
21209 = attr_chainon (type_specifier_seq
->attributes
,
21210 cp_parser_attributes_opt (parser
));
21214 /* record the token of the beginning of the type specifier seq,
21215 for error reporting purposes*/
21217 start_token
= cp_lexer_peek_token (parser
->lexer
);
21219 /* Look for the type-specifier. */
21220 type_specifier
= cp_parser_type_specifier (parser
,
21222 type_specifier_seq
,
21223 /*is_declaration=*/false,
21226 if (!type_specifier
)
21228 /* If the first type-specifier could not be found, this is not a
21229 type-specifier-seq at all. */
21230 if (!seen_type_specifier
)
21232 /* Set in_declarator_p to avoid skipping to the semicolon. */
21233 int in_decl
= parser
->in_declarator_p
;
21234 parser
->in_declarator_p
= true;
21236 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21237 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21238 cp_parser_error (parser
, "expected type-specifier");
21240 parser
->in_declarator_p
= in_decl
;
21242 type_specifier_seq
->type
= error_mark_node
;
21245 /* If subsequent type-specifiers could not be found, the
21246 type-specifier-seq is complete. */
21250 seen_type_specifier
= true;
21251 /* The standard says that a condition can be:
21253 type-specifier-seq declarator = assignment-expression
21260 we should treat the "S" as a declarator, not as a
21261 type-specifier. The standard doesn't say that explicitly for
21262 type-specifier-seq, but it does say that for
21263 decl-specifier-seq in an ordinary declaration. Perhaps it
21264 would be clearer just to allow a decl-specifier-seq here, and
21265 then add a semantic restriction that if any decl-specifiers
21266 that are not type-specifiers appear, the program is invalid. */
21267 if (is_declaration
&& !is_cv_qualifier
)
21268 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21272 /* Return whether the function currently being declared has an associated
21273 template parameter list. */
21276 function_being_declared_is_template_p (cp_parser
* parser
)
21278 if (!current_template_parms
|| processing_template_parmlist
)
21281 if (parser
->implicit_template_scope
)
21284 if (at_class_scope_p ()
21285 && TYPE_BEING_DEFINED (current_class_type
))
21286 return parser
->num_template_parameter_lists
!= 0;
21288 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21289 (current_class_type
));
21292 /* Parse a parameter-declaration-clause.
21294 parameter-declaration-clause:
21295 parameter-declaration-list [opt] ... [opt]
21296 parameter-declaration-list , ...
21298 Returns a representation for the parameter declarations. A return
21299 value of NULL indicates a parameter-declaration-clause consisting
21300 only of an ellipsis. */
21303 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21309 temp_override
<bool> cleanup
21310 (parser
->auto_is_implicit_function_template_parm_p
);
21312 if (!processing_specialization
21313 && !processing_template_parmlist
21314 && !processing_explicit_instantiation
21315 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21316 actual function or a random abstract declarator. */
21317 && parser
->default_arg_ok_p
)
21318 if (!current_function_decl
21319 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21320 parser
->auto_is_implicit_function_template_parm_p
= true;
21322 /* Peek at the next token. */
21323 token
= cp_lexer_peek_token (parser
->lexer
);
21324 /* Check for trivial parameter-declaration-clauses. */
21325 if (token
->type
== CPP_ELLIPSIS
)
21327 /* Consume the `...' token. */
21328 cp_lexer_consume_token (parser
->lexer
);
21331 else if (token
->type
== CPP_CLOSE_PAREN
)
21332 /* There are no parameters. */
21334 #ifndef NO_IMPLICIT_EXTERN_C
21335 if (in_system_header_at (input_location
)
21336 && current_class_type
== NULL
21337 && current_lang_name
== lang_name_c
)
21341 return void_list_node
;
21343 /* Check for `(void)', too, which is a special case. */
21344 else if (token
->keyword
== RID_VOID
21345 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21346 == CPP_CLOSE_PAREN
))
21348 /* Consume the `void' token. */
21349 cp_lexer_consume_token (parser
->lexer
);
21350 /* There are no parameters. */
21351 return void_list_node
;
21354 /* Parse the parameter-declaration-list. */
21355 parameters
= cp_parser_parameter_declaration_list (parser
);
21356 /* If a parse error occurred while parsing the
21357 parameter-declaration-list, then the entire
21358 parameter-declaration-clause is erroneous. */
21359 if (parameters
== error_mark_node
)
21362 /* Peek at the next token. */
21363 token
= cp_lexer_peek_token (parser
->lexer
);
21364 /* If it's a `,', the clause should terminate with an ellipsis. */
21365 if (token
->type
== CPP_COMMA
)
21367 /* Consume the `,'. */
21368 cp_lexer_consume_token (parser
->lexer
);
21369 /* Expect an ellipsis. */
21371 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21373 /* It might also be `...' if the optional trailing `,' was
21375 else if (token
->type
== CPP_ELLIPSIS
)
21377 /* Consume the `...' token. */
21378 cp_lexer_consume_token (parser
->lexer
);
21379 /* And remember that we saw it. */
21383 ellipsis_p
= false;
21385 /* Finish the parameter list. */
21387 parameters
= chainon (parameters
, void_list_node
);
21392 /* Parse a parameter-declaration-list.
21394 parameter-declaration-list:
21395 parameter-declaration
21396 parameter-declaration-list , parameter-declaration
21398 Returns a representation of the parameter-declaration-list, as for
21399 cp_parser_parameter_declaration_clause. However, the
21400 `void_list_node' is never appended to the list. */
21403 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21405 tree parameters
= NULL_TREE
;
21406 tree
*tail
= ¶meters
;
21407 bool saved_in_unbraced_linkage_specification_p
;
21410 /* The special considerations that apply to a function within an
21411 unbraced linkage specifications do not apply to the parameters
21412 to the function. */
21413 saved_in_unbraced_linkage_specification_p
21414 = parser
->in_unbraced_linkage_specification_p
;
21415 parser
->in_unbraced_linkage_specification_p
= false;
21417 /* Look for more parameters. */
21420 cp_parameter_declarator
*parameter
;
21421 tree decl
= error_mark_node
;
21422 bool parenthesized_p
= false;
21424 /* Parse the parameter. */
21426 = cp_parser_parameter_declaration (parser
,
21427 /*template_parm_p=*/false,
21430 /* We don't know yet if the enclosing context is deprecated, so wait
21431 and warn in grokparms if appropriate. */
21432 deprecated_state
= DEPRECATED_SUPPRESS
;
21436 decl
= grokdeclarator (parameter
->declarator
,
21437 ¶meter
->decl_specifiers
,
21439 parameter
->default_argument
!= NULL_TREE
,
21440 ¶meter
->decl_specifiers
.attributes
);
21441 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21442 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21445 deprecated_state
= DEPRECATED_NORMAL
;
21447 /* If a parse error occurred parsing the parameter declaration,
21448 then the entire parameter-declaration-list is erroneous. */
21449 if (decl
== error_mark_node
)
21451 parameters
= error_mark_node
;
21455 if (parameter
->decl_specifiers
.attributes
)
21456 cplus_decl_attributes (&decl
,
21457 parameter
->decl_specifiers
.attributes
,
21459 if (DECL_NAME (decl
))
21460 decl
= pushdecl (decl
);
21462 if (decl
!= error_mark_node
)
21464 retrofit_lang_decl (decl
);
21465 DECL_PARM_INDEX (decl
) = ++index
;
21466 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21469 /* Add the new parameter to the list. */
21470 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21471 tail
= &TREE_CHAIN (*tail
);
21473 /* Peek at the next token. */
21474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21475 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21476 /* These are for Objective-C++ */
21477 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21478 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21479 /* The parameter-declaration-list is complete. */
21481 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21485 /* Peek at the next token. */
21486 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21487 /* If it's an ellipsis, then the list is complete. */
21488 if (token
->type
== CPP_ELLIPSIS
)
21490 /* Otherwise, there must be more parameters. Consume the
21492 cp_lexer_consume_token (parser
->lexer
);
21493 /* When parsing something like:
21495 int i(float f, double d)
21497 we can tell after seeing the declaration for "f" that we
21498 are not looking at an initialization of a variable "i",
21499 but rather at the declaration of a function "i".
21501 Due to the fact that the parsing of template arguments
21502 (as specified to a template-id) requires backtracking we
21503 cannot use this technique when inside a template argument
21505 if (!parser
->in_template_argument_list_p
21506 && !parser
->in_type_id_in_expr_p
21507 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21508 /* However, a parameter-declaration of the form
21509 "float(f)" (which is a valid declaration of a
21510 parameter "f") can also be interpreted as an
21511 expression (the conversion of "f" to "float"). */
21512 && !parenthesized_p
)
21513 cp_parser_commit_to_tentative_parse (parser
);
21517 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21518 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21519 cp_parser_skip_to_closing_parenthesis (parser
,
21520 /*recovering=*/true,
21521 /*or_comma=*/false,
21522 /*consume_paren=*/false);
21527 parser
->in_unbraced_linkage_specification_p
21528 = saved_in_unbraced_linkage_specification_p
;
21530 /* Reset implicit_template_scope if we are about to leave the function
21531 parameter list that introduced it. Note that for out-of-line member
21532 definitions, there will be one or more class scopes before we get to
21533 the template parameter scope. */
21535 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21536 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21538 while (maybe_its
->kind
== sk_class
)
21539 maybe_its
= maybe_its
->level_chain
;
21540 if (maybe_its
== its
)
21542 parser
->implicit_template_parms
= 0;
21543 parser
->implicit_template_scope
= 0;
21550 /* Parse a parameter declaration.
21552 parameter-declaration:
21553 decl-specifier-seq ... [opt] declarator
21554 decl-specifier-seq declarator = assignment-expression
21555 decl-specifier-seq ... [opt] abstract-declarator [opt]
21556 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21558 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21559 declares a template parameter. (In that case, a non-nested `>'
21560 token encountered during the parsing of the assignment-expression
21561 is not interpreted as a greater-than operator.)
21563 Returns a representation of the parameter, or NULL if an error
21564 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21565 true iff the declarator is of the form "(p)". */
21567 static cp_parameter_declarator
*
21568 cp_parser_parameter_declaration (cp_parser
*parser
,
21569 bool template_parm_p
,
21570 bool *parenthesized_p
)
21572 int declares_class_or_enum
;
21573 cp_decl_specifier_seq decl_specifiers
;
21574 cp_declarator
*declarator
;
21575 tree default_argument
;
21576 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21577 const char *saved_message
;
21578 bool template_parameter_pack_p
= false;
21580 /* In a template parameter, `>' is not an operator.
21584 When parsing a default template-argument for a non-type
21585 template-parameter, the first non-nested `>' is taken as the end
21586 of the template parameter-list rather than a greater-than
21589 /* Type definitions may not appear in parameter types. */
21590 saved_message
= parser
->type_definition_forbidden_message
;
21591 parser
->type_definition_forbidden_message
21592 = G_("types may not be defined in parameter types");
21594 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21595 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21596 (current_template_parms
)) : 0);
21598 /* Parse the declaration-specifiers. */
21599 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21600 cp_parser_decl_specifier_seq (parser
,
21601 CP_PARSER_FLAGS_NONE
,
21603 &declares_class_or_enum
);
21605 /* Complain about missing 'typename' or other invalid type names. */
21606 if (!decl_specifiers
.any_type_specifiers_p
21607 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21608 decl_specifiers
.type
= error_mark_node
;
21610 /* If an error occurred, there's no reason to attempt to parse the
21611 rest of the declaration. */
21612 if (cp_parser_error_occurred (parser
))
21614 parser
->type_definition_forbidden_message
= saved_message
;
21618 /* Peek at the next token. */
21619 token
= cp_lexer_peek_token (parser
->lexer
);
21621 /* If the next token is a `)', `,', `=', `>', or `...', then there
21622 is no declarator. However, when variadic templates are enabled,
21623 there may be a declarator following `...'. */
21624 if (token
->type
== CPP_CLOSE_PAREN
21625 || token
->type
== CPP_COMMA
21626 || token
->type
== CPP_EQ
21627 || token
->type
== CPP_GREATER
)
21630 if (parenthesized_p
)
21631 *parenthesized_p
= false;
21633 /* Otherwise, there should be a declarator. */
21636 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21637 parser
->default_arg_ok_p
= false;
21639 /* After seeing a decl-specifier-seq, if the next token is not a
21640 "(", there is no possibility that the code is a valid
21641 expression. Therefore, if parsing tentatively, we commit at
21643 if (!parser
->in_template_argument_list_p
21644 /* In an expression context, having seen:
21648 we cannot be sure whether we are looking at a
21649 function-type (taking a "char" as a parameter) or a cast
21650 of some object of type "char" to "int". */
21651 && !parser
->in_type_id_in_expr_p
21652 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21653 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21654 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21655 cp_parser_commit_to_tentative_parse (parser
);
21656 /* Parse the declarator. */
21657 declarator_token_start
= token
;
21658 declarator
= cp_parser_declarator (parser
,
21659 CP_PARSER_DECLARATOR_EITHER
,
21660 /*ctor_dtor_or_conv_p=*/NULL
,
21662 /*member_p=*/false,
21663 /*friend_p=*/false);
21664 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21665 /* After the declarator, allow more attributes. */
21666 decl_specifiers
.attributes
21667 = attr_chainon (decl_specifiers
.attributes
,
21668 cp_parser_attributes_opt (parser
));
21670 /* If the declarator is a template parameter pack, remember that and
21671 clear the flag in the declarator itself so we don't get errors
21672 from grokdeclarator. */
21673 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21675 declarator
->parameter_pack_p
= false;
21676 template_parameter_pack_p
= true;
21680 /* If the next token is an ellipsis, and we have not seen a declarator
21681 name, and if either the type of the declarator contains parameter
21682 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21683 for, eg, abbreviated integral type names), then we actually have a
21684 parameter pack expansion expression. Otherwise, leave the ellipsis
21685 for a C-style variadic function. */
21686 token
= cp_lexer_peek_token (parser
->lexer
);
21688 /* If a function parameter pack was specified and an implicit template
21689 parameter was introduced during cp_parser_parameter_declaration,
21690 change any implicit parameters introduced into packs. */
21691 if (parser
->implicit_template_parms
21692 && (token
->type
== CPP_ELLIPSIS
21693 || (declarator
&& declarator
->parameter_pack_p
)))
21695 int latest_template_parm_idx
= TREE_VEC_LENGTH
21696 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21698 if (latest_template_parm_idx
!= template_parm_idx
)
21699 decl_specifiers
.type
= convert_generic_types_to_packs
21700 (decl_specifiers
.type
,
21701 template_parm_idx
, latest_template_parm_idx
);
21704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21706 tree type
= decl_specifiers
.type
;
21708 if (type
&& DECL_P (type
))
21709 type
= TREE_TYPE (type
);
21712 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21713 && (template_parm_p
|| uses_parameter_packs (type
)))
21714 || (!type
&& template_parm_p
))
21715 && declarator_can_be_parameter_pack (declarator
))
21717 /* Consume the `...'. */
21718 cp_lexer_consume_token (parser
->lexer
);
21719 maybe_warn_variadic_templates ();
21721 /* Build a pack expansion type */
21722 if (template_parm_p
)
21723 template_parameter_pack_p
= true;
21724 else if (declarator
)
21725 declarator
->parameter_pack_p
= true;
21727 decl_specifiers
.type
= make_pack_expansion (type
);
21731 /* The restriction on defining new types applies only to the type
21732 of the parameter, not to the default argument. */
21733 parser
->type_definition_forbidden_message
= saved_message
;
21735 /* If the next token is `=', then process a default argument. */
21736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21738 tree type
= decl_specifiers
.type
;
21739 token
= cp_lexer_peek_token (parser
->lexer
);
21740 /* If we are defining a class, then the tokens that make up the
21741 default argument must be saved and processed later. */
21742 if (!template_parm_p
&& at_class_scope_p ()
21743 && TYPE_BEING_DEFINED (current_class_type
)
21744 && !LAMBDA_TYPE_P (current_class_type
))
21745 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21747 // A constrained-type-specifier may declare a type template-parameter.
21748 else if (declares_constrained_type_template_parameter (type
))
21750 = cp_parser_default_type_template_argument (parser
);
21752 // A constrained-type-specifier may declare a template-template-parameter.
21753 else if (declares_constrained_template_template_parameter (type
))
21755 = cp_parser_default_template_template_argument (parser
);
21757 /* Outside of a class definition, we can just parse the
21758 assignment-expression. */
21761 = cp_parser_default_argument (parser
, template_parm_p
);
21763 if (!parser
->default_arg_ok_p
)
21765 permerror (token
->location
,
21766 "default arguments are only "
21767 "permitted for function parameters");
21769 else if ((declarator
&& declarator
->parameter_pack_p
)
21770 || template_parameter_pack_p
21771 || (decl_specifiers
.type
21772 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21774 /* Find the name of the parameter pack. */
21775 cp_declarator
*id_declarator
= declarator
;
21776 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21777 id_declarator
= id_declarator
->declarator
;
21779 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21780 error_at (declarator_token_start
->location
,
21782 ? G_("template parameter pack %qD "
21783 "cannot have a default argument")
21784 : G_("parameter pack %qD cannot have "
21785 "a default argument"),
21786 id_declarator
->u
.id
.unqualified_name
);
21788 error_at (declarator_token_start
->location
,
21790 ? G_("template parameter pack cannot have "
21791 "a default argument")
21792 : G_("parameter pack cannot have a "
21793 "default argument"));
21795 default_argument
= NULL_TREE
;
21799 default_argument
= NULL_TREE
;
21801 /* Generate a location for the parameter, ranging from the start of the
21802 initial token to the end of the final token (using input_location for
21803 the latter, set up by cp_lexer_set_source_position_from_token when
21806 If we have a identifier, then use it for the caret location, e.g.
21808 extern int callee (int one, int (*two)(int, int), float three);
21809 ~~~~~~^~~~~~~~~~~~~~
21811 otherwise, reuse the start location for the caret location e.g.:
21813 extern int callee (int one, int (*)(int, int), float three);
21817 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21818 ? declarator
->id_loc
21819 : decl_spec_token_start
->location
);
21820 location_t param_loc
= make_location (caret_loc
,
21821 decl_spec_token_start
->location
,
21824 return make_parameter_declarator (&decl_specifiers
,
21828 template_parameter_pack_p
);
21831 /* Parse a default argument and return it.
21833 TEMPLATE_PARM_P is true if this is a default argument for a
21834 non-type template parameter. */
21836 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21838 tree default_argument
= NULL_TREE
;
21839 bool saved_greater_than_is_operator_p
;
21840 bool saved_local_variables_forbidden_p
;
21841 bool non_constant_p
, is_direct_init
;
21843 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21845 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21846 parser
->greater_than_is_operator_p
= !template_parm_p
;
21847 /* Local variable names (and the `this' keyword) may not
21848 appear in a default argument. */
21849 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21850 parser
->local_variables_forbidden_p
= true;
21851 /* Parse the assignment-expression. */
21852 if (template_parm_p
)
21853 push_deferring_access_checks (dk_no_deferred
);
21854 tree saved_class_ptr
= NULL_TREE
;
21855 tree saved_class_ref
= NULL_TREE
;
21856 /* The "this" pointer is not valid in a default argument. */
21859 saved_class_ptr
= current_class_ptr
;
21860 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21861 saved_class_ref
= current_class_ref
;
21862 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21865 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21866 /* Restore the "this" pointer. */
21869 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21870 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21872 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21873 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21874 if (template_parm_p
)
21875 pop_deferring_access_checks ();
21876 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21877 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21879 return default_argument
;
21882 /* Parse a function-body.
21885 compound_statement */
21888 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21890 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21891 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21895 /* Parse a ctor-initializer-opt followed by a function-body. Return
21896 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21897 is true we are parsing a function-try-block. */
21900 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21901 bool in_function_try_block
)
21904 const bool check_body_p
=
21905 DECL_CONSTRUCTOR_P (current_function_decl
)
21906 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21909 /* Begin the function body. */
21910 body
= begin_function_body ();
21911 /* Parse the optional ctor-initializer. */
21912 cp_parser_ctor_initializer_opt (parser
);
21914 /* If we're parsing a constexpr constructor definition, we need
21915 to check that the constructor body is indeed empty. However,
21916 before we get to cp_parser_function_body lot of junk has been
21917 generated, so we can't just check that we have an empty block.
21918 Rather we take a snapshot of the outermost block, and check whether
21919 cp_parser_function_body changed its state. */
21922 list
= cur_stmt_list
;
21923 if (STATEMENT_LIST_TAIL (list
))
21924 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21926 /* Parse the function-body. */
21927 cp_parser_function_body (parser
, in_function_try_block
);
21929 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21930 /* Finish the function body. */
21931 finish_function_body (body
);
21934 /* Parse an initializer.
21937 = initializer-clause
21938 ( expression-list )
21940 Returns an expression representing the initializer. If no
21941 initializer is present, NULL_TREE is returned.
21943 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21944 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21945 set to TRUE if there is no initializer present. If there is an
21946 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21947 is set to true; otherwise it is set to false. */
21950 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21951 bool* non_constant_p
, bool subexpression_p
)
21956 /* Peek at the next token. */
21957 token
= cp_lexer_peek_token (parser
->lexer
);
21959 /* Let our caller know whether or not this initializer was
21961 *is_direct_init
= (token
->type
!= CPP_EQ
);
21962 /* Assume that the initializer is constant. */
21963 *non_constant_p
= false;
21965 if (token
->type
== CPP_EQ
)
21967 /* Consume the `='. */
21968 cp_lexer_consume_token (parser
->lexer
);
21969 /* Parse the initializer-clause. */
21970 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21972 else if (token
->type
== CPP_OPEN_PAREN
)
21974 vec
<tree
, va_gc
> *vec
;
21975 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21977 /*allow_expansion_p=*/true,
21980 return error_mark_node
;
21981 init
= build_tree_list_vec (vec
);
21982 release_tree_vector (vec
);
21984 else if (token
->type
== CPP_OPEN_BRACE
)
21986 cp_lexer_set_source_position (parser
->lexer
);
21987 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21988 init
= cp_parser_braced_list (parser
, non_constant_p
);
21989 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21993 /* Anything else is an error. */
21994 cp_parser_error (parser
, "expected initializer");
21995 init
= error_mark_node
;
21998 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
21999 init
= error_mark_node
;
22004 /* Parse an initializer-clause.
22006 initializer-clause:
22007 assignment-expression
22010 Returns an expression representing the initializer.
22012 If the `assignment-expression' production is used the value
22013 returned is simply a representation for the expression.
22015 Otherwise, calls cp_parser_braced_list. */
22018 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22020 cp_expr initializer
;
22022 /* Assume the expression is constant. */
22023 *non_constant_p
= false;
22025 /* If it is not a `{', then we are looking at an
22026 assignment-expression. */
22027 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22030 = cp_parser_constant_expression (parser
,
22031 /*allow_non_constant_p=*/true,
22035 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22037 return initializer
;
22040 /* Parse a brace-enclosed initializer list.
22043 { initializer-list , [opt] }
22044 { designated-initializer-list , [opt] }
22047 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22048 the elements of the initializer-list (or NULL, if the last
22049 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22050 NULL_TREE. There is no way to detect whether or not the optional
22051 trailing `,' was provided. NON_CONSTANT_P is as for
22052 cp_parser_initializer. */
22055 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22058 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22060 /* Consume the `{' token. */
22061 matching_braces braces
;
22062 braces
.require_open (parser
);
22063 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22064 initializer
= make_node (CONSTRUCTOR
);
22065 /* If it's not a `}', then there is a non-trivial initializer. */
22066 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22068 /* Parse the initializer list. */
22069 CONSTRUCTOR_ELTS (initializer
)
22070 = cp_parser_initializer_list (parser
, non_constant_p
);
22071 /* A trailing `,' token is allowed. */
22072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22073 cp_lexer_consume_token (parser
->lexer
);
22076 *non_constant_p
= false;
22077 /* Now, there should be a trailing `}'. */
22078 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22079 braces
.require_close (parser
);
22080 TREE_TYPE (initializer
) = init_list_type_node
;
22082 cp_expr
result (initializer
);
22083 /* Build a location of the form:
22086 with caret==start at the open brace, finish at the close brace. */
22087 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22088 result
.set_location (combined_loc
);
22092 /* Consume tokens up to, and including, the next non-nested closing `]'.
22093 Returns true iff we found a closing `]'. */
22096 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22098 unsigned square_depth
= 0;
22102 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22104 switch (token
->type
)
22107 case CPP_PRAGMA_EOL
:
22108 /* If we've run out of tokens, then there is no closing `]'. */
22111 case CPP_OPEN_SQUARE
:
22115 case CPP_CLOSE_SQUARE
:
22116 if (!square_depth
--)
22118 cp_lexer_consume_token (parser
->lexer
);
22127 /* Consume the token. */
22128 cp_lexer_consume_token (parser
->lexer
);
22132 /* Return true if we are looking at an array-designator, false otherwise. */
22135 cp_parser_array_designator_p (cp_parser
*parser
)
22137 /* Consume the `['. */
22138 cp_lexer_consume_token (parser
->lexer
);
22140 cp_lexer_save_tokens (parser
->lexer
);
22142 /* Skip tokens until the next token is a closing square bracket.
22143 If we find the closing `]', and the next token is a `=', then
22144 we are looking at an array designator. */
22145 bool array_designator_p
22146 = (cp_parser_skip_to_closing_square_bracket (parser
)
22147 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22149 /* Roll back the tokens we skipped. */
22150 cp_lexer_rollback_tokens (parser
->lexer
);
22152 return array_designator_p
;
22155 /* Parse an initializer-list.
22158 initializer-clause ... [opt]
22159 initializer-list , initializer-clause ... [opt]
22163 designated-initializer-list:
22164 designated-initializer-clause
22165 designated-initializer-list , designated-initializer-clause
22167 designated-initializer-clause:
22168 designator brace-or-equal-initializer
22176 designation initializer-clause ...[opt]
22177 initializer-list , designation initializer-clause ...[opt]
22182 [ constant-expression ] =
22184 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22185 for the initializer. If the INDEX of the elt is non-NULL, it is the
22186 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22187 as for cp_parser_initializer. */
22189 static vec
<constructor_elt
, va_gc
> *
22190 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22192 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22193 bool first_p
= true;
22194 tree first_designator
= NULL_TREE
;
22196 /* Assume all of the expressions are constant. */
22197 *non_constant_p
= false;
22199 /* Parse the rest of the list. */
22205 bool clause_non_constant_p
;
22206 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22208 /* Handle the C++2A syntax, '. id ='. */
22209 if ((cxx_dialect
>= cxx2a
22210 || cp_parser_allow_gnu_extensions_p (parser
))
22211 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22212 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22213 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22214 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22215 == CPP_OPEN_BRACE
)))
22217 if (cxx_dialect
< cxx2a
)
22218 pedwarn (loc
, OPT_Wpedantic
,
22219 "C++ designated initializers only available with "
22220 "-std=c++2a or -std=gnu++2a");
22221 /* Consume the `.'. */
22222 cp_lexer_consume_token (parser
->lexer
);
22223 /* Consume the identifier. */
22224 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22226 /* Consume the `='. */
22227 cp_lexer_consume_token (parser
->lexer
);
22229 /* Also, if the next token is an identifier and the following one is a
22230 colon, we are looking at the GNU designated-initializer
22232 else if (cp_parser_allow_gnu_extensions_p (parser
)
22233 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22234 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22237 /* Warn the user that they are using an extension. */
22238 pedwarn (loc
, OPT_Wpedantic
,
22239 "ISO C++ does not allow GNU designated initializers");
22240 /* Consume the identifier. */
22241 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22242 /* Consume the `:'. */
22243 cp_lexer_consume_token (parser
->lexer
);
22245 /* Also handle C99 array designators, '[ const ] ='. */
22246 else if (cp_parser_allow_gnu_extensions_p (parser
)
22247 && !c_dialect_objc ()
22248 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22250 /* In C++11, [ could start a lambda-introducer. */
22251 bool non_const
= false;
22253 cp_parser_parse_tentatively (parser
);
22255 if (!cp_parser_array_designator_p (parser
))
22257 cp_parser_simulate_error (parser
);
22258 designator
= NULL_TREE
;
22262 designator
= cp_parser_constant_expression (parser
, true,
22264 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22265 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22268 if (!cp_parser_parse_definitely (parser
))
22269 designator
= NULL_TREE
;
22271 && (!require_potential_rvalue_constant_expression
22273 designator
= NULL_TREE
;
22275 /* Warn the user that they are using an extension. */
22276 pedwarn (loc
, OPT_Wpedantic
,
22277 "ISO C++ does not allow C99 designated initializers");
22280 designator
= NULL_TREE
;
22284 first_designator
= designator
;
22287 else if (cxx_dialect
>= cxx2a
22288 && first_designator
!= error_mark_node
22289 && (!first_designator
!= !designator
))
22291 error_at (loc
, "either all initializer clauses should be designated "
22292 "or none of them should be");
22293 first_designator
= error_mark_node
;
22295 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22296 first_designator
= designator
;
22298 /* Parse the initializer. */
22299 initializer
= cp_parser_initializer_clause (parser
,
22300 &clause_non_constant_p
);
22301 /* If any clause is non-constant, so is the entire initializer. */
22302 if (clause_non_constant_p
)
22303 *non_constant_p
= true;
22305 /* If we have an ellipsis, this is an initializer pack
22307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22309 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22311 /* Consume the `...'. */
22312 cp_lexer_consume_token (parser
->lexer
);
22314 if (designator
&& cxx_dialect
>= cxx2a
)
22316 "%<...%> not allowed in designated initializer list");
22318 /* Turn the initializer into an initializer expansion. */
22319 initializer
= make_pack_expansion (initializer
);
22322 /* Add it to the vector. */
22323 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22325 /* If the next token is not a comma, we have reached the end of
22327 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22330 /* Peek at the next token. */
22331 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22332 /* If the next token is a `}', then we're still done. An
22333 initializer-clause can have a trailing `,' after the
22334 initializer-list and before the closing `}'. */
22335 if (token
->type
== CPP_CLOSE_BRACE
)
22338 /* Consume the `,' token. */
22339 cp_lexer_consume_token (parser
->lexer
);
22342 /* The same identifier shall not appear in multiple designators
22343 of a designated-initializer-list. */
22344 if (first_designator
)
22347 tree designator
, val
;
22348 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22349 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22351 if (IDENTIFIER_MARKED (designator
))
22353 error_at (cp_expr_loc_or_loc (val
, input_location
),
22354 "%<.%s%> designator used multiple times in "
22355 "the same initializer list",
22356 IDENTIFIER_POINTER (designator
));
22357 (*v
)[i
].index
= NULL_TREE
;
22360 IDENTIFIER_MARKED (designator
) = 1;
22362 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22363 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22364 IDENTIFIER_MARKED (designator
) = 0;
22370 /* Classes [gram.class] */
22372 /* Parse a class-name.
22378 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22379 to indicate that names looked up in dependent types should be
22380 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22381 keyword has been used to indicate that the name that appears next
22382 is a template. TAG_TYPE indicates the explicit tag given before
22383 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22384 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22385 is the class being defined in a class-head. If ENUM_OK is TRUE,
22386 enum-names are also accepted.
22388 Returns the TYPE_DECL representing the class. */
22391 cp_parser_class_name (cp_parser
*parser
,
22392 bool typename_keyword_p
,
22393 bool template_keyword_p
,
22394 enum tag_types tag_type
,
22395 bool check_dependency_p
,
22397 bool is_declaration
,
22404 tree identifier
= NULL_TREE
;
22406 /* All class-names start with an identifier. */
22407 token
= cp_lexer_peek_token (parser
->lexer
);
22408 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22410 cp_parser_error (parser
, "expected class-name");
22411 return error_mark_node
;
22414 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22415 to a template-id, so we save it here. */
22416 scope
= parser
->scope
;
22417 if (scope
== error_mark_node
)
22418 return error_mark_node
;
22420 /* Any name names a type if we're following the `typename' keyword
22421 in a qualified name where the enclosing scope is type-dependent. */
22422 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22423 && dependent_type_p (scope
));
22424 /* Handle the common case (an identifier, but not a template-id)
22426 if (token
->type
== CPP_NAME
22427 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22429 cp_token
*identifier_token
;
22432 /* Look for the identifier. */
22433 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22434 ambiguous_p
= identifier_token
->error_reported
;
22435 identifier
= cp_parser_identifier (parser
);
22436 /* If the next token isn't an identifier, we are certainly not
22437 looking at a class-name. */
22438 if (identifier
== error_mark_node
)
22439 decl
= error_mark_node
;
22440 /* If we know this is a type-name, there's no need to look it
22442 else if (typename_p
)
22446 tree ambiguous_decls
;
22447 /* If we already know that this lookup is ambiguous, then
22448 we've already issued an error message; there's no reason
22452 cp_parser_simulate_error (parser
);
22453 return error_mark_node
;
22455 /* If the next token is a `::', then the name must be a type
22458 [basic.lookup.qual]
22460 During the lookup for a name preceding the :: scope
22461 resolution operator, object, function, and enumerator
22462 names are ignored. */
22463 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22464 tag_type
= scope_type
;
22465 /* Look up the name. */
22466 decl
= cp_parser_lookup_name (parser
, identifier
,
22468 /*is_template=*/false,
22469 /*is_namespace=*/false,
22470 check_dependency_p
,
22472 identifier_token
->location
);
22473 if (ambiguous_decls
)
22475 if (cp_parser_parsing_tentatively (parser
))
22476 cp_parser_simulate_error (parser
);
22477 return error_mark_node
;
22483 /* Try a template-id. */
22484 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22485 check_dependency_p
,
22488 if (decl
== error_mark_node
)
22489 return error_mark_node
;
22492 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22494 /* If this is a typename, create a TYPENAME_TYPE. */
22495 if (typename_p
&& decl
!= error_mark_node
)
22497 decl
= make_typename_type (scope
, decl
, typename_type
,
22498 /*complain=*/tf_error
);
22499 if (decl
!= error_mark_node
)
22500 decl
= TYPE_NAME (decl
);
22503 decl
= strip_using_decl (decl
);
22505 /* Check to see that it is really the name of a class. */
22506 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22507 && identifier_p (TREE_OPERAND (decl
, 0))
22508 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22509 /* Situations like this:
22511 template <typename T> struct A {
22512 typename T::template X<int>::I i;
22515 are problematic. Is `T::template X<int>' a class-name? The
22516 standard does not seem to be definitive, but there is no other
22517 valid interpretation of the following `::'. Therefore, those
22518 names are considered class-names. */
22520 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22521 if (decl
!= error_mark_node
)
22522 decl
= TYPE_NAME (decl
);
22524 else if (TREE_CODE (decl
) != TYPE_DECL
22525 || TREE_TYPE (decl
) == error_mark_node
22526 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22527 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22528 /* In Objective-C 2.0, a classname followed by '.' starts a
22529 dot-syntax expression, and it's not a type-name. */
22530 || (c_dialect_objc ()
22531 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22532 && objc_is_class_name (decl
)))
22533 decl
= error_mark_node
;
22535 if (decl
== error_mark_node
)
22536 cp_parser_error (parser
, "expected class-name");
22537 else if (identifier
&& !parser
->scope
)
22538 maybe_note_name_used_in_class (identifier
, decl
);
22543 /* Parse a class-specifier.
22546 class-head { member-specification [opt] }
22548 Returns the TREE_TYPE representing the class. */
22551 cp_parser_class_specifier_1 (cp_parser
* parser
)
22554 tree attributes
= NULL_TREE
;
22555 bool nested_name_specifier_p
;
22556 unsigned saved_num_template_parameter_lists
;
22557 bool saved_in_function_body
;
22558 unsigned char in_statement
;
22559 bool in_switch_statement_p
;
22560 bool saved_in_unbraced_linkage_specification_p
;
22561 tree old_scope
= NULL_TREE
;
22562 tree scope
= NULL_TREE
;
22563 cp_token
*closing_brace
;
22565 push_deferring_access_checks (dk_no_deferred
);
22567 /* Parse the class-head. */
22568 type
= cp_parser_class_head (parser
,
22569 &nested_name_specifier_p
);
22570 /* If the class-head was a semantic disaster, skip the entire body
22574 cp_parser_skip_to_end_of_block_or_statement (parser
);
22575 pop_deferring_access_checks ();
22576 return error_mark_node
;
22579 /* Look for the `{'. */
22580 matching_braces braces
;
22581 if (!braces
.require_open (parser
))
22583 pop_deferring_access_checks ();
22584 return error_mark_node
;
22587 cp_ensure_no_omp_declare_simd (parser
);
22588 cp_ensure_no_oacc_routine (parser
);
22590 /* Issue an error message if type-definitions are forbidden here. */
22591 cp_parser_check_type_definition (parser
);
22592 /* Remember that we are defining one more class. */
22593 ++parser
->num_classes_being_defined
;
22594 /* Inside the class, surrounding template-parameter-lists do not
22596 saved_num_template_parameter_lists
22597 = parser
->num_template_parameter_lists
;
22598 parser
->num_template_parameter_lists
= 0;
22599 /* We are not in a function body. */
22600 saved_in_function_body
= parser
->in_function_body
;
22601 parser
->in_function_body
= false;
22602 /* Or in a loop. */
22603 in_statement
= parser
->in_statement
;
22604 parser
->in_statement
= 0;
22605 /* Or in a switch. */
22606 in_switch_statement_p
= parser
->in_switch_statement_p
;
22607 parser
->in_switch_statement_p
= false;
22608 /* We are not immediately inside an extern "lang" block. */
22609 saved_in_unbraced_linkage_specification_p
22610 = parser
->in_unbraced_linkage_specification_p
;
22611 parser
->in_unbraced_linkage_specification_p
= false;
22613 // Associate constraints with the type.
22615 type
= associate_classtype_constraints (type
);
22617 /* Start the class. */
22618 if (nested_name_specifier_p
)
22620 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22621 old_scope
= push_inner_scope (scope
);
22623 type
= begin_class_definition (type
);
22625 if (type
== error_mark_node
)
22626 /* If the type is erroneous, skip the entire body of the class. */
22627 cp_parser_skip_to_closing_brace (parser
);
22629 /* Parse the member-specification. */
22630 cp_parser_member_specification_opt (parser
);
22632 /* Look for the trailing `}'. */
22633 closing_brace
= braces
.require_close (parser
);
22634 /* Look for trailing attributes to apply to this class. */
22635 if (cp_parser_allow_gnu_extensions_p (parser
))
22636 attributes
= cp_parser_gnu_attributes_opt (parser
);
22637 if (type
!= error_mark_node
)
22638 type
= finish_struct (type
, attributes
);
22639 if (nested_name_specifier_p
)
22640 pop_inner_scope (old_scope
, scope
);
22642 /* We've finished a type definition. Check for the common syntax
22643 error of forgetting a semicolon after the definition. We need to
22644 be careful, as we can't just check for not-a-semicolon and be done
22645 with it; the user might have typed:
22647 class X { } c = ...;
22648 class X { } *p = ...;
22650 and so forth. Instead, enumerate all the possible tokens that
22651 might follow this production; if we don't see one of them, then
22652 complain and silently insert the semicolon. */
22654 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22655 bool want_semicolon
= true;
22657 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22658 /* Don't try to parse c++11 attributes here. As per the
22659 grammar, that should be a task for
22660 cp_parser_decl_specifier_seq. */
22661 want_semicolon
= false;
22663 switch (token
->type
)
22666 case CPP_SEMICOLON
:
22669 case CPP_OPEN_PAREN
:
22670 case CPP_CLOSE_PAREN
:
22672 want_semicolon
= false;
22675 /* While it's legal for type qualifiers and storage class
22676 specifiers to follow type definitions in the grammar, only
22677 compiler testsuites contain code like that. Assume that if
22678 we see such code, then what we're really seeing is a case
22682 const <type> var = ...;
22687 static <type> func (...) ...
22689 i.e. the qualifier or specifier applies to the next
22690 declaration. To do so, however, we need to look ahead one
22691 more token to see if *that* token is a type specifier.
22693 This code could be improved to handle:
22696 static const <type> var = ...; */
22698 if (keyword_is_decl_specifier (token
->keyword
))
22700 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22702 /* Handling user-defined types here would be nice, but very
22705 = (lookahead
->type
== CPP_KEYWORD
22706 && keyword_begins_type_specifier (lookahead
->keyword
));
22713 /* If we don't have a type, then something is very wrong and we
22714 shouldn't try to do anything clever. Likewise for not seeing the
22716 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22718 /* Locate the closing brace. */
22719 cp_token_position prev
22720 = cp_lexer_previous_token_position (parser
->lexer
);
22721 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22722 location_t loc
= prev_token
->location
;
22724 /* We want to suggest insertion of a ';' immediately *after* the
22725 closing brace, so, if we can, offset the location by 1 column. */
22726 location_t next_loc
= loc
;
22727 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22728 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22730 rich_location
richloc (line_table
, next_loc
);
22732 /* If we successfully offset the location, suggest the fix-it. */
22733 if (next_loc
!= loc
)
22734 richloc
.add_fixit_insert_before (next_loc
, ";");
22736 if (CLASSTYPE_DECLARED_CLASS (type
))
22737 error_at (&richloc
,
22738 "expected %<;%> after class definition");
22739 else if (TREE_CODE (type
) == RECORD_TYPE
)
22740 error_at (&richloc
,
22741 "expected %<;%> after struct definition");
22742 else if (TREE_CODE (type
) == UNION_TYPE
)
22743 error_at (&richloc
,
22744 "expected %<;%> after union definition");
22746 gcc_unreachable ();
22748 /* Unget one token and smash it to look as though we encountered
22749 a semicolon in the input stream. */
22750 cp_lexer_set_token_position (parser
->lexer
, prev
);
22751 token
= cp_lexer_peek_token (parser
->lexer
);
22752 token
->type
= CPP_SEMICOLON
;
22753 token
->keyword
= RID_MAX
;
22757 /* If this class is not itself within the scope of another class,
22758 then we need to parse the bodies of all of the queued function
22759 definitions. Note that the queued functions defined in a class
22760 are not always processed immediately following the
22761 class-specifier for that class. Consider:
22764 struct B { void f() { sizeof (A); } };
22767 If `f' were processed before the processing of `A' were
22768 completed, there would be no way to compute the size of `A'.
22769 Note that the nesting we are interested in here is lexical --
22770 not the semantic nesting given by TYPE_CONTEXT. In particular,
22773 struct A { struct B; };
22774 struct A::B { void f() { } };
22776 there is no need to delay the parsing of `A::B::f'. */
22777 if (--parser
->num_classes_being_defined
== 0)
22780 tree class_type
= NULL_TREE
;
22781 tree pushed_scope
= NULL_TREE
;
22783 cp_default_arg_entry
*e
;
22784 tree save_ccp
, save_ccr
;
22786 if (any_erroneous_template_args_p (type
))
22788 /* Skip default arguments, NSDMIs, etc, in order to improve
22789 error recovery (c++/71169, c++/71832). */
22790 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22791 vec_safe_truncate (unparsed_nsdmis
, 0);
22792 vec_safe_truncate (unparsed_classes
, 0);
22793 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22796 /* In a first pass, parse default arguments to the functions.
22797 Then, in a second pass, parse the bodies of the functions.
22798 This two-phased approach handles cases like:
22806 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22809 /* If there are default arguments that have not yet been processed,
22810 take care of them now. */
22811 if (class_type
!= e
->class_type
)
22814 pop_scope (pushed_scope
);
22815 class_type
= e
->class_type
;
22816 pushed_scope
= push_scope (class_type
);
22818 /* Make sure that any template parameters are in scope. */
22819 maybe_begin_member_template_processing (decl
);
22820 /* Parse the default argument expressions. */
22821 cp_parser_late_parsing_default_args (parser
, decl
);
22822 /* Remove any template parameters from the symbol table. */
22823 maybe_end_member_template_processing ();
22825 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22826 /* Now parse any NSDMIs. */
22827 save_ccp
= current_class_ptr
;
22828 save_ccr
= current_class_ref
;
22829 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22831 if (class_type
!= DECL_CONTEXT (decl
))
22834 pop_scope (pushed_scope
);
22835 class_type
= DECL_CONTEXT (decl
);
22836 pushed_scope
= push_scope (class_type
);
22838 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22839 cp_parser_late_parsing_nsdmi (parser
, decl
);
22841 vec_safe_truncate (unparsed_nsdmis
, 0);
22842 current_class_ptr
= save_ccp
;
22843 current_class_ref
= save_ccr
;
22845 pop_scope (pushed_scope
);
22847 /* Now do some post-NSDMI bookkeeping. */
22848 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22849 after_nsdmi_defaulted_late_checks (class_type
);
22850 vec_safe_truncate (unparsed_classes
, 0);
22851 after_nsdmi_defaulted_late_checks (type
);
22853 /* Now parse the body of the functions. */
22856 /* OpenMP UDRs need to be parsed before all other functions. */
22857 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22858 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22859 cp_parser_late_parsing_for_member (parser
, decl
);
22860 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22861 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22862 cp_parser_late_parsing_for_member (parser
, decl
);
22865 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22866 cp_parser_late_parsing_for_member (parser
, decl
);
22867 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22870 vec_safe_push (unparsed_classes
, type
);
22872 /* Put back any saved access checks. */
22873 pop_deferring_access_checks ();
22875 /* Restore saved state. */
22876 parser
->in_switch_statement_p
= in_switch_statement_p
;
22877 parser
->in_statement
= in_statement
;
22878 parser
->in_function_body
= saved_in_function_body
;
22879 parser
->num_template_parameter_lists
22880 = saved_num_template_parameter_lists
;
22881 parser
->in_unbraced_linkage_specification_p
22882 = saved_in_unbraced_linkage_specification_p
;
22888 cp_parser_class_specifier (cp_parser
* parser
)
22891 timevar_push (TV_PARSE_STRUCT
);
22892 ret
= cp_parser_class_specifier_1 (parser
);
22893 timevar_pop (TV_PARSE_STRUCT
);
22897 /* Parse a class-head.
22900 class-key identifier [opt] base-clause [opt]
22901 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22902 class-key nested-name-specifier [opt] template-id
22905 class-virt-specifier:
22909 class-key attributes identifier [opt] base-clause [opt]
22910 class-key attributes nested-name-specifier identifier base-clause [opt]
22911 class-key attributes nested-name-specifier [opt] template-id
22914 Upon return BASES is initialized to the list of base classes (or
22915 NULL, if there are none) in the same form returned by
22916 cp_parser_base_clause.
22918 Returns the TYPE of the indicated class. Sets
22919 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22920 involving a nested-name-specifier was used, and FALSE otherwise.
22922 Returns error_mark_node if this is not a class-head.
22924 Returns NULL_TREE if the class-head is syntactically valid, but
22925 semantically invalid in a way that means we should skip the entire
22926 body of the class. */
22929 cp_parser_class_head (cp_parser
* parser
,
22930 bool* nested_name_specifier_p
)
22932 tree nested_name_specifier
;
22933 enum tag_types class_key
;
22934 tree id
= NULL_TREE
;
22935 tree type
= NULL_TREE
;
22938 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22939 bool template_id_p
= false;
22940 bool qualified_p
= false;
22941 bool invalid_nested_name_p
= false;
22942 bool invalid_explicit_specialization_p
= false;
22943 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22944 tree pushed_scope
= NULL_TREE
;
22945 unsigned num_templates
;
22946 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22947 /* Assume no nested-name-specifier will be present. */
22948 *nested_name_specifier_p
= false;
22949 /* Assume no template parameter lists will be used in defining the
22952 parser
->colon_corrects_to_scope_p
= false;
22954 /* Look for the class-key. */
22955 class_key
= cp_parser_class_key (parser
);
22956 if (class_key
== none_type
)
22957 return error_mark_node
;
22959 location_t class_head_start_location
= input_location
;
22961 /* Parse the attributes. */
22962 attributes
= cp_parser_attributes_opt (parser
);
22964 /* If the next token is `::', that is invalid -- but sometimes
22965 people do try to write:
22969 Handle this gracefully by accepting the extra qualifier, and then
22970 issuing an error about it later if this really is a
22971 class-head. If it turns out just to be an elaborated type
22972 specifier, remain silent. */
22973 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22974 qualified_p
= true;
22976 push_deferring_access_checks (dk_no_check
);
22978 /* Determine the name of the class. Begin by looking for an
22979 optional nested-name-specifier. */
22980 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22981 nested_name_specifier
22982 = cp_parser_nested_name_specifier_opt (parser
,
22983 /*typename_keyword_p=*/false,
22984 /*check_dependency_p=*/false,
22986 /*is_declaration=*/false);
22987 /* If there was a nested-name-specifier, then there *must* be an
22990 cp_token
*bad_template_keyword
= NULL
;
22992 if (nested_name_specifier
)
22994 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22995 /* Although the grammar says `identifier', it really means
22996 `class-name' or `template-name'. You are only allowed to
22997 define a class that has already been declared with this
23000 The proposed resolution for Core Issue 180 says that wherever
23001 you see `class T::X' you should treat `X' as a type-name.
23003 It is OK to define an inaccessible class; for example:
23005 class A { class B; };
23008 We do not know if we will see a class-name, or a
23009 template-name. We look for a class-name first, in case the
23010 class-name is a template-id; if we looked for the
23011 template-name first we would stop after the template-name. */
23012 cp_parser_parse_tentatively (parser
);
23013 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23014 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23015 type
= cp_parser_class_name (parser
,
23016 /*typename_keyword_p=*/false,
23017 /*template_keyword_p=*/false,
23019 /*check_dependency_p=*/false,
23020 /*class_head_p=*/true,
23021 /*is_declaration=*/false);
23022 /* If that didn't work, ignore the nested-name-specifier. */
23023 if (!cp_parser_parse_definitely (parser
))
23025 invalid_nested_name_p
= true;
23026 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23027 id
= cp_parser_identifier (parser
);
23028 if (id
== error_mark_node
)
23031 /* If we could not find a corresponding TYPE, treat this
23032 declaration like an unqualified declaration. */
23033 if (type
== error_mark_node
)
23034 nested_name_specifier
= NULL_TREE
;
23035 /* Otherwise, count the number of templates used in TYPE and its
23036 containing scopes. */
23038 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23040 /* Otherwise, the identifier is optional. */
23043 /* We don't know whether what comes next is a template-id,
23044 an identifier, or nothing at all. */
23045 cp_parser_parse_tentatively (parser
);
23046 /* Check for a template-id. */
23047 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23048 id
= cp_parser_template_id (parser
,
23049 /*template_keyword_p=*/false,
23050 /*check_dependency_p=*/true,
23052 /*is_declaration=*/true);
23053 /* If that didn't work, it could still be an identifier. */
23054 if (!cp_parser_parse_definitely (parser
))
23056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23058 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23059 id
= cp_parser_identifier (parser
);
23066 template_id_p
= true;
23071 pop_deferring_access_checks ();
23075 cp_parser_check_for_invalid_template_id (parser
, id
,
23077 type_start_token
->location
);
23079 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23081 /* If it's not a `:' or a `{' then we can't really be looking at a
23082 class-head, since a class-head only appears as part of a
23083 class-specifier. We have to detect this situation before calling
23084 xref_tag, since that has irreversible side-effects. */
23085 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23087 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23088 type
= error_mark_node
;
23092 /* At this point, we're going ahead with the class-specifier, even
23093 if some other problem occurs. */
23094 cp_parser_commit_to_tentative_parse (parser
);
23095 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23097 cp_parser_error (parser
,
23098 "cannot specify %<override%> for a class");
23099 type
= error_mark_node
;
23102 /* Issue the error about the overly-qualified name now. */
23105 cp_parser_error (parser
,
23106 "global qualification of class name is invalid");
23107 type
= error_mark_node
;
23110 else if (invalid_nested_name_p
)
23112 cp_parser_error (parser
,
23113 "qualified name does not name a class");
23114 type
= error_mark_node
;
23117 else if (nested_name_specifier
)
23121 if (bad_template_keyword
)
23122 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23123 keyword template shall not appear at the top level. */
23124 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23125 "keyword %<template%> not allowed in class-head-name");
23127 /* Reject typedef-names in class heads. */
23128 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23130 error_at (type_start_token
->location
,
23131 "invalid class name in declaration of %qD",
23137 /* Figure out in what scope the declaration is being placed. */
23138 scope
= current_scope ();
23139 /* If that scope does not contain the scope in which the
23140 class was originally declared, the program is invalid. */
23141 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23143 if (at_namespace_scope_p ())
23144 error_at (type_start_token
->location
,
23145 "declaration of %qD in namespace %qD which does not "
23147 type
, scope
, nested_name_specifier
);
23149 error_at (type_start_token
->location
,
23150 "declaration of %qD in %qD which does not enclose %qD",
23151 type
, scope
, nested_name_specifier
);
23157 A declarator-id shall not be qualified except for the
23158 definition of a ... nested class outside of its class
23159 ... [or] the definition or explicit instantiation of a
23160 class member of a namespace outside of its namespace. */
23161 if (scope
== nested_name_specifier
)
23163 permerror (nested_name_specifier_token_start
->location
,
23164 "extra qualification not allowed");
23165 nested_name_specifier
= NULL_TREE
;
23169 /* An explicit-specialization must be preceded by "template <>". If
23170 it is not, try to recover gracefully. */
23171 if (at_namespace_scope_p ()
23172 && parser
->num_template_parameter_lists
== 0
23173 && !processing_template_parmlist
23176 /* Build a location of this form:
23177 struct typename <ARGS>
23178 ^~~~~~~~~~~~~~~~~~~~~~
23179 with caret==start at the start token, and
23180 finishing at the end of the type. */
23181 location_t reported_loc
23182 = make_location (class_head_start_location
,
23183 class_head_start_location
,
23184 get_finish (type_start_token
->location
));
23185 rich_location
richloc (line_table
, reported_loc
);
23186 richloc
.add_fixit_insert_before (class_head_start_location
,
23188 error_at (&richloc
,
23189 "an explicit specialization must be preceded by"
23190 " %<template <>%>");
23191 invalid_explicit_specialization_p
= true;
23192 /* Take the same action that would have been taken by
23193 cp_parser_explicit_specialization. */
23194 ++parser
->num_template_parameter_lists
;
23195 begin_specialization ();
23197 /* There must be no "return" statements between this point and the
23198 end of this function; set "type "to the correct return value and
23199 use "goto done;" to return. */
23200 /* Make sure that the right number of template parameters were
23202 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23204 type_start_token
->location
,
23205 /*declarator=*/NULL
))
23207 /* If something went wrong, there is no point in even trying to
23208 process the class-definition. */
23213 /* Look up the type. */
23216 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23217 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23218 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23220 error_at (type_start_token
->location
,
23221 "function template %qD redeclared as a class template", id
);
23222 type
= error_mark_node
;
23226 type
= TREE_TYPE (id
);
23227 type
= maybe_process_partial_specialization (type
);
23229 /* Check the scope while we still know whether or not we had a
23230 nested-name-specifier. */
23231 if (type
!= error_mark_node
)
23232 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23234 if (nested_name_specifier
)
23235 pushed_scope
= push_scope (nested_name_specifier
);
23237 else if (nested_name_specifier
)
23243 template <typename T> struct S { struct T };
23244 template <typename T> struct S<T>::T { };
23246 we will get a TYPENAME_TYPE when processing the definition of
23247 `S::T'. We need to resolve it to the actual type before we
23248 try to define it. */
23249 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23251 class_type
= resolve_typename_type (TREE_TYPE (type
),
23252 /*only_current_p=*/false);
23253 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23254 type
= TYPE_NAME (class_type
);
23257 cp_parser_error (parser
, "could not resolve typename type");
23258 type
= error_mark_node
;
23262 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23263 == error_mark_node
)
23269 class_type
= current_class_type
;
23270 /* Enter the scope indicated by the nested-name-specifier. */
23271 pushed_scope
= push_scope (nested_name_specifier
);
23272 /* Get the canonical version of this type. */
23273 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23274 /* Call push_template_decl if it seems like we should be defining a
23275 template either from the template headers or the type we're
23276 defining, so that we diagnose both extra and missing headers. */
23277 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23278 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23279 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23281 type
= push_template_decl (type
);
23282 if (type
== error_mark_node
)
23289 type
= TREE_TYPE (type
);
23290 *nested_name_specifier_p
= true;
23292 else /* The name is not a nested name. */
23294 /* If the class was unnamed, create a dummy name. */
23296 id
= make_anon_name ();
23297 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23298 ? ts_within_enclosing_non_class
23300 type
= xref_tag (class_key
, id
, tag_scope
,
23301 parser
->num_template_parameter_lists
);
23304 /* Indicate whether this class was declared as a `class' or as a
23306 if (TREE_CODE (type
) == RECORD_TYPE
)
23307 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23308 cp_parser_check_class_key (class_key
, type
);
23310 /* If this type was already complete, and we see another definition,
23311 that's an error. */
23312 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23314 error_at (type_start_token
->location
, "redefinition of %q#T",
23316 inform (location_of (type
), "previous definition of %q#T",
23321 else if (type
== error_mark_node
)
23326 /* Apply attributes now, before any use of the class as a template
23327 argument in its base list. */
23328 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23329 fixup_attribute_variants (type
);
23332 /* We will have entered the scope containing the class; the names of
23333 base classes should be looked up in that context. For example:
23335 struct A { struct B {}; struct C; };
23336 struct A::C : B {};
23340 /* Get the list of base-classes, if there is one. */
23341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23343 /* PR59482: enter the class scope so that base-specifiers are looked
23347 bases
= cp_parser_base_clause (parser
);
23348 /* PR59482: get out of the previously pushed class scope so that the
23349 subsequent pops pop the right thing. */
23356 /* If we're really defining a class, process the base classes.
23357 If they're invalid, fail. */
23358 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23359 xref_basetypes (type
, bases
);
23362 /* Leave the scope given by the nested-name-specifier. We will
23363 enter the class scope itself while processing the members. */
23365 pop_scope (pushed_scope
);
23367 if (invalid_explicit_specialization_p
)
23369 end_specialization ();
23370 --parser
->num_template_parameter_lists
;
23374 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23375 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23376 CLASSTYPE_FINAL (type
) = 1;
23378 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23382 /* Parse a class-key.
23389 Returns the kind of class-key specified, or none_type to indicate
23392 static enum tag_types
23393 cp_parser_class_key (cp_parser
* parser
)
23396 enum tag_types tag_type
;
23398 /* Look for the class-key. */
23399 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23403 /* Check to see if the TOKEN is a class-key. */
23404 tag_type
= cp_parser_token_is_class_key (token
);
23406 cp_parser_error (parser
, "expected class-key");
23410 /* Parse a type-parameter-key.
23412 type-parameter-key:
23418 cp_parser_type_parameter_key (cp_parser
* parser
)
23420 /* Look for the type-parameter-key. */
23421 enum tag_types tag_type
= none_type
;
23422 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23423 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23425 cp_lexer_consume_token (parser
->lexer
);
23426 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23427 /* typename is not allowed in a template template parameter
23428 by the standard until C++17. */
23429 pedwarn (token
->location
, OPT_Wpedantic
,
23430 "ISO C++ forbids typename key in template template parameter;"
23431 " use -std=c++17 or -std=gnu++17");
23434 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23439 /* Parse an (optional) member-specification.
23441 member-specification:
23442 member-declaration member-specification [opt]
23443 access-specifier : member-specification [opt] */
23446 cp_parser_member_specification_opt (cp_parser
* parser
)
23453 /* Peek at the next token. */
23454 token
= cp_lexer_peek_token (parser
->lexer
);
23455 /* If it's a `}', or EOF then we've seen all the members. */
23456 if (token
->type
== CPP_CLOSE_BRACE
23457 || token
->type
== CPP_EOF
23458 || token
->type
== CPP_PRAGMA_EOL
)
23461 /* See if this token is a keyword. */
23462 keyword
= token
->keyword
;
23466 case RID_PROTECTED
:
23468 /* Consume the access-specifier. */
23469 cp_lexer_consume_token (parser
->lexer
);
23470 /* Remember which access-specifier is active. */
23471 current_access_specifier
= token
->u
.value
;
23472 /* Look for the `:'. */
23473 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23477 /* Accept #pragmas at class scope. */
23478 if (token
->type
== CPP_PRAGMA
)
23480 cp_parser_pragma (parser
, pragma_member
, NULL
);
23484 /* Otherwise, the next construction must be a
23485 member-declaration. */
23486 cp_parser_member_declaration (parser
);
23491 /* Parse a member-declaration.
23493 member-declaration:
23494 decl-specifier-seq [opt] member-declarator-list [opt] ;
23495 function-definition ; [opt]
23496 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23498 template-declaration
23501 member-declarator-list:
23503 member-declarator-list , member-declarator
23506 declarator pure-specifier [opt]
23507 declarator constant-initializer [opt]
23508 identifier [opt] : constant-expression
23512 member-declaration:
23513 __extension__ member-declaration
23516 declarator attributes [opt] pure-specifier [opt]
23517 declarator attributes [opt] constant-initializer [opt]
23518 identifier [opt] attributes [opt] : constant-expression
23522 member-declaration:
23523 static_assert-declaration */
23526 cp_parser_member_declaration (cp_parser
* parser
)
23528 cp_decl_specifier_seq decl_specifiers
;
23529 tree prefix_attributes
;
23531 int declares_class_or_enum
;
23533 cp_token
*token
= NULL
;
23534 cp_token
*decl_spec_token_start
= NULL
;
23535 cp_token
*initializer_token_start
= NULL
;
23536 int saved_pedantic
;
23537 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23539 /* Check for the `__extension__' keyword. */
23540 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23543 cp_parser_member_declaration (parser
);
23544 /* Restore the old value of the PEDANTIC flag. */
23545 pedantic
= saved_pedantic
;
23550 /* Check for a template-declaration. */
23551 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23553 /* An explicit specialization here is an error condition, and we
23554 expect the specialization handler to detect and report this. */
23555 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23556 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23557 cp_parser_explicit_specialization (parser
);
23559 cp_parser_template_declaration (parser
, /*member_p=*/true);
23563 /* Check for a template introduction. */
23564 else if (cp_parser_template_declaration_after_export (parser
, true))
23567 /* Check for a using-declaration. */
23568 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23570 if (cxx_dialect
< cxx11
)
23572 /* Parse the using-declaration. */
23573 cp_parser_using_declaration (parser
,
23574 /*access_declaration_p=*/false);
23580 bool alias_decl_expected
;
23581 cp_parser_parse_tentatively (parser
);
23582 decl
= cp_parser_alias_declaration (parser
);
23583 /* Note that if we actually see the '=' token after the
23584 identifier, cp_parser_alias_declaration commits the
23585 tentative parse. In that case, we really expect an
23586 alias-declaration. Otherwise, we expect a using
23588 alias_decl_expected
=
23589 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23590 cp_parser_parse_definitely (parser
);
23592 if (alias_decl_expected
)
23593 finish_member_declaration (decl
);
23595 cp_parser_using_declaration (parser
,
23596 /*access_declaration_p=*/false);
23601 /* Check for @defs. */
23602 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23605 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23606 ivar
= ivar_chains
;
23610 ivar
= TREE_CHAIN (member
);
23611 TREE_CHAIN (member
) = NULL_TREE
;
23612 finish_member_declaration (member
);
23617 /* If the next token is `static_assert' we have a static assertion. */
23618 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23620 cp_parser_static_assert (parser
, /*member_p=*/true);
23624 parser
->colon_corrects_to_scope_p
= false;
23626 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23629 /* Parse the decl-specifier-seq. */
23630 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23631 cp_parser_decl_specifier_seq (parser
,
23632 CP_PARSER_FLAGS_OPTIONAL
,
23634 &declares_class_or_enum
);
23635 /* Check for an invalid type-name. */
23636 if (!decl_specifiers
.any_type_specifiers_p
23637 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23639 /* If there is no declarator, then the decl-specifier-seq should
23641 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23643 /* If there was no decl-specifier-seq, and the next token is a
23644 `;', then we have something like:
23650 Each member-declaration shall declare at least one member
23651 name of the class. */
23652 if (!decl_specifiers
.any_specifiers_p
)
23654 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23655 if (!in_system_header_at (token
->location
))
23657 gcc_rich_location
richloc (token
->location
);
23658 richloc
.add_fixit_remove ();
23659 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23666 /* See if this declaration is a friend. */
23667 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23668 /* If there were decl-specifiers, check to see if there was
23669 a class-declaration. */
23670 type
= check_tag_decl (&decl_specifiers
,
23671 /*explicit_type_instantiation_p=*/false);
23672 /* Nested classes have already been added to the class, but
23673 a `friend' needs to be explicitly registered. */
23676 /* If the `friend' keyword was present, the friend must
23677 be introduced with a class-key. */
23678 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23679 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23680 "in C++03 a class-key must be used "
23681 "when declaring a friend");
23684 template <typename T> struct A {
23685 friend struct A<T>::B;
23688 A<T>::B will be represented by a TYPENAME_TYPE, and
23689 therefore not recognized by check_tag_decl. */
23692 type
= decl_specifiers
.type
;
23693 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23694 type
= TREE_TYPE (type
);
23696 if (!type
|| !TYPE_P (type
))
23697 error_at (decl_spec_token_start
->location
,
23698 "friend declaration does not name a class or "
23701 make_friend_class (current_class_type
, type
,
23702 /*complain=*/true);
23704 /* If there is no TYPE, an error message will already have
23706 else if (!type
|| type
== error_mark_node
)
23708 /* An anonymous aggregate has to be handled specially; such
23709 a declaration really declares a data member (with a
23710 particular type), as opposed to a nested class. */
23711 else if (ANON_AGGR_TYPE_P (type
))
23714 if (decl_specifiers
.storage_class
!= sc_none
)
23715 error_at (decl_spec_token_start
->location
,
23716 "a storage class on an anonymous aggregate "
23717 "in class scope is not allowed");
23719 /* Remove constructors and such from TYPE, now that we
23720 know it is an anonymous aggregate. */
23721 fixup_anonymous_aggr (type
);
23722 /* And make the corresponding data member. */
23723 decl
= build_decl (decl_spec_token_start
->location
,
23724 FIELD_DECL
, NULL_TREE
, type
);
23725 /* Add it to the class. */
23726 finish_member_declaration (decl
);
23729 cp_parser_check_access_in_redeclaration
23731 decl_spec_token_start
->location
);
23736 bool assume_semicolon
= false;
23738 /* Clear attributes from the decl_specifiers but keep them
23739 around as prefix attributes that apply them to the entity
23741 prefix_attributes
= decl_specifiers
.attributes
;
23742 decl_specifiers
.attributes
= NULL_TREE
;
23744 /* See if these declarations will be friends. */
23745 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23747 /* Keep going until we hit the `;' at the end of the
23749 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23751 tree attributes
= NULL_TREE
;
23752 tree first_attribute
;
23754 bool named_bitfld
= false;
23756 /* Peek at the next token. */
23757 token
= cp_lexer_peek_token (parser
->lexer
);
23759 /* The following code wants to know early if it is a bit-field
23760 or some other declaration. Attributes can appear before
23761 the `:' token. Skip over them without consuming any tokens
23762 to peek if they are followed by `:'. */
23763 if (cp_next_tokens_can_be_attribute_p (parser
)
23764 || (token
->type
== CPP_NAME
23765 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23766 && (named_bitfld
= true)))
23769 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23770 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23773 /* Check for a bitfield declaration. */
23774 if (token
->type
== CPP_COLON
23775 || (token
->type
== CPP_NAME
23776 && token
== cp_lexer_peek_token (parser
->lexer
)
23777 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23778 && (named_bitfld
= true)))
23782 tree late_attributes
= NULL_TREE
;
23785 identifier
= cp_parser_identifier (parser
);
23787 identifier
= NULL_TREE
;
23789 /* Look for attributes that apply to the bitfield. */
23790 attributes
= cp_parser_attributes_opt (parser
);
23792 /* Consume the `:' token. */
23793 cp_lexer_consume_token (parser
->lexer
);
23795 /* Get the width of the bitfield. */
23796 width
= cp_parser_constant_expression (parser
, false, NULL
,
23797 cxx_dialect
>= cxx11
);
23799 /* In C++2A and as extension for C++11 and above we allow
23800 default member initializers for bit-fields. */
23801 initializer
= NULL_TREE
;
23802 if (cxx_dialect
>= cxx11
23803 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23804 || cp_lexer_next_token_is (parser
->lexer
,
23808 = cp_lexer_peek_token (parser
->lexer
)->location
;
23809 if (cxx_dialect
< cxx2a
23810 && !in_system_header_at (loc
)
23811 && identifier
!= NULL_TREE
)
23813 "default member initializers for bit-fields "
23814 "only available with -std=c++2a or "
23817 initializer
= cp_parser_save_nsdmi (parser
);
23818 if (identifier
== NULL_TREE
)
23820 error_at (loc
, "default member initializer for "
23821 "unnamed bit-field");
23822 initializer
= NULL_TREE
;
23827 /* Look for attributes that apply to the bitfield after
23828 the `:' token and width. This is where GCC used to
23829 parse attributes in the past, pedwarn if there is
23830 a std attribute. */
23831 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23832 pedwarn (input_location
, OPT_Wpedantic
,
23833 "ISO C++ allows bit-field attributes only "
23834 "before the %<:%> token");
23836 late_attributes
= cp_parser_attributes_opt (parser
);
23839 attributes
= attr_chainon (attributes
, late_attributes
);
23841 /* Remember which attributes are prefix attributes and
23843 first_attribute
= attributes
;
23844 /* Combine the attributes. */
23845 attributes
= attr_chainon (prefix_attributes
, attributes
);
23847 /* Create the bitfield declaration. */
23848 decl
= grokbitfield (identifier
23849 ? make_id_declarator (NULL_TREE
,
23854 width
, initializer
,
23859 cp_declarator
*declarator
;
23860 tree asm_specification
;
23861 int ctor_dtor_or_conv_p
;
23863 /* Parse the declarator. */
23865 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23866 &ctor_dtor_or_conv_p
,
23867 /*parenthesized_p=*/NULL
,
23871 /* If something went wrong parsing the declarator, make sure
23872 that we at least consume some tokens. */
23873 if (declarator
== cp_error_declarator
)
23875 /* Skip to the end of the statement. */
23876 cp_parser_skip_to_end_of_statement (parser
);
23877 /* If the next token is not a semicolon, that is
23878 probably because we just skipped over the body of
23879 a function. So, we consume a semicolon if
23880 present, but do not issue an error message if it
23882 if (cp_lexer_next_token_is (parser
->lexer
,
23884 cp_lexer_consume_token (parser
->lexer
);
23888 if (declares_class_or_enum
& 2)
23889 cp_parser_check_for_definition_in_return_type
23890 (declarator
, decl_specifiers
.type
,
23891 decl_specifiers
.locations
[ds_type_spec
]);
23893 /* Look for an asm-specification. */
23894 asm_specification
= cp_parser_asm_specification_opt (parser
);
23895 /* Look for attributes that apply to the declaration. */
23896 attributes
= cp_parser_attributes_opt (parser
);
23897 /* Remember which attributes are prefix attributes and
23899 first_attribute
= attributes
;
23900 /* Combine the attributes. */
23901 attributes
= attr_chainon (prefix_attributes
, attributes
);
23903 /* If it's an `=', then we have a constant-initializer or a
23904 pure-specifier. It is not correct to parse the
23905 initializer before registering the member declaration
23906 since the member declaration should be in scope while
23907 its initializer is processed. However, the rest of the
23908 front end does not yet provide an interface that allows
23909 us to handle this correctly. */
23910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23914 A pure-specifier shall be used only in the declaration of
23915 a virtual function.
23917 A member-declarator can contain a constant-initializer
23918 only if it declares a static member of integral or
23921 Therefore, if the DECLARATOR is for a function, we look
23922 for a pure-specifier; otherwise, we look for a
23923 constant-initializer. When we call `grokfield', it will
23924 perform more stringent semantics checks. */
23925 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23926 if (function_declarator_p (declarator
)
23927 || (decl_specifiers
.type
23928 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23929 && declarator
->kind
== cdk_id
23930 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23931 == FUNCTION_TYPE
)))
23932 initializer
= cp_parser_pure_specifier (parser
);
23933 else if (decl_specifiers
.storage_class
!= sc_static
)
23934 initializer
= cp_parser_save_nsdmi (parser
);
23935 else if (cxx_dialect
>= cxx11
)
23938 /* Don't require a constant rvalue in C++11, since we
23939 might want a reference constant. We'll enforce
23940 constancy later. */
23941 cp_lexer_consume_token (parser
->lexer
);
23942 /* Parse the initializer. */
23943 initializer
= cp_parser_initializer_clause (parser
,
23947 /* Parse the initializer. */
23948 initializer
= cp_parser_constant_initializer (parser
);
23950 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23951 && !function_declarator_p (declarator
))
23954 if (decl_specifiers
.storage_class
!= sc_static
)
23955 initializer
= cp_parser_save_nsdmi (parser
);
23957 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23959 /* Otherwise, there is no initializer. */
23961 initializer
= NULL_TREE
;
23963 /* See if we are probably looking at a function
23964 definition. We are certainly not looking at a
23965 member-declarator. Calling `grokfield' has
23966 side-effects, so we must not do it unless we are sure
23967 that we are looking at a member-declarator. */
23968 if (cp_parser_token_starts_function_definition_p
23969 (cp_lexer_peek_token (parser
->lexer
)))
23971 /* The grammar does not allow a pure-specifier to be
23972 used when a member function is defined. (It is
23973 possible that this fact is an oversight in the
23974 standard, since a pure function may be defined
23975 outside of the class-specifier. */
23976 if (initializer
&& initializer_token_start
)
23977 error_at (initializer_token_start
->location
,
23978 "pure-specifier on function-definition");
23979 decl
= cp_parser_save_member_function_body (parser
,
23983 if (parser
->fully_implicit_function_template_p
)
23984 decl
= finish_fully_implicit_template (parser
, decl
);
23985 /* If the member was not a friend, declare it here. */
23987 finish_member_declaration (decl
);
23988 /* Peek at the next token. */
23989 token
= cp_lexer_peek_token (parser
->lexer
);
23990 /* If the next token is a semicolon, consume it. */
23991 if (token
->type
== CPP_SEMICOLON
)
23993 location_t semicolon_loc
23994 = cp_lexer_consume_token (parser
->lexer
)->location
;
23995 gcc_rich_location
richloc (semicolon_loc
);
23996 richloc
.add_fixit_remove ();
23997 warning_at (&richloc
, OPT_Wextra_semi
,
23998 "extra %<;%> after in-class "
23999 "function definition");
24004 if (declarator
->kind
== cdk_function
)
24005 declarator
->id_loc
= token
->location
;
24006 /* Create the declaration. */
24007 decl
= grokfield (declarator
, &decl_specifiers
,
24008 initializer
, /*init_const_expr_p=*/true,
24009 asm_specification
, attributes
);
24010 if (parser
->fully_implicit_function_template_p
)
24013 finish_fully_implicit_template (parser
, 0);
24015 decl
= finish_fully_implicit_template (parser
, decl
);
24019 cp_finalize_omp_declare_simd (parser
, decl
);
24020 cp_finalize_oacc_routine (parser
, decl
, false);
24022 /* Reset PREFIX_ATTRIBUTES. */
24023 if (attributes
!= error_mark_node
)
24025 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24026 attributes
= TREE_CHAIN (attributes
);
24028 TREE_CHAIN (attributes
) = NULL_TREE
;
24031 /* If there is any qualification still in effect, clear it
24032 now; we will be starting fresh with the next declarator. */
24033 parser
->scope
= NULL_TREE
;
24034 parser
->qualifying_scope
= NULL_TREE
;
24035 parser
->object_scope
= NULL_TREE
;
24036 /* If it's a `,', then there are more declarators. */
24037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24039 cp_lexer_consume_token (parser
->lexer
);
24040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24042 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24043 gcc_rich_location
richloc (token
->location
);
24044 richloc
.add_fixit_remove ();
24045 error_at (&richloc
, "stray %<,%> at end of "
24046 "member declaration");
24049 /* If the next token isn't a `;', then we have a parse error. */
24050 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24053 /* The next token might be a ways away from where the
24054 actual semicolon is missing. Find the previous token
24055 and use that for our error position. */
24056 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24057 gcc_rich_location
richloc (token
->location
);
24058 richloc
.add_fixit_insert_after (";");
24059 error_at (&richloc
, "expected %<;%> at end of "
24060 "member declaration");
24062 /* Assume that the user meant to provide a semicolon. If
24063 we were to cp_parser_skip_to_end_of_statement, we might
24064 skip to a semicolon inside a member function definition
24065 and issue nonsensical error messages. */
24066 assume_semicolon
= true;
24071 /* Add DECL to the list of members. */
24073 /* Explicitly include, eg, NSDMIs, for better error
24074 recovery (c++/58650). */
24075 || !DECL_DECLARES_FUNCTION_P (decl
))
24076 finish_member_declaration (decl
);
24078 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24079 cp_parser_save_default_args (parser
, decl
);
24080 else if (TREE_CODE (decl
) == FIELD_DECL
24081 && DECL_INITIAL (decl
))
24082 /* Add DECL to the queue of NSDMI to be parsed later. */
24083 vec_safe_push (unparsed_nsdmis
, decl
);
24086 if (assume_semicolon
)
24091 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24093 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24096 /* Parse a pure-specifier.
24101 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24102 Otherwise, ERROR_MARK_NODE is returned. */
24105 cp_parser_pure_specifier (cp_parser
* parser
)
24109 /* Look for the `=' token. */
24110 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24111 return error_mark_node
;
24112 /* Look for the `0' token. */
24113 token
= cp_lexer_peek_token (parser
->lexer
);
24115 if (token
->type
== CPP_EOF
24116 || token
->type
== CPP_PRAGMA_EOL
)
24117 return error_mark_node
;
24119 cp_lexer_consume_token (parser
->lexer
);
24121 /* Accept = default or = delete in c++0x mode. */
24122 if (token
->keyword
== RID_DEFAULT
24123 || token
->keyword
== RID_DELETE
)
24125 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24126 return token
->u
.value
;
24129 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24130 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24132 cp_parser_error (parser
,
24133 "invalid pure specifier (only %<= 0%> is allowed)");
24134 cp_parser_skip_to_end_of_statement (parser
);
24135 return error_mark_node
;
24137 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24139 error_at (token
->location
, "templates may not be %<virtual%>");
24140 return error_mark_node
;
24143 return integer_zero_node
;
24146 /* Parse a constant-initializer.
24148 constant-initializer:
24149 = constant-expression
24151 Returns a representation of the constant-expression. */
24154 cp_parser_constant_initializer (cp_parser
* parser
)
24156 /* Look for the `=' token. */
24157 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24158 return error_mark_node
;
24160 /* It is invalid to write:
24162 struct S { static const int i = { 7 }; };
24165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24167 cp_parser_error (parser
,
24168 "a brace-enclosed initializer is not allowed here");
24169 /* Consume the opening brace. */
24170 matching_braces braces
;
24171 braces
.consume_open (parser
);
24172 /* Skip the initializer. */
24173 cp_parser_skip_to_closing_brace (parser
);
24174 /* Look for the trailing `}'. */
24175 braces
.require_close (parser
);
24177 return error_mark_node
;
24180 return cp_parser_constant_expression (parser
);
24183 /* Derived classes [gram.class.derived] */
24185 /* Parse a base-clause.
24188 : base-specifier-list
24190 base-specifier-list:
24191 base-specifier ... [opt]
24192 base-specifier-list , base-specifier ... [opt]
24194 Returns a TREE_LIST representing the base-classes, in the order in
24195 which they were declared. The representation of each node is as
24196 described by cp_parser_base_specifier.
24198 In the case that no bases are specified, this function will return
24199 NULL_TREE, not ERROR_MARK_NODE. */
24202 cp_parser_base_clause (cp_parser
* parser
)
24204 tree bases
= NULL_TREE
;
24206 /* Look for the `:' that begins the list. */
24207 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24209 /* Scan the base-specifier-list. */
24214 bool pack_expansion_p
= false;
24216 /* Look for the base-specifier. */
24217 base
= cp_parser_base_specifier (parser
);
24218 /* Look for the (optional) ellipsis. */
24219 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24221 /* Consume the `...'. */
24222 cp_lexer_consume_token (parser
->lexer
);
24224 pack_expansion_p
= true;
24227 /* Add BASE to the front of the list. */
24228 if (base
&& base
!= error_mark_node
)
24230 if (pack_expansion_p
)
24231 /* Make this a pack expansion type. */
24232 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24234 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24236 TREE_CHAIN (base
) = bases
;
24240 /* Peek at the next token. */
24241 token
= cp_lexer_peek_token (parser
->lexer
);
24242 /* If it's not a comma, then the list is complete. */
24243 if (token
->type
!= CPP_COMMA
)
24245 /* Consume the `,'. */
24246 cp_lexer_consume_token (parser
->lexer
);
24249 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24250 base class had a qualified name. However, the next name that
24251 appears is certainly not qualified. */
24252 parser
->scope
= NULL_TREE
;
24253 parser
->qualifying_scope
= NULL_TREE
;
24254 parser
->object_scope
= NULL_TREE
;
24256 return nreverse (bases
);
24259 /* Parse a base-specifier.
24262 :: [opt] nested-name-specifier [opt] class-name
24263 virtual access-specifier [opt] :: [opt] nested-name-specifier
24265 access-specifier virtual [opt] :: [opt] nested-name-specifier
24268 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24269 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24270 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24271 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24274 cp_parser_base_specifier (cp_parser
* parser
)
24278 bool virtual_p
= false;
24279 bool duplicate_virtual_error_issued_p
= false;
24280 bool duplicate_access_error_issued_p
= false;
24281 bool class_scope_p
, template_p
;
24282 tree access
= access_default_node
;
24285 /* Process the optional `virtual' and `access-specifier'. */
24288 /* Peek at the next token. */
24289 token
= cp_lexer_peek_token (parser
->lexer
);
24290 /* Process `virtual'. */
24291 switch (token
->keyword
)
24294 /* If `virtual' appears more than once, issue an error. */
24295 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24297 cp_parser_error (parser
,
24298 "%<virtual%> specified more than once in base-specifier");
24299 duplicate_virtual_error_issued_p
= true;
24304 /* Consume the `virtual' token. */
24305 cp_lexer_consume_token (parser
->lexer
);
24310 case RID_PROTECTED
:
24312 /* If more than one access specifier appears, issue an
24314 if (access
!= access_default_node
24315 && !duplicate_access_error_issued_p
)
24317 cp_parser_error (parser
,
24318 "more than one access specifier in base-specifier");
24319 duplicate_access_error_issued_p
= true;
24322 access
= ridpointers
[(int) token
->keyword
];
24324 /* Consume the access-specifier. */
24325 cp_lexer_consume_token (parser
->lexer
);
24334 /* It is not uncommon to see programs mechanically, erroneously, use
24335 the 'typename' keyword to denote (dependent) qualified types
24336 as base classes. */
24337 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24339 token
= cp_lexer_peek_token (parser
->lexer
);
24340 if (!processing_template_decl
)
24341 error_at (token
->location
,
24342 "keyword %<typename%> not allowed outside of templates");
24344 error_at (token
->location
,
24345 "keyword %<typename%> not allowed in this context "
24346 "(the base class is implicitly a type)");
24347 cp_lexer_consume_token (parser
->lexer
);
24350 /* Look for the optional `::' operator. */
24351 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24352 /* Look for the nested-name-specifier. The simplest way to
24357 The keyword `typename' is not permitted in a base-specifier or
24358 mem-initializer; in these contexts a qualified name that
24359 depends on a template-parameter is implicitly assumed to be a
24362 is to pretend that we have seen the `typename' keyword at this
24364 cp_parser_nested_name_specifier_opt (parser
,
24365 /*typename_keyword_p=*/true,
24366 /*check_dependency_p=*/true,
24368 /*is_declaration=*/true);
24369 /* If the base class is given by a qualified name, assume that names
24370 we see are type names or templates, as appropriate. */
24371 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24372 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24375 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24376 /* DR 950 allows decltype as a base-specifier. */
24377 type
= cp_parser_decltype (parser
);
24380 /* Otherwise, look for the class-name. */
24381 type
= cp_parser_class_name (parser
,
24385 /*check_dependency_p=*/true,
24386 /*class_head_p=*/false,
24387 /*is_declaration=*/true);
24388 type
= TREE_TYPE (type
);
24391 if (type
== error_mark_node
)
24392 return error_mark_node
;
24394 return finish_base_specifier (type
, access
, virtual_p
);
24397 /* Exception handling [gram.exception] */
24399 /* Parse an (optional) noexcept-specification.
24401 noexcept-specification:
24402 noexcept ( constant-expression ) [opt]
24404 If no noexcept-specification is present, returns NULL_TREE.
24405 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24406 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24407 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24408 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24409 in which case a boolean condition is returned instead. */
24412 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24413 bool require_constexpr
,
24414 bool* consumed_expr
,
24418 const char *saved_message
;
24420 /* Peek at the next token. */
24421 token
= cp_lexer_peek_token (parser
->lexer
);
24423 /* Is it a noexcept-specification? */
24424 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24427 cp_lexer_consume_token (parser
->lexer
);
24429 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24431 matching_parens parens
;
24432 parens
.consume_open (parser
);
24434 if (require_constexpr
)
24436 /* Types may not be defined in an exception-specification. */
24437 saved_message
= parser
->type_definition_forbidden_message
;
24438 parser
->type_definition_forbidden_message
24439 = G_("types may not be defined in an exception-specification");
24441 expr
= cp_parser_constant_expression (parser
);
24443 /* Restore the saved message. */
24444 parser
->type_definition_forbidden_message
= saved_message
;
24448 expr
= cp_parser_expression (parser
);
24449 *consumed_expr
= true;
24452 parens
.require_close (parser
);
24456 expr
= boolean_true_node
;
24457 if (!require_constexpr
)
24458 *consumed_expr
= false;
24461 /* We cannot build a noexcept-spec right away because this will check
24462 that expr is a constexpr. */
24464 return build_noexcept_spec (expr
, tf_warning_or_error
);
24472 /* Parse an (optional) exception-specification.
24474 exception-specification:
24475 throw ( type-id-list [opt] )
24477 Returns a TREE_LIST representing the exception-specification. The
24478 TREE_VALUE of each node is a type. */
24481 cp_parser_exception_specification_opt (cp_parser
* parser
)
24485 const char *saved_message
;
24487 /* Peek at the next token. */
24488 token
= cp_lexer_peek_token (parser
->lexer
);
24490 /* Is it a noexcept-specification? */
24491 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24493 if (type_id_list
!= NULL_TREE
)
24494 return type_id_list
;
24496 /* If it's not `throw', then there's no exception-specification. */
24497 if (!cp_parser_is_keyword (token
, RID_THROW
))
24500 location_t loc
= token
->location
;
24502 /* Consume the `throw'. */
24503 cp_lexer_consume_token (parser
->lexer
);
24505 /* Look for the `('. */
24506 matching_parens parens
;
24507 parens
.require_open (parser
);
24509 /* Peek at the next token. */
24510 token
= cp_lexer_peek_token (parser
->lexer
);
24511 /* If it's not a `)', then there is a type-id-list. */
24512 if (token
->type
!= CPP_CLOSE_PAREN
)
24514 /* Types may not be defined in an exception-specification. */
24515 saved_message
= parser
->type_definition_forbidden_message
;
24516 parser
->type_definition_forbidden_message
24517 = G_("types may not be defined in an exception-specification");
24518 /* Parse the type-id-list. */
24519 type_id_list
= cp_parser_type_id_list (parser
);
24520 /* Restore the saved message. */
24521 parser
->type_definition_forbidden_message
= saved_message
;
24523 if (cxx_dialect
>= cxx17
)
24525 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24527 type_id_list
= NULL_TREE
;
24529 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24530 warning_at (loc
, OPT_Wdeprecated
,
24531 "dynamic exception specifications are deprecated in "
24534 /* In C++17, throw() is equivalent to noexcept (true). throw()
24535 is deprecated in C++11 and above as well, but is still widely used,
24536 so don't warn about it yet. */
24537 else if (cxx_dialect
>= cxx17
)
24538 type_id_list
= noexcept_true_spec
;
24540 type_id_list
= empty_except_spec
;
24542 /* Look for the `)'. */
24543 parens
.require_close (parser
);
24545 return type_id_list
;
24548 /* Parse an (optional) type-id-list.
24552 type-id-list , type-id ... [opt]
24554 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24555 in the order that the types were presented. */
24558 cp_parser_type_id_list (cp_parser
* parser
)
24560 tree types
= NULL_TREE
;
24567 token
= cp_lexer_peek_token (parser
->lexer
);
24569 /* Get the next type-id. */
24570 type
= cp_parser_type_id (parser
);
24571 /* Check for invalid 'auto'. */
24572 if (flag_concepts
&& type_uses_auto (type
))
24574 error_at (token
->location
,
24575 "invalid use of %<auto%> in exception-specification");
24576 type
= error_mark_node
;
24578 /* Parse the optional ellipsis. */
24579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24581 /* Consume the `...'. */
24582 cp_lexer_consume_token (parser
->lexer
);
24584 /* Turn the type into a pack expansion expression. */
24585 type
= make_pack_expansion (type
);
24587 /* Add it to the list. */
24588 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24589 /* Peek at the next token. */
24590 token
= cp_lexer_peek_token (parser
->lexer
);
24591 /* If it is not a `,', we are done. */
24592 if (token
->type
!= CPP_COMMA
)
24594 /* Consume the `,'. */
24595 cp_lexer_consume_token (parser
->lexer
);
24598 return nreverse (types
);
24601 /* Parse a try-block.
24604 try compound-statement handler-seq */
24607 cp_parser_try_block (cp_parser
* parser
)
24611 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24612 if (parser
->in_function_body
24613 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24614 error ("%<try%> in %<constexpr%> function");
24616 try_block
= begin_try_block ();
24617 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24618 finish_try_block (try_block
);
24619 cp_parser_handler_seq (parser
);
24620 finish_handler_sequence (try_block
);
24625 /* Parse a function-try-block.
24627 function-try-block:
24628 try ctor-initializer [opt] function-body handler-seq */
24631 cp_parser_function_try_block (cp_parser
* parser
)
24633 tree compound_stmt
;
24636 /* Look for the `try' keyword. */
24637 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24639 /* Let the rest of the front end know where we are. */
24640 try_block
= begin_function_try_block (&compound_stmt
);
24641 /* Parse the function-body. */
24642 cp_parser_ctor_initializer_opt_and_function_body
24643 (parser
, /*in_function_try_block=*/true);
24644 /* We're done with the `try' part. */
24645 finish_function_try_block (try_block
);
24646 /* Parse the handlers. */
24647 cp_parser_handler_seq (parser
);
24648 /* We're done with the handlers. */
24649 finish_function_handler_sequence (try_block
, compound_stmt
);
24652 /* Parse a handler-seq.
24655 handler handler-seq [opt] */
24658 cp_parser_handler_seq (cp_parser
* parser
)
24664 /* Parse the handler. */
24665 cp_parser_handler (parser
);
24666 /* Peek at the next token. */
24667 token
= cp_lexer_peek_token (parser
->lexer
);
24668 /* If it's not `catch' then there are no more handlers. */
24669 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24674 /* Parse a handler.
24677 catch ( exception-declaration ) compound-statement */
24680 cp_parser_handler (cp_parser
* parser
)
24685 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24686 handler
= begin_handler ();
24687 matching_parens parens
;
24688 parens
.require_open (parser
);
24689 declaration
= cp_parser_exception_declaration (parser
);
24690 finish_handler_parms (declaration
, handler
);
24691 parens
.require_close (parser
);
24692 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24693 finish_handler (handler
);
24696 /* Parse an exception-declaration.
24698 exception-declaration:
24699 type-specifier-seq declarator
24700 type-specifier-seq abstract-declarator
24704 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24705 ellipsis variant is used. */
24708 cp_parser_exception_declaration (cp_parser
* parser
)
24710 cp_decl_specifier_seq type_specifiers
;
24711 cp_declarator
*declarator
;
24712 const char *saved_message
;
24714 /* If it's an ellipsis, it's easy to handle. */
24715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24717 /* Consume the `...' token. */
24718 cp_lexer_consume_token (parser
->lexer
);
24722 /* Types may not be defined in exception-declarations. */
24723 saved_message
= parser
->type_definition_forbidden_message
;
24724 parser
->type_definition_forbidden_message
24725 = G_("types may not be defined in exception-declarations");
24727 /* Parse the type-specifier-seq. */
24728 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24729 /*is_trailing_return=*/false,
24731 /* If it's a `)', then there is no declarator. */
24732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24735 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24736 /*ctor_dtor_or_conv_p=*/NULL
,
24737 /*parenthesized_p=*/NULL
,
24738 /*member_p=*/false,
24739 /*friend_p=*/false);
24741 /* Restore the saved message. */
24742 parser
->type_definition_forbidden_message
= saved_message
;
24744 if (!type_specifiers
.any_specifiers_p
)
24745 return error_mark_node
;
24747 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24750 /* Parse a throw-expression.
24753 throw assignment-expression [opt]
24755 Returns a THROW_EXPR representing the throw-expression. */
24758 cp_parser_throw_expression (cp_parser
* parser
)
24763 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24764 token
= cp_lexer_peek_token (parser
->lexer
);
24765 /* Figure out whether or not there is an assignment-expression
24766 following the "throw" keyword. */
24767 if (token
->type
== CPP_COMMA
24768 || token
->type
== CPP_SEMICOLON
24769 || token
->type
== CPP_CLOSE_PAREN
24770 || token
->type
== CPP_CLOSE_SQUARE
24771 || token
->type
== CPP_CLOSE_BRACE
24772 || token
->type
== CPP_COLON
)
24773 expression
= NULL_TREE
;
24775 expression
= cp_parser_assignment_expression (parser
);
24777 return build_throw (expression
);
24780 /* GNU Extensions */
24782 /* Parse an (optional) asm-specification.
24785 asm ( string-literal )
24787 If the asm-specification is present, returns a STRING_CST
24788 corresponding to the string-literal. Otherwise, returns
24792 cp_parser_asm_specification_opt (cp_parser
* parser
)
24795 tree asm_specification
;
24797 /* Peek at the next token. */
24798 token
= cp_lexer_peek_token (parser
->lexer
);
24799 /* If the next token isn't the `asm' keyword, then there's no
24800 asm-specification. */
24801 if (!cp_parser_is_keyword (token
, RID_ASM
))
24804 /* Consume the `asm' token. */
24805 cp_lexer_consume_token (parser
->lexer
);
24806 /* Look for the `('. */
24807 matching_parens parens
;
24808 parens
.require_open (parser
);
24810 /* Look for the string-literal. */
24811 asm_specification
= cp_parser_string_literal (parser
, false, false);
24813 /* Look for the `)'. */
24814 parens
.require_close (parser
);
24816 return asm_specification
;
24819 /* Parse an asm-operand-list.
24823 asm-operand-list , asm-operand
24826 string-literal ( expression )
24827 [ string-literal ] string-literal ( expression )
24829 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24830 each node is the expression. The TREE_PURPOSE is itself a
24831 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24832 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24833 is a STRING_CST for the string literal before the parenthesis. Returns
24834 ERROR_MARK_NODE if any of the operands are invalid. */
24837 cp_parser_asm_operand_list (cp_parser
* parser
)
24839 tree asm_operands
= NULL_TREE
;
24840 bool invalid_operands
= false;
24844 tree string_literal
;
24848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24850 /* Consume the `[' token. */
24851 cp_lexer_consume_token (parser
->lexer
);
24852 /* Read the operand name. */
24853 name
= cp_parser_identifier (parser
);
24854 if (name
!= error_mark_node
)
24855 name
= build_string (IDENTIFIER_LENGTH (name
),
24856 IDENTIFIER_POINTER (name
));
24857 /* Look for the closing `]'. */
24858 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24862 /* Look for the string-literal. */
24863 string_literal
= cp_parser_string_literal (parser
, false, false);
24865 /* Look for the `('. */
24866 matching_parens parens
;
24867 parens
.require_open (parser
);
24868 /* Parse the expression. */
24869 expression
= cp_parser_expression (parser
);
24870 /* Look for the `)'. */
24871 parens
.require_close (parser
);
24873 if (name
== error_mark_node
24874 || string_literal
== error_mark_node
24875 || expression
== error_mark_node
)
24876 invalid_operands
= true;
24878 /* Add this operand to the list. */
24879 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24882 /* If the next token is not a `,', there are no more
24884 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24886 /* Consume the `,'. */
24887 cp_lexer_consume_token (parser
->lexer
);
24890 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24893 /* Parse an asm-clobber-list.
24897 asm-clobber-list , string-literal
24899 Returns a TREE_LIST, indicating the clobbers in the order that they
24900 appeared. The TREE_VALUE of each node is a STRING_CST. */
24903 cp_parser_asm_clobber_list (cp_parser
* parser
)
24905 tree clobbers
= NULL_TREE
;
24909 tree string_literal
;
24911 /* Look for the string literal. */
24912 string_literal
= cp_parser_string_literal (parser
, false, false);
24913 /* Add it to the list. */
24914 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24915 /* If the next token is not a `,', then the list is
24917 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24919 /* Consume the `,' token. */
24920 cp_lexer_consume_token (parser
->lexer
);
24926 /* Parse an asm-label-list.
24930 asm-label-list , identifier
24932 Returns a TREE_LIST, indicating the labels in the order that they
24933 appeared. The TREE_VALUE of each node is a label. */
24936 cp_parser_asm_label_list (cp_parser
* parser
)
24938 tree labels
= NULL_TREE
;
24942 tree identifier
, label
, name
;
24944 /* Look for the identifier. */
24945 identifier
= cp_parser_identifier (parser
);
24946 if (!error_operand_p (identifier
))
24948 label
= lookup_label (identifier
);
24949 if (TREE_CODE (label
) == LABEL_DECL
)
24951 TREE_USED (label
) = 1;
24952 check_goto (label
);
24953 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24954 IDENTIFIER_POINTER (identifier
));
24955 labels
= tree_cons (name
, label
, labels
);
24958 /* If the next token is not a `,', then the list is
24960 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24962 /* Consume the `,' token. */
24963 cp_lexer_consume_token (parser
->lexer
);
24966 return nreverse (labels
);
24969 /* Return TRUE iff the next tokens in the stream are possibly the
24970 beginning of a GNU extension attribute. */
24973 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24975 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24978 /* Return TRUE iff the next tokens in the stream are possibly the
24979 beginning of a standard C++-11 attribute specifier. */
24982 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24984 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24987 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24988 beginning of a standard C++-11 attribute specifier. */
24991 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24993 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24995 return (cxx_dialect
>= cxx11
24996 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24997 || (token
->type
== CPP_OPEN_SQUARE
24998 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24999 && token
->type
== CPP_OPEN_SQUARE
)));
25002 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25003 beginning of a GNU extension attribute. */
25006 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25008 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25010 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25013 /* Return true iff the next tokens can be the beginning of either a
25014 GNU attribute list, or a standard C++11 attribute sequence. */
25017 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25019 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25020 || cp_next_tokens_can_be_std_attribute_p (parser
));
25023 /* Return true iff the next Nth tokens can be the beginning of either
25024 a GNU attribute list, or a standard C++11 attribute sequence. */
25027 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25029 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25030 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25033 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25034 of GNU attributes, or return NULL. */
25037 cp_parser_attributes_opt (cp_parser
*parser
)
25039 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25040 return cp_parser_gnu_attributes_opt (parser
);
25041 return cp_parser_std_attribute_spec_seq (parser
);
25044 /* Parse an (optional) series of attributes.
25047 attributes attribute
25050 __attribute__ (( attribute-list [opt] ))
25052 The return value is as for cp_parser_gnu_attribute_list. */
25055 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25057 tree attributes
= NULL_TREE
;
25059 temp_override
<bool> cleanup
25060 (parser
->auto_is_implicit_function_template_parm_p
, false);
25065 tree attribute_list
;
25068 /* Peek at the next token. */
25069 token
= cp_lexer_peek_token (parser
->lexer
);
25070 /* If it's not `__attribute__', then we're done. */
25071 if (token
->keyword
!= RID_ATTRIBUTE
)
25074 /* Consume the `__attribute__' keyword. */
25075 cp_lexer_consume_token (parser
->lexer
);
25076 /* Look for the two `(' tokens. */
25077 matching_parens outer_parens
;
25078 outer_parens
.require_open (parser
);
25079 matching_parens inner_parens
;
25080 inner_parens
.require_open (parser
);
25082 /* Peek at the next token. */
25083 token
= cp_lexer_peek_token (parser
->lexer
);
25084 if (token
->type
!= CPP_CLOSE_PAREN
)
25085 /* Parse the attribute-list. */
25086 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25088 /* If the next token is a `)', then there is no attribute
25090 attribute_list
= NULL
;
25092 /* Look for the two `)' tokens. */
25093 if (!inner_parens
.require_close (parser
))
25095 if (!outer_parens
.require_close (parser
))
25098 cp_parser_skip_to_end_of_statement (parser
);
25100 /* Add these new attributes to the list. */
25101 attributes
= attr_chainon (attributes
, attribute_list
);
25107 /* Parse a GNU attribute-list.
25111 attribute-list , attribute
25115 identifier ( identifier )
25116 identifier ( identifier , expression-list )
25117 identifier ( expression-list )
25119 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25120 to an attribute. The TREE_PURPOSE of each node is the identifier
25121 indicating which attribute is in use. The TREE_VALUE represents
25122 the arguments, if any. */
25125 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25127 tree attribute_list
= NULL_TREE
;
25128 bool save_translate_strings_p
= parser
->translate_strings_p
;
25130 parser
->translate_strings_p
= false;
25137 /* Look for the identifier. We also allow keywords here; for
25138 example `__attribute__ ((const))' is legal. */
25139 token
= cp_lexer_peek_token (parser
->lexer
);
25140 if (token
->type
== CPP_NAME
25141 || token
->type
== CPP_KEYWORD
)
25143 tree arguments
= NULL_TREE
;
25145 /* Consume the token, but save it since we need it for the
25146 SIMD enabled function parsing. */
25147 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25149 /* Save away the identifier that indicates which attribute
25151 identifier
= (token
->type
== CPP_KEYWORD
)
25152 /* For keywords, use the canonical spelling, not the
25153 parsed identifier. */
25154 ? ridpointers
[(int) token
->keyword
]
25155 : id_token
->u
.value
;
25157 identifier
= canonicalize_attr_name (identifier
);
25158 attribute
= build_tree_list (identifier
, NULL_TREE
);
25160 /* Peek at the next token. */
25161 token
= cp_lexer_peek_token (parser
->lexer
);
25162 /* If it's an `(', then parse the attribute arguments. */
25163 if (token
->type
== CPP_OPEN_PAREN
)
25165 vec
<tree
, va_gc
> *vec
;
25166 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25167 ? id_attr
: normal_attr
);
25168 vec
= cp_parser_parenthesized_expression_list
25169 (parser
, attr_flag
, /*cast_p=*/false,
25170 /*allow_expansion_p=*/false,
25171 /*non_constant_p=*/NULL
);
25173 arguments
= error_mark_node
;
25176 arguments
= build_tree_list_vec (vec
);
25177 release_tree_vector (vec
);
25179 /* Save the arguments away. */
25180 TREE_VALUE (attribute
) = arguments
;
25183 if (arguments
!= error_mark_node
)
25185 /* Add this attribute to the list. */
25186 TREE_CHAIN (attribute
) = attribute_list
;
25187 attribute_list
= attribute
;
25190 token
= cp_lexer_peek_token (parser
->lexer
);
25192 /* Now, look for more attributes. If the next token isn't a
25193 `,', we're done. */
25194 if (token
->type
!= CPP_COMMA
)
25197 /* Consume the comma and keep going. */
25198 cp_lexer_consume_token (parser
->lexer
);
25200 parser
->translate_strings_p
= save_translate_strings_p
;
25202 /* We built up the list in reverse order. */
25203 return nreverse (attribute_list
);
25206 /* Parse a standard C++11 attribute.
25208 The returned representation is a TREE_LIST which TREE_PURPOSE is
25209 the scoped name of the attribute, and the TREE_VALUE is its
25212 Note that the scoped name of the attribute is itself a TREE_LIST
25213 which TREE_PURPOSE is the namespace of the attribute, and
25214 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25215 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25216 and which TREE_PURPOSE is directly the attribute name.
25218 Clients of the attribute code should use get_attribute_namespace
25219 and get_attribute_name to get the actual namespace and name of
25220 attributes, regardless of their being GNU or C++11 attributes.
25223 attribute-token attribute-argument-clause [opt]
25227 attribute-scoped-token
25229 attribute-scoped-token:
25230 attribute-namespace :: identifier
25232 attribute-namespace:
25235 attribute-argument-clause:
25236 ( balanced-token-seq )
25238 balanced-token-seq:
25239 balanced-token [opt]
25240 balanced-token-seq balanced-token
25243 ( balanced-token-seq )
25244 [ balanced-token-seq ]
25245 { balanced-token-seq }. */
25248 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25250 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25253 temp_override
<bool> cleanup
25254 (parser
->auto_is_implicit_function_template_parm_p
, false);
25256 /* First, parse name of the attribute, a.k.a attribute-token. */
25258 token
= cp_lexer_peek_token (parser
->lexer
);
25259 if (token
->type
== CPP_NAME
)
25260 attr_id
= token
->u
.value
;
25261 else if (token
->type
== CPP_KEYWORD
)
25262 attr_id
= ridpointers
[(int) token
->keyword
];
25263 else if (token
->flags
& NAMED_OP
)
25264 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25266 if (attr_id
== NULL_TREE
)
25269 cp_lexer_consume_token (parser
->lexer
);
25271 token
= cp_lexer_peek_token (parser
->lexer
);
25272 if (token
->type
== CPP_SCOPE
)
25274 /* We are seeing a scoped attribute token. */
25276 cp_lexer_consume_token (parser
->lexer
);
25278 error_at (token
->location
, "attribute using prefix used together "
25279 "with scoped attribute token");
25282 token
= cp_lexer_consume_token (parser
->lexer
);
25283 if (token
->type
== CPP_NAME
)
25284 attr_id
= token
->u
.value
;
25285 else if (token
->type
== CPP_KEYWORD
)
25286 attr_id
= ridpointers
[(int) token
->keyword
];
25287 else if (token
->flags
& NAMED_OP
)
25288 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25291 error_at (token
->location
,
25292 "expected an identifier for the attribute name");
25293 return error_mark_node
;
25296 attr_id
= canonicalize_attr_name (attr_id
);
25297 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25299 token
= cp_lexer_peek_token (parser
->lexer
);
25302 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25306 attr_id
= canonicalize_attr_name (attr_id
);
25307 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25309 /* C++11 noreturn attribute is equivalent to GNU's. */
25310 if (is_attribute_p ("noreturn", attr_id
))
25311 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25312 /* C++14 deprecated attribute is equivalent to GNU's. */
25313 else if (is_attribute_p ("deprecated", attr_id
))
25314 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25315 /* C++17 fallthrough attribute is equivalent to GNU's. */
25316 else if (is_attribute_p ("fallthrough", attr_id
))
25317 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25318 /* Transactional Memory TS optimize_for_synchronized attribute is
25319 equivalent to GNU transaction_callable. */
25320 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25321 TREE_PURPOSE (attribute
)
25322 = get_identifier ("transaction_callable");
25323 /* Transactional Memory attributes are GNU attributes. */
25324 else if (tm_attr_to_mask (attr_id
))
25325 TREE_PURPOSE (attribute
) = attr_id
;
25328 /* Now parse the optional argument clause of the attribute. */
25330 if (token
->type
!= CPP_OPEN_PAREN
)
25334 vec
<tree
, va_gc
> *vec
;
25335 int attr_flag
= normal_attr
;
25337 if (attr_ns
== get_identifier ("gnu")
25338 && attribute_takes_identifier_p (attr_id
))
25339 /* A GNU attribute that takes an identifier in parameter. */
25340 attr_flag
= id_attr
;
25342 vec
= cp_parser_parenthesized_expression_list
25343 (parser
, attr_flag
, /*cast_p=*/false,
25344 /*allow_expansion_p=*/true,
25345 /*non_constant_p=*/NULL
);
25347 arguments
= error_mark_node
;
25350 arguments
= build_tree_list_vec (vec
);
25351 release_tree_vector (vec
);
25354 if (arguments
== error_mark_node
)
25355 attribute
= error_mark_node
;
25357 TREE_VALUE (attribute
) = arguments
;
25363 /* Check that the attribute ATTRIBUTE appears at most once in the
25364 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25365 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25366 isn't implemented yet in GCC. */
25369 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25373 tree name
= get_attribute_name (attribute
);
25374 if (is_attribute_p ("noreturn", name
)
25375 && lookup_attribute ("noreturn", attributes
))
25376 error ("attribute %<noreturn%> can appear at most once "
25377 "in an attribute-list");
25378 else if (is_attribute_p ("deprecated", name
)
25379 && lookup_attribute ("deprecated", attributes
))
25380 error ("attribute %<deprecated%> can appear at most once "
25381 "in an attribute-list");
25385 /* Parse a list of standard C++-11 attributes.
25389 attribute-list , attribute[opt]
25391 attribute-list , attribute ...
25395 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25397 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25398 cp_token
*token
= NULL
;
25402 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25403 if (attribute
== error_mark_node
)
25405 if (attribute
!= NULL_TREE
)
25407 cp_parser_check_std_attribute (attributes
, attribute
);
25408 TREE_CHAIN (attribute
) = attributes
;
25409 attributes
= attribute
;
25411 token
= cp_lexer_peek_token (parser
->lexer
);
25412 if (token
->type
== CPP_ELLIPSIS
)
25414 cp_lexer_consume_token (parser
->lexer
);
25415 if (attribute
== NULL_TREE
)
25416 error_at (token
->location
,
25417 "expected attribute before %<...%>");
25420 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25421 if (pack
== error_mark_node
)
25422 return error_mark_node
;
25423 TREE_VALUE (attribute
) = pack
;
25425 token
= cp_lexer_peek_token (parser
->lexer
);
25427 if (token
->type
!= CPP_COMMA
)
25429 cp_lexer_consume_token (parser
->lexer
);
25431 attributes
= nreverse (attributes
);
25435 /* Parse a standard C++-11 attribute specifier.
25437 attribute-specifier:
25438 [ [ attribute-using-prefix [opt] attribute-list ] ]
25439 alignment-specifier
25441 attribute-using-prefix:
25442 using attribute-namespace :
25444 alignment-specifier:
25445 alignas ( type-id ... [opt] )
25446 alignas ( alignment-expression ... [opt] ). */
25449 cp_parser_std_attribute_spec (cp_parser
*parser
)
25451 tree attributes
= NULL_TREE
;
25452 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25454 if (token
->type
== CPP_OPEN_SQUARE
25455 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25457 tree attr_ns
= NULL_TREE
;
25459 cp_lexer_consume_token (parser
->lexer
);
25460 cp_lexer_consume_token (parser
->lexer
);
25462 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25464 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25465 if (token
->type
== CPP_NAME
)
25466 attr_ns
= token
->u
.value
;
25467 else if (token
->type
== CPP_KEYWORD
)
25468 attr_ns
= ridpointers
[(int) token
->keyword
];
25469 else if (token
->flags
& NAMED_OP
)
25470 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25473 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25475 if (cxx_dialect
< cxx17
25476 && !in_system_header_at (input_location
))
25477 pedwarn (input_location
, 0,
25478 "attribute using prefix only available "
25479 "with -std=c++17 or -std=gnu++17");
25481 cp_lexer_consume_token (parser
->lexer
);
25482 cp_lexer_consume_token (parser
->lexer
);
25483 cp_lexer_consume_token (parser
->lexer
);
25486 attr_ns
= NULL_TREE
;
25489 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25491 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25492 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25493 cp_parser_skip_to_end_of_statement (parser
);
25495 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25496 when we are sure that we have actually parsed them. */
25497 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25503 /* Look for an alignment-specifier. */
25505 token
= cp_lexer_peek_token (parser
->lexer
);
25507 if (token
->type
!= CPP_KEYWORD
25508 || token
->keyword
!= RID_ALIGNAS
)
25511 cp_lexer_consume_token (parser
->lexer
);
25512 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25514 matching_parens parens
;
25515 if (!parens
.require_open (parser
))
25516 return error_mark_node
;
25518 cp_parser_parse_tentatively (parser
);
25519 alignas_expr
= cp_parser_type_id (parser
);
25521 if (!cp_parser_parse_definitely (parser
))
25523 alignas_expr
= cp_parser_assignment_expression (parser
);
25524 if (alignas_expr
== error_mark_node
)
25525 cp_parser_skip_to_end_of_statement (parser
);
25526 if (alignas_expr
== NULL_TREE
25527 || alignas_expr
== error_mark_node
)
25528 return alignas_expr
;
25531 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25532 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25534 /* Handle alignas (pack...). */
25535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25537 cp_lexer_consume_token (parser
->lexer
);
25538 alignas_expr
= make_pack_expansion (alignas_expr
);
25541 /* Something went wrong, so don't build the attribute. */
25542 if (alignas_expr
== error_mark_node
)
25543 return error_mark_node
;
25545 if (!parens
.require_close (parser
))
25546 return error_mark_node
;
25548 /* Build the C++-11 representation of an 'aligned'
25551 build_tree_list (build_tree_list (get_identifier ("gnu"),
25552 get_identifier ("aligned")),
25559 /* Parse a standard C++-11 attribute-specifier-seq.
25561 attribute-specifier-seq:
25562 attribute-specifier-seq [opt] attribute-specifier
25566 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25568 tree attr_specs
= NULL_TREE
;
25569 tree attr_last
= NULL_TREE
;
25573 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25574 if (attr_spec
== NULL_TREE
)
25576 if (attr_spec
== error_mark_node
)
25577 return error_mark_node
;
25580 TREE_CHAIN (attr_last
) = attr_spec
;
25582 attr_specs
= attr_last
= attr_spec
;
25583 attr_last
= tree_last (attr_last
);
25589 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25590 return index of the first token after balanced-token, or N on failure. */
25593 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25596 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25598 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25601 case CPP_PRAGMA_EOL
:
25602 /* Ran out of tokens. */
25604 case CPP_OPEN_PAREN
:
25607 case CPP_OPEN_BRACE
:
25610 case CPP_OPEN_SQUARE
:
25613 case CPP_CLOSE_PAREN
:
25616 case CPP_CLOSE_BRACE
:
25619 case CPP_CLOSE_SQUARE
:
25625 while (nparens
|| nbraces
|| nsquares
);
25629 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25630 return index of the first token after the GNU attribute tokens, or N on
25634 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25638 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25639 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25640 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25643 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25646 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25653 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25654 next token), return index of the first token after the standard C++11
25655 attribute tokens, or N on failure. */
25658 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25662 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25663 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25665 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25668 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25672 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25673 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25675 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25686 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25687 as the next token), return index of the first token after the attribute
25688 tokens, or N on failure. */
25691 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25693 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25694 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25695 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25698 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25699 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25700 current value of the PEDANTIC flag, regardless of whether or not
25701 the `__extension__' keyword is present. The caller is responsible
25702 for restoring the value of the PEDANTIC flag. */
25705 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25707 /* Save the old value of the PEDANTIC flag. */
25708 *saved_pedantic
= pedantic
;
25710 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25712 /* Consume the `__extension__' token. */
25713 cp_lexer_consume_token (parser
->lexer
);
25714 /* We're not being pedantic while the `__extension__' keyword is
25724 /* Parse a label declaration.
25727 __label__ label-declarator-seq ;
25729 label-declarator-seq:
25730 identifier , label-declarator-seq
25734 cp_parser_label_declaration (cp_parser
* parser
)
25736 /* Look for the `__label__' keyword. */
25737 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25743 /* Look for an identifier. */
25744 identifier
= cp_parser_identifier (parser
);
25745 /* If we failed, stop. */
25746 if (identifier
== error_mark_node
)
25748 /* Declare it as a label. */
25749 finish_label_decl (identifier
);
25750 /* If the next token is a `;', stop. */
25751 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25753 /* Look for the `,' separating the label declarations. */
25754 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25757 /* Look for the final `;'. */
25758 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25761 // -------------------------------------------------------------------------- //
25764 // Parse a requires clause.
25766 // requires-clause:
25767 // 'requires' logical-or-expression
25769 // The required logical-or-expression must be a constant expression. Note
25770 // that we don't check that the expression is constepxr here. We defer until
25771 // we analyze constraints and then, we only check atomic constraints.
25773 cp_parser_requires_clause (cp_parser
*parser
)
25775 // Parse the requires clause so that it is not automatically folded.
25776 ++processing_template_decl
;
25777 tree expr
= cp_parser_binary_expression (parser
, false, false,
25778 PREC_NOT_OPERATOR
, NULL
);
25779 if (check_for_bare_parameter_packs (expr
))
25780 expr
= error_mark_node
;
25781 --processing_template_decl
;
25785 // Optionally parse a requires clause:
25787 cp_parser_requires_clause_opt (cp_parser
*parser
)
25789 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25790 if (tok
->keyword
!= RID_REQUIRES
)
25792 if (!flag_concepts
&& tok
->type
== CPP_NAME
25793 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25795 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25796 "%<requires%> only available with -fconcepts");
25797 /* Parse and discard the requires-clause. */
25798 cp_lexer_consume_token (parser
->lexer
);
25799 cp_parser_requires_clause (parser
);
25803 cp_lexer_consume_token (parser
->lexer
);
25804 return cp_parser_requires_clause (parser
);
25808 /*---------------------------------------------------------------------------
25809 Requires expressions
25810 ---------------------------------------------------------------------------*/
25812 /* Parse a requires expression
25814 requirement-expression:
25815 'requires' requirement-parameter-list [opt] requirement-body */
25817 cp_parser_requires_expression (cp_parser
*parser
)
25819 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25820 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25822 /* A requires-expression shall appear only within a concept
25823 definition or a requires-clause.
25825 TODO: Implement this diagnostic correctly. */
25826 if (!processing_template_decl
)
25828 error_at (loc
, "a requires expression cannot appear outside a template");
25829 cp_parser_skip_to_end_of_statement (parser
);
25830 return error_mark_node
;
25835 /* Local parameters are delared as variables within the scope
25836 of the expression. They are not visible past the end of
25837 the expression. Expressions within the requires-expression
25838 are unevaluated. */
25839 struct scope_sentinel
25843 ++cp_unevaluated_operand
;
25844 begin_scope (sk_block
, NULL_TREE
);
25849 pop_bindings_and_leave_scope ();
25850 --cp_unevaluated_operand
;
25854 /* Parse the optional parameter list. */
25855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25857 parms
= cp_parser_requirement_parameter_list (parser
);
25858 if (parms
== error_mark_node
)
25859 return error_mark_node
;
25864 /* Parse the requirement body. */
25865 reqs
= cp_parser_requirement_body (parser
);
25866 if (reqs
== error_mark_node
)
25867 return error_mark_node
;
25870 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25872 grokparms (parms
, &parms
);
25873 return finish_requires_expr (parms
, reqs
);
25876 /* Parse a parameterized requirement.
25878 requirement-parameter-list:
25879 '(' parameter-declaration-clause ')' */
25881 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25883 matching_parens parens
;
25884 if (!parens
.require_open (parser
))
25885 return error_mark_node
;
25887 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25889 if (!parens
.require_close (parser
))
25890 return error_mark_node
;
25895 /* Parse the body of a requirement.
25898 '{' requirement-list '}' */
25900 cp_parser_requirement_body (cp_parser
*parser
)
25902 matching_braces braces
;
25903 if (!braces
.require_open (parser
))
25904 return error_mark_node
;
25906 tree reqs
= cp_parser_requirement_list (parser
);
25908 if (!braces
.require_close (parser
))
25909 return error_mark_node
;
25914 /* Parse a list of requirements.
25918 requirement-list ';' requirement[opt] */
25920 cp_parser_requirement_list (cp_parser
*parser
)
25922 tree result
= NULL_TREE
;
25925 tree req
= cp_parser_requirement (parser
);
25926 if (req
== error_mark_node
)
25927 return error_mark_node
;
25929 result
= tree_cons (NULL_TREE
, req
, result
);
25931 /* If we see a semi-colon, consume it. */
25932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25933 cp_lexer_consume_token (parser
->lexer
);
25935 /* Stop processing at the end of the list. */
25936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25940 /* Reverse the order of requirements so they are analyzed in
25941 declaration order. */
25942 return nreverse (result
);
25945 /* Parse a syntactic requirement or type requirement.
25949 compound-requirement
25951 nested-requirement */
25953 cp_parser_requirement (cp_parser
*parser
)
25955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25956 return cp_parser_compound_requirement (parser
);
25957 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25958 return cp_parser_type_requirement (parser
);
25959 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25960 return cp_parser_nested_requirement (parser
);
25962 return cp_parser_simple_requirement (parser
);
25965 /* Parse a simple requirement.
25967 simple-requirement:
25970 cp_parser_simple_requirement (cp_parser
*parser
)
25972 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25973 if (!expr
|| expr
== error_mark_node
)
25974 return error_mark_node
;
25976 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25977 return error_mark_node
;
25979 return finish_simple_requirement (expr
);
25982 /* Parse a type requirement
25985 nested-name-specifier [opt] required-type-name ';'
25987 required-type-name:
25989 'template' [opt] simple-template-id */
25991 cp_parser_type_requirement (cp_parser
*parser
)
25993 cp_lexer_consume_token (parser
->lexer
);
25995 // Save the scope before parsing name specifiers.
25996 tree saved_scope
= parser
->scope
;
25997 tree saved_object_scope
= parser
->object_scope
;
25998 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25999 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26000 cp_parser_nested_name_specifier_opt (parser
,
26001 /*typename_keyword_p=*/true,
26002 /*check_dependency_p=*/false,
26004 /*is_declaration=*/false);
26007 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26009 cp_lexer_consume_token (parser
->lexer
);
26010 type
= cp_parser_template_id (parser
,
26011 /*template_keyword_p=*/true,
26012 /*check_dependency=*/false,
26013 /*tag_type=*/none_type
,
26014 /*is_declaration=*/false);
26015 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26016 /*complain=*/tf_error
);
26019 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26021 if (TREE_CODE (type
) == TYPE_DECL
)
26022 type
= TREE_TYPE (type
);
26024 parser
->scope
= saved_scope
;
26025 parser
->object_scope
= saved_object_scope
;
26026 parser
->qualifying_scope
= saved_qualifying_scope
;
26028 if (type
== error_mark_node
)
26029 cp_parser_skip_to_end_of_statement (parser
);
26031 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26032 return error_mark_node
;
26033 if (type
== error_mark_node
)
26034 return error_mark_node
;
26036 return finish_type_requirement (type
);
26039 /* Parse a compound requirement
26041 compound-requirement:
26042 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26044 cp_parser_compound_requirement (cp_parser
*parser
)
26046 /* Parse an expression enclosed in '{ }'s. */
26047 matching_braces braces
;
26048 if (!braces
.require_open (parser
))
26049 return error_mark_node
;
26051 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26052 if (!expr
|| expr
== error_mark_node
)
26053 return error_mark_node
;
26055 if (!braces
.require_close (parser
))
26056 return error_mark_node
;
26058 /* Parse the optional noexcept. */
26059 bool noexcept_p
= false;
26060 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26062 cp_lexer_consume_token (parser
->lexer
);
26066 /* Parse the optional trailing return type. */
26067 tree type
= NULL_TREE
;
26068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26070 cp_lexer_consume_token (parser
->lexer
);
26071 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26072 parser
->in_result_type_constraint_p
= true;
26073 type
= cp_parser_trailing_type_id (parser
);
26074 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26075 if (type
== error_mark_node
)
26076 return error_mark_node
;
26079 return finish_compound_requirement (expr
, type
, noexcept_p
);
26082 /* Parse a nested requirement. This is the same as a requires clause.
26084 nested-requirement:
26087 cp_parser_nested_requirement (cp_parser
*parser
)
26089 cp_lexer_consume_token (parser
->lexer
);
26090 tree req
= cp_parser_requires_clause (parser
);
26091 if (req
== error_mark_node
)
26092 return error_mark_node
;
26093 return finish_nested_requirement (req
);
26096 /* Support Functions */
26098 /* Return the appropriate prefer_type argument for lookup_name_real based on
26099 tag_type and template_mem_access. */
26102 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26104 /* DR 141: When looking in the current enclosing context for a template-name
26105 after -> or ., only consider class templates. */
26106 if (template_mem_access
)
26110 case none_type
: return 0; // No preference.
26111 case scope_type
: return 1; // Type or namespace.
26112 default: return 2; // Type only.
26116 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26117 NAME should have one of the representations used for an
26118 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26119 is returned. If PARSER->SCOPE is a dependent type, then a
26120 SCOPE_REF is returned.
26122 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26123 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26124 was formed. Abstractly, such entities should not be passed to this
26125 function, because they do not need to be looked up, but it is
26126 simpler to check for this special case here, rather than at the
26129 In cases not explicitly covered above, this function returns a
26130 DECL, OVERLOAD, or baselink representing the result of the lookup.
26131 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26134 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26135 (e.g., "struct") that was used. In that case bindings that do not
26136 refer to types are ignored.
26138 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26141 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26144 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26147 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26148 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26149 NULL_TREE otherwise. */
26152 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26153 enum tag_types tag_type
,
26156 bool check_dependency
,
26157 tree
*ambiguous_decls
,
26158 location_t name_location
)
26161 tree object_type
= parser
->context
->object_type
;
26163 /* Assume that the lookup will be unambiguous. */
26164 if (ambiguous_decls
)
26165 *ambiguous_decls
= NULL_TREE
;
26167 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26168 no longer valid. Note that if we are parsing tentatively, and
26169 the parse fails, OBJECT_TYPE will be automatically restored. */
26170 parser
->context
->object_type
= NULL_TREE
;
26172 if (name
== error_mark_node
)
26173 return error_mark_node
;
26175 /* A template-id has already been resolved; there is no lookup to
26177 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26179 if (BASELINK_P (name
))
26181 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26182 == TEMPLATE_ID_EXPR
);
26186 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26187 it should already have been checked to make sure that the name
26188 used matches the type being destroyed. */
26189 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26193 /* Figure out to which type this destructor applies. */
26195 type
= parser
->scope
;
26196 else if (object_type
)
26197 type
= object_type
;
26199 type
= current_class_type
;
26200 /* If that's not a class type, there is no destructor. */
26201 if (!type
|| !CLASS_TYPE_P (type
))
26202 return error_mark_node
;
26204 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26205 lazily_declare_fn (sfk_destructor
, type
);
26207 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26210 return error_mark_node
;
26213 /* By this point, the NAME should be an ordinary identifier. If
26214 the id-expression was a qualified name, the qualifying scope is
26215 stored in PARSER->SCOPE at this point. */
26216 gcc_assert (identifier_p (name
));
26218 /* Perform the lookup. */
26223 if (parser
->scope
== error_mark_node
)
26224 return error_mark_node
;
26226 /* If the SCOPE is dependent, the lookup must be deferred until
26227 the template is instantiated -- unless we are explicitly
26228 looking up names in uninstantiated templates. Even then, we
26229 cannot look up the name if the scope is not a class type; it
26230 might, for example, be a template type parameter. */
26231 dependent_p
= (TYPE_P (parser
->scope
)
26232 && dependent_scope_p (parser
->scope
));
26233 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26235 /* Defer lookup. */
26236 decl
= error_mark_node
;
26239 tree pushed_scope
= NULL_TREE
;
26241 /* If PARSER->SCOPE is a dependent type, then it must be a
26242 class type, and we must not be checking dependencies;
26243 otherwise, we would have processed this lookup above. So
26244 that PARSER->SCOPE is not considered a dependent base by
26245 lookup_member, we must enter the scope here. */
26247 pushed_scope
= push_scope (parser
->scope
);
26249 /* If the PARSER->SCOPE is a template specialization, it
26250 may be instantiated during name lookup. In that case,
26251 errors may be issued. Even if we rollback the current
26252 tentative parse, those errors are valid. */
26253 decl
= lookup_qualified_name (parser
->scope
, name
,
26254 prefer_type_arg (tag_type
),
26255 /*complain=*/true);
26257 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26258 lookup result and the nested-name-specifier nominates a class C:
26259 * if the name specified after the nested-name-specifier, when
26260 looked up in C, is the injected-class-name of C (Clause 9), or
26261 * if the name specified after the nested-name-specifier is the
26262 same as the identifier or the simple-template-id's template-
26263 name in the last component of the nested-name-specifier,
26264 the name is instead considered to name the constructor of
26265 class C. [ Note: for example, the constructor is not an
26266 acceptable lookup result in an elaborated-type-specifier so
26267 the constructor would not be used in place of the
26268 injected-class-name. --end note ] Such a constructor name
26269 shall be used only in the declarator-id of a declaration that
26270 names a constructor or in a using-declaration. */
26271 if (tag_type
== none_type
26272 && DECL_SELF_REFERENCE_P (decl
)
26273 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26274 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26275 prefer_type_arg (tag_type
),
26276 /*complain=*/true);
26278 /* If we have a single function from a using decl, pull it out. */
26279 if (TREE_CODE (decl
) == OVERLOAD
26280 && !really_overloaded_fn (decl
))
26281 decl
= OVL_FUNCTION (decl
);
26284 pop_scope (pushed_scope
);
26287 /* If the scope is a dependent type and either we deferred lookup or
26288 we did lookup but didn't find the name, rememeber the name. */
26289 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26290 && dependent_type_p (parser
->scope
))
26296 /* The resolution to Core Issue 180 says that `struct
26297 A::B' should be considered a type-name, even if `A'
26299 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26300 /*complain=*/tf_error
);
26301 if (type
!= error_mark_node
)
26302 decl
= TYPE_NAME (type
);
26304 else if (is_template
26305 && (cp_parser_next_token_ends_template_argument_p (parser
)
26306 || cp_lexer_next_token_is (parser
->lexer
,
26308 decl
= make_unbound_class_template (parser
->scope
,
26310 /*complain=*/tf_error
);
26312 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26313 parser
->scope
, name
,
26316 parser
->qualifying_scope
= parser
->scope
;
26317 parser
->object_scope
= NULL_TREE
;
26319 else if (object_type
)
26321 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26322 OBJECT_TYPE is not a class. */
26323 if (CLASS_TYPE_P (object_type
))
26324 /* If the OBJECT_TYPE is a template specialization, it may
26325 be instantiated during name lookup. In that case, errors
26326 may be issued. Even if we rollback the current tentative
26327 parse, those errors are valid. */
26328 decl
= lookup_member (object_type
,
26331 prefer_type_arg (tag_type
),
26332 tf_warning_or_error
);
26337 /* Look it up in the enclosing context. DR 141: When looking for a
26338 template-name after -> or ., only consider class templates. */
26339 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26341 /*block_p=*/true, is_namespace
, 0);
26342 if (object_type
== unknown_type_node
)
26343 /* The object is type-dependent, so we can't look anything up; we used
26344 this to get the DR 141 behavior. */
26345 object_type
= NULL_TREE
;
26346 parser
->object_scope
= object_type
;
26347 parser
->qualifying_scope
= NULL_TREE
;
26351 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26353 /*block_p=*/true, is_namespace
, 0);
26354 parser
->qualifying_scope
= NULL_TREE
;
26355 parser
->object_scope
= NULL_TREE
;
26358 /* If the lookup failed, let our caller know. */
26359 if (!decl
|| decl
== error_mark_node
)
26360 return error_mark_node
;
26362 /* Pull out the template from an injected-class-name (or multiple). */
26364 decl
= maybe_get_template_decl_from_type_decl (decl
);
26366 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26367 if (TREE_CODE (decl
) == TREE_LIST
)
26369 if (ambiguous_decls
)
26370 *ambiguous_decls
= decl
;
26371 /* The error message we have to print is too complicated for
26372 cp_parser_error, so we incorporate its actions directly. */
26373 if (!cp_parser_simulate_error (parser
))
26375 error_at (name_location
, "reference to %qD is ambiguous",
26377 print_candidates (decl
);
26379 return error_mark_node
;
26382 gcc_assert (DECL_P (decl
)
26383 || TREE_CODE (decl
) == OVERLOAD
26384 || TREE_CODE (decl
) == SCOPE_REF
26385 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26386 || BASELINK_P (decl
));
26388 /* If we have resolved the name of a member declaration, check to
26389 see if the declaration is accessible. When the name resolves to
26390 set of overloaded functions, accessibility is checked when
26391 overload resolution is done.
26393 During an explicit instantiation, access is not checked at all,
26394 as per [temp.explicit]. */
26396 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26398 maybe_record_typedef_use (decl
);
26400 return cp_expr (decl
, name_location
);
26403 /* Like cp_parser_lookup_name, but for use in the typical case where
26404 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26405 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26408 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26410 return cp_parser_lookup_name (parser
, name
,
26412 /*is_template=*/false,
26413 /*is_namespace=*/false,
26414 /*check_dependency=*/true,
26415 /*ambiguous_decls=*/NULL
,
26419 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26420 the current context, return the TYPE_DECL. If TAG_NAME_P is
26421 true, the DECL indicates the class being defined in a class-head,
26422 or declared in an elaborated-type-specifier.
26424 Otherwise, return DECL. */
26427 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26429 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26430 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26433 template <typename T> struct B;
26436 template <typename T> struct A::B {};
26438 Similarly, in an elaborated-type-specifier:
26440 namespace N { struct X{}; }
26443 template <typename T> friend struct N::X;
26446 However, if the DECL refers to a class type, and we are in
26447 the scope of the class, then the name lookup automatically
26448 finds the TYPE_DECL created by build_self_reference rather
26449 than a TEMPLATE_DECL. For example, in:
26451 template <class T> struct S {
26455 there is no need to handle such case. */
26457 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26458 return DECL_TEMPLATE_RESULT (decl
);
26463 /* If too many, or too few, template-parameter lists apply to the
26464 declarator, issue an error message. Returns TRUE if all went well,
26465 and FALSE otherwise. */
26468 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26469 cp_declarator
*declarator
,
26470 location_t declarator_location
)
26472 switch (declarator
->kind
)
26476 unsigned num_templates
= 0;
26477 tree scope
= declarator
->u
.id
.qualifying_scope
;
26478 bool template_id_p
= false;
26481 num_templates
= num_template_headers_for_class (scope
);
26482 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26483 == TEMPLATE_ID_EXPR
)
26485 /* If the DECLARATOR has the form `X<y>' then it uses one
26486 additional level of template parameters. */
26488 template_id_p
= true;
26491 return cp_parser_check_template_parameters
26492 (parser
, num_templates
, template_id_p
, declarator_location
,
26499 case cdk_reference
:
26501 return (cp_parser_check_declarator_template_parameters
26502 (parser
, declarator
->declarator
, declarator_location
));
26509 gcc_unreachable ();
26514 /* NUM_TEMPLATES were used in the current declaration. If that is
26515 invalid, return FALSE and issue an error messages. Otherwise,
26516 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26517 declarator and we can print more accurate diagnostics. */
26520 cp_parser_check_template_parameters (cp_parser
* parser
,
26521 unsigned num_templates
,
26522 bool template_id_p
,
26523 location_t location
,
26524 cp_declarator
*declarator
)
26526 /* If there are the same number of template classes and parameter
26527 lists, that's OK. */
26528 if (parser
->num_template_parameter_lists
== num_templates
)
26530 /* If there are more, but only one more, and the name ends in an identifier,
26531 then we are declaring a primary template. That's OK too. */
26533 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26535 /* If there are more template classes than parameter lists, we have
26538 template <class T> void S<T>::R<T>::f (); */
26539 if (parser
->num_template_parameter_lists
< num_templates
)
26541 if (declarator
&& !current_function_decl
)
26542 error_at (location
, "specializing member %<%T::%E%> "
26543 "requires %<template<>%> syntax",
26544 declarator
->u
.id
.qualifying_scope
,
26545 declarator
->u
.id
.unqualified_name
);
26546 else if (declarator
)
26547 error_at (location
, "invalid declaration of %<%T::%E%>",
26548 declarator
->u
.id
.qualifying_scope
,
26549 declarator
->u
.id
.unqualified_name
);
26551 error_at (location
, "too few template-parameter-lists");
26554 /* Otherwise, there are too many template parameter lists. We have
26557 template <class T> template <class U> void S::f(); */
26558 error_at (location
, "too many template-parameter-lists");
26562 /* Parse an optional `::' token indicating that the following name is
26563 from the global namespace. If so, PARSER->SCOPE is set to the
26564 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26565 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26566 Returns the new value of PARSER->SCOPE, if the `::' token is
26567 present, and NULL_TREE otherwise. */
26570 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26574 /* Peek at the next token. */
26575 token
= cp_lexer_peek_token (parser
->lexer
);
26576 /* If we're looking at a `::' token then we're starting from the
26577 global namespace, not our current location. */
26578 if (token
->type
== CPP_SCOPE
)
26580 /* Consume the `::' token. */
26581 cp_lexer_consume_token (parser
->lexer
);
26582 /* Set the SCOPE so that we know where to start the lookup. */
26583 parser
->scope
= global_namespace
;
26584 parser
->qualifying_scope
= global_namespace
;
26585 parser
->object_scope
= NULL_TREE
;
26587 return parser
->scope
;
26589 else if (!current_scope_valid_p
)
26591 parser
->scope
= NULL_TREE
;
26592 parser
->qualifying_scope
= NULL_TREE
;
26593 parser
->object_scope
= NULL_TREE
;
26599 /* Returns TRUE if the upcoming token sequence is the start of a
26600 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26601 declarator is preceded by the `friend' specifier. */
26604 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26606 bool constructor_p
;
26607 bool outside_class_specifier_p
;
26608 tree nested_name_specifier
;
26609 cp_token
*next_token
;
26611 /* The common case is that this is not a constructor declarator, so
26612 try to avoid doing lots of work if at all possible. It's not
26613 valid declare a constructor at function scope. */
26614 if (parser
->in_function_body
)
26616 /* And only certain tokens can begin a constructor declarator. */
26617 next_token
= cp_lexer_peek_token (parser
->lexer
);
26618 if (next_token
->type
!= CPP_NAME
26619 && next_token
->type
!= CPP_SCOPE
26620 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26621 && next_token
->type
!= CPP_TEMPLATE_ID
)
26624 /* Parse tentatively; we are going to roll back all of the tokens
26626 cp_parser_parse_tentatively (parser
);
26627 /* Assume that we are looking at a constructor declarator. */
26628 constructor_p
= true;
26630 /* Look for the optional `::' operator. */
26631 cp_parser_global_scope_opt (parser
,
26632 /*current_scope_valid_p=*/false);
26633 /* Look for the nested-name-specifier. */
26634 nested_name_specifier
26635 = (cp_parser_nested_name_specifier_opt (parser
,
26636 /*typename_keyword_p=*/false,
26637 /*check_dependency_p=*/false,
26639 /*is_declaration=*/false));
26641 outside_class_specifier_p
= (!at_class_scope_p ()
26642 || !TYPE_BEING_DEFINED (current_class_type
)
26645 /* Outside of a class-specifier, there must be a
26646 nested-name-specifier. Except in C++17 mode, where we
26647 might be declaring a guiding declaration. */
26648 if (!nested_name_specifier
&& outside_class_specifier_p
26649 && cxx_dialect
< cxx17
)
26650 constructor_p
= false;
26651 else if (nested_name_specifier
== error_mark_node
)
26652 constructor_p
= false;
26654 /* If we have a class scope, this is easy; DR 147 says that S::S always
26655 names the constructor, and no other qualified name could. */
26656 if (constructor_p
&& nested_name_specifier
26657 && CLASS_TYPE_P (nested_name_specifier
))
26659 tree id
= cp_parser_unqualified_id (parser
,
26660 /*template_keyword_p=*/false,
26661 /*check_dependency_p=*/false,
26662 /*declarator_p=*/true,
26663 /*optional_p=*/false);
26664 if (is_overloaded_fn (id
))
26665 id
= DECL_NAME (get_first_fn (id
));
26666 if (!constructor_name_p (id
, nested_name_specifier
))
26667 constructor_p
= false;
26669 /* If we still think that this might be a constructor-declarator,
26670 look for a class-name. */
26671 else if (constructor_p
)
26675 template <typename T> struct S {
26679 we must recognize that the nested `S' names a class. */
26680 if (cxx_dialect
>= cxx17
)
26681 cp_parser_parse_tentatively (parser
);
26684 type_decl
= cp_parser_class_name (parser
,
26685 /*typename_keyword_p=*/false,
26686 /*template_keyword_p=*/false,
26688 /*check_dependency_p=*/false,
26689 /*class_head_p=*/false,
26690 /*is_declaration=*/false);
26692 if (cxx_dialect
>= cxx17
26693 && !cp_parser_parse_definitely (parser
))
26695 type_decl
= NULL_TREE
;
26696 tree tmpl
= cp_parser_template_name (parser
,
26697 /*template_keyword*/false,
26698 /*check_dependency_p*/false,
26699 /*is_declaration*/false,
26701 /*is_identifier*/NULL
);
26702 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26703 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26704 /* It's a deduction guide, return true. */;
26706 cp_parser_simulate_error (parser
);
26709 /* If there was no class-name, then this is not a constructor.
26710 Otherwise, if we are in a class-specifier and we aren't
26711 handling a friend declaration, check that its type matches
26712 current_class_type (c++/38313). Note: error_mark_node
26713 is left alone for error recovery purposes. */
26714 constructor_p
= (!cp_parser_error_occurred (parser
)
26715 && (outside_class_specifier_p
26716 || type_decl
== NULL_TREE
26717 || type_decl
== error_mark_node
26718 || same_type_p (current_class_type
,
26719 TREE_TYPE (type_decl
))));
26721 /* If we're still considering a constructor, we have to see a `(',
26722 to begin the parameter-declaration-clause, followed by either a
26723 `)', an `...', or a decl-specifier. We need to check for a
26724 type-specifier to avoid being fooled into thinking that:
26728 is a constructor. (It is actually a function named `f' that
26729 takes one parameter (of type `int') and returns a value of type
26732 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26733 constructor_p
= false;
26736 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26737 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26738 /* A parameter declaration begins with a decl-specifier,
26739 which is either the "attribute" keyword, a storage class
26740 specifier, or (usually) a type-specifier. */
26741 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26744 tree pushed_scope
= NULL_TREE
;
26745 unsigned saved_num_template_parameter_lists
;
26747 /* Names appearing in the type-specifier should be looked up
26748 in the scope of the class. */
26749 if (current_class_type
)
26751 else if (type_decl
)
26753 type
= TREE_TYPE (type_decl
);
26754 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26756 type
= resolve_typename_type (type
,
26757 /*only_current_p=*/false);
26758 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26760 cp_parser_abort_tentative_parse (parser
);
26764 pushed_scope
= push_scope (type
);
26767 /* Inside the constructor parameter list, surrounding
26768 template-parameter-lists do not apply. */
26769 saved_num_template_parameter_lists
26770 = parser
->num_template_parameter_lists
;
26771 parser
->num_template_parameter_lists
= 0;
26773 /* Look for the type-specifier. */
26774 cp_parser_type_specifier (parser
,
26775 CP_PARSER_FLAGS_NONE
,
26776 /*decl_specs=*/NULL
,
26777 /*is_declarator=*/true,
26778 /*declares_class_or_enum=*/NULL
,
26779 /*is_cv_qualifier=*/NULL
);
26781 parser
->num_template_parameter_lists
26782 = saved_num_template_parameter_lists
;
26784 /* Leave the scope of the class. */
26786 pop_scope (pushed_scope
);
26788 constructor_p
= !cp_parser_error_occurred (parser
);
26792 /* We did not really want to consume any tokens. */
26793 cp_parser_abort_tentative_parse (parser
);
26795 return constructor_p
;
26798 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26799 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26800 they must be performed once we are in the scope of the function.
26802 Returns the function defined. */
26805 cp_parser_function_definition_from_specifiers_and_declarator
26806 (cp_parser
* parser
,
26807 cp_decl_specifier_seq
*decl_specifiers
,
26809 const cp_declarator
*declarator
)
26814 /* Begin the function-definition. */
26815 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26817 /* The things we're about to see are not directly qualified by any
26818 template headers we've seen thus far. */
26819 reset_specialization ();
26821 /* If there were names looked up in the decl-specifier-seq that we
26822 did not check, check them now. We must wait until we are in the
26823 scope of the function to perform the checks, since the function
26824 might be a friend. */
26825 perform_deferred_access_checks (tf_warning_or_error
);
26829 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26830 parser
->omp_declare_simd
= NULL
;
26831 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26832 parser
->oacc_routine
= NULL
;
26837 /* Skip the entire function. */
26838 cp_parser_skip_to_end_of_block_or_statement (parser
);
26839 fn
= error_mark_node
;
26841 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26843 /* Seen already, skip it. An error message has already been output. */
26844 cp_parser_skip_to_end_of_block_or_statement (parser
);
26845 fn
= current_function_decl
;
26846 current_function_decl
= NULL_TREE
;
26847 /* If this is a function from a class, pop the nested class. */
26848 if (current_class_name
)
26849 pop_nested_class ();
26854 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26855 tv
= TV_PARSE_INLINE
;
26857 tv
= TV_PARSE_FUNC
;
26859 fn
= cp_parser_function_definition_after_declarator (parser
,
26860 /*inline_p=*/false);
26867 /* Parse the part of a function-definition that follows the
26868 declarator. INLINE_P is TRUE iff this function is an inline
26869 function defined within a class-specifier.
26871 Returns the function defined. */
26874 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26878 bool saved_in_unbraced_linkage_specification_p
;
26879 bool saved_in_function_body
;
26880 unsigned saved_num_template_parameter_lists
;
26882 bool fully_implicit_function_template_p
26883 = parser
->fully_implicit_function_template_p
;
26884 parser
->fully_implicit_function_template_p
= false;
26885 tree implicit_template_parms
26886 = parser
->implicit_template_parms
;
26887 parser
->implicit_template_parms
= 0;
26888 cp_binding_level
* implicit_template_scope
26889 = parser
->implicit_template_scope
;
26890 parser
->implicit_template_scope
= 0;
26892 saved_in_function_body
= parser
->in_function_body
;
26893 parser
->in_function_body
= true;
26894 /* If the next token is `return', then the code may be trying to
26895 make use of the "named return value" extension that G++ used to
26897 token
= cp_lexer_peek_token (parser
->lexer
);
26898 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26900 /* Consume the `return' keyword. */
26901 cp_lexer_consume_token (parser
->lexer
);
26902 /* Look for the identifier that indicates what value is to be
26904 cp_parser_identifier (parser
);
26905 /* Issue an error message. */
26906 error_at (token
->location
,
26907 "named return values are no longer supported");
26908 /* Skip tokens until we reach the start of the function body. */
26911 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26912 if (token
->type
== CPP_OPEN_BRACE
26913 || token
->type
== CPP_EOF
26914 || token
->type
== CPP_PRAGMA_EOL
)
26916 cp_lexer_consume_token (parser
->lexer
);
26919 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26920 anything declared inside `f'. */
26921 saved_in_unbraced_linkage_specification_p
26922 = parser
->in_unbraced_linkage_specification_p
;
26923 parser
->in_unbraced_linkage_specification_p
= false;
26924 /* Inside the function, surrounding template-parameter-lists do not
26926 saved_num_template_parameter_lists
26927 = parser
->num_template_parameter_lists
;
26928 parser
->num_template_parameter_lists
= 0;
26930 /* If the next token is `try', `__transaction_atomic', or
26931 `__transaction_relaxed`, then we are looking at either function-try-block
26932 or function-transaction-block. Note that all of these include the
26934 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26935 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26936 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26937 RID_TRANSACTION_RELAXED
))
26938 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26939 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26940 cp_parser_function_try_block (parser
);
26942 cp_parser_ctor_initializer_opt_and_function_body
26943 (parser
, /*in_function_try_block=*/false);
26945 /* Finish the function. */
26946 fn
= finish_function (inline_p
);
26947 /* Generate code for it, if necessary. */
26948 expand_or_defer_fn (fn
);
26949 /* Restore the saved values. */
26950 parser
->in_unbraced_linkage_specification_p
26951 = saved_in_unbraced_linkage_specification_p
;
26952 parser
->num_template_parameter_lists
26953 = saved_num_template_parameter_lists
;
26954 parser
->in_function_body
= saved_in_function_body
;
26956 parser
->fully_implicit_function_template_p
26957 = fully_implicit_function_template_p
;
26958 parser
->implicit_template_parms
26959 = implicit_template_parms
;
26960 parser
->implicit_template_scope
26961 = implicit_template_scope
;
26963 if (parser
->fully_implicit_function_template_p
)
26964 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26969 /* Parse a template-declaration body (following argument list). */
26972 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26973 tree parameter_list
,
26976 tree decl
= NULL_TREE
;
26977 bool friend_p
= false;
26979 /* We just processed one more parameter list. */
26980 ++parser
->num_template_parameter_lists
;
26982 /* Get the deferred access checks from the parameter list. These
26983 will be checked once we know what is being declared, as for a
26984 member template the checks must be performed in the scope of the
26985 class containing the member. */
26986 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26988 /* Tentatively parse for a new template parameter list, which can either be
26989 the template keyword or a template introduction. */
26990 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26992 else if (cxx_dialect
>= cxx11
26993 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26994 decl
= cp_parser_alias_declaration (parser
);
26997 /* There are no access checks when parsing a template, as we do not
26998 know if a specialization will be a friend. */
26999 push_deferring_access_checks (dk_no_check
);
27000 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27001 decl
= cp_parser_single_declaration (parser
,
27004 /*explicit_specialization_p=*/false,
27006 pop_deferring_access_checks ();
27008 /* If this is a member template declaration, let the front
27010 if (member_p
&& !friend_p
&& decl
)
27012 if (TREE_CODE (decl
) == TYPE_DECL
)
27013 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27015 decl
= finish_member_template_decl (decl
);
27017 else if (friend_p
&& decl
27018 && DECL_DECLARES_TYPE_P (decl
))
27019 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27020 /*complain=*/true);
27022 /* We are done with the current parameter list. */
27023 --parser
->num_template_parameter_lists
;
27025 pop_deferring_access_checks ();
27028 finish_template_decl (parameter_list
);
27030 /* Check the template arguments for a literal operator template. */
27032 && DECL_DECLARES_FUNCTION_P (decl
)
27033 && UDLIT_OPER_P (DECL_NAME (decl
)))
27036 if (parameter_list
== NULL_TREE
)
27040 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27041 if (num_parms
== 1)
27043 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27044 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27045 if (TREE_TYPE (parm
) != char_type_node
27046 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27049 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27051 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27052 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27053 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27054 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27055 if (parm
== error_mark_node
27056 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27057 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27065 if (cxx_dialect
>= cxx14
)
27066 error ("literal operator template %qD has invalid parameter list."
27067 " Expected non-type template argument pack <char...>"
27068 " or <typename CharT, CharT...>",
27071 error ("literal operator template %qD has invalid parameter list."
27072 " Expected non-type template argument pack <char...>",
27077 /* Register member declarations. */
27078 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27079 finish_member_declaration (decl
);
27080 /* If DECL is a function template, we must return to parse it later.
27081 (Even though there is no definition, there might be default
27082 arguments that need handling.) */
27083 if (member_p
&& decl
27084 && DECL_DECLARES_FUNCTION_P (decl
))
27085 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27088 /* Parse a template introduction header for a template-declaration. Returns
27089 false if tentative parse fails. */
27092 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27094 cp_parser_parse_tentatively (parser
);
27096 tree saved_scope
= parser
->scope
;
27097 tree saved_object_scope
= parser
->object_scope
;
27098 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27100 /* Look for the optional `::' operator. */
27101 cp_parser_global_scope_opt (parser
,
27102 /*current_scope_valid_p=*/false);
27103 /* Look for the nested-name-specifier. */
27104 cp_parser_nested_name_specifier_opt (parser
,
27105 /*typename_keyword_p=*/false,
27106 /*check_dependency_p=*/true,
27108 /*is_declaration=*/false);
27110 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27111 tree concept_name
= cp_parser_identifier (parser
);
27113 /* Look up the concept for which we will be matching
27114 template parameters. */
27115 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27117 parser
->scope
= saved_scope
;
27118 parser
->object_scope
= saved_object_scope
;
27119 parser
->qualifying_scope
= saved_qualifying_scope
;
27121 if (concept_name
== error_mark_node
)
27122 cp_parser_simulate_error (parser
);
27124 /* Look for opening brace for introduction. */
27125 matching_braces braces
;
27126 braces
.require_open (parser
);
27128 if (!cp_parser_parse_definitely (parser
))
27131 push_deferring_access_checks (dk_deferred
);
27133 /* Build vector of placeholder parameters and grab
27134 matching identifiers. */
27135 tree introduction_list
= cp_parser_introduction_list (parser
);
27137 /* The introduction-list shall not be empty. */
27138 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27141 error ("empty introduction-list");
27145 /* Look for closing brace for introduction. */
27146 if (!braces
.require_close (parser
))
27149 if (tmpl_decl
== error_mark_node
)
27151 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27156 /* Build and associate the constraint. */
27157 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27158 if (parms
&& parms
!= error_mark_node
)
27160 cp_parser_template_declaration_after_parameters (parser
, parms
,
27165 error_at (token
->location
, "no matching concept for template-introduction");
27169 /* Parse a normal template-declaration following the template keyword. */
27172 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27174 tree parameter_list
;
27175 bool need_lang_pop
;
27176 location_t location
= input_location
;
27178 /* Look for the `<' token. */
27179 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27181 if (at_class_scope_p () && current_function_decl
)
27183 /* 14.5.2.2 [temp.mem]
27185 A local class shall not have member templates. */
27186 error_at (location
,
27187 "invalid declaration of member template in local class");
27188 cp_parser_skip_to_end_of_block_or_statement (parser
);
27193 A template ... shall not have C linkage. */
27194 if (current_lang_name
== lang_name_c
)
27196 error_at (location
, "template with C linkage");
27197 maybe_show_extern_c_location ();
27198 /* Give it C++ linkage to avoid confusing other parts of the
27200 push_lang_context (lang_name_cplusplus
);
27201 need_lang_pop
= true;
27204 need_lang_pop
= false;
27206 /* We cannot perform access checks on the template parameter
27207 declarations until we know what is being declared, just as we
27208 cannot check the decl-specifier list. */
27209 push_deferring_access_checks (dk_deferred
);
27211 /* If the next token is `>', then we have an invalid
27212 specialization. Rather than complain about an invalid template
27213 parameter, issue an error message here. */
27214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27216 cp_parser_error (parser
, "invalid explicit specialization");
27217 begin_specialization ();
27218 parameter_list
= NULL_TREE
;
27222 /* Parse the template parameters. */
27223 parameter_list
= cp_parser_template_parameter_list (parser
);
27226 /* Look for the `>'. */
27227 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27229 /* Manage template requirements */
27232 tree reqs
= get_shorthand_constraints (current_template_parms
);
27233 if (tree r
= cp_parser_requires_clause_opt (parser
))
27234 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27235 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27238 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27241 /* For the erroneous case of a template with C linkage, we pushed an
27242 implicit C++ linkage scope; exit that scope now. */
27244 pop_lang_context ();
27247 /* Parse a template-declaration, assuming that the `export' (and
27248 `extern') keywords, if present, has already been scanned. MEMBER_P
27249 is as for cp_parser_template_declaration. */
27252 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27254 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27256 cp_lexer_consume_token (parser
->lexer
);
27257 cp_parser_explicit_template_declaration (parser
, member_p
);
27260 else if (flag_concepts
)
27261 return cp_parser_template_introduction (parser
, member_p
);
27266 /* Perform the deferred access checks from a template-parameter-list.
27267 CHECKS is a TREE_LIST of access checks, as returned by
27268 get_deferred_access_checks. */
27271 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27273 ++processing_template_parmlist
;
27274 perform_access_checks (checks
, tf_warning_or_error
);
27275 --processing_template_parmlist
;
27278 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27279 `function-definition' sequence that follows a template header.
27280 If MEMBER_P is true, this declaration appears in a class scope.
27282 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27283 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27286 cp_parser_single_declaration (cp_parser
* parser
,
27287 vec
<deferred_access_check
, va_gc
> *checks
,
27289 bool explicit_specialization_p
,
27292 int declares_class_or_enum
;
27293 tree decl
= NULL_TREE
;
27294 cp_decl_specifier_seq decl_specifiers
;
27295 bool function_definition_p
= false;
27296 cp_token
*decl_spec_token_start
;
27298 /* This function is only used when processing a template
27300 gcc_assert (innermost_scope_kind () == sk_template_parms
27301 || innermost_scope_kind () == sk_template_spec
);
27303 /* Defer access checks until we know what is being declared. */
27304 push_deferring_access_checks (dk_deferred
);
27306 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27308 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27309 cp_parser_decl_specifier_seq (parser
,
27310 CP_PARSER_FLAGS_OPTIONAL
,
27312 &declares_class_or_enum
);
27314 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27316 /* There are no template typedefs. */
27317 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27319 error_at (decl_spec_token_start
->location
,
27320 "template declaration of %<typedef%>");
27321 decl
= error_mark_node
;
27324 /* Gather up the access checks that occurred the
27325 decl-specifier-seq. */
27326 stop_deferring_access_checks ();
27328 /* Check for the declaration of a template class. */
27329 if (declares_class_or_enum
)
27331 if (cp_parser_declares_only_class_p (parser
)
27332 || (declares_class_or_enum
& 2))
27334 // If this is a declaration, but not a definition, associate
27335 // any constraints with the type declaration. Constraints
27336 // are associated with definitions in cp_parser_class_specifier.
27337 if (declares_class_or_enum
== 1)
27338 associate_classtype_constraints (decl_specifiers
.type
);
27340 decl
= shadow_tag (&decl_specifiers
);
27345 friend template <typename T> struct A<T>::B;
27348 A<T>::B will be represented by a TYPENAME_TYPE, and
27349 therefore not recognized by shadow_tag. */
27350 if (friend_p
&& *friend_p
27352 && decl_specifiers
.type
27353 && TYPE_P (decl_specifiers
.type
))
27354 decl
= decl_specifiers
.type
;
27356 if (decl
&& decl
!= error_mark_node
)
27357 decl
= TYPE_NAME (decl
);
27359 decl
= error_mark_node
;
27361 /* Perform access checks for template parameters. */
27362 cp_parser_perform_template_parameter_access_checks (checks
);
27364 /* Give a helpful diagnostic for
27365 template <class T> struct A { } a;
27366 if we aren't already recovering from an error. */
27367 if (!cp_parser_declares_only_class_p (parser
)
27370 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27371 "a class template declaration must not declare "
27373 cp_parser_skip_to_end_of_block_or_statement (parser
);
27379 /* Complain about missing 'typename' or other invalid type names. */
27380 if (!decl_specifiers
.any_type_specifiers_p
27381 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27383 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27384 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27385 the rest of this declaration. */
27386 decl
= error_mark_node
;
27390 /* If it's not a template class, try for a template function. If
27391 the next token is a `;', then this declaration does not declare
27392 anything. But, if there were errors in the decl-specifiers, then
27393 the error might well have come from an attempted class-specifier.
27394 In that case, there's no need to warn about a missing declarator. */
27396 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27397 || decl_specifiers
.type
!= error_mark_node
))
27399 decl
= cp_parser_init_declarator (parser
,
27402 /*function_definition_allowed_p=*/true,
27404 declares_class_or_enum
,
27405 &function_definition_p
,
27408 /* 7.1.1-1 [dcl.stc]
27410 A storage-class-specifier shall not be specified in an explicit
27411 specialization... */
27413 && explicit_specialization_p
27414 && decl_specifiers
.storage_class
!= sc_none
)
27416 error_at (decl_spec_token_start
->location
,
27417 "explicit template specialization cannot have a storage class");
27418 decl
= error_mark_node
;
27421 if (decl
&& VAR_P (decl
))
27422 check_template_variable (decl
);
27425 /* Look for a trailing `;' after the declaration. */
27426 if (!function_definition_p
27427 && (decl
== error_mark_node
27428 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27429 cp_parser_skip_to_end_of_block_or_statement (parser
);
27432 pop_deferring_access_checks ();
27434 /* Clear any current qualification; whatever comes next is the start
27435 of something new. */
27436 parser
->scope
= NULL_TREE
;
27437 parser
->qualifying_scope
= NULL_TREE
;
27438 parser
->object_scope
= NULL_TREE
;
27443 /* Parse a cast-expression that is not the operand of a unary "&". */
27446 cp_parser_simple_cast_expression (cp_parser
*parser
)
27448 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27449 /*cast_p=*/false, /*decltype*/false, NULL
);
27452 /* Parse a functional cast to TYPE. Returns an expression
27453 representing the cast. */
27456 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27458 vec
<tree
, va_gc
> *vec
;
27459 tree expression_list
;
27463 location_t start_loc
= input_location
;
27466 type
= error_mark_node
;
27468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27470 cp_lexer_set_source_position (parser
->lexer
);
27471 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27472 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27473 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27474 if (TREE_CODE (type
) == TYPE_DECL
)
27475 type
= TREE_TYPE (type
);
27477 cast
= finish_compound_literal (type
, expression_list
,
27478 tf_warning_or_error
, fcl_functional
);
27479 /* Create a location of the form:
27482 with caret == start at the start of the type name,
27483 finishing at the closing brace. */
27484 location_t finish_loc
27485 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27486 location_t combined_loc
= make_location (start_loc
, start_loc
,
27488 cast
.set_location (combined_loc
);
27493 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27495 /*allow_expansion_p=*/true,
27496 /*non_constant_p=*/NULL
);
27498 expression_list
= error_mark_node
;
27501 expression_list
= build_tree_list_vec (vec
);
27502 release_tree_vector (vec
);
27505 cast
= build_functional_cast (type
, expression_list
,
27506 tf_warning_or_error
);
27507 /* [expr.const]/1: In an integral constant expression "only type
27508 conversions to integral or enumeration type can be used". */
27509 if (TREE_CODE (type
) == TYPE_DECL
)
27510 type
= TREE_TYPE (type
);
27511 if (cast
!= error_mark_node
27512 && !cast_valid_in_integral_constant_expression_p (type
)
27513 && cp_parser_non_integral_constant_expression (parser
,
27515 return error_mark_node
;
27517 /* Create a location of the form:
27520 with caret == start at the start of the type name,
27521 finishing at the closing paren. */
27522 location_t finish_loc
27523 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27524 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27525 cast
.set_location (combined_loc
);
27529 /* Save the tokens that make up the body of a member function defined
27530 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27531 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27532 specifiers applied to the declaration. Returns the FUNCTION_DECL
27533 for the member function. */
27536 cp_parser_save_member_function_body (cp_parser
* parser
,
27537 cp_decl_specifier_seq
*decl_specifiers
,
27538 cp_declarator
*declarator
,
27544 bool function_try_block
= false;
27546 /* Create the FUNCTION_DECL. */
27547 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27548 cp_finalize_omp_declare_simd (parser
, fn
);
27549 cp_finalize_oacc_routine (parser
, fn
, true);
27550 /* If something went badly wrong, bail out now. */
27551 if (fn
== error_mark_node
)
27553 /* If there's a function-body, skip it. */
27554 if (cp_parser_token_starts_function_definition_p
27555 (cp_lexer_peek_token (parser
->lexer
)))
27556 cp_parser_skip_to_end_of_block_or_statement (parser
);
27557 return error_mark_node
;
27560 /* Remember it, if there default args to post process. */
27561 cp_parser_save_default_args (parser
, fn
);
27563 /* Save away the tokens that make up the body of the
27565 first
= parser
->lexer
->next_token
;
27567 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27568 cp_lexer_consume_token (parser
->lexer
);
27569 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27570 RID_TRANSACTION_ATOMIC
))
27572 cp_lexer_consume_token (parser
->lexer
);
27573 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27575 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27576 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27577 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27578 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27579 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27581 cp_lexer_consume_token (parser
->lexer
);
27582 cp_lexer_consume_token (parser
->lexer
);
27583 cp_lexer_consume_token (parser
->lexer
);
27584 cp_lexer_consume_token (parser
->lexer
);
27585 cp_lexer_consume_token (parser
->lexer
);
27588 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27589 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27591 cp_lexer_consume_token (parser
->lexer
);
27592 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27597 /* Handle function try blocks. */
27598 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27600 cp_lexer_consume_token (parser
->lexer
);
27601 function_try_block
= true;
27603 /* We can have braced-init-list mem-initializers before the fn body. */
27604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27606 cp_lexer_consume_token (parser
->lexer
);
27607 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27609 /* cache_group will stop after an un-nested { } pair, too. */
27610 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27613 /* variadic mem-inits have ... after the ')'. */
27614 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27615 cp_lexer_consume_token (parser
->lexer
);
27618 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27619 /* Handle function try blocks. */
27620 if (function_try_block
)
27621 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27622 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27623 last
= parser
->lexer
->next_token
;
27625 /* Save away the inline definition; we will process it when the
27626 class is complete. */
27627 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27628 DECL_PENDING_INLINE_P (fn
) = 1;
27630 /* We need to know that this was defined in the class, so that
27631 friend templates are handled correctly. */
27632 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27634 /* Add FN to the queue of functions to be parsed later. */
27635 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27640 /* Save the tokens that make up the in-class initializer for a non-static
27641 data member. Returns a DEFAULT_ARG. */
27644 cp_parser_save_nsdmi (cp_parser
* parser
)
27646 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27649 /* Parse a template-argument-list, as well as the trailing ">" (but
27650 not the opening "<"). See cp_parser_template_argument_list for the
27654 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27658 tree saved_qualifying_scope
;
27659 tree saved_object_scope
;
27660 bool saved_greater_than_is_operator_p
;
27661 int saved_unevaluated_operand
;
27662 int saved_inhibit_evaluation_warnings
;
27666 When parsing a template-id, the first non-nested `>' is taken as
27667 the end of the template-argument-list rather than a greater-than
27669 saved_greater_than_is_operator_p
27670 = parser
->greater_than_is_operator_p
;
27671 parser
->greater_than_is_operator_p
= false;
27672 /* Parsing the argument list may modify SCOPE, so we save it
27674 saved_scope
= parser
->scope
;
27675 saved_qualifying_scope
= parser
->qualifying_scope
;
27676 saved_object_scope
= parser
->object_scope
;
27677 /* We need to evaluate the template arguments, even though this
27678 template-id may be nested within a "sizeof". */
27679 saved_unevaluated_operand
= cp_unevaluated_operand
;
27680 cp_unevaluated_operand
= 0;
27681 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27682 c_inhibit_evaluation_warnings
= 0;
27683 /* Parse the template-argument-list itself. */
27684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27685 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27686 arguments
= NULL_TREE
;
27688 arguments
= cp_parser_template_argument_list (parser
);
27689 /* Look for the `>' that ends the template-argument-list. If we find
27690 a '>>' instead, it's probably just a typo. */
27691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27693 if (cxx_dialect
!= cxx98
)
27695 /* In C++0x, a `>>' in a template argument list or cast
27696 expression is considered to be two separate `>'
27697 tokens. So, change the current token to a `>', but don't
27698 consume it: it will be consumed later when the outer
27699 template argument list (or cast expression) is parsed.
27700 Note that this replacement of `>' for `>>' is necessary
27701 even if we are parsing tentatively: in the tentative
27702 case, after calling
27703 cp_parser_enclosed_template_argument_list we will always
27704 throw away all of the template arguments and the first
27705 closing `>', either because the template argument list
27706 was erroneous or because we are replacing those tokens
27707 with a CPP_TEMPLATE_ID token. The second `>' (which will
27708 not have been thrown away) is needed either to close an
27709 outer template argument list or to complete a new-style
27711 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27712 token
->type
= CPP_GREATER
;
27714 else if (!saved_greater_than_is_operator_p
)
27716 /* If we're in a nested template argument list, the '>>' has
27717 to be a typo for '> >'. We emit the error message, but we
27718 continue parsing and we push a '>' as next token, so that
27719 the argument list will be parsed correctly. Note that the
27720 global source location is still on the token before the
27721 '>>', so we need to say explicitly where we want it. */
27722 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27723 gcc_rich_location
richloc (token
->location
);
27724 richloc
.add_fixit_replace ("> >");
27725 error_at (&richloc
, "%<>>%> should be %<> >%> "
27726 "within a nested template argument list");
27728 token
->type
= CPP_GREATER
;
27732 /* If this is not a nested template argument list, the '>>'
27733 is a typo for '>'. Emit an error message and continue.
27734 Same deal about the token location, but here we can get it
27735 right by consuming the '>>' before issuing the diagnostic. */
27736 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27737 error_at (token
->location
,
27738 "spurious %<>>%>, use %<>%> to terminate "
27739 "a template argument list");
27743 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27744 /* The `>' token might be a greater-than operator again now. */
27745 parser
->greater_than_is_operator_p
27746 = saved_greater_than_is_operator_p
;
27747 /* Restore the SAVED_SCOPE. */
27748 parser
->scope
= saved_scope
;
27749 parser
->qualifying_scope
= saved_qualifying_scope
;
27750 parser
->object_scope
= saved_object_scope
;
27751 cp_unevaluated_operand
= saved_unevaluated_operand
;
27752 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27757 /* MEMBER_FUNCTION is a member function, or a friend. If default
27758 arguments, or the body of the function have not yet been parsed,
27762 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27764 timevar_push (TV_PARSE_INMETH
);
27765 /* If this member is a template, get the underlying
27767 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27768 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27770 /* There should not be any class definitions in progress at this
27771 point; the bodies of members are only parsed outside of all class
27773 gcc_assert (parser
->num_classes_being_defined
== 0);
27774 /* While we're parsing the member functions we might encounter more
27775 classes. We want to handle them right away, but we don't want
27776 them getting mixed up with functions that are currently in the
27778 push_unparsed_function_queues (parser
);
27780 /* Make sure that any template parameters are in scope. */
27781 maybe_begin_member_template_processing (member_function
);
27783 /* If the body of the function has not yet been parsed, parse it
27785 if (DECL_PENDING_INLINE_P (member_function
))
27787 tree function_scope
;
27788 cp_token_cache
*tokens
;
27790 /* The function is no longer pending; we are processing it. */
27791 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27792 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27793 DECL_PENDING_INLINE_P (member_function
) = 0;
27795 /* If this is a local class, enter the scope of the containing
27797 function_scope
= current_function_decl
;
27798 if (function_scope
)
27799 push_function_context ();
27801 /* Push the body of the function onto the lexer stack. */
27802 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27804 /* Let the front end know that we going to be defining this
27806 start_preparsed_function (member_function
, NULL_TREE
,
27807 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27809 /* Don't do access checking if it is a templated function. */
27810 if (processing_template_decl
)
27811 push_deferring_access_checks (dk_no_check
);
27813 /* #pragma omp declare reduction needs special parsing. */
27814 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27816 parser
->lexer
->in_pragma
= true;
27817 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27818 finish_function (/*inline_p=*/true);
27819 cp_check_omp_declare_reduction (member_function
);
27822 /* Now, parse the body of the function. */
27823 cp_parser_function_definition_after_declarator (parser
,
27824 /*inline_p=*/true);
27826 if (processing_template_decl
)
27827 pop_deferring_access_checks ();
27829 /* Leave the scope of the containing function. */
27830 if (function_scope
)
27831 pop_function_context ();
27832 cp_parser_pop_lexer (parser
);
27835 /* Remove any template parameters from the symbol table. */
27836 maybe_end_member_template_processing ();
27838 /* Restore the queue. */
27839 pop_unparsed_function_queues (parser
);
27840 timevar_pop (TV_PARSE_INMETH
);
27843 /* If DECL contains any default args, remember it on the unparsed
27844 functions queue. */
27847 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27851 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27853 probe
= TREE_CHAIN (probe
))
27854 if (TREE_PURPOSE (probe
))
27856 cp_default_arg_entry entry
= {current_class_type
, decl
};
27857 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27862 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27863 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27864 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27865 from the parameter-type-list. */
27868 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27869 tree default_arg
, tree parmtype
)
27871 cp_token_cache
*tokens
;
27875 if (default_arg
== error_mark_node
)
27876 return error_mark_node
;
27878 /* Push the saved tokens for the default argument onto the parser's
27880 tokens
= DEFARG_TOKENS (default_arg
);
27881 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27883 start_lambda_scope (decl
);
27885 /* Parse the default argument. */
27886 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27887 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27888 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27890 finish_lambda_scope ();
27892 if (parsed_arg
== error_mark_node
)
27893 cp_parser_skip_to_end_of_statement (parser
);
27895 if (!processing_template_decl
)
27897 /* In a non-template class, check conversions now. In a template,
27898 we'll wait and instantiate these as needed. */
27899 if (TREE_CODE (decl
) == PARM_DECL
)
27900 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27901 tf_warning_or_error
);
27902 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27903 parsed_arg
= error_mark_node
;
27905 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27908 /* If the token stream has not been completely used up, then
27909 there was extra junk after the end of the default
27911 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27913 if (TREE_CODE (decl
) == PARM_DECL
)
27914 cp_parser_error (parser
, "expected %<,%>");
27916 cp_parser_error (parser
, "expected %<;%>");
27919 /* Revert to the main lexer. */
27920 cp_parser_pop_lexer (parser
);
27925 /* FIELD is a non-static data member with an initializer which we saved for
27926 later; parse it now. */
27929 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27933 maybe_begin_member_template_processing (field
);
27935 push_unparsed_function_queues (parser
);
27936 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27937 DECL_INITIAL (field
),
27939 pop_unparsed_function_queues (parser
);
27941 maybe_end_member_template_processing ();
27943 DECL_INITIAL (field
) = def
;
27946 /* FN is a FUNCTION_DECL which may contains a parameter with an
27947 unparsed DEFAULT_ARG. Parse the default args now. This function
27948 assumes that the current scope is the scope in which the default
27949 argument should be processed. */
27952 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27954 bool saved_local_variables_forbidden_p
;
27955 tree parm
, parmdecl
;
27957 /* While we're parsing the default args, we might (due to the
27958 statement expression extension) encounter more classes. We want
27959 to handle them right away, but we don't want them getting mixed
27960 up with default args that are currently in the queue. */
27961 push_unparsed_function_queues (parser
);
27963 /* Local variable names (and the `this' keyword) may not appear
27964 in a default argument. */
27965 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27966 parser
->local_variables_forbidden_p
= true;
27968 push_defarg_context (fn
);
27970 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27971 parmdecl
= DECL_ARGUMENTS (fn
);
27972 parm
&& parm
!= void_list_node
;
27973 parm
= TREE_CHAIN (parm
),
27974 parmdecl
= DECL_CHAIN (parmdecl
))
27976 tree default_arg
= TREE_PURPOSE (parm
);
27978 vec
<tree
, va_gc
> *insts
;
27985 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27986 /* This can happen for a friend declaration for a function
27987 already declared with default arguments. */
27991 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27993 TREE_VALUE (parm
));
27994 TREE_PURPOSE (parm
) = parsed_arg
;
27996 /* Update any instantiations we've already created. */
27997 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27998 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27999 TREE_PURPOSE (copy
) = parsed_arg
;
28002 pop_defarg_context ();
28004 /* Make sure no default arg is missing. */
28005 check_default_args (fn
);
28007 /* Restore the state of local_variables_forbidden_p. */
28008 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28010 /* Restore the queue. */
28011 pop_unparsed_function_queues (parser
);
28014 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28016 sizeof ... ( identifier )
28018 where the 'sizeof' token has already been consumed. */
28021 cp_parser_sizeof_pack (cp_parser
*parser
)
28023 /* Consume the `...'. */
28024 cp_lexer_consume_token (parser
->lexer
);
28025 maybe_warn_variadic_templates ();
28027 matching_parens parens
;
28028 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28030 parens
.consume_open (parser
);
28032 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28033 "%<sizeof...%> argument must be surrounded by parentheses");
28035 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28036 tree name
= cp_parser_identifier (parser
);
28037 if (name
== error_mark_node
)
28038 return error_mark_node
;
28039 /* The name is not qualified. */
28040 parser
->scope
= NULL_TREE
;
28041 parser
->qualifying_scope
= NULL_TREE
;
28042 parser
->object_scope
= NULL_TREE
;
28043 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28044 if (expr
== error_mark_node
)
28045 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28047 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28048 expr
= TREE_TYPE (expr
);
28049 else if (TREE_CODE (expr
) == CONST_DECL
)
28050 expr
= DECL_INITIAL (expr
);
28051 expr
= make_pack_expansion (expr
);
28052 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28055 parens
.require_close (parser
);
28060 /* Parse the operand of `sizeof' (or a similar operator). Returns
28061 either a TYPE or an expression, depending on the form of the
28062 input. The KEYWORD indicates which kind of expression we have
28066 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28068 tree expr
= NULL_TREE
;
28069 const char *saved_message
;
28071 bool saved_integral_constant_expression_p
;
28072 bool saved_non_integral_constant_expression_p
;
28074 /* If it's a `...', then we are computing the length of a parameter
28076 if (keyword
== RID_SIZEOF
28077 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28078 return cp_parser_sizeof_pack (parser
);
28080 /* Types cannot be defined in a `sizeof' expression. Save away the
28082 saved_message
= parser
->type_definition_forbidden_message
;
28083 /* And create the new one. */
28084 tmp
= concat ("types may not be defined in %<",
28085 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28086 "%> expressions", NULL
);
28087 parser
->type_definition_forbidden_message
= tmp
;
28089 /* The restrictions on constant-expressions do not apply inside
28090 sizeof expressions. */
28091 saved_integral_constant_expression_p
28092 = parser
->integral_constant_expression_p
;
28093 saved_non_integral_constant_expression_p
28094 = parser
->non_integral_constant_expression_p
;
28095 parser
->integral_constant_expression_p
= false;
28097 /* Do not actually evaluate the expression. */
28098 ++cp_unevaluated_operand
;
28099 ++c_inhibit_evaluation_warnings
;
28100 /* If it's a `(', then we might be looking at the type-id
28102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28104 tree type
= NULL_TREE
;
28106 /* We can't be sure yet whether we're looking at a type-id or an
28108 cp_parser_parse_tentatively (parser
);
28110 matching_parens parens
;
28111 parens
.consume_open (parser
);
28113 /* Note: as a GNU Extension, compound literals are considered
28114 postfix-expressions as they are in C99, so they are valid
28115 arguments to sizeof. See comment in cp_parser_cast_expression
28117 if (cp_parser_compound_literal_p (parser
))
28118 cp_parser_simulate_error (parser
);
28121 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28122 parser
->in_type_id_in_expr_p
= true;
28123 /* Look for the type-id. */
28124 type
= cp_parser_type_id (parser
);
28125 /* Look for the closing `)'. */
28126 parens
.require_close (parser
);
28127 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28130 /* If all went well, then we're done. */
28131 if (cp_parser_parse_definitely (parser
))
28133 cp_decl_specifier_seq decl_specs
;
28135 /* Build a trivial decl-specifier-seq. */
28136 clear_decl_specs (&decl_specs
);
28137 decl_specs
.type
= type
;
28139 /* Call grokdeclarator to figure out what type this is. */
28140 expr
= grokdeclarator (NULL
,
28144 /*attrlist=*/NULL
);
28148 /* If the type-id production did not work out, then we must be
28149 looking at the unary-expression production. */
28151 expr
= cp_parser_unary_expression (parser
);
28153 /* Go back to evaluating expressions. */
28154 --cp_unevaluated_operand
;
28155 --c_inhibit_evaluation_warnings
;
28157 /* Free the message we created. */
28159 /* And restore the old one. */
28160 parser
->type_definition_forbidden_message
= saved_message
;
28161 parser
->integral_constant_expression_p
28162 = saved_integral_constant_expression_p
;
28163 parser
->non_integral_constant_expression_p
28164 = saved_non_integral_constant_expression_p
;
28169 /* If the current declaration has no declarator, return true. */
28172 cp_parser_declares_only_class_p (cp_parser
*parser
)
28174 /* If the next token is a `;' or a `,' then there is no
28176 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28177 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28180 /* Update the DECL_SPECS to reflect the storage class indicated by
28184 cp_parser_set_storage_class (cp_parser
*parser
,
28185 cp_decl_specifier_seq
*decl_specs
,
28189 cp_storage_class storage_class
;
28191 if (parser
->in_unbraced_linkage_specification_p
)
28193 error_at (token
->location
, "invalid use of %qD in linkage specification",
28194 ridpointers
[keyword
]);
28197 else if (decl_specs
->storage_class
!= sc_none
)
28199 decl_specs
->conflicting_specifiers_p
= true;
28203 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28204 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28205 && decl_specs
->gnu_thread_keyword_p
)
28207 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28208 "%<__thread%> before %qD", ridpointers
[keyword
]);
28214 storage_class
= sc_auto
;
28217 storage_class
= sc_register
;
28220 storage_class
= sc_static
;
28223 storage_class
= sc_extern
;
28226 storage_class
= sc_mutable
;
28229 gcc_unreachable ();
28231 decl_specs
->storage_class
= storage_class
;
28232 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28234 /* A storage class specifier cannot be applied alongside a typedef
28235 specifier. If there is a typedef specifier present then set
28236 conflicting_specifiers_p which will trigger an error later
28237 on in grokdeclarator. */
28238 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28239 decl_specs
->conflicting_specifiers_p
= true;
28242 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28243 is true, the type is a class or enum definition. */
28246 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28249 bool type_definition_p
)
28251 decl_specs
->any_specifiers_p
= true;
28253 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28254 (with, for example, in "typedef int wchar_t;") we remember that
28255 this is what happened. In system headers, we ignore these
28256 declarations so that G++ can work with system headers that are not
28258 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28259 && !type_definition_p
28260 && (type_spec
== boolean_type_node
28261 || type_spec
== char16_type_node
28262 || type_spec
== char32_type_node
28263 || type_spec
== wchar_type_node
)
28264 && (decl_specs
->type
28265 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28266 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28267 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28268 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28270 decl_specs
->redefined_builtin_type
= type_spec
;
28271 set_and_check_decl_spec_loc (decl_specs
,
28272 ds_redefined_builtin_type_spec
,
28274 if (!decl_specs
->type
)
28276 decl_specs
->type
= type_spec
;
28277 decl_specs
->type_definition_p
= false;
28278 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28281 else if (decl_specs
->type
)
28282 decl_specs
->multiple_types_p
= true;
28285 decl_specs
->type
= type_spec
;
28286 decl_specs
->type_definition_p
= type_definition_p
;
28287 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28288 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28292 /* True iff TOKEN is the GNU keyword __thread. */
28295 token_is__thread (cp_token
*token
)
28297 gcc_assert (token
->keyword
== RID_THREAD
);
28298 return id_equal (token
->u
.value
, "__thread");
28301 /* Set the location for a declarator specifier and check if it is
28304 DECL_SPECS is the sequence of declarator specifiers onto which to
28307 DS is the single declarator specifier to set which location is to
28308 be set onto the existing sequence of declarators.
28310 LOCATION is the location for the declarator specifier to
28314 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28315 cp_decl_spec ds
, cp_token
*token
)
28317 gcc_assert (ds
< ds_last
);
28319 if (decl_specs
== NULL
)
28322 source_location location
= token
->location
;
28324 if (decl_specs
->locations
[ds
] == 0)
28326 decl_specs
->locations
[ds
] = location
;
28327 if (ds
== ds_thread
)
28328 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28334 if (decl_specs
->locations
[ds_long_long
] != 0)
28335 error_at (location
,
28336 "%<long long long%> is too long for GCC");
28339 decl_specs
->locations
[ds_long_long
] = location
;
28340 pedwarn_cxx98 (location
,
28342 "ISO C++ 1998 does not support %<long long%>");
28345 else if (ds
== ds_thread
)
28347 bool gnu
= token_is__thread (token
);
28348 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28349 error_at (location
,
28350 "both %<__thread%> and %<thread_local%> specified");
28353 gcc_rich_location
richloc (location
);
28354 richloc
.add_fixit_remove ();
28355 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28360 static const char *const decl_spec_names
[] = {
28377 gcc_rich_location
richloc (location
);
28378 richloc
.add_fixit_remove ();
28379 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28384 /* Return true iff the declarator specifier DS is present in the
28385 sequence of declarator specifiers DECL_SPECS. */
28388 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28391 gcc_assert (ds
< ds_last
);
28393 if (decl_specs
== NULL
)
28396 return decl_specs
->locations
[ds
] != 0;
28399 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28400 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28403 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28405 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28408 /* Issue an error message indicating that TOKEN_DESC was expected.
28409 If KEYWORD is true, it indicated this function is called by
28410 cp_parser_require_keword and the required token can only be
28411 a indicated keyword.
28413 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28414 within any error as the location of an "opening" token matching
28415 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28416 RT_CLOSE_PAREN). */
28419 cp_parser_required_error (cp_parser
*parser
,
28420 required_token token_desc
,
28422 location_t matching_location
)
28424 if (cp_parser_simulate_error (parser
))
28427 const char *gmsgid
= NULL
;
28428 switch (token_desc
)
28431 gmsgid
= G_("expected %<new%>");
28434 gmsgid
= G_("expected %<delete%>");
28437 gmsgid
= G_("expected %<return%>");
28440 gmsgid
= G_("expected %<while%>");
28443 gmsgid
= G_("expected %<extern%>");
28445 case RT_STATIC_ASSERT
:
28446 gmsgid
= G_("expected %<static_assert%>");
28449 gmsgid
= G_("expected %<decltype%>");
28452 gmsgid
= G_("expected %<operator%>");
28455 gmsgid
= G_("expected %<class%>");
28458 gmsgid
= G_("expected %<template%>");
28461 gmsgid
= G_("expected %<namespace%>");
28464 gmsgid
= G_("expected %<using%>");
28467 gmsgid
= G_("expected %<asm%>");
28470 gmsgid
= G_("expected %<try%>");
28473 gmsgid
= G_("expected %<catch%>");
28476 gmsgid
= G_("expected %<throw%>");
28479 gmsgid
= G_("expected %<__label__%>");
28482 gmsgid
= G_("expected %<@try%>");
28484 case RT_AT_SYNCHRONIZED
:
28485 gmsgid
= G_("expected %<@synchronized%>");
28488 gmsgid
= G_("expected %<@throw%>");
28490 case RT_TRANSACTION_ATOMIC
:
28491 gmsgid
= G_("expected %<__transaction_atomic%>");
28493 case RT_TRANSACTION_RELAXED
:
28494 gmsgid
= G_("expected %<__transaction_relaxed%>");
28500 if (!gmsgid
&& !keyword
)
28502 switch (token_desc
)
28505 gmsgid
= G_("expected %<;%>");
28507 case RT_OPEN_PAREN
:
28508 gmsgid
= G_("expected %<(%>");
28510 case RT_CLOSE_BRACE
:
28511 gmsgid
= G_("expected %<}%>");
28513 case RT_OPEN_BRACE
:
28514 gmsgid
= G_("expected %<{%>");
28516 case RT_CLOSE_SQUARE
:
28517 gmsgid
= G_("expected %<]%>");
28519 case RT_OPEN_SQUARE
:
28520 gmsgid
= G_("expected %<[%>");
28523 gmsgid
= G_("expected %<,%>");
28526 gmsgid
= G_("expected %<::%>");
28529 gmsgid
= G_("expected %<<%>");
28532 gmsgid
= G_("expected %<>%>");
28535 gmsgid
= G_("expected %<=%>");
28538 gmsgid
= G_("expected %<...%>");
28541 gmsgid
= G_("expected %<*%>");
28544 gmsgid
= G_("expected %<~%>");
28547 gmsgid
= G_("expected %<:%>");
28549 case RT_COLON_SCOPE
:
28550 gmsgid
= G_("expected %<:%> or %<::%>");
28552 case RT_CLOSE_PAREN
:
28553 gmsgid
= G_("expected %<)%>");
28555 case RT_COMMA_CLOSE_PAREN
:
28556 gmsgid
= G_("expected %<,%> or %<)%>");
28558 case RT_PRAGMA_EOL
:
28559 gmsgid
= G_("expected end of line");
28562 gmsgid
= G_("expected identifier");
28565 gmsgid
= G_("expected selection-statement");
28568 gmsgid
= G_("expected iteration-statement");
28571 gmsgid
= G_("expected jump-statement");
28574 gmsgid
= G_("expected class-key");
28576 case RT_CLASS_TYPENAME_TEMPLATE
:
28577 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28580 gcc_unreachable ();
28585 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28589 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28590 issue an error message indicating that TOKEN_DESC was expected.
28592 Returns the token consumed, if the token had the appropriate type.
28593 Otherwise, returns NULL.
28595 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28596 within any error as the location of an "opening" token matching
28597 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28598 RT_CLOSE_PAREN). */
28601 cp_parser_require (cp_parser
* parser
,
28602 enum cpp_ttype type
,
28603 required_token token_desc
,
28604 location_t matching_location
)
28606 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28607 return cp_lexer_consume_token (parser
->lexer
);
28610 /* Output the MESSAGE -- unless we're parsing tentatively. */
28611 if (!cp_parser_simulate_error (parser
))
28612 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28613 matching_location
);
28618 /* An error message is produced if the next token is not '>'.
28619 All further tokens are skipped until the desired token is
28620 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28623 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28625 /* Current level of '< ... >'. */
28626 unsigned level
= 0;
28627 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28628 unsigned nesting_depth
= 0;
28630 /* Are we ready, yet? If not, issue error message. */
28631 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28634 /* Skip tokens until the desired token is found. */
28637 /* Peek at the next token. */
28638 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28641 if (!nesting_depth
)
28646 if (cxx_dialect
== cxx98
)
28647 /* C++0x views the `>>' operator as two `>' tokens, but
28650 else if (!nesting_depth
&& level
-- == 0)
28652 /* We've hit a `>>' where the first `>' closes the
28653 template argument list, and the second `>' is
28654 spurious. Just consume the `>>' and stop; we've
28655 already produced at least one error. */
28656 cp_lexer_consume_token (parser
->lexer
);
28659 /* Fall through for C++0x, so we handle the second `>' in
28661 gcc_fallthrough ();
28664 if (!nesting_depth
&& level
-- == 0)
28666 /* We've reached the token we want, consume it and stop. */
28667 cp_lexer_consume_token (parser
->lexer
);
28672 case CPP_OPEN_PAREN
:
28673 case CPP_OPEN_SQUARE
:
28677 case CPP_CLOSE_PAREN
:
28678 case CPP_CLOSE_SQUARE
:
28679 if (nesting_depth
-- == 0)
28684 case CPP_PRAGMA_EOL
:
28685 case CPP_SEMICOLON
:
28686 case CPP_OPEN_BRACE
:
28687 case CPP_CLOSE_BRACE
:
28688 /* The '>' was probably forgotten, don't look further. */
28695 /* Consume this token. */
28696 cp_lexer_consume_token (parser
->lexer
);
28700 /* If the next token is the indicated keyword, consume it. Otherwise,
28701 issue an error message indicating that TOKEN_DESC was expected.
28703 Returns the token consumed, if the token had the appropriate type.
28704 Otherwise, returns NULL. */
28707 cp_parser_require_keyword (cp_parser
* parser
,
28709 required_token token_desc
)
28711 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28713 if (token
&& token
->keyword
!= keyword
)
28715 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28723 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28724 function-definition. */
28727 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28729 return (/* An ordinary function-body begins with an `{'. */
28730 token
->type
== CPP_OPEN_BRACE
28731 /* A ctor-initializer begins with a `:'. */
28732 || token
->type
== CPP_COLON
28733 /* A function-try-block begins with `try'. */
28734 || token
->keyword
== RID_TRY
28735 /* A function-transaction-block begins with `__transaction_atomic'
28736 or `__transaction_relaxed'. */
28737 || token
->keyword
== RID_TRANSACTION_ATOMIC
28738 || token
->keyword
== RID_TRANSACTION_RELAXED
28739 /* The named return value extension begins with `return'. */
28740 || token
->keyword
== RID_RETURN
);
28743 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28747 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28751 token
= cp_lexer_peek_token (parser
->lexer
);
28752 return (token
->type
== CPP_OPEN_BRACE
28753 || (token
->type
== CPP_COLON
28754 && !parser
->colon_doesnt_start_class_def_p
));
28757 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28758 C++0x) ending a template-argument. */
28761 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28765 token
= cp_lexer_peek_token (parser
->lexer
);
28766 return (token
->type
== CPP_COMMA
28767 || token
->type
== CPP_GREATER
28768 || token
->type
== CPP_ELLIPSIS
28769 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28772 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28773 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28776 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28781 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28782 if (token
->type
== CPP_LESS
)
28784 /* Check for the sequence `<::' in the original code. It would be lexed as
28785 `[:', where `[' is a digraph, and there is no whitespace before
28787 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28790 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28791 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28797 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28798 or none_type otherwise. */
28800 static enum tag_types
28801 cp_parser_token_is_class_key (cp_token
* token
)
28803 switch (token
->keyword
)
28808 return record_type
;
28817 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28818 or none_type otherwise or if the token is null. */
28820 static enum tag_types
28821 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28826 switch (token
->keyword
)
28831 return typename_type
;
28838 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28841 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28843 if (type
== error_mark_node
)
28845 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28847 if (permerror (input_location
, "%qs tag used in naming %q#T",
28848 class_key
== union_type
? "union"
28849 : class_key
== record_type
? "struct" : "class",
28851 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28852 "%q#T was previously declared here", type
);
28856 /* Issue an error message if DECL is redeclared with different
28857 access than its original declaration [class.access.spec/3].
28858 This applies to nested classes, nested class templates and
28859 enumerations [class.mem/1]. */
28862 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28865 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28866 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28869 if ((TREE_PRIVATE (decl
)
28870 != (current_access_specifier
== access_private_node
))
28871 || (TREE_PROTECTED (decl
)
28872 != (current_access_specifier
== access_protected_node
)))
28873 error_at (location
, "%qD redeclared with different access", decl
);
28876 /* Look for the `template' keyword, as a syntactic disambiguator.
28877 Return TRUE iff it is present, in which case it will be
28881 cp_parser_optional_template_keyword (cp_parser
*parser
)
28883 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28885 /* In C++98 the `template' keyword can only be used within templates;
28886 outside templates the parser can always figure out what is a
28887 template and what is not. In C++11, per the resolution of DR 468,
28888 `template' is allowed in cases where it is not strictly necessary. */
28889 if (!processing_template_decl
28890 && pedantic
&& cxx_dialect
== cxx98
)
28892 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28893 pedwarn (token
->location
, OPT_Wpedantic
,
28894 "in C++98 %<template%> (as a disambiguator) is only "
28895 "allowed within templates");
28896 /* If this part of the token stream is rescanned, the same
28897 error message would be generated. So, we purge the token
28898 from the stream. */
28899 cp_lexer_purge_token (parser
->lexer
);
28904 /* Consume the `template' keyword. */
28905 cp_lexer_consume_token (parser
->lexer
);
28912 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28913 set PARSER->SCOPE, and perform other related actions. */
28916 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28918 struct tree_check
*check_value
;
28920 /* Get the stored value. */
28921 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28922 /* Set the scope from the stored value. */
28923 parser
->scope
= saved_checks_value (check_value
);
28924 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28925 parser
->object_scope
= NULL_TREE
;
28928 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28929 encounter the end of a block before what we were looking for. */
28932 cp_parser_cache_group (cp_parser
*parser
,
28933 enum cpp_ttype end
,
28938 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28940 /* Abort a parenthesized expression if we encounter a semicolon. */
28941 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28942 && token
->type
== CPP_SEMICOLON
)
28944 /* If we've reached the end of the file, stop. */
28945 if (token
->type
== CPP_EOF
28946 || (end
!= CPP_PRAGMA_EOL
28947 && token
->type
== CPP_PRAGMA_EOL
))
28949 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28950 /* We've hit the end of an enclosing block, so there's been some
28951 kind of syntax error. */
28954 /* Consume the token. */
28955 cp_lexer_consume_token (parser
->lexer
);
28956 /* See if it starts a new group. */
28957 if (token
->type
== CPP_OPEN_BRACE
)
28959 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28960 /* In theory this should probably check end == '}', but
28961 cp_parser_save_member_function_body needs it to exit
28962 after either '}' or ')' when called with ')'. */
28966 else if (token
->type
== CPP_OPEN_PAREN
)
28968 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28969 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28972 else if (token
->type
== CPP_PRAGMA
)
28973 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28974 else if (token
->type
== end
)
28979 /* Like above, for caching a default argument or NSDMI. Both of these are
28980 terminated by a non-nested comma, but it can be unclear whether or not a
28981 comma is nested in a template argument list unless we do more parsing.
28982 In order to handle this ambiguity, when we encounter a ',' after a '<'
28983 we try to parse what follows as a parameter-declaration-list (in the
28984 case of a default argument) or a member-declarator (in the case of an
28985 NSDMI). If that succeeds, then we stop caching. */
28988 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28990 unsigned depth
= 0;
28991 int maybe_template_id
= 0;
28992 cp_token
*first_token
;
28994 tree default_argument
;
28996 /* Add tokens until we have processed the entire default
28997 argument. We add the range [first_token, token). */
28998 first_token
= cp_lexer_peek_token (parser
->lexer
);
28999 if (first_token
->type
== CPP_OPEN_BRACE
)
29001 /* For list-initialization, this is straightforward. */
29002 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29003 token
= cp_lexer_peek_token (parser
->lexer
);
29009 /* Peek at the next token. */
29010 token
= cp_lexer_peek_token (parser
->lexer
);
29011 /* What we do depends on what token we have. */
29012 switch (token
->type
)
29014 /* In valid code, a default argument must be
29015 immediately followed by a `,' `)', or `...'. */
29017 if (depth
== 0 && maybe_template_id
)
29019 /* If we've seen a '<', we might be in a
29020 template-argument-list. Until Core issue 325 is
29021 resolved, we don't know how this situation ought
29022 to be handled, so try to DTRT. We check whether
29023 what comes after the comma is a valid parameter
29024 declaration list. If it is, then the comma ends
29025 the default argument; otherwise the default
29026 argument continues. */
29027 bool error
= false;
29030 /* Set ITALP so cp_parser_parameter_declaration_list
29031 doesn't decide to commit to this parse. */
29032 bool saved_italp
= parser
->in_template_argument_list_p
;
29033 parser
->in_template_argument_list_p
= true;
29035 cp_parser_parse_tentatively (parser
);
29039 /* Parse declarators until we reach a non-comma or
29040 somthing that cannot be an initializer.
29041 Just checking whether we're looking at a single
29042 declarator is insufficient. Consider:
29043 int var = tuple<T,U>::x;
29044 The template parameter 'U' looks exactly like a
29048 int ctor_dtor_or_conv_p
;
29049 cp_lexer_consume_token (parser
->lexer
);
29050 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29051 &ctor_dtor_or_conv_p
,
29052 /*parenthesized_p=*/NULL
,
29054 /*friend_p=*/false);
29055 peek
= cp_lexer_peek_token (parser
->lexer
);
29056 if (cp_parser_error_occurred (parser
))
29059 while (peek
->type
== CPP_COMMA
);
29060 /* If we met an '=' or ';' then the original comma
29061 was the end of the NSDMI. Otherwise assume
29062 we're still in the NSDMI. */
29063 error
= (peek
->type
!= CPP_EQ
29064 && peek
->type
!= CPP_SEMICOLON
);
29068 cp_lexer_consume_token (parser
->lexer
);
29069 begin_scope (sk_function_parms
, NULL_TREE
);
29070 if (cp_parser_parameter_declaration_list (parser
)
29071 == error_mark_node
)
29073 pop_bindings_and_leave_scope ();
29075 if (!cp_parser_error_occurred (parser
) && !error
)
29077 cp_parser_abort_tentative_parse (parser
);
29079 parser
->in_template_argument_list_p
= saved_italp
;
29083 case CPP_CLOSE_PAREN
:
29085 /* If we run into a non-nested `;', `}', or `]',
29086 then the code is invalid -- but the default
29087 argument is certainly over. */
29088 case CPP_SEMICOLON
:
29089 case CPP_CLOSE_BRACE
:
29090 case CPP_CLOSE_SQUARE
:
29092 /* Handle correctly int n = sizeof ... ( p ); */
29093 && token
->type
!= CPP_ELLIPSIS
)
29095 /* Update DEPTH, if necessary. */
29096 else if (token
->type
== CPP_CLOSE_PAREN
29097 || token
->type
== CPP_CLOSE_BRACE
29098 || token
->type
== CPP_CLOSE_SQUARE
)
29102 case CPP_OPEN_PAREN
:
29103 case CPP_OPEN_SQUARE
:
29104 case CPP_OPEN_BRACE
:
29110 /* This might be the comparison operator, or it might
29111 start a template argument list. */
29112 ++maybe_template_id
;
29116 if (cxx_dialect
== cxx98
)
29118 /* Fall through for C++0x, which treats the `>>'
29119 operator like two `>' tokens in certain
29121 gcc_fallthrough ();
29126 /* This might be an operator, or it might close a
29127 template argument list. But if a previous '<'
29128 started a template argument list, this will have
29129 closed it, so we can't be in one anymore. */
29130 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29131 if (maybe_template_id
< 0)
29132 maybe_template_id
= 0;
29136 /* If we run out of tokens, issue an error message. */
29138 case CPP_PRAGMA_EOL
:
29139 error_at (token
->location
, "file ends in default argument");
29140 return error_mark_node
;
29144 /* In these cases, we should look for template-ids.
29145 For example, if the default argument is
29146 `X<int, double>()', we need to do name lookup to
29147 figure out whether or not `X' is a template; if
29148 so, the `,' does not end the default argument.
29150 That is not yet done. */
29157 /* If we've reached the end, stop. */
29161 /* Add the token to the token block. */
29162 token
= cp_lexer_consume_token (parser
->lexer
);
29165 /* Create a DEFAULT_ARG to represent the unparsed default
29167 default_argument
= make_node (DEFAULT_ARG
);
29168 DEFARG_TOKENS (default_argument
)
29169 = cp_token_cache_new (first_token
, token
);
29170 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29172 return default_argument
;
29175 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29178 defarg_location (tree default_argument
)
29180 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29181 location_t start
= tokens
->first
->location
;
29182 location_t end
= tokens
->last
->location
;
29183 return make_location (start
, start
, end
);
29186 /* Begin parsing tentatively. We always save tokens while parsing
29187 tentatively so that if the tentative parsing fails we can restore the
29191 cp_parser_parse_tentatively (cp_parser
* parser
)
29193 /* Enter a new parsing context. */
29194 parser
->context
= cp_parser_context_new (parser
->context
);
29195 /* Begin saving tokens. */
29196 cp_lexer_save_tokens (parser
->lexer
);
29197 /* In order to avoid repetitive access control error messages,
29198 access checks are queued up until we are no longer parsing
29200 push_deferring_access_checks (dk_deferred
);
29203 /* Commit to the currently active tentative parse. */
29206 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29208 cp_parser_context
*context
;
29211 /* Mark all of the levels as committed. */
29212 lexer
= parser
->lexer
;
29213 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29215 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29217 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29218 while (!cp_lexer_saving_tokens (lexer
))
29219 lexer
= lexer
->next
;
29220 cp_lexer_commit_tokens (lexer
);
29224 /* Commit to the topmost currently active tentative parse.
29226 Note that this function shouldn't be called when there are
29227 irreversible side-effects while in a tentative state. For
29228 example, we shouldn't create a permanent entry in the symbol
29229 table, or issue an error message that might not apply if the
29230 tentative parse is aborted. */
29233 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29235 cp_parser_context
*context
= parser
->context
;
29236 cp_lexer
*lexer
= parser
->lexer
;
29240 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29242 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29244 while (!cp_lexer_saving_tokens (lexer
))
29245 lexer
= lexer
->next
;
29246 cp_lexer_commit_tokens (lexer
);
29250 /* Abort the currently active tentative parse. All consumed tokens
29251 will be rolled back, and no diagnostics will be issued. */
29254 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29256 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29257 || errorcount
> 0);
29258 cp_parser_simulate_error (parser
);
29259 /* Now, pretend that we want to see if the construct was
29260 successfully parsed. */
29261 cp_parser_parse_definitely (parser
);
29264 /* Stop parsing tentatively. If a parse error has occurred, restore the
29265 token stream. Otherwise, commit to the tokens we have consumed.
29266 Returns true if no error occurred; false otherwise. */
29269 cp_parser_parse_definitely (cp_parser
* parser
)
29271 bool error_occurred
;
29272 cp_parser_context
*context
;
29274 /* Remember whether or not an error occurred, since we are about to
29275 destroy that information. */
29276 error_occurred
= cp_parser_error_occurred (parser
);
29277 /* Remove the topmost context from the stack. */
29278 context
= parser
->context
;
29279 parser
->context
= context
->next
;
29280 /* If no parse errors occurred, commit to the tentative parse. */
29281 if (!error_occurred
)
29283 /* Commit to the tokens read tentatively, unless that was
29285 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29286 cp_lexer_commit_tokens (parser
->lexer
);
29288 pop_to_parent_deferring_access_checks ();
29290 /* Otherwise, if errors occurred, roll back our state so that things
29291 are just as they were before we began the tentative parse. */
29294 cp_lexer_rollback_tokens (parser
->lexer
);
29295 pop_deferring_access_checks ();
29297 /* Add the context to the front of the free list. */
29298 context
->next
= cp_parser_context_free_list
;
29299 cp_parser_context_free_list
= context
;
29301 return !error_occurred
;
29304 /* Returns true if we are parsing tentatively and are not committed to
29305 this tentative parse. */
29308 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29310 return (cp_parser_parsing_tentatively (parser
)
29311 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29314 /* Returns nonzero iff an error has occurred during the most recent
29315 tentative parse. */
29318 cp_parser_error_occurred (cp_parser
* parser
)
29320 return (cp_parser_parsing_tentatively (parser
)
29321 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29324 /* Returns nonzero if GNU extensions are allowed. */
29327 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29329 return parser
->allow_gnu_extensions_p
;
29332 /* Objective-C++ Productions */
29335 /* Parse an Objective-C expression, which feeds into a primary-expression
29339 objc-message-expression
29340 objc-string-literal
29341 objc-encode-expression
29342 objc-protocol-expression
29343 objc-selector-expression
29345 Returns a tree representation of the expression. */
29348 cp_parser_objc_expression (cp_parser
* parser
)
29350 /* Try to figure out what kind of declaration is present. */
29351 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29355 case CPP_OPEN_SQUARE
:
29356 return cp_parser_objc_message_expression (parser
);
29358 case CPP_OBJC_STRING
:
29359 kwd
= cp_lexer_consume_token (parser
->lexer
);
29360 return objc_build_string_object (kwd
->u
.value
);
29363 switch (kwd
->keyword
)
29365 case RID_AT_ENCODE
:
29366 return cp_parser_objc_encode_expression (parser
);
29368 case RID_AT_PROTOCOL
:
29369 return cp_parser_objc_protocol_expression (parser
);
29371 case RID_AT_SELECTOR
:
29372 return cp_parser_objc_selector_expression (parser
);
29379 error_at (kwd
->location
,
29380 "misplaced %<@%D%> Objective-C++ construct",
29382 cp_parser_skip_to_end_of_block_or_statement (parser
);
29385 return error_mark_node
;
29388 /* Parse an Objective-C message expression.
29390 objc-message-expression:
29391 [ objc-message-receiver objc-message-args ]
29393 Returns a representation of an Objective-C message. */
29396 cp_parser_objc_message_expression (cp_parser
* parser
)
29398 tree receiver
, messageargs
;
29400 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29401 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29402 receiver
= cp_parser_objc_message_receiver (parser
);
29403 messageargs
= cp_parser_objc_message_args (parser
);
29404 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29405 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29407 tree result
= objc_build_message_expr (receiver
, messageargs
);
29409 /* Construct a location e.g.
29412 ranging from the '[' to the ']', with the caret at the start. */
29413 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29414 protected_set_expr_location (result
, combined_loc
);
29419 /* Parse an objc-message-receiver.
29421 objc-message-receiver:
29423 simple-type-specifier
29425 Returns a representation of the type or expression. */
29428 cp_parser_objc_message_receiver (cp_parser
* parser
)
29432 /* An Objective-C message receiver may be either (1) a type
29433 or (2) an expression. */
29434 cp_parser_parse_tentatively (parser
);
29435 rcv
= cp_parser_expression (parser
);
29437 /* If that worked out, fine. */
29438 if (cp_parser_parse_definitely (parser
))
29441 cp_parser_parse_tentatively (parser
);
29442 rcv
= cp_parser_simple_type_specifier (parser
,
29443 /*decl_specs=*/NULL
,
29444 CP_PARSER_FLAGS_NONE
);
29446 if (cp_parser_parse_definitely (parser
))
29447 return objc_get_class_reference (rcv
);
29449 cp_parser_error (parser
, "objective-c++ message receiver expected");
29450 return error_mark_node
;
29453 /* Parse the arguments and selectors comprising an Objective-C message.
29458 objc-selector-args , objc-comma-args
29460 objc-selector-args:
29461 objc-selector [opt] : assignment-expression
29462 objc-selector-args objc-selector [opt] : assignment-expression
29465 assignment-expression
29466 objc-comma-args , assignment-expression
29468 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29469 selector arguments and TREE_VALUE containing a list of comma
29473 cp_parser_objc_message_args (cp_parser
* parser
)
29475 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29476 bool maybe_unary_selector_p
= true;
29477 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29479 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29481 tree selector
= NULL_TREE
, arg
;
29483 if (token
->type
!= CPP_COLON
)
29484 selector
= cp_parser_objc_selector (parser
);
29486 /* Detect if we have a unary selector. */
29487 if (maybe_unary_selector_p
29488 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29489 return build_tree_list (selector
, NULL_TREE
);
29491 maybe_unary_selector_p
= false;
29492 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29493 arg
= cp_parser_assignment_expression (parser
);
29496 = chainon (sel_args
,
29497 build_tree_list (selector
, arg
));
29499 token
= cp_lexer_peek_token (parser
->lexer
);
29502 /* Handle non-selector arguments, if any. */
29503 while (token
->type
== CPP_COMMA
)
29507 cp_lexer_consume_token (parser
->lexer
);
29508 arg
= cp_parser_assignment_expression (parser
);
29511 = chainon (addl_args
,
29512 build_tree_list (NULL_TREE
, arg
));
29514 token
= cp_lexer_peek_token (parser
->lexer
);
29517 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29519 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29520 return build_tree_list (error_mark_node
, error_mark_node
);
29523 return build_tree_list (sel_args
, addl_args
);
29526 /* Parse an Objective-C encode expression.
29528 objc-encode-expression:
29529 @encode objc-typename
29531 Returns an encoded representation of the type argument. */
29534 cp_parser_objc_encode_expression (cp_parser
* parser
)
29538 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29540 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29541 matching_parens parens
;
29542 parens
.require_open (parser
);
29543 token
= cp_lexer_peek_token (parser
->lexer
);
29544 type
= complete_type (cp_parser_type_id (parser
));
29545 parens
.require_close (parser
);
29549 error_at (token
->location
,
29550 "%<@encode%> must specify a type as an argument");
29551 return error_mark_node
;
29554 /* This happens if we find @encode(T) (where T is a template
29555 typename or something dependent on a template typename) when
29556 parsing a template. In that case, we can't compile it
29557 immediately, but we rather create an AT_ENCODE_EXPR which will
29558 need to be instantiated when the template is used.
29560 if (dependent_type_p (type
))
29562 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29563 TREE_READONLY (value
) = 1;
29568 /* Build a location of the form:
29571 with caret==start at the @ token, finishing at the close paren. */
29572 location_t combined_loc
29573 = make_location (start_loc
, start_loc
,
29574 cp_lexer_previous_token (parser
->lexer
)->location
);
29576 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29579 /* Parse an Objective-C @defs expression. */
29582 cp_parser_objc_defs_expression (cp_parser
*parser
)
29586 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29587 matching_parens parens
;
29588 parens
.require_open (parser
);
29589 name
= cp_parser_identifier (parser
);
29590 parens
.require_close (parser
);
29592 return objc_get_class_ivars (name
);
29595 /* Parse an Objective-C protocol expression.
29597 objc-protocol-expression:
29598 @protocol ( identifier )
29600 Returns a representation of the protocol expression. */
29603 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29606 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29608 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29609 matching_parens parens
;
29610 parens
.require_open (parser
);
29611 proto
= cp_parser_identifier (parser
);
29612 parens
.require_close (parser
);
29614 /* Build a location of the form:
29617 with caret==start at the @ token, finishing at the close paren. */
29618 location_t combined_loc
29619 = make_location (start_loc
, start_loc
,
29620 cp_lexer_previous_token (parser
->lexer
)->location
);
29621 tree result
= objc_build_protocol_expr (proto
);
29622 protected_set_expr_location (result
, combined_loc
);
29626 /* Parse an Objective-C selector expression.
29628 objc-selector-expression:
29629 @selector ( objc-method-signature )
29631 objc-method-signature:
29637 objc-selector-seq objc-selector :
29639 Returns a representation of the method selector. */
29642 cp_parser_objc_selector_expression (cp_parser
* parser
)
29644 tree sel_seq
= NULL_TREE
;
29645 bool maybe_unary_selector_p
= true;
29647 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29649 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29650 matching_parens parens
;
29651 parens
.require_open (parser
);
29652 token
= cp_lexer_peek_token (parser
->lexer
);
29654 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29655 || token
->type
== CPP_SCOPE
)
29657 tree selector
= NULL_TREE
;
29659 if (token
->type
!= CPP_COLON
29660 || token
->type
== CPP_SCOPE
)
29661 selector
= cp_parser_objc_selector (parser
);
29663 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29664 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29666 /* Detect if we have a unary selector. */
29667 if (maybe_unary_selector_p
)
29669 sel_seq
= selector
;
29670 goto finish_selector
;
29674 cp_parser_error (parser
, "expected %<:%>");
29677 maybe_unary_selector_p
= false;
29678 token
= cp_lexer_consume_token (parser
->lexer
);
29680 if (token
->type
== CPP_SCOPE
)
29683 = chainon (sel_seq
,
29684 build_tree_list (selector
, NULL_TREE
));
29686 = chainon (sel_seq
,
29687 build_tree_list (NULL_TREE
, NULL_TREE
));
29691 = chainon (sel_seq
,
29692 build_tree_list (selector
, NULL_TREE
));
29694 token
= cp_lexer_peek_token (parser
->lexer
);
29698 parens
.require_close (parser
);
29701 /* Build a location of the form:
29704 with caret==start at the @ token, finishing at the close paren. */
29705 location_t combined_loc
29706 = make_location (loc
, loc
,
29707 cp_lexer_previous_token (parser
->lexer
)->location
);
29708 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29709 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29710 protected_set_expr_location (result
, combined_loc
);
29714 /* Parse a list of identifiers.
29716 objc-identifier-list:
29718 objc-identifier-list , identifier
29720 Returns a TREE_LIST of identifier nodes. */
29723 cp_parser_objc_identifier_list (cp_parser
* parser
)
29729 identifier
= cp_parser_identifier (parser
);
29730 if (identifier
== error_mark_node
)
29731 return error_mark_node
;
29733 list
= build_tree_list (NULL_TREE
, identifier
);
29734 sep
= cp_lexer_peek_token (parser
->lexer
);
29736 while (sep
->type
== CPP_COMMA
)
29738 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29739 identifier
= cp_parser_identifier (parser
);
29740 if (identifier
== error_mark_node
)
29743 list
= chainon (list
, build_tree_list (NULL_TREE
,
29745 sep
= cp_lexer_peek_token (parser
->lexer
);
29751 /* Parse an Objective-C alias declaration.
29753 objc-alias-declaration:
29754 @compatibility_alias identifier identifier ;
29756 This function registers the alias mapping with the Objective-C front end.
29757 It returns nothing. */
29760 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29764 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29765 alias
= cp_parser_identifier (parser
);
29766 orig
= cp_parser_identifier (parser
);
29767 objc_declare_alias (alias
, orig
);
29768 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29771 /* Parse an Objective-C class forward-declaration.
29773 objc-class-declaration:
29774 @class objc-identifier-list ;
29776 The function registers the forward declarations with the Objective-C
29777 front end. It returns nothing. */
29780 cp_parser_objc_class_declaration (cp_parser
* parser
)
29782 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29787 id
= cp_parser_identifier (parser
);
29788 if (id
== error_mark_node
)
29791 objc_declare_class (id
);
29793 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29794 cp_lexer_consume_token (parser
->lexer
);
29798 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29801 /* Parse a list of Objective-C protocol references.
29803 objc-protocol-refs-opt:
29804 objc-protocol-refs [opt]
29806 objc-protocol-refs:
29807 < objc-identifier-list >
29809 Returns a TREE_LIST of identifiers, if any. */
29812 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29814 tree protorefs
= NULL_TREE
;
29816 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29818 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29819 protorefs
= cp_parser_objc_identifier_list (parser
);
29820 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29826 /* Parse a Objective-C visibility specification. */
29829 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29831 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29833 switch (vis
->keyword
)
29835 case RID_AT_PRIVATE
:
29836 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29838 case RID_AT_PROTECTED
:
29839 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29841 case RID_AT_PUBLIC
:
29842 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29844 case RID_AT_PACKAGE
:
29845 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29851 /* Eat '@private'/'@protected'/'@public'. */
29852 cp_lexer_consume_token (parser
->lexer
);
29855 /* Parse an Objective-C method type. Return 'true' if it is a class
29856 (+) method, and 'false' if it is an instance (-) method. */
29859 cp_parser_objc_method_type (cp_parser
* parser
)
29861 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29867 /* Parse an Objective-C protocol qualifier. */
29870 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29872 tree quals
= NULL_TREE
, node
;
29873 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29875 node
= token
->u
.value
;
29877 while (node
&& identifier_p (node
)
29878 && (node
== ridpointers
[(int) RID_IN
]
29879 || node
== ridpointers
[(int) RID_OUT
]
29880 || node
== ridpointers
[(int) RID_INOUT
]
29881 || node
== ridpointers
[(int) RID_BYCOPY
]
29882 || node
== ridpointers
[(int) RID_BYREF
]
29883 || node
== ridpointers
[(int) RID_ONEWAY
]))
29885 quals
= tree_cons (NULL_TREE
, node
, quals
);
29886 cp_lexer_consume_token (parser
->lexer
);
29887 token
= cp_lexer_peek_token (parser
->lexer
);
29888 node
= token
->u
.value
;
29894 /* Parse an Objective-C typename. */
29897 cp_parser_objc_typename (cp_parser
* parser
)
29899 tree type_name
= NULL_TREE
;
29901 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29903 tree proto_quals
, cp_type
= NULL_TREE
;
29905 matching_parens parens
;
29906 parens
.consume_open (parser
); /* Eat '('. */
29907 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29909 /* An ObjC type name may consist of just protocol qualifiers, in which
29910 case the type shall default to 'id'. */
29911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29913 cp_type
= cp_parser_type_id (parser
);
29915 /* If the type could not be parsed, an error has already
29916 been produced. For error recovery, behave as if it had
29917 not been specified, which will use the default type
29919 if (cp_type
== error_mark_node
)
29921 cp_type
= NULL_TREE
;
29922 /* We need to skip to the closing parenthesis as
29923 cp_parser_type_id() does not seem to do it for
29925 cp_parser_skip_to_closing_parenthesis (parser
,
29926 /*recovering=*/true,
29927 /*or_comma=*/false,
29928 /*consume_paren=*/false);
29932 parens
.require_close (parser
);
29933 type_name
= build_tree_list (proto_quals
, cp_type
);
29939 /* Check to see if TYPE refers to an Objective-C selector name. */
29942 cp_parser_objc_selector_p (enum cpp_ttype type
)
29944 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29945 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29946 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29947 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29948 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29951 /* Parse an Objective-C selector. */
29954 cp_parser_objc_selector (cp_parser
* parser
)
29956 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29958 if (!cp_parser_objc_selector_p (token
->type
))
29960 error_at (token
->location
, "invalid Objective-C++ selector name");
29961 return error_mark_node
;
29964 /* C++ operator names are allowed to appear in ObjC selectors. */
29965 switch (token
->type
)
29967 case CPP_AND_AND
: return get_identifier ("and");
29968 case CPP_AND_EQ
: return get_identifier ("and_eq");
29969 case CPP_AND
: return get_identifier ("bitand");
29970 case CPP_OR
: return get_identifier ("bitor");
29971 case CPP_COMPL
: return get_identifier ("compl");
29972 case CPP_NOT
: return get_identifier ("not");
29973 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29974 case CPP_OR_OR
: return get_identifier ("or");
29975 case CPP_OR_EQ
: return get_identifier ("or_eq");
29976 case CPP_XOR
: return get_identifier ("xor");
29977 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29978 default: return token
->u
.value
;
29982 /* Parse an Objective-C params list. */
29985 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29987 tree params
= NULL_TREE
;
29988 bool maybe_unary_selector_p
= true;
29989 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29991 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29993 tree selector
= NULL_TREE
, type_name
, identifier
;
29994 tree parm_attr
= NULL_TREE
;
29996 if (token
->keyword
== RID_ATTRIBUTE
)
29999 if (token
->type
!= CPP_COLON
)
30000 selector
= cp_parser_objc_selector (parser
);
30002 /* Detect if we have a unary selector. */
30003 if (maybe_unary_selector_p
30004 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30006 params
= selector
; /* Might be followed by attributes. */
30010 maybe_unary_selector_p
= false;
30011 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30013 /* Something went quite wrong. There should be a colon
30014 here, but there is not. Stop parsing parameters. */
30017 type_name
= cp_parser_objc_typename (parser
);
30018 /* New ObjC allows attributes on parameters too. */
30019 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30020 parm_attr
= cp_parser_attributes_opt (parser
);
30021 identifier
= cp_parser_identifier (parser
);
30025 objc_build_keyword_decl (selector
,
30030 token
= cp_lexer_peek_token (parser
->lexer
);
30033 if (params
== NULL_TREE
)
30035 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30036 return error_mark_node
;
30039 /* We allow tail attributes for the method. */
30040 if (token
->keyword
== RID_ATTRIBUTE
)
30042 *attributes
= cp_parser_attributes_opt (parser
);
30043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30044 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30046 cp_parser_error (parser
,
30047 "method attributes must be specified at the end");
30048 return error_mark_node
;
30051 if (params
== NULL_TREE
)
30053 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30054 return error_mark_node
;
30059 /* Parse the non-keyword Objective-C params. */
30062 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30065 tree params
= make_node (TREE_LIST
);
30066 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30067 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30069 while (token
->type
== CPP_COMMA
)
30071 cp_parameter_declarator
*parmdecl
;
30074 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30075 token
= cp_lexer_peek_token (parser
->lexer
);
30077 if (token
->type
== CPP_ELLIPSIS
)
30079 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30081 token
= cp_lexer_peek_token (parser
->lexer
);
30085 /* TODO: parse attributes for tail parameters. */
30086 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30087 parm
= grokdeclarator (parmdecl
->declarator
,
30088 &parmdecl
->decl_specifiers
,
30089 PARM
, /*initialized=*/0,
30090 /*attrlist=*/NULL
);
30092 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30093 token
= cp_lexer_peek_token (parser
->lexer
);
30096 /* We allow tail attributes for the method. */
30097 if (token
->keyword
== RID_ATTRIBUTE
)
30099 if (*attributes
== NULL_TREE
)
30101 *attributes
= cp_parser_attributes_opt (parser
);
30102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30103 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30107 /* We have an error, but parse the attributes, so that we can
30109 *attributes
= cp_parser_attributes_opt (parser
);
30111 cp_parser_error (parser
,
30112 "method attributes must be specified at the end");
30113 return error_mark_node
;
30119 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30122 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30124 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30126 /* If the next token is `extern' and the following token is a string
30127 literal, then we have a linkage specification. */
30128 if (token
->keyword
== RID_EXTERN
30129 && cp_parser_is_pure_string_literal
30130 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30131 cp_parser_linkage_specification (parser
);
30132 /* Handle #pragma, if any. */
30133 else if (token
->type
== CPP_PRAGMA
)
30134 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30135 /* Allow stray semicolons. */
30136 else if (token
->type
== CPP_SEMICOLON
)
30137 cp_lexer_consume_token (parser
->lexer
);
30138 /* Mark methods as optional or required, when building protocols. */
30139 else if (token
->keyword
== RID_AT_OPTIONAL
)
30141 cp_lexer_consume_token (parser
->lexer
);
30142 objc_set_method_opt (true);
30144 else if (token
->keyword
== RID_AT_REQUIRED
)
30146 cp_lexer_consume_token (parser
->lexer
);
30147 objc_set_method_opt (false);
30149 else if (token
->keyword
== RID_NAMESPACE
)
30150 cp_parser_namespace_definition (parser
);
30151 /* Other stray characters must generate errors. */
30152 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30154 cp_lexer_consume_token (parser
->lexer
);
30155 error ("stray %qs between Objective-C++ methods",
30156 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30158 /* Finally, try to parse a block-declaration, or a function-definition. */
30160 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30163 /* Parse a method signature. */
30166 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30168 tree rettype
, kwdparms
, optparms
;
30169 bool ellipsis
= false;
30170 bool is_class_method
;
30172 is_class_method
= cp_parser_objc_method_type (parser
);
30173 rettype
= cp_parser_objc_typename (parser
);
30174 *attributes
= NULL_TREE
;
30175 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30176 if (kwdparms
== error_mark_node
)
30177 return error_mark_node
;
30178 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30179 if (optparms
== error_mark_node
)
30180 return error_mark_node
;
30182 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30186 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30189 cp_lexer_save_tokens (parser
->lexer
);
30190 tattr
= cp_parser_attributes_opt (parser
);
30191 gcc_assert (tattr
) ;
30193 /* If the attributes are followed by a method introducer, this is not allowed.
30194 Dump the attributes and flag the situation. */
30195 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30196 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30199 /* Otherwise, the attributes introduce some interstitial code, possibly so
30200 rewind to allow that check. */
30201 cp_lexer_rollback_tokens (parser
->lexer
);
30205 /* Parse an Objective-C method prototype list. */
30208 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30210 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30212 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30214 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30216 tree attributes
, sig
;
30217 bool is_class_method
;
30218 if (token
->type
== CPP_PLUS
)
30219 is_class_method
= true;
30221 is_class_method
= false;
30222 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30223 if (sig
== error_mark_node
)
30225 cp_parser_skip_to_end_of_block_or_statement (parser
);
30226 token
= cp_lexer_peek_token (parser
->lexer
);
30229 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30230 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30232 else if (token
->keyword
== RID_AT_PROPERTY
)
30233 cp_parser_objc_at_property_declaration (parser
);
30234 else if (token
->keyword
== RID_ATTRIBUTE
30235 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30236 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30238 "prefix attributes are ignored for methods");
30240 /* Allow for interspersed non-ObjC++ code. */
30241 cp_parser_objc_interstitial_code (parser
);
30243 token
= cp_lexer_peek_token (parser
->lexer
);
30246 if (token
->type
!= CPP_EOF
)
30247 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30249 cp_parser_error (parser
, "expected %<@end%>");
30251 objc_finish_interface ();
30254 /* Parse an Objective-C method definition list. */
30257 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30259 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30261 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30265 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30268 tree sig
, attribute
;
30269 bool is_class_method
;
30270 if (token
->type
== CPP_PLUS
)
30271 is_class_method
= true;
30273 is_class_method
= false;
30274 push_deferring_access_checks (dk_deferred
);
30275 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30276 if (sig
== error_mark_node
)
30278 cp_parser_skip_to_end_of_block_or_statement (parser
);
30279 token
= cp_lexer_peek_token (parser
->lexer
);
30282 objc_start_method_definition (is_class_method
, sig
, attribute
,
30285 /* For historical reasons, we accept an optional semicolon. */
30286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30287 cp_lexer_consume_token (parser
->lexer
);
30289 ptk
= cp_lexer_peek_token (parser
->lexer
);
30290 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30291 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30293 perform_deferred_access_checks (tf_warning_or_error
);
30294 stop_deferring_access_checks ();
30295 meth
= cp_parser_function_definition_after_declarator (parser
,
30297 pop_deferring_access_checks ();
30298 objc_finish_method_definition (meth
);
30301 /* The following case will be removed once @synthesize is
30302 completely implemented. */
30303 else if (token
->keyword
== RID_AT_PROPERTY
)
30304 cp_parser_objc_at_property_declaration (parser
);
30305 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30306 cp_parser_objc_at_synthesize_declaration (parser
);
30307 else if (token
->keyword
== RID_AT_DYNAMIC
)
30308 cp_parser_objc_at_dynamic_declaration (parser
);
30309 else if (token
->keyword
== RID_ATTRIBUTE
30310 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30311 warning_at (token
->location
, OPT_Wattributes
,
30312 "prefix attributes are ignored for methods");
30314 /* Allow for interspersed non-ObjC++ code. */
30315 cp_parser_objc_interstitial_code (parser
);
30317 token
= cp_lexer_peek_token (parser
->lexer
);
30320 if (token
->type
!= CPP_EOF
)
30321 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30323 cp_parser_error (parser
, "expected %<@end%>");
30325 objc_finish_implementation ();
30328 /* Parse Objective-C ivars. */
30331 cp_parser_objc_class_ivars (cp_parser
* parser
)
30333 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30335 if (token
->type
!= CPP_OPEN_BRACE
)
30336 return; /* No ivars specified. */
30338 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30339 token
= cp_lexer_peek_token (parser
->lexer
);
30341 while (token
->type
!= CPP_CLOSE_BRACE
30342 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30344 cp_decl_specifier_seq declspecs
;
30345 int decl_class_or_enum_p
;
30346 tree prefix_attributes
;
30348 cp_parser_objc_visibility_spec (parser
);
30350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30353 cp_parser_decl_specifier_seq (parser
,
30354 CP_PARSER_FLAGS_OPTIONAL
,
30356 &decl_class_or_enum_p
);
30358 /* auto, register, static, extern, mutable. */
30359 if (declspecs
.storage_class
!= sc_none
)
30361 cp_parser_error (parser
, "invalid type for instance variable");
30362 declspecs
.storage_class
= sc_none
;
30365 /* thread_local. */
30366 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30368 cp_parser_error (parser
, "invalid type for instance variable");
30369 declspecs
.locations
[ds_thread
] = 0;
30373 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30375 cp_parser_error (parser
, "invalid type for instance variable");
30376 declspecs
.locations
[ds_typedef
] = 0;
30379 prefix_attributes
= declspecs
.attributes
;
30380 declspecs
.attributes
= NULL_TREE
;
30382 /* Keep going until we hit the `;' at the end of the
30384 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30386 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30387 cp_declarator
*declarator
= NULL
;
30388 int ctor_dtor_or_conv_p
;
30390 /* Check for a (possibly unnamed) bitfield declaration. */
30391 token
= cp_lexer_peek_token (parser
->lexer
);
30392 if (token
->type
== CPP_COLON
)
30395 if (token
->type
== CPP_NAME
30396 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30399 /* Get the name of the bitfield. */
30400 declarator
= make_id_declarator (NULL_TREE
,
30401 cp_parser_identifier (parser
),
30405 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30406 /* Get the width of the bitfield. */
30408 = cp_parser_constant_expression (parser
);
30412 /* Parse the declarator. */
30414 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30415 &ctor_dtor_or_conv_p
,
30416 /*parenthesized_p=*/NULL
,
30417 /*member_p=*/false,
30418 /*friend_p=*/false);
30421 /* Look for attributes that apply to the ivar. */
30422 attributes
= cp_parser_attributes_opt (parser
);
30423 /* Remember which attributes are prefix attributes and
30425 first_attribute
= attributes
;
30426 /* Combine the attributes. */
30427 attributes
= attr_chainon (prefix_attributes
, attributes
);
30430 /* Create the bitfield declaration. */
30431 decl
= grokbitfield (declarator
, &declspecs
,
30432 width
, NULL_TREE
, attributes
);
30434 decl
= grokfield (declarator
, &declspecs
,
30435 NULL_TREE
, /*init_const_expr_p=*/false,
30436 NULL_TREE
, attributes
);
30438 /* Add the instance variable. */
30439 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30440 objc_add_instance_variable (decl
);
30442 /* Reset PREFIX_ATTRIBUTES. */
30443 if (attributes
!= error_mark_node
)
30445 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30446 attributes
= TREE_CHAIN (attributes
);
30448 TREE_CHAIN (attributes
) = NULL_TREE
;
30451 token
= cp_lexer_peek_token (parser
->lexer
);
30453 if (token
->type
== CPP_COMMA
)
30455 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30461 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30462 token
= cp_lexer_peek_token (parser
->lexer
);
30465 if (token
->keyword
== RID_AT_END
)
30466 cp_parser_error (parser
, "expected %<}%>");
30468 /* Do not consume the RID_AT_END, so it will be read again as terminating
30469 the @interface of @implementation. */
30470 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30471 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30473 /* For historical reasons, we accept an optional semicolon. */
30474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30475 cp_lexer_consume_token (parser
->lexer
);
30478 /* Parse an Objective-C protocol declaration. */
30481 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30483 tree proto
, protorefs
;
30486 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30487 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30489 tok
= cp_lexer_peek_token (parser
->lexer
);
30490 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30491 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30495 /* See if we have a forward declaration or a definition. */
30496 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30498 /* Try a forward declaration first. */
30499 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30505 id
= cp_parser_identifier (parser
);
30506 if (id
== error_mark_node
)
30509 objc_declare_protocol (id
, attributes
);
30511 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30512 cp_lexer_consume_token (parser
->lexer
);
30516 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30519 /* Ok, we got a full-fledged definition (or at least should). */
30522 proto
= cp_parser_identifier (parser
);
30523 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30524 objc_start_protocol (proto
, protorefs
, attributes
);
30525 cp_parser_objc_method_prototype_list (parser
);
30529 /* Parse an Objective-C superclass or category. */
30532 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30535 tree
*categ
, bool *is_class_extension
)
30537 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30539 *super
= *categ
= NULL_TREE
;
30540 *is_class_extension
= false;
30541 if (next
->type
== CPP_COLON
)
30543 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30544 *super
= cp_parser_identifier (parser
);
30546 else if (next
->type
== CPP_OPEN_PAREN
)
30548 matching_parens parens
;
30549 parens
.consume_open (parser
); /* Eat '('. */
30551 /* If there is no category name, and this is an @interface, we
30552 have a class extension. */
30553 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30555 *categ
= NULL_TREE
;
30556 *is_class_extension
= true;
30559 *categ
= cp_parser_identifier (parser
);
30561 parens
.require_close (parser
);
30565 /* Parse an Objective-C class interface. */
30568 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30570 tree name
, super
, categ
, protos
;
30571 bool is_class_extension
;
30573 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30574 name
= cp_parser_identifier (parser
);
30575 if (name
== error_mark_node
)
30577 /* It's hard to recover because even if valid @interface stuff
30578 is to follow, we can't compile it (or validate it) if we
30579 don't even know which class it refers to. Let's assume this
30580 was a stray '@interface' token in the stream and skip it.
30584 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30585 &is_class_extension
);
30586 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30588 /* We have either a class or a category on our hands. */
30589 if (categ
|| is_class_extension
)
30590 objc_start_category_interface (name
, categ
, protos
, attributes
);
30593 objc_start_class_interface (name
, super
, protos
, attributes
);
30594 /* Handle instance variable declarations, if any. */
30595 cp_parser_objc_class_ivars (parser
);
30596 objc_continue_interface ();
30599 cp_parser_objc_method_prototype_list (parser
);
30602 /* Parse an Objective-C class implementation. */
30605 cp_parser_objc_class_implementation (cp_parser
* parser
)
30607 tree name
, super
, categ
;
30608 bool is_class_extension
;
30610 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30611 name
= cp_parser_identifier (parser
);
30612 if (name
== error_mark_node
)
30614 /* It's hard to recover because even if valid @implementation
30615 stuff is to follow, we can't compile it (or validate it) if
30616 we don't even know which class it refers to. Let's assume
30617 this was a stray '@implementation' token in the stream and
30622 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30623 &is_class_extension
);
30625 /* We have either a class or a category on our hands. */
30627 objc_start_category_implementation (name
, categ
);
30630 objc_start_class_implementation (name
, super
);
30631 /* Handle instance variable declarations, if any. */
30632 cp_parser_objc_class_ivars (parser
);
30633 objc_continue_implementation ();
30636 cp_parser_objc_method_definition_list (parser
);
30639 /* Consume the @end token and finish off the implementation. */
30642 cp_parser_objc_end_implementation (cp_parser
* parser
)
30644 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30645 objc_finish_implementation ();
30648 /* Parse an Objective-C declaration. */
30651 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30653 /* Try to figure out what kind of declaration is present. */
30654 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30657 switch (kwd
->keyword
)
30662 error_at (kwd
->location
, "attributes may not be specified before"
30663 " the %<@%D%> Objective-C++ keyword",
30667 case RID_AT_IMPLEMENTATION
:
30668 warning_at (kwd
->location
, OPT_Wattributes
,
30669 "prefix attributes are ignored before %<@%D%>",
30676 switch (kwd
->keyword
)
30679 cp_parser_objc_alias_declaration (parser
);
30682 cp_parser_objc_class_declaration (parser
);
30684 case RID_AT_PROTOCOL
:
30685 cp_parser_objc_protocol_declaration (parser
, attributes
);
30687 case RID_AT_INTERFACE
:
30688 cp_parser_objc_class_interface (parser
, attributes
);
30690 case RID_AT_IMPLEMENTATION
:
30691 cp_parser_objc_class_implementation (parser
);
30694 cp_parser_objc_end_implementation (parser
);
30697 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30699 cp_parser_skip_to_end_of_block_or_statement (parser
);
30703 /* Parse an Objective-C try-catch-finally statement.
30705 objc-try-catch-finally-stmt:
30706 @try compound-statement objc-catch-clause-seq [opt]
30707 objc-finally-clause [opt]
30709 objc-catch-clause-seq:
30710 objc-catch-clause objc-catch-clause-seq [opt]
30713 @catch ( objc-exception-declaration ) compound-statement
30715 objc-finally-clause:
30716 @finally compound-statement
30718 objc-exception-declaration:
30719 parameter-declaration
30722 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30726 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30727 for C. Keep them in sync. */
30730 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30732 location_t location
;
30735 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30736 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30737 objc_maybe_warn_exceptions (location
);
30738 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30739 node, lest it get absorbed into the surrounding block. */
30740 stmt
= push_stmt_list ();
30741 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30742 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30744 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30746 cp_parameter_declarator
*parm
;
30747 tree parameter_declaration
= error_mark_node
;
30748 bool seen_open_paren
= false;
30749 matching_parens parens
;
30751 cp_lexer_consume_token (parser
->lexer
);
30752 if (parens
.require_open (parser
))
30753 seen_open_paren
= true;
30754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30756 /* We have "@catch (...)" (where the '...' are literally
30757 what is in the code). Skip the '...'.
30758 parameter_declaration is set to NULL_TREE, and
30759 objc_being_catch_clauses() knows that that means
30761 cp_lexer_consume_token (parser
->lexer
);
30762 parameter_declaration
= NULL_TREE
;
30766 /* We have "@catch (NSException *exception)" or something
30767 like that. Parse the parameter declaration. */
30768 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30770 parameter_declaration
= error_mark_node
;
30772 parameter_declaration
= grokdeclarator (parm
->declarator
,
30773 &parm
->decl_specifiers
,
30774 PARM
, /*initialized=*/0,
30775 /*attrlist=*/NULL
);
30777 if (seen_open_paren
)
30778 parens
.require_close (parser
);
30781 /* If there was no open parenthesis, we are recovering from
30782 an error, and we are trying to figure out what mistake
30783 the user has made. */
30785 /* If there is an immediate closing parenthesis, the user
30786 probably forgot the opening one (ie, they typed "@catch
30787 NSException *e)". Parse the closing parenthesis and keep
30789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30790 cp_lexer_consume_token (parser
->lexer
);
30792 /* If these is no immediate closing parenthesis, the user
30793 probably doesn't know that parenthesis are required at
30794 all (ie, they typed "@catch NSException *e"). So, just
30795 forget about the closing parenthesis and keep going. */
30797 objc_begin_catch_clause (parameter_declaration
);
30798 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30799 objc_finish_catch_clause ();
30801 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30803 cp_lexer_consume_token (parser
->lexer
);
30804 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30805 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30806 node, lest it get absorbed into the surrounding block. */
30807 stmt
= push_stmt_list ();
30808 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30809 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30812 return objc_finish_try_stmt ();
30815 /* Parse an Objective-C synchronized statement.
30817 objc-synchronized-stmt:
30818 @synchronized ( expression ) compound-statement
30820 Returns NULL_TREE. */
30823 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30825 location_t location
;
30828 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30830 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30831 objc_maybe_warn_exceptions (location
);
30832 matching_parens parens
;
30833 parens
.require_open (parser
);
30834 lock
= cp_parser_expression (parser
);
30835 parens
.require_close (parser
);
30837 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30838 node, lest it get absorbed into the surrounding block. */
30839 stmt
= push_stmt_list ();
30840 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30842 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30845 /* Parse an Objective-C throw statement.
30848 @throw assignment-expression [opt] ;
30850 Returns a constructed '@throw' statement. */
30853 cp_parser_objc_throw_statement (cp_parser
*parser
)
30855 tree expr
= NULL_TREE
;
30856 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30858 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30860 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30861 expr
= cp_parser_expression (parser
);
30863 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30865 return objc_build_throw_stmt (loc
, expr
);
30868 /* Parse an Objective-C statement. */
30871 cp_parser_objc_statement (cp_parser
* parser
)
30873 /* Try to figure out what kind of declaration is present. */
30874 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30876 switch (kwd
->keyword
)
30879 return cp_parser_objc_try_catch_finally_statement (parser
);
30880 case RID_AT_SYNCHRONIZED
:
30881 return cp_parser_objc_synchronized_statement (parser
);
30883 return cp_parser_objc_throw_statement (parser
);
30885 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30887 cp_parser_skip_to_end_of_block_or_statement (parser
);
30890 return error_mark_node
;
30893 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30894 look ahead to see if an objc keyword follows the attributes. This
30895 is to detect the use of prefix attributes on ObjC @interface and
30899 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30901 cp_lexer_save_tokens (parser
->lexer
);
30902 *attrib
= cp_parser_attributes_opt (parser
);
30903 gcc_assert (*attrib
);
30904 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30906 cp_lexer_commit_tokens (parser
->lexer
);
30909 cp_lexer_rollback_tokens (parser
->lexer
);
30913 /* This routine is a minimal replacement for
30914 c_parser_struct_declaration () used when parsing the list of
30915 types/names or ObjC++ properties. For example, when parsing the
30918 @property (readonly) int a, b, c;
30920 this function is responsible for parsing "int a, int b, int c" and
30921 returning the declarations as CHAIN of DECLs.
30923 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30924 similar parsing. */
30926 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30928 tree decls
= NULL_TREE
;
30929 cp_decl_specifier_seq declspecs
;
30930 int decl_class_or_enum_p
;
30931 tree prefix_attributes
;
30933 cp_parser_decl_specifier_seq (parser
,
30934 CP_PARSER_FLAGS_NONE
,
30936 &decl_class_or_enum_p
);
30938 if (declspecs
.type
== error_mark_node
)
30939 return error_mark_node
;
30941 /* auto, register, static, extern, mutable. */
30942 if (declspecs
.storage_class
!= sc_none
)
30944 cp_parser_error (parser
, "invalid type for property");
30945 declspecs
.storage_class
= sc_none
;
30948 /* thread_local. */
30949 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30951 cp_parser_error (parser
, "invalid type for property");
30952 declspecs
.locations
[ds_thread
] = 0;
30956 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30958 cp_parser_error (parser
, "invalid type for property");
30959 declspecs
.locations
[ds_typedef
] = 0;
30962 prefix_attributes
= declspecs
.attributes
;
30963 declspecs
.attributes
= NULL_TREE
;
30965 /* Keep going until we hit the `;' at the end of the declaration. */
30966 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30968 tree attributes
, first_attribute
, decl
;
30969 cp_declarator
*declarator
;
30972 /* Parse the declarator. */
30973 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30974 NULL
, NULL
, false, false);
30976 /* Look for attributes that apply to the ivar. */
30977 attributes
= cp_parser_attributes_opt (parser
);
30978 /* Remember which attributes are prefix attributes and
30980 first_attribute
= attributes
;
30981 /* Combine the attributes. */
30982 attributes
= attr_chainon (prefix_attributes
, attributes
);
30984 decl
= grokfield (declarator
, &declspecs
,
30985 NULL_TREE
, /*init_const_expr_p=*/false,
30986 NULL_TREE
, attributes
);
30988 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30989 return error_mark_node
;
30991 /* Reset PREFIX_ATTRIBUTES. */
30992 if (attributes
!= error_mark_node
)
30994 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30995 attributes
= TREE_CHAIN (attributes
);
30997 TREE_CHAIN (attributes
) = NULL_TREE
;
31000 DECL_CHAIN (decl
) = decls
;
31003 token
= cp_lexer_peek_token (parser
->lexer
);
31004 if (token
->type
== CPP_COMMA
)
31006 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31015 /* Parse an Objective-C @property declaration. The syntax is:
31017 objc-property-declaration:
31018 '@property' objc-property-attributes[opt] struct-declaration ;
31020 objc-property-attributes:
31021 '(' objc-property-attribute-list ')'
31023 objc-property-attribute-list:
31024 objc-property-attribute
31025 objc-property-attribute-list, objc-property-attribute
31027 objc-property-attribute
31028 'getter' = identifier
31029 'setter' = identifier
31038 @property NSString *name;
31039 @property (readonly) id object;
31040 @property (retain, nonatomic, getter=getTheName) id name;
31041 @property int a, b, c;
31043 PS: This function is identical to
31044 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31046 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31048 /* The following variables hold the attributes of the properties as
31049 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31050 seen. When we see an attribute, we set them to 'true' (if they
31051 are boolean properties) or to the identifier (if they have an
31052 argument, ie, for getter and setter). Note that here we only
31053 parse the list of attributes, check the syntax and accumulate the
31054 attributes that we find. objc_add_property_declaration() will
31055 then process the information. */
31056 bool property_assign
= false;
31057 bool property_copy
= false;
31058 tree property_getter_ident
= NULL_TREE
;
31059 bool property_nonatomic
= false;
31060 bool property_readonly
= false;
31061 bool property_readwrite
= false;
31062 bool property_retain
= false;
31063 tree property_setter_ident
= NULL_TREE
;
31065 /* 'properties' is the list of properties that we read. Usually a
31066 single one, but maybe more (eg, in "@property int a, b, c;" there
31071 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31073 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31075 /* Parse the optional attribute list... */
31076 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31079 matching_parens parens
;
31080 parens
.consume_open (parser
);
31084 bool syntax_error
= false;
31085 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31088 if (token
->type
!= CPP_NAME
)
31090 cp_parser_error (parser
, "expected identifier");
31093 keyword
= C_RID_CODE (token
->u
.value
);
31094 cp_lexer_consume_token (parser
->lexer
);
31097 case RID_ASSIGN
: property_assign
= true; break;
31098 case RID_COPY
: property_copy
= true; break;
31099 case RID_NONATOMIC
: property_nonatomic
= true; break;
31100 case RID_READONLY
: property_readonly
= true; break;
31101 case RID_READWRITE
: property_readwrite
= true; break;
31102 case RID_RETAIN
: property_retain
= true; break;
31106 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31108 if (keyword
== RID_GETTER
)
31109 cp_parser_error (parser
,
31110 "missing %<=%> (after %<getter%> attribute)");
31112 cp_parser_error (parser
,
31113 "missing %<=%> (after %<setter%> attribute)");
31114 syntax_error
= true;
31117 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31118 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31120 cp_parser_error (parser
, "expected identifier");
31121 syntax_error
= true;
31124 if (keyword
== RID_SETTER
)
31126 if (property_setter_ident
!= NULL_TREE
)
31128 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31129 cp_lexer_consume_token (parser
->lexer
);
31132 property_setter_ident
= cp_parser_objc_selector (parser
);
31133 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31134 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31136 cp_lexer_consume_token (parser
->lexer
);
31140 if (property_getter_ident
!= NULL_TREE
)
31142 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31143 cp_lexer_consume_token (parser
->lexer
);
31146 property_getter_ident
= cp_parser_objc_selector (parser
);
31150 cp_parser_error (parser
, "unknown property attribute");
31151 syntax_error
= true;
31158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31159 cp_lexer_consume_token (parser
->lexer
);
31164 /* FIXME: "@property (setter, assign);" will generate a spurious
31165 "error: expected ‘)’ before ‘,’ token". This is because
31166 cp_parser_require, unlike the C counterpart, will produce an
31167 error even if we are in error recovery. */
31168 if (!parens
.require_close (parser
))
31170 cp_parser_skip_to_closing_parenthesis (parser
,
31171 /*recovering=*/true,
31172 /*or_comma=*/false,
31173 /*consume_paren=*/true);
31177 /* ... and the property declaration(s). */
31178 properties
= cp_parser_objc_struct_declaration (parser
);
31180 if (properties
== error_mark_node
)
31182 cp_parser_skip_to_end_of_statement (parser
);
31183 /* If the next token is now a `;', consume it. */
31184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31185 cp_lexer_consume_token (parser
->lexer
);
31189 if (properties
== NULL_TREE
)
31190 cp_parser_error (parser
, "expected identifier");
31193 /* Comma-separated properties are chained together in
31194 reverse order; add them one by one. */
31195 properties
= nreverse (properties
);
31197 for (; properties
; properties
= TREE_CHAIN (properties
))
31198 objc_add_property_declaration (loc
, copy_node (properties
),
31199 property_readonly
, property_readwrite
,
31200 property_assign
, property_retain
,
31201 property_copy
, property_nonatomic
,
31202 property_getter_ident
, property_setter_ident
);
31205 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31208 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31210 objc-synthesize-declaration:
31211 @synthesize objc-synthesize-identifier-list ;
31213 objc-synthesize-identifier-list:
31214 objc-synthesize-identifier
31215 objc-synthesize-identifier-list, objc-synthesize-identifier
31217 objc-synthesize-identifier
31219 identifier = identifier
31222 @synthesize MyProperty;
31223 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31225 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31226 for C. Keep them in sync.
31229 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31231 tree list
= NULL_TREE
;
31233 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31235 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31238 tree property
, ivar
;
31239 property
= cp_parser_identifier (parser
);
31240 if (property
== error_mark_node
)
31242 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31247 cp_lexer_consume_token (parser
->lexer
);
31248 ivar
= cp_parser_identifier (parser
);
31249 if (ivar
== error_mark_node
)
31251 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31257 list
= chainon (list
, build_tree_list (ivar
, property
));
31258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31259 cp_lexer_consume_token (parser
->lexer
);
31263 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31264 objc_add_synthesize_declaration (loc
, list
);
31267 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31269 objc-dynamic-declaration:
31270 @dynamic identifier-list ;
31273 @dynamic MyProperty;
31274 @dynamic MyProperty, AnotherProperty;
31276 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31277 for C. Keep them in sync.
31280 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31282 tree list
= NULL_TREE
;
31284 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31286 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31290 property
= cp_parser_identifier (parser
);
31291 if (property
== error_mark_node
)
31293 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31296 list
= chainon (list
, build_tree_list (NULL
, property
));
31297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31298 cp_lexer_consume_token (parser
->lexer
);
31302 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31303 objc_add_dynamic_declaration (loc
, list
);
31307 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31309 /* Returns name of the next clause.
31310 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31311 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31312 returned and the token is consumed. */
31314 static pragma_omp_clause
31315 cp_parser_omp_clause_name (cp_parser
*parser
)
31317 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31319 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31320 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31321 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31322 result
= PRAGMA_OMP_CLAUSE_IF
;
31323 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31324 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31325 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31326 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31327 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31328 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31329 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31330 result
= PRAGMA_OMP_CLAUSE_FOR
;
31331 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31333 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31334 const char *p
= IDENTIFIER_POINTER (id
);
31339 if (!strcmp ("aligned", p
))
31340 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31341 else if (!strcmp ("async", p
))
31342 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31345 if (!strcmp ("collapse", p
))
31346 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31347 else if (!strcmp ("copy", p
))
31348 result
= PRAGMA_OACC_CLAUSE_COPY
;
31349 else if (!strcmp ("copyin", p
))
31350 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31351 else if (!strcmp ("copyout", p
))
31352 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31353 else if (!strcmp ("copyprivate", p
))
31354 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31355 else if (!strcmp ("create", p
))
31356 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31359 if (!strcmp ("defaultmap", p
))
31360 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31361 else if (!strcmp ("depend", p
))
31362 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31363 else if (!strcmp ("device", p
))
31364 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31365 else if (!strcmp ("deviceptr", p
))
31366 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31367 else if (!strcmp ("device_resident", p
))
31368 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31369 else if (!strcmp ("dist_schedule", p
))
31370 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31373 if (!strcmp ("final", p
))
31374 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31375 else if (!strcmp ("firstprivate", p
))
31376 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31377 else if (!strcmp ("from", p
))
31378 result
= PRAGMA_OMP_CLAUSE_FROM
;
31381 if (!strcmp ("gang", p
))
31382 result
= PRAGMA_OACC_CLAUSE_GANG
;
31383 else if (!strcmp ("grainsize", p
))
31384 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31387 if (!strcmp ("hint", p
))
31388 result
= PRAGMA_OMP_CLAUSE_HINT
;
31389 else if (!strcmp ("host", p
))
31390 result
= PRAGMA_OACC_CLAUSE_HOST
;
31393 if (!strcmp ("inbranch", p
))
31394 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31395 else if (!strcmp ("independent", p
))
31396 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31397 else if (!strcmp ("is_device_ptr", p
))
31398 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31401 if (!strcmp ("lastprivate", p
))
31402 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31403 else if (!strcmp ("linear", p
))
31404 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31405 else if (!strcmp ("link", p
))
31406 result
= PRAGMA_OMP_CLAUSE_LINK
;
31409 if (!strcmp ("map", p
))
31410 result
= PRAGMA_OMP_CLAUSE_MAP
;
31411 else if (!strcmp ("mergeable", p
))
31412 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31415 if (!strcmp ("nogroup", p
))
31416 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31417 else if (!strcmp ("notinbranch", p
))
31418 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31419 else if (!strcmp ("nowait", p
))
31420 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31421 else if (!strcmp ("num_gangs", p
))
31422 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31423 else if (!strcmp ("num_tasks", p
))
31424 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31425 else if (!strcmp ("num_teams", p
))
31426 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31427 else if (!strcmp ("num_threads", p
))
31428 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31429 else if (!strcmp ("num_workers", p
))
31430 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31433 if (!strcmp ("ordered", p
))
31434 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31437 if (!strcmp ("parallel", p
))
31438 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31439 else if (!strcmp ("present", p
))
31440 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31441 else if (!strcmp ("present_or_copy", p
)
31442 || !strcmp ("pcopy", p
))
31443 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31444 else if (!strcmp ("present_or_copyin", p
)
31445 || !strcmp ("pcopyin", p
))
31446 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31447 else if (!strcmp ("present_or_copyout", p
)
31448 || !strcmp ("pcopyout", p
))
31449 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31450 else if (!strcmp ("present_or_create", p
)
31451 || !strcmp ("pcreate", p
))
31452 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31453 else if (!strcmp ("priority", p
))
31454 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31455 else if (!strcmp ("proc_bind", p
))
31456 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31459 if (!strcmp ("reduction", p
))
31460 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31463 if (!strcmp ("safelen", p
))
31464 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31465 else if (!strcmp ("schedule", p
))
31466 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31467 else if (!strcmp ("sections", p
))
31468 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31469 else if (!strcmp ("self", p
))
31470 result
= PRAGMA_OACC_CLAUSE_SELF
;
31471 else if (!strcmp ("seq", p
))
31472 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31473 else if (!strcmp ("shared", p
))
31474 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31475 else if (!strcmp ("simd", p
))
31476 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31477 else if (!strcmp ("simdlen", p
))
31478 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31481 if (!strcmp ("taskgroup", p
))
31482 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31483 else if (!strcmp ("thread_limit", p
))
31484 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31485 else if (!strcmp ("threads", p
))
31486 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31487 else if (!strcmp ("tile", p
))
31488 result
= PRAGMA_OACC_CLAUSE_TILE
;
31489 else if (!strcmp ("to", p
))
31490 result
= PRAGMA_OMP_CLAUSE_TO
;
31493 if (!strcmp ("uniform", p
))
31494 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31495 else if (!strcmp ("untied", p
))
31496 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31497 else if (!strcmp ("use_device", p
))
31498 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31499 else if (!strcmp ("use_device_ptr", p
))
31500 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31503 if (!strcmp ("vector", p
))
31504 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31505 else if (!strcmp ("vector_length", p
))
31506 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31509 if (!strcmp ("wait", p
))
31510 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31511 else if (!strcmp ("worker", p
))
31512 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31517 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31518 cp_lexer_consume_token (parser
->lexer
);
31523 /* Validate that a clause of the given type does not already exist. */
31526 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31527 const char *name
, location_t location
)
31531 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31532 if (OMP_CLAUSE_CODE (c
) == code
)
31534 error_at (location
, "too many %qs clauses", name
);
31542 variable-list , identifier
31544 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31545 colon). An opening parenthesis will have been consumed by the caller.
31547 If KIND is nonzero, create the appropriate node and install the decl
31548 in OMP_CLAUSE_DECL and add the node to the head of the list.
31550 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31551 return the list created.
31553 COLON can be NULL if only closing parenthesis should end the list,
31554 or pointer to bool which will receive false if the list is terminated
31555 by closing parenthesis or true if the list is terminated by colon. */
31558 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31559 tree list
, bool *colon
)
31562 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31565 parser
->colon_corrects_to_scope_p
= false;
31572 token
= cp_lexer_peek_token (parser
->lexer
);
31574 && current_class_ptr
31575 && cp_parser_is_keyword (token
, RID_THIS
))
31577 decl
= finish_this_expr ();
31578 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31579 || CONVERT_EXPR_P (decl
))
31580 decl
= TREE_OPERAND (decl
, 0);
31581 cp_lexer_consume_token (parser
->lexer
);
31585 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31586 /*check_dependency_p=*/true,
31587 /*template_p=*/NULL
,
31588 /*declarator_p=*/false,
31589 /*optional_p=*/false);
31590 if (name
== error_mark_node
)
31593 if (identifier_p (name
))
31594 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31597 if (decl
== error_mark_node
)
31598 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31601 if (decl
== error_mark_node
)
31603 else if (kind
!= 0)
31607 case OMP_CLAUSE__CACHE_
:
31608 /* The OpenACC cache directive explicitly only allows "array
31609 elements or subarrays". */
31610 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31612 error_at (token
->location
, "expected %<[%>");
31613 decl
= error_mark_node
;
31617 case OMP_CLAUSE_MAP
:
31618 case OMP_CLAUSE_FROM
:
31619 case OMP_CLAUSE_TO
:
31620 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31623 = cp_lexer_peek_token (parser
->lexer
)->location
;
31624 cp_id_kind idk
= CP_ID_KIND_NONE
;
31625 cp_lexer_consume_token (parser
->lexer
);
31626 decl
= convert_from_reference (decl
);
31628 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31633 case OMP_CLAUSE_DEPEND
:
31634 case OMP_CLAUSE_REDUCTION
:
31635 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31637 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31639 parser
->colon_corrects_to_scope_p
= false;
31640 cp_lexer_consume_token (parser
->lexer
);
31641 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31642 low_bound
= cp_parser_expression (parser
);
31644 parser
->colon_corrects_to_scope_p
31645 = saved_colon_corrects_to_scope_p
;
31646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31647 length
= integer_one_node
;
31650 /* Look for `:'. */
31651 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31653 if (!cp_lexer_next_token_is (parser
->lexer
,
31655 length
= cp_parser_expression (parser
);
31657 /* Look for the closing `]'. */
31658 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31662 decl
= tree_cons (low_bound
, length
, decl
);
31669 tree u
= build_omp_clause (token
->location
, kind
);
31670 OMP_CLAUSE_DECL (u
) = decl
;
31671 OMP_CLAUSE_CHAIN (u
) = list
;
31675 list
= tree_cons (decl
, NULL_TREE
, list
);
31678 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31680 cp_lexer_consume_token (parser
->lexer
);
31684 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31686 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31689 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31693 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31697 /* Try to resync to an unnested comma. Copied from
31698 cp_parser_parenthesized_expression_list. */
31701 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31702 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31703 /*recovering=*/true,
31705 /*consume_paren=*/true);
31713 /* Similarly, but expect leading and trailing parenthesis. This is a very
31714 common case for omp clauses. */
31717 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31719 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31720 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31725 copy ( variable-list )
31726 copyin ( variable-list )
31727 copyout ( variable-list )
31728 create ( variable-list )
31729 delete ( variable-list )
31730 present ( variable-list )
31731 present_or_copy ( variable-list )
31732 pcopy ( variable-list )
31733 present_or_copyin ( variable-list )
31734 pcopyin ( variable-list )
31735 present_or_copyout ( variable-list )
31736 pcopyout ( variable-list )
31737 present_or_create ( variable-list )
31738 pcreate ( variable-list ) */
31741 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31744 enum gomp_map_kind kind
;
31747 case PRAGMA_OACC_CLAUSE_COPY
:
31748 kind
= GOMP_MAP_FORCE_TOFROM
;
31750 case PRAGMA_OACC_CLAUSE_COPYIN
:
31751 kind
= GOMP_MAP_FORCE_TO
;
31753 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31754 kind
= GOMP_MAP_FORCE_FROM
;
31756 case PRAGMA_OACC_CLAUSE_CREATE
:
31757 kind
= GOMP_MAP_FORCE_ALLOC
;
31759 case PRAGMA_OACC_CLAUSE_DELETE
:
31760 kind
= GOMP_MAP_DELETE
;
31762 case PRAGMA_OACC_CLAUSE_DEVICE
:
31763 kind
= GOMP_MAP_FORCE_TO
;
31765 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31766 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31768 case PRAGMA_OACC_CLAUSE_HOST
:
31769 case PRAGMA_OACC_CLAUSE_SELF
:
31770 kind
= GOMP_MAP_FORCE_FROM
;
31772 case PRAGMA_OACC_CLAUSE_LINK
:
31773 kind
= GOMP_MAP_LINK
;
31775 case PRAGMA_OACC_CLAUSE_PRESENT
:
31776 kind
= GOMP_MAP_FORCE_PRESENT
;
31778 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31779 kind
= GOMP_MAP_TOFROM
;
31781 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31782 kind
= GOMP_MAP_TO
;
31784 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31785 kind
= GOMP_MAP_FROM
;
31787 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31788 kind
= GOMP_MAP_ALLOC
;
31791 gcc_unreachable ();
31794 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31796 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31797 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31803 deviceptr ( variable-list ) */
31806 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31808 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31811 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31812 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31813 variable-list must only allow for pointer variables. */
31814 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31815 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31817 tree v
= TREE_PURPOSE (t
);
31818 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31819 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31820 OMP_CLAUSE_DECL (u
) = v
;
31821 OMP_CLAUSE_CHAIN (u
) = list
;
31835 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31836 enum omp_clause_code code
,
31837 tree list
, location_t location
)
31839 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31840 tree c
= build_omp_clause (location
, code
);
31841 OMP_CLAUSE_CHAIN (c
) = list
;
31846 num_gangs ( expression )
31847 num_workers ( expression )
31848 vector_length ( expression ) */
31851 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31852 const char *str
, tree list
)
31854 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31856 matching_parens parens
;
31857 if (!parens
.require_open (parser
))
31860 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31862 if (t
== error_mark_node
31863 || !parens
.require_close (parser
))
31865 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31866 /*or_comma=*/false,
31867 /*consume_paren=*/true);
31871 check_no_duplicate_clause (list
, code
, str
, loc
);
31873 tree c
= build_omp_clause (loc
, code
);
31874 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31875 OMP_CLAUSE_CHAIN (c
) = list
;
31881 gang [( gang-arg-list )]
31882 worker [( [num:] int-expr )]
31883 vector [( [length:] int-expr )]
31885 where gang-arg is one of:
31890 and size-expr may be:
31897 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31898 const char *str
, tree list
)
31900 const char *id
= "num";
31901 cp_lexer
*lexer
= parser
->lexer
;
31902 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31903 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31905 if (kind
== OMP_CLAUSE_VECTOR
)
31908 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31910 matching_parens parens
;
31911 parens
.consume_open (parser
);
31915 cp_token
*next
= cp_lexer_peek_token (lexer
);
31918 /* Gang static argument. */
31919 if (kind
== OMP_CLAUSE_GANG
31920 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31922 cp_lexer_consume_token (lexer
);
31924 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31925 goto cleanup_error
;
31928 if (ops
[idx
] != NULL
)
31930 cp_parser_error (parser
, "too many %<static%> arguments");
31931 goto cleanup_error
;
31934 /* Check for the '*' argument. */
31935 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31936 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31937 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31940 cp_lexer_consume_token (lexer
);
31941 ops
[idx
] = integer_minus_one_node
;
31943 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31945 cp_lexer_consume_token (lexer
);
31951 /* Worker num: argument and vector length: arguments. */
31952 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31953 && id_equal (next
->u
.value
, id
)
31954 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31956 cp_lexer_consume_token (lexer
); /* id */
31957 cp_lexer_consume_token (lexer
); /* ':' */
31960 /* Now collect the actual argument. */
31961 if (ops
[idx
] != NULL_TREE
)
31963 cp_parser_error (parser
, "unexpected argument");
31964 goto cleanup_error
;
31967 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31969 if (expr
== error_mark_node
)
31970 goto cleanup_error
;
31972 mark_exp_read (expr
);
31975 if (kind
== OMP_CLAUSE_GANG
31976 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31978 cp_lexer_consume_token (lexer
);
31985 if (!parens
.require_close (parser
))
31986 goto cleanup_error
;
31989 check_no_duplicate_clause (list
, kind
, str
, loc
);
31991 c
= build_omp_clause (loc
, kind
);
31994 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31996 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31997 OMP_CLAUSE_CHAIN (c
) = list
;
32002 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32007 tile ( size-expr-list ) */
32010 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32012 tree c
, expr
= error_mark_node
;
32013 tree tile
= NULL_TREE
;
32015 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32016 so, but the spec authors never considered such a case and have
32017 differing opinions on what it might mean, including 'not
32019 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32020 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32023 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32028 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32031 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32032 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32033 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32035 cp_lexer_consume_token (parser
->lexer
);
32036 expr
= integer_zero_node
;
32039 expr
= cp_parser_constant_expression (parser
);
32041 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32043 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32045 /* Consume the trailing ')'. */
32046 cp_lexer_consume_token (parser
->lexer
);
32048 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32049 tile
= nreverse (tile
);
32050 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32051 OMP_CLAUSE_CHAIN (c
) = list
;
32056 Parse wait clause or directive parameters. */
32059 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32061 vec
<tree
, va_gc
> *args
;
32064 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32066 /*allow_expansion_p=*/true,
32067 /*non_constant_p=*/NULL
);
32069 if (args
== NULL
|| args
->length () == 0)
32071 cp_parser_error (parser
, "expected integer expression before ')'");
32073 release_tree_vector (args
);
32077 args_tree
= build_tree_list_vec (args
);
32079 release_tree_vector (args
);
32081 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32083 tree targ
= TREE_VALUE (t
);
32085 if (targ
!= error_mark_node
)
32087 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32088 error ("%<wait%> expression must be integral");
32091 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32093 targ
= mark_rvalue_use (targ
);
32094 OMP_CLAUSE_DECL (c
) = targ
;
32095 OMP_CLAUSE_CHAIN (c
) = list
;
32105 wait ( int-expr-list ) */
32108 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32110 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32112 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32115 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32121 collapse ( constant-expression ) */
32124 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32130 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32131 matching_parens parens
;
32132 if (!parens
.require_open (parser
))
32135 num
= cp_parser_constant_expression (parser
);
32137 if (!parens
.require_close (parser
))
32138 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32139 /*or_comma=*/false,
32140 /*consume_paren=*/true);
32142 if (num
== error_mark_node
)
32144 num
= fold_non_dependent_expr (num
);
32145 if (!tree_fits_shwi_p (num
)
32146 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32147 || (n
= tree_to_shwi (num
)) <= 0
32150 error_at (loc
, "collapse argument needs positive constant integer expression");
32154 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32155 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32156 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32157 OMP_CLAUSE_CHAIN (c
) = list
;
32158 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32164 default ( none | shared )
32167 default ( none | present ) */
32170 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32171 location_t location
, bool is_oacc
)
32173 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32176 matching_parens parens
;
32177 if (!parens
.require_open (parser
))
32179 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32181 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32182 const char *p
= IDENTIFIER_POINTER (id
);
32187 if (strcmp ("none", p
) != 0)
32189 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32193 if (strcmp ("present", p
) != 0 || !is_oacc
)
32195 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32199 if (strcmp ("shared", p
) != 0 || is_oacc
)
32201 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32208 cp_lexer_consume_token (parser
->lexer
);
32214 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32216 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32219 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32220 || !parens
.require_close (parser
))
32221 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32222 /*or_comma=*/false,
32223 /*consume_paren=*/true);
32225 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32228 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32229 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32230 OMP_CLAUSE_CHAIN (c
) = list
;
32231 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32237 final ( expression ) */
32240 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32244 matching_parens parens
;
32245 if (!parens
.require_open (parser
))
32248 t
= cp_parser_condition (parser
);
32250 if (t
== error_mark_node
32251 || !parens
.require_close (parser
))
32252 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32253 /*or_comma=*/false,
32254 /*consume_paren=*/true);
32256 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32258 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32259 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32260 OMP_CLAUSE_CHAIN (c
) = list
;
32269 if ( directive-name-modifier : expression )
32271 directive-name-modifier:
32272 parallel | task | taskloop | target data | target | target update
32273 | target enter data | target exit data */
32276 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32280 enum tree_code if_modifier
= ERROR_MARK
;
32282 matching_parens parens
;
32283 if (!parens
.require_open (parser
))
32286 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32288 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32289 const char *p
= IDENTIFIER_POINTER (id
);
32292 if (strcmp ("parallel", p
) == 0)
32293 if_modifier
= OMP_PARALLEL
;
32294 else if (strcmp ("task", p
) == 0)
32295 if_modifier
= OMP_TASK
;
32296 else if (strcmp ("taskloop", p
) == 0)
32297 if_modifier
= OMP_TASKLOOP
;
32298 else if (strcmp ("target", p
) == 0)
32300 if_modifier
= OMP_TARGET
;
32301 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32303 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32304 p
= IDENTIFIER_POINTER (id
);
32305 if (strcmp ("data", p
) == 0)
32306 if_modifier
= OMP_TARGET_DATA
;
32307 else if (strcmp ("update", p
) == 0)
32308 if_modifier
= OMP_TARGET_UPDATE
;
32309 else if (strcmp ("enter", p
) == 0)
32310 if_modifier
= OMP_TARGET_ENTER_DATA
;
32311 else if (strcmp ("exit", p
) == 0)
32312 if_modifier
= OMP_TARGET_EXIT_DATA
;
32313 if (if_modifier
!= OMP_TARGET
)
32318 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32319 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32321 if_modifier
= ERROR_MARK
;
32323 if (if_modifier
== OMP_TARGET_ENTER_DATA
32324 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32326 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32328 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32329 p
= IDENTIFIER_POINTER (id
);
32330 if (strcmp ("data", p
) == 0)
32336 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32337 error_at (loc
, "expected %<data%>");
32338 if_modifier
= ERROR_MARK
;
32343 if (if_modifier
!= ERROR_MARK
)
32345 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32348 cp_lexer_consume_token (parser
->lexer
);
32355 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32356 error_at (loc
, "expected %<:%>");
32358 if_modifier
= ERROR_MARK
;
32363 t
= cp_parser_condition (parser
);
32365 if (t
== error_mark_node
32366 || !parens
.require_close (parser
))
32367 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32368 /*or_comma=*/false,
32369 /*consume_paren=*/true);
32371 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32372 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32374 if (if_modifier
!= ERROR_MARK
32375 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32377 const char *p
= NULL
;
32378 switch (if_modifier
)
32380 case OMP_PARALLEL
: p
= "parallel"; break;
32381 case OMP_TASK
: p
= "task"; break;
32382 case OMP_TASKLOOP
: p
= "taskloop"; break;
32383 case OMP_TARGET_DATA
: p
= "target data"; break;
32384 case OMP_TARGET
: p
= "target"; break;
32385 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32386 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32387 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32388 default: gcc_unreachable ();
32390 error_at (location
, "too many %<if%> clauses with %qs modifier",
32394 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32397 error_at (location
, "too many %<if%> clauses");
32399 error_at (location
, "too many %<if%> clauses without modifier");
32402 else if (if_modifier
== ERROR_MARK
32403 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32405 error_at (location
, "if any %<if%> clause has modifier, then all "
32406 "%<if%> clauses have to use modifier");
32411 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32412 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32413 OMP_CLAUSE_IF_EXPR (c
) = t
;
32414 OMP_CLAUSE_CHAIN (c
) = list
;
32423 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32424 tree list
, location_t location
)
32428 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32431 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32432 OMP_CLAUSE_CHAIN (c
) = list
;
32440 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32441 tree list
, location_t location
)
32445 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32447 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32448 OMP_CLAUSE_CHAIN (c
) = list
;
32453 num_threads ( expression ) */
32456 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32457 location_t location
)
32461 matching_parens parens
;
32462 if (!parens
.require_open (parser
))
32465 t
= cp_parser_expression (parser
);
32467 if (t
== error_mark_node
32468 || !parens
.require_close (parser
))
32469 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32470 /*or_comma=*/false,
32471 /*consume_paren=*/true);
32473 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32474 "num_threads", location
);
32476 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32477 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32478 OMP_CLAUSE_CHAIN (c
) = list
;
32484 num_tasks ( expression ) */
32487 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32488 location_t location
)
32492 matching_parens parens
;
32493 if (!parens
.require_open (parser
))
32496 t
= cp_parser_expression (parser
);
32498 if (t
== error_mark_node
32499 || !parens
.require_close (parser
))
32500 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32501 /*or_comma=*/false,
32502 /*consume_paren=*/true);
32504 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32505 "num_tasks", location
);
32507 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32508 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32509 OMP_CLAUSE_CHAIN (c
) = list
;
32515 grainsize ( expression ) */
32518 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32519 location_t location
)
32523 matching_parens parens
;
32524 if (!parens
.require_open (parser
))
32527 t
= cp_parser_expression (parser
);
32529 if (t
== error_mark_node
32530 || !parens
.require_close (parser
))
32531 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32532 /*or_comma=*/false,
32533 /*consume_paren=*/true);
32535 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32536 "grainsize", location
);
32538 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32539 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32540 OMP_CLAUSE_CHAIN (c
) = list
;
32546 priority ( expression ) */
32549 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32550 location_t location
)
32554 matching_parens parens
;
32555 if (!parens
.require_open (parser
))
32558 t
= cp_parser_expression (parser
);
32560 if (t
== error_mark_node
32561 || !parens
.require_close (parser
))
32562 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32563 /*or_comma=*/false,
32564 /*consume_paren=*/true);
32566 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32567 "priority", location
);
32569 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32570 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32571 OMP_CLAUSE_CHAIN (c
) = list
;
32577 hint ( expression ) */
32580 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32581 location_t location
)
32585 matching_parens parens
;
32586 if (!parens
.require_open (parser
))
32589 t
= cp_parser_expression (parser
);
32591 if (t
== error_mark_node
32592 || !parens
.require_close (parser
))
32593 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32594 /*or_comma=*/false,
32595 /*consume_paren=*/true);
32597 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32599 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32600 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32601 OMP_CLAUSE_CHAIN (c
) = list
;
32607 defaultmap ( tofrom : scalar ) */
32610 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32611 location_t location
)
32616 matching_parens parens
;
32617 if (!parens
.require_open (parser
))
32620 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32622 cp_parser_error (parser
, "expected %<tofrom%>");
32625 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32626 p
= IDENTIFIER_POINTER (id
);
32627 if (strcmp (p
, "tofrom") != 0)
32629 cp_parser_error (parser
, "expected %<tofrom%>");
32632 cp_lexer_consume_token (parser
->lexer
);
32633 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32636 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32638 cp_parser_error (parser
, "expected %<scalar%>");
32641 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32642 p
= IDENTIFIER_POINTER (id
);
32643 if (strcmp (p
, "scalar") != 0)
32645 cp_parser_error (parser
, "expected %<scalar%>");
32648 cp_lexer_consume_token (parser
->lexer
);
32649 if (!parens
.require_close (parser
))
32652 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32655 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32656 OMP_CLAUSE_CHAIN (c
) = list
;
32660 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32661 /*or_comma=*/false,
32662 /*consume_paren=*/true);
32670 ordered ( constant-expression ) */
32673 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32674 tree list
, location_t location
)
32676 tree c
, num
= NULL_TREE
;
32679 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32680 "ordered", location
);
32682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32684 matching_parens parens
;
32685 parens
.consume_open (parser
);
32687 num
= cp_parser_constant_expression (parser
);
32689 if (!parens
.require_close (parser
))
32690 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32691 /*or_comma=*/false,
32692 /*consume_paren=*/true);
32694 if (num
== error_mark_node
)
32696 num
= fold_non_dependent_expr (num
);
32697 if (!tree_fits_shwi_p (num
)
32698 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32699 || (n
= tree_to_shwi (num
)) <= 0
32702 error_at (location
,
32703 "ordered argument needs positive constant integer "
32709 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32710 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32711 OMP_CLAUSE_CHAIN (c
) = list
;
32716 reduction ( reduction-operator : variable-list )
32718 reduction-operator:
32719 One of: + * - & ^ | && ||
32723 reduction-operator:
32724 One of: + * - & ^ | && || min max
32728 reduction-operator:
32729 One of: + * - & ^ | && ||
32733 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32735 enum tree_code code
= ERROR_MARK
;
32736 tree nlist
, c
, id
= NULL_TREE
;
32738 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32741 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32743 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32744 case CPP_MULT
: code
= MULT_EXPR
; break;
32745 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32746 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32747 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32748 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32749 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32750 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32754 if (code
!= ERROR_MARK
)
32755 cp_lexer_consume_token (parser
->lexer
);
32758 bool saved_colon_corrects_to_scope_p
;
32759 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32760 parser
->colon_corrects_to_scope_p
= false;
32761 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32762 /*check_dependency_p=*/true,
32763 /*template_p=*/NULL
,
32764 /*declarator_p=*/false,
32765 /*optional_p=*/false);
32766 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32767 if (identifier_p (id
))
32769 const char *p
= IDENTIFIER_POINTER (id
);
32771 if (strcmp (p
, "min") == 0)
32773 else if (strcmp (p
, "max") == 0)
32775 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32777 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32779 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32781 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32782 code
= BIT_AND_EXPR
;
32783 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32784 code
= BIT_IOR_EXPR
;
32785 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32786 code
= BIT_XOR_EXPR
;
32787 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32788 code
= TRUTH_ANDIF_EXPR
;
32789 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32790 code
= TRUTH_ORIF_EXPR
;
32791 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32792 tree scope
= parser
->scope
;
32794 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32795 parser
->scope
= NULL_TREE
;
32796 parser
->qualifying_scope
= NULL_TREE
;
32797 parser
->object_scope
= NULL_TREE
;
32801 error ("invalid reduction-identifier");
32803 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32804 /*or_comma=*/false,
32805 /*consume_paren=*/true);
32810 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32813 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32815 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32817 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32818 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32825 schedule ( schedule-kind )
32826 schedule ( schedule-kind , expression )
32829 static | dynamic | guided | runtime | auto
32832 schedule ( schedule-modifier : schedule-kind )
32833 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32841 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32844 int modifiers
= 0, nmodifiers
= 0;
32846 matching_parens parens
;
32847 if (!parens
.require_open (parser
))
32850 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32852 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32854 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32855 const char *p
= IDENTIFIER_POINTER (id
);
32856 if (strcmp ("simd", p
) == 0)
32857 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32858 else if (strcmp ("monotonic", p
) == 0)
32859 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32860 else if (strcmp ("nonmonotonic", p
) == 0)
32861 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32864 cp_lexer_consume_token (parser
->lexer
);
32865 if (nmodifiers
++ == 0
32866 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32867 cp_lexer_consume_token (parser
->lexer
);
32870 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32877 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32878 const char *p
= IDENTIFIER_POINTER (id
);
32883 if (strcmp ("dynamic", p
) != 0)
32885 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32889 if (strcmp ("guided", p
) != 0)
32891 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32895 if (strcmp ("runtime", p
) != 0)
32897 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32904 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32905 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32906 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32907 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32910 cp_lexer_consume_token (parser
->lexer
);
32912 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32913 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32914 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32915 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32917 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32925 cp_lexer_consume_token (parser
->lexer
);
32927 token
= cp_lexer_peek_token (parser
->lexer
);
32928 t
= cp_parser_assignment_expression (parser
);
32930 if (t
== error_mark_node
)
32932 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32933 error_at (token
->location
, "schedule %<runtime%> does not take "
32934 "a %<chunk_size%> parameter");
32935 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32936 error_at (token
->location
, "schedule %<auto%> does not take "
32937 "a %<chunk_size%> parameter");
32939 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32941 if (!parens
.require_close (parser
))
32944 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32947 OMP_CLAUSE_SCHEDULE_KIND (c
)
32948 = (enum omp_clause_schedule_kind
)
32949 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32951 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32952 OMP_CLAUSE_CHAIN (c
) = list
;
32956 cp_parser_error (parser
, "invalid schedule kind");
32958 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32959 /*or_comma=*/false,
32960 /*consume_paren=*/true);
32968 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32969 tree list
, location_t location
)
32973 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32975 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32976 OMP_CLAUSE_CHAIN (c
) = list
;
32985 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32986 tree list
, location_t location
)
32988 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32989 tree c
= build_omp_clause (location
, code
);
32990 OMP_CLAUSE_CHAIN (c
) = list
;
33001 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33002 enum omp_clause_code code
,
33003 tree list
, location_t location
)
33005 tree c
= build_omp_clause (location
, code
);
33006 OMP_CLAUSE_CHAIN (c
) = list
;
33014 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33015 tree list
, location_t location
)
33017 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33018 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33019 OMP_CLAUSE_CHAIN (c
) = list
;
33028 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33029 enum omp_clause_code code
,
33030 tree list
, location_t location
)
33032 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33033 tree c
= build_omp_clause (location
, code
);
33034 OMP_CLAUSE_CHAIN (c
) = list
;
33039 num_teams ( expression ) */
33042 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33043 location_t location
)
33047 matching_parens parens
;
33048 if (!parens
.require_open (parser
))
33051 t
= cp_parser_expression (parser
);
33053 if (t
== error_mark_node
33054 || !parens
.require_close (parser
))
33055 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33056 /*or_comma=*/false,
33057 /*consume_paren=*/true);
33059 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33060 "num_teams", location
);
33062 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33063 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33064 OMP_CLAUSE_CHAIN (c
) = list
;
33070 thread_limit ( expression ) */
33073 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33074 location_t location
)
33078 matching_parens parens
;
33079 if (!parens
.require_open (parser
))
33082 t
= cp_parser_expression (parser
);
33084 if (t
== error_mark_node
33085 || !parens
.require_close (parser
))
33086 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33087 /*or_comma=*/false,
33088 /*consume_paren=*/true);
33090 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33091 "thread_limit", location
);
33093 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33094 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33095 OMP_CLAUSE_CHAIN (c
) = list
;
33101 aligned ( variable-list )
33102 aligned ( variable-list : constant-expression ) */
33105 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33107 tree nlist
, c
, alignment
= NULL_TREE
;
33110 matching_parens parens
;
33111 if (!parens
.require_open (parser
))
33114 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33119 alignment
= cp_parser_constant_expression (parser
);
33121 if (!parens
.require_close (parser
))
33122 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33123 /*or_comma=*/false,
33124 /*consume_paren=*/true);
33126 if (alignment
== error_mark_node
)
33127 alignment
= NULL_TREE
;
33130 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33131 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33137 linear ( variable-list )
33138 linear ( variable-list : expression )
33141 linear ( modifier ( variable-list ) )
33142 linear ( modifier ( variable-list ) : expression ) */
33145 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33148 tree nlist
, c
, step
= integer_one_node
;
33150 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33152 matching_parens parens
;
33153 if (!parens
.require_open (parser
))
33156 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33158 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33159 const char *p
= IDENTIFIER_POINTER (id
);
33161 if (strcmp ("ref", p
) == 0)
33162 kind
= OMP_CLAUSE_LINEAR_REF
;
33163 else if (strcmp ("val", p
) == 0)
33164 kind
= OMP_CLAUSE_LINEAR_VAL
;
33165 else if (strcmp ("uval", p
) == 0)
33166 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33167 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33168 cp_lexer_consume_token (parser
->lexer
);
33170 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33173 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33174 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33178 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33179 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33181 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33182 else if (!parens
.require_close (parser
))
33183 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33184 /*or_comma=*/false,
33185 /*consume_paren=*/true);
33192 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33193 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33195 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33196 cp_parser_parse_tentatively (parser
);
33197 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33198 /*check_dependency_p=*/true,
33199 /*template_p=*/NULL
,
33200 /*declarator_p=*/false,
33201 /*optional_p=*/false);
33202 if (step
!= error_mark_node
)
33203 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33204 if (step
== error_mark_node
)
33207 cp_parser_abort_tentative_parse (parser
);
33209 else if (!cp_parser_parse_definitely (parser
))
33213 step
= cp_parser_expression (parser
);
33215 if (!parens
.require_close (parser
))
33216 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33217 /*or_comma=*/false,
33218 /*consume_paren=*/true);
33220 if (step
== error_mark_node
)
33224 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33226 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33227 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33234 safelen ( constant-expression ) */
33237 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33238 location_t location
)
33242 matching_parens parens
;
33243 if (!parens
.require_open (parser
))
33246 t
= cp_parser_constant_expression (parser
);
33248 if (t
== error_mark_node
33249 || !parens
.require_close (parser
))
33250 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33251 /*or_comma=*/false,
33252 /*consume_paren=*/true);
33254 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33256 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33257 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33258 OMP_CLAUSE_CHAIN (c
) = list
;
33264 simdlen ( constant-expression ) */
33267 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33268 location_t location
)
33272 matching_parens parens
;
33273 if (!parens
.require_open (parser
))
33276 t
= cp_parser_constant_expression (parser
);
33278 if (t
== error_mark_node
33279 || !parens
.require_close (parser
))
33280 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33281 /*or_comma=*/false,
33282 /*consume_paren=*/true);
33284 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33286 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33287 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33288 OMP_CLAUSE_CHAIN (c
) = list
;
33295 identifier [+/- integer]
33296 vec , identifier [+/- integer]
33300 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33305 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33307 cp_parser_error (parser
, "expected identifier");
33311 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33313 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33314 tree t
, identifier
= cp_parser_identifier (parser
);
33315 tree addend
= NULL
;
33317 if (identifier
== error_mark_node
)
33318 t
= error_mark_node
;
33321 t
= cp_parser_lookup_name_simple
33322 (parser
, identifier
,
33323 cp_lexer_peek_token (parser
->lexer
)->location
);
33324 if (t
== error_mark_node
)
33325 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33332 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33334 addend
= integer_zero_node
;
33335 goto add_to_vector
;
33337 cp_lexer_consume_token (parser
->lexer
);
33339 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33341 cp_parser_error (parser
, "expected integer");
33345 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33346 if (TREE_CODE (addend
) != INTEGER_CST
)
33348 cp_parser_error (parser
, "expected integer");
33351 cp_lexer_consume_token (parser
->lexer
);
33354 if (t
!= error_mark_node
)
33356 vec
= tree_cons (addend
, t
, vec
);
33358 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33361 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33364 cp_lexer_consume_token (parser
->lexer
);
33367 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33369 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33370 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33371 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33372 OMP_CLAUSE_CHAIN (u
) = list
;
33379 depend ( depend-kind : variable-list )
33387 depend ( sink : vec ) */
33390 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33393 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33395 matching_parens parens
;
33396 if (!parens
.require_open (parser
))
33399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33401 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33402 const char *p
= IDENTIFIER_POINTER (id
);
33404 if (strcmp ("in", p
) == 0)
33405 kind
= OMP_CLAUSE_DEPEND_IN
;
33406 else if (strcmp ("inout", p
) == 0)
33407 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33408 else if (strcmp ("out", p
) == 0)
33409 kind
= OMP_CLAUSE_DEPEND_OUT
;
33410 else if (strcmp ("source", p
) == 0)
33411 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33412 else if (strcmp ("sink", p
) == 0)
33413 kind
= OMP_CLAUSE_DEPEND_SINK
;
33420 cp_lexer_consume_token (parser
->lexer
);
33422 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33424 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33425 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33426 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33427 OMP_CLAUSE_CHAIN (c
) = list
;
33428 if (!parens
.require_close (parser
))
33429 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33430 /*or_comma=*/false,
33431 /*consume_paren=*/true);
33435 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33438 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33439 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33442 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33445 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33446 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33451 cp_parser_error (parser
, "invalid depend kind");
33453 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33454 /*or_comma=*/false,
33455 /*consume_paren=*/true);
33460 map ( map-kind : variable-list )
33461 map ( variable-list )
33464 alloc | to | from | tofrom
33468 alloc | to | from | tofrom | release | delete
33470 map ( always [,] map-kind: variable-list ) */
33473 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33476 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33477 bool always
= false;
33479 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33484 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33485 const char *p
= IDENTIFIER_POINTER (id
);
33487 if (strcmp ("always", p
) == 0)
33490 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33492 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33493 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33495 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33499 cp_lexer_consume_token (parser
->lexer
);
33501 cp_lexer_consume_token (parser
->lexer
);
33506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33507 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33509 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33510 const char *p
= IDENTIFIER_POINTER (id
);
33512 if (strcmp ("alloc", p
) == 0)
33513 kind
= GOMP_MAP_ALLOC
;
33514 else if (strcmp ("to", p
) == 0)
33515 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33516 else if (strcmp ("from", p
) == 0)
33517 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33518 else if (strcmp ("tofrom", p
) == 0)
33519 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33520 else if (strcmp ("release", p
) == 0)
33521 kind
= GOMP_MAP_RELEASE
;
33524 cp_parser_error (parser
, "invalid map kind");
33525 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33526 /*or_comma=*/false,
33527 /*consume_paren=*/true);
33530 cp_lexer_consume_token (parser
->lexer
);
33531 cp_lexer_consume_token (parser
->lexer
);
33533 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33534 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33536 kind
= GOMP_MAP_DELETE
;
33537 cp_lexer_consume_token (parser
->lexer
);
33538 cp_lexer_consume_token (parser
->lexer
);
33541 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33544 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33545 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33551 device ( expression ) */
33554 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33555 location_t location
)
33559 matching_parens parens
;
33560 if (!parens
.require_open (parser
))
33563 t
= cp_parser_expression (parser
);
33565 if (t
== error_mark_node
33566 || !parens
.require_close (parser
))
33567 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33568 /*or_comma=*/false,
33569 /*consume_paren=*/true);
33571 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33572 "device", location
);
33574 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33575 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33576 OMP_CLAUSE_CHAIN (c
) = list
;
33582 dist_schedule ( static )
33583 dist_schedule ( static , expression ) */
33586 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33587 location_t location
)
33591 matching_parens parens
;
33592 if (!parens
.require_open (parser
))
33595 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33597 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33599 cp_lexer_consume_token (parser
->lexer
);
33601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33603 cp_lexer_consume_token (parser
->lexer
);
33605 t
= cp_parser_assignment_expression (parser
);
33607 if (t
== error_mark_node
)
33609 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33611 if (!parens
.require_close (parser
))
33614 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33617 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33619 OMP_CLAUSE_CHAIN (c
) = list
;
33623 cp_parser_error (parser
, "invalid dist_schedule kind");
33625 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33626 /*or_comma=*/false,
33627 /*consume_paren=*/true);
33632 proc_bind ( proc-bind-kind )
33635 master | close | spread */
33638 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33639 location_t location
)
33642 enum omp_clause_proc_bind_kind kind
;
33644 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33649 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33650 const char *p
= IDENTIFIER_POINTER (id
);
33652 if (strcmp ("master", p
) == 0)
33653 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33654 else if (strcmp ("close", p
) == 0)
33655 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33656 else if (strcmp ("spread", p
) == 0)
33657 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33664 cp_lexer_consume_token (parser
->lexer
);
33665 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33668 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33669 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33671 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33672 OMP_CLAUSE_CHAIN (c
) = list
;
33676 cp_parser_error (parser
, "invalid depend kind");
33678 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33679 /*or_comma=*/false,
33680 /*consume_paren=*/true);
33685 async [( int-expr )] */
33688 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33691 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33693 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33695 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33697 matching_parens parens
;
33698 parens
.consume_open (parser
);
33700 t
= cp_parser_expression (parser
);
33701 if (t
== error_mark_node
33702 || !parens
.require_close (parser
))
33703 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33704 /*or_comma=*/false,
33705 /*consume_paren=*/true);
33708 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33710 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33711 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33712 OMP_CLAUSE_CHAIN (c
) = list
;
33718 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33719 is a bitmask in MASK. Return the list of clauses found. */
33722 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33723 const char *where
, cp_token
*pragma_tok
,
33724 bool finish_p
= true)
33726 tree clauses
= NULL
;
33729 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33732 pragma_omp_clause c_kind
;
33733 omp_clause_code code
;
33734 const char *c_name
;
33735 tree prev
= clauses
;
33737 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33738 cp_lexer_consume_token (parser
->lexer
);
33740 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33741 c_kind
= cp_parser_omp_clause_name (parser
);
33745 case PRAGMA_OACC_CLAUSE_ASYNC
:
33746 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33749 case PRAGMA_OACC_CLAUSE_AUTO
:
33750 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33754 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33755 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33756 c_name
= "collapse";
33758 case PRAGMA_OACC_CLAUSE_COPY
:
33759 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33762 case PRAGMA_OACC_CLAUSE_COPYIN
:
33763 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33766 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33767 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33768 c_name
= "copyout";
33770 case PRAGMA_OACC_CLAUSE_CREATE
:
33771 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33774 case PRAGMA_OACC_CLAUSE_DELETE
:
33775 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33778 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33779 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33780 c_name
= "default";
33782 case PRAGMA_OACC_CLAUSE_DEVICE
:
33783 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33786 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33787 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33788 c_name
= "deviceptr";
33790 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33791 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33792 c_name
= "device_resident";
33794 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33795 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33797 c_name
= "firstprivate";
33799 case PRAGMA_OACC_CLAUSE_GANG
:
33801 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33804 case PRAGMA_OACC_CLAUSE_HOST
:
33805 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33808 case PRAGMA_OACC_CLAUSE_IF
:
33809 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33812 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33813 clauses
= cp_parser_oacc_simple_clause (parser
,
33814 OMP_CLAUSE_INDEPENDENT
,
33816 c_name
= "independent";
33818 case PRAGMA_OACC_CLAUSE_LINK
:
33819 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33822 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33823 code
= OMP_CLAUSE_NUM_GANGS
;
33824 c_name
= "num_gangs";
33825 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33828 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33829 c_name
= "num_workers";
33830 code
= OMP_CLAUSE_NUM_WORKERS
;
33831 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33834 case PRAGMA_OACC_CLAUSE_PRESENT
:
33835 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33836 c_name
= "present";
33838 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33839 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33840 c_name
= "present_or_copy";
33842 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33843 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33844 c_name
= "present_or_copyin";
33846 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33847 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33848 c_name
= "present_or_copyout";
33850 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33851 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33852 c_name
= "present_or_create";
33854 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33855 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33857 c_name
= "private";
33859 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33860 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33861 c_name
= "reduction";
33863 case PRAGMA_OACC_CLAUSE_SELF
:
33864 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33867 case PRAGMA_OACC_CLAUSE_SEQ
:
33868 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33872 case PRAGMA_OACC_CLAUSE_TILE
:
33873 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33876 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33877 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33879 c_name
= "use_device";
33881 case PRAGMA_OACC_CLAUSE_VECTOR
:
33883 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33886 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33887 c_name
= "vector_length";
33888 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33889 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33892 case PRAGMA_OACC_CLAUSE_WAIT
:
33893 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33896 case PRAGMA_OACC_CLAUSE_WORKER
:
33898 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33902 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33908 if (((mask
>> c_kind
) & 1) == 0)
33910 /* Remove the invalid clause(s) from the list to avoid
33911 confusing the rest of the compiler. */
33913 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33918 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33921 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33926 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33927 is a bitmask in MASK. Return the list of clauses found; the result
33928 of clause default goes in *pdefault. */
33931 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33932 const char *where
, cp_token
*pragma_tok
,
33933 bool finish_p
= true)
33935 tree clauses
= NULL
;
33937 cp_token
*token
= NULL
;
33939 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33941 pragma_omp_clause c_kind
;
33942 const char *c_name
;
33943 tree prev
= clauses
;
33945 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33946 cp_lexer_consume_token (parser
->lexer
);
33948 token
= cp_lexer_peek_token (parser
->lexer
);
33949 c_kind
= cp_parser_omp_clause_name (parser
);
33953 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33954 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33956 c_name
= "collapse";
33958 case PRAGMA_OMP_CLAUSE_COPYIN
:
33959 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33962 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33963 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33965 c_name
= "copyprivate";
33967 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33968 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33969 token
->location
, false);
33970 c_name
= "default";
33972 case PRAGMA_OMP_CLAUSE_FINAL
:
33973 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33976 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33977 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33979 c_name
= "firstprivate";
33981 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33982 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33984 c_name
= "grainsize";
33986 case PRAGMA_OMP_CLAUSE_HINT
:
33987 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33991 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33992 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33994 c_name
= "defaultmap";
33996 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33997 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33999 c_name
= "use_device_ptr";
34001 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34002 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34004 c_name
= "is_device_ptr";
34006 case PRAGMA_OMP_CLAUSE_IF
:
34007 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34011 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34012 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
34014 c_name
= "lastprivate";
34016 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34017 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34019 c_name
= "mergeable";
34021 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34022 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34025 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34026 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34028 c_name
= "num_tasks";
34030 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34031 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34033 c_name
= "num_threads";
34035 case PRAGMA_OMP_CLAUSE_ORDERED
:
34036 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34038 c_name
= "ordered";
34040 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34041 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34043 c_name
= "priority";
34045 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34046 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34048 c_name
= "private";
34050 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34051 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34052 c_name
= "reduction";
34054 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34055 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34057 c_name
= "schedule";
34059 case PRAGMA_OMP_CLAUSE_SHARED
:
34060 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34064 case PRAGMA_OMP_CLAUSE_UNTIED
:
34065 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34069 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34070 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34071 clauses
, token
->location
);
34072 c_name
= "inbranch";
34074 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34075 clauses
= cp_parser_omp_clause_branch (parser
,
34076 OMP_CLAUSE_NOTINBRANCH
,
34077 clauses
, token
->location
);
34078 c_name
= "notinbranch";
34080 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34081 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34082 clauses
, token
->location
);
34083 c_name
= "parallel";
34087 error_at (token
->location
, "%qs must be the first clause of %qs",
34092 case PRAGMA_OMP_CLAUSE_FOR
:
34093 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34094 clauses
, token
->location
);
34097 goto clause_not_first
;
34099 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34100 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34101 clauses
, token
->location
);
34102 c_name
= "sections";
34104 goto clause_not_first
;
34106 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34107 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34108 clauses
, token
->location
);
34109 c_name
= "taskgroup";
34111 goto clause_not_first
;
34113 case PRAGMA_OMP_CLAUSE_LINK
:
34114 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34117 case PRAGMA_OMP_CLAUSE_TO
:
34118 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34119 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34122 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34125 case PRAGMA_OMP_CLAUSE_FROM
:
34126 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34129 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34130 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34132 c_name
= "uniform";
34134 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34135 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34137 c_name
= "num_teams";
34139 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34140 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34142 c_name
= "thread_limit";
34144 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34145 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34146 c_name
= "aligned";
34148 case PRAGMA_OMP_CLAUSE_LINEAR
:
34150 bool declare_simd
= false;
34151 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34152 declare_simd
= true;
34153 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34157 case PRAGMA_OMP_CLAUSE_DEPEND
:
34158 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34162 case PRAGMA_OMP_CLAUSE_MAP
:
34163 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34166 case PRAGMA_OMP_CLAUSE_DEVICE
:
34167 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34171 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34172 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34174 c_name
= "dist_schedule";
34176 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34177 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34179 c_name
= "proc_bind";
34181 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34182 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34184 c_name
= "safelen";
34186 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34187 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34189 c_name
= "simdlen";
34191 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34192 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34194 c_name
= "nogroup";
34196 case PRAGMA_OMP_CLAUSE_THREADS
:
34198 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34199 clauses
, token
->location
);
34200 c_name
= "threads";
34202 case PRAGMA_OMP_CLAUSE_SIMD
:
34204 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34205 clauses
, token
->location
);
34209 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34215 if (((mask
>> c_kind
) & 1) == 0)
34217 /* Remove the invalid clause(s) from the list to avoid
34218 confusing the rest of the compiler. */
34220 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34224 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34227 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34228 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34230 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34239 In practice, we're also interested in adding the statement to an
34240 outer node. So it is convenient if we work around the fact that
34241 cp_parser_statement calls add_stmt. */
34244 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34246 unsigned save
= parser
->in_statement
;
34248 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34249 This preserves the "not within loop or switch" style error messages
34250 for nonsense cases like
34256 if (parser
->in_statement
)
34257 parser
->in_statement
= IN_OMP_BLOCK
;
34263 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34265 parser
->in_statement
= save
;
34269 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34271 tree stmt
= begin_omp_structured_block ();
34272 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34274 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34276 cp_parser_end_omp_structured_block (parser
, save
);
34277 return finish_omp_structured_block (stmt
);
34281 # pragma omp atomic new-line
34285 x binop= expr | x++ | ++x | x-- | --x
34287 +, *, -, /, &, ^, |, <<, >>
34289 where x is an lvalue expression with scalar type.
34292 # pragma omp atomic new-line
34295 # pragma omp atomic read new-line
34298 # pragma omp atomic write new-line
34301 # pragma omp atomic update new-line
34304 # pragma omp atomic capture new-line
34307 # pragma omp atomic capture new-line
34315 expression-stmt | x = x binop expr
34317 v = expression-stmt
34319 { v = x; update-stmt; } | { update-stmt; v = x; }
34323 expression-stmt | x = x binop expr | x = expr binop x
34327 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34329 where x and v are lvalue expressions with scalar type. */
34332 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34334 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34335 tree rhs1
= NULL_TREE
, orig_lhs
;
34336 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34337 bool structured_block
= false;
34338 bool seq_cst
= false;
34340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34342 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34343 const char *p
= IDENTIFIER_POINTER (id
);
34345 if (!strcmp (p
, "seq_cst"))
34348 cp_lexer_consume_token (parser
->lexer
);
34349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34350 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34351 cp_lexer_consume_token (parser
->lexer
);
34354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34356 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34357 const char *p
= IDENTIFIER_POINTER (id
);
34359 if (!strcmp (p
, "read"))
34360 code
= OMP_ATOMIC_READ
;
34361 else if (!strcmp (p
, "write"))
34363 else if (!strcmp (p
, "update"))
34365 else if (!strcmp (p
, "capture"))
34366 code
= OMP_ATOMIC_CAPTURE_NEW
;
34370 cp_lexer_consume_token (parser
->lexer
);
34374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34375 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34376 cp_lexer_consume_token (parser
->lexer
);
34378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34380 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34381 const char *p
= IDENTIFIER_POINTER (id
);
34383 if (!strcmp (p
, "seq_cst"))
34386 cp_lexer_consume_token (parser
->lexer
);
34390 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34394 case OMP_ATOMIC_READ
:
34395 case NOP_EXPR
: /* atomic write */
34396 v
= cp_parser_unary_expression (parser
);
34397 if (v
== error_mark_node
)
34399 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34401 if (code
== NOP_EXPR
)
34402 lhs
= cp_parser_expression (parser
);
34404 lhs
= cp_parser_unary_expression (parser
);
34405 if (lhs
== error_mark_node
)
34407 if (code
== NOP_EXPR
)
34409 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34417 case OMP_ATOMIC_CAPTURE_NEW
:
34418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34420 cp_lexer_consume_token (parser
->lexer
);
34421 structured_block
= true;
34425 v
= cp_parser_unary_expression (parser
);
34426 if (v
== error_mark_node
)
34428 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34436 lhs
= cp_parser_unary_expression (parser
);
34438 switch (TREE_CODE (lhs
))
34443 case POSTINCREMENT_EXPR
:
34444 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34445 code
= OMP_ATOMIC_CAPTURE_OLD
;
34447 case PREINCREMENT_EXPR
:
34448 lhs
= TREE_OPERAND (lhs
, 0);
34449 opcode
= PLUS_EXPR
;
34450 rhs
= integer_one_node
;
34453 case POSTDECREMENT_EXPR
:
34454 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34455 code
= OMP_ATOMIC_CAPTURE_OLD
;
34457 case PREDECREMENT_EXPR
:
34458 lhs
= TREE_OPERAND (lhs
, 0);
34459 opcode
= MINUS_EXPR
;
34460 rhs
= integer_one_node
;
34463 case COMPOUND_EXPR
:
34464 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34465 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34466 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34467 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34468 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34469 (TREE_OPERAND (lhs
, 1), 0), 0)))
34471 /* Undo effects of boolean_increment for post {in,de}crement. */
34472 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34475 if (TREE_CODE (lhs
) == MODIFY_EXPR
34476 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34478 /* Undo effects of boolean_increment. */
34479 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34481 /* This is pre or post increment. */
34482 rhs
= TREE_OPERAND (lhs
, 1);
34483 lhs
= TREE_OPERAND (lhs
, 0);
34485 if (code
== OMP_ATOMIC_CAPTURE_NEW
34486 && !structured_block
34487 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34488 code
= OMP_ATOMIC_CAPTURE_OLD
;
34494 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34497 opcode
= MULT_EXPR
;
34500 opcode
= TRUNC_DIV_EXPR
;
34503 opcode
= PLUS_EXPR
;
34506 opcode
= MINUS_EXPR
;
34508 case CPP_LSHIFT_EQ
:
34509 opcode
= LSHIFT_EXPR
;
34511 case CPP_RSHIFT_EQ
:
34512 opcode
= RSHIFT_EXPR
;
34515 opcode
= BIT_AND_EXPR
;
34518 opcode
= BIT_IOR_EXPR
;
34521 opcode
= BIT_XOR_EXPR
;
34524 enum cp_parser_prec oprec
;
34526 cp_lexer_consume_token (parser
->lexer
);
34527 cp_parser_parse_tentatively (parser
);
34528 rhs1
= cp_parser_simple_cast_expression (parser
);
34529 if (rhs1
== error_mark_node
)
34531 cp_parser_abort_tentative_parse (parser
);
34532 cp_parser_simple_cast_expression (parser
);
34535 token
= cp_lexer_peek_token (parser
->lexer
);
34536 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34538 cp_parser_abort_tentative_parse (parser
);
34539 cp_parser_parse_tentatively (parser
);
34540 rhs
= cp_parser_binary_expression (parser
, false, true,
34541 PREC_NOT_OPERATOR
, NULL
);
34542 if (rhs
== error_mark_node
)
34544 cp_parser_abort_tentative_parse (parser
);
34545 cp_parser_binary_expression (parser
, false, true,
34546 PREC_NOT_OPERATOR
, NULL
);
34549 switch (TREE_CODE (rhs
))
34552 case TRUNC_DIV_EXPR
:
34561 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34563 if (cp_parser_parse_definitely (parser
))
34565 opcode
= TREE_CODE (rhs
);
34566 rhs1
= TREE_OPERAND (rhs
, 0);
34567 rhs
= TREE_OPERAND (rhs
, 1);
34577 cp_parser_abort_tentative_parse (parser
);
34578 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34580 rhs
= cp_parser_expression (parser
);
34581 if (rhs
== error_mark_node
)
34587 cp_parser_error (parser
,
34588 "invalid form of %<#pragma omp atomic%>");
34591 if (!cp_parser_parse_definitely (parser
))
34593 switch (token
->type
)
34595 case CPP_SEMICOLON
:
34596 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34598 code
= OMP_ATOMIC_CAPTURE_OLD
;
34603 cp_lexer_consume_token (parser
->lexer
);
34606 else if (structured_block
)
34613 cp_parser_error (parser
,
34614 "invalid form of %<#pragma omp atomic%>");
34617 opcode
= MULT_EXPR
;
34620 opcode
= TRUNC_DIV_EXPR
;
34623 opcode
= PLUS_EXPR
;
34626 opcode
= MINUS_EXPR
;
34629 opcode
= LSHIFT_EXPR
;
34632 opcode
= RSHIFT_EXPR
;
34635 opcode
= BIT_AND_EXPR
;
34638 opcode
= BIT_IOR_EXPR
;
34641 opcode
= BIT_XOR_EXPR
;
34644 cp_parser_error (parser
,
34645 "invalid operator for %<#pragma omp atomic%>");
34648 oprec
= TOKEN_PRECEDENCE (token
);
34649 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34650 if (commutative_tree_code (opcode
))
34651 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34652 cp_lexer_consume_token (parser
->lexer
);
34653 rhs
= cp_parser_binary_expression (parser
, false, false,
34655 if (rhs
== error_mark_node
)
34660 cp_parser_error (parser
,
34661 "invalid operator for %<#pragma omp atomic%>");
34664 cp_lexer_consume_token (parser
->lexer
);
34666 rhs
= cp_parser_expression (parser
);
34667 if (rhs
== error_mark_node
)
34672 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34674 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34676 v
= cp_parser_unary_expression (parser
);
34677 if (v
== error_mark_node
)
34679 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34681 lhs1
= cp_parser_unary_expression (parser
);
34682 if (lhs1
== error_mark_node
)
34685 if (structured_block
)
34687 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34688 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34691 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34692 if (!structured_block
)
34693 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34697 cp_parser_skip_to_end_of_block_or_statement (parser
);
34698 if (structured_block
)
34700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34701 cp_lexer_consume_token (parser
->lexer
);
34702 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34704 cp_parser_skip_to_end_of_block_or_statement (parser
);
34705 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34706 cp_lexer_consume_token (parser
->lexer
);
34713 # pragma omp barrier new-line */
34716 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34718 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34719 finish_omp_barrier ();
34723 # pragma omp critical [(name)] new-line
34727 # pragma omp critical [(name) [hint(expression)]] new-line
34728 structured-block */
34730 #define OMP_CRITICAL_CLAUSE_MASK \
34731 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34734 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34736 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34740 matching_parens parens
;
34741 parens
.consume_open (parser
);
34743 name
= cp_parser_identifier (parser
);
34745 if (name
== error_mark_node
34746 || !parens
.require_close (parser
))
34747 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34748 /*or_comma=*/false,
34749 /*consume_paren=*/true);
34750 if (name
== error_mark_node
)
34753 clauses
= cp_parser_omp_all_clauses (parser
,
34754 OMP_CRITICAL_CLAUSE_MASK
,
34755 "#pragma omp critical", pragma_tok
);
34758 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34760 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34761 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34765 # pragma omp flush flush-vars[opt] new-line
34768 ( variable-list ) */
34771 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34774 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34775 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34777 finish_omp_flush ();
34780 /* Helper function, to parse omp for increment expression. */
34783 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34785 tree cond
= cp_parser_binary_expression (parser
, false, true,
34786 PREC_NOT_OPERATOR
, NULL
);
34787 if (cond
== error_mark_node
34788 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34790 cp_parser_skip_to_end_of_statement (parser
);
34791 return error_mark_node
;
34794 switch (TREE_CODE (cond
))
34802 /* Fall through: OpenMP disallows NE_EXPR. */
34803 gcc_fallthrough ();
34805 return error_mark_node
;
34808 /* If decl is an iterator, preserve LHS and RHS of the relational
34809 expr until finish_omp_for. */
34811 && (type_dependent_expression_p (decl
)
34812 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34815 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
34817 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34818 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34819 /*overload=*/NULL
, tf_warning_or_error
);
34822 /* Helper function, to parse omp for increment expression. */
34825 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34827 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34833 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34835 op
= (token
->type
== CPP_PLUS_PLUS
34836 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34837 cp_lexer_consume_token (parser
->lexer
);
34838 lhs
= cp_parser_simple_cast_expression (parser
);
34840 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34841 return error_mark_node
;
34842 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34845 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34847 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34848 return error_mark_node
;
34850 token
= cp_lexer_peek_token (parser
->lexer
);
34851 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34853 op
= (token
->type
== CPP_PLUS_PLUS
34854 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34855 cp_lexer_consume_token (parser
->lexer
);
34856 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34859 op
= cp_parser_assignment_operator_opt (parser
);
34860 if (op
== ERROR_MARK
)
34861 return error_mark_node
;
34863 if (op
!= NOP_EXPR
)
34865 rhs
= cp_parser_assignment_expression (parser
);
34866 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34867 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34870 lhs
= cp_parser_binary_expression (parser
, false, false,
34871 PREC_ADDITIVE_EXPRESSION
, NULL
);
34872 token
= cp_lexer_peek_token (parser
->lexer
);
34873 decl_first
= (lhs
== decl
34874 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34877 if (token
->type
!= CPP_PLUS
34878 && token
->type
!= CPP_MINUS
)
34879 return error_mark_node
;
34883 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34884 cp_lexer_consume_token (parser
->lexer
);
34885 rhs
= cp_parser_binary_expression (parser
, false, false,
34886 PREC_ADDITIVE_EXPRESSION
, NULL
);
34887 token
= cp_lexer_peek_token (parser
->lexer
);
34888 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34890 if (lhs
== NULL_TREE
)
34892 if (op
== PLUS_EXPR
)
34895 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34896 tf_warning_or_error
);
34899 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34900 ERROR_MARK
, NULL
, tf_warning_or_error
);
34903 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34908 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34909 || op
== MINUS_EXPR
)
34910 return error_mark_node
;
34911 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34914 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34916 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34919 /* Parse the initialization statement of an OpenMP for loop.
34921 Return true if the resulting construct should have an
34922 OMP_CLAUSE_PRIVATE added to it. */
34925 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34926 tree
&this_pre_body
,
34927 vec
<tree
, va_gc
> *for_block
,
34933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34936 tree add_private_clause
= NULL_TREE
;
34938 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34942 integer-type var = lb
34943 random-access-iterator-type var = lb
34944 pointer-type var = lb
34946 cp_decl_specifier_seq type_specifiers
;
34948 /* First, try to parse as an initialized declaration. See
34949 cp_parser_condition, from whence the bulk of this is copied. */
34951 cp_parser_parse_tentatively (parser
);
34952 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34953 /*is_trailing_return=*/false,
34955 if (cp_parser_parse_definitely (parser
))
34957 /* If parsing a type specifier seq succeeded, then this
34958 MUST be a initialized declaration. */
34959 tree asm_specification
, attributes
;
34960 cp_declarator
*declarator
;
34962 declarator
= cp_parser_declarator (parser
,
34963 CP_PARSER_DECLARATOR_NAMED
,
34964 /*ctor_dtor_or_conv_p=*/NULL
,
34965 /*parenthesized_p=*/NULL
,
34966 /*member_p=*/false,
34967 /*friend_p=*/false);
34968 attributes
= cp_parser_attributes_opt (parser
);
34969 asm_specification
= cp_parser_asm_specification_opt (parser
);
34971 if (declarator
== cp_error_declarator
)
34972 cp_parser_skip_to_end_of_statement (parser
);
34976 tree pushed_scope
, auto_node
;
34978 decl
= start_decl (declarator
, &type_specifiers
,
34979 SD_INITIALIZED
, attributes
,
34980 /*prefix_attributes=*/NULL_TREE
,
34983 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34984 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34986 if (cp_lexer_next_token_is (parser
->lexer
,
34988 error ("parenthesized initialization is not allowed in "
34989 "OpenMP %<for%> loop");
34991 /* Trigger an error. */
34992 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34994 init
= error_mark_node
;
34995 cp_parser_skip_to_end_of_statement (parser
);
34997 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34998 || type_dependent_expression_p (decl
)
35001 bool is_direct_init
, is_non_constant_init
;
35003 init
= cp_parser_initializer (parser
,
35005 &is_non_constant_init
);
35010 = do_auto_deduction (TREE_TYPE (decl
), init
,
35013 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35014 && !type_dependent_expression_p (decl
))
35018 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35020 LOOKUP_ONLYCONVERTING
);
35022 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35024 vec_safe_push (for_block
, this_pre_body
);
35029 init
= pop_stmt_list (this_pre_body
);
35030 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35032 tree_stmt_iterator i
= tsi_start (init
);
35033 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35034 while (!tsi_end_p (i
))
35036 tree t
= tsi_stmt (i
);
35037 if (TREE_CODE (t
) == DECL_EXPR
35038 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35041 vec_safe_push (for_block
, t
);
35046 if (tsi_one_before_end_p (i
))
35048 tree t
= tsi_stmt (i
);
35050 free_stmt_list (init
);
35055 this_pre_body
= NULL_TREE
;
35060 cp_lexer_consume_token (parser
->lexer
);
35061 init
= cp_parser_assignment_expression (parser
);
35064 if (TYPE_REF_P (TREE_TYPE (decl
)))
35065 init
= error_mark_node
;
35067 cp_finish_decl (decl
, NULL_TREE
,
35068 /*init_const_expr_p=*/false,
35070 LOOKUP_ONLYCONVERTING
);
35074 pop_scope (pushed_scope
);
35080 /* If parsing a type specifier sequence failed, then
35081 this MUST be a simple expression. */
35082 cp_parser_parse_tentatively (parser
);
35083 decl
= cp_parser_primary_expression (parser
, false, false,
35085 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35086 if (!cp_parser_error_occurred (parser
)
35088 && (TREE_CODE (decl
) == COMPONENT_REF
35089 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35091 cp_parser_abort_tentative_parse (parser
);
35092 cp_parser_parse_tentatively (parser
);
35093 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35094 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35095 /*check_dependency_p=*/true,
35096 /*template_p=*/NULL
,
35097 /*declarator_p=*/false,
35098 /*optional_p=*/false);
35099 if (name
!= error_mark_node
35100 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35102 decl
= cp_parser_lookup_name_simple (parser
, name
,
35104 if (TREE_CODE (decl
) == FIELD_DECL
)
35105 add_private_clause
= omp_privatize_field (decl
, false);
35107 cp_parser_abort_tentative_parse (parser
);
35108 cp_parser_parse_tentatively (parser
);
35109 decl
= cp_parser_primary_expression (parser
, false, false,
35112 if (!cp_parser_error_occurred (parser
)
35115 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35119 cp_parser_parse_definitely (parser
);
35120 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35121 rhs
= cp_parser_assignment_expression (parser
);
35123 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35126 tf_warning_or_error
));
35127 if (!add_private_clause
)
35128 add_private_clause
= decl
;
35133 cp_parser_abort_tentative_parse (parser
);
35134 init
= cp_parser_expression (parser
);
35137 if (TREE_CODE (init
) == MODIFY_EXPR
35138 || TREE_CODE (init
) == MODOP_EXPR
)
35139 real_decl
= TREE_OPERAND (init
, 0);
35143 return add_private_clause
;
35146 /* Parse the restricted form of the for statement allowed by OpenMP. */
35149 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35150 tree
*cclauses
, bool *if_p
)
35152 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35153 tree real_decl
, initv
, condv
, incrv
, declv
;
35154 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35155 location_t loc_first
;
35156 bool collapse_err
= false;
35157 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35158 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35159 auto_vec
<tree
, 4> orig_inits
;
35160 bool tiling
= false;
35162 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35163 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35164 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35165 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35168 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35170 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35171 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35174 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35177 if (ordered
&& ordered
< collapse
)
35179 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35180 "%<ordered%> clause parameter is less than %<collapse%>");
35181 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35182 = build_int_cst (NULL_TREE
, collapse
);
35183 ordered
= collapse
;
35187 for (tree
*pc
= &clauses
; *pc
; )
35188 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35190 error_at (OMP_CLAUSE_LOCATION (*pc
),
35191 "%<linear%> clause may not be specified together "
35192 "with %<ordered%> clause with a parameter");
35193 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35196 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35199 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35200 count
= ordered
? ordered
: collapse
;
35202 declv
= make_tree_vec (count
);
35203 initv
= make_tree_vec (count
);
35204 condv
= make_tree_vec (count
);
35205 incrv
= make_tree_vec (count
);
35207 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35209 for (i
= 0; i
< count
; i
++)
35211 int bracecount
= 0;
35212 tree add_private_clause
= NULL_TREE
;
35215 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35218 cp_parser_error (parser
, "for statement expected");
35221 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35223 matching_parens parens
;
35224 if (!parens
.require_open (parser
))
35227 init
= orig_init
= decl
= real_decl
= NULL
;
35228 this_pre_body
= push_stmt_list ();
35231 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35232 init
, orig_init
, decl
, real_decl
);
35234 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35237 this_pre_body
= pop_stmt_list (this_pre_body
);
35241 pre_body
= push_stmt_list ();
35243 add_stmt (this_pre_body
);
35244 pre_body
= pop_stmt_list (pre_body
);
35247 pre_body
= this_pre_body
;
35252 if (cclauses
!= NULL
35253 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35254 && real_decl
!= NULL_TREE
)
35257 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35258 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35259 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35261 error_at (loc
, "iteration variable %qD"
35262 " should not be firstprivate", real_decl
);
35263 *c
= OMP_CLAUSE_CHAIN (*c
);
35265 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35266 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35268 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35270 *c
= OMP_CLAUSE_CHAIN (*c
);
35271 if (code
== OMP_SIMD
)
35273 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35274 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35278 OMP_CLAUSE_CHAIN (l
) = clauses
;
35281 add_private_clause
= NULL_TREE
;
35285 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35286 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35287 add_private_clause
= NULL_TREE
;
35288 c
= &OMP_CLAUSE_CHAIN (*c
);
35292 if (add_private_clause
)
35295 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35297 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35298 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35299 && OMP_CLAUSE_DECL (c
) == decl
)
35301 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35302 && OMP_CLAUSE_DECL (c
) == decl
)
35303 error_at (loc
, "iteration variable %qD "
35304 "should not be firstprivate",
35306 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35307 && OMP_CLAUSE_DECL (c
) == decl
)
35308 error_at (loc
, "iteration variable %qD should not be reduction",
35313 if (code
!= OMP_SIMD
)
35314 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35315 else if (collapse
== 1)
35316 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35318 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35319 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35320 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35323 OMP_CLAUSE_CHAIN (c
) = clauses
;
35325 /* For linear, signal that we need to fill up
35326 the so far unknown linear step. */
35327 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35328 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35334 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35335 cond
= cp_parser_omp_for_cond (parser
, decl
);
35336 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35339 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35341 /* If decl is an iterator, preserve the operator on decl
35342 until finish_omp_for. */
35344 && ((processing_template_decl
35345 && (TREE_TYPE (real_decl
) == NULL_TREE
35346 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35347 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35348 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35350 incr
= cp_parser_expression (parser
);
35351 if (!EXPR_HAS_LOCATION (incr
))
35352 protected_set_expr_location (incr
, input_location
);
35355 if (!parens
.require_close (parser
))
35356 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35357 /*or_comma=*/false,
35358 /*consume_paren=*/true);
35360 TREE_VEC_ELT (declv
, i
) = decl
;
35361 TREE_VEC_ELT (initv
, i
) = init
;
35362 TREE_VEC_ELT (condv
, i
) = cond
;
35363 TREE_VEC_ELT (incrv
, i
) = incr
;
35366 orig_inits
.safe_grow_cleared (i
+ 1);
35367 orig_inits
[i
] = orig_init
;
35370 if (i
== count
- 1)
35373 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35374 in between the collapsed for loops to be still considered perfectly
35375 nested. Hopefully the final version clarifies this.
35376 For now handle (multiple) {'s and empty statements. */
35377 cp_parser_parse_tentatively (parser
);
35380 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35382 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35384 cp_lexer_consume_token (parser
->lexer
);
35387 else if (bracecount
35388 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35389 cp_lexer_consume_token (parser
->lexer
);
35392 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35393 error_at (loc
, "not enough for loops to collapse");
35394 collapse_err
= true;
35395 cp_parser_abort_tentative_parse (parser
);
35403 cp_parser_parse_definitely (parser
);
35404 nbraces
+= bracecount
;
35411 /* Note that we saved the original contents of this flag when we entered
35412 the structured block, and so we don't need to re-save it here. */
35413 parser
->in_statement
= IN_OMP_FOR
;
35415 /* Note that the grammar doesn't call for a structured block here,
35416 though the loop as a whole is a structured block. */
35417 body
= push_stmt_list ();
35418 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35419 body
= pop_stmt_list (body
);
35421 if (declv
== NULL_TREE
)
35424 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35425 body
, pre_body
, &orig_inits
, clauses
);
35429 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35431 cp_lexer_consume_token (parser
->lexer
);
35434 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35435 cp_lexer_consume_token (parser
->lexer
);
35440 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35441 "collapsed loops not perfectly nested");
35443 collapse_err
= true;
35444 cp_parser_statement_seq_opt (parser
, NULL
);
35445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35450 while (!for_block
->is_empty ())
35452 tree t
= for_block
->pop ();
35453 if (TREE_CODE (t
) == STATEMENT_LIST
)
35454 add_stmt (pop_stmt_list (t
));
35458 release_tree_vector (for_block
);
35463 /* Helper function for OpenMP parsing, split clauses and call
35464 finish_omp_clauses on each of the set of clauses afterwards. */
35467 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35468 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35471 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35472 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35474 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35478 #pragma omp simd simd-clause[optseq] new-line
35481 #define OMP_SIMD_CLAUSE_MASK \
35482 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35492 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35493 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35496 tree clauses
, sb
, ret
;
35498 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35500 strcat (p_name
, " simd");
35501 mask
|= OMP_SIMD_CLAUSE_MASK
;
35503 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35507 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35508 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35509 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35510 OMP_CLAUSE_ORDERED
);
35511 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35513 error_at (OMP_CLAUSE_LOCATION (c
),
35514 "%<ordered%> clause with parameter may not be specified "
35515 "on %qs construct", p_name
);
35516 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35520 sb
= begin_omp_structured_block ();
35521 save
= cp_parser_begin_omp_structured_block (parser
);
35523 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35525 cp_parser_end_omp_structured_block (parser
, save
);
35526 add_stmt (finish_omp_structured_block (sb
));
35532 #pragma omp for for-clause[optseq] new-line
35536 #pragma omp for simd for-simd-clause[optseq] new-line
35539 #define OMP_FOR_CLAUSE_MASK \
35540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35551 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35552 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35555 tree clauses
, sb
, ret
;
35557 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35559 strcat (p_name
, " for");
35560 mask
|= OMP_FOR_CLAUSE_MASK
;
35561 /* parallel for{, simd} disallows nowait clause, but for
35562 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35563 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35564 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35565 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35566 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35567 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35571 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35572 const char *p
= IDENTIFIER_POINTER (id
);
35574 if (strcmp (p
, "simd") == 0)
35576 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35577 if (cclauses
== NULL
)
35578 cclauses
= cclauses_buf
;
35580 cp_lexer_consume_token (parser
->lexer
);
35581 if (!flag_openmp
) /* flag_openmp_simd */
35582 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35584 sb
= begin_omp_structured_block ();
35585 save
= cp_parser_begin_omp_structured_block (parser
);
35586 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35588 cp_parser_end_omp_structured_block (parser
, save
);
35589 tree body
= finish_omp_structured_block (sb
);
35592 ret
= make_node (OMP_FOR
);
35593 TREE_TYPE (ret
) = void_type_node
;
35594 OMP_FOR_BODY (ret
) = body
;
35595 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35596 SET_EXPR_LOCATION (ret
, loc
);
35601 if (!flag_openmp
) /* flag_openmp_simd */
35603 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35607 /* Composite distribute parallel for disallows linear clause. */
35608 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35609 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35611 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35615 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35616 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35619 sb
= begin_omp_structured_block ();
35620 save
= cp_parser_begin_omp_structured_block (parser
);
35622 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35624 cp_parser_end_omp_structured_block (parser
, save
);
35625 add_stmt (finish_omp_structured_block (sb
));
35631 # pragma omp master new-line
35632 structured-block */
35635 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35637 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35638 return c_finish_omp_master (input_location
,
35639 cp_parser_omp_structured_block (parser
, if_p
));
35643 # pragma omp ordered new-line
35647 # pragma omp ordered ordered-clauses new-line
35648 structured-block */
35650 #define OMP_ORDERED_CLAUSE_MASK \
35651 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35654 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35655 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35658 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35659 enum pragma_context context
, bool *if_p
)
35661 location_t loc
= pragma_tok
->location
;
35663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35665 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35666 const char *p
= IDENTIFIER_POINTER (id
);
35668 if (strcmp (p
, "depend") == 0)
35670 if (!flag_openmp
) /* flag_openmp_simd */
35672 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35675 if (context
== pragma_stmt
)
35677 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35678 "%<depend%> clause may only be used in compound "
35680 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35684 = cp_parser_omp_all_clauses (parser
,
35685 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35686 "#pragma omp ordered", pragma_tok
);
35687 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35693 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35694 "#pragma omp ordered", pragma_tok
);
35696 if (!flag_openmp
/* flag_openmp_simd */
35697 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35700 c_finish_omp_ordered (loc
, clauses
,
35701 cp_parser_omp_structured_block (parser
, if_p
));
35708 { section-sequence }
35711 section-directive[opt] structured-block
35712 section-sequence section-directive structured-block */
35715 cp_parser_omp_sections_scope (cp_parser
*parser
)
35717 tree stmt
, substmt
;
35718 bool error_suppress
= false;
35721 matching_braces braces
;
35722 if (!braces
.require_open (parser
))
35725 stmt
= push_stmt_list ();
35727 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35728 != PRAGMA_OMP_SECTION
)
35730 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35731 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35732 add_stmt (substmt
);
35737 tok
= cp_lexer_peek_token (parser
->lexer
);
35738 if (tok
->type
== CPP_CLOSE_BRACE
)
35740 if (tok
->type
== CPP_EOF
)
35743 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35745 cp_lexer_consume_token (parser
->lexer
);
35746 cp_parser_require_pragma_eol (parser
, tok
);
35747 error_suppress
= false;
35749 else if (!error_suppress
)
35751 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35752 error_suppress
= true;
35755 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35756 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35757 add_stmt (substmt
);
35759 braces
.require_close (parser
);
35761 substmt
= pop_stmt_list (stmt
);
35763 stmt
= make_node (OMP_SECTIONS
);
35764 TREE_TYPE (stmt
) = void_type_node
;
35765 OMP_SECTIONS_BODY (stmt
) = substmt
;
35772 # pragma omp sections sections-clause[optseq] newline
35775 #define OMP_SECTIONS_CLAUSE_MASK \
35776 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35783 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35784 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35787 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35789 strcat (p_name
, " sections");
35790 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35792 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35794 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35798 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35799 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35802 ret
= cp_parser_omp_sections_scope (parser
);
35804 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35810 # pragma omp parallel parallel-clause[optseq] new-line
35812 # pragma omp parallel for parallel-for-clause[optseq] new-line
35814 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35818 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35819 structured-block */
35821 #define OMP_PARALLEL_CLAUSE_MASK \
35822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35833 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35834 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35837 tree stmt
, clauses
, block
;
35839 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35841 strcat (p_name
, " parallel");
35842 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35843 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35844 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35845 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35846 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35848 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35850 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35851 if (cclauses
== NULL
)
35852 cclauses
= cclauses_buf
;
35854 cp_lexer_consume_token (parser
->lexer
);
35855 if (!flag_openmp
) /* flag_openmp_simd */
35856 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35858 block
= begin_omp_parallel ();
35859 save
= cp_parser_begin_omp_structured_block (parser
);
35860 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35862 cp_parser_end_omp_structured_block (parser
, save
);
35863 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35865 if (ret
== NULL_TREE
)
35867 OMP_PARALLEL_COMBINED (stmt
) = 1;
35870 /* When combined with distribute, parallel has to be followed by for.
35871 #pragma omp target parallel is allowed though. */
35873 && (mask
& (OMP_CLAUSE_MASK_1
35874 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35876 error_at (loc
, "expected %<for%> after %qs", p_name
);
35877 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35880 else if (!flag_openmp
) /* flag_openmp_simd */
35882 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35885 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35887 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35888 const char *p
= IDENTIFIER_POINTER (id
);
35889 if (strcmp (p
, "sections") == 0)
35891 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35892 cclauses
= cclauses_buf
;
35894 cp_lexer_consume_token (parser
->lexer
);
35895 block
= begin_omp_parallel ();
35896 save
= cp_parser_begin_omp_structured_block (parser
);
35897 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35898 cp_parser_end_omp_structured_block (parser
, save
);
35899 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35901 OMP_PARALLEL_COMBINED (stmt
) = 1;
35906 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35910 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35911 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35914 block
= begin_omp_parallel ();
35915 save
= cp_parser_begin_omp_structured_block (parser
);
35916 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35917 cp_parser_end_omp_structured_block (parser
, save
);
35918 stmt
= finish_omp_parallel (clauses
, block
);
35923 # pragma omp single single-clause[optseq] new-line
35924 structured-block */
35926 #define OMP_SINGLE_CLAUSE_MASK \
35927 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35933 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35935 tree stmt
= make_node (OMP_SINGLE
);
35936 TREE_TYPE (stmt
) = void_type_node
;
35938 OMP_SINGLE_CLAUSES (stmt
)
35939 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35940 "#pragma omp single", pragma_tok
);
35941 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35943 return add_stmt (stmt
);
35947 # pragma omp task task-clause[optseq] new-line
35948 structured-block */
35950 #define OMP_TASK_CLAUSE_MASK \
35951 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35963 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35965 tree clauses
, block
;
35968 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35969 "#pragma omp task", pragma_tok
);
35970 block
= begin_omp_task ();
35971 save
= cp_parser_begin_omp_structured_block (parser
);
35972 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35973 cp_parser_end_omp_structured_block (parser
, save
);
35974 return finish_omp_task (clauses
, block
);
35978 # pragma omp taskwait new-line */
35981 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35983 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35984 finish_omp_taskwait ();
35988 # pragma omp taskyield new-line */
35991 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35993 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35994 finish_omp_taskyield ();
35998 # pragma omp taskgroup new-line
35999 structured-block */
36002 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36004 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36005 return c_finish_omp_taskgroup (input_location
,
36006 cp_parser_omp_structured_block (parser
,
36012 # pragma omp threadprivate (variable-list) */
36015 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
36019 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36020 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36022 finish_omp_threadprivate (vars
);
36026 # pragma omp cancel cancel-clause[optseq] new-line */
36028 #define OMP_CANCEL_CLAUSE_MASK \
36029 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36036 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36038 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36039 "#pragma omp cancel", pragma_tok
);
36040 finish_omp_cancel (clauses
);
36044 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36046 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36047 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36053 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36054 enum pragma_context context
)
36057 bool point_seen
= false;
36059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36061 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36062 const char *p
= IDENTIFIER_POINTER (id
);
36064 if (strcmp (p
, "point") == 0)
36066 cp_lexer_consume_token (parser
->lexer
);
36072 cp_parser_error (parser
, "expected %<point%>");
36073 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36077 if (context
!= pragma_compound
)
36079 if (context
== pragma_stmt
)
36080 error_at (pragma_tok
->location
,
36081 "%<#pragma %s%> may only be used in compound statements",
36082 "omp cancellation point");
36084 cp_parser_error (parser
, "expected declaration specifiers");
36085 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36089 clauses
= cp_parser_omp_all_clauses (parser
,
36090 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36091 "#pragma omp cancellation point",
36093 finish_omp_cancellation_point (clauses
);
36097 #pragma omp distribute distribute-clause[optseq] new-line
36100 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36101 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36108 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36109 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36112 tree clauses
, sb
, ret
;
36114 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36116 strcat (p_name
, " distribute");
36117 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36121 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36122 const char *p
= IDENTIFIER_POINTER (id
);
36124 bool parallel
= false;
36126 if (strcmp (p
, "simd") == 0)
36129 parallel
= strcmp (p
, "parallel") == 0;
36130 if (parallel
|| simd
)
36132 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36133 if (cclauses
== NULL
)
36134 cclauses
= cclauses_buf
;
36135 cp_lexer_consume_token (parser
->lexer
);
36136 if (!flag_openmp
) /* flag_openmp_simd */
36139 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36142 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36145 sb
= begin_omp_structured_block ();
36146 save
= cp_parser_begin_omp_structured_block (parser
);
36148 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36151 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36153 cp_parser_end_omp_structured_block (parser
, save
);
36154 tree body
= finish_omp_structured_block (sb
);
36157 ret
= make_node (OMP_DISTRIBUTE
);
36158 TREE_TYPE (ret
) = void_type_node
;
36159 OMP_FOR_BODY (ret
) = body
;
36160 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36161 SET_EXPR_LOCATION (ret
, loc
);
36166 if (!flag_openmp
) /* flag_openmp_simd */
36168 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36172 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36176 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36177 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36180 sb
= begin_omp_structured_block ();
36181 save
= cp_parser_begin_omp_structured_block (parser
);
36183 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36185 cp_parser_end_omp_structured_block (parser
, save
);
36186 add_stmt (finish_omp_structured_block (sb
));
36192 # pragma omp teams teams-clause[optseq] new-line
36193 structured-block */
36195 #define OMP_TEAMS_CLAUSE_MASK \
36196 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36205 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36206 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36209 tree clauses
, sb
, ret
;
36211 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36213 strcat (p_name
, " teams");
36214 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36218 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36219 const char *p
= IDENTIFIER_POINTER (id
);
36220 if (strcmp (p
, "distribute") == 0)
36222 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36223 if (cclauses
== NULL
)
36224 cclauses
= cclauses_buf
;
36226 cp_lexer_consume_token (parser
->lexer
);
36227 if (!flag_openmp
) /* flag_openmp_simd */
36228 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36230 sb
= begin_omp_structured_block ();
36231 save
= cp_parser_begin_omp_structured_block (parser
);
36232 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36234 cp_parser_end_omp_structured_block (parser
, save
);
36235 tree body
= finish_omp_structured_block (sb
);
36238 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36239 ret
= make_node (OMP_TEAMS
);
36240 TREE_TYPE (ret
) = void_type_node
;
36241 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36242 OMP_TEAMS_BODY (ret
) = body
;
36243 OMP_TEAMS_COMBINED (ret
) = 1;
36244 SET_EXPR_LOCATION (ret
, loc
);
36245 return add_stmt (ret
);
36248 if (!flag_openmp
) /* flag_openmp_simd */
36250 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36254 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36258 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36259 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36262 tree stmt
= make_node (OMP_TEAMS
);
36263 TREE_TYPE (stmt
) = void_type_node
;
36264 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36265 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36266 SET_EXPR_LOCATION (stmt
, loc
);
36268 return add_stmt (stmt
);
36272 # pragma omp target data target-data-clause[optseq] new-line
36273 structured-block */
36275 #define OMP_TARGET_DATA_CLAUSE_MASK \
36276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36282 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36285 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36286 "#pragma omp target data", pragma_tok
);
36288 for (tree
*pc
= &clauses
; *pc
;)
36290 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36291 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36294 case GOMP_MAP_ALWAYS_TO
:
36295 case GOMP_MAP_FROM
:
36296 case GOMP_MAP_ALWAYS_FROM
:
36297 case GOMP_MAP_TOFROM
:
36298 case GOMP_MAP_ALWAYS_TOFROM
:
36299 case GOMP_MAP_ALLOC
:
36302 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36303 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36304 case GOMP_MAP_ALWAYS_POINTER
:
36308 error_at (OMP_CLAUSE_LOCATION (*pc
),
36309 "%<#pragma omp target data%> with map-type other "
36310 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36311 "on %<map%> clause");
36312 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36315 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36321 error_at (pragma_tok
->location
,
36322 "%<#pragma omp target data%> must contain at least "
36323 "one %<map%> clause");
36327 tree stmt
= make_node (OMP_TARGET_DATA
);
36328 TREE_TYPE (stmt
) = void_type_node
;
36329 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36331 keep_next_level (true);
36332 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36334 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36335 return add_stmt (stmt
);
36339 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36340 structured-block */
36342 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36343 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36350 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36351 enum pragma_context context
)
36353 bool data_seen
= false;
36354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36356 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36357 const char *p
= IDENTIFIER_POINTER (id
);
36359 if (strcmp (p
, "data") == 0)
36361 cp_lexer_consume_token (parser
->lexer
);
36367 cp_parser_error (parser
, "expected %<data%>");
36368 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36372 if (context
== pragma_stmt
)
36374 error_at (pragma_tok
->location
,
36375 "%<#pragma %s%> may only be used in compound statements",
36376 "omp target enter data");
36377 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36382 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36383 "#pragma omp target enter data", pragma_tok
);
36385 for (tree
*pc
= &clauses
; *pc
;)
36387 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36388 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36391 case GOMP_MAP_ALWAYS_TO
:
36392 case GOMP_MAP_ALLOC
:
36395 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36396 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36397 case GOMP_MAP_ALWAYS_POINTER
:
36401 error_at (OMP_CLAUSE_LOCATION (*pc
),
36402 "%<#pragma omp target enter data%> with map-type other "
36403 "than %<to%> or %<alloc%> on %<map%> clause");
36404 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36407 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36413 error_at (pragma_tok
->location
,
36414 "%<#pragma omp target enter data%> must contain at least "
36415 "one %<map%> clause");
36419 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36420 TREE_TYPE (stmt
) = void_type_node
;
36421 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36422 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36423 return add_stmt (stmt
);
36427 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36428 structured-block */
36430 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36431 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36438 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36439 enum pragma_context context
)
36441 bool data_seen
= false;
36442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36444 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36445 const char *p
= IDENTIFIER_POINTER (id
);
36447 if (strcmp (p
, "data") == 0)
36449 cp_lexer_consume_token (parser
->lexer
);
36455 cp_parser_error (parser
, "expected %<data%>");
36456 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36460 if (context
== pragma_stmt
)
36462 error_at (pragma_tok
->location
,
36463 "%<#pragma %s%> may only be used in compound statements",
36464 "omp target exit data");
36465 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36470 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36471 "#pragma omp target exit data", pragma_tok
);
36473 for (tree
*pc
= &clauses
; *pc
;)
36475 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36476 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36478 case GOMP_MAP_FROM
:
36479 case GOMP_MAP_ALWAYS_FROM
:
36480 case GOMP_MAP_RELEASE
:
36481 case GOMP_MAP_DELETE
:
36484 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36485 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36486 case GOMP_MAP_ALWAYS_POINTER
:
36490 error_at (OMP_CLAUSE_LOCATION (*pc
),
36491 "%<#pragma omp target exit data%> with map-type other "
36492 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36494 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36497 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36503 error_at (pragma_tok
->location
,
36504 "%<#pragma omp target exit data%> must contain at least "
36505 "one %<map%> clause");
36509 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36510 TREE_TYPE (stmt
) = void_type_node
;
36511 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36512 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36513 return add_stmt (stmt
);
36517 # pragma omp target update target-update-clause[optseq] new-line */
36519 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36520 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36528 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36529 enum pragma_context context
)
36531 if (context
== pragma_stmt
)
36533 error_at (pragma_tok
->location
,
36534 "%<#pragma %s%> may only be used in compound statements",
36535 "omp target update");
36536 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36541 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36542 "#pragma omp target update", pragma_tok
);
36543 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36544 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36546 error_at (pragma_tok
->location
,
36547 "%<#pragma omp target update%> must contain at least one "
36548 "%<from%> or %<to%> clauses");
36552 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36553 TREE_TYPE (stmt
) = void_type_node
;
36554 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36555 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36561 # pragma omp target target-clause[optseq] new-line
36562 structured-block */
36564 #define OMP_TARGET_CLAUSE_MASK \
36565 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36576 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36577 enum pragma_context context
, bool *if_p
)
36579 tree
*pc
= NULL
, stmt
;
36581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36583 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36584 const char *p
= IDENTIFIER_POINTER (id
);
36585 enum tree_code ccode
= ERROR_MARK
;
36587 if (strcmp (p
, "teams") == 0)
36589 else if (strcmp (p
, "parallel") == 0)
36590 ccode
= OMP_PARALLEL
;
36591 else if (strcmp (p
, "simd") == 0)
36593 if (ccode
!= ERROR_MARK
)
36595 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36596 char p_name
[sizeof ("#pragma omp target teams distribute "
36597 "parallel for simd")];
36599 cp_lexer_consume_token (parser
->lexer
);
36600 strcpy (p_name
, "#pragma omp target");
36601 if (!flag_openmp
) /* flag_openmp_simd */
36607 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36608 OMP_TARGET_CLAUSE_MASK
,
36612 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36613 OMP_TARGET_CLAUSE_MASK
,
36617 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36618 OMP_TARGET_CLAUSE_MASK
,
36622 gcc_unreachable ();
36624 return stmt
!= NULL_TREE
;
36626 keep_next_level (true);
36627 tree sb
= begin_omp_structured_block (), ret
;
36628 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36632 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36633 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36637 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36638 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36642 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36643 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36647 gcc_unreachable ();
36649 cp_parser_end_omp_structured_block (parser
, save
);
36650 tree body
= finish_omp_structured_block (sb
);
36651 if (ret
== NULL_TREE
)
36653 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36655 /* For combined target teams, ensure the num_teams and
36656 thread_limit clause expressions are evaluated on the host,
36657 before entering the target construct. */
36659 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36660 c
; c
= OMP_CLAUSE_CHAIN (c
))
36661 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36662 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36663 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36665 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36666 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36667 if (expr
== error_mark_node
)
36669 tree tmp
= TARGET_EXPR_SLOT (expr
);
36671 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36672 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36673 OMP_CLAUSE_FIRSTPRIVATE
);
36674 OMP_CLAUSE_DECL (tc
) = tmp
;
36675 OMP_CLAUSE_CHAIN (tc
)
36676 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36677 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36680 tree stmt
= make_node (OMP_TARGET
);
36681 TREE_TYPE (stmt
) = void_type_node
;
36682 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36683 OMP_TARGET_BODY (stmt
) = body
;
36684 OMP_TARGET_COMBINED (stmt
) = 1;
36685 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36687 pc
= &OMP_TARGET_CLAUSES (stmt
);
36688 goto check_clauses
;
36690 else if (!flag_openmp
) /* flag_openmp_simd */
36692 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36695 else if (strcmp (p
, "data") == 0)
36697 cp_lexer_consume_token (parser
->lexer
);
36698 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36701 else if (strcmp (p
, "enter") == 0)
36703 cp_lexer_consume_token (parser
->lexer
);
36704 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36707 else if (strcmp (p
, "exit") == 0)
36709 cp_lexer_consume_token (parser
->lexer
);
36710 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36713 else if (strcmp (p
, "update") == 0)
36715 cp_lexer_consume_token (parser
->lexer
);
36716 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36719 if (!flag_openmp
) /* flag_openmp_simd */
36721 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36725 stmt
= make_node (OMP_TARGET
);
36726 TREE_TYPE (stmt
) = void_type_node
;
36728 OMP_TARGET_CLAUSES (stmt
)
36729 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36730 "#pragma omp target", pragma_tok
);
36731 pc
= &OMP_TARGET_CLAUSES (stmt
);
36732 keep_next_level (true);
36733 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36735 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36741 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36742 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36745 case GOMP_MAP_ALWAYS_TO
:
36746 case GOMP_MAP_FROM
:
36747 case GOMP_MAP_ALWAYS_FROM
:
36748 case GOMP_MAP_TOFROM
:
36749 case GOMP_MAP_ALWAYS_TOFROM
:
36750 case GOMP_MAP_ALLOC
:
36751 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36752 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36753 case GOMP_MAP_ALWAYS_POINTER
:
36756 error_at (OMP_CLAUSE_LOCATION (*pc
),
36757 "%<#pragma omp target%> with map-type other "
36758 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36759 "on %<map%> clause");
36760 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36763 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36769 # pragma acc cache (variable-list) new-line
36773 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36775 tree stmt
, clauses
;
36777 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36778 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36780 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36782 stmt
= make_node (OACC_CACHE
);
36783 TREE_TYPE (stmt
) = void_type_node
;
36784 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36785 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36792 # pragma acc data oacc-data-clause[optseq] new-line
36793 structured-block */
36795 #define OACC_DATA_CLAUSE_MASK \
36796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36809 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36811 tree stmt
, clauses
, block
;
36814 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36815 "#pragma acc data", pragma_tok
);
36817 block
= begin_omp_parallel ();
36818 save
= cp_parser_begin_omp_structured_block (parser
);
36819 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36820 cp_parser_end_omp_structured_block (parser
, save
);
36821 stmt
= finish_oacc_data (clauses
, block
);
36826 # pragma acc host_data <clauses> new-line
36827 structured-block */
36829 #define OACC_HOST_DATA_CLAUSE_MASK \
36830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36833 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36835 tree stmt
, clauses
, block
;
36838 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36839 "#pragma acc host_data", pragma_tok
);
36841 block
= begin_omp_parallel ();
36842 save
= cp_parser_begin_omp_structured_block (parser
);
36843 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36844 cp_parser_end_omp_structured_block (parser
, save
);
36845 stmt
= finish_oacc_host_data (clauses
, block
);
36850 # pragma acc declare oacc-data-clause[optseq] new-line
36853 #define OACC_DECLARE_CLAUSE_MASK \
36854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36868 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36870 tree clauses
, stmt
;
36871 bool error
= false;
36873 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36874 "#pragma acc declare", pragma_tok
, true);
36877 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36879 error_at (pragma_tok
->location
,
36880 "no valid clauses specified in %<#pragma acc declare%>");
36884 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36886 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36887 tree decl
= OMP_CLAUSE_DECL (t
);
36888 if (!DECL_P (decl
))
36890 error_at (loc
, "array section in %<#pragma acc declare%>");
36894 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36895 switch (OMP_CLAUSE_MAP_KIND (t
))
36897 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36898 case GOMP_MAP_FORCE_ALLOC
:
36899 case GOMP_MAP_FORCE_TO
:
36900 case GOMP_MAP_FORCE_DEVICEPTR
:
36901 case GOMP_MAP_DEVICE_RESIDENT
:
36904 case GOMP_MAP_LINK
:
36905 if (!global_bindings_p ()
36906 && (TREE_STATIC (decl
)
36907 || !DECL_EXTERNAL (decl
)))
36910 "%qD must be a global variable in "
36911 "%<#pragma acc declare link%>",
36919 if (global_bindings_p ())
36921 error_at (loc
, "invalid OpenACC clause at file scope");
36925 if (DECL_EXTERNAL (decl
))
36928 "invalid use of %<extern%> variable %qD "
36929 "in %<#pragma acc declare%>", decl
);
36933 else if (TREE_PUBLIC (decl
))
36936 "invalid use of %<global%> variable %qD "
36937 "in %<#pragma acc declare%>", decl
);
36944 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36945 || lookup_attribute ("omp declare target link",
36946 DECL_ATTRIBUTES (decl
)))
36948 error_at (loc
, "variable %qD used more than once with "
36949 "%<#pragma acc declare%>", decl
);
36958 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36959 id
= get_identifier ("omp declare target link");
36961 id
= get_identifier ("omp declare target");
36963 DECL_ATTRIBUTES (decl
)
36964 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36965 if (global_bindings_p ())
36967 symtab_node
*node
= symtab_node::get (decl
);
36970 node
->offloadable
= 1;
36971 if (ENABLE_OFFLOADING
)
36973 g
->have_offload
= true;
36974 if (is_a
<varpool_node
*> (node
))
36975 vec_safe_push (offload_vars
, decl
);
36982 if (error
|| global_bindings_p ())
36985 stmt
= make_node (OACC_DECLARE
);
36986 TREE_TYPE (stmt
) = void_type_node
;
36987 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36988 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36996 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37000 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37002 LOC is the location of the #pragma token.
37005 #define OACC_ENTER_DATA_CLAUSE_MASK \
37006 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37014 #define OACC_EXIT_DATA_CLAUSE_MASK \
37015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37022 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37025 location_t loc
= pragma_tok
->location
;
37026 tree stmt
, clauses
;
37027 const char *p
= "";
37029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37030 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37032 if (strcmp (p
, "data") != 0)
37034 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37035 enter
? "enter" : "exit");
37036 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37040 cp_lexer_consume_token (parser
->lexer
);
37043 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37044 "#pragma acc enter data", pragma_tok
);
37046 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37047 "#pragma acc exit data", pragma_tok
);
37049 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37051 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37052 enter
? "enter" : "exit");
37056 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37057 TREE_TYPE (stmt
) = void_type_node
;
37058 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37059 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37065 # pragma acc loop oacc-loop-clause[optseq] new-line
37066 structured-block */
37068 #define OACC_LOOP_CLAUSE_MASK \
37069 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37081 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37082 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37084 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37086 strcat (p_name
, " loop");
37087 mask
|= OACC_LOOP_CLAUSE_MASK
;
37089 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37093 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37095 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37097 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37100 tree block
= begin_omp_structured_block ();
37101 int save
= cp_parser_begin_omp_structured_block (parser
);
37102 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37103 cp_parser_end_omp_structured_block (parser
, save
);
37104 add_stmt (finish_omp_structured_block (block
));
37110 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37115 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37119 #define OACC_KERNELS_CLAUSE_MASK \
37120 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37138 #define OACC_PARALLEL_CLAUSE_MASK \
37139 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37161 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37162 char *p_name
, bool *if_p
)
37164 omp_clause_mask mask
;
37165 enum tree_code code
;
37166 switch (cp_parser_pragma_kind (pragma_tok
))
37168 case PRAGMA_OACC_KERNELS
:
37169 strcat (p_name
, " kernels");
37170 mask
= OACC_KERNELS_CLAUSE_MASK
;
37171 code
= OACC_KERNELS
;
37173 case PRAGMA_OACC_PARALLEL
:
37174 strcat (p_name
, " parallel");
37175 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37176 code
= OACC_PARALLEL
;
37179 gcc_unreachable ();
37182 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37185 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37186 if (strcmp (p
, "loop") == 0)
37188 cp_lexer_consume_token (parser
->lexer
);
37189 tree block
= begin_omp_parallel ();
37191 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37193 return finish_omp_construct (code
, block
, clauses
);
37197 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37199 tree block
= begin_omp_parallel ();
37200 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37201 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37202 cp_parser_end_omp_structured_block (parser
, save
);
37203 return finish_omp_construct (code
, block
, clauses
);
37207 # pragma acc update oacc-update-clause[optseq] new-line
37210 #define OACC_UPDATE_CLAUSE_MASK \
37211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37219 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37221 tree stmt
, clauses
;
37223 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37224 "#pragma acc update", pragma_tok
);
37226 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37228 error_at (pragma_tok
->location
,
37229 "%<#pragma acc update%> must contain at least one "
37230 "%<device%> or %<host%> or %<self%> clause");
37234 stmt
= make_node (OACC_UPDATE
);
37235 TREE_TYPE (stmt
) = void_type_node
;
37236 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37237 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37243 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37245 LOC is the location of the #pragma token.
37248 #define OACC_WAIT_CLAUSE_MASK \
37249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37252 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37254 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37255 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37257 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37258 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37260 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37261 "#pragma acc wait", pragma_tok
);
37263 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37264 stmt
= finish_expr_stmt (stmt
);
37270 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37272 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37273 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37281 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37282 enum pragma_context context
)
37284 bool first_p
= parser
->omp_declare_simd
== NULL
;
37285 cp_omp_declare_simd_data data
;
37288 data
.error_seen
= false;
37289 data
.fndecl_seen
= false;
37290 data
.tokens
= vNULL
;
37291 data
.clauses
= NULL_TREE
;
37292 /* It is safe to take the address of a local variable; it will only be
37293 used while this scope is live. */
37294 parser
->omp_declare_simd
= &data
;
37297 /* Store away all pragma tokens. */
37298 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37299 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37300 cp_lexer_consume_token (parser
->lexer
);
37301 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37302 parser
->omp_declare_simd
->error_seen
= true;
37303 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37304 struct cp_token_cache
*cp
37305 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37306 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37310 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37311 cp_parser_pragma (parser
, context
, NULL
);
37314 case pragma_external
:
37315 cp_parser_declaration (parser
);
37317 case pragma_member
:
37318 cp_parser_member_declaration (parser
);
37320 case pragma_objc_icode
:
37321 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37324 cp_parser_declaration_statement (parser
);
37327 if (parser
->omp_declare_simd
37328 && !parser
->omp_declare_simd
->error_seen
37329 && !parser
->omp_declare_simd
->fndecl_seen
)
37330 error_at (pragma_tok
->location
,
37331 "%<#pragma omp declare simd%> not immediately followed by "
37332 "function declaration or definition");
37333 data
.tokens
.release ();
37334 parser
->omp_declare_simd
= NULL
;
37338 /* Finalize #pragma omp declare simd clauses after direct declarator has
37339 been parsed, and put that into "omp declare simd" attribute. */
37342 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37344 struct cp_token_cache
*ce
;
37345 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37348 if (!data
->error_seen
&& data
->fndecl_seen
)
37350 error ("%<#pragma omp declare simd%> not immediately followed by "
37351 "a single function declaration or definition");
37352 data
->error_seen
= true;
37354 if (data
->error_seen
)
37357 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37361 cp_parser_push_lexer_for_tokens (parser
, ce
);
37362 parser
->lexer
->in_pragma
= true;
37363 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37364 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37365 cp_lexer_consume_token (parser
->lexer
);
37366 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37367 "#pragma omp declare simd", pragma_tok
);
37368 cp_parser_pop_lexer (parser
);
37370 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37371 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37372 TREE_CHAIN (c
) = attrs
;
37373 if (processing_template_decl
)
37374 ATTR_IS_DEPENDENT (c
) = 1;
37378 data
->fndecl_seen
= true;
37384 # pragma omp declare target new-line
37385 declarations and definitions
37386 # pragma omp end declare target new-line
37389 # pragma omp declare target ( extended-list ) new-line
37391 # pragma omp declare target declare-target-clauses[seq] new-line */
37393 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37394 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37398 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37400 tree clauses
= NULL_TREE
;
37401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37403 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37404 "#pragma omp declare target", pragma_tok
);
37405 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37407 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37409 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37410 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37414 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37415 scope_chain
->omp_declare_target_attribute
++;
37418 if (scope_chain
->omp_declare_target_attribute
)
37419 error_at (pragma_tok
->location
,
37420 "%<#pragma omp declare target%> with clauses in between "
37421 "%<#pragma omp declare target%> without clauses and "
37422 "%<#pragma omp end declare target%>");
37423 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37425 tree t
= OMP_CLAUSE_DECL (c
), id
;
37426 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37427 tree at2
= lookup_attribute ("omp declare target link",
37428 DECL_ATTRIBUTES (t
));
37429 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37431 id
= get_identifier ("omp declare target link");
37432 std::swap (at1
, at2
);
37435 id
= get_identifier ("omp declare target");
37438 error_at (OMP_CLAUSE_LOCATION (c
),
37439 "%qD specified both in declare target %<link%> and %<to%>"
37445 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37446 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37449 symtab_node
*node
= symtab_node::get (t
);
37452 node
->offloadable
= 1;
37453 if (ENABLE_OFFLOADING
)
37455 g
->have_offload
= true;
37456 if (is_a
<varpool_node
*> (node
))
37457 vec_safe_push (offload_vars
, t
);
37465 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37467 const char *p
= "";
37468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37470 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37471 p
= IDENTIFIER_POINTER (id
);
37473 if (strcmp (p
, "declare") == 0)
37475 cp_lexer_consume_token (parser
->lexer
);
37477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37479 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37480 p
= IDENTIFIER_POINTER (id
);
37482 if (strcmp (p
, "target") == 0)
37483 cp_lexer_consume_token (parser
->lexer
);
37486 cp_parser_error (parser
, "expected %<target%>");
37487 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37493 cp_parser_error (parser
, "expected %<declare%>");
37494 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37497 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37498 if (!scope_chain
->omp_declare_target_attribute
)
37499 error_at (pragma_tok
->location
,
37500 "%<#pragma omp end declare target%> without corresponding "
37501 "%<#pragma omp declare target%>");
37503 scope_chain
->omp_declare_target_attribute
--;
37506 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37507 expression and optional initializer clause of
37508 #pragma omp declare reduction. We store the expression(s) as
37509 either 3, 6 or 7 special statements inside of the artificial function's
37510 body. The first two statements are DECL_EXPRs for the artificial
37511 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37512 expression that uses those variables.
37513 If there was any INITIALIZER clause, this is followed by further statements,
37514 the fourth and fifth statements are DECL_EXPRs for the artificial
37515 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37516 constructor variant (first token after open paren is not omp_priv),
37517 then the sixth statement is a statement with the function call expression
37518 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37519 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37520 to initialize the OMP_PRIV artificial variable and there is seventh
37521 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37524 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37526 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37527 gcc_assert (TYPE_REF_P (type
));
37528 type
= TREE_TYPE (type
);
37529 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37530 DECL_ARTIFICIAL (omp_out
) = 1;
37531 pushdecl (omp_out
);
37532 add_decl_expr (omp_out
);
37533 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37534 DECL_ARTIFICIAL (omp_in
) = 1;
37536 add_decl_expr (omp_in
);
37538 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37540 keep_next_level (true);
37541 tree block
= begin_omp_structured_block ();
37542 combiner
= cp_parser_expression (parser
);
37543 finish_expr_stmt (combiner
);
37544 block
= finish_omp_structured_block (block
);
37547 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37550 const char *p
= "";
37551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37553 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37554 p
= IDENTIFIER_POINTER (id
);
37557 if (strcmp (p
, "initializer") == 0)
37559 cp_lexer_consume_token (parser
->lexer
);
37560 matching_parens parens
;
37561 if (!parens
.require_open (parser
))
37565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37567 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37568 p
= IDENTIFIER_POINTER (id
);
37571 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37572 DECL_ARTIFICIAL (omp_priv
) = 1;
37573 pushdecl (omp_priv
);
37574 add_decl_expr (omp_priv
);
37575 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37576 DECL_ARTIFICIAL (omp_orig
) = 1;
37577 pushdecl (omp_orig
);
37578 add_decl_expr (omp_orig
);
37580 keep_next_level (true);
37581 block
= begin_omp_structured_block ();
37584 if (strcmp (p
, "omp_priv") == 0)
37586 bool is_direct_init
, is_non_constant_init
;
37588 cp_lexer_consume_token (parser
->lexer
);
37589 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37591 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37592 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37594 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37595 == CPP_CLOSE_PAREN
))
37597 finish_omp_structured_block (block
);
37598 error ("invalid initializer clause");
37601 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37602 &is_non_constant_init
);
37603 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37604 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37608 cp_parser_parse_tentatively (parser
);
37609 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37610 /*check_dependency_p=*/true,
37611 /*template_p=*/NULL
,
37612 /*declarator_p=*/false,
37613 /*optional_p=*/false);
37614 vec
<tree
, va_gc
> *args
;
37615 if (fn_name
== error_mark_node
37616 || cp_parser_error_occurred (parser
)
37617 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37618 || ((args
= cp_parser_parenthesized_expression_list
37619 (parser
, non_attr
, /*cast_p=*/false,
37620 /*allow_expansion_p=*/true,
37621 /*non_constant_p=*/NULL
)),
37622 cp_parser_error_occurred (parser
)))
37624 finish_omp_structured_block (block
);
37625 cp_parser_abort_tentative_parse (parser
);
37626 cp_parser_error (parser
, "expected id-expression (arguments)");
37631 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37632 if (arg
== omp_priv
37633 || (TREE_CODE (arg
) == ADDR_EXPR
37634 && TREE_OPERAND (arg
, 0) == omp_priv
))
37636 cp_parser_abort_tentative_parse (parser
);
37637 if (arg
== NULL_TREE
)
37638 error ("one of the initializer call arguments should be %<omp_priv%>"
37639 " or %<&omp_priv%>");
37640 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37642 finish_expr_stmt (initializer
);
37645 block
= finish_omp_structured_block (block
);
37646 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37650 add_decl_expr (omp_orig
);
37652 if (!parens
.require_close (parser
))
37656 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37657 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37664 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37665 initializer-clause[opt] new-line
37667 initializer-clause:
37668 initializer (omp_priv initializer)
37669 initializer (function-name (argument-list)) */
37672 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37673 enum pragma_context
)
37675 auto_vec
<tree
> types
;
37676 enum tree_code reduc_code
= ERROR_MARK
;
37677 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37679 cp_token
*first_token
;
37680 cp_token_cache
*cp
;
37684 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37685 p
= obstack_alloc (&declarator_obstack
, 0);
37687 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37690 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37693 reduc_code
= PLUS_EXPR
;
37696 reduc_code
= MULT_EXPR
;
37699 reduc_code
= MINUS_EXPR
;
37702 reduc_code
= BIT_AND_EXPR
;
37705 reduc_code
= BIT_XOR_EXPR
;
37708 reduc_code
= BIT_IOR_EXPR
;
37711 reduc_code
= TRUTH_ANDIF_EXPR
;
37714 reduc_code
= TRUTH_ORIF_EXPR
;
37717 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37720 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37721 "%<|%>, %<&&%>, %<||%> or identifier");
37725 if (reduc_code
!= ERROR_MARK
)
37726 cp_lexer_consume_token (parser
->lexer
);
37728 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37729 if (reduc_id
== error_mark_node
)
37732 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37735 /* Types may not be defined in declare reduction type list. */
37736 const char *saved_message
;
37737 saved_message
= parser
->type_definition_forbidden_message
;
37738 parser
->type_definition_forbidden_message
37739 = G_("types may not be defined in declare reduction type list");
37740 bool saved_colon_corrects_to_scope_p
;
37741 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37742 parser
->colon_corrects_to_scope_p
= false;
37743 bool saved_colon_doesnt_start_class_def_p
;
37744 saved_colon_doesnt_start_class_def_p
37745 = parser
->colon_doesnt_start_class_def_p
;
37746 parser
->colon_doesnt_start_class_def_p
= true;
37750 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37751 type
= cp_parser_type_id (parser
);
37752 if (type
== error_mark_node
)
37754 else if (ARITHMETIC_TYPE_P (type
)
37755 && (orig_reduc_id
== NULL_TREE
37756 || (TREE_CODE (type
) != COMPLEX_TYPE
37757 && (id_equal (orig_reduc_id
, "min")
37758 || id_equal (orig_reduc_id
, "max")))))
37759 error_at (loc
, "predeclared arithmetic type %qT in "
37760 "%<#pragma omp declare reduction%>", type
);
37761 else if (TREE_CODE (type
) == FUNCTION_TYPE
37762 || TREE_CODE (type
) == METHOD_TYPE
37763 || TREE_CODE (type
) == ARRAY_TYPE
)
37764 error_at (loc
, "function or array type %qT in "
37765 "%<#pragma omp declare reduction%>", type
);
37766 else if (TYPE_REF_P (type
))
37767 error_at (loc
, "reference type %qT in "
37768 "%<#pragma omp declare reduction%>", type
);
37769 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37770 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37771 "%<#pragma omp declare reduction%>", type
);
37773 types
.safe_push (type
);
37775 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37776 cp_lexer_consume_token (parser
->lexer
);
37781 /* Restore the saved message. */
37782 parser
->type_definition_forbidden_message
= saved_message
;
37783 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37784 parser
->colon_doesnt_start_class_def_p
37785 = saved_colon_doesnt_start_class_def_p
;
37787 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37788 || types
.is_empty ())
37791 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37795 first_token
= cp_lexer_peek_token (parser
->lexer
);
37798 FOR_EACH_VEC_ELT (types
, i
, type
)
37801 = build_function_type_list (void_type_node
,
37802 cp_build_reference_type (type
, false),
37804 tree this_reduc_id
= reduc_id
;
37805 if (!dependent_type_p (type
))
37806 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37807 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37808 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37809 DECL_ARTIFICIAL (fndecl
) = 1;
37810 DECL_EXTERNAL (fndecl
) = 1;
37811 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37812 DECL_IGNORED_P (fndecl
) = 1;
37813 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37814 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37815 DECL_ATTRIBUTES (fndecl
)
37816 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37817 DECL_ATTRIBUTES (fndecl
));
37818 if (processing_template_decl
)
37819 fndecl
= push_template_decl (fndecl
);
37820 bool block_scope
= false;
37821 tree block
= NULL_TREE
;
37822 if (current_function_decl
)
37824 block_scope
= true;
37825 DECL_CONTEXT (fndecl
) = global_namespace
;
37826 if (!processing_template_decl
)
37829 else if (current_class_type
)
37833 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37834 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37835 cp_lexer_consume_token (parser
->lexer
);
37836 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37838 cp
= cp_token_cache_new (first_token
,
37839 cp_lexer_peek_nth_token (parser
->lexer
,
37842 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37843 finish_member_declaration (fndecl
);
37844 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37845 DECL_PENDING_INLINE_P (fndecl
) = 1;
37846 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37851 DECL_CONTEXT (fndecl
) = current_namespace
;
37855 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37857 block
= begin_omp_structured_block ();
37860 cp_parser_push_lexer_for_tokens (parser
, cp
);
37861 parser
->lexer
->in_pragma
= true;
37863 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37866 finish_function (/*inline_p=*/false);
37868 DECL_CONTEXT (fndecl
) = current_function_decl
;
37870 cp_parser_pop_lexer (parser
);
37874 cp_parser_pop_lexer (parser
);
37876 finish_function (/*inline_p=*/false);
37879 DECL_CONTEXT (fndecl
) = current_function_decl
;
37880 block
= finish_omp_structured_block (block
);
37881 if (TREE_CODE (block
) == BIND_EXPR
)
37882 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37883 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37884 DECL_SAVED_TREE (fndecl
) = block
;
37885 if (processing_template_decl
)
37886 add_decl_expr (fndecl
);
37888 cp_check_omp_declare_reduction (fndecl
);
37889 if (cp
== NULL
&& types
.length () > 1)
37890 cp
= cp_token_cache_new (first_token
,
37891 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37892 if (errs
!= errorcount
)
37896 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37899 /* Free any declarators allocated. */
37900 obstack_free (&declarator_obstack
, p
);
37904 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37905 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37906 initializer-clause[opt] new-line
37907 #pragma omp declare target new-line */
37910 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37911 enum pragma_context context
)
37913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37915 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37916 const char *p
= IDENTIFIER_POINTER (id
);
37918 if (strcmp (p
, "simd") == 0)
37920 cp_lexer_consume_token (parser
->lexer
);
37921 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37925 cp_ensure_no_omp_declare_simd (parser
);
37926 if (strcmp (p
, "reduction") == 0)
37928 cp_lexer_consume_token (parser
->lexer
);
37929 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37933 if (!flag_openmp
) /* flag_openmp_simd */
37935 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37938 if (strcmp (p
, "target") == 0)
37940 cp_lexer_consume_token (parser
->lexer
);
37941 cp_parser_omp_declare_target (parser
, pragma_tok
);
37945 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37947 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37952 #pragma omp taskloop taskloop-clause[optseq] new-line
37955 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37958 #define OMP_TASKLOOP_CLAUSE_MASK \
37959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37975 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37976 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37979 tree clauses
, sb
, ret
;
37981 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37983 strcat (p_name
, " taskloop");
37984 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37988 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37989 const char *p
= IDENTIFIER_POINTER (id
);
37991 if (strcmp (p
, "simd") == 0)
37993 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37994 if (cclauses
== NULL
)
37995 cclauses
= cclauses_buf
;
37997 cp_lexer_consume_token (parser
->lexer
);
37998 if (!flag_openmp
) /* flag_openmp_simd */
37999 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38001 sb
= begin_omp_structured_block ();
38002 save
= cp_parser_begin_omp_structured_block (parser
);
38003 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38005 cp_parser_end_omp_structured_block (parser
, save
);
38006 tree body
= finish_omp_structured_block (sb
);
38009 ret
= make_node (OMP_TASKLOOP
);
38010 TREE_TYPE (ret
) = void_type_node
;
38011 OMP_FOR_BODY (ret
) = body
;
38012 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38013 SET_EXPR_LOCATION (ret
, loc
);
38018 if (!flag_openmp
) /* flag_openmp_simd */
38020 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38024 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38028 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38029 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38032 sb
= begin_omp_structured_block ();
38033 save
= cp_parser_begin_omp_structured_block (parser
);
38035 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38038 cp_parser_end_omp_structured_block (parser
, save
);
38039 add_stmt (finish_omp_structured_block (sb
));
38046 # pragma acc routine oacc-routine-clause[optseq] new-line
38047 function-definition
38049 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38052 #define OACC_ROUTINE_CLAUSE_MASK \
38053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38059 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38060 component, which must resolve to a declared namespace-scope
38061 function. The clauses are either processed directly (for a named
38062 function), or defered until the immediatley following declaration
38066 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38067 enum pragma_context context
)
38069 gcc_checking_assert (context
== pragma_external
);
38070 /* The checking for "another pragma following this one" in the "no optional
38071 '( name )'" case makes sure that we dont re-enter. */
38072 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38074 cp_oacc_routine_data data
;
38075 data
.error_seen
= false;
38076 data
.fndecl_seen
= false;
38077 data
.tokens
= vNULL
;
38078 data
.clauses
= NULL_TREE
;
38079 data
.loc
= pragma_tok
->location
;
38080 /* It is safe to take the address of a local variable; it will only be
38081 used while this scope is live. */
38082 parser
->oacc_routine
= &data
;
38084 /* Look for optional '( name )'. */
38085 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38087 matching_parens parens
;
38088 parens
.consume_open (parser
); /* '(' */
38090 /* We parse the name as an id-expression. If it resolves to
38091 anything other than a non-overloaded function at namespace
38092 scope, it's an error. */
38093 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38094 tree name
= cp_parser_id_expression (parser
,
38095 /*template_keyword_p=*/false,
38096 /*check_dependency_p=*/false,
38097 /*template_p=*/NULL
,
38098 /*declarator_p=*/false,
38099 /*optional_p=*/false);
38100 tree decl
= (identifier_p (name
)
38101 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38103 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38104 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38106 if (decl
== error_mark_node
38107 || !parens
.require_close (parser
))
38109 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38110 parser
->oacc_routine
= NULL
;
38115 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38116 "#pragma acc routine",
38117 cp_lexer_peek_token (parser
->lexer
));
38119 if (decl
&& is_overloaded_fn (decl
)
38120 && (TREE_CODE (decl
) != FUNCTION_DECL
38121 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38123 error_at (name_loc
,
38124 "%<#pragma acc routine%> names a set of overloads");
38125 parser
->oacc_routine
= NULL
;
38129 /* Perhaps we should use the same rule as declarations in different
38131 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38133 error_at (name_loc
,
38134 "%qD does not refer to a namespace scope function", decl
);
38135 parser
->oacc_routine
= NULL
;
38139 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38141 error_at (name_loc
, "%qD does not refer to a function", decl
);
38142 parser
->oacc_routine
= NULL
;
38146 cp_finalize_oacc_routine (parser
, decl
, false);
38147 parser
->oacc_routine
= NULL
;
38149 else /* No optional '( name )'. */
38151 /* Store away all pragma tokens. */
38152 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38153 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38154 cp_lexer_consume_token (parser
->lexer
);
38155 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38156 parser
->oacc_routine
->error_seen
= true;
38157 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38158 struct cp_token_cache
*cp
38159 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38160 parser
->oacc_routine
->tokens
.safe_push (cp
);
38162 /* Emit a helpful diagnostic if there's another pragma following this
38164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38166 cp_ensure_no_oacc_routine (parser
);
38167 data
.tokens
.release ();
38168 /* ..., and then just keep going. */
38172 /* We only have to consider the pragma_external case here. */
38173 cp_parser_declaration (parser
);
38174 if (parser
->oacc_routine
38175 && !parser
->oacc_routine
->fndecl_seen
)
38176 cp_ensure_no_oacc_routine (parser
);
38178 parser
->oacc_routine
= NULL
;
38179 data
.tokens
.release ();
38183 /* Finalize #pragma acc routine clauses after direct declarator has
38187 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38189 struct cp_token_cache
*ce
;
38190 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38192 if (!data
->error_seen
&& data
->fndecl_seen
)
38194 error_at (data
->loc
,
38195 "%<#pragma acc routine%> not immediately followed by "
38196 "a single function declaration or definition");
38197 data
->error_seen
= true;
38199 if (data
->error_seen
)
38202 gcc_checking_assert (data
->tokens
.length () == 1);
38203 ce
= data
->tokens
[0];
38205 cp_parser_push_lexer_for_tokens (parser
, ce
);
38206 parser
->lexer
->in_pragma
= true;
38207 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38209 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38210 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38211 parser
->oacc_routine
->clauses
38212 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38213 "#pragma acc routine", pragma_tok
);
38214 cp_parser_pop_lexer (parser
);
38215 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38221 /* Apply any saved OpenACC routine clauses to a just-parsed
38225 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38227 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38229 /* Keep going if we're in error reporting mode. */
38230 if (parser
->oacc_routine
->error_seen
38231 || fndecl
== error_mark_node
)
38234 if (parser
->oacc_routine
->fndecl_seen
)
38236 error_at (parser
->oacc_routine
->loc
,
38237 "%<#pragma acc routine%> not immediately followed by"
38238 " a single function declaration or definition");
38239 parser
->oacc_routine
= NULL
;
38242 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38244 cp_ensure_no_oacc_routine (parser
);
38248 if (oacc_get_fn_attrib (fndecl
))
38250 error_at (parser
->oacc_routine
->loc
,
38251 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38252 parser
->oacc_routine
= NULL
;
38256 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38258 error_at (parser
->oacc_routine
->loc
,
38260 ? G_("%<#pragma acc routine%> must be applied before use")
38261 : G_("%<#pragma acc routine%> must be applied before "
38263 parser
->oacc_routine
= NULL
;
38267 /* Process the routine's dimension clauses. */
38268 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38269 oacc_replace_fn_attrib (fndecl
, dims
);
38271 /* Add an "omp declare target" attribute. */
38272 DECL_ATTRIBUTES (fndecl
)
38273 = tree_cons (get_identifier ("omp declare target"),
38274 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38276 /* Don't unset parser->oacc_routine here: we may still need it to
38277 diagnose wrong usage. But, remember that we've used this "#pragma acc
38279 parser
->oacc_routine
->fndecl_seen
= true;
38283 /* Main entry point to OpenMP statement pragmas. */
38286 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38289 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38290 omp_clause_mask
mask (0);
38292 switch (cp_parser_pragma_kind (pragma_tok
))
38294 case PRAGMA_OACC_ATOMIC
:
38295 cp_parser_omp_atomic (parser
, pragma_tok
);
38297 case PRAGMA_OACC_CACHE
:
38298 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38300 case PRAGMA_OACC_DATA
:
38301 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38303 case PRAGMA_OACC_ENTER_DATA
:
38304 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38306 case PRAGMA_OACC_EXIT_DATA
:
38307 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38309 case PRAGMA_OACC_HOST_DATA
:
38310 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38312 case PRAGMA_OACC_KERNELS
:
38313 case PRAGMA_OACC_PARALLEL
:
38314 strcpy (p_name
, "#pragma acc");
38315 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38318 case PRAGMA_OACC_LOOP
:
38319 strcpy (p_name
, "#pragma acc");
38320 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38323 case PRAGMA_OACC_UPDATE
:
38324 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38326 case PRAGMA_OACC_WAIT
:
38327 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38329 case PRAGMA_OMP_ATOMIC
:
38330 cp_parser_omp_atomic (parser
, pragma_tok
);
38332 case PRAGMA_OMP_CRITICAL
:
38333 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38335 case PRAGMA_OMP_DISTRIBUTE
:
38336 strcpy (p_name
, "#pragma omp");
38337 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38340 case PRAGMA_OMP_FOR
:
38341 strcpy (p_name
, "#pragma omp");
38342 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38345 case PRAGMA_OMP_MASTER
:
38346 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38348 case PRAGMA_OMP_PARALLEL
:
38349 strcpy (p_name
, "#pragma omp");
38350 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38353 case PRAGMA_OMP_SECTIONS
:
38354 strcpy (p_name
, "#pragma omp");
38355 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38357 case PRAGMA_OMP_SIMD
:
38358 strcpy (p_name
, "#pragma omp");
38359 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38362 case PRAGMA_OMP_SINGLE
:
38363 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38365 case PRAGMA_OMP_TASK
:
38366 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38368 case PRAGMA_OMP_TASKGROUP
:
38369 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38371 case PRAGMA_OMP_TASKLOOP
:
38372 strcpy (p_name
, "#pragma omp");
38373 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38376 case PRAGMA_OMP_TEAMS
:
38377 strcpy (p_name
, "#pragma omp");
38378 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38382 gcc_unreachable ();
38385 protected_set_expr_location (stmt
, pragma_tok
->location
);
38388 /* Transactional Memory parsing routines. */
38390 /* Parse a transaction attribute.
38396 We use this instead of cp_parser_attributes_opt for transactions to avoid
38397 the pedwarn in C++98 mode. */
38400 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38403 tree attr_name
, attr
= NULL
;
38405 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38406 return cp_parser_attributes_opt (parser
);
38408 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38410 cp_lexer_consume_token (parser
->lexer
);
38411 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38414 token
= cp_lexer_peek_token (parser
->lexer
);
38415 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38417 token
= cp_lexer_consume_token (parser
->lexer
);
38419 attr_name
= (token
->type
== CPP_KEYWORD
38420 /* For keywords, use the canonical spelling,
38421 not the parsed identifier. */
38422 ? ridpointers
[(int) token
->keyword
]
38424 attr
= build_tree_list (attr_name
, NULL_TREE
);
38427 cp_parser_error (parser
, "expected identifier");
38429 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38431 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38435 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38437 transaction-statement:
38438 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38440 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38444 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38446 unsigned char old_in
= parser
->in_transaction
;
38447 unsigned char this_in
= 1, new_in
;
38448 enum rid keyword
= token
->keyword
;
38449 tree stmt
, attrs
, noex
;
38451 cp_lexer_consume_token (parser
->lexer
);
38453 if (keyword
== RID_TRANSACTION_RELAXED
38454 || keyword
== RID_SYNCHRONIZED
)
38455 this_in
|= TM_STMT_ATTR_RELAXED
;
38458 attrs
= cp_parser_txn_attribute_opt (parser
);
38460 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38463 /* Parse a noexcept specification. */
38464 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38465 noex
= boolean_true_node
;
38466 else if (keyword
== RID_ATOMIC_CANCEL
)
38468 /* cancel-and-throw is unimplemented. */
38469 sorry ("atomic_cancel");
38473 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38475 /* Keep track if we're in the lexical scope of an outer transaction. */
38476 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38478 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38480 parser
->in_transaction
= new_in
;
38481 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38482 parser
->in_transaction
= old_in
;
38484 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38489 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38491 transaction-expression:
38492 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38493 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38497 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38499 unsigned char old_in
= parser
->in_transaction
;
38500 unsigned char this_in
= 1;
38504 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38506 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38507 || keyword
== RID_TRANSACTION_RELAXED
);
38511 keyword
== RID_TRANSACTION_RELAXED
38512 ? G_("%<__transaction_relaxed%> without transactional memory "
38514 : G_("%<__transaction_atomic%> without transactional memory "
38515 "support enabled"));
38517 token
= cp_parser_require_keyword (parser
, keyword
,
38518 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38519 : RT_TRANSACTION_RELAXED
));
38520 gcc_assert (token
!= NULL
);
38522 if (keyword
== RID_TRANSACTION_RELAXED
)
38523 this_in
|= TM_STMT_ATTR_RELAXED
;
38525 /* Set this early. This might mean that we allow transaction_cancel in
38526 an expression that we find out later actually has to be a constexpr.
38527 However, we expect that cxx_constant_value will be able to deal with
38528 this; also, if the noexcept has no constexpr, then what we parse next
38529 really is a transaction's body. */
38530 parser
->in_transaction
= this_in
;
38532 /* Parse a noexcept specification. */
38533 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38536 if (!noex
|| !noex_expr
38537 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38539 matching_parens parens
;
38540 parens
.require_open (parser
);
38542 expr
= cp_parser_expression (parser
);
38543 expr
= finish_parenthesized_expr (expr
);
38545 parens
.require_close (parser
);
38549 /* The only expression that is available got parsed for the noexcept
38550 already. noexcept is true then. */
38552 noex
= boolean_true_node
;
38555 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38556 parser
->in_transaction
= old_in
;
38558 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38559 return error_mark_node
;
38561 return (flag_tm
? expr
: error_mark_node
);
38564 /* Parse a function-transaction-block.
38566 function-transaction-block:
38567 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38569 __transaction_atomic txn-attribute[opt] function-try-block
38570 __transaction_relaxed ctor-initializer[opt] function-body
38571 __transaction_relaxed function-try-block
38575 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38577 unsigned char old_in
= parser
->in_transaction
;
38578 unsigned char new_in
= 1;
38579 tree compound_stmt
, stmt
, attrs
;
38582 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38583 || keyword
== RID_TRANSACTION_RELAXED
);
38584 token
= cp_parser_require_keyword (parser
, keyword
,
38585 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38586 : RT_TRANSACTION_RELAXED
));
38587 gcc_assert (token
!= NULL
);
38589 if (keyword
== RID_TRANSACTION_RELAXED
)
38590 new_in
|= TM_STMT_ATTR_RELAXED
;
38593 attrs
= cp_parser_txn_attribute_opt (parser
);
38595 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38598 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38600 parser
->in_transaction
= new_in
;
38602 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38603 cp_parser_function_try_block (parser
);
38605 cp_parser_ctor_initializer_opt_and_function_body
38606 (parser
, /*in_function_try_block=*/false);
38608 parser
->in_transaction
= old_in
;
38610 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38613 /* Parse a __transaction_cancel statement.
38616 __transaction_cancel txn-attribute[opt] ;
38617 __transaction_cancel txn-attribute[opt] throw-expression ;
38619 ??? Cancel and throw is not yet implemented. */
38622 cp_parser_transaction_cancel (cp_parser
*parser
)
38625 bool is_outer
= false;
38628 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38629 RT_TRANSACTION_CANCEL
);
38630 gcc_assert (token
!= NULL
);
38632 attrs
= cp_parser_txn_attribute_opt (parser
);
38634 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38636 /* ??? Parse cancel-and-throw here. */
38638 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38642 error_at (token
->location
, "%<__transaction_cancel%> without "
38643 "transactional memory support enabled");
38644 return error_mark_node
;
38646 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38648 error_at (token
->location
, "%<__transaction_cancel%> within a "
38649 "%<__transaction_relaxed%>");
38650 return error_mark_node
;
38654 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38655 && !is_tm_may_cancel_outer (current_function_decl
))
38657 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38658 "within outer %<__transaction_atomic%>");
38659 error_at (token
->location
,
38660 " or a %<transaction_may_cancel_outer%> function");
38661 return error_mark_node
;
38664 else if (parser
->in_transaction
== 0)
38666 error_at (token
->location
, "%<__transaction_cancel%> not within "
38667 "%<__transaction_atomic%>");
38668 return error_mark_node
;
38671 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38679 static GTY (()) cp_parser
*the_parser
;
38682 /* Special handling for the first token or line in the file. The first
38683 thing in the file might be #pragma GCC pch_preprocess, which loads a
38684 PCH file, which is a GC collection point. So we need to handle this
38685 first pragma without benefit of an existing lexer structure.
38687 Always returns one token to the caller in *FIRST_TOKEN. This is
38688 either the true first token of the file, or the first token after
38689 the initial pragma. */
38692 cp_parser_initial_pragma (cp_token
*first_token
)
38696 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38697 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38700 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38701 if (first_token
->type
== CPP_STRING
)
38703 name
= first_token
->u
.value
;
38705 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38706 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38707 error_at (first_token
->location
,
38708 "junk at end of %<#pragma GCC pch_preprocess%>");
38711 error_at (first_token
->location
, "expected string literal");
38713 /* Skip to the end of the pragma. */
38714 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38715 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38717 /* Now actually load the PCH file. */
38719 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38721 /* Read one more token to return to our caller. We have to do this
38722 after reading the PCH file in, since its pointers have to be
38724 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38727 /* Parse a pragma GCC ivdep. */
38730 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38732 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38736 /* Parse a pragma GCC unroll. */
38738 static unsigned short
38739 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38741 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38742 tree expr
= cp_parser_constant_expression (parser
);
38743 unsigned short unroll
;
38744 expr
= maybe_constant_value (expr
);
38745 HOST_WIDE_INT lunroll
= 0;
38746 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38747 || TREE_CODE (expr
) != INTEGER_CST
38748 || (lunroll
= tree_to_shwi (expr
)) < 0
38749 || lunroll
>= USHRT_MAX
)
38751 error_at (location
, "%<#pragma GCC unroll%> requires an"
38752 " assignment-expression that evaluates to a non-negative"
38753 " integral constant less than %u", USHRT_MAX
);
38758 unroll
= (unsigned short)lunroll
;
38762 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38766 /* Normal parsing of a pragma token. Here we can (and must) use the
38770 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38772 cp_token
*pragma_tok
;
38777 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38778 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38779 parser
->lexer
->in_pragma
= true;
38781 id
= cp_parser_pragma_kind (pragma_tok
);
38782 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38783 cp_ensure_no_omp_declare_simd (parser
);
38786 case PRAGMA_GCC_PCH_PREPROCESS
:
38787 error_at (pragma_tok
->location
,
38788 "%<#pragma GCC pch_preprocess%> must be first");
38791 case PRAGMA_OMP_BARRIER
:
38794 case pragma_compound
:
38795 cp_parser_omp_barrier (parser
, pragma_tok
);
38798 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38799 "used in compound statements", "omp barrier");
38806 case PRAGMA_OMP_FLUSH
:
38809 case pragma_compound
:
38810 cp_parser_omp_flush (parser
, pragma_tok
);
38813 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38814 "used in compound statements", "omp flush");
38821 case PRAGMA_OMP_TASKWAIT
:
38824 case pragma_compound
:
38825 cp_parser_omp_taskwait (parser
, pragma_tok
);
38828 error_at (pragma_tok
->location
,
38829 "%<#pragma %s%> may only be used in compound statements",
38837 case PRAGMA_OMP_TASKYIELD
:
38840 case pragma_compound
:
38841 cp_parser_omp_taskyield (parser
, pragma_tok
);
38844 error_at (pragma_tok
->location
,
38845 "%<#pragma %s%> may only be used in compound statements",
38853 case PRAGMA_OMP_CANCEL
:
38856 case pragma_compound
:
38857 cp_parser_omp_cancel (parser
, pragma_tok
);
38860 error_at (pragma_tok
->location
,
38861 "%<#pragma %s%> may only be used in compound statements",
38869 case PRAGMA_OMP_CANCELLATION_POINT
:
38870 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38873 case PRAGMA_OMP_THREADPRIVATE
:
38874 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38877 case PRAGMA_OMP_DECLARE
:
38878 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38880 case PRAGMA_OACC_DECLARE
:
38881 cp_parser_oacc_declare (parser
, pragma_tok
);
38884 case PRAGMA_OACC_ENTER_DATA
:
38885 if (context
== pragma_stmt
)
38887 error_at (pragma_tok
->location
,
38888 "%<#pragma %s%> may only be used in compound statements",
38892 else if (context
!= pragma_compound
)
38894 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38897 case PRAGMA_OACC_EXIT_DATA
:
38898 if (context
== pragma_stmt
)
38900 error_at (pragma_tok
->location
,
38901 "%<#pragma %s%> may only be used in compound statements",
38905 else if (context
!= pragma_compound
)
38907 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38910 case PRAGMA_OACC_ROUTINE
:
38911 if (context
!= pragma_external
)
38913 error_at (pragma_tok
->location
,
38914 "%<#pragma acc routine%> must be at file scope");
38917 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38920 case PRAGMA_OACC_UPDATE
:
38921 if (context
== pragma_stmt
)
38923 error_at (pragma_tok
->location
,
38924 "%<#pragma %s%> may only be used in compound statements",
38928 else if (context
!= pragma_compound
)
38930 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38933 case PRAGMA_OACC_WAIT
:
38934 if (context
== pragma_stmt
)
38936 error_at (pragma_tok
->location
,
38937 "%<#pragma %s%> may only be used in compound statements",
38941 else if (context
!= pragma_compound
)
38943 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38946 case PRAGMA_OACC_ATOMIC
:
38947 case PRAGMA_OACC_CACHE
:
38948 case PRAGMA_OACC_DATA
:
38949 case PRAGMA_OACC_HOST_DATA
:
38950 case PRAGMA_OACC_KERNELS
:
38951 case PRAGMA_OACC_PARALLEL
:
38952 case PRAGMA_OACC_LOOP
:
38953 case PRAGMA_OMP_ATOMIC
:
38954 case PRAGMA_OMP_CRITICAL
:
38955 case PRAGMA_OMP_DISTRIBUTE
:
38956 case PRAGMA_OMP_FOR
:
38957 case PRAGMA_OMP_MASTER
:
38958 case PRAGMA_OMP_PARALLEL
:
38959 case PRAGMA_OMP_SECTIONS
:
38960 case PRAGMA_OMP_SIMD
:
38961 case PRAGMA_OMP_SINGLE
:
38962 case PRAGMA_OMP_TASK
:
38963 case PRAGMA_OMP_TASKGROUP
:
38964 case PRAGMA_OMP_TASKLOOP
:
38965 case PRAGMA_OMP_TEAMS
:
38966 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38968 stmt
= push_omp_privatization_clauses (false);
38969 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38970 pop_omp_privatization_clauses (stmt
);
38973 case PRAGMA_OMP_ORDERED
:
38974 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38976 stmt
= push_omp_privatization_clauses (false);
38977 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38978 pop_omp_privatization_clauses (stmt
);
38981 case PRAGMA_OMP_TARGET
:
38982 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38984 stmt
= push_omp_privatization_clauses (false);
38985 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38986 pop_omp_privatization_clauses (stmt
);
38989 case PRAGMA_OMP_END_DECLARE_TARGET
:
38990 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38993 case PRAGMA_OMP_SECTION
:
38994 error_at (pragma_tok
->location
,
38995 "%<#pragma omp section%> may only be used in "
38996 "%<#pragma omp sections%> construct");
39001 if (context
== pragma_external
)
39003 error_at (pragma_tok
->location
,
39004 "%<#pragma GCC ivdep%> must be inside a function");
39007 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
39008 unsigned short unroll
;
39009 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39010 if (tok
->type
== CPP_PRAGMA
39011 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
39013 tok
= cp_lexer_consume_token (parser
->lexer
);
39014 unroll
= cp_parser_pragma_unroll (parser
, tok
);
39015 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39019 if (tok
->type
!= CPP_KEYWORD
39020 || (tok
->keyword
!= RID_FOR
39021 && tok
->keyword
!= RID_WHILE
39022 && tok
->keyword
!= RID_DO
))
39024 cp_parser_error (parser
, "for, while or do statement expected");
39027 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39031 case PRAGMA_UNROLL
:
39033 if (context
== pragma_external
)
39035 error_at (pragma_tok
->location
,
39036 "%<#pragma GCC unroll%> must be inside a function");
39039 const unsigned short unroll
39040 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39042 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39043 if (tok
->type
== CPP_PRAGMA
39044 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39046 tok
= cp_lexer_consume_token (parser
->lexer
);
39047 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39048 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39052 if (tok
->type
!= CPP_KEYWORD
39053 || (tok
->keyword
!= RID_FOR
39054 && tok
->keyword
!= RID_WHILE
39055 && tok
->keyword
!= RID_DO
))
39057 cp_parser_error (parser
, "for, while or do statement expected");
39060 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39065 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39066 c_invoke_pragma_handler (id
);
39070 cp_parser_error (parser
, "expected declaration specifiers");
39074 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39078 /* The interface the pragma parsers have to the lexer. */
39081 pragma_lex (tree
*value
, location_t
*loc
)
39083 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39084 enum cpp_ttype ret
= tok
->type
;
39086 *value
= tok
->u
.value
;
39088 *loc
= tok
->location
;
39090 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39092 else if (ret
== CPP_STRING
)
39093 *value
= cp_parser_string_literal (the_parser
, false, false);
39096 if (ret
== CPP_KEYWORD
)
39098 cp_lexer_consume_token (the_parser
->lexer
);
39105 /* External interface. */
39107 /* Parse one entire translation unit. */
39110 c_parse_file (void)
39112 static bool already_called
= false;
39114 if (already_called
)
39115 fatal_error (input_location
,
39116 "inter-module optimizations not implemented for C++");
39117 already_called
= true;
39119 the_parser
= cp_parser_new ();
39120 push_deferring_access_checks (flag_access_control
39121 ? dk_no_deferred
: dk_no_check
);
39122 cp_parser_translation_unit (the_parser
);
39126 /* Create an identifier for a generic parameter type (a synthesized
39127 template parameter implied by `auto' or a concept identifier). */
39129 static GTY(()) int generic_parm_count
;
39131 make_generic_type_name ()
39134 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39135 return get_identifier (buf
);
39138 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39139 (creating a new template parameter list if necessary). Returns the newly
39140 created template type parm. */
39143 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39145 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39147 /* Before committing to modifying any scope, if we're in an
39148 implicit template scope, and we're trying to synthesize a
39149 constrained parameter, try to find a previous parameter with
39150 the same name. This is the same-type rule for abbreviated
39151 function templates.
39153 NOTE: We can generate implicit parameters when tentatively
39154 parsing a nested name specifier, only to reject that parse
39155 later. However, matching the same template-id as part of a
39156 direct-declarator should generate an identical template
39157 parameter, so this rule will merge them. */
39158 if (parser
->implicit_template_scope
&& constr
)
39160 tree t
= parser
->implicit_template_parms
;
39163 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39165 tree d
= TREE_VALUE (t
);
39166 if (TREE_CODE (d
) == PARM_DECL
)
39167 /* Return the TEMPLATE_PARM_INDEX. */
39168 d
= DECL_INITIAL (d
);
39171 t
= TREE_CHAIN (t
);
39175 /* We are either continuing a function template that already contains implicit
39176 template parameters, creating a new fully-implicit function template, or
39177 extending an existing explicit function template with implicit template
39180 cp_binding_level
*const entry_scope
= current_binding_level
;
39182 bool become_template
= false;
39183 cp_binding_level
*parent_scope
= 0;
39185 if (parser
->implicit_template_scope
)
39187 gcc_assert (parser
->implicit_template_parms
);
39189 current_binding_level
= parser
->implicit_template_scope
;
39193 /* Roll back to the existing template parameter scope (in the case of
39194 extending an explicit function template) or introduce a new template
39195 parameter scope ahead of the function parameter scope (or class scope
39196 in the case of out-of-line member definitions). The function scope is
39197 added back after template parameter synthesis below. */
39199 cp_binding_level
*scope
= entry_scope
;
39201 while (scope
->kind
== sk_function_parms
)
39203 parent_scope
= scope
;
39204 scope
= scope
->level_chain
;
39206 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39208 /* If not defining a class, then any class scope is a scope level in
39209 an out-of-line member definition. In this case simply wind back
39210 beyond the first such scope to inject the template parameter list.
39211 Otherwise wind back to the class being defined. The latter can
39212 occur in class member friend declarations such as:
39218 friend void A::foo (auto);
39221 The template parameter list synthesized for the friend declaration
39222 must be injected in the scope of 'B'. This can also occur in
39223 erroneous cases such as:
39229 void B::foo (auto) {}
39232 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39233 but, nevertheless, the template parameter list synthesized for the
39234 declarator should be injected into the scope of 'A' as if the
39235 ill-formed template was specified explicitly. */
39237 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39239 parent_scope
= scope
;
39240 scope
= scope
->level_chain
;
39244 current_binding_level
= scope
;
39246 if (scope
->kind
!= sk_template_parms
39247 || !function_being_declared_is_template_p (parser
))
39249 /* Introduce a new template parameter list for implicit template
39252 become_template
= true;
39254 parser
->implicit_template_scope
39255 = begin_scope (sk_template_parms
, NULL
);
39257 ++processing_template_decl
;
39259 parser
->fully_implicit_function_template_p
= true;
39260 ++parser
->num_template_parameter_lists
;
39264 /* Synthesize implicit template parameters at the end of the explicit
39265 template parameter list. */
39267 gcc_assert (current_template_parms
);
39269 parser
->implicit_template_scope
= scope
;
39271 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39272 parser
->implicit_template_parms
39273 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39277 /* Synthesize a new template parameter and track the current template
39278 parameter chain with implicit_template_parms. */
39280 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39281 tree synth_id
= make_generic_type_name ();
39282 tree synth_tmpl_parm
;
39283 bool non_type
= false;
39285 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39287 = finish_template_type_parm (class_type_node
, synth_id
);
39288 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39290 = finish_constrained_template_template_parm (proto
, synth_id
);
39293 synth_tmpl_parm
= copy_decl (proto
);
39294 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39298 // Attach the constraint to the parm before processing.
39299 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39300 TREE_TYPE (node
) = constr
;
39302 = process_template_parm (parser
->implicit_template_parms
,
39305 /*non_type=*/non_type
,
39306 /*param_pack=*/false);
39308 // Chain the new parameter to the list of implicit parameters.
39309 if (parser
->implicit_template_parms
)
39310 parser
->implicit_template_parms
39311 = TREE_CHAIN (parser
->implicit_template_parms
);
39313 parser
->implicit_template_parms
= new_parm
;
39315 tree new_decl
= get_local_decls ();
39317 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39318 new_decl
= DECL_INITIAL (new_decl
);
39320 /* If creating a fully implicit function template, start the new implicit
39321 template parameter list with this synthesized type, otherwise grow the
39322 current template parameter list. */
39324 if (become_template
)
39326 parent_scope
->level_chain
= current_binding_level
;
39328 tree new_parms
= make_tree_vec (1);
39329 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39330 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39331 new_parms
, current_template_parms
);
39335 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39336 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39337 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39338 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39341 // If the new parameter was constrained, we need to add that to the
39342 // constraints in the template parameter list.
39343 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39345 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39346 reqs
= conjoin_constraints (reqs
, req
);
39347 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39350 current_binding_level
= entry_scope
;
39355 /* Finish the declaration of a fully implicit function template. Such a
39356 template has no explicit template parameter list so has not been through the
39357 normal template head and tail processing. synthesize_implicit_template_parm
39358 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39359 provided if the declaration is a class member such that its template
39360 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39361 form is returned. Otherwise NULL_TREE is returned. */
39364 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39366 gcc_assert (parser
->fully_implicit_function_template_p
);
39368 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39369 && DECL_VIRTUAL_P (member_decl_opt
))
39371 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39372 "implicit templates may not be %<virtual%>");
39373 DECL_VIRTUAL_P (member_decl_opt
) = false;
39376 if (member_decl_opt
)
39377 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39378 end_template_decl ();
39380 parser
->fully_implicit_function_template_p
= false;
39381 parser
->implicit_template_parms
= 0;
39382 parser
->implicit_template_scope
= 0;
39383 --parser
->num_template_parameter_lists
;
39385 return member_decl_opt
;
39388 /* Like finish_fully_implicit_template, but to be used in error
39389 recovery, rearranging scopes so that we restore the state we had
39390 before synthesize_implicit_template_parm inserted the implement
39391 template parms scope. */
39394 abort_fully_implicit_template (cp_parser
*parser
)
39396 cp_binding_level
*return_to_scope
= current_binding_level
;
39398 if (parser
->implicit_template_scope
39399 && return_to_scope
!= parser
->implicit_template_scope
)
39401 cp_binding_level
*child
= return_to_scope
;
39402 for (cp_binding_level
*scope
= child
->level_chain
;
39403 scope
!= parser
->implicit_template_scope
;
39404 scope
= child
->level_chain
)
39406 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39407 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39408 current_binding_level
= parser
->implicit_template_scope
;
39411 return_to_scope
= return_to_scope
->level_chain
;
39413 finish_fully_implicit_template (parser
, NULL
);
39415 gcc_assert (current_binding_level
== return_to_scope
);
39418 /* Helper function for diagnostics that have complained about things
39419 being used with 'extern "C"' linkage.
39421 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39424 maybe_show_extern_c_location (void)
39426 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39427 inform (the_parser
->innermost_linkage_specification_location
,
39428 "%<extern \"C\"%> linkage started here");
39431 #include "gt-cp-parser.h"