2 Copyright (C) 2000-2017 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
);
2051 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2053 static void cp_parser_pseudo_destructor_name
2054 (cp_parser
*, tree
, tree
*, tree
*);
2055 static cp_expr cp_parser_unary_expression
2056 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2059 static tree cp_parser_new_expression
2061 static vec
<tree
, va_gc
> *cp_parser_new_placement
2063 static tree cp_parser_new_type_id
2064 (cp_parser
*, tree
*);
2065 static cp_declarator
*cp_parser_new_declarator_opt
2067 static cp_declarator
*cp_parser_direct_new_declarator
2069 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2071 static tree cp_parser_delete_expression
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser
*, cp_expr
);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2083 static cp_expr cp_parser_expression
2084 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2089 static cp_expr cp_parser_lambda_expression
2091 static void cp_parser_lambda_introducer
2092 (cp_parser
*, tree
);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser
*, tree
);
2095 static void cp_parser_lambda_body
2096 (cp_parser
*, tree
);
2098 /* Statements [gram.stmt.stmt] */
2100 static void cp_parser_statement
2101 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser
*, tree
);
2104 static tree cp_parser_expression_statement
2105 (cp_parser
*, tree
);
2106 static tree cp_parser_compound_statement
2107 (cp_parser
*, tree
, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser
*, tree
);
2110 static tree cp_parser_selection_statement
2111 (cp_parser
*, bool *, vec
<tree
> *);
2112 static tree cp_parser_condition
2114 static tree cp_parser_iteration_statement
2115 (cp_parser
*, bool *, bool);
2116 static bool cp_parser_init_statement
2117 (cp_parser
*, tree
*decl
);
2118 static tree cp_parser_for
2119 (cp_parser
*, bool);
2120 static tree cp_parser_c_for
2121 (cp_parser
*, tree
, tree
, bool);
2122 static tree cp_parser_range_for
2123 (cp_parser
*, tree
, tree
, tree
, bool);
2124 static void do_range_for_auto_deduction
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree
, tree
*, tree
*);
2128 static tree cp_parser_range_for_member_function
2130 static tree cp_parser_jump_statement
2132 static void cp_parser_declaration_statement
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser
*, bool *, const token_indent_info
&);
2140 /* Declarations [gram.dcl.dcl] */
2142 static void cp_parser_declaration_seq_opt
2144 static void cp_parser_declaration
2146 static void cp_parser_block_declaration
2147 (cp_parser
*, bool);
2148 static void cp_parser_simple_declaration
2149 (cp_parser
*, bool, tree
*);
2150 static void cp_parser_decl_specifier_seq
2151 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2152 static tree cp_parser_storage_class_specifier_opt
2154 static tree cp_parser_function_specifier_opt
2155 (cp_parser
*, cp_decl_specifier_seq
*);
2156 static tree cp_parser_type_specifier
2157 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2159 static tree cp_parser_simple_type_specifier
2160 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2161 static tree cp_parser_type_name
2162 (cp_parser
*, bool);
2163 static tree cp_parser_type_name
2165 static tree cp_parser_nonclass_name
2166 (cp_parser
* parser
);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser
*, bool, bool);
2169 static tree cp_parser_enum_specifier
2171 static void cp_parser_enumerator_list
2172 (cp_parser
*, tree
);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser
*, tree
);
2175 static tree cp_parser_namespace_name
2177 static void cp_parser_namespace_definition
2179 static void cp_parser_namespace_body
2181 static tree cp_parser_qualified_namespace_specifier
2183 static void cp_parser_namespace_alias_definition
2185 static bool cp_parser_using_declaration
2186 (cp_parser
*, bool);
2187 static void cp_parser_using_directive
2189 static tree cp_parser_alias_declaration
2191 static void cp_parser_asm_definition
2193 static void cp_parser_linkage_specification
2195 static void cp_parser_static_assert
2196 (cp_parser
*, bool);
2197 static tree cp_parser_decltype
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2202 /* Declarators [gram.dcl.decl] */
2204 static tree cp_parser_init_declarator
2205 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2206 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2207 static cp_declarator
*cp_parser_declarator
2208 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2209 static cp_declarator
*cp_parser_direct_declarator
2210 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2211 static enum tree_code cp_parser_ptr_operator
2212 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2213 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 static tree cp_parser_tx_qualifier_opt
2221 static tree cp_parser_late_return_type_opt
2222 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2223 static tree cp_parser_declarator_id
2224 (cp_parser
*, bool);
2225 static tree cp_parser_type_id
2227 static tree cp_parser_template_type_arg
2229 static tree
cp_parser_trailing_type_id (cp_parser
*);
2230 static tree cp_parser_type_id_1
2231 (cp_parser
*, bool, bool);
2232 static void cp_parser_type_specifier_seq
2233 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2234 static tree cp_parser_parameter_declaration_clause
2236 static tree cp_parser_parameter_declaration_list
2237 (cp_parser
*, bool *);
2238 static cp_parameter_declarator
*cp_parser_parameter_declaration
2239 (cp_parser
*, bool, bool *);
2240 static tree cp_parser_default_argument
2241 (cp_parser
*, bool);
2242 static void cp_parser_function_body
2243 (cp_parser
*, bool);
2244 static tree cp_parser_initializer
2245 (cp_parser
*, bool *, bool *);
2246 static cp_expr cp_parser_initializer_clause
2247 (cp_parser
*, bool *);
2248 static cp_expr cp_parser_braced_list
2249 (cp_parser
*, bool*);
2250 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2251 (cp_parser
*, bool *);
2253 static void cp_parser_ctor_initializer_opt_and_function_body
2254 (cp_parser
*, bool);
2256 static tree cp_parser_late_parsing_omp_declare_simd
2257 (cp_parser
*, tree
);
2259 static tree cp_parser_late_parsing_oacc_routine
2260 (cp_parser
*, tree
);
2262 static tree synthesize_implicit_template_parm
2263 (cp_parser
*, tree
);
2264 static tree finish_fully_implicit_template
2265 (cp_parser
*, tree
);
2267 /* Classes [gram.class] */
2269 static tree cp_parser_class_name
2270 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2271 static tree cp_parser_class_specifier
2273 static tree cp_parser_class_head
2274 (cp_parser
*, bool *);
2275 static enum tag_types cp_parser_class_key
2277 static void cp_parser_type_parameter_key
2278 (cp_parser
* parser
);
2279 static void cp_parser_member_specification_opt
2281 static void cp_parser_member_declaration
2283 static tree cp_parser_pure_specifier
2285 static tree cp_parser_constant_initializer
2288 /* Derived classes [gram.class.derived] */
2290 static tree cp_parser_base_clause
2292 static tree cp_parser_base_specifier
2295 /* Special member functions [gram.special] */
2297 static tree cp_parser_conversion_function_id
2299 static tree cp_parser_conversion_type_id
2301 static cp_declarator
*cp_parser_conversion_declarator_opt
2303 static void cp_parser_ctor_initializer_opt
2305 static void cp_parser_mem_initializer_list
2307 static tree cp_parser_mem_initializer
2309 static tree cp_parser_mem_initializer_id
2312 /* Overloading [gram.over] */
2314 static cp_expr cp_parser_operator_function_id
2316 static cp_expr cp_parser_operator
2319 /* Templates [gram.temp] */
2321 static void cp_parser_template_declaration
2322 (cp_parser
*, bool);
2323 static tree cp_parser_template_parameter_list
2325 static tree cp_parser_template_parameter
2326 (cp_parser
*, bool *, bool *);
2327 static tree cp_parser_type_parameter
2328 (cp_parser
*, bool *);
2329 static tree cp_parser_template_id
2330 (cp_parser
*, bool, bool, enum tag_types
, bool);
2331 static tree cp_parser_template_name
2332 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2333 static tree cp_parser_template_argument_list
2335 static tree cp_parser_template_argument
2337 static void cp_parser_explicit_instantiation
2339 static void cp_parser_explicit_specialization
2342 /* Exception handling [gram.exception] */
2344 static tree cp_parser_try_block
2346 static void cp_parser_function_try_block
2348 static void cp_parser_handler_seq
2350 static void cp_parser_handler
2352 static tree cp_parser_exception_declaration
2354 static tree cp_parser_throw_expression
2356 static tree cp_parser_exception_specification_opt
2358 static tree cp_parser_type_id_list
2361 /* GNU Extensions */
2363 static tree cp_parser_asm_specification_opt
2365 static tree cp_parser_asm_operand_list
2367 static tree cp_parser_asm_clobber_list
2369 static tree cp_parser_asm_label_list
2371 static bool cp_next_tokens_can_be_attribute_p
2373 static bool cp_next_tokens_can_be_gnu_attribute_p
2375 static bool cp_next_tokens_can_be_std_attribute_p
2377 static bool cp_nth_tokens_can_be_std_attribute_p
2378 (cp_parser
*, size_t);
2379 static bool cp_nth_tokens_can_be_gnu_attribute_p
2380 (cp_parser
*, size_t);
2381 static bool cp_nth_tokens_can_be_attribute_p
2382 (cp_parser
*, size_t);
2383 static tree cp_parser_attributes_opt
2385 static tree cp_parser_gnu_attributes_opt
2387 static tree cp_parser_gnu_attribute_list
2389 static tree cp_parser_std_attribute
2390 (cp_parser
*, tree
);
2391 static tree cp_parser_std_attribute_spec
2393 static tree cp_parser_std_attribute_spec_seq
2395 static bool cp_parser_extension_opt
2396 (cp_parser
*, int *);
2397 static void cp_parser_label_declaration
2400 /* Concept Extensions */
2402 static tree cp_parser_requires_clause
2404 static tree cp_parser_requires_clause_opt
2406 static tree cp_parser_requires_expression
2408 static tree cp_parser_requirement_parameter_list
2410 static tree cp_parser_requirement_body
2412 static tree cp_parser_requirement_list
2414 static tree cp_parser_requirement
2416 static tree cp_parser_simple_requirement
2418 static tree cp_parser_compound_requirement
2420 static tree cp_parser_type_requirement
2422 static tree cp_parser_nested_requirement
2425 /* Transactional Memory Extensions */
2427 static tree cp_parser_transaction
2428 (cp_parser
*, cp_token
*);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser
*, enum rid
);
2431 static void cp_parser_function_transaction
2432 (cp_parser
*, enum rid
);
2433 static tree cp_parser_transaction_cancel
2436 enum pragma_context
{
2443 static bool cp_parser_pragma
2444 (cp_parser
*, enum pragma_context
, bool *);
2446 /* Objective-C++ Productions */
2448 static tree cp_parser_objc_message_receiver
2450 static tree cp_parser_objc_message_args
2452 static tree cp_parser_objc_message_expression
2454 static cp_expr cp_parser_objc_encode_expression
2456 static tree cp_parser_objc_defs_expression
2458 static tree cp_parser_objc_protocol_expression
2460 static tree cp_parser_objc_selector_expression
2462 static cp_expr cp_parser_objc_expression
2464 static bool cp_parser_objc_selector_p
2466 static tree cp_parser_objc_selector
2468 static tree cp_parser_objc_protocol_refs_opt
2470 static void cp_parser_objc_declaration
2471 (cp_parser
*, tree
);
2472 static tree cp_parser_objc_statement
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser
*, tree
*);
2476 static void cp_parser_objc_at_property_declaration
2478 static void cp_parser_objc_at_synthesize_declaration
2480 static void cp_parser_objc_at_dynamic_declaration
2482 static tree cp_parser_objc_struct_declaration
2485 /* Utility Routines */
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser
*, tree
, location_t
);
2491 static tree cp_parser_maybe_treat_template_as_class
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser
*, cp_declarator
*, location_t
);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2497 static cp_expr cp_parser_simple_cast_expression
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser
*, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser
*, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser
*, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser
*, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec
<deferred_access_check
, va_gc
> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser
*, tree
);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2517 static tree cp_parser_save_nsdmi
2519 static tree cp_parser_enclosed_template_argument_list
2521 static void cp_parser_save_default_args
2522 (cp_parser
*, tree
);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser
*, tree
);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser
*, tree
, tree
, tree
);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser
*, tree
);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser
*, tree
);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser
*, enum rid
);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser
*, enum rid
);
2535 static bool cp_parser_declares_only_class_p
2537 static void cp_parser_set_storage_class
2538 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq
*decl_specs
,
2543 cp_decl_spec ds
, cp_token
*);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq
*);
2546 static void cp_parser_required_error
2547 (cp_parser
*, required_token
, bool, location_t
);
2548 static cp_token
*cp_parser_require
2549 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2550 static cp_token
*cp_parser_require_keyword
2551 (cp_parser
*, enum rid
, required_token
);
2552 static bool cp_parser_token_starts_function_definition_p
2554 static bool cp_parser_next_token_starts_class_definition_p
2556 static bool cp_parser_next_token_ends_template_argument_p
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser
*, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2564 static void cp_parser_check_class_key
2565 (enum tag_types
, tree type
);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type
, location_t location
);
2568 static bool cp_parser_optional_template_keyword
2570 static void cp_parser_pre_parsed_nested_name_specifier
2572 static bool cp_parser_cache_group
2573 (cp_parser
*, enum cpp_ttype
, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser
*parser
, bool nsdmi
);
2576 static void cp_parser_parse_tentatively
2578 static void cp_parser_commit_to_tentative_parse
2580 static void cp_parser_commit_to_topmost_tentative_parse
2582 static void cp_parser_abort_tentative_parse
2584 static bool cp_parser_parse_definitely
2586 static inline bool cp_parser_parsing_tentatively
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2590 static void cp_parser_error
2591 (cp_parser
*, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2594 static bool cp_parser_simulate_error
2596 static bool cp_parser_check_type_definition
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator
*, tree
, location_t type_location
);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser
*, non_integral_constant
);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser
*, tree
, location_t
);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser
*, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2614 static void cp_parser_skip_to_end_of_block_or_statement
2616 static bool cp_parser_skip_to_closing_brace
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser
*, cp_token
*);
2622 static bool cp_parser_error_occurred
2624 static bool cp_parser_allow_gnu_extensions_p
2626 static bool cp_parser_is_pure_string_literal
2628 static bool cp_parser_is_string_literal
2630 static bool cp_parser_is_keyword
2631 (cp_token
*, enum rid
);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser
*, tree
, location_t location
);
2634 static cp_declarator
* cp_parser_make_indirect_declarator
2635 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2636 static bool cp_parser_compound_literal_p
2638 static bool cp_parser_array_designator_p
2640 static bool cp_parser_init_statement_p
2642 static bool cp_parser_skip_to_closing_square_bracket
2645 /* Concept-related syntactic transformations */
2647 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2648 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2656 ++cp_unevaluated_operand
;
2657 ++c_inhibit_evaluation_warnings
;
2660 cp_unevaluated::~cp_unevaluated ()
2662 --c_inhibit_evaluation_warnings
;
2663 --cp_unevaluated_operand
;
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2669 /* Returns nonzero if we are parsing tentatively. */
2672 cp_parser_parsing_tentatively (cp_parser
* parser
)
2674 return parser
->context
->next
!= NULL
;
2677 /* Returns nonzero if TOKEN is a string literal. */
2680 cp_parser_is_pure_string_literal (cp_token
* token
)
2682 return (token
->type
== CPP_STRING
||
2683 token
->type
== CPP_STRING16
||
2684 token
->type
== CPP_STRING32
||
2685 token
->type
== CPP_WSTRING
||
2686 token
->type
== CPP_UTF8STRING
);
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2693 cp_parser_is_string_literal (cp_token
* token
)
2695 return (cp_parser_is_pure_string_literal (token
) ||
2696 token
->type
== CPP_STRING_USERDEF
||
2697 token
->type
== CPP_STRING16_USERDEF
||
2698 token
->type
== CPP_STRING32_USERDEF
||
2699 token
->type
== CPP_WSTRING_USERDEF
||
2700 token
->type
== CPP_UTF8STRING_USERDEF
);
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2706 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2708 return token
->keyword
== keyword
;
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token
*token
)
2717 if (token
->type
!= CPP_PRAGMA
)
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2735 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2736 location_t
*out_loc
)
2738 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2739 if (token2
->type
!= tok1_kind
)
2741 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2742 if (token3
->type
!= tok1_kind
)
2744 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2745 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2748 /* It must be at the start of the line. */
2749 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2750 if (LOCATION_COLUMN (start_loc
) != 1)
2753 /* We have a conflict marker. Construct a location of the form:
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc
= get_finish (token4
->location
);
2758 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2767 get_matching_symbol (required_token token_desc
)
2774 case RT_CLOSE_BRACE
:
2776 case RT_CLOSE_PAREN
:
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc
)
2790 return CPP_SEMICOLON
;
2792 return CPP_OPEN_PAREN
;
2793 case RT_CLOSE_BRACE
:
2794 return CPP_CLOSE_BRACE
;
2796 return CPP_OPEN_BRACE
;
2797 case RT_CLOSE_SQUARE
:
2798 return CPP_CLOSE_SQUARE
;
2799 case RT_OPEN_SQUARE
:
2800 return CPP_OPEN_SQUARE
;
2805 case RT_CLOSE_PAREN
:
2806 return CPP_CLOSE_PAREN
;
2809 /* Use CPP_EOF as a "no completions possible" code. */
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2834 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2835 required_token missing_token_desc
,
2836 location_t matching_location
)
2838 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token
);
2843 if (token
->type
== CPP_PRAGMA
)
2845 error_at (token
->location
,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser
, token
);
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token
->type
== CPP_LSHIFT
2853 || token
->type
== CPP_RSHIFT
2854 || token
->type
== CPP_EQ_EQ
)
2857 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2859 error_at (loc
, "version control conflict marker in file");
2864 gcc_rich_location
richloc (input_location
);
2866 bool added_matching_location
= false;
2868 if (missing_token_desc
!= RT_NONE
)
2870 /* Potentially supply a fix-it hint, suggesting to add the
2871 missing token immediately after the *previous* token.
2872 This may move the primary location within richloc. */
2873 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2874 location_t prev_token_loc
2875 = cp_lexer_previous_token (parser
->lexer
)->location
;
2876 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2878 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2879 Attempt to consolidate diagnostics by printing it as a
2880 secondary range within the main diagnostic. */
2881 if (matching_location
!= UNKNOWN_LOCATION
)
2882 added_matching_location
2883 = richloc
.add_location_if_nearby (matching_location
);
2886 /* Actually emit the error. */
2887 c_parse_error (gmsgid
,
2888 /* Because c_parser_error does not understand
2889 CPP_KEYWORD, keywords are treated like
2891 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2892 token
->u
.value
, token
->flags
, &richloc
);
2894 if (missing_token_desc
!= RT_NONE
)
2896 /* If we weren't able to consolidate matching_location, then
2897 print it as a secondary diagnostic. */
2898 if (matching_location
!= UNKNOWN_LOCATION
2899 && !added_matching_location
)
2900 inform (matching_location
, "to match this %qs",
2901 get_matching_symbol (missing_token_desc
));
2905 /* If not parsing tentatively, issue a diagnostic of the form
2906 FILE:LINE: MESSAGE before TOKEN
2907 where TOKEN is the next token in the input stream. MESSAGE
2908 (specified by the caller) is usually of the form "expected
2912 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2914 if (!cp_parser_simulate_error (parser
))
2915 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2918 /* Issue an error about name-lookup failing. NAME is the
2919 IDENTIFIER_NODE DECL is the result of
2920 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2921 the thing that we hoped to find. */
2924 cp_parser_name_lookup_error (cp_parser
* parser
,
2927 name_lookup_error desired
,
2928 location_t location
)
2930 /* If name lookup completely failed, tell the user that NAME was not
2932 if (decl
== error_mark_node
)
2934 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2935 error_at (location
, "%<%E::%E%> has not been declared",
2936 parser
->scope
, name
);
2937 else if (parser
->scope
== global_namespace
)
2938 error_at (location
, "%<::%E%> has not been declared", name
);
2939 else if (parser
->object_scope
2940 && !CLASS_TYPE_P (parser
->object_scope
))
2941 error_at (location
, "request for member %qE in non-class type %qT",
2942 name
, parser
->object_scope
);
2943 else if (parser
->object_scope
)
2944 error_at (location
, "%<%T::%E%> has not been declared",
2945 parser
->object_scope
, name
);
2947 error_at (location
, "%qE has not been declared", name
);
2949 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2954 error_at (location
, "%<%E::%E%> is not a type",
2955 parser
->scope
, name
);
2958 error_at (location
, "%<%E::%E%> is not a class or namespace",
2959 parser
->scope
, name
);
2963 "%<%E::%E%> is not a class, namespace, or enumeration",
2964 parser
->scope
, name
);
2971 else if (parser
->scope
== global_namespace
)
2976 error_at (location
, "%<::%E%> is not a type", name
);
2979 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2983 "%<::%E%> is not a class, namespace, or enumeration",
2995 error_at (location
, "%qE is not a type", name
);
2998 error_at (location
, "%qE is not a class or namespace", name
);
3002 "%qE is not a class, namespace, or enumeration", name
);
3010 /* If we are parsing tentatively, remember that an error has occurred
3011 during this tentative parse. Returns true if the error was
3012 simulated; false if a message should be issued by the caller. */
3015 cp_parser_simulate_error (cp_parser
* parser
)
3017 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3019 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3025 /* This function is called when a type is defined. If type
3026 definitions are forbidden at this point, an error message is
3030 cp_parser_check_type_definition (cp_parser
* parser
)
3032 /* If types are forbidden here, issue a message. */
3033 if (parser
->type_definition_forbidden_message
)
3035 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3036 in the message need to be interpreted. */
3037 error (parser
->type_definition_forbidden_message
);
3043 /* This function is called when the DECLARATOR is processed. The TYPE
3044 was a type defined in the decl-specifiers. If it is invalid to
3045 define a type in the decl-specifiers for DECLARATOR, an error is
3046 issued. TYPE_LOCATION is the location of TYPE and is used
3047 for error reporting. */
3050 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3051 tree type
, location_t type_location
)
3053 /* [dcl.fct] forbids type definitions in return types.
3054 Unfortunately, it's not easy to know whether or not we are
3055 processing a return type until after the fact. */
3057 && (declarator
->kind
== cdk_pointer
3058 || declarator
->kind
== cdk_reference
3059 || declarator
->kind
== cdk_ptrmem
))
3060 declarator
= declarator
->declarator
;
3062 && declarator
->kind
== cdk_function
)
3064 error_at (type_location
,
3065 "new types may not be defined in a return type");
3066 inform (type_location
,
3067 "(perhaps a semicolon is missing after the definition of %qT)",
3072 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3073 "<" in any valid C++ program. If the next token is indeed "<",
3074 issue a message warning the user about what appears to be an
3075 invalid attempt to form a template-id. LOCATION is the location
3076 of the type-specifier (TYPE) */
3079 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3081 enum tag_types tag_type
,
3082 location_t location
)
3084 cp_token_position start
= 0;
3086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3088 if (TREE_CODE (type
) == TYPE_DECL
)
3089 type
= TREE_TYPE (type
);
3090 if (TYPE_P (type
) && !template_placeholder_p (type
))
3091 error_at (location
, "%qT is not a template", type
);
3092 else if (identifier_p (type
))
3094 if (tag_type
!= none_type
)
3095 error_at (location
, "%qE is not a class template", type
);
3097 error_at (location
, "%qE is not a template", type
);
3100 error_at (location
, "invalid template-id");
3101 /* Remember the location of the invalid "<". */
3102 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3103 start
= cp_lexer_token_position (parser
->lexer
, true);
3104 /* Consume the "<". */
3105 cp_lexer_consume_token (parser
->lexer
);
3106 /* Parse the template arguments. */
3107 cp_parser_enclosed_template_argument_list (parser
);
3108 /* Permanently remove the invalid template arguments so that
3109 this error message is not issued again. */
3111 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3115 /* If parsing an integral constant-expression, issue an error message
3116 about the fact that THING appeared and return true. Otherwise,
3117 return false. In either case, set
3118 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3121 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3122 non_integral_constant thing
)
3124 parser
->non_integral_constant_expression_p
= true;
3125 if (parser
->integral_constant_expression_p
)
3127 if (!parser
->allow_non_integral_constant_expression_p
)
3129 const char *msg
= NULL
;
3133 pedwarn (input_location
, OPT_Wpedantic
,
3134 "ISO C++ forbids using a floating-point literal "
3135 "in a constant-expression");
3138 error ("a cast to a type other than an integral or "
3139 "enumeration type cannot appear in a "
3140 "constant-expression");
3143 error ("%<typeid%> operator "
3144 "cannot appear in a constant-expression");
3147 error ("non-constant compound literals "
3148 "cannot appear in a constant-expression");
3151 error ("a function call "
3152 "cannot appear in a constant-expression");
3155 error ("an increment "
3156 "cannot appear in a constant-expression");
3159 error ("an decrement "
3160 "cannot appear in a constant-expression");
3163 error ("an array reference "
3164 "cannot appear in a constant-expression");
3166 case NIC_ADDR_LABEL
:
3167 error ("the address of a label "
3168 "cannot appear in a constant-expression");
3170 case NIC_OVERLOADED
:
3171 error ("calls to overloaded operators "
3172 "cannot appear in a constant-expression");
3174 case NIC_ASSIGNMENT
:
3175 error ("an assignment cannot appear in a constant-expression");
3178 error ("a comma operator "
3179 "cannot appear in a constant-expression");
3181 case NIC_CONSTRUCTOR
:
3182 error ("a call to a constructor "
3183 "cannot appear in a constant-expression");
3185 case NIC_TRANSACTION
:
3186 error ("a transaction expression "
3187 "cannot appear in a constant-expression");
3193 msg
= "__FUNCTION__";
3195 case NIC_PRETTY_FUNC
:
3196 msg
= "__PRETTY_FUNCTION__";
3216 case NIC_PREINCREMENT
:
3219 case NIC_PREDECREMENT
:
3232 error ("%qs cannot appear in a constant-expression", msg
);
3239 /* Emit a diagnostic for an invalid type name. This function commits
3240 to the current active tentative parse, if any. (Otherwise, the
3241 problematic construct might be encountered again later, resulting
3242 in duplicate error messages.) LOCATION is the location of ID. */
3245 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3246 location_t location
)
3248 tree decl
, ambiguous_decls
;
3249 cp_parser_commit_to_tentative_parse (parser
);
3250 /* Try to lookup the identifier. */
3251 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3252 /*is_template=*/false,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3255 &ambiguous_decls
, location
);
3256 if (ambiguous_decls
)
3257 /* If the lookup was ambiguous, an error will already have
3260 /* If the lookup found a template-name, it means that the user forgot
3261 to specify an argument list. Emit a useful error message. */
3262 if (DECL_TYPE_TEMPLATE_P (decl
))
3265 "invalid use of template-name %qE without an argument list",
3267 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3268 inform (location
, "class template argument deduction is only available "
3269 "with -std=c++17 or -std=gnu++17");
3270 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3272 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3273 error_at (location
, "invalid use of destructor %qD as a type", id
);
3274 else if (TREE_CODE (decl
) == TYPE_DECL
)
3275 /* Something like 'unsigned A a;' */
3276 error_at (location
, "invalid combination of multiple type-specifiers");
3277 else if (!parser
->scope
)
3279 /* Issue an error message. */
3281 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3282 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3285 gcc_rich_location
richloc (location
);
3286 richloc
.add_fixit_replace (hint
.suggestion ());
3288 "%qE does not name a type; did you mean %qs?",
3289 id
, hint
.suggestion ());
3292 error_at (location
, "%qE does not name a type", id
);
3293 /* If we're in a template class, it's possible that the user was
3294 referring to a type from a base class. For example:
3296 template <typename T> struct A { typedef T X; };
3297 template <typename T> struct B : public A<T> { X x; };
3299 The user should have said "typename A<T>::X". */
3300 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3301 inform (location
, "C++11 %<constexpr%> only available with "
3302 "-std=c++11 or -std=gnu++11");
3303 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3304 inform (location
, "C++11 %<noexcept%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect
< cxx11
3307 && TREE_CODE (id
) == IDENTIFIER_NODE
3308 && id_equal (id
, "thread_local"))
3309 inform (location
, "C++11 %<thread_local%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3312 inform (location
, "%<concept%> only available with -fconcepts");
3313 else if (processing_template_decl
&& current_class_type
3314 && TYPE_BINFO (current_class_type
))
3318 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3322 tree base_type
= BINFO_TYPE (b
);
3323 if (CLASS_TYPE_P (base_type
)
3324 && dependent_type_p (base_type
))
3327 /* Go from a particular instantiation of the
3328 template (which will have an empty TYPE_FIELDs),
3329 to the main version. */
3330 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3331 for (field
= TYPE_FIELDS (base_type
);
3333 field
= DECL_CHAIN (field
))
3334 if (TREE_CODE (field
) == TYPE_DECL
3335 && DECL_NAME (field
) == id
)
3338 "(perhaps %<typename %T::%E%> was intended)",
3339 BINFO_TYPE (b
), id
);
3348 /* Here we diagnose qualified-ids where the scope is actually correct,
3349 but the identifier does not resolve to a valid type name. */
3350 else if (parser
->scope
!= error_mark_node
)
3352 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3355 error_at (location_of (id
),
3356 "%qE in namespace %qE does not name a template type",
3358 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3359 error_at (location_of (id
),
3360 "%qE in namespace %qE does not name a template type",
3361 TREE_OPERAND (id
, 0), parser
->scope
);
3363 error_at (location_of (id
),
3364 "%qE in namespace %qE does not name a type",
3367 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3368 else if (decl
== error_mark_node
)
3369 suggest_alternative_in_explicit_scope (location
, id
,
3372 else if (CLASS_TYPE_P (parser
->scope
)
3373 && constructor_name_p (id
, parser
->scope
))
3376 error_at (location
, "%<%T::%E%> names the constructor, not"
3377 " the type", parser
->scope
, id
);
3378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3379 error_at (location
, "and %qT has no template constructors",
3382 else if (TYPE_P (parser
->scope
)
3383 && dependent_scope_p (parser
->scope
))
3385 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3387 "need %<typename%> before %<%T::%D::%E%> because "
3388 "%<%T::%D%> is a dependent scope",
3389 TYPE_CONTEXT (parser
->scope
),
3390 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3392 TYPE_CONTEXT (parser
->scope
),
3393 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3395 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3396 "%qT is a dependent scope",
3397 parser
->scope
, id
, parser
->scope
);
3399 else if (TYPE_P (parser
->scope
))
3401 if (!COMPLETE_TYPE_P (parser
->scope
))
3402 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3404 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3405 error_at (location_of (id
),
3406 "%qE in %q#T does not name a template type",
3408 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3409 error_at (location_of (id
),
3410 "%qE in %q#T does not name a template type",
3411 TREE_OPERAND (id
, 0), parser
->scope
);
3413 error_at (location_of (id
),
3414 "%qE in %q#T does not name a type",
3417 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3424 /* Check for a common situation where a type-name should be present,
3425 but is not, and issue a sensible error message. Returns true if an
3426 invalid type-name was detected.
3428 The situation handled by this function are variable declarations of the
3429 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3430 Usually, `ID' should name a type, but if we got here it means that it
3431 does not. We try to emit the best possible error message depending on
3432 how exactly the id-expression looks like. */
3435 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3438 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3440 /* Avoid duplicate error about ambiguous lookup. */
3441 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3443 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3444 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3448 cp_parser_parse_tentatively (parser
);
3449 id
= cp_parser_id_expression (parser
,
3450 /*template_keyword_p=*/false,
3451 /*check_dependency_p=*/true,
3452 /*template_p=*/NULL
,
3453 /*declarator_p=*/true,
3454 /*optional_p=*/false);
3455 /* If the next token is a (, this is a function with no explicit return
3456 type, i.e. constructor, destructor or conversion op. */
3457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3458 || TREE_CODE (id
) == TYPE_DECL
)
3460 cp_parser_abort_tentative_parse (parser
);
3463 if (!cp_parser_parse_definitely (parser
))
3466 /* Emit a diagnostic for the invalid type. */
3467 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3469 /* If we aren't in the middle of a declarator (i.e. in a
3470 parameter-declaration-clause), skip to the end of the declaration;
3471 there's no point in trying to process it. */
3472 if (!parser
->in_declarator_p
)
3473 cp_parser_skip_to_end_of_block_or_statement (parser
);
3477 /* Consume tokens up to, and including, the next non-nested closing `)'.
3478 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3479 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3480 found an unnested token of that type. */
3483 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3488 unsigned paren_depth
= 0;
3489 unsigned brace_depth
= 0;
3490 unsigned square_depth
= 0;
3492 if (recovering
&& or_ttype
== CPP_EOF
3493 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3498 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3500 /* Have we found what we're looking for before the closing paren? */
3501 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3502 && !brace_depth
&& !paren_depth
&& !square_depth
)
3505 switch (token
->type
)
3508 case CPP_PRAGMA_EOL
:
3509 /* If we've run out of tokens, then there is no closing `)'. */
3512 /* This is good for lambda expression capture-lists. */
3513 case CPP_OPEN_SQUARE
:
3516 case CPP_CLOSE_SQUARE
:
3517 if (!square_depth
--)
3522 /* This matches the processing in skip_to_end_of_statement. */
3527 case CPP_OPEN_BRACE
:
3530 case CPP_CLOSE_BRACE
:
3535 case CPP_OPEN_PAREN
:
3540 case CPP_CLOSE_PAREN
:
3541 if (!brace_depth
&& !paren_depth
--)
3544 cp_lexer_consume_token (parser
->lexer
);
3553 /* Consume the token. */
3554 cp_lexer_consume_token (parser
->lexer
);
3558 /* Consume tokens up to, and including, the next non-nested closing `)'.
3559 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3560 are doing error recovery. Returns -1 if OR_COMMA is true and we
3561 found an unnested token of that type. */
3564 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3569 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3570 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3571 ttype
, consume_paren
);
3574 /* Consume tokens until we reach the end of the current statement.
3575 Normally, that will be just before consuming a `;'. However, if a
3576 non-nested `}' comes first, then we stop before consuming that. */
3579 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3581 unsigned nesting_depth
= 0;
3583 /* Unwind generic function template scope if necessary. */
3584 if (parser
->fully_implicit_function_template_p
)
3585 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3589 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3591 switch (token
->type
)
3594 case CPP_PRAGMA_EOL
:
3595 /* If we've run out of tokens, stop. */
3599 /* If the next token is a `;', we have reached the end of the
3605 case CPP_CLOSE_BRACE
:
3606 /* If this is a non-nested '}', stop before consuming it.
3607 That way, when confronted with something like:
3611 we stop before consuming the closing '}', even though we
3612 have not yet reached a `;'. */
3613 if (nesting_depth
== 0)
3616 /* If it is the closing '}' for a block that we have
3617 scanned, stop -- but only after consuming the token.
3623 we will stop after the body of the erroneously declared
3624 function, but before consuming the following `typedef'
3626 if (--nesting_depth
== 0)
3628 cp_lexer_consume_token (parser
->lexer
);
3633 case CPP_OPEN_BRACE
:
3641 /* Consume the token. */
3642 cp_lexer_consume_token (parser
->lexer
);
3646 /* This function is called at the end of a statement or declaration.
3647 If the next token is a semicolon, it is consumed; otherwise, error
3648 recovery is attempted. */
3651 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3653 /* Look for the trailing `;'. */
3654 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3656 /* If there is additional (erroneous) input, skip to the end of
3658 cp_parser_skip_to_end_of_statement (parser
);
3659 /* If the next token is now a `;', consume it. */
3660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3661 cp_lexer_consume_token (parser
->lexer
);
3665 /* Skip tokens until we have consumed an entire block, or until we
3666 have consumed a non-nested `;'. */
3669 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3671 int nesting_depth
= 0;
3673 /* Unwind generic function template scope if necessary. */
3674 if (parser
->fully_implicit_function_template_p
)
3675 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3677 while (nesting_depth
>= 0)
3679 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3681 switch (token
->type
)
3684 case CPP_PRAGMA_EOL
:
3685 /* If we've run out of tokens, stop. */
3689 /* Stop if this is an unnested ';'. */
3694 case CPP_CLOSE_BRACE
:
3695 /* Stop if this is an unnested '}', or closes the outermost
3698 if (nesting_depth
< 0)
3704 case CPP_OPEN_BRACE
:
3713 /* Consume the token. */
3714 cp_lexer_consume_token (parser
->lexer
);
3718 /* Skip tokens until a non-nested closing curly brace is the next
3719 token, or there are no more tokens. Return true in the first case,
3723 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3725 unsigned nesting_depth
= 0;
3729 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3731 switch (token
->type
)
3734 case CPP_PRAGMA_EOL
:
3735 /* If we've run out of tokens, stop. */
3738 case CPP_CLOSE_BRACE
:
3739 /* If the next token is a non-nested `}', then we have reached
3740 the end of the current block. */
3741 if (nesting_depth
-- == 0)
3745 case CPP_OPEN_BRACE
:
3746 /* If it the next token is a `{', then we are entering a new
3747 block. Consume the entire block. */
3755 /* Consume the token. */
3756 cp_lexer_consume_token (parser
->lexer
);
3760 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3761 parameter is the PRAGMA token, allowing us to purge the entire pragma
3765 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3769 parser
->lexer
->in_pragma
= false;
3772 token
= cp_lexer_consume_token (parser
->lexer
);
3773 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3775 /* Ensure that the pragma is not parsed again. */
3776 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3779 /* Require pragma end of line, resyncing with it as necessary. The
3780 arguments are as for cp_parser_skip_to_pragma_eol. */
3783 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3785 parser
->lexer
->in_pragma
= false;
3786 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3787 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3790 /* This is a simple wrapper around make_typename_type. When the id is
3791 an unresolved identifier node, we can provide a superior diagnostic
3792 using cp_parser_diagnose_invalid_type_name. */
3795 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3796 location_t id_location
)
3799 if (identifier_p (id
))
3801 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3802 /*complain=*/tf_none
);
3803 if (result
== error_mark_node
)
3804 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3807 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3810 /* This is a wrapper around the
3811 make_{pointer,ptrmem,reference}_declarator functions that decides
3812 which one to call based on the CODE and CLASS_TYPE arguments. The
3813 CODE argument should be one of the values returned by
3814 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3815 appertain to the pointer or reference. */
3817 static cp_declarator
*
3818 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3819 cp_cv_quals cv_qualifiers
,
3820 cp_declarator
*target
,
3823 if (code
== ERROR_MARK
)
3824 return cp_error_declarator
;
3826 if (code
== INDIRECT_REF
)
3827 if (class_type
== NULL_TREE
)
3828 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3830 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3831 target
, attributes
);
3832 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3833 return make_reference_declarator (cv_qualifiers
, target
,
3835 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3836 return make_reference_declarator (cv_qualifiers
, target
,
3841 /* Create a new C++ parser. */
3844 cp_parser_new (void)
3850 /* cp_lexer_new_main is called before doing GC allocation because
3851 cp_lexer_new_main might load a PCH file. */
3852 lexer
= cp_lexer_new_main ();
3854 /* Initialize the binops_by_token so that we can get the tree
3855 directly from the token. */
3856 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3857 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3859 parser
= ggc_cleared_alloc
<cp_parser
> ();
3860 parser
->lexer
= lexer
;
3861 parser
->context
= cp_parser_context_new (NULL
);
3863 /* For now, we always accept GNU extensions. */
3864 parser
->allow_gnu_extensions_p
= 1;
3866 /* The `>' token is a greater-than operator, not the end of a
3868 parser
->greater_than_is_operator_p
= true;
3870 parser
->default_arg_ok_p
= true;
3872 /* We are not parsing a constant-expression. */
3873 parser
->integral_constant_expression_p
= false;
3874 parser
->allow_non_integral_constant_expression_p
= false;
3875 parser
->non_integral_constant_expression_p
= false;
3877 /* Local variable names are not forbidden. */
3878 parser
->local_variables_forbidden_p
= false;
3880 /* We are not processing an `extern "C"' declaration. */
3881 parser
->in_unbraced_linkage_specification_p
= false;
3883 /* We are not processing a declarator. */
3884 parser
->in_declarator_p
= false;
3886 /* We are not processing a template-argument-list. */
3887 parser
->in_template_argument_list_p
= false;
3889 /* We are not in an iteration statement. */
3890 parser
->in_statement
= 0;
3892 /* We are not in a switch statement. */
3893 parser
->in_switch_statement_p
= false;
3895 /* We are not parsing a type-id inside an expression. */
3896 parser
->in_type_id_in_expr_p
= false;
3898 /* Declarations aren't implicitly extern "C". */
3899 parser
->implicit_extern_c
= false;
3901 /* String literals should be translated to the execution character set. */
3902 parser
->translate_strings_p
= true;
3904 /* We are not parsing a function body. */
3905 parser
->in_function_body
= false;
3907 /* We can correct until told otherwise. */
3908 parser
->colon_corrects_to_scope_p
= true;
3910 /* The unparsed function queue is empty. */
3911 push_unparsed_function_queues (parser
);
3913 /* There are no classes being defined. */
3914 parser
->num_classes_being_defined
= 0;
3916 /* No template parameters apply. */
3917 parser
->num_template_parameter_lists
= 0;
3919 /* Special parsing data structures. */
3920 parser
->omp_declare_simd
= NULL
;
3921 parser
->oacc_routine
= NULL
;
3923 /* Not declaring an implicit function template. */
3924 parser
->auto_is_implicit_function_template_parm_p
= false;
3925 parser
->fully_implicit_function_template_p
= false;
3926 parser
->implicit_template_parms
= 0;
3927 parser
->implicit_template_scope
= 0;
3929 /* Allow constrained-type-specifiers. */
3930 parser
->prevent_constrained_type_specifiers
= 0;
3932 /* We haven't yet seen an 'extern "C"'. */
3933 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3938 /* Create a cp_lexer structure which will emit the tokens in CACHE
3939 and push it onto the parser's lexer stack. This is used for delayed
3940 parsing of in-class method bodies and default arguments, and should
3941 not be confused with tentative parsing. */
3943 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3945 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3946 lexer
->next
= parser
->lexer
;
3947 parser
->lexer
= lexer
;
3949 /* Move the current source position to that of the first token in the
3951 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3954 /* Pop the top lexer off the parser stack. This is never used for the
3955 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3957 cp_parser_pop_lexer (cp_parser
*parser
)
3959 cp_lexer
*lexer
= parser
->lexer
;
3960 parser
->lexer
= lexer
->next
;
3961 cp_lexer_destroy (lexer
);
3963 /* Put the current source position back where it was before this
3964 lexer was pushed. */
3965 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3968 /* Lexical conventions [gram.lex] */
3970 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3974 cp_parser_identifier (cp_parser
* parser
)
3978 /* Look for the identifier. */
3979 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3980 /* Return the value. */
3982 return cp_expr (token
->u
.value
, token
->location
);
3984 return error_mark_node
;
3987 /* Parse a sequence of adjacent string constants. Returns a
3988 TREE_STRING representing the combined, nul-terminated string
3989 constant. If TRANSLATE is true, translate the string to the
3990 execution character set. If WIDE_OK is true, a wide string is
3993 C++98 [lex.string] says that if a narrow string literal token is
3994 adjacent to a wide string literal token, the behavior is undefined.
3995 However, C99 6.4.5p4 says that this results in a wide string literal.
3996 We follow C99 here, for consistency with the C front end.
3998 This code is largely lifted from lex_string() in c-lex.c.
4000 FUTURE: ObjC++ will need to handle @-strings here. */
4002 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4003 bool lookup_udlit
= true)
4007 struct obstack str_ob
;
4008 cpp_string str
, istr
, *strs
;
4010 enum cpp_ttype type
, curr_type
;
4011 int have_suffix_p
= 0;
4013 tree suffix_id
= NULL_TREE
;
4014 bool curr_tok_is_userdef_p
= false;
4016 tok
= cp_lexer_peek_token (parser
->lexer
);
4017 if (!cp_parser_is_string_literal (tok
))
4019 cp_parser_error (parser
, "expected string-literal");
4020 return error_mark_node
;
4023 location_t loc
= tok
->location
;
4025 if (cpp_userdef_string_p (tok
->type
))
4027 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4028 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4029 curr_tok_is_userdef_p
= true;
4033 string_tree
= tok
->u
.value
;
4034 curr_type
= tok
->type
;
4038 /* Try to avoid the overhead of creating and destroying an obstack
4039 for the common case of just one string. */
4040 if (!cp_parser_is_string_literal
4041 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4043 cp_lexer_consume_token (parser
->lexer
);
4045 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4046 str
.len
= TREE_STRING_LENGTH (string_tree
);
4049 if (curr_tok_is_userdef_p
)
4051 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4053 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4056 curr_type
= tok
->type
;
4062 location_t last_tok_loc
= tok
->location
;
4063 gcc_obstack_init (&str_ob
);
4068 cp_lexer_consume_token (parser
->lexer
);
4070 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4071 str
.len
= TREE_STRING_LENGTH (string_tree
);
4073 if (curr_tok_is_userdef_p
)
4075 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4076 if (have_suffix_p
== 0)
4078 suffix_id
= curr_suffix_id
;
4081 else if (have_suffix_p
== 1
4082 && curr_suffix_id
!= suffix_id
)
4084 error ("inconsistent user-defined literal suffixes"
4085 " %qD and %qD in string literal",
4086 suffix_id
, curr_suffix_id
);
4089 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4092 curr_type
= tok
->type
;
4094 if (type
!= curr_type
)
4096 if (type
== CPP_STRING
)
4098 else if (curr_type
!= CPP_STRING
)
4100 rich_location
rich_loc (line_table
, tok
->location
);
4101 rich_loc
.add_range (last_tok_loc
, false);
4102 error_at (&rich_loc
,
4103 "unsupported non-standard concatenation "
4104 "of string literals");
4108 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4110 last_tok_loc
= tok
->location
;
4112 tok
= cp_lexer_peek_token (parser
->lexer
);
4113 if (cpp_userdef_string_p (tok
->type
))
4115 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4116 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4117 curr_tok_is_userdef_p
= true;
4121 string_tree
= tok
->u
.value
;
4122 curr_type
= tok
->type
;
4123 curr_tok_is_userdef_p
= false;
4126 while (cp_parser_is_string_literal (tok
));
4128 /* A string literal built by concatenation has its caret=start at
4129 the start of the initial string, and its finish at the finish of
4130 the final string literal. */
4131 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4133 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4136 if (type
!= CPP_STRING
&& !wide_ok
)
4138 cp_parser_error (parser
, "a wide string is invalid in this context");
4142 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4143 (parse_in
, strs
, count
, &istr
, type
))
4145 value
= build_string (istr
.len
, (const char *)istr
.text
);
4146 free (CONST_CAST (unsigned char *, istr
.text
));
4152 case CPP_UTF8STRING
:
4153 TREE_TYPE (value
) = char_array_type_node
;
4156 TREE_TYPE (value
) = char16_array_type_node
;
4159 TREE_TYPE (value
) = char32_array_type_node
;
4162 TREE_TYPE (value
) = wchar_array_type_node
;
4166 value
= fix_string_type (value
);
4170 tree literal
= build_userdef_literal (suffix_id
, value
,
4171 OT_NONE
, NULL_TREE
);
4173 value
= cp_parser_userdef_string_literal (literal
);
4179 /* cpp_interpret_string has issued an error. */
4180 value
= error_mark_node
;
4183 obstack_free (&str_ob
, 0);
4185 return cp_expr (value
, loc
);
4188 /* Look up a literal operator with the name and the exact arguments. */
4191 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4194 decl
= lookup_name (name
);
4195 if (!decl
|| !is_overloaded_fn (decl
))
4196 return error_mark_node
;
4198 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4203 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4204 if (parmtypes
!= NULL_TREE
)
4206 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4207 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4209 tree tparm
= TREE_VALUE (parmtypes
);
4210 tree targ
= TREE_TYPE ((*args
)[ix
]);
4211 bool ptr
= TYPE_PTR_P (tparm
);
4212 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4213 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4215 || !same_type_p (TREE_TYPE (tparm
),
4220 && ix
== vec_safe_length (args
)
4221 /* May be this should be sufficient_parms_p instead,
4222 depending on how exactly should user-defined literals
4223 work in presence of default arguments on the literal
4224 operator parameters. */
4225 && parmtypes
== void_list_node
)
4230 return error_mark_node
;
4233 /* Parse a user-defined char constant. Returns a call to a user-defined
4234 literal operator taking the character as an argument. */
4237 cp_parser_userdef_char_literal (cp_parser
*parser
)
4239 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4240 tree literal
= token
->u
.value
;
4241 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4242 tree value
= USERDEF_LITERAL_VALUE (literal
);
4243 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4246 /* Build up a call to the user-defined operator */
4247 /* Lookup the name we got back from the id-expression. */
4248 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4249 vec_safe_push (args
, value
);
4250 decl
= lookup_literal_operator (name
, args
);
4251 if (!decl
|| decl
== error_mark_node
)
4253 error ("unable to find character literal operator %qD with %qT argument",
4254 name
, TREE_TYPE (value
));
4255 release_tree_vector (args
);
4256 return error_mark_node
;
4258 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4259 release_tree_vector (args
);
4263 /* A subroutine of cp_parser_userdef_numeric_literal to
4264 create a char... template parameter pack from a string node. */
4267 make_char_string_pack (tree value
)
4270 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4271 const char *str
= TREE_STRING_POINTER (value
);
4272 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4273 tree argvec
= make_tree_vec (1);
4275 /* Fill in CHARVEC with all of the parameters. */
4276 charvec
= make_tree_vec (len
);
4277 for (i
= 0; i
< len
; ++i
)
4278 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4280 /* Build the argument packs. */
4281 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4283 TREE_VEC_ELT (argvec
, 0) = argpack
;
4288 /* A subroutine of cp_parser_userdef_numeric_literal to
4289 create a char... template parameter pack from a string node. */
4292 make_string_pack (tree value
)
4295 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4296 const unsigned char *str
4297 = (const unsigned char *) TREE_STRING_POINTER (value
);
4298 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4299 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4300 tree argvec
= make_tree_vec (2);
4302 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4303 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4305 /* First template parm is character type. */
4306 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4308 /* Fill in CHARVEC with all of the parameters. */
4309 charvec
= make_tree_vec (len
);
4310 for (int i
= 0; i
< len
; ++i
)
4311 TREE_VEC_ELT (charvec
, i
)
4312 = double_int_to_tree (str_char_type_node
,
4313 double_int::from_buffer (str
+ i
* sz
, sz
));
4315 /* Build the argument packs. */
4316 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4318 TREE_VEC_ELT (argvec
, 1) = argpack
;
4323 /* Parse a user-defined numeric constant. returns a call to a user-defined
4324 literal operator. */
4327 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4329 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4330 tree literal
= token
->u
.value
;
4331 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4332 tree value
= USERDEF_LITERAL_VALUE (literal
);
4333 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4334 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4335 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4337 vec
<tree
, va_gc
> *args
;
4339 /* Look for a literal operator taking the exact type of numeric argument
4340 as the literal value. */
4341 args
= make_tree_vector ();
4342 vec_safe_push (args
, value
);
4343 decl
= lookup_literal_operator (name
, args
);
4344 if (decl
&& decl
!= error_mark_node
)
4346 result
= finish_call_expr (decl
, &args
, false, true,
4347 tf_warning_or_error
);
4349 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4351 warning_at (token
->location
, OPT_Woverflow
,
4352 "integer literal exceeds range of %qT type",
4353 long_long_unsigned_type_node
);
4358 warning_at (token
->location
, OPT_Woverflow
,
4359 "floating literal exceeds range of %qT type",
4360 long_double_type_node
);
4361 else if (overflow
< 0)
4362 warning_at (token
->location
, OPT_Woverflow
,
4363 "floating literal truncated to zero");
4366 release_tree_vector (args
);
4369 release_tree_vector (args
);
4371 /* If the numeric argument didn't work, look for a raw literal
4372 operator taking a const char* argument consisting of the number
4373 in string format. */
4374 args
= make_tree_vector ();
4375 vec_safe_push (args
, num_string
);
4376 decl
= lookup_literal_operator (name
, args
);
4377 if (decl
&& decl
!= error_mark_node
)
4379 result
= finish_call_expr (decl
, &args
, false, true,
4380 tf_warning_or_error
);
4381 release_tree_vector (args
);
4384 release_tree_vector (args
);
4386 /* If the raw literal didn't work, look for a non-type template
4387 function with parameter pack char.... Call the function with
4388 template parameter characters representing the number. */
4389 args
= make_tree_vector ();
4390 decl
= lookup_literal_operator (name
, args
);
4391 if (decl
&& decl
!= error_mark_node
)
4393 tree tmpl_args
= make_char_string_pack (num_string
);
4394 decl
= lookup_template_function (decl
, tmpl_args
);
4395 result
= finish_call_expr (decl
, &args
, false, true,
4396 tf_warning_or_error
);
4397 release_tree_vector (args
);
4401 release_tree_vector (args
);
4403 /* In C++14 the standard library defines complex number suffixes that
4404 conflict with GNU extensions. Prefer them if <complex> is #included. */
4405 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4406 bool i14
= (cxx_dialect
> cxx11
4407 && (id_equal (suffix_id
, "i")
4408 || id_equal (suffix_id
, "if")
4409 || id_equal (suffix_id
, "il")));
4410 diagnostic_t kind
= DK_ERROR
;
4415 tree cxlit
= lookup_qualified_name (std_node
,
4416 get_identifier ("complex_literals"),
4418 if (cxlit
== error_mark_node
)
4420 /* No <complex>, so pedwarn and use GNU semantics. */
4422 opt
= OPT_Wpedantic
;
4427 = emit_diagnostic (kind
, input_location
, opt
,
4428 "unable to find numeric literal operator %qD", name
);
4431 /* Don't inform either. */;
4434 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4435 "(from <complex>) to enable the C++14 user-defined literal "
4438 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4439 "GNU built-in suffix");
4442 inform (token
->location
, "use -fext-numeric-literals "
4443 "to enable more built-in suffixes");
4445 if (kind
== DK_ERROR
)
4446 value
= error_mark_node
;
4449 /* Use the built-in semantics. */
4451 if (id_equal (suffix_id
, "i"))
4453 if (TREE_CODE (value
) == INTEGER_CST
)
4454 type
= integer_type_node
;
4456 type
= double_type_node
;
4458 else if (id_equal (suffix_id
, "if"))
4459 type
= float_type_node
;
4460 else /* if (id_equal (suffix_id, "il")) */
4461 type
= long_double_type_node
;
4463 value
= build_complex (build_complex_type (type
),
4464 fold_convert (type
, integer_zero_node
),
4465 fold_convert (type
, value
));
4468 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4469 /* Avoid repeated diagnostics. */
4470 token
->u
.value
= value
;
4474 /* Parse a user-defined string constant. Returns a call to a user-defined
4475 literal operator taking a character pointer and the length of the string
4479 cp_parser_userdef_string_literal (tree literal
)
4481 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4482 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4483 tree value
= USERDEF_LITERAL_VALUE (literal
);
4484 int len
= TREE_STRING_LENGTH (value
)
4485 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4487 vec
<tree
, va_gc
> *args
;
4489 /* Build up a call to the user-defined operator. */
4490 /* Lookup the name we got back from the id-expression. */
4491 args
= make_tree_vector ();
4492 vec_safe_push (args
, value
);
4493 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4494 decl
= lookup_literal_operator (name
, args
);
4496 if (decl
&& decl
!= error_mark_node
)
4498 result
= finish_call_expr (decl
, &args
, false, true,
4499 tf_warning_or_error
);
4500 release_tree_vector (args
);
4503 release_tree_vector (args
);
4505 /* Look for a template function with typename parameter CharT
4506 and parameter pack CharT... Call the function with
4507 template parameter characters representing the string. */
4508 args
= make_tree_vector ();
4509 decl
= lookup_literal_operator (name
, args
);
4510 if (decl
&& decl
!= error_mark_node
)
4512 tree tmpl_args
= make_string_pack (value
);
4513 decl
= lookup_template_function (decl
, tmpl_args
);
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 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4522 name
, TREE_TYPE (value
), size_type_node
);
4523 return error_mark_node
;
4527 /* Basic concepts [gram.basic] */
4529 /* Parse a translation-unit.
4532 declaration-seq [opt]
4534 Returns TRUE if all went well. */
4537 cp_parser_translation_unit (cp_parser
* parser
)
4539 /* The address of the first non-permanent object on the declarator
4541 static void *declarator_obstack_base
;
4545 /* Create the declarator obstack, if necessary. */
4546 if (!cp_error_declarator
)
4548 gcc_obstack_init (&declarator_obstack
);
4549 /* Create the error declarator. */
4550 cp_error_declarator
= make_declarator (cdk_error
);
4551 /* Create the empty parameter list. */
4552 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4554 /* Remember where the base of the declarator obstack lies. */
4555 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4558 cp_parser_declaration_seq_opt (parser
);
4560 /* If there are no tokens left then all went well. */
4561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4563 /* Get rid of the token array; we don't need it any more. */
4564 cp_lexer_destroy (parser
->lexer
);
4565 parser
->lexer
= NULL
;
4567 /* This file might have been a context that's implicitly extern
4568 "C". If so, pop the lang context. (Only relevant for PCH.) */
4569 if (parser
->implicit_extern_c
)
4571 pop_lang_context ();
4572 parser
->implicit_extern_c
= false;
4576 finish_translation_unit ();
4582 cp_parser_error (parser
, "expected declaration");
4586 /* Make sure the declarator obstack was fully cleaned up. */
4587 gcc_assert (obstack_next_free (&declarator_obstack
)
4588 == declarator_obstack_base
);
4590 /* All went well. */
4594 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4595 decltype context. */
4597 static inline tsubst_flags_t
4598 complain_flags (bool decltype_p
)
4600 tsubst_flags_t complain
= tf_warning_or_error
;
4602 complain
|= tf_decltype
;
4606 /* We're about to parse a collection of statements. If we're currently
4607 parsing tentatively, set up a firewall so that any nested
4608 cp_parser_commit_to_tentative_parse won't affect the current context. */
4610 static cp_token_position
4611 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4613 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4616 cp_parser_parse_tentatively (parser
);
4617 cp_parser_commit_to_topmost_tentative_parse (parser
);
4618 return cp_lexer_token_position (parser
->lexer
, false);
4621 /* We've finished parsing the collection of statements. Wrap up the
4622 firewall and replace the relevant tokens with the parsed form. */
4625 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4631 /* Finish the firewall level. */
4632 cp_parser_parse_definitely (parser
);
4633 /* And remember the result of the parse for when we try again. */
4634 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4635 token
->type
= CPP_PREPARSED_EXPR
;
4636 token
->u
.value
= expr
;
4637 token
->keyword
= RID_MAX
;
4638 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4641 /* Like the above functions, but let the user modify the tokens. Used by
4642 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4643 later parses, so it makes sense to localize the effects of
4644 cp_parser_commit_to_tentative_parse. */
4646 struct tentative_firewall
4651 tentative_firewall (cp_parser
*p
): parser(p
)
4653 /* If we're currently parsing tentatively, start a committed level as a
4654 firewall and then an inner tentative parse. */
4655 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4657 cp_parser_parse_tentatively (parser
);
4658 cp_parser_commit_to_topmost_tentative_parse (parser
);
4659 cp_parser_parse_tentatively (parser
);
4663 ~tentative_firewall()
4667 /* Finish the inner tentative parse and the firewall, propagating any
4668 uncommitted error state to the outer tentative parse. */
4669 bool err
= cp_parser_error_occurred (parser
);
4670 cp_parser_parse_definitely (parser
);
4671 cp_parser_parse_definitely (parser
);
4673 cp_parser_simulate_error (parser
);
4678 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4679 This class is for tracking such a matching pair of symbols.
4680 In particular, it tracks the location of the first token,
4681 so that if the second token is missing, we can highlight the
4682 location of the first token when notifying the user about the
4685 template <typename traits_t
>
4689 /* token_pair's ctor. */
4690 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4692 /* If the next token is the opening symbol for this pair, consume it and
4694 Otherwise, issue an error and return false.
4695 In either case, record the location of the opening token. */
4697 bool require_open (cp_parser
*parser
)
4699 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4700 return cp_parser_require (parser
, traits_t::open_token_type
,
4701 traits_t::required_token_open
);
4704 /* Consume the next token from PARSER, recording its location as
4705 that of the opening token within the pair. */
4707 cp_token
* consume_open (cp_parser
*parser
)
4709 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4710 gcc_assert (tok
->type
== traits_t::open_token_type
);
4711 m_open_loc
= tok
->location
;
4715 /* If the next token is the closing symbol for this pair, consume it
4717 Otherwise, issue an error, highlighting the location of the
4718 corresponding opening token, and return NULL. */
4720 cp_token
*require_close (cp_parser
*parser
) const
4722 return cp_parser_require (parser
, traits_t::close_token_type
,
4723 traits_t::required_token_close
,
4728 location_t m_open_loc
;
4731 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4733 struct matching_paren_traits
4735 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4736 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4737 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4738 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4741 /* "matching_parens" is a token_pair<T> class for tracking matching
4742 pairs of parentheses. */
4744 typedef token_pair
<matching_paren_traits
> matching_parens
;
4746 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4748 struct matching_brace_traits
4750 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4751 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4752 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4753 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4756 /* "matching_braces" is a token_pair<T> class for tracking matching
4759 typedef token_pair
<matching_brace_traits
> matching_braces
;
4762 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4763 enclosing parentheses. */
4766 cp_parser_statement_expr (cp_parser
*parser
)
4768 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4770 /* Consume the '('. */
4771 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4772 matching_parens parens
;
4773 parens
.consume_open (parser
);
4774 /* Start the statement-expression. */
4775 tree expr
= begin_stmt_expr ();
4776 /* Parse the compound-statement. */
4777 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4779 expr
= finish_stmt_expr (expr
, false);
4780 /* Consume the ')'. */
4781 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4782 if (!parens
.require_close (parser
))
4783 cp_parser_skip_to_end_of_statement (parser
);
4785 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4786 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4787 return cp_expr (expr
, combined_loc
);
4790 /* Expressions [gram.expr] */
4792 /* Parse a fold-operator.
4795 - * / % ^ & | = < > << >>
4796 = -= *= /= %= ^= &= |= <<= >>=
4797 == != <= >= && || , .* ->*
4799 This returns the tree code corresponding to the matched operator
4800 as an int. When the current token matches a compound assignment
4801 opertor, the resulting tree code is the negative value of the
4802 non-assignment operator. */
4805 cp_parser_fold_operator (cp_token
*token
)
4807 switch (token
->type
)
4809 case CPP_PLUS
: return PLUS_EXPR
;
4810 case CPP_MINUS
: return MINUS_EXPR
;
4811 case CPP_MULT
: return MULT_EXPR
;
4812 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4813 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4814 case CPP_XOR
: return BIT_XOR_EXPR
;
4815 case CPP_AND
: return BIT_AND_EXPR
;
4816 case CPP_OR
: return BIT_IOR_EXPR
;
4817 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4818 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4820 case CPP_EQ
: return -NOP_EXPR
;
4821 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4822 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4823 case CPP_MULT_EQ
: return -MULT_EXPR
;
4824 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4825 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4826 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4827 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4828 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4829 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4830 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4832 case CPP_EQ_EQ
: return EQ_EXPR
;
4833 case CPP_NOT_EQ
: return NE_EXPR
;
4834 case CPP_LESS
: return LT_EXPR
;
4835 case CPP_GREATER
: return GT_EXPR
;
4836 case CPP_LESS_EQ
: return LE_EXPR
;
4837 case CPP_GREATER_EQ
: return GE_EXPR
;
4839 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4840 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4842 case CPP_COMMA
: return COMPOUND_EXPR
;
4844 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4845 case CPP_DEREF_STAR
: return MEMBER_REF
;
4847 default: return ERROR_MARK
;
4851 /* Returns true if CODE indicates a binary expression, which is not allowed in
4852 the LHS of a fold-expression. More codes will need to be added to use this
4853 function in other contexts. */
4856 is_binary_op (tree_code code
)
4861 case POINTER_PLUS_EXPR
:
4864 case TRUNC_DIV_EXPR
:
4865 case TRUNC_MOD_EXPR
:
4881 case TRUTH_ANDIF_EXPR
:
4882 case TRUTH_ORIF_EXPR
:
4895 /* If the next token is a suitable fold operator, consume it and return as
4896 the function above. */
4899 cp_parser_fold_operator (cp_parser
*parser
)
4901 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4902 int code
= cp_parser_fold_operator (token
);
4903 if (code
!= ERROR_MARK
)
4904 cp_lexer_consume_token (parser
->lexer
);
4908 /* Parse a fold-expression.
4911 ( ... folding-operator cast-expression)
4912 ( cast-expression folding-operator ... )
4913 ( cast-expression folding operator ... folding-operator cast-expression)
4915 Note that the '(' and ')' are matched in primary expression. */
4918 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4925 cp_lexer_consume_token (parser
->lexer
);
4926 int op
= cp_parser_fold_operator (parser
);
4927 if (op
== ERROR_MARK
)
4929 cp_parser_error (parser
, "expected binary operator");
4930 return error_mark_node
;
4933 tree expr
= cp_parser_cast_expression (parser
, false, false,
4935 if (expr
== error_mark_node
)
4936 return error_mark_node
;
4937 return finish_left_unary_fold_expr (expr
, op
);
4940 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4941 int op
= cp_parser_fold_operator (parser
);
4942 if (op
== ERROR_MARK
)
4944 cp_parser_error (parser
, "expected binary operator");
4945 return error_mark_node
;
4948 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4950 cp_parser_error (parser
, "expected ...");
4951 return error_mark_node
;
4953 cp_lexer_consume_token (parser
->lexer
);
4955 /* The operands of a fold-expression are cast-expressions, so binary or
4956 conditional expressions are not allowed. We check this here to avoid
4957 tentative parsing. */
4958 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4959 /* OK, the expression was parenthesized. */;
4960 else if (is_binary_op (TREE_CODE (expr1
)))
4961 error_at (location_of (expr1
),
4962 "binary expression in operand of fold-expression");
4963 else if (TREE_CODE (expr1
) == COND_EXPR
)
4964 error_at (location_of (expr1
),
4965 "conditional expression in operand of fold-expression");
4968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4969 return finish_right_unary_fold_expr (expr1
, op
);
4971 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4973 cp_parser_error (parser
, "mismatched operator in fold-expression");
4974 return error_mark_node
;
4976 cp_lexer_consume_token (parser
->lexer
);
4978 // Binary left or right fold.
4979 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4980 if (expr2
== error_mark_node
)
4981 return error_mark_node
;
4982 return finish_binary_fold_expr (expr1
, expr2
, op
);
4985 /* Parse a primary-expression.
4992 lambda-expression (C++11)
4997 ( compound-statement )
4998 __builtin_va_arg ( assignment-expression , type-id )
4999 __builtin_offsetof ( type-id , offsetof-expression )
5002 __has_nothrow_assign ( type-id )
5003 __has_nothrow_constructor ( type-id )
5004 __has_nothrow_copy ( type-id )
5005 __has_trivial_assign ( type-id )
5006 __has_trivial_constructor ( type-id )
5007 __has_trivial_copy ( type-id )
5008 __has_trivial_destructor ( type-id )
5009 __has_virtual_destructor ( type-id )
5010 __is_abstract ( type-id )
5011 __is_base_of ( type-id , type-id )
5012 __is_class ( type-id )
5013 __is_empty ( type-id )
5014 __is_enum ( type-id )
5015 __is_final ( type-id )
5016 __is_literal_type ( type-id )
5017 __is_pod ( type-id )
5018 __is_polymorphic ( type-id )
5019 __is_std_layout ( type-id )
5020 __is_trivial ( type-id )
5021 __is_union ( type-id )
5023 Objective-C++ Extension:
5031 ADDRESS_P is true iff this expression was immediately preceded by
5032 "&" and therefore might denote a pointer-to-member. CAST_P is true
5033 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5034 true iff this expression is a template argument.
5036 Returns a representation of the expression. Upon return, *IDK
5037 indicates what kind of id-expression (if any) was present. */
5040 cp_parser_primary_expression (cp_parser
*parser
,
5043 bool template_arg_p
,
5047 cp_token
*token
= NULL
;
5049 /* Assume the primary expression is not an id-expression. */
5050 *idk
= CP_ID_KIND_NONE
;
5052 /* Peek at the next token. */
5053 token
= cp_lexer_peek_token (parser
->lexer
);
5054 switch ((int) token
->type
)
5063 user-defined-literal */
5070 case CPP_PREPARSED_EXPR
:
5071 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5072 return cp_parser_userdef_numeric_literal (parser
);
5073 token
= cp_lexer_consume_token (parser
->lexer
);
5074 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5076 error_at (token
->location
,
5077 "fixed-point types not supported in C++");
5078 return error_mark_node
;
5080 /* Floating-point literals are only allowed in an integral
5081 constant expression if they are cast to an integral or
5082 enumeration type. */
5083 if (TREE_CODE (token
->u
.value
) == REAL_CST
5084 && parser
->integral_constant_expression_p
5087 /* CAST_P will be set even in invalid code like "int(2.7 +
5088 ...)". Therefore, we have to check that the next token
5089 is sure to end the cast. */
5092 cp_token
*next_token
;
5094 next_token
= cp_lexer_peek_token (parser
->lexer
);
5095 if (/* The comma at the end of an
5096 enumerator-definition. */
5097 next_token
->type
!= CPP_COMMA
5098 /* The curly brace at the end of an enum-specifier. */
5099 && next_token
->type
!= CPP_CLOSE_BRACE
5100 /* The end of a statement. */
5101 && next_token
->type
!= CPP_SEMICOLON
5102 /* The end of the cast-expression. */
5103 && next_token
->type
!= CPP_CLOSE_PAREN
5104 /* The end of an array bound. */
5105 && next_token
->type
!= CPP_CLOSE_SQUARE
5106 /* The closing ">" in a template-argument-list. */
5107 && (next_token
->type
!= CPP_GREATER
5108 || parser
->greater_than_is_operator_p
)
5109 /* C++0x only: A ">>" treated like two ">" tokens,
5110 in a template-argument-list. */
5111 && (next_token
->type
!= CPP_RSHIFT
5112 || (cxx_dialect
== cxx98
)
5113 || parser
->greater_than_is_operator_p
))
5117 /* If we are within a cast, then the constraint that the
5118 cast is to an integral or enumeration type will be
5119 checked at that point. If we are not within a cast, then
5120 this code is invalid. */
5122 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5124 return cp_expr (token
->u
.value
, token
->location
);
5126 case CPP_CHAR_USERDEF
:
5127 case CPP_CHAR16_USERDEF
:
5128 case CPP_CHAR32_USERDEF
:
5129 case CPP_WCHAR_USERDEF
:
5130 case CPP_UTF8CHAR_USERDEF
:
5131 return cp_parser_userdef_char_literal (parser
);
5137 case CPP_UTF8STRING
:
5138 case CPP_STRING_USERDEF
:
5139 case CPP_STRING16_USERDEF
:
5140 case CPP_STRING32_USERDEF
:
5141 case CPP_WSTRING_USERDEF
:
5142 case CPP_UTF8STRING_USERDEF
:
5143 /* ??? Should wide strings be allowed when parser->translate_strings_p
5144 is false (i.e. in attributes)? If not, we can kill the third
5145 argument to cp_parser_string_literal. */
5146 return cp_parser_string_literal (parser
,
5147 parser
->translate_strings_p
,
5150 case CPP_OPEN_PAREN
:
5151 /* If we see `( { ' then we are looking at the beginning of
5152 a GNU statement-expression. */
5153 if (cp_parser_allow_gnu_extensions_p (parser
)
5154 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5156 /* Statement-expressions are not allowed by the standard. */
5157 pedwarn (token
->location
, OPT_Wpedantic
,
5158 "ISO C++ forbids braced-groups within expressions");
5160 /* And they're not allowed outside of a function-body; you
5161 cannot, for example, write:
5163 int i = ({ int j = 3; j + 1; });
5165 at class or namespace scope. */
5166 if (!parser
->in_function_body
5167 || parser
->in_template_argument_list_p
)
5169 error_at (token
->location
,
5170 "statement-expressions are not allowed outside "
5171 "functions nor in template-argument lists");
5172 cp_parser_skip_to_end_of_block_or_statement (parser
);
5173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5174 cp_lexer_consume_token (parser
->lexer
);
5175 return error_mark_node
;
5178 return cp_parser_statement_expr (parser
);
5180 /* Otherwise it's a normal parenthesized expression. */
5183 bool saved_greater_than_is_operator_p
;
5185 location_t open_paren_loc
= token
->location
;
5187 /* Consume the `('. */
5188 matching_parens parens
;
5189 parens
.consume_open (parser
);
5190 /* Within a parenthesized expression, a `>' token is always
5191 the greater-than operator. */
5192 saved_greater_than_is_operator_p
5193 = parser
->greater_than_is_operator_p
;
5194 parser
->greater_than_is_operator_p
= true;
5196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5197 /* Left fold expression. */
5200 /* Parse the parenthesized expression. */
5201 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5203 token
= cp_lexer_peek_token (parser
->lexer
);
5204 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5206 expr
= cp_parser_fold_expression (parser
, expr
);
5207 if (expr
!= error_mark_node
5208 && cxx_dialect
< cxx17
5209 && !in_system_header_at (input_location
))
5210 pedwarn (input_location
, 0, "fold-expressions only available "
5211 "with -std=c++17 or -std=gnu++17");
5214 /* Let the front end know that this expression was
5215 enclosed in parentheses. This matters in case, for
5216 example, the expression is of the form `A::B', since
5217 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5219 expr
= finish_parenthesized_expr (expr
);
5221 /* DR 705: Wrapping an unqualified name in parentheses
5222 suppresses arg-dependent lookup. We want to pass back
5223 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5224 (c++/37862), but none of the others. */
5225 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5226 *idk
= CP_ID_KIND_NONE
;
5228 /* The `>' token might be the end of a template-id or
5229 template-parameter-list now. */
5230 parser
->greater_than_is_operator_p
5231 = saved_greater_than_is_operator_p
;
5233 /* Consume the `)'. */
5234 token
= cp_lexer_peek_token (parser
->lexer
);
5235 location_t close_paren_loc
= token
->location
;
5236 expr
.set_range (open_paren_loc
, close_paren_loc
);
5237 if (!parens
.require_close (parser
)
5238 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5239 cp_parser_skip_to_end_of_statement (parser
);
5244 case CPP_OPEN_SQUARE
:
5246 if (c_dialect_objc ())
5248 /* We might have an Objective-C++ message. */
5249 cp_parser_parse_tentatively (parser
);
5250 tree msg
= cp_parser_objc_message_expression (parser
);
5251 /* If that works out, we're done ... */
5252 if (cp_parser_parse_definitely (parser
))
5254 /* ... else, fall though to see if it's a lambda. */
5256 cp_expr lam
= cp_parser_lambda_expression (parser
);
5257 /* Don't warn about a failed tentative parse. */
5258 if (cp_parser_error_occurred (parser
))
5259 return error_mark_node
;
5260 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5264 case CPP_OBJC_STRING
:
5265 if (c_dialect_objc ())
5266 /* We have an Objective-C++ string literal. */
5267 return cp_parser_objc_expression (parser
);
5268 cp_parser_error (parser
, "expected primary-expression");
5269 return error_mark_node
;
5272 switch (token
->keyword
)
5274 /* These two are the boolean literals. */
5276 cp_lexer_consume_token (parser
->lexer
);
5277 return cp_expr (boolean_true_node
, token
->location
);
5279 cp_lexer_consume_token (parser
->lexer
);
5280 return cp_expr (boolean_false_node
, token
->location
);
5282 /* The `__null' literal. */
5284 cp_lexer_consume_token (parser
->lexer
);
5285 return cp_expr (null_node
, token
->location
);
5287 /* The `nullptr' literal. */
5289 cp_lexer_consume_token (parser
->lexer
);
5290 return cp_expr (nullptr_node
, token
->location
);
5292 /* Recognize the `this' keyword. */
5294 cp_lexer_consume_token (parser
->lexer
);
5295 if (parser
->local_variables_forbidden_p
)
5297 error_at (token
->location
,
5298 "%<this%> may not be used in this context");
5299 return error_mark_node
;
5301 /* Pointers cannot appear in constant-expressions. */
5302 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5303 return error_mark_node
;
5304 return cp_expr (finish_this_expr (), token
->location
);
5306 /* The `operator' keyword can be the beginning of an
5311 case RID_FUNCTION_NAME
:
5312 case RID_PRETTY_FUNCTION_NAME
:
5313 case RID_C99_FUNCTION_NAME
:
5315 non_integral_constant name
;
5317 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5318 __func__ are the names of variables -- but they are
5319 treated specially. Therefore, they are handled here,
5320 rather than relying on the generic id-expression logic
5321 below. Grammatically, these names are id-expressions.
5323 Consume the token. */
5324 token
= cp_lexer_consume_token (parser
->lexer
);
5326 switch (token
->keyword
)
5328 case RID_FUNCTION_NAME
:
5329 name
= NIC_FUNC_NAME
;
5331 case RID_PRETTY_FUNCTION_NAME
:
5332 name
= NIC_PRETTY_FUNC
;
5334 case RID_C99_FUNCTION_NAME
:
5335 name
= NIC_C99_FUNC
;
5341 if (cp_parser_non_integral_constant_expression (parser
, name
))
5342 return error_mark_node
;
5344 /* Look up the name. */
5345 return finish_fname (token
->u
.value
);
5352 source_location type_location
;
5353 location_t start_loc
5354 = cp_lexer_peek_token (parser
->lexer
)->location
;
5355 /* The `__builtin_va_arg' construct is used to handle
5356 `va_arg'. Consume the `__builtin_va_arg' token. */
5357 cp_lexer_consume_token (parser
->lexer
);
5358 /* Look for the opening `('. */
5359 matching_parens parens
;
5360 parens
.require_open (parser
);
5361 /* Now, parse the assignment-expression. */
5362 expression
= cp_parser_assignment_expression (parser
);
5363 /* Look for the `,'. */
5364 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5365 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5366 /* Parse the type-id. */
5368 type_id_in_expr_sentinel
s (parser
);
5369 type
= cp_parser_type_id (parser
);
5371 /* Look for the closing `)'. */
5372 location_t finish_loc
5373 = cp_lexer_peek_token (parser
->lexer
)->location
;
5374 parens
.require_close (parser
);
5375 /* Using `va_arg' in a constant-expression is not
5377 if (cp_parser_non_integral_constant_expression (parser
,
5379 return error_mark_node
;
5380 /* Construct a location of the form:
5381 __builtin_va_arg (v, int)
5382 ~~~~~~~~~~~~~~~~~~~~~^~~~
5383 with the caret at the type, ranging from the start of the
5384 "__builtin_va_arg" token to the close paren. */
5385 location_t combined_loc
5386 = make_location (type_location
, start_loc
, finish_loc
);
5387 return build_x_va_arg (combined_loc
, expression
, type
);
5391 return cp_parser_builtin_offsetof (parser
);
5393 case RID_HAS_NOTHROW_ASSIGN
:
5394 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5395 case RID_HAS_NOTHROW_COPY
:
5396 case RID_HAS_TRIVIAL_ASSIGN
:
5397 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5398 case RID_HAS_TRIVIAL_COPY
:
5399 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5400 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5401 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5402 case RID_IS_ABSTRACT
:
5403 case RID_IS_AGGREGATE
:
5404 case RID_IS_BASE_OF
:
5409 case RID_IS_LITERAL_TYPE
:
5411 case RID_IS_POLYMORPHIC
:
5412 case RID_IS_SAME_AS
:
5413 case RID_IS_STD_LAYOUT
:
5414 case RID_IS_TRIVIAL
:
5415 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5416 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5417 case RID_IS_TRIVIALLY_COPYABLE
:
5419 case RID_IS_ASSIGNABLE
:
5420 case RID_IS_CONSTRUCTIBLE
:
5421 return cp_parser_trait_expr (parser
, token
->keyword
);
5425 return cp_parser_requires_expression (parser
);
5427 /* Objective-C++ expressions. */
5429 case RID_AT_PROTOCOL
:
5430 case RID_AT_SELECTOR
:
5431 return cp_parser_objc_expression (parser
);
5434 if (parser
->in_function_body
5435 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5438 error_at (token
->location
,
5439 "a template declaration cannot appear at block scope");
5440 cp_parser_skip_to_end_of_block_or_statement (parser
);
5441 return error_mark_node
;
5445 cp_parser_error (parser
, "expected primary-expression");
5446 return error_mark_node
;
5449 /* An id-expression can start with either an identifier, a
5450 `::' as the beginning of a qualified-id, or the "operator"
5454 case CPP_TEMPLATE_ID
:
5455 case CPP_NESTED_NAME_SPECIFIER
:
5458 cp_expr id_expression
;
5460 const char *error_msg
;
5463 cp_token
*id_expr_token
;
5465 /* Parse the id-expression. */
5467 = cp_parser_id_expression (parser
,
5468 /*template_keyword_p=*/false,
5469 /*check_dependency_p=*/true,
5471 /*declarator_p=*/false,
5472 /*optional_p=*/false);
5473 if (id_expression
== error_mark_node
)
5474 return error_mark_node
;
5475 id_expr_token
= token
;
5476 token
= cp_lexer_peek_token (parser
->lexer
);
5477 done
= (token
->type
!= CPP_OPEN_SQUARE
5478 && token
->type
!= CPP_OPEN_PAREN
5479 && token
->type
!= CPP_DOT
5480 && token
->type
!= CPP_DEREF
5481 && token
->type
!= CPP_PLUS_PLUS
5482 && token
->type
!= CPP_MINUS_MINUS
);
5483 /* If we have a template-id, then no further lookup is
5484 required. If the template-id was for a template-class, we
5485 will sometimes have a TYPE_DECL at this point. */
5486 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5487 || TREE_CODE (id_expression
) == TYPE_DECL
)
5488 decl
= id_expression
;
5489 /* Look up the name. */
5492 tree ambiguous_decls
;
5494 /* If we already know that this lookup is ambiguous, then
5495 we've already issued an error message; there's no reason
5497 if (id_expr_token
->type
== CPP_NAME
5498 && id_expr_token
->error_reported
)
5500 cp_parser_simulate_error (parser
);
5501 return error_mark_node
;
5504 decl
= cp_parser_lookup_name (parser
, id_expression
,
5507 /*is_namespace=*/false,
5508 /*check_dependency=*/true,
5510 id_expr_token
->location
);
5511 /* If the lookup was ambiguous, an error will already have
5513 if (ambiguous_decls
)
5514 return error_mark_node
;
5516 /* In Objective-C++, we may have an Objective-C 2.0
5517 dot-syntax for classes here. */
5518 if (c_dialect_objc ()
5519 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5520 && TREE_CODE (decl
) == TYPE_DECL
5521 && objc_is_class_name (decl
))
5524 cp_lexer_consume_token (parser
->lexer
);
5525 component
= cp_parser_identifier (parser
);
5526 if (component
== error_mark_node
)
5527 return error_mark_node
;
5529 tree result
= objc_build_class_component_ref (id_expression
,
5531 /* Build a location of the form:
5534 with caret at the start of the component name (at
5535 input_location), ranging from the start of the id_expression
5536 to the end of the component name. */
5537 location_t combined_loc
5538 = make_location (input_location
, id_expression
.get_start (),
5539 get_finish (input_location
));
5540 protected_set_expr_location (result
, combined_loc
);
5544 /* In Objective-C++, an instance variable (ivar) may be preferred
5545 to whatever cp_parser_lookup_name() found.
5546 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5547 rest of c-family, we have to do a little extra work to preserve
5548 any location information in cp_expr "decl". Given that
5549 objc_lookup_ivar is implemented in "c-family" and "objc", we
5550 have a trip through the pure "tree" type, rather than cp_expr.
5551 Naively copying it back to "decl" would implicitly give the
5552 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5553 store an EXPR_LOCATION. Hence we only update "decl" (and
5554 hence its location_t) if we get back a different tree node. */
5555 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5557 if (decl_tree
!= decl
.get_value ())
5558 decl
= cp_expr (decl_tree
);
5560 /* If name lookup gives us a SCOPE_REF, then the
5561 qualifying scope was dependent. */
5562 if (TREE_CODE (decl
) == SCOPE_REF
)
5564 /* At this point, we do not know if DECL is a valid
5565 integral constant expression. We assume that it is
5566 in fact such an expression, so that code like:
5568 template <int N> struct A {
5572 is accepted. At template-instantiation time, we
5573 will check that B<N>::i is actually a constant. */
5576 /* Check to see if DECL is a local variable in a context
5577 where that is forbidden. */
5578 if (parser
->local_variables_forbidden_p
5579 && local_variable_p (decl
))
5581 /* It might be that we only found DECL because we are
5582 trying to be generous with pre-ISO scoping rules.
5583 For example, consider:
5587 for (int i = 0; i < 10; ++i) {}
5588 extern void f(int j = i);
5591 Here, name look up will originally find the out
5592 of scope `i'. We need to issue a warning message,
5593 but then use the global `i'. */
5594 decl
= check_for_out_of_scope_variable (decl
);
5595 if (local_variable_p (decl
))
5597 error_at (id_expr_token
->location
,
5598 "local variable %qD may not appear in this context",
5600 return error_mark_node
;
5605 decl
= (finish_id_expression
5606 (id_expression
, decl
, parser
->scope
,
5608 parser
->integral_constant_expression_p
,
5609 parser
->allow_non_integral_constant_expression_p
,
5610 &parser
->non_integral_constant_expression_p
,
5611 template_p
, done
, address_p
,
5614 id_expression
.get_location ()));
5616 cp_parser_error (parser
, error_msg
);
5617 decl
.set_location (id_expr_token
->location
);
5621 /* Anything else is an error. */
5623 cp_parser_error (parser
, "expected primary-expression");
5624 return error_mark_node
;
5628 static inline cp_expr
5629 cp_parser_primary_expression (cp_parser
*parser
,
5632 bool template_arg_p
,
5635 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5636 /*decltype*/false, idk
);
5639 /* Parse an id-expression.
5646 :: [opt] nested-name-specifier template [opt] unqualified-id
5648 :: operator-function-id
5651 Return a representation of the unqualified portion of the
5652 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5653 a `::' or nested-name-specifier.
5655 Often, if the id-expression was a qualified-id, the caller will
5656 want to make a SCOPE_REF to represent the qualified-id. This
5657 function does not do this in order to avoid wastefully creating
5658 SCOPE_REFs when they are not required.
5660 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5663 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5664 uninstantiated templates.
5666 If *TEMPLATE_P is non-NULL, it is set to true iff the
5667 `template' keyword is used to explicitly indicate that the entity
5668 named is a template.
5670 If DECLARATOR_P is true, the id-expression is appearing as part of
5671 a declarator, rather than as part of an expression. */
5674 cp_parser_id_expression (cp_parser
*parser
,
5675 bool template_keyword_p
,
5676 bool check_dependency_p
,
5681 bool global_scope_p
;
5682 bool nested_name_specifier_p
;
5684 /* Assume the `template' keyword was not used. */
5686 *template_p
= template_keyword_p
;
5688 /* Look for the optional `::' operator. */
5690 = (!template_keyword_p
5691 && (cp_parser_global_scope_opt (parser
,
5692 /*current_scope_valid_p=*/false)
5695 /* Look for the optional nested-name-specifier. */
5696 nested_name_specifier_p
5697 = (cp_parser_nested_name_specifier_opt (parser
,
5698 /*typename_keyword_p=*/false,
5705 /* If there is a nested-name-specifier, then we are looking at
5706 the first qualified-id production. */
5707 if (nested_name_specifier_p
)
5710 tree saved_object_scope
;
5711 tree saved_qualifying_scope
;
5712 cp_expr unqualified_id
;
5715 /* See if the next token is the `template' keyword. */
5717 template_p
= &is_template
;
5718 *template_p
= cp_parser_optional_template_keyword (parser
);
5719 /* Name lookup we do during the processing of the
5720 unqualified-id might obliterate SCOPE. */
5721 saved_scope
= parser
->scope
;
5722 saved_object_scope
= parser
->object_scope
;
5723 saved_qualifying_scope
= parser
->qualifying_scope
;
5724 /* Process the final unqualified-id. */
5725 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5728 /*optional_p=*/false);
5729 /* Restore the SAVED_SCOPE for our caller. */
5730 parser
->scope
= saved_scope
;
5731 parser
->object_scope
= saved_object_scope
;
5732 parser
->qualifying_scope
= saved_qualifying_scope
;
5734 return unqualified_id
;
5736 /* Otherwise, if we are in global scope, then we are looking at one
5737 of the other qualified-id productions. */
5738 else if (global_scope_p
)
5743 /* Peek at the next token. */
5744 token
= cp_lexer_peek_token (parser
->lexer
);
5746 /* If it's an identifier, and the next token is not a "<", then
5747 we can avoid the template-id case. This is an optimization
5748 for this common case. */
5749 if (token
->type
== CPP_NAME
5750 && !cp_parser_nth_token_starts_template_argument_list_p
5752 return cp_parser_identifier (parser
);
5754 cp_parser_parse_tentatively (parser
);
5755 /* Try a template-id. */
5756 id
= cp_parser_template_id (parser
,
5757 /*template_keyword_p=*/false,
5758 /*check_dependency_p=*/true,
5761 /* If that worked, we're done. */
5762 if (cp_parser_parse_definitely (parser
))
5765 /* Peek at the next token. (Changes in the token buffer may
5766 have invalidated the pointer obtained above.) */
5767 token
= cp_lexer_peek_token (parser
->lexer
);
5769 switch (token
->type
)
5772 return cp_parser_identifier (parser
);
5775 if (token
->keyword
== RID_OPERATOR
)
5776 return cp_parser_operator_function_id (parser
);
5780 cp_parser_error (parser
, "expected id-expression");
5781 return error_mark_node
;
5785 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5786 /*check_dependency_p=*/true,
5791 /* Parse an unqualified-id.
5795 operator-function-id
5796 conversion-function-id
5800 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5801 keyword, in a construct like `A::template ...'.
5803 Returns a representation of unqualified-id. For the `identifier'
5804 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5805 production a BIT_NOT_EXPR is returned; the operand of the
5806 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5807 other productions, see the documentation accompanying the
5808 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5809 names are looked up in uninstantiated templates. If DECLARATOR_P
5810 is true, the unqualified-id is appearing as part of a declarator,
5811 rather than as part of an expression. */
5814 cp_parser_unqualified_id (cp_parser
* parser
,
5815 bool template_keyword_p
,
5816 bool check_dependency_p
,
5822 /* Peek at the next token. */
5823 token
= cp_lexer_peek_token (parser
->lexer
);
5825 switch ((int) token
->type
)
5831 /* We don't know yet whether or not this will be a
5833 cp_parser_parse_tentatively (parser
);
5834 /* Try a template-id. */
5835 id
= cp_parser_template_id (parser
, template_keyword_p
,
5839 /* If it worked, we're done. */
5840 if (cp_parser_parse_definitely (parser
))
5842 /* Otherwise, it's an ordinary identifier. */
5843 return cp_parser_identifier (parser
);
5846 case CPP_TEMPLATE_ID
:
5847 return cp_parser_template_id (parser
, template_keyword_p
,
5855 tree qualifying_scope
;
5860 /* Consume the `~' token. */
5861 cp_lexer_consume_token (parser
->lexer
);
5862 /* Parse the class-name. The standard, as written, seems to
5865 template <typename T> struct S { ~S (); };
5866 template <typename T> S<T>::~S() {}
5868 is invalid, since `~' must be followed by a class-name, but
5869 `S<T>' is dependent, and so not known to be a class.
5870 That's not right; we need to look in uninstantiated
5871 templates. A further complication arises from:
5873 template <typename T> void f(T t) {
5877 Here, it is not possible to look up `T' in the scope of `T'
5878 itself. We must look in both the current scope, and the
5879 scope of the containing complete expression.
5881 Yet another issue is:
5890 The standard does not seem to say that the `S' in `~S'
5891 should refer to the type `S' and not the data member
5894 /* DR 244 says that we look up the name after the "~" in the
5895 same scope as we looked up the qualifying name. That idea
5896 isn't fully worked out; it's more complicated than that. */
5897 scope
= parser
->scope
;
5898 object_scope
= parser
->object_scope
;
5899 qualifying_scope
= parser
->qualifying_scope
;
5901 /* Check for invalid scopes. */
5902 if (scope
== error_mark_node
)
5904 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5905 cp_lexer_consume_token (parser
->lexer
);
5906 return error_mark_node
;
5908 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5910 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5911 error_at (token
->location
,
5912 "scope %qT before %<~%> is not a class-name",
5914 cp_parser_simulate_error (parser
);
5915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5916 cp_lexer_consume_token (parser
->lexer
);
5917 return error_mark_node
;
5919 gcc_assert (!scope
|| TYPE_P (scope
));
5921 /* If the name is of the form "X::~X" it's OK even if X is a
5923 token
= cp_lexer_peek_token (parser
->lexer
);
5925 && token
->type
== CPP_NAME
5926 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5928 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5929 || (CLASS_TYPE_P (scope
)
5930 && constructor_name_p (token
->u
.value
, scope
))))
5932 cp_lexer_consume_token (parser
->lexer
);
5933 return build_nt (BIT_NOT_EXPR
, scope
);
5936 /* ~auto means the destructor of whatever the object is. */
5937 if (cp_parser_is_keyword (token
, RID_AUTO
))
5939 if (cxx_dialect
< cxx14
)
5940 pedwarn (input_location
, 0,
5941 "%<~auto%> only available with "
5942 "-std=c++14 or -std=gnu++14");
5943 cp_lexer_consume_token (parser
->lexer
);
5944 return build_nt (BIT_NOT_EXPR
, make_auto ());
5947 /* If there was an explicit qualification (S::~T), first look
5948 in the scope given by the qualification (i.e., S).
5950 Note: in the calls to cp_parser_class_name below we pass
5951 typename_type so that lookup finds the injected-class-name
5952 rather than the constructor. */
5954 type_decl
= NULL_TREE
;
5957 cp_parser_parse_tentatively (parser
);
5958 type_decl
= cp_parser_class_name (parser
,
5959 /*typename_keyword_p=*/false,
5960 /*template_keyword_p=*/false,
5962 /*check_dependency=*/false,
5963 /*class_head_p=*/false,
5965 if (cp_parser_parse_definitely (parser
))
5968 /* In "N::S::~S", look in "N" as well. */
5969 if (!done
&& scope
&& qualifying_scope
)
5971 cp_parser_parse_tentatively (parser
);
5972 parser
->scope
= qualifying_scope
;
5973 parser
->object_scope
= NULL_TREE
;
5974 parser
->qualifying_scope
= NULL_TREE
;
5976 = cp_parser_class_name (parser
,
5977 /*typename_keyword_p=*/false,
5978 /*template_keyword_p=*/false,
5980 /*check_dependency=*/false,
5981 /*class_head_p=*/false,
5983 if (cp_parser_parse_definitely (parser
))
5986 /* In "p->S::~T", look in the scope given by "*p" as well. */
5987 else if (!done
&& object_scope
)
5989 cp_parser_parse_tentatively (parser
);
5990 parser
->scope
= object_scope
;
5991 parser
->object_scope
= NULL_TREE
;
5992 parser
->qualifying_scope
= NULL_TREE
;
5994 = cp_parser_class_name (parser
,
5995 /*typename_keyword_p=*/false,
5996 /*template_keyword_p=*/false,
5998 /*check_dependency=*/false,
5999 /*class_head_p=*/false,
6001 if (cp_parser_parse_definitely (parser
))
6004 /* Look in the surrounding context. */
6007 parser
->scope
= NULL_TREE
;
6008 parser
->object_scope
= NULL_TREE
;
6009 parser
->qualifying_scope
= NULL_TREE
;
6010 if (processing_template_decl
)
6011 cp_parser_parse_tentatively (parser
);
6013 = cp_parser_class_name (parser
,
6014 /*typename_keyword_p=*/false,
6015 /*template_keyword_p=*/false,
6017 /*check_dependency=*/false,
6018 /*class_head_p=*/false,
6020 if (processing_template_decl
6021 && ! cp_parser_parse_definitely (parser
))
6023 /* We couldn't find a type with this name. If we're parsing
6024 tentatively, fail and try something else. */
6025 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6027 cp_parser_simulate_error (parser
);
6028 return error_mark_node
;
6030 /* Otherwise, accept it and check for a match at instantiation
6032 type_decl
= cp_parser_identifier (parser
);
6033 if (type_decl
!= error_mark_node
)
6034 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6038 /* If an error occurred, assume that the name of the
6039 destructor is the same as the name of the qualifying
6040 class. That allows us to keep parsing after running
6041 into ill-formed destructor names. */
6042 if (type_decl
== error_mark_node
&& scope
)
6043 return build_nt (BIT_NOT_EXPR
, scope
);
6044 else if (type_decl
== error_mark_node
)
6045 return error_mark_node
;
6047 /* Check that destructor name and scope match. */
6048 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6051 error_at (token
->location
,
6052 "declaration of %<~%T%> as member of %qT",
6054 cp_parser_simulate_error (parser
);
6055 return error_mark_node
;
6060 A typedef-name that names a class shall not be used as the
6061 identifier in the declarator for a destructor declaration. */
6063 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6064 && !DECL_SELF_REFERENCE_P (type_decl
)
6065 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6066 error_at (token
->location
,
6067 "typedef-name %qD used as destructor declarator",
6070 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6074 if (token
->keyword
== RID_OPERATOR
)
6078 /* This could be a template-id, so we try that first. */
6079 cp_parser_parse_tentatively (parser
);
6080 /* Try a template-id. */
6081 id
= cp_parser_template_id (parser
, template_keyword_p
,
6082 /*check_dependency_p=*/true,
6085 /* If that worked, we're done. */
6086 if (cp_parser_parse_definitely (parser
))
6088 /* We still don't know whether we're looking at an
6089 operator-function-id or a conversion-function-id. */
6090 cp_parser_parse_tentatively (parser
);
6091 /* Try an operator-function-id. */
6092 id
= cp_parser_operator_function_id (parser
);
6093 /* If that didn't work, try a conversion-function-id. */
6094 if (!cp_parser_parse_definitely (parser
))
6095 id
= cp_parser_conversion_function_id (parser
);
6096 else if (UDLIT_OPER_P (id
))
6099 const char *name
= UDLIT_OP_SUFFIX (id
);
6100 if (name
[0] != '_' && !in_system_header_at (input_location
)
6102 warning (OPT_Wliteral_suffix
,
6103 "literal operator suffixes not preceded by %<_%>"
6104 " are reserved for future standardization");
6114 cp_parser_error (parser
, "expected unqualified-id");
6115 return error_mark_node
;
6119 /* Parse an (optional) nested-name-specifier.
6121 nested-name-specifier: [C++98]
6122 class-or-namespace-name :: nested-name-specifier [opt]
6123 class-or-namespace-name :: template nested-name-specifier [opt]
6125 nested-name-specifier: [C++0x]
6128 nested-name-specifier identifier ::
6129 nested-name-specifier template [opt] simple-template-id ::
6131 PARSER->SCOPE should be set appropriately before this function is
6132 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6133 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6136 Sets PARSER->SCOPE to the class (TYPE) or namespace
6137 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6138 it unchanged if there is no nested-name-specifier. Returns the new
6139 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6141 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6142 part of a declaration and/or decl-specifier. */
6145 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6146 bool typename_keyword_p
,
6147 bool check_dependency_p
,
6149 bool is_declaration
,
6150 bool template_keyword_p
/* = false */)
6152 bool success
= false;
6153 cp_token_position start
= 0;
6156 /* Remember where the nested-name-specifier starts. */
6157 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6159 start
= cp_lexer_token_position (parser
->lexer
, false);
6160 push_deferring_access_checks (dk_deferred
);
6167 tree saved_qualifying_scope
;
6169 /* Spot cases that cannot be the beginning of a
6170 nested-name-specifier. */
6171 token
= cp_lexer_peek_token (parser
->lexer
);
6173 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6174 the already parsed nested-name-specifier. */
6175 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6177 /* Grab the nested-name-specifier and continue the loop. */
6178 cp_parser_pre_parsed_nested_name_specifier (parser
);
6179 /* If we originally encountered this nested-name-specifier
6180 with IS_DECLARATION set to false, we will not have
6181 resolved TYPENAME_TYPEs, so we must do so here. */
6183 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6185 new_scope
= resolve_typename_type (parser
->scope
,
6186 /*only_current_p=*/false);
6187 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6188 parser
->scope
= new_scope
;
6194 /* Spot cases that cannot be the beginning of a
6195 nested-name-specifier. On the second and subsequent times
6196 through the loop, we look for the `template' keyword. */
6197 if (success
&& token
->keyword
== RID_TEMPLATE
)
6199 /* A template-id can start a nested-name-specifier. */
6200 else if (token
->type
== CPP_TEMPLATE_ID
)
6202 /* DR 743: decltype can be used in a nested-name-specifier. */
6203 else if (token_is_decltype (token
))
6207 /* If the next token is not an identifier, then it is
6208 definitely not a type-name or namespace-name. */
6209 if (token
->type
!= CPP_NAME
)
6211 /* If the following token is neither a `<' (to begin a
6212 template-id), nor a `::', then we are not looking at a
6213 nested-name-specifier. */
6214 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6216 if (token
->type
== CPP_COLON
6217 && parser
->colon_corrects_to_scope_p
6218 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6220 gcc_rich_location
richloc (token
->location
);
6221 richloc
.add_fixit_replace ("::");
6223 "found %<:%> in nested-name-specifier, "
6225 token
->type
= CPP_SCOPE
;
6228 if (token
->type
!= CPP_SCOPE
6229 && !cp_parser_nth_token_starts_template_argument_list_p
6234 /* The nested-name-specifier is optional, so we parse
6236 cp_parser_parse_tentatively (parser
);
6238 /* Look for the optional `template' keyword, if this isn't the
6239 first time through the loop. */
6241 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6243 /* Save the old scope since the name lookup we are about to do
6244 might destroy it. */
6245 old_scope
= parser
->scope
;
6246 saved_qualifying_scope
= parser
->qualifying_scope
;
6247 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6248 look up names in "X<T>::I" in order to determine that "Y" is
6249 a template. So, if we have a typename at this point, we make
6250 an effort to look through it. */
6252 && !typename_keyword_p
6254 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6255 parser
->scope
= resolve_typename_type (parser
->scope
,
6256 /*only_current_p=*/false);
6257 /* Parse the qualifying entity. */
6259 = cp_parser_qualifying_entity (parser
,
6265 /* Look for the `::' token. */
6266 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6268 /* If we found what we wanted, we keep going; otherwise, we're
6270 if (!cp_parser_parse_definitely (parser
))
6272 bool error_p
= false;
6274 /* Restore the OLD_SCOPE since it was valid before the
6275 failed attempt at finding the last
6276 class-or-namespace-name. */
6277 parser
->scope
= old_scope
;
6278 parser
->qualifying_scope
= saved_qualifying_scope
;
6280 /* If the next token is a decltype, and the one after that is a
6281 `::', then the decltype has failed to resolve to a class or
6282 enumeration type. Give this error even when parsing
6283 tentatively since it can't possibly be valid--and we're going
6284 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6285 won't get another chance.*/
6286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6287 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6290 token
= cp_lexer_consume_token (parser
->lexer
);
6291 error_at (token
->location
, "decltype evaluates to %qT, "
6292 "which is not a class or enumeration type",
6293 token
->u
.tree_check_value
->value
);
6294 parser
->scope
= error_mark_node
;
6298 cp_lexer_consume_token (parser
->lexer
);
6301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6302 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6304 /* If we have a non-type template-id followed by ::, it can't
6305 possibly be valid. */
6306 token
= cp_lexer_peek_token (parser
->lexer
);
6307 tree tid
= token
->u
.tree_check_value
->value
;
6308 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6309 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6311 tree tmpl
= NULL_TREE
;
6312 if (is_overloaded_fn (tid
))
6314 tree fns
= get_fns (tid
);
6315 if (OVL_SINGLE_P (fns
))
6316 tmpl
= OVL_FIRST (fns
);
6317 error_at (token
->location
, "function template-id %qD "
6318 "in nested-name-specifier", tid
);
6322 /* Variable template. */
6323 tmpl
= TREE_OPERAND (tid
, 0);
6324 gcc_assert (variable_template_p (tmpl
));
6325 error_at (token
->location
, "variable template-id %qD "
6326 "in nested-name-specifier", tid
);
6329 inform (DECL_SOURCE_LOCATION (tmpl
),
6330 "%qD declared here", tmpl
);
6332 parser
->scope
= error_mark_node
;
6336 cp_lexer_consume_token (parser
->lexer
);
6337 cp_lexer_consume_token (parser
->lexer
);
6341 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6343 /* If the next token is an identifier, and the one after
6344 that is a `::', then any valid interpretation would have
6345 found a class-or-namespace-name. */
6346 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6347 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6349 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6352 token
= cp_lexer_consume_token (parser
->lexer
);
6355 if (!token
->error_reported
)
6358 tree ambiguous_decls
;
6360 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6362 /*is_template=*/false,
6363 /*is_namespace=*/false,
6364 /*check_dependency=*/true,
6367 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6368 error_at (token
->location
,
6369 "%qD used without template parameters",
6371 else if (ambiguous_decls
)
6373 // cp_parser_lookup_name has the same diagnostic,
6374 // thus make sure to emit it at most once.
6375 if (cp_parser_uncommitted_to_tentative_parse_p
6378 error_at (token
->location
,
6379 "reference to %qD is ambiguous",
6381 print_candidates (ambiguous_decls
);
6383 decl
= error_mark_node
;
6387 if (cxx_dialect
!= cxx98
)
6388 cp_parser_name_lookup_error
6389 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6392 cp_parser_name_lookup_error
6393 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6397 parser
->scope
= error_mark_node
;
6399 /* Treat this as a successful nested-name-specifier
6404 If the name found is not a class-name (clause
6405 _class_) or namespace-name (_namespace.def_), the
6406 program is ill-formed. */
6409 cp_lexer_consume_token (parser
->lexer
);
6413 /* We've found one valid nested-name-specifier. */
6415 /* Name lookup always gives us a DECL. */
6416 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6417 new_scope
= TREE_TYPE (new_scope
);
6418 /* Uses of "template" must be followed by actual templates. */
6419 if (template_keyword_p
6420 && !(CLASS_TYPE_P (new_scope
)
6421 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6422 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6423 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6424 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6425 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6426 == TEMPLATE_ID_EXPR
)))
6427 permerror (input_location
, TYPE_P (new_scope
)
6428 ? G_("%qT is not a template")
6429 : G_("%qD is not a template"),
6431 /* If it is a class scope, try to complete it; we are about to
6432 be looking up names inside the class. */
6433 if (TYPE_P (new_scope
)
6434 /* Since checking types for dependency can be expensive,
6435 avoid doing it if the type is already complete. */
6436 && !COMPLETE_TYPE_P (new_scope
)
6437 /* Do not try to complete dependent types. */
6438 && !dependent_type_p (new_scope
))
6440 new_scope
= complete_type (new_scope
);
6441 /* If it is a typedef to current class, use the current
6442 class instead, as the typedef won't have any names inside
6444 if (!COMPLETE_TYPE_P (new_scope
)
6445 && currently_open_class (new_scope
))
6446 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6448 /* Make sure we look in the right scope the next time through
6450 parser
->scope
= new_scope
;
6453 /* If parsing tentatively, replace the sequence of tokens that makes
6454 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6455 token. That way, should we re-parse the token stream, we will
6456 not have to repeat the effort required to do the parse, nor will
6457 we issue duplicate error messages. */
6458 if (success
&& start
)
6462 token
= cp_lexer_token_at (parser
->lexer
, start
);
6463 /* Reset the contents of the START token. */
6464 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6465 /* Retrieve any deferred checks. Do not pop this access checks yet
6466 so the memory will not be reclaimed during token replacing below. */
6467 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6468 token
->u
.tree_check_value
->value
= parser
->scope
;
6469 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6470 token
->u
.tree_check_value
->qualifying_scope
=
6471 parser
->qualifying_scope
;
6472 token
->keyword
= RID_MAX
;
6474 /* Purge all subsequent tokens. */
6475 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6479 pop_to_parent_deferring_access_checks ();
6481 return success
? parser
->scope
: NULL_TREE
;
6484 /* Parse a nested-name-specifier. See
6485 cp_parser_nested_name_specifier_opt for details. This function
6486 behaves identically, except that it will an issue an error if no
6487 nested-name-specifier is present. */
6490 cp_parser_nested_name_specifier (cp_parser
*parser
,
6491 bool typename_keyword_p
,
6492 bool check_dependency_p
,
6494 bool is_declaration
)
6498 /* Look for the nested-name-specifier. */
6499 scope
= cp_parser_nested_name_specifier_opt (parser
,
6504 /* If it was not present, issue an error message. */
6507 cp_parser_error (parser
, "expected nested-name-specifier");
6508 parser
->scope
= NULL_TREE
;
6514 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6515 this is either a class-name or a namespace-name (which corresponds
6516 to the class-or-namespace-name production in the grammar). For
6517 C++0x, it can also be a type-name that refers to an enumeration
6518 type or a simple-template-id.
6520 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6521 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6522 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6523 TYPE_P is TRUE iff the next name should be taken as a class-name,
6524 even the same name is declared to be another entity in the same
6527 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6528 specified by the class-or-namespace-name. If neither is found the
6529 ERROR_MARK_NODE is returned. */
6532 cp_parser_qualifying_entity (cp_parser
*parser
,
6533 bool typename_keyword_p
,
6534 bool template_keyword_p
,
6535 bool check_dependency_p
,
6537 bool is_declaration
)
6540 tree saved_qualifying_scope
;
6541 tree saved_object_scope
;
6544 bool successful_parse_p
;
6546 /* DR 743: decltype can appear in a nested-name-specifier. */
6547 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6549 scope
= cp_parser_decltype (parser
);
6550 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6551 && !MAYBE_CLASS_TYPE_P (scope
))
6553 cp_parser_simulate_error (parser
);
6554 return error_mark_node
;
6556 if (TYPE_NAME (scope
))
6557 scope
= TYPE_NAME (scope
);
6561 /* Before we try to parse the class-name, we must save away the
6562 current PARSER->SCOPE since cp_parser_class_name will destroy
6564 saved_scope
= parser
->scope
;
6565 saved_qualifying_scope
= parser
->qualifying_scope
;
6566 saved_object_scope
= parser
->object_scope
;
6567 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6568 there is no need to look for a namespace-name. */
6569 only_class_p
= template_keyword_p
6570 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6572 cp_parser_parse_tentatively (parser
);
6573 scope
= cp_parser_class_name (parser
,
6576 type_p
? class_type
: none_type
,
6578 /*class_head_p=*/false,
6580 /*enum_ok=*/cxx_dialect
> cxx98
);
6581 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6582 /* If that didn't work, try for a namespace-name. */
6583 if (!only_class_p
&& !successful_parse_p
)
6585 /* Restore the saved scope. */
6586 parser
->scope
= saved_scope
;
6587 parser
->qualifying_scope
= saved_qualifying_scope
;
6588 parser
->object_scope
= saved_object_scope
;
6589 /* If we are not looking at an identifier followed by the scope
6590 resolution operator, then this is not part of a
6591 nested-name-specifier. (Note that this function is only used
6592 to parse the components of a nested-name-specifier.) */
6593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6594 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6595 return error_mark_node
;
6596 scope
= cp_parser_namespace_name (parser
);
6602 /* Return true if we are looking at a compound-literal, false otherwise. */
6605 cp_parser_compound_literal_p (cp_parser
*parser
)
6607 cp_lexer_save_tokens (parser
->lexer
);
6609 /* Skip tokens until the next token is a closing parenthesis.
6610 If we find the closing `)', and the next token is a `{', then
6611 we are looking at a compound-literal. */
6612 bool compound_literal_p
6613 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6614 /*consume_paren=*/true)
6615 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6617 /* Roll back the tokens we skipped. */
6618 cp_lexer_rollback_tokens (parser
->lexer
);
6620 return compound_literal_p
;
6623 /* Parse a postfix-expression.
6627 postfix-expression [ expression ]
6628 postfix-expression ( expression-list [opt] )
6629 simple-type-specifier ( expression-list [opt] )
6630 typename :: [opt] nested-name-specifier identifier
6631 ( expression-list [opt] )
6632 typename :: [opt] nested-name-specifier template [opt] template-id
6633 ( expression-list [opt] )
6634 postfix-expression . template [opt] id-expression
6635 postfix-expression -> template [opt] id-expression
6636 postfix-expression . pseudo-destructor-name
6637 postfix-expression -> pseudo-destructor-name
6638 postfix-expression ++
6639 postfix-expression --
6640 dynamic_cast < type-id > ( expression )
6641 static_cast < type-id > ( expression )
6642 reinterpret_cast < type-id > ( expression )
6643 const_cast < type-id > ( expression )
6644 typeid ( expression )
6650 ( type-id ) { initializer-list , [opt] }
6652 This extension is a GNU version of the C99 compound-literal
6653 construct. (The C99 grammar uses `type-name' instead of `type-id',
6654 but they are essentially the same concept.)
6656 If ADDRESS_P is true, the postfix expression is the operand of the
6657 `&' operator. CAST_P is true if this expression is the target of a
6660 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6661 class member access expressions [expr.ref].
6663 Returns a representation of the expression. */
6666 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6667 bool member_access_only_p
, bool decltype_p
,
6668 cp_id_kind
* pidk_return
)
6673 cp_id_kind idk
= CP_ID_KIND_NONE
;
6674 cp_expr postfix_expression
= NULL_TREE
;
6675 bool is_member_access
= false;
6677 /* Peek at the next token. */
6678 token
= cp_lexer_peek_token (parser
->lexer
);
6679 loc
= token
->location
;
6680 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6682 /* Some of the productions are determined by keywords. */
6683 keyword
= token
->keyword
;
6693 const char *saved_message
;
6694 bool saved_in_type_id_in_expr_p
;
6696 /* All of these can be handled in the same way from the point
6697 of view of parsing. Begin by consuming the token
6698 identifying the cast. */
6699 cp_lexer_consume_token (parser
->lexer
);
6701 /* New types cannot be defined in the cast. */
6702 saved_message
= parser
->type_definition_forbidden_message
;
6703 parser
->type_definition_forbidden_message
6704 = G_("types may not be defined in casts");
6706 /* Look for the opening `<'. */
6707 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6708 /* Parse the type to which we are casting. */
6709 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6710 parser
->in_type_id_in_expr_p
= true;
6711 type
= cp_parser_type_id (parser
);
6712 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6713 /* Look for the closing `>'. */
6714 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6715 /* Restore the old message. */
6716 parser
->type_definition_forbidden_message
= saved_message
;
6718 bool saved_greater_than_is_operator_p
6719 = parser
->greater_than_is_operator_p
;
6720 parser
->greater_than_is_operator_p
= true;
6722 /* And the expression which is being cast. */
6723 matching_parens parens
;
6724 parens
.require_open (parser
);
6725 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6726 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6728 location_t end_loc
= close_paren
?
6729 close_paren
->location
: UNKNOWN_LOCATION
;
6731 parser
->greater_than_is_operator_p
6732 = saved_greater_than_is_operator_p
;
6734 /* Only type conversions to integral or enumeration types
6735 can be used in constant-expressions. */
6736 if (!cast_valid_in_integral_constant_expression_p (type
)
6737 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6739 postfix_expression
= error_mark_node
;
6747 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6751 = build_static_cast (type
, expression
, tf_warning_or_error
);
6755 = build_reinterpret_cast (type
, expression
,
6756 tf_warning_or_error
);
6760 = build_const_cast (type
, expression
, tf_warning_or_error
);
6766 /* Construct a location e.g. :
6767 reinterpret_cast <int *> (expr)
6768 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6769 ranging from the start of the "*_cast" token to the final closing
6770 paren, with the caret at the start. */
6771 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6772 postfix_expression
.set_location (cp_cast_loc
);
6779 const char *saved_message
;
6780 bool saved_in_type_id_in_expr_p
;
6782 /* Consume the `typeid' token. */
6783 cp_lexer_consume_token (parser
->lexer
);
6784 /* Look for the `(' token. */
6785 matching_parens parens
;
6786 parens
.require_open (parser
);
6787 /* Types cannot be defined in a `typeid' expression. */
6788 saved_message
= parser
->type_definition_forbidden_message
;
6789 parser
->type_definition_forbidden_message
6790 = G_("types may not be defined in a %<typeid%> expression");
6791 /* We can't be sure yet whether we're looking at a type-id or an
6793 cp_parser_parse_tentatively (parser
);
6794 /* Try a type-id first. */
6795 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6796 parser
->in_type_id_in_expr_p
= true;
6797 type
= cp_parser_type_id (parser
);
6798 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6799 /* Look for the `)' token. Otherwise, we can't be sure that
6800 we're not looking at an expression: consider `typeid (int
6801 (3))', for example. */
6802 cp_token
*close_paren
= parens
.require_close (parser
);
6803 /* If all went well, simply lookup the type-id. */
6804 if (cp_parser_parse_definitely (parser
))
6805 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6806 /* Otherwise, fall back to the expression variant. */
6811 /* Look for an expression. */
6812 expression
= cp_parser_expression (parser
, & idk
);
6813 /* Compute its typeid. */
6814 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6815 /* Look for the `)' token. */
6816 close_paren
= parens
.require_close (parser
);
6818 /* Restore the saved message. */
6819 parser
->type_definition_forbidden_message
= saved_message
;
6820 /* `typeid' may not appear in an integral constant expression. */
6821 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6822 postfix_expression
= error_mark_node
;
6824 /* Construct a location e.g. :
6827 ranging from the start of the "typeid" token to the final closing
6828 paren, with the caret at the start. */
6831 location_t typeid_loc
6832 = make_location (start_loc
, start_loc
, close_paren
->location
);
6833 postfix_expression
.set_location (typeid_loc
);
6841 /* The syntax permitted here is the same permitted for an
6842 elaborated-type-specifier. */
6843 ++parser
->prevent_constrained_type_specifiers
;
6844 type
= cp_parser_elaborated_type_specifier (parser
,
6845 /*is_friend=*/false,
6846 /*is_declaration=*/false);
6847 --parser
->prevent_constrained_type_specifiers
;
6848 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6853 case RID_BUILTIN_SHUFFLE
:
6854 case RID_BUILTIN_LAUNDER
:
6856 vec
<tree
, va_gc
> *vec
;
6860 cp_lexer_consume_token (parser
->lexer
);
6861 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6862 /*cast_p=*/false, /*allow_expansion_p=*/true,
6863 /*non_constant_p=*/NULL
);
6866 postfix_expression
= error_mark_node
;
6870 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6876 if (vec
->length () == 1)
6878 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6881 error_at (loc
, "wrong number of arguments to "
6882 "%<__builtin_addressof%>");
6883 postfix_expression
= error_mark_node
;
6887 case RID_BUILTIN_LAUNDER
:
6888 if (vec
->length () == 1)
6889 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6890 tf_warning_or_error
);
6893 error_at (loc
, "wrong number of arguments to "
6894 "%<__builtin_launder%>");
6895 postfix_expression
= error_mark_node
;
6899 case RID_BUILTIN_SHUFFLE
:
6900 if (vec
->length () == 2)
6902 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6903 (*vec
)[1], tf_warning_or_error
);
6904 else if (vec
->length () == 3)
6906 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6907 (*vec
)[2], tf_warning_or_error
);
6910 error_at (loc
, "wrong number of arguments to "
6911 "%<__builtin_shuffle%>");
6912 postfix_expression
= error_mark_node
;
6926 /* If the next thing is a simple-type-specifier, we may be
6927 looking at a functional cast. We could also be looking at
6928 an id-expression. So, we try the functional cast, and if
6929 that doesn't work we fall back to the primary-expression. */
6930 cp_parser_parse_tentatively (parser
);
6931 /* Look for the simple-type-specifier. */
6932 ++parser
->prevent_constrained_type_specifiers
;
6933 type
= cp_parser_simple_type_specifier (parser
,
6934 /*decl_specs=*/NULL
,
6935 CP_PARSER_FLAGS_NONE
);
6936 --parser
->prevent_constrained_type_specifiers
;
6937 /* Parse the cast itself. */
6938 if (!cp_parser_error_occurred (parser
))
6940 = cp_parser_functional_cast (parser
, type
);
6941 /* If that worked, we're done. */
6942 if (cp_parser_parse_definitely (parser
))
6945 /* If the functional-cast didn't work out, try a
6946 compound-literal. */
6947 if (cp_parser_allow_gnu_extensions_p (parser
)
6948 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6950 cp_expr initializer
= NULL_TREE
;
6952 cp_parser_parse_tentatively (parser
);
6954 matching_parens parens
;
6955 parens
.consume_open (parser
);
6957 /* Avoid calling cp_parser_type_id pointlessly, see comment
6958 in cp_parser_cast_expression about c++/29234. */
6959 if (!cp_parser_compound_literal_p (parser
))
6960 cp_parser_simulate_error (parser
);
6963 /* Parse the type. */
6964 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6965 parser
->in_type_id_in_expr_p
= true;
6966 type
= cp_parser_type_id (parser
);
6967 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6968 parens
.require_close (parser
);
6971 /* If things aren't going well, there's no need to
6973 if (!cp_parser_error_occurred (parser
))
6975 bool non_constant_p
;
6976 /* Parse the brace-enclosed initializer list. */
6977 initializer
= cp_parser_braced_list (parser
,
6980 /* If that worked, we're definitely looking at a
6981 compound-literal expression. */
6982 if (cp_parser_parse_definitely (parser
))
6984 /* Warn the user that a compound literal is not
6985 allowed in standard C++. */
6986 pedwarn (input_location
, OPT_Wpedantic
,
6987 "ISO C++ forbids compound-literals");
6988 /* For simplicity, we disallow compound literals in
6989 constant-expressions. We could
6990 allow compound literals of integer type, whose
6991 initializer was a constant, in constant
6992 expressions. Permitting that usage, as a further
6993 extension, would not change the meaning of any
6994 currently accepted programs. (Of course, as
6995 compound literals are not part of ISO C++, the
6996 standard has nothing to say.) */
6997 if (cp_parser_non_integral_constant_expression (parser
,
7000 postfix_expression
= error_mark_node
;
7003 /* Form the representation of the compound-literal. */
7005 = finish_compound_literal (type
, initializer
,
7006 tf_warning_or_error
, fcl_c99
);
7007 postfix_expression
.set_location (initializer
.get_location ());
7012 /* It must be a primary-expression. */
7014 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7015 /*template_arg_p=*/false,
7022 /* Note that we don't need to worry about calling build_cplus_new on a
7023 class-valued CALL_EXPR in decltype when it isn't the end of the
7024 postfix-expression; unary_complex_lvalue will take care of that for
7027 /* Keep looping until the postfix-expression is complete. */
7030 if (idk
== CP_ID_KIND_UNQUALIFIED
7031 && identifier_p (postfix_expression
)
7032 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7033 /* It is not a Koenig lookup function call. */
7035 = unqualified_name_lookup_error (postfix_expression
);
7037 /* Peek at the next token. */
7038 token
= cp_lexer_peek_token (parser
->lexer
);
7040 switch (token
->type
)
7042 case CPP_OPEN_SQUARE
:
7043 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7045 cp_parser_error (parser
,
7046 "two consecutive %<[%> shall "
7047 "only introduce an attribute");
7048 return error_mark_node
;
7051 = cp_parser_postfix_open_square_expression (parser
,
7055 postfix_expression
.set_range (start_loc
,
7056 postfix_expression
.get_location ());
7058 idk
= CP_ID_KIND_NONE
;
7059 is_member_access
= false;
7062 case CPP_OPEN_PAREN
:
7063 /* postfix-expression ( expression-list [opt] ) */
7066 bool is_builtin_constant_p
;
7067 bool saved_integral_constant_expression_p
= false;
7068 bool saved_non_integral_constant_expression_p
= false;
7069 tsubst_flags_t complain
= complain_flags (decltype_p
);
7070 vec
<tree
, va_gc
> *args
;
7071 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7073 is_member_access
= false;
7075 is_builtin_constant_p
7076 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7077 if (is_builtin_constant_p
)
7079 /* The whole point of __builtin_constant_p is to allow
7080 non-constant expressions to appear as arguments. */
7081 saved_integral_constant_expression_p
7082 = parser
->integral_constant_expression_p
;
7083 saved_non_integral_constant_expression_p
7084 = parser
->non_integral_constant_expression_p
;
7085 parser
->integral_constant_expression_p
= false;
7087 args
= (cp_parser_parenthesized_expression_list
7089 /*cast_p=*/false, /*allow_expansion_p=*/true,
7090 /*non_constant_p=*/NULL
,
7091 /*close_paren_loc=*/&close_paren_loc
));
7092 if (is_builtin_constant_p
)
7094 parser
->integral_constant_expression_p
7095 = saved_integral_constant_expression_p
;
7096 parser
->non_integral_constant_expression_p
7097 = saved_non_integral_constant_expression_p
;
7102 postfix_expression
= error_mark_node
;
7106 /* Function calls are not permitted in
7107 constant-expressions. */
7108 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7109 && cp_parser_non_integral_constant_expression (parser
,
7112 postfix_expression
= error_mark_node
;
7113 release_tree_vector (args
);
7118 if (idk
== CP_ID_KIND_UNQUALIFIED
7119 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7121 if (identifier_p (postfix_expression
))
7123 if (!args
->is_empty ())
7126 if (!any_type_dependent_arguments_p (args
))
7128 = perform_koenig_lookup (postfix_expression
, args
,
7133 = unqualified_fn_lookup_error (postfix_expression
);
7135 /* We do not perform argument-dependent lookup if
7136 normal lookup finds a non-function, in accordance
7137 with the expected resolution of DR 218. */
7138 else if (!args
->is_empty ()
7139 && is_overloaded_fn (postfix_expression
))
7141 tree fn
= get_first_fn (postfix_expression
);
7142 fn
= STRIP_TEMPLATE (fn
);
7144 /* Do not do argument dependent lookup if regular
7145 lookup finds a member function or a block-scope
7146 function declaration. [basic.lookup.argdep]/3 */
7147 if (!DECL_FUNCTION_MEMBER_P (fn
)
7148 && !DECL_LOCAL_FUNCTION_P (fn
))
7151 if (!any_type_dependent_arguments_p (args
))
7153 = perform_koenig_lookup (postfix_expression
, args
,
7159 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7160 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7161 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7162 && vec_safe_length (args
) == 3)
7164 tree arg0
= (*args
)[0];
7165 tree arg1
= (*args
)[1];
7166 tree arg2
= (*args
)[2];
7167 int literal_mask
= ((!!integer_zerop (arg1
) << 1)
7168 | (!!integer_zerop (arg2
) << 2));
7169 if (TREE_CODE (arg2
) == CONST_DECL
)
7170 arg2
= DECL_INITIAL (arg2
);
7171 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7174 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7176 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7177 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7179 if (processing_template_decl
7180 && (type_dependent_object_expression_p (instance
)
7181 || (!BASELINK_P (fn
)
7182 && TREE_CODE (fn
) != FIELD_DECL
)
7183 || type_dependent_expression_p (fn
)
7184 || any_type_dependent_arguments_p (args
)))
7186 maybe_generic_this_capture (instance
, fn
);
7188 = build_min_nt_call_vec (postfix_expression
, args
);
7189 release_tree_vector (args
);
7193 if (BASELINK_P (fn
))
7196 = (build_new_method_call
7197 (instance
, fn
, &args
, NULL_TREE
,
7198 (idk
== CP_ID_KIND_QUALIFIED
7199 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7206 = finish_call_expr (postfix_expression
, &args
,
7207 /*disallow_virtual=*/false,
7211 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7212 || TREE_CODE (postfix_expression
) == MEMBER_REF
7213 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7214 postfix_expression
= (build_offset_ref_call_from_tree
7215 (postfix_expression
, &args
,
7217 else if (idk
== CP_ID_KIND_QUALIFIED
)
7218 /* A call to a static class member, or a namespace-scope
7221 = finish_call_expr (postfix_expression
, &args
,
7222 /*disallow_virtual=*/true,
7226 /* All other function calls. */
7228 = finish_call_expr (postfix_expression
, &args
,
7229 /*disallow_virtual=*/false,
7233 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7235 location_t combined_loc
= make_location (token
->location
,
7238 postfix_expression
.set_location (combined_loc
);
7241 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7242 idk
= CP_ID_KIND_NONE
;
7244 release_tree_vector (args
);
7250 /* postfix-expression . template [opt] id-expression
7251 postfix-expression . pseudo-destructor-name
7252 postfix-expression -> template [opt] id-expression
7253 postfix-expression -> pseudo-destructor-name */
7255 /* Consume the `.' or `->' operator. */
7256 cp_lexer_consume_token (parser
->lexer
);
7259 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7263 is_member_access
= true;
7267 /* postfix-expression ++ */
7268 /* Consume the `++' token. */
7269 cp_lexer_consume_token (parser
->lexer
);
7270 /* Generate a representation for the complete expression. */
7272 = finish_increment_expr (postfix_expression
,
7273 POSTINCREMENT_EXPR
);
7274 /* Increments may not appear in constant-expressions. */
7275 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7276 postfix_expression
= error_mark_node
;
7277 idk
= CP_ID_KIND_NONE
;
7278 is_member_access
= false;
7281 case CPP_MINUS_MINUS
:
7282 /* postfix-expression -- */
7283 /* Consume the `--' token. */
7284 cp_lexer_consume_token (parser
->lexer
);
7285 /* Generate a representation for the complete expression. */
7287 = finish_increment_expr (postfix_expression
,
7288 POSTDECREMENT_EXPR
);
7289 /* Decrements may not appear in constant-expressions. */
7290 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7291 postfix_expression
= error_mark_node
;
7292 idk
= CP_ID_KIND_NONE
;
7293 is_member_access
= false;
7297 if (pidk_return
!= NULL
)
7298 * pidk_return
= idk
;
7299 if (member_access_only_p
)
7300 return is_member_access
7301 ? postfix_expression
7302 : cp_expr (error_mark_node
);
7304 return postfix_expression
;
7308 /* We should never get here. */
7310 return error_mark_node
;
7313 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7314 by cp_parser_builtin_offsetof. We're looking for
7316 postfix-expression [ expression ]
7317 postfix-expression [ braced-init-list ] (C++11)
7319 FOR_OFFSETOF is set if we're being called in that context, which
7320 changes how we deal with integer constant expressions. */
7323 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7324 tree postfix_expression
,
7328 tree index
= NULL_TREE
;
7329 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7330 bool saved_greater_than_is_operator_p
;
7332 /* Consume the `[' token. */
7333 cp_lexer_consume_token (parser
->lexer
);
7335 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7336 parser
->greater_than_is_operator_p
= true;
7338 /* Parse the index expression. */
7339 /* ??? For offsetof, there is a question of what to allow here. If
7340 offsetof is not being used in an integral constant expression context,
7341 then we *could* get the right answer by computing the value at runtime.
7342 If we are in an integral constant expression context, then we might
7343 could accept any constant expression; hard to say without analysis.
7344 Rather than open the barn door too wide right away, allow only integer
7345 constant expressions here. */
7347 index
= cp_parser_constant_expression (parser
);
7350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7352 bool expr_nonconst_p
;
7353 cp_lexer_set_source_position (parser
->lexer
);
7354 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7355 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7358 index
= cp_parser_expression (parser
);
7361 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7363 /* Look for the closing `]'. */
7364 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7366 /* Build the ARRAY_REF. */
7367 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7370 /* When not doing offsetof, array references are not permitted in
7371 constant-expressions. */
7373 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7374 postfix_expression
= error_mark_node
;
7376 return postfix_expression
;
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7382 postfix-expression . template [opt] id-expression
7383 postfix-expression . pseudo-destructor-name
7384 postfix-expression -> template [opt] id-expression
7385 postfix-expression -> pseudo-destructor-name
7387 FOR_OFFSETOF is set if we're being called in that context. That sorta
7388 limits what of the above we'll actually accept, but nevermind.
7389 TOKEN_TYPE is the "." or "->" token, which will already have been
7390 removed from the stream. */
7393 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7394 enum cpp_ttype token_type
,
7395 cp_expr postfix_expression
,
7396 bool for_offsetof
, cp_id_kind
*idk
,
7397 location_t location
)
7401 bool pseudo_destructor_p
;
7402 tree scope
= NULL_TREE
;
7403 location_t start_loc
= postfix_expression
.get_start ();
7405 /* If this is a `->' operator, dereference the pointer. */
7406 if (token_type
== CPP_DEREF
)
7407 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7408 tf_warning_or_error
);
7409 /* Check to see whether or not the expression is type-dependent and
7410 not the current instantiation. */
7411 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7412 /* The identifier following the `->' or `.' is not qualified. */
7413 parser
->scope
= NULL_TREE
;
7414 parser
->qualifying_scope
= NULL_TREE
;
7415 parser
->object_scope
= NULL_TREE
;
7416 *idk
= CP_ID_KIND_NONE
;
7418 /* Enter the scope corresponding to the type of the object
7419 given by the POSTFIX_EXPRESSION. */
7422 scope
= TREE_TYPE (postfix_expression
);
7423 /* According to the standard, no expression should ever have
7424 reference type. Unfortunately, we do not currently match
7425 the standard in this respect in that our internal representation
7426 of an expression may have reference type even when the standard
7427 says it does not. Therefore, we have to manually obtain the
7428 underlying type here. */
7429 scope
= non_reference (scope
);
7430 /* The type of the POSTFIX_EXPRESSION must be complete. */
7431 /* Unlike the object expression in other contexts, *this is not
7432 required to be of complete type for purposes of class member
7433 access (5.2.5) outside the member function body. */
7434 if (postfix_expression
!= current_class_ref
7435 && scope
!= error_mark_node
7436 && !(processing_template_decl
7437 && current_class_type
7438 && (same_type_ignoring_top_level_qualifiers_p
7439 (scope
, current_class_type
))))
7441 scope
= complete_type (scope
);
7442 if (!COMPLETE_TYPE_P (scope
)
7443 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7444 && EXPR_P (postfix_expression
))
7446 /* In a template, be permissive by treating an object expression
7447 of incomplete type as dependent (after a pedwarn). */
7448 diagnostic_t kind
= (processing_template_decl
7449 && MAYBE_CLASS_TYPE_P (scope
)
7452 cxx_incomplete_type_diagnostic
7453 (location_of (postfix_expression
),
7454 postfix_expression
, scope
, kind
);
7455 if (!MAYBE_CLASS_TYPE_P (scope
))
7456 return error_mark_node
;
7457 if (processing_template_decl
)
7460 scope
= TREE_TYPE (postfix_expression
) = NULL_TREE
;
7467 /* Let the name lookup machinery know that we are processing a
7468 class member access expression. */
7469 parser
->context
->object_type
= scope
;
7470 /* If something went wrong, we want to be able to discern that case,
7471 as opposed to the case where there was no SCOPE due to the type
7472 of expression being dependent. */
7474 scope
= error_mark_node
;
7475 /* If the SCOPE was erroneous, make the various semantic analysis
7476 functions exit quickly -- and without issuing additional error
7478 if (scope
== error_mark_node
)
7479 postfix_expression
= error_mark_node
;
7484 /* Tell cp_parser_lookup_name that there was an object, even though it's
7486 parser
->context
->object_type
= unknown_type_node
;
7488 /* Assume this expression is not a pseudo-destructor access. */
7489 pseudo_destructor_p
= false;
7491 /* If the SCOPE is a scalar type, then, if this is a valid program,
7492 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7493 is type dependent, it can be pseudo-destructor-name or something else.
7494 Try to parse it as pseudo-destructor-name first. */
7495 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7500 cp_parser_parse_tentatively (parser
);
7501 /* Parse the pseudo-destructor-name. */
7503 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7506 && (cp_parser_error_occurred (parser
)
7507 || !SCALAR_TYPE_P (type
)))
7508 cp_parser_abort_tentative_parse (parser
);
7509 else if (cp_parser_parse_definitely (parser
))
7511 pseudo_destructor_p
= true;
7513 = finish_pseudo_destructor_expr (postfix_expression
,
7518 if (!pseudo_destructor_p
)
7520 /* If the SCOPE is not a scalar type, we are looking at an
7521 ordinary class member access expression, rather than a
7522 pseudo-destructor-name. */
7524 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7525 /* Parse the id-expression. */
7526 name
= (cp_parser_id_expression
7528 cp_parser_optional_template_keyword (parser
),
7529 /*check_dependency_p=*/true,
7531 /*declarator_p=*/false,
7532 /*optional_p=*/false));
7533 /* In general, build a SCOPE_REF if the member name is qualified.
7534 However, if the name was not dependent and has already been
7535 resolved; there is no need to build the SCOPE_REF. For example;
7537 struct X { void f(); };
7538 template <typename T> void f(T* t) { t->X::f(); }
7540 Even though "t" is dependent, "X::f" is not and has been resolved
7541 to a BASELINK; there is no need to include scope information. */
7543 /* But we do need to remember that there was an explicit scope for
7544 virtual function calls. */
7546 *idk
= CP_ID_KIND_QUALIFIED
;
7548 /* If the name is a template-id that names a type, we will get a
7549 TYPE_DECL here. That is invalid code. */
7550 if (TREE_CODE (name
) == TYPE_DECL
)
7552 error_at (token
->location
, "invalid use of %qD", name
);
7553 postfix_expression
= error_mark_node
;
7557 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7559 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7561 error_at (token
->location
, "%<%D::%D%> is not a class member",
7562 parser
->scope
, name
);
7563 postfix_expression
= error_mark_node
;
7566 name
= build_qualified_name (/*type=*/NULL_TREE
,
7570 parser
->scope
= NULL_TREE
;
7571 parser
->qualifying_scope
= NULL_TREE
;
7572 parser
->object_scope
= NULL_TREE
;
7574 if (parser
->scope
&& name
&& BASELINK_P (name
))
7575 adjust_result_of_qualified_name_lookup
7576 (name
, parser
->scope
, scope
);
7578 = finish_class_member_access_expr (postfix_expression
, name
,
7580 tf_warning_or_error
);
7581 /* Build a location e.g.:
7584 where the caret is at the deref token, ranging from
7585 the start of postfix_expression to the end of the access expr. */
7587 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7588 location_t combined_loc
7589 = make_location (input_location
, start_loc
, end_loc
);
7590 protected_set_expr_location (postfix_expression
, combined_loc
);
7594 /* We no longer need to look up names in the scope of the object on
7595 the left-hand side of the `.' or `->' operator. */
7596 parser
->context
->object_type
= NULL_TREE
;
7598 /* Outside of offsetof, these operators may not appear in
7599 constant-expressions. */
7601 && (cp_parser_non_integral_constant_expression
7602 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7603 postfix_expression
= error_mark_node
;
7605 return postfix_expression
;
7608 /* Parse a parenthesized expression-list.
7611 assignment-expression
7612 expression-list, assignment-expression
7617 identifier, expression-list
7619 CAST_P is true if this expression is the target of a cast.
7621 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7624 Returns a vector of trees. Each element is a representation of an
7625 assignment-expression. NULL is returned if the ( and or ) are
7626 missing. An empty, but allocated, vector is returned on no
7627 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7628 if we are parsing an attribute list for an attribute that wants a
7629 plain identifier argument, normal_attr for an attribute that wants
7630 an expression, or non_attr if we aren't parsing an attribute list. If
7631 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7632 not all of the expressions in the list were constant.
7633 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7634 will be written to with the location of the closing parenthesis. If
7635 an error occurs, it may or may not be written to. */
7637 static vec
<tree
, va_gc
> *
7638 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7639 int is_attribute_list
,
7641 bool allow_expansion_p
,
7642 bool *non_constant_p
,
7643 location_t
*close_paren_loc
)
7645 vec
<tree
, va_gc
> *expression_list
;
7646 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7647 tree identifier
= NULL_TREE
;
7648 bool saved_greater_than_is_operator_p
;
7650 /* Assume all the expressions will be constant. */
7652 *non_constant_p
= false;
7654 matching_parens parens
;
7655 if (!parens
.require_open (parser
))
7658 expression_list
= make_tree_vector ();
7660 /* Within a parenthesized expression, a `>' token is always
7661 the greater-than operator. */
7662 saved_greater_than_is_operator_p
7663 = parser
->greater_than_is_operator_p
;
7664 parser
->greater_than_is_operator_p
= true;
7666 /* Consume expressions until there are no more. */
7667 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7672 /* At the beginning of attribute lists, check to see if the
7673 next token is an identifier. */
7674 if (is_attribute_list
== id_attr
7675 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7679 /* Consume the identifier. */
7680 token
= cp_lexer_consume_token (parser
->lexer
);
7681 /* Save the identifier. */
7682 identifier
= token
->u
.value
;
7686 bool expr_non_constant_p
;
7688 /* Parse the next assignment-expression. */
7689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7691 /* A braced-init-list. */
7692 cp_lexer_set_source_position (parser
->lexer
);
7693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7694 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7695 if (non_constant_p
&& expr_non_constant_p
)
7696 *non_constant_p
= true;
7698 else if (non_constant_p
)
7700 expr
= (cp_parser_constant_expression
7701 (parser
, /*allow_non_constant_p=*/true,
7702 &expr_non_constant_p
));
7703 if (expr_non_constant_p
)
7704 *non_constant_p
= true;
7707 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7711 expr
= instantiate_non_dependent_expr (expr
);
7713 /* If we have an ellipsis, then this is an expression
7715 if (allow_expansion_p
7716 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7718 /* Consume the `...'. */
7719 cp_lexer_consume_token (parser
->lexer
);
7721 /* Build the argument pack. */
7722 expr
= make_pack_expansion (expr
);
7725 /* Add it to the list. We add error_mark_node
7726 expressions to the list, so that we can still tell if
7727 the correct form for a parenthesized expression-list
7728 is found. That gives better errors. */
7729 vec_safe_push (expression_list
, expr
);
7731 if (expr
== error_mark_node
)
7735 /* After the first item, attribute lists look the same as
7736 expression lists. */
7737 is_attribute_list
= non_attr
;
7740 /* If the next token isn't a `,', then we are done. */
7741 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7744 /* Otherwise, consume the `,' and keep going. */
7745 cp_lexer_consume_token (parser
->lexer
);
7748 if (close_paren_loc
)
7749 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7751 if (!parens
.require_close (parser
))
7756 /* We try and resync to an unnested comma, as that will give the
7757 user better diagnostics. */
7758 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7759 /*recovering=*/true,
7761 /*consume_paren=*/true);
7766 parser
->greater_than_is_operator_p
7767 = saved_greater_than_is_operator_p
;
7772 parser
->greater_than_is_operator_p
7773 = saved_greater_than_is_operator_p
;
7776 vec_safe_insert (expression_list
, 0, identifier
);
7778 return expression_list
;
7781 /* Parse a pseudo-destructor-name.
7783 pseudo-destructor-name:
7784 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7785 :: [opt] nested-name-specifier template template-id :: ~ type-name
7786 :: [opt] nested-name-specifier [opt] ~ type-name
7788 If either of the first two productions is used, sets *SCOPE to the
7789 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7790 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7791 or ERROR_MARK_NODE if the parse fails. */
7794 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7799 bool nested_name_specifier_p
;
7802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7803 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7804 && !type_dependent_expression_p (object
))
7806 if (cxx_dialect
< cxx14
)
7807 pedwarn (input_location
, 0,
7808 "%<~auto%> only available with "
7809 "-std=c++14 or -std=gnu++14");
7810 cp_lexer_consume_token (parser
->lexer
);
7811 cp_lexer_consume_token (parser
->lexer
);
7813 *type
= TREE_TYPE (object
);
7817 /* Assume that things will not work out. */
7818 *type
= error_mark_node
;
7820 /* Look for the optional `::' operator. */
7821 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7822 /* Look for the optional nested-name-specifier. */
7823 nested_name_specifier_p
7824 = (cp_parser_nested_name_specifier_opt (parser
,
7825 /*typename_keyword_p=*/false,
7826 /*check_dependency_p=*/true,
7828 /*is_declaration=*/false)
7830 /* Now, if we saw a nested-name-specifier, we might be doing the
7831 second production. */
7832 if (nested_name_specifier_p
7833 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7835 /* Consume the `template' keyword. */
7836 cp_lexer_consume_token (parser
->lexer
);
7837 /* Parse the template-id. */
7838 cp_parser_template_id (parser
,
7839 /*template_keyword_p=*/true,
7840 /*check_dependency_p=*/false,
7842 /*is_declaration=*/true);
7843 /* Look for the `::' token. */
7844 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7846 /* If the next token is not a `~', then there might be some
7847 additional qualification. */
7848 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7850 /* At this point, we're looking for "type-name :: ~". The type-name
7851 must not be a class-name, since this is a pseudo-destructor. So,
7852 it must be either an enum-name, or a typedef-name -- both of which
7853 are just identifiers. So, we peek ahead to check that the "::"
7854 and "~" tokens are present; if they are not, then we can avoid
7855 calling type_name. */
7856 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7857 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7858 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7860 cp_parser_error (parser
, "non-scalar type");
7864 /* Look for the type-name. */
7865 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7866 if (*scope
== error_mark_node
)
7869 /* Look for the `::' token. */
7870 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7875 /* Look for the `~'. */
7876 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7878 /* Once we see the ~, this has to be a pseudo-destructor. */
7879 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7880 cp_parser_commit_to_topmost_tentative_parse (parser
);
7882 /* Look for the type-name again. We are not responsible for
7883 checking that it matches the first type-name. */
7884 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7887 /* Parse a unary-expression.
7893 unary-operator cast-expression
7894 sizeof unary-expression
7896 alignof ( type-id ) [C++0x]
7903 __extension__ cast-expression
7904 __alignof__ unary-expression
7905 __alignof__ ( type-id )
7906 alignof unary-expression [C++0x]
7907 __real__ cast-expression
7908 __imag__ cast-expression
7910 sizeof ( type-id ) { initializer-list , [opt] }
7911 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7912 __alignof__ ( type-id ) { initializer-list , [opt] }
7914 ADDRESS_P is true iff the unary-expression is appearing as the
7915 operand of the `&' operator. CAST_P is true if this expression is
7916 the target of a cast.
7918 Returns a representation of the expression. */
7921 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7922 bool address_p
, bool cast_p
, bool decltype_p
)
7925 enum tree_code unary_operator
;
7927 /* Peek at the next token. */
7928 token
= cp_lexer_peek_token (parser
->lexer
);
7929 /* Some keywords give away the kind of expression. */
7930 if (token
->type
== CPP_KEYWORD
)
7932 enum rid keyword
= token
->keyword
;
7941 location_t start_loc
= token
->location
;
7943 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7944 /* Consume the token. */
7945 cp_lexer_consume_token (parser
->lexer
);
7946 /* Parse the operand. */
7947 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7949 if (TYPE_P (operand
))
7950 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7953 /* ISO C++ defines alignof only with types, not with
7954 expressions. So pedwarn if alignof is used with a non-
7955 type expression. However, __alignof__ is ok. */
7956 if (id_equal (token
->u
.value
, "alignof"))
7957 pedwarn (token
->location
, OPT_Wpedantic
,
7958 "ISO C++ does not allow %<alignof%> "
7961 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7963 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7964 SIZEOF_EXPR with the original operand. */
7965 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7967 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7969 if (!processing_template_decl
&& TYPE_P (operand
))
7971 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7972 build1 (NOP_EXPR
, operand
,
7974 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7977 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7978 TREE_SIDE_EFFECTS (ret
) = 0;
7979 TREE_READONLY (ret
) = 1;
7983 /* Construct a location e.g. :
7986 with start == caret at the start of the "alignof"/"sizeof"
7987 token, with the endpoint at the final closing paren. */
7988 location_t finish_loc
7989 = cp_lexer_previous_token (parser
->lexer
)->location
;
7990 location_t compound_loc
7991 = make_location (start_loc
, start_loc
, finish_loc
);
7993 cp_expr
ret_expr (ret
);
7994 ret_expr
.set_location (compound_loc
);
7999 return cp_parser_new_expression (parser
);
8002 return cp_parser_delete_expression (parser
);
8006 /* The saved value of the PEDANTIC flag. */
8010 /* Save away the PEDANTIC flag. */
8011 cp_parser_extension_opt (parser
, &saved_pedantic
);
8012 /* Parse the cast-expression. */
8013 expr
= cp_parser_simple_cast_expression (parser
);
8014 /* Restore the PEDANTIC flag. */
8015 pedantic
= saved_pedantic
;
8025 /* Consume the `__real__' or `__imag__' token. */
8026 cp_lexer_consume_token (parser
->lexer
);
8027 /* Parse the cast-expression. */
8028 expression
= cp_parser_simple_cast_expression (parser
);
8029 /* Create the complete representation. */
8030 return build_x_unary_op (token
->location
,
8031 (keyword
== RID_REALPART
8032 ? REALPART_EXPR
: IMAGPART_EXPR
),
8034 tf_warning_or_error
);
8038 case RID_TRANSACTION_ATOMIC
:
8039 case RID_TRANSACTION_RELAXED
:
8040 return cp_parser_transaction_expression (parser
, keyword
);
8045 const char *saved_message
;
8046 bool saved_integral_constant_expression_p
;
8047 bool saved_non_integral_constant_expression_p
;
8048 bool saved_greater_than_is_operator_p
;
8050 location_t start_loc
= token
->location
;
8052 cp_lexer_consume_token (parser
->lexer
);
8053 matching_parens parens
;
8054 parens
.require_open (parser
);
8056 saved_message
= parser
->type_definition_forbidden_message
;
8057 parser
->type_definition_forbidden_message
8058 = G_("types may not be defined in %<noexcept%> expressions");
8060 saved_integral_constant_expression_p
8061 = parser
->integral_constant_expression_p
;
8062 saved_non_integral_constant_expression_p
8063 = parser
->non_integral_constant_expression_p
;
8064 parser
->integral_constant_expression_p
= false;
8066 saved_greater_than_is_operator_p
8067 = parser
->greater_than_is_operator_p
;
8068 parser
->greater_than_is_operator_p
= true;
8070 ++cp_unevaluated_operand
;
8071 ++c_inhibit_evaluation_warnings
;
8072 ++cp_noexcept_operand
;
8073 expr
= cp_parser_expression (parser
);
8074 --cp_noexcept_operand
;
8075 --c_inhibit_evaluation_warnings
;
8076 --cp_unevaluated_operand
;
8078 parser
->greater_than_is_operator_p
8079 = saved_greater_than_is_operator_p
;
8081 parser
->integral_constant_expression_p
8082 = saved_integral_constant_expression_p
;
8083 parser
->non_integral_constant_expression_p
8084 = saved_non_integral_constant_expression_p
;
8086 parser
->type_definition_forbidden_message
= saved_message
;
8088 location_t finish_loc
8089 = cp_lexer_peek_token (parser
->lexer
)->location
;
8090 parens
.require_close (parser
);
8092 /* Construct a location of the form:
8095 with start == caret, finishing at the close-paren. */
8096 location_t noexcept_loc
8097 = make_location (start_loc
, start_loc
, finish_loc
);
8099 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8108 /* Look for the `:: new' and `:: delete', which also signal the
8109 beginning of a new-expression, or delete-expression,
8110 respectively. If the next token is `::', then it might be one of
8112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8116 /* See if the token after the `::' is one of the keywords in
8117 which we're interested. */
8118 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8119 /* If it's `new', we have a new-expression. */
8120 if (keyword
== RID_NEW
)
8121 return cp_parser_new_expression (parser
);
8122 /* Similarly, for `delete'. */
8123 else if (keyword
== RID_DELETE
)
8124 return cp_parser_delete_expression (parser
);
8127 /* Look for a unary operator. */
8128 unary_operator
= cp_parser_unary_operator (token
);
8129 /* The `++' and `--' operators can be handled similarly, even though
8130 they are not technically unary-operators in the grammar. */
8131 if (unary_operator
== ERROR_MARK
)
8133 if (token
->type
== CPP_PLUS_PLUS
)
8134 unary_operator
= PREINCREMENT_EXPR
;
8135 else if (token
->type
== CPP_MINUS_MINUS
)
8136 unary_operator
= PREDECREMENT_EXPR
;
8137 /* Handle the GNU address-of-label extension. */
8138 else if (cp_parser_allow_gnu_extensions_p (parser
)
8139 && token
->type
== CPP_AND_AND
)
8143 location_t start_loc
= token
->location
;
8145 /* Consume the '&&' token. */
8146 cp_lexer_consume_token (parser
->lexer
);
8147 /* Look for the identifier. */
8148 location_t finish_loc
8149 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8150 identifier
= cp_parser_identifier (parser
);
8151 /* Construct a location of the form:
8154 with caret==start at the "&&", finish at the end of the label. */
8155 location_t combined_loc
8156 = make_location (start_loc
, start_loc
, finish_loc
);
8157 /* Create an expression representing the address. */
8158 expression
= finish_label_address_expr (identifier
, combined_loc
);
8159 if (cp_parser_non_integral_constant_expression (parser
,
8161 expression
= error_mark_node
;
8165 if (unary_operator
!= ERROR_MARK
)
8167 cp_expr cast_expression
;
8168 cp_expr expression
= error_mark_node
;
8169 non_integral_constant non_constant_p
= NIC_NONE
;
8170 location_t loc
= token
->location
;
8171 tsubst_flags_t complain
= complain_flags (decltype_p
);
8173 /* Consume the operator token. */
8174 token
= cp_lexer_consume_token (parser
->lexer
);
8175 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8177 /* Parse the cast-expression. */
8179 = cp_parser_cast_expression (parser
,
8180 unary_operator
== ADDR_EXPR
,
8186 OP_TOKEN CAST_EXPRESSION
8187 ^~~~~~~~~~~~~~~~~~~~~~~~~
8188 with start==caret at the operator token, and
8189 extending to the end of the cast_expression. */
8190 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8192 /* Now, build an appropriate representation. */
8193 switch (unary_operator
)
8196 non_constant_p
= NIC_STAR
;
8197 expression
= build_x_indirect_ref (loc
, cast_expression
,
8200 /* TODO: build_x_indirect_ref does not always honor the
8201 location, so ensure it is set. */
8202 expression
.set_location (loc
);
8206 non_constant_p
= NIC_ADDR
;
8209 expression
= build_x_unary_op (loc
, unary_operator
,
8212 /* TODO: build_x_unary_op does not always honor the location,
8213 so ensure it is set. */
8214 expression
.set_location (loc
);
8217 case PREINCREMENT_EXPR
:
8218 case PREDECREMENT_EXPR
:
8219 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8220 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8223 /* Immediately fold negation of a constant, unless the constant is 0
8224 (since -0 == 0) or it would overflow. */
8225 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8226 && CONSTANT_CLASS_P (cast_expression
)
8227 && !integer_zerop (cast_expression
)
8228 && !TREE_OVERFLOW (cast_expression
))
8230 tree folded
= fold_build1 (unary_operator
,
8231 TREE_TYPE (cast_expression
),
8233 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8235 expression
= cp_expr (folded
, loc
);
8240 case UNARY_PLUS_EXPR
:
8241 case TRUTH_NOT_EXPR
:
8242 expression
= finish_unary_op_expr (loc
, unary_operator
,
8243 cast_expression
, complain
);
8250 if (non_constant_p
!= NIC_NONE
8251 && cp_parser_non_integral_constant_expression (parser
,
8253 expression
= error_mark_node
;
8258 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8259 /*member_access_only_p=*/false,
8264 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8265 unary-operator, the corresponding tree code is returned. */
8267 static enum tree_code
8268 cp_parser_unary_operator (cp_token
* token
)
8270 switch (token
->type
)
8273 return INDIRECT_REF
;
8279 return UNARY_PLUS_EXPR
;
8285 return TRUTH_NOT_EXPR
;
8288 return BIT_NOT_EXPR
;
8295 /* Parse a new-expression.
8298 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8299 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8301 Returns a representation of the expression. */
8304 cp_parser_new_expression (cp_parser
* parser
)
8306 bool global_scope_p
;
8307 vec
<tree
, va_gc
> *placement
;
8309 vec
<tree
, va_gc
> *initializer
;
8310 tree nelts
= NULL_TREE
;
8313 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8315 /* Look for the optional `::' operator. */
8317 = (cp_parser_global_scope_opt (parser
,
8318 /*current_scope_valid_p=*/false)
8320 /* Look for the `new' operator. */
8321 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8322 /* There's no easy way to tell a new-placement from the
8323 `( type-id )' construct. */
8324 cp_parser_parse_tentatively (parser
);
8325 /* Look for a new-placement. */
8326 placement
= cp_parser_new_placement (parser
);
8327 /* If that didn't work out, there's no new-placement. */
8328 if (!cp_parser_parse_definitely (parser
))
8330 if (placement
!= NULL
)
8331 release_tree_vector (placement
);
8335 /* If the next token is a `(', then we have a parenthesized
8337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8340 const char *saved_message
= parser
->type_definition_forbidden_message
;
8342 /* Consume the `('. */
8343 matching_parens parens
;
8344 parens
.consume_open (parser
);
8346 /* Parse the type-id. */
8347 parser
->type_definition_forbidden_message
8348 = G_("types may not be defined in a new-expression");
8350 type_id_in_expr_sentinel
s (parser
);
8351 type
= cp_parser_type_id (parser
);
8353 parser
->type_definition_forbidden_message
= saved_message
;
8355 /* Look for the closing `)'. */
8356 parens
.require_close (parser
);
8357 token
= cp_lexer_peek_token (parser
->lexer
);
8358 /* There should not be a direct-new-declarator in this production,
8359 but GCC used to allowed this, so we check and emit a sensible error
8360 message for this case. */
8361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8363 error_at (token
->location
,
8364 "array bound forbidden after parenthesized type-id");
8365 inform (token
->location
,
8366 "try removing the parentheses around the type-id");
8367 cp_parser_direct_new_declarator (parser
);
8370 /* Otherwise, there must be a new-type-id. */
8372 type
= cp_parser_new_type_id (parser
, &nelts
);
8374 /* If the next token is a `(' or '{', then we have a new-initializer. */
8375 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8376 if (token
->type
== CPP_OPEN_PAREN
8377 || token
->type
== CPP_OPEN_BRACE
)
8378 initializer
= cp_parser_new_initializer (parser
);
8382 /* A new-expression may not appear in an integral constant
8384 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8385 ret
= error_mark_node
;
8386 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8387 of a new-type-id or type-id of a new-expression, the new-expression shall
8388 contain a new-initializer of the form ( assignment-expression )".
8389 Additionally, consistently with the spirit of DR 1467, we want to accept
8390 'new auto { 2 }' too. */
8391 else if ((ret
= type_uses_auto (type
))
8392 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8393 && (vec_safe_length (initializer
) != 1
8394 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8395 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8397 error_at (token
->location
,
8398 "initialization of new-expression for type %<auto%> "
8399 "requires exactly one element");
8400 ret
= error_mark_node
;
8404 /* Construct a location e.g.:
8407 with caret == start at the start of the "new" token, and the end
8408 at the end of the final token we consumed. */
8409 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8410 location_t end_loc
= get_finish (end_tok
->location
);
8411 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8413 /* Create a representation of the new-expression. */
8414 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8415 tf_warning_or_error
);
8416 protected_set_expr_location (ret
, combined_loc
);
8419 if (placement
!= NULL
)
8420 release_tree_vector (placement
);
8421 if (initializer
!= NULL
)
8422 release_tree_vector (initializer
);
8427 /* Parse a new-placement.
8432 Returns the same representation as for an expression-list. */
8434 static vec
<tree
, va_gc
> *
8435 cp_parser_new_placement (cp_parser
* parser
)
8437 vec
<tree
, va_gc
> *expression_list
;
8439 /* Parse the expression-list. */
8440 expression_list
= (cp_parser_parenthesized_expression_list
8441 (parser
, non_attr
, /*cast_p=*/false,
8442 /*allow_expansion_p=*/true,
8443 /*non_constant_p=*/NULL
));
8445 if (expression_list
&& expression_list
->is_empty ())
8446 error ("expected expression-list or type-id");
8448 return expression_list
;
8451 /* Parse a new-type-id.
8454 type-specifier-seq new-declarator [opt]
8456 Returns the TYPE allocated. If the new-type-id indicates an array
8457 type, *NELTS is set to the number of elements in the last array
8458 bound; the TYPE will not include the last array bound. */
8461 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8463 cp_decl_specifier_seq type_specifier_seq
;
8464 cp_declarator
*new_declarator
;
8465 cp_declarator
*declarator
;
8466 cp_declarator
*outer_declarator
;
8467 const char *saved_message
;
8469 /* The type-specifier sequence must not contain type definitions.
8470 (It cannot contain declarations of new types either, but if they
8471 are not definitions we will catch that because they are not
8473 saved_message
= parser
->type_definition_forbidden_message
;
8474 parser
->type_definition_forbidden_message
8475 = G_("types may not be defined in a new-type-id");
8476 /* Parse the type-specifier-seq. */
8477 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8478 /*is_trailing_return=*/false,
8479 &type_specifier_seq
);
8480 /* Restore the old message. */
8481 parser
->type_definition_forbidden_message
= saved_message
;
8483 if (type_specifier_seq
.type
== error_mark_node
)
8484 return error_mark_node
;
8486 /* Parse the new-declarator. */
8487 new_declarator
= cp_parser_new_declarator_opt (parser
);
8489 /* Determine the number of elements in the last array dimension, if
8492 /* Skip down to the last array dimension. */
8493 declarator
= new_declarator
;
8494 outer_declarator
= NULL
;
8495 while (declarator
&& (declarator
->kind
== cdk_pointer
8496 || declarator
->kind
== cdk_ptrmem
))
8498 outer_declarator
= declarator
;
8499 declarator
= declarator
->declarator
;
8502 && declarator
->kind
== cdk_array
8503 && declarator
->declarator
8504 && declarator
->declarator
->kind
== cdk_array
)
8506 outer_declarator
= declarator
;
8507 declarator
= declarator
->declarator
;
8510 if (declarator
&& declarator
->kind
== cdk_array
)
8512 *nelts
= declarator
->u
.array
.bounds
;
8513 if (*nelts
== error_mark_node
)
8514 *nelts
= integer_one_node
;
8516 if (outer_declarator
)
8517 outer_declarator
->declarator
= declarator
->declarator
;
8519 new_declarator
= NULL
;
8522 return groktypename (&type_specifier_seq
, new_declarator
, false);
8525 /* Parse an (optional) new-declarator.
8528 ptr-operator new-declarator [opt]
8529 direct-new-declarator
8531 Returns the declarator. */
8533 static cp_declarator
*
8534 cp_parser_new_declarator_opt (cp_parser
* parser
)
8536 enum tree_code code
;
8537 tree type
, std_attributes
= NULL_TREE
;
8538 cp_cv_quals cv_quals
;
8540 /* We don't know if there's a ptr-operator next, or not. */
8541 cp_parser_parse_tentatively (parser
);
8542 /* Look for a ptr-operator. */
8543 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8544 /* If that worked, look for more new-declarators. */
8545 if (cp_parser_parse_definitely (parser
))
8547 cp_declarator
*declarator
;
8549 /* Parse another optional declarator. */
8550 declarator
= cp_parser_new_declarator_opt (parser
);
8552 declarator
= cp_parser_make_indirect_declarator
8553 (code
, type
, cv_quals
, declarator
, std_attributes
);
8558 /* If the next token is a `[', there is a direct-new-declarator. */
8559 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8560 return cp_parser_direct_new_declarator (parser
);
8565 /* Parse a direct-new-declarator.
8567 direct-new-declarator:
8569 direct-new-declarator [constant-expression]
8573 static cp_declarator
*
8574 cp_parser_direct_new_declarator (cp_parser
* parser
)
8576 cp_declarator
*declarator
= NULL
;
8583 /* Look for the opening `['. */
8584 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8586 token
= cp_lexer_peek_token (parser
->lexer
);
8587 expression
= cp_parser_expression (parser
);
8588 /* The standard requires that the expression have integral
8589 type. DR 74 adds enumeration types. We believe that the
8590 real intent is that these expressions be handled like the
8591 expression in a `switch' condition, which also allows
8592 classes with a single conversion to integral or
8593 enumeration type. */
8594 if (!processing_template_decl
)
8597 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8602 error_at (token
->location
,
8603 "expression in new-declarator must have integral "
8604 "or enumeration type");
8605 expression
= error_mark_node
;
8609 /* Look for the closing `]'. */
8610 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8612 /* Add this bound to the declarator. */
8613 declarator
= make_array_declarator (declarator
, expression
);
8615 /* If the next token is not a `[', then there are no more
8617 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8624 /* Parse a new-initializer.
8627 ( expression-list [opt] )
8630 Returns a representation of the expression-list. */
8632 static vec
<tree
, va_gc
> *
8633 cp_parser_new_initializer (cp_parser
* parser
)
8635 vec
<tree
, va_gc
> *expression_list
;
8637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8640 bool expr_non_constant_p
;
8641 cp_lexer_set_source_position (parser
->lexer
);
8642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8643 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8644 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8645 expression_list
= make_tree_vector_single (t
);
8648 expression_list
= (cp_parser_parenthesized_expression_list
8649 (parser
, non_attr
, /*cast_p=*/false,
8650 /*allow_expansion_p=*/true,
8651 /*non_constant_p=*/NULL
));
8653 return expression_list
;
8656 /* Parse a delete-expression.
8659 :: [opt] delete cast-expression
8660 :: [opt] delete [ ] cast-expression
8662 Returns a representation of the expression. */
8665 cp_parser_delete_expression (cp_parser
* parser
)
8667 bool global_scope_p
;
8671 /* Look for the optional `::' operator. */
8673 = (cp_parser_global_scope_opt (parser
,
8674 /*current_scope_valid_p=*/false)
8676 /* Look for the `delete' keyword. */
8677 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8678 /* See if the array syntax is in use. */
8679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8681 /* Consume the `[' token. */
8682 cp_lexer_consume_token (parser
->lexer
);
8683 /* Look for the `]' token. */
8684 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8685 /* Remember that this is the `[]' construct. */
8691 /* Parse the cast-expression. */
8692 expression
= cp_parser_simple_cast_expression (parser
);
8694 /* A delete-expression may not appear in an integral constant
8696 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8697 return error_mark_node
;
8699 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8700 tf_warning_or_error
);
8703 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8704 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8708 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8710 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8711 switch (token
->type
)
8717 case CPP_CLOSE_SQUARE
:
8718 case CPP_CLOSE_PAREN
:
8719 case CPP_CLOSE_BRACE
:
8720 case CPP_OPEN_BRACE
:
8724 case CPP_DEREF_STAR
:
8732 case CPP_GREATER_EQ
:
8753 case CPP_OPEN_PAREN
:
8754 /* In ((type ()) () the last () isn't a valid cast-expression,
8755 so the whole must be parsed as postfix-expression. */
8756 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8759 case CPP_OPEN_SQUARE
:
8760 /* '[' may start a primary-expression in obj-c++ and in C++11,
8761 as a lambda-expression, eg, '(void)[]{}'. */
8762 if (cxx_dialect
>= cxx11
)
8764 return c_dialect_objc ();
8767 case CPP_MINUS_MINUS
:
8768 /* '++' and '--' may or may not start a cast-expression:
8770 struct T { void operator++(int); };
8771 void f() { (T())++; }
8784 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8785 in the order: const_cast, static_cast, reinterpret_cast.
8787 Don't suggest dynamic_cast.
8789 Return the first legal cast kind found, or NULL otherwise. */
8792 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8796 /* Reuse the parser logic by attempting to build the various kinds of
8797 cast, with "complain" disabled.
8798 Identify the first such cast that is valid. */
8800 /* Don't attempt to run such logic within template processing. */
8801 if (processing_template_decl
)
8804 /* First try const_cast. */
8805 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8806 if (trial
!= error_mark_node
)
8807 return "const_cast";
8809 /* If that fails, try static_cast. */
8810 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8811 if (trial
!= error_mark_node
)
8812 return "static_cast";
8814 /* Finally, try reinterpret_cast. */
8815 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8816 if (trial
!= error_mark_node
)
8817 return "reinterpret_cast";
8819 /* No such cast possible. */
8823 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8824 suggesting how to convert a C-style cast of the form:
8828 to a C++-style cast.
8830 The primary range of RICHLOC is asssumed to be that of the original
8831 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8832 of the parens in the C-style cast. */
8835 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8836 location_t close_paren_loc
, tree orig_expr
,
8839 /* This function is non-trivial, so bail out now if the warning isn't
8840 going to be emitted. */
8841 if (!warn_old_style_cast
)
8844 /* Try to find a legal C++ cast, trying them in order:
8845 const_cast, static_cast, reinterpret_cast. */
8846 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8847 if (!cast_suggestion
)
8850 /* Replace the open paren with "CAST_SUGGESTION<". */
8852 pp_printf (&pp
, "%s<", cast_suggestion
);
8853 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8855 /* Replace the close paren with "> (". */
8856 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8858 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8859 rich_loc
->add_fixit_insert_after (")");
8863 /* Parse a cast-expression.
8867 ( type-id ) cast-expression
8869 ADDRESS_P is true iff the unary-expression is appearing as the
8870 operand of the `&' operator. CAST_P is true if this expression is
8871 the target of a cast.
8873 Returns a representation of the expression. */
8876 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8877 bool decltype_p
, cp_id_kind
* pidk
)
8879 /* If it's a `(', then we might be looking at a cast. */
8880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8882 tree type
= NULL_TREE
;
8883 cp_expr
expr (NULL_TREE
);
8884 int cast_expression
= 0;
8885 const char *saved_message
;
8887 /* There's no way to know yet whether or not this is a cast.
8888 For example, `(int (3))' is a unary-expression, while `(int)
8889 3' is a cast. So, we resort to parsing tentatively. */
8890 cp_parser_parse_tentatively (parser
);
8891 /* Types may not be defined in a cast. */
8892 saved_message
= parser
->type_definition_forbidden_message
;
8893 parser
->type_definition_forbidden_message
8894 = G_("types may not be defined in casts");
8895 /* Consume the `('. */
8896 matching_parens parens
;
8897 cp_token
*open_paren
= parens
.consume_open (parser
);
8898 location_t open_paren_loc
= open_paren
->location
;
8899 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8901 /* A very tricky bit is that `(struct S) { 3 }' is a
8902 compound-literal (which we permit in C++ as an extension).
8903 But, that construct is not a cast-expression -- it is a
8904 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8905 is legal; if the compound-literal were a cast-expression,
8906 you'd need an extra set of parentheses.) But, if we parse
8907 the type-id, and it happens to be a class-specifier, then we
8908 will commit to the parse at that point, because we cannot
8909 undo the action that is done when creating a new class. So,
8910 then we cannot back up and do a postfix-expression.
8912 Another tricky case is the following (c++/29234):
8914 struct S { void operator () (); };
8921 As a type-id we parse the parenthesized S()() as a function
8922 returning a function, groktypename complains and we cannot
8923 back up in this case either.
8925 Therefore, we scan ahead to the closing `)', and check to see
8926 if the tokens after the `)' can start a cast-expression. Otherwise
8927 we are dealing with an unary-expression, a postfix-expression
8930 Yet another tricky case, in C++11, is the following (c++/54891):
8934 The issue is that usually, besides the case of lambda-expressions,
8935 the parenthesized type-id cannot be followed by '[', and, eg, we
8936 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8937 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8938 we don't commit, we try a cast-expression, then an unary-expression.
8940 Save tokens so that we can put them back. */
8941 cp_lexer_save_tokens (parser
->lexer
);
8943 /* We may be looking at a cast-expression. */
8944 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8945 /*consume_paren=*/true))
8947 = cp_parser_tokens_start_cast_expression (parser
);
8949 /* Roll back the tokens we skipped. */
8950 cp_lexer_rollback_tokens (parser
->lexer
);
8951 /* If we aren't looking at a cast-expression, simulate an error so
8952 that the call to cp_parser_error_occurred below returns true. */
8953 if (!cast_expression
)
8954 cp_parser_simulate_error (parser
);
8957 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8958 parser
->in_type_id_in_expr_p
= true;
8959 /* Look for the type-id. */
8960 type
= cp_parser_type_id (parser
);
8961 /* Look for the closing `)'. */
8962 cp_token
*close_paren
= parens
.require_close (parser
);
8964 close_paren_loc
= close_paren
->location
;
8965 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8968 /* Restore the saved message. */
8969 parser
->type_definition_forbidden_message
= saved_message
;
8971 /* At this point this can only be either a cast or a
8972 parenthesized ctor such as `(T ())' that looks like a cast to
8973 function returning T. */
8974 if (!cp_parser_error_occurred (parser
))
8976 /* Only commit if the cast-expression doesn't start with
8977 '++', '--', or '[' in C++11. */
8978 if (cast_expression
> 0)
8979 cp_parser_commit_to_topmost_tentative_parse (parser
);
8981 expr
= cp_parser_cast_expression (parser
,
8982 /*address_p=*/false,
8984 /*decltype_p=*/false,
8987 if (cp_parser_parse_definitely (parser
))
8989 /* Warn about old-style casts, if so requested. */
8990 if (warn_old_style_cast
8991 && !in_system_header_at (input_location
)
8992 && !VOID_TYPE_P (type
)
8993 && current_lang_name
!= lang_name_c
)
8995 gcc_rich_location
rich_loc (input_location
);
8996 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
8998 warning_at (&rich_loc
, OPT_Wold_style_cast
,
8999 "use of old-style cast to %q#T", type
);
9002 /* Only type conversions to integral or enumeration types
9003 can be used in constant-expressions. */
9004 if (!cast_valid_in_integral_constant_expression_p (type
)
9005 && cp_parser_non_integral_constant_expression (parser
,
9007 return error_mark_node
;
9009 /* Perform the cast. */
9013 with start==caret at the open paren, extending to the
9015 location_t cast_loc
= make_location (open_paren_loc
,
9017 expr
.get_finish ());
9018 expr
= build_c_cast (cast_loc
, type
, expr
);
9023 cp_parser_abort_tentative_parse (parser
);
9026 /* If we get here, then it's not a cast, so it must be a
9027 unary-expression. */
9028 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9029 cast_p
, decltype_p
);
9032 /* Parse a binary expression of the general form:
9036 pm-expression .* cast-expression
9037 pm-expression ->* cast-expression
9039 multiplicative-expression:
9041 multiplicative-expression * pm-expression
9042 multiplicative-expression / pm-expression
9043 multiplicative-expression % pm-expression
9045 additive-expression:
9046 multiplicative-expression
9047 additive-expression + multiplicative-expression
9048 additive-expression - multiplicative-expression
9052 shift-expression << additive-expression
9053 shift-expression >> additive-expression
9055 relational-expression:
9057 relational-expression < shift-expression
9058 relational-expression > shift-expression
9059 relational-expression <= shift-expression
9060 relational-expression >= shift-expression
9064 relational-expression:
9065 relational-expression <? shift-expression
9066 relational-expression >? shift-expression
9068 equality-expression:
9069 relational-expression
9070 equality-expression == relational-expression
9071 equality-expression != relational-expression
9075 and-expression & equality-expression
9077 exclusive-or-expression:
9079 exclusive-or-expression ^ and-expression
9081 inclusive-or-expression:
9082 exclusive-or-expression
9083 inclusive-or-expression | exclusive-or-expression
9085 logical-and-expression:
9086 inclusive-or-expression
9087 logical-and-expression && inclusive-or-expression
9089 logical-or-expression:
9090 logical-and-expression
9091 logical-or-expression || logical-and-expression
9093 All these are implemented with a single function like:
9096 simple-cast-expression
9097 binary-expression <token> binary-expression
9099 CAST_P is true if this expression is the target of a cast.
9101 The binops_by_token map is used to get the tree codes for each <token> type.
9102 binary-expressions are associated according to a precedence table. */
9104 #define TOKEN_PRECEDENCE(token) \
9105 (((token->type == CPP_GREATER \
9106 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9107 && !parser->greater_than_is_operator_p) \
9108 ? PREC_NOT_OPERATOR \
9109 : binops_by_token[token->type].prec)
9112 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9113 bool no_toplevel_fold_p
,
9115 enum cp_parser_prec prec
,
9118 cp_parser_expression_stack stack
;
9119 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9120 cp_parser_expression_stack_entry current
;
9123 enum tree_code rhs_type
;
9124 enum cp_parser_prec new_prec
, lookahead_prec
;
9127 /* Parse the first expression. */
9128 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9129 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9130 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9131 cast_p
, decltype_p
, pidk
);
9132 current
.prec
= prec
;
9134 if (cp_parser_error_occurred (parser
))
9135 return error_mark_node
;
9139 /* Get an operator token. */
9140 token
= cp_lexer_peek_token (parser
->lexer
);
9142 if (warn_cxx11_compat
9143 && token
->type
== CPP_RSHIFT
9144 && !parser
->greater_than_is_operator_p
)
9146 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9147 "%<>>%> operator is treated"
9148 " as two right angle brackets in C++11"))
9149 inform (token
->location
,
9150 "suggest parentheses around %<>>%> expression");
9153 new_prec
= TOKEN_PRECEDENCE (token
);
9154 if (new_prec
!= PREC_NOT_OPERATOR
9155 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9156 /* This is a fold-expression; handle it later. */
9157 new_prec
= PREC_NOT_OPERATOR
;
9159 /* Popping an entry off the stack means we completed a subexpression:
9160 - either we found a token which is not an operator (`>' where it is not
9161 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9162 will happen repeatedly;
9163 - or, we found an operator which has lower priority. This is the case
9164 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9166 if (new_prec
<= current
.prec
)
9175 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9176 current
.loc
= token
->location
;
9178 /* We used the operator token. */
9179 cp_lexer_consume_token (parser
->lexer
);
9181 /* For "false && x" or "true || x", x will never be executed;
9182 disable warnings while evaluating it. */
9183 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9184 c_inhibit_evaluation_warnings
+=
9185 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9186 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9187 c_inhibit_evaluation_warnings
+=
9188 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9190 /* Extract another operand. It may be the RHS of this expression
9191 or the LHS of a new, higher priority expression. */
9192 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9193 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9194 rhs
= cp_parser_simple_cast_expression (parser
);
9196 /* Get another operator token. Look up its precedence to avoid
9197 building a useless (immediately popped) stack entry for common
9198 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9199 token
= cp_lexer_peek_token (parser
->lexer
);
9200 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9201 if (lookahead_prec
!= PREC_NOT_OPERATOR
9202 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9203 lookahead_prec
= PREC_NOT_OPERATOR
;
9204 if (lookahead_prec
> new_prec
)
9206 /* ... and prepare to parse the RHS of the new, higher priority
9207 expression. Since precedence levels on the stack are
9208 monotonically increasing, we do not have to care about
9213 current
.lhs_type
= rhs_type
;
9214 current
.prec
= new_prec
;
9215 new_prec
= lookahead_prec
;
9219 lookahead_prec
= new_prec
;
9220 /* If the stack is not empty, we have parsed into LHS the right side
9221 (`4' in the example above) of an expression we had suspended.
9222 We can use the information on the stack to recover the LHS (`3')
9223 from the stack together with the tree code (`MULT_EXPR'), and
9224 the precedence of the higher level subexpression
9225 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9226 which will be used to actually build the additive expression. */
9228 rhs_type
= current
.lhs_type
;
9233 /* Undo the disabling of warnings done above. */
9234 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9235 c_inhibit_evaluation_warnings
-=
9236 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9237 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9238 c_inhibit_evaluation_warnings
-=
9239 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9241 if (warn_logical_not_paren
9242 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9243 && current
.lhs_type
== TRUTH_NOT_EXPR
9244 /* Avoid warning for !!x == y. */
9245 && (TREE_CODE (current
.lhs
) != NE_EXPR
9246 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9247 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9248 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9249 /* Avoid warning for !b == y where b is boolean. */
9250 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9251 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9253 /* Avoid warning for !!b == y where b is boolean. */
9254 && (!DECL_P (current
.lhs
)
9255 || TREE_TYPE (current
.lhs
) == NULL_TREE
9256 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9257 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9258 current
.lhs
, maybe_constant_value (rhs
));
9262 location_t combined_loc
= make_location (current
.loc
,
9263 current
.lhs
.get_start (),
9266 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9267 ERROR_MARK for everything that is not a binary expression.
9268 This makes warn_about_parentheses miss some warnings that
9269 involve unary operators. For unary expressions we should
9270 pass the correct tree_code unless the unary expression was
9271 surrounded by parentheses.
9273 if (no_toplevel_fold_p
9274 && lookahead_prec
<= current
.prec
9276 current
.lhs
= build2_loc (combined_loc
,
9278 TREE_CODE_CLASS (current
.tree_type
)
9280 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9284 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9285 current
.lhs
, current
.lhs_type
,
9286 rhs
, rhs_type
, &overload
,
9287 complain_flags (decltype_p
));
9288 /* TODO: build_x_binary_op doesn't always honor the location. */
9289 current
.lhs
.set_location (combined_loc
);
9291 current
.lhs_type
= current
.tree_type
;
9293 /* If the binary operator required the use of an overloaded operator,
9294 then this expression cannot be an integral constant-expression.
9295 An overloaded operator can be used even if both operands are
9296 otherwise permissible in an integral constant-expression if at
9297 least one of the operands is of enumeration type. */
9300 && cp_parser_non_integral_constant_expression (parser
,
9302 return error_mark_node
;
9309 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9310 bool no_toplevel_fold_p
,
9311 enum cp_parser_prec prec
,
9314 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9315 /*decltype*/false, prec
, pidk
);
9318 /* Parse the `? expression : assignment-expression' part of a
9319 conditional-expression. The LOGICAL_OR_EXPR is the
9320 logical-or-expression that started the conditional-expression.
9321 Returns a representation of the entire conditional-expression.
9323 This routine is used by cp_parser_assignment_expression.
9325 ? expression : assignment-expression
9329 ? : assignment-expression */
9332 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9334 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9335 cp_expr assignment_expr
;
9336 struct cp_token
*token
;
9337 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9339 /* Consume the `?' token. */
9340 cp_lexer_consume_token (parser
->lexer
);
9341 token
= cp_lexer_peek_token (parser
->lexer
);
9342 if (cp_parser_allow_gnu_extensions_p (parser
)
9343 && token
->type
== CPP_COLON
)
9345 pedwarn (token
->location
, OPT_Wpedantic
,
9346 "ISO C++ does not allow ?: with omitted middle operand");
9347 /* Implicit true clause. */
9349 c_inhibit_evaluation_warnings
+=
9350 folded_logical_or_expr
== truthvalue_true_node
;
9351 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9355 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9356 parser
->colon_corrects_to_scope_p
= false;
9357 /* Parse the expression. */
9358 c_inhibit_evaluation_warnings
+=
9359 folded_logical_or_expr
== truthvalue_false_node
;
9360 expr
= cp_parser_expression (parser
);
9361 c_inhibit_evaluation_warnings
+=
9362 ((folded_logical_or_expr
== truthvalue_true_node
)
9363 - (folded_logical_or_expr
== truthvalue_false_node
));
9364 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9367 /* The next token should be a `:'. */
9368 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9369 /* Parse the assignment-expression. */
9370 assignment_expr
= cp_parser_assignment_expression (parser
);
9371 c_inhibit_evaluation_warnings
-=
9372 folded_logical_or_expr
== truthvalue_true_node
;
9375 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9376 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9377 with the caret at the "?", ranging from the start of
9378 the logical_or_expr to the end of the assignment_expr. */
9379 loc
= make_location (loc
,
9380 logical_or_expr
.get_start (),
9381 assignment_expr
.get_finish ());
9383 /* Build the conditional-expression. */
9384 return build_x_conditional_expr (loc
, logical_or_expr
,
9387 tf_warning_or_error
);
9390 /* Parse an assignment-expression.
9392 assignment-expression:
9393 conditional-expression
9394 logical-or-expression assignment-operator assignment_expression
9397 CAST_P is true if this expression is the target of a cast.
9398 DECLTYPE_P is true if this expression is the operand of decltype.
9400 Returns a representation for the expression. */
9403 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9404 bool cast_p
, bool decltype_p
)
9408 /* If the next token is the `throw' keyword, then we're looking at
9409 a throw-expression. */
9410 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9411 expr
= cp_parser_throw_expression (parser
);
9412 /* Otherwise, it must be that we are looking at a
9413 logical-or-expression. */
9416 /* Parse the binary expressions (logical-or-expression). */
9417 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9419 PREC_NOT_OPERATOR
, pidk
);
9420 /* If the next token is a `?' then we're actually looking at a
9421 conditional-expression. */
9422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9423 return cp_parser_question_colon_clause (parser
, expr
);
9426 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9428 /* If it's an assignment-operator, we're using the second
9430 enum tree_code assignment_operator
9431 = cp_parser_assignment_operator_opt (parser
);
9432 if (assignment_operator
!= ERROR_MARK
)
9434 bool non_constant_p
;
9436 /* Parse the right-hand side of the assignment. */
9437 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9440 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9441 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9443 /* An assignment may not appear in a
9444 constant-expression. */
9445 if (cp_parser_non_integral_constant_expression (parser
,
9447 return error_mark_node
;
9448 /* Build the assignment expression. Its default
9452 is the location of the '=' token as the
9453 caret, ranging from the start of the lhs to the
9455 loc
= make_location (loc
,
9458 expr
= build_x_modify_expr (loc
, expr
,
9459 assignment_operator
,
9461 complain_flags (decltype_p
));
9462 /* TODO: build_x_modify_expr doesn't honor the location,
9463 so we must set it here. */
9464 expr
.set_location (loc
);
9472 /* Parse an (optional) assignment-operator.
9474 assignment-operator: one of
9475 = *= /= %= += -= >>= <<= &= ^= |=
9479 assignment-operator: one of
9482 If the next token is an assignment operator, the corresponding tree
9483 code is returned, and the token is consumed. For example, for
9484 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9485 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9486 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9487 operator, ERROR_MARK is returned. */
9489 static enum tree_code
9490 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9495 /* Peek at the next token. */
9496 token
= cp_lexer_peek_token (parser
->lexer
);
9498 switch (token
->type
)
9509 op
= TRUNC_DIV_EXPR
;
9513 op
= TRUNC_MOD_EXPR
;
9545 /* Nothing else is an assignment operator. */
9549 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9550 if (op
!= ERROR_MARK
9551 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9554 /* If it was an assignment operator, consume it. */
9555 if (op
!= ERROR_MARK
)
9556 cp_lexer_consume_token (parser
->lexer
);
9561 /* Parse an expression.
9564 assignment-expression
9565 expression , assignment-expression
9567 CAST_P is true if this expression is the target of a cast.
9568 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9569 except possibly parenthesized or on the RHS of a comma (N3276).
9571 Returns a representation of the expression. */
9574 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9575 bool cast_p
, bool decltype_p
)
9577 cp_expr expression
= NULL_TREE
;
9578 location_t loc
= UNKNOWN_LOCATION
;
9582 cp_expr assignment_expression
;
9584 /* Parse the next assignment-expression. */
9585 assignment_expression
9586 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9588 /* We don't create a temporary for a call that is the immediate operand
9589 of decltype or on the RHS of a comma. But when we see a comma, we
9590 need to create a temporary for a call on the LHS. */
9591 if (decltype_p
&& !processing_template_decl
9592 && TREE_CODE (assignment_expression
) == CALL_EXPR
9593 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9594 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9595 assignment_expression
9596 = build_cplus_new (TREE_TYPE (assignment_expression
),
9597 assignment_expression
, tf_warning_or_error
);
9599 /* If this is the first assignment-expression, we can just
9602 expression
= assignment_expression
;
9605 /* Create a location with caret at the comma, ranging
9606 from the start of the LHS to the end of the RHS. */
9607 loc
= make_location (loc
,
9608 expression
.get_start (),
9609 assignment_expression
.get_finish ());
9610 expression
= build_x_compound_expr (loc
, expression
,
9611 assignment_expression
,
9612 complain_flags (decltype_p
));
9613 expression
.set_location (loc
);
9615 /* If the next token is not a comma, or we're in a fold-expression, then
9616 we are done with the expression. */
9617 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9618 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9620 /* Consume the `,'. */
9621 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9622 cp_lexer_consume_token (parser
->lexer
);
9623 /* A comma operator cannot appear in a constant-expression. */
9624 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9625 expression
= error_mark_node
;
9631 /* Parse a constant-expression.
9633 constant-expression:
9634 conditional-expression
9636 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9637 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9638 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9639 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9640 only parse a conditional-expression, otherwise parse an
9641 assignment-expression. See below for rationale. */
9644 cp_parser_constant_expression (cp_parser
* parser
,
9645 bool allow_non_constant_p
,
9646 bool *non_constant_p
,
9649 bool saved_integral_constant_expression_p
;
9650 bool saved_allow_non_integral_constant_expression_p
;
9651 bool saved_non_integral_constant_expression_p
;
9654 /* It might seem that we could simply parse the
9655 conditional-expression, and then check to see if it were
9656 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9657 one that the compiler can figure out is constant, possibly after
9658 doing some simplifications or optimizations. The standard has a
9659 precise definition of constant-expression, and we must honor
9660 that, even though it is somewhat more restrictive.
9666 is not a legal declaration, because `(2, 3)' is not a
9667 constant-expression. The `,' operator is forbidden in a
9668 constant-expression. However, GCC's constant-folding machinery
9669 will fold this operation to an INTEGER_CST for `3'. */
9671 /* Save the old settings. */
9672 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9673 saved_allow_non_integral_constant_expression_p
9674 = parser
->allow_non_integral_constant_expression_p
;
9675 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9676 /* We are now parsing a constant-expression. */
9677 parser
->integral_constant_expression_p
= true;
9678 parser
->allow_non_integral_constant_expression_p
9679 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9680 parser
->non_integral_constant_expression_p
= false;
9681 /* Although the grammar says "conditional-expression", when not STRICT_P,
9682 we parse an "assignment-expression", which also permits
9683 "throw-expression" and the use of assignment operators. In the case
9684 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9685 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9686 actually essential that we look for an assignment-expression.
9687 For example, cp_parser_initializer_clauses uses this function to
9688 determine whether a particular assignment-expression is in fact
9692 /* Parse the binary expressions (logical-or-expression). */
9693 expression
= cp_parser_binary_expression (parser
, false, false, false,
9694 PREC_NOT_OPERATOR
, NULL
);
9695 /* If the next token is a `?' then we're actually looking at
9696 a conditional-expression; otherwise we're done. */
9697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9698 expression
= cp_parser_question_colon_clause (parser
, expression
);
9701 expression
= cp_parser_assignment_expression (parser
);
9702 /* Restore the old settings. */
9703 parser
->integral_constant_expression_p
9704 = saved_integral_constant_expression_p
;
9705 parser
->allow_non_integral_constant_expression_p
9706 = saved_allow_non_integral_constant_expression_p
;
9707 if (cxx_dialect
>= cxx11
)
9709 /* Require an rvalue constant expression here; that's what our
9710 callers expect. Reference constant expressions are handled
9711 separately in e.g. cp_parser_template_argument. */
9712 tree decay
= expression
;
9713 if (TREE_TYPE (expression
)
9714 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9715 decay
= build_address (expression
);
9716 bool is_const
= potential_rvalue_constant_expression (decay
);
9717 parser
->non_integral_constant_expression_p
= !is_const
;
9718 if (!is_const
&& !allow_non_constant_p
)
9719 require_potential_rvalue_constant_expression (decay
);
9721 if (allow_non_constant_p
)
9722 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9723 parser
->non_integral_constant_expression_p
9724 = saved_non_integral_constant_expression_p
;
9729 /* Parse __builtin_offsetof.
9731 offsetof-expression:
9732 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9734 offsetof-member-designator:
9736 | offsetof-member-designator "." id-expression
9737 | offsetof-member-designator "[" expression "]"
9738 | offsetof-member-designator "->" id-expression */
9741 cp_parser_builtin_offsetof (cp_parser
*parser
)
9743 int save_ice_p
, save_non_ice_p
;
9748 location_t finish_loc
;
9750 /* We're about to accept non-integral-constant things, but will
9751 definitely yield an integral constant expression. Save and
9752 restore these values around our local parsing. */
9753 save_ice_p
= parser
->integral_constant_expression_p
;
9754 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9756 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9758 /* Consume the "__builtin_offsetof" token. */
9759 cp_lexer_consume_token (parser
->lexer
);
9760 /* Consume the opening `('. */
9761 matching_parens parens
;
9762 parens
.require_open (parser
);
9763 /* Parse the type-id. */
9764 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9765 type
= cp_parser_type_id (parser
);
9766 /* Look for the `,'. */
9767 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9768 token
= cp_lexer_peek_token (parser
->lexer
);
9770 /* Build the (type *)null that begins the traditional offsetof macro. */
9772 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9773 tf_warning_or_error
);
9775 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9776 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9777 true, &dummy
, token
->location
);
9780 token
= cp_lexer_peek_token (parser
->lexer
);
9781 switch (token
->type
)
9783 case CPP_OPEN_SQUARE
:
9784 /* offsetof-member-designator "[" expression "]" */
9785 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9790 /* offsetof-member-designator "->" identifier */
9791 expr
= grok_array_decl (token
->location
, expr
,
9792 integer_zero_node
, false);
9796 /* offsetof-member-designator "." identifier */
9797 cp_lexer_consume_token (parser
->lexer
);
9798 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9803 case CPP_CLOSE_PAREN
:
9804 /* Consume the ")" token. */
9805 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9806 cp_lexer_consume_token (parser
->lexer
);
9810 /* Error. We know the following require will fail, but
9811 that gives the proper error message. */
9812 parens
.require_close (parser
);
9813 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9814 expr
= error_mark_node
;
9820 /* Make a location of the form:
9821 __builtin_offsetof (struct s, f)
9822 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9823 with caret at the type-id, ranging from the start of the
9824 "_builtin_offsetof" token to the close paren. */
9825 loc
= make_location (loc
, start_loc
, finish_loc
);
9826 /* The result will be an INTEGER_CST, so we need to explicitly
9827 preserve the location. */
9828 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9831 parser
->integral_constant_expression_p
= save_ice_p
;
9832 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9837 /* Parse a trait expression.
9839 Returns a representation of the expression, the underlying type
9840 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9843 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9846 tree type1
, type2
= NULL_TREE
;
9847 bool binary
= false;
9848 bool variadic
= false;
9852 case RID_HAS_NOTHROW_ASSIGN
:
9853 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9855 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9856 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9858 case RID_HAS_NOTHROW_COPY
:
9859 kind
= CPTK_HAS_NOTHROW_COPY
;
9861 case RID_HAS_TRIVIAL_ASSIGN
:
9862 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9864 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9865 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9867 case RID_HAS_TRIVIAL_COPY
:
9868 kind
= CPTK_HAS_TRIVIAL_COPY
;
9870 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9871 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9873 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9874 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9876 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9877 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9879 case RID_IS_ABSTRACT
:
9880 kind
= CPTK_IS_ABSTRACT
;
9882 case RID_IS_AGGREGATE
:
9883 kind
= CPTK_IS_AGGREGATE
;
9885 case RID_IS_BASE_OF
:
9886 kind
= CPTK_IS_BASE_OF
;
9890 kind
= CPTK_IS_CLASS
;
9893 kind
= CPTK_IS_EMPTY
;
9896 kind
= CPTK_IS_ENUM
;
9899 kind
= CPTK_IS_FINAL
;
9901 case RID_IS_LITERAL_TYPE
:
9902 kind
= CPTK_IS_LITERAL_TYPE
;
9907 case RID_IS_POLYMORPHIC
:
9908 kind
= CPTK_IS_POLYMORPHIC
;
9910 case RID_IS_SAME_AS
:
9911 kind
= CPTK_IS_SAME_AS
;
9914 case RID_IS_STD_LAYOUT
:
9915 kind
= CPTK_IS_STD_LAYOUT
;
9917 case RID_IS_TRIVIAL
:
9918 kind
= CPTK_IS_TRIVIAL
;
9920 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9921 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9924 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9925 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9928 case RID_IS_TRIVIALLY_COPYABLE
:
9929 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9932 kind
= CPTK_IS_UNION
;
9934 case RID_UNDERLYING_TYPE
:
9935 kind
= CPTK_UNDERLYING_TYPE
;
9940 case RID_DIRECT_BASES
:
9941 kind
= CPTK_DIRECT_BASES
;
9943 case RID_IS_ASSIGNABLE
:
9944 kind
= CPTK_IS_ASSIGNABLE
;
9947 case RID_IS_CONSTRUCTIBLE
:
9948 kind
= CPTK_IS_CONSTRUCTIBLE
;
9955 /* Get location of initial token. */
9956 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9958 /* Consume the token. */
9959 cp_lexer_consume_token (parser
->lexer
);
9961 matching_parens parens
;
9962 parens
.require_open (parser
);
9965 type_id_in_expr_sentinel
s (parser
);
9966 type1
= cp_parser_type_id (parser
);
9969 if (type1
== error_mark_node
)
9970 return error_mark_node
;
9974 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9977 type_id_in_expr_sentinel
s (parser
);
9978 type2
= cp_parser_type_id (parser
);
9981 if (type2
== error_mark_node
)
9982 return error_mark_node
;
9986 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9988 cp_lexer_consume_token (parser
->lexer
);
9989 tree elt
= cp_parser_type_id (parser
);
9990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9992 cp_lexer_consume_token (parser
->lexer
);
9993 elt
= make_pack_expansion (elt
);
9995 if (elt
== error_mark_node
)
9996 return error_mark_node
;
9997 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10001 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10002 parens
.require_close (parser
);
10004 /* Construct a location of the form:
10005 __is_trivially_copyable(_Tp)
10006 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10007 with start == caret, finishing at the close-paren. */
10008 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10010 /* Complete the trait expression, which may mean either processing
10011 the trait expr now or saving it for template instantiation. */
10014 case CPTK_UNDERLYING_TYPE
:
10015 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10017 return cp_expr (finish_bases (type1
, false), trait_loc
);
10018 case CPTK_DIRECT_BASES
:
10019 return cp_expr (finish_bases (type1
, true), trait_loc
);
10021 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10025 /* Parse a lambda expression.
10028 lambda-introducer lambda-declarator [opt] compound-statement
10030 Returns a representation of the expression. */
10033 cp_parser_lambda_expression (cp_parser
* parser
)
10035 tree lambda_expr
= build_lambda_expr ();
10038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10039 cp_token_position start
= 0;
10041 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10043 if (cp_unevaluated_operand
)
10045 if (!token
->error_reported
)
10047 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10048 "lambda-expression in unevaluated context");
10049 token
->error_reported
= true;
10053 else if (parser
->in_template_argument_list_p
)
10055 if (!token
->error_reported
)
10057 error_at (token
->location
, "lambda-expression in template-argument");
10058 token
->error_reported
= true;
10063 /* We may be in the middle of deferred access check. Disable
10065 push_deferring_access_checks (dk_no_deferred
);
10067 cp_parser_lambda_introducer (parser
, lambda_expr
);
10069 type
= begin_lambda_type (lambda_expr
);
10070 if (type
== error_mark_node
)
10071 return error_mark_node
;
10073 record_lambda_scope (lambda_expr
);
10075 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10076 determine_visibility (TYPE_NAME (type
));
10078 /* Now that we've started the type, add the capture fields for any
10079 explicit captures. */
10080 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10083 /* Inside the class, surrounding template-parameter-lists do not apply. */
10084 unsigned int saved_num_template_parameter_lists
10085 = parser
->num_template_parameter_lists
;
10086 unsigned char in_statement
= parser
->in_statement
;
10087 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10088 bool fully_implicit_function_template_p
10089 = parser
->fully_implicit_function_template_p
;
10090 tree implicit_template_parms
= parser
->implicit_template_parms
;
10091 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10092 bool auto_is_implicit_function_template_parm_p
10093 = parser
->auto_is_implicit_function_template_parm_p
;
10095 parser
->num_template_parameter_lists
= 0;
10096 parser
->in_statement
= 0;
10097 parser
->in_switch_statement_p
= false;
10098 parser
->fully_implicit_function_template_p
= false;
10099 parser
->implicit_template_parms
= 0;
10100 parser
->implicit_template_scope
= 0;
10101 parser
->auto_is_implicit_function_template_parm_p
= false;
10103 /* By virtue of defining a local class, a lambda expression has access to
10104 the private variables of enclosing classes. */
10106 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10108 if (ok
&& cp_parser_error_occurred (parser
))
10113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10114 && cp_parser_start_tentative_firewall (parser
))
10116 cp_parser_lambda_body (parser
, lambda_expr
);
10118 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10120 if (cp_parser_skip_to_closing_brace (parser
))
10121 cp_lexer_consume_token (parser
->lexer
);
10124 /* The capture list was built up in reverse order; fix that now. */
10125 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10126 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10129 maybe_add_lambda_conv_op (type
);
10131 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10133 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10134 parser
->in_statement
= in_statement
;
10135 parser
->in_switch_statement_p
= in_switch_statement_p
;
10136 parser
->fully_implicit_function_template_p
10137 = fully_implicit_function_template_p
;
10138 parser
->implicit_template_parms
= implicit_template_parms
;
10139 parser
->implicit_template_scope
= implicit_template_scope
;
10140 parser
->auto_is_implicit_function_template_parm_p
10141 = auto_is_implicit_function_template_parm_p
;
10144 /* This field is only used during parsing of the lambda. */
10145 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10147 /* This lambda shouldn't have any proxies left at this point. */
10148 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10149 /* And now that we're done, push proxies for an enclosing lambda. */
10150 insert_pending_capture_proxies ();
10153 lambda_expr
= build_lambda_object (lambda_expr
);
10155 lambda_expr
= error_mark_node
;
10157 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10159 pop_deferring_access_checks ();
10161 return lambda_expr
;
10164 /* Parse the beginning of a lambda expression.
10167 [ lambda-capture [opt] ]
10169 LAMBDA_EXPR is the current representation of the lambda expression. */
10172 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10174 /* Need commas after the first capture. */
10177 /* Eat the leading `['. */
10178 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10180 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10182 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10183 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10184 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10185 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10187 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10189 cp_lexer_consume_token (parser
->lexer
);
10193 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10195 cp_token
* capture_token
;
10197 tree capture_init_expr
;
10198 cp_id_kind idk
= CP_ID_KIND_NONE
;
10199 bool explicit_init_p
= false;
10201 enum capture_kind_type
10206 enum capture_kind_type capture_kind
= BY_COPY
;
10208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10210 error ("expected end of capture-list");
10217 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10219 /* Possibly capture `this'. */
10220 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10222 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10223 if (cxx_dialect
< cxx2a
10224 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10225 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10226 "with by-copy capture default");
10227 cp_lexer_consume_token (parser
->lexer
);
10228 add_capture (lambda_expr
,
10229 /*id=*/this_identifier
,
10230 /*initializer=*/finish_this_expr (),
10231 /*by_reference_p=*/true,
10236 /* Possibly capture `*this'. */
10237 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10238 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10240 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10241 if (cxx_dialect
< cxx17
)
10242 pedwarn (loc
, 0, "%<*this%> capture only available with "
10243 "-std=c++17 or -std=gnu++17");
10244 cp_lexer_consume_token (parser
->lexer
);
10245 cp_lexer_consume_token (parser
->lexer
);
10246 add_capture (lambda_expr
,
10247 /*id=*/this_identifier
,
10248 /*initializer=*/finish_this_expr (),
10249 /*by_reference_p=*/false,
10254 /* Remember whether we want to capture as a reference or not. */
10255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10257 capture_kind
= BY_REFERENCE
;
10258 cp_lexer_consume_token (parser
->lexer
);
10261 /* Get the identifier. */
10262 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10263 capture_id
= cp_parser_identifier (parser
);
10265 if (capture_id
== error_mark_node
)
10266 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10267 delimiters, but I modified this to stop on unnested ']' as well. It
10268 was already changed to stop on unnested '}', so the
10269 "closing_parenthesis" name is no more misleading with my change. */
10271 cp_parser_skip_to_closing_parenthesis (parser
,
10272 /*recovering=*/true,
10274 /*consume_paren=*/true);
10278 /* Find the initializer for this capture. */
10279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10280 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10281 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10283 bool direct
, non_constant
;
10284 /* An explicit initializer exists. */
10285 if (cxx_dialect
< cxx14
)
10286 pedwarn (input_location
, 0,
10287 "lambda capture initializers "
10288 "only available with -std=c++14 or -std=gnu++14");
10289 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10291 explicit_init_p
= true;
10292 if (capture_init_expr
== NULL_TREE
)
10294 error ("empty initializer for lambda init-capture");
10295 capture_init_expr
= error_mark_node
;
10300 const char* error_msg
;
10302 /* Turn the identifier into an id-expression. */
10304 = cp_parser_lookup_name_simple (parser
, capture_id
,
10305 capture_token
->location
);
10307 if (capture_init_expr
== error_mark_node
)
10309 unqualified_name_lookup_error (capture_id
);
10312 else if (DECL_P (capture_init_expr
)
10313 && (!VAR_P (capture_init_expr
)
10314 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10316 error_at (capture_token
->location
,
10317 "capture of non-variable %qD ",
10318 capture_init_expr
);
10319 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10320 "%q#D declared here", capture_init_expr
);
10323 if (VAR_P (capture_init_expr
)
10324 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10326 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10327 "%qD with non-automatic storage duration",
10328 capture_init_expr
))
10329 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10330 "%q#D declared here", capture_init_expr
);
10335 = finish_id_expression
10340 /*integral_constant_expression_p=*/false,
10341 /*allow_non_integral_constant_expression_p=*/false,
10342 /*non_integral_constant_expression_p=*/NULL
,
10343 /*template_p=*/false,
10345 /*address_p=*/false,
10346 /*template_arg_p=*/false,
10348 capture_token
->location
);
10350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10352 cp_lexer_consume_token (parser
->lexer
);
10353 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10356 check_for_bare_parameter_packs (capture_init_expr
);
10359 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10360 && !explicit_init_p
)
10362 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10363 && capture_kind
== BY_COPY
)
10364 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10365 "of %qD redundant with by-copy capture default",
10367 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10368 && capture_kind
== BY_REFERENCE
)
10369 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10370 "capture of %qD redundant with by-reference capture "
10371 "default", capture_id
);
10374 add_capture (lambda_expr
,
10377 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10381 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10384 /* Parse the (optional) middle of a lambda expression.
10387 < template-parameter-list [opt] >
10388 ( parameter-declaration-clause [opt] )
10389 attribute-specifier [opt]
10390 decl-specifier-seq [opt]
10391 exception-specification [opt]
10392 lambda-return-type-clause [opt]
10394 LAMBDA_EXPR is the current representation of the lambda expression. */
10397 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10399 /* 5.1.1.4 of the standard says:
10400 If a lambda-expression does not include a lambda-declarator, it is as if
10401 the lambda-declarator were ().
10402 This means an empty parameter list, no attributes, and no exception
10404 tree param_list
= void_list_node
;
10405 tree attributes
= NULL_TREE
;
10406 tree exception_spec
= NULL_TREE
;
10407 tree template_param_list
= NULL_TREE
;
10408 tree tx_qual
= NULL_TREE
;
10409 tree return_type
= NULL_TREE
;
10410 cp_decl_specifier_seq lambda_specs
;
10411 clear_decl_specs (&lambda_specs
);
10413 /* The template-parameter-list is optional, but must begin with
10414 an opening angle if present. */
10415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10417 if (cxx_dialect
< cxx14
)
10418 pedwarn (parser
->lexer
->next_token
->location
, 0,
10419 "lambda templates are only available with "
10420 "-std=c++14 or -std=gnu++14");
10421 else if (cxx_dialect
< cxx2a
)
10422 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10423 "lambda templates are only available with "
10424 "-std=c++2a or -std=gnu++2a");
10426 cp_lexer_consume_token (parser
->lexer
);
10428 template_param_list
= cp_parser_template_parameter_list (parser
);
10430 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10432 /* We just processed one more parameter list. */
10433 ++parser
->num_template_parameter_lists
;
10436 /* The parameter-declaration-clause is optional (unless
10437 template-parameter-list was given), but must begin with an
10438 opening parenthesis if present. */
10439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10441 matching_parens parens
;
10442 parens
.consume_open (parser
);
10444 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10446 /* Parse parameters. */
10447 param_list
= cp_parser_parameter_declaration_clause (parser
);
10449 /* Default arguments shall not be specified in the
10450 parameter-declaration-clause of a lambda-declarator. */
10451 if (cxx_dialect
< cxx14
)
10452 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10453 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10454 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10455 "default argument specified for lambda parameter");
10457 parens
.require_close (parser
);
10459 attributes
= cp_parser_attributes_opt (parser
);
10461 /* In the decl-specifier-seq of the lambda-declarator, each
10462 decl-specifier shall either be mutable or constexpr. */
10463 int declares_class_or_enum
;
10464 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10465 cp_parser_decl_specifier_seq (parser
,
10466 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10467 &lambda_specs
, &declares_class_or_enum
);
10468 if (lambda_specs
.storage_class
== sc_mutable
)
10470 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10471 if (lambda_specs
.conflicting_specifiers_p
)
10472 error_at (lambda_specs
.locations
[ds_storage_class
],
10473 "duplicate %<mutable%>");
10476 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10478 /* Parse optional exception specification. */
10479 exception_spec
= cp_parser_exception_specification_opt (parser
);
10481 /* Parse optional trailing return type. */
10482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10484 cp_lexer_consume_token (parser
->lexer
);
10485 return_type
= cp_parser_trailing_type_id (parser
);
10488 /* The function parameters must be in scope all the way until after the
10489 trailing-return-type in case of decltype. */
10490 pop_bindings_and_leave_scope ();
10492 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10493 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10495 /* Create the function call operator.
10497 Messing with declarators like this is no uglier than building up the
10498 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10501 cp_decl_specifier_seq return_type_specs
;
10502 cp_declarator
* declarator
;
10507 clear_decl_specs (&return_type_specs
);
10509 return_type_specs
.type
= return_type
;
10511 /* Maybe we will deduce the return type later. */
10512 return_type_specs
.type
= make_auto ();
10514 if (lambda_specs
.locations
[ds_constexpr
])
10516 if (cxx_dialect
>= cxx17
)
10517 return_type_specs
.locations
[ds_constexpr
]
10518 = lambda_specs
.locations
[ds_constexpr
];
10520 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10521 "lambda only available with -std=c++17 or -std=gnu++17");
10524 p
= obstack_alloc (&declarator_obstack
, 0);
10526 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10528 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10529 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10530 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10531 VIRT_SPEC_UNSPECIFIED
,
10535 /*late_return_type=*/NULL_TREE
,
10536 /*requires_clause*/NULL_TREE
);
10537 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10539 fco
= grokmethod (&return_type_specs
,
10542 if (fco
!= error_mark_node
)
10544 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10545 DECL_ARTIFICIAL (fco
) = 1;
10546 /* Give the object parameter a different name. */
10547 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10549 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10551 if (template_param_list
)
10553 fco
= finish_member_template_decl (fco
);
10554 finish_template_decl (template_param_list
);
10555 --parser
->num_template_parameter_lists
;
10557 else if (parser
->fully_implicit_function_template_p
)
10558 fco
= finish_fully_implicit_template (parser
, fco
);
10560 finish_member_declaration (fco
);
10562 obstack_free (&declarator_obstack
, p
);
10564 return (fco
!= error_mark_node
);
10568 /* Parse the body of a lambda expression, which is simply
10572 but which requires special handling.
10573 LAMBDA_EXPR is the current representation of the lambda expression. */
10576 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10578 bool nested
= (current_function_decl
!= NULL_TREE
);
10579 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10580 bool in_function_body
= parser
->in_function_body
;
10582 push_function_context ();
10584 /* Still increment function_depth so that we don't GC in the
10585 middle of an expression. */
10587 vec
<tree
> omp_privatization_save
;
10588 save_omp_privatization_clauses (omp_privatization_save
);
10589 /* Clear this in case we're in the middle of a default argument. */
10590 parser
->local_variables_forbidden_p
= false;
10591 parser
->in_function_body
= true;
10593 /* Finish the function call operator
10595 + late_parsing_for_member
10596 + function_definition_after_declarator
10597 + ctor_initializer_opt_and_function_body */
10599 local_specialization_stack
s (lss_copy
);
10601 tree fco
= lambda_function (lambda_expr
);
10602 tree body
= start_lambda_function (fco
, lambda_expr
);
10604 tree compound_stmt
;
10606 matching_braces braces
;
10607 if (!braces
.require_open (parser
))
10610 compound_stmt
= begin_compound_stmt (0);
10612 /* 5.1.1.4 of the standard says:
10613 If a lambda-expression does not include a trailing-return-type, it
10614 is as if the trailing-return-type denotes the following type:
10615 * if the compound-statement is of the form
10616 { return attribute-specifier [opt] expression ; }
10617 the type of the returned expression after lvalue-to-rvalue
10618 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10619 (_conv.array_ 4.2), and function-to-pointer conversion
10621 * otherwise, void. */
10623 /* In a lambda that has neither a lambda-return-type-clause
10624 nor a deducible form, errors should be reported for return statements
10625 in the body. Since we used void as the placeholder return type, parsing
10626 the body as usual will give such desired behavior. */
10627 if (is_auto (TREE_TYPE (TREE_TYPE (fco
)))
10628 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
10629 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
10631 tree expr
= NULL_TREE
;
10632 cp_id_kind idk
= CP_ID_KIND_NONE
;
10634 /* Parse tentatively in case there's more after the initial return
10636 cp_parser_parse_tentatively (parser
);
10638 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
10640 expr
= cp_parser_expression (parser
, &idk
);
10642 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10643 braces
.require_close (parser
);
10645 if (cp_parser_parse_definitely (parser
))
10647 if (!processing_template_decl
)
10649 tree type
= lambda_return_type (expr
);
10650 apply_deduced_return_type (fco
, type
);
10651 if (type
== error_mark_node
)
10652 expr
= error_mark_node
;
10655 /* Will get error here if type not deduced yet. */
10656 finish_return_stmt (expr
);
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
);
10670 finish_compound_stmt (compound_stmt
);
10673 finish_lambda_function (body
);
10676 restore_omp_privatization_clauses (omp_privatization_save
);
10677 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10678 parser
->in_function_body
= in_function_body
;
10680 pop_function_context();
10685 /* Statements [gram.stmt.stmt] */
10687 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10690 add_debug_begin_stmt (location_t loc
)
10692 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10695 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10696 SET_EXPR_LOCATION (stmt
, loc
);
10700 /* Parse a statement.
10704 expression-statement
10706 selection-statement
10707 iteration-statement
10709 declaration-statement
10716 attribute-specifier-seq (opt) expression-statement
10717 attribute-specifier-seq (opt) compound-statement
10718 attribute-specifier-seq (opt) selection-statement
10719 attribute-specifier-seq (opt) iteration-statement
10720 attribute-specifier-seq (opt) jump-statement
10721 declaration-statement
10722 attribute-specifier-seq (opt) try-block
10725 expression-statement
10733 IN_COMPOUND is true when the statement is nested inside a
10734 cp_parser_compound_statement; this matters for certain pragmas.
10736 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10737 is a (possibly labeled) if statement which is not enclosed in braces
10738 and has an else clause. This is used to implement -Wparentheses.
10740 CHAIN is a vector of if-else-if conditions. */
10743 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10744 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10745 location_t
*loc_after_labels
)
10747 tree statement
, std_attrs
= NULL_TREE
;
10749 location_t statement_location
, attrs_location
;
10754 /* There is no statement yet. */
10755 statement
= NULL_TREE
;
10757 saved_token_sentinel
saved_tokens (parser
->lexer
);
10758 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10759 if (c_dialect_objc ())
10760 /* In obj-c++, seeing '[[' might be the either the beginning of
10761 c++11 attributes, or a nested objc-message-expression. So
10762 let's parse the c++11 attributes tentatively. */
10763 cp_parser_parse_tentatively (parser
);
10764 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10765 if (c_dialect_objc ())
10767 if (!cp_parser_parse_definitely (parser
))
10768 std_attrs
= NULL_TREE
;
10771 /* Peek at the next token. */
10772 token
= cp_lexer_peek_token (parser
->lexer
);
10773 /* Remember the location of the first token in the statement. */
10774 statement_location
= token
->location
;
10775 add_debug_begin_stmt (statement_location
);
10776 /* If this is a keyword, then that will often determine what kind of
10777 statement we have. */
10778 if (token
->type
== CPP_KEYWORD
)
10780 enum rid keyword
= token
->keyword
;
10786 /* Looks like a labeled-statement with a case label.
10787 Parse the label, and then use tail recursion to parse
10789 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10790 in_compound
= false;
10795 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10801 statement
= cp_parser_iteration_statement (parser
, if_p
, false);
10808 statement
= cp_parser_jump_statement (parser
);
10811 /* Objective-C++ exception-handling constructs. */
10814 case RID_AT_FINALLY
:
10815 case RID_AT_SYNCHRONIZED
:
10817 statement
= cp_parser_objc_statement (parser
);
10821 statement
= cp_parser_try_block (parser
);
10824 case RID_NAMESPACE
:
10825 /* This must be a namespace alias definition. */
10826 cp_parser_declaration_statement (parser
);
10829 case RID_TRANSACTION_ATOMIC
:
10830 case RID_TRANSACTION_RELAXED
:
10831 case RID_SYNCHRONIZED
:
10832 case RID_ATOMIC_NOEXCEPT
:
10833 case RID_ATOMIC_CANCEL
:
10834 statement
= cp_parser_transaction (parser
, token
);
10836 case RID_TRANSACTION_CANCEL
:
10837 statement
= cp_parser_transaction_cancel (parser
);
10841 /* It might be a keyword like `int' that can start a
10842 declaration-statement. */
10846 else if (token
->type
== CPP_NAME
)
10848 /* If the next token is a `:', then we are looking at a
10849 labeled-statement. */
10850 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10851 if (token
->type
== CPP_COLON
)
10853 /* Looks like a labeled-statement with an ordinary label.
10854 Parse the label, and then use tail recursion to parse
10857 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10858 in_compound
= false;
10862 /* Anything that starts with a `{' must be a compound-statement. */
10863 else if (token
->type
== CPP_OPEN_BRACE
)
10864 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10865 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10866 a statement all its own. */
10867 else if (token
->type
== CPP_PRAGMA
)
10869 /* Only certain OpenMP pragmas are attached to statements, and thus
10870 are considered statements themselves. All others are not. In
10871 the context of a compound, accept the pragma as a "statement" and
10872 return so that we can check for a close brace. Otherwise we
10873 require a real statement and must go back and read one. */
10875 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10876 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10880 else if (token
->type
== CPP_EOF
)
10882 cp_parser_error (parser
, "expected statement");
10886 /* Everything else must be a declaration-statement or an
10887 expression-statement. Try for the declaration-statement
10888 first, unless we are looking at a `;', in which case we know that
10889 we have an expression-statement. */
10892 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10894 if (std_attrs
!= NULL_TREE
)
10896 /* Attributes should be parsed as part of the the
10897 declaration, so let's un-parse them. */
10898 saved_tokens
.rollback();
10899 std_attrs
= NULL_TREE
;
10902 cp_parser_parse_tentatively (parser
);
10903 /* Try to parse the declaration-statement. */
10904 cp_parser_declaration_statement (parser
);
10905 /* If that worked, we're done. */
10906 if (cp_parser_parse_definitely (parser
))
10909 /* All preceding labels have been parsed at this point. */
10910 if (loc_after_labels
!= NULL
)
10911 *loc_after_labels
= statement_location
;
10913 /* Look for an expression-statement instead. */
10914 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10916 /* Handle [[fallthrough]];. */
10917 if (attribute_fallthrough_p (std_attrs
))
10919 /* The next token after the fallthrough attribute is ';'. */
10920 if (statement
== NULL_TREE
)
10922 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10923 statement
= build_call_expr_internal_loc (statement_location
,
10925 void_type_node
, 0);
10926 finish_expr_stmt (statement
);
10929 warning_at (statement_location
, OPT_Wattributes
,
10930 "%<fallthrough%> attribute not followed by %<;%>");
10931 std_attrs
= NULL_TREE
;
10935 /* Set the line number for the statement. */
10936 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10937 SET_EXPR_LOCATION (statement
, statement_location
);
10939 /* Allow "[[fallthrough]];", but warn otherwise. */
10940 if (std_attrs
!= NULL_TREE
)
10941 warning_at (attrs_location
,
10943 "attributes at the beginning of statement are ignored");
10946 /* Parse the label for a labeled-statement, i.e.
10949 case constant-expression :
10953 case constant-expression ... constant-expression : statement
10955 When a label is parsed without errors, the label is added to the
10956 parse tree by the finish_* functions, so this function doesn't
10957 have to return the label. */
10960 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10963 tree label
= NULL_TREE
;
10964 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10966 /* The next token should be an identifier. */
10967 token
= cp_lexer_peek_token (parser
->lexer
);
10968 if (token
->type
!= CPP_NAME
10969 && token
->type
!= CPP_KEYWORD
)
10971 cp_parser_error (parser
, "expected labeled-statement");
10975 /* Remember whether this case or a user-defined label is allowed to fall
10977 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10979 parser
->colon_corrects_to_scope_p
= false;
10980 switch (token
->keyword
)
10984 tree expr
, expr_hi
;
10985 cp_token
*ellipsis
;
10987 /* Consume the `case' token. */
10988 cp_lexer_consume_token (parser
->lexer
);
10989 /* Parse the constant-expression. */
10990 expr
= cp_parser_constant_expression (parser
);
10991 if (check_for_bare_parameter_packs (expr
))
10992 expr
= error_mark_node
;
10994 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10995 if (ellipsis
->type
== CPP_ELLIPSIS
)
10997 /* Consume the `...' token. */
10998 cp_lexer_consume_token (parser
->lexer
);
10999 expr_hi
= cp_parser_constant_expression (parser
);
11000 if (check_for_bare_parameter_packs (expr_hi
))
11001 expr_hi
= error_mark_node
;
11003 /* We don't need to emit warnings here, as the common code
11004 will do this for us. */
11007 expr_hi
= NULL_TREE
;
11009 if (parser
->in_switch_statement_p
)
11011 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11012 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11013 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11016 error_at (token
->location
,
11017 "case label %qE not within a switch statement",
11023 /* Consume the `default' token. */
11024 cp_lexer_consume_token (parser
->lexer
);
11026 if (parser
->in_switch_statement_p
)
11028 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11029 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11030 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11033 error_at (token
->location
, "case label not within a switch statement");
11037 /* Anything else must be an ordinary label. */
11038 label
= finish_label_stmt (cp_parser_identifier (parser
));
11039 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11040 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11044 /* Require the `:' token. */
11045 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11047 /* An ordinary label may optionally be followed by attributes.
11048 However, this is only permitted if the attributes are then
11049 followed by a semicolon. This is because, for backward
11050 compatibility, when parsing
11051 lab: __attribute__ ((unused)) int i;
11052 we want the attribute to attach to "i", not "lab". */
11053 if (label
!= NULL_TREE
11054 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11057 cp_parser_parse_tentatively (parser
);
11058 attrs
= cp_parser_gnu_attributes_opt (parser
);
11059 if (attrs
== NULL_TREE
11060 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11061 cp_parser_abort_tentative_parse (parser
);
11062 else if (!cp_parser_parse_definitely (parser
))
11065 attributes
= chainon (attributes
, attrs
);
11068 if (attributes
!= NULL_TREE
)
11069 cplus_decl_attributes (&label
, attributes
, 0);
11071 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11074 /* Parse an expression-statement.
11076 expression-statement:
11079 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11080 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11081 indicates whether this expression-statement is part of an
11082 expression statement. */
11085 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11087 tree statement
= NULL_TREE
;
11088 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11089 location_t loc
= token
->location
;
11091 /* There might be attribute fallthrough. */
11092 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11094 /* If the next token is a ';', then there is no expression
11096 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11098 statement
= cp_parser_expression (parser
);
11099 if (statement
== error_mark_node
11100 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11102 cp_parser_skip_to_end_of_block_or_statement (parser
);
11103 return error_mark_node
;
11107 /* Handle [[fallthrough]];. */
11108 if (attribute_fallthrough_p (attr
))
11110 /* The next token after the fallthrough attribute is ';'. */
11111 if (statement
== NULL_TREE
)
11112 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11113 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11114 void_type_node
, 0);
11116 warning_at (loc
, OPT_Wattributes
,
11117 "%<fallthrough%> attribute not followed by %<;%>");
11121 /* Allow "[[fallthrough]];", but warn otherwise. */
11122 if (attr
!= NULL_TREE
)
11123 warning_at (loc
, OPT_Wattributes
,
11124 "attributes at the beginning of statement are ignored");
11126 /* Give a helpful message for "A<T>::type t;" and the like. */
11127 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11128 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11130 if (TREE_CODE (statement
) == SCOPE_REF
)
11131 error_at (token
->location
, "need %<typename%> before %qE because "
11132 "%qT is a dependent scope",
11133 statement
, TREE_OPERAND (statement
, 0));
11134 else if (is_overloaded_fn (statement
)
11135 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11138 tree fn
= get_first_fn (statement
);
11139 error_at (token
->location
,
11140 "%<%T::%D%> names the constructor, not the type",
11141 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11145 /* Consume the final `;'. */
11146 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11148 if (in_statement_expr
11149 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11150 /* This is the final expression statement of a statement
11152 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11153 else if (statement
)
11154 statement
= finish_expr_stmt (statement
);
11159 /* Parse a compound-statement.
11161 compound-statement:
11162 { statement-seq [opt] }
11166 compound-statement:
11167 { label-declaration-seq [opt] statement-seq [opt] }
11169 label-declaration-seq:
11171 label-declaration-seq label-declaration
11173 Returns a tree representing the statement. */
11176 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11177 int bcs_flags
, bool function_body
)
11179 tree compound_stmt
;
11180 matching_braces braces
;
11182 /* Consume the `{'. */
11183 if (!braces
.require_open (parser
))
11184 return error_mark_node
;
11185 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11186 && !function_body
&& cxx_dialect
< cxx14
)
11187 pedwarn (input_location
, OPT_Wpedantic
,
11188 "compound-statement in %<constexpr%> function");
11189 /* Begin the compound-statement. */
11190 compound_stmt
= begin_compound_stmt (bcs_flags
);
11191 /* If the next keyword is `__label__' we have a label declaration. */
11192 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11193 cp_parser_label_declaration (parser
);
11194 /* Parse an (optional) statement-seq. */
11195 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11196 /* Finish the compound-statement. */
11197 finish_compound_stmt (compound_stmt
);
11198 /* Consume the `}'. */
11199 braces
.require_close (parser
);
11201 return compound_stmt
;
11204 /* Parse an (optional) statement-seq.
11208 statement-seq [opt] statement */
11211 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11213 /* Scan statements until there aren't any more. */
11216 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11218 /* If we are looking at a `}', then we have run out of
11219 statements; the same is true if we have reached the end
11220 of file, or have stumbled upon a stray '@end'. */
11221 if (token
->type
== CPP_CLOSE_BRACE
11222 || token
->type
== CPP_EOF
11223 || token
->type
== CPP_PRAGMA_EOL
11224 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11227 /* If we are in a compound statement and find 'else' then
11228 something went wrong. */
11229 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11231 if (parser
->in_statement
& IN_IF_STMT
)
11235 token
= cp_lexer_consume_token (parser
->lexer
);
11236 error_at (token
->location
, "%<else%> without a previous %<if%>");
11240 /* Parse the statement. */
11241 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11245 /* Return true if we're looking at (init; cond), false otherwise. */
11248 cp_parser_init_statement_p (cp_parser
*parser
)
11250 /* Save tokens so that we can put them back. */
11251 cp_lexer_save_tokens (parser
->lexer
);
11253 /* Look for ';' that is not nested in () or {}. */
11254 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11255 /*recovering=*/false,
11257 /*consume_paren=*/false);
11259 /* Roll back the tokens we skipped. */
11260 cp_lexer_rollback_tokens (parser
->lexer
);
11265 /* Parse a selection-statement.
11267 selection-statement:
11268 if ( init-statement [opt] condition ) statement
11269 if ( init-statement [opt] condition ) statement else statement
11270 switch ( init-statement [opt] condition ) statement
11272 Returns the new IF_STMT or SWITCH_STMT.
11274 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11275 is a (possibly labeled) if statement which is not enclosed in
11276 braces and has an else clause. This is used to implement
11279 CHAIN is a vector of if-else-if conditions. This is used to implement
11280 -Wduplicated-cond. */
11283 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11288 token_indent_info guard_tinfo
;
11293 /* Peek at the next token. */
11294 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11295 guard_tinfo
= get_token_indent_info (token
);
11297 /* See what kind of keyword it is. */
11298 keyword
= token
->keyword
;
11308 if (keyword
== RID_IF
11309 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11313 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11314 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11315 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11316 "with -std=c++17 or -std=gnu++17");
11319 /* Look for the `('. */
11320 matching_parens parens
;
11321 if (!parens
.require_open (parser
))
11323 cp_parser_skip_to_end_of_statement (parser
);
11324 return error_mark_node
;
11327 /* Begin the selection-statement. */
11328 if (keyword
== RID_IF
)
11330 statement
= begin_if_stmt ();
11331 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11334 statement
= begin_switch_stmt ();
11336 /* Parse the optional init-statement. */
11337 if (cp_parser_init_statement_p (parser
))
11340 if (cxx_dialect
< cxx17
)
11341 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11342 "init-statement in selection statements only available "
11343 "with -std=c++17 or -std=gnu++17");
11344 cp_parser_init_statement (parser
, &decl
);
11347 /* Parse the condition. */
11348 condition
= cp_parser_condition (parser
);
11349 /* Look for the `)'. */
11350 if (!parens
.require_close (parser
))
11351 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11352 /*consume_paren=*/true);
11354 if (keyword
== RID_IF
)
11357 unsigned char in_statement
;
11359 /* Add the condition. */
11360 condition
= finish_if_stmt_cond (condition
, statement
);
11362 if (warn_duplicated_cond
)
11363 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11366 /* Parse the then-clause. */
11367 in_statement
= parser
->in_statement
;
11368 parser
->in_statement
|= IN_IF_STMT
;
11370 /* Outside a template, the non-selected branch of a constexpr
11371 if is a 'discarded statement', i.e. unevaluated. */
11372 bool was_discarded
= in_discarded_stmt
;
11373 bool discard_then
= (cx
&& !processing_template_decl
11374 && integer_zerop (condition
));
11377 in_discarded_stmt
= true;
11378 ++c_inhibit_evaluation_warnings
;
11381 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11384 parser
->in_statement
= in_statement
;
11386 finish_then_clause (statement
);
11390 THEN_CLAUSE (statement
) = NULL_TREE
;
11391 in_discarded_stmt
= was_discarded
;
11392 --c_inhibit_evaluation_warnings
;
11395 /* If the next token is `else', parse the else-clause. */
11396 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11399 bool discard_else
= (cx
&& !processing_template_decl
11400 && integer_nonzerop (condition
));
11403 in_discarded_stmt
= true;
11404 ++c_inhibit_evaluation_warnings
;
11408 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11409 /* Consume the `else' keyword. */
11410 cp_lexer_consume_token (parser
->lexer
);
11411 if (warn_duplicated_cond
)
11413 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11417 /* We've got "if (COND) else if (COND2)". Start
11418 the condition chain and add COND as the first
11420 chain
= new vec
<tree
> ();
11421 if (!CONSTANT_CLASS_P (condition
)
11422 && !TREE_SIDE_EFFECTS (condition
))
11424 /* Wrap it in a NOP_EXPR so that we can set the
11425 location of the condition. */
11426 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11428 SET_EXPR_LOCATION (e
, token
->location
);
11429 chain
->safe_push (e
);
11432 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11435 /* This is if-else without subsequent if. Zap the
11436 condition chain; we would have already warned at
11442 begin_else_clause (statement
);
11443 /* Parse the else-clause. */
11444 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11445 guard_tinfo
, chain
);
11447 finish_else_clause (statement
);
11449 /* If we are currently parsing a then-clause, then
11450 IF_P will not be NULL. We set it to true to
11451 indicate that this if statement has an else clause.
11452 This may trigger the Wparentheses warning below
11453 when we get back up to the parent if statement. */
11459 ELSE_CLAUSE (statement
) = NULL_TREE
;
11460 in_discarded_stmt
= was_discarded
;
11461 --c_inhibit_evaluation_warnings
;
11466 /* This if statement does not have an else clause. If
11467 NESTED_IF is true, then the then-clause has an if
11468 statement which does have an else clause. We warn
11469 about the potential ambiguity. */
11471 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11472 "suggest explicit braces to avoid ambiguous"
11474 if (warn_duplicated_cond
)
11476 /* We don't need the condition chain anymore. */
11482 /* Now we're all done with the if-statement. */
11483 finish_if_stmt (statement
);
11487 bool in_switch_statement_p
;
11488 unsigned char in_statement
;
11490 /* Add the condition. */
11491 finish_switch_cond (condition
, statement
);
11493 /* Parse the body of the switch-statement. */
11494 in_switch_statement_p
= parser
->in_switch_statement_p
;
11495 in_statement
= parser
->in_statement
;
11496 parser
->in_switch_statement_p
= true;
11497 parser
->in_statement
|= IN_SWITCH_STMT
;
11498 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11500 parser
->in_switch_statement_p
= in_switch_statement_p
;
11501 parser
->in_statement
= in_statement
;
11503 /* Now we're all done with the switch-statement. */
11504 finish_switch_stmt (statement
);
11512 cp_parser_error (parser
, "expected selection-statement");
11513 return error_mark_node
;
11517 /* Parse a condition.
11521 type-specifier-seq declarator = initializer-clause
11522 type-specifier-seq declarator braced-init-list
11527 type-specifier-seq declarator asm-specification [opt]
11528 attributes [opt] = assignment-expression
11530 Returns the expression that should be tested. */
11533 cp_parser_condition (cp_parser
* parser
)
11535 cp_decl_specifier_seq type_specifiers
;
11536 const char *saved_message
;
11537 int declares_class_or_enum
;
11539 /* Try the declaration first. */
11540 cp_parser_parse_tentatively (parser
);
11541 /* New types are not allowed in the type-specifier-seq for a
11543 saved_message
= parser
->type_definition_forbidden_message
;
11544 parser
->type_definition_forbidden_message
11545 = G_("types may not be defined in conditions");
11546 /* Parse the type-specifier-seq. */
11547 cp_parser_decl_specifier_seq (parser
,
11548 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11550 &declares_class_or_enum
);
11551 /* Restore the saved message. */
11552 parser
->type_definition_forbidden_message
= saved_message
;
11553 /* If all is well, we might be looking at a declaration. */
11554 if (!cp_parser_error_occurred (parser
))
11557 tree asm_specification
;
11559 cp_declarator
*declarator
;
11560 tree initializer
= NULL_TREE
;
11562 /* Parse the declarator. */
11563 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11564 /*ctor_dtor_or_conv_p=*/NULL
,
11565 /*parenthesized_p=*/NULL
,
11566 /*member_p=*/false,
11567 /*friend_p=*/false);
11568 /* Parse the attributes. */
11569 attributes
= cp_parser_attributes_opt (parser
);
11570 /* Parse the asm-specification. */
11571 asm_specification
= cp_parser_asm_specification_opt (parser
);
11572 /* If the next token is not an `=' or '{', then we might still be
11573 looking at an expression. For example:
11577 looks like a decl-specifier-seq and a declarator -- but then
11578 there is no `=', so this is an expression. */
11579 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11580 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11581 cp_parser_simulate_error (parser
);
11583 /* If we did see an `=' or '{', then we are looking at a declaration
11585 if (cp_parser_parse_definitely (parser
))
11588 bool non_constant_p
;
11589 int flags
= LOOKUP_ONLYCONVERTING
;
11591 /* Create the declaration. */
11592 decl
= start_decl (declarator
, &type_specifiers
,
11593 /*initialized_p=*/true,
11594 attributes
, /*prefix_attributes=*/NULL_TREE
,
11597 /* Parse the initializer. */
11598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11600 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11601 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11606 /* Consume the `='. */
11607 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11608 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11610 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11611 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11613 /* Process the initializer. */
11614 cp_finish_decl (decl
,
11615 initializer
, !non_constant_p
,
11620 pop_scope (pushed_scope
);
11622 return convert_from_reference (decl
);
11625 /* If we didn't even get past the declarator successfully, we are
11626 definitely not looking at a declaration. */
11628 cp_parser_abort_tentative_parse (parser
);
11630 /* Otherwise, we are looking at an expression. */
11631 return cp_parser_expression (parser
);
11634 /* Parses a for-statement or range-for-statement until the closing ')',
11638 cp_parser_for (cp_parser
*parser
, bool ivdep
)
11640 tree init
, scope
, decl
;
11643 /* Begin the for-statement. */
11644 scope
= begin_for_scope (&init
);
11646 /* Parse the initialization. */
11647 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11650 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
11652 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
11656 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
11658 /* Normal for loop */
11659 tree condition
= NULL_TREE
;
11660 tree expression
= NULL_TREE
;
11663 stmt
= begin_for_stmt (scope
, init
);
11664 /* The init-statement has already been parsed in
11665 cp_parser_init_statement, so no work is needed here. */
11666 finish_init_stmt (stmt
);
11668 /* If there's a condition, process it. */
11669 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11670 condition
= cp_parser_condition (parser
);
11673 cp_parser_error (parser
, "missing loop condition in loop with "
11674 "%<GCC ivdep%> pragma");
11675 condition
= error_mark_node
;
11677 finish_for_cond (condition
, stmt
, ivdep
);
11678 /* Look for the `;'. */
11679 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11681 /* If there's an expression, process it. */
11682 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11683 expression
= cp_parser_expression (parser
);
11684 finish_for_expr (expression
, stmt
);
11689 /* Tries to parse a range-based for-statement:
11692 decl-specifier-seq declarator : expression
11694 The decl-specifier-seq declarator and the `:' are already parsed by
11695 cp_parser_init_statement. If processing_template_decl it returns a
11696 newly created RANGE_FOR_STMT; if not, it is converted to a
11697 regular FOR_STMT. */
11700 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11703 tree stmt
, range_expr
;
11704 auto_vec
<cxx_binding
*, 16> bindings
;
11705 auto_vec
<tree
, 16> names
;
11706 tree decomp_first_name
= NULL_TREE
;
11707 unsigned int decomp_cnt
= 0;
11709 /* Get the range declaration momentarily out of the way so that
11710 the range expression doesn't clash with it. */
11711 if (range_decl
!= error_mark_node
)
11713 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11715 tree v
= DECL_VALUE_EXPR (range_decl
);
11716 /* For decomposition declaration get all of the corresponding
11717 declarations out of the way. */
11718 if (TREE_CODE (v
) == ARRAY_REF
11719 && VAR_P (TREE_OPERAND (v
, 0))
11720 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11722 tree d
= range_decl
;
11723 range_decl
= TREE_OPERAND (v
, 0);
11724 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11725 decomp_first_name
= d
;
11726 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11728 tree name
= DECL_NAME (d
);
11729 names
.safe_push (name
);
11730 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11731 IDENTIFIER_BINDING (name
)
11732 = IDENTIFIER_BINDING (name
)->previous
;
11736 if (names
.is_empty ())
11738 tree name
= DECL_NAME (range_decl
);
11739 names
.safe_push (name
);
11740 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11741 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11745 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11747 bool expr_non_constant_p
;
11748 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11751 range_expr
= cp_parser_expression (parser
);
11753 /* Put the range declaration(s) back into scope. */
11754 for (unsigned int i
= 0; i
< names
.length (); i
++)
11756 cxx_binding
*binding
= bindings
[i
];
11757 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11758 IDENTIFIER_BINDING (names
[i
]) = binding
;
11761 /* If in template, STMT is converted to a normal for-statement
11762 at instantiation. If not, it is done just ahead. */
11763 if (processing_template_decl
)
11765 if (check_for_bare_parameter_packs (range_expr
))
11766 range_expr
= error_mark_node
;
11767 stmt
= begin_range_for_stmt (scope
, init
);
11769 RANGE_FOR_IVDEP (stmt
) = 1;
11770 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11771 if (!type_dependent_expression_p (range_expr
)
11772 /* do_auto_deduction doesn't mess with template init-lists. */
11773 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11774 do_range_for_auto_deduction (range_decl
, range_expr
);
11778 stmt
= begin_for_stmt (scope
, init
);
11779 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11780 decomp_first_name
, decomp_cnt
, ivdep
);
11785 /* Subroutine of cp_convert_range_for: given the initializer expression,
11786 builds up the range temporary. */
11789 build_range_temp (tree range_expr
)
11791 tree range_type
, range_temp
;
11793 /* Find out the type deduced by the declaration
11794 `auto &&__range = range_expr'. */
11795 range_type
= cp_build_reference_type (make_auto (), true);
11796 range_type
= do_auto_deduction (range_type
, range_expr
,
11797 type_uses_auto (range_type
));
11799 /* Create the __range variable. */
11800 range_temp
= build_decl (input_location
, VAR_DECL
,
11801 get_identifier ("__for_range"), range_type
);
11802 TREE_USED (range_temp
) = 1;
11803 DECL_ARTIFICIAL (range_temp
) = 1;
11808 /* Used by cp_parser_range_for in template context: we aren't going to
11809 do a full conversion yet, but we still need to resolve auto in the
11810 type of the for-range-declaration if present. This is basically
11811 a shortcut version of cp_convert_range_for. */
11814 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11816 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11819 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11820 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11821 iter_type
= (cp_parser_perform_range_for_lookup
11822 (range_temp
, &begin_dummy
, &end_dummy
));
11825 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11827 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11829 tf_warning_or_error
);
11830 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11831 iter_decl
, auto_node
);
11836 /* Converts a range-based for-statement into a normal
11837 for-statement, as per the definition.
11839 for (RANGE_DECL : RANGE_EXPR)
11842 should be equivalent to:
11845 auto &&__range = RANGE_EXPR;
11846 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11850 RANGE_DECL = *__begin;
11855 If RANGE_EXPR is an array:
11856 BEGIN_EXPR = __range
11857 END_EXPR = __range + ARRAY_SIZE(__range)
11858 Else if RANGE_EXPR has a member 'begin' or 'end':
11859 BEGIN_EXPR = __range.begin()
11860 END_EXPR = __range.end()
11862 BEGIN_EXPR = begin(__range)
11863 END_EXPR = end(__range);
11865 If __range has a member 'begin' but not 'end', or vice versa, we must
11866 still use the second alternative (it will surely fail, however).
11867 When calling begin()/end() in the third alternative we must use
11868 argument dependent lookup, but always considering 'std' as an associated
11872 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11873 tree decomp_first_name
, unsigned int decomp_cnt
,
11877 tree iter_type
, begin_expr
, end_expr
;
11878 tree condition
, expression
;
11880 range_expr
= mark_lvalue_use (range_expr
);
11882 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11883 /* If an error happened previously do nothing or else a lot of
11884 unhelpful errors would be issued. */
11885 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11890 if (VAR_P (range_expr
)
11891 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11892 /* Can't bind a reference to an array of runtime bound. */
11893 range_temp
= range_expr
;
11896 range_temp
= build_range_temp (range_expr
);
11897 pushdecl (range_temp
);
11898 cp_finish_decl (range_temp
, range_expr
,
11899 /*is_constant_init*/false, NULL_TREE
,
11900 LOOKUP_ONLYCONVERTING
);
11901 range_temp
= convert_from_reference (range_temp
);
11903 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11904 &begin_expr
, &end_expr
);
11907 /* The new for initialization statement. */
11908 begin
= build_decl (input_location
, VAR_DECL
,
11909 get_identifier ("__for_begin"), iter_type
);
11910 TREE_USED (begin
) = 1;
11911 DECL_ARTIFICIAL (begin
) = 1;
11913 cp_finish_decl (begin
, begin_expr
,
11914 /*is_constant_init*/false, NULL_TREE
,
11915 LOOKUP_ONLYCONVERTING
);
11917 if (cxx_dialect
>= cxx17
)
11918 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11919 end
= build_decl (input_location
, VAR_DECL
,
11920 get_identifier ("__for_end"), iter_type
);
11921 TREE_USED (end
) = 1;
11922 DECL_ARTIFICIAL (end
) = 1;
11924 cp_finish_decl (end
, end_expr
,
11925 /*is_constant_init*/false, NULL_TREE
,
11926 LOOKUP_ONLYCONVERTING
);
11928 finish_init_stmt (statement
);
11930 /* The new for condition. */
11931 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11934 NULL
, tf_warning_or_error
);
11935 finish_for_cond (condition
, statement
, ivdep
);
11937 /* The new increment expression. */
11938 expression
= finish_unary_op_expr (input_location
,
11939 PREINCREMENT_EXPR
, begin
,
11940 tf_warning_or_error
);
11941 finish_for_expr (expression
, statement
);
11943 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11944 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11946 /* The declaration is initialized with *__begin inside the loop body. */
11947 cp_finish_decl (range_decl
,
11948 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11949 tf_warning_or_error
),
11950 /*is_constant_init*/false, NULL_TREE
,
11951 LOOKUP_ONLYCONVERTING
);
11952 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11953 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11958 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11959 We need to solve both at the same time because the method used
11960 depends on the existence of members begin or end.
11961 Returns the type deduced for the iterator expression. */
11964 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11966 if (error_operand_p (range
))
11968 *begin
= *end
= error_mark_node
;
11969 return error_mark_node
;
11972 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11974 error ("range-based %<for%> expression of type %qT "
11975 "has incomplete type", TREE_TYPE (range
));
11976 *begin
= *end
= error_mark_node
;
11977 return error_mark_node
;
11979 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11981 /* If RANGE is an array, we will use pointer arithmetic. */
11982 *begin
= decay_conversion (range
, tf_warning_or_error
);
11983 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11985 array_type_nelts_top (TREE_TYPE (range
)),
11987 return TREE_TYPE (*begin
);
11991 /* If it is not an array, we must do a bit of magic. */
11992 tree id_begin
, id_end
;
11993 tree member_begin
, member_end
;
11995 *begin
= *end
= error_mark_node
;
11997 id_begin
= get_identifier ("begin");
11998 id_end
= get_identifier ("end");
11999 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12000 /*protect=*/2, /*want_type=*/false,
12001 tf_warning_or_error
);
12002 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12003 /*protect=*/2, /*want_type=*/false,
12004 tf_warning_or_error
);
12006 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
12008 /* Use the member functions. */
12009 if (member_begin
!= NULL_TREE
)
12010 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12012 error ("range-based %<for%> expression of type %qT has an "
12013 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
12015 if (member_end
!= NULL_TREE
)
12016 *end
= cp_parser_range_for_member_function (range
, id_end
);
12018 error ("range-based %<for%> expression of type %qT has a "
12019 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
12023 /* Use global functions with ADL. */
12024 vec
<tree
, va_gc
> *vec
;
12025 vec
= make_tree_vector ();
12027 vec_safe_push (vec
, range
);
12029 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12030 tf_warning_or_error
);
12031 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12032 tf_warning_or_error
);
12033 member_end
= perform_koenig_lookup (id_end
, vec
,
12034 tf_warning_or_error
);
12035 *end
= finish_call_expr (member_end
, &vec
, false, true,
12036 tf_warning_or_error
);
12038 release_tree_vector (vec
);
12041 /* Last common checks. */
12042 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12044 /* If one of the expressions is an error do no more checks. */
12045 *begin
= *end
= error_mark_node
;
12046 return error_mark_node
;
12048 else if (type_dependent_expression_p (*begin
)
12049 || type_dependent_expression_p (*end
))
12050 /* Can happen, when, eg, in a template context, Koenig lookup
12051 can't resolve begin/end (c++/58503). */
12055 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12056 /* The unqualified type of the __begin and __end temporaries should
12057 be the same, as required by the multiple auto declaration. */
12058 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12060 if (cxx_dialect
>= cxx17
12061 && (build_x_binary_op (input_location
, NE_EXPR
,
12062 *begin
, ERROR_MARK
,
12065 != error_mark_node
))
12066 /* P0184R0 allows __begin and __end to have different types,
12067 but make sure they are comparable so we can give a better
12070 error ("inconsistent begin/end types in range-based %<for%> "
12071 "statement: %qT and %qT",
12072 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12079 /* Helper function for cp_parser_perform_range_for_lookup.
12080 Builds a tree for RANGE.IDENTIFIER(). */
12083 cp_parser_range_for_member_function (tree range
, tree identifier
)
12086 vec
<tree
, va_gc
> *vec
;
12088 member
= finish_class_member_access_expr (range
, identifier
,
12089 false, tf_warning_or_error
);
12090 if (member
== error_mark_node
)
12091 return error_mark_node
;
12093 vec
= make_tree_vector ();
12094 res
= finish_call_expr (member
, &vec
,
12095 /*disallow_virtual=*/false,
12096 /*koenig_p=*/false,
12097 tf_warning_or_error
);
12098 release_tree_vector (vec
);
12102 /* Parse an iteration-statement.
12104 iteration-statement:
12105 while ( condition ) statement
12106 do statement while ( expression ) ;
12107 for ( init-statement condition [opt] ; expression [opt] )
12110 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12113 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
)
12118 unsigned char in_statement
;
12119 token_indent_info guard_tinfo
;
12121 /* Peek at the next token. */
12122 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12124 return error_mark_node
;
12126 guard_tinfo
= get_token_indent_info (token
);
12128 /* Remember whether or not we are already within an iteration
12130 in_statement
= parser
->in_statement
;
12132 /* See what kind of keyword it is. */
12133 keyword
= token
->keyword
;
12140 /* Begin the while-statement. */
12141 statement
= begin_while_stmt ();
12142 /* Look for the `('. */
12143 matching_parens parens
;
12144 parens
.require_open (parser
);
12145 /* Parse the condition. */
12146 condition
= cp_parser_condition (parser
);
12147 finish_while_stmt_cond (condition
, statement
, ivdep
);
12148 /* Look for the `)'. */
12149 parens
.require_close (parser
);
12150 /* Parse the dependent statement. */
12151 parser
->in_statement
= IN_ITERATION_STMT
;
12152 bool prev
= note_iteration_stmt_body_start ();
12153 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12154 note_iteration_stmt_body_end (prev
);
12155 parser
->in_statement
= in_statement
;
12156 /* We're done with the while-statement. */
12157 finish_while_stmt (statement
);
12165 /* Begin the do-statement. */
12166 statement
= begin_do_stmt ();
12167 /* Parse the body of the do-statement. */
12168 parser
->in_statement
= IN_ITERATION_STMT
;
12169 bool prev
= note_iteration_stmt_body_start ();
12170 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12171 note_iteration_stmt_body_end (prev
);
12172 parser
->in_statement
= in_statement
;
12173 finish_do_body (statement
);
12174 /* Look for the `while' keyword. */
12175 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12176 /* Look for the `('. */
12177 matching_parens parens
;
12178 parens
.require_open (parser
);
12179 /* Parse the expression. */
12180 expression
= cp_parser_expression (parser
);
12181 /* We're done with the do-statement. */
12182 finish_do_stmt (expression
, statement
, ivdep
);
12183 /* Look for the `)'. */
12184 parens
.require_close (parser
);
12185 /* Look for the `;'. */
12186 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12192 /* Look for the `('. */
12193 matching_parens parens
;
12194 parens
.require_open (parser
);
12196 statement
= cp_parser_for (parser
, ivdep
);
12198 /* Look for the `)'. */
12199 parens
.require_close (parser
);
12201 /* Parse the body of the for-statement. */
12202 parser
->in_statement
= IN_ITERATION_STMT
;
12203 bool prev
= note_iteration_stmt_body_start ();
12204 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12205 note_iteration_stmt_body_end (prev
);
12206 parser
->in_statement
= in_statement
;
12208 /* We're done with the for-statement. */
12209 finish_for_stmt (statement
);
12214 cp_parser_error (parser
, "expected iteration-statement");
12215 statement
= error_mark_node
;
12222 /* Parse a init-statement or the declarator of a range-based-for.
12223 Returns true if a range-based-for declaration is seen.
12226 expression-statement
12227 simple-declaration */
12230 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12232 /* If the next token is a `;', then we have an empty
12233 expression-statement. Grammatically, this is also a
12234 simple-declaration, but an invalid one, because it does not
12235 declare anything. Therefore, if we did not handle this case
12236 specially, we would issue an error message about an invalid
12238 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12240 bool is_range_for
= false;
12241 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12243 /* A colon is used in range-based for. */
12244 parser
->colon_corrects_to_scope_p
= false;
12246 /* We're going to speculatively look for a declaration, falling back
12247 to an expression, if necessary. */
12248 cp_parser_parse_tentatively (parser
);
12249 /* Parse the declaration. */
12250 cp_parser_simple_declaration (parser
,
12251 /*function_definition_allowed_p=*/false,
12253 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12256 /* It is a range-for, consume the ':' */
12257 cp_lexer_consume_token (parser
->lexer
);
12258 is_range_for
= true;
12259 if (cxx_dialect
< cxx11
)
12261 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12262 "range-based %<for%> loops only available with "
12263 "-std=c++11 or -std=gnu++11");
12264 *decl
= error_mark_node
;
12268 /* The ';' is not consumed yet because we told
12269 cp_parser_simple_declaration not to. */
12270 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12272 if (cp_parser_parse_definitely (parser
))
12273 return is_range_for
;
12274 /* If the tentative parse failed, then we shall need to look for an
12275 expression-statement. */
12277 /* If we are here, it is an expression-statement. */
12278 cp_parser_expression_statement (parser
, NULL_TREE
);
12282 /* Parse a jump-statement.
12287 return expression [opt] ;
12288 return braced-init-list ;
12294 goto * expression ;
12296 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12299 cp_parser_jump_statement (cp_parser
* parser
)
12301 tree statement
= error_mark_node
;
12304 unsigned char in_statement
;
12306 /* Peek at the next token. */
12307 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12309 return error_mark_node
;
12311 /* See what kind of keyword it is. */
12312 keyword
= token
->keyword
;
12316 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12317 switch (in_statement
)
12320 error_at (token
->location
, "break statement not within loop or switch");
12323 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12324 || in_statement
== IN_ITERATION_STMT
);
12325 statement
= finish_break_stmt ();
12326 if (in_statement
== IN_ITERATION_STMT
)
12327 break_maybe_infinite_loop ();
12330 error_at (token
->location
, "invalid exit from OpenMP structured block");
12333 error_at (token
->location
, "break statement used with OpenMP for loop");
12336 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12340 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12343 error_at (token
->location
, "continue statement not within a loop");
12345 /* Fall through. */
12346 case IN_ITERATION_STMT
:
12348 statement
= finish_continue_stmt ();
12351 error_at (token
->location
, "invalid exit from OpenMP structured block");
12354 gcc_unreachable ();
12356 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12362 bool expr_non_constant_p
;
12364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12366 cp_lexer_set_source_position (parser
->lexer
);
12367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12368 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12370 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12371 expr
= cp_parser_expression (parser
);
12373 /* If the next token is a `;', then there is no
12376 /* Build the return-statement. */
12377 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12378 /* Don't deduce from a discarded return statement. */;
12380 statement
= finish_return_stmt (expr
);
12381 /* Look for the final `;'. */
12382 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12387 if (parser
->in_function_body
12388 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12390 error ("%<goto%> in %<constexpr%> function");
12391 cp_function_chain
->invalid_constexpr
= true;
12394 /* Create the goto-statement. */
12395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12397 /* Issue a warning about this use of a GNU extension. */
12398 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12399 /* Consume the '*' token. */
12400 cp_lexer_consume_token (parser
->lexer
);
12401 /* Parse the dependent expression. */
12402 finish_goto_stmt (cp_parser_expression (parser
));
12405 finish_goto_stmt (cp_parser_identifier (parser
));
12406 /* Look for the final `;'. */
12407 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12411 cp_parser_error (parser
, "expected jump-statement");
12418 /* Parse a declaration-statement.
12420 declaration-statement:
12421 block-declaration */
12424 cp_parser_declaration_statement (cp_parser
* parser
)
12428 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12429 p
= obstack_alloc (&declarator_obstack
, 0);
12431 /* Parse the block-declaration. */
12432 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12434 /* Free any declarators allocated. */
12435 obstack_free (&declarator_obstack
, p
);
12438 /* Some dependent statements (like `if (cond) statement'), are
12439 implicitly in their own scope. In other words, if the statement is
12440 a single statement (as opposed to a compound-statement), it is
12441 none-the-less treated as if it were enclosed in braces. Any
12442 declarations appearing in the dependent statement are out of scope
12443 after control passes that point. This function parses a statement,
12444 but ensures that is in its own scope, even if it is not a
12445 compound-statement.
12447 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12448 is a (possibly labeled) if statement which is not enclosed in
12449 braces and has an else clause. This is used to implement
12452 CHAIN is a vector of if-else-if conditions. This is used to implement
12455 Returns the new statement. */
12458 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12459 const token_indent_info
&guard_tinfo
,
12463 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12464 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12465 token_indent_info body_tinfo
12466 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12471 /* Mark if () ; with a special NOP_EXPR. */
12472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12474 cp_lexer_consume_token (parser
->lexer
);
12475 statement
= add_stmt (build_empty_stmt (body_loc
));
12477 if (guard_tinfo
.keyword
== RID_IF
12478 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12479 warning_at (body_loc
, OPT_Wempty_body
,
12480 "suggest braces around empty body in an %<if%> statement");
12481 else if (guard_tinfo
.keyword
== RID_ELSE
)
12482 warning_at (body_loc
, OPT_Wempty_body
,
12483 "suggest braces around empty body in an %<else%> statement");
12485 /* if a compound is opened, we simply parse the statement directly. */
12486 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12487 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12488 /* If the token is not a `{', then we must take special action. */
12491 /* Create a compound-statement. */
12492 statement
= begin_compound_stmt (0);
12493 /* Parse the dependent-statement. */
12494 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12495 &body_loc_after_labels
);
12496 /* Finish the dummy compound-statement. */
12497 finish_compound_stmt (statement
);
12500 token_indent_info next_tinfo
12501 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12502 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12504 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12505 && next_tinfo
.type
!= CPP_SEMICOLON
)
12506 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12507 guard_tinfo
.location
, guard_tinfo
.keyword
);
12509 /* Return the statement. */
12513 /* For some dependent statements (like `while (cond) statement'), we
12514 have already created a scope. Therefore, even if the dependent
12515 statement is a compound-statement, we do not want to create another
12519 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12520 const token_indent_info
&guard_tinfo
)
12522 /* If the token is a `{', then we must take special action. */
12523 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12525 token_indent_info body_tinfo
12526 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12527 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12529 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12530 &loc_after_labels
);
12531 token_indent_info next_tinfo
12532 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12533 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12535 if (loc_after_labels
!= UNKNOWN_LOCATION
12536 && next_tinfo
.type
!= CPP_SEMICOLON
)
12537 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12538 guard_tinfo
.location
,
12539 guard_tinfo
.keyword
);
12543 /* Avoid calling cp_parser_compound_statement, so that we
12544 don't create a new scope. Do everything else by hand. */
12545 matching_braces braces
;
12546 braces
.require_open (parser
);
12547 /* If the next keyword is `__label__' we have a label declaration. */
12548 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12549 cp_parser_label_declaration (parser
);
12550 /* Parse an (optional) statement-seq. */
12551 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12552 braces
.require_close (parser
);
12556 /* Declarations [gram.dcl.dcl] */
12558 /* Parse an optional declaration-sequence.
12562 declaration-seq declaration */
12565 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12571 token
= cp_lexer_peek_token (parser
->lexer
);
12573 if (token
->type
== CPP_CLOSE_BRACE
12574 || token
->type
== CPP_EOF
12575 || token
->type
== CPP_PRAGMA_EOL
)
12578 if (token
->type
== CPP_SEMICOLON
)
12580 /* A declaration consisting of a single semicolon is
12581 invalid. Allow it unless we're being pedantic. */
12582 cp_lexer_consume_token (parser
->lexer
);
12583 if (!in_system_header_at (input_location
))
12584 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12588 /* If we're entering or exiting a region that's implicitly
12589 extern "C", modify the lang context appropriately. */
12590 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12592 push_lang_context (lang_name_c
);
12593 parser
->implicit_extern_c
= true;
12595 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12597 pop_lang_context ();
12598 parser
->implicit_extern_c
= false;
12601 if (token
->type
== CPP_PRAGMA
)
12603 /* A top-level declaration can consist solely of a #pragma.
12604 A nested declaration cannot, so this is done here and not
12605 in cp_parser_declaration. (A #pragma at block scope is
12606 handled in cp_parser_statement.) */
12607 cp_parser_pragma (parser
, pragma_external
, NULL
);
12611 /* Parse the declaration itself. */
12612 cp_parser_declaration (parser
);
12616 /* Parse a declaration.
12620 function-definition
12621 template-declaration
12622 explicit-instantiation
12623 explicit-specialization
12624 linkage-specification
12625 namespace-definition
12633 __extension__ declaration */
12636 cp_parser_declaration (cp_parser
* parser
)
12640 int saved_pedantic
;
12642 tree attributes
= NULL_TREE
;
12644 /* Check for the `__extension__' keyword. */
12645 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12647 /* Parse the qualified declaration. */
12648 cp_parser_declaration (parser
);
12649 /* Restore the PEDANTIC flag. */
12650 pedantic
= saved_pedantic
;
12655 /* Try to figure out what kind of declaration is present. */
12656 token1
= *cp_lexer_peek_token (parser
->lexer
);
12658 if (token1
.type
!= CPP_EOF
)
12659 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12662 token2
.type
= CPP_EOF
;
12663 token2
.keyword
= RID_MAX
;
12666 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12667 p
= obstack_alloc (&declarator_obstack
, 0);
12669 /* If the next token is `extern' and the following token is a string
12670 literal, then we have a linkage specification. */
12671 if (token1
.keyword
== RID_EXTERN
12672 && cp_parser_is_pure_string_literal (&token2
))
12673 cp_parser_linkage_specification (parser
);
12674 /* If the next token is `template', then we have either a template
12675 declaration, an explicit instantiation, or an explicit
12677 else if (token1
.keyword
== RID_TEMPLATE
)
12679 /* `template <>' indicates a template specialization. */
12680 if (token2
.type
== CPP_LESS
12681 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12682 cp_parser_explicit_specialization (parser
);
12683 /* `template <' indicates a template declaration. */
12684 else if (token2
.type
== CPP_LESS
)
12685 cp_parser_template_declaration (parser
, /*member_p=*/false);
12686 /* Anything else must be an explicit instantiation. */
12688 cp_parser_explicit_instantiation (parser
);
12690 /* If the next token is `export', then we have a template
12692 else if (token1
.keyword
== RID_EXPORT
)
12693 cp_parser_template_declaration (parser
, /*member_p=*/false);
12694 /* If the next token is `extern', 'static' or 'inline' and the one
12695 after that is `template', we have a GNU extended explicit
12696 instantiation directive. */
12697 else if (cp_parser_allow_gnu_extensions_p (parser
)
12698 && (token1
.keyword
== RID_EXTERN
12699 || token1
.keyword
== RID_STATIC
12700 || token1
.keyword
== RID_INLINE
)
12701 && token2
.keyword
== RID_TEMPLATE
)
12702 cp_parser_explicit_instantiation (parser
);
12703 /* If the next token is `namespace', check for a named or unnamed
12704 namespace definition. */
12705 else if (token1
.keyword
== RID_NAMESPACE
12706 && (/* A named namespace definition. */
12707 (token2
.type
== CPP_NAME
12708 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12710 || (token2
.type
== CPP_OPEN_SQUARE
12711 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12712 == CPP_OPEN_SQUARE
)
12713 /* An unnamed namespace definition. */
12714 || token2
.type
== CPP_OPEN_BRACE
12715 || token2
.keyword
== RID_ATTRIBUTE
))
12716 cp_parser_namespace_definition (parser
);
12717 /* An inline (associated) namespace definition. */
12718 else if (token1
.keyword
== RID_INLINE
12719 && token2
.keyword
== RID_NAMESPACE
)
12720 cp_parser_namespace_definition (parser
);
12721 /* Objective-C++ declaration/definition. */
12722 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12723 cp_parser_objc_declaration (parser
, NULL_TREE
);
12724 else if (c_dialect_objc ()
12725 && token1
.keyword
== RID_ATTRIBUTE
12726 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12727 cp_parser_objc_declaration (parser
, attributes
);
12728 /* At this point we may have a template declared by a concept
12730 else if (flag_concepts
12731 && cp_parser_template_declaration_after_export (parser
,
12732 /*member_p=*/false))
12735 /* Try to parse a block-declaration, or a function-definition. */
12736 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12738 /* Free any declarators allocated. */
12739 obstack_free (&declarator_obstack
, p
);
12742 /* Parse a block-declaration.
12747 namespace-alias-definition
12754 __extension__ block-declaration
12759 static_assert-declaration
12761 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12762 part of a declaration-statement. */
12765 cp_parser_block_declaration (cp_parser
*parser
,
12769 int saved_pedantic
;
12771 /* Check for the `__extension__' keyword. */
12772 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12774 /* Parse the qualified declaration. */
12775 cp_parser_block_declaration (parser
, statement_p
);
12776 /* Restore the PEDANTIC flag. */
12777 pedantic
= saved_pedantic
;
12782 /* Peek at the next token to figure out which kind of declaration is
12784 token1
= cp_lexer_peek_token (parser
->lexer
);
12786 /* If the next keyword is `asm', we have an asm-definition. */
12787 if (token1
->keyword
== RID_ASM
)
12790 cp_parser_commit_to_tentative_parse (parser
);
12791 cp_parser_asm_definition (parser
);
12793 /* If the next keyword is `namespace', we have a
12794 namespace-alias-definition. */
12795 else if (token1
->keyword
== RID_NAMESPACE
)
12796 cp_parser_namespace_alias_definition (parser
);
12797 /* If the next keyword is `using', we have a
12798 using-declaration, a using-directive, or an alias-declaration. */
12799 else if (token1
->keyword
== RID_USING
)
12804 cp_parser_commit_to_tentative_parse (parser
);
12805 /* If the token after `using' is `namespace', then we have a
12806 using-directive. */
12807 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12808 if (token2
->keyword
== RID_NAMESPACE
)
12809 cp_parser_using_directive (parser
);
12810 /* If the second token after 'using' is '=', then we have an
12811 alias-declaration. */
12812 else if (cxx_dialect
>= cxx11
12813 && token2
->type
== CPP_NAME
12814 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12815 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12816 cp_parser_alias_declaration (parser
);
12817 /* Otherwise, it's a using-declaration. */
12819 cp_parser_using_declaration (parser
,
12820 /*access_declaration_p=*/false);
12822 /* If the next keyword is `__label__' we have a misplaced label
12824 else if (token1
->keyword
== RID_LABEL
)
12826 cp_lexer_consume_token (parser
->lexer
);
12827 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12828 cp_parser_skip_to_end_of_statement (parser
);
12829 /* If the next token is now a `;', consume it. */
12830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12831 cp_lexer_consume_token (parser
->lexer
);
12833 /* If the next token is `static_assert' we have a static assertion. */
12834 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12835 cp_parser_static_assert (parser
, /*member_p=*/false);
12836 /* Anything else must be a simple-declaration. */
12838 cp_parser_simple_declaration (parser
, !statement_p
,
12839 /*maybe_range_for_decl*/NULL
);
12842 /* Parse a simple-declaration.
12844 simple-declaration:
12845 decl-specifier-seq [opt] init-declarator-list [opt] ;
12846 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12847 brace-or-equal-initializer ;
12849 init-declarator-list:
12851 init-declarator-list , init-declarator
12853 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12854 function-definition as a simple-declaration.
12856 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12857 parsed declaration if it is an uninitialized single declarator not followed
12858 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12859 if present, will not be consumed. */
12862 cp_parser_simple_declaration (cp_parser
* parser
,
12863 bool function_definition_allowed_p
,
12864 tree
*maybe_range_for_decl
)
12866 cp_decl_specifier_seq decl_specifiers
;
12867 int declares_class_or_enum
;
12868 bool saw_declarator
;
12869 location_t comma_loc
= UNKNOWN_LOCATION
;
12870 location_t init_loc
= UNKNOWN_LOCATION
;
12872 if (maybe_range_for_decl
)
12873 *maybe_range_for_decl
= NULL_TREE
;
12875 /* Defer access checks until we know what is being declared; the
12876 checks for names appearing in the decl-specifier-seq should be
12877 done as if we were in the scope of the thing being declared. */
12878 push_deferring_access_checks (dk_deferred
);
12880 /* Parse the decl-specifier-seq. We have to keep track of whether
12881 or not the decl-specifier-seq declares a named class or
12882 enumeration type, since that is the only case in which the
12883 init-declarator-list is allowed to be empty.
12887 In a simple-declaration, the optional init-declarator-list can be
12888 omitted only when declaring a class or enumeration, that is when
12889 the decl-specifier-seq contains either a class-specifier, an
12890 elaborated-type-specifier, or an enum-specifier. */
12891 cp_parser_decl_specifier_seq (parser
,
12892 CP_PARSER_FLAGS_OPTIONAL
,
12894 &declares_class_or_enum
);
12895 /* We no longer need to defer access checks. */
12896 stop_deferring_access_checks ();
12898 /* In a block scope, a valid declaration must always have a
12899 decl-specifier-seq. By not trying to parse declarators, we can
12900 resolve the declaration/expression ambiguity more quickly. */
12901 if (!function_definition_allowed_p
12902 && !decl_specifiers
.any_specifiers_p
)
12904 cp_parser_error (parser
, "expected declaration");
12908 /* If the next two tokens are both identifiers, the code is
12909 erroneous. The usual cause of this situation is code like:
12913 where "T" should name a type -- but does not. */
12914 if (!decl_specifiers
.any_type_specifiers_p
12915 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12917 /* If parsing tentatively, we should commit; we really are
12918 looking at a declaration. */
12919 cp_parser_commit_to_tentative_parse (parser
);
12924 /* If we have seen at least one decl-specifier, and the next token
12925 is not a parenthesis, then we must be looking at a declaration.
12926 (After "int (" we might be looking at a functional cast.) */
12927 if (decl_specifiers
.any_specifiers_p
12928 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12929 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12930 && !cp_parser_error_occurred (parser
))
12931 cp_parser_commit_to_tentative_parse (parser
);
12933 /* Look for C++17 decomposition declaration. */
12934 for (size_t n
= 1; ; n
++)
12935 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12936 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12938 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12939 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12940 && decl_specifiers
.any_specifiers_p
)
12943 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12944 maybe_range_for_decl
,
12947 /* The next token should be either a `,' or a `;'. */
12948 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12949 /* If it's a `;', we are done. */
12950 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12952 /* Anything else is an error. */
12955 /* If we have already issued an error message we don't need
12956 to issue another one. */
12957 if ((decl
!= error_mark_node
12958 && DECL_INITIAL (decl
) != error_mark_node
)
12959 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12960 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12961 /* Skip tokens until we reach the end of the statement. */
12962 cp_parser_skip_to_end_of_statement (parser
);
12963 /* If the next token is now a `;', consume it. */
12964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12965 cp_lexer_consume_token (parser
->lexer
);
12973 bool auto_specifier_p
;
12974 /* NULL_TREE if both variable and function declaration are allowed,
12975 error_mark_node if function declaration are not allowed and
12976 a FUNCTION_DECL that should be diagnosed if it is followed by
12977 variable declarations. */
12978 tree auto_function_declaration
;
12980 last_type
= NULL_TREE
;
12982 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
12983 auto_function_declaration
= NULL_TREE
;
12985 /* Keep going until we hit the `;' at the end of the simple
12987 saw_declarator
= false;
12988 while (cp_lexer_next_token_is_not (parser
->lexer
,
12992 bool function_definition_p
;
12994 tree auto_result
= NULL_TREE
;
12996 if (saw_declarator
)
12998 /* If we are processing next declarator, comma is expected */
12999 token
= cp_lexer_peek_token (parser
->lexer
);
13000 gcc_assert (token
->type
== CPP_COMMA
);
13001 cp_lexer_consume_token (parser
->lexer
);
13002 if (maybe_range_for_decl
)
13004 *maybe_range_for_decl
= error_mark_node
;
13005 if (comma_loc
== UNKNOWN_LOCATION
)
13006 comma_loc
= token
->location
;
13010 saw_declarator
= true;
13012 /* Parse the init-declarator. */
13013 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13015 function_definition_allowed_p
,
13016 /*member_p=*/false,
13017 declares_class_or_enum
,
13018 &function_definition_p
,
13019 maybe_range_for_decl
,
13022 /* If an error occurred while parsing tentatively, exit quickly.
13023 (That usually happens when in the body of a function; each
13024 statement is treated as a declaration-statement until proven
13026 if (cp_parser_error_occurred (parser
))
13029 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13031 /* If the init-declarator-list contains more than one
13032 init-declarator, they shall all form declarations of
13034 if (auto_function_declaration
== NULL_TREE
)
13035 auto_function_declaration
13036 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13037 else if (TREE_CODE (decl
) == FUNCTION_DECL
13038 || auto_function_declaration
!= error_mark_node
)
13040 error_at (decl_specifiers
.locations
[ds_type_spec
],
13041 "non-variable %qD in declaration with more than one "
13042 "declarator with placeholder type",
13043 TREE_CODE (decl
) == FUNCTION_DECL
13044 ? decl
: auto_function_declaration
);
13045 auto_function_declaration
= error_mark_node
;
13050 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13053 && last_type
!= error_mark_node
13054 && !same_type_p (auto_result
, last_type
))
13056 /* If the list of declarators contains more than one declarator,
13057 the type of each declared variable is determined as described
13058 above. If the type deduced for the template parameter U is not
13059 the same in each deduction, the program is ill-formed. */
13060 error_at (decl_specifiers
.locations
[ds_type_spec
],
13061 "inconsistent deduction for %qT: %qT and then %qT",
13062 decl_specifiers
.type
, last_type
, auto_result
);
13063 last_type
= error_mark_node
;
13066 last_type
= auto_result
;
13069 /* Handle function definitions specially. */
13070 if (function_definition_p
)
13072 /* If the next token is a `,', then we are probably
13073 processing something like:
13077 which is erroneous. */
13078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13080 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13081 error_at (token
->location
,
13083 " declarations and function-definitions is forbidden");
13085 /* Otherwise, we're done with the list of declarators. */
13088 pop_deferring_access_checks ();
13092 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13093 *maybe_range_for_decl
= decl
;
13094 /* The next token should be either a `,' or a `;'. */
13095 token
= cp_lexer_peek_token (parser
->lexer
);
13096 /* If it's a `,', there are more declarators to come. */
13097 if (token
->type
== CPP_COMMA
)
13098 /* will be consumed next time around */;
13099 /* If it's a `;', we are done. */
13100 else if (token
->type
== CPP_SEMICOLON
)
13102 else if (maybe_range_for_decl
)
13104 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13105 permerror (decl_specifiers
.locations
[ds_type_spec
],
13106 "types may not be defined in a for-range-declaration");
13109 /* Anything else is an error. */
13112 /* If we have already issued an error message we don't need
13113 to issue another one. */
13114 if ((decl
!= error_mark_node
13115 && DECL_INITIAL (decl
) != error_mark_node
)
13116 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13117 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13118 /* Skip tokens until we reach the end of the statement. */
13119 cp_parser_skip_to_end_of_statement (parser
);
13120 /* If the next token is now a `;', consume it. */
13121 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13122 cp_lexer_consume_token (parser
->lexer
);
13125 /* After the first time around, a function-definition is not
13126 allowed -- even if it was OK at first. For example:
13131 function_definition_allowed_p
= false;
13134 /* Issue an error message if no declarators are present, and the
13135 decl-specifier-seq does not itself declare a class or
13136 enumeration: [dcl.dcl]/3. */
13137 if (!saw_declarator
)
13139 if (cp_parser_declares_only_class_p (parser
))
13141 if (!declares_class_or_enum
13142 && decl_specifiers
.type
13143 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13144 /* Ensure an error is issued anyway when finish_decltype_type,
13145 called via cp_parser_decl_specifier_seq, returns a class or
13146 an enumeration (c++/51786). */
13147 decl_specifiers
.type
= NULL_TREE
;
13148 shadow_tag (&decl_specifiers
);
13150 /* Perform any deferred access checks. */
13151 perform_deferred_access_checks (tf_warning_or_error
);
13154 /* Consume the `;'. */
13156 if (!maybe_range_for_decl
)
13157 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13158 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13160 if (init_loc
!= UNKNOWN_LOCATION
)
13161 error_at (init_loc
, "initializer in range-based %<for%> loop");
13162 if (comma_loc
!= UNKNOWN_LOCATION
)
13163 error_at (comma_loc
,
13164 "multiple declarations in range-based %<for%> loop");
13168 pop_deferring_access_checks ();
13171 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13172 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13176 cp_parser_decomposition_declaration (cp_parser
*parser
,
13177 cp_decl_specifier_seq
*decl_specifiers
,
13178 tree
*maybe_range_for_decl
,
13179 location_t
*init_loc
)
13181 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13182 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13183 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13185 /* Parse the identifier-list. */
13186 auto_vec
<cp_expr
, 10> v
;
13187 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13190 cp_expr e
= cp_parser_identifier (parser
);
13191 if (e
.get_value () == error_mark_node
)
13194 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13196 cp_lexer_consume_token (parser
->lexer
);
13199 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13200 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13202 end_loc
= UNKNOWN_LOCATION
;
13203 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13205 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13206 cp_lexer_consume_token (parser
->lexer
);
13209 cp_parser_skip_to_end_of_statement (parser
);
13210 return error_mark_node
;
13214 if (cxx_dialect
< cxx17
)
13215 pedwarn (loc
, 0, "structured bindings only available with "
13216 "-std=c++17 or -std=gnu++17");
13219 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13220 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13221 declarator
->id_loc
= loc
;
13222 if (ref_qual
!= REF_QUAL_NONE
)
13223 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13224 ref_qual
== REF_QUAL_RVALUE
,
13226 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13227 NULL_TREE
, decl_specifiers
->attributes
,
13229 tree orig_decl
= decl
;
13233 cp_decl_specifier_seq decl_specs
;
13234 clear_decl_specs (&decl_specs
);
13235 decl_specs
.type
= make_auto ();
13237 FOR_EACH_VEC_ELT (v
, i
, e
)
13240 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13242 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13243 declarator
->id_loc
= e
.get_location ();
13244 tree elt_pushed_scope
;
13245 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13246 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13247 if (decl2
== error_mark_node
)
13248 decl
= error_mark_node
;
13249 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13251 /* Ensure we've diagnosed redeclaration if we aren't creating
13253 gcc_assert (errorcount
);
13254 decl
= error_mark_node
;
13258 if (elt_pushed_scope
)
13259 pop_scope (elt_pushed_scope
);
13264 error_at (loc
, "empty structured binding declaration");
13265 decl
= error_mark_node
;
13268 if (maybe_range_for_decl
== NULL
13269 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13271 bool non_constant_p
= false, is_direct_init
= false;
13272 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13273 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13275 if (initializer
== NULL_TREE
13276 || (TREE_CODE (initializer
) == TREE_LIST
13277 && TREE_CHAIN (initializer
))
13279 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13280 && CONSTRUCTOR_NELTS (initializer
) != 1))
13282 error_at (loc
, "invalid initializer for structured binding "
13284 initializer
= error_mark_node
;
13287 if (decl
!= error_mark_node
)
13289 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13290 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13291 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13292 cp_finish_decomp (decl
, prev
, v
.length ());
13295 else if (decl
!= error_mark_node
)
13297 *maybe_range_for_decl
= prev
;
13298 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13299 the underlying DECL. */
13300 cp_finish_decomp (decl
, prev
, v
.length ());
13304 pop_scope (pushed_scope
);
13306 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13308 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13309 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13315 /* Parse a decl-specifier-seq.
13317 decl-specifier-seq:
13318 decl-specifier-seq [opt] decl-specifier
13319 decl-specifier attribute-specifier-seq [opt] (C++11)
13322 storage-class-specifier
13333 Concepts Extension:
13338 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13340 The parser flags FLAGS is used to control type-specifier parsing.
13342 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13345 1: one of the decl-specifiers is an elaborated-type-specifier
13346 (i.e., a type declaration)
13347 2: one of the decl-specifiers is an enum-specifier or a
13348 class-specifier (i.e., a type definition)
13353 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13354 cp_parser_flags flags
,
13355 cp_decl_specifier_seq
*decl_specs
,
13356 int* declares_class_or_enum
)
13358 bool constructor_possible_p
= !parser
->in_declarator_p
;
13359 bool found_decl_spec
= false;
13360 cp_token
*start_token
= NULL
;
13363 /* Clear DECL_SPECS. */
13364 clear_decl_specs (decl_specs
);
13366 /* Assume no class or enumeration type is declared. */
13367 *declares_class_or_enum
= 0;
13369 /* Keep reading specifiers until there are no more to read. */
13372 bool constructor_p
;
13376 /* Peek at the next token. */
13377 token
= cp_lexer_peek_token (parser
->lexer
);
13379 /* Save the first token of the decl spec list for error
13382 start_token
= token
;
13383 /* Handle attributes. */
13384 if (cp_next_tokens_can_be_attribute_p (parser
))
13386 /* Parse the attributes. */
13387 tree attrs
= cp_parser_attributes_opt (parser
);
13389 /* In a sequence of declaration specifiers, c++11 attributes
13390 appertain to the type that precede them. In that case
13393 The attribute-specifier-seq affects the type only for
13394 the declaration it appears in, not other declarations
13395 involving the same type.
13397 But for now let's force the user to position the
13398 attribute either at the beginning of the declaration or
13399 after the declarator-id, which would clearly mean that it
13400 applies to the declarator. */
13401 if (cxx11_attribute_p (attrs
))
13403 if (!found_decl_spec
)
13404 /* The c++11 attribute is at the beginning of the
13405 declaration. It appertains to the entity being
13409 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13411 /* This is an attribute following a
13412 class-specifier. */
13413 if (decl_specs
->type_definition_p
)
13414 warn_misplaced_attr_for_class_type (token
->location
,
13420 decl_specs
->std_attributes
13421 = chainon (decl_specs
->std_attributes
,
13423 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13424 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13430 decl_specs
->attributes
13431 = chainon (decl_specs
->attributes
,
13433 if (decl_specs
->locations
[ds_attribute
] == 0)
13434 decl_specs
->locations
[ds_attribute
] = token
->location
;
13437 /* Assume we will find a decl-specifier keyword. */
13438 found_decl_spec
= true;
13439 /* If the next token is an appropriate keyword, we can simply
13440 add it to the list. */
13441 switch (token
->keyword
)
13447 if (!at_class_scope_p ())
13449 gcc_rich_location
richloc (token
->location
);
13450 richloc
.add_fixit_remove ();
13451 error_at (&richloc
, "%<friend%> used outside of class");
13452 cp_lexer_purge_token (parser
->lexer
);
13457 /* Consume the token. */
13458 cp_lexer_consume_token (parser
->lexer
);
13462 case RID_CONSTEXPR
:
13464 cp_lexer_consume_token (parser
->lexer
);
13469 cp_lexer_consume_token (parser
->lexer
);
13472 /* function-specifier:
13479 cp_parser_function_specifier_opt (parser
, decl_specs
);
13486 /* Consume the token. */
13487 cp_lexer_consume_token (parser
->lexer
);
13488 /* A constructor declarator cannot appear in a typedef. */
13489 constructor_possible_p
= false;
13490 /* The "typedef" keyword can only occur in a declaration; we
13491 may as well commit at this point. */
13492 cp_parser_commit_to_tentative_parse (parser
);
13494 if (decl_specs
->storage_class
!= sc_none
)
13495 decl_specs
->conflicting_specifiers_p
= true;
13498 /* storage-class-specifier:
13508 if (cxx_dialect
== cxx98
)
13510 /* Consume the token. */
13511 cp_lexer_consume_token (parser
->lexer
);
13513 /* Complain about `auto' as a storage specifier, if
13514 we're complaining about C++0x compatibility. */
13515 gcc_rich_location
richloc (token
->location
);
13516 richloc
.add_fixit_remove ();
13517 warning_at (&richloc
, OPT_Wc__11_compat
,
13518 "%<auto%> changes meaning in C++11; "
13519 "please remove it");
13521 /* Set the storage class anyway. */
13522 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13526 /* C++0x auto type-specifier. */
13527 found_decl_spec
= false;
13534 /* Consume the token. */
13535 cp_lexer_consume_token (parser
->lexer
);
13536 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13540 /* Consume the token. */
13542 cp_lexer_consume_token (parser
->lexer
);
13546 /* We did not yet find a decl-specifier yet. */
13547 found_decl_spec
= false;
13551 if (found_decl_spec
13552 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13553 && token
->keyword
!= RID_CONSTEXPR
)
13554 error ("decl-specifier invalid in condition");
13556 if (found_decl_spec
13557 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13558 && token
->keyword
!= RID_MUTABLE
13559 && token
->keyword
!= RID_CONSTEXPR
)
13560 error_at (token
->location
, "%qD invalid in lambda",
13561 ridpointers
[token
->keyword
]);
13564 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13566 /* Constructors are a special case. The `S' in `S()' is not a
13567 decl-specifier; it is the beginning of the declarator. */
13569 = (!found_decl_spec
13570 && constructor_possible_p
13571 && (cp_parser_constructor_declarator_p
13572 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13574 /* If we don't have a DECL_SPEC yet, then we must be looking at
13575 a type-specifier. */
13576 if (!found_decl_spec
&& !constructor_p
)
13578 int decl_spec_declares_class_or_enum
;
13579 bool is_cv_qualifier
;
13583 = cp_parser_type_specifier (parser
, flags
,
13585 /*is_declaration=*/true,
13586 &decl_spec_declares_class_or_enum
,
13588 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13590 /* If this type-specifier referenced a user-defined type
13591 (a typedef, class-name, etc.), then we can't allow any
13592 more such type-specifiers henceforth.
13596 The longest sequence of decl-specifiers that could
13597 possibly be a type name is taken as the
13598 decl-specifier-seq of a declaration. The sequence shall
13599 be self-consistent as described below.
13603 As a general rule, at most one type-specifier is allowed
13604 in the complete decl-specifier-seq of a declaration. The
13605 only exceptions are the following:
13607 -- const or volatile can be combined with any other
13610 -- signed or unsigned can be combined with char, long,
13618 void g (const int Pc);
13620 Here, Pc is *not* part of the decl-specifier seq; it's
13621 the declarator. Therefore, once we see a type-specifier
13622 (other than a cv-qualifier), we forbid any additional
13623 user-defined types. We *do* still allow things like `int
13624 int' to be considered a decl-specifier-seq, and issue the
13625 error message later. */
13626 if (type_spec
&& !is_cv_qualifier
)
13627 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13628 /* A constructor declarator cannot follow a type-specifier. */
13631 constructor_possible_p
= false;
13632 found_decl_spec
= true;
13633 if (!is_cv_qualifier
)
13634 decl_specs
->any_type_specifiers_p
= true;
13638 /* If we still do not have a DECL_SPEC, then there are no more
13639 decl-specifiers. */
13640 if (!found_decl_spec
)
13643 decl_specs
->any_specifiers_p
= true;
13644 /* After we see one decl-specifier, further decl-specifiers are
13645 always optional. */
13646 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13649 /* Don't allow a friend specifier with a class definition. */
13650 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13651 && (*declares_class_or_enum
& 2))
13652 error_at (decl_specs
->locations
[ds_friend
],
13653 "class definition may not be declared a friend");
13656 /* Parse an (optional) storage-class-specifier.
13658 storage-class-specifier:
13667 storage-class-specifier:
13670 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13673 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13675 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13678 if (cxx_dialect
!= cxx98
)
13680 /* Fall through for C++98. */
13681 gcc_fallthrough ();
13688 /* Consume the token. */
13689 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13696 /* Parse an (optional) function-specifier.
13698 function-specifier:
13703 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13704 Updates DECL_SPECS, if it is non-NULL. */
13707 cp_parser_function_specifier_opt (cp_parser
* parser
,
13708 cp_decl_specifier_seq
*decl_specs
)
13710 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13711 switch (token
->keyword
)
13714 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13718 /* 14.5.2.3 [temp.mem]
13720 A member function template shall not be virtual. */
13721 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13722 && current_class_type
)
13723 error_at (token
->location
, "templates may not be %<virtual%>");
13725 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13729 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13736 /* Consume the token. */
13737 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13740 /* Parse a linkage-specification.
13742 linkage-specification:
13743 extern string-literal { declaration-seq [opt] }
13744 extern string-literal declaration */
13747 cp_parser_linkage_specification (cp_parser
* parser
)
13751 /* Look for the `extern' keyword. */
13752 cp_token
*extern_token
13753 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13755 /* Look for the string-literal. */
13756 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13757 linkage
= cp_parser_string_literal (parser
, false, false);
13759 /* Transform the literal into an identifier. If the literal is a
13760 wide-character string, or contains embedded NULs, then we can't
13761 handle it as the user wants. */
13762 if (strlen (TREE_STRING_POINTER (linkage
))
13763 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13765 cp_parser_error (parser
, "invalid linkage-specification");
13766 /* Assume C++ linkage. */
13767 linkage
= lang_name_cplusplus
;
13770 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13772 /* We're now using the new linkage. */
13773 push_lang_context (linkage
);
13775 /* Preserve the location of the the innermost linkage specification,
13776 tracking the locations of nested specifications via a local. */
13777 location_t saved_location
13778 = parser
->innermost_linkage_specification_location
;
13779 /* Construct a location ranging from the start of the "extern" to
13780 the end of the string-literal, with the caret at the start, e.g.:
13784 parser
->innermost_linkage_specification_location
13785 = make_location (extern_token
->location
,
13786 extern_token
->location
,
13787 get_finish (string_token
->location
));
13789 /* If the next token is a `{', then we're using the first
13791 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13793 cp_ensure_no_omp_declare_simd (parser
);
13794 cp_ensure_no_oacc_routine (parser
);
13796 /* Consume the `{' token. */
13797 matching_braces braces
;
13798 braces
.consume_open (parser
)->location
;
13799 /* Parse the declarations. */
13800 cp_parser_declaration_seq_opt (parser
);
13801 /* Look for the closing `}'. */
13802 braces
.require_close (parser
);
13804 /* Otherwise, there's just one declaration. */
13807 bool saved_in_unbraced_linkage_specification_p
;
13809 saved_in_unbraced_linkage_specification_p
13810 = parser
->in_unbraced_linkage_specification_p
;
13811 parser
->in_unbraced_linkage_specification_p
= true;
13812 cp_parser_declaration (parser
);
13813 parser
->in_unbraced_linkage_specification_p
13814 = saved_in_unbraced_linkage_specification_p
;
13817 /* We're done with the linkage-specification. */
13818 pop_lang_context ();
13820 /* Restore location of parent linkage specification, if any. */
13821 parser
->innermost_linkage_specification_location
= saved_location
;
13824 /* Parse a static_assert-declaration.
13826 static_assert-declaration:
13827 static_assert ( constant-expression , string-literal ) ;
13828 static_assert ( constant-expression ) ; (C++17)
13830 If MEMBER_P, this static_assert is a class member. */
13833 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13836 location_t token_loc
;
13840 /* Peek at the `static_assert' token so we can keep track of exactly
13841 where the static assertion started. */
13842 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13844 /* Look for the `static_assert' keyword. */
13845 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13849 /* We know we are in a static assertion; commit to any tentative
13851 if (cp_parser_parsing_tentatively (parser
))
13852 cp_parser_commit_to_tentative_parse (parser
);
13854 /* Parse the `(' starting the static assertion condition. */
13855 matching_parens parens
;
13856 parens
.require_open (parser
);
13858 /* Parse the constant-expression. Allow a non-constant expression
13859 here in order to give better diagnostics in finish_static_assert. */
13861 cp_parser_constant_expression (parser
,
13862 /*allow_non_constant_p=*/true,
13863 /*non_constant_p=*/&dummy
);
13865 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13867 if (cxx_dialect
< cxx17
)
13868 pedwarn (input_location
, OPT_Wpedantic
,
13869 "static_assert without a message "
13870 "only available with -std=c++17 or -std=gnu++17");
13872 cp_lexer_consume_token (parser
->lexer
);
13873 message
= build_string (1, "");
13874 TREE_TYPE (message
) = char_array_type_node
;
13875 fix_string_type (message
);
13879 /* Parse the separating `,'. */
13880 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13882 /* Parse the string-literal message. */
13883 message
= cp_parser_string_literal (parser
,
13884 /*translate=*/false,
13887 /* A `)' completes the static assertion. */
13888 if (!parens
.require_close (parser
))
13889 cp_parser_skip_to_closing_parenthesis (parser
,
13890 /*recovering=*/true,
13891 /*or_comma=*/false,
13892 /*consume_paren=*/true);
13895 /* A semicolon terminates the declaration. */
13896 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13898 /* Get the location for the static assertion. Use that of the
13899 condition if available, otherwise, use that of the "static_assert"
13901 location_t assert_loc
= condition
.get_location ();
13902 if (assert_loc
== UNKNOWN_LOCATION
)
13903 assert_loc
= token_loc
;
13905 /* Complete the static assertion, which may mean either processing
13906 the static assert now or saving it for template instantiation. */
13907 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13910 /* Parse the expression in decltype ( expression ). */
13913 cp_parser_decltype_expr (cp_parser
*parser
,
13914 bool &id_expression_or_member_access_p
)
13916 cp_token
*id_expr_start_token
;
13919 /* Since we're going to preserve any side-effects from this parse, set up a
13920 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13921 in the expression. */
13922 tentative_firewall
firewall (parser
);
13924 /* First, try parsing an id-expression. */
13925 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13926 cp_parser_parse_tentatively (parser
);
13927 expr
= cp_parser_id_expression (parser
,
13928 /*template_keyword_p=*/false,
13929 /*check_dependency_p=*/true,
13930 /*template_p=*/NULL
,
13931 /*declarator_p=*/false,
13932 /*optional_p=*/false);
13934 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13936 bool non_integral_constant_expression_p
= false;
13937 tree id_expression
= expr
;
13939 const char *error_msg
;
13941 if (identifier_p (expr
))
13942 /* Lookup the name we got back from the id-expression. */
13943 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13944 id_expr_start_token
->location
);
13947 && expr
!= error_mark_node
13948 && TREE_CODE (expr
) != TYPE_DECL
13949 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13950 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13951 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13953 /* Complete lookup of the id-expression. */
13954 expr
= (finish_id_expression
13955 (id_expression
, expr
, parser
->scope
, &idk
,
13956 /*integral_constant_expression_p=*/false,
13957 /*allow_non_integral_constant_expression_p=*/true,
13958 &non_integral_constant_expression_p
,
13959 /*template_p=*/false,
13961 /*address_p=*/false,
13962 /*template_arg_p=*/false,
13964 id_expr_start_token
->location
));
13966 if (expr
== error_mark_node
)
13967 /* We found an id-expression, but it was something that we
13968 should not have found. This is an error, not something
13969 we can recover from, so note that we found an
13970 id-expression and we'll recover as gracefully as
13972 id_expression_or_member_access_p
= true;
13976 && expr
!= error_mark_node
13977 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13978 /* We have an id-expression. */
13979 id_expression_or_member_access_p
= true;
13982 if (!id_expression_or_member_access_p
)
13984 /* Abort the id-expression parse. */
13985 cp_parser_abort_tentative_parse (parser
);
13987 /* Parsing tentatively, again. */
13988 cp_parser_parse_tentatively (parser
);
13990 /* Parse a class member access. */
13991 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
13992 /*cast_p=*/false, /*decltype*/true,
13993 /*member_access_only_p=*/true, NULL
);
13996 && expr
!= error_mark_node
13997 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13998 /* We have an id-expression. */
13999 id_expression_or_member_access_p
= true;
14002 if (id_expression_or_member_access_p
)
14003 /* We have parsed the complete id-expression or member access. */
14004 cp_parser_parse_definitely (parser
);
14007 /* Abort our attempt to parse an id-expression or member access
14009 cp_parser_abort_tentative_parse (parser
);
14011 /* Parse a full expression. */
14012 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14013 /*decltype_p=*/true);
14019 /* Parse a `decltype' type. Returns the type.
14021 simple-type-specifier:
14022 decltype ( expression )
14024 decltype ( auto ) */
14027 cp_parser_decltype (cp_parser
*parser
)
14030 bool id_expression_or_member_access_p
= false;
14031 const char *saved_message
;
14032 bool saved_integral_constant_expression_p
;
14033 bool saved_non_integral_constant_expression_p
;
14034 bool saved_greater_than_is_operator_p
;
14035 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14037 if (start_token
->type
== CPP_DECLTYPE
)
14039 /* Already parsed. */
14040 cp_lexer_consume_token (parser
->lexer
);
14041 return saved_checks_value (start_token
->u
.tree_check_value
);
14044 /* Look for the `decltype' token. */
14045 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14046 return error_mark_node
;
14048 /* Parse the opening `('. */
14049 matching_parens parens
;
14050 if (!parens
.require_open (parser
))
14051 return error_mark_node
;
14053 /* decltype (auto) */
14054 if (cxx_dialect
>= cxx14
14055 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14057 cp_lexer_consume_token (parser
->lexer
);
14058 if (!parens
.require_close (parser
))
14059 return error_mark_node
;
14060 expr
= make_decltype_auto ();
14061 AUTO_IS_DECLTYPE (expr
) = true;
14065 /* Types cannot be defined in a `decltype' expression. Save away the
14067 saved_message
= parser
->type_definition_forbidden_message
;
14069 /* And create the new one. */
14070 parser
->type_definition_forbidden_message
14071 = G_("types may not be defined in %<decltype%> expressions");
14073 /* The restrictions on constant-expressions do not apply inside
14074 decltype expressions. */
14075 saved_integral_constant_expression_p
14076 = parser
->integral_constant_expression_p
;
14077 saved_non_integral_constant_expression_p
14078 = parser
->non_integral_constant_expression_p
;
14079 parser
->integral_constant_expression_p
= false;
14081 /* Within a parenthesized expression, a `>' token is always
14082 the greater-than operator. */
14083 saved_greater_than_is_operator_p
14084 = parser
->greater_than_is_operator_p
;
14085 parser
->greater_than_is_operator_p
= true;
14087 /* Do not actually evaluate the expression. */
14088 ++cp_unevaluated_operand
;
14090 /* Do not warn about problems with the expression. */
14091 ++c_inhibit_evaluation_warnings
;
14093 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14095 /* Go back to evaluating expressions. */
14096 --cp_unevaluated_operand
;
14097 --c_inhibit_evaluation_warnings
;
14099 /* The `>' token might be the end of a template-id or
14100 template-parameter-list now. */
14101 parser
->greater_than_is_operator_p
14102 = saved_greater_than_is_operator_p
;
14104 /* Restore the old message and the integral constant expression
14106 parser
->type_definition_forbidden_message
= saved_message
;
14107 parser
->integral_constant_expression_p
14108 = saved_integral_constant_expression_p
;
14109 parser
->non_integral_constant_expression_p
14110 = saved_non_integral_constant_expression_p
;
14112 /* Parse to the closing `)'. */
14113 if (!parens
.require_close (parser
))
14115 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14116 /*consume_paren=*/true);
14117 return error_mark_node
;
14120 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14121 tf_warning_or_error
);
14124 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14126 start_token
->type
= CPP_DECLTYPE
;
14127 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14128 start_token
->u
.tree_check_value
->value
= expr
;
14129 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14130 start_token
->keyword
= RID_MAX
;
14131 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14136 /* Special member functions [gram.special] */
14138 /* Parse a conversion-function-id.
14140 conversion-function-id:
14141 operator conversion-type-id
14143 Returns an IDENTIFIER_NODE representing the operator. */
14146 cp_parser_conversion_function_id (cp_parser
* parser
)
14150 tree saved_qualifying_scope
;
14151 tree saved_object_scope
;
14152 tree pushed_scope
= NULL_TREE
;
14154 /* Look for the `operator' token. */
14155 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14156 return error_mark_node
;
14157 /* When we parse the conversion-type-id, the current scope will be
14158 reset. However, we need that information in able to look up the
14159 conversion function later, so we save it here. */
14160 saved_scope
= parser
->scope
;
14161 saved_qualifying_scope
= parser
->qualifying_scope
;
14162 saved_object_scope
= parser
->object_scope
;
14163 /* We must enter the scope of the class so that the names of
14164 entities declared within the class are available in the
14165 conversion-type-id. For example, consider:
14172 S::operator I() { ... }
14174 In order to see that `I' is a type-name in the definition, we
14175 must be in the scope of `S'. */
14177 pushed_scope
= push_scope (saved_scope
);
14178 /* Parse the conversion-type-id. */
14179 type
= cp_parser_conversion_type_id (parser
);
14180 /* Leave the scope of the class, if any. */
14182 pop_scope (pushed_scope
);
14183 /* Restore the saved scope. */
14184 parser
->scope
= saved_scope
;
14185 parser
->qualifying_scope
= saved_qualifying_scope
;
14186 parser
->object_scope
= saved_object_scope
;
14187 /* If the TYPE is invalid, indicate failure. */
14188 if (type
== error_mark_node
)
14189 return error_mark_node
;
14190 return make_conv_op_name (type
);
14193 /* Parse a conversion-type-id:
14195 conversion-type-id:
14196 type-specifier-seq conversion-declarator [opt]
14198 Returns the TYPE specified. */
14201 cp_parser_conversion_type_id (cp_parser
* parser
)
14204 cp_decl_specifier_seq type_specifiers
;
14205 cp_declarator
*declarator
;
14206 tree type_specified
;
14207 const char *saved_message
;
14209 /* Parse the attributes. */
14210 attributes
= cp_parser_attributes_opt (parser
);
14212 saved_message
= parser
->type_definition_forbidden_message
;
14213 parser
->type_definition_forbidden_message
14214 = G_("types may not be defined in a conversion-type-id");
14216 /* Parse the type-specifiers. */
14217 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14218 /*is_trailing_return=*/false,
14221 parser
->type_definition_forbidden_message
= saved_message
;
14223 /* If that didn't work, stop. */
14224 if (type_specifiers
.type
== error_mark_node
)
14225 return error_mark_node
;
14226 /* Parse the conversion-declarator. */
14227 declarator
= cp_parser_conversion_declarator_opt (parser
);
14229 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14230 /*initialized=*/0, &attributes
);
14232 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14234 /* Don't give this error when parsing tentatively. This happens to
14235 work because we always parse this definitively once. */
14236 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14237 && type_uses_auto (type_specified
))
14239 if (cxx_dialect
< cxx14
)
14241 error ("invalid use of %<auto%> in conversion operator");
14242 return error_mark_node
;
14244 else if (template_parm_scope_p ())
14245 warning (0, "use of %<auto%> in member template "
14246 "conversion operator can never be deduced");
14249 return type_specified
;
14252 /* Parse an (optional) conversion-declarator.
14254 conversion-declarator:
14255 ptr-operator conversion-declarator [opt]
14259 static cp_declarator
*
14260 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14262 enum tree_code code
;
14263 tree class_type
, std_attributes
= NULL_TREE
;
14264 cp_cv_quals cv_quals
;
14266 /* We don't know if there's a ptr-operator next, or not. */
14267 cp_parser_parse_tentatively (parser
);
14268 /* Try the ptr-operator. */
14269 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14271 /* If it worked, look for more conversion-declarators. */
14272 if (cp_parser_parse_definitely (parser
))
14274 cp_declarator
*declarator
;
14276 /* Parse another optional declarator. */
14277 declarator
= cp_parser_conversion_declarator_opt (parser
);
14279 declarator
= cp_parser_make_indirect_declarator
14280 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14288 /* Parse an (optional) ctor-initializer.
14291 : mem-initializer-list */
14294 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14296 /* If the next token is not a `:', then there is no
14297 ctor-initializer. */
14298 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14300 /* Do default initialization of any bases and members. */
14301 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14302 finish_mem_initializers (NULL_TREE
);
14306 /* Consume the `:' token. */
14307 cp_lexer_consume_token (parser
->lexer
);
14308 /* And the mem-initializer-list. */
14309 cp_parser_mem_initializer_list (parser
);
14312 /* Parse a mem-initializer-list.
14314 mem-initializer-list:
14315 mem-initializer ... [opt]
14316 mem-initializer ... [opt] , mem-initializer-list */
14319 cp_parser_mem_initializer_list (cp_parser
* parser
)
14321 tree mem_initializer_list
= NULL_TREE
;
14322 tree target_ctor
= error_mark_node
;
14323 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14325 /* Let the semantic analysis code know that we are starting the
14326 mem-initializer-list. */
14327 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14328 error_at (token
->location
,
14329 "only constructors take member initializers");
14331 /* Loop through the list. */
14334 tree mem_initializer
;
14336 token
= cp_lexer_peek_token (parser
->lexer
);
14337 /* Parse the mem-initializer. */
14338 mem_initializer
= cp_parser_mem_initializer (parser
);
14339 /* If the next token is a `...', we're expanding member initializers. */
14340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14342 /* Consume the `...'. */
14343 cp_lexer_consume_token (parser
->lexer
);
14345 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14346 can be expanded but members cannot. */
14347 if (mem_initializer
!= error_mark_node
14348 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14350 error_at (token
->location
,
14351 "cannot expand initializer for member %qD",
14352 TREE_PURPOSE (mem_initializer
));
14353 mem_initializer
= error_mark_node
;
14356 /* Construct the pack expansion type. */
14357 if (mem_initializer
!= error_mark_node
)
14358 mem_initializer
= make_pack_expansion (mem_initializer
);
14360 if (target_ctor
!= error_mark_node
14361 && mem_initializer
!= error_mark_node
)
14363 error ("mem-initializer for %qD follows constructor delegation",
14364 TREE_PURPOSE (mem_initializer
));
14365 mem_initializer
= error_mark_node
;
14367 /* Look for a target constructor. */
14368 if (mem_initializer
!= error_mark_node
14369 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14370 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14372 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14373 if (mem_initializer_list
)
14375 error ("constructor delegation follows mem-initializer for %qD",
14376 TREE_PURPOSE (mem_initializer_list
));
14377 mem_initializer
= error_mark_node
;
14379 target_ctor
= mem_initializer
;
14381 /* Add it to the list, unless it was erroneous. */
14382 if (mem_initializer
!= error_mark_node
)
14384 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14385 mem_initializer_list
= mem_initializer
;
14387 /* If the next token is not a `,', we're done. */
14388 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14390 /* Consume the `,' token. */
14391 cp_lexer_consume_token (parser
->lexer
);
14394 /* Perform semantic analysis. */
14395 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14396 finish_mem_initializers (mem_initializer_list
);
14399 /* Parse a mem-initializer.
14402 mem-initializer-id ( expression-list [opt] )
14403 mem-initializer-id braced-init-list
14408 ( expression-list [opt] )
14410 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14411 class) or FIELD_DECL (for a non-static data member) to initialize;
14412 the TREE_VALUE is the expression-list. An empty initialization
14413 list is represented by void_list_node. */
14416 cp_parser_mem_initializer (cp_parser
* parser
)
14418 tree mem_initializer_id
;
14419 tree expression_list
;
14421 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14423 /* Find out what is being initialized. */
14424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14426 permerror (token
->location
,
14427 "anachronistic old-style base class initializer");
14428 mem_initializer_id
= NULL_TREE
;
14432 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14433 if (mem_initializer_id
== error_mark_node
)
14434 return mem_initializer_id
;
14436 member
= expand_member_init (mem_initializer_id
);
14437 if (member
&& !DECL_P (member
))
14438 in_base_initializer
= 1;
14440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14442 bool expr_non_constant_p
;
14443 cp_lexer_set_source_position (parser
->lexer
);
14444 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14445 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14446 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14447 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14451 vec
<tree
, va_gc
> *vec
;
14452 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14454 /*allow_expansion_p=*/true,
14455 /*non_constant_p=*/NULL
);
14457 return error_mark_node
;
14458 expression_list
= build_tree_list_vec (vec
);
14459 release_tree_vector (vec
);
14462 if (expression_list
== error_mark_node
)
14463 return error_mark_node
;
14464 if (!expression_list
)
14465 expression_list
= void_type_node
;
14467 in_base_initializer
= 0;
14469 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14472 /* Parse a mem-initializer-id.
14474 mem-initializer-id:
14475 :: [opt] nested-name-specifier [opt] class-name
14476 decltype-specifier (C++11)
14479 Returns a TYPE indicating the class to be initialized for the first
14480 production (and the second in C++11). Returns an IDENTIFIER_NODE
14481 indicating the data member to be initialized for the last production. */
14484 cp_parser_mem_initializer_id (cp_parser
* parser
)
14486 bool global_scope_p
;
14487 bool nested_name_specifier_p
;
14488 bool template_p
= false;
14491 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14493 /* `typename' is not allowed in this context ([temp.res]). */
14494 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14496 error_at (token
->location
,
14497 "keyword %<typename%> not allowed in this context (a qualified "
14498 "member initializer is implicitly a type)");
14499 cp_lexer_consume_token (parser
->lexer
);
14501 /* Look for the optional `::' operator. */
14503 = (cp_parser_global_scope_opt (parser
,
14504 /*current_scope_valid_p=*/false)
14506 /* Look for the optional nested-name-specifier. The simplest way to
14511 The keyword `typename' is not permitted in a base-specifier or
14512 mem-initializer; in these contexts a qualified name that
14513 depends on a template-parameter is implicitly assumed to be a
14516 is to assume that we have seen the `typename' keyword at this
14518 nested_name_specifier_p
14519 = (cp_parser_nested_name_specifier_opt (parser
,
14520 /*typename_keyword_p=*/true,
14521 /*check_dependency_p=*/true,
14523 /*is_declaration=*/true)
14525 if (nested_name_specifier_p
)
14526 template_p
= cp_parser_optional_template_keyword (parser
);
14527 /* If there is a `::' operator or a nested-name-specifier, then we
14528 are definitely looking for a class-name. */
14529 if (global_scope_p
|| nested_name_specifier_p
)
14530 return cp_parser_class_name (parser
,
14531 /*typename_keyword_p=*/true,
14532 /*template_keyword_p=*/template_p
,
14534 /*check_dependency_p=*/true,
14535 /*class_head_p=*/false,
14536 /*is_declaration=*/true);
14537 /* Otherwise, we could also be looking for an ordinary identifier. */
14538 cp_parser_parse_tentatively (parser
);
14539 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14540 /* Try a decltype-specifier. */
14541 id
= cp_parser_decltype (parser
);
14543 /* Otherwise, try a class-name. */
14544 id
= cp_parser_class_name (parser
,
14545 /*typename_keyword_p=*/true,
14546 /*template_keyword_p=*/false,
14548 /*check_dependency_p=*/true,
14549 /*class_head_p=*/false,
14550 /*is_declaration=*/true);
14551 /* If we found one, we're done. */
14552 if (cp_parser_parse_definitely (parser
))
14554 /* Otherwise, look for an ordinary identifier. */
14555 return cp_parser_identifier (parser
);
14558 /* Overloading [gram.over] */
14560 /* Parse an operator-function-id.
14562 operator-function-id:
14565 Returns an IDENTIFIER_NODE for the operator which is a
14566 human-readable spelling of the identifier, e.g., `operator +'. */
14569 cp_parser_operator_function_id (cp_parser
* parser
)
14571 /* Look for the `operator' keyword. */
14572 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14573 return error_mark_node
;
14574 /* And then the name of the operator itself. */
14575 return cp_parser_operator (parser
);
14578 /* Return an identifier node for a user-defined literal operator.
14579 The suffix identifier is chained to the operator name identifier. */
14582 cp_literal_operator_id (const char* name
)
14585 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14586 + strlen (name
) + 10);
14587 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14588 identifier
= get_identifier (buffer
);
14593 /* Parse an operator.
14596 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14597 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14598 || ++ -- , ->* -> () []
14605 Returns an IDENTIFIER_NODE for the operator which is a
14606 human-readable spelling of the identifier, e.g., `operator +'. */
14609 cp_parser_operator (cp_parser
* parser
)
14611 tree id
= NULL_TREE
;
14615 /* Peek at the next token. */
14616 token
= cp_lexer_peek_token (parser
->lexer
);
14618 location_t start_loc
= token
->location
;
14620 /* Figure out which operator we have. */
14621 enum tree_code op
= ERROR_MARK
;
14622 bool assop
= false;
14623 bool consumed
= false;
14624 switch (token
->type
)
14628 /* The keyword should be either `new' or `delete'. */
14629 if (token
->keyword
== RID_NEW
)
14631 else if (token
->keyword
== RID_DELETE
)
14636 /* Consume the `new' or `delete' token. */
14637 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14639 /* Peek at the next token. */
14640 token
= cp_lexer_peek_token (parser
->lexer
);
14641 /* If it's a `[' token then this is the array variant of the
14643 if (token
->type
== CPP_OPEN_SQUARE
)
14645 /* Consume the `[' token. */
14646 cp_lexer_consume_token (parser
->lexer
);
14647 /* Look for the `]' token. */
14648 if (cp_token
*close_token
14649 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14650 end_loc
= close_token
->location
;
14651 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14653 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14671 op
= TRUNC_DIV_EXPR
;
14675 op
= TRUNC_MOD_EXPR
;
14695 op
= TRUTH_NOT_EXPR
;
14728 op
= TRUNC_DIV_EXPR
;
14733 op
= TRUNC_MOD_EXPR
;
14759 case CPP_LSHIFT_EQ
:
14764 case CPP_RSHIFT_EQ
:
14781 case CPP_GREATER_EQ
:
14786 op
= TRUTH_ANDIF_EXPR
;
14790 op
= TRUTH_ORIF_EXPR
;
14793 case CPP_PLUS_PLUS
:
14794 op
= POSTINCREMENT_EXPR
;
14797 case CPP_MINUS_MINUS
:
14798 op
= PREDECREMENT_EXPR
;
14802 op
= COMPOUND_EXPR
;
14805 case CPP_DEREF_STAR
:
14810 op
= COMPONENT_REF
;
14813 case CPP_OPEN_PAREN
:
14815 /* Consume the `('. */
14816 matching_parens parens
;
14817 parens
.consume_open (parser
);
14818 /* Look for the matching `)'. */
14819 parens
.require_close (parser
);
14825 case CPP_OPEN_SQUARE
:
14826 /* Consume the `['. */
14827 cp_lexer_consume_token (parser
->lexer
);
14828 /* Look for the matching `]'. */
14829 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14834 case CPP_UTF8STRING
:
14835 case CPP_UTF8STRING_USERDEF
:
14842 case CPP_STRING_USERDEF
:
14843 case CPP_WSTRING_USERDEF
:
14844 case CPP_STRING16_USERDEF
:
14845 case CPP_STRING32_USERDEF
:
14847 tree str
, string_tree
;
14850 if (cxx_dialect
== cxx98
)
14851 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14853 /* Consume the string. */
14854 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14855 /*wide_ok=*/true, /*lookup_udlit=*/false);
14856 if (str
== error_mark_node
)
14857 return error_mark_node
;
14858 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14860 string_tree
= USERDEF_LITERAL_VALUE (str
);
14861 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14866 /* Look for the suffix identifier. */
14867 token
= cp_lexer_peek_token (parser
->lexer
);
14868 if (token
->type
== CPP_NAME
)
14869 id
= cp_parser_identifier (parser
);
14870 else if (token
->type
== CPP_KEYWORD
)
14872 error ("unexpected keyword;"
14873 " remove space between quotes and suffix identifier");
14874 return error_mark_node
;
14878 error ("expected suffix identifier");
14879 return error_mark_node
;
14882 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14883 (TREE_TYPE (TREE_TYPE (string_tree
))));
14884 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14887 error ("expected empty string after %<operator%> keyword");
14888 return error_mark_node
;
14890 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14893 error ("invalid encoding prefix in literal operator");
14894 return error_mark_node
;
14896 if (id
!= error_mark_node
)
14898 const char *name
= IDENTIFIER_POINTER (id
);
14899 id
= cp_literal_operator_id (name
);
14905 /* Anything else is an error. */
14909 /* If we have selected an identifier, we need to consume the
14911 if (op
!= ERROR_MARK
)
14913 id
= ovl_op_identifier (assop
, op
);
14915 cp_lexer_consume_token (parser
->lexer
);
14917 /* Otherwise, no valid operator name was present. */
14920 cp_parser_error (parser
, "expected operator");
14921 id
= error_mark_node
;
14924 return cp_expr (id
, start_loc
);
14927 /* Parse a template-declaration.
14929 template-declaration:
14930 export [opt] template < template-parameter-list > declaration
14932 If MEMBER_P is TRUE, this template-declaration occurs within a
14935 The grammar rule given by the standard isn't correct. What
14936 is really meant is:
14938 template-declaration:
14939 export [opt] template-parameter-list-seq
14940 decl-specifier-seq [opt] init-declarator [opt] ;
14941 export [opt] template-parameter-list-seq
14942 function-definition
14944 template-parameter-list-seq:
14945 template-parameter-list-seq [opt]
14946 template < template-parameter-list >
14948 Concept Extensions:
14950 template-parameter-list-seq:
14951 template < template-parameter-list > requires-clause [opt]
14954 requires logical-or-expression */
14957 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14959 /* Check for `export'. */
14960 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
14962 /* Consume the `export' token. */
14963 cp_lexer_consume_token (parser
->lexer
);
14964 /* Warn that we do not support `export'. */
14965 warning (0, "keyword %<export%> not implemented, and will be ignored");
14968 cp_parser_template_declaration_after_export (parser
, member_p
);
14971 /* Parse a template-parameter-list.
14973 template-parameter-list:
14975 template-parameter-list , template-parameter
14977 Returns a TREE_LIST. Each node represents a template parameter.
14978 The nodes are connected via their TREE_CHAINs. */
14981 cp_parser_template_parameter_list (cp_parser
* parser
)
14983 tree parameter_list
= NULL_TREE
;
14985 begin_template_parm_list ();
14987 /* The loop below parses the template parms. We first need to know
14988 the total number of template parms to be able to compute proper
14989 canonical types of each dependent type. So after the loop, when
14990 we know the total number of template parms,
14991 end_template_parm_list computes the proper canonical types and
14992 fixes up the dependent types accordingly. */
14997 bool is_parameter_pack
;
14998 location_t parm_loc
;
15000 /* Parse the template-parameter. */
15001 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15002 parameter
= cp_parser_template_parameter (parser
,
15004 &is_parameter_pack
);
15005 /* Add it to the list. */
15006 if (parameter
!= error_mark_node
)
15007 parameter_list
= process_template_parm (parameter_list
,
15011 is_parameter_pack
);
15014 tree err_parm
= build_tree_list (parameter
, parameter
);
15015 parameter_list
= chainon (parameter_list
, err_parm
);
15018 /* If the next token is not a `,', we're done. */
15019 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15021 /* Otherwise, consume the `,' token. */
15022 cp_lexer_consume_token (parser
->lexer
);
15025 return end_template_parm_list (parameter_list
);
15028 /* Parse a introduction-list.
15031 introduced-parameter
15032 introduction-list , introduced-parameter
15034 introduced-parameter:
15035 ...[opt] identifier
15037 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15038 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15039 WILDCARD_DECL will also have DECL_NAME set and token location in
15040 DECL_SOURCE_LOCATION. */
15043 cp_parser_introduction_list (cp_parser
*parser
)
15045 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15049 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15051 cp_lexer_consume_token (parser
->lexer
);
15053 /* Build placeholder. */
15054 tree parm
= build_nt (WILDCARD_DECL
);
15055 DECL_SOURCE_LOCATION (parm
)
15056 = cp_lexer_peek_token (parser
->lexer
)->location
;
15057 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15058 WILDCARD_PACK_P (parm
) = is_pack
;
15059 vec_safe_push (introduction_vec
, parm
);
15061 /* If the next token is not a `,', we're done. */
15062 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15064 /* Otherwise, consume the `,' token. */
15065 cp_lexer_consume_token (parser
->lexer
);
15068 /* Convert the vec into a TREE_VEC. */
15069 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15072 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15073 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15075 release_tree_vector (introduction_vec
);
15076 return introduction_list
;
15079 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15080 is an abstract declarator. */
15082 static inline cp_declarator
*
15083 get_id_declarator (cp_declarator
*declarator
)
15085 cp_declarator
*d
= declarator
;
15086 while (d
&& d
->kind
!= cdk_id
)
15091 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15092 is an abstract declarator. */
15095 get_unqualified_id (cp_declarator
*declarator
)
15097 declarator
= get_id_declarator (declarator
);
15099 return declarator
->u
.id
.unqualified_name
;
15104 /* Returns true if DECL represents a constrained-parameter. */
15107 is_constrained_parameter (tree decl
)
15110 && TREE_CODE (decl
) == TYPE_DECL
15111 && CONSTRAINED_PARM_CONCEPT (decl
)
15112 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15115 /* Returns true if PARM declares a constrained-parameter. */
15118 is_constrained_parameter (cp_parameter_declarator
*parm
)
15120 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15123 /* Check that the type parameter is only a declarator-id, and that its
15124 type is not cv-qualified. */
15127 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15128 cp_parameter_declarator
*parm
)
15130 if (!parm
->declarator
)
15133 if (parm
->declarator
->kind
!= cdk_id
)
15135 cp_parser_error (parser
, "invalid constrained type parameter");
15139 /* Don't allow cv-qualified type parameters. */
15140 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15141 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15143 cp_parser_error (parser
, "cv-qualified type parameter");
15150 /* Finish parsing/processing a template type parameter and checking
15151 various restrictions. */
15154 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15156 cp_parameter_declarator
* parmdecl
)
15158 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15159 return finish_template_type_parm (class_type_node
, id
);
15161 return error_mark_node
;
15165 finish_constrained_template_template_parm (tree proto
, tree id
)
15167 /* FIXME: This should probably be copied, and we may need to adjust
15168 the template parameter depths. */
15169 tree saved_parms
= current_template_parms
;
15170 begin_template_parm_list ();
15171 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15172 end_template_parm_list ();
15174 tree parm
= finish_template_template_parm (class_type_node
, id
);
15175 current_template_parms
= saved_parms
;
15180 /* Finish parsing/processing a template template parameter by borrowing
15181 the template parameter list from the prototype parameter. */
15184 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15187 cp_parameter_declarator
*parmdecl
)
15189 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15190 return error_mark_node
;
15191 return finish_constrained_template_template_parm (proto
, id
);
15194 /* Create a new non-type template parameter from the given PARM
15198 constrained_non_type_template_parm (bool *is_non_type
,
15199 cp_parameter_declarator
*parm
)
15201 *is_non_type
= true;
15202 cp_declarator
*decl
= parm
->declarator
;
15203 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15204 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15205 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15208 /* Build a constrained template parameter based on the PARMDECL
15209 declarator. The type of PARMDECL is the constrained type, which
15210 refers to the prototype template parameter that ultimately
15211 specifies the type of the declared parameter. */
15214 finish_constrained_parameter (cp_parser
*parser
,
15215 cp_parameter_declarator
*parmdecl
,
15217 bool *is_parameter_pack
)
15219 tree decl
= parmdecl
->decl_specifiers
.type
;
15220 tree id
= get_unqualified_id (parmdecl
->declarator
);
15221 tree def
= parmdecl
->default_argument
;
15222 tree proto
= DECL_INITIAL (decl
);
15224 /* A template parameter constrained by a variadic concept shall also
15225 be declared as a template parameter pack. */
15226 bool is_variadic
= template_parameter_pack_p (proto
);
15227 if (is_variadic
&& !*is_parameter_pack
)
15228 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15230 /* Build the parameter. Return an error if the declarator was invalid. */
15232 if (TREE_CODE (proto
) == TYPE_DECL
)
15233 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15234 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15235 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15238 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15239 if (parm
== error_mark_node
)
15240 return error_mark_node
;
15242 /* Finish the parameter decl and create a node attaching the
15243 default argument and constraint. */
15244 parm
= build_tree_list (def
, parm
);
15245 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15250 /* Returns true if the parsed type actually represents the declaration
15251 of a type template-parameter. */
15254 declares_constrained_type_template_parameter (tree type
)
15256 return (is_constrained_parameter (type
)
15257 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15261 /* Returns true if the parsed type actually represents the declaration of
15262 a template template-parameter. */
15265 declares_constrained_template_template_parameter (tree type
)
15267 return (is_constrained_parameter (type
)
15268 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15271 /* Parse a default argument for a type template-parameter.
15272 Note that diagnostics are handled in cp_parser_template_parameter. */
15275 cp_parser_default_type_template_argument (cp_parser
*parser
)
15277 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15279 /* Consume the `=' token. */
15280 cp_lexer_consume_token (parser
->lexer
);
15282 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15284 /* Parse the default-argument. */
15285 push_deferring_access_checks (dk_no_deferred
);
15286 tree default_argument
= cp_parser_type_id (parser
);
15287 pop_deferring_access_checks ();
15289 if (flag_concepts
&& type_uses_auto (default_argument
))
15291 error_at (token
->location
,
15292 "invalid use of %<auto%> in default template argument");
15293 return error_mark_node
;
15296 return default_argument
;
15299 /* Parse a default argument for a template template-parameter. */
15302 cp_parser_default_template_template_argument (cp_parser
*parser
)
15304 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15308 /* Consume the `='. */
15309 cp_lexer_consume_token (parser
->lexer
);
15310 /* Parse the id-expression. */
15311 push_deferring_access_checks (dk_no_deferred
);
15312 /* save token before parsing the id-expression, for error
15314 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15315 tree default_argument
15316 = cp_parser_id_expression (parser
,
15317 /*template_keyword_p=*/false,
15318 /*check_dependency_p=*/true,
15319 /*template_p=*/&is_template
,
15320 /*declarator_p=*/false,
15321 /*optional_p=*/false);
15322 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15323 /* If the id-expression was a template-id that refers to
15324 a template-class, we already have the declaration here,
15325 so no further lookup is needed. */
15328 /* Look up the name. */
15330 = cp_parser_lookup_name (parser
, default_argument
,
15332 /*is_template=*/is_template
,
15333 /*is_namespace=*/false,
15334 /*check_dependency=*/true,
15335 /*ambiguous_decls=*/NULL
,
15337 /* See if the default argument is valid. */
15338 default_argument
= check_template_template_default_arg (default_argument
);
15339 pop_deferring_access_checks ();
15340 return default_argument
;
15343 /* Parse a template-parameter.
15345 template-parameter:
15347 parameter-declaration
15349 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15350 the parameter. The TREE_PURPOSE is the default value, if any.
15351 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15352 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15353 set to true iff this parameter is a parameter pack. */
15356 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15357 bool *is_parameter_pack
)
15360 cp_parameter_declarator
*parameter_declarator
;
15363 /* Assume it is a type parameter or a template parameter. */
15364 *is_non_type
= false;
15365 /* Assume it not a parameter pack. */
15366 *is_parameter_pack
= false;
15367 /* Peek at the next token. */
15368 token
= cp_lexer_peek_token (parser
->lexer
);
15369 /* If it is `template', we have a type-parameter. */
15370 if (token
->keyword
== RID_TEMPLATE
)
15371 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15372 /* If it is `class' or `typename' we do not know yet whether it is a
15373 type parameter or a non-type parameter. Consider:
15375 template <typename T, typename T::X X> ...
15379 template <class C, class D*> ...
15381 Here, the first parameter is a type parameter, and the second is
15382 a non-type parameter. We can tell by looking at the token after
15383 the identifier -- if it is a `,', `=', or `>' then we have a type
15385 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15387 /* Peek at the token after `class' or `typename'. */
15388 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15389 /* If it's an ellipsis, we have a template type parameter
15391 if (token
->type
== CPP_ELLIPSIS
)
15392 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15393 /* If it's an identifier, skip it. */
15394 if (token
->type
== CPP_NAME
)
15395 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15396 /* Now, see if the token looks like the end of a template
15398 if (token
->type
== CPP_COMMA
15399 || token
->type
== CPP_EQ
15400 || token
->type
== CPP_GREATER
)
15401 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15404 /* Otherwise, it is a non-type parameter or a constrained parameter.
15408 When parsing a default template-argument for a non-type
15409 template-parameter, the first non-nested `>' is taken as the end
15410 of the template parameter-list rather than a greater-than
15412 parameter_declarator
15413 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15414 /*parenthesized_p=*/NULL
);
15416 if (!parameter_declarator
)
15417 return error_mark_node
;
15419 /* If the parameter declaration is marked as a parameter pack, set
15420 *IS_PARAMETER_PACK to notify the caller. */
15421 if (parameter_declarator
->template_parameter_pack_p
)
15422 *is_parameter_pack
= true;
15424 if (parameter_declarator
->default_argument
)
15426 /* Can happen in some cases of erroneous input (c++/34892). */
15427 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15428 /* Consume the `...' for better error recovery. */
15429 cp_lexer_consume_token (parser
->lexer
);
15432 // The parameter may have been constrained.
15433 if (is_constrained_parameter (parameter_declarator
))
15434 return finish_constrained_parameter (parser
,
15435 parameter_declarator
,
15437 is_parameter_pack
);
15439 // Now we're sure that the parameter is a non-type parameter.
15440 *is_non_type
= true;
15442 parm
= grokdeclarator (parameter_declarator
->declarator
,
15443 ¶meter_declarator
->decl_specifiers
,
15444 TPARM
, /*initialized=*/0,
15445 /*attrlist=*/NULL
);
15446 if (parm
== error_mark_node
)
15447 return error_mark_node
;
15449 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15452 /* Parse a type-parameter.
15455 class identifier [opt]
15456 class identifier [opt] = type-id
15457 typename identifier [opt]
15458 typename identifier [opt] = type-id
15459 template < template-parameter-list > class identifier [opt]
15460 template < template-parameter-list > class identifier [opt]
15463 GNU Extension (variadic templates):
15466 class ... identifier [opt]
15467 typename ... identifier [opt]
15469 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15470 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15471 the declaration of the parameter.
15473 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15476 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15481 /* Look for a keyword to tell us what kind of parameter this is. */
15482 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15484 return error_mark_node
;
15486 switch (token
->keyword
)
15492 tree default_argument
;
15494 /* If the next token is an ellipsis, we have a template
15496 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15498 /* Consume the `...' token. */
15499 cp_lexer_consume_token (parser
->lexer
);
15500 maybe_warn_variadic_templates ();
15502 *is_parameter_pack
= true;
15505 /* If the next token is an identifier, then it names the
15507 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15508 identifier
= cp_parser_identifier (parser
);
15510 identifier
= NULL_TREE
;
15512 /* Create the parameter. */
15513 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15515 /* If the next token is an `=', we have a default argument. */
15516 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15519 = cp_parser_default_type_template_argument (parser
);
15521 /* Template parameter packs cannot have default
15523 if (*is_parameter_pack
)
15526 error_at (token
->location
,
15527 "template parameter pack %qD cannot have a "
15528 "default argument", identifier
);
15530 error_at (token
->location
,
15531 "template parameter packs cannot have "
15532 "default arguments");
15533 default_argument
= NULL_TREE
;
15535 else if (check_for_bare_parameter_packs (default_argument
))
15536 default_argument
= error_mark_node
;
15539 default_argument
= NULL_TREE
;
15541 /* Create the combined representation of the parameter and the
15542 default argument. */
15543 parameter
= build_tree_list (default_argument
, parameter
);
15550 tree default_argument
;
15552 /* Look for the `<'. */
15553 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15554 /* Parse the template-parameter-list. */
15555 cp_parser_template_parameter_list (parser
);
15556 /* Look for the `>'. */
15557 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15559 // If template requirements are present, parse them.
15562 tree reqs
= get_shorthand_constraints (current_template_parms
);
15563 if (tree r
= cp_parser_requires_clause_opt (parser
))
15564 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15565 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15568 /* Look for the `class' or 'typename' keywords. */
15569 cp_parser_type_parameter_key (parser
);
15570 /* If the next token is an ellipsis, we have a template
15572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15574 /* Consume the `...' token. */
15575 cp_lexer_consume_token (parser
->lexer
);
15576 maybe_warn_variadic_templates ();
15578 *is_parameter_pack
= true;
15580 /* If the next token is an `=', then there is a
15581 default-argument. If the next token is a `>', we are at
15582 the end of the parameter-list. If the next token is a `,',
15583 then we are at the end of this parameter. */
15584 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15585 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15586 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15588 identifier
= cp_parser_identifier (parser
);
15589 /* Treat invalid names as if the parameter were nameless. */
15590 if (identifier
== error_mark_node
)
15591 identifier
= NULL_TREE
;
15594 identifier
= NULL_TREE
;
15596 /* Create the template parameter. */
15597 parameter
= finish_template_template_parm (class_type_node
,
15600 /* If the next token is an `=', then there is a
15601 default-argument. */
15602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15605 = cp_parser_default_template_template_argument (parser
);
15607 /* Template parameter packs cannot have default
15609 if (*is_parameter_pack
)
15612 error_at (token
->location
,
15613 "template parameter pack %qD cannot "
15614 "have a default argument",
15617 error_at (token
->location
, "template parameter packs cannot "
15618 "have default arguments");
15619 default_argument
= NULL_TREE
;
15623 default_argument
= NULL_TREE
;
15625 /* Create the combined representation of the parameter and the
15626 default argument. */
15627 parameter
= build_tree_list (default_argument
, parameter
);
15632 gcc_unreachable ();
15639 /* Parse a template-id.
15642 template-name < template-argument-list [opt] >
15644 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15645 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15646 returned. Otherwise, if the template-name names a function, or set
15647 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15648 names a class, returns a TYPE_DECL for the specialization.
15650 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15651 uninstantiated templates. */
15654 cp_parser_template_id (cp_parser
*parser
,
15655 bool template_keyword_p
,
15656 bool check_dependency_p
,
15657 enum tag_types tag_type
,
15658 bool is_declaration
)
15663 cp_token_position start_of_id
= 0;
15664 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15665 bool is_identifier
;
15667 /* If the next token corresponds to a template-id, there is no need
15669 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15670 if (token
->type
== CPP_TEMPLATE_ID
)
15672 cp_lexer_consume_token (parser
->lexer
);
15673 return saved_checks_value (token
->u
.tree_check_value
);
15676 /* Avoid performing name lookup if there is no possibility of
15677 finding a template-id. */
15678 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15679 || (token
->type
== CPP_NAME
15680 && !cp_parser_nth_token_starts_template_argument_list_p
15683 cp_parser_error (parser
, "expected template-id");
15684 return error_mark_node
;
15687 /* Remember where the template-id starts. */
15688 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15689 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15691 push_deferring_access_checks (dk_deferred
);
15693 /* Parse the template-name. */
15694 is_identifier
= false;
15695 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15696 check_dependency_p
,
15700 if (templ
== error_mark_node
|| is_identifier
)
15702 pop_deferring_access_checks ();
15706 /* Since we're going to preserve any side-effects from this parse, set up a
15707 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15708 in the template arguments. */
15709 tentative_firewall
firewall (parser
);
15711 /* If we find the sequence `[:' after a template-name, it's probably
15712 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15713 parse correctly the argument list. */
15714 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15715 == CPP_OPEN_SQUARE
)
15716 && next_token
->flags
& DIGRAPH
15717 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15719 && !(next_token_2
->flags
& PREV_WHITE
))
15721 cp_parser_parse_tentatively (parser
);
15722 /* Change `:' into `::'. */
15723 next_token_2
->type
= CPP_SCOPE
;
15724 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15726 cp_lexer_consume_token (parser
->lexer
);
15728 /* Parse the arguments. */
15729 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15730 if (!cp_parser_parse_definitely (parser
))
15732 /* If we couldn't parse an argument list, then we revert our changes
15733 and return simply an error. Maybe this is not a template-id
15735 next_token_2
->type
= CPP_COLON
;
15736 cp_parser_error (parser
, "expected %<<%>");
15737 pop_deferring_access_checks ();
15738 return error_mark_node
;
15740 /* Otherwise, emit an error about the invalid digraph, but continue
15741 parsing because we got our argument list. */
15742 if (permerror (next_token
->location
,
15743 "%<<::%> cannot begin a template-argument list"))
15745 static bool hint
= false;
15746 inform (next_token
->location
,
15747 "%<<:%> is an alternate spelling for %<[%>."
15748 " Insert whitespace between %<<%> and %<::%>");
15749 if (!hint
&& !flag_permissive
)
15751 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15752 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15753 "accept your code)");
15760 /* Look for the `<' that starts the template-argument-list. */
15761 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15763 pop_deferring_access_checks ();
15764 return error_mark_node
;
15766 /* Parse the arguments. */
15767 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15770 /* Set the location to be of the form:
15771 template-name < template-argument-list [opt] >
15772 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15773 with caret == start at the start of the template-name,
15774 ranging until the closing '>'. */
15775 location_t finish_loc
15776 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15777 location_t combined_loc
15778 = make_location (token
->location
, token
->location
, finish_loc
);
15780 /* Build a representation of the specialization. */
15781 if (identifier_p (templ
))
15782 template_id
= build_min_nt_loc (combined_loc
,
15785 else if (DECL_TYPE_TEMPLATE_P (templ
)
15786 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15788 bool entering_scope
;
15789 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15790 template (rather than some instantiation thereof) only if
15791 is not nested within some other construct. For example, in
15792 "template <typename T> void f(T) { A<T>::", A<T> is just an
15793 instantiation of A. */
15794 entering_scope
= (template_parm_scope_p ()
15795 && cp_lexer_next_token_is (parser
->lexer
,
15798 = finish_template_type (templ
, arguments
, entering_scope
);
15800 /* A template-like identifier may be a partial concept id. */
15801 else if (flag_concepts
15802 && (template_id
= (cp_parser_maybe_partial_concept_id
15803 (parser
, templ
, arguments
))))
15804 return template_id
;
15805 else if (variable_template_p (templ
))
15807 template_id
= lookup_template_variable (templ
, arguments
);
15808 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15809 SET_EXPR_LOCATION (template_id
, combined_loc
);
15813 /* If it's not a class-template or a template-template, it should be
15814 a function-template. */
15815 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15816 || TREE_CODE (templ
) == OVERLOAD
15817 || BASELINK_P (templ
)));
15819 template_id
= lookup_template_function (templ
, arguments
);
15820 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15821 SET_EXPR_LOCATION (template_id
, combined_loc
);
15824 /* If parsing tentatively, replace the sequence of tokens that makes
15825 up the template-id with a CPP_TEMPLATE_ID token. That way,
15826 should we re-parse the token stream, we will not have to repeat
15827 the effort required to do the parse, nor will we issue duplicate
15828 error messages about problems during instantiation of the
15831 /* Don't do this if we had a parse error in a declarator; re-parsing
15832 might succeed if a name changes meaning (60361). */
15833 && !(cp_parser_error_occurred (parser
)
15834 && cp_parser_parsing_tentatively (parser
)
15835 && parser
->in_declarator_p
))
15837 /* Reset the contents of the START_OF_ID token. */
15838 token
->type
= CPP_TEMPLATE_ID
;
15839 token
->location
= combined_loc
;
15841 /* We must mark the lookup as kept, so we don't throw it away on
15842 the first parse. */
15843 if (is_overloaded_fn (template_id
))
15844 lookup_keep (get_fns (template_id
), true);
15846 /* Retrieve any deferred checks. Do not pop this access checks yet
15847 so the memory will not be reclaimed during token replacing below. */
15848 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15849 token
->u
.tree_check_value
->value
= template_id
;
15850 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15851 token
->keyword
= RID_MAX
;
15853 /* Purge all subsequent tokens. */
15854 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15856 /* ??? Can we actually assume that, if template_id ==
15857 error_mark_node, we will have issued a diagnostic to the
15858 user, as opposed to simply marking the tentative parse as
15860 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15861 error_at (token
->location
, "parse error in template argument list");
15864 pop_to_parent_deferring_access_checks ();
15865 return template_id
;
15868 /* Parse a template-name.
15873 The standard should actually say:
15877 operator-function-id
15879 A defect report has been filed about this issue.
15881 A conversion-function-id cannot be a template name because they cannot
15882 be part of a template-id. In fact, looking at this code:
15884 a.operator K<int>()
15886 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15887 It is impossible to call a templated conversion-function-id with an
15888 explicit argument list, since the only allowed template parameter is
15889 the type to which it is converting.
15891 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15892 `template' keyword, in a construction like:
15896 In that case `f' is taken to be a template-name, even though there
15897 is no way of knowing for sure.
15899 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15900 name refers to a set of overloaded functions, at least one of which
15901 is a template, or an IDENTIFIER_NODE with the name of the template,
15902 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15903 names are looked up inside uninstantiated templates. */
15906 cp_parser_template_name (cp_parser
* parser
,
15907 bool template_keyword_p
,
15908 bool check_dependency_p
,
15909 bool is_declaration
,
15910 enum tag_types tag_type
,
15911 bool *is_identifier
)
15915 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15917 /* If the next token is `operator', then we have either an
15918 operator-function-id or a conversion-function-id. */
15919 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15921 /* We don't know whether we're looking at an
15922 operator-function-id or a conversion-function-id. */
15923 cp_parser_parse_tentatively (parser
);
15924 /* Try an operator-function-id. */
15925 identifier
= cp_parser_operator_function_id (parser
);
15926 /* If that didn't work, try a conversion-function-id. */
15927 if (!cp_parser_parse_definitely (parser
))
15929 cp_parser_error (parser
, "expected template-name");
15930 return error_mark_node
;
15933 /* Look for the identifier. */
15935 identifier
= cp_parser_identifier (parser
);
15937 /* If we didn't find an identifier, we don't have a template-id. */
15938 if (identifier
== error_mark_node
)
15939 return error_mark_node
;
15941 /* If the name immediately followed the `template' keyword, then it
15942 is a template-name. However, if the next token is not `<', then
15943 we do not treat it as a template-name, since it is not being used
15944 as part of a template-id. This enables us to handle constructs
15947 template <typename T> struct S { S(); };
15948 template <typename T> S<T>::S();
15950 correctly. We would treat `S' as a template -- if it were `S<T>'
15951 -- but we do not if there is no `<'. */
15953 if (processing_template_decl
15954 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15956 /* In a declaration, in a dependent context, we pretend that the
15957 "template" keyword was present in order to improve error
15958 recovery. For example, given:
15960 template <typename T> void f(T::X<int>);
15962 we want to treat "X<int>" as a template-id. */
15964 && !template_keyword_p
15965 && parser
->scope
&& TYPE_P (parser
->scope
)
15966 && check_dependency_p
15967 && dependent_scope_p (parser
->scope
)
15968 /* Do not do this for dtors (or ctors), since they never
15969 need the template keyword before their name. */
15970 && !constructor_name_p (identifier
, parser
->scope
))
15972 cp_token_position start
= 0;
15974 /* Explain what went wrong. */
15975 error_at (token
->location
, "non-template %qD used as template",
15977 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
15978 parser
->scope
, identifier
);
15979 /* If parsing tentatively, find the location of the "<" token. */
15980 if (cp_parser_simulate_error (parser
))
15981 start
= cp_lexer_token_position (parser
->lexer
, true);
15982 /* Parse the template arguments so that we can issue error
15983 messages about them. */
15984 cp_lexer_consume_token (parser
->lexer
);
15985 cp_parser_enclosed_template_argument_list (parser
);
15986 /* Skip tokens until we find a good place from which to
15987 continue parsing. */
15988 cp_parser_skip_to_closing_parenthesis (parser
,
15989 /*recovering=*/true,
15991 /*consume_paren=*/false);
15992 /* If parsing tentatively, permanently remove the
15993 template argument list. That will prevent duplicate
15994 error messages from being issued about the missing
15995 "template" keyword. */
15997 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
15999 *is_identifier
= true;
16000 parser
->context
->object_type
= NULL_TREE
;
16004 /* If the "template" keyword is present, then there is generally
16005 no point in doing name-lookup, so we just return IDENTIFIER.
16006 But, if the qualifying scope is non-dependent then we can
16007 (and must) do name-lookup normally. */
16008 if (template_keyword_p
)
16010 tree scope
= (parser
->scope
? parser
->scope
16011 : parser
->context
->object_type
);
16012 if (scope
&& TYPE_P (scope
)
16013 && (!CLASS_TYPE_P (scope
)
16014 || (check_dependency_p
&& dependent_type_p (scope
))))
16016 /* We're optimizing away the call to cp_parser_lookup_name, but
16017 we still need to do this. */
16018 parser
->context
->object_type
= NULL_TREE
;
16024 /* Look up the name. */
16025 decl
= cp_parser_lookup_name (parser
, identifier
,
16027 /*is_template=*/true,
16028 /*is_namespace=*/false,
16029 check_dependency_p
,
16030 /*ambiguous_decls=*/NULL
,
16033 decl
= strip_using_decl (decl
);
16035 /* If DECL is a template, then the name was a template-name. */
16036 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16038 if (TREE_DEPRECATED (decl
)
16039 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16040 warn_deprecated_use (decl
, NULL_TREE
);
16044 /* The standard does not explicitly indicate whether a name that
16045 names a set of overloaded declarations, some of which are
16046 templates, is a template-name. However, such a name should
16047 be a template-name; otherwise, there is no way to form a
16048 template-id for the overloaded templates. */
16049 bool found
= false;
16051 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16052 !found
&& iter
; ++iter
)
16053 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16058 /* The name does not name a template. */
16059 cp_parser_error (parser
, "expected template-name");
16060 return error_mark_node
;
16064 /* If DECL is dependent, and refers to a function, then just return
16065 its name; we will look it up again during template instantiation. */
16066 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16068 tree scope
= ovl_scope (decl
);
16069 if (TYPE_P (scope
) && dependent_type_p (scope
))
16076 /* Parse a template-argument-list.
16078 template-argument-list:
16079 template-argument ... [opt]
16080 template-argument-list , template-argument ... [opt]
16082 Returns a TREE_VEC containing the arguments. */
16085 cp_parser_template_argument_list (cp_parser
* parser
)
16087 tree fixed_args
[10];
16088 unsigned n_args
= 0;
16089 unsigned alloced
= 10;
16090 tree
*arg_ary
= fixed_args
;
16092 bool saved_in_template_argument_list_p
;
16094 bool saved_non_ice_p
;
16096 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16097 parser
->in_template_argument_list_p
= true;
16098 /* Even if the template-id appears in an integral
16099 constant-expression, the contents of the argument list do
16101 saved_ice_p
= parser
->integral_constant_expression_p
;
16102 parser
->integral_constant_expression_p
= false;
16103 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16104 parser
->non_integral_constant_expression_p
= false;
16106 /* Parse the arguments. */
16112 /* Consume the comma. */
16113 cp_lexer_consume_token (parser
->lexer
);
16115 /* Parse the template-argument. */
16116 argument
= cp_parser_template_argument (parser
);
16118 /* If the next token is an ellipsis, we're expanding a template
16120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16122 if (argument
== error_mark_node
)
16124 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16125 error_at (token
->location
,
16126 "expected parameter pack before %<...%>");
16128 /* Consume the `...' token. */
16129 cp_lexer_consume_token (parser
->lexer
);
16131 /* Make the argument into a TYPE_PACK_EXPANSION or
16132 EXPR_PACK_EXPANSION. */
16133 argument
= make_pack_expansion (argument
);
16136 if (n_args
== alloced
)
16140 if (arg_ary
== fixed_args
)
16142 arg_ary
= XNEWVEC (tree
, alloced
);
16143 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16146 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16148 arg_ary
[n_args
++] = argument
;
16150 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16152 vec
= make_tree_vec (n_args
);
16155 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16157 if (arg_ary
!= fixed_args
)
16159 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16160 parser
->integral_constant_expression_p
= saved_ice_p
;
16161 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16163 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16167 /* Parse a template-argument.
16170 assignment-expression
16174 The representation is that of an assignment-expression, type-id, or
16175 id-expression -- except that the qualified id-expression is
16176 evaluated, so that the value returned is either a DECL or an
16179 Although the standard says "assignment-expression", it forbids
16180 throw-expressions or assignments in the template argument.
16181 Therefore, we use "conditional-expression" instead. */
16184 cp_parser_template_argument (cp_parser
* parser
)
16189 bool maybe_type_id
= false;
16190 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16191 location_t loc
= 0;
16194 /* There's really no way to know what we're looking at, so we just
16195 try each alternative in order.
16199 In a template-argument, an ambiguity between a type-id and an
16200 expression is resolved to a type-id, regardless of the form of
16201 the corresponding template-parameter.
16203 Therefore, we try a type-id first. */
16204 cp_parser_parse_tentatively (parser
);
16205 argument
= cp_parser_template_type_arg (parser
);
16206 /* If there was no error parsing the type-id but the next token is a
16207 '>>', our behavior depends on which dialect of C++ we're
16208 parsing. In C++98, we probably found a typo for '> >'. But there
16209 are type-id which are also valid expressions. For instance:
16211 struct X { int operator >> (int); };
16212 template <int V> struct Foo {};
16215 Here 'X()' is a valid type-id of a function type, but the user just
16216 wanted to write the expression "X() >> 5". Thus, we remember that we
16217 found a valid type-id, but we still try to parse the argument as an
16218 expression to see what happens.
16220 In C++0x, the '>>' will be considered two separate '>'
16222 if (!cp_parser_error_occurred (parser
)
16223 && cxx_dialect
== cxx98
16224 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16226 maybe_type_id
= true;
16227 cp_parser_abort_tentative_parse (parser
);
16231 /* If the next token isn't a `,' or a `>', then this argument wasn't
16232 really finished. This means that the argument is not a valid
16234 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16235 cp_parser_error (parser
, "expected template-argument");
16236 /* If that worked, we're done. */
16237 if (cp_parser_parse_definitely (parser
))
16240 /* We're still not sure what the argument will be. */
16241 cp_parser_parse_tentatively (parser
);
16242 /* Try a template. */
16243 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16244 argument
= cp_parser_id_expression (parser
,
16245 /*template_keyword_p=*/false,
16246 /*check_dependency_p=*/true,
16248 /*declarator_p=*/false,
16249 /*optional_p=*/false);
16250 /* If the next token isn't a `,' or a `>', then this argument wasn't
16251 really finished. */
16252 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16253 cp_parser_error (parser
, "expected template-argument");
16254 if (!cp_parser_error_occurred (parser
))
16256 /* Figure out what is being referred to. If the id-expression
16257 was for a class template specialization, then we will have a
16258 TYPE_DECL at this point. There is no need to do name lookup
16259 at this point in that case. */
16260 if (TREE_CODE (argument
) != TYPE_DECL
)
16261 argument
= cp_parser_lookup_name (parser
, argument
,
16263 /*is_template=*/template_p
,
16264 /*is_namespace=*/false,
16265 /*check_dependency=*/true,
16266 /*ambiguous_decls=*/NULL
,
16267 argument_start_token
->location
);
16268 /* Handle a constrained-type-specifier for a non-type template
16270 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16272 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16273 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16274 cp_parser_error (parser
, "expected template-name");
16276 if (cp_parser_parse_definitely (parser
))
16278 if (TREE_DEPRECATED (argument
))
16279 warn_deprecated_use (argument
, NULL_TREE
);
16282 /* It must be a non-type argument. In C++17 any constant-expression is
16284 if (cxx_dialect
> cxx14
)
16287 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16289 -- an integral constant-expression of integral or enumeration
16292 -- the name of a non-type template-parameter; or
16294 -- the name of an object or function with external linkage...
16296 -- the address of an object or function with external linkage...
16298 -- a pointer to member... */
16299 /* Look for a non-type template parameter. */
16300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16302 cp_parser_parse_tentatively (parser
);
16303 argument
= cp_parser_primary_expression (parser
,
16304 /*address_p=*/false,
16306 /*template_arg_p=*/true,
16308 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16309 || !cp_parser_next_token_ends_template_argument_p (parser
))
16310 cp_parser_simulate_error (parser
);
16311 if (cp_parser_parse_definitely (parser
))
16315 /* If the next token is "&", the argument must be the address of an
16316 object or function with external linkage. */
16317 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16320 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16321 cp_lexer_consume_token (parser
->lexer
);
16323 /* See if we might have an id-expression. */
16324 token
= cp_lexer_peek_token (parser
->lexer
);
16325 if (token
->type
== CPP_NAME
16326 || token
->keyword
== RID_OPERATOR
16327 || token
->type
== CPP_SCOPE
16328 || token
->type
== CPP_TEMPLATE_ID
16329 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16331 cp_parser_parse_tentatively (parser
);
16332 argument
= cp_parser_primary_expression (parser
,
16335 /*template_arg_p=*/true,
16337 if (cp_parser_error_occurred (parser
)
16338 || !cp_parser_next_token_ends_template_argument_p (parser
))
16339 cp_parser_abort_tentative_parse (parser
);
16344 if (INDIRECT_REF_P (argument
))
16346 /* Strip the dereference temporarily. */
16347 gcc_assert (REFERENCE_REF_P (argument
));
16348 argument
= TREE_OPERAND (argument
, 0);
16351 /* If we're in a template, we represent a qualified-id referring
16352 to a static data member as a SCOPE_REF even if the scope isn't
16353 dependent so that we can check access control later. */
16355 if (TREE_CODE (probe
) == SCOPE_REF
)
16356 probe
= TREE_OPERAND (probe
, 1);
16359 /* A variable without external linkage might still be a
16360 valid constant-expression, so no error is issued here
16361 if the external-linkage check fails. */
16362 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16363 cp_parser_simulate_error (parser
);
16365 else if (is_overloaded_fn (argument
))
16366 /* All overloaded functions are allowed; if the external
16367 linkage test does not pass, an error will be issued
16371 && (TREE_CODE (argument
) == OFFSET_REF
16372 || TREE_CODE (argument
) == SCOPE_REF
))
16373 /* A pointer-to-member. */
16375 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16378 cp_parser_simulate_error (parser
);
16380 if (cp_parser_parse_definitely (parser
))
16383 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16384 tf_warning_or_error
);
16386 argument
= convert_from_reference (argument
);
16391 /* If the argument started with "&", there are no other valid
16392 alternatives at this point. */
16395 cp_parser_error (parser
, "invalid non-type template argument");
16396 return error_mark_node
;
16400 /* If the argument wasn't successfully parsed as a type-id followed
16401 by '>>', the argument can only be a constant expression now.
16402 Otherwise, we try parsing the constant-expression tentatively,
16403 because the argument could really be a type-id. */
16405 cp_parser_parse_tentatively (parser
);
16407 if (cxx_dialect
<= cxx14
)
16408 argument
= cp_parser_constant_expression (parser
);
16411 /* With C++17 generalized non-type template arguments we need to handle
16412 lvalue constant expressions, too. */
16413 argument
= cp_parser_assignment_expression (parser
);
16414 require_potential_constant_expression (argument
);
16417 if (!maybe_type_id
)
16419 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16420 cp_parser_error (parser
, "expected template-argument");
16421 if (cp_parser_parse_definitely (parser
))
16423 /* We did our best to parse the argument as a non type-id, but that
16424 was the only alternative that matched (albeit with a '>' after
16425 it). We can assume it's just a typo from the user, and a
16426 diagnostic will then be issued. */
16427 return cp_parser_template_type_arg (parser
);
16430 /* Parse an explicit-instantiation.
16432 explicit-instantiation:
16433 template declaration
16435 Although the standard says `declaration', what it really means is:
16437 explicit-instantiation:
16438 template decl-specifier-seq [opt] declarator [opt] ;
16440 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16441 supposed to be allowed. A defect report has been filed about this
16446 explicit-instantiation:
16447 storage-class-specifier template
16448 decl-specifier-seq [opt] declarator [opt] ;
16449 function-specifier template
16450 decl-specifier-seq [opt] declarator [opt] ; */
16453 cp_parser_explicit_instantiation (cp_parser
* parser
)
16455 int declares_class_or_enum
;
16456 cp_decl_specifier_seq decl_specifiers
;
16457 tree extension_specifier
= NULL_TREE
;
16459 timevar_push (TV_TEMPLATE_INST
);
16461 /* Look for an (optional) storage-class-specifier or
16462 function-specifier. */
16463 if (cp_parser_allow_gnu_extensions_p (parser
))
16465 extension_specifier
16466 = cp_parser_storage_class_specifier_opt (parser
);
16467 if (!extension_specifier
)
16468 extension_specifier
16469 = cp_parser_function_specifier_opt (parser
,
16470 /*decl_specs=*/NULL
);
16473 /* Look for the `template' keyword. */
16474 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16475 /* Let the front end know that we are processing an explicit
16477 begin_explicit_instantiation ();
16478 /* [temp.explicit] says that we are supposed to ignore access
16479 control while processing explicit instantiation directives. */
16480 push_deferring_access_checks (dk_no_check
);
16481 /* Parse a decl-specifier-seq. */
16482 cp_parser_decl_specifier_seq (parser
,
16483 CP_PARSER_FLAGS_OPTIONAL
,
16485 &declares_class_or_enum
);
16486 /* If there was exactly one decl-specifier, and it declared a class,
16487 and there's no declarator, then we have an explicit type
16489 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16493 type
= check_tag_decl (&decl_specifiers
,
16494 /*explicit_type_instantiation_p=*/true);
16495 /* Turn access control back on for names used during
16496 template instantiation. */
16497 pop_deferring_access_checks ();
16499 do_type_instantiation (type
, extension_specifier
,
16500 /*complain=*/tf_error
);
16504 cp_declarator
*declarator
;
16507 /* Parse the declarator. */
16509 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16510 /*ctor_dtor_or_conv_p=*/NULL
,
16511 /*parenthesized_p=*/NULL
,
16512 /*member_p=*/false,
16513 /*friend_p=*/false);
16514 if (declares_class_or_enum
& 2)
16515 cp_parser_check_for_definition_in_return_type (declarator
,
16516 decl_specifiers
.type
,
16517 decl_specifiers
.locations
[ds_type_spec
]);
16518 if (declarator
!= cp_error_declarator
)
16520 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16521 permerror (decl_specifiers
.locations
[ds_inline
],
16522 "explicit instantiation shall not use"
16523 " %<inline%> specifier");
16524 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16525 permerror (decl_specifiers
.locations
[ds_constexpr
],
16526 "explicit instantiation shall not use"
16527 " %<constexpr%> specifier");
16529 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16530 NORMAL
, 0, &decl_specifiers
.attributes
);
16531 /* Turn access control back on for names used during
16532 template instantiation. */
16533 pop_deferring_access_checks ();
16534 /* Do the explicit instantiation. */
16535 do_decl_instantiation (decl
, extension_specifier
);
16539 pop_deferring_access_checks ();
16540 /* Skip the body of the explicit instantiation. */
16541 cp_parser_skip_to_end_of_statement (parser
);
16544 /* We're done with the instantiation. */
16545 end_explicit_instantiation ();
16547 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16549 timevar_pop (TV_TEMPLATE_INST
);
16552 /* Parse an explicit-specialization.
16554 explicit-specialization:
16555 template < > declaration
16557 Although the standard says `declaration', what it really means is:
16559 explicit-specialization:
16560 template <> decl-specifier [opt] init-declarator [opt] ;
16561 template <> function-definition
16562 template <> explicit-specialization
16563 template <> template-declaration */
16566 cp_parser_explicit_specialization (cp_parser
* parser
)
16568 bool need_lang_pop
;
16569 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16571 /* Look for the `template' keyword. */
16572 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16573 /* Look for the `<'. */
16574 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16575 /* Look for the `>'. */
16576 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16577 /* We have processed another parameter list. */
16578 ++parser
->num_template_parameter_lists
;
16581 A template ... explicit specialization ... shall not have C
16583 if (current_lang_name
== lang_name_c
)
16585 error_at (token
->location
, "template specialization with C linkage");
16586 maybe_show_extern_c_location ();
16587 /* Give it C++ linkage to avoid confusing other parts of the
16589 push_lang_context (lang_name_cplusplus
);
16590 need_lang_pop
= true;
16593 need_lang_pop
= false;
16594 /* Let the front end know that we are beginning a specialization. */
16595 if (!begin_specialization ())
16597 end_specialization ();
16601 /* If the next keyword is `template', we need to figure out whether
16602 or not we're looking a template-declaration. */
16603 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16605 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16606 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16607 cp_parser_template_declaration_after_export (parser
,
16608 /*member_p=*/false);
16610 cp_parser_explicit_specialization (parser
);
16613 /* Parse the dependent declaration. */
16614 cp_parser_single_declaration (parser
,
16616 /*member_p=*/false,
16617 /*explicit_specialization_p=*/true,
16618 /*friend_p=*/NULL
);
16619 /* We're done with the specialization. */
16620 end_specialization ();
16621 /* For the erroneous case of a template with C linkage, we pushed an
16622 implicit C++ linkage scope; exit that scope now. */
16624 pop_lang_context ();
16625 /* We're done with this parameter list. */
16626 --parser
->num_template_parameter_lists
;
16629 /* Parse a type-specifier.
16632 simple-type-specifier
16635 elaborated-type-specifier
16643 Returns a representation of the type-specifier. For a
16644 class-specifier, enum-specifier, or elaborated-type-specifier, a
16645 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16647 The parser flags FLAGS is used to control type-specifier parsing.
16649 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16650 in a decl-specifier-seq.
16652 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16653 class-specifier, enum-specifier, or elaborated-type-specifier, then
16654 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16655 if a type is declared; 2 if it is defined. Otherwise, it is set to
16658 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16659 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16660 is set to FALSE. */
16663 cp_parser_type_specifier (cp_parser
* parser
,
16664 cp_parser_flags flags
,
16665 cp_decl_specifier_seq
*decl_specs
,
16666 bool is_declaration
,
16667 int* declares_class_or_enum
,
16668 bool* is_cv_qualifier
)
16670 tree type_spec
= NULL_TREE
;
16673 cp_decl_spec ds
= ds_last
;
16675 /* Assume this type-specifier does not declare a new type. */
16676 if (declares_class_or_enum
)
16677 *declares_class_or_enum
= 0;
16678 /* And that it does not specify a cv-qualifier. */
16679 if (is_cv_qualifier
)
16680 *is_cv_qualifier
= false;
16681 /* Peek at the next token. */
16682 token
= cp_lexer_peek_token (parser
->lexer
);
16684 /* If we're looking at a keyword, we can use that to guide the
16685 production we choose. */
16686 keyword
= token
->keyword
;
16690 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16691 goto elaborated_type_specifier
;
16693 /* Look for the enum-specifier. */
16694 type_spec
= cp_parser_enum_specifier (parser
);
16695 /* If that worked, we're done. */
16698 if (declares_class_or_enum
)
16699 *declares_class_or_enum
= 2;
16701 cp_parser_set_decl_spec_type (decl_specs
,
16704 /*type_definition_p=*/true);
16708 goto elaborated_type_specifier
;
16710 /* Any of these indicate either a class-specifier, or an
16711 elaborated-type-specifier. */
16715 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16716 goto elaborated_type_specifier
;
16718 /* Parse tentatively so that we can back up if we don't find a
16719 class-specifier. */
16720 cp_parser_parse_tentatively (parser
);
16721 /* Look for the class-specifier. */
16722 type_spec
= cp_parser_class_specifier (parser
);
16723 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16724 /* If that worked, we're done. */
16725 if (cp_parser_parse_definitely (parser
))
16727 if (declares_class_or_enum
)
16728 *declares_class_or_enum
= 2;
16730 cp_parser_set_decl_spec_type (decl_specs
,
16733 /*type_definition_p=*/true);
16737 /* Fall through. */
16738 elaborated_type_specifier
:
16739 /* We're declaring (not defining) a class or enum. */
16740 if (declares_class_or_enum
)
16741 *declares_class_or_enum
= 1;
16743 /* Fall through. */
16745 /* Look for an elaborated-type-specifier. */
16747 = (cp_parser_elaborated_type_specifier
16749 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16752 cp_parser_set_decl_spec_type (decl_specs
,
16755 /*type_definition_p=*/false);
16760 if (is_cv_qualifier
)
16761 *is_cv_qualifier
= true;
16766 if (is_cv_qualifier
)
16767 *is_cv_qualifier
= true;
16772 if (is_cv_qualifier
)
16773 *is_cv_qualifier
= true;
16777 /* The `__complex__' keyword is a GNU extension. */
16785 /* Handle simple keywords. */
16790 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16791 decl_specs
->any_specifiers_p
= true;
16793 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16796 /* If we do not already have a type-specifier, assume we are looking
16797 at a simple-type-specifier. */
16798 type_spec
= cp_parser_simple_type_specifier (parser
,
16802 /* If we didn't find a type-specifier, and a type-specifier was not
16803 optional in this context, issue an error message. */
16804 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16806 cp_parser_error (parser
, "expected type specifier");
16807 return error_mark_node
;
16813 /* Parse a simple-type-specifier.
16815 simple-type-specifier:
16816 :: [opt] nested-name-specifier [opt] type-name
16817 :: [opt] nested-name-specifier template template-id
16832 simple-type-specifier:
16834 decltype ( expression )
16837 __underlying_type ( type-id )
16841 nested-name-specifier(opt) template-name
16845 simple-type-specifier:
16847 __typeof__ unary-expression
16848 __typeof__ ( type-id )
16849 __typeof__ ( type-id ) { initializer-list , [opt] }
16851 Concepts Extension:
16853 simple-type-specifier:
16854 constrained-type-specifier
16856 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16857 appropriately updated. */
16860 cp_parser_simple_type_specifier (cp_parser
* parser
,
16861 cp_decl_specifier_seq
*decl_specs
,
16862 cp_parser_flags flags
)
16864 tree type
= NULL_TREE
;
16868 /* Peek at the next token. */
16869 token
= cp_lexer_peek_token (parser
->lexer
);
16871 /* If we're looking at a keyword, things are easy. */
16872 switch (token
->keyword
)
16876 decl_specs
->explicit_char_p
= true;
16877 type
= char_type_node
;
16880 type
= char16_type_node
;
16883 type
= char32_type_node
;
16886 type
= wchar_type_node
;
16889 type
= boolean_type_node
;
16892 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16893 type
= short_integer_type_node
;
16897 decl_specs
->explicit_int_p
= true;
16898 type
= integer_type_node
;
16904 idx
= token
->keyword
- RID_INT_N_0
;
16905 if (! int_n_enabled_p
[idx
])
16909 decl_specs
->explicit_intN_p
= true;
16910 decl_specs
->int_n_idx
= idx
;
16912 type
= int_n_trees
[idx
].signed_type
;
16916 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16917 type
= long_integer_type_node
;
16920 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16921 type
= integer_type_node
;
16924 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16925 type
= unsigned_type_node
;
16928 type
= float_type_node
;
16931 type
= double_type_node
;
16934 type
= void_type_node
;
16938 maybe_warn_cpp0x (CPP0X_AUTO
);
16939 if (parser
->auto_is_implicit_function_template_parm_p
)
16941 /* The 'auto' might be the placeholder return type for a function decl
16942 with trailing return type. */
16943 bool have_trailing_return_fn_decl
= false;
16945 cp_parser_parse_tentatively (parser
);
16946 cp_lexer_consume_token (parser
->lexer
);
16947 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16948 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16949 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16950 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16954 cp_lexer_consume_token (parser
->lexer
);
16955 cp_parser_skip_to_closing_parenthesis (parser
,
16956 /*recovering*/false,
16958 /*consume_paren*/true);
16962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
16964 have_trailing_return_fn_decl
= true;
16968 cp_lexer_consume_token (parser
->lexer
);
16970 cp_parser_abort_tentative_parse (parser
);
16972 if (have_trailing_return_fn_decl
)
16974 type
= make_auto ();
16978 if (cxx_dialect
>= cxx14
)
16980 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
16981 type
= TREE_TYPE (type
);
16984 type
= error_mark_node
;
16986 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
16988 if (cxx_dialect
< cxx14
)
16989 error_at (token
->location
,
16990 "use of %<auto%> in lambda parameter declaration "
16991 "only available with "
16992 "-std=c++14 or -std=gnu++14");
16994 else if (cxx_dialect
< cxx14
)
16995 error_at (token
->location
,
16996 "use of %<auto%> in parameter declaration "
16997 "only available with "
16998 "-std=c++14 or -std=gnu++14");
16999 else if (!flag_concepts
)
17000 pedwarn (token
->location
, OPT_Wpedantic
,
17001 "ISO C++ forbids use of %<auto%> in parameter "
17005 type
= make_auto ();
17009 /* Since DR 743, decltype can either be a simple-type-specifier by
17010 itself or begin a nested-name-specifier. Parsing it will replace
17011 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17012 handling below decide what to do. */
17013 cp_parser_decltype (parser
);
17014 cp_lexer_set_token_position (parser
->lexer
, token
);
17018 /* Consume the `typeof' token. */
17019 cp_lexer_consume_token (parser
->lexer
);
17020 /* Parse the operand to `typeof'. */
17021 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17022 /* If it is not already a TYPE, take its type. */
17023 if (!TYPE_P (type
))
17024 type
= finish_typeof (type
);
17027 cp_parser_set_decl_spec_type (decl_specs
, type
,
17029 /*type_definition_p=*/false);
17033 case RID_UNDERLYING_TYPE
:
17034 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17036 cp_parser_set_decl_spec_type (decl_specs
, type
,
17038 /*type_definition_p=*/false);
17043 case RID_DIRECT_BASES
:
17044 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17046 cp_parser_set_decl_spec_type (decl_specs
, type
,
17048 /*type_definition_p=*/false);
17054 /* If token is an already-parsed decltype not followed by ::,
17055 it's a simple-type-specifier. */
17056 if (token
->type
== CPP_DECLTYPE
17057 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17059 type
= saved_checks_value (token
->u
.tree_check_value
);
17062 cp_parser_set_decl_spec_type (decl_specs
, type
,
17064 /*type_definition_p=*/false);
17065 /* Remember that we are handling a decltype in order to
17066 implement the resolution of DR 1510 when the argument
17067 isn't instantiation dependent. */
17068 decl_specs
->decltype_p
= true;
17070 cp_lexer_consume_token (parser
->lexer
);
17074 /* If the type-specifier was for a built-in type, we're done. */
17077 /* Record the type. */
17079 && (token
->keyword
!= RID_SIGNED
17080 && token
->keyword
!= RID_UNSIGNED
17081 && token
->keyword
!= RID_SHORT
17082 && token
->keyword
!= RID_LONG
))
17083 cp_parser_set_decl_spec_type (decl_specs
,
17086 /*type_definition_p=*/false);
17088 decl_specs
->any_specifiers_p
= true;
17090 /* Consume the token. */
17091 cp_lexer_consume_token (parser
->lexer
);
17093 if (type
== error_mark_node
)
17094 return error_mark_node
;
17096 /* There is no valid C++ program where a non-template type is
17097 followed by a "<". That usually indicates that the user thought
17098 that the type was a template. */
17099 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17102 return TYPE_NAME (type
);
17105 /* The type-specifier must be a user-defined type. */
17106 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17111 /* Don't gobble tokens or issue error messages if this is an
17112 optional type-specifier. */
17113 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17114 cp_parser_parse_tentatively (parser
);
17116 token
= cp_lexer_peek_token (parser
->lexer
);
17118 /* Look for the optional `::' operator. */
17120 = (cp_parser_global_scope_opt (parser
,
17121 /*current_scope_valid_p=*/false)
17123 /* Look for the nested-name specifier. */
17125 = (cp_parser_nested_name_specifier_opt (parser
,
17126 /*typename_keyword_p=*/false,
17127 /*check_dependency_p=*/true,
17129 /*is_declaration=*/false)
17131 /* If we have seen a nested-name-specifier, and the next token
17132 is `template', then we are using the template-id production. */
17134 && cp_parser_optional_template_keyword (parser
))
17136 /* Look for the template-id. */
17137 type
= cp_parser_template_id (parser
,
17138 /*template_keyword_p=*/true,
17139 /*check_dependency_p=*/true,
17141 /*is_declaration=*/false);
17142 /* If the template-id did not name a type, we are out of
17144 if (TREE_CODE (type
) != TYPE_DECL
)
17146 cp_parser_error (parser
, "expected template-id for type");
17150 /* Otherwise, look for a type-name. */
17152 type
= cp_parser_type_name (parser
);
17153 /* Keep track of all name-lookups performed in class scopes. */
17157 && TREE_CODE (type
) == TYPE_DECL
17158 && identifier_p (DECL_NAME (type
)))
17159 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17160 /* If it didn't work out, we don't have a TYPE. */
17161 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17162 && !cp_parser_parse_definitely (parser
))
17164 if (!type
&& cxx_dialect
>= cxx17
)
17166 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17167 cp_parser_parse_tentatively (parser
);
17169 cp_parser_global_scope_opt (parser
,
17170 /*current_scope_valid_p=*/false);
17171 cp_parser_nested_name_specifier_opt (parser
,
17172 /*typename_keyword_p=*/false,
17173 /*check_dependency_p=*/true,
17175 /*is_declaration=*/false);
17176 tree name
= cp_parser_identifier (parser
);
17177 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17178 && parser
->scope
!= error_mark_node
)
17180 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17182 /*is_template=*/false,
17183 /*is_namespace=*/false,
17184 /*check_dependency=*/true,
17185 /*ambiguous_decls=*/NULL
,
17187 if (tmpl
&& tmpl
!= error_mark_node
17188 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17189 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17190 type
= make_template_placeholder (tmpl
);
17193 type
= error_mark_node
;
17194 if (!cp_parser_simulate_error (parser
))
17195 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17196 NLE_TYPE
, token
->location
);
17200 type
= error_mark_node
;
17202 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17203 && !cp_parser_parse_definitely (parser
))
17206 if (type
&& decl_specs
)
17207 cp_parser_set_decl_spec_type (decl_specs
, type
,
17209 /*type_definition_p=*/false);
17212 /* If we didn't get a type-name, issue an error message. */
17213 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17215 cp_parser_error (parser
, "expected type-name");
17216 return error_mark_node
;
17219 if (type
&& type
!= error_mark_node
)
17221 /* See if TYPE is an Objective-C type, and if so, parse and
17222 accept any protocol references following it. Do this before
17223 the cp_parser_check_for_invalid_template_id() call, because
17224 Objective-C types can be followed by '<...>' which would
17225 enclose protocol names rather than template arguments, and so
17226 everything is fine. */
17227 if (c_dialect_objc () && !parser
->scope
17228 && (objc_is_id (type
) || objc_is_class_name (type
)))
17230 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17231 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17233 /* Clobber the "unqualified" type previously entered into
17234 DECL_SPECS with the new, improved protocol-qualified version. */
17236 decl_specs
->type
= qual_type
;
17241 /* There is no valid C++ program where a non-template type is
17242 followed by a "<". That usually indicates that the user
17243 thought that the type was a template. */
17244 cp_parser_check_for_invalid_template_id (parser
, type
,
17252 /* Parse a type-name.
17258 simple-template-id [in c++0x]
17275 Returns a TYPE_DECL for the type. */
17278 cp_parser_type_name (cp_parser
* parser
)
17280 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17285 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17289 /* We can't know yet whether it is a class-name or not. */
17290 cp_parser_parse_tentatively (parser
);
17291 /* Try a class-name. */
17292 type_decl
= cp_parser_class_name (parser
,
17293 typename_keyword_p
,
17294 /*template_keyword_p=*/false,
17296 /*check_dependency_p=*/true,
17297 /*class_head_p=*/false,
17298 /*is_declaration=*/false);
17299 /* If it's not a class-name, keep looking. */
17300 if (!cp_parser_parse_definitely (parser
))
17302 if (cxx_dialect
< cxx11
)
17303 /* It must be a typedef-name or an enum-name. */
17304 return cp_parser_nonclass_name (parser
);
17306 cp_parser_parse_tentatively (parser
);
17307 /* It is either a simple-template-id representing an
17308 instantiation of an alias template... */
17309 type_decl
= cp_parser_template_id (parser
,
17310 /*template_keyword_p=*/false,
17311 /*check_dependency_p=*/true,
17313 /*is_declaration=*/false);
17314 /* Note that this must be an instantiation of an alias template
17315 because [temp.names]/6 says:
17317 A template-id that names an alias template specialization
17320 Whereas [temp.names]/7 says:
17322 A simple-template-id that names a class template
17323 specialization is a class-name.
17325 With concepts, this could also be a partial-concept-id that
17326 declares a non-type template parameter. */
17327 if (type_decl
!= NULL_TREE
17328 && TREE_CODE (type_decl
) == TYPE_DECL
17329 && TYPE_DECL_ALIAS_P (type_decl
))
17330 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17331 else if (is_constrained_parameter (type_decl
))
17332 /* Don't do anything. */ ;
17334 cp_parser_simulate_error (parser
);
17336 if (!cp_parser_parse_definitely (parser
))
17337 /* ... Or a typedef-name or an enum-name. */
17338 return cp_parser_nonclass_name (parser
);
17344 /* Check if DECL and ARGS can form a constrained-type-specifier.
17345 If ARGS is non-null, we try to form a concept check of the
17346 form DECL<?, ARGS> where ? is a wildcard that matches any
17347 kind of template argument. If ARGS is NULL, then we try to
17348 form a concept check of the form DECL<?>. */
17351 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17352 tree decl
, tree args
)
17354 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17356 /* If we a constrained-type-specifier cannot be deduced. */
17357 if (parser
->prevent_constrained_type_specifiers
)
17360 /* A constrained type specifier can only be found in an
17361 overload set or as a reference to a template declaration.
17363 FIXME: This might be masking a bug. It's possible that
17364 that the deduction below is causing template specializations
17365 to be formed with the wildcard as an argument. */
17366 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17369 /* Try to build a call expression that evaluates the
17370 concept. This can fail if the overload set refers
17371 only to non-templates. */
17372 tree placeholder
= build_nt (WILDCARD_DECL
);
17373 tree check
= build_concept_check (decl
, placeholder
, args
);
17374 if (check
== error_mark_node
)
17377 /* Deduce the checked constraint and the prototype parameter.
17379 FIXME: In certain cases, failure to deduce should be a
17380 diagnosable error. */
17383 if (!deduce_constrained_parameter (check
, conc
, proto
))
17386 /* In template parameter scope, this results in a constrained
17387 parameter. Return a descriptor of that parm. */
17388 if (processing_template_parmlist
)
17389 return build_constrained_parameter (conc
, proto
, args
);
17391 /* In a parameter-declaration-clause, constrained-type
17392 specifiers result in invented template parameters. */
17393 if (parser
->auto_is_implicit_function_template_parm_p
)
17395 tree x
= build_constrained_parameter (conc
, proto
, args
);
17396 return synthesize_implicit_template_parm (parser
, x
);
17400 /* Otherwise, we're in a context where the constrained
17401 type name is deduced and the constraint applies
17402 after deduction. */
17403 return make_constrained_auto (conc
, args
);
17409 /* If DECL refers to a concept, return a TYPE_DECL representing
17410 the result of using the constrained type specifier in the
17411 current context. DECL refers to a concept if
17413 - it is an overload set containing a function concept taking a single
17416 - it is a variable concept taking a single type argument. */
17419 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17422 && (TREE_CODE (decl
) == OVERLOAD
17423 || BASELINK_P (decl
)
17424 || variable_concept_p (decl
)))
17425 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17430 /* Check if DECL and ARGS form a partial-concept-id. If so,
17431 assign ID to the resulting constrained placeholder.
17433 Returns true if the partial-concept-id designates a placeholder
17434 and false otherwise. Note that *id is set to NULL_TREE in
17438 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17440 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17443 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17455 Returns a TYPE_DECL for the type. */
17458 cp_parser_nonclass_name (cp_parser
* parser
)
17463 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17464 identifier
= cp_parser_identifier (parser
);
17465 if (identifier
== error_mark_node
)
17466 return error_mark_node
;
17468 /* Look up the type-name. */
17469 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17471 type_decl
= strip_using_decl (type_decl
);
17473 /* If we found an overload set, then it may refer to a concept-name. */
17474 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17477 if (TREE_CODE (type_decl
) != TYPE_DECL
17478 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17480 /* See if this is an Objective-C type. */
17481 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17482 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17484 type_decl
= TYPE_NAME (type
);
17487 /* Issue an error if we did not find a type-name. */
17488 if (TREE_CODE (type_decl
) != TYPE_DECL
17489 /* In Objective-C, we have the complication that class names are
17490 normally type names and start declarations (eg, the
17491 "NSObject" in "NSObject *object;"), but can be used in an
17492 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17493 is an expression. So, a classname followed by a dot is not a
17494 valid type-name. */
17495 || (objc_is_class_name (TREE_TYPE (type_decl
))
17496 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17498 if (!cp_parser_simulate_error (parser
))
17499 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17500 NLE_TYPE
, token
->location
);
17501 return error_mark_node
;
17503 /* Remember that the name was used in the definition of the
17504 current class so that we can check later to see if the
17505 meaning would have been different after the class was
17506 entirely defined. */
17507 else if (type_decl
!= error_mark_node
17509 maybe_note_name_used_in_class (identifier
, type_decl
);
17514 /* Parse an elaborated-type-specifier. Note that the grammar given
17515 here incorporates the resolution to DR68.
17517 elaborated-type-specifier:
17518 class-key :: [opt] nested-name-specifier [opt] identifier
17519 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17520 enum-key :: [opt] nested-name-specifier [opt] identifier
17521 typename :: [opt] nested-name-specifier identifier
17522 typename :: [opt] nested-name-specifier template [opt]
17527 elaborated-type-specifier:
17528 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17529 class-key attributes :: [opt] nested-name-specifier [opt]
17530 template [opt] template-id
17531 enum attributes :: [opt] nested-name-specifier [opt] identifier
17533 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17534 declared `friend'. If IS_DECLARATION is TRUE, then this
17535 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17536 something is being declared.
17538 Returns the TYPE specified. */
17541 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17543 bool is_declaration
)
17545 enum tag_types tag_type
;
17547 tree type
= NULL_TREE
;
17548 tree attributes
= NULL_TREE
;
17550 cp_token
*token
= NULL
;
17552 /* See if we're looking at the `enum' keyword. */
17553 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17555 /* Consume the `enum' token. */
17556 cp_lexer_consume_token (parser
->lexer
);
17557 /* Remember that it's an enumeration type. */
17558 tag_type
= enum_type
;
17559 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17560 enums) is used here. */
17561 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17562 if (cp_parser_is_keyword (token
, RID_CLASS
)
17563 || cp_parser_is_keyword (token
, RID_STRUCT
))
17565 gcc_rich_location
richloc (token
->location
);
17566 richloc
.add_range (input_location
, false);
17567 richloc
.add_fixit_remove ();
17568 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17569 "a scoped enum must not use the %qD keyword",
17571 /* Consume the `struct' or `class' and parse it anyway. */
17572 cp_lexer_consume_token (parser
->lexer
);
17574 /* Parse the attributes. */
17575 attributes
= cp_parser_attributes_opt (parser
);
17577 /* Or, it might be `typename'. */
17578 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17581 /* Consume the `typename' token. */
17582 cp_lexer_consume_token (parser
->lexer
);
17583 /* Remember that it's a `typename' type. */
17584 tag_type
= typename_type
;
17586 /* Otherwise it must be a class-key. */
17589 tag_type
= cp_parser_class_key (parser
);
17590 if (tag_type
== none_type
)
17591 return error_mark_node
;
17592 /* Parse the attributes. */
17593 attributes
= cp_parser_attributes_opt (parser
);
17596 /* Look for the `::' operator. */
17597 globalscope
= cp_parser_global_scope_opt (parser
,
17598 /*current_scope_valid_p=*/false);
17599 /* Look for the nested-name-specifier. */
17600 tree nested_name_specifier
;
17601 if (tag_type
== typename_type
&& !globalscope
)
17603 nested_name_specifier
17604 = cp_parser_nested_name_specifier (parser
,
17605 /*typename_keyword_p=*/true,
17606 /*check_dependency_p=*/true,
17609 if (!nested_name_specifier
)
17610 return error_mark_node
;
17613 /* Even though `typename' is not present, the proposed resolution
17614 to Core Issue 180 says that in `class A<T>::B', `B' should be
17615 considered a type-name, even if `A<T>' is dependent. */
17616 nested_name_specifier
17617 = cp_parser_nested_name_specifier_opt (parser
,
17618 /*typename_keyword_p=*/true,
17619 /*check_dependency_p=*/true,
17622 /* For everything but enumeration types, consider a template-id.
17623 For an enumeration type, consider only a plain identifier. */
17624 if (tag_type
!= enum_type
)
17626 bool template_p
= false;
17629 /* Allow the `template' keyword. */
17630 template_p
= cp_parser_optional_template_keyword (parser
);
17631 /* If we didn't see `template', we don't know if there's a
17632 template-id or not. */
17634 cp_parser_parse_tentatively (parser
);
17635 /* Parse the template-id. */
17636 token
= cp_lexer_peek_token (parser
->lexer
);
17637 decl
= cp_parser_template_id (parser
, template_p
,
17638 /*check_dependency_p=*/true,
17641 /* If we didn't find a template-id, look for an ordinary
17643 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17645 /* We can get here when cp_parser_template_id, called by
17646 cp_parser_class_name with tag_type == none_type, succeeds
17647 and caches a BASELINK. Then, when called again here,
17648 instead of failing and returning an error_mark_node
17649 returns it (see template/typename17.C in C++11).
17650 ??? Could we diagnose this earlier? */
17651 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17653 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17654 type
= error_mark_node
;
17656 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17657 in effect, then we must assume that, upon instantiation, the
17658 template will correspond to a class. */
17659 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17660 && tag_type
== typename_type
)
17661 type
= make_typename_type (parser
->scope
, decl
,
17663 /*complain=*/tf_error
);
17664 /* If the `typename' keyword is in effect and DECL is not a type
17665 decl, then type is non existent. */
17666 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17668 else if (TREE_CODE (decl
) == TYPE_DECL
)
17670 type
= check_elaborated_type_specifier (tag_type
, decl
,
17671 /*allow_template_p=*/true);
17673 /* If the next token is a semicolon, this must be a specialization,
17674 instantiation, or friend declaration. Check the scope while we
17675 still know whether or not we had a nested-name-specifier. */
17676 if (type
!= error_mark_node
17677 && !nested_name_specifier
&& !is_friend
17678 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17679 check_unqualified_spec_or_inst (type
, token
->location
);
17681 else if (decl
== error_mark_node
)
17682 type
= error_mark_node
;
17687 token
= cp_lexer_peek_token (parser
->lexer
);
17688 identifier
= cp_parser_identifier (parser
);
17690 if (identifier
== error_mark_node
)
17692 parser
->scope
= NULL_TREE
;
17693 return error_mark_node
;
17696 /* For a `typename', we needn't call xref_tag. */
17697 if (tag_type
== typename_type
17698 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17699 return cp_parser_make_typename_type (parser
, identifier
,
17702 /* Template parameter lists apply only if we are not within a
17703 function parameter list. */
17704 bool template_parm_lists_apply
17705 = parser
->num_template_parameter_lists
;
17706 if (template_parm_lists_apply
)
17707 for (cp_binding_level
*s
= current_binding_level
;
17708 s
&& s
->kind
!= sk_template_parms
;
17709 s
= s
->level_chain
)
17710 if (s
->kind
== sk_function_parms
)
17711 template_parm_lists_apply
= false;
17713 /* Look up a qualified name in the usual way. */
17717 tree ambiguous_decls
;
17719 decl
= cp_parser_lookup_name (parser
, identifier
,
17721 /*is_template=*/false,
17722 /*is_namespace=*/false,
17723 /*check_dependency=*/true,
17727 /* If the lookup was ambiguous, an error will already have been
17729 if (ambiguous_decls
)
17730 return error_mark_node
;
17732 /* If we are parsing friend declaration, DECL may be a
17733 TEMPLATE_DECL tree node here. However, we need to check
17734 whether this TEMPLATE_DECL results in valid code. Consider
17735 the following example:
17738 template <class T> class C {};
17741 template <class T> friend class N::C; // #1, valid code
17743 template <class T> class Y {
17744 friend class N::C; // #2, invalid code
17747 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17748 name lookup of `N::C'. We see that friend declaration must
17749 be template for the code to be valid. Note that
17750 processing_template_decl does not work here since it is
17751 always 1 for the above two cases. */
17753 decl
= (cp_parser_maybe_treat_template_as_class
17754 (decl
, /*tag_name_p=*/is_friend
17755 && template_parm_lists_apply
));
17757 if (TREE_CODE (decl
) != TYPE_DECL
)
17759 cp_parser_diagnose_invalid_type_name (parser
,
17762 return error_mark_node
;
17765 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17767 bool allow_template
= (template_parm_lists_apply
17768 || DECL_SELF_REFERENCE_P (decl
));
17769 type
= check_elaborated_type_specifier (tag_type
, decl
,
17772 if (type
== error_mark_node
)
17773 return error_mark_node
;
17776 /* Forward declarations of nested types, such as
17781 are invalid unless all components preceding the final '::'
17782 are complete. If all enclosing types are complete, these
17783 declarations become merely pointless.
17785 Invalid forward declarations of nested types are errors
17786 caught elsewhere in parsing. Those that are pointless arrive
17789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17790 && !is_friend
&& !processing_explicit_instantiation
)
17791 warning (0, "declaration %qD does not declare anything", decl
);
17793 type
= TREE_TYPE (decl
);
17797 /* An elaborated-type-specifier sometimes introduces a new type and
17798 sometimes names an existing type. Normally, the rule is that it
17799 introduces a new type only if there is not an existing type of
17800 the same name already in scope. For example, given:
17803 void f() { struct S s; }
17805 the `struct S' in the body of `f' is the same `struct S' as in
17806 the global scope; the existing definition is used. However, if
17807 there were no global declaration, this would introduce a new
17808 local class named `S'.
17810 An exception to this rule applies to the following code:
17812 namespace N { struct S; }
17814 Here, the elaborated-type-specifier names a new type
17815 unconditionally; even if there is already an `S' in the
17816 containing scope this declaration names a new type.
17817 This exception only applies if the elaborated-type-specifier
17818 forms the complete declaration:
17822 A declaration consisting solely of `class-key identifier ;' is
17823 either a redeclaration of the name in the current scope or a
17824 forward declaration of the identifier as a class name. It
17825 introduces the name into the current scope.
17827 We are in this situation precisely when the next token is a `;'.
17829 An exception to the exception is that a `friend' declaration does
17830 *not* name a new type; i.e., given:
17832 struct S { friend struct T; };
17834 `T' is not a new type in the scope of `S'.
17836 Also, `new struct S' or `sizeof (struct S)' never results in the
17837 definition of a new type; a new type can only be declared in a
17838 declaration context. */
17844 /* Friends have special name lookup rules. */
17845 ts
= ts_within_enclosing_non_class
;
17846 else if (is_declaration
17847 && cp_lexer_next_token_is (parser
->lexer
,
17849 /* This is a `class-key identifier ;' */
17855 (template_parm_lists_apply
17856 && (cp_parser_next_token_starts_class_definition_p (parser
)
17857 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17858 /* An unqualified name was used to reference this type, so
17859 there were no qualifying templates. */
17860 if (template_parm_lists_apply
17861 && !cp_parser_check_template_parameters (parser
,
17862 /*num_templates=*/0,
17864 /*declarator=*/NULL
))
17865 return error_mark_node
;
17866 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17870 if (type
== error_mark_node
)
17871 return error_mark_node
;
17873 /* Allow attributes on forward declarations of classes. */
17876 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17877 warning (OPT_Wattributes
,
17878 "attributes ignored on uninstantiated type");
17879 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17880 && ! processing_explicit_instantiation
)
17881 warning (OPT_Wattributes
,
17882 "attributes ignored on template instantiation");
17883 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17884 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17886 warning (OPT_Wattributes
,
17887 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17890 if (tag_type
!= enum_type
)
17892 /* Indicate whether this class was declared as a `class' or as a
17894 if (CLASS_TYPE_P (type
))
17895 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17896 cp_parser_check_class_key (tag_type
, type
);
17899 /* A "<" cannot follow an elaborated type specifier. If that
17900 happens, the user was probably trying to form a template-id. */
17901 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17907 /* Parse an enum-specifier.
17910 enum-head { enumerator-list [opt] }
17911 enum-head { enumerator-list , } [C++0x]
17914 enum-key identifier [opt] enum-base [opt]
17915 enum-key nested-name-specifier identifier enum-base [opt]
17920 enum struct [C++0x]
17923 : type-specifier-seq
17925 opaque-enum-specifier:
17926 enum-key identifier enum-base [opt] ;
17929 enum-key attributes[opt] identifier [opt] enum-base [opt]
17930 { enumerator-list [opt] }attributes[opt]
17931 enum-key attributes[opt] identifier [opt] enum-base [opt]
17932 { enumerator-list, }attributes[opt] [C++0x]
17934 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17935 if the token stream isn't an enum-specifier after all. */
17938 cp_parser_enum_specifier (cp_parser
* parser
)
17941 tree type
= NULL_TREE
;
17943 tree nested_name_specifier
= NULL_TREE
;
17945 bool scoped_enum_p
= false;
17946 bool has_underlying_type
= false;
17947 bool nested_being_defined
= false;
17948 bool new_value_list
= false;
17949 bool is_new_type
= false;
17950 bool is_unnamed
= false;
17951 tree underlying_type
= NULL_TREE
;
17952 cp_token
*type_start_token
= NULL
;
17953 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17955 parser
->colon_corrects_to_scope_p
= false;
17957 /* Parse tentatively so that we can back up if we don't find a
17959 cp_parser_parse_tentatively (parser
);
17961 /* Caller guarantees that the current token is 'enum', an identifier
17962 possibly follows, and the token after that is an opening brace.
17963 If we don't have an identifier, fabricate an anonymous name for
17964 the enumeration being defined. */
17965 cp_lexer_consume_token (parser
->lexer
);
17967 /* Parse the "class" or "struct", which indicates a scoped
17968 enumeration type in C++0x. */
17969 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17970 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17972 if (cxx_dialect
< cxx11
)
17973 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17975 /* Consume the `struct' or `class' token. */
17976 cp_lexer_consume_token (parser
->lexer
);
17978 scoped_enum_p
= true;
17981 attributes
= cp_parser_attributes_opt (parser
);
17983 /* Clear the qualification. */
17984 parser
->scope
= NULL_TREE
;
17985 parser
->qualifying_scope
= NULL_TREE
;
17986 parser
->object_scope
= NULL_TREE
;
17988 /* Figure out in what scope the declaration is being placed. */
17989 prev_scope
= current_scope ();
17991 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17993 push_deferring_access_checks (dk_no_check
);
17994 nested_name_specifier
17995 = cp_parser_nested_name_specifier_opt (parser
,
17996 /*typename_keyword_p=*/true,
17997 /*check_dependency_p=*/false,
17999 /*is_declaration=*/false);
18001 if (nested_name_specifier
)
18005 identifier
= cp_parser_identifier (parser
);
18006 name
= cp_parser_lookup_name (parser
, identifier
,
18008 /*is_template=*/false,
18009 /*is_namespace=*/false,
18010 /*check_dependency=*/true,
18011 /*ambiguous_decls=*/NULL
,
18013 if (name
&& name
!= error_mark_node
)
18015 type
= TREE_TYPE (name
);
18016 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18018 /* Are template enums allowed in ISO? */
18019 if (template_parm_scope_p ())
18020 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18021 "%qD is an enumeration template", name
);
18022 /* ignore a typename reference, for it will be solved by name
18027 else if (nested_name_specifier
== error_mark_node
)
18028 /* We already issued an error. */;
18031 error_at (type_start_token
->location
,
18032 "%qD does not name an enumeration in %qT",
18033 identifier
, nested_name_specifier
);
18034 nested_name_specifier
= error_mark_node
;
18039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18040 identifier
= cp_parser_identifier (parser
);
18043 identifier
= make_anon_name ();
18046 error_at (type_start_token
->location
,
18047 "unnamed scoped enum is not allowed");
18050 pop_deferring_access_checks ();
18052 /* Check for the `:' that denotes a specified underlying type in C++0x.
18053 Note that a ':' could also indicate a bitfield width, however. */
18054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18056 cp_decl_specifier_seq type_specifiers
;
18058 /* Consume the `:'. */
18059 cp_lexer_consume_token (parser
->lexer
);
18061 /* Parse the type-specifier-seq. */
18062 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18063 /*is_trailing_return=*/false,
18066 /* At this point this is surely not elaborated type specifier. */
18067 if (!cp_parser_parse_definitely (parser
))
18070 if (cxx_dialect
< cxx11
)
18071 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18073 has_underlying_type
= true;
18075 /* If that didn't work, stop. */
18076 if (type_specifiers
.type
!= error_mark_node
)
18078 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18079 /*initialized=*/0, NULL
);
18080 if (underlying_type
== error_mark_node
18081 || check_for_bare_parameter_packs (underlying_type
))
18082 underlying_type
= NULL_TREE
;
18086 /* Look for the `{' but don't consume it yet. */
18087 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18089 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18091 cp_parser_error (parser
, "expected %<{%>");
18092 if (has_underlying_type
)
18098 /* An opaque-enum-specifier must have a ';' here. */
18099 if ((scoped_enum_p
|| underlying_type
)
18100 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18102 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18103 if (has_underlying_type
)
18111 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18114 if (nested_name_specifier
)
18116 if (CLASS_TYPE_P (nested_name_specifier
))
18118 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18119 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18120 push_scope (nested_name_specifier
);
18122 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18124 push_nested_namespace (nested_name_specifier
);
18128 /* Issue an error message if type-definitions are forbidden here. */
18129 if (!cp_parser_check_type_definition (parser
))
18130 type
= error_mark_node
;
18132 /* Create the new type. We do this before consuming the opening
18133 brace so the enum will be recorded as being on the line of its
18134 tag (or the 'enum' keyword, if there is no tag). */
18135 type
= start_enum (identifier
, type
, underlying_type
,
18136 attributes
, scoped_enum_p
, &is_new_type
);
18138 /* If the next token is not '{' it is an opaque-enum-specifier or an
18139 elaborated-type-specifier. */
18140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18142 timevar_push (TV_PARSE_ENUM
);
18143 if (nested_name_specifier
18144 && nested_name_specifier
!= error_mark_node
)
18146 /* The following catches invalid code such as:
18147 enum class S<int>::E { A, B, C }; */
18148 if (!processing_specialization
18149 && CLASS_TYPE_P (nested_name_specifier
)
18150 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18151 error_at (type_start_token
->location
, "cannot add an enumerator "
18152 "list to a template instantiation");
18154 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18156 error_at (type_start_token
->location
,
18157 "%<%T::%E%> has not been declared",
18158 TYPE_CONTEXT (nested_name_specifier
),
18159 nested_name_specifier
);
18160 type
= error_mark_node
;
18162 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18163 && !CLASS_TYPE_P (nested_name_specifier
))
18165 error_at (type_start_token
->location
, "nested name specifier "
18166 "%qT for enum declaration does not name a class "
18167 "or namespace", nested_name_specifier
);
18168 type
= error_mark_node
;
18170 /* If that scope does not contain the scope in which the
18171 class was originally declared, the program is invalid. */
18172 else if (prev_scope
&& !is_ancestor (prev_scope
,
18173 nested_name_specifier
))
18175 if (at_namespace_scope_p ())
18176 error_at (type_start_token
->location
,
18177 "declaration of %qD in namespace %qD which does not "
18179 type
, prev_scope
, nested_name_specifier
);
18181 error_at (type_start_token
->location
,
18182 "declaration of %qD in %qD which does not "
18184 type
, prev_scope
, nested_name_specifier
);
18185 type
= error_mark_node
;
18187 /* If that scope is the scope where the declaration is being placed
18188 the program is invalid. */
18189 else if (CLASS_TYPE_P (nested_name_specifier
)
18190 && CLASS_TYPE_P (prev_scope
)
18191 && same_type_p (nested_name_specifier
, prev_scope
))
18193 permerror (type_start_token
->location
,
18194 "extra qualification not allowed");
18195 nested_name_specifier
= NULL_TREE
;
18200 begin_scope (sk_scoped_enum
, type
);
18202 /* Consume the opening brace. */
18203 matching_braces braces
;
18204 braces
.consume_open (parser
);
18206 if (type
== error_mark_node
)
18207 ; /* Nothing to add */
18208 else if (OPAQUE_ENUM_P (type
)
18209 || (cxx_dialect
> cxx98
&& processing_specialization
))
18211 new_value_list
= true;
18212 SET_OPAQUE_ENUM_P (type
, false);
18213 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18217 error_at (type_start_token
->location
,
18218 "multiple definition of %q#T", type
);
18219 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18220 "previous definition here");
18221 type
= error_mark_node
;
18224 if (type
== error_mark_node
)
18225 cp_parser_skip_to_end_of_block_or_statement (parser
);
18226 /* If the next token is not '}', then there are some enumerators. */
18227 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18229 if (is_unnamed
&& !scoped_enum_p
)
18230 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18231 "ISO C++ forbids empty unnamed enum");
18234 cp_parser_enumerator_list (parser
, type
);
18236 /* Consume the final '}'. */
18237 braces
.require_close (parser
);
18241 timevar_pop (TV_PARSE_ENUM
);
18245 /* If a ';' follows, then it is an opaque-enum-specifier
18246 and additional restrictions apply. */
18247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18250 error_at (type_start_token
->location
,
18251 "opaque-enum-specifier without name");
18252 else if (nested_name_specifier
)
18253 error_at (type_start_token
->location
,
18254 "opaque-enum-specifier must use a simple identifier");
18258 /* Look for trailing attributes to apply to this enumeration, and
18259 apply them if appropriate. */
18260 if (cp_parser_allow_gnu_extensions_p (parser
))
18262 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18263 cplus_decl_attributes (&type
,
18265 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18268 /* Finish up the enumeration. */
18269 if (type
!= error_mark_node
)
18271 if (new_value_list
)
18272 finish_enum_value_list (type
);
18274 finish_enum (type
);
18277 if (nested_name_specifier
)
18279 if (CLASS_TYPE_P (nested_name_specifier
))
18281 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18282 pop_scope (nested_name_specifier
);
18284 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18286 pop_nested_namespace (nested_name_specifier
);
18290 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18294 /* Parse an enumerator-list. The enumerators all have the indicated
18298 enumerator-definition
18299 enumerator-list , enumerator-definition */
18302 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18306 /* Parse an enumerator-definition. */
18307 cp_parser_enumerator_definition (parser
, type
);
18309 /* If the next token is not a ',', we've reached the end of
18311 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18313 /* Otherwise, consume the `,' and keep going. */
18314 cp_lexer_consume_token (parser
->lexer
);
18315 /* If the next token is a `}', there is a trailing comma. */
18316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18318 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18319 pedwarn (input_location
, OPT_Wpedantic
,
18320 "comma at end of enumerator list");
18326 /* Parse an enumerator-definition. The enumerator has the indicated
18329 enumerator-definition:
18331 enumerator = constant-expression
18338 enumerator-definition:
18339 enumerator attributes [opt]
18340 enumerator attributes [opt] = constant-expression */
18343 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18349 /* Save the input location because we are interested in the location
18350 of the identifier and not the location of the explicit value. */
18351 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18353 /* Look for the identifier. */
18354 identifier
= cp_parser_identifier (parser
);
18355 if (identifier
== error_mark_node
)
18358 /* Parse any specified attributes. */
18359 tree attrs
= cp_parser_attributes_opt (parser
);
18361 /* If the next token is an '=', then there is an explicit value. */
18362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18364 /* Consume the `=' token. */
18365 cp_lexer_consume_token (parser
->lexer
);
18366 /* Parse the value. */
18367 value
= cp_parser_constant_expression (parser
);
18372 /* If we are processing a template, make sure the initializer of the
18373 enumerator doesn't contain any bare template parameter pack. */
18374 if (check_for_bare_parameter_packs (value
))
18375 value
= error_mark_node
;
18377 /* Create the enumerator. */
18378 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18381 /* Parse a namespace-name.
18384 original-namespace-name
18387 Returns the NAMESPACE_DECL for the namespace. */
18390 cp_parser_namespace_name (cp_parser
* parser
)
18393 tree namespace_decl
;
18395 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18397 /* Get the name of the namespace. */
18398 identifier
= cp_parser_identifier (parser
);
18399 if (identifier
== error_mark_node
)
18400 return error_mark_node
;
18402 /* Look up the identifier in the currently active scope. Look only
18403 for namespaces, due to:
18405 [basic.lookup.udir]
18407 When looking up a namespace-name in a using-directive or alias
18408 definition, only namespace names are considered.
18412 [basic.lookup.qual]
18414 During the lookup of a name preceding the :: scope resolution
18415 operator, object, function, and enumerator names are ignored.
18417 (Note that cp_parser_qualifying_entity only calls this
18418 function if the token after the name is the scope resolution
18420 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18422 /*is_template=*/false,
18423 /*is_namespace=*/true,
18424 /*check_dependency=*/true,
18425 /*ambiguous_decls=*/NULL
,
18427 /* If it's not a namespace, issue an error. */
18428 if (namespace_decl
== error_mark_node
18429 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18431 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18433 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18434 if (namespace_decl
== error_mark_node
18435 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18436 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18439 cp_parser_error (parser
, "expected namespace-name");
18440 namespace_decl
= error_mark_node
;
18443 return namespace_decl
;
18446 /* Parse a namespace-definition.
18448 namespace-definition:
18449 named-namespace-definition
18450 unnamed-namespace-definition
18452 named-namespace-definition:
18453 original-namespace-definition
18454 extension-namespace-definition
18456 original-namespace-definition:
18457 namespace identifier { namespace-body }
18459 extension-namespace-definition:
18460 namespace original-namespace-name { namespace-body }
18462 unnamed-namespace-definition:
18463 namespace { namespace-body } */
18466 cp_parser_namespace_definition (cp_parser
* parser
)
18469 int nested_definition_count
= 0;
18471 cp_ensure_no_omp_declare_simd (parser
);
18472 cp_ensure_no_oacc_routine (parser
);
18474 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18478 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18479 cp_lexer_consume_token (parser
->lexer
);
18482 /* Look for the `namespace' keyword. */
18484 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18486 /* Parse any specified attributes before the identifier. */
18487 tree attribs
= cp_parser_attributes_opt (parser
);
18491 identifier
= NULL_TREE
;
18493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18495 identifier
= cp_parser_identifier (parser
);
18497 /* Parse any attributes specified after the identifier. */
18498 attribs
= chainon (attribs
, cp_parser_attributes_opt (parser
));
18501 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18504 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18505 pedwarn (input_location
, OPT_Wpedantic
,
18506 "nested namespace definitions only available with "
18507 "-std=c++17 or -std=gnu++17");
18509 /* Nested namespace names can create new namespaces (unlike
18510 other qualified-ids). */
18511 if (int count
= identifier
? push_namespace (identifier
) : 0)
18512 nested_definition_count
+= count
;
18514 cp_parser_error (parser
, "nested namespace name required");
18515 cp_lexer_consume_token (parser
->lexer
);
18518 if (nested_definition_count
&& !identifier
)
18519 cp_parser_error (parser
, "namespace name required");
18521 if (nested_definition_count
&& attribs
)
18522 error_at (token
->location
,
18523 "a nested namespace definition cannot have attributes");
18524 if (nested_definition_count
&& is_inline
)
18525 error_at (token
->location
,
18526 "a nested namespace definition cannot be inline");
18528 /* Start the namespace. */
18529 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18531 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18533 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18535 /* Look for the `{' to validate starting the namespace. */
18536 matching_braces braces
;
18537 if (braces
.require_open (parser
))
18539 /* Parse the body of the namespace. */
18540 cp_parser_namespace_body (parser
);
18542 /* Look for the final `}'. */
18543 braces
.require_close (parser
);
18546 if (has_visibility
)
18547 pop_visibility (1);
18549 /* Pop the nested namespace definitions. */
18550 while (nested_definition_count
--)
18554 /* Parse a namespace-body.
18557 declaration-seq [opt] */
18560 cp_parser_namespace_body (cp_parser
* parser
)
18562 cp_parser_declaration_seq_opt (parser
);
18565 /* Parse a namespace-alias-definition.
18567 namespace-alias-definition:
18568 namespace identifier = qualified-namespace-specifier ; */
18571 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18574 tree namespace_specifier
;
18576 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18578 /* Look for the `namespace' keyword. */
18579 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18580 /* Look for the identifier. */
18581 identifier
= cp_parser_identifier (parser
);
18582 if (identifier
== error_mark_node
)
18584 /* Look for the `=' token. */
18585 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18586 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18588 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18589 /* Skip the definition. */
18590 cp_lexer_consume_token (parser
->lexer
);
18591 if (cp_parser_skip_to_closing_brace (parser
))
18592 cp_lexer_consume_token (parser
->lexer
);
18595 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18596 /* Look for the qualified-namespace-specifier. */
18597 namespace_specifier
18598 = cp_parser_qualified_namespace_specifier (parser
);
18599 /* Look for the `;' token. */
18600 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18602 /* Register the alias in the symbol table. */
18603 do_namespace_alias (identifier
, namespace_specifier
);
18606 /* Parse a qualified-namespace-specifier.
18608 qualified-namespace-specifier:
18609 :: [opt] nested-name-specifier [opt] namespace-name
18611 Returns a NAMESPACE_DECL corresponding to the specified
18615 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18617 /* Look for the optional `::'. */
18618 cp_parser_global_scope_opt (parser
,
18619 /*current_scope_valid_p=*/false);
18621 /* Look for the optional nested-name-specifier. */
18622 cp_parser_nested_name_specifier_opt (parser
,
18623 /*typename_keyword_p=*/false,
18624 /*check_dependency_p=*/true,
18626 /*is_declaration=*/true);
18628 return cp_parser_namespace_name (parser
);
18631 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18632 access declaration.
18635 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18636 using :: unqualified-id ;
18638 access-declaration:
18644 cp_parser_using_declaration (cp_parser
* parser
,
18645 bool access_declaration_p
)
18648 bool typename_p
= false;
18649 bool global_scope_p
;
18653 int oldcount
= errorcount
;
18654 cp_token
*diag_token
= NULL
;
18656 if (access_declaration_p
)
18658 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18659 cp_parser_parse_tentatively (parser
);
18663 /* Look for the `using' keyword. */
18664 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18667 /* Peek at the next token. */
18668 token
= cp_lexer_peek_token (parser
->lexer
);
18669 /* See if it's `typename'. */
18670 if (token
->keyword
== RID_TYPENAME
)
18672 /* Remember that we've seen it. */
18674 /* Consume the `typename' token. */
18675 cp_lexer_consume_token (parser
->lexer
);
18679 /* Look for the optional global scope qualification. */
18681 = (cp_parser_global_scope_opt (parser
,
18682 /*current_scope_valid_p=*/false)
18685 /* If we saw `typename', or didn't see `::', then there must be a
18686 nested-name-specifier present. */
18687 if (typename_p
|| !global_scope_p
)
18689 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18690 /*check_dependency_p=*/true,
18692 /*is_declaration=*/true);
18693 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18695 cp_parser_skip_to_end_of_block_or_statement (parser
);
18699 /* Otherwise, we could be in either of the two productions. In that
18700 case, treat the nested-name-specifier as optional. */
18702 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18703 /*typename_keyword_p=*/false,
18704 /*check_dependency_p=*/true,
18706 /*is_declaration=*/true);
18708 qscope
= global_namespace
;
18709 else if (UNSCOPED_ENUM_P (qscope
))
18710 qscope
= CP_TYPE_CONTEXT (qscope
);
18712 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18713 /* Something has already gone wrong; there's no need to parse
18714 further. Since an error has occurred, the return value of
18715 cp_parser_parse_definitely will be false, as required. */
18716 return cp_parser_parse_definitely (parser
);
18718 token
= cp_lexer_peek_token (parser
->lexer
);
18719 /* Parse the unqualified-id. */
18720 identifier
= cp_parser_unqualified_id (parser
,
18721 /*template_keyword_p=*/false,
18722 /*check_dependency_p=*/true,
18723 /*declarator_p=*/true,
18724 /*optional_p=*/false);
18726 if (access_declaration_p
)
18728 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18729 cp_parser_simulate_error (parser
);
18730 if (!cp_parser_parse_definitely (parser
))
18733 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18735 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18736 if (cxx_dialect
< cxx17
18737 && !in_system_header_at (ell
->location
))
18738 pedwarn (ell
->location
, 0,
18739 "pack expansion in using-declaration only available "
18740 "with -std=c++17 or -std=gnu++17");
18741 qscope
= make_pack_expansion (qscope
);
18744 /* The function we call to handle a using-declaration is different
18745 depending on what scope we are in. */
18746 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18748 else if (!identifier_p (identifier
)
18749 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18750 /* [namespace.udecl]
18752 A using declaration shall not name a template-id. */
18753 error_at (token
->location
,
18754 "a template-id may not appear in a using-declaration");
18757 if (at_class_scope_p ())
18759 /* Create the USING_DECL. */
18760 decl
= do_class_using_decl (qscope
, identifier
);
18762 if (decl
&& typename_p
)
18763 USING_DECL_TYPENAME_P (decl
) = 1;
18765 if (check_for_bare_parameter_packs (decl
))
18767 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18771 /* Add it to the list of members in this class. */
18772 finish_member_declaration (decl
);
18776 decl
= cp_parser_lookup_name_simple (parser
,
18779 if (decl
== error_mark_node
)
18780 cp_parser_name_lookup_error (parser
, identifier
,
18783 else if (check_for_bare_parameter_packs (decl
))
18785 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18788 else if (!at_namespace_scope_p ())
18789 finish_local_using_decl (decl
, qscope
, identifier
);
18791 finish_namespace_using_decl (decl
, qscope
, identifier
);
18795 if (!access_declaration_p
18796 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18798 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18799 if (cxx_dialect
< cxx17
)
18800 pedwarn (comma
->location
, 0,
18801 "comma-separated list in using-declaration only available "
18802 "with -std=c++17 or -std=gnu++17");
18806 /* Look for the final `;'. */
18807 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18809 if (access_declaration_p
&& errorcount
== oldcount
)
18810 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18811 "access declarations are deprecated "
18812 "in favour of using-declarations; "
18813 "suggestion: add the %<using%> keyword");
18818 /* Parse an alias-declaration.
18821 using identifier attribute-specifier-seq [opt] = type-id */
18824 cp_parser_alias_declaration (cp_parser
* parser
)
18826 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18827 location_t id_location
;
18828 cp_declarator
*declarator
;
18829 cp_decl_specifier_seq decl_specs
;
18831 const char *saved_message
= NULL
;
18833 /* Look for the `using' keyword. */
18834 cp_token
*using_token
18835 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18836 if (using_token
== NULL
)
18837 return error_mark_node
;
18839 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18840 id
= cp_parser_identifier (parser
);
18841 if (id
== error_mark_node
)
18842 return error_mark_node
;
18844 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18845 attributes
= cp_parser_attributes_opt (parser
);
18846 if (attributes
== error_mark_node
)
18847 return error_mark_node
;
18849 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18851 if (cp_parser_error_occurred (parser
))
18852 return error_mark_node
;
18854 cp_parser_commit_to_tentative_parse (parser
);
18856 /* Now we are going to parse the type-id of the declaration. */
18861 "A type-specifier-seq shall not define a class or enumeration
18862 unless it appears in the type-id of an alias-declaration (7.1.3) that
18863 is not the declaration of a template-declaration."
18865 In other words, if we currently are in an alias template, the
18866 type-id should not define a type.
18868 So let's set parser->type_definition_forbidden_message in that
18869 case; cp_parser_check_type_definition (called by
18870 cp_parser_class_specifier) will then emit an error if a type is
18871 defined in the type-id. */
18872 if (parser
->num_template_parameter_lists
)
18874 saved_message
= parser
->type_definition_forbidden_message
;
18875 parser
->type_definition_forbidden_message
=
18876 G_("types may not be defined in alias template declarations");
18879 type
= cp_parser_type_id (parser
);
18881 /* Restore the error message if need be. */
18882 if (parser
->num_template_parameter_lists
)
18883 parser
->type_definition_forbidden_message
= saved_message
;
18885 if (type
== error_mark_node
18886 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18888 cp_parser_skip_to_end_of_block_or_statement (parser
);
18889 return error_mark_node
;
18892 /* A typedef-name can also be introduced by an alias-declaration. The
18893 identifier following the using keyword becomes a typedef-name. It has
18894 the same semantics as if it were introduced by the typedef
18895 specifier. In particular, it does not define a new type and it shall
18896 not appear in the type-id. */
18898 clear_decl_specs (&decl_specs
);
18899 decl_specs
.type
= type
;
18900 if (attributes
!= NULL_TREE
)
18902 decl_specs
.attributes
= attributes
;
18903 set_and_check_decl_spec_loc (&decl_specs
,
18907 set_and_check_decl_spec_loc (&decl_specs
,
18910 set_and_check_decl_spec_loc (&decl_specs
,
18914 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18915 declarator
->id_loc
= id_location
;
18917 member_p
= at_class_scope_p ();
18919 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18920 NULL_TREE
, attributes
);
18922 decl
= start_decl (declarator
, &decl_specs
, 0,
18923 attributes
, NULL_TREE
, &pushed_scope
);
18924 if (decl
== error_mark_node
)
18927 // Attach constraints to the alias declaration.
18928 if (flag_concepts
&& current_template_parms
)
18930 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18931 tree constr
= build_constraints (reqs
, NULL_TREE
);
18932 set_constraints (decl
, constr
);
18935 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18938 pop_scope (pushed_scope
);
18940 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18941 added into the symbol table; otherwise, return the TYPE_DECL. */
18942 if (DECL_LANG_SPECIFIC (decl
)
18943 && DECL_TEMPLATE_INFO (decl
)
18944 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18946 decl
= DECL_TI_TEMPLATE (decl
);
18948 check_member_template (decl
);
18954 /* Parse a using-directive.
18957 using namespace :: [opt] nested-name-specifier [opt]
18958 namespace-name ; */
18961 cp_parser_using_directive (cp_parser
* parser
)
18963 tree namespace_decl
;
18966 /* Look for the `using' keyword. */
18967 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18968 /* And the `namespace' keyword. */
18969 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18970 /* Look for the optional `::' operator. */
18971 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18972 /* And the optional nested-name-specifier. */
18973 cp_parser_nested_name_specifier_opt (parser
,
18974 /*typename_keyword_p=*/false,
18975 /*check_dependency_p=*/true,
18977 /*is_declaration=*/true);
18978 /* Get the namespace being used. */
18979 namespace_decl
= cp_parser_namespace_name (parser
);
18980 /* And any specified attributes. */
18981 attribs
= cp_parser_attributes_opt (parser
);
18983 /* Update the symbol table. */
18984 if (namespace_bindings_p ())
18985 finish_namespace_using_directive (namespace_decl
, attribs
);
18987 finish_local_using_directive (namespace_decl
, attribs
);
18989 /* Look for the final `;'. */
18990 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18993 /* Parse an asm-definition.
18996 asm ( string-literal ) ;
19001 asm volatile [opt] ( string-literal ) ;
19002 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19003 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19004 : asm-operand-list [opt] ) ;
19005 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19006 : asm-operand-list [opt]
19007 : asm-clobber-list [opt] ) ;
19008 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19009 : asm-clobber-list [opt]
19010 : asm-goto-list ) ; */
19013 cp_parser_asm_definition (cp_parser
* parser
)
19016 tree outputs
= NULL_TREE
;
19017 tree inputs
= NULL_TREE
;
19018 tree clobbers
= NULL_TREE
;
19019 tree labels
= NULL_TREE
;
19021 bool volatile_p
= false;
19022 bool extended_p
= false;
19023 bool invalid_inputs_p
= false;
19024 bool invalid_outputs_p
= false;
19025 bool goto_p
= false;
19026 required_token missing
= RT_NONE
;
19028 /* Look for the `asm' keyword. */
19029 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19031 if (parser
->in_function_body
19032 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19034 error ("%<asm%> in %<constexpr%> function");
19035 cp_function_chain
->invalid_constexpr
= true;
19038 /* See if the next token is `volatile'. */
19039 if (cp_parser_allow_gnu_extensions_p (parser
)
19040 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19042 /* Remember that we saw the `volatile' keyword. */
19044 /* Consume the token. */
19045 cp_lexer_consume_token (parser
->lexer
);
19047 if (cp_parser_allow_gnu_extensions_p (parser
)
19048 && parser
->in_function_body
19049 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19051 /* Remember that we saw the `goto' keyword. */
19053 /* Consume the token. */
19054 cp_lexer_consume_token (parser
->lexer
);
19056 /* Look for the opening `('. */
19057 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19059 /* Look for the string. */
19060 string
= cp_parser_string_literal (parser
, false, false);
19061 if (string
== error_mark_node
)
19063 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19064 /*consume_paren=*/true);
19068 /* If we're allowing GNU extensions, check for the extended assembly
19069 syntax. Unfortunately, the `:' tokens need not be separated by
19070 a space in C, and so, for compatibility, we tolerate that here
19071 too. Doing that means that we have to treat the `::' operator as
19073 if (cp_parser_allow_gnu_extensions_p (parser
)
19074 && parser
->in_function_body
19075 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19076 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19078 bool inputs_p
= false;
19079 bool clobbers_p
= false;
19080 bool labels_p
= false;
19082 /* The extended syntax was used. */
19085 /* Look for outputs. */
19086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19088 /* Consume the `:'. */
19089 cp_lexer_consume_token (parser
->lexer
);
19090 /* Parse the output-operands. */
19091 if (cp_lexer_next_token_is_not (parser
->lexer
,
19093 && cp_lexer_next_token_is_not (parser
->lexer
,
19095 && cp_lexer_next_token_is_not (parser
->lexer
,
19099 outputs
= cp_parser_asm_operand_list (parser
);
19100 if (outputs
== error_mark_node
)
19101 invalid_outputs_p
= true;
19104 /* If the next token is `::', there are no outputs, and the
19105 next token is the beginning of the inputs. */
19106 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19107 /* The inputs are coming next. */
19110 /* Look for inputs. */
19112 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19114 /* Consume the `:' or `::'. */
19115 cp_lexer_consume_token (parser
->lexer
);
19116 /* Parse the output-operands. */
19117 if (cp_lexer_next_token_is_not (parser
->lexer
,
19119 && cp_lexer_next_token_is_not (parser
->lexer
,
19121 && cp_lexer_next_token_is_not (parser
->lexer
,
19124 inputs
= cp_parser_asm_operand_list (parser
);
19125 if (inputs
== error_mark_node
)
19126 invalid_inputs_p
= true;
19129 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19130 /* The clobbers are coming next. */
19133 /* Look for clobbers. */
19135 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19138 /* Consume the `:' or `::'. */
19139 cp_lexer_consume_token (parser
->lexer
);
19140 /* Parse the clobbers. */
19141 if (cp_lexer_next_token_is_not (parser
->lexer
,
19143 && cp_lexer_next_token_is_not (parser
->lexer
,
19145 clobbers
= cp_parser_asm_clobber_list (parser
);
19148 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19149 /* The labels are coming next. */
19152 /* Look for labels. */
19154 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19157 /* Consume the `:' or `::'. */
19158 cp_lexer_consume_token (parser
->lexer
);
19159 /* Parse the labels. */
19160 labels
= cp_parser_asm_label_list (parser
);
19163 if (goto_p
&& !labels_p
)
19164 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19167 missing
= RT_COLON_SCOPE
;
19169 /* Look for the closing `)'. */
19170 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19171 missing
? missing
: RT_CLOSE_PAREN
))
19172 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19173 /*consume_paren=*/true);
19174 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19176 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19178 /* Create the ASM_EXPR. */
19179 if (parser
->in_function_body
)
19181 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19182 inputs
, clobbers
, labels
);
19183 /* If the extended syntax was not used, mark the ASM_EXPR. */
19186 tree temp
= asm_stmt
;
19187 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19188 temp
= TREE_OPERAND (temp
, 0);
19190 ASM_INPUT_P (temp
) = 1;
19194 symtab
->finalize_toplevel_asm (string
);
19198 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19199 type that comes from the decl-specifier-seq. */
19202 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19204 for (cp_declarator
*d
= declarator
; d
;)
19214 if (TYPE_PTRMEMFUNC_P (type
))
19215 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19216 type
= TREE_TYPE (type
);
19224 /* Declarators [gram.dcl.decl] */
19226 /* Parse an init-declarator.
19229 declarator initializer [opt]
19234 declarator asm-specification [opt] attributes [opt] initializer [opt]
19236 function-definition:
19237 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19239 decl-specifier-seq [opt] declarator function-try-block
19243 function-definition:
19244 __extension__ function-definition
19248 function-definition:
19249 decl-specifier-seq [opt] declarator function-transaction-block
19251 The DECL_SPECIFIERS apply to this declarator. Returns a
19252 representation of the entity declared. If MEMBER_P is TRUE, then
19253 this declarator appears in a class scope. The new DECL created by
19254 this declarator is returned.
19256 The CHECKS are access checks that should be performed once we know
19257 what entity is being declared (and, therefore, what classes have
19260 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19261 for a function-definition here as well. If the declarator is a
19262 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19263 be TRUE upon return. By that point, the function-definition will
19264 have been completely parsed.
19266 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19269 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19270 parsed declaration if it is an uninitialized single declarator not followed
19271 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19272 if present, will not be consumed. If returned, this declarator will be
19273 created with SD_INITIALIZED but will not call cp_finish_decl.
19275 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19276 and there is an initializer, the pointed location_t is set to the
19277 location of the '=' or `(', or '{' in C++11 token introducing the
19281 cp_parser_init_declarator (cp_parser
* parser
,
19282 cp_decl_specifier_seq
*decl_specifiers
,
19283 vec
<deferred_access_check
, va_gc
> *checks
,
19284 bool function_definition_allowed_p
,
19286 int declares_class_or_enum
,
19287 bool* function_definition_p
,
19288 tree
* maybe_range_for_decl
,
19289 location_t
* init_loc
,
19292 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19293 *attributes_start_token
= NULL
;
19294 cp_declarator
*declarator
;
19295 tree prefix_attributes
;
19296 tree attributes
= NULL
;
19297 tree asm_specification
;
19299 tree decl
= NULL_TREE
;
19301 int is_initialized
;
19302 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19303 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19305 enum cpp_ttype initialization_kind
;
19306 bool is_direct_init
= false;
19307 bool is_non_constant_init
;
19308 int ctor_dtor_or_conv_p
;
19309 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19310 tree pushed_scope
= NULL_TREE
;
19311 bool range_for_decl_p
= false;
19312 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19313 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19315 /* Gather the attributes that were provided with the
19316 decl-specifiers. */
19317 prefix_attributes
= decl_specifiers
->attributes
;
19319 /* Assume that this is not the declarator for a function
19321 if (function_definition_p
)
19322 *function_definition_p
= false;
19324 /* Default arguments are only permitted for function parameters. */
19325 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19326 parser
->default_arg_ok_p
= false;
19328 /* Defer access checks while parsing the declarator; we cannot know
19329 what names are accessible until we know what is being
19331 resume_deferring_access_checks ();
19333 token
= cp_lexer_peek_token (parser
->lexer
);
19335 /* Parse the declarator. */
19337 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19338 &ctor_dtor_or_conv_p
,
19339 /*parenthesized_p=*/NULL
,
19340 member_p
, friend_p
);
19341 /* Gather up the deferred checks. */
19342 stop_deferring_access_checks ();
19344 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19346 /* If the DECLARATOR was erroneous, there's no need to go
19348 if (declarator
== cp_error_declarator
)
19349 return error_mark_node
;
19351 /* Check that the number of template-parameter-lists is OK. */
19352 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19354 return error_mark_node
;
19356 if (declares_class_or_enum
& 2)
19357 cp_parser_check_for_definition_in_return_type (declarator
,
19358 decl_specifiers
->type
,
19359 decl_specifiers
->locations
[ds_type_spec
]);
19361 /* Figure out what scope the entity declared by the DECLARATOR is
19362 located in. `grokdeclarator' sometimes changes the scope, so
19363 we compute it now. */
19364 scope
= get_scope_of_declarator (declarator
);
19366 /* Perform any lookups in the declared type which were thought to be
19367 dependent, but are not in the scope of the declarator. */
19368 decl_specifiers
->type
19369 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19371 /* If we're allowing GNU extensions, look for an
19372 asm-specification. */
19373 if (cp_parser_allow_gnu_extensions_p (parser
))
19375 /* Look for an asm-specification. */
19376 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19377 asm_specification
= cp_parser_asm_specification_opt (parser
);
19380 asm_specification
= NULL_TREE
;
19382 /* Look for attributes. */
19383 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19384 attributes
= cp_parser_attributes_opt (parser
);
19386 /* Peek at the next token. */
19387 token
= cp_lexer_peek_token (parser
->lexer
);
19389 bool bogus_implicit_tmpl
= false;
19391 if (function_declarator_p (declarator
))
19393 /* Handle C++17 deduction guides. */
19394 if (!decl_specifiers
->type
19395 && ctor_dtor_or_conv_p
<= 0
19396 && cxx_dialect
>= cxx17
)
19398 cp_declarator
*id
= get_id_declarator (declarator
);
19399 tree name
= id
->u
.id
.unqualified_name
;
19400 parser
->scope
= id
->u
.id
.qualifying_scope
;
19401 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19403 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19404 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19406 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19407 id
->u
.id
.sfk
= sfk_deduction_guide
;
19408 ctor_dtor_or_conv_p
= 1;
19412 /* Check to see if the token indicates the start of a
19413 function-definition. */
19414 if (cp_parser_token_starts_function_definition_p (token
))
19416 if (!function_definition_allowed_p
)
19418 /* If a function-definition should not appear here, issue an
19420 cp_parser_error (parser
,
19421 "a function-definition is not allowed here");
19422 return error_mark_node
;
19425 location_t func_brace_location
19426 = cp_lexer_peek_token (parser
->lexer
)->location
;
19428 /* Neither attributes nor an asm-specification are allowed
19429 on a function-definition. */
19430 if (asm_specification
)
19431 error_at (asm_spec_start_token
->location
,
19432 "an asm-specification is not allowed "
19433 "on a function-definition");
19435 error_at (attributes_start_token
->location
,
19436 "attributes are not allowed "
19437 "on a function-definition");
19438 /* This is a function-definition. */
19439 *function_definition_p
= true;
19441 /* Parse the function definition. */
19443 decl
= cp_parser_save_member_function_body (parser
,
19446 prefix_attributes
);
19449 (cp_parser_function_definition_from_specifiers_and_declarator
19450 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19452 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19454 /* This is where the prologue starts... */
19455 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19456 = func_brace_location
;
19462 else if (parser
->fully_implicit_function_template_p
)
19464 /* A non-template declaration involving a function parameter list
19465 containing an implicit template parameter will be made into a
19466 template. If the resulting declaration is not going to be an
19467 actual function then finish the template scope here to prevent it.
19468 An error message will be issued once we have a decl to talk about.
19470 FIXME probably we should do type deduction rather than create an
19471 implicit template, but the standard currently doesn't allow it. */
19472 bogus_implicit_tmpl
= true;
19473 finish_fully_implicit_template (parser
, NULL_TREE
);
19478 Only in function declarations for constructors, destructors, type
19479 conversions, and deduction guides can the decl-specifier-seq be omitted.
19481 We explicitly postpone this check past the point where we handle
19482 function-definitions because we tolerate function-definitions
19483 that are missing their return types in some modes. */
19484 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19486 cp_parser_error (parser
,
19487 "expected constructor, destructor, or type conversion");
19488 return error_mark_node
;
19491 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19492 if (token
->type
== CPP_EQ
19493 || token
->type
== CPP_OPEN_PAREN
19494 || token
->type
== CPP_OPEN_BRACE
)
19496 is_initialized
= SD_INITIALIZED
;
19497 initialization_kind
= token
->type
;
19498 if (maybe_range_for_decl
)
19499 *maybe_range_for_decl
= error_mark_node
;
19500 tmp_init_loc
= token
->location
;
19501 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19502 *init_loc
= tmp_init_loc
;
19504 if (token
->type
== CPP_EQ
19505 && function_declarator_p (declarator
))
19507 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19508 if (t2
->keyword
== RID_DEFAULT
)
19509 is_initialized
= SD_DEFAULTED
;
19510 else if (t2
->keyword
== RID_DELETE
)
19511 is_initialized
= SD_DELETED
;
19516 /* If the init-declarator isn't initialized and isn't followed by a
19517 `,' or `;', it's not a valid init-declarator. */
19518 if (token
->type
!= CPP_COMMA
19519 && token
->type
!= CPP_SEMICOLON
)
19521 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19522 range_for_decl_p
= true;
19525 if (!maybe_range_for_decl
)
19526 cp_parser_error (parser
, "expected initializer");
19527 return error_mark_node
;
19530 is_initialized
= SD_UNINITIALIZED
;
19531 initialization_kind
= CPP_EOF
;
19534 /* Because start_decl has side-effects, we should only call it if we
19535 know we're going ahead. By this point, we know that we cannot
19536 possibly be looking at any other construct. */
19537 cp_parser_commit_to_tentative_parse (parser
);
19539 /* Enter the newly declared entry in the symbol table. If we're
19540 processing a declaration in a class-specifier, we wait until
19541 after processing the initializer. */
19544 if (parser
->in_unbraced_linkage_specification_p
)
19545 decl_specifiers
->storage_class
= sc_extern
;
19546 decl
= start_decl (declarator
, decl_specifiers
,
19547 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19548 attributes
, prefix_attributes
, &pushed_scope
);
19549 cp_finalize_omp_declare_simd (parser
, decl
);
19550 cp_finalize_oacc_routine (parser
, decl
, false);
19551 /* Adjust location of decl if declarator->id_loc is more appropriate:
19552 set, and decl wasn't merged with another decl, in which case its
19553 location would be different from input_location, and more accurate. */
19555 && declarator
->id_loc
!= UNKNOWN_LOCATION
19556 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19557 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19560 /* Enter the SCOPE. That way unqualified names appearing in the
19561 initializer will be looked up in SCOPE. */
19562 pushed_scope
= push_scope (scope
);
19564 /* Perform deferred access control checks, now that we know in which
19565 SCOPE the declared entity resides. */
19566 if (!member_p
&& decl
)
19568 tree saved_current_function_decl
= NULL_TREE
;
19570 /* If the entity being declared is a function, pretend that we
19571 are in its scope. If it is a `friend', it may have access to
19572 things that would not otherwise be accessible. */
19573 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19575 saved_current_function_decl
= current_function_decl
;
19576 current_function_decl
= decl
;
19579 /* Perform access checks for template parameters. */
19580 cp_parser_perform_template_parameter_access_checks (checks
);
19582 /* Perform the access control checks for the declarator and the
19583 decl-specifiers. */
19584 perform_deferred_access_checks (tf_warning_or_error
);
19586 /* Restore the saved value. */
19587 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19588 current_function_decl
= saved_current_function_decl
;
19591 /* Parse the initializer. */
19592 initializer
= NULL_TREE
;
19593 is_direct_init
= false;
19594 is_non_constant_init
= true;
19595 if (is_initialized
)
19597 if (function_declarator_p (declarator
))
19599 if (initialization_kind
== CPP_EQ
)
19600 initializer
= cp_parser_pure_specifier (parser
);
19603 /* If the declaration was erroneous, we don't really
19604 know what the user intended, so just silently
19605 consume the initializer. */
19606 if (decl
!= error_mark_node
)
19607 error_at (tmp_init_loc
, "initializer provided for function");
19608 cp_parser_skip_to_closing_parenthesis (parser
,
19609 /*recovering=*/true,
19610 /*or_comma=*/false,
19611 /*consume_paren=*/true);
19616 /* We want to record the extra mangling scope for in-class
19617 initializers of class members and initializers of static data
19618 member templates. The former involves deferring
19619 parsing of the initializer until end of class as with default
19620 arguments. So right here we only handle the latter. */
19621 if (!member_p
&& processing_template_decl
)
19622 start_lambda_scope (decl
);
19623 initializer
= cp_parser_initializer (parser
,
19625 &is_non_constant_init
);
19626 if (!member_p
&& processing_template_decl
)
19627 finish_lambda_scope ();
19628 if (initializer
== error_mark_node
)
19629 cp_parser_skip_to_end_of_statement (parser
);
19633 /* The old parser allows attributes to appear after a parenthesized
19634 initializer. Mark Mitchell proposed removing this functionality
19635 on the GCC mailing lists on 2002-08-13. This parser accepts the
19636 attributes -- but ignores them. */
19637 if (cp_parser_allow_gnu_extensions_p (parser
)
19638 && initialization_kind
== CPP_OPEN_PAREN
)
19639 if (cp_parser_attributes_opt (parser
))
19640 warning (OPT_Wattributes
,
19641 "attributes after parenthesized initializer ignored");
19643 /* And now complain about a non-function implicit template. */
19644 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19645 error_at (DECL_SOURCE_LOCATION (decl
),
19646 "non-function %qD declared as implicit template", decl
);
19648 /* For an in-class declaration, use `grokfield' to create the
19654 pop_scope (pushed_scope
);
19655 pushed_scope
= NULL_TREE
;
19657 decl
= grokfield (declarator
, decl_specifiers
,
19658 initializer
, !is_non_constant_init
,
19659 /*asmspec=*/NULL_TREE
,
19660 chainon (attributes
, prefix_attributes
));
19661 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19662 cp_parser_save_default_args (parser
, decl
);
19663 cp_finalize_omp_declare_simd (parser
, decl
);
19664 cp_finalize_oacc_routine (parser
, decl
, false);
19667 /* Finish processing the declaration. But, skip member
19669 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19671 cp_finish_decl (decl
,
19672 initializer
, !is_non_constant_init
,
19674 /* If the initializer is in parentheses, then this is
19675 a direct-initialization, which means that an
19676 `explicit' constructor is OK. Otherwise, an
19677 `explicit' constructor cannot be used. */
19678 ((is_direct_init
|| !is_initialized
)
19679 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19681 else if ((cxx_dialect
!= cxx98
) && friend_p
19682 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19683 /* Core issue #226 (C++0x only): A default template-argument
19684 shall not be specified in a friend class template
19686 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19687 /*is_partial=*/false, /*is_friend_decl=*/1);
19689 if (!friend_p
&& pushed_scope
)
19690 pop_scope (pushed_scope
);
19692 if (function_declarator_p (declarator
)
19693 && parser
->fully_implicit_function_template_p
)
19696 decl
= finish_fully_implicit_template (parser
, decl
);
19698 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19701 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19702 && type_uses_auto (decl_specifiers
->type
))
19703 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19708 /* Parse a declarator.
19712 ptr-operator declarator
19714 abstract-declarator:
19715 ptr-operator abstract-declarator [opt]
19716 direct-abstract-declarator
19721 attributes [opt] direct-declarator
19722 attributes [opt] ptr-operator declarator
19724 abstract-declarator:
19725 attributes [opt] ptr-operator abstract-declarator [opt]
19726 attributes [opt] direct-abstract-declarator
19728 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19729 detect constructors, destructors, deduction guides, or conversion operators.
19730 It is set to -1 if the declarator is a name, and +1 if it is a
19731 function. Otherwise it is set to zero. Usually you just want to
19732 test for >0, but internally the negative value is used.
19734 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19735 a decl-specifier-seq unless it declares a constructor, destructor,
19736 or conversion. It might seem that we could check this condition in
19737 semantic analysis, rather than parsing, but that makes it difficult
19738 to handle something like `f()'. We want to notice that there are
19739 no decl-specifiers, and therefore realize that this is an
19740 expression, not a declaration.)
19742 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19743 the declarator is a direct-declarator of the form "(...)".
19745 MEMBER_P is true iff this declarator is a member-declarator.
19747 FRIEND_P is true iff this declarator is a friend. */
19749 static cp_declarator
*
19750 cp_parser_declarator (cp_parser
* parser
,
19751 cp_parser_declarator_kind dcl_kind
,
19752 int* ctor_dtor_or_conv_p
,
19753 bool* parenthesized_p
,
19754 bool member_p
, bool friend_p
)
19756 cp_declarator
*declarator
;
19757 enum tree_code code
;
19758 cp_cv_quals cv_quals
;
19760 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19762 /* Assume this is not a constructor, destructor, or type-conversion
19764 if (ctor_dtor_or_conv_p
)
19765 *ctor_dtor_or_conv_p
= 0;
19767 if (cp_parser_allow_gnu_extensions_p (parser
))
19768 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19770 /* Check for the ptr-operator production. */
19771 cp_parser_parse_tentatively (parser
);
19772 /* Parse the ptr-operator. */
19773 code
= cp_parser_ptr_operator (parser
,
19778 /* If that worked, then we have a ptr-operator. */
19779 if (cp_parser_parse_definitely (parser
))
19781 /* If a ptr-operator was found, then this declarator was not
19783 if (parenthesized_p
)
19784 *parenthesized_p
= true;
19785 /* The dependent declarator is optional if we are parsing an
19786 abstract-declarator. */
19787 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19788 cp_parser_parse_tentatively (parser
);
19790 /* Parse the dependent declarator. */
19791 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19792 /*ctor_dtor_or_conv_p=*/NULL
,
19793 /*parenthesized_p=*/NULL
,
19794 /*member_p=*/false,
19797 /* If we are parsing an abstract-declarator, we must handle the
19798 case where the dependent declarator is absent. */
19799 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19800 && !cp_parser_parse_definitely (parser
))
19803 declarator
= cp_parser_make_indirect_declarator
19804 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19806 /* Everything else is a direct-declarator. */
19809 if (parenthesized_p
)
19810 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19812 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19813 ctor_dtor_or_conv_p
,
19814 member_p
, friend_p
);
19817 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19818 declarator
->attributes
= gnu_attributes
;
19822 /* Parse a direct-declarator or direct-abstract-declarator.
19826 direct-declarator ( parameter-declaration-clause )
19827 cv-qualifier-seq [opt]
19828 ref-qualifier [opt]
19829 exception-specification [opt]
19830 direct-declarator [ constant-expression [opt] ]
19833 direct-abstract-declarator:
19834 direct-abstract-declarator [opt]
19835 ( parameter-declaration-clause )
19836 cv-qualifier-seq [opt]
19837 ref-qualifier [opt]
19838 exception-specification [opt]
19839 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19840 ( abstract-declarator )
19842 Returns a representation of the declarator. DCL_KIND is
19843 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19844 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19845 we are parsing a direct-declarator. It is
19846 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19847 of ambiguity we prefer an abstract declarator, as per
19848 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19849 as for cp_parser_declarator. */
19851 static cp_declarator
*
19852 cp_parser_direct_declarator (cp_parser
* parser
,
19853 cp_parser_declarator_kind dcl_kind
,
19854 int* ctor_dtor_or_conv_p
,
19855 bool member_p
, bool friend_p
)
19858 cp_declarator
*declarator
= NULL
;
19859 tree scope
= NULL_TREE
;
19860 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19861 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19863 tree pushed_scope
= NULL_TREE
;
19864 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19868 /* Peek at the next token. */
19869 token
= cp_lexer_peek_token (parser
->lexer
);
19870 if (token
->type
== CPP_OPEN_PAREN
)
19872 /* This is either a parameter-declaration-clause, or a
19873 parenthesized declarator. When we know we are parsing a
19874 named declarator, it must be a parenthesized declarator
19875 if FIRST is true. For instance, `(int)' is a
19876 parameter-declaration-clause, with an omitted
19877 direct-abstract-declarator. But `((*))', is a
19878 parenthesized abstract declarator. Finally, when T is a
19879 template parameter `(T)' is a
19880 parameter-declaration-clause, and not a parenthesized
19883 We first try and parse a parameter-declaration-clause,
19884 and then try a nested declarator (if FIRST is true).
19886 It is not an error for it not to be a
19887 parameter-declaration-clause, even when FIRST is
19893 The first is the declaration of a function while the
19894 second is the definition of a variable, including its
19897 Having seen only the parenthesis, we cannot know which of
19898 these two alternatives should be selected. Even more
19899 complex are examples like:
19904 The former is a function-declaration; the latter is a
19905 variable initialization.
19907 Thus again, we try a parameter-declaration-clause, and if
19908 that fails, we back out and return. */
19910 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19913 bool is_declarator
= false;
19917 /* In a member-declarator, the only valid interpretation
19918 of a parenthesis is the start of a
19919 parameter-declaration-clause. (It is invalid to
19920 initialize a static data member with a parenthesized
19921 initializer; only the "=" form of initialization is
19924 cp_parser_parse_tentatively (parser
);
19926 /* Consume the `('. */
19927 matching_parens parens
;
19928 parens
.consume_open (parser
);
19931 /* If this is going to be an abstract declarator, we're
19932 in a declarator and we can't have default args. */
19933 parser
->default_arg_ok_p
= false;
19934 parser
->in_declarator_p
= true;
19937 begin_scope (sk_function_parms
, NULL_TREE
);
19939 /* Parse the parameter-declaration-clause. */
19940 params
= cp_parser_parameter_declaration_clause (parser
);
19942 /* Consume the `)'. */
19943 parens
.require_close (parser
);
19945 /* If all went well, parse the cv-qualifier-seq,
19946 ref-qualifier and the exception-specification. */
19947 if (member_p
|| cp_parser_parse_definitely (parser
))
19949 cp_cv_quals cv_quals
;
19950 cp_virt_specifiers virt_specifiers
;
19951 cp_ref_qualifier ref_qual
;
19952 tree exception_specification
;
19955 bool memfn
= (member_p
|| (pushed_scope
19956 && CLASS_TYPE_P (pushed_scope
)));
19958 is_declarator
= true;
19960 if (ctor_dtor_or_conv_p
)
19961 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
19964 /* Parse the cv-qualifier-seq. */
19965 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19966 /* Parse the ref-qualifier. */
19967 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
19968 /* Parse the tx-qualifier. */
19969 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
19970 /* And the exception-specification. */
19971 exception_specification
19972 = cp_parser_exception_specification_opt (parser
);
19974 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19976 /* In here, we handle cases where attribute is used after
19977 the function declaration. For example:
19978 void func (int x) __attribute__((vector(..))); */
19979 tree gnu_attrs
= NULL_TREE
;
19980 tree requires_clause
= NULL_TREE
;
19981 late_return
= (cp_parser_late_return_type_opt
19982 (parser
, declarator
, requires_clause
,
19983 memfn
? cv_quals
: -1));
19985 /* Parse the virt-specifier-seq. */
19986 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19988 /* Create the function-declarator. */
19989 declarator
= make_call_declarator (declarator
,
19995 exception_specification
,
19998 declarator
->std_attributes
= attrs
;
19999 declarator
->attributes
= gnu_attrs
;
20000 /* Any subsequent parameter lists are to do with
20001 return type, so are not those of the declared
20003 parser
->default_arg_ok_p
= false;
20006 /* Remove the function parms from scope. */
20007 pop_bindings_and_leave_scope ();
20010 /* Repeat the main loop. */
20014 /* If this is the first, we can try a parenthesized
20018 bool saved_in_type_id_in_expr_p
;
20020 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20021 parser
->in_declarator_p
= saved_in_declarator_p
;
20023 open_paren
= token
;
20024 /* Consume the `('. */
20025 matching_parens parens
;
20026 parens
.consume_open (parser
);
20027 /* Parse the nested declarator. */
20028 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20029 parser
->in_type_id_in_expr_p
= true;
20031 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20032 /*parenthesized_p=*/NULL
,
20033 member_p
, friend_p
);
20034 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20036 /* Expect a `)'. */
20037 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20038 if (!parens
.require_close (parser
))
20039 declarator
= cp_error_declarator
;
20040 if (declarator
== cp_error_declarator
)
20043 goto handle_declarator
;
20045 /* Otherwise, we must be done. */
20049 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20050 && token
->type
== CPP_OPEN_SQUARE
20051 && !cp_next_tokens_can_be_attribute_p (parser
))
20053 /* Parse an array-declarator. */
20054 tree bounds
, attrs
;
20056 if (ctor_dtor_or_conv_p
)
20057 *ctor_dtor_or_conv_p
= 0;
20061 parser
->default_arg_ok_p
= false;
20062 parser
->in_declarator_p
= true;
20063 /* Consume the `['. */
20064 cp_lexer_consume_token (parser
->lexer
);
20065 /* Peek at the next token. */
20066 token
= cp_lexer_peek_token (parser
->lexer
);
20067 /* If the next token is `]', then there is no
20068 constant-expression. */
20069 if (token
->type
!= CPP_CLOSE_SQUARE
)
20071 bool non_constant_p
;
20073 = cp_parser_constant_expression (parser
,
20074 /*allow_non_constant=*/true,
20076 if (!non_constant_p
)
20078 else if (error_operand_p (bounds
))
20079 /* Already gave an error. */;
20080 else if (!parser
->in_function_body
20081 || current_binding_level
->kind
== sk_function_parms
)
20083 /* Normally, the array bound must be an integral constant
20084 expression. However, as an extension, we allow VLAs
20085 in function scopes as long as they aren't part of a
20086 parameter declaration. */
20087 cp_parser_error (parser
,
20088 "array bound is not an integer constant");
20089 bounds
= error_mark_node
;
20091 else if (processing_template_decl
20092 && !type_dependent_expression_p (bounds
))
20094 /* Remember this wasn't a constant-expression. */
20095 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20096 TREE_SIDE_EFFECTS (bounds
) = 1;
20100 bounds
= NULL_TREE
;
20101 /* Look for the closing `]'. */
20102 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20104 declarator
= cp_error_declarator
;
20108 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20109 declarator
= make_array_declarator (declarator
, bounds
);
20110 declarator
->std_attributes
= attrs
;
20112 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20115 tree qualifying_scope
;
20116 tree unqualified_name
;
20118 special_function_kind sfk
;
20120 bool pack_expansion_p
= false;
20121 cp_token
*declarator_id_start_token
;
20123 /* Parse a declarator-id */
20124 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20127 cp_parser_parse_tentatively (parser
);
20129 /* If we see an ellipsis, we should be looking at a
20131 if (token
->type
== CPP_ELLIPSIS
)
20133 /* Consume the `...' */
20134 cp_lexer_consume_token (parser
->lexer
);
20136 pack_expansion_p
= true;
20140 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20142 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20143 qualifying_scope
= parser
->scope
;
20148 if (!unqualified_name
&& pack_expansion_p
)
20150 /* Check whether an error occurred. */
20151 okay
= !cp_parser_error_occurred (parser
);
20153 /* We already consumed the ellipsis to mark a
20154 parameter pack, but we have no way to report it,
20155 so abort the tentative parse. We will be exiting
20156 immediately anyway. */
20157 cp_parser_abort_tentative_parse (parser
);
20160 okay
= cp_parser_parse_definitely (parser
);
20163 unqualified_name
= error_mark_node
;
20164 else if (unqualified_name
20165 && (qualifying_scope
20166 || (!identifier_p (unqualified_name
))))
20168 cp_parser_error (parser
, "expected unqualified-id");
20169 unqualified_name
= error_mark_node
;
20173 if (!unqualified_name
)
20175 if (unqualified_name
== error_mark_node
)
20177 declarator
= cp_error_declarator
;
20178 pack_expansion_p
= false;
20179 declarator
->parameter_pack_p
= false;
20183 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20185 if (qualifying_scope
&& at_namespace_scope_p ()
20186 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20188 /* In the declaration of a member of a template class
20189 outside of the class itself, the SCOPE will sometimes
20190 be a TYPENAME_TYPE. For example, given:
20192 template <typename T>
20193 int S<T>::R::i = 3;
20195 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20196 this context, we must resolve S<T>::R to an ordinary
20197 type, rather than a typename type.
20199 The reason we normally avoid resolving TYPENAME_TYPEs
20200 is that a specialization of `S' might render
20201 `S<T>::R' not a type. However, if `S' is
20202 specialized, then this `i' will not be used, so there
20203 is no harm in resolving the types here. */
20206 /* Resolve the TYPENAME_TYPE. */
20207 type
= resolve_typename_type (qualifying_scope
,
20208 /*only_current_p=*/false);
20209 /* If that failed, the declarator is invalid. */
20210 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20212 if (typedef_variant_p (type
))
20213 error_at (declarator_id_start_token
->location
,
20214 "cannot define member of dependent typedef "
20217 error_at (declarator_id_start_token
->location
,
20218 "%<%T::%E%> is not a type",
20219 TYPE_CONTEXT (qualifying_scope
),
20220 TYPE_IDENTIFIER (qualifying_scope
));
20222 qualifying_scope
= type
;
20227 if (unqualified_name
)
20231 if (qualifying_scope
20232 && CLASS_TYPE_P (qualifying_scope
))
20233 class_type
= qualifying_scope
;
20235 class_type
= current_class_type
;
20237 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20239 tree name_type
= TREE_TYPE (unqualified_name
);
20241 if (!class_type
|| !same_type_p (name_type
, class_type
))
20243 /* We do not attempt to print the declarator
20244 here because we do not have enough
20245 information about its original syntactic
20247 cp_parser_error (parser
, "invalid declarator");
20248 declarator
= cp_error_declarator
;
20251 else if (qualifying_scope
20252 && CLASSTYPE_USE_TEMPLATE (name_type
))
20254 error_at (declarator_id_start_token
->location
,
20255 "invalid use of constructor as a template");
20256 inform (declarator_id_start_token
->location
,
20257 "use %<%T::%D%> instead of %<%T::%D%> to "
20258 "name the constructor in a qualified name",
20260 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20261 class_type
, name_type
);
20262 declarator
= cp_error_declarator
;
20265 unqualified_name
= constructor_name (class_type
);
20270 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20271 sfk
= sfk_destructor
;
20272 else if (identifier_p (unqualified_name
)
20273 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20274 sfk
= sfk_conversion
;
20275 else if (/* There's no way to declare a constructor
20276 for an unnamed type, even if the type
20277 got a name for linkage purposes. */
20278 !TYPE_WAS_UNNAMED (class_type
)
20279 /* Handle correctly (c++/19200):
20293 friend void N::S();
20295 && (!friend_p
|| class_type
== qualifying_scope
)
20296 && constructor_name_p (unqualified_name
,
20298 sfk
= sfk_constructor
;
20299 else if (is_overloaded_fn (unqualified_name
)
20300 && DECL_CONSTRUCTOR_P (get_first_fn
20301 (unqualified_name
)))
20302 sfk
= sfk_constructor
;
20304 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20305 *ctor_dtor_or_conv_p
= -1;
20308 declarator
= make_id_declarator (qualifying_scope
,
20311 declarator
->std_attributes
= attrs
;
20312 declarator
->id_loc
= token
->location
;
20313 declarator
->parameter_pack_p
= pack_expansion_p
;
20315 if (pack_expansion_p
)
20316 maybe_warn_variadic_templates ();
20319 handle_declarator
:;
20320 scope
= get_scope_of_declarator (declarator
);
20323 /* Any names that appear after the declarator-id for a
20324 member are looked up in the containing scope. */
20325 if (at_function_scope_p ())
20327 /* But declarations with qualified-ids can't appear in a
20329 cp_parser_error (parser
, "qualified-id in declaration");
20330 declarator
= cp_error_declarator
;
20333 pushed_scope
= push_scope (scope
);
20335 parser
->in_declarator_p
= true;
20336 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20337 || (declarator
&& declarator
->kind
== cdk_id
))
20338 /* Default args are only allowed on function
20340 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20342 parser
->default_arg_ok_p
= false;
20351 /* For an abstract declarator, we might wind up with nothing at this
20352 point. That's an error; the declarator is not optional. */
20354 cp_parser_error (parser
, "expected declarator");
20355 else if (open_paren
)
20357 /* Record overly parenthesized declarator so we can give a
20358 diagnostic about confusing decl/expr disambiguation. */
20359 if (declarator
->kind
== cdk_array
)
20361 /* If the open and close parens are on different lines, this
20362 is probably a formatting thing, so ignore. */
20363 expanded_location open
= expand_location (open_paren
->location
);
20364 expanded_location close
= expand_location (close_paren
->location
);
20365 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20369 declarator
->parenthesized
= open_paren
->location
;
20372 /* If we entered a scope, we must exit it now. */
20374 pop_scope (pushed_scope
);
20376 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20377 parser
->in_declarator_p
= saved_in_declarator_p
;
20382 /* Parse a ptr-operator.
20385 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20386 * cv-qualifier-seq [opt]
20388 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20389 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20394 & cv-qualifier-seq [opt]
20396 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20397 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20398 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20399 filled in with the TYPE containing the member. *CV_QUALS is
20400 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20401 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20402 Note that the tree codes returned by this function have nothing
20403 to do with the types of trees that will be eventually be created
20404 to represent the pointer or reference type being parsed. They are
20405 just constants with suggestive names. */
20406 static enum tree_code
20407 cp_parser_ptr_operator (cp_parser
* parser
,
20409 cp_cv_quals
*cv_quals
,
20412 enum tree_code code
= ERROR_MARK
;
20414 tree attrs
= NULL_TREE
;
20416 /* Assume that it's not a pointer-to-member. */
20418 /* And that there are no cv-qualifiers. */
20419 *cv_quals
= TYPE_UNQUALIFIED
;
20421 /* Peek at the next token. */
20422 token
= cp_lexer_peek_token (parser
->lexer
);
20424 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20425 if (token
->type
== CPP_MULT
)
20426 code
= INDIRECT_REF
;
20427 else if (token
->type
== CPP_AND
)
20429 else if ((cxx_dialect
!= cxx98
) &&
20430 token
->type
== CPP_AND_AND
) /* C++0x only */
20431 code
= NON_LVALUE_EXPR
;
20433 if (code
!= ERROR_MARK
)
20435 /* Consume the `*', `&' or `&&'. */
20436 cp_lexer_consume_token (parser
->lexer
);
20438 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20439 `&', if we are allowing GNU extensions. (The only qualifier
20440 that can legally appear after `&' is `restrict', but that is
20441 enforced during semantic analysis. */
20442 if (code
== INDIRECT_REF
20443 || cp_parser_allow_gnu_extensions_p (parser
))
20444 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20446 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20447 if (attributes
!= NULL
)
20448 *attributes
= attrs
;
20452 /* Try the pointer-to-member case. */
20453 cp_parser_parse_tentatively (parser
);
20454 /* Look for the optional `::' operator. */
20455 cp_parser_global_scope_opt (parser
,
20456 /*current_scope_valid_p=*/false);
20457 /* Look for the nested-name specifier. */
20458 token
= cp_lexer_peek_token (parser
->lexer
);
20459 cp_parser_nested_name_specifier (parser
,
20460 /*typename_keyword_p=*/false,
20461 /*check_dependency_p=*/true,
20463 /*is_declaration=*/false);
20464 /* If we found it, and the next token is a `*', then we are
20465 indeed looking at a pointer-to-member operator. */
20466 if (!cp_parser_error_occurred (parser
)
20467 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20469 /* Indicate that the `*' operator was used. */
20470 code
= INDIRECT_REF
;
20472 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20473 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20474 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20475 error_at (token
->location
, "cannot form pointer to member of "
20476 "non-class %q#T", parser
->scope
);
20479 /* The type of which the member is a member is given by the
20481 *type
= parser
->scope
;
20482 /* The next name will not be qualified. */
20483 parser
->scope
= NULL_TREE
;
20484 parser
->qualifying_scope
= NULL_TREE
;
20485 parser
->object_scope
= NULL_TREE
;
20486 /* Look for optional c++11 attributes. */
20487 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20488 if (attributes
!= NULL
)
20489 *attributes
= attrs
;
20490 /* Look for the optional cv-qualifier-seq. */
20491 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20494 /* If that didn't work we don't have a ptr-operator. */
20495 if (!cp_parser_parse_definitely (parser
))
20496 cp_parser_error (parser
, "expected ptr-operator");
20502 /* Parse an (optional) cv-qualifier-seq.
20505 cv-qualifier cv-qualifier-seq [opt]
20516 Returns a bitmask representing the cv-qualifiers. */
20519 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20521 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20526 cp_cv_quals cv_qualifier
;
20528 /* Peek at the next token. */
20529 token
= cp_lexer_peek_token (parser
->lexer
);
20530 /* See if it's a cv-qualifier. */
20531 switch (token
->keyword
)
20534 cv_qualifier
= TYPE_QUAL_CONST
;
20538 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20542 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20546 cv_qualifier
= TYPE_UNQUALIFIED
;
20553 if (cv_quals
& cv_qualifier
)
20555 gcc_rich_location
richloc (token
->location
);
20556 richloc
.add_fixit_remove ();
20557 error_at (&richloc
, "duplicate cv-qualifier");
20558 cp_lexer_purge_token (parser
->lexer
);
20562 cp_lexer_consume_token (parser
->lexer
);
20563 cv_quals
|= cv_qualifier
;
20570 /* Parse an (optional) ref-qualifier
20576 Returns cp_ref_qualifier representing ref-qualifier. */
20578 static cp_ref_qualifier
20579 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20581 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20583 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20584 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20589 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20592 switch (token
->type
)
20595 curr_ref_qual
= REF_QUAL_LVALUE
;
20599 curr_ref_qual
= REF_QUAL_RVALUE
;
20603 curr_ref_qual
= REF_QUAL_NONE
;
20607 if (!curr_ref_qual
)
20611 error_at (token
->location
, "multiple ref-qualifiers");
20612 cp_lexer_purge_token (parser
->lexer
);
20616 ref_qual
= curr_ref_qual
;
20617 cp_lexer_consume_token (parser
->lexer
);
20624 /* Parse an optional tx-qualifier.
20628 transaction_safe_dynamic */
20631 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20633 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20634 if (token
->type
== CPP_NAME
)
20636 tree name
= token
->u
.value
;
20637 const char *p
= IDENTIFIER_POINTER (name
);
20638 const int len
= strlen ("transaction_safe");
20639 if (!strncmp (p
, "transaction_safe", len
))
20643 || !strcmp (p
, "_dynamic"))
20645 cp_lexer_consume_token (parser
->lexer
);
20648 error ("%qE requires %<-fgnu-tm%>", name
);
20659 /* Parse an (optional) virt-specifier-seq.
20661 virt-specifier-seq:
20662 virt-specifier virt-specifier-seq [opt]
20668 Returns a bitmask representing the virt-specifiers. */
20670 static cp_virt_specifiers
20671 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20673 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20678 cp_virt_specifiers virt_specifier
;
20680 /* Peek at the next token. */
20681 token
= cp_lexer_peek_token (parser
->lexer
);
20682 /* See if it's a virt-specifier-qualifier. */
20683 if (token
->type
!= CPP_NAME
)
20685 if (id_equal (token
->u
.value
, "override"))
20687 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20688 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20690 else if (id_equal (token
->u
.value
, "final"))
20692 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20693 virt_specifier
= VIRT_SPEC_FINAL
;
20695 else if (id_equal (token
->u
.value
, "__final"))
20697 virt_specifier
= VIRT_SPEC_FINAL
;
20702 if (virt_specifiers
& virt_specifier
)
20704 gcc_rich_location
richloc (token
->location
);
20705 richloc
.add_fixit_remove ();
20706 error_at (&richloc
, "duplicate virt-specifier");
20707 cp_lexer_purge_token (parser
->lexer
);
20711 cp_lexer_consume_token (parser
->lexer
);
20712 virt_specifiers
|= virt_specifier
;
20715 return virt_specifiers
;
20718 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20719 is in scope even though it isn't real. */
20722 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20726 if (current_class_ptr
)
20728 /* We don't clear this between NSDMIs. Is it already what we want? */
20729 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20730 if (DECL_P (current_class_ptr
)
20731 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20732 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20733 && cp_type_quals (type
) == quals
)
20737 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20738 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20739 current_class_ptr
= NULL_TREE
;
20741 = cp_build_fold_indirect_ref (this_parm
);
20742 current_class_ptr
= this_parm
;
20745 /* Return true iff our current scope is a non-static data member
20749 parsing_nsdmi (void)
20751 /* We recognize NSDMI context by the context-less 'this' pointer set up
20752 by the function above. */
20753 if (current_class_ptr
20754 && TREE_CODE (current_class_ptr
) == PARM_DECL
20755 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20760 /* Parse a late-specified return type, if any. This is not a separate
20761 non-terminal, but part of a function declarator, which looks like
20763 -> trailing-type-specifier-seq abstract-declarator(opt)
20765 Returns the type indicated by the type-id.
20767 In addition to this, parse any queued up #pragma omp declare simd
20768 clauses, and #pragma acc routine clauses.
20770 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20774 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20775 tree
& requires_clause
, cp_cv_quals quals
)
20778 tree type
= NULL_TREE
;
20779 bool declare_simd_p
= (parser
->omp_declare_simd
20781 && declarator
->kind
== cdk_id
);
20783 bool oacc_routine_p
= (parser
->oacc_routine
20785 && declarator
->kind
== cdk_id
);
20787 /* Peek at the next token. */
20788 token
= cp_lexer_peek_token (parser
->lexer
);
20789 /* A late-specified return type is indicated by an initial '->'. */
20790 if (token
->type
!= CPP_DEREF
20791 && token
->keyword
!= RID_REQUIRES
20792 && !(token
->type
== CPP_NAME
20793 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20794 && !(declare_simd_p
|| oacc_routine_p
))
20797 tree save_ccp
= current_class_ptr
;
20798 tree save_ccr
= current_class_ref
;
20801 /* DR 1207: 'this' is in scope in the trailing return type. */
20802 inject_this_parameter (current_class_type
, quals
);
20805 if (token
->type
== CPP_DEREF
)
20807 /* Consume the ->. */
20808 cp_lexer_consume_token (parser
->lexer
);
20810 type
= cp_parser_trailing_type_id (parser
);
20813 /* Function declarations may be followed by a trailing
20814 requires-clause. */
20815 requires_clause
= cp_parser_requires_clause_opt (parser
);
20817 if (declare_simd_p
)
20818 declarator
->attributes
20819 = cp_parser_late_parsing_omp_declare_simd (parser
,
20820 declarator
->attributes
);
20821 if (oacc_routine_p
)
20822 declarator
->attributes
20823 = cp_parser_late_parsing_oacc_routine (parser
,
20824 declarator
->attributes
);
20828 current_class_ptr
= save_ccp
;
20829 current_class_ref
= save_ccr
;
20835 /* Parse a declarator-id.
20839 :: [opt] nested-name-specifier [opt] type-name
20841 In the `id-expression' case, the value returned is as for
20842 cp_parser_id_expression if the id-expression was an unqualified-id.
20843 If the id-expression was a qualified-id, then a SCOPE_REF is
20844 returned. The first operand is the scope (either a NAMESPACE_DECL
20845 or TREE_TYPE), but the second is still just a representation of an
20849 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20852 /* The expression must be an id-expression. Assume that qualified
20853 names are the names of types so that:
20856 int S<T>::R::i = 3;
20858 will work; we must treat `S<T>::R' as the name of a type.
20859 Similarly, assume that qualified names are templates, where
20863 int S<T>::R<T>::i = 3;
20866 id
= cp_parser_id_expression (parser
,
20867 /*template_keyword_p=*/false,
20868 /*check_dependency_p=*/false,
20869 /*template_p=*/NULL
,
20870 /*declarator_p=*/true,
20872 if (id
&& BASELINK_P (id
))
20873 id
= BASELINK_FUNCTIONS (id
);
20877 /* Parse a type-id.
20880 type-specifier-seq abstract-declarator [opt]
20882 Returns the TYPE specified. */
20885 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20886 bool is_trailing_return
)
20888 cp_decl_specifier_seq type_specifier_seq
;
20889 cp_declarator
*abstract_declarator
;
20891 /* Parse the type-specifier-seq. */
20892 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20893 is_trailing_return
,
20894 &type_specifier_seq
);
20895 if (is_template_arg
&& type_specifier_seq
.type
20896 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20897 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20898 /* A bare template name as a template argument is a template template
20899 argument, not a placeholder, so fail parsing it as a type argument. */
20901 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20902 cp_parser_simulate_error (parser
);
20903 return error_mark_node
;
20905 if (type_specifier_seq
.type
== error_mark_node
)
20906 return error_mark_node
;
20908 /* There might or might not be an abstract declarator. */
20909 cp_parser_parse_tentatively (parser
);
20910 /* Look for the declarator. */
20911 abstract_declarator
20912 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20913 /*parenthesized_p=*/NULL
,
20914 /*member_p=*/false,
20915 /*friend_p=*/false);
20916 /* Check to see if there really was a declarator. */
20917 if (!cp_parser_parse_definitely (parser
))
20918 abstract_declarator
= NULL
;
20920 if (type_specifier_seq
.type
20921 /* The concepts TS allows 'auto' as a type-id. */
20922 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20923 /* None of the valid uses of 'auto' in C++14 involve the type-id
20924 nonterminal, but it is valid in a trailing-return-type. */
20925 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20926 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20928 /* A type-id with type 'auto' is only ok if the abstract declarator
20929 is a function declarator with a late-specified return type.
20931 A type-id with 'auto' is also valid in a trailing-return-type
20932 in a compound-requirement. */
20933 if (abstract_declarator
20934 && abstract_declarator
->kind
== cdk_function
20935 && abstract_declarator
->u
.function
.late_return_type
)
20937 else if (parser
->in_result_type_constraint_p
)
20941 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20942 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20944 error_at (loc
, "missing template arguments after %qT",
20946 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
20950 error_at (loc
, "invalid use of %qT", auto_node
);
20951 return error_mark_node
;
20955 return groktypename (&type_specifier_seq
, abstract_declarator
,
20960 cp_parser_type_id (cp_parser
*parser
)
20962 return cp_parser_type_id_1 (parser
, false, false);
20966 cp_parser_template_type_arg (cp_parser
*parser
)
20969 const char *saved_message
= parser
->type_definition_forbidden_message
;
20970 parser
->type_definition_forbidden_message
20971 = G_("types may not be defined in template arguments");
20972 r
= cp_parser_type_id_1 (parser
, true, false);
20973 parser
->type_definition_forbidden_message
= saved_message
;
20974 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
20976 error ("invalid use of %<auto%> in template argument");
20977 r
= error_mark_node
;
20983 cp_parser_trailing_type_id (cp_parser
*parser
)
20985 return cp_parser_type_id_1 (parser
, false, true);
20988 /* Parse a type-specifier-seq.
20990 type-specifier-seq:
20991 type-specifier type-specifier-seq [opt]
20995 type-specifier-seq:
20996 attributes type-specifier-seq [opt]
20998 If IS_DECLARATION is true, we are at the start of a "condition" or
20999 exception-declaration, so we might be followed by a declarator-id.
21001 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21002 i.e. we've just seen "->".
21004 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21007 cp_parser_type_specifier_seq (cp_parser
* parser
,
21008 bool is_declaration
,
21009 bool is_trailing_return
,
21010 cp_decl_specifier_seq
*type_specifier_seq
)
21012 bool seen_type_specifier
= false;
21013 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21014 cp_token
*start_token
= NULL
;
21016 /* Clear the TYPE_SPECIFIER_SEQ. */
21017 clear_decl_specs (type_specifier_seq
);
21019 /* In the context of a trailing return type, enum E { } is an
21020 elaborated-type-specifier followed by a function-body, not an
21022 if (is_trailing_return
)
21023 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21025 /* Parse the type-specifiers and attributes. */
21028 tree type_specifier
;
21029 bool is_cv_qualifier
;
21031 /* Check for attributes first. */
21032 if (cp_next_tokens_can_be_attribute_p (parser
))
21034 type_specifier_seq
->attributes
=
21035 chainon (type_specifier_seq
->attributes
,
21036 cp_parser_attributes_opt (parser
));
21040 /* record the token of the beginning of the type specifier seq,
21041 for error reporting purposes*/
21043 start_token
= cp_lexer_peek_token (parser
->lexer
);
21045 /* Look for the type-specifier. */
21046 type_specifier
= cp_parser_type_specifier (parser
,
21048 type_specifier_seq
,
21049 /*is_declaration=*/false,
21052 if (!type_specifier
)
21054 /* If the first type-specifier could not be found, this is not a
21055 type-specifier-seq at all. */
21056 if (!seen_type_specifier
)
21058 /* Set in_declarator_p to avoid skipping to the semicolon. */
21059 int in_decl
= parser
->in_declarator_p
;
21060 parser
->in_declarator_p
= true;
21062 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21063 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21064 cp_parser_error (parser
, "expected type-specifier");
21066 parser
->in_declarator_p
= in_decl
;
21068 type_specifier_seq
->type
= error_mark_node
;
21071 /* If subsequent type-specifiers could not be found, the
21072 type-specifier-seq is complete. */
21076 seen_type_specifier
= true;
21077 /* The standard says that a condition can be:
21079 type-specifier-seq declarator = assignment-expression
21086 we should treat the "S" as a declarator, not as a
21087 type-specifier. The standard doesn't say that explicitly for
21088 type-specifier-seq, but it does say that for
21089 decl-specifier-seq in an ordinary declaration. Perhaps it
21090 would be clearer just to allow a decl-specifier-seq here, and
21091 then add a semantic restriction that if any decl-specifiers
21092 that are not type-specifiers appear, the program is invalid. */
21093 if (is_declaration
&& !is_cv_qualifier
)
21094 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21098 /* Return whether the function currently being declared has an associated
21099 template parameter list. */
21102 function_being_declared_is_template_p (cp_parser
* parser
)
21104 if (!current_template_parms
|| processing_template_parmlist
)
21107 if (parser
->implicit_template_scope
)
21110 if (at_class_scope_p ()
21111 && TYPE_BEING_DEFINED (current_class_type
))
21112 return parser
->num_template_parameter_lists
!= 0;
21114 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21115 (current_class_type
));
21118 /* Parse a parameter-declaration-clause.
21120 parameter-declaration-clause:
21121 parameter-declaration-list [opt] ... [opt]
21122 parameter-declaration-list , ...
21124 Returns a representation for the parameter declarations. A return
21125 value of NULL indicates a parameter-declaration-clause consisting
21126 only of an ellipsis. */
21129 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21138 int auto_is_implicit_function_template_parm_p
;
21140 parser
->auto_is_implicit_function_template_parm_p
21141 = auto_is_implicit_function_template_parm_p
;
21143 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
21147 if (!processing_specialization
21148 && !processing_template_parmlist
21149 && !processing_explicit_instantiation
)
21150 if (!current_function_decl
21151 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21152 parser
->auto_is_implicit_function_template_parm_p
= true;
21154 /* Peek at the next token. */
21155 token
= cp_lexer_peek_token (parser
->lexer
);
21156 /* Check for trivial parameter-declaration-clauses. */
21157 if (token
->type
== CPP_ELLIPSIS
)
21159 /* Consume the `...' token. */
21160 cp_lexer_consume_token (parser
->lexer
);
21163 else if (token
->type
== CPP_CLOSE_PAREN
)
21164 /* There are no parameters. */
21166 #ifndef NO_IMPLICIT_EXTERN_C
21167 if (in_system_header_at (input_location
)
21168 && current_class_type
== NULL
21169 && current_lang_name
== lang_name_c
)
21173 return void_list_node
;
21175 /* Check for `(void)', too, which is a special case. */
21176 else if (token
->keyword
== RID_VOID
21177 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21178 == CPP_CLOSE_PAREN
))
21180 /* Consume the `void' token. */
21181 cp_lexer_consume_token (parser
->lexer
);
21182 /* There are no parameters. */
21183 return void_list_node
;
21186 /* Parse the parameter-declaration-list. */
21187 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21188 /* If a parse error occurred while parsing the
21189 parameter-declaration-list, then the entire
21190 parameter-declaration-clause is erroneous. */
21194 /* Peek at the next token. */
21195 token
= cp_lexer_peek_token (parser
->lexer
);
21196 /* If it's a `,', the clause should terminate with an ellipsis. */
21197 if (token
->type
== CPP_COMMA
)
21199 /* Consume the `,'. */
21200 cp_lexer_consume_token (parser
->lexer
);
21201 /* Expect an ellipsis. */
21203 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21205 /* It might also be `...' if the optional trailing `,' was
21207 else if (token
->type
== CPP_ELLIPSIS
)
21209 /* Consume the `...' token. */
21210 cp_lexer_consume_token (parser
->lexer
);
21211 /* And remember that we saw it. */
21215 ellipsis_p
= false;
21217 /* Finish the parameter list. */
21219 parameters
= chainon (parameters
, void_list_node
);
21224 /* Parse a parameter-declaration-list.
21226 parameter-declaration-list:
21227 parameter-declaration
21228 parameter-declaration-list , parameter-declaration
21230 Returns a representation of the parameter-declaration-list, as for
21231 cp_parser_parameter_declaration_clause. However, the
21232 `void_list_node' is never appended to the list. Upon return,
21233 *IS_ERROR will be true iff an error occurred. */
21236 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21238 tree parameters
= NULL_TREE
;
21239 tree
*tail
= ¶meters
;
21240 bool saved_in_unbraced_linkage_specification_p
;
21243 /* Assume all will go well. */
21245 /* The special considerations that apply to a function within an
21246 unbraced linkage specifications do not apply to the parameters
21247 to the function. */
21248 saved_in_unbraced_linkage_specification_p
21249 = parser
->in_unbraced_linkage_specification_p
;
21250 parser
->in_unbraced_linkage_specification_p
= false;
21252 /* Look for more parameters. */
21255 cp_parameter_declarator
*parameter
;
21256 tree decl
= error_mark_node
;
21257 bool parenthesized_p
= false;
21258 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21259 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21260 (current_template_parms
)) : 0);
21262 /* Parse the parameter. */
21264 = cp_parser_parameter_declaration (parser
,
21265 /*template_parm_p=*/false,
21268 /* We don't know yet if the enclosing context is deprecated, so wait
21269 and warn in grokparms if appropriate. */
21270 deprecated_state
= DEPRECATED_SUPPRESS
;
21274 /* If a function parameter pack was specified and an implicit template
21275 parameter was introduced during cp_parser_parameter_declaration,
21276 change any implicit parameters introduced into packs. */
21277 if (parser
->implicit_template_parms
21278 && parameter
->declarator
21279 && parameter
->declarator
->parameter_pack_p
)
21281 int latest_template_parm_idx
= TREE_VEC_LENGTH
21282 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21284 if (latest_template_parm_idx
!= template_parm_idx
)
21285 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21286 (parameter
->decl_specifiers
.type
,
21287 template_parm_idx
, latest_template_parm_idx
);
21290 decl
= grokdeclarator (parameter
->declarator
,
21291 ¶meter
->decl_specifiers
,
21293 parameter
->default_argument
!= NULL_TREE
,
21294 ¶meter
->decl_specifiers
.attributes
);
21295 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21296 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21299 deprecated_state
= DEPRECATED_NORMAL
;
21301 /* If a parse error occurred parsing the parameter declaration,
21302 then the entire parameter-declaration-list is erroneous. */
21303 if (decl
== error_mark_node
)
21306 parameters
= error_mark_node
;
21310 if (parameter
->decl_specifiers
.attributes
)
21311 cplus_decl_attributes (&decl
,
21312 parameter
->decl_specifiers
.attributes
,
21314 if (DECL_NAME (decl
))
21315 decl
= pushdecl (decl
);
21317 if (decl
!= error_mark_node
)
21319 retrofit_lang_decl (decl
);
21320 DECL_PARM_INDEX (decl
) = ++index
;
21321 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21324 /* Add the new parameter to the list. */
21325 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21326 tail
= &TREE_CHAIN (*tail
);
21328 /* Peek at the next token. */
21329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21330 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21331 /* These are for Objective-C++ */
21332 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21333 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21334 /* The parameter-declaration-list is complete. */
21336 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21340 /* Peek at the next token. */
21341 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21342 /* If it's an ellipsis, then the list is complete. */
21343 if (token
->type
== CPP_ELLIPSIS
)
21345 /* Otherwise, there must be more parameters. Consume the
21347 cp_lexer_consume_token (parser
->lexer
);
21348 /* When parsing something like:
21350 int i(float f, double d)
21352 we can tell after seeing the declaration for "f" that we
21353 are not looking at an initialization of a variable "i",
21354 but rather at the declaration of a function "i".
21356 Due to the fact that the parsing of template arguments
21357 (as specified to a template-id) requires backtracking we
21358 cannot use this technique when inside a template argument
21360 if (!parser
->in_template_argument_list_p
21361 && !parser
->in_type_id_in_expr_p
21362 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21363 /* However, a parameter-declaration of the form
21364 "float(f)" (which is a valid declaration of a
21365 parameter "f") can also be interpreted as an
21366 expression (the conversion of "f" to "float"). */
21367 && !parenthesized_p
)
21368 cp_parser_commit_to_tentative_parse (parser
);
21372 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21373 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21374 cp_parser_skip_to_closing_parenthesis (parser
,
21375 /*recovering=*/true,
21376 /*or_comma=*/false,
21377 /*consume_paren=*/false);
21382 parser
->in_unbraced_linkage_specification_p
21383 = saved_in_unbraced_linkage_specification_p
;
21385 /* Reset implicit_template_scope if we are about to leave the function
21386 parameter list that introduced it. Note that for out-of-line member
21387 definitions, there will be one or more class scopes before we get to
21388 the template parameter scope. */
21390 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21391 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21393 while (maybe_its
->kind
== sk_class
)
21394 maybe_its
= maybe_its
->level_chain
;
21395 if (maybe_its
== its
)
21397 parser
->implicit_template_parms
= 0;
21398 parser
->implicit_template_scope
= 0;
21405 /* Parse a parameter declaration.
21407 parameter-declaration:
21408 decl-specifier-seq ... [opt] declarator
21409 decl-specifier-seq declarator = assignment-expression
21410 decl-specifier-seq ... [opt] abstract-declarator [opt]
21411 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21413 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21414 declares a template parameter. (In that case, a non-nested `>'
21415 token encountered during the parsing of the assignment-expression
21416 is not interpreted as a greater-than operator.)
21418 Returns a representation of the parameter, or NULL if an error
21419 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21420 true iff the declarator is of the form "(p)". */
21422 static cp_parameter_declarator
*
21423 cp_parser_parameter_declaration (cp_parser
*parser
,
21424 bool template_parm_p
,
21425 bool *parenthesized_p
)
21427 int declares_class_or_enum
;
21428 cp_decl_specifier_seq decl_specifiers
;
21429 cp_declarator
*declarator
;
21430 tree default_argument
;
21431 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21432 const char *saved_message
;
21433 bool template_parameter_pack_p
= false;
21435 /* In a template parameter, `>' is not an operator.
21439 When parsing a default template-argument for a non-type
21440 template-parameter, the first non-nested `>' is taken as the end
21441 of the template parameter-list rather than a greater-than
21444 /* Type definitions may not appear in parameter types. */
21445 saved_message
= parser
->type_definition_forbidden_message
;
21446 parser
->type_definition_forbidden_message
21447 = G_("types may not be defined in parameter types");
21449 /* Parse the declaration-specifiers. */
21450 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21451 cp_parser_decl_specifier_seq (parser
,
21452 CP_PARSER_FLAGS_NONE
,
21454 &declares_class_or_enum
);
21456 /* Complain about missing 'typename' or other invalid type names. */
21457 if (!decl_specifiers
.any_type_specifiers_p
21458 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21459 decl_specifiers
.type
= error_mark_node
;
21461 /* If an error occurred, there's no reason to attempt to parse the
21462 rest of the declaration. */
21463 if (cp_parser_error_occurred (parser
))
21465 parser
->type_definition_forbidden_message
= saved_message
;
21469 /* Peek at the next token. */
21470 token
= cp_lexer_peek_token (parser
->lexer
);
21472 /* If the next token is a `)', `,', `=', `>', or `...', then there
21473 is no declarator. However, when variadic templates are enabled,
21474 there may be a declarator following `...'. */
21475 if (token
->type
== CPP_CLOSE_PAREN
21476 || token
->type
== CPP_COMMA
21477 || token
->type
== CPP_EQ
21478 || token
->type
== CPP_GREATER
)
21481 if (parenthesized_p
)
21482 *parenthesized_p
= false;
21484 /* Otherwise, there should be a declarator. */
21487 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21488 parser
->default_arg_ok_p
= false;
21490 /* After seeing a decl-specifier-seq, if the next token is not a
21491 "(", there is no possibility that the code is a valid
21492 expression. Therefore, if parsing tentatively, we commit at
21494 if (!parser
->in_template_argument_list_p
21495 /* In an expression context, having seen:
21499 we cannot be sure whether we are looking at a
21500 function-type (taking a "char" as a parameter) or a cast
21501 of some object of type "char" to "int". */
21502 && !parser
->in_type_id_in_expr_p
21503 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21504 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21505 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21506 cp_parser_commit_to_tentative_parse (parser
);
21507 /* Parse the declarator. */
21508 declarator_token_start
= token
;
21509 declarator
= cp_parser_declarator (parser
,
21510 CP_PARSER_DECLARATOR_EITHER
,
21511 /*ctor_dtor_or_conv_p=*/NULL
,
21513 /*member_p=*/false,
21514 /*friend_p=*/false);
21515 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21516 /* After the declarator, allow more attributes. */
21517 decl_specifiers
.attributes
21518 = chainon (decl_specifiers
.attributes
,
21519 cp_parser_attributes_opt (parser
));
21521 /* If the declarator is a template parameter pack, remember that and
21522 clear the flag in the declarator itself so we don't get errors
21523 from grokdeclarator. */
21524 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21526 declarator
->parameter_pack_p
= false;
21527 template_parameter_pack_p
= true;
21531 /* If the next token is an ellipsis, and we have not seen a declarator
21532 name, and if either the type of the declarator contains parameter
21533 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21534 for, eg, abbreviated integral type names), then we actually have a
21535 parameter pack expansion expression. Otherwise, leave the ellipsis
21536 for a C-style variadic function. */
21537 token
= cp_lexer_peek_token (parser
->lexer
);
21538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21540 tree type
= decl_specifiers
.type
;
21542 if (type
&& DECL_P (type
))
21543 type
= TREE_TYPE (type
);
21546 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21547 && (template_parm_p
|| uses_parameter_packs (type
)))
21548 || (!type
&& template_parm_p
))
21549 && declarator_can_be_parameter_pack (declarator
))
21551 /* Consume the `...'. */
21552 cp_lexer_consume_token (parser
->lexer
);
21553 maybe_warn_variadic_templates ();
21555 /* Build a pack expansion type */
21556 if (template_parm_p
)
21557 template_parameter_pack_p
= true;
21558 else if (declarator
)
21559 declarator
->parameter_pack_p
= true;
21561 decl_specifiers
.type
= make_pack_expansion (type
);
21565 /* The restriction on defining new types applies only to the type
21566 of the parameter, not to the default argument. */
21567 parser
->type_definition_forbidden_message
= saved_message
;
21569 /* If the next token is `=', then process a default argument. */
21570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21572 tree type
= decl_specifiers
.type
;
21573 token
= cp_lexer_peek_token (parser
->lexer
);
21574 /* If we are defining a class, then the tokens that make up the
21575 default argument must be saved and processed later. */
21576 if (!template_parm_p
&& at_class_scope_p ()
21577 && TYPE_BEING_DEFINED (current_class_type
)
21578 && !LAMBDA_TYPE_P (current_class_type
))
21579 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21581 // A constrained-type-specifier may declare a type template-parameter.
21582 else if (declares_constrained_type_template_parameter (type
))
21584 = cp_parser_default_type_template_argument (parser
);
21586 // A constrained-type-specifier may declare a template-template-parameter.
21587 else if (declares_constrained_template_template_parameter (type
))
21589 = cp_parser_default_template_template_argument (parser
);
21591 /* Outside of a class definition, we can just parse the
21592 assignment-expression. */
21595 = cp_parser_default_argument (parser
, template_parm_p
);
21597 if (!parser
->default_arg_ok_p
)
21599 permerror (token
->location
,
21600 "default arguments are only "
21601 "permitted for function parameters");
21603 else if ((declarator
&& declarator
->parameter_pack_p
)
21604 || template_parameter_pack_p
21605 || (decl_specifiers
.type
21606 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21608 /* Find the name of the parameter pack. */
21609 cp_declarator
*id_declarator
= declarator
;
21610 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21611 id_declarator
= id_declarator
->declarator
;
21613 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21614 error_at (declarator_token_start
->location
,
21616 ? G_("template parameter pack %qD "
21617 "cannot have a default argument")
21618 : G_("parameter pack %qD cannot have "
21619 "a default argument"),
21620 id_declarator
->u
.id
.unqualified_name
);
21622 error_at (declarator_token_start
->location
,
21624 ? G_("template parameter pack cannot have "
21625 "a default argument")
21626 : G_("parameter pack cannot have a "
21627 "default argument"));
21629 default_argument
= NULL_TREE
;
21633 default_argument
= NULL_TREE
;
21635 /* Generate a location for the parameter, ranging from the start of the
21636 initial token to the end of the final token (using input_location for
21637 the latter, set up by cp_lexer_set_source_position_from_token when
21640 If we have a identifier, then use it for the caret location, e.g.
21642 extern int callee (int one, int (*two)(int, int), float three);
21643 ~~~~~~^~~~~~~~~~~~~~
21645 otherwise, reuse the start location for the caret location e.g.:
21647 extern int callee (int one, int (*)(int, int), float three);
21651 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21652 ? declarator
->id_loc
21653 : decl_spec_token_start
->location
);
21654 location_t param_loc
= make_location (caret_loc
,
21655 decl_spec_token_start
->location
,
21658 return make_parameter_declarator (&decl_specifiers
,
21662 template_parameter_pack_p
);
21665 /* Parse a default argument and return it.
21667 TEMPLATE_PARM_P is true if this is a default argument for a
21668 non-type template parameter. */
21670 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21672 tree default_argument
= NULL_TREE
;
21673 bool saved_greater_than_is_operator_p
;
21674 bool saved_local_variables_forbidden_p
;
21675 bool non_constant_p
, is_direct_init
;
21677 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21679 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21680 parser
->greater_than_is_operator_p
= !template_parm_p
;
21681 /* Local variable names (and the `this' keyword) may not
21682 appear in a default argument. */
21683 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21684 parser
->local_variables_forbidden_p
= true;
21685 /* Parse the assignment-expression. */
21686 if (template_parm_p
)
21687 push_deferring_access_checks (dk_no_deferred
);
21688 tree saved_class_ptr
= NULL_TREE
;
21689 tree saved_class_ref
= NULL_TREE
;
21690 /* The "this" pointer is not valid in a default argument. */
21693 saved_class_ptr
= current_class_ptr
;
21694 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21695 saved_class_ref
= current_class_ref
;
21696 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21699 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21700 /* Restore the "this" pointer. */
21703 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21704 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21706 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21707 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21708 if (template_parm_p
)
21709 pop_deferring_access_checks ();
21710 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21711 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21713 return default_argument
;
21716 /* Parse a function-body.
21719 compound_statement */
21722 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21724 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21725 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21729 /* Parse a ctor-initializer-opt followed by a function-body. Return
21730 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21731 is true we are parsing a function-try-block. */
21734 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21735 bool in_function_try_block
)
21738 const bool check_body_p
=
21739 DECL_CONSTRUCTOR_P (current_function_decl
)
21740 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21743 /* Begin the function body. */
21744 body
= begin_function_body ();
21745 /* Parse the optional ctor-initializer. */
21746 cp_parser_ctor_initializer_opt (parser
);
21748 /* If we're parsing a constexpr constructor definition, we need
21749 to check that the constructor body is indeed empty. However,
21750 before we get to cp_parser_function_body lot of junk has been
21751 generated, so we can't just check that we have an empty block.
21752 Rather we take a snapshot of the outermost block, and check whether
21753 cp_parser_function_body changed its state. */
21756 list
= cur_stmt_list
;
21757 if (STATEMENT_LIST_TAIL (list
))
21758 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21760 /* Parse the function-body. */
21761 cp_parser_function_body (parser
, in_function_try_block
);
21763 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21764 /* Finish the function body. */
21765 finish_function_body (body
);
21768 /* Parse an initializer.
21771 = initializer-clause
21772 ( expression-list )
21774 Returns an expression representing the initializer. If no
21775 initializer is present, NULL_TREE is returned.
21777 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21778 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21779 set to TRUE if there is no initializer present. If there is an
21780 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21781 is set to true; otherwise it is set to false. */
21784 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21785 bool* non_constant_p
)
21790 /* Peek at the next token. */
21791 token
= cp_lexer_peek_token (parser
->lexer
);
21793 /* Let our caller know whether or not this initializer was
21795 *is_direct_init
= (token
->type
!= CPP_EQ
);
21796 /* Assume that the initializer is constant. */
21797 *non_constant_p
= false;
21799 if (token
->type
== CPP_EQ
)
21801 /* Consume the `='. */
21802 cp_lexer_consume_token (parser
->lexer
);
21803 /* Parse the initializer-clause. */
21804 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21806 else if (token
->type
== CPP_OPEN_PAREN
)
21808 vec
<tree
, va_gc
> *vec
;
21809 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21811 /*allow_expansion_p=*/true,
21814 return error_mark_node
;
21815 init
= build_tree_list_vec (vec
);
21816 release_tree_vector (vec
);
21818 else if (token
->type
== CPP_OPEN_BRACE
)
21820 cp_lexer_set_source_position (parser
->lexer
);
21821 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21822 init
= cp_parser_braced_list (parser
, non_constant_p
);
21823 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21827 /* Anything else is an error. */
21828 cp_parser_error (parser
, "expected initializer");
21829 init
= error_mark_node
;
21832 if (check_for_bare_parameter_packs (init
))
21833 init
= error_mark_node
;
21838 /* Parse an initializer-clause.
21840 initializer-clause:
21841 assignment-expression
21844 Returns an expression representing the initializer.
21846 If the `assignment-expression' production is used the value
21847 returned is simply a representation for the expression.
21849 Otherwise, calls cp_parser_braced_list. */
21852 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21854 cp_expr initializer
;
21856 /* Assume the expression is constant. */
21857 *non_constant_p
= false;
21859 /* If it is not a `{', then we are looking at an
21860 assignment-expression. */
21861 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21864 = cp_parser_constant_expression (parser
,
21865 /*allow_non_constant_p=*/true,
21869 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21871 return initializer
;
21874 /* Parse a brace-enclosed initializer list.
21877 { initializer-list , [opt] }
21878 { designated-initializer-list , [opt] }
21881 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21882 the elements of the initializer-list (or NULL, if the last
21883 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21884 NULL_TREE. There is no way to detect whether or not the optional
21885 trailing `,' was provided. NON_CONSTANT_P is as for
21886 cp_parser_initializer. */
21889 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21892 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21894 /* Consume the `{' token. */
21895 matching_braces braces
;
21896 braces
.consume_open (parser
);
21897 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21898 initializer
= make_node (CONSTRUCTOR
);
21899 /* If it's not a `}', then there is a non-trivial initializer. */
21900 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21902 /* Parse the initializer list. */
21903 CONSTRUCTOR_ELTS (initializer
)
21904 = cp_parser_initializer_list (parser
, non_constant_p
);
21905 /* A trailing `,' token is allowed. */
21906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21907 cp_lexer_consume_token (parser
->lexer
);
21910 *non_constant_p
= false;
21911 /* Now, there should be a trailing `}'. */
21912 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21913 braces
.require_close (parser
);
21914 TREE_TYPE (initializer
) = init_list_type_node
;
21916 cp_expr
result (initializer
);
21917 /* Build a location of the form:
21920 with caret==start at the open brace, finish at the close brace. */
21921 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21922 result
.set_location (combined_loc
);
21926 /* Consume tokens up to, and including, the next non-nested closing `]'.
21927 Returns true iff we found a closing `]'. */
21930 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21932 unsigned square_depth
= 0;
21936 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21938 switch (token
->type
)
21941 case CPP_PRAGMA_EOL
:
21942 /* If we've run out of tokens, then there is no closing `]'. */
21945 case CPP_OPEN_SQUARE
:
21949 case CPP_CLOSE_SQUARE
:
21950 if (!square_depth
--)
21952 cp_lexer_consume_token (parser
->lexer
);
21961 /* Consume the token. */
21962 cp_lexer_consume_token (parser
->lexer
);
21966 /* Return true if we are looking at an array-designator, false otherwise. */
21969 cp_parser_array_designator_p (cp_parser
*parser
)
21971 /* Consume the `['. */
21972 cp_lexer_consume_token (parser
->lexer
);
21974 cp_lexer_save_tokens (parser
->lexer
);
21976 /* Skip tokens until the next token is a closing square bracket.
21977 If we find the closing `]', and the next token is a `=', then
21978 we are looking at an array designator. */
21979 bool array_designator_p
21980 = (cp_parser_skip_to_closing_square_bracket (parser
)
21981 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
21983 /* Roll back the tokens we skipped. */
21984 cp_lexer_rollback_tokens (parser
->lexer
);
21986 return array_designator_p
;
21989 /* Parse an initializer-list.
21992 initializer-clause ... [opt]
21993 initializer-list , initializer-clause ... [opt]
21997 designated-initializer-list:
21998 designated-initializer-clause
21999 designated-initializer-list , designated-initializer-clause
22001 designated-initializer-clause:
22002 designator brace-or-equal-initializer
22010 designation initializer-clause ...[opt]
22011 initializer-list , designation initializer-clause ...[opt]
22016 [ constant-expression ] =
22018 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22019 for the initializer. If the INDEX of the elt is non-NULL, it is the
22020 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22021 as for cp_parser_initializer. */
22023 static vec
<constructor_elt
, va_gc
> *
22024 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22026 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22027 bool first_p
= true;
22028 tree first_designator
= NULL_TREE
;
22030 /* Assume all of the expressions are constant. */
22031 *non_constant_p
= false;
22033 /* Parse the rest of the list. */
22039 bool clause_non_constant_p
;
22040 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22042 /* Handle the C++2A syntax, '. id ='. */
22043 if ((cxx_dialect
>= cxx2a
22044 || cp_parser_allow_gnu_extensions_p (parser
))
22045 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22046 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22047 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22048 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22049 == CPP_OPEN_BRACE
)))
22051 if (cxx_dialect
< cxx2a
)
22052 pedwarn (loc
, OPT_Wpedantic
,
22053 "C++ designated initializers only available with "
22054 "-std=c++2a or -std=gnu++2a");
22055 /* Consume the `.'. */
22056 cp_lexer_consume_token (parser
->lexer
);
22057 /* Consume the identifier. */
22058 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22060 /* Consume the `='. */
22061 cp_lexer_consume_token (parser
->lexer
);
22063 /* Also, if the next token is an identifier and the following one is a
22064 colon, we are looking at the GNU designated-initializer
22066 else if (cp_parser_allow_gnu_extensions_p (parser
)
22067 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22068 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22071 /* Warn the user that they are using an extension. */
22072 pedwarn (loc
, OPT_Wpedantic
,
22073 "ISO C++ does not allow GNU designated initializers");
22074 /* Consume the identifier. */
22075 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22076 /* Consume the `:'. */
22077 cp_lexer_consume_token (parser
->lexer
);
22079 /* Also handle C99 array designators, '[ const ] ='. */
22080 else if (cp_parser_allow_gnu_extensions_p (parser
)
22081 && !c_dialect_objc ()
22082 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22084 /* In C++11, [ could start a lambda-introducer. */
22085 bool non_const
= false;
22087 cp_parser_parse_tentatively (parser
);
22089 if (!cp_parser_array_designator_p (parser
))
22091 cp_parser_simulate_error (parser
);
22092 designator
= NULL_TREE
;
22096 designator
= cp_parser_constant_expression (parser
, true,
22098 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22099 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22102 if (!cp_parser_parse_definitely (parser
))
22103 designator
= NULL_TREE
;
22104 else if (non_const
)
22105 require_potential_rvalue_constant_expression (designator
);
22107 /* Warn the user that they are using an extension. */
22108 pedwarn (loc
, OPT_Wpedantic
,
22109 "ISO C++ does not allow C99 designated initializers");
22112 designator
= NULL_TREE
;
22116 first_designator
= designator
;
22119 else if (cxx_dialect
>= cxx2a
22120 && first_designator
!= error_mark_node
22121 && (!first_designator
!= !designator
))
22123 error_at (loc
, "either all initializer clauses should be designated "
22124 "or none of them should be");
22125 first_designator
= error_mark_node
;
22127 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22128 first_designator
= designator
;
22130 /* Parse the initializer. */
22131 initializer
= cp_parser_initializer_clause (parser
,
22132 &clause_non_constant_p
);
22133 /* If any clause is non-constant, so is the entire initializer. */
22134 if (clause_non_constant_p
)
22135 *non_constant_p
= true;
22137 /* If we have an ellipsis, this is an initializer pack
22139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22141 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22143 /* Consume the `...'. */
22144 cp_lexer_consume_token (parser
->lexer
);
22146 if (designator
&& cxx_dialect
>= cxx2a
)
22148 "%<...%> not allowed in designated initializer list");
22150 /* Turn the initializer into an initializer expansion. */
22151 initializer
= make_pack_expansion (initializer
);
22154 /* Add it to the vector. */
22155 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22157 /* If the next token is not a comma, we have reached the end of
22159 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22162 /* Peek at the next token. */
22163 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22164 /* If the next token is a `}', then we're still done. An
22165 initializer-clause can have a trailing `,' after the
22166 initializer-list and before the closing `}'. */
22167 if (token
->type
== CPP_CLOSE_BRACE
)
22170 /* Consume the `,' token. */
22171 cp_lexer_consume_token (parser
->lexer
);
22174 /* The same identifier shall not appear in multiple designators
22175 of a designated-initializer-list. */
22176 if (first_designator
)
22179 tree designator
, val
;
22180 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22181 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22183 if (IDENTIFIER_MARKED (designator
))
22185 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22186 "%<.%s%> designator used multiple times in "
22187 "the same initializer list",
22188 IDENTIFIER_POINTER (designator
));
22189 (*v
)[i
].index
= NULL_TREE
;
22192 IDENTIFIER_MARKED (designator
) = 1;
22194 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22195 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22196 IDENTIFIER_MARKED (designator
) = 0;
22202 /* Classes [gram.class] */
22204 /* Parse a class-name.
22210 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22211 to indicate that names looked up in dependent types should be
22212 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22213 keyword has been used to indicate that the name that appears next
22214 is a template. TAG_TYPE indicates the explicit tag given before
22215 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22216 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22217 is the class being defined in a class-head. If ENUM_OK is TRUE,
22218 enum-names are also accepted.
22220 Returns the TYPE_DECL representing the class. */
22223 cp_parser_class_name (cp_parser
*parser
,
22224 bool typename_keyword_p
,
22225 bool template_keyword_p
,
22226 enum tag_types tag_type
,
22227 bool check_dependency_p
,
22229 bool is_declaration
,
22236 tree identifier
= NULL_TREE
;
22238 /* All class-names start with an identifier. */
22239 token
= cp_lexer_peek_token (parser
->lexer
);
22240 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22242 cp_parser_error (parser
, "expected class-name");
22243 return error_mark_node
;
22246 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22247 to a template-id, so we save it here. */
22248 scope
= parser
->scope
;
22249 if (scope
== error_mark_node
)
22250 return error_mark_node
;
22252 /* Any name names a type if we're following the `typename' keyword
22253 in a qualified name where the enclosing scope is type-dependent. */
22254 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22255 && dependent_type_p (scope
));
22256 /* Handle the common case (an identifier, but not a template-id)
22258 if (token
->type
== CPP_NAME
22259 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22261 cp_token
*identifier_token
;
22264 /* Look for the identifier. */
22265 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22266 ambiguous_p
= identifier_token
->error_reported
;
22267 identifier
= cp_parser_identifier (parser
);
22268 /* If the next token isn't an identifier, we are certainly not
22269 looking at a class-name. */
22270 if (identifier
== error_mark_node
)
22271 decl
= error_mark_node
;
22272 /* If we know this is a type-name, there's no need to look it
22274 else if (typename_p
)
22278 tree ambiguous_decls
;
22279 /* If we already know that this lookup is ambiguous, then
22280 we've already issued an error message; there's no reason
22284 cp_parser_simulate_error (parser
);
22285 return error_mark_node
;
22287 /* If the next token is a `::', then the name must be a type
22290 [basic.lookup.qual]
22292 During the lookup for a name preceding the :: scope
22293 resolution operator, object, function, and enumerator
22294 names are ignored. */
22295 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22296 tag_type
= scope_type
;
22297 /* Look up the name. */
22298 decl
= cp_parser_lookup_name (parser
, identifier
,
22300 /*is_template=*/false,
22301 /*is_namespace=*/false,
22302 check_dependency_p
,
22304 identifier_token
->location
);
22305 if (ambiguous_decls
)
22307 if (cp_parser_parsing_tentatively (parser
))
22308 cp_parser_simulate_error (parser
);
22309 return error_mark_node
;
22315 /* Try a template-id. */
22316 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22317 check_dependency_p
,
22320 if (decl
== error_mark_node
)
22321 return error_mark_node
;
22324 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22326 /* If this is a typename, create a TYPENAME_TYPE. */
22327 if (typename_p
&& decl
!= error_mark_node
)
22329 decl
= make_typename_type (scope
, decl
, typename_type
,
22330 /*complain=*/tf_error
);
22331 if (decl
!= error_mark_node
)
22332 decl
= TYPE_NAME (decl
);
22335 decl
= strip_using_decl (decl
);
22337 /* Check to see that it is really the name of a class. */
22338 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22339 && identifier_p (TREE_OPERAND (decl
, 0))
22340 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22341 /* Situations like this:
22343 template <typename T> struct A {
22344 typename T::template X<int>::I i;
22347 are problematic. Is `T::template X<int>' a class-name? The
22348 standard does not seem to be definitive, but there is no other
22349 valid interpretation of the following `::'. Therefore, those
22350 names are considered class-names. */
22352 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22353 if (decl
!= error_mark_node
)
22354 decl
= TYPE_NAME (decl
);
22356 else if (TREE_CODE (decl
) != TYPE_DECL
22357 || TREE_TYPE (decl
) == error_mark_node
22358 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22359 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22360 /* In Objective-C 2.0, a classname followed by '.' starts a
22361 dot-syntax expression, and it's not a type-name. */
22362 || (c_dialect_objc ()
22363 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22364 && objc_is_class_name (decl
)))
22365 decl
= error_mark_node
;
22367 if (decl
== error_mark_node
)
22368 cp_parser_error (parser
, "expected class-name");
22369 else if (identifier
&& !parser
->scope
)
22370 maybe_note_name_used_in_class (identifier
, decl
);
22375 /* Parse a class-specifier.
22378 class-head { member-specification [opt] }
22380 Returns the TREE_TYPE representing the class. */
22383 cp_parser_class_specifier_1 (cp_parser
* parser
)
22386 tree attributes
= NULL_TREE
;
22387 bool nested_name_specifier_p
;
22388 unsigned saved_num_template_parameter_lists
;
22389 bool saved_in_function_body
;
22390 unsigned char in_statement
;
22391 bool in_switch_statement_p
;
22392 bool saved_in_unbraced_linkage_specification_p
;
22393 tree old_scope
= NULL_TREE
;
22394 tree scope
= NULL_TREE
;
22395 cp_token
*closing_brace
;
22397 push_deferring_access_checks (dk_no_deferred
);
22399 /* Parse the class-head. */
22400 type
= cp_parser_class_head (parser
,
22401 &nested_name_specifier_p
);
22402 /* If the class-head was a semantic disaster, skip the entire body
22406 cp_parser_skip_to_end_of_block_or_statement (parser
);
22407 pop_deferring_access_checks ();
22408 return error_mark_node
;
22411 /* Look for the `{'. */
22412 matching_braces braces
;
22413 if (!braces
.require_open (parser
))
22415 pop_deferring_access_checks ();
22416 return error_mark_node
;
22419 cp_ensure_no_omp_declare_simd (parser
);
22420 cp_ensure_no_oacc_routine (parser
);
22422 /* Issue an error message if type-definitions are forbidden here. */
22423 cp_parser_check_type_definition (parser
);
22424 /* Remember that we are defining one more class. */
22425 ++parser
->num_classes_being_defined
;
22426 /* Inside the class, surrounding template-parameter-lists do not
22428 saved_num_template_parameter_lists
22429 = parser
->num_template_parameter_lists
;
22430 parser
->num_template_parameter_lists
= 0;
22431 /* We are not in a function body. */
22432 saved_in_function_body
= parser
->in_function_body
;
22433 parser
->in_function_body
= false;
22434 /* Or in a loop. */
22435 in_statement
= parser
->in_statement
;
22436 parser
->in_statement
= 0;
22437 /* Or in a switch. */
22438 in_switch_statement_p
= parser
->in_switch_statement_p
;
22439 parser
->in_switch_statement_p
= false;
22440 /* We are not immediately inside an extern "lang" block. */
22441 saved_in_unbraced_linkage_specification_p
22442 = parser
->in_unbraced_linkage_specification_p
;
22443 parser
->in_unbraced_linkage_specification_p
= false;
22445 // Associate constraints with the type.
22447 type
= associate_classtype_constraints (type
);
22449 /* Start the class. */
22450 if (nested_name_specifier_p
)
22452 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22453 old_scope
= push_inner_scope (scope
);
22455 type
= begin_class_definition (type
);
22457 if (type
== error_mark_node
)
22458 /* If the type is erroneous, skip the entire body of the class. */
22459 cp_parser_skip_to_closing_brace (parser
);
22461 /* Parse the member-specification. */
22462 cp_parser_member_specification_opt (parser
);
22464 /* Look for the trailing `}'. */
22465 closing_brace
= braces
.require_close (parser
);
22466 /* Look for trailing attributes to apply to this class. */
22467 if (cp_parser_allow_gnu_extensions_p (parser
))
22468 attributes
= cp_parser_gnu_attributes_opt (parser
);
22469 if (type
!= error_mark_node
)
22470 type
= finish_struct (type
, attributes
);
22471 if (nested_name_specifier_p
)
22472 pop_inner_scope (old_scope
, scope
);
22474 /* We've finished a type definition. Check for the common syntax
22475 error of forgetting a semicolon after the definition. We need to
22476 be careful, as we can't just check for not-a-semicolon and be done
22477 with it; the user might have typed:
22479 class X { } c = ...;
22480 class X { } *p = ...;
22482 and so forth. Instead, enumerate all the possible tokens that
22483 might follow this production; if we don't see one of them, then
22484 complain and silently insert the semicolon. */
22486 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22487 bool want_semicolon
= true;
22489 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22490 /* Don't try to parse c++11 attributes here. As per the
22491 grammar, that should be a task for
22492 cp_parser_decl_specifier_seq. */
22493 want_semicolon
= false;
22495 switch (token
->type
)
22498 case CPP_SEMICOLON
:
22501 case CPP_OPEN_PAREN
:
22502 case CPP_CLOSE_PAREN
:
22504 want_semicolon
= false;
22507 /* While it's legal for type qualifiers and storage class
22508 specifiers to follow type definitions in the grammar, only
22509 compiler testsuites contain code like that. Assume that if
22510 we see such code, then what we're really seeing is a case
22514 const <type> var = ...;
22519 static <type> func (...) ...
22521 i.e. the qualifier or specifier applies to the next
22522 declaration. To do so, however, we need to look ahead one
22523 more token to see if *that* token is a type specifier.
22525 This code could be improved to handle:
22528 static const <type> var = ...; */
22530 if (keyword_is_decl_specifier (token
->keyword
))
22532 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22534 /* Handling user-defined types here would be nice, but very
22537 = (lookahead
->type
== CPP_KEYWORD
22538 && keyword_begins_type_specifier (lookahead
->keyword
));
22545 /* If we don't have a type, then something is very wrong and we
22546 shouldn't try to do anything clever. Likewise for not seeing the
22548 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22550 /* Locate the closing brace. */
22551 cp_token_position prev
22552 = cp_lexer_previous_token_position (parser
->lexer
);
22553 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22554 location_t loc
= prev_token
->location
;
22556 /* We want to suggest insertion of a ';' immediately *after* the
22557 closing brace, so, if we can, offset the location by 1 column. */
22558 location_t next_loc
= loc
;
22559 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22560 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22562 rich_location
richloc (line_table
, next_loc
);
22564 /* If we successfully offset the location, suggest the fix-it. */
22565 if (next_loc
!= loc
)
22566 richloc
.add_fixit_insert_before (next_loc
, ";");
22568 if (CLASSTYPE_DECLARED_CLASS (type
))
22569 error_at (&richloc
,
22570 "expected %<;%> after class definition");
22571 else if (TREE_CODE (type
) == RECORD_TYPE
)
22572 error_at (&richloc
,
22573 "expected %<;%> after struct definition");
22574 else if (TREE_CODE (type
) == UNION_TYPE
)
22575 error_at (&richloc
,
22576 "expected %<;%> after union definition");
22578 gcc_unreachable ();
22580 /* Unget one token and smash it to look as though we encountered
22581 a semicolon in the input stream. */
22582 cp_lexer_set_token_position (parser
->lexer
, prev
);
22583 token
= cp_lexer_peek_token (parser
->lexer
);
22584 token
->type
= CPP_SEMICOLON
;
22585 token
->keyword
= RID_MAX
;
22589 /* If this class is not itself within the scope of another class,
22590 then we need to parse the bodies of all of the queued function
22591 definitions. Note that the queued functions defined in a class
22592 are not always processed immediately following the
22593 class-specifier for that class. Consider:
22596 struct B { void f() { sizeof (A); } };
22599 If `f' were processed before the processing of `A' were
22600 completed, there would be no way to compute the size of `A'.
22601 Note that the nesting we are interested in here is lexical --
22602 not the semantic nesting given by TYPE_CONTEXT. In particular,
22605 struct A { struct B; };
22606 struct A::B { void f() { } };
22608 there is no need to delay the parsing of `A::B::f'. */
22609 if (--parser
->num_classes_being_defined
== 0)
22612 tree class_type
= NULL_TREE
;
22613 tree pushed_scope
= NULL_TREE
;
22615 cp_default_arg_entry
*e
;
22616 tree save_ccp
, save_ccr
;
22618 /* In a first pass, parse default arguments to the functions.
22619 Then, in a second pass, parse the bodies of the functions.
22620 This two-phased approach handles cases like:
22628 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22631 /* If there are default arguments that have not yet been processed,
22632 take care of them now. */
22633 if (class_type
!= e
->class_type
)
22636 pop_scope (pushed_scope
);
22637 class_type
= e
->class_type
;
22638 pushed_scope
= push_scope (class_type
);
22640 /* Make sure that any template parameters are in scope. */
22641 maybe_begin_member_template_processing (decl
);
22642 /* Parse the default argument expressions. */
22643 cp_parser_late_parsing_default_args (parser
, decl
);
22644 /* Remove any template parameters from the symbol table. */
22645 maybe_end_member_template_processing ();
22647 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22648 /* Now parse any NSDMIs. */
22649 save_ccp
= current_class_ptr
;
22650 save_ccr
= current_class_ref
;
22651 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22653 if (class_type
!= DECL_CONTEXT (decl
))
22656 pop_scope (pushed_scope
);
22657 class_type
= DECL_CONTEXT (decl
);
22658 pushed_scope
= push_scope (class_type
);
22660 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22661 cp_parser_late_parsing_nsdmi (parser
, decl
);
22663 vec_safe_truncate (unparsed_nsdmis
, 0);
22664 current_class_ptr
= save_ccp
;
22665 current_class_ref
= save_ccr
;
22667 pop_scope (pushed_scope
);
22669 /* Now do some post-NSDMI bookkeeping. */
22670 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22671 after_nsdmi_defaulted_late_checks (class_type
);
22672 vec_safe_truncate (unparsed_classes
, 0);
22673 after_nsdmi_defaulted_late_checks (type
);
22675 /* Now parse the body of the functions. */
22678 /* OpenMP UDRs need to be parsed before all other functions. */
22679 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22680 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22681 cp_parser_late_parsing_for_member (parser
, decl
);
22682 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22683 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22684 cp_parser_late_parsing_for_member (parser
, decl
);
22687 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22688 cp_parser_late_parsing_for_member (parser
, decl
);
22689 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22692 vec_safe_push (unparsed_classes
, type
);
22694 /* Put back any saved access checks. */
22695 pop_deferring_access_checks ();
22697 /* Restore saved state. */
22698 parser
->in_switch_statement_p
= in_switch_statement_p
;
22699 parser
->in_statement
= in_statement
;
22700 parser
->in_function_body
= saved_in_function_body
;
22701 parser
->num_template_parameter_lists
22702 = saved_num_template_parameter_lists
;
22703 parser
->in_unbraced_linkage_specification_p
22704 = saved_in_unbraced_linkage_specification_p
;
22710 cp_parser_class_specifier (cp_parser
* parser
)
22713 timevar_push (TV_PARSE_STRUCT
);
22714 ret
= cp_parser_class_specifier_1 (parser
);
22715 timevar_pop (TV_PARSE_STRUCT
);
22719 /* Parse a class-head.
22722 class-key identifier [opt] base-clause [opt]
22723 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22724 class-key nested-name-specifier [opt] template-id
22727 class-virt-specifier:
22731 class-key attributes identifier [opt] base-clause [opt]
22732 class-key attributes nested-name-specifier identifier base-clause [opt]
22733 class-key attributes nested-name-specifier [opt] template-id
22736 Upon return BASES is initialized to the list of base classes (or
22737 NULL, if there are none) in the same form returned by
22738 cp_parser_base_clause.
22740 Returns the TYPE of the indicated class. Sets
22741 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22742 involving a nested-name-specifier was used, and FALSE otherwise.
22744 Returns error_mark_node if this is not a class-head.
22746 Returns NULL_TREE if the class-head is syntactically valid, but
22747 semantically invalid in a way that means we should skip the entire
22748 body of the class. */
22751 cp_parser_class_head (cp_parser
* parser
,
22752 bool* nested_name_specifier_p
)
22754 tree nested_name_specifier
;
22755 enum tag_types class_key
;
22756 tree id
= NULL_TREE
;
22757 tree type
= NULL_TREE
;
22760 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22761 bool template_id_p
= false;
22762 bool qualified_p
= false;
22763 bool invalid_nested_name_p
= false;
22764 bool invalid_explicit_specialization_p
= false;
22765 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22766 tree pushed_scope
= NULL_TREE
;
22767 unsigned num_templates
;
22768 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22769 /* Assume no nested-name-specifier will be present. */
22770 *nested_name_specifier_p
= false;
22771 /* Assume no template parameter lists will be used in defining the
22774 parser
->colon_corrects_to_scope_p
= false;
22776 /* Look for the class-key. */
22777 class_key
= cp_parser_class_key (parser
);
22778 if (class_key
== none_type
)
22779 return error_mark_node
;
22781 location_t class_head_start_location
= input_location
;
22783 /* Parse the attributes. */
22784 attributes
= cp_parser_attributes_opt (parser
);
22786 /* If the next token is `::', that is invalid -- but sometimes
22787 people do try to write:
22791 Handle this gracefully by accepting the extra qualifier, and then
22792 issuing an error about it later if this really is a
22793 class-head. If it turns out just to be an elaborated type
22794 specifier, remain silent. */
22795 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22796 qualified_p
= true;
22798 push_deferring_access_checks (dk_no_check
);
22800 /* Determine the name of the class. Begin by looking for an
22801 optional nested-name-specifier. */
22802 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22803 nested_name_specifier
22804 = cp_parser_nested_name_specifier_opt (parser
,
22805 /*typename_keyword_p=*/false,
22806 /*check_dependency_p=*/false,
22808 /*is_declaration=*/false);
22809 /* If there was a nested-name-specifier, then there *must* be an
22812 cp_token
*bad_template_keyword
= NULL
;
22814 if (nested_name_specifier
)
22816 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22817 /* Although the grammar says `identifier', it really means
22818 `class-name' or `template-name'. You are only allowed to
22819 define a class that has already been declared with this
22822 The proposed resolution for Core Issue 180 says that wherever
22823 you see `class T::X' you should treat `X' as a type-name.
22825 It is OK to define an inaccessible class; for example:
22827 class A { class B; };
22830 We do not know if we will see a class-name, or a
22831 template-name. We look for a class-name first, in case the
22832 class-name is a template-id; if we looked for the
22833 template-name first we would stop after the template-name. */
22834 cp_parser_parse_tentatively (parser
);
22835 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22836 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22837 type
= cp_parser_class_name (parser
,
22838 /*typename_keyword_p=*/false,
22839 /*template_keyword_p=*/false,
22841 /*check_dependency_p=*/false,
22842 /*class_head_p=*/true,
22843 /*is_declaration=*/false);
22844 /* If that didn't work, ignore the nested-name-specifier. */
22845 if (!cp_parser_parse_definitely (parser
))
22847 invalid_nested_name_p
= true;
22848 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22849 id
= cp_parser_identifier (parser
);
22850 if (id
== error_mark_node
)
22853 /* If we could not find a corresponding TYPE, treat this
22854 declaration like an unqualified declaration. */
22855 if (type
== error_mark_node
)
22856 nested_name_specifier
= NULL_TREE
;
22857 /* Otherwise, count the number of templates used in TYPE and its
22858 containing scopes. */
22863 for (scope
= TREE_TYPE (type
);
22864 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22865 scope
= get_containing_scope (scope
))
22867 && CLASS_TYPE_P (scope
)
22868 && CLASSTYPE_TEMPLATE_INFO (scope
)
22869 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22870 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22871 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22875 /* Otherwise, the identifier is optional. */
22878 /* We don't know whether what comes next is a template-id,
22879 an identifier, or nothing at all. */
22880 cp_parser_parse_tentatively (parser
);
22881 /* Check for a template-id. */
22882 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22883 id
= cp_parser_template_id (parser
,
22884 /*template_keyword_p=*/false,
22885 /*check_dependency_p=*/true,
22887 /*is_declaration=*/true);
22888 /* If that didn't work, it could still be an identifier. */
22889 if (!cp_parser_parse_definitely (parser
))
22891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22893 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22894 id
= cp_parser_identifier (parser
);
22901 template_id_p
= true;
22906 pop_deferring_access_checks ();
22910 cp_parser_check_for_invalid_template_id (parser
, id
,
22912 type_start_token
->location
);
22914 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22916 /* If it's not a `:' or a `{' then we can't really be looking at a
22917 class-head, since a class-head only appears as part of a
22918 class-specifier. We have to detect this situation before calling
22919 xref_tag, since that has irreversible side-effects. */
22920 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22922 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22923 type
= error_mark_node
;
22927 /* At this point, we're going ahead with the class-specifier, even
22928 if some other problem occurs. */
22929 cp_parser_commit_to_tentative_parse (parser
);
22930 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22932 cp_parser_error (parser
,
22933 "cannot specify %<override%> for a class");
22934 type
= error_mark_node
;
22937 /* Issue the error about the overly-qualified name now. */
22940 cp_parser_error (parser
,
22941 "global qualification of class name is invalid");
22942 type
= error_mark_node
;
22945 else if (invalid_nested_name_p
)
22947 cp_parser_error (parser
,
22948 "qualified name does not name a class");
22949 type
= error_mark_node
;
22952 else if (nested_name_specifier
)
22956 if (bad_template_keyword
)
22957 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22958 keyword template shall not appear at the top level. */
22959 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
22960 "keyword %<template%> not allowed in class-head-name");
22962 /* Reject typedef-names in class heads. */
22963 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
22965 error_at (type_start_token
->location
,
22966 "invalid class name in declaration of %qD",
22972 /* Figure out in what scope the declaration is being placed. */
22973 scope
= current_scope ();
22974 /* If that scope does not contain the scope in which the
22975 class was originally declared, the program is invalid. */
22976 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
22978 if (at_namespace_scope_p ())
22979 error_at (type_start_token
->location
,
22980 "declaration of %qD in namespace %qD which does not "
22982 type
, scope
, nested_name_specifier
);
22984 error_at (type_start_token
->location
,
22985 "declaration of %qD in %qD which does not enclose %qD",
22986 type
, scope
, nested_name_specifier
);
22992 A declarator-id shall not be qualified except for the
22993 definition of a ... nested class outside of its class
22994 ... [or] the definition or explicit instantiation of a
22995 class member of a namespace outside of its namespace. */
22996 if (scope
== nested_name_specifier
)
22998 permerror (nested_name_specifier_token_start
->location
,
22999 "extra qualification not allowed");
23000 nested_name_specifier
= NULL_TREE
;
23004 /* An explicit-specialization must be preceded by "template <>". If
23005 it is not, try to recover gracefully. */
23006 if (at_namespace_scope_p ()
23007 && parser
->num_template_parameter_lists
== 0
23008 && !processing_template_parmlist
23011 /* Build a location of this form:
23012 struct typename <ARGS>
23013 ^~~~~~~~~~~~~~~~~~~~~~
23014 with caret==start at the start token, and
23015 finishing at the end of the type. */
23016 location_t reported_loc
23017 = make_location (class_head_start_location
,
23018 class_head_start_location
,
23019 get_finish (type_start_token
->location
));
23020 rich_location
richloc (line_table
, reported_loc
);
23021 richloc
.add_fixit_insert_before (class_head_start_location
,
23023 error_at (&richloc
,
23024 "an explicit specialization must be preceded by"
23025 " %<template <>%>");
23026 invalid_explicit_specialization_p
= true;
23027 /* Take the same action that would have been taken by
23028 cp_parser_explicit_specialization. */
23029 ++parser
->num_template_parameter_lists
;
23030 begin_specialization ();
23032 /* There must be no "return" statements between this point and the
23033 end of this function; set "type "to the correct return value and
23034 use "goto done;" to return. */
23035 /* Make sure that the right number of template parameters were
23037 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23038 type_start_token
->location
,
23039 /*declarator=*/NULL
))
23041 /* If something went wrong, there is no point in even trying to
23042 process the class-definition. */
23047 /* Look up the type. */
23050 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23051 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23052 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23054 error_at (type_start_token
->location
,
23055 "function template %qD redeclared as a class template", id
);
23056 type
= error_mark_node
;
23060 type
= TREE_TYPE (id
);
23061 type
= maybe_process_partial_specialization (type
);
23063 /* Check the scope while we still know whether or not we had a
23064 nested-name-specifier. */
23065 if (type
!= error_mark_node
)
23066 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23068 if (nested_name_specifier
)
23069 pushed_scope
= push_scope (nested_name_specifier
);
23071 else if (nested_name_specifier
)
23077 template <typename T> struct S { struct T };
23078 template <typename T> struct S<T>::T { };
23080 we will get a TYPENAME_TYPE when processing the definition of
23081 `S::T'. We need to resolve it to the actual type before we
23082 try to define it. */
23083 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23085 class_type
= resolve_typename_type (TREE_TYPE (type
),
23086 /*only_current_p=*/false);
23087 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23088 type
= TYPE_NAME (class_type
);
23091 cp_parser_error (parser
, "could not resolve typename type");
23092 type
= error_mark_node
;
23096 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23097 == error_mark_node
)
23103 class_type
= current_class_type
;
23104 /* Enter the scope indicated by the nested-name-specifier. */
23105 pushed_scope
= push_scope (nested_name_specifier
);
23106 /* Get the canonical version of this type. */
23107 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23108 /* Call push_template_decl if it seems like we should be defining a
23109 template either from the template headers or the type we're
23110 defining, so that we diagnose both extra and missing headers. */
23111 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23112 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23113 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23115 type
= push_template_decl (type
);
23116 if (type
== error_mark_node
)
23123 type
= TREE_TYPE (type
);
23124 *nested_name_specifier_p
= true;
23126 else /* The name is not a nested name. */
23128 /* If the class was unnamed, create a dummy name. */
23130 id
= make_anon_name ();
23131 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23132 ? ts_within_enclosing_non_class
23134 type
= xref_tag (class_key
, id
, tag_scope
,
23135 parser
->num_template_parameter_lists
);
23138 /* Indicate whether this class was declared as a `class' or as a
23140 if (TREE_CODE (type
) == RECORD_TYPE
)
23141 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23142 cp_parser_check_class_key (class_key
, type
);
23144 /* If this type was already complete, and we see another definition,
23145 that's an error. */
23146 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23148 error_at (type_start_token
->location
, "redefinition of %q#T",
23150 inform (location_of (type
), "previous definition of %q#T",
23155 else if (type
== error_mark_node
)
23160 /* Apply attributes now, before any use of the class as a template
23161 argument in its base list. */
23162 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23163 fixup_attribute_variants (type
);
23166 /* We will have entered the scope containing the class; the names of
23167 base classes should be looked up in that context. For example:
23169 struct A { struct B {}; struct C; };
23170 struct A::C : B {};
23174 /* Get the list of base-classes, if there is one. */
23175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23177 /* PR59482: enter the class scope so that base-specifiers are looked
23181 bases
= cp_parser_base_clause (parser
);
23182 /* PR59482: get out of the previously pushed class scope so that the
23183 subsequent pops pop the right thing. */
23190 /* If we're really defining a class, process the base classes.
23191 If they're invalid, fail. */
23192 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23193 xref_basetypes (type
, bases
);
23196 /* Leave the scope given by the nested-name-specifier. We will
23197 enter the class scope itself while processing the members. */
23199 pop_scope (pushed_scope
);
23201 if (invalid_explicit_specialization_p
)
23203 end_specialization ();
23204 --parser
->num_template_parameter_lists
;
23208 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23209 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23210 CLASSTYPE_FINAL (type
) = 1;
23212 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23216 /* Parse a class-key.
23223 Returns the kind of class-key specified, or none_type to indicate
23226 static enum tag_types
23227 cp_parser_class_key (cp_parser
* parser
)
23230 enum tag_types tag_type
;
23232 /* Look for the class-key. */
23233 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23237 /* Check to see if the TOKEN is a class-key. */
23238 tag_type
= cp_parser_token_is_class_key (token
);
23240 cp_parser_error (parser
, "expected class-key");
23244 /* Parse a type-parameter-key.
23246 type-parameter-key:
23252 cp_parser_type_parameter_key (cp_parser
* parser
)
23254 /* Look for the type-parameter-key. */
23255 enum tag_types tag_type
= none_type
;
23256 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23257 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23259 cp_lexer_consume_token (parser
->lexer
);
23260 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23261 /* typename is not allowed in a template template parameter
23262 by the standard until C++17. */
23263 pedwarn (token
->location
, OPT_Wpedantic
,
23264 "ISO C++ forbids typename key in template template parameter;"
23265 " use -std=c++17 or -std=gnu++17");
23268 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23273 /* Parse an (optional) member-specification.
23275 member-specification:
23276 member-declaration member-specification [opt]
23277 access-specifier : member-specification [opt] */
23280 cp_parser_member_specification_opt (cp_parser
* parser
)
23287 /* Peek at the next token. */
23288 token
= cp_lexer_peek_token (parser
->lexer
);
23289 /* If it's a `}', or EOF then we've seen all the members. */
23290 if (token
->type
== CPP_CLOSE_BRACE
23291 || token
->type
== CPP_EOF
23292 || token
->type
== CPP_PRAGMA_EOL
)
23295 /* See if this token is a keyword. */
23296 keyword
= token
->keyword
;
23300 case RID_PROTECTED
:
23302 /* Consume the access-specifier. */
23303 cp_lexer_consume_token (parser
->lexer
);
23304 /* Remember which access-specifier is active. */
23305 current_access_specifier
= token
->u
.value
;
23306 /* Look for the `:'. */
23307 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23311 /* Accept #pragmas at class scope. */
23312 if (token
->type
== CPP_PRAGMA
)
23314 cp_parser_pragma (parser
, pragma_member
, NULL
);
23318 /* Otherwise, the next construction must be a
23319 member-declaration. */
23320 cp_parser_member_declaration (parser
);
23325 /* Parse a member-declaration.
23327 member-declaration:
23328 decl-specifier-seq [opt] member-declarator-list [opt] ;
23329 function-definition ; [opt]
23330 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23332 template-declaration
23335 member-declarator-list:
23337 member-declarator-list , member-declarator
23340 declarator pure-specifier [opt]
23341 declarator constant-initializer [opt]
23342 identifier [opt] : constant-expression
23346 member-declaration:
23347 __extension__ member-declaration
23350 declarator attributes [opt] pure-specifier [opt]
23351 declarator attributes [opt] constant-initializer [opt]
23352 identifier [opt] attributes [opt] : constant-expression
23356 member-declaration:
23357 static_assert-declaration */
23360 cp_parser_member_declaration (cp_parser
* parser
)
23362 cp_decl_specifier_seq decl_specifiers
;
23363 tree prefix_attributes
;
23365 int declares_class_or_enum
;
23367 cp_token
*token
= NULL
;
23368 cp_token
*decl_spec_token_start
= NULL
;
23369 cp_token
*initializer_token_start
= NULL
;
23370 int saved_pedantic
;
23371 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23373 /* Check for the `__extension__' keyword. */
23374 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23377 cp_parser_member_declaration (parser
);
23378 /* Restore the old value of the PEDANTIC flag. */
23379 pedantic
= saved_pedantic
;
23384 /* Check for a template-declaration. */
23385 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23387 /* An explicit specialization here is an error condition, and we
23388 expect the specialization handler to detect and report this. */
23389 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23390 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23391 cp_parser_explicit_specialization (parser
);
23393 cp_parser_template_declaration (parser
, /*member_p=*/true);
23397 /* Check for a template introduction. */
23398 else if (cp_parser_template_declaration_after_export (parser
, true))
23401 /* Check for a using-declaration. */
23402 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23404 if (cxx_dialect
< cxx11
)
23406 /* Parse the using-declaration. */
23407 cp_parser_using_declaration (parser
,
23408 /*access_declaration_p=*/false);
23414 bool alias_decl_expected
;
23415 cp_parser_parse_tentatively (parser
);
23416 decl
= cp_parser_alias_declaration (parser
);
23417 /* Note that if we actually see the '=' token after the
23418 identifier, cp_parser_alias_declaration commits the
23419 tentative parse. In that case, we really expect an
23420 alias-declaration. Otherwise, we expect a using
23422 alias_decl_expected
=
23423 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23424 cp_parser_parse_definitely (parser
);
23426 if (alias_decl_expected
)
23427 finish_member_declaration (decl
);
23429 cp_parser_using_declaration (parser
,
23430 /*access_declaration_p=*/false);
23435 /* Check for @defs. */
23436 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23439 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23440 ivar
= ivar_chains
;
23444 ivar
= TREE_CHAIN (member
);
23445 TREE_CHAIN (member
) = NULL_TREE
;
23446 finish_member_declaration (member
);
23451 /* If the next token is `static_assert' we have a static assertion. */
23452 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23454 cp_parser_static_assert (parser
, /*member_p=*/true);
23458 parser
->colon_corrects_to_scope_p
= false;
23460 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23463 /* Parse the decl-specifier-seq. */
23464 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23465 cp_parser_decl_specifier_seq (parser
,
23466 CP_PARSER_FLAGS_OPTIONAL
,
23468 &declares_class_or_enum
);
23469 /* Check for an invalid type-name. */
23470 if (!decl_specifiers
.any_type_specifiers_p
23471 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23473 /* If there is no declarator, then the decl-specifier-seq should
23475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23477 /* If there was no decl-specifier-seq, and the next token is a
23478 `;', then we have something like:
23484 Each member-declaration shall declare at least one member
23485 name of the class. */
23486 if (!decl_specifiers
.any_specifiers_p
)
23488 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23489 if (!in_system_header_at (token
->location
))
23491 gcc_rich_location
richloc (token
->location
);
23492 richloc
.add_fixit_remove ();
23493 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23500 /* See if this declaration is a friend. */
23501 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23502 /* If there were decl-specifiers, check to see if there was
23503 a class-declaration. */
23504 type
= check_tag_decl (&decl_specifiers
,
23505 /*explicit_type_instantiation_p=*/false);
23506 /* Nested classes have already been added to the class, but
23507 a `friend' needs to be explicitly registered. */
23510 /* If the `friend' keyword was present, the friend must
23511 be introduced with a class-key. */
23512 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23513 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23514 "in C++03 a class-key must be used "
23515 "when declaring a friend");
23518 template <typename T> struct A {
23519 friend struct A<T>::B;
23522 A<T>::B will be represented by a TYPENAME_TYPE, and
23523 therefore not recognized by check_tag_decl. */
23526 type
= decl_specifiers
.type
;
23527 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23528 type
= TREE_TYPE (type
);
23530 if (!type
|| !TYPE_P (type
))
23531 error_at (decl_spec_token_start
->location
,
23532 "friend declaration does not name a class or "
23535 make_friend_class (current_class_type
, type
,
23536 /*complain=*/true);
23538 /* If there is no TYPE, an error message will already have
23540 else if (!type
|| type
== error_mark_node
)
23542 /* An anonymous aggregate has to be handled specially; such
23543 a declaration really declares a data member (with a
23544 particular type), as opposed to a nested class. */
23545 else if (ANON_AGGR_TYPE_P (type
))
23548 if (decl_specifiers
.storage_class
!= sc_none
)
23549 error_at (decl_spec_token_start
->location
,
23550 "a storage class on an anonymous aggregate "
23551 "in class scope is not allowed");
23553 /* Remove constructors and such from TYPE, now that we
23554 know it is an anonymous aggregate. */
23555 fixup_anonymous_aggr (type
);
23556 /* And make the corresponding data member. */
23557 decl
= build_decl (decl_spec_token_start
->location
,
23558 FIELD_DECL
, NULL_TREE
, type
);
23559 /* Add it to the class. */
23560 finish_member_declaration (decl
);
23563 cp_parser_check_access_in_redeclaration
23565 decl_spec_token_start
->location
);
23570 bool assume_semicolon
= false;
23572 /* Clear attributes from the decl_specifiers but keep them
23573 around as prefix attributes that apply them to the entity
23575 prefix_attributes
= decl_specifiers
.attributes
;
23576 decl_specifiers
.attributes
= NULL_TREE
;
23578 /* See if these declarations will be friends. */
23579 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23581 /* Keep going until we hit the `;' at the end of the
23583 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23585 tree attributes
= NULL_TREE
;
23586 tree first_attribute
;
23588 bool is_bitfld
= false;
23589 bool named_bitfld
= false;
23591 /* Peek at the next token. */
23592 token
= cp_lexer_peek_token (parser
->lexer
);
23594 /* The following code wants to know early if it is a bit-field
23595 or some other declaration. Attributes can appear before
23596 the `:' token, but are hopefully rare enough that the
23597 simplicity of the tentative lookup pays off. */
23598 if (cp_next_tokens_can_be_attribute_p (parser
)
23599 || (token
->type
== CPP_NAME
23600 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23601 && (named_bitfld
= true)))
23603 cp_parser_parse_tentatively (parser
);
23605 cp_lexer_consume_token (parser
->lexer
);
23606 cp_parser_attributes_opt (parser
);
23607 token
= cp_lexer_peek_token (parser
->lexer
);
23608 is_bitfld
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
23609 cp_parser_abort_tentative_parse (parser
);
23612 /* Check for a bitfield declaration. */
23614 || token
->type
== CPP_COLON
23615 || (token
->type
== CPP_NAME
23616 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23617 && (named_bitfld
= true)))
23621 tree late_attributes
= NULL_TREE
;
23624 identifier
= cp_parser_identifier (parser
);
23626 identifier
= NULL_TREE
;
23628 /* Look for attributes that apply to the bitfield. */
23629 attributes
= cp_parser_attributes_opt (parser
);
23631 /* Consume the `:' token. */
23632 cp_lexer_consume_token (parser
->lexer
);
23634 /* Get the width of the bitfield. */
23635 width
= cp_parser_constant_expression (parser
, false, NULL
,
23636 cxx_dialect
>= cxx11
);
23638 /* In C++2A and as extension for C++11 and above we allow
23639 default member initializers for bit-fields. */
23640 initializer
= NULL_TREE
;
23641 if (cxx_dialect
>= cxx11
23642 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23643 || cp_lexer_next_token_is (parser
->lexer
,
23647 = cp_lexer_peek_token (parser
->lexer
)->location
;
23648 if (cxx_dialect
< cxx2a
23649 && !in_system_header_at (loc
)
23650 && identifier
!= NULL_TREE
)
23652 "default member initializers for bit-fields "
23653 "only available with -std=c++2a or "
23656 initializer
= cp_parser_save_nsdmi (parser
);
23657 if (identifier
== NULL_TREE
)
23659 error_at (loc
, "default member initializer for "
23660 "unnamed bit-field");
23661 initializer
= NULL_TREE
;
23666 /* Look for attributes that apply to the bitfield after
23667 the `:' token and width. This is where GCC used to
23668 parse attributes in the past, pedwarn if there is
23669 a std attribute. */
23670 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23671 pedwarn (input_location
, OPT_Wpedantic
,
23672 "ISO C++ allows bit-field attributes only "
23673 "before the %<:%> token");
23675 late_attributes
= cp_parser_attributes_opt (parser
);
23678 attributes
= chainon (attributes
, late_attributes
);
23680 /* Remember which attributes are prefix attributes and
23682 first_attribute
= attributes
;
23683 /* Combine the attributes. */
23684 attributes
= chainon (prefix_attributes
, attributes
);
23686 /* Create the bitfield declaration. */
23687 decl
= grokbitfield (identifier
23688 ? make_id_declarator (NULL_TREE
,
23693 width
, initializer
,
23698 cp_declarator
*declarator
;
23699 tree asm_specification
;
23700 int ctor_dtor_or_conv_p
;
23702 /* Parse the declarator. */
23704 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23705 &ctor_dtor_or_conv_p
,
23706 /*parenthesized_p=*/NULL
,
23710 /* If something went wrong parsing the declarator, make sure
23711 that we at least consume some tokens. */
23712 if (declarator
== cp_error_declarator
)
23714 /* Skip to the end of the statement. */
23715 cp_parser_skip_to_end_of_statement (parser
);
23716 /* If the next token is not a semicolon, that is
23717 probably because we just skipped over the body of
23718 a function. So, we consume a semicolon if
23719 present, but do not issue an error message if it
23721 if (cp_lexer_next_token_is (parser
->lexer
,
23723 cp_lexer_consume_token (parser
->lexer
);
23727 if (declares_class_or_enum
& 2)
23728 cp_parser_check_for_definition_in_return_type
23729 (declarator
, decl_specifiers
.type
,
23730 decl_specifiers
.locations
[ds_type_spec
]);
23732 /* Look for an asm-specification. */
23733 asm_specification
= cp_parser_asm_specification_opt (parser
);
23734 /* Look for attributes that apply to the declaration. */
23735 attributes
= cp_parser_attributes_opt (parser
);
23736 /* Remember which attributes are prefix attributes and
23738 first_attribute
= attributes
;
23739 /* Combine the attributes. */
23740 attributes
= chainon (prefix_attributes
, attributes
);
23742 /* If it's an `=', then we have a constant-initializer or a
23743 pure-specifier. It is not correct to parse the
23744 initializer before registering the member declaration
23745 since the member declaration should be in scope while
23746 its initializer is processed. However, the rest of the
23747 front end does not yet provide an interface that allows
23748 us to handle this correctly. */
23749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23753 A pure-specifier shall be used only in the declaration of
23754 a virtual function.
23756 A member-declarator can contain a constant-initializer
23757 only if it declares a static member of integral or
23760 Therefore, if the DECLARATOR is for a function, we look
23761 for a pure-specifier; otherwise, we look for a
23762 constant-initializer. When we call `grokfield', it will
23763 perform more stringent semantics checks. */
23764 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23765 if (function_declarator_p (declarator
)
23766 || (decl_specifiers
.type
23767 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23768 && declarator
->kind
== cdk_id
23769 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23770 == FUNCTION_TYPE
)))
23771 initializer
= cp_parser_pure_specifier (parser
);
23772 else if (decl_specifiers
.storage_class
!= sc_static
)
23773 initializer
= cp_parser_save_nsdmi (parser
);
23774 else if (cxx_dialect
>= cxx11
)
23777 /* Don't require a constant rvalue in C++11, since we
23778 might want a reference constant. We'll enforce
23779 constancy later. */
23780 cp_lexer_consume_token (parser
->lexer
);
23781 /* Parse the initializer. */
23782 initializer
= cp_parser_initializer_clause (parser
,
23786 /* Parse the initializer. */
23787 initializer
= cp_parser_constant_initializer (parser
);
23789 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23790 && !function_declarator_p (declarator
))
23793 if (decl_specifiers
.storage_class
!= sc_static
)
23794 initializer
= cp_parser_save_nsdmi (parser
);
23796 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23798 /* Otherwise, there is no initializer. */
23800 initializer
= NULL_TREE
;
23802 /* See if we are probably looking at a function
23803 definition. We are certainly not looking at a
23804 member-declarator. Calling `grokfield' has
23805 side-effects, so we must not do it unless we are sure
23806 that we are looking at a member-declarator. */
23807 if (cp_parser_token_starts_function_definition_p
23808 (cp_lexer_peek_token (parser
->lexer
)))
23810 /* The grammar does not allow a pure-specifier to be
23811 used when a member function is defined. (It is
23812 possible that this fact is an oversight in the
23813 standard, since a pure function may be defined
23814 outside of the class-specifier. */
23815 if (initializer
&& initializer_token_start
)
23816 error_at (initializer_token_start
->location
,
23817 "pure-specifier on function-definition");
23818 decl
= cp_parser_save_member_function_body (parser
,
23822 if (parser
->fully_implicit_function_template_p
)
23823 decl
= finish_fully_implicit_template (parser
, decl
);
23824 /* If the member was not a friend, declare it here. */
23826 finish_member_declaration (decl
);
23827 /* Peek at the next token. */
23828 token
= cp_lexer_peek_token (parser
->lexer
);
23829 /* If the next token is a semicolon, consume it. */
23830 if (token
->type
== CPP_SEMICOLON
)
23832 location_t semicolon_loc
23833 = cp_lexer_consume_token (parser
->lexer
)->location
;
23834 gcc_rich_location
richloc (semicolon_loc
);
23835 richloc
.add_fixit_remove ();
23836 warning_at (&richloc
, OPT_Wextra_semi
,
23837 "extra %<;%> after in-class "
23838 "function definition");
23843 if (declarator
->kind
== cdk_function
)
23844 declarator
->id_loc
= token
->location
;
23845 /* Create the declaration. */
23846 decl
= grokfield (declarator
, &decl_specifiers
,
23847 initializer
, /*init_const_expr_p=*/true,
23848 asm_specification
, attributes
);
23849 if (parser
->fully_implicit_function_template_p
)
23852 finish_fully_implicit_template (parser
, 0);
23854 decl
= finish_fully_implicit_template (parser
, decl
);
23858 cp_finalize_omp_declare_simd (parser
, decl
);
23859 cp_finalize_oacc_routine (parser
, decl
, false);
23861 /* Reset PREFIX_ATTRIBUTES. */
23862 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23863 attributes
= TREE_CHAIN (attributes
);
23865 TREE_CHAIN (attributes
) = NULL_TREE
;
23867 /* If there is any qualification still in effect, clear it
23868 now; we will be starting fresh with the next declarator. */
23869 parser
->scope
= NULL_TREE
;
23870 parser
->qualifying_scope
= NULL_TREE
;
23871 parser
->object_scope
= NULL_TREE
;
23872 /* If it's a `,', then there are more declarators. */
23873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23875 cp_lexer_consume_token (parser
->lexer
);
23876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23878 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23879 gcc_rich_location
richloc (token
->location
);
23880 richloc
.add_fixit_remove ();
23881 error_at (&richloc
, "stray %<,%> at end of "
23882 "member declaration");
23885 /* If the next token isn't a `;', then we have a parse error. */
23886 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23889 /* The next token might be a ways away from where the
23890 actual semicolon is missing. Find the previous token
23891 and use that for our error position. */
23892 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23893 gcc_rich_location
richloc (token
->location
);
23894 richloc
.add_fixit_insert_after (";");
23895 error_at (&richloc
, "expected %<;%> at end of "
23896 "member declaration");
23898 /* Assume that the user meant to provide a semicolon. If
23899 we were to cp_parser_skip_to_end_of_statement, we might
23900 skip to a semicolon inside a member function definition
23901 and issue nonsensical error messages. */
23902 assume_semicolon
= true;
23907 /* Add DECL to the list of members. */
23909 /* Explicitly include, eg, NSDMIs, for better error
23910 recovery (c++/58650). */
23911 || !DECL_DECLARES_FUNCTION_P (decl
))
23912 finish_member_declaration (decl
);
23914 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23915 cp_parser_save_default_args (parser
, decl
);
23916 else if (TREE_CODE (decl
) == FIELD_DECL
23917 && DECL_INITIAL (decl
))
23918 /* Add DECL to the queue of NSDMI to be parsed later. */
23919 vec_safe_push (unparsed_nsdmis
, decl
);
23922 if (assume_semicolon
)
23927 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23929 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23932 /* Parse a pure-specifier.
23937 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23938 Otherwise, ERROR_MARK_NODE is returned. */
23941 cp_parser_pure_specifier (cp_parser
* parser
)
23945 /* Look for the `=' token. */
23946 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23947 return error_mark_node
;
23948 /* Look for the `0' token. */
23949 token
= cp_lexer_peek_token (parser
->lexer
);
23951 if (token
->type
== CPP_EOF
23952 || token
->type
== CPP_PRAGMA_EOL
)
23953 return error_mark_node
;
23955 cp_lexer_consume_token (parser
->lexer
);
23957 /* Accept = default or = delete in c++0x mode. */
23958 if (token
->keyword
== RID_DEFAULT
23959 || token
->keyword
== RID_DELETE
)
23961 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
23962 return token
->u
.value
;
23965 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23966 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
23968 cp_parser_error (parser
,
23969 "invalid pure specifier (only %<= 0%> is allowed)");
23970 cp_parser_skip_to_end_of_statement (parser
);
23971 return error_mark_node
;
23973 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23975 error_at (token
->location
, "templates may not be %<virtual%>");
23976 return error_mark_node
;
23979 return integer_zero_node
;
23982 /* Parse a constant-initializer.
23984 constant-initializer:
23985 = constant-expression
23987 Returns a representation of the constant-expression. */
23990 cp_parser_constant_initializer (cp_parser
* parser
)
23992 /* Look for the `=' token. */
23993 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23994 return error_mark_node
;
23996 /* It is invalid to write:
23998 struct S { static const int i = { 7 }; };
24001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24003 cp_parser_error (parser
,
24004 "a brace-enclosed initializer is not allowed here");
24005 /* Consume the opening brace. */
24006 matching_braces braces
;
24007 braces
.consume_open (parser
);
24008 /* Skip the initializer. */
24009 cp_parser_skip_to_closing_brace (parser
);
24010 /* Look for the trailing `}'. */
24011 braces
.require_close (parser
);
24013 return error_mark_node
;
24016 return cp_parser_constant_expression (parser
);
24019 /* Derived classes [gram.class.derived] */
24021 /* Parse a base-clause.
24024 : base-specifier-list
24026 base-specifier-list:
24027 base-specifier ... [opt]
24028 base-specifier-list , base-specifier ... [opt]
24030 Returns a TREE_LIST representing the base-classes, in the order in
24031 which they were declared. The representation of each node is as
24032 described by cp_parser_base_specifier.
24034 In the case that no bases are specified, this function will return
24035 NULL_TREE, not ERROR_MARK_NODE. */
24038 cp_parser_base_clause (cp_parser
* parser
)
24040 tree bases
= NULL_TREE
;
24042 /* Look for the `:' that begins the list. */
24043 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24045 /* Scan the base-specifier-list. */
24050 bool pack_expansion_p
= false;
24052 /* Look for the base-specifier. */
24053 base
= cp_parser_base_specifier (parser
);
24054 /* Look for the (optional) ellipsis. */
24055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24057 /* Consume the `...'. */
24058 cp_lexer_consume_token (parser
->lexer
);
24060 pack_expansion_p
= true;
24063 /* Add BASE to the front of the list. */
24064 if (base
&& base
!= error_mark_node
)
24066 if (pack_expansion_p
)
24067 /* Make this a pack expansion type. */
24068 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24070 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24072 TREE_CHAIN (base
) = bases
;
24076 /* Peek at the next token. */
24077 token
= cp_lexer_peek_token (parser
->lexer
);
24078 /* If it's not a comma, then the list is complete. */
24079 if (token
->type
!= CPP_COMMA
)
24081 /* Consume the `,'. */
24082 cp_lexer_consume_token (parser
->lexer
);
24085 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24086 base class had a qualified name. However, the next name that
24087 appears is certainly not qualified. */
24088 parser
->scope
= NULL_TREE
;
24089 parser
->qualifying_scope
= NULL_TREE
;
24090 parser
->object_scope
= NULL_TREE
;
24092 return nreverse (bases
);
24095 /* Parse a base-specifier.
24098 :: [opt] nested-name-specifier [opt] class-name
24099 virtual access-specifier [opt] :: [opt] nested-name-specifier
24101 access-specifier virtual [opt] :: [opt] nested-name-specifier
24104 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24105 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24106 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24107 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24110 cp_parser_base_specifier (cp_parser
* parser
)
24114 bool virtual_p
= false;
24115 bool duplicate_virtual_error_issued_p
= false;
24116 bool duplicate_access_error_issued_p
= false;
24117 bool class_scope_p
, template_p
;
24118 tree access
= access_default_node
;
24121 /* Process the optional `virtual' and `access-specifier'. */
24124 /* Peek at the next token. */
24125 token
= cp_lexer_peek_token (parser
->lexer
);
24126 /* Process `virtual'. */
24127 switch (token
->keyword
)
24130 /* If `virtual' appears more than once, issue an error. */
24131 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24133 cp_parser_error (parser
,
24134 "%<virtual%> specified more than once in base-specifier");
24135 duplicate_virtual_error_issued_p
= true;
24140 /* Consume the `virtual' token. */
24141 cp_lexer_consume_token (parser
->lexer
);
24146 case RID_PROTECTED
:
24148 /* If more than one access specifier appears, issue an
24150 if (access
!= access_default_node
24151 && !duplicate_access_error_issued_p
)
24153 cp_parser_error (parser
,
24154 "more than one access specifier in base-specifier");
24155 duplicate_access_error_issued_p
= true;
24158 access
= ridpointers
[(int) token
->keyword
];
24160 /* Consume the access-specifier. */
24161 cp_lexer_consume_token (parser
->lexer
);
24170 /* It is not uncommon to see programs mechanically, erroneously, use
24171 the 'typename' keyword to denote (dependent) qualified types
24172 as base classes. */
24173 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24175 token
= cp_lexer_peek_token (parser
->lexer
);
24176 if (!processing_template_decl
)
24177 error_at (token
->location
,
24178 "keyword %<typename%> not allowed outside of templates");
24180 error_at (token
->location
,
24181 "keyword %<typename%> not allowed in this context "
24182 "(the base class is implicitly a type)");
24183 cp_lexer_consume_token (parser
->lexer
);
24186 /* Look for the optional `::' operator. */
24187 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24188 /* Look for the nested-name-specifier. The simplest way to
24193 The keyword `typename' is not permitted in a base-specifier or
24194 mem-initializer; in these contexts a qualified name that
24195 depends on a template-parameter is implicitly assumed to be a
24198 is to pretend that we have seen the `typename' keyword at this
24200 cp_parser_nested_name_specifier_opt (parser
,
24201 /*typename_keyword_p=*/true,
24202 /*check_dependency_p=*/true,
24204 /*is_declaration=*/true);
24205 /* If the base class is given by a qualified name, assume that names
24206 we see are type names or templates, as appropriate. */
24207 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24208 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24211 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24212 /* DR 950 allows decltype as a base-specifier. */
24213 type
= cp_parser_decltype (parser
);
24216 /* Otherwise, look for the class-name. */
24217 type
= cp_parser_class_name (parser
,
24221 /*check_dependency_p=*/true,
24222 /*class_head_p=*/false,
24223 /*is_declaration=*/true);
24224 type
= TREE_TYPE (type
);
24227 if (type
== error_mark_node
)
24228 return error_mark_node
;
24230 return finish_base_specifier (type
, access
, virtual_p
);
24233 /* Exception handling [gram.exception] */
24235 /* Parse an (optional) noexcept-specification.
24237 noexcept-specification:
24238 noexcept ( constant-expression ) [opt]
24240 If no noexcept-specification is present, returns NULL_TREE.
24241 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24242 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24243 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24244 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24245 in which case a boolean condition is returned instead. */
24248 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24249 bool require_constexpr
,
24250 bool* consumed_expr
,
24254 const char *saved_message
;
24256 /* Peek at the next token. */
24257 token
= cp_lexer_peek_token (parser
->lexer
);
24259 /* Is it a noexcept-specification? */
24260 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24263 cp_lexer_consume_token (parser
->lexer
);
24265 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24267 matching_parens parens
;
24268 parens
.consume_open (parser
);
24270 if (require_constexpr
)
24272 /* Types may not be defined in an exception-specification. */
24273 saved_message
= parser
->type_definition_forbidden_message
;
24274 parser
->type_definition_forbidden_message
24275 = G_("types may not be defined in an exception-specification");
24277 expr
= cp_parser_constant_expression (parser
);
24279 /* Restore the saved message. */
24280 parser
->type_definition_forbidden_message
= saved_message
;
24284 expr
= cp_parser_expression (parser
);
24285 *consumed_expr
= true;
24288 parens
.require_close (parser
);
24292 expr
= boolean_true_node
;
24293 if (!require_constexpr
)
24294 *consumed_expr
= false;
24297 /* We cannot build a noexcept-spec right away because this will check
24298 that expr is a constexpr. */
24300 return build_noexcept_spec (expr
, tf_warning_or_error
);
24308 /* Parse an (optional) exception-specification.
24310 exception-specification:
24311 throw ( type-id-list [opt] )
24313 Returns a TREE_LIST representing the exception-specification. The
24314 TREE_VALUE of each node is a type. */
24317 cp_parser_exception_specification_opt (cp_parser
* parser
)
24321 const char *saved_message
;
24323 /* Peek at the next token. */
24324 token
= cp_lexer_peek_token (parser
->lexer
);
24326 /* Is it a noexcept-specification? */
24327 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24329 if (type_id_list
!= NULL_TREE
)
24330 return type_id_list
;
24332 /* If it's not `throw', then there's no exception-specification. */
24333 if (!cp_parser_is_keyword (token
, RID_THROW
))
24336 location_t loc
= token
->location
;
24338 /* Consume the `throw'. */
24339 cp_lexer_consume_token (parser
->lexer
);
24341 /* Look for the `('. */
24342 matching_parens parens
;
24343 parens
.require_open (parser
);
24345 /* Peek at the next token. */
24346 token
= cp_lexer_peek_token (parser
->lexer
);
24347 /* If it's not a `)', then there is a type-id-list. */
24348 if (token
->type
!= CPP_CLOSE_PAREN
)
24350 /* Types may not be defined in an exception-specification. */
24351 saved_message
= parser
->type_definition_forbidden_message
;
24352 parser
->type_definition_forbidden_message
24353 = G_("types may not be defined in an exception-specification");
24354 /* Parse the type-id-list. */
24355 type_id_list
= cp_parser_type_id_list (parser
);
24356 /* Restore the saved message. */
24357 parser
->type_definition_forbidden_message
= saved_message
;
24359 if (cxx_dialect
>= cxx17
)
24361 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24363 type_id_list
= NULL_TREE
;
24365 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24366 warning_at (loc
, OPT_Wdeprecated
,
24367 "dynamic exception specifications are deprecated in "
24370 /* In C++17, throw() is equivalent to noexcept (true). throw()
24371 is deprecated in C++11 and above as well, but is still widely used,
24372 so don't warn about it yet. */
24373 else if (cxx_dialect
>= cxx17
)
24374 type_id_list
= noexcept_true_spec
;
24376 type_id_list
= empty_except_spec
;
24378 /* Look for the `)'. */
24379 parens
.require_close (parser
);
24381 return type_id_list
;
24384 /* Parse an (optional) type-id-list.
24388 type-id-list , type-id ... [opt]
24390 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24391 in the order that the types were presented. */
24394 cp_parser_type_id_list (cp_parser
* parser
)
24396 tree types
= NULL_TREE
;
24403 token
= cp_lexer_peek_token (parser
->lexer
);
24405 /* Get the next type-id. */
24406 type
= cp_parser_type_id (parser
);
24407 /* Check for invalid 'auto'. */
24408 if (flag_concepts
&& type_uses_auto (type
))
24410 error_at (token
->location
,
24411 "invalid use of %<auto%> in exception-specification");
24412 type
= error_mark_node
;
24414 /* Parse the optional ellipsis. */
24415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24417 /* Consume the `...'. */
24418 cp_lexer_consume_token (parser
->lexer
);
24420 /* Turn the type into a pack expansion expression. */
24421 type
= make_pack_expansion (type
);
24423 /* Add it to the list. */
24424 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24425 /* Peek at the next token. */
24426 token
= cp_lexer_peek_token (parser
->lexer
);
24427 /* If it is not a `,', we are done. */
24428 if (token
->type
!= CPP_COMMA
)
24430 /* Consume the `,'. */
24431 cp_lexer_consume_token (parser
->lexer
);
24434 return nreverse (types
);
24437 /* Parse a try-block.
24440 try compound-statement handler-seq */
24443 cp_parser_try_block (cp_parser
* parser
)
24447 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24448 if (parser
->in_function_body
24449 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24450 error ("%<try%> in %<constexpr%> function");
24452 try_block
= begin_try_block ();
24453 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24454 finish_try_block (try_block
);
24455 cp_parser_handler_seq (parser
);
24456 finish_handler_sequence (try_block
);
24461 /* Parse a function-try-block.
24463 function-try-block:
24464 try ctor-initializer [opt] function-body handler-seq */
24467 cp_parser_function_try_block (cp_parser
* parser
)
24469 tree compound_stmt
;
24472 /* Look for the `try' keyword. */
24473 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24475 /* Let the rest of the front end know where we are. */
24476 try_block
= begin_function_try_block (&compound_stmt
);
24477 /* Parse the function-body. */
24478 cp_parser_ctor_initializer_opt_and_function_body
24479 (parser
, /*in_function_try_block=*/true);
24480 /* We're done with the `try' part. */
24481 finish_function_try_block (try_block
);
24482 /* Parse the handlers. */
24483 cp_parser_handler_seq (parser
);
24484 /* We're done with the handlers. */
24485 finish_function_handler_sequence (try_block
, compound_stmt
);
24488 /* Parse a handler-seq.
24491 handler handler-seq [opt] */
24494 cp_parser_handler_seq (cp_parser
* parser
)
24500 /* Parse the handler. */
24501 cp_parser_handler (parser
);
24502 /* Peek at the next token. */
24503 token
= cp_lexer_peek_token (parser
->lexer
);
24504 /* If it's not `catch' then there are no more handlers. */
24505 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24510 /* Parse a handler.
24513 catch ( exception-declaration ) compound-statement */
24516 cp_parser_handler (cp_parser
* parser
)
24521 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24522 handler
= begin_handler ();
24523 matching_parens parens
;
24524 parens
.require_open (parser
);
24525 declaration
= cp_parser_exception_declaration (parser
);
24526 finish_handler_parms (declaration
, handler
);
24527 parens
.require_close (parser
);
24528 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24529 finish_handler (handler
);
24532 /* Parse an exception-declaration.
24534 exception-declaration:
24535 type-specifier-seq declarator
24536 type-specifier-seq abstract-declarator
24540 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24541 ellipsis variant is used. */
24544 cp_parser_exception_declaration (cp_parser
* parser
)
24546 cp_decl_specifier_seq type_specifiers
;
24547 cp_declarator
*declarator
;
24548 const char *saved_message
;
24550 /* If it's an ellipsis, it's easy to handle. */
24551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24553 /* Consume the `...' token. */
24554 cp_lexer_consume_token (parser
->lexer
);
24558 /* Types may not be defined in exception-declarations. */
24559 saved_message
= parser
->type_definition_forbidden_message
;
24560 parser
->type_definition_forbidden_message
24561 = G_("types may not be defined in exception-declarations");
24563 /* Parse the type-specifier-seq. */
24564 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24565 /*is_trailing_return=*/false,
24567 /* If it's a `)', then there is no declarator. */
24568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24571 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24572 /*ctor_dtor_or_conv_p=*/NULL
,
24573 /*parenthesized_p=*/NULL
,
24574 /*member_p=*/false,
24575 /*friend_p=*/false);
24577 /* Restore the saved message. */
24578 parser
->type_definition_forbidden_message
= saved_message
;
24580 if (!type_specifiers
.any_specifiers_p
)
24581 return error_mark_node
;
24583 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24586 /* Parse a throw-expression.
24589 throw assignment-expression [opt]
24591 Returns a THROW_EXPR representing the throw-expression. */
24594 cp_parser_throw_expression (cp_parser
* parser
)
24599 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24600 token
= cp_lexer_peek_token (parser
->lexer
);
24601 /* Figure out whether or not there is an assignment-expression
24602 following the "throw" keyword. */
24603 if (token
->type
== CPP_COMMA
24604 || token
->type
== CPP_SEMICOLON
24605 || token
->type
== CPP_CLOSE_PAREN
24606 || token
->type
== CPP_CLOSE_SQUARE
24607 || token
->type
== CPP_CLOSE_BRACE
24608 || token
->type
== CPP_COLON
)
24609 expression
= NULL_TREE
;
24611 expression
= cp_parser_assignment_expression (parser
);
24613 return build_throw (expression
);
24616 /* GNU Extensions */
24618 /* Parse an (optional) asm-specification.
24621 asm ( string-literal )
24623 If the asm-specification is present, returns a STRING_CST
24624 corresponding to the string-literal. Otherwise, returns
24628 cp_parser_asm_specification_opt (cp_parser
* parser
)
24631 tree asm_specification
;
24633 /* Peek at the next token. */
24634 token
= cp_lexer_peek_token (parser
->lexer
);
24635 /* If the next token isn't the `asm' keyword, then there's no
24636 asm-specification. */
24637 if (!cp_parser_is_keyword (token
, RID_ASM
))
24640 /* Consume the `asm' token. */
24641 cp_lexer_consume_token (parser
->lexer
);
24642 /* Look for the `('. */
24643 matching_parens parens
;
24644 parens
.require_open (parser
);
24646 /* Look for the string-literal. */
24647 asm_specification
= cp_parser_string_literal (parser
, false, false);
24649 /* Look for the `)'. */
24650 parens
.require_close (parser
);
24652 return asm_specification
;
24655 /* Parse an asm-operand-list.
24659 asm-operand-list , asm-operand
24662 string-literal ( expression )
24663 [ string-literal ] string-literal ( expression )
24665 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24666 each node is the expression. The TREE_PURPOSE is itself a
24667 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24668 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24669 is a STRING_CST for the string literal before the parenthesis. Returns
24670 ERROR_MARK_NODE if any of the operands are invalid. */
24673 cp_parser_asm_operand_list (cp_parser
* parser
)
24675 tree asm_operands
= NULL_TREE
;
24676 bool invalid_operands
= false;
24680 tree string_literal
;
24684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24686 /* Consume the `[' token. */
24687 cp_lexer_consume_token (parser
->lexer
);
24688 /* Read the operand name. */
24689 name
= cp_parser_identifier (parser
);
24690 if (name
!= error_mark_node
)
24691 name
= build_string (IDENTIFIER_LENGTH (name
),
24692 IDENTIFIER_POINTER (name
));
24693 /* Look for the closing `]'. */
24694 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24698 /* Look for the string-literal. */
24699 string_literal
= cp_parser_string_literal (parser
, false, false);
24701 /* Look for the `('. */
24702 matching_parens parens
;
24703 parens
.require_open (parser
);
24704 /* Parse the expression. */
24705 expression
= cp_parser_expression (parser
);
24706 /* Look for the `)'. */
24707 parens
.require_close (parser
);
24709 if (name
== error_mark_node
24710 || string_literal
== error_mark_node
24711 || expression
== error_mark_node
)
24712 invalid_operands
= true;
24714 /* Add this operand to the list. */
24715 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24718 /* If the next token is not a `,', there are no more
24720 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24722 /* Consume the `,'. */
24723 cp_lexer_consume_token (parser
->lexer
);
24726 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24729 /* Parse an asm-clobber-list.
24733 asm-clobber-list , string-literal
24735 Returns a TREE_LIST, indicating the clobbers in the order that they
24736 appeared. The TREE_VALUE of each node is a STRING_CST. */
24739 cp_parser_asm_clobber_list (cp_parser
* parser
)
24741 tree clobbers
= NULL_TREE
;
24745 tree string_literal
;
24747 /* Look for the string literal. */
24748 string_literal
= cp_parser_string_literal (parser
, false, false);
24749 /* Add it to the list. */
24750 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24751 /* If the next token is not a `,', then the list is
24753 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24755 /* Consume the `,' token. */
24756 cp_lexer_consume_token (parser
->lexer
);
24762 /* Parse an asm-label-list.
24766 asm-label-list , identifier
24768 Returns a TREE_LIST, indicating the labels in the order that they
24769 appeared. The TREE_VALUE of each node is a label. */
24772 cp_parser_asm_label_list (cp_parser
* parser
)
24774 tree labels
= NULL_TREE
;
24778 tree identifier
, label
, name
;
24780 /* Look for the identifier. */
24781 identifier
= cp_parser_identifier (parser
);
24782 if (!error_operand_p (identifier
))
24784 label
= lookup_label (identifier
);
24785 if (TREE_CODE (label
) == LABEL_DECL
)
24787 TREE_USED (label
) = 1;
24788 check_goto (label
);
24789 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24790 IDENTIFIER_POINTER (identifier
));
24791 labels
= tree_cons (name
, label
, labels
);
24794 /* If the next token is not a `,', then the list is
24796 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24798 /* Consume the `,' token. */
24799 cp_lexer_consume_token (parser
->lexer
);
24802 return nreverse (labels
);
24805 /* Return TRUE iff the next tokens in the stream are possibly the
24806 beginning of a GNU extension attribute. */
24809 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24811 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24814 /* Return TRUE iff the next tokens in the stream are possibly the
24815 beginning of a standard C++-11 attribute specifier. */
24818 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24820 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24823 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24824 beginning of a standard C++-11 attribute specifier. */
24827 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24829 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24831 return (cxx_dialect
>= cxx11
24832 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24833 || (token
->type
== CPP_OPEN_SQUARE
24834 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24835 && token
->type
== CPP_OPEN_SQUARE
)));
24838 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24839 beginning of a GNU extension attribute. */
24842 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24844 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24846 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24849 /* Return true iff the next tokens can be the beginning of either a
24850 GNU attribute list, or a standard C++11 attribute sequence. */
24853 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24855 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24856 || cp_next_tokens_can_be_std_attribute_p (parser
));
24859 /* Return true iff the next Nth tokens can be the beginning of either
24860 a GNU attribute list, or a standard C++11 attribute sequence. */
24863 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24865 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24866 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24869 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24870 of GNU attributes, or return NULL. */
24873 cp_parser_attributes_opt (cp_parser
*parser
)
24875 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24876 return cp_parser_gnu_attributes_opt (parser
);
24877 return cp_parser_std_attribute_spec_seq (parser
);
24880 /* Parse an (optional) series of attributes.
24883 attributes attribute
24886 __attribute__ (( attribute-list [opt] ))
24888 The return value is as for cp_parser_gnu_attribute_list. */
24891 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24893 tree attributes
= NULL_TREE
;
24898 tree attribute_list
;
24901 /* Peek at the next token. */
24902 token
= cp_lexer_peek_token (parser
->lexer
);
24903 /* If it's not `__attribute__', then we're done. */
24904 if (token
->keyword
!= RID_ATTRIBUTE
)
24907 /* Consume the `__attribute__' keyword. */
24908 cp_lexer_consume_token (parser
->lexer
);
24909 /* Look for the two `(' tokens. */
24910 matching_parens outer_parens
;
24911 outer_parens
.require_open (parser
);
24912 matching_parens inner_parens
;
24913 inner_parens
.require_open (parser
);
24915 /* Peek at the next token. */
24916 token
= cp_lexer_peek_token (parser
->lexer
);
24917 if (token
->type
!= CPP_CLOSE_PAREN
)
24918 /* Parse the attribute-list. */
24919 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24921 /* If the next token is a `)', then there is no attribute
24923 attribute_list
= NULL
;
24925 /* Look for the two `)' tokens. */
24926 if (!inner_parens
.require_close (parser
))
24928 if (!outer_parens
.require_close (parser
))
24931 cp_parser_skip_to_end_of_statement (parser
);
24933 /* Add these new attributes to the list. */
24934 attributes
= chainon (attributes
, attribute_list
);
24940 /* Parse a GNU attribute-list.
24944 attribute-list , attribute
24948 identifier ( identifier )
24949 identifier ( identifier , expression-list )
24950 identifier ( expression-list )
24952 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24953 to an attribute. The TREE_PURPOSE of each node is the identifier
24954 indicating which attribute is in use. The TREE_VALUE represents
24955 the arguments, if any. */
24958 cp_parser_gnu_attribute_list (cp_parser
* parser
)
24960 tree attribute_list
= NULL_TREE
;
24961 bool save_translate_strings_p
= parser
->translate_strings_p
;
24963 parser
->translate_strings_p
= false;
24970 /* Look for the identifier. We also allow keywords here; for
24971 example `__attribute__ ((const))' is legal. */
24972 token
= cp_lexer_peek_token (parser
->lexer
);
24973 if (token
->type
== CPP_NAME
24974 || token
->type
== CPP_KEYWORD
)
24976 tree arguments
= NULL_TREE
;
24978 /* Consume the token, but save it since we need it for the
24979 SIMD enabled function parsing. */
24980 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
24982 /* Save away the identifier that indicates which attribute
24984 identifier
= (token
->type
== CPP_KEYWORD
)
24985 /* For keywords, use the canonical spelling, not the
24986 parsed identifier. */
24987 ? ridpointers
[(int) token
->keyword
]
24988 : id_token
->u
.value
;
24990 identifier
= canonicalize_attr_name (identifier
);
24991 attribute
= build_tree_list (identifier
, NULL_TREE
);
24993 /* Peek at the next token. */
24994 token
= cp_lexer_peek_token (parser
->lexer
);
24995 /* If it's an `(', then parse the attribute arguments. */
24996 if (token
->type
== CPP_OPEN_PAREN
)
24998 vec
<tree
, va_gc
> *vec
;
24999 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25000 ? id_attr
: normal_attr
);
25001 vec
= cp_parser_parenthesized_expression_list
25002 (parser
, attr_flag
, /*cast_p=*/false,
25003 /*allow_expansion_p=*/false,
25004 /*non_constant_p=*/NULL
);
25006 arguments
= error_mark_node
;
25009 arguments
= build_tree_list_vec (vec
);
25010 release_tree_vector (vec
);
25012 /* Save the arguments away. */
25013 TREE_VALUE (attribute
) = arguments
;
25016 if (arguments
!= error_mark_node
)
25018 /* Add this attribute to the list. */
25019 TREE_CHAIN (attribute
) = attribute_list
;
25020 attribute_list
= attribute
;
25023 token
= cp_lexer_peek_token (parser
->lexer
);
25025 /* Now, look for more attributes. If the next token isn't a
25026 `,', we're done. */
25027 if (token
->type
!= CPP_COMMA
)
25030 /* Consume the comma and keep going. */
25031 cp_lexer_consume_token (parser
->lexer
);
25033 parser
->translate_strings_p
= save_translate_strings_p
;
25035 /* We built up the list in reverse order. */
25036 return nreverse (attribute_list
);
25039 /* Parse a standard C++11 attribute.
25041 The returned representation is a TREE_LIST which TREE_PURPOSE is
25042 the scoped name of the attribute, and the TREE_VALUE is its
25045 Note that the scoped name of the attribute is itself a TREE_LIST
25046 which TREE_PURPOSE is the namespace of the attribute, and
25047 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25048 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25049 and which TREE_PURPOSE is directly the attribute name.
25051 Clients of the attribute code should use get_attribute_namespace
25052 and get_attribute_name to get the actual namespace and name of
25053 attributes, regardless of their being GNU or C++11 attributes.
25056 attribute-token attribute-argument-clause [opt]
25060 attribute-scoped-token
25062 attribute-scoped-token:
25063 attribute-namespace :: identifier
25065 attribute-namespace:
25068 attribute-argument-clause:
25069 ( balanced-token-seq )
25071 balanced-token-seq:
25072 balanced-token [opt]
25073 balanced-token-seq balanced-token
25076 ( balanced-token-seq )
25077 [ balanced-token-seq ]
25078 { balanced-token-seq }. */
25081 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25083 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25086 /* First, parse name of the attribute, a.k.a attribute-token. */
25088 token
= cp_lexer_peek_token (parser
->lexer
);
25089 if (token
->type
== CPP_NAME
)
25090 attr_id
= token
->u
.value
;
25091 else if (token
->type
== CPP_KEYWORD
)
25092 attr_id
= ridpointers
[(int) token
->keyword
];
25093 else if (token
->flags
& NAMED_OP
)
25094 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25096 if (attr_id
== NULL_TREE
)
25099 cp_lexer_consume_token (parser
->lexer
);
25101 token
= cp_lexer_peek_token (parser
->lexer
);
25102 if (token
->type
== CPP_SCOPE
)
25104 /* We are seeing a scoped attribute token. */
25106 cp_lexer_consume_token (parser
->lexer
);
25108 error_at (token
->location
, "attribute using prefix used together "
25109 "with scoped attribute token");
25112 token
= cp_lexer_consume_token (parser
->lexer
);
25113 if (token
->type
== CPP_NAME
)
25114 attr_id
= token
->u
.value
;
25115 else if (token
->type
== CPP_KEYWORD
)
25116 attr_id
= ridpointers
[(int) token
->keyword
];
25117 else if (token
->flags
& NAMED_OP
)
25118 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25121 error_at (token
->location
,
25122 "expected an identifier for the attribute name");
25123 return error_mark_node
;
25126 attr_id
= canonicalize_attr_name (attr_id
);
25127 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25129 token
= cp_lexer_peek_token (parser
->lexer
);
25132 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25136 attr_id
= canonicalize_attr_name (attr_id
);
25137 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25139 /* C++11 noreturn attribute is equivalent to GNU's. */
25140 if (is_attribute_p ("noreturn", attr_id
))
25141 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25142 /* C++14 deprecated attribute is equivalent to GNU's. */
25143 else if (is_attribute_p ("deprecated", attr_id
))
25144 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25145 /* C++17 fallthrough attribute is equivalent to GNU's. */
25146 else if (is_attribute_p ("fallthrough", attr_id
))
25147 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25148 /* Transactional Memory TS optimize_for_synchronized attribute is
25149 equivalent to GNU transaction_callable. */
25150 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25151 TREE_PURPOSE (attribute
)
25152 = get_identifier ("transaction_callable");
25153 /* Transactional Memory attributes are GNU attributes. */
25154 else if (tm_attr_to_mask (attr_id
))
25155 TREE_PURPOSE (attribute
) = attr_id
;
25158 /* Now parse the optional argument clause of the attribute. */
25160 if (token
->type
!= CPP_OPEN_PAREN
)
25164 vec
<tree
, va_gc
> *vec
;
25165 int attr_flag
= normal_attr
;
25167 if (attr_ns
== get_identifier ("gnu")
25168 && attribute_takes_identifier_p (attr_id
))
25169 /* A GNU attribute that takes an identifier in parameter. */
25170 attr_flag
= id_attr
;
25172 vec
= cp_parser_parenthesized_expression_list
25173 (parser
, attr_flag
, /*cast_p=*/false,
25174 /*allow_expansion_p=*/true,
25175 /*non_constant_p=*/NULL
);
25177 arguments
= error_mark_node
;
25180 arguments
= build_tree_list_vec (vec
);
25181 release_tree_vector (vec
);
25184 if (arguments
== error_mark_node
)
25185 attribute
= error_mark_node
;
25187 TREE_VALUE (attribute
) = arguments
;
25193 /* Check that the attribute ATTRIBUTE appears at most once in the
25194 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25195 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25196 isn't implemented yet in GCC. */
25199 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25203 tree name
= get_attribute_name (attribute
);
25204 if (is_attribute_p ("noreturn", name
)
25205 && lookup_attribute ("noreturn", attributes
))
25206 error ("attribute %<noreturn%> can appear at most once "
25207 "in an attribute-list");
25208 else if (is_attribute_p ("deprecated", name
)
25209 && lookup_attribute ("deprecated", attributes
))
25210 error ("attribute %<deprecated%> can appear at most once "
25211 "in an attribute-list");
25215 /* Parse a list of standard C++-11 attributes.
25219 attribute-list , attribute[opt]
25221 attribute-list , attribute ...
25225 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25227 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25228 cp_token
*token
= NULL
;
25232 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25233 if (attribute
== error_mark_node
)
25235 if (attribute
!= NULL_TREE
)
25237 cp_parser_check_std_attribute (attributes
, attribute
);
25238 TREE_CHAIN (attribute
) = attributes
;
25239 attributes
= attribute
;
25241 token
= cp_lexer_peek_token (parser
->lexer
);
25242 if (token
->type
== CPP_ELLIPSIS
)
25244 cp_lexer_consume_token (parser
->lexer
);
25245 if (attribute
== NULL_TREE
)
25246 error_at (token
->location
,
25247 "expected attribute before %<...%>");
25250 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25251 if (pack
== error_mark_node
)
25252 return error_mark_node
;
25253 TREE_VALUE (attribute
) = pack
;
25255 token
= cp_lexer_peek_token (parser
->lexer
);
25257 if (token
->type
!= CPP_COMMA
)
25259 cp_lexer_consume_token (parser
->lexer
);
25261 attributes
= nreverse (attributes
);
25265 /* Parse a standard C++-11 attribute specifier.
25267 attribute-specifier:
25268 [ [ attribute-using-prefix [opt] attribute-list ] ]
25269 alignment-specifier
25271 attribute-using-prefix:
25272 using attribute-namespace :
25274 alignment-specifier:
25275 alignas ( type-id ... [opt] )
25276 alignas ( alignment-expression ... [opt] ). */
25279 cp_parser_std_attribute_spec (cp_parser
*parser
)
25281 tree attributes
= NULL_TREE
;
25282 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25284 if (token
->type
== CPP_OPEN_SQUARE
25285 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25287 tree attr_ns
= NULL_TREE
;
25289 cp_lexer_consume_token (parser
->lexer
);
25290 cp_lexer_consume_token (parser
->lexer
);
25292 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25294 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25295 if (token
->type
== CPP_NAME
)
25296 attr_ns
= token
->u
.value
;
25297 else if (token
->type
== CPP_KEYWORD
)
25298 attr_ns
= ridpointers
[(int) token
->keyword
];
25299 else if (token
->flags
& NAMED_OP
)
25300 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25303 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25305 if (cxx_dialect
< cxx17
25306 && !in_system_header_at (input_location
))
25307 pedwarn (input_location
, 0,
25308 "attribute using prefix only available "
25309 "with -std=c++17 or -std=gnu++17");
25311 cp_lexer_consume_token (parser
->lexer
);
25312 cp_lexer_consume_token (parser
->lexer
);
25313 cp_lexer_consume_token (parser
->lexer
);
25316 attr_ns
= NULL_TREE
;
25319 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25321 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25322 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25323 cp_parser_skip_to_end_of_statement (parser
);
25325 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25326 when we are sure that we have actually parsed them. */
25327 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25333 /* Look for an alignment-specifier. */
25335 token
= cp_lexer_peek_token (parser
->lexer
);
25337 if (token
->type
!= CPP_KEYWORD
25338 || token
->keyword
!= RID_ALIGNAS
)
25341 cp_lexer_consume_token (parser
->lexer
);
25342 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25344 matching_parens parens
;
25345 if (!parens
.require_open (parser
))
25347 cp_parser_error (parser
, "expected %<(%>");
25348 return error_mark_node
;
25351 cp_parser_parse_tentatively (parser
);
25352 alignas_expr
= cp_parser_type_id (parser
);
25354 if (!cp_parser_parse_definitely (parser
))
25356 alignas_expr
= cp_parser_assignment_expression (parser
);
25357 if (alignas_expr
== error_mark_node
)
25358 cp_parser_skip_to_end_of_statement (parser
);
25359 if (alignas_expr
== NULL_TREE
25360 || alignas_expr
== error_mark_node
)
25361 return alignas_expr
;
25364 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25365 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25367 /* Handle alignas (pack...). */
25368 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25370 cp_lexer_consume_token (parser
->lexer
);
25371 alignas_expr
= make_pack_expansion (alignas_expr
);
25374 /* Something went wrong, so don't build the attribute. */
25375 if (alignas_expr
== error_mark_node
)
25376 return error_mark_node
;
25378 if (!parens
.require_close (parser
))
25380 cp_parser_error (parser
, "expected %<)%>");
25381 return error_mark_node
;
25384 /* Build the C++-11 representation of an 'aligned'
25387 build_tree_list (build_tree_list (get_identifier ("gnu"),
25388 get_identifier ("aligned")),
25395 /* Parse a standard C++-11 attribute-specifier-seq.
25397 attribute-specifier-seq:
25398 attribute-specifier-seq [opt] attribute-specifier
25402 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25404 tree attr_specs
= NULL_TREE
;
25405 tree attr_last
= NULL_TREE
;
25409 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25410 if (attr_spec
== NULL_TREE
)
25412 if (attr_spec
== error_mark_node
)
25413 return error_mark_node
;
25416 TREE_CHAIN (attr_last
) = attr_spec
;
25418 attr_specs
= attr_last
= attr_spec
;
25419 attr_last
= tree_last (attr_last
);
25425 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25426 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25427 current value of the PEDANTIC flag, regardless of whether or not
25428 the `__extension__' keyword is present. The caller is responsible
25429 for restoring the value of the PEDANTIC flag. */
25432 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25434 /* Save the old value of the PEDANTIC flag. */
25435 *saved_pedantic
= pedantic
;
25437 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25439 /* Consume the `__extension__' token. */
25440 cp_lexer_consume_token (parser
->lexer
);
25441 /* We're not being pedantic while the `__extension__' keyword is
25451 /* Parse a label declaration.
25454 __label__ label-declarator-seq ;
25456 label-declarator-seq:
25457 identifier , label-declarator-seq
25461 cp_parser_label_declaration (cp_parser
* parser
)
25463 /* Look for the `__label__' keyword. */
25464 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25470 /* Look for an identifier. */
25471 identifier
= cp_parser_identifier (parser
);
25472 /* If we failed, stop. */
25473 if (identifier
== error_mark_node
)
25475 /* Declare it as a label. */
25476 finish_label_decl (identifier
);
25477 /* If the next token is a `;', stop. */
25478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25480 /* Look for the `,' separating the label declarations. */
25481 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25484 /* Look for the final `;'. */
25485 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25488 // -------------------------------------------------------------------------- //
25491 // Parse a requires clause.
25493 // requires-clause:
25494 // 'requires' logical-or-expression
25496 // The required logical-or-expression must be a constant expression. Note
25497 // that we don't check that the expression is constepxr here. We defer until
25498 // we analyze constraints and then, we only check atomic constraints.
25500 cp_parser_requires_clause (cp_parser
*parser
)
25502 // Parse the requires clause so that it is not automatically folded.
25503 ++processing_template_decl
;
25504 tree expr
= cp_parser_binary_expression (parser
, false, false,
25505 PREC_NOT_OPERATOR
, NULL
);
25506 if (check_for_bare_parameter_packs (expr
))
25507 expr
= error_mark_node
;
25508 --processing_template_decl
;
25512 // Optionally parse a requires clause:
25514 cp_parser_requires_clause_opt (cp_parser
*parser
)
25516 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25517 if (tok
->keyword
!= RID_REQUIRES
)
25519 if (!flag_concepts
&& tok
->type
== CPP_NAME
25520 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25522 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25523 "%<requires%> only available with -fconcepts");
25524 /* Parse and discard the requires-clause. */
25525 cp_lexer_consume_token (parser
->lexer
);
25526 cp_parser_requires_clause (parser
);
25530 cp_lexer_consume_token (parser
->lexer
);
25531 return cp_parser_requires_clause (parser
);
25535 /*---------------------------------------------------------------------------
25536 Requires expressions
25537 ---------------------------------------------------------------------------*/
25539 /* Parse a requires expression
25541 requirement-expression:
25542 'requires' requirement-parameter-list [opt] requirement-body */
25544 cp_parser_requires_expression (cp_parser
*parser
)
25546 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25547 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25549 /* A requires-expression shall appear only within a concept
25550 definition or a requires-clause.
25552 TODO: Implement this diagnostic correctly. */
25553 if (!processing_template_decl
)
25555 error_at (loc
, "a requires expression cannot appear outside a template");
25556 cp_parser_skip_to_end_of_statement (parser
);
25557 return error_mark_node
;
25562 /* Local parameters are delared as variables within the scope
25563 of the expression. They are not visible past the end of
25564 the expression. Expressions within the requires-expression
25565 are unevaluated. */
25566 struct scope_sentinel
25570 ++cp_unevaluated_operand
;
25571 begin_scope (sk_block
, NULL_TREE
);
25576 pop_bindings_and_leave_scope ();
25577 --cp_unevaluated_operand
;
25581 /* Parse the optional parameter list. */
25582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25584 parms
= cp_parser_requirement_parameter_list (parser
);
25585 if (parms
== error_mark_node
)
25586 return error_mark_node
;
25591 /* Parse the requirement body. */
25592 reqs
= cp_parser_requirement_body (parser
);
25593 if (reqs
== error_mark_node
)
25594 return error_mark_node
;
25597 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25599 grokparms (parms
, &parms
);
25600 return finish_requires_expr (parms
, reqs
);
25603 /* Parse a parameterized requirement.
25605 requirement-parameter-list:
25606 '(' parameter-declaration-clause ')' */
25608 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25610 matching_parens parens
;
25611 if (!parens
.require_open (parser
))
25612 return error_mark_node
;
25614 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25616 if (!parens
.require_close (parser
))
25617 return error_mark_node
;
25622 /* Parse the body of a requirement.
25625 '{' requirement-list '}' */
25627 cp_parser_requirement_body (cp_parser
*parser
)
25629 matching_braces braces
;
25630 if (!braces
.require_open (parser
))
25631 return error_mark_node
;
25633 tree reqs
= cp_parser_requirement_list (parser
);
25635 if (!braces
.require_close (parser
))
25636 return error_mark_node
;
25641 /* Parse a list of requirements.
25645 requirement-list ';' requirement[opt] */
25647 cp_parser_requirement_list (cp_parser
*parser
)
25649 tree result
= NULL_TREE
;
25652 tree req
= cp_parser_requirement (parser
);
25653 if (req
== error_mark_node
)
25654 return error_mark_node
;
25656 result
= tree_cons (NULL_TREE
, req
, result
);
25658 /* If we see a semi-colon, consume it. */
25659 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25660 cp_lexer_consume_token (parser
->lexer
);
25662 /* Stop processing at the end of the list. */
25663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25667 /* Reverse the order of requirements so they are analyzed in
25668 declaration order. */
25669 return nreverse (result
);
25672 /* Parse a syntactic requirement or type requirement.
25676 compound-requirement
25678 nested-requirement */
25680 cp_parser_requirement (cp_parser
*parser
)
25682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25683 return cp_parser_compound_requirement (parser
);
25684 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25685 return cp_parser_type_requirement (parser
);
25686 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25687 return cp_parser_nested_requirement (parser
);
25689 return cp_parser_simple_requirement (parser
);
25692 /* Parse a simple requirement.
25694 simple-requirement:
25697 cp_parser_simple_requirement (cp_parser
*parser
)
25699 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25700 if (!expr
|| expr
== error_mark_node
)
25701 return error_mark_node
;
25703 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25704 return error_mark_node
;
25706 return finish_simple_requirement (expr
);
25709 /* Parse a type requirement
25712 nested-name-specifier [opt] required-type-name ';'
25714 required-type-name:
25716 'template' [opt] simple-template-id */
25718 cp_parser_type_requirement (cp_parser
*parser
)
25720 cp_lexer_consume_token (parser
->lexer
);
25722 // Save the scope before parsing name specifiers.
25723 tree saved_scope
= parser
->scope
;
25724 tree saved_object_scope
= parser
->object_scope
;
25725 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25726 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25727 cp_parser_nested_name_specifier_opt (parser
,
25728 /*typename_keyword_p=*/true,
25729 /*check_dependency_p=*/false,
25731 /*is_declaration=*/false);
25734 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25736 cp_lexer_consume_token (parser
->lexer
);
25737 type
= cp_parser_template_id (parser
,
25738 /*template_keyword_p=*/true,
25739 /*check_dependency=*/false,
25740 /*tag_type=*/none_type
,
25741 /*is_declaration=*/false);
25742 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25743 /*complain=*/tf_error
);
25746 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25748 if (TREE_CODE (type
) == TYPE_DECL
)
25749 type
= TREE_TYPE (type
);
25751 parser
->scope
= saved_scope
;
25752 parser
->object_scope
= saved_object_scope
;
25753 parser
->qualifying_scope
= saved_qualifying_scope
;
25755 if (type
== error_mark_node
)
25756 cp_parser_skip_to_end_of_statement (parser
);
25758 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25759 return error_mark_node
;
25760 if (type
== error_mark_node
)
25761 return error_mark_node
;
25763 return finish_type_requirement (type
);
25766 /* Parse a compound requirement
25768 compound-requirement:
25769 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25771 cp_parser_compound_requirement (cp_parser
*parser
)
25773 /* Parse an expression enclosed in '{ }'s. */
25774 matching_braces braces
;
25775 if (!braces
.require_open (parser
))
25776 return error_mark_node
;
25778 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25779 if (!expr
|| expr
== error_mark_node
)
25780 return error_mark_node
;
25782 if (!braces
.require_close (parser
))
25783 return error_mark_node
;
25785 /* Parse the optional noexcept. */
25786 bool noexcept_p
= false;
25787 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25789 cp_lexer_consume_token (parser
->lexer
);
25793 /* Parse the optional trailing return type. */
25794 tree type
= NULL_TREE
;
25795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25797 cp_lexer_consume_token (parser
->lexer
);
25798 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25799 parser
->in_result_type_constraint_p
= true;
25800 type
= cp_parser_trailing_type_id (parser
);
25801 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25802 if (type
== error_mark_node
)
25803 return error_mark_node
;
25806 return finish_compound_requirement (expr
, type
, noexcept_p
);
25809 /* Parse a nested requirement. This is the same as a requires clause.
25811 nested-requirement:
25814 cp_parser_nested_requirement (cp_parser
*parser
)
25816 cp_lexer_consume_token (parser
->lexer
);
25817 tree req
= cp_parser_requires_clause (parser
);
25818 if (req
== error_mark_node
)
25819 return error_mark_node
;
25820 return finish_nested_requirement (req
);
25823 /* Support Functions */
25825 /* Return the appropriate prefer_type argument for lookup_name_real based on
25826 tag_type and template_mem_access. */
25829 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25831 /* DR 141: When looking in the current enclosing context for a template-name
25832 after -> or ., only consider class templates. */
25833 if (template_mem_access
)
25837 case none_type
: return 0; // No preference.
25838 case scope_type
: return 1; // Type or namespace.
25839 default: return 2; // Type only.
25843 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25844 NAME should have one of the representations used for an
25845 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25846 is returned. If PARSER->SCOPE is a dependent type, then a
25847 SCOPE_REF is returned.
25849 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25850 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25851 was formed. Abstractly, such entities should not be passed to this
25852 function, because they do not need to be looked up, but it is
25853 simpler to check for this special case here, rather than at the
25856 In cases not explicitly covered above, this function returns a
25857 DECL, OVERLOAD, or baselink representing the result of the lookup.
25858 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25861 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25862 (e.g., "struct") that was used. In that case bindings that do not
25863 refer to types are ignored.
25865 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25868 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25871 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25874 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25875 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25876 NULL_TREE otherwise. */
25879 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
25880 enum tag_types tag_type
,
25883 bool check_dependency
,
25884 tree
*ambiguous_decls
,
25885 location_t name_location
)
25888 tree object_type
= parser
->context
->object_type
;
25890 /* Assume that the lookup will be unambiguous. */
25891 if (ambiguous_decls
)
25892 *ambiguous_decls
= NULL_TREE
;
25894 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25895 no longer valid. Note that if we are parsing tentatively, and
25896 the parse fails, OBJECT_TYPE will be automatically restored. */
25897 parser
->context
->object_type
= NULL_TREE
;
25899 if (name
== error_mark_node
)
25900 return error_mark_node
;
25902 /* A template-id has already been resolved; there is no lookup to
25904 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
25906 if (BASELINK_P (name
))
25908 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
25909 == TEMPLATE_ID_EXPR
);
25913 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25914 it should already have been checked to make sure that the name
25915 used matches the type being destroyed. */
25916 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
25920 /* Figure out to which type this destructor applies. */
25922 type
= parser
->scope
;
25923 else if (object_type
)
25924 type
= object_type
;
25926 type
= current_class_type
;
25927 /* If that's not a class type, there is no destructor. */
25928 if (!type
|| !CLASS_TYPE_P (type
))
25929 return error_mark_node
;
25931 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
25932 lazily_declare_fn (sfk_destructor
, type
);
25934 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
25937 return error_mark_node
;
25940 /* By this point, the NAME should be an ordinary identifier. If
25941 the id-expression was a qualified name, the qualifying scope is
25942 stored in PARSER->SCOPE at this point. */
25943 gcc_assert (identifier_p (name
));
25945 /* Perform the lookup. */
25950 if (parser
->scope
== error_mark_node
)
25951 return error_mark_node
;
25953 /* If the SCOPE is dependent, the lookup must be deferred until
25954 the template is instantiated -- unless we are explicitly
25955 looking up names in uninstantiated templates. Even then, we
25956 cannot look up the name if the scope is not a class type; it
25957 might, for example, be a template type parameter. */
25958 dependent_p
= (TYPE_P (parser
->scope
)
25959 && dependent_scope_p (parser
->scope
));
25960 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
25962 /* Defer lookup. */
25963 decl
= error_mark_node
;
25966 tree pushed_scope
= NULL_TREE
;
25968 /* If PARSER->SCOPE is a dependent type, then it must be a
25969 class type, and we must not be checking dependencies;
25970 otherwise, we would have processed this lookup above. So
25971 that PARSER->SCOPE is not considered a dependent base by
25972 lookup_member, we must enter the scope here. */
25974 pushed_scope
= push_scope (parser
->scope
);
25976 /* If the PARSER->SCOPE is a template specialization, it
25977 may be instantiated during name lookup. In that case,
25978 errors may be issued. Even if we rollback the current
25979 tentative parse, those errors are valid. */
25980 decl
= lookup_qualified_name (parser
->scope
, name
,
25981 prefer_type_arg (tag_type
),
25982 /*complain=*/true);
25984 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25985 lookup result and the nested-name-specifier nominates a class C:
25986 * if the name specified after the nested-name-specifier, when
25987 looked up in C, is the injected-class-name of C (Clause 9), or
25988 * if the name specified after the nested-name-specifier is the
25989 same as the identifier or the simple-template-id's template-
25990 name in the last component of the nested-name-specifier,
25991 the name is instead considered to name the constructor of
25992 class C. [ Note: for example, the constructor is not an
25993 acceptable lookup result in an elaborated-type-specifier so
25994 the constructor would not be used in place of the
25995 injected-class-name. --end note ] Such a constructor name
25996 shall be used only in the declarator-id of a declaration that
25997 names a constructor or in a using-declaration. */
25998 if (tag_type
== none_type
25999 && DECL_SELF_REFERENCE_P (decl
)
26000 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26001 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26002 prefer_type_arg (tag_type
),
26003 /*complain=*/true);
26005 /* If we have a single function from a using decl, pull it out. */
26006 if (TREE_CODE (decl
) == OVERLOAD
26007 && !really_overloaded_fn (decl
))
26008 decl
= OVL_FUNCTION (decl
);
26011 pop_scope (pushed_scope
);
26014 /* If the scope is a dependent type and either we deferred lookup or
26015 we did lookup but didn't find the name, rememeber the name. */
26016 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26017 && dependent_type_p (parser
->scope
))
26023 /* The resolution to Core Issue 180 says that `struct
26024 A::B' should be considered a type-name, even if `A'
26026 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26027 /*complain=*/tf_error
);
26028 if (type
!= error_mark_node
)
26029 decl
= TYPE_NAME (type
);
26031 else if (is_template
26032 && (cp_parser_next_token_ends_template_argument_p (parser
)
26033 || cp_lexer_next_token_is (parser
->lexer
,
26035 decl
= make_unbound_class_template (parser
->scope
,
26037 /*complain=*/tf_error
);
26039 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26040 parser
->scope
, name
,
26043 parser
->qualifying_scope
= parser
->scope
;
26044 parser
->object_scope
= NULL_TREE
;
26046 else if (object_type
)
26048 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26049 OBJECT_TYPE is not a class. */
26050 if (CLASS_TYPE_P (object_type
))
26051 /* If the OBJECT_TYPE is a template specialization, it may
26052 be instantiated during name lookup. In that case, errors
26053 may be issued. Even if we rollback the current tentative
26054 parse, those errors are valid. */
26055 decl
= lookup_member (object_type
,
26058 prefer_type_arg (tag_type
),
26059 tf_warning_or_error
);
26064 /* Look it up in the enclosing context. DR 141: When looking for a
26065 template-name after -> or ., only consider class templates. */
26066 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26068 /*block_p=*/true, is_namespace
, 0);
26069 if (object_type
== unknown_type_node
)
26070 /* The object is type-dependent, so we can't look anything up; we used
26071 this to get the DR 141 behavior. */
26072 object_type
= NULL_TREE
;
26073 parser
->object_scope
= object_type
;
26074 parser
->qualifying_scope
= NULL_TREE
;
26078 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26080 /*block_p=*/true, is_namespace
, 0);
26081 parser
->qualifying_scope
= NULL_TREE
;
26082 parser
->object_scope
= NULL_TREE
;
26085 /* If the lookup failed, let our caller know. */
26086 if (!decl
|| decl
== error_mark_node
)
26087 return error_mark_node
;
26089 /* Pull out the template from an injected-class-name (or multiple). */
26091 decl
= maybe_get_template_decl_from_type_decl (decl
);
26093 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26094 if (TREE_CODE (decl
) == TREE_LIST
)
26096 if (ambiguous_decls
)
26097 *ambiguous_decls
= decl
;
26098 /* The error message we have to print is too complicated for
26099 cp_parser_error, so we incorporate its actions directly. */
26100 if (!cp_parser_simulate_error (parser
))
26102 error_at (name_location
, "reference to %qD is ambiguous",
26104 print_candidates (decl
);
26106 return error_mark_node
;
26109 gcc_assert (DECL_P (decl
)
26110 || TREE_CODE (decl
) == OVERLOAD
26111 || TREE_CODE (decl
) == SCOPE_REF
26112 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26113 || BASELINK_P (decl
));
26115 /* If we have resolved the name of a member declaration, check to
26116 see if the declaration is accessible. When the name resolves to
26117 set of overloaded functions, accessibility is checked when
26118 overload resolution is done.
26120 During an explicit instantiation, access is not checked at all,
26121 as per [temp.explicit]. */
26123 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26125 maybe_record_typedef_use (decl
);
26127 return cp_expr (decl
, name_location
);
26130 /* Like cp_parser_lookup_name, but for use in the typical case where
26131 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26132 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26135 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26137 return cp_parser_lookup_name (parser
, name
,
26139 /*is_template=*/false,
26140 /*is_namespace=*/false,
26141 /*check_dependency=*/true,
26142 /*ambiguous_decls=*/NULL
,
26146 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26147 the current context, return the TYPE_DECL. If TAG_NAME_P is
26148 true, the DECL indicates the class being defined in a class-head,
26149 or declared in an elaborated-type-specifier.
26151 Otherwise, return DECL. */
26154 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26156 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26157 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26160 template <typename T> struct B;
26163 template <typename T> struct A::B {};
26165 Similarly, in an elaborated-type-specifier:
26167 namespace N { struct X{}; }
26170 template <typename T> friend struct N::X;
26173 However, if the DECL refers to a class type, and we are in
26174 the scope of the class, then the name lookup automatically
26175 finds the TYPE_DECL created by build_self_reference rather
26176 than a TEMPLATE_DECL. For example, in:
26178 template <class T> struct S {
26182 there is no need to handle such case. */
26184 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26185 return DECL_TEMPLATE_RESULT (decl
);
26190 /* If too many, or too few, template-parameter lists apply to the
26191 declarator, issue an error message. Returns TRUE if all went well,
26192 and FALSE otherwise. */
26195 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26196 cp_declarator
*declarator
,
26197 location_t declarator_location
)
26199 switch (declarator
->kind
)
26203 unsigned num_templates
= 0;
26204 tree scope
= declarator
->u
.id
.qualifying_scope
;
26207 num_templates
= num_template_headers_for_class (scope
);
26208 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26209 == TEMPLATE_ID_EXPR
)
26210 /* If the DECLARATOR has the form `X<y>' then it uses one
26211 additional level of template parameters. */
26214 return cp_parser_check_template_parameters
26215 (parser
, num_templates
, declarator_location
, declarator
);
26221 case cdk_reference
:
26223 return (cp_parser_check_declarator_template_parameters
26224 (parser
, declarator
->declarator
, declarator_location
));
26231 gcc_unreachable ();
26236 /* NUM_TEMPLATES were used in the current declaration. If that is
26237 invalid, return FALSE and issue an error messages. Otherwise,
26238 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26239 declarator and we can print more accurate diagnostics. */
26242 cp_parser_check_template_parameters (cp_parser
* parser
,
26243 unsigned num_templates
,
26244 location_t location
,
26245 cp_declarator
*declarator
)
26247 /* If there are the same number of template classes and parameter
26248 lists, that's OK. */
26249 if (parser
->num_template_parameter_lists
== num_templates
)
26251 /* If there are more, but only one more, then we are referring to a
26252 member template. That's OK too. */
26253 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26255 /* If there are more template classes than parameter lists, we have
26258 template <class T> void S<T>::R<T>::f (); */
26259 if (parser
->num_template_parameter_lists
< num_templates
)
26261 if (declarator
&& !current_function_decl
)
26262 error_at (location
, "specializing member %<%T::%E%> "
26263 "requires %<template<>%> syntax",
26264 declarator
->u
.id
.qualifying_scope
,
26265 declarator
->u
.id
.unqualified_name
);
26266 else if (declarator
)
26267 error_at (location
, "invalid declaration of %<%T::%E%>",
26268 declarator
->u
.id
.qualifying_scope
,
26269 declarator
->u
.id
.unqualified_name
);
26271 error_at (location
, "too few template-parameter-lists");
26274 /* Otherwise, there are too many template parameter lists. We have
26277 template <class T> template <class U> void S::f(); */
26278 error_at (location
, "too many template-parameter-lists");
26282 /* Parse an optional `::' token indicating that the following name is
26283 from the global namespace. If so, PARSER->SCOPE is set to the
26284 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26285 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26286 Returns the new value of PARSER->SCOPE, if the `::' token is
26287 present, and NULL_TREE otherwise. */
26290 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26294 /* Peek at the next token. */
26295 token
= cp_lexer_peek_token (parser
->lexer
);
26296 /* If we're looking at a `::' token then we're starting from the
26297 global namespace, not our current location. */
26298 if (token
->type
== CPP_SCOPE
)
26300 /* Consume the `::' token. */
26301 cp_lexer_consume_token (parser
->lexer
);
26302 /* Set the SCOPE so that we know where to start the lookup. */
26303 parser
->scope
= global_namespace
;
26304 parser
->qualifying_scope
= global_namespace
;
26305 parser
->object_scope
= NULL_TREE
;
26307 return parser
->scope
;
26309 else if (!current_scope_valid_p
)
26311 parser
->scope
= NULL_TREE
;
26312 parser
->qualifying_scope
= NULL_TREE
;
26313 parser
->object_scope
= NULL_TREE
;
26319 /* Returns TRUE if the upcoming token sequence is the start of a
26320 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26321 declarator is preceded by the `friend' specifier. */
26324 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26326 bool constructor_p
;
26327 bool outside_class_specifier_p
;
26328 tree nested_name_specifier
;
26329 cp_token
*next_token
;
26331 /* The common case is that this is not a constructor declarator, so
26332 try to avoid doing lots of work if at all possible. It's not
26333 valid declare a constructor at function scope. */
26334 if (parser
->in_function_body
)
26336 /* And only certain tokens can begin a constructor declarator. */
26337 next_token
= cp_lexer_peek_token (parser
->lexer
);
26338 if (next_token
->type
!= CPP_NAME
26339 && next_token
->type
!= CPP_SCOPE
26340 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26341 && next_token
->type
!= CPP_TEMPLATE_ID
)
26344 /* Parse tentatively; we are going to roll back all of the tokens
26346 cp_parser_parse_tentatively (parser
);
26347 /* Assume that we are looking at a constructor declarator. */
26348 constructor_p
= true;
26350 /* Look for the optional `::' operator. */
26351 cp_parser_global_scope_opt (parser
,
26352 /*current_scope_valid_p=*/false);
26353 /* Look for the nested-name-specifier. */
26354 nested_name_specifier
26355 = (cp_parser_nested_name_specifier_opt (parser
,
26356 /*typename_keyword_p=*/false,
26357 /*check_dependency_p=*/false,
26359 /*is_declaration=*/false));
26361 outside_class_specifier_p
= (!at_class_scope_p ()
26362 || !TYPE_BEING_DEFINED (current_class_type
)
26365 /* Outside of a class-specifier, there must be a
26366 nested-name-specifier. Except in C++17 mode, where we
26367 might be declaring a guiding declaration. */
26368 if (!nested_name_specifier
&& outside_class_specifier_p
26369 && cxx_dialect
< cxx17
)
26370 constructor_p
= false;
26371 else if (nested_name_specifier
== error_mark_node
)
26372 constructor_p
= false;
26374 /* If we have a class scope, this is easy; DR 147 says that S::S always
26375 names the constructor, and no other qualified name could. */
26376 if (constructor_p
&& nested_name_specifier
26377 && CLASS_TYPE_P (nested_name_specifier
))
26379 tree id
= cp_parser_unqualified_id (parser
,
26380 /*template_keyword_p=*/false,
26381 /*check_dependency_p=*/false,
26382 /*declarator_p=*/true,
26383 /*optional_p=*/false);
26384 if (is_overloaded_fn (id
))
26385 id
= DECL_NAME (get_first_fn (id
));
26386 if (!constructor_name_p (id
, nested_name_specifier
))
26387 constructor_p
= false;
26389 /* If we still think that this might be a constructor-declarator,
26390 look for a class-name. */
26391 else if (constructor_p
)
26395 template <typename T> struct S {
26399 we must recognize that the nested `S' names a class. */
26400 if (cxx_dialect
>= cxx17
)
26401 cp_parser_parse_tentatively (parser
);
26404 type_decl
= cp_parser_class_name (parser
,
26405 /*typename_keyword_p=*/false,
26406 /*template_keyword_p=*/false,
26408 /*check_dependency_p=*/false,
26409 /*class_head_p=*/false,
26410 /*is_declaration=*/false);
26412 if (cxx_dialect
>= cxx17
26413 && !cp_parser_parse_definitely (parser
))
26415 type_decl
= NULL_TREE
;
26416 tree tmpl
= cp_parser_template_name (parser
,
26417 /*template_keyword*/false,
26418 /*check_dependency_p*/false,
26419 /*is_declaration*/false,
26421 /*is_identifier*/NULL
);
26422 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26423 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26424 /* It's a deduction guide, return true. */;
26426 cp_parser_simulate_error (parser
);
26429 /* If there was no class-name, then this is not a constructor.
26430 Otherwise, if we are in a class-specifier and we aren't
26431 handling a friend declaration, check that its type matches
26432 current_class_type (c++/38313). Note: error_mark_node
26433 is left alone for error recovery purposes. */
26434 constructor_p
= (!cp_parser_error_occurred (parser
)
26435 && (outside_class_specifier_p
26436 || type_decl
== NULL_TREE
26437 || type_decl
== error_mark_node
26438 || same_type_p (current_class_type
,
26439 TREE_TYPE (type_decl
))));
26441 /* If we're still considering a constructor, we have to see a `(',
26442 to begin the parameter-declaration-clause, followed by either a
26443 `)', an `...', or a decl-specifier. We need to check for a
26444 type-specifier to avoid being fooled into thinking that:
26448 is a constructor. (It is actually a function named `f' that
26449 takes one parameter (of type `int') and returns a value of type
26452 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26453 constructor_p
= false;
26456 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26457 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26458 /* A parameter declaration begins with a decl-specifier,
26459 which is either the "attribute" keyword, a storage class
26460 specifier, or (usually) a type-specifier. */
26461 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26464 tree pushed_scope
= NULL_TREE
;
26465 unsigned saved_num_template_parameter_lists
;
26467 /* Names appearing in the type-specifier should be looked up
26468 in the scope of the class. */
26469 if (current_class_type
)
26471 else if (type_decl
)
26473 type
= TREE_TYPE (type_decl
);
26474 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26476 type
= resolve_typename_type (type
,
26477 /*only_current_p=*/false);
26478 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26480 cp_parser_abort_tentative_parse (parser
);
26484 pushed_scope
= push_scope (type
);
26487 /* Inside the constructor parameter list, surrounding
26488 template-parameter-lists do not apply. */
26489 saved_num_template_parameter_lists
26490 = parser
->num_template_parameter_lists
;
26491 parser
->num_template_parameter_lists
= 0;
26493 /* Look for the type-specifier. */
26494 cp_parser_type_specifier (parser
,
26495 CP_PARSER_FLAGS_NONE
,
26496 /*decl_specs=*/NULL
,
26497 /*is_declarator=*/true,
26498 /*declares_class_or_enum=*/NULL
,
26499 /*is_cv_qualifier=*/NULL
);
26501 parser
->num_template_parameter_lists
26502 = saved_num_template_parameter_lists
;
26504 /* Leave the scope of the class. */
26506 pop_scope (pushed_scope
);
26508 constructor_p
= !cp_parser_error_occurred (parser
);
26512 /* We did not really want to consume any tokens. */
26513 cp_parser_abort_tentative_parse (parser
);
26515 return constructor_p
;
26518 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26519 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26520 they must be performed once we are in the scope of the function.
26522 Returns the function defined. */
26525 cp_parser_function_definition_from_specifiers_and_declarator
26526 (cp_parser
* parser
,
26527 cp_decl_specifier_seq
*decl_specifiers
,
26529 const cp_declarator
*declarator
)
26534 /* Begin the function-definition. */
26535 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26537 /* The things we're about to see are not directly qualified by any
26538 template headers we've seen thus far. */
26539 reset_specialization ();
26541 /* If there were names looked up in the decl-specifier-seq that we
26542 did not check, check them now. We must wait until we are in the
26543 scope of the function to perform the checks, since the function
26544 might be a friend. */
26545 perform_deferred_access_checks (tf_warning_or_error
);
26549 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26550 parser
->omp_declare_simd
= NULL
;
26551 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26552 parser
->oacc_routine
= NULL
;
26557 /* Skip the entire function. */
26558 cp_parser_skip_to_end_of_block_or_statement (parser
);
26559 fn
= error_mark_node
;
26561 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26563 /* Seen already, skip it. An error message has already been output. */
26564 cp_parser_skip_to_end_of_block_or_statement (parser
);
26565 fn
= current_function_decl
;
26566 current_function_decl
= NULL_TREE
;
26567 /* If this is a function from a class, pop the nested class. */
26568 if (current_class_name
)
26569 pop_nested_class ();
26574 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26575 tv
= TV_PARSE_INLINE
;
26577 tv
= TV_PARSE_FUNC
;
26579 fn
= cp_parser_function_definition_after_declarator (parser
,
26580 /*inline_p=*/false);
26587 /* Parse the part of a function-definition that follows the
26588 declarator. INLINE_P is TRUE iff this function is an inline
26589 function defined within a class-specifier.
26591 Returns the function defined. */
26594 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26598 bool saved_in_unbraced_linkage_specification_p
;
26599 bool saved_in_function_body
;
26600 unsigned saved_num_template_parameter_lists
;
26602 bool fully_implicit_function_template_p
26603 = parser
->fully_implicit_function_template_p
;
26604 parser
->fully_implicit_function_template_p
= false;
26605 tree implicit_template_parms
26606 = parser
->implicit_template_parms
;
26607 parser
->implicit_template_parms
= 0;
26608 cp_binding_level
* implicit_template_scope
26609 = parser
->implicit_template_scope
;
26610 parser
->implicit_template_scope
= 0;
26612 saved_in_function_body
= parser
->in_function_body
;
26613 parser
->in_function_body
= true;
26614 /* If the next token is `return', then the code may be trying to
26615 make use of the "named return value" extension that G++ used to
26617 token
= cp_lexer_peek_token (parser
->lexer
);
26618 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26620 /* Consume the `return' keyword. */
26621 cp_lexer_consume_token (parser
->lexer
);
26622 /* Look for the identifier that indicates what value is to be
26624 cp_parser_identifier (parser
);
26625 /* Issue an error message. */
26626 error_at (token
->location
,
26627 "named return values are no longer supported");
26628 /* Skip tokens until we reach the start of the function body. */
26631 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26632 if (token
->type
== CPP_OPEN_BRACE
26633 || token
->type
== CPP_EOF
26634 || token
->type
== CPP_PRAGMA_EOL
)
26636 cp_lexer_consume_token (parser
->lexer
);
26639 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26640 anything declared inside `f'. */
26641 saved_in_unbraced_linkage_specification_p
26642 = parser
->in_unbraced_linkage_specification_p
;
26643 parser
->in_unbraced_linkage_specification_p
= false;
26644 /* Inside the function, surrounding template-parameter-lists do not
26646 saved_num_template_parameter_lists
26647 = parser
->num_template_parameter_lists
;
26648 parser
->num_template_parameter_lists
= 0;
26650 /* If the next token is `try', `__transaction_atomic', or
26651 `__transaction_relaxed`, then we are looking at either function-try-block
26652 or function-transaction-block. Note that all of these include the
26654 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26655 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26656 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26657 RID_TRANSACTION_RELAXED
))
26658 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26659 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26660 cp_parser_function_try_block (parser
);
26662 cp_parser_ctor_initializer_opt_and_function_body
26663 (parser
, /*in_function_try_block=*/false);
26665 /* Finish the function. */
26666 fn
= finish_function (inline_p
);
26667 /* Generate code for it, if necessary. */
26668 expand_or_defer_fn (fn
);
26669 /* Restore the saved values. */
26670 parser
->in_unbraced_linkage_specification_p
26671 = saved_in_unbraced_linkage_specification_p
;
26672 parser
->num_template_parameter_lists
26673 = saved_num_template_parameter_lists
;
26674 parser
->in_function_body
= saved_in_function_body
;
26676 parser
->fully_implicit_function_template_p
26677 = fully_implicit_function_template_p
;
26678 parser
->implicit_template_parms
26679 = implicit_template_parms
;
26680 parser
->implicit_template_scope
26681 = implicit_template_scope
;
26683 if (parser
->fully_implicit_function_template_p
)
26684 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26689 /* Parse a template-declaration body (following argument list). */
26692 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26693 tree parameter_list
,
26696 tree decl
= NULL_TREE
;
26697 bool friend_p
= false;
26699 /* We just processed one more parameter list. */
26700 ++parser
->num_template_parameter_lists
;
26702 /* Get the deferred access checks from the parameter list. These
26703 will be checked once we know what is being declared, as for a
26704 member template the checks must be performed in the scope of the
26705 class containing the member. */
26706 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26708 /* Tentatively parse for a new template parameter list, which can either be
26709 the template keyword or a template introduction. */
26710 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26712 else if (cxx_dialect
>= cxx11
26713 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26714 decl
= cp_parser_alias_declaration (parser
);
26717 /* There are no access checks when parsing a template, as we do not
26718 know if a specialization will be a friend. */
26719 push_deferring_access_checks (dk_no_check
);
26720 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26721 decl
= cp_parser_single_declaration (parser
,
26724 /*explicit_specialization_p=*/false,
26726 pop_deferring_access_checks ();
26728 /* If this is a member template declaration, let the front
26730 if (member_p
&& !friend_p
&& decl
)
26732 if (TREE_CODE (decl
) == TYPE_DECL
)
26733 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26735 decl
= finish_member_template_decl (decl
);
26737 else if (friend_p
&& decl
26738 && DECL_DECLARES_TYPE_P (decl
))
26739 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26740 /*complain=*/true);
26742 /* We are done with the current parameter list. */
26743 --parser
->num_template_parameter_lists
;
26745 pop_deferring_access_checks ();
26748 finish_template_decl (parameter_list
);
26750 /* Check the template arguments for a literal operator template. */
26752 && DECL_DECLARES_FUNCTION_P (decl
)
26753 && UDLIT_OPER_P (DECL_NAME (decl
)))
26756 if (parameter_list
== NULL_TREE
)
26760 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26761 if (num_parms
== 1)
26763 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26764 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26765 if (TREE_TYPE (parm
) != char_type_node
26766 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26769 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26771 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26772 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26773 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26774 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26775 if (parm
== error_mark_node
26776 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26777 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26785 if (cxx_dialect
>= cxx14
)
26786 error ("literal operator template %qD has invalid parameter list."
26787 " Expected non-type template argument pack <char...>"
26788 " or <typename CharT, CharT...>",
26791 error ("literal operator template %qD has invalid parameter list."
26792 " Expected non-type template argument pack <char...>",
26797 /* Register member declarations. */
26798 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26799 finish_member_declaration (decl
);
26800 /* If DECL is a function template, we must return to parse it later.
26801 (Even though there is no definition, there might be default
26802 arguments that need handling.) */
26803 if (member_p
&& decl
26804 && DECL_DECLARES_FUNCTION_P (decl
))
26805 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26808 /* Parse a template introduction header for a template-declaration. Returns
26809 false if tentative parse fails. */
26812 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26814 cp_parser_parse_tentatively (parser
);
26816 tree saved_scope
= parser
->scope
;
26817 tree saved_object_scope
= parser
->object_scope
;
26818 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26820 /* Look for the optional `::' operator. */
26821 cp_parser_global_scope_opt (parser
,
26822 /*current_scope_valid_p=*/false);
26823 /* Look for the nested-name-specifier. */
26824 cp_parser_nested_name_specifier_opt (parser
,
26825 /*typename_keyword_p=*/false,
26826 /*check_dependency_p=*/true,
26828 /*is_declaration=*/false);
26830 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26831 tree concept_name
= cp_parser_identifier (parser
);
26833 /* Look up the concept for which we will be matching
26834 template parameters. */
26835 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26837 parser
->scope
= saved_scope
;
26838 parser
->object_scope
= saved_object_scope
;
26839 parser
->qualifying_scope
= saved_qualifying_scope
;
26841 if (concept_name
== error_mark_node
)
26842 cp_parser_simulate_error (parser
);
26844 /* Look for opening brace for introduction. */
26845 matching_braces braces
;
26846 braces
.require_open (parser
);
26848 if (!cp_parser_parse_definitely (parser
))
26851 push_deferring_access_checks (dk_deferred
);
26853 /* Build vector of placeholder parameters and grab
26854 matching identifiers. */
26855 tree introduction_list
= cp_parser_introduction_list (parser
);
26857 /* The introduction-list shall not be empty. */
26858 int nargs
= TREE_VEC_LENGTH (introduction_list
);
26861 error ("empty introduction-list");
26865 /* Look for closing brace for introduction. */
26866 if (!braces
.require_close (parser
))
26869 if (tmpl_decl
== error_mark_node
)
26871 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
26876 /* Build and associate the constraint. */
26877 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
26878 if (parms
&& parms
!= error_mark_node
)
26880 cp_parser_template_declaration_after_parameters (parser
, parms
,
26885 error_at (token
->location
, "no matching concept for template-introduction");
26889 /* Parse a normal template-declaration following the template keyword. */
26892 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
26894 tree parameter_list
;
26895 bool need_lang_pop
;
26896 location_t location
= input_location
;
26898 /* Look for the `<' token. */
26899 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
26901 if (at_class_scope_p () && current_function_decl
)
26903 /* 14.5.2.2 [temp.mem]
26905 A local class shall not have member templates. */
26906 error_at (location
,
26907 "invalid declaration of member template in local class");
26908 cp_parser_skip_to_end_of_block_or_statement (parser
);
26913 A template ... shall not have C linkage. */
26914 if (current_lang_name
== lang_name_c
)
26916 error_at (location
, "template with C linkage");
26917 maybe_show_extern_c_location ();
26918 /* Give it C++ linkage to avoid confusing other parts of the
26920 push_lang_context (lang_name_cplusplus
);
26921 need_lang_pop
= true;
26924 need_lang_pop
= false;
26926 /* We cannot perform access checks on the template parameter
26927 declarations until we know what is being declared, just as we
26928 cannot check the decl-specifier list. */
26929 push_deferring_access_checks (dk_deferred
);
26931 /* If the next token is `>', then we have an invalid
26932 specialization. Rather than complain about an invalid template
26933 parameter, issue an error message here. */
26934 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
26936 cp_parser_error (parser
, "invalid explicit specialization");
26937 begin_specialization ();
26938 parameter_list
= NULL_TREE
;
26942 /* Parse the template parameters. */
26943 parameter_list
= cp_parser_template_parameter_list (parser
);
26946 /* Look for the `>'. */
26947 cp_parser_skip_to_end_of_template_parameter_list (parser
);
26949 /* Manage template requirements */
26952 tree reqs
= get_shorthand_constraints (current_template_parms
);
26953 if (tree r
= cp_parser_requires_clause_opt (parser
))
26954 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
26955 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
26958 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
26961 /* For the erroneous case of a template with C linkage, we pushed an
26962 implicit C++ linkage scope; exit that scope now. */
26964 pop_lang_context ();
26967 /* Parse a template-declaration, assuming that the `export' (and
26968 `extern') keywords, if present, has already been scanned. MEMBER_P
26969 is as for cp_parser_template_declaration. */
26972 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
26974 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26976 cp_lexer_consume_token (parser
->lexer
);
26977 cp_parser_explicit_template_declaration (parser
, member_p
);
26980 else if (flag_concepts
)
26981 return cp_parser_template_introduction (parser
, member_p
);
26986 /* Perform the deferred access checks from a template-parameter-list.
26987 CHECKS is a TREE_LIST of access checks, as returned by
26988 get_deferred_access_checks. */
26991 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
26993 ++processing_template_parmlist
;
26994 perform_access_checks (checks
, tf_warning_or_error
);
26995 --processing_template_parmlist
;
26998 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26999 `function-definition' sequence that follows a template header.
27000 If MEMBER_P is true, this declaration appears in a class scope.
27002 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27003 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27006 cp_parser_single_declaration (cp_parser
* parser
,
27007 vec
<deferred_access_check
, va_gc
> *checks
,
27009 bool explicit_specialization_p
,
27012 int declares_class_or_enum
;
27013 tree decl
= NULL_TREE
;
27014 cp_decl_specifier_seq decl_specifiers
;
27015 bool function_definition_p
= false;
27016 cp_token
*decl_spec_token_start
;
27018 /* This function is only used when processing a template
27020 gcc_assert (innermost_scope_kind () == sk_template_parms
27021 || innermost_scope_kind () == sk_template_spec
);
27023 /* Defer access checks until we know what is being declared. */
27024 push_deferring_access_checks (dk_deferred
);
27026 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27028 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27029 cp_parser_decl_specifier_seq (parser
,
27030 CP_PARSER_FLAGS_OPTIONAL
,
27032 &declares_class_or_enum
);
27034 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27036 /* There are no template typedefs. */
27037 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27039 error_at (decl_spec_token_start
->location
,
27040 "template declaration of %<typedef%>");
27041 decl
= error_mark_node
;
27044 /* Gather up the access checks that occurred the
27045 decl-specifier-seq. */
27046 stop_deferring_access_checks ();
27048 /* Check for the declaration of a template class. */
27049 if (declares_class_or_enum
)
27051 if (cp_parser_declares_only_class_p (parser
)
27052 || (declares_class_or_enum
& 2))
27054 // If this is a declaration, but not a definition, associate
27055 // any constraints with the type declaration. Constraints
27056 // are associated with definitions in cp_parser_class_specifier.
27057 if (declares_class_or_enum
== 1)
27058 associate_classtype_constraints (decl_specifiers
.type
);
27060 decl
= shadow_tag (&decl_specifiers
);
27065 friend template <typename T> struct A<T>::B;
27068 A<T>::B will be represented by a TYPENAME_TYPE, and
27069 therefore not recognized by shadow_tag. */
27070 if (friend_p
&& *friend_p
27072 && decl_specifiers
.type
27073 && TYPE_P (decl_specifiers
.type
))
27074 decl
= decl_specifiers
.type
;
27076 if (decl
&& decl
!= error_mark_node
)
27077 decl
= TYPE_NAME (decl
);
27079 decl
= error_mark_node
;
27081 /* Perform access checks for template parameters. */
27082 cp_parser_perform_template_parameter_access_checks (checks
);
27084 /* Give a helpful diagnostic for
27085 template <class T> struct A { } a;
27086 if we aren't already recovering from an error. */
27087 if (!cp_parser_declares_only_class_p (parser
)
27090 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27091 "a class template declaration must not declare "
27093 cp_parser_skip_to_end_of_block_or_statement (parser
);
27099 /* Complain about missing 'typename' or other invalid type names. */
27100 if (!decl_specifiers
.any_type_specifiers_p
27101 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27103 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27104 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27105 the rest of this declaration. */
27106 decl
= error_mark_node
;
27110 /* If it's not a template class, try for a template function. If
27111 the next token is a `;', then this declaration does not declare
27112 anything. But, if there were errors in the decl-specifiers, then
27113 the error might well have come from an attempted class-specifier.
27114 In that case, there's no need to warn about a missing declarator. */
27116 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27117 || decl_specifiers
.type
!= error_mark_node
))
27119 decl
= cp_parser_init_declarator (parser
,
27122 /*function_definition_allowed_p=*/true,
27124 declares_class_or_enum
,
27125 &function_definition_p
,
27128 /* 7.1.1-1 [dcl.stc]
27130 A storage-class-specifier shall not be specified in an explicit
27131 specialization... */
27133 && explicit_specialization_p
27134 && decl_specifiers
.storage_class
!= sc_none
)
27136 error_at (decl_spec_token_start
->location
,
27137 "explicit template specialization cannot have a storage class");
27138 decl
= error_mark_node
;
27141 if (decl
&& VAR_P (decl
))
27142 check_template_variable (decl
);
27145 /* Look for a trailing `;' after the declaration. */
27146 if (!function_definition_p
27147 && (decl
== error_mark_node
27148 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27149 cp_parser_skip_to_end_of_block_or_statement (parser
);
27152 pop_deferring_access_checks ();
27154 /* Clear any current qualification; whatever comes next is the start
27155 of something new. */
27156 parser
->scope
= NULL_TREE
;
27157 parser
->qualifying_scope
= NULL_TREE
;
27158 parser
->object_scope
= NULL_TREE
;
27163 /* Parse a cast-expression that is not the operand of a unary "&". */
27166 cp_parser_simple_cast_expression (cp_parser
*parser
)
27168 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27169 /*cast_p=*/false, /*decltype*/false, NULL
);
27172 /* Parse a functional cast to TYPE. Returns an expression
27173 representing the cast. */
27176 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27178 vec
<tree
, va_gc
> *vec
;
27179 tree expression_list
;
27183 location_t start_loc
= input_location
;
27186 type
= error_mark_node
;
27188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27190 cp_lexer_set_source_position (parser
->lexer
);
27191 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27192 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27193 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27194 if (TREE_CODE (type
) == TYPE_DECL
)
27195 type
= TREE_TYPE (type
);
27197 cast
= finish_compound_literal (type
, expression_list
,
27198 tf_warning_or_error
, fcl_functional
);
27199 /* Create a location of the form:
27202 with caret == start at the start of the type name,
27203 finishing at the closing brace. */
27204 location_t finish_loc
27205 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27206 location_t combined_loc
= make_location (start_loc
, start_loc
,
27208 cast
.set_location (combined_loc
);
27213 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27215 /*allow_expansion_p=*/true,
27216 /*non_constant_p=*/NULL
);
27218 expression_list
= error_mark_node
;
27221 expression_list
= build_tree_list_vec (vec
);
27222 release_tree_vector (vec
);
27225 cast
= build_functional_cast (type
, expression_list
,
27226 tf_warning_or_error
);
27227 /* [expr.const]/1: In an integral constant expression "only type
27228 conversions to integral or enumeration type can be used". */
27229 if (TREE_CODE (type
) == TYPE_DECL
)
27230 type
= TREE_TYPE (type
);
27231 if (cast
!= error_mark_node
27232 && !cast_valid_in_integral_constant_expression_p (type
)
27233 && cp_parser_non_integral_constant_expression (parser
,
27235 return error_mark_node
;
27237 /* Create a location of the form:
27240 with caret == start at the start of the type name,
27241 finishing at the closing paren. */
27242 location_t finish_loc
27243 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27244 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27245 cast
.set_location (combined_loc
);
27249 /* Save the tokens that make up the body of a member function defined
27250 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27251 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27252 specifiers applied to the declaration. Returns the FUNCTION_DECL
27253 for the member function. */
27256 cp_parser_save_member_function_body (cp_parser
* parser
,
27257 cp_decl_specifier_seq
*decl_specifiers
,
27258 cp_declarator
*declarator
,
27264 bool function_try_block
= false;
27266 /* Create the FUNCTION_DECL. */
27267 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27268 cp_finalize_omp_declare_simd (parser
, fn
);
27269 cp_finalize_oacc_routine (parser
, fn
, true);
27270 /* If something went badly wrong, bail out now. */
27271 if (fn
== error_mark_node
)
27273 /* If there's a function-body, skip it. */
27274 if (cp_parser_token_starts_function_definition_p
27275 (cp_lexer_peek_token (parser
->lexer
)))
27276 cp_parser_skip_to_end_of_block_or_statement (parser
);
27277 return error_mark_node
;
27280 /* Remember it, if there default args to post process. */
27281 cp_parser_save_default_args (parser
, fn
);
27283 /* Save away the tokens that make up the body of the
27285 first
= parser
->lexer
->next_token
;
27287 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27288 cp_lexer_consume_token (parser
->lexer
);
27289 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27290 RID_TRANSACTION_ATOMIC
))
27292 cp_lexer_consume_token (parser
->lexer
);
27293 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27294 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27295 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27296 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27297 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27298 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27299 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27301 cp_lexer_consume_token (parser
->lexer
);
27302 cp_lexer_consume_token (parser
->lexer
);
27303 cp_lexer_consume_token (parser
->lexer
);
27304 cp_lexer_consume_token (parser
->lexer
);
27305 cp_lexer_consume_token (parser
->lexer
);
27308 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27309 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27311 cp_lexer_consume_token (parser
->lexer
);
27312 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27317 /* Handle function try blocks. */
27318 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27320 cp_lexer_consume_token (parser
->lexer
);
27321 function_try_block
= true;
27323 /* We can have braced-init-list mem-initializers before the fn body. */
27324 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27326 cp_lexer_consume_token (parser
->lexer
);
27327 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27329 /* cache_group will stop after an un-nested { } pair, too. */
27330 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27333 /* variadic mem-inits have ... after the ')'. */
27334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27335 cp_lexer_consume_token (parser
->lexer
);
27338 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27339 /* Handle function try blocks. */
27340 if (function_try_block
)
27341 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27342 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27343 last
= parser
->lexer
->next_token
;
27345 /* Save away the inline definition; we will process it when the
27346 class is complete. */
27347 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27348 DECL_PENDING_INLINE_P (fn
) = 1;
27350 /* We need to know that this was defined in the class, so that
27351 friend templates are handled correctly. */
27352 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27354 /* Add FN to the queue of functions to be parsed later. */
27355 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27360 /* Save the tokens that make up the in-class initializer for a non-static
27361 data member. Returns a DEFAULT_ARG. */
27364 cp_parser_save_nsdmi (cp_parser
* parser
)
27366 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27369 /* Parse a template-argument-list, as well as the trailing ">" (but
27370 not the opening "<"). See cp_parser_template_argument_list for the
27374 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27378 tree saved_qualifying_scope
;
27379 tree saved_object_scope
;
27380 bool saved_greater_than_is_operator_p
;
27381 int saved_unevaluated_operand
;
27382 int saved_inhibit_evaluation_warnings
;
27386 When parsing a template-id, the first non-nested `>' is taken as
27387 the end of the template-argument-list rather than a greater-than
27389 saved_greater_than_is_operator_p
27390 = parser
->greater_than_is_operator_p
;
27391 parser
->greater_than_is_operator_p
= false;
27392 /* Parsing the argument list may modify SCOPE, so we save it
27394 saved_scope
= parser
->scope
;
27395 saved_qualifying_scope
= parser
->qualifying_scope
;
27396 saved_object_scope
= parser
->object_scope
;
27397 /* We need to evaluate the template arguments, even though this
27398 template-id may be nested within a "sizeof". */
27399 saved_unevaluated_operand
= cp_unevaluated_operand
;
27400 cp_unevaluated_operand
= 0;
27401 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27402 c_inhibit_evaluation_warnings
= 0;
27403 /* Parse the template-argument-list itself. */
27404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27405 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27406 arguments
= NULL_TREE
;
27408 arguments
= cp_parser_template_argument_list (parser
);
27409 /* Look for the `>' that ends the template-argument-list. If we find
27410 a '>>' instead, it's probably just a typo. */
27411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27413 if (cxx_dialect
!= cxx98
)
27415 /* In C++0x, a `>>' in a template argument list or cast
27416 expression is considered to be two separate `>'
27417 tokens. So, change the current token to a `>', but don't
27418 consume it: it will be consumed later when the outer
27419 template argument list (or cast expression) is parsed.
27420 Note that this replacement of `>' for `>>' is necessary
27421 even if we are parsing tentatively: in the tentative
27422 case, after calling
27423 cp_parser_enclosed_template_argument_list we will always
27424 throw away all of the template arguments and the first
27425 closing `>', either because the template argument list
27426 was erroneous or because we are replacing those tokens
27427 with a CPP_TEMPLATE_ID token. The second `>' (which will
27428 not have been thrown away) is needed either to close an
27429 outer template argument list or to complete a new-style
27431 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27432 token
->type
= CPP_GREATER
;
27434 else if (!saved_greater_than_is_operator_p
)
27436 /* If we're in a nested template argument list, the '>>' has
27437 to be a typo for '> >'. We emit the error message, but we
27438 continue parsing and we push a '>' as next token, so that
27439 the argument list will be parsed correctly. Note that the
27440 global source location is still on the token before the
27441 '>>', so we need to say explicitly where we want it. */
27442 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27443 gcc_rich_location
richloc (token
->location
);
27444 richloc
.add_fixit_replace ("> >");
27445 error_at (&richloc
, "%<>>%> should be %<> >%> "
27446 "within a nested template argument list");
27448 token
->type
= CPP_GREATER
;
27452 /* If this is not a nested template argument list, the '>>'
27453 is a typo for '>'. Emit an error message and continue.
27454 Same deal about the token location, but here we can get it
27455 right by consuming the '>>' before issuing the diagnostic. */
27456 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27457 error_at (token
->location
,
27458 "spurious %<>>%>, use %<>%> to terminate "
27459 "a template argument list");
27463 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27464 /* The `>' token might be a greater-than operator again now. */
27465 parser
->greater_than_is_operator_p
27466 = saved_greater_than_is_operator_p
;
27467 /* Restore the SAVED_SCOPE. */
27468 parser
->scope
= saved_scope
;
27469 parser
->qualifying_scope
= saved_qualifying_scope
;
27470 parser
->object_scope
= saved_object_scope
;
27471 cp_unevaluated_operand
= saved_unevaluated_operand
;
27472 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27477 /* MEMBER_FUNCTION is a member function, or a friend. If default
27478 arguments, or the body of the function have not yet been parsed,
27482 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27484 timevar_push (TV_PARSE_INMETH
);
27485 /* If this member is a template, get the underlying
27487 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27488 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27490 /* There should not be any class definitions in progress at this
27491 point; the bodies of members are only parsed outside of all class
27493 gcc_assert (parser
->num_classes_being_defined
== 0);
27494 /* While we're parsing the member functions we might encounter more
27495 classes. We want to handle them right away, but we don't want
27496 them getting mixed up with functions that are currently in the
27498 push_unparsed_function_queues (parser
);
27500 /* Make sure that any template parameters are in scope. */
27501 maybe_begin_member_template_processing (member_function
);
27503 /* If the body of the function has not yet been parsed, parse it
27505 if (DECL_PENDING_INLINE_P (member_function
))
27507 tree function_scope
;
27508 cp_token_cache
*tokens
;
27510 /* The function is no longer pending; we are processing it. */
27511 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27512 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27513 DECL_PENDING_INLINE_P (member_function
) = 0;
27515 /* If this is a local class, enter the scope of the containing
27517 function_scope
= current_function_decl
;
27518 if (function_scope
)
27519 push_function_context ();
27521 /* Push the body of the function onto the lexer stack. */
27522 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27524 /* Let the front end know that we going to be defining this
27526 start_preparsed_function (member_function
, NULL_TREE
,
27527 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27529 /* Don't do access checking if it is a templated function. */
27530 if (processing_template_decl
)
27531 push_deferring_access_checks (dk_no_check
);
27533 /* #pragma omp declare reduction needs special parsing. */
27534 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27536 parser
->lexer
->in_pragma
= true;
27537 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27538 finish_function (/*inline_p=*/true);
27539 cp_check_omp_declare_reduction (member_function
);
27542 /* Now, parse the body of the function. */
27543 cp_parser_function_definition_after_declarator (parser
,
27544 /*inline_p=*/true);
27546 if (processing_template_decl
)
27547 pop_deferring_access_checks ();
27549 /* Leave the scope of the containing function. */
27550 if (function_scope
)
27551 pop_function_context ();
27552 cp_parser_pop_lexer (parser
);
27555 /* Remove any template parameters from the symbol table. */
27556 maybe_end_member_template_processing ();
27558 /* Restore the queue. */
27559 pop_unparsed_function_queues (parser
);
27560 timevar_pop (TV_PARSE_INMETH
);
27563 /* If DECL contains any default args, remember it on the unparsed
27564 functions queue. */
27567 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27571 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27573 probe
= TREE_CHAIN (probe
))
27574 if (TREE_PURPOSE (probe
))
27576 cp_default_arg_entry entry
= {current_class_type
, decl
};
27577 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27582 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27583 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27584 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27585 from the parameter-type-list. */
27588 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27589 tree default_arg
, tree parmtype
)
27591 cp_token_cache
*tokens
;
27595 if (default_arg
== error_mark_node
)
27596 return error_mark_node
;
27598 /* Push the saved tokens for the default argument onto the parser's
27600 tokens
= DEFARG_TOKENS (default_arg
);
27601 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27603 start_lambda_scope (decl
);
27605 /* Parse the default argument. */
27606 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27607 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27608 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27610 finish_lambda_scope ();
27612 if (parsed_arg
== error_mark_node
)
27613 cp_parser_skip_to_end_of_statement (parser
);
27615 if (!processing_template_decl
)
27617 /* In a non-template class, check conversions now. In a template,
27618 we'll wait and instantiate these as needed. */
27619 if (TREE_CODE (decl
) == PARM_DECL
)
27620 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27621 tf_warning_or_error
);
27622 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27623 parsed_arg
= error_mark_node
;
27625 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27628 /* If the token stream has not been completely used up, then
27629 there was extra junk after the end of the default
27631 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27633 if (TREE_CODE (decl
) == PARM_DECL
)
27634 cp_parser_error (parser
, "expected %<,%>");
27636 cp_parser_error (parser
, "expected %<;%>");
27639 /* Revert to the main lexer. */
27640 cp_parser_pop_lexer (parser
);
27645 /* FIELD is a non-static data member with an initializer which we saved for
27646 later; parse it now. */
27649 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27653 maybe_begin_member_template_processing (field
);
27655 push_unparsed_function_queues (parser
);
27656 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27657 DECL_INITIAL (field
),
27659 pop_unparsed_function_queues (parser
);
27661 maybe_end_member_template_processing ();
27663 DECL_INITIAL (field
) = def
;
27666 /* FN is a FUNCTION_DECL which may contains a parameter with an
27667 unparsed DEFAULT_ARG. Parse the default args now. This function
27668 assumes that the current scope is the scope in which the default
27669 argument should be processed. */
27672 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27674 bool saved_local_variables_forbidden_p
;
27675 tree parm
, parmdecl
;
27677 /* While we're parsing the default args, we might (due to the
27678 statement expression extension) encounter more classes. We want
27679 to handle them right away, but we don't want them getting mixed
27680 up with default args that are currently in the queue. */
27681 push_unparsed_function_queues (parser
);
27683 /* Local variable names (and the `this' keyword) may not appear
27684 in a default argument. */
27685 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27686 parser
->local_variables_forbidden_p
= true;
27688 push_defarg_context (fn
);
27690 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27691 parmdecl
= DECL_ARGUMENTS (fn
);
27692 parm
&& parm
!= void_list_node
;
27693 parm
= TREE_CHAIN (parm
),
27694 parmdecl
= DECL_CHAIN (parmdecl
))
27696 tree default_arg
= TREE_PURPOSE (parm
);
27698 vec
<tree
, va_gc
> *insts
;
27705 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27706 /* This can happen for a friend declaration for a function
27707 already declared with default arguments. */
27711 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27713 TREE_VALUE (parm
));
27714 TREE_PURPOSE (parm
) = parsed_arg
;
27716 /* Update any instantiations we've already created. */
27717 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27718 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27719 TREE_PURPOSE (copy
) = parsed_arg
;
27722 pop_defarg_context ();
27724 /* Make sure no default arg is missing. */
27725 check_default_args (fn
);
27727 /* Restore the state of local_variables_forbidden_p. */
27728 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27730 /* Restore the queue. */
27731 pop_unparsed_function_queues (parser
);
27734 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27736 sizeof ... ( identifier )
27738 where the 'sizeof' token has already been consumed. */
27741 cp_parser_sizeof_pack (cp_parser
*parser
)
27743 /* Consume the `...'. */
27744 cp_lexer_consume_token (parser
->lexer
);
27745 maybe_warn_variadic_templates ();
27747 matching_parens parens
;
27748 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27750 parens
.consume_open (parser
);
27752 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27753 "%<sizeof...%> argument must be surrounded by parentheses");
27755 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27756 tree name
= cp_parser_identifier (parser
);
27757 if (name
== error_mark_node
)
27758 return error_mark_node
;
27759 /* The name is not qualified. */
27760 parser
->scope
= NULL_TREE
;
27761 parser
->qualifying_scope
= NULL_TREE
;
27762 parser
->object_scope
= NULL_TREE
;
27763 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27764 if (expr
== error_mark_node
)
27765 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27767 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27768 expr
= TREE_TYPE (expr
);
27769 else if (TREE_CODE (expr
) == CONST_DECL
)
27770 expr
= DECL_INITIAL (expr
);
27771 expr
= make_pack_expansion (expr
);
27772 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27775 parens
.require_close (parser
);
27780 /* Parse the operand of `sizeof' (or a similar operator). Returns
27781 either a TYPE or an expression, depending on the form of the
27782 input. The KEYWORD indicates which kind of expression we have
27786 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27788 tree expr
= NULL_TREE
;
27789 const char *saved_message
;
27791 bool saved_integral_constant_expression_p
;
27792 bool saved_non_integral_constant_expression_p
;
27794 /* If it's a `...', then we are computing the length of a parameter
27796 if (keyword
== RID_SIZEOF
27797 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27798 return cp_parser_sizeof_pack (parser
);
27800 /* Types cannot be defined in a `sizeof' expression. Save away the
27802 saved_message
= parser
->type_definition_forbidden_message
;
27803 /* And create the new one. */
27804 tmp
= concat ("types may not be defined in %<",
27805 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27806 "%> expressions", NULL
);
27807 parser
->type_definition_forbidden_message
= tmp
;
27809 /* The restrictions on constant-expressions do not apply inside
27810 sizeof expressions. */
27811 saved_integral_constant_expression_p
27812 = parser
->integral_constant_expression_p
;
27813 saved_non_integral_constant_expression_p
27814 = parser
->non_integral_constant_expression_p
;
27815 parser
->integral_constant_expression_p
= false;
27817 /* Do not actually evaluate the expression. */
27818 ++cp_unevaluated_operand
;
27819 ++c_inhibit_evaluation_warnings
;
27820 /* If it's a `(', then we might be looking at the type-id
27822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27824 tree type
= NULL_TREE
;
27826 /* We can't be sure yet whether we're looking at a type-id or an
27828 cp_parser_parse_tentatively (parser
);
27830 matching_parens parens
;
27831 parens
.consume_open (parser
);
27833 /* Note: as a GNU Extension, compound literals are considered
27834 postfix-expressions as they are in C99, so they are valid
27835 arguments to sizeof. See comment in cp_parser_cast_expression
27837 if (cp_parser_compound_literal_p (parser
))
27838 cp_parser_simulate_error (parser
);
27841 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27842 parser
->in_type_id_in_expr_p
= true;
27843 /* Look for the type-id. */
27844 type
= cp_parser_type_id (parser
);
27845 /* Look for the closing `)'. */
27846 parens
.require_close (parser
);
27847 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
27850 /* If all went well, then we're done. */
27851 if (cp_parser_parse_definitely (parser
))
27853 cp_decl_specifier_seq decl_specs
;
27855 /* Build a trivial decl-specifier-seq. */
27856 clear_decl_specs (&decl_specs
);
27857 decl_specs
.type
= type
;
27859 /* Call grokdeclarator to figure out what type this is. */
27860 expr
= grokdeclarator (NULL
,
27864 /*attrlist=*/NULL
);
27868 /* If the type-id production did not work out, then we must be
27869 looking at the unary-expression production. */
27871 expr
= cp_parser_unary_expression (parser
);
27873 /* Go back to evaluating expressions. */
27874 --cp_unevaluated_operand
;
27875 --c_inhibit_evaluation_warnings
;
27877 /* Free the message we created. */
27879 /* And restore the old one. */
27880 parser
->type_definition_forbidden_message
= saved_message
;
27881 parser
->integral_constant_expression_p
27882 = saved_integral_constant_expression_p
;
27883 parser
->non_integral_constant_expression_p
27884 = saved_non_integral_constant_expression_p
;
27889 /* If the current declaration has no declarator, return true. */
27892 cp_parser_declares_only_class_p (cp_parser
*parser
)
27894 /* If the next token is a `;' or a `,' then there is no
27896 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27897 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
27900 /* Update the DECL_SPECS to reflect the storage class indicated by
27904 cp_parser_set_storage_class (cp_parser
*parser
,
27905 cp_decl_specifier_seq
*decl_specs
,
27909 cp_storage_class storage_class
;
27911 if (parser
->in_unbraced_linkage_specification_p
)
27913 error_at (token
->location
, "invalid use of %qD in linkage specification",
27914 ridpointers
[keyword
]);
27917 else if (decl_specs
->storage_class
!= sc_none
)
27919 decl_specs
->conflicting_specifiers_p
= true;
27923 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
27924 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
27925 && decl_specs
->gnu_thread_keyword_p
)
27927 pedwarn (decl_specs
->locations
[ds_thread
], 0,
27928 "%<__thread%> before %qD", ridpointers
[keyword
]);
27934 storage_class
= sc_auto
;
27937 storage_class
= sc_register
;
27940 storage_class
= sc_static
;
27943 storage_class
= sc_extern
;
27946 storage_class
= sc_mutable
;
27949 gcc_unreachable ();
27951 decl_specs
->storage_class
= storage_class
;
27952 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
27954 /* A storage class specifier cannot be applied alongside a typedef
27955 specifier. If there is a typedef specifier present then set
27956 conflicting_specifiers_p which will trigger an error later
27957 on in grokdeclarator. */
27958 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
27959 decl_specs
->conflicting_specifiers_p
= true;
27962 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27963 is true, the type is a class or enum definition. */
27966 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
27969 bool type_definition_p
)
27971 decl_specs
->any_specifiers_p
= true;
27973 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27974 (with, for example, in "typedef int wchar_t;") we remember that
27975 this is what happened. In system headers, we ignore these
27976 declarations so that G++ can work with system headers that are not
27978 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
27979 && !type_definition_p
27980 && (type_spec
== boolean_type_node
27981 || type_spec
== char16_type_node
27982 || type_spec
== char32_type_node
27983 || type_spec
== wchar_type_node
)
27984 && (decl_specs
->type
27985 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
27986 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
27987 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
27988 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
27990 decl_specs
->redefined_builtin_type
= type_spec
;
27991 set_and_check_decl_spec_loc (decl_specs
,
27992 ds_redefined_builtin_type_spec
,
27994 if (!decl_specs
->type
)
27996 decl_specs
->type
= type_spec
;
27997 decl_specs
->type_definition_p
= false;
27998 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28001 else if (decl_specs
->type
)
28002 decl_specs
->multiple_types_p
= true;
28005 decl_specs
->type
= type_spec
;
28006 decl_specs
->type_definition_p
= type_definition_p
;
28007 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28008 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28012 /* True iff TOKEN is the GNU keyword __thread. */
28015 token_is__thread (cp_token
*token
)
28017 gcc_assert (token
->keyword
== RID_THREAD
);
28018 return id_equal (token
->u
.value
, "__thread");
28021 /* Set the location for a declarator specifier and check if it is
28024 DECL_SPECS is the sequence of declarator specifiers onto which to
28027 DS is the single declarator specifier to set which location is to
28028 be set onto the existing sequence of declarators.
28030 LOCATION is the location for the declarator specifier to
28034 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28035 cp_decl_spec ds
, cp_token
*token
)
28037 gcc_assert (ds
< ds_last
);
28039 if (decl_specs
== NULL
)
28042 source_location location
= token
->location
;
28044 if (decl_specs
->locations
[ds
] == 0)
28046 decl_specs
->locations
[ds
] = location
;
28047 if (ds
== ds_thread
)
28048 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28054 if (decl_specs
->locations
[ds_long_long
] != 0)
28055 error_at (location
,
28056 "%<long long long%> is too long for GCC");
28059 decl_specs
->locations
[ds_long_long
] = location
;
28060 pedwarn_cxx98 (location
,
28062 "ISO C++ 1998 does not support %<long long%>");
28065 else if (ds
== ds_thread
)
28067 bool gnu
= token_is__thread (token
);
28068 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28069 error_at (location
,
28070 "both %<__thread%> and %<thread_local%> specified");
28073 gcc_rich_location
richloc (location
);
28074 richloc
.add_fixit_remove ();
28075 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28080 static const char *const decl_spec_names
[] = {
28097 gcc_rich_location
richloc (location
);
28098 richloc
.add_fixit_remove ();
28099 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28104 /* Return true iff the declarator specifier DS is present in the
28105 sequence of declarator specifiers DECL_SPECS. */
28108 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28111 gcc_assert (ds
< ds_last
);
28113 if (decl_specs
== NULL
)
28116 return decl_specs
->locations
[ds
] != 0;
28119 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28120 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28123 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28125 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28128 /* Issue an error message indicating that TOKEN_DESC was expected.
28129 If KEYWORD is true, it indicated this function is called by
28130 cp_parser_require_keword and the required token can only be
28131 a indicated keyword.
28133 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28134 within any error as the location of an "opening" token matching
28135 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28136 RT_CLOSE_PAREN). */
28139 cp_parser_required_error (cp_parser
*parser
,
28140 required_token token_desc
,
28142 location_t matching_location
)
28144 if (cp_parser_simulate_error (parser
))
28147 const char *gmsgid
= NULL
;
28148 switch (token_desc
)
28151 gmsgid
= G_("expected %<new%>");
28154 gmsgid
= G_("expected %<delete%>");
28157 gmsgid
= G_("expected %<return%>");
28160 gmsgid
= G_("expected %<while%>");
28163 gmsgid
= G_("expected %<extern%>");
28165 case RT_STATIC_ASSERT
:
28166 gmsgid
= G_("expected %<static_assert%>");
28169 gmsgid
= G_("expected %<decltype%>");
28172 gmsgid
= G_("expected %<operator%>");
28175 gmsgid
= G_("expected %<class%>");
28178 gmsgid
= G_("expected %<template%>");
28181 gmsgid
= G_("expected %<namespace%>");
28184 gmsgid
= G_("expected %<using%>");
28187 gmsgid
= G_("expected %<asm%>");
28190 gmsgid
= G_("expected %<try%>");
28193 gmsgid
= G_("expected %<catch%>");
28196 gmsgid
= G_("expected %<throw%>");
28199 gmsgid
= G_("expected %<__label__%>");
28202 gmsgid
= G_("expected %<@try%>");
28204 case RT_AT_SYNCHRONIZED
:
28205 gmsgid
= G_("expected %<@synchronized%>");
28208 gmsgid
= G_("expected %<@throw%>");
28210 case RT_TRANSACTION_ATOMIC
:
28211 gmsgid
= G_("expected %<__transaction_atomic%>");
28213 case RT_TRANSACTION_RELAXED
:
28214 gmsgid
= G_("expected %<__transaction_relaxed%>");
28220 if (!gmsgid
&& !keyword
)
28222 switch (token_desc
)
28225 gmsgid
= G_("expected %<;%>");
28227 case RT_OPEN_PAREN
:
28228 gmsgid
= G_("expected %<(%>");
28230 case RT_CLOSE_BRACE
:
28231 gmsgid
= G_("expected %<}%>");
28233 case RT_OPEN_BRACE
:
28234 gmsgid
= G_("expected %<{%>");
28236 case RT_CLOSE_SQUARE
:
28237 gmsgid
= G_("expected %<]%>");
28239 case RT_OPEN_SQUARE
:
28240 gmsgid
= G_("expected %<[%>");
28243 gmsgid
= G_("expected %<,%>");
28246 gmsgid
= G_("expected %<::%>");
28249 gmsgid
= G_("expected %<<%>");
28252 gmsgid
= G_("expected %<>%>");
28255 gmsgid
= G_("expected %<=%>");
28258 gmsgid
= G_("expected %<...%>");
28261 gmsgid
= G_("expected %<*%>");
28264 gmsgid
= G_("expected %<~%>");
28267 gmsgid
= G_("expected %<:%>");
28269 case RT_COLON_SCOPE
:
28270 gmsgid
= G_("expected %<:%> or %<::%>");
28272 case RT_CLOSE_PAREN
:
28273 gmsgid
= G_("expected %<)%>");
28275 case RT_COMMA_CLOSE_PAREN
:
28276 gmsgid
= G_("expected %<,%> or %<)%>");
28278 case RT_PRAGMA_EOL
:
28279 gmsgid
= G_("expected end of line");
28282 gmsgid
= G_("expected identifier");
28285 gmsgid
= G_("expected selection-statement");
28288 gmsgid
= G_("expected iteration-statement");
28291 gmsgid
= G_("expected jump-statement");
28294 gmsgid
= G_("expected class-key");
28296 case RT_CLASS_TYPENAME_TEMPLATE
:
28297 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28300 gcc_unreachable ();
28305 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28309 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28310 issue an error message indicating that TOKEN_DESC was expected.
28312 Returns the token consumed, if the token had the appropriate type.
28313 Otherwise, returns NULL.
28315 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28316 within any error as the location of an "opening" token matching
28317 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28318 RT_CLOSE_PAREN). */
28321 cp_parser_require (cp_parser
* parser
,
28322 enum cpp_ttype type
,
28323 required_token token_desc
,
28324 location_t matching_location
)
28326 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28327 return cp_lexer_consume_token (parser
->lexer
);
28330 /* Output the MESSAGE -- unless we're parsing tentatively. */
28331 if (!cp_parser_simulate_error (parser
))
28332 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28333 matching_location
);
28338 /* An error message is produced if the next token is not '>'.
28339 All further tokens are skipped until the desired token is
28340 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28343 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28345 /* Current level of '< ... >'. */
28346 unsigned level
= 0;
28347 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28348 unsigned nesting_depth
= 0;
28350 /* Are we ready, yet? If not, issue error message. */
28351 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28354 /* Skip tokens until the desired token is found. */
28357 /* Peek at the next token. */
28358 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28361 if (!nesting_depth
)
28366 if (cxx_dialect
== cxx98
)
28367 /* C++0x views the `>>' operator as two `>' tokens, but
28370 else if (!nesting_depth
&& level
-- == 0)
28372 /* We've hit a `>>' where the first `>' closes the
28373 template argument list, and the second `>' is
28374 spurious. Just consume the `>>' and stop; we've
28375 already produced at least one error. */
28376 cp_lexer_consume_token (parser
->lexer
);
28379 /* Fall through for C++0x, so we handle the second `>' in
28381 gcc_fallthrough ();
28384 if (!nesting_depth
&& level
-- == 0)
28386 /* We've reached the token we want, consume it and stop. */
28387 cp_lexer_consume_token (parser
->lexer
);
28392 case CPP_OPEN_PAREN
:
28393 case CPP_OPEN_SQUARE
:
28397 case CPP_CLOSE_PAREN
:
28398 case CPP_CLOSE_SQUARE
:
28399 if (nesting_depth
-- == 0)
28404 case CPP_PRAGMA_EOL
:
28405 case CPP_SEMICOLON
:
28406 case CPP_OPEN_BRACE
:
28407 case CPP_CLOSE_BRACE
:
28408 /* The '>' was probably forgotten, don't look further. */
28415 /* Consume this token. */
28416 cp_lexer_consume_token (parser
->lexer
);
28420 /* If the next token is the indicated keyword, consume it. Otherwise,
28421 issue an error message indicating that TOKEN_DESC was expected.
28423 Returns the token consumed, if the token had the appropriate type.
28424 Otherwise, returns NULL. */
28427 cp_parser_require_keyword (cp_parser
* parser
,
28429 required_token token_desc
)
28431 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28433 if (token
&& token
->keyword
!= keyword
)
28435 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28443 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28444 function-definition. */
28447 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28449 return (/* An ordinary function-body begins with an `{'. */
28450 token
->type
== CPP_OPEN_BRACE
28451 /* A ctor-initializer begins with a `:'. */
28452 || token
->type
== CPP_COLON
28453 /* A function-try-block begins with `try'. */
28454 || token
->keyword
== RID_TRY
28455 /* A function-transaction-block begins with `__transaction_atomic'
28456 or `__transaction_relaxed'. */
28457 || token
->keyword
== RID_TRANSACTION_ATOMIC
28458 || token
->keyword
== RID_TRANSACTION_RELAXED
28459 /* The named return value extension begins with `return'. */
28460 || token
->keyword
== RID_RETURN
);
28463 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28467 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28471 token
= cp_lexer_peek_token (parser
->lexer
);
28472 return (token
->type
== CPP_OPEN_BRACE
28473 || (token
->type
== CPP_COLON
28474 && !parser
->colon_doesnt_start_class_def_p
));
28477 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28478 C++0x) ending a template-argument. */
28481 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28485 token
= cp_lexer_peek_token (parser
->lexer
);
28486 return (token
->type
== CPP_COMMA
28487 || token
->type
== CPP_GREATER
28488 || token
->type
== CPP_ELLIPSIS
28489 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28492 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28493 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28496 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28501 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28502 if (token
->type
== CPP_LESS
)
28504 /* Check for the sequence `<::' in the original code. It would be lexed as
28505 `[:', where `[' is a digraph, and there is no whitespace before
28507 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28510 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28511 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28517 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28518 or none_type otherwise. */
28520 static enum tag_types
28521 cp_parser_token_is_class_key (cp_token
* token
)
28523 switch (token
->keyword
)
28528 return record_type
;
28537 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28538 or none_type otherwise or if the token is null. */
28540 static enum tag_types
28541 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28546 switch (token
->keyword
)
28551 return typename_type
;
28558 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28561 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28563 if (type
== error_mark_node
)
28565 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28567 if (permerror (input_location
, "%qs tag used in naming %q#T",
28568 class_key
== union_type
? "union"
28569 : class_key
== record_type
? "struct" : "class",
28571 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28572 "%q#T was previously declared here", type
);
28576 /* Issue an error message if DECL is redeclared with different
28577 access than its original declaration [class.access.spec/3].
28578 This applies to nested classes, nested class templates and
28579 enumerations [class.mem/1]. */
28582 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28585 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28586 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28589 if ((TREE_PRIVATE (decl
)
28590 != (current_access_specifier
== access_private_node
))
28591 || (TREE_PROTECTED (decl
)
28592 != (current_access_specifier
== access_protected_node
)))
28593 error_at (location
, "%qD redeclared with different access", decl
);
28596 /* Look for the `template' keyword, as a syntactic disambiguator.
28597 Return TRUE iff it is present, in which case it will be
28601 cp_parser_optional_template_keyword (cp_parser
*parser
)
28603 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28605 /* In C++98 the `template' keyword can only be used within templates;
28606 outside templates the parser can always figure out what is a
28607 template and what is not. In C++11, per the resolution of DR 468,
28608 `template' is allowed in cases where it is not strictly necessary. */
28609 if (!processing_template_decl
28610 && pedantic
&& cxx_dialect
== cxx98
)
28612 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28613 pedwarn (token
->location
, OPT_Wpedantic
,
28614 "in C++98 %<template%> (as a disambiguator) is only "
28615 "allowed within templates");
28616 /* If this part of the token stream is rescanned, the same
28617 error message would be generated. So, we purge the token
28618 from the stream. */
28619 cp_lexer_purge_token (parser
->lexer
);
28624 /* Consume the `template' keyword. */
28625 cp_lexer_consume_token (parser
->lexer
);
28632 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28633 set PARSER->SCOPE, and perform other related actions. */
28636 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28638 struct tree_check
*check_value
;
28640 /* Get the stored value. */
28641 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28642 /* Set the scope from the stored value. */
28643 parser
->scope
= saved_checks_value (check_value
);
28644 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28645 parser
->object_scope
= NULL_TREE
;
28648 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28649 encounter the end of a block before what we were looking for. */
28652 cp_parser_cache_group (cp_parser
*parser
,
28653 enum cpp_ttype end
,
28658 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28660 /* Abort a parenthesized expression if we encounter a semicolon. */
28661 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28662 && token
->type
== CPP_SEMICOLON
)
28664 /* If we've reached the end of the file, stop. */
28665 if (token
->type
== CPP_EOF
28666 || (end
!= CPP_PRAGMA_EOL
28667 && token
->type
== CPP_PRAGMA_EOL
))
28669 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28670 /* We've hit the end of an enclosing block, so there's been some
28671 kind of syntax error. */
28674 /* Consume the token. */
28675 cp_lexer_consume_token (parser
->lexer
);
28676 /* See if it starts a new group. */
28677 if (token
->type
== CPP_OPEN_BRACE
)
28679 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28680 /* In theory this should probably check end == '}', but
28681 cp_parser_save_member_function_body needs it to exit
28682 after either '}' or ')' when called with ')'. */
28686 else if (token
->type
== CPP_OPEN_PAREN
)
28688 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28689 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28692 else if (token
->type
== CPP_PRAGMA
)
28693 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28694 else if (token
->type
== end
)
28699 /* Like above, for caching a default argument or NSDMI. Both of these are
28700 terminated by a non-nested comma, but it can be unclear whether or not a
28701 comma is nested in a template argument list unless we do more parsing.
28702 In order to handle this ambiguity, when we encounter a ',' after a '<'
28703 we try to parse what follows as a parameter-declaration-list (in the
28704 case of a default argument) or a member-declarator (in the case of an
28705 NSDMI). If that succeeds, then we stop caching. */
28708 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28710 unsigned depth
= 0;
28711 int maybe_template_id
= 0;
28712 cp_token
*first_token
;
28714 tree default_argument
;
28716 /* Add tokens until we have processed the entire default
28717 argument. We add the range [first_token, token). */
28718 first_token
= cp_lexer_peek_token (parser
->lexer
);
28719 if (first_token
->type
== CPP_OPEN_BRACE
)
28721 /* For list-initialization, this is straightforward. */
28722 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28723 token
= cp_lexer_peek_token (parser
->lexer
);
28729 /* Peek at the next token. */
28730 token
= cp_lexer_peek_token (parser
->lexer
);
28731 /* What we do depends on what token we have. */
28732 switch (token
->type
)
28734 /* In valid code, a default argument must be
28735 immediately followed by a `,' `)', or `...'. */
28737 if (depth
== 0 && maybe_template_id
)
28739 /* If we've seen a '<', we might be in a
28740 template-argument-list. Until Core issue 325 is
28741 resolved, we don't know how this situation ought
28742 to be handled, so try to DTRT. We check whether
28743 what comes after the comma is a valid parameter
28744 declaration list. If it is, then the comma ends
28745 the default argument; otherwise the default
28746 argument continues. */
28747 bool error
= false;
28750 /* Set ITALP so cp_parser_parameter_declaration_list
28751 doesn't decide to commit to this parse. */
28752 bool saved_italp
= parser
->in_template_argument_list_p
;
28753 parser
->in_template_argument_list_p
= true;
28755 cp_parser_parse_tentatively (parser
);
28759 /* Parse declarators until we reach a non-comma or
28760 somthing that cannot be an initializer.
28761 Just checking whether we're looking at a single
28762 declarator is insufficient. Consider:
28763 int var = tuple<T,U>::x;
28764 The template parameter 'U' looks exactly like a
28768 int ctor_dtor_or_conv_p
;
28769 cp_lexer_consume_token (parser
->lexer
);
28770 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28771 &ctor_dtor_or_conv_p
,
28772 /*parenthesized_p=*/NULL
,
28774 /*friend_p=*/false);
28775 peek
= cp_lexer_peek_token (parser
->lexer
);
28776 if (cp_parser_error_occurred (parser
))
28779 while (peek
->type
== CPP_COMMA
);
28780 /* If we met an '=' or ';' then the original comma
28781 was the end of the NSDMI. Otherwise assume
28782 we're still in the NSDMI. */
28783 error
= (peek
->type
!= CPP_EQ
28784 && peek
->type
!= CPP_SEMICOLON
);
28788 cp_lexer_consume_token (parser
->lexer
);
28789 begin_scope (sk_function_parms
, NULL_TREE
);
28790 cp_parser_parameter_declaration_list (parser
, &error
);
28791 pop_bindings_and_leave_scope ();
28793 if (!cp_parser_error_occurred (parser
) && !error
)
28795 cp_parser_abort_tentative_parse (parser
);
28797 parser
->in_template_argument_list_p
= saved_italp
;
28801 case CPP_CLOSE_PAREN
:
28803 /* If we run into a non-nested `;', `}', or `]',
28804 then the code is invalid -- but the default
28805 argument is certainly over. */
28806 case CPP_SEMICOLON
:
28807 case CPP_CLOSE_BRACE
:
28808 case CPP_CLOSE_SQUARE
:
28810 /* Handle correctly int n = sizeof ... ( p ); */
28811 && token
->type
!= CPP_ELLIPSIS
)
28813 /* Update DEPTH, if necessary. */
28814 else if (token
->type
== CPP_CLOSE_PAREN
28815 || token
->type
== CPP_CLOSE_BRACE
28816 || token
->type
== CPP_CLOSE_SQUARE
)
28820 case CPP_OPEN_PAREN
:
28821 case CPP_OPEN_SQUARE
:
28822 case CPP_OPEN_BRACE
:
28828 /* This might be the comparison operator, or it might
28829 start a template argument list. */
28830 ++maybe_template_id
;
28834 if (cxx_dialect
== cxx98
)
28836 /* Fall through for C++0x, which treats the `>>'
28837 operator like two `>' tokens in certain
28839 gcc_fallthrough ();
28844 /* This might be an operator, or it might close a
28845 template argument list. But if a previous '<'
28846 started a template argument list, this will have
28847 closed it, so we can't be in one anymore. */
28848 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
28849 if (maybe_template_id
< 0)
28850 maybe_template_id
= 0;
28854 /* If we run out of tokens, issue an error message. */
28856 case CPP_PRAGMA_EOL
:
28857 error_at (token
->location
, "file ends in default argument");
28858 return error_mark_node
;
28862 /* In these cases, we should look for template-ids.
28863 For example, if the default argument is
28864 `X<int, double>()', we need to do name lookup to
28865 figure out whether or not `X' is a template; if
28866 so, the `,' does not end the default argument.
28868 That is not yet done. */
28875 /* If we've reached the end, stop. */
28879 /* Add the token to the token block. */
28880 token
= cp_lexer_consume_token (parser
->lexer
);
28883 /* Create a DEFAULT_ARG to represent the unparsed default
28885 default_argument
= make_node (DEFAULT_ARG
);
28886 DEFARG_TOKENS (default_argument
)
28887 = cp_token_cache_new (first_token
, token
);
28888 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
28890 return default_argument
;
28893 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28896 defarg_location (tree default_argument
)
28898 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
28899 location_t start
= tokens
->first
->location
;
28900 location_t end
= tokens
->last
->location
;
28901 return make_location (start
, start
, end
);
28904 /* Begin parsing tentatively. We always save tokens while parsing
28905 tentatively so that if the tentative parsing fails we can restore the
28909 cp_parser_parse_tentatively (cp_parser
* parser
)
28911 /* Enter a new parsing context. */
28912 parser
->context
= cp_parser_context_new (parser
->context
);
28913 /* Begin saving tokens. */
28914 cp_lexer_save_tokens (parser
->lexer
);
28915 /* In order to avoid repetitive access control error messages,
28916 access checks are queued up until we are no longer parsing
28918 push_deferring_access_checks (dk_deferred
);
28921 /* Commit to the currently active tentative parse. */
28924 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
28926 cp_parser_context
*context
;
28929 /* Mark all of the levels as committed. */
28930 lexer
= parser
->lexer
;
28931 for (context
= parser
->context
; context
->next
; context
= context
->next
)
28933 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28935 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28936 while (!cp_lexer_saving_tokens (lexer
))
28937 lexer
= lexer
->next
;
28938 cp_lexer_commit_tokens (lexer
);
28942 /* Commit to the topmost currently active tentative parse.
28944 Note that this function shouldn't be called when there are
28945 irreversible side-effects while in a tentative state. For
28946 example, we shouldn't create a permanent entry in the symbol
28947 table, or issue an error message that might not apply if the
28948 tentative parse is aborted. */
28951 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
28953 cp_parser_context
*context
= parser
->context
;
28954 cp_lexer
*lexer
= parser
->lexer
;
28958 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28960 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28962 while (!cp_lexer_saving_tokens (lexer
))
28963 lexer
= lexer
->next
;
28964 cp_lexer_commit_tokens (lexer
);
28968 /* Abort the currently active tentative parse. All consumed tokens
28969 will be rolled back, and no diagnostics will be issued. */
28972 cp_parser_abort_tentative_parse (cp_parser
* parser
)
28974 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
28975 || errorcount
> 0);
28976 cp_parser_simulate_error (parser
);
28977 /* Now, pretend that we want to see if the construct was
28978 successfully parsed. */
28979 cp_parser_parse_definitely (parser
);
28982 /* Stop parsing tentatively. If a parse error has occurred, restore the
28983 token stream. Otherwise, commit to the tokens we have consumed.
28984 Returns true if no error occurred; false otherwise. */
28987 cp_parser_parse_definitely (cp_parser
* parser
)
28989 bool error_occurred
;
28990 cp_parser_context
*context
;
28992 /* Remember whether or not an error occurred, since we are about to
28993 destroy that information. */
28994 error_occurred
= cp_parser_error_occurred (parser
);
28995 /* Remove the topmost context from the stack. */
28996 context
= parser
->context
;
28997 parser
->context
= context
->next
;
28998 /* If no parse errors occurred, commit to the tentative parse. */
28999 if (!error_occurred
)
29001 /* Commit to the tokens read tentatively, unless that was
29003 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29004 cp_lexer_commit_tokens (parser
->lexer
);
29006 pop_to_parent_deferring_access_checks ();
29008 /* Otherwise, if errors occurred, roll back our state so that things
29009 are just as they were before we began the tentative parse. */
29012 cp_lexer_rollback_tokens (parser
->lexer
);
29013 pop_deferring_access_checks ();
29015 /* Add the context to the front of the free list. */
29016 context
->next
= cp_parser_context_free_list
;
29017 cp_parser_context_free_list
= context
;
29019 return !error_occurred
;
29022 /* Returns true if we are parsing tentatively and are not committed to
29023 this tentative parse. */
29026 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29028 return (cp_parser_parsing_tentatively (parser
)
29029 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29032 /* Returns nonzero iff an error has occurred during the most recent
29033 tentative parse. */
29036 cp_parser_error_occurred (cp_parser
* parser
)
29038 return (cp_parser_parsing_tentatively (parser
)
29039 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29042 /* Returns nonzero if GNU extensions are allowed. */
29045 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29047 return parser
->allow_gnu_extensions_p
;
29050 /* Objective-C++ Productions */
29053 /* Parse an Objective-C expression, which feeds into a primary-expression
29057 objc-message-expression
29058 objc-string-literal
29059 objc-encode-expression
29060 objc-protocol-expression
29061 objc-selector-expression
29063 Returns a tree representation of the expression. */
29066 cp_parser_objc_expression (cp_parser
* parser
)
29068 /* Try to figure out what kind of declaration is present. */
29069 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29073 case CPP_OPEN_SQUARE
:
29074 return cp_parser_objc_message_expression (parser
);
29076 case CPP_OBJC_STRING
:
29077 kwd
= cp_lexer_consume_token (parser
->lexer
);
29078 return objc_build_string_object (kwd
->u
.value
);
29081 switch (kwd
->keyword
)
29083 case RID_AT_ENCODE
:
29084 return cp_parser_objc_encode_expression (parser
);
29086 case RID_AT_PROTOCOL
:
29087 return cp_parser_objc_protocol_expression (parser
);
29089 case RID_AT_SELECTOR
:
29090 return cp_parser_objc_selector_expression (parser
);
29097 error_at (kwd
->location
,
29098 "misplaced %<@%D%> Objective-C++ construct",
29100 cp_parser_skip_to_end_of_block_or_statement (parser
);
29103 return error_mark_node
;
29106 /* Parse an Objective-C message expression.
29108 objc-message-expression:
29109 [ objc-message-receiver objc-message-args ]
29111 Returns a representation of an Objective-C message. */
29114 cp_parser_objc_message_expression (cp_parser
* parser
)
29116 tree receiver
, messageargs
;
29118 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29119 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29120 receiver
= cp_parser_objc_message_receiver (parser
);
29121 messageargs
= cp_parser_objc_message_args (parser
);
29122 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29123 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29125 tree result
= objc_build_message_expr (receiver
, messageargs
);
29127 /* Construct a location e.g.
29130 ranging from the '[' to the ']', with the caret at the start. */
29131 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29132 protected_set_expr_location (result
, combined_loc
);
29137 /* Parse an objc-message-receiver.
29139 objc-message-receiver:
29141 simple-type-specifier
29143 Returns a representation of the type or expression. */
29146 cp_parser_objc_message_receiver (cp_parser
* parser
)
29150 /* An Objective-C message receiver may be either (1) a type
29151 or (2) an expression. */
29152 cp_parser_parse_tentatively (parser
);
29153 rcv
= cp_parser_expression (parser
);
29155 /* If that worked out, fine. */
29156 if (cp_parser_parse_definitely (parser
))
29159 cp_parser_parse_tentatively (parser
);
29160 rcv
= cp_parser_simple_type_specifier (parser
,
29161 /*decl_specs=*/NULL
,
29162 CP_PARSER_FLAGS_NONE
);
29164 if (cp_parser_parse_definitely (parser
))
29165 return objc_get_class_reference (rcv
);
29167 cp_parser_error (parser
, "objective-c++ message receiver expected");
29168 return error_mark_node
;
29171 /* Parse the arguments and selectors comprising an Objective-C message.
29176 objc-selector-args , objc-comma-args
29178 objc-selector-args:
29179 objc-selector [opt] : assignment-expression
29180 objc-selector-args objc-selector [opt] : assignment-expression
29183 assignment-expression
29184 objc-comma-args , assignment-expression
29186 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29187 selector arguments and TREE_VALUE containing a list of comma
29191 cp_parser_objc_message_args (cp_parser
* parser
)
29193 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29194 bool maybe_unary_selector_p
= true;
29195 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29197 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29199 tree selector
= NULL_TREE
, arg
;
29201 if (token
->type
!= CPP_COLON
)
29202 selector
= cp_parser_objc_selector (parser
);
29204 /* Detect if we have a unary selector. */
29205 if (maybe_unary_selector_p
29206 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29207 return build_tree_list (selector
, NULL_TREE
);
29209 maybe_unary_selector_p
= false;
29210 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29211 arg
= cp_parser_assignment_expression (parser
);
29214 = chainon (sel_args
,
29215 build_tree_list (selector
, arg
));
29217 token
= cp_lexer_peek_token (parser
->lexer
);
29220 /* Handle non-selector arguments, if any. */
29221 while (token
->type
== CPP_COMMA
)
29225 cp_lexer_consume_token (parser
->lexer
);
29226 arg
= cp_parser_assignment_expression (parser
);
29229 = chainon (addl_args
,
29230 build_tree_list (NULL_TREE
, arg
));
29232 token
= cp_lexer_peek_token (parser
->lexer
);
29235 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29237 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29238 return build_tree_list (error_mark_node
, error_mark_node
);
29241 return build_tree_list (sel_args
, addl_args
);
29244 /* Parse an Objective-C encode expression.
29246 objc-encode-expression:
29247 @encode objc-typename
29249 Returns an encoded representation of the type argument. */
29252 cp_parser_objc_encode_expression (cp_parser
* parser
)
29256 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29258 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29259 matching_parens parens
;
29260 parens
.require_open (parser
);
29261 token
= cp_lexer_peek_token (parser
->lexer
);
29262 type
= complete_type (cp_parser_type_id (parser
));
29263 parens
.require_close (parser
);
29267 error_at (token
->location
,
29268 "%<@encode%> must specify a type as an argument");
29269 return error_mark_node
;
29272 /* This happens if we find @encode(T) (where T is a template
29273 typename or something dependent on a template typename) when
29274 parsing a template. In that case, we can't compile it
29275 immediately, but we rather create an AT_ENCODE_EXPR which will
29276 need to be instantiated when the template is used.
29278 if (dependent_type_p (type
))
29280 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29281 TREE_READONLY (value
) = 1;
29286 /* Build a location of the form:
29289 with caret==start at the @ token, finishing at the close paren. */
29290 location_t combined_loc
29291 = make_location (start_loc
, start_loc
,
29292 cp_lexer_previous_token (parser
->lexer
)->location
);
29294 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29297 /* Parse an Objective-C @defs expression. */
29300 cp_parser_objc_defs_expression (cp_parser
*parser
)
29304 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29305 matching_parens parens
;
29306 parens
.require_open (parser
);
29307 name
= cp_parser_identifier (parser
);
29308 parens
.require_close (parser
);
29310 return objc_get_class_ivars (name
);
29313 /* Parse an Objective-C protocol expression.
29315 objc-protocol-expression:
29316 @protocol ( identifier )
29318 Returns a representation of the protocol expression. */
29321 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29324 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29326 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29327 matching_parens parens
;
29328 parens
.require_open (parser
);
29329 proto
= cp_parser_identifier (parser
);
29330 parens
.require_close (parser
);
29332 /* Build a location of the form:
29335 with caret==start at the @ token, finishing at the close paren. */
29336 location_t combined_loc
29337 = make_location (start_loc
, start_loc
,
29338 cp_lexer_previous_token (parser
->lexer
)->location
);
29339 tree result
= objc_build_protocol_expr (proto
);
29340 protected_set_expr_location (result
, combined_loc
);
29344 /* Parse an Objective-C selector expression.
29346 objc-selector-expression:
29347 @selector ( objc-method-signature )
29349 objc-method-signature:
29355 objc-selector-seq objc-selector :
29357 Returns a representation of the method selector. */
29360 cp_parser_objc_selector_expression (cp_parser
* parser
)
29362 tree sel_seq
= NULL_TREE
;
29363 bool maybe_unary_selector_p
= true;
29365 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29367 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29368 matching_parens parens
;
29369 parens
.require_open (parser
);
29370 token
= cp_lexer_peek_token (parser
->lexer
);
29372 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29373 || token
->type
== CPP_SCOPE
)
29375 tree selector
= NULL_TREE
;
29377 if (token
->type
!= CPP_COLON
29378 || token
->type
== CPP_SCOPE
)
29379 selector
= cp_parser_objc_selector (parser
);
29381 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29382 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29384 /* Detect if we have a unary selector. */
29385 if (maybe_unary_selector_p
)
29387 sel_seq
= selector
;
29388 goto finish_selector
;
29392 cp_parser_error (parser
, "expected %<:%>");
29395 maybe_unary_selector_p
= false;
29396 token
= cp_lexer_consume_token (parser
->lexer
);
29398 if (token
->type
== CPP_SCOPE
)
29401 = chainon (sel_seq
,
29402 build_tree_list (selector
, NULL_TREE
));
29404 = chainon (sel_seq
,
29405 build_tree_list (NULL_TREE
, NULL_TREE
));
29409 = chainon (sel_seq
,
29410 build_tree_list (selector
, NULL_TREE
));
29412 token
= cp_lexer_peek_token (parser
->lexer
);
29416 parens
.require_close (parser
);
29419 /* Build a location of the form:
29422 with caret==start at the @ token, finishing at the close paren. */
29423 location_t combined_loc
29424 = make_location (loc
, loc
,
29425 cp_lexer_previous_token (parser
->lexer
)->location
);
29426 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29427 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29428 protected_set_expr_location (result
, combined_loc
);
29432 /* Parse a list of identifiers.
29434 objc-identifier-list:
29436 objc-identifier-list , identifier
29438 Returns a TREE_LIST of identifier nodes. */
29441 cp_parser_objc_identifier_list (cp_parser
* parser
)
29447 identifier
= cp_parser_identifier (parser
);
29448 if (identifier
== error_mark_node
)
29449 return error_mark_node
;
29451 list
= build_tree_list (NULL_TREE
, identifier
);
29452 sep
= cp_lexer_peek_token (parser
->lexer
);
29454 while (sep
->type
== CPP_COMMA
)
29456 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29457 identifier
= cp_parser_identifier (parser
);
29458 if (identifier
== error_mark_node
)
29461 list
= chainon (list
, build_tree_list (NULL_TREE
,
29463 sep
= cp_lexer_peek_token (parser
->lexer
);
29469 /* Parse an Objective-C alias declaration.
29471 objc-alias-declaration:
29472 @compatibility_alias identifier identifier ;
29474 This function registers the alias mapping with the Objective-C front end.
29475 It returns nothing. */
29478 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29482 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29483 alias
= cp_parser_identifier (parser
);
29484 orig
= cp_parser_identifier (parser
);
29485 objc_declare_alias (alias
, orig
);
29486 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29489 /* Parse an Objective-C class forward-declaration.
29491 objc-class-declaration:
29492 @class objc-identifier-list ;
29494 The function registers the forward declarations with the Objective-C
29495 front end. It returns nothing. */
29498 cp_parser_objc_class_declaration (cp_parser
* parser
)
29500 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29505 id
= cp_parser_identifier (parser
);
29506 if (id
== error_mark_node
)
29509 objc_declare_class (id
);
29511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29512 cp_lexer_consume_token (parser
->lexer
);
29516 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29519 /* Parse a list of Objective-C protocol references.
29521 objc-protocol-refs-opt:
29522 objc-protocol-refs [opt]
29524 objc-protocol-refs:
29525 < objc-identifier-list >
29527 Returns a TREE_LIST of identifiers, if any. */
29530 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29532 tree protorefs
= NULL_TREE
;
29534 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29536 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29537 protorefs
= cp_parser_objc_identifier_list (parser
);
29538 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29544 /* Parse a Objective-C visibility specification. */
29547 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29549 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29551 switch (vis
->keyword
)
29553 case RID_AT_PRIVATE
:
29554 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29556 case RID_AT_PROTECTED
:
29557 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29559 case RID_AT_PUBLIC
:
29560 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29562 case RID_AT_PACKAGE
:
29563 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29569 /* Eat '@private'/'@protected'/'@public'. */
29570 cp_lexer_consume_token (parser
->lexer
);
29573 /* Parse an Objective-C method type. Return 'true' if it is a class
29574 (+) method, and 'false' if it is an instance (-) method. */
29577 cp_parser_objc_method_type (cp_parser
* parser
)
29579 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29585 /* Parse an Objective-C protocol qualifier. */
29588 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29590 tree quals
= NULL_TREE
, node
;
29591 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29593 node
= token
->u
.value
;
29595 while (node
&& identifier_p (node
)
29596 && (node
== ridpointers
[(int) RID_IN
]
29597 || node
== ridpointers
[(int) RID_OUT
]
29598 || node
== ridpointers
[(int) RID_INOUT
]
29599 || node
== ridpointers
[(int) RID_BYCOPY
]
29600 || node
== ridpointers
[(int) RID_BYREF
]
29601 || node
== ridpointers
[(int) RID_ONEWAY
]))
29603 quals
= tree_cons (NULL_TREE
, node
, quals
);
29604 cp_lexer_consume_token (parser
->lexer
);
29605 token
= cp_lexer_peek_token (parser
->lexer
);
29606 node
= token
->u
.value
;
29612 /* Parse an Objective-C typename. */
29615 cp_parser_objc_typename (cp_parser
* parser
)
29617 tree type_name
= NULL_TREE
;
29619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29621 tree proto_quals
, cp_type
= NULL_TREE
;
29623 matching_parens parens
;
29624 parens
.consume_open (parser
); /* Eat '('. */
29625 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29627 /* An ObjC type name may consist of just protocol qualifiers, in which
29628 case the type shall default to 'id'. */
29629 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29631 cp_type
= cp_parser_type_id (parser
);
29633 /* If the type could not be parsed, an error has already
29634 been produced. For error recovery, behave as if it had
29635 not been specified, which will use the default type
29637 if (cp_type
== error_mark_node
)
29639 cp_type
= NULL_TREE
;
29640 /* We need to skip to the closing parenthesis as
29641 cp_parser_type_id() does not seem to do it for
29643 cp_parser_skip_to_closing_parenthesis (parser
,
29644 /*recovering=*/true,
29645 /*or_comma=*/false,
29646 /*consume_paren=*/false);
29650 parens
.require_close (parser
);
29651 type_name
= build_tree_list (proto_quals
, cp_type
);
29657 /* Check to see if TYPE refers to an Objective-C selector name. */
29660 cp_parser_objc_selector_p (enum cpp_ttype type
)
29662 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29663 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29664 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29665 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29666 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29669 /* Parse an Objective-C selector. */
29672 cp_parser_objc_selector (cp_parser
* parser
)
29674 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29676 if (!cp_parser_objc_selector_p (token
->type
))
29678 error_at (token
->location
, "invalid Objective-C++ selector name");
29679 return error_mark_node
;
29682 /* C++ operator names are allowed to appear in ObjC selectors. */
29683 switch (token
->type
)
29685 case CPP_AND_AND
: return get_identifier ("and");
29686 case CPP_AND_EQ
: return get_identifier ("and_eq");
29687 case CPP_AND
: return get_identifier ("bitand");
29688 case CPP_OR
: return get_identifier ("bitor");
29689 case CPP_COMPL
: return get_identifier ("compl");
29690 case CPP_NOT
: return get_identifier ("not");
29691 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29692 case CPP_OR_OR
: return get_identifier ("or");
29693 case CPP_OR_EQ
: return get_identifier ("or_eq");
29694 case CPP_XOR
: return get_identifier ("xor");
29695 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29696 default: return token
->u
.value
;
29700 /* Parse an Objective-C params list. */
29703 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29705 tree params
= NULL_TREE
;
29706 bool maybe_unary_selector_p
= true;
29707 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29709 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29711 tree selector
= NULL_TREE
, type_name
, identifier
;
29712 tree parm_attr
= NULL_TREE
;
29714 if (token
->keyword
== RID_ATTRIBUTE
)
29717 if (token
->type
!= CPP_COLON
)
29718 selector
= cp_parser_objc_selector (parser
);
29720 /* Detect if we have a unary selector. */
29721 if (maybe_unary_selector_p
29722 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29724 params
= selector
; /* Might be followed by attributes. */
29728 maybe_unary_selector_p
= false;
29729 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29731 /* Something went quite wrong. There should be a colon
29732 here, but there is not. Stop parsing parameters. */
29735 type_name
= cp_parser_objc_typename (parser
);
29736 /* New ObjC allows attributes on parameters too. */
29737 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29738 parm_attr
= cp_parser_attributes_opt (parser
);
29739 identifier
= cp_parser_identifier (parser
);
29743 objc_build_keyword_decl (selector
,
29748 token
= cp_lexer_peek_token (parser
->lexer
);
29751 if (params
== NULL_TREE
)
29753 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29754 return error_mark_node
;
29757 /* We allow tail attributes for the method. */
29758 if (token
->keyword
== RID_ATTRIBUTE
)
29760 *attributes
= cp_parser_attributes_opt (parser
);
29761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29762 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29764 cp_parser_error (parser
,
29765 "method attributes must be specified at the end");
29766 return error_mark_node
;
29769 if (params
== NULL_TREE
)
29771 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29772 return error_mark_node
;
29777 /* Parse the non-keyword Objective-C params. */
29780 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29783 tree params
= make_node (TREE_LIST
);
29784 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29785 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29787 while (token
->type
== CPP_COMMA
)
29789 cp_parameter_declarator
*parmdecl
;
29792 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29793 token
= cp_lexer_peek_token (parser
->lexer
);
29795 if (token
->type
== CPP_ELLIPSIS
)
29797 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29799 token
= cp_lexer_peek_token (parser
->lexer
);
29803 /* TODO: parse attributes for tail parameters. */
29804 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29805 parm
= grokdeclarator (parmdecl
->declarator
,
29806 &parmdecl
->decl_specifiers
,
29807 PARM
, /*initialized=*/0,
29808 /*attrlist=*/NULL
);
29810 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29811 token
= cp_lexer_peek_token (parser
->lexer
);
29814 /* We allow tail attributes for the method. */
29815 if (token
->keyword
== RID_ATTRIBUTE
)
29817 if (*attributes
== NULL_TREE
)
29819 *attributes
= cp_parser_attributes_opt (parser
);
29820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29821 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29825 /* We have an error, but parse the attributes, so that we can
29827 *attributes
= cp_parser_attributes_opt (parser
);
29829 cp_parser_error (parser
,
29830 "method attributes must be specified at the end");
29831 return error_mark_node
;
29837 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29840 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29842 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29844 /* If the next token is `extern' and the following token is a string
29845 literal, then we have a linkage specification. */
29846 if (token
->keyword
== RID_EXTERN
29847 && cp_parser_is_pure_string_literal
29848 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
29849 cp_parser_linkage_specification (parser
);
29850 /* Handle #pragma, if any. */
29851 else if (token
->type
== CPP_PRAGMA
)
29852 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
29853 /* Allow stray semicolons. */
29854 else if (token
->type
== CPP_SEMICOLON
)
29855 cp_lexer_consume_token (parser
->lexer
);
29856 /* Mark methods as optional or required, when building protocols. */
29857 else if (token
->keyword
== RID_AT_OPTIONAL
)
29859 cp_lexer_consume_token (parser
->lexer
);
29860 objc_set_method_opt (true);
29862 else if (token
->keyword
== RID_AT_REQUIRED
)
29864 cp_lexer_consume_token (parser
->lexer
);
29865 objc_set_method_opt (false);
29867 else if (token
->keyword
== RID_NAMESPACE
)
29868 cp_parser_namespace_definition (parser
);
29869 /* Other stray characters must generate errors. */
29870 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
29872 cp_lexer_consume_token (parser
->lexer
);
29873 error ("stray %qs between Objective-C++ methods",
29874 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
29876 /* Finally, try to parse a block-declaration, or a function-definition. */
29878 cp_parser_block_declaration (parser
, /*statement_p=*/false);
29881 /* Parse a method signature. */
29884 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
29886 tree rettype
, kwdparms
, optparms
;
29887 bool ellipsis
= false;
29888 bool is_class_method
;
29890 is_class_method
= cp_parser_objc_method_type (parser
);
29891 rettype
= cp_parser_objc_typename (parser
);
29892 *attributes
= NULL_TREE
;
29893 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
29894 if (kwdparms
== error_mark_node
)
29895 return error_mark_node
;
29896 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
29897 if (optparms
== error_mark_node
)
29898 return error_mark_node
;
29900 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
29904 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
29907 cp_lexer_save_tokens (parser
->lexer
);
29908 tattr
= cp_parser_attributes_opt (parser
);
29909 gcc_assert (tattr
) ;
29911 /* If the attributes are followed by a method introducer, this is not allowed.
29912 Dump the attributes and flag the situation. */
29913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
29914 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
29917 /* Otherwise, the attributes introduce some interstitial code, possibly so
29918 rewind to allow that check. */
29919 cp_lexer_rollback_tokens (parser
->lexer
);
29923 /* Parse an Objective-C method prototype list. */
29926 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
29928 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29930 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29932 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29934 tree attributes
, sig
;
29935 bool is_class_method
;
29936 if (token
->type
== CPP_PLUS
)
29937 is_class_method
= true;
29939 is_class_method
= false;
29940 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
29941 if (sig
== error_mark_node
)
29943 cp_parser_skip_to_end_of_block_or_statement (parser
);
29944 token
= cp_lexer_peek_token (parser
->lexer
);
29947 objc_add_method_declaration (is_class_method
, sig
, attributes
);
29948 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29950 else if (token
->keyword
== RID_AT_PROPERTY
)
29951 cp_parser_objc_at_property_declaration (parser
);
29952 else if (token
->keyword
== RID_ATTRIBUTE
29953 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29954 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29956 "prefix attributes are ignored for methods");
29958 /* Allow for interspersed non-ObjC++ code. */
29959 cp_parser_objc_interstitial_code (parser
);
29961 token
= cp_lexer_peek_token (parser
->lexer
);
29964 if (token
->type
!= CPP_EOF
)
29965 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29967 cp_parser_error (parser
, "expected %<@end%>");
29969 objc_finish_interface ();
29972 /* Parse an Objective-C method definition list. */
29975 cp_parser_objc_method_definition_list (cp_parser
* parser
)
29977 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29979 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29983 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29986 tree sig
, attribute
;
29987 bool is_class_method
;
29988 if (token
->type
== CPP_PLUS
)
29989 is_class_method
= true;
29991 is_class_method
= false;
29992 push_deferring_access_checks (dk_deferred
);
29993 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
29994 if (sig
== error_mark_node
)
29996 cp_parser_skip_to_end_of_block_or_statement (parser
);
29997 token
= cp_lexer_peek_token (parser
->lexer
);
30000 objc_start_method_definition (is_class_method
, sig
, attribute
,
30003 /* For historical reasons, we accept an optional semicolon. */
30004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30005 cp_lexer_consume_token (parser
->lexer
);
30007 ptk
= cp_lexer_peek_token (parser
->lexer
);
30008 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30009 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30011 perform_deferred_access_checks (tf_warning_or_error
);
30012 stop_deferring_access_checks ();
30013 meth
= cp_parser_function_definition_after_declarator (parser
,
30015 pop_deferring_access_checks ();
30016 objc_finish_method_definition (meth
);
30019 /* The following case will be removed once @synthesize is
30020 completely implemented. */
30021 else if (token
->keyword
== RID_AT_PROPERTY
)
30022 cp_parser_objc_at_property_declaration (parser
);
30023 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30024 cp_parser_objc_at_synthesize_declaration (parser
);
30025 else if (token
->keyword
== RID_AT_DYNAMIC
)
30026 cp_parser_objc_at_dynamic_declaration (parser
);
30027 else if (token
->keyword
== RID_ATTRIBUTE
30028 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30029 warning_at (token
->location
, OPT_Wattributes
,
30030 "prefix attributes are ignored for methods");
30032 /* Allow for interspersed non-ObjC++ code. */
30033 cp_parser_objc_interstitial_code (parser
);
30035 token
= cp_lexer_peek_token (parser
->lexer
);
30038 if (token
->type
!= CPP_EOF
)
30039 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30041 cp_parser_error (parser
, "expected %<@end%>");
30043 objc_finish_implementation ();
30046 /* Parse Objective-C ivars. */
30049 cp_parser_objc_class_ivars (cp_parser
* parser
)
30051 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30053 if (token
->type
!= CPP_OPEN_BRACE
)
30054 return; /* No ivars specified. */
30056 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30057 token
= cp_lexer_peek_token (parser
->lexer
);
30059 while (token
->type
!= CPP_CLOSE_BRACE
30060 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30062 cp_decl_specifier_seq declspecs
;
30063 int decl_class_or_enum_p
;
30064 tree prefix_attributes
;
30066 cp_parser_objc_visibility_spec (parser
);
30068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30071 cp_parser_decl_specifier_seq (parser
,
30072 CP_PARSER_FLAGS_OPTIONAL
,
30074 &decl_class_or_enum_p
);
30076 /* auto, register, static, extern, mutable. */
30077 if (declspecs
.storage_class
!= sc_none
)
30079 cp_parser_error (parser
, "invalid type for instance variable");
30080 declspecs
.storage_class
= sc_none
;
30083 /* thread_local. */
30084 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30086 cp_parser_error (parser
, "invalid type for instance variable");
30087 declspecs
.locations
[ds_thread
] = 0;
30091 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30093 cp_parser_error (parser
, "invalid type for instance variable");
30094 declspecs
.locations
[ds_typedef
] = 0;
30097 prefix_attributes
= declspecs
.attributes
;
30098 declspecs
.attributes
= NULL_TREE
;
30100 /* Keep going until we hit the `;' at the end of the
30102 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30104 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30105 cp_declarator
*declarator
= NULL
;
30106 int ctor_dtor_or_conv_p
;
30108 /* Check for a (possibly unnamed) bitfield declaration. */
30109 token
= cp_lexer_peek_token (parser
->lexer
);
30110 if (token
->type
== CPP_COLON
)
30113 if (token
->type
== CPP_NAME
30114 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30117 /* Get the name of the bitfield. */
30118 declarator
= make_id_declarator (NULL_TREE
,
30119 cp_parser_identifier (parser
),
30123 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30124 /* Get the width of the bitfield. */
30126 = cp_parser_constant_expression (parser
);
30130 /* Parse the declarator. */
30132 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30133 &ctor_dtor_or_conv_p
,
30134 /*parenthesized_p=*/NULL
,
30135 /*member_p=*/false,
30136 /*friend_p=*/false);
30139 /* Look for attributes that apply to the ivar. */
30140 attributes
= cp_parser_attributes_opt (parser
);
30141 /* Remember which attributes are prefix attributes and
30143 first_attribute
= attributes
;
30144 /* Combine the attributes. */
30145 attributes
= chainon (prefix_attributes
, attributes
);
30148 /* Create the bitfield declaration. */
30149 decl
= grokbitfield (declarator
, &declspecs
,
30150 width
, NULL_TREE
, attributes
);
30152 decl
= grokfield (declarator
, &declspecs
,
30153 NULL_TREE
, /*init_const_expr_p=*/false,
30154 NULL_TREE
, attributes
);
30156 /* Add the instance variable. */
30157 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30158 objc_add_instance_variable (decl
);
30160 /* Reset PREFIX_ATTRIBUTES. */
30161 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30162 attributes
= TREE_CHAIN (attributes
);
30164 TREE_CHAIN (attributes
) = NULL_TREE
;
30166 token
= cp_lexer_peek_token (parser
->lexer
);
30168 if (token
->type
== CPP_COMMA
)
30170 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30176 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30177 token
= cp_lexer_peek_token (parser
->lexer
);
30180 if (token
->keyword
== RID_AT_END
)
30181 cp_parser_error (parser
, "expected %<}%>");
30183 /* Do not consume the RID_AT_END, so it will be read again as terminating
30184 the @interface of @implementation. */
30185 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30186 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30188 /* For historical reasons, we accept an optional semicolon. */
30189 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30190 cp_lexer_consume_token (parser
->lexer
);
30193 /* Parse an Objective-C protocol declaration. */
30196 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30198 tree proto
, protorefs
;
30201 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30202 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30204 tok
= cp_lexer_peek_token (parser
->lexer
);
30205 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30206 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30210 /* See if we have a forward declaration or a definition. */
30211 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30213 /* Try a forward declaration first. */
30214 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30220 id
= cp_parser_identifier (parser
);
30221 if (id
== error_mark_node
)
30224 objc_declare_protocol (id
, attributes
);
30226 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30227 cp_lexer_consume_token (parser
->lexer
);
30231 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30234 /* Ok, we got a full-fledged definition (or at least should). */
30237 proto
= cp_parser_identifier (parser
);
30238 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30239 objc_start_protocol (proto
, protorefs
, attributes
);
30240 cp_parser_objc_method_prototype_list (parser
);
30244 /* Parse an Objective-C superclass or category. */
30247 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30250 tree
*categ
, bool *is_class_extension
)
30252 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30254 *super
= *categ
= NULL_TREE
;
30255 *is_class_extension
= false;
30256 if (next
->type
== CPP_COLON
)
30258 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30259 *super
= cp_parser_identifier (parser
);
30261 else if (next
->type
== CPP_OPEN_PAREN
)
30263 matching_parens parens
;
30264 parens
.consume_open (parser
); /* Eat '('. */
30266 /* If there is no category name, and this is an @interface, we
30267 have a class extension. */
30268 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30270 *categ
= NULL_TREE
;
30271 *is_class_extension
= true;
30274 *categ
= cp_parser_identifier (parser
);
30276 parens
.require_close (parser
);
30280 /* Parse an Objective-C class interface. */
30283 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30285 tree name
, super
, categ
, protos
;
30286 bool is_class_extension
;
30288 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30289 name
= cp_parser_identifier (parser
);
30290 if (name
== error_mark_node
)
30292 /* It's hard to recover because even if valid @interface stuff
30293 is to follow, we can't compile it (or validate it) if we
30294 don't even know which class it refers to. Let's assume this
30295 was a stray '@interface' token in the stream and skip it.
30299 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30300 &is_class_extension
);
30301 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30303 /* We have either a class or a category on our hands. */
30304 if (categ
|| is_class_extension
)
30305 objc_start_category_interface (name
, categ
, protos
, attributes
);
30308 objc_start_class_interface (name
, super
, protos
, attributes
);
30309 /* Handle instance variable declarations, if any. */
30310 cp_parser_objc_class_ivars (parser
);
30311 objc_continue_interface ();
30314 cp_parser_objc_method_prototype_list (parser
);
30317 /* Parse an Objective-C class implementation. */
30320 cp_parser_objc_class_implementation (cp_parser
* parser
)
30322 tree name
, super
, categ
;
30323 bool is_class_extension
;
30325 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30326 name
= cp_parser_identifier (parser
);
30327 if (name
== error_mark_node
)
30329 /* It's hard to recover because even if valid @implementation
30330 stuff is to follow, we can't compile it (or validate it) if
30331 we don't even know which class it refers to. Let's assume
30332 this was a stray '@implementation' token in the stream and
30337 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30338 &is_class_extension
);
30340 /* We have either a class or a category on our hands. */
30342 objc_start_category_implementation (name
, categ
);
30345 objc_start_class_implementation (name
, super
);
30346 /* Handle instance variable declarations, if any. */
30347 cp_parser_objc_class_ivars (parser
);
30348 objc_continue_implementation ();
30351 cp_parser_objc_method_definition_list (parser
);
30354 /* Consume the @end token and finish off the implementation. */
30357 cp_parser_objc_end_implementation (cp_parser
* parser
)
30359 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30360 objc_finish_implementation ();
30363 /* Parse an Objective-C declaration. */
30366 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30368 /* Try to figure out what kind of declaration is present. */
30369 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30372 switch (kwd
->keyword
)
30377 error_at (kwd
->location
, "attributes may not be specified before"
30378 " the %<@%D%> Objective-C++ keyword",
30382 case RID_AT_IMPLEMENTATION
:
30383 warning_at (kwd
->location
, OPT_Wattributes
,
30384 "prefix attributes are ignored before %<@%D%>",
30391 switch (kwd
->keyword
)
30394 cp_parser_objc_alias_declaration (parser
);
30397 cp_parser_objc_class_declaration (parser
);
30399 case RID_AT_PROTOCOL
:
30400 cp_parser_objc_protocol_declaration (parser
, attributes
);
30402 case RID_AT_INTERFACE
:
30403 cp_parser_objc_class_interface (parser
, attributes
);
30405 case RID_AT_IMPLEMENTATION
:
30406 cp_parser_objc_class_implementation (parser
);
30409 cp_parser_objc_end_implementation (parser
);
30412 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30414 cp_parser_skip_to_end_of_block_or_statement (parser
);
30418 /* Parse an Objective-C try-catch-finally statement.
30420 objc-try-catch-finally-stmt:
30421 @try compound-statement objc-catch-clause-seq [opt]
30422 objc-finally-clause [opt]
30424 objc-catch-clause-seq:
30425 objc-catch-clause objc-catch-clause-seq [opt]
30428 @catch ( objc-exception-declaration ) compound-statement
30430 objc-finally-clause:
30431 @finally compound-statement
30433 objc-exception-declaration:
30434 parameter-declaration
30437 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30441 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30442 for C. Keep them in sync. */
30445 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30447 location_t location
;
30450 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30451 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30452 objc_maybe_warn_exceptions (location
);
30453 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30454 node, lest it get absorbed into the surrounding block. */
30455 stmt
= push_stmt_list ();
30456 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30457 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30459 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30461 cp_parameter_declarator
*parm
;
30462 tree parameter_declaration
= error_mark_node
;
30463 bool seen_open_paren
= false;
30464 matching_parens parens
;
30466 cp_lexer_consume_token (parser
->lexer
);
30467 if (parens
.require_open (parser
))
30468 seen_open_paren
= true;
30469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30471 /* We have "@catch (...)" (where the '...' are literally
30472 what is in the code). Skip the '...'.
30473 parameter_declaration is set to NULL_TREE, and
30474 objc_being_catch_clauses() knows that that means
30476 cp_lexer_consume_token (parser
->lexer
);
30477 parameter_declaration
= NULL_TREE
;
30481 /* We have "@catch (NSException *exception)" or something
30482 like that. Parse the parameter declaration. */
30483 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30485 parameter_declaration
= error_mark_node
;
30487 parameter_declaration
= grokdeclarator (parm
->declarator
,
30488 &parm
->decl_specifiers
,
30489 PARM
, /*initialized=*/0,
30490 /*attrlist=*/NULL
);
30492 if (seen_open_paren
)
30493 parens
.require_close (parser
);
30496 /* If there was no open parenthesis, we are recovering from
30497 an error, and we are trying to figure out what mistake
30498 the user has made. */
30500 /* If there is an immediate closing parenthesis, the user
30501 probably forgot the opening one (ie, they typed "@catch
30502 NSException *e)". Parse the closing parenthesis and keep
30504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30505 cp_lexer_consume_token (parser
->lexer
);
30507 /* If these is no immediate closing parenthesis, the user
30508 probably doesn't know that parenthesis are required at
30509 all (ie, they typed "@catch NSException *e"). So, just
30510 forget about the closing parenthesis and keep going. */
30512 objc_begin_catch_clause (parameter_declaration
);
30513 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30514 objc_finish_catch_clause ();
30516 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30518 cp_lexer_consume_token (parser
->lexer
);
30519 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30520 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30521 node, lest it get absorbed into the surrounding block. */
30522 stmt
= push_stmt_list ();
30523 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30524 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30527 return objc_finish_try_stmt ();
30530 /* Parse an Objective-C synchronized statement.
30532 objc-synchronized-stmt:
30533 @synchronized ( expression ) compound-statement
30535 Returns NULL_TREE. */
30538 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30540 location_t location
;
30543 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30545 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30546 objc_maybe_warn_exceptions (location
);
30547 matching_parens parens
;
30548 parens
.require_open (parser
);
30549 lock
= cp_parser_expression (parser
);
30550 parens
.require_close (parser
);
30552 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30553 node, lest it get absorbed into the surrounding block. */
30554 stmt
= push_stmt_list ();
30555 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30557 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30560 /* Parse an Objective-C throw statement.
30563 @throw assignment-expression [opt] ;
30565 Returns a constructed '@throw' statement. */
30568 cp_parser_objc_throw_statement (cp_parser
*parser
)
30570 tree expr
= NULL_TREE
;
30571 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30573 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30575 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30576 expr
= cp_parser_expression (parser
);
30578 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30580 return objc_build_throw_stmt (loc
, expr
);
30583 /* Parse an Objective-C statement. */
30586 cp_parser_objc_statement (cp_parser
* parser
)
30588 /* Try to figure out what kind of declaration is present. */
30589 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30591 switch (kwd
->keyword
)
30594 return cp_parser_objc_try_catch_finally_statement (parser
);
30595 case RID_AT_SYNCHRONIZED
:
30596 return cp_parser_objc_synchronized_statement (parser
);
30598 return cp_parser_objc_throw_statement (parser
);
30600 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30602 cp_parser_skip_to_end_of_block_or_statement (parser
);
30605 return error_mark_node
;
30608 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30609 look ahead to see if an objc keyword follows the attributes. This
30610 is to detect the use of prefix attributes on ObjC @interface and
30614 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30616 cp_lexer_save_tokens (parser
->lexer
);
30617 *attrib
= cp_parser_attributes_opt (parser
);
30618 gcc_assert (*attrib
);
30619 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30621 cp_lexer_commit_tokens (parser
->lexer
);
30624 cp_lexer_rollback_tokens (parser
->lexer
);
30628 /* This routine is a minimal replacement for
30629 c_parser_struct_declaration () used when parsing the list of
30630 types/names or ObjC++ properties. For example, when parsing the
30633 @property (readonly) int a, b, c;
30635 this function is responsible for parsing "int a, int b, int c" and
30636 returning the declarations as CHAIN of DECLs.
30638 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30639 similar parsing. */
30641 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30643 tree decls
= NULL_TREE
;
30644 cp_decl_specifier_seq declspecs
;
30645 int decl_class_or_enum_p
;
30646 tree prefix_attributes
;
30648 cp_parser_decl_specifier_seq (parser
,
30649 CP_PARSER_FLAGS_NONE
,
30651 &decl_class_or_enum_p
);
30653 if (declspecs
.type
== error_mark_node
)
30654 return error_mark_node
;
30656 /* auto, register, static, extern, mutable. */
30657 if (declspecs
.storage_class
!= sc_none
)
30659 cp_parser_error (parser
, "invalid type for property");
30660 declspecs
.storage_class
= sc_none
;
30663 /* thread_local. */
30664 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30666 cp_parser_error (parser
, "invalid type for property");
30667 declspecs
.locations
[ds_thread
] = 0;
30671 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30673 cp_parser_error (parser
, "invalid type for property");
30674 declspecs
.locations
[ds_typedef
] = 0;
30677 prefix_attributes
= declspecs
.attributes
;
30678 declspecs
.attributes
= NULL_TREE
;
30680 /* Keep going until we hit the `;' at the end of the declaration. */
30681 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30683 tree attributes
, first_attribute
, decl
;
30684 cp_declarator
*declarator
;
30687 /* Parse the declarator. */
30688 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30689 NULL
, NULL
, false, false);
30691 /* Look for attributes that apply to the ivar. */
30692 attributes
= cp_parser_attributes_opt (parser
);
30693 /* Remember which attributes are prefix attributes and
30695 first_attribute
= attributes
;
30696 /* Combine the attributes. */
30697 attributes
= chainon (prefix_attributes
, attributes
);
30699 decl
= grokfield (declarator
, &declspecs
,
30700 NULL_TREE
, /*init_const_expr_p=*/false,
30701 NULL_TREE
, attributes
);
30703 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30704 return error_mark_node
;
30706 /* Reset PREFIX_ATTRIBUTES. */
30707 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30708 attributes
= TREE_CHAIN (attributes
);
30710 TREE_CHAIN (attributes
) = NULL_TREE
;
30712 DECL_CHAIN (decl
) = decls
;
30715 token
= cp_lexer_peek_token (parser
->lexer
);
30716 if (token
->type
== CPP_COMMA
)
30718 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30727 /* Parse an Objective-C @property declaration. The syntax is:
30729 objc-property-declaration:
30730 '@property' objc-property-attributes[opt] struct-declaration ;
30732 objc-property-attributes:
30733 '(' objc-property-attribute-list ')'
30735 objc-property-attribute-list:
30736 objc-property-attribute
30737 objc-property-attribute-list, objc-property-attribute
30739 objc-property-attribute
30740 'getter' = identifier
30741 'setter' = identifier
30750 @property NSString *name;
30751 @property (readonly) id object;
30752 @property (retain, nonatomic, getter=getTheName) id name;
30753 @property int a, b, c;
30755 PS: This function is identical to
30756 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30758 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30760 /* The following variables hold the attributes of the properties as
30761 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30762 seen. When we see an attribute, we set them to 'true' (if they
30763 are boolean properties) or to the identifier (if they have an
30764 argument, ie, for getter and setter). Note that here we only
30765 parse the list of attributes, check the syntax and accumulate the
30766 attributes that we find. objc_add_property_declaration() will
30767 then process the information. */
30768 bool property_assign
= false;
30769 bool property_copy
= false;
30770 tree property_getter_ident
= NULL_TREE
;
30771 bool property_nonatomic
= false;
30772 bool property_readonly
= false;
30773 bool property_readwrite
= false;
30774 bool property_retain
= false;
30775 tree property_setter_ident
= NULL_TREE
;
30777 /* 'properties' is the list of properties that we read. Usually a
30778 single one, but maybe more (eg, in "@property int a, b, c;" there
30783 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30785 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30787 /* Parse the optional attribute list... */
30788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30791 matching_parens parens
;
30792 parens
.consume_open (parser
);
30796 bool syntax_error
= false;
30797 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30800 if (token
->type
!= CPP_NAME
)
30802 cp_parser_error (parser
, "expected identifier");
30805 keyword
= C_RID_CODE (token
->u
.value
);
30806 cp_lexer_consume_token (parser
->lexer
);
30809 case RID_ASSIGN
: property_assign
= true; break;
30810 case RID_COPY
: property_copy
= true; break;
30811 case RID_NONATOMIC
: property_nonatomic
= true; break;
30812 case RID_READONLY
: property_readonly
= true; break;
30813 case RID_READWRITE
: property_readwrite
= true; break;
30814 case RID_RETAIN
: property_retain
= true; break;
30818 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30820 if (keyword
== RID_GETTER
)
30821 cp_parser_error (parser
,
30822 "missing %<=%> (after %<getter%> attribute)");
30824 cp_parser_error (parser
,
30825 "missing %<=%> (after %<setter%> attribute)");
30826 syntax_error
= true;
30829 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30830 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30832 cp_parser_error (parser
, "expected identifier");
30833 syntax_error
= true;
30836 if (keyword
== RID_SETTER
)
30838 if (property_setter_ident
!= NULL_TREE
)
30840 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
30841 cp_lexer_consume_token (parser
->lexer
);
30844 property_setter_ident
= cp_parser_objc_selector (parser
);
30845 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30846 cp_parser_error (parser
, "setter name must terminate with %<:%>");
30848 cp_lexer_consume_token (parser
->lexer
);
30852 if (property_getter_ident
!= NULL_TREE
)
30854 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
30855 cp_lexer_consume_token (parser
->lexer
);
30858 property_getter_ident
= cp_parser_objc_selector (parser
);
30862 cp_parser_error (parser
, "unknown property attribute");
30863 syntax_error
= true;
30870 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30871 cp_lexer_consume_token (parser
->lexer
);
30876 /* FIXME: "@property (setter, assign);" will generate a spurious
30877 "error: expected ‘)’ before ‘,’ token". This is because
30878 cp_parser_require, unlike the C counterpart, will produce an
30879 error even if we are in error recovery. */
30880 if (!parens
.require_close (parser
))
30882 cp_parser_skip_to_closing_parenthesis (parser
,
30883 /*recovering=*/true,
30884 /*or_comma=*/false,
30885 /*consume_paren=*/true);
30889 /* ... and the property declaration(s). */
30890 properties
= cp_parser_objc_struct_declaration (parser
);
30892 if (properties
== error_mark_node
)
30894 cp_parser_skip_to_end_of_statement (parser
);
30895 /* If the next token is now a `;', consume it. */
30896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30897 cp_lexer_consume_token (parser
->lexer
);
30901 if (properties
== NULL_TREE
)
30902 cp_parser_error (parser
, "expected identifier");
30905 /* Comma-separated properties are chained together in
30906 reverse order; add them one by one. */
30907 properties
= nreverse (properties
);
30909 for (; properties
; properties
= TREE_CHAIN (properties
))
30910 objc_add_property_declaration (loc
, copy_node (properties
),
30911 property_readonly
, property_readwrite
,
30912 property_assign
, property_retain
,
30913 property_copy
, property_nonatomic
,
30914 property_getter_ident
, property_setter_ident
);
30917 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30920 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30922 objc-synthesize-declaration:
30923 @synthesize objc-synthesize-identifier-list ;
30925 objc-synthesize-identifier-list:
30926 objc-synthesize-identifier
30927 objc-synthesize-identifier-list, objc-synthesize-identifier
30929 objc-synthesize-identifier
30931 identifier = identifier
30934 @synthesize MyProperty;
30935 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30937 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30938 for C. Keep them in sync.
30941 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
30943 tree list
= NULL_TREE
;
30945 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30947 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
30950 tree property
, ivar
;
30951 property
= cp_parser_identifier (parser
);
30952 if (property
== error_mark_node
)
30954 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
30959 cp_lexer_consume_token (parser
->lexer
);
30960 ivar
= cp_parser_identifier (parser
);
30961 if (ivar
== error_mark_node
)
30963 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30969 list
= chainon (list
, build_tree_list (ivar
, property
));
30970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30971 cp_lexer_consume_token (parser
->lexer
);
30975 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30976 objc_add_synthesize_declaration (loc
, list
);
30979 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30981 objc-dynamic-declaration:
30982 @dynamic identifier-list ;
30985 @dynamic MyProperty;
30986 @dynamic MyProperty, AnotherProperty;
30988 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30989 for C. Keep them in sync.
30992 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
30994 tree list
= NULL_TREE
;
30996 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30998 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31002 property
= cp_parser_identifier (parser
);
31003 if (property
== error_mark_node
)
31005 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31008 list
= chainon (list
, build_tree_list (NULL
, property
));
31009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31010 cp_lexer_consume_token (parser
->lexer
);
31014 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31015 objc_add_dynamic_declaration (loc
, list
);
31019 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31021 /* Returns name of the next clause.
31022 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31023 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31024 returned and the token is consumed. */
31026 static pragma_omp_clause
31027 cp_parser_omp_clause_name (cp_parser
*parser
)
31029 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31031 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31032 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31033 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31034 result
= PRAGMA_OMP_CLAUSE_IF
;
31035 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31036 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31037 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31038 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31039 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31040 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31041 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31042 result
= PRAGMA_OMP_CLAUSE_FOR
;
31043 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31045 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31046 const char *p
= IDENTIFIER_POINTER (id
);
31051 if (!strcmp ("aligned", p
))
31052 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31053 else if (!strcmp ("async", p
))
31054 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31057 if (!strcmp ("collapse", p
))
31058 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31059 else if (!strcmp ("copy", p
))
31060 result
= PRAGMA_OACC_CLAUSE_COPY
;
31061 else if (!strcmp ("copyin", p
))
31062 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31063 else if (!strcmp ("copyout", p
))
31064 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31065 else if (!strcmp ("copyprivate", p
))
31066 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31067 else if (!strcmp ("create", p
))
31068 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31071 if (!strcmp ("defaultmap", p
))
31072 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31073 else if (!strcmp ("depend", p
))
31074 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31075 else if (!strcmp ("device", p
))
31076 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31077 else if (!strcmp ("deviceptr", p
))
31078 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31079 else if (!strcmp ("device_resident", p
))
31080 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31081 else if (!strcmp ("dist_schedule", p
))
31082 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31085 if (!strcmp ("final", p
))
31086 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31087 else if (!strcmp ("firstprivate", p
))
31088 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31089 else if (!strcmp ("from", p
))
31090 result
= PRAGMA_OMP_CLAUSE_FROM
;
31093 if (!strcmp ("gang", p
))
31094 result
= PRAGMA_OACC_CLAUSE_GANG
;
31095 else if (!strcmp ("grainsize", p
))
31096 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31099 if (!strcmp ("hint", p
))
31100 result
= PRAGMA_OMP_CLAUSE_HINT
;
31101 else if (!strcmp ("host", p
))
31102 result
= PRAGMA_OACC_CLAUSE_HOST
;
31105 if (!strcmp ("inbranch", p
))
31106 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31107 else if (!strcmp ("independent", p
))
31108 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31109 else if (!strcmp ("is_device_ptr", p
))
31110 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31113 if (!strcmp ("lastprivate", p
))
31114 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31115 else if (!strcmp ("linear", p
))
31116 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31117 else if (!strcmp ("link", p
))
31118 result
= PRAGMA_OMP_CLAUSE_LINK
;
31121 if (!strcmp ("map", p
))
31122 result
= PRAGMA_OMP_CLAUSE_MAP
;
31123 else if (!strcmp ("mergeable", p
))
31124 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31127 if (!strcmp ("nogroup", p
))
31128 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31129 else if (!strcmp ("notinbranch", p
))
31130 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31131 else if (!strcmp ("nowait", p
))
31132 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31133 else if (!strcmp ("num_gangs", p
))
31134 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31135 else if (!strcmp ("num_tasks", p
))
31136 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31137 else if (!strcmp ("num_teams", p
))
31138 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31139 else if (!strcmp ("num_threads", p
))
31140 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31141 else if (!strcmp ("num_workers", p
))
31142 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31145 if (!strcmp ("ordered", p
))
31146 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31149 if (!strcmp ("parallel", p
))
31150 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31151 else if (!strcmp ("present", p
))
31152 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31153 else if (!strcmp ("present_or_copy", p
)
31154 || !strcmp ("pcopy", p
))
31155 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31156 else if (!strcmp ("present_or_copyin", p
)
31157 || !strcmp ("pcopyin", p
))
31158 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31159 else if (!strcmp ("present_or_copyout", p
)
31160 || !strcmp ("pcopyout", p
))
31161 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31162 else if (!strcmp ("present_or_create", p
)
31163 || !strcmp ("pcreate", p
))
31164 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31165 else if (!strcmp ("priority", p
))
31166 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31167 else if (!strcmp ("proc_bind", p
))
31168 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31171 if (!strcmp ("reduction", p
))
31172 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31175 if (!strcmp ("safelen", p
))
31176 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31177 else if (!strcmp ("schedule", p
))
31178 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31179 else if (!strcmp ("sections", p
))
31180 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31181 else if (!strcmp ("self", p
))
31182 result
= PRAGMA_OACC_CLAUSE_SELF
;
31183 else if (!strcmp ("seq", p
))
31184 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31185 else if (!strcmp ("shared", p
))
31186 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31187 else if (!strcmp ("simd", p
))
31188 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31189 else if (!strcmp ("simdlen", p
))
31190 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31193 if (!strcmp ("taskgroup", p
))
31194 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31195 else if (!strcmp ("thread_limit", p
))
31196 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31197 else if (!strcmp ("threads", p
))
31198 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31199 else if (!strcmp ("tile", p
))
31200 result
= PRAGMA_OACC_CLAUSE_TILE
;
31201 else if (!strcmp ("to", p
))
31202 result
= PRAGMA_OMP_CLAUSE_TO
;
31205 if (!strcmp ("uniform", p
))
31206 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31207 else if (!strcmp ("untied", p
))
31208 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31209 else if (!strcmp ("use_device", p
))
31210 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31211 else if (!strcmp ("use_device_ptr", p
))
31212 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31215 if (!strcmp ("vector", p
))
31216 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31217 else if (!strcmp ("vector_length", p
))
31218 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31221 if (!strcmp ("wait", p
))
31222 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31223 else if (!strcmp ("worker", p
))
31224 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31229 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31230 cp_lexer_consume_token (parser
->lexer
);
31235 /* Validate that a clause of the given type does not already exist. */
31238 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31239 const char *name
, location_t location
)
31243 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31244 if (OMP_CLAUSE_CODE (c
) == code
)
31246 error_at (location
, "too many %qs clauses", name
);
31254 variable-list , identifier
31256 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31257 colon). An opening parenthesis will have been consumed by the caller.
31259 If KIND is nonzero, create the appropriate node and install the decl
31260 in OMP_CLAUSE_DECL and add the node to the head of the list.
31262 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31263 return the list created.
31265 COLON can be NULL if only closing parenthesis should end the list,
31266 or pointer to bool which will receive false if the list is terminated
31267 by closing parenthesis or true if the list is terminated by colon. */
31270 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31271 tree list
, bool *colon
)
31274 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31277 parser
->colon_corrects_to_scope_p
= false;
31284 token
= cp_lexer_peek_token (parser
->lexer
);
31286 && current_class_ptr
31287 && cp_parser_is_keyword (token
, RID_THIS
))
31289 decl
= finish_this_expr ();
31290 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31291 || CONVERT_EXPR_P (decl
))
31292 decl
= TREE_OPERAND (decl
, 0);
31293 cp_lexer_consume_token (parser
->lexer
);
31297 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31298 /*check_dependency_p=*/true,
31299 /*template_p=*/NULL
,
31300 /*declarator_p=*/false,
31301 /*optional_p=*/false);
31302 if (name
== error_mark_node
)
31305 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31306 if (decl
== error_mark_node
)
31307 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31310 if (decl
== error_mark_node
)
31312 else if (kind
!= 0)
31316 case OMP_CLAUSE__CACHE_
:
31317 /* The OpenACC cache directive explicitly only allows "array
31318 elements or subarrays". */
31319 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31321 error_at (token
->location
, "expected %<[%>");
31322 decl
= error_mark_node
;
31326 case OMP_CLAUSE_MAP
:
31327 case OMP_CLAUSE_FROM
:
31328 case OMP_CLAUSE_TO
:
31329 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31332 = cp_lexer_peek_token (parser
->lexer
)->location
;
31333 cp_id_kind idk
= CP_ID_KIND_NONE
;
31334 cp_lexer_consume_token (parser
->lexer
);
31335 decl
= convert_from_reference (decl
);
31337 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31342 case OMP_CLAUSE_DEPEND
:
31343 case OMP_CLAUSE_REDUCTION
:
31344 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31346 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31348 parser
->colon_corrects_to_scope_p
= false;
31349 cp_lexer_consume_token (parser
->lexer
);
31350 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31351 low_bound
= cp_parser_expression (parser
);
31353 parser
->colon_corrects_to_scope_p
31354 = saved_colon_corrects_to_scope_p
;
31355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31356 length
= integer_one_node
;
31359 /* Look for `:'. */
31360 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31362 if (!cp_lexer_next_token_is (parser
->lexer
,
31364 length
= cp_parser_expression (parser
);
31366 /* Look for the closing `]'. */
31367 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31371 decl
= tree_cons (low_bound
, length
, decl
);
31378 tree u
= build_omp_clause (token
->location
, kind
);
31379 OMP_CLAUSE_DECL (u
) = decl
;
31380 OMP_CLAUSE_CHAIN (u
) = list
;
31384 list
= tree_cons (decl
, NULL_TREE
, list
);
31387 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31389 cp_lexer_consume_token (parser
->lexer
);
31393 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31395 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31398 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31402 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31406 /* Try to resync to an unnested comma. Copied from
31407 cp_parser_parenthesized_expression_list. */
31410 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31411 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31412 /*recovering=*/true,
31414 /*consume_paren=*/true);
31422 /* Similarly, but expect leading and trailing parenthesis. This is a very
31423 common case for omp clauses. */
31426 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31428 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31429 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31434 copy ( variable-list )
31435 copyin ( variable-list )
31436 copyout ( variable-list )
31437 create ( variable-list )
31438 delete ( variable-list )
31439 present ( variable-list )
31440 present_or_copy ( variable-list )
31441 pcopy ( variable-list )
31442 present_or_copyin ( variable-list )
31443 pcopyin ( variable-list )
31444 present_or_copyout ( variable-list )
31445 pcopyout ( variable-list )
31446 present_or_create ( variable-list )
31447 pcreate ( variable-list ) */
31450 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31453 enum gomp_map_kind kind
;
31456 case PRAGMA_OACC_CLAUSE_COPY
:
31457 kind
= GOMP_MAP_FORCE_TOFROM
;
31459 case PRAGMA_OACC_CLAUSE_COPYIN
:
31460 kind
= GOMP_MAP_FORCE_TO
;
31462 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31463 kind
= GOMP_MAP_FORCE_FROM
;
31465 case PRAGMA_OACC_CLAUSE_CREATE
:
31466 kind
= GOMP_MAP_FORCE_ALLOC
;
31468 case PRAGMA_OACC_CLAUSE_DELETE
:
31469 kind
= GOMP_MAP_DELETE
;
31471 case PRAGMA_OACC_CLAUSE_DEVICE
:
31472 kind
= GOMP_MAP_FORCE_TO
;
31474 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31475 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31477 case PRAGMA_OACC_CLAUSE_HOST
:
31478 case PRAGMA_OACC_CLAUSE_SELF
:
31479 kind
= GOMP_MAP_FORCE_FROM
;
31481 case PRAGMA_OACC_CLAUSE_LINK
:
31482 kind
= GOMP_MAP_LINK
;
31484 case PRAGMA_OACC_CLAUSE_PRESENT
:
31485 kind
= GOMP_MAP_FORCE_PRESENT
;
31487 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31488 kind
= GOMP_MAP_TOFROM
;
31490 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31491 kind
= GOMP_MAP_TO
;
31493 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31494 kind
= GOMP_MAP_FROM
;
31496 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31497 kind
= GOMP_MAP_ALLOC
;
31500 gcc_unreachable ();
31503 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31505 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31506 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31512 deviceptr ( variable-list ) */
31515 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31517 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31520 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31521 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31522 variable-list must only allow for pointer variables. */
31523 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31524 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31526 tree v
= TREE_PURPOSE (t
);
31527 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31528 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31529 OMP_CLAUSE_DECL (u
) = v
;
31530 OMP_CLAUSE_CHAIN (u
) = list
;
31544 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31545 enum omp_clause_code code
,
31546 tree list
, location_t location
)
31548 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31549 tree c
= build_omp_clause (location
, code
);
31550 OMP_CLAUSE_CHAIN (c
) = list
;
31555 num_gangs ( expression )
31556 num_workers ( expression )
31557 vector_length ( expression ) */
31560 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31561 const char *str
, tree list
)
31563 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31565 matching_parens parens
;
31566 if (!parens
.require_open (parser
))
31569 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31571 if (t
== error_mark_node
31572 || !parens
.require_close (parser
))
31574 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31575 /*or_comma=*/false,
31576 /*consume_paren=*/true);
31580 check_no_duplicate_clause (list
, code
, str
, loc
);
31582 tree c
= build_omp_clause (loc
, code
);
31583 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31584 OMP_CLAUSE_CHAIN (c
) = list
;
31590 gang [( gang-arg-list )]
31591 worker [( [num:] int-expr )]
31592 vector [( [length:] int-expr )]
31594 where gang-arg is one of:
31599 and size-expr may be:
31606 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31607 const char *str
, tree list
)
31609 const char *id
= "num";
31610 cp_lexer
*lexer
= parser
->lexer
;
31611 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31612 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31614 if (kind
== OMP_CLAUSE_VECTOR
)
31617 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31619 matching_parens parens
;
31620 parens
.consume_open (parser
);
31624 cp_token
*next
= cp_lexer_peek_token (lexer
);
31627 /* Gang static argument. */
31628 if (kind
== OMP_CLAUSE_GANG
31629 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31631 cp_lexer_consume_token (lexer
);
31633 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31634 goto cleanup_error
;
31637 if (ops
[idx
] != NULL
)
31639 cp_parser_error (parser
, "too many %<static%> arguments");
31640 goto cleanup_error
;
31643 /* Check for the '*' argument. */
31644 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31645 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31646 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31649 cp_lexer_consume_token (lexer
);
31650 ops
[idx
] = integer_minus_one_node
;
31652 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31654 cp_lexer_consume_token (lexer
);
31660 /* Worker num: argument and vector length: arguments. */
31661 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31662 && id_equal (next
->u
.value
, id
)
31663 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31665 cp_lexer_consume_token (lexer
); /* id */
31666 cp_lexer_consume_token (lexer
); /* ':' */
31669 /* Now collect the actual argument. */
31670 if (ops
[idx
] != NULL_TREE
)
31672 cp_parser_error (parser
, "unexpected argument");
31673 goto cleanup_error
;
31676 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31678 if (expr
== error_mark_node
)
31679 goto cleanup_error
;
31681 mark_exp_read (expr
);
31684 if (kind
== OMP_CLAUSE_GANG
31685 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31687 cp_lexer_consume_token (lexer
);
31694 if (!parens
.require_close (parser
))
31695 goto cleanup_error
;
31698 check_no_duplicate_clause (list
, kind
, str
, loc
);
31700 c
= build_omp_clause (loc
, kind
);
31703 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31705 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31706 OMP_CLAUSE_CHAIN (c
) = list
;
31711 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31716 tile ( size-expr-list ) */
31719 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31721 tree c
, expr
= error_mark_node
;
31722 tree tile
= NULL_TREE
;
31724 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31725 so, but the spec authors never considered such a case and have
31726 differing opinions on what it might mean, including 'not
31728 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31729 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31732 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31737 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31740 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31741 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31742 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31744 cp_lexer_consume_token (parser
->lexer
);
31745 expr
= integer_zero_node
;
31748 expr
= cp_parser_constant_expression (parser
);
31750 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31752 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31754 /* Consume the trailing ')'. */
31755 cp_lexer_consume_token (parser
->lexer
);
31757 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31758 tile
= nreverse (tile
);
31759 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31760 OMP_CLAUSE_CHAIN (c
) = list
;
31765 Parse wait clause or directive parameters. */
31768 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31770 vec
<tree
, va_gc
> *args
;
31773 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31775 /*allow_expansion_p=*/true,
31776 /*non_constant_p=*/NULL
);
31778 if (args
== NULL
|| args
->length () == 0)
31780 cp_parser_error (parser
, "expected integer expression before ')'");
31782 release_tree_vector (args
);
31786 args_tree
= build_tree_list_vec (args
);
31788 release_tree_vector (args
);
31790 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31792 tree targ
= TREE_VALUE (t
);
31794 if (targ
!= error_mark_node
)
31796 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31797 error ("%<wait%> expression must be integral");
31800 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31802 targ
= mark_rvalue_use (targ
);
31803 OMP_CLAUSE_DECL (c
) = targ
;
31804 OMP_CLAUSE_CHAIN (c
) = list
;
31814 wait ( int-expr-list ) */
31817 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31819 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31821 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31824 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31830 collapse ( constant-expression ) */
31833 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
31839 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31840 matching_parens parens
;
31841 if (!parens
.require_open (parser
))
31844 num
= cp_parser_constant_expression (parser
);
31846 if (!parens
.require_close (parser
))
31847 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31848 /*or_comma=*/false,
31849 /*consume_paren=*/true);
31851 if (num
== error_mark_node
)
31853 num
= fold_non_dependent_expr (num
);
31854 if (!tree_fits_shwi_p (num
)
31855 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31856 || (n
= tree_to_shwi (num
)) <= 0
31859 error_at (loc
, "collapse argument needs positive constant integer expression");
31863 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
31864 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
31865 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
31866 OMP_CLAUSE_CHAIN (c
) = list
;
31867 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
31873 default ( none | shared )
31876 default ( none | present ) */
31879 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
31880 location_t location
, bool is_oacc
)
31882 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
31885 matching_parens parens
;
31886 if (!parens
.require_open (parser
))
31888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31890 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31891 const char *p
= IDENTIFIER_POINTER (id
);
31896 if (strcmp ("none", p
) != 0)
31898 kind
= OMP_CLAUSE_DEFAULT_NONE
;
31902 if (strcmp ("present", p
) != 0 || !is_oacc
)
31904 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
31908 if (strcmp ("shared", p
) != 0 || is_oacc
)
31910 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
31917 cp_lexer_consume_token (parser
->lexer
);
31923 cp_parser_error (parser
, "expected %<none%> or %<present%>");
31925 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
31928 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
31929 || !parens
.require_close (parser
))
31930 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31931 /*or_comma=*/false,
31932 /*consume_paren=*/true);
31934 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
31937 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
31938 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
31939 OMP_CLAUSE_CHAIN (c
) = list
;
31940 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
31946 final ( expression ) */
31949 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
31953 matching_parens parens
;
31954 if (!parens
.require_open (parser
))
31957 t
= cp_parser_condition (parser
);
31959 if (t
== error_mark_node
31960 || !parens
.require_close (parser
))
31961 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31962 /*or_comma=*/false,
31963 /*consume_paren=*/true);
31965 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
31967 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
31968 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
31969 OMP_CLAUSE_CHAIN (c
) = list
;
31978 if ( directive-name-modifier : expression )
31980 directive-name-modifier:
31981 parallel | task | taskloop | target data | target | target update
31982 | target enter data | target exit data */
31985 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
31989 enum tree_code if_modifier
= ERROR_MARK
;
31991 matching_parens parens
;
31992 if (!parens
.require_open (parser
))
31995 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31997 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31998 const char *p
= IDENTIFIER_POINTER (id
);
32001 if (strcmp ("parallel", p
) == 0)
32002 if_modifier
= OMP_PARALLEL
;
32003 else if (strcmp ("task", p
) == 0)
32004 if_modifier
= OMP_TASK
;
32005 else if (strcmp ("taskloop", p
) == 0)
32006 if_modifier
= OMP_TASKLOOP
;
32007 else if (strcmp ("target", p
) == 0)
32009 if_modifier
= OMP_TARGET
;
32010 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32012 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32013 p
= IDENTIFIER_POINTER (id
);
32014 if (strcmp ("data", p
) == 0)
32015 if_modifier
= OMP_TARGET_DATA
;
32016 else if (strcmp ("update", p
) == 0)
32017 if_modifier
= OMP_TARGET_UPDATE
;
32018 else if (strcmp ("enter", p
) == 0)
32019 if_modifier
= OMP_TARGET_ENTER_DATA
;
32020 else if (strcmp ("exit", p
) == 0)
32021 if_modifier
= OMP_TARGET_EXIT_DATA
;
32022 if (if_modifier
!= OMP_TARGET
)
32027 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32028 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32030 if_modifier
= ERROR_MARK
;
32032 if (if_modifier
== OMP_TARGET_ENTER_DATA
32033 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32035 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32037 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32038 p
= IDENTIFIER_POINTER (id
);
32039 if (strcmp ("data", p
) == 0)
32045 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32046 error_at (loc
, "expected %<data%>");
32047 if_modifier
= ERROR_MARK
;
32052 if (if_modifier
!= ERROR_MARK
)
32054 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32057 cp_lexer_consume_token (parser
->lexer
);
32064 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32065 error_at (loc
, "expected %<:%>");
32067 if_modifier
= ERROR_MARK
;
32072 t
= cp_parser_condition (parser
);
32074 if (t
== error_mark_node
32075 || !parens
.require_close (parser
))
32076 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32077 /*or_comma=*/false,
32078 /*consume_paren=*/true);
32080 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32081 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32083 if (if_modifier
!= ERROR_MARK
32084 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32086 const char *p
= NULL
;
32087 switch (if_modifier
)
32089 case OMP_PARALLEL
: p
= "parallel"; break;
32090 case OMP_TASK
: p
= "task"; break;
32091 case OMP_TASKLOOP
: p
= "taskloop"; break;
32092 case OMP_TARGET_DATA
: p
= "target data"; break;
32093 case OMP_TARGET
: p
= "target"; break;
32094 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32095 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32096 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32097 default: gcc_unreachable ();
32099 error_at (location
, "too many %<if%> clauses with %qs modifier",
32103 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32106 error_at (location
, "too many %<if%> clauses");
32108 error_at (location
, "too many %<if%> clauses without modifier");
32111 else if (if_modifier
== ERROR_MARK
32112 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32114 error_at (location
, "if any %<if%> clause has modifier, then all "
32115 "%<if%> clauses have to use modifier");
32120 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32121 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32122 OMP_CLAUSE_IF_EXPR (c
) = t
;
32123 OMP_CLAUSE_CHAIN (c
) = list
;
32132 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32133 tree list
, location_t location
)
32137 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32140 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32141 OMP_CLAUSE_CHAIN (c
) = list
;
32149 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32150 tree list
, location_t location
)
32154 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32156 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32157 OMP_CLAUSE_CHAIN (c
) = list
;
32162 num_threads ( expression ) */
32165 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32166 location_t location
)
32170 matching_parens parens
;
32171 if (!parens
.require_open (parser
))
32174 t
= cp_parser_expression (parser
);
32176 if (t
== error_mark_node
32177 || !parens
.require_close (parser
))
32178 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32179 /*or_comma=*/false,
32180 /*consume_paren=*/true);
32182 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32183 "num_threads", location
);
32185 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32186 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32187 OMP_CLAUSE_CHAIN (c
) = list
;
32193 num_tasks ( expression ) */
32196 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32197 location_t location
)
32201 matching_parens parens
;
32202 if (!parens
.require_open (parser
))
32205 t
= cp_parser_expression (parser
);
32207 if (t
== error_mark_node
32208 || !parens
.require_close (parser
))
32209 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32210 /*or_comma=*/false,
32211 /*consume_paren=*/true);
32213 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32214 "num_tasks", location
);
32216 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32217 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32218 OMP_CLAUSE_CHAIN (c
) = list
;
32224 grainsize ( expression ) */
32227 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32228 location_t location
)
32232 matching_parens parens
;
32233 if (!parens
.require_open (parser
))
32236 t
= cp_parser_expression (parser
);
32238 if (t
== error_mark_node
32239 || !parens
.require_close (parser
))
32240 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32241 /*or_comma=*/false,
32242 /*consume_paren=*/true);
32244 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32245 "grainsize", location
);
32247 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32248 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32249 OMP_CLAUSE_CHAIN (c
) = list
;
32255 priority ( expression ) */
32258 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32259 location_t location
)
32263 matching_parens parens
;
32264 if (!parens
.require_open (parser
))
32267 t
= cp_parser_expression (parser
);
32269 if (t
== error_mark_node
32270 || !parens
.require_close (parser
))
32271 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32272 /*or_comma=*/false,
32273 /*consume_paren=*/true);
32275 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32276 "priority", location
);
32278 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32279 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32280 OMP_CLAUSE_CHAIN (c
) = list
;
32286 hint ( expression ) */
32289 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32290 location_t location
)
32294 matching_parens parens
;
32295 if (!parens
.require_open (parser
))
32298 t
= cp_parser_expression (parser
);
32300 if (t
== error_mark_node
32301 || !parens
.require_close (parser
))
32302 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32303 /*or_comma=*/false,
32304 /*consume_paren=*/true);
32306 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32308 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32309 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32310 OMP_CLAUSE_CHAIN (c
) = list
;
32316 defaultmap ( tofrom : scalar ) */
32319 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32320 location_t location
)
32325 matching_parens parens
;
32326 if (!parens
.require_open (parser
))
32329 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32331 cp_parser_error (parser
, "expected %<tofrom%>");
32334 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32335 p
= IDENTIFIER_POINTER (id
);
32336 if (strcmp (p
, "tofrom") != 0)
32338 cp_parser_error (parser
, "expected %<tofrom%>");
32341 cp_lexer_consume_token (parser
->lexer
);
32342 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32345 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32347 cp_parser_error (parser
, "expected %<scalar%>");
32350 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32351 p
= IDENTIFIER_POINTER (id
);
32352 if (strcmp (p
, "scalar") != 0)
32354 cp_parser_error (parser
, "expected %<scalar%>");
32357 cp_lexer_consume_token (parser
->lexer
);
32358 if (!parens
.require_close (parser
))
32361 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32364 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32365 OMP_CLAUSE_CHAIN (c
) = list
;
32369 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32370 /*or_comma=*/false,
32371 /*consume_paren=*/true);
32379 ordered ( constant-expression ) */
32382 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32383 tree list
, location_t location
)
32385 tree c
, num
= NULL_TREE
;
32388 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32389 "ordered", location
);
32391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32393 matching_parens parens
;
32394 parens
.consume_open (parser
);
32396 num
= cp_parser_constant_expression (parser
);
32398 if (!parens
.require_close (parser
))
32399 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32400 /*or_comma=*/false,
32401 /*consume_paren=*/true);
32403 if (num
== error_mark_node
)
32405 num
= fold_non_dependent_expr (num
);
32406 if (!tree_fits_shwi_p (num
)
32407 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32408 || (n
= tree_to_shwi (num
)) <= 0
32411 error_at (location
,
32412 "ordered argument needs positive constant integer "
32418 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32419 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32420 OMP_CLAUSE_CHAIN (c
) = list
;
32425 reduction ( reduction-operator : variable-list )
32427 reduction-operator:
32428 One of: + * - & ^ | && ||
32432 reduction-operator:
32433 One of: + * - & ^ | && || min max
32437 reduction-operator:
32438 One of: + * - & ^ | && ||
32442 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32444 enum tree_code code
= ERROR_MARK
;
32445 tree nlist
, c
, id
= NULL_TREE
;
32447 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32450 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32452 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32453 case CPP_MULT
: code
= MULT_EXPR
; break;
32454 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32455 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32456 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32457 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32458 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32459 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32463 if (code
!= ERROR_MARK
)
32464 cp_lexer_consume_token (parser
->lexer
);
32467 bool saved_colon_corrects_to_scope_p
;
32468 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32469 parser
->colon_corrects_to_scope_p
= false;
32470 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32471 /*check_dependency_p=*/true,
32472 /*template_p=*/NULL
,
32473 /*declarator_p=*/false,
32474 /*optional_p=*/false);
32475 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32476 if (identifier_p (id
))
32478 const char *p
= IDENTIFIER_POINTER (id
);
32480 if (strcmp (p
, "min") == 0)
32482 else if (strcmp (p
, "max") == 0)
32484 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32486 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32488 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32490 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32491 code
= BIT_AND_EXPR
;
32492 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32493 code
= BIT_IOR_EXPR
;
32494 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32495 code
= BIT_XOR_EXPR
;
32496 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32497 code
= TRUTH_ANDIF_EXPR
;
32498 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32499 code
= TRUTH_ORIF_EXPR
;
32500 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32501 tree scope
= parser
->scope
;
32503 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32504 parser
->scope
= NULL_TREE
;
32505 parser
->qualifying_scope
= NULL_TREE
;
32506 parser
->object_scope
= NULL_TREE
;
32510 error ("invalid reduction-identifier");
32512 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32513 /*or_comma=*/false,
32514 /*consume_paren=*/true);
32519 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32522 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32524 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32526 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32527 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32534 schedule ( schedule-kind )
32535 schedule ( schedule-kind , expression )
32538 static | dynamic | guided | runtime | auto
32541 schedule ( schedule-modifier : schedule-kind )
32542 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32550 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32553 int modifiers
= 0, nmodifiers
= 0;
32555 matching_parens parens
;
32556 if (!parens
.require_open (parser
))
32559 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32561 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32563 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32564 const char *p
= IDENTIFIER_POINTER (id
);
32565 if (strcmp ("simd", p
) == 0)
32566 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32567 else if (strcmp ("monotonic", p
) == 0)
32568 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32569 else if (strcmp ("nonmonotonic", p
) == 0)
32570 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32573 cp_lexer_consume_token (parser
->lexer
);
32574 if (nmodifiers
++ == 0
32575 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32576 cp_lexer_consume_token (parser
->lexer
);
32579 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32586 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32587 const char *p
= IDENTIFIER_POINTER (id
);
32592 if (strcmp ("dynamic", p
) != 0)
32594 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32598 if (strcmp ("guided", p
) != 0)
32600 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32604 if (strcmp ("runtime", p
) != 0)
32606 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32613 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32614 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32615 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32616 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32619 cp_lexer_consume_token (parser
->lexer
);
32621 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32622 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32623 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32624 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32626 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32634 cp_lexer_consume_token (parser
->lexer
);
32636 token
= cp_lexer_peek_token (parser
->lexer
);
32637 t
= cp_parser_assignment_expression (parser
);
32639 if (t
== error_mark_node
)
32641 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32642 error_at (token
->location
, "schedule %<runtime%> does not take "
32643 "a %<chunk_size%> parameter");
32644 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32645 error_at (token
->location
, "schedule %<auto%> does not take "
32646 "a %<chunk_size%> parameter");
32648 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32650 if (!parens
.require_close (parser
))
32653 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32656 OMP_CLAUSE_SCHEDULE_KIND (c
)
32657 = (enum omp_clause_schedule_kind
)
32658 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32660 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32661 OMP_CLAUSE_CHAIN (c
) = list
;
32665 cp_parser_error (parser
, "invalid schedule kind");
32667 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32668 /*or_comma=*/false,
32669 /*consume_paren=*/true);
32677 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32678 tree list
, location_t location
)
32682 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32684 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32685 OMP_CLAUSE_CHAIN (c
) = list
;
32694 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32695 tree list
, location_t location
)
32697 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32698 tree c
= build_omp_clause (location
, code
);
32699 OMP_CLAUSE_CHAIN (c
) = list
;
32710 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32711 enum omp_clause_code code
,
32712 tree list
, location_t location
)
32714 tree c
= build_omp_clause (location
, code
);
32715 OMP_CLAUSE_CHAIN (c
) = list
;
32723 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32724 tree list
, location_t location
)
32726 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32727 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32728 OMP_CLAUSE_CHAIN (c
) = list
;
32737 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32738 enum omp_clause_code code
,
32739 tree list
, location_t location
)
32741 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32742 tree c
= build_omp_clause (location
, code
);
32743 OMP_CLAUSE_CHAIN (c
) = list
;
32748 num_teams ( expression ) */
32751 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32752 location_t location
)
32756 matching_parens parens
;
32757 if (!parens
.require_open (parser
))
32760 t
= cp_parser_expression (parser
);
32762 if (t
== error_mark_node
32763 || !parens
.require_close (parser
))
32764 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32765 /*or_comma=*/false,
32766 /*consume_paren=*/true);
32768 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32769 "num_teams", location
);
32771 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32772 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32773 OMP_CLAUSE_CHAIN (c
) = list
;
32779 thread_limit ( expression ) */
32782 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32783 location_t location
)
32787 matching_parens parens
;
32788 if (!parens
.require_open (parser
))
32791 t
= cp_parser_expression (parser
);
32793 if (t
== error_mark_node
32794 || !parens
.require_close (parser
))
32795 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32796 /*or_comma=*/false,
32797 /*consume_paren=*/true);
32799 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32800 "thread_limit", location
);
32802 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32803 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32804 OMP_CLAUSE_CHAIN (c
) = list
;
32810 aligned ( variable-list )
32811 aligned ( variable-list : constant-expression ) */
32814 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32816 tree nlist
, c
, alignment
= NULL_TREE
;
32819 matching_parens parens
;
32820 if (!parens
.require_open (parser
))
32823 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32828 alignment
= cp_parser_constant_expression (parser
);
32830 if (!parens
.require_close (parser
))
32831 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32832 /*or_comma=*/false,
32833 /*consume_paren=*/true);
32835 if (alignment
== error_mark_node
)
32836 alignment
= NULL_TREE
;
32839 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32840 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
32846 linear ( variable-list )
32847 linear ( variable-list : expression )
32850 linear ( modifier ( variable-list ) )
32851 linear ( modifier ( variable-list ) : expression ) */
32854 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
32857 tree nlist
, c
, step
= integer_one_node
;
32859 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32861 matching_parens parens
;
32862 if (!parens
.require_open (parser
))
32865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32867 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32868 const char *p
= IDENTIFIER_POINTER (id
);
32870 if (strcmp ("ref", p
) == 0)
32871 kind
= OMP_CLAUSE_LINEAR_REF
;
32872 else if (strcmp ("val", p
) == 0)
32873 kind
= OMP_CLAUSE_LINEAR_VAL
;
32874 else if (strcmp ("uval", p
) == 0)
32875 kind
= OMP_CLAUSE_LINEAR_UVAL
;
32876 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
32877 cp_lexer_consume_token (parser
->lexer
);
32879 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32882 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
32883 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
32887 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
32888 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
32890 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32891 else if (!parens
.require_close (parser
))
32892 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32893 /*or_comma=*/false,
32894 /*consume_paren=*/true);
32901 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32902 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
32904 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32905 cp_parser_parse_tentatively (parser
);
32906 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
32907 /*check_dependency_p=*/true,
32908 /*template_p=*/NULL
,
32909 /*declarator_p=*/false,
32910 /*optional_p=*/false);
32911 if (step
!= error_mark_node
)
32912 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
32913 if (step
== error_mark_node
)
32916 cp_parser_abort_tentative_parse (parser
);
32918 else if (!cp_parser_parse_definitely (parser
))
32922 step
= cp_parser_expression (parser
);
32924 if (!parens
.require_close (parser
))
32925 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32926 /*or_comma=*/false,
32927 /*consume_paren=*/true);
32929 if (step
== error_mark_node
)
32933 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32935 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
32936 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
32943 safelen ( constant-expression ) */
32946 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
32947 location_t location
)
32951 matching_parens parens
;
32952 if (!parens
.require_open (parser
))
32955 t
= cp_parser_constant_expression (parser
);
32957 if (t
== error_mark_node
32958 || !parens
.require_close (parser
))
32959 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32960 /*or_comma=*/false,
32961 /*consume_paren=*/true);
32963 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
32965 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
32966 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
32967 OMP_CLAUSE_CHAIN (c
) = list
;
32973 simdlen ( constant-expression ) */
32976 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
32977 location_t location
)
32981 matching_parens parens
;
32982 if (!parens
.require_open (parser
))
32985 t
= cp_parser_constant_expression (parser
);
32987 if (t
== error_mark_node
32988 || !parens
.require_close (parser
))
32989 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32990 /*or_comma=*/false,
32991 /*consume_paren=*/true);
32993 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
32995 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
32996 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
32997 OMP_CLAUSE_CHAIN (c
) = list
;
33004 identifier [+/- integer]
33005 vec , identifier [+/- integer]
33009 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33014 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33016 cp_parser_error (parser
, "expected identifier");
33020 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33022 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33023 tree t
, identifier
= cp_parser_identifier (parser
);
33024 tree addend
= NULL
;
33026 if (identifier
== error_mark_node
)
33027 t
= error_mark_node
;
33030 t
= cp_parser_lookup_name_simple
33031 (parser
, identifier
,
33032 cp_lexer_peek_token (parser
->lexer
)->location
);
33033 if (t
== error_mark_node
)
33034 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33041 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33043 addend
= integer_zero_node
;
33044 goto add_to_vector
;
33046 cp_lexer_consume_token (parser
->lexer
);
33048 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33050 cp_parser_error (parser
, "expected integer");
33054 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33055 if (TREE_CODE (addend
) != INTEGER_CST
)
33057 cp_parser_error (parser
, "expected integer");
33060 cp_lexer_consume_token (parser
->lexer
);
33063 if (t
!= error_mark_node
)
33065 vec
= tree_cons (addend
, t
, vec
);
33067 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33070 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33073 cp_lexer_consume_token (parser
->lexer
);
33076 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33078 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33079 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33080 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33081 OMP_CLAUSE_CHAIN (u
) = list
;
33088 depend ( depend-kind : variable-list )
33096 depend ( sink : vec ) */
33099 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33102 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33104 matching_parens parens
;
33105 if (!parens
.require_open (parser
))
33108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33110 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33111 const char *p
= IDENTIFIER_POINTER (id
);
33113 if (strcmp ("in", p
) == 0)
33114 kind
= OMP_CLAUSE_DEPEND_IN
;
33115 else if (strcmp ("inout", p
) == 0)
33116 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33117 else if (strcmp ("out", p
) == 0)
33118 kind
= OMP_CLAUSE_DEPEND_OUT
;
33119 else if (strcmp ("source", p
) == 0)
33120 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33121 else if (strcmp ("sink", p
) == 0)
33122 kind
= OMP_CLAUSE_DEPEND_SINK
;
33129 cp_lexer_consume_token (parser
->lexer
);
33131 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33133 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33134 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33135 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33136 OMP_CLAUSE_CHAIN (c
) = list
;
33137 if (!parens
.require_close (parser
))
33138 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33139 /*or_comma=*/false,
33140 /*consume_paren=*/true);
33144 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33147 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33148 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33151 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33154 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33155 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33160 cp_parser_error (parser
, "invalid depend kind");
33162 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33163 /*or_comma=*/false,
33164 /*consume_paren=*/true);
33169 map ( map-kind : variable-list )
33170 map ( variable-list )
33173 alloc | to | from | tofrom
33177 alloc | to | from | tofrom | release | delete
33179 map ( always [,] map-kind: variable-list ) */
33182 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33185 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33186 bool always
= false;
33188 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33193 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33194 const char *p
= IDENTIFIER_POINTER (id
);
33196 if (strcmp ("always", p
) == 0)
33199 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33201 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33202 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33204 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33208 cp_lexer_consume_token (parser
->lexer
);
33210 cp_lexer_consume_token (parser
->lexer
);
33215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33216 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33218 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33219 const char *p
= IDENTIFIER_POINTER (id
);
33221 if (strcmp ("alloc", p
) == 0)
33222 kind
= GOMP_MAP_ALLOC
;
33223 else if (strcmp ("to", p
) == 0)
33224 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33225 else if (strcmp ("from", p
) == 0)
33226 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33227 else if (strcmp ("tofrom", p
) == 0)
33228 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33229 else if (strcmp ("release", p
) == 0)
33230 kind
= GOMP_MAP_RELEASE
;
33233 cp_parser_error (parser
, "invalid map kind");
33234 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33235 /*or_comma=*/false,
33236 /*consume_paren=*/true);
33239 cp_lexer_consume_token (parser
->lexer
);
33240 cp_lexer_consume_token (parser
->lexer
);
33242 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33243 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33245 kind
= GOMP_MAP_DELETE
;
33246 cp_lexer_consume_token (parser
->lexer
);
33247 cp_lexer_consume_token (parser
->lexer
);
33250 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33253 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33254 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33260 device ( expression ) */
33263 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33264 location_t location
)
33268 matching_parens parens
;
33269 if (!parens
.require_open (parser
))
33272 t
= cp_parser_expression (parser
);
33274 if (t
== error_mark_node
33275 || !parens
.require_close (parser
))
33276 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33280 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33281 "device", location
);
33283 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33284 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33285 OMP_CLAUSE_CHAIN (c
) = list
;
33291 dist_schedule ( static )
33292 dist_schedule ( static , expression ) */
33295 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33296 location_t location
)
33300 matching_parens parens
;
33301 if (!parens
.require_open (parser
))
33304 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33306 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33308 cp_lexer_consume_token (parser
->lexer
);
33310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33312 cp_lexer_consume_token (parser
->lexer
);
33314 t
= cp_parser_assignment_expression (parser
);
33316 if (t
== error_mark_node
)
33318 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33320 if (!parens
.require_close (parser
))
33323 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33326 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33328 OMP_CLAUSE_CHAIN (c
) = list
;
33332 cp_parser_error (parser
, "invalid dist_schedule kind");
33334 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33335 /*or_comma=*/false,
33336 /*consume_paren=*/true);
33341 proc_bind ( proc-bind-kind )
33344 master | close | spread */
33347 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33348 location_t location
)
33351 enum omp_clause_proc_bind_kind kind
;
33353 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33358 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33359 const char *p
= IDENTIFIER_POINTER (id
);
33361 if (strcmp ("master", p
) == 0)
33362 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33363 else if (strcmp ("close", p
) == 0)
33364 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33365 else if (strcmp ("spread", p
) == 0)
33366 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33373 cp_lexer_consume_token (parser
->lexer
);
33374 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33377 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33378 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33380 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33381 OMP_CLAUSE_CHAIN (c
) = list
;
33385 cp_parser_error (parser
, "invalid depend kind");
33387 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33388 /*or_comma=*/false,
33389 /*consume_paren=*/true);
33394 async [( int-expr )] */
33397 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33400 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33402 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33404 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33406 matching_parens parens
;
33407 parens
.consume_open (parser
);
33409 t
= cp_parser_expression (parser
);
33410 if (t
== error_mark_node
33411 || !parens
.require_close (parser
))
33412 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33413 /*or_comma=*/false,
33414 /*consume_paren=*/true);
33417 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33419 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33420 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33421 OMP_CLAUSE_CHAIN (c
) = list
;
33427 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33428 is a bitmask in MASK. Return the list of clauses found. */
33431 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33432 const char *where
, cp_token
*pragma_tok
,
33433 bool finish_p
= true)
33435 tree clauses
= NULL
;
33438 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33441 pragma_omp_clause c_kind
;
33442 omp_clause_code code
;
33443 const char *c_name
;
33444 tree prev
= clauses
;
33446 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33447 cp_lexer_consume_token (parser
->lexer
);
33449 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33450 c_kind
= cp_parser_omp_clause_name (parser
);
33454 case PRAGMA_OACC_CLAUSE_ASYNC
:
33455 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33458 case PRAGMA_OACC_CLAUSE_AUTO
:
33459 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33463 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33464 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33465 c_name
= "collapse";
33467 case PRAGMA_OACC_CLAUSE_COPY
:
33468 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33471 case PRAGMA_OACC_CLAUSE_COPYIN
:
33472 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33475 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33476 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33477 c_name
= "copyout";
33479 case PRAGMA_OACC_CLAUSE_CREATE
:
33480 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33483 case PRAGMA_OACC_CLAUSE_DELETE
:
33484 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33487 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33488 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33489 c_name
= "default";
33491 case PRAGMA_OACC_CLAUSE_DEVICE
:
33492 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33495 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33496 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33497 c_name
= "deviceptr";
33499 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33500 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33501 c_name
= "device_resident";
33503 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33504 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33506 c_name
= "firstprivate";
33508 case PRAGMA_OACC_CLAUSE_GANG
:
33510 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33513 case PRAGMA_OACC_CLAUSE_HOST
:
33514 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33517 case PRAGMA_OACC_CLAUSE_IF
:
33518 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33521 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33522 clauses
= cp_parser_oacc_simple_clause (parser
,
33523 OMP_CLAUSE_INDEPENDENT
,
33525 c_name
= "independent";
33527 case PRAGMA_OACC_CLAUSE_LINK
:
33528 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33531 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33532 code
= OMP_CLAUSE_NUM_GANGS
;
33533 c_name
= "num_gangs";
33534 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33537 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33538 c_name
= "num_workers";
33539 code
= OMP_CLAUSE_NUM_WORKERS
;
33540 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33543 case PRAGMA_OACC_CLAUSE_PRESENT
:
33544 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33545 c_name
= "present";
33547 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33548 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33549 c_name
= "present_or_copy";
33551 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33552 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33553 c_name
= "present_or_copyin";
33555 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33556 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33557 c_name
= "present_or_copyout";
33559 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33560 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33561 c_name
= "present_or_create";
33563 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33564 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33566 c_name
= "private";
33568 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33569 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33570 c_name
= "reduction";
33572 case PRAGMA_OACC_CLAUSE_SELF
:
33573 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33576 case PRAGMA_OACC_CLAUSE_SEQ
:
33577 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33581 case PRAGMA_OACC_CLAUSE_TILE
:
33582 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33585 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33586 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33588 c_name
= "use_device";
33590 case PRAGMA_OACC_CLAUSE_VECTOR
:
33592 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33595 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33596 c_name
= "vector_length";
33597 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33598 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33601 case PRAGMA_OACC_CLAUSE_WAIT
:
33602 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33605 case PRAGMA_OACC_CLAUSE_WORKER
:
33607 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33611 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33617 if (((mask
>> c_kind
) & 1) == 0)
33619 /* Remove the invalid clause(s) from the list to avoid
33620 confusing the rest of the compiler. */
33622 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33627 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33630 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33635 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33636 is a bitmask in MASK. Return the list of clauses found; the result
33637 of clause default goes in *pdefault. */
33640 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33641 const char *where
, cp_token
*pragma_tok
,
33642 bool finish_p
= true)
33644 tree clauses
= NULL
;
33646 cp_token
*token
= NULL
;
33648 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33650 pragma_omp_clause c_kind
;
33651 const char *c_name
;
33652 tree prev
= clauses
;
33654 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33655 cp_lexer_consume_token (parser
->lexer
);
33657 token
= cp_lexer_peek_token (parser
->lexer
);
33658 c_kind
= cp_parser_omp_clause_name (parser
);
33662 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33663 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33665 c_name
= "collapse";
33667 case PRAGMA_OMP_CLAUSE_COPYIN
:
33668 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33671 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33672 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33674 c_name
= "copyprivate";
33676 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33677 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33678 token
->location
, false);
33679 c_name
= "default";
33681 case PRAGMA_OMP_CLAUSE_FINAL
:
33682 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33685 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33686 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33688 c_name
= "firstprivate";
33690 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33691 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33693 c_name
= "grainsize";
33695 case PRAGMA_OMP_CLAUSE_HINT
:
33696 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33700 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33701 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33703 c_name
= "defaultmap";
33705 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33706 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33708 c_name
= "use_device_ptr";
33710 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33711 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33713 c_name
= "is_device_ptr";
33715 case PRAGMA_OMP_CLAUSE_IF
:
33716 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33720 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33721 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33723 c_name
= "lastprivate";
33725 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33726 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33728 c_name
= "mergeable";
33730 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33731 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33734 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33735 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33737 c_name
= "num_tasks";
33739 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33740 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33742 c_name
= "num_threads";
33744 case PRAGMA_OMP_CLAUSE_ORDERED
:
33745 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33747 c_name
= "ordered";
33749 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33750 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33752 c_name
= "priority";
33754 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33755 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33757 c_name
= "private";
33759 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33760 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33761 c_name
= "reduction";
33763 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33764 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33766 c_name
= "schedule";
33768 case PRAGMA_OMP_CLAUSE_SHARED
:
33769 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33773 case PRAGMA_OMP_CLAUSE_UNTIED
:
33774 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33778 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33779 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33780 clauses
, token
->location
);
33781 c_name
= "inbranch";
33783 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33784 clauses
= cp_parser_omp_clause_branch (parser
,
33785 OMP_CLAUSE_NOTINBRANCH
,
33786 clauses
, token
->location
);
33787 c_name
= "notinbranch";
33789 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33790 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33791 clauses
, token
->location
);
33792 c_name
= "parallel";
33796 error_at (token
->location
, "%qs must be the first clause of %qs",
33801 case PRAGMA_OMP_CLAUSE_FOR
:
33802 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33803 clauses
, token
->location
);
33806 goto clause_not_first
;
33808 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33809 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33810 clauses
, token
->location
);
33811 c_name
= "sections";
33813 goto clause_not_first
;
33815 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33816 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33817 clauses
, token
->location
);
33818 c_name
= "taskgroup";
33820 goto clause_not_first
;
33822 case PRAGMA_OMP_CLAUSE_LINK
:
33823 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33826 case PRAGMA_OMP_CLAUSE_TO
:
33827 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33828 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33831 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33834 case PRAGMA_OMP_CLAUSE_FROM
:
33835 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
33838 case PRAGMA_OMP_CLAUSE_UNIFORM
:
33839 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
33841 c_name
= "uniform";
33843 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
33844 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
33846 c_name
= "num_teams";
33848 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
33849 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
33851 c_name
= "thread_limit";
33853 case PRAGMA_OMP_CLAUSE_ALIGNED
:
33854 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
33855 c_name
= "aligned";
33857 case PRAGMA_OMP_CLAUSE_LINEAR
:
33859 bool declare_simd
= false;
33860 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
33861 declare_simd
= true;
33862 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
33866 case PRAGMA_OMP_CLAUSE_DEPEND
:
33867 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
33871 case PRAGMA_OMP_CLAUSE_MAP
:
33872 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
33875 case PRAGMA_OMP_CLAUSE_DEVICE
:
33876 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
33880 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
33881 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
33883 c_name
= "dist_schedule";
33885 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
33886 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
33888 c_name
= "proc_bind";
33890 case PRAGMA_OMP_CLAUSE_SAFELEN
:
33891 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
33893 c_name
= "safelen";
33895 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
33896 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
33898 c_name
= "simdlen";
33900 case PRAGMA_OMP_CLAUSE_NOGROUP
:
33901 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
33903 c_name
= "nogroup";
33905 case PRAGMA_OMP_CLAUSE_THREADS
:
33907 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
33908 clauses
, token
->location
);
33909 c_name
= "threads";
33911 case PRAGMA_OMP_CLAUSE_SIMD
:
33913 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
33914 clauses
, token
->location
);
33918 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
33924 if (((mask
>> c_kind
) & 1) == 0)
33926 /* Remove the invalid clause(s) from the list to avoid
33927 confusing the rest of the compiler. */
33929 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
33933 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33936 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
33937 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
33939 return finish_omp_clauses (clauses
, C_ORT_OMP
);
33948 In practice, we're also interested in adding the statement to an
33949 outer node. So it is convenient if we work around the fact that
33950 cp_parser_statement calls add_stmt. */
33953 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
33955 unsigned save
= parser
->in_statement
;
33957 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33958 This preserves the "not within loop or switch" style error messages
33959 for nonsense cases like
33965 if (parser
->in_statement
)
33966 parser
->in_statement
= IN_OMP_BLOCK
;
33972 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
33974 parser
->in_statement
= save
;
33978 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
33980 tree stmt
= begin_omp_structured_block ();
33981 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
33983 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
33985 cp_parser_end_omp_structured_block (parser
, save
);
33986 return finish_omp_structured_block (stmt
);
33990 # pragma omp atomic new-line
33994 x binop= expr | x++ | ++x | x-- | --x
33996 +, *, -, /, &, ^, |, <<, >>
33998 where x is an lvalue expression with scalar type.
34001 # pragma omp atomic new-line
34004 # pragma omp atomic read new-line
34007 # pragma omp atomic write new-line
34010 # pragma omp atomic update new-line
34013 # pragma omp atomic capture new-line
34016 # pragma omp atomic capture new-line
34024 expression-stmt | x = x binop expr
34026 v = expression-stmt
34028 { v = x; update-stmt; } | { update-stmt; v = x; }
34032 expression-stmt | x = x binop expr | x = expr binop x
34036 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34038 where x and v are lvalue expressions with scalar type. */
34041 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34043 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34044 tree rhs1
= NULL_TREE
, orig_lhs
;
34045 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34046 bool structured_block
= false;
34047 bool seq_cst
= false;
34049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34051 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34052 const char *p
= IDENTIFIER_POINTER (id
);
34054 if (!strcmp (p
, "seq_cst"))
34057 cp_lexer_consume_token (parser
->lexer
);
34058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34059 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34060 cp_lexer_consume_token (parser
->lexer
);
34063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34065 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34066 const char *p
= IDENTIFIER_POINTER (id
);
34068 if (!strcmp (p
, "read"))
34069 code
= OMP_ATOMIC_READ
;
34070 else if (!strcmp (p
, "write"))
34072 else if (!strcmp (p
, "update"))
34074 else if (!strcmp (p
, "capture"))
34075 code
= OMP_ATOMIC_CAPTURE_NEW
;
34079 cp_lexer_consume_token (parser
->lexer
);
34083 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34084 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34085 cp_lexer_consume_token (parser
->lexer
);
34087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34089 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34090 const char *p
= IDENTIFIER_POINTER (id
);
34092 if (!strcmp (p
, "seq_cst"))
34095 cp_lexer_consume_token (parser
->lexer
);
34099 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34103 case OMP_ATOMIC_READ
:
34104 case NOP_EXPR
: /* atomic write */
34105 v
= cp_parser_unary_expression (parser
);
34106 if (v
== error_mark_node
)
34108 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34110 if (code
== NOP_EXPR
)
34111 lhs
= cp_parser_expression (parser
);
34113 lhs
= cp_parser_unary_expression (parser
);
34114 if (lhs
== error_mark_node
)
34116 if (code
== NOP_EXPR
)
34118 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34126 case OMP_ATOMIC_CAPTURE_NEW
:
34127 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34129 cp_lexer_consume_token (parser
->lexer
);
34130 structured_block
= true;
34134 v
= cp_parser_unary_expression (parser
);
34135 if (v
== error_mark_node
)
34137 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34145 lhs
= cp_parser_unary_expression (parser
);
34147 switch (TREE_CODE (lhs
))
34152 case POSTINCREMENT_EXPR
:
34153 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34154 code
= OMP_ATOMIC_CAPTURE_OLD
;
34156 case PREINCREMENT_EXPR
:
34157 lhs
= TREE_OPERAND (lhs
, 0);
34158 opcode
= PLUS_EXPR
;
34159 rhs
= integer_one_node
;
34162 case POSTDECREMENT_EXPR
:
34163 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34164 code
= OMP_ATOMIC_CAPTURE_OLD
;
34166 case PREDECREMENT_EXPR
:
34167 lhs
= TREE_OPERAND (lhs
, 0);
34168 opcode
= MINUS_EXPR
;
34169 rhs
= integer_one_node
;
34172 case COMPOUND_EXPR
:
34173 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34174 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34175 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34176 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34177 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34178 (TREE_OPERAND (lhs
, 1), 0), 0)))
34180 /* Undo effects of boolean_increment for post {in,de}crement. */
34181 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34184 if (TREE_CODE (lhs
) == MODIFY_EXPR
34185 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34187 /* Undo effects of boolean_increment. */
34188 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34190 /* This is pre or post increment. */
34191 rhs
= TREE_OPERAND (lhs
, 1);
34192 lhs
= TREE_OPERAND (lhs
, 0);
34194 if (code
== OMP_ATOMIC_CAPTURE_NEW
34195 && !structured_block
34196 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34197 code
= OMP_ATOMIC_CAPTURE_OLD
;
34203 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34206 opcode
= MULT_EXPR
;
34209 opcode
= TRUNC_DIV_EXPR
;
34212 opcode
= PLUS_EXPR
;
34215 opcode
= MINUS_EXPR
;
34217 case CPP_LSHIFT_EQ
:
34218 opcode
= LSHIFT_EXPR
;
34220 case CPP_RSHIFT_EQ
:
34221 opcode
= RSHIFT_EXPR
;
34224 opcode
= BIT_AND_EXPR
;
34227 opcode
= BIT_IOR_EXPR
;
34230 opcode
= BIT_XOR_EXPR
;
34233 enum cp_parser_prec oprec
;
34235 cp_lexer_consume_token (parser
->lexer
);
34236 cp_parser_parse_tentatively (parser
);
34237 rhs1
= cp_parser_simple_cast_expression (parser
);
34238 if (rhs1
== error_mark_node
)
34240 cp_parser_abort_tentative_parse (parser
);
34241 cp_parser_simple_cast_expression (parser
);
34244 token
= cp_lexer_peek_token (parser
->lexer
);
34245 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34247 cp_parser_abort_tentative_parse (parser
);
34248 cp_parser_parse_tentatively (parser
);
34249 rhs
= cp_parser_binary_expression (parser
, false, true,
34250 PREC_NOT_OPERATOR
, NULL
);
34251 if (rhs
== error_mark_node
)
34253 cp_parser_abort_tentative_parse (parser
);
34254 cp_parser_binary_expression (parser
, false, true,
34255 PREC_NOT_OPERATOR
, NULL
);
34258 switch (TREE_CODE (rhs
))
34261 case TRUNC_DIV_EXPR
:
34270 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34272 if (cp_parser_parse_definitely (parser
))
34274 opcode
= TREE_CODE (rhs
);
34275 rhs1
= TREE_OPERAND (rhs
, 0);
34276 rhs
= TREE_OPERAND (rhs
, 1);
34286 cp_parser_abort_tentative_parse (parser
);
34287 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34289 rhs
= cp_parser_expression (parser
);
34290 if (rhs
== error_mark_node
)
34296 cp_parser_error (parser
,
34297 "invalid form of %<#pragma omp atomic%>");
34300 if (!cp_parser_parse_definitely (parser
))
34302 switch (token
->type
)
34304 case CPP_SEMICOLON
:
34305 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34307 code
= OMP_ATOMIC_CAPTURE_OLD
;
34312 cp_lexer_consume_token (parser
->lexer
);
34315 else if (structured_block
)
34322 cp_parser_error (parser
,
34323 "invalid form of %<#pragma omp atomic%>");
34326 opcode
= MULT_EXPR
;
34329 opcode
= TRUNC_DIV_EXPR
;
34332 opcode
= PLUS_EXPR
;
34335 opcode
= MINUS_EXPR
;
34338 opcode
= LSHIFT_EXPR
;
34341 opcode
= RSHIFT_EXPR
;
34344 opcode
= BIT_AND_EXPR
;
34347 opcode
= BIT_IOR_EXPR
;
34350 opcode
= BIT_XOR_EXPR
;
34353 cp_parser_error (parser
,
34354 "invalid operator for %<#pragma omp atomic%>");
34357 oprec
= TOKEN_PRECEDENCE (token
);
34358 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34359 if (commutative_tree_code (opcode
))
34360 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34361 cp_lexer_consume_token (parser
->lexer
);
34362 rhs
= cp_parser_binary_expression (parser
, false, false,
34364 if (rhs
== error_mark_node
)
34369 cp_parser_error (parser
,
34370 "invalid operator for %<#pragma omp atomic%>");
34373 cp_lexer_consume_token (parser
->lexer
);
34375 rhs
= cp_parser_expression (parser
);
34376 if (rhs
== error_mark_node
)
34381 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34383 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34385 v
= cp_parser_unary_expression (parser
);
34386 if (v
== error_mark_node
)
34388 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34390 lhs1
= cp_parser_unary_expression (parser
);
34391 if (lhs1
== error_mark_node
)
34394 if (structured_block
)
34396 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34397 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34400 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34401 if (!structured_block
)
34402 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34406 cp_parser_skip_to_end_of_block_or_statement (parser
);
34407 if (structured_block
)
34409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34410 cp_lexer_consume_token (parser
->lexer
);
34411 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34413 cp_parser_skip_to_end_of_block_or_statement (parser
);
34414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34415 cp_lexer_consume_token (parser
->lexer
);
34422 # pragma omp barrier new-line */
34425 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34427 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34428 finish_omp_barrier ();
34432 # pragma omp critical [(name)] new-line
34436 # pragma omp critical [(name) [hint(expression)]] new-line
34437 structured-block */
34439 #define OMP_CRITICAL_CLAUSE_MASK \
34440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34443 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34445 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34449 matching_parens parens
;
34450 parens
.consume_open (parser
);
34452 name
= cp_parser_identifier (parser
);
34454 if (name
== error_mark_node
34455 || !parens
.require_close (parser
))
34456 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34457 /*or_comma=*/false,
34458 /*consume_paren=*/true);
34459 if (name
== error_mark_node
)
34462 clauses
= cp_parser_omp_all_clauses (parser
,
34463 OMP_CRITICAL_CLAUSE_MASK
,
34464 "#pragma omp critical", pragma_tok
);
34467 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34469 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34470 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34474 # pragma omp flush flush-vars[opt] new-line
34477 ( variable-list ) */
34480 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34483 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34484 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34486 finish_omp_flush ();
34489 /* Helper function, to parse omp for increment expression. */
34492 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34494 tree cond
= cp_parser_binary_expression (parser
, false, true,
34495 PREC_NOT_OPERATOR
, NULL
);
34496 if (cond
== error_mark_node
34497 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34499 cp_parser_skip_to_end_of_statement (parser
);
34500 return error_mark_node
;
34503 switch (TREE_CODE (cond
))
34511 /* Fall through: OpenMP disallows NE_EXPR. */
34512 gcc_fallthrough ();
34514 return error_mark_node
;
34517 /* If decl is an iterator, preserve LHS and RHS of the relational
34518 expr until finish_omp_for. */
34520 && (type_dependent_expression_p (decl
)
34521 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34524 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34526 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34527 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34528 /*overload=*/NULL
, tf_warning_or_error
);
34531 /* Helper function, to parse omp for increment expression. */
34534 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34536 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34542 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34544 op
= (token
->type
== CPP_PLUS_PLUS
34545 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34546 cp_lexer_consume_token (parser
->lexer
);
34547 lhs
= cp_parser_simple_cast_expression (parser
);
34549 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34550 return error_mark_node
;
34551 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34554 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34556 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34557 return error_mark_node
;
34559 token
= cp_lexer_peek_token (parser
->lexer
);
34560 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34562 op
= (token
->type
== CPP_PLUS_PLUS
34563 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34564 cp_lexer_consume_token (parser
->lexer
);
34565 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34568 op
= cp_parser_assignment_operator_opt (parser
);
34569 if (op
== ERROR_MARK
)
34570 return error_mark_node
;
34572 if (op
!= NOP_EXPR
)
34574 rhs
= cp_parser_assignment_expression (parser
);
34575 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34576 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34579 lhs
= cp_parser_binary_expression (parser
, false, false,
34580 PREC_ADDITIVE_EXPRESSION
, NULL
);
34581 token
= cp_lexer_peek_token (parser
->lexer
);
34582 decl_first
= (lhs
== decl
34583 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34586 if (token
->type
!= CPP_PLUS
34587 && token
->type
!= CPP_MINUS
)
34588 return error_mark_node
;
34592 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34593 cp_lexer_consume_token (parser
->lexer
);
34594 rhs
= cp_parser_binary_expression (parser
, false, false,
34595 PREC_ADDITIVE_EXPRESSION
, NULL
);
34596 token
= cp_lexer_peek_token (parser
->lexer
);
34597 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34599 if (lhs
== NULL_TREE
)
34601 if (op
== PLUS_EXPR
)
34604 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34605 tf_warning_or_error
);
34608 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34609 ERROR_MARK
, NULL
, tf_warning_or_error
);
34612 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34617 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34618 || op
== MINUS_EXPR
)
34619 return error_mark_node
;
34620 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34623 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34625 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34628 /* Parse the initialization statement of an OpenMP for loop.
34630 Return true if the resulting construct should have an
34631 OMP_CLAUSE_PRIVATE added to it. */
34634 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34635 tree
&this_pre_body
,
34636 vec
<tree
, va_gc
> *for_block
,
34642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34645 tree add_private_clause
= NULL_TREE
;
34647 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34651 integer-type var = lb
34652 random-access-iterator-type var = lb
34653 pointer-type var = lb
34655 cp_decl_specifier_seq type_specifiers
;
34657 /* First, try to parse as an initialized declaration. See
34658 cp_parser_condition, from whence the bulk of this is copied. */
34660 cp_parser_parse_tentatively (parser
);
34661 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34662 /*is_trailing_return=*/false,
34664 if (cp_parser_parse_definitely (parser
))
34666 /* If parsing a type specifier seq succeeded, then this
34667 MUST be a initialized declaration. */
34668 tree asm_specification
, attributes
;
34669 cp_declarator
*declarator
;
34671 declarator
= cp_parser_declarator (parser
,
34672 CP_PARSER_DECLARATOR_NAMED
,
34673 /*ctor_dtor_or_conv_p=*/NULL
,
34674 /*parenthesized_p=*/NULL
,
34675 /*member_p=*/false,
34676 /*friend_p=*/false);
34677 attributes
= cp_parser_attributes_opt (parser
);
34678 asm_specification
= cp_parser_asm_specification_opt (parser
);
34680 if (declarator
== cp_error_declarator
)
34681 cp_parser_skip_to_end_of_statement (parser
);
34685 tree pushed_scope
, auto_node
;
34687 decl
= start_decl (declarator
, &type_specifiers
,
34688 SD_INITIALIZED
, attributes
,
34689 /*prefix_attributes=*/NULL_TREE
,
34692 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34693 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34695 if (cp_lexer_next_token_is (parser
->lexer
,
34697 error ("parenthesized initialization is not allowed in "
34698 "OpenMP %<for%> loop");
34700 /* Trigger an error. */
34701 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34703 init
= error_mark_node
;
34704 cp_parser_skip_to_end_of_statement (parser
);
34706 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34707 || type_dependent_expression_p (decl
)
34710 bool is_direct_init
, is_non_constant_init
;
34712 init
= cp_parser_initializer (parser
,
34714 &is_non_constant_init
);
34719 = do_auto_deduction (TREE_TYPE (decl
), init
,
34722 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34723 && !type_dependent_expression_p (decl
))
34727 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34729 LOOKUP_ONLYCONVERTING
);
34731 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34733 vec_safe_push (for_block
, this_pre_body
);
34738 init
= pop_stmt_list (this_pre_body
);
34739 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34741 tree_stmt_iterator i
= tsi_start (init
);
34742 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34743 while (!tsi_end_p (i
))
34745 tree t
= tsi_stmt (i
);
34746 if (TREE_CODE (t
) == DECL_EXPR
34747 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34750 vec_safe_push (for_block
, t
);
34755 if (tsi_one_before_end_p (i
))
34757 tree t
= tsi_stmt (i
);
34759 free_stmt_list (init
);
34764 this_pre_body
= NULL_TREE
;
34769 cp_lexer_consume_token (parser
->lexer
);
34770 init
= cp_parser_assignment_expression (parser
);
34773 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34774 init
= error_mark_node
;
34776 cp_finish_decl (decl
, NULL_TREE
,
34777 /*init_const_expr_p=*/false,
34779 LOOKUP_ONLYCONVERTING
);
34783 pop_scope (pushed_scope
);
34789 /* If parsing a type specifier sequence failed, then
34790 this MUST be a simple expression. */
34791 cp_parser_parse_tentatively (parser
);
34792 decl
= cp_parser_primary_expression (parser
, false, false,
34794 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34795 if (!cp_parser_error_occurred (parser
)
34797 && (TREE_CODE (decl
) == COMPONENT_REF
34798 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34800 cp_parser_abort_tentative_parse (parser
);
34801 cp_parser_parse_tentatively (parser
);
34802 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34803 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34804 /*check_dependency_p=*/true,
34805 /*template_p=*/NULL
,
34806 /*declarator_p=*/false,
34807 /*optional_p=*/false);
34808 if (name
!= error_mark_node
34809 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34811 decl
= cp_parser_lookup_name_simple (parser
, name
,
34813 if (TREE_CODE (decl
) == FIELD_DECL
)
34814 add_private_clause
= omp_privatize_field (decl
, false);
34816 cp_parser_abort_tentative_parse (parser
);
34817 cp_parser_parse_tentatively (parser
);
34818 decl
= cp_parser_primary_expression (parser
, false, false,
34821 if (!cp_parser_error_occurred (parser
)
34824 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34828 cp_parser_parse_definitely (parser
);
34829 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34830 rhs
= cp_parser_assignment_expression (parser
);
34832 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34835 tf_warning_or_error
));
34836 if (!add_private_clause
)
34837 add_private_clause
= decl
;
34842 cp_parser_abort_tentative_parse (parser
);
34843 init
= cp_parser_expression (parser
);
34846 if (TREE_CODE (init
) == MODIFY_EXPR
34847 || TREE_CODE (init
) == MODOP_EXPR
)
34848 real_decl
= TREE_OPERAND (init
, 0);
34852 return add_private_clause
;
34855 /* Parse the restricted form of the for statement allowed by OpenMP. */
34858 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
34859 tree
*cclauses
, bool *if_p
)
34861 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
34862 tree real_decl
, initv
, condv
, incrv
, declv
;
34863 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
34864 location_t loc_first
;
34865 bool collapse_err
= false;
34866 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
34867 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
34868 auto_vec
<tree
, 4> orig_inits
;
34869 bool tiling
= false;
34871 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
34872 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
34873 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
34874 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
34877 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
34879 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
34880 && OMP_CLAUSE_ORDERED_EXPR (cl
))
34883 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
34886 if (ordered
&& ordered
< collapse
)
34888 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
34889 "%<ordered%> clause parameter is less than %<collapse%>");
34890 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
34891 = build_int_cst (NULL_TREE
, collapse
);
34892 ordered
= collapse
;
34896 for (tree
*pc
= &clauses
; *pc
; )
34897 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
34899 error_at (OMP_CLAUSE_LOCATION (*pc
),
34900 "%<linear%> clause may not be specified together "
34901 "with %<ordered%> clause with a parameter");
34902 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34905 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34908 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
34909 count
= ordered
? ordered
: collapse
;
34911 declv
= make_tree_vec (count
);
34912 initv
= make_tree_vec (count
);
34913 condv
= make_tree_vec (count
);
34914 incrv
= make_tree_vec (count
);
34916 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
34918 for (i
= 0; i
< count
; i
++)
34920 int bracecount
= 0;
34921 tree add_private_clause
= NULL_TREE
;
34924 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34927 cp_parser_error (parser
, "for statement expected");
34930 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
34932 matching_parens parens
;
34933 if (!parens
.require_open (parser
))
34936 init
= orig_init
= decl
= real_decl
= NULL
;
34937 this_pre_body
= push_stmt_list ();
34940 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
34941 init
, orig_init
, decl
, real_decl
);
34943 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34946 this_pre_body
= pop_stmt_list (this_pre_body
);
34950 pre_body
= push_stmt_list ();
34952 add_stmt (this_pre_body
);
34953 pre_body
= pop_stmt_list (pre_body
);
34956 pre_body
= this_pre_body
;
34961 if (cclauses
!= NULL
34962 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
34963 && real_decl
!= NULL_TREE
)
34966 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
34967 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
34968 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34970 error_at (loc
, "iteration variable %qD"
34971 " should not be firstprivate", real_decl
);
34972 *c
= OMP_CLAUSE_CHAIN (*c
);
34974 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
34975 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34977 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34979 *c
= OMP_CLAUSE_CHAIN (*c
);
34980 if (code
== OMP_SIMD
)
34982 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34983 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
34987 OMP_CLAUSE_CHAIN (l
) = clauses
;
34990 add_private_clause
= NULL_TREE
;
34994 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
34995 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34996 add_private_clause
= NULL_TREE
;
34997 c
= &OMP_CLAUSE_CHAIN (*c
);
35001 if (add_private_clause
)
35004 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35006 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35007 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35008 && OMP_CLAUSE_DECL (c
) == decl
)
35010 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35011 && OMP_CLAUSE_DECL (c
) == decl
)
35012 error_at (loc
, "iteration variable %qD "
35013 "should not be firstprivate",
35015 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35016 && OMP_CLAUSE_DECL (c
) == decl
)
35017 error_at (loc
, "iteration variable %qD should not be reduction",
35022 if (code
!= OMP_SIMD
)
35023 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35024 else if (collapse
== 1)
35025 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35027 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35028 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35029 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35032 OMP_CLAUSE_CHAIN (c
) = clauses
;
35034 /* For linear, signal that we need to fill up
35035 the so far unknown linear step. */
35036 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35037 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35043 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35044 cond
= cp_parser_omp_for_cond (parser
, decl
);
35045 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35048 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35050 /* If decl is an iterator, preserve the operator on decl
35051 until finish_omp_for. */
35053 && ((processing_template_decl
35054 && (TREE_TYPE (real_decl
) == NULL_TREE
35055 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35056 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35057 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35059 incr
= cp_parser_expression (parser
);
35060 if (!EXPR_HAS_LOCATION (incr
))
35061 protected_set_expr_location (incr
, input_location
);
35064 if (!parens
.require_close (parser
))
35065 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35066 /*or_comma=*/false,
35067 /*consume_paren=*/true);
35069 TREE_VEC_ELT (declv
, i
) = decl
;
35070 TREE_VEC_ELT (initv
, i
) = init
;
35071 TREE_VEC_ELT (condv
, i
) = cond
;
35072 TREE_VEC_ELT (incrv
, i
) = incr
;
35075 orig_inits
.safe_grow_cleared (i
+ 1);
35076 orig_inits
[i
] = orig_init
;
35079 if (i
== count
- 1)
35082 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35083 in between the collapsed for loops to be still considered perfectly
35084 nested. Hopefully the final version clarifies this.
35085 For now handle (multiple) {'s and empty statements. */
35086 cp_parser_parse_tentatively (parser
);
35089 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35091 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35093 cp_lexer_consume_token (parser
->lexer
);
35096 else if (bracecount
35097 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35098 cp_lexer_consume_token (parser
->lexer
);
35101 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35102 error_at (loc
, "not enough for loops to collapse");
35103 collapse_err
= true;
35104 cp_parser_abort_tentative_parse (parser
);
35112 cp_parser_parse_definitely (parser
);
35113 nbraces
+= bracecount
;
35120 /* Note that we saved the original contents of this flag when we entered
35121 the structured block, and so we don't need to re-save it here. */
35122 parser
->in_statement
= IN_OMP_FOR
;
35124 /* Note that the grammar doesn't call for a structured block here,
35125 though the loop as a whole is a structured block. */
35126 body
= push_stmt_list ();
35127 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35128 body
= pop_stmt_list (body
);
35130 if (declv
== NULL_TREE
)
35133 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35134 body
, pre_body
, &orig_inits
, clauses
);
35138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35140 cp_lexer_consume_token (parser
->lexer
);
35143 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35144 cp_lexer_consume_token (parser
->lexer
);
35149 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35150 "collapsed loops not perfectly nested");
35152 collapse_err
= true;
35153 cp_parser_statement_seq_opt (parser
, NULL
);
35154 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35159 while (!for_block
->is_empty ())
35161 tree t
= for_block
->pop ();
35162 if (TREE_CODE (t
) == STATEMENT_LIST
)
35163 add_stmt (pop_stmt_list (t
));
35167 release_tree_vector (for_block
);
35172 /* Helper function for OpenMP parsing, split clauses and call
35173 finish_omp_clauses on each of the set of clauses afterwards. */
35176 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35177 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35180 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35181 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35183 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35187 #pragma omp simd simd-clause[optseq] new-line
35190 #define OMP_SIMD_CLAUSE_MASK \
35191 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35201 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35202 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35205 tree clauses
, sb
, ret
;
35207 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35209 strcat (p_name
, " simd");
35210 mask
|= OMP_SIMD_CLAUSE_MASK
;
35212 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35216 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35217 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35218 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35219 OMP_CLAUSE_ORDERED
);
35220 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35222 error_at (OMP_CLAUSE_LOCATION (c
),
35223 "%<ordered%> clause with parameter may not be specified "
35224 "on %qs construct", p_name
);
35225 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35229 sb
= begin_omp_structured_block ();
35230 save
= cp_parser_begin_omp_structured_block (parser
);
35232 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35234 cp_parser_end_omp_structured_block (parser
, save
);
35235 add_stmt (finish_omp_structured_block (sb
));
35241 #pragma omp for for-clause[optseq] new-line
35245 #pragma omp for simd for-simd-clause[optseq] new-line
35248 #define OMP_FOR_CLAUSE_MASK \
35249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35260 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35261 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35264 tree clauses
, sb
, ret
;
35266 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35268 strcat (p_name
, " for");
35269 mask
|= OMP_FOR_CLAUSE_MASK
;
35270 /* parallel for{, simd} disallows nowait clause, but for
35271 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35272 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35273 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35274 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35275 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35276 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35278 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35280 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35281 const char *p
= IDENTIFIER_POINTER (id
);
35283 if (strcmp (p
, "simd") == 0)
35285 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35286 if (cclauses
== NULL
)
35287 cclauses
= cclauses_buf
;
35289 cp_lexer_consume_token (parser
->lexer
);
35290 if (!flag_openmp
) /* flag_openmp_simd */
35291 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35293 sb
= begin_omp_structured_block ();
35294 save
= cp_parser_begin_omp_structured_block (parser
);
35295 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35297 cp_parser_end_omp_structured_block (parser
, save
);
35298 tree body
= finish_omp_structured_block (sb
);
35301 ret
= make_node (OMP_FOR
);
35302 TREE_TYPE (ret
) = void_type_node
;
35303 OMP_FOR_BODY (ret
) = body
;
35304 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35305 SET_EXPR_LOCATION (ret
, loc
);
35310 if (!flag_openmp
) /* flag_openmp_simd */
35312 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35316 /* Composite distribute parallel for disallows linear clause. */
35317 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35318 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35320 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35324 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35325 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35328 sb
= begin_omp_structured_block ();
35329 save
= cp_parser_begin_omp_structured_block (parser
);
35331 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35333 cp_parser_end_omp_structured_block (parser
, save
);
35334 add_stmt (finish_omp_structured_block (sb
));
35340 # pragma omp master new-line
35341 structured-block */
35344 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35346 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35347 return c_finish_omp_master (input_location
,
35348 cp_parser_omp_structured_block (parser
, if_p
));
35352 # pragma omp ordered new-line
35356 # pragma omp ordered ordered-clauses new-line
35357 structured-block */
35359 #define OMP_ORDERED_CLAUSE_MASK \
35360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35363 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35364 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35367 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35368 enum pragma_context context
, bool *if_p
)
35370 location_t loc
= pragma_tok
->location
;
35372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35374 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35375 const char *p
= IDENTIFIER_POINTER (id
);
35377 if (strcmp (p
, "depend") == 0)
35379 if (!flag_openmp
) /* flag_openmp_simd */
35381 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35384 if (context
== pragma_stmt
)
35386 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35387 "%<depend%> clause may only be used in compound "
35389 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35393 = cp_parser_omp_all_clauses (parser
,
35394 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35395 "#pragma omp ordered", pragma_tok
);
35396 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35402 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35403 "#pragma omp ordered", pragma_tok
);
35405 if (!flag_openmp
/* flag_openmp_simd */
35406 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35409 c_finish_omp_ordered (loc
, clauses
,
35410 cp_parser_omp_structured_block (parser
, if_p
));
35417 { section-sequence }
35420 section-directive[opt] structured-block
35421 section-sequence section-directive structured-block */
35424 cp_parser_omp_sections_scope (cp_parser
*parser
)
35426 tree stmt
, substmt
;
35427 bool error_suppress
= false;
35430 matching_braces braces
;
35431 if (!braces
.require_open (parser
))
35434 stmt
= push_stmt_list ();
35436 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35437 != PRAGMA_OMP_SECTION
)
35439 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35440 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35441 add_stmt (substmt
);
35446 tok
= cp_lexer_peek_token (parser
->lexer
);
35447 if (tok
->type
== CPP_CLOSE_BRACE
)
35449 if (tok
->type
== CPP_EOF
)
35452 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35454 cp_lexer_consume_token (parser
->lexer
);
35455 cp_parser_require_pragma_eol (parser
, tok
);
35456 error_suppress
= false;
35458 else if (!error_suppress
)
35460 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35461 error_suppress
= true;
35464 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35465 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35466 add_stmt (substmt
);
35468 braces
.require_close (parser
);
35470 substmt
= pop_stmt_list (stmt
);
35472 stmt
= make_node (OMP_SECTIONS
);
35473 TREE_TYPE (stmt
) = void_type_node
;
35474 OMP_SECTIONS_BODY (stmt
) = substmt
;
35481 # pragma omp sections sections-clause[optseq] newline
35484 #define OMP_SECTIONS_CLAUSE_MASK \
35485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
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_NOWAIT))
35492 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35493 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35496 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35498 strcat (p_name
, " sections");
35499 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35501 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35503 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35507 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35508 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35511 ret
= cp_parser_omp_sections_scope (parser
);
35513 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35519 # pragma omp parallel parallel-clause[optseq] new-line
35521 # pragma omp parallel for parallel-for-clause[optseq] new-line
35523 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35527 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35528 structured-block */
35530 #define OMP_PARALLEL_CLAUSE_MASK \
35531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35542 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35543 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35546 tree stmt
, clauses
, block
;
35548 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35550 strcat (p_name
, " parallel");
35551 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35552 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35553 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35554 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35555 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35557 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35559 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35560 if (cclauses
== NULL
)
35561 cclauses
= cclauses_buf
;
35563 cp_lexer_consume_token (parser
->lexer
);
35564 if (!flag_openmp
) /* flag_openmp_simd */
35565 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35567 block
= begin_omp_parallel ();
35568 save
= cp_parser_begin_omp_structured_block (parser
);
35569 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35571 cp_parser_end_omp_structured_block (parser
, save
);
35572 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35574 if (ret
== NULL_TREE
)
35576 OMP_PARALLEL_COMBINED (stmt
) = 1;
35579 /* When combined with distribute, parallel has to be followed by for.
35580 #pragma omp target parallel is allowed though. */
35582 && (mask
& (OMP_CLAUSE_MASK_1
35583 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35585 error_at (loc
, "expected %<for%> after %qs", p_name
);
35586 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35589 else if (!flag_openmp
) /* flag_openmp_simd */
35591 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35594 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35596 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35597 const char *p
= IDENTIFIER_POINTER (id
);
35598 if (strcmp (p
, "sections") == 0)
35600 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35601 cclauses
= cclauses_buf
;
35603 cp_lexer_consume_token (parser
->lexer
);
35604 block
= begin_omp_parallel ();
35605 save
= cp_parser_begin_omp_structured_block (parser
);
35606 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35607 cp_parser_end_omp_structured_block (parser
, save
);
35608 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35610 OMP_PARALLEL_COMBINED (stmt
) = 1;
35615 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35619 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35620 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35623 block
= begin_omp_parallel ();
35624 save
= cp_parser_begin_omp_structured_block (parser
);
35625 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35626 cp_parser_end_omp_structured_block (parser
, save
);
35627 stmt
= finish_omp_parallel (clauses
, block
);
35632 # pragma omp single single-clause[optseq] new-line
35633 structured-block */
35635 #define OMP_SINGLE_CLAUSE_MASK \
35636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35642 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35644 tree stmt
= make_node (OMP_SINGLE
);
35645 TREE_TYPE (stmt
) = void_type_node
;
35647 OMP_SINGLE_CLAUSES (stmt
)
35648 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35649 "#pragma omp single", pragma_tok
);
35650 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35652 return add_stmt (stmt
);
35656 # pragma omp task task-clause[optseq] new-line
35657 structured-block */
35659 #define OMP_TASK_CLAUSE_MASK \
35660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35672 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35674 tree clauses
, block
;
35677 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35678 "#pragma omp task", pragma_tok
);
35679 block
= begin_omp_task ();
35680 save
= cp_parser_begin_omp_structured_block (parser
);
35681 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35682 cp_parser_end_omp_structured_block (parser
, save
);
35683 return finish_omp_task (clauses
, block
);
35687 # pragma omp taskwait new-line */
35690 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35692 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35693 finish_omp_taskwait ();
35697 # pragma omp taskyield new-line */
35700 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35702 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35703 finish_omp_taskyield ();
35707 # pragma omp taskgroup new-line
35708 structured-block */
35711 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35713 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35714 return c_finish_omp_taskgroup (input_location
,
35715 cp_parser_omp_structured_block (parser
,
35721 # pragma omp threadprivate (variable-list) */
35724 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35728 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35729 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35731 finish_omp_threadprivate (vars
);
35735 # pragma omp cancel cancel-clause[optseq] new-line */
35737 #define OMP_CANCEL_CLAUSE_MASK \
35738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35745 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35747 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35748 "#pragma omp cancel", pragma_tok
);
35749 finish_omp_cancel (clauses
);
35753 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35755 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35756 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35762 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35763 enum pragma_context context
)
35766 bool point_seen
= false;
35768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35770 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35771 const char *p
= IDENTIFIER_POINTER (id
);
35773 if (strcmp (p
, "point") == 0)
35775 cp_lexer_consume_token (parser
->lexer
);
35781 cp_parser_error (parser
, "expected %<point%>");
35782 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35786 if (context
!= pragma_compound
)
35788 if (context
== pragma_stmt
)
35789 error_at (pragma_tok
->location
,
35790 "%<#pragma %s%> may only be used in compound statements",
35791 "omp cancellation point");
35793 cp_parser_error (parser
, "expected declaration specifiers");
35794 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35798 clauses
= cp_parser_omp_all_clauses (parser
,
35799 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35800 "#pragma omp cancellation point",
35802 finish_omp_cancellation_point (clauses
);
35806 #pragma omp distribute distribute-clause[optseq] new-line
35809 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35817 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35818 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35821 tree clauses
, sb
, ret
;
35823 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35825 strcat (p_name
, " distribute");
35826 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
35828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35830 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35831 const char *p
= IDENTIFIER_POINTER (id
);
35833 bool parallel
= false;
35835 if (strcmp (p
, "simd") == 0)
35838 parallel
= strcmp (p
, "parallel") == 0;
35839 if (parallel
|| simd
)
35841 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35842 if (cclauses
== NULL
)
35843 cclauses
= cclauses_buf
;
35844 cp_lexer_consume_token (parser
->lexer
);
35845 if (!flag_openmp
) /* flag_openmp_simd */
35848 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35851 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35854 sb
= begin_omp_structured_block ();
35855 save
= cp_parser_begin_omp_structured_block (parser
);
35857 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35860 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35862 cp_parser_end_omp_structured_block (parser
, save
);
35863 tree body
= finish_omp_structured_block (sb
);
35866 ret
= make_node (OMP_DISTRIBUTE
);
35867 TREE_TYPE (ret
) = void_type_node
;
35868 OMP_FOR_BODY (ret
) = body
;
35869 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35870 SET_EXPR_LOCATION (ret
, loc
);
35875 if (!flag_openmp
) /* flag_openmp_simd */
35877 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35881 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35885 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
35886 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35889 sb
= begin_omp_structured_block ();
35890 save
= cp_parser_begin_omp_structured_block (parser
);
35892 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
35894 cp_parser_end_omp_structured_block (parser
, save
);
35895 add_stmt (finish_omp_structured_block (sb
));
35901 # pragma omp teams teams-clause[optseq] new-line
35902 structured-block */
35904 #define OMP_TEAMS_CLAUSE_MASK \
35905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35914 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
35915 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35918 tree clauses
, sb
, ret
;
35920 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35922 strcat (p_name
, " teams");
35923 mask
|= OMP_TEAMS_CLAUSE_MASK
;
35925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35927 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35928 const char *p
= IDENTIFIER_POINTER (id
);
35929 if (strcmp (p
, "distribute") == 0)
35931 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35932 if (cclauses
== NULL
)
35933 cclauses
= cclauses_buf
;
35935 cp_lexer_consume_token (parser
->lexer
);
35936 if (!flag_openmp
) /* flag_openmp_simd */
35937 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35939 sb
= begin_omp_structured_block ();
35940 save
= cp_parser_begin_omp_structured_block (parser
);
35941 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35943 cp_parser_end_omp_structured_block (parser
, save
);
35944 tree body
= finish_omp_structured_block (sb
);
35947 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35948 ret
= make_node (OMP_TEAMS
);
35949 TREE_TYPE (ret
) = void_type_node
;
35950 OMP_TEAMS_CLAUSES (ret
) = clauses
;
35951 OMP_TEAMS_BODY (ret
) = body
;
35952 OMP_TEAMS_COMBINED (ret
) = 1;
35953 SET_EXPR_LOCATION (ret
, loc
);
35954 return add_stmt (ret
);
35957 if (!flag_openmp
) /* flag_openmp_simd */
35959 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35963 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35967 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
35968 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35971 tree stmt
= make_node (OMP_TEAMS
);
35972 TREE_TYPE (stmt
) = void_type_node
;
35973 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
35974 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35975 SET_EXPR_LOCATION (stmt
, loc
);
35977 return add_stmt (stmt
);
35981 # pragma omp target data target-data-clause[optseq] new-line
35982 structured-block */
35984 #define OMP_TARGET_DATA_CLAUSE_MASK \
35985 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35991 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35994 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
35995 "#pragma omp target data", pragma_tok
);
35997 for (tree
*pc
= &clauses
; *pc
;)
35999 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36000 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36003 case GOMP_MAP_ALWAYS_TO
:
36004 case GOMP_MAP_FROM
:
36005 case GOMP_MAP_ALWAYS_FROM
:
36006 case GOMP_MAP_TOFROM
:
36007 case GOMP_MAP_ALWAYS_TOFROM
:
36008 case GOMP_MAP_ALLOC
:
36011 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36012 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36013 case GOMP_MAP_ALWAYS_POINTER
:
36017 error_at (OMP_CLAUSE_LOCATION (*pc
),
36018 "%<#pragma omp target data%> with map-type other "
36019 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36020 "on %<map%> clause");
36021 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36024 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36030 error_at (pragma_tok
->location
,
36031 "%<#pragma omp target data%> must contain at least "
36032 "one %<map%> clause");
36036 tree stmt
= make_node (OMP_TARGET_DATA
);
36037 TREE_TYPE (stmt
) = void_type_node
;
36038 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36040 keep_next_level (true);
36041 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36043 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36044 return add_stmt (stmt
);
36048 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36049 structured-block */
36051 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36052 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36059 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36060 enum pragma_context context
)
36062 bool data_seen
= false;
36063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36065 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36066 const char *p
= IDENTIFIER_POINTER (id
);
36068 if (strcmp (p
, "data") == 0)
36070 cp_lexer_consume_token (parser
->lexer
);
36076 cp_parser_error (parser
, "expected %<data%>");
36077 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36081 if (context
== pragma_stmt
)
36083 error_at (pragma_tok
->location
,
36084 "%<#pragma %s%> may only be used in compound statements",
36085 "omp target enter data");
36086 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36091 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36092 "#pragma omp target enter data", pragma_tok
);
36094 for (tree
*pc
= &clauses
; *pc
;)
36096 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36097 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36100 case GOMP_MAP_ALWAYS_TO
:
36101 case GOMP_MAP_ALLOC
:
36104 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36105 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36106 case GOMP_MAP_ALWAYS_POINTER
:
36110 error_at (OMP_CLAUSE_LOCATION (*pc
),
36111 "%<#pragma omp target enter data%> with map-type other "
36112 "than %<to%> or %<alloc%> on %<map%> clause");
36113 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36116 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36122 error_at (pragma_tok
->location
,
36123 "%<#pragma omp target enter data%> must contain at least "
36124 "one %<map%> clause");
36128 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36129 TREE_TYPE (stmt
) = void_type_node
;
36130 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36131 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36132 return add_stmt (stmt
);
36136 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36137 structured-block */
36139 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36147 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36148 enum pragma_context context
)
36150 bool data_seen
= false;
36151 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36153 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36154 const char *p
= IDENTIFIER_POINTER (id
);
36156 if (strcmp (p
, "data") == 0)
36158 cp_lexer_consume_token (parser
->lexer
);
36164 cp_parser_error (parser
, "expected %<data%>");
36165 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36169 if (context
== pragma_stmt
)
36171 error_at (pragma_tok
->location
,
36172 "%<#pragma %s%> may only be used in compound statements",
36173 "omp target exit data");
36174 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36179 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36180 "#pragma omp target exit data", pragma_tok
);
36182 for (tree
*pc
= &clauses
; *pc
;)
36184 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36185 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36187 case GOMP_MAP_FROM
:
36188 case GOMP_MAP_ALWAYS_FROM
:
36189 case GOMP_MAP_RELEASE
:
36190 case GOMP_MAP_DELETE
:
36193 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36194 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36195 case GOMP_MAP_ALWAYS_POINTER
:
36199 error_at (OMP_CLAUSE_LOCATION (*pc
),
36200 "%<#pragma omp target exit data%> with map-type other "
36201 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36203 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36206 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36212 error_at (pragma_tok
->location
,
36213 "%<#pragma omp target exit data%> must contain at least "
36214 "one %<map%> clause");
36218 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36219 TREE_TYPE (stmt
) = void_type_node
;
36220 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36221 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36222 return add_stmt (stmt
);
36226 # pragma omp target update target-update-clause[optseq] new-line */
36228 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36229 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36237 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36238 enum pragma_context context
)
36240 if (context
== pragma_stmt
)
36242 error_at (pragma_tok
->location
,
36243 "%<#pragma %s%> may only be used in compound statements",
36244 "omp target update");
36245 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36250 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36251 "#pragma omp target update", pragma_tok
);
36252 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36253 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36255 error_at (pragma_tok
->location
,
36256 "%<#pragma omp target update%> must contain at least one "
36257 "%<from%> or %<to%> clauses");
36261 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36262 TREE_TYPE (stmt
) = void_type_node
;
36263 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36264 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36270 # pragma omp target target-clause[optseq] new-line
36271 structured-block */
36273 #define OMP_TARGET_CLAUSE_MASK \
36274 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36285 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36286 enum pragma_context context
, bool *if_p
)
36288 tree
*pc
= NULL
, stmt
;
36290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36292 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36293 const char *p
= IDENTIFIER_POINTER (id
);
36294 enum tree_code ccode
= ERROR_MARK
;
36296 if (strcmp (p
, "teams") == 0)
36298 else if (strcmp (p
, "parallel") == 0)
36299 ccode
= OMP_PARALLEL
;
36300 else if (strcmp (p
, "simd") == 0)
36302 if (ccode
!= ERROR_MARK
)
36304 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36305 char p_name
[sizeof ("#pragma omp target teams distribute "
36306 "parallel for simd")];
36308 cp_lexer_consume_token (parser
->lexer
);
36309 strcpy (p_name
, "#pragma omp target");
36310 if (!flag_openmp
) /* flag_openmp_simd */
36316 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36317 OMP_TARGET_CLAUSE_MASK
,
36321 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36322 OMP_TARGET_CLAUSE_MASK
,
36326 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36327 OMP_TARGET_CLAUSE_MASK
,
36331 gcc_unreachable ();
36333 return stmt
!= NULL_TREE
;
36335 keep_next_level (true);
36336 tree sb
= begin_omp_structured_block (), ret
;
36337 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36341 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36342 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36346 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36347 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36351 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36352 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36356 gcc_unreachable ();
36358 cp_parser_end_omp_structured_block (parser
, save
);
36359 tree body
= finish_omp_structured_block (sb
);
36360 if (ret
== NULL_TREE
)
36362 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36364 /* For combined target teams, ensure the num_teams and
36365 thread_limit clause expressions are evaluated on the host,
36366 before entering the target construct. */
36368 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36369 c
; c
= OMP_CLAUSE_CHAIN (c
))
36370 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36371 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36372 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36374 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36375 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36376 if (expr
== error_mark_node
)
36378 tree tmp
= TARGET_EXPR_SLOT (expr
);
36380 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36381 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36382 OMP_CLAUSE_FIRSTPRIVATE
);
36383 OMP_CLAUSE_DECL (tc
) = tmp
;
36384 OMP_CLAUSE_CHAIN (tc
)
36385 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36386 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36389 tree stmt
= make_node (OMP_TARGET
);
36390 TREE_TYPE (stmt
) = void_type_node
;
36391 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36392 OMP_TARGET_BODY (stmt
) = body
;
36393 OMP_TARGET_COMBINED (stmt
) = 1;
36394 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36396 pc
= &OMP_TARGET_CLAUSES (stmt
);
36397 goto check_clauses
;
36399 else if (!flag_openmp
) /* flag_openmp_simd */
36401 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36404 else if (strcmp (p
, "data") == 0)
36406 cp_lexer_consume_token (parser
->lexer
);
36407 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36410 else if (strcmp (p
, "enter") == 0)
36412 cp_lexer_consume_token (parser
->lexer
);
36413 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36416 else if (strcmp (p
, "exit") == 0)
36418 cp_lexer_consume_token (parser
->lexer
);
36419 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36422 else if (strcmp (p
, "update") == 0)
36424 cp_lexer_consume_token (parser
->lexer
);
36425 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36428 if (!flag_openmp
) /* flag_openmp_simd */
36430 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36434 stmt
= make_node (OMP_TARGET
);
36435 TREE_TYPE (stmt
) = void_type_node
;
36437 OMP_TARGET_CLAUSES (stmt
)
36438 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36439 "#pragma omp target", pragma_tok
);
36440 pc
= &OMP_TARGET_CLAUSES (stmt
);
36441 keep_next_level (true);
36442 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36444 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36450 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36451 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36454 case GOMP_MAP_ALWAYS_TO
:
36455 case GOMP_MAP_FROM
:
36456 case GOMP_MAP_ALWAYS_FROM
:
36457 case GOMP_MAP_TOFROM
:
36458 case GOMP_MAP_ALWAYS_TOFROM
:
36459 case GOMP_MAP_ALLOC
:
36460 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36461 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36462 case GOMP_MAP_ALWAYS_POINTER
:
36465 error_at (OMP_CLAUSE_LOCATION (*pc
),
36466 "%<#pragma omp target%> with map-type other "
36467 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36468 "on %<map%> clause");
36469 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36472 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36478 # pragma acc cache (variable-list) new-line
36482 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36484 tree stmt
, clauses
;
36486 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36487 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36489 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36491 stmt
= make_node (OACC_CACHE
);
36492 TREE_TYPE (stmt
) = void_type_node
;
36493 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36494 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36501 # pragma acc data oacc-data-clause[optseq] new-line
36502 structured-block */
36504 #define OACC_DATA_CLAUSE_MASK \
36505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36518 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36520 tree stmt
, clauses
, block
;
36523 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36524 "#pragma acc data", pragma_tok
);
36526 block
= begin_omp_parallel ();
36527 save
= cp_parser_begin_omp_structured_block (parser
);
36528 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36529 cp_parser_end_omp_structured_block (parser
, save
);
36530 stmt
= finish_oacc_data (clauses
, block
);
36535 # pragma acc host_data <clauses> new-line
36536 structured-block */
36538 #define OACC_HOST_DATA_CLAUSE_MASK \
36539 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36542 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36544 tree stmt
, clauses
, block
;
36547 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36548 "#pragma acc host_data", pragma_tok
);
36550 block
= begin_omp_parallel ();
36551 save
= cp_parser_begin_omp_structured_block (parser
);
36552 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36553 cp_parser_end_omp_structured_block (parser
, save
);
36554 stmt
= finish_oacc_host_data (clauses
, block
);
36559 # pragma acc declare oacc-data-clause[optseq] new-line
36562 #define OACC_DECLARE_CLAUSE_MASK \
36563 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36577 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36579 tree clauses
, stmt
;
36580 bool error
= false;
36582 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36583 "#pragma acc declare", pragma_tok
, true);
36586 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36588 error_at (pragma_tok
->location
,
36589 "no valid clauses specified in %<#pragma acc declare%>");
36593 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36595 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36596 tree decl
= OMP_CLAUSE_DECL (t
);
36597 if (!DECL_P (decl
))
36599 error_at (loc
, "array section in %<#pragma acc declare%>");
36603 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36604 switch (OMP_CLAUSE_MAP_KIND (t
))
36606 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36607 case GOMP_MAP_FORCE_ALLOC
:
36608 case GOMP_MAP_FORCE_TO
:
36609 case GOMP_MAP_FORCE_DEVICEPTR
:
36610 case GOMP_MAP_DEVICE_RESIDENT
:
36613 case GOMP_MAP_LINK
:
36614 if (!global_bindings_p ()
36615 && (TREE_STATIC (decl
)
36616 || !DECL_EXTERNAL (decl
)))
36619 "%qD must be a global variable in "
36620 "%<#pragma acc declare link%>",
36628 if (global_bindings_p ())
36630 error_at (loc
, "invalid OpenACC clause at file scope");
36634 if (DECL_EXTERNAL (decl
))
36637 "invalid use of %<extern%> variable %qD "
36638 "in %<#pragma acc declare%>", decl
);
36642 else if (TREE_PUBLIC (decl
))
36645 "invalid use of %<global%> variable %qD "
36646 "in %<#pragma acc declare%>", decl
);
36653 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36654 || lookup_attribute ("omp declare target link",
36655 DECL_ATTRIBUTES (decl
)))
36657 error_at (loc
, "variable %qD used more than once with "
36658 "%<#pragma acc declare%>", decl
);
36667 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36668 id
= get_identifier ("omp declare target link");
36670 id
= get_identifier ("omp declare target");
36672 DECL_ATTRIBUTES (decl
)
36673 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36674 if (global_bindings_p ())
36676 symtab_node
*node
= symtab_node::get (decl
);
36679 node
->offloadable
= 1;
36680 if (ENABLE_OFFLOADING
)
36682 g
->have_offload
= true;
36683 if (is_a
<varpool_node
*> (node
))
36684 vec_safe_push (offload_vars
, decl
);
36691 if (error
|| global_bindings_p ())
36694 stmt
= make_node (OACC_DECLARE
);
36695 TREE_TYPE (stmt
) = void_type_node
;
36696 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36697 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36705 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36709 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36711 LOC is the location of the #pragma token.
36714 #define OACC_ENTER_DATA_CLAUSE_MASK \
36715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36723 #define OACC_EXIT_DATA_CLAUSE_MASK \
36724 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36731 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36734 location_t loc
= pragma_tok
->location
;
36735 tree stmt
, clauses
;
36736 const char *p
= "";
36738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36739 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36741 if (strcmp (p
, "data") != 0)
36743 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36744 enter
? "enter" : "exit");
36745 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36749 cp_lexer_consume_token (parser
->lexer
);
36752 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36753 "#pragma acc enter data", pragma_tok
);
36755 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36756 "#pragma acc exit data", pragma_tok
);
36758 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36760 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36761 enter
? "enter" : "exit");
36765 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36766 TREE_TYPE (stmt
) = void_type_node
;
36767 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36768 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36774 # pragma acc loop oacc-loop-clause[optseq] new-line
36775 structured-block */
36777 #define OACC_LOOP_CLAUSE_MASK \
36778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36790 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36791 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36793 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36795 strcat (p_name
, " loop");
36796 mask
|= OACC_LOOP_CLAUSE_MASK
;
36798 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36802 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36804 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36806 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36809 tree block
= begin_omp_structured_block ();
36810 int save
= cp_parser_begin_omp_structured_block (parser
);
36811 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36812 cp_parser_end_omp_structured_block (parser
, save
);
36813 add_stmt (finish_omp_structured_block (block
));
36819 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36824 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36828 #define OACC_KERNELS_CLAUSE_MASK \
36829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36847 #define OACC_PARALLEL_CLAUSE_MASK \
36848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36870 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
36871 char *p_name
, bool *if_p
)
36873 omp_clause_mask mask
;
36874 enum tree_code code
;
36875 switch (cp_parser_pragma_kind (pragma_tok
))
36877 case PRAGMA_OACC_KERNELS
:
36878 strcat (p_name
, " kernels");
36879 mask
= OACC_KERNELS_CLAUSE_MASK
;
36880 code
= OACC_KERNELS
;
36882 case PRAGMA_OACC_PARALLEL
:
36883 strcat (p_name
, " parallel");
36884 mask
= OACC_PARALLEL_CLAUSE_MASK
;
36885 code
= OACC_PARALLEL
;
36888 gcc_unreachable ();
36891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36894 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36895 if (strcmp (p
, "loop") == 0)
36897 cp_lexer_consume_token (parser
->lexer
);
36898 tree block
= begin_omp_parallel ();
36900 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
36902 return finish_omp_construct (code
, block
, clauses
);
36906 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
36908 tree block
= begin_omp_parallel ();
36909 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
36910 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36911 cp_parser_end_omp_structured_block (parser
, save
);
36912 return finish_omp_construct (code
, block
, clauses
);
36916 # pragma acc update oacc-update-clause[optseq] new-line
36919 #define OACC_UPDATE_CLAUSE_MASK \
36920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36928 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
36930 tree stmt
, clauses
;
36932 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
36933 "#pragma acc update", pragma_tok
);
36935 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36937 error_at (pragma_tok
->location
,
36938 "%<#pragma acc update%> must contain at least one "
36939 "%<device%> or %<host%> or %<self%> clause");
36943 stmt
= make_node (OACC_UPDATE
);
36944 TREE_TYPE (stmt
) = void_type_node
;
36945 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
36946 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36952 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36954 LOC is the location of the #pragma token.
36957 #define OACC_WAIT_CLAUSE_MASK \
36958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36961 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
36963 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
36964 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36966 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36967 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
36969 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
36970 "#pragma acc wait", pragma_tok
);
36972 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
36973 stmt
= finish_expr_stmt (stmt
);
36979 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36981 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36982 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36990 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36991 enum pragma_context context
)
36993 bool first_p
= parser
->omp_declare_simd
== NULL
;
36994 cp_omp_declare_simd_data data
;
36997 data
.error_seen
= false;
36998 data
.fndecl_seen
= false;
36999 data
.tokens
= vNULL
;
37000 data
.clauses
= NULL_TREE
;
37001 /* It is safe to take the address of a local variable; it will only be
37002 used while this scope is live. */
37003 parser
->omp_declare_simd
= &data
;
37006 /* Store away all pragma tokens. */
37007 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37008 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37009 cp_lexer_consume_token (parser
->lexer
);
37010 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37011 parser
->omp_declare_simd
->error_seen
= true;
37012 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37013 struct cp_token_cache
*cp
37014 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37015 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37019 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37020 cp_parser_pragma (parser
, context
, NULL
);
37023 case pragma_external
:
37024 cp_parser_declaration (parser
);
37026 case pragma_member
:
37027 cp_parser_member_declaration (parser
);
37029 case pragma_objc_icode
:
37030 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37033 cp_parser_declaration_statement (parser
);
37036 if (parser
->omp_declare_simd
37037 && !parser
->omp_declare_simd
->error_seen
37038 && !parser
->omp_declare_simd
->fndecl_seen
)
37039 error_at (pragma_tok
->location
,
37040 "%<#pragma omp declare simd%> not immediately followed by "
37041 "function declaration or definition");
37042 data
.tokens
.release ();
37043 parser
->omp_declare_simd
= NULL
;
37047 /* Finalize #pragma omp declare simd clauses after direct declarator has
37048 been parsed, and put that into "omp declare simd" attribute. */
37051 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37053 struct cp_token_cache
*ce
;
37054 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37057 if (!data
->error_seen
&& data
->fndecl_seen
)
37059 error ("%<#pragma omp declare simd%> not immediately followed by "
37060 "a single function declaration or definition");
37061 data
->error_seen
= true;
37063 if (data
->error_seen
)
37066 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37070 cp_parser_push_lexer_for_tokens (parser
, ce
);
37071 parser
->lexer
->in_pragma
= true;
37072 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37073 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37074 cp_lexer_consume_token (parser
->lexer
);
37075 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37076 "#pragma omp declare simd", pragma_tok
);
37077 cp_parser_pop_lexer (parser
);
37079 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37080 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37081 TREE_CHAIN (c
) = attrs
;
37082 if (processing_template_decl
)
37083 ATTR_IS_DEPENDENT (c
) = 1;
37087 data
->fndecl_seen
= true;
37093 # pragma omp declare target new-line
37094 declarations and definitions
37095 # pragma omp end declare target new-line
37098 # pragma omp declare target ( extended-list ) new-line
37100 # pragma omp declare target declare-target-clauses[seq] new-line */
37102 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37103 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37107 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37109 tree clauses
= NULL_TREE
;
37110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37112 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37113 "#pragma omp declare target", pragma_tok
);
37114 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37116 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37118 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37119 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37123 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37124 scope_chain
->omp_declare_target_attribute
++;
37127 if (scope_chain
->omp_declare_target_attribute
)
37128 error_at (pragma_tok
->location
,
37129 "%<#pragma omp declare target%> with clauses in between "
37130 "%<#pragma omp declare target%> without clauses and "
37131 "%<#pragma omp end declare target%>");
37132 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37134 tree t
= OMP_CLAUSE_DECL (c
), id
;
37135 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37136 tree at2
= lookup_attribute ("omp declare target link",
37137 DECL_ATTRIBUTES (t
));
37138 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37140 id
= get_identifier ("omp declare target link");
37141 std::swap (at1
, at2
);
37144 id
= get_identifier ("omp declare target");
37147 error_at (OMP_CLAUSE_LOCATION (c
),
37148 "%qD specified both in declare target %<link%> and %<to%>"
37154 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37155 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37158 symtab_node
*node
= symtab_node::get (t
);
37161 node
->offloadable
= 1;
37162 if (ENABLE_OFFLOADING
)
37164 g
->have_offload
= true;
37165 if (is_a
<varpool_node
*> (node
))
37166 vec_safe_push (offload_vars
, t
);
37174 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37176 const char *p
= "";
37177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37179 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37180 p
= IDENTIFIER_POINTER (id
);
37182 if (strcmp (p
, "declare") == 0)
37184 cp_lexer_consume_token (parser
->lexer
);
37186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37188 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37189 p
= IDENTIFIER_POINTER (id
);
37191 if (strcmp (p
, "target") == 0)
37192 cp_lexer_consume_token (parser
->lexer
);
37195 cp_parser_error (parser
, "expected %<target%>");
37196 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37202 cp_parser_error (parser
, "expected %<declare%>");
37203 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37206 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37207 if (!scope_chain
->omp_declare_target_attribute
)
37208 error_at (pragma_tok
->location
,
37209 "%<#pragma omp end declare target%> without corresponding "
37210 "%<#pragma omp declare target%>");
37212 scope_chain
->omp_declare_target_attribute
--;
37215 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37216 expression and optional initializer clause of
37217 #pragma omp declare reduction. We store the expression(s) as
37218 either 3, 6 or 7 special statements inside of the artificial function's
37219 body. The first two statements are DECL_EXPRs for the artificial
37220 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37221 expression that uses those variables.
37222 If there was any INITIALIZER clause, this is followed by further statements,
37223 the fourth and fifth statements are DECL_EXPRs for the artificial
37224 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37225 constructor variant (first token after open paren is not omp_priv),
37226 then the sixth statement is a statement with the function call expression
37227 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37228 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37229 to initialize the OMP_PRIV artificial variable and there is seventh
37230 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37233 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37235 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37236 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37237 type
= TREE_TYPE (type
);
37238 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37239 DECL_ARTIFICIAL (omp_out
) = 1;
37240 pushdecl (omp_out
);
37241 add_decl_expr (omp_out
);
37242 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37243 DECL_ARTIFICIAL (omp_in
) = 1;
37245 add_decl_expr (omp_in
);
37247 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37249 keep_next_level (true);
37250 tree block
= begin_omp_structured_block ();
37251 combiner
= cp_parser_expression (parser
);
37252 finish_expr_stmt (combiner
);
37253 block
= finish_omp_structured_block (block
);
37256 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37259 const char *p
= "";
37260 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37262 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37263 p
= IDENTIFIER_POINTER (id
);
37266 if (strcmp (p
, "initializer") == 0)
37268 cp_lexer_consume_token (parser
->lexer
);
37269 matching_parens parens
;
37270 if (!parens
.require_open (parser
))
37274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37276 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37277 p
= IDENTIFIER_POINTER (id
);
37280 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37281 DECL_ARTIFICIAL (omp_priv
) = 1;
37282 pushdecl (omp_priv
);
37283 add_decl_expr (omp_priv
);
37284 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37285 DECL_ARTIFICIAL (omp_orig
) = 1;
37286 pushdecl (omp_orig
);
37287 add_decl_expr (omp_orig
);
37289 keep_next_level (true);
37290 block
= begin_omp_structured_block ();
37293 if (strcmp (p
, "omp_priv") == 0)
37295 bool is_direct_init
, is_non_constant_init
;
37297 cp_lexer_consume_token (parser
->lexer
);
37298 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37300 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37301 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37303 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37304 == CPP_CLOSE_PAREN
))
37306 finish_omp_structured_block (block
);
37307 error ("invalid initializer clause");
37310 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37311 &is_non_constant_init
);
37312 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37313 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37317 cp_parser_parse_tentatively (parser
);
37318 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37319 /*check_dependency_p=*/true,
37320 /*template_p=*/NULL
,
37321 /*declarator_p=*/false,
37322 /*optional_p=*/false);
37323 vec
<tree
, va_gc
> *args
;
37324 if (fn_name
== error_mark_node
37325 || cp_parser_error_occurred (parser
)
37326 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37327 || ((args
= cp_parser_parenthesized_expression_list
37328 (parser
, non_attr
, /*cast_p=*/false,
37329 /*allow_expansion_p=*/true,
37330 /*non_constant_p=*/NULL
)),
37331 cp_parser_error_occurred (parser
)))
37333 finish_omp_structured_block (block
);
37334 cp_parser_abort_tentative_parse (parser
);
37335 cp_parser_error (parser
, "expected id-expression (arguments)");
37340 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37341 if (arg
== omp_priv
37342 || (TREE_CODE (arg
) == ADDR_EXPR
37343 && TREE_OPERAND (arg
, 0) == omp_priv
))
37345 cp_parser_abort_tentative_parse (parser
);
37346 if (arg
== NULL_TREE
)
37347 error ("one of the initializer call arguments should be %<omp_priv%>"
37348 " or %<&omp_priv%>");
37349 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37351 finish_expr_stmt (initializer
);
37354 block
= finish_omp_structured_block (block
);
37355 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37359 add_decl_expr (omp_orig
);
37361 if (!parens
.require_close (parser
))
37365 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37366 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37373 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37374 initializer-clause[opt] new-line
37376 initializer-clause:
37377 initializer (omp_priv initializer)
37378 initializer (function-name (argument-list)) */
37381 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37382 enum pragma_context
)
37384 auto_vec
<tree
> types
;
37385 enum tree_code reduc_code
= ERROR_MARK
;
37386 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37388 cp_token
*first_token
;
37389 cp_token_cache
*cp
;
37393 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37394 p
= obstack_alloc (&declarator_obstack
, 0);
37396 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37399 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37402 reduc_code
= PLUS_EXPR
;
37405 reduc_code
= MULT_EXPR
;
37408 reduc_code
= MINUS_EXPR
;
37411 reduc_code
= BIT_AND_EXPR
;
37414 reduc_code
= BIT_XOR_EXPR
;
37417 reduc_code
= BIT_IOR_EXPR
;
37420 reduc_code
= TRUTH_ANDIF_EXPR
;
37423 reduc_code
= TRUTH_ORIF_EXPR
;
37426 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37429 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37430 "%<|%>, %<&&%>, %<||%> or identifier");
37434 if (reduc_code
!= ERROR_MARK
)
37435 cp_lexer_consume_token (parser
->lexer
);
37437 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37438 if (reduc_id
== error_mark_node
)
37441 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37444 /* Types may not be defined in declare reduction type list. */
37445 const char *saved_message
;
37446 saved_message
= parser
->type_definition_forbidden_message
;
37447 parser
->type_definition_forbidden_message
37448 = G_("types may not be defined in declare reduction type list");
37449 bool saved_colon_corrects_to_scope_p
;
37450 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37451 parser
->colon_corrects_to_scope_p
= false;
37452 bool saved_colon_doesnt_start_class_def_p
;
37453 saved_colon_doesnt_start_class_def_p
37454 = parser
->colon_doesnt_start_class_def_p
;
37455 parser
->colon_doesnt_start_class_def_p
= true;
37459 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37460 type
= cp_parser_type_id (parser
);
37461 if (type
== error_mark_node
)
37463 else if (ARITHMETIC_TYPE_P (type
)
37464 && (orig_reduc_id
== NULL_TREE
37465 || (TREE_CODE (type
) != COMPLEX_TYPE
37466 && (id_equal (orig_reduc_id
, "min")
37467 || id_equal (orig_reduc_id
, "max")))))
37468 error_at (loc
, "predeclared arithmetic type %qT in "
37469 "%<#pragma omp declare reduction%>", type
);
37470 else if (TREE_CODE (type
) == FUNCTION_TYPE
37471 || TREE_CODE (type
) == METHOD_TYPE
37472 || TREE_CODE (type
) == ARRAY_TYPE
)
37473 error_at (loc
, "function or array type %qT in "
37474 "%<#pragma omp declare reduction%>", type
);
37475 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37476 error_at (loc
, "reference type %qT in "
37477 "%<#pragma omp declare reduction%>", type
);
37478 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37479 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37480 "%<#pragma omp declare reduction%>", type
);
37482 types
.safe_push (type
);
37484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37485 cp_lexer_consume_token (parser
->lexer
);
37490 /* Restore the saved message. */
37491 parser
->type_definition_forbidden_message
= saved_message
;
37492 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37493 parser
->colon_doesnt_start_class_def_p
37494 = saved_colon_doesnt_start_class_def_p
;
37496 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37497 || types
.is_empty ())
37500 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37504 first_token
= cp_lexer_peek_token (parser
->lexer
);
37507 FOR_EACH_VEC_ELT (types
, i
, type
)
37510 = build_function_type_list (void_type_node
,
37511 cp_build_reference_type (type
, false),
37513 tree this_reduc_id
= reduc_id
;
37514 if (!dependent_type_p (type
))
37515 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37516 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37517 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37518 DECL_ARTIFICIAL (fndecl
) = 1;
37519 DECL_EXTERNAL (fndecl
) = 1;
37520 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37521 DECL_IGNORED_P (fndecl
) = 1;
37522 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37523 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37524 DECL_ATTRIBUTES (fndecl
)
37525 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37526 DECL_ATTRIBUTES (fndecl
));
37527 if (processing_template_decl
)
37528 fndecl
= push_template_decl (fndecl
);
37529 bool block_scope
= false;
37530 tree block
= NULL_TREE
;
37531 if (current_function_decl
)
37533 block_scope
= true;
37534 DECL_CONTEXT (fndecl
) = global_namespace
;
37535 if (!processing_template_decl
)
37538 else if (current_class_type
)
37542 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37543 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37544 cp_lexer_consume_token (parser
->lexer
);
37545 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37547 cp
= cp_token_cache_new (first_token
,
37548 cp_lexer_peek_nth_token (parser
->lexer
,
37551 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37552 finish_member_declaration (fndecl
);
37553 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37554 DECL_PENDING_INLINE_P (fndecl
) = 1;
37555 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37560 DECL_CONTEXT (fndecl
) = current_namespace
;
37564 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37566 block
= begin_omp_structured_block ();
37569 cp_parser_push_lexer_for_tokens (parser
, cp
);
37570 parser
->lexer
->in_pragma
= true;
37572 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37575 finish_function (/*inline_p=*/false);
37577 DECL_CONTEXT (fndecl
) = current_function_decl
;
37579 cp_parser_pop_lexer (parser
);
37583 cp_parser_pop_lexer (parser
);
37585 finish_function (/*inline_p=*/false);
37588 DECL_CONTEXT (fndecl
) = current_function_decl
;
37589 block
= finish_omp_structured_block (block
);
37590 if (TREE_CODE (block
) == BIND_EXPR
)
37591 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37592 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37593 DECL_SAVED_TREE (fndecl
) = block
;
37594 if (processing_template_decl
)
37595 add_decl_expr (fndecl
);
37597 cp_check_omp_declare_reduction (fndecl
);
37598 if (cp
== NULL
&& types
.length () > 1)
37599 cp
= cp_token_cache_new (first_token
,
37600 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37601 if (errs
!= errorcount
)
37605 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37608 /* Free any declarators allocated. */
37609 obstack_free (&declarator_obstack
, p
);
37613 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37614 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37615 initializer-clause[opt] new-line
37616 #pragma omp declare target new-line */
37619 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37620 enum pragma_context context
)
37622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37624 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37625 const char *p
= IDENTIFIER_POINTER (id
);
37627 if (strcmp (p
, "simd") == 0)
37629 cp_lexer_consume_token (parser
->lexer
);
37630 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37634 cp_ensure_no_omp_declare_simd (parser
);
37635 if (strcmp (p
, "reduction") == 0)
37637 cp_lexer_consume_token (parser
->lexer
);
37638 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37642 if (!flag_openmp
) /* flag_openmp_simd */
37644 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37647 if (strcmp (p
, "target") == 0)
37649 cp_lexer_consume_token (parser
->lexer
);
37650 cp_parser_omp_declare_target (parser
, pragma_tok
);
37654 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37656 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37661 #pragma omp taskloop taskloop-clause[optseq] new-line
37664 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37667 #define OMP_TASKLOOP_CLAUSE_MASK \
37668 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37684 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37685 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37688 tree clauses
, sb
, ret
;
37690 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37692 strcat (p_name
, " taskloop");
37693 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37695 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37697 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37698 const char *p
= IDENTIFIER_POINTER (id
);
37700 if (strcmp (p
, "simd") == 0)
37702 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37703 if (cclauses
== NULL
)
37704 cclauses
= cclauses_buf
;
37706 cp_lexer_consume_token (parser
->lexer
);
37707 if (!flag_openmp
) /* flag_openmp_simd */
37708 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37710 sb
= begin_omp_structured_block ();
37711 save
= cp_parser_begin_omp_structured_block (parser
);
37712 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37714 cp_parser_end_omp_structured_block (parser
, save
);
37715 tree body
= finish_omp_structured_block (sb
);
37718 ret
= make_node (OMP_TASKLOOP
);
37719 TREE_TYPE (ret
) = void_type_node
;
37720 OMP_FOR_BODY (ret
) = body
;
37721 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37722 SET_EXPR_LOCATION (ret
, loc
);
37727 if (!flag_openmp
) /* flag_openmp_simd */
37729 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37733 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37737 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37738 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37741 sb
= begin_omp_structured_block ();
37742 save
= cp_parser_begin_omp_structured_block (parser
);
37744 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37747 cp_parser_end_omp_structured_block (parser
, save
);
37748 add_stmt (finish_omp_structured_block (sb
));
37755 # pragma acc routine oacc-routine-clause[optseq] new-line
37756 function-definition
37758 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37761 #define OACC_ROUTINE_CLAUSE_MASK \
37762 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37768 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37769 component, which must resolve to a declared namespace-scope
37770 function. The clauses are either processed directly (for a named
37771 function), or defered until the immediatley following declaration
37775 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37776 enum pragma_context context
)
37778 gcc_checking_assert (context
== pragma_external
);
37779 /* The checking for "another pragma following this one" in the "no optional
37780 '( name )'" case makes sure that we dont re-enter. */
37781 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37783 cp_oacc_routine_data data
;
37784 data
.error_seen
= false;
37785 data
.fndecl_seen
= false;
37786 data
.tokens
= vNULL
;
37787 data
.clauses
= NULL_TREE
;
37788 data
.loc
= pragma_tok
->location
;
37789 /* It is safe to take the address of a local variable; it will only be
37790 used while this scope is live. */
37791 parser
->oacc_routine
= &data
;
37793 /* Look for optional '( name )'. */
37794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37796 matching_parens parens
;
37797 parens
.consume_open (parser
); /* '(' */
37799 /* We parse the name as an id-expression. If it resolves to
37800 anything other than a non-overloaded function at namespace
37801 scope, it's an error. */
37802 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37803 tree name
= cp_parser_id_expression (parser
,
37804 /*template_keyword_p=*/false,
37805 /*check_dependency_p=*/false,
37806 /*template_p=*/NULL
,
37807 /*declarator_p=*/false,
37808 /*optional_p=*/false);
37809 tree decl
= cp_parser_lookup_name_simple (parser
, name
, name_loc
);
37810 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37811 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37813 if (decl
== error_mark_node
37814 || !parens
.require_close (parser
))
37816 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37817 parser
->oacc_routine
= NULL
;
37822 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37823 "#pragma acc routine",
37824 cp_lexer_peek_token (parser
->lexer
));
37826 if (decl
&& is_overloaded_fn (decl
)
37827 && (TREE_CODE (decl
) != FUNCTION_DECL
37828 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37830 error_at (name_loc
,
37831 "%<#pragma acc routine%> names a set of overloads");
37832 parser
->oacc_routine
= NULL
;
37836 /* Perhaps we should use the same rule as declarations in different
37838 if (!DECL_NAMESPACE_SCOPE_P (decl
))
37840 error_at (name_loc
,
37841 "%qD does not refer to a namespace scope function", decl
);
37842 parser
->oacc_routine
= NULL
;
37846 if (TREE_CODE (decl
) != FUNCTION_DECL
)
37848 error_at (name_loc
, "%qD does not refer to a function", decl
);
37849 parser
->oacc_routine
= NULL
;
37853 cp_finalize_oacc_routine (parser
, decl
, false);
37854 parser
->oacc_routine
= NULL
;
37856 else /* No optional '( name )'. */
37858 /* Store away all pragma tokens. */
37859 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37860 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37861 cp_lexer_consume_token (parser
->lexer
);
37862 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37863 parser
->oacc_routine
->error_seen
= true;
37864 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37865 struct cp_token_cache
*cp
37866 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37867 parser
->oacc_routine
->tokens
.safe_push (cp
);
37869 /* Emit a helpful diagnostic if there's another pragma following this
37871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37873 cp_ensure_no_oacc_routine (parser
);
37874 data
.tokens
.release ();
37875 /* ..., and then just keep going. */
37879 /* We only have to consider the pragma_external case here. */
37880 cp_parser_declaration (parser
);
37881 if (parser
->oacc_routine
37882 && !parser
->oacc_routine
->fndecl_seen
)
37883 cp_ensure_no_oacc_routine (parser
);
37885 parser
->oacc_routine
= NULL
;
37886 data
.tokens
.release ();
37890 /* Finalize #pragma acc routine clauses after direct declarator has
37894 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
37896 struct cp_token_cache
*ce
;
37897 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
37899 if (!data
->error_seen
&& data
->fndecl_seen
)
37901 error_at (data
->loc
,
37902 "%<#pragma acc routine%> not immediately followed by "
37903 "a single function declaration or definition");
37904 data
->error_seen
= true;
37906 if (data
->error_seen
)
37909 gcc_checking_assert (data
->tokens
.length () == 1);
37910 ce
= data
->tokens
[0];
37912 cp_parser_push_lexer_for_tokens (parser
, ce
);
37913 parser
->lexer
->in_pragma
= true;
37914 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37916 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37917 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
37918 parser
->oacc_routine
->clauses
37919 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37920 "#pragma acc routine", pragma_tok
);
37921 cp_parser_pop_lexer (parser
);
37922 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37928 /* Apply any saved OpenACC routine clauses to a just-parsed
37932 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
37934 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
37936 /* Keep going if we're in error reporting mode. */
37937 if (parser
->oacc_routine
->error_seen
37938 || fndecl
== error_mark_node
)
37941 if (parser
->oacc_routine
->fndecl_seen
)
37943 error_at (parser
->oacc_routine
->loc
,
37944 "%<#pragma acc routine%> not immediately followed by"
37945 " a single function declaration or definition");
37946 parser
->oacc_routine
= NULL
;
37949 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
37951 cp_ensure_no_oacc_routine (parser
);
37955 if (oacc_get_fn_attrib (fndecl
))
37957 error_at (parser
->oacc_routine
->loc
,
37958 "%<#pragma acc routine%> already applied to %qD", fndecl
);
37959 parser
->oacc_routine
= NULL
;
37963 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
37965 error_at (parser
->oacc_routine
->loc
,
37967 ? G_("%<#pragma acc routine%> must be applied before use")
37968 : G_("%<#pragma acc routine%> must be applied before "
37970 parser
->oacc_routine
= NULL
;
37974 /* Process the routine's dimension clauses. */
37975 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
37976 oacc_replace_fn_attrib (fndecl
, dims
);
37978 /* Add an "omp declare target" attribute. */
37979 DECL_ATTRIBUTES (fndecl
)
37980 = tree_cons (get_identifier ("omp declare target"),
37981 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
37983 /* Don't unset parser->oacc_routine here: we may still need it to
37984 diagnose wrong usage. But, remember that we've used this "#pragma acc
37986 parser
->oacc_routine
->fndecl_seen
= true;
37990 /* Main entry point to OpenMP statement pragmas. */
37993 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37996 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
37997 omp_clause_mask
mask (0);
37999 switch (cp_parser_pragma_kind (pragma_tok
))
38001 case PRAGMA_OACC_ATOMIC
:
38002 cp_parser_omp_atomic (parser
, pragma_tok
);
38004 case PRAGMA_OACC_CACHE
:
38005 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38007 case PRAGMA_OACC_DATA
:
38008 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38010 case PRAGMA_OACC_ENTER_DATA
:
38011 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38013 case PRAGMA_OACC_EXIT_DATA
:
38014 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38016 case PRAGMA_OACC_HOST_DATA
:
38017 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38019 case PRAGMA_OACC_KERNELS
:
38020 case PRAGMA_OACC_PARALLEL
:
38021 strcpy (p_name
, "#pragma acc");
38022 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38025 case PRAGMA_OACC_LOOP
:
38026 strcpy (p_name
, "#pragma acc");
38027 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38030 case PRAGMA_OACC_UPDATE
:
38031 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38033 case PRAGMA_OACC_WAIT
:
38034 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38036 case PRAGMA_OMP_ATOMIC
:
38037 cp_parser_omp_atomic (parser
, pragma_tok
);
38039 case PRAGMA_OMP_CRITICAL
:
38040 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38042 case PRAGMA_OMP_DISTRIBUTE
:
38043 strcpy (p_name
, "#pragma omp");
38044 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38047 case PRAGMA_OMP_FOR
:
38048 strcpy (p_name
, "#pragma omp");
38049 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38052 case PRAGMA_OMP_MASTER
:
38053 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38055 case PRAGMA_OMP_PARALLEL
:
38056 strcpy (p_name
, "#pragma omp");
38057 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38060 case PRAGMA_OMP_SECTIONS
:
38061 strcpy (p_name
, "#pragma omp");
38062 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38064 case PRAGMA_OMP_SIMD
:
38065 strcpy (p_name
, "#pragma omp");
38066 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38069 case PRAGMA_OMP_SINGLE
:
38070 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38072 case PRAGMA_OMP_TASK
:
38073 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38075 case PRAGMA_OMP_TASKGROUP
:
38076 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38078 case PRAGMA_OMP_TASKLOOP
:
38079 strcpy (p_name
, "#pragma omp");
38080 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38083 case PRAGMA_OMP_TEAMS
:
38084 strcpy (p_name
, "#pragma omp");
38085 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38089 gcc_unreachable ();
38092 protected_set_expr_location (stmt
, pragma_tok
->location
);
38095 /* Transactional Memory parsing routines. */
38097 /* Parse a transaction attribute.
38103 We use this instead of cp_parser_attributes_opt for transactions to avoid
38104 the pedwarn in C++98 mode. */
38107 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38110 tree attr_name
, attr
= NULL
;
38112 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38113 return cp_parser_attributes_opt (parser
);
38115 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38117 cp_lexer_consume_token (parser
->lexer
);
38118 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38121 token
= cp_lexer_peek_token (parser
->lexer
);
38122 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38124 token
= cp_lexer_consume_token (parser
->lexer
);
38126 attr_name
= (token
->type
== CPP_KEYWORD
38127 /* For keywords, use the canonical spelling,
38128 not the parsed identifier. */
38129 ? ridpointers
[(int) token
->keyword
]
38131 attr
= build_tree_list (attr_name
, NULL_TREE
);
38134 cp_parser_error (parser
, "expected identifier");
38136 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38138 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38142 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38144 transaction-statement:
38145 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38147 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38151 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38153 unsigned char old_in
= parser
->in_transaction
;
38154 unsigned char this_in
= 1, new_in
;
38155 enum rid keyword
= token
->keyword
;
38156 tree stmt
, attrs
, noex
;
38158 cp_lexer_consume_token (parser
->lexer
);
38160 if (keyword
== RID_TRANSACTION_RELAXED
38161 || keyword
== RID_SYNCHRONIZED
)
38162 this_in
|= TM_STMT_ATTR_RELAXED
;
38165 attrs
= cp_parser_txn_attribute_opt (parser
);
38167 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38170 /* Parse a noexcept specification. */
38171 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38172 noex
= boolean_true_node
;
38173 else if (keyword
== RID_ATOMIC_CANCEL
)
38175 /* cancel-and-throw is unimplemented. */
38176 sorry ("atomic_cancel");
38180 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38182 /* Keep track if we're in the lexical scope of an outer transaction. */
38183 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38185 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38187 parser
->in_transaction
= new_in
;
38188 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38189 parser
->in_transaction
= old_in
;
38191 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38196 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38198 transaction-expression:
38199 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38200 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38204 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38206 unsigned char old_in
= parser
->in_transaction
;
38207 unsigned char this_in
= 1;
38211 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38213 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38214 || keyword
== RID_TRANSACTION_RELAXED
);
38218 keyword
== RID_TRANSACTION_RELAXED
38219 ? G_("%<__transaction_relaxed%> without transactional memory "
38221 : G_("%<__transaction_atomic%> without transactional memory "
38222 "support enabled"));
38224 token
= cp_parser_require_keyword (parser
, keyword
,
38225 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38226 : RT_TRANSACTION_RELAXED
));
38227 gcc_assert (token
!= NULL
);
38229 if (keyword
== RID_TRANSACTION_RELAXED
)
38230 this_in
|= TM_STMT_ATTR_RELAXED
;
38232 /* Set this early. This might mean that we allow transaction_cancel in
38233 an expression that we find out later actually has to be a constexpr.
38234 However, we expect that cxx_constant_value will be able to deal with
38235 this; also, if the noexcept has no constexpr, then what we parse next
38236 really is a transaction's body. */
38237 parser
->in_transaction
= this_in
;
38239 /* Parse a noexcept specification. */
38240 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38243 if (!noex
|| !noex_expr
38244 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38246 matching_parens parens
;
38247 parens
.require_open (parser
);
38249 expr
= cp_parser_expression (parser
);
38250 expr
= finish_parenthesized_expr (expr
);
38252 parens
.require_close (parser
);
38256 /* The only expression that is available got parsed for the noexcept
38257 already. noexcept is true then. */
38259 noex
= boolean_true_node
;
38262 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38263 parser
->in_transaction
= old_in
;
38265 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38266 return error_mark_node
;
38268 return (flag_tm
? expr
: error_mark_node
);
38271 /* Parse a function-transaction-block.
38273 function-transaction-block:
38274 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38276 __transaction_atomic txn-attribute[opt] function-try-block
38277 __transaction_relaxed ctor-initializer[opt] function-body
38278 __transaction_relaxed function-try-block
38282 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38284 unsigned char old_in
= parser
->in_transaction
;
38285 unsigned char new_in
= 1;
38286 tree compound_stmt
, stmt
, attrs
;
38289 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38290 || keyword
== RID_TRANSACTION_RELAXED
);
38291 token
= cp_parser_require_keyword (parser
, keyword
,
38292 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38293 : RT_TRANSACTION_RELAXED
));
38294 gcc_assert (token
!= NULL
);
38296 if (keyword
== RID_TRANSACTION_RELAXED
)
38297 new_in
|= TM_STMT_ATTR_RELAXED
;
38300 attrs
= cp_parser_txn_attribute_opt (parser
);
38302 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38305 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38307 parser
->in_transaction
= new_in
;
38309 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38310 cp_parser_function_try_block (parser
);
38312 cp_parser_ctor_initializer_opt_and_function_body
38313 (parser
, /*in_function_try_block=*/false);
38315 parser
->in_transaction
= old_in
;
38317 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38320 /* Parse a __transaction_cancel statement.
38323 __transaction_cancel txn-attribute[opt] ;
38324 __transaction_cancel txn-attribute[opt] throw-expression ;
38326 ??? Cancel and throw is not yet implemented. */
38329 cp_parser_transaction_cancel (cp_parser
*parser
)
38332 bool is_outer
= false;
38335 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38336 RT_TRANSACTION_CANCEL
);
38337 gcc_assert (token
!= NULL
);
38339 attrs
= cp_parser_txn_attribute_opt (parser
);
38341 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38343 /* ??? Parse cancel-and-throw here. */
38345 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38349 error_at (token
->location
, "%<__transaction_cancel%> without "
38350 "transactional memory support enabled");
38351 return error_mark_node
;
38353 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38355 error_at (token
->location
, "%<__transaction_cancel%> within a "
38356 "%<__transaction_relaxed%>");
38357 return error_mark_node
;
38361 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38362 && !is_tm_may_cancel_outer (current_function_decl
))
38364 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38365 "within outer %<__transaction_atomic%>");
38366 error_at (token
->location
,
38367 " or a %<transaction_may_cancel_outer%> function");
38368 return error_mark_node
;
38371 else if (parser
->in_transaction
== 0)
38373 error_at (token
->location
, "%<__transaction_cancel%> not within "
38374 "%<__transaction_atomic%>");
38375 return error_mark_node
;
38378 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38386 static GTY (()) cp_parser
*the_parser
;
38389 /* Special handling for the first token or line in the file. The first
38390 thing in the file might be #pragma GCC pch_preprocess, which loads a
38391 PCH file, which is a GC collection point. So we need to handle this
38392 first pragma without benefit of an existing lexer structure.
38394 Always returns one token to the caller in *FIRST_TOKEN. This is
38395 either the true first token of the file, or the first token after
38396 the initial pragma. */
38399 cp_parser_initial_pragma (cp_token
*first_token
)
38403 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38404 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38407 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38408 if (first_token
->type
== CPP_STRING
)
38410 name
= first_token
->u
.value
;
38412 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38413 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38414 error_at (first_token
->location
,
38415 "junk at end of %<#pragma GCC pch_preprocess%>");
38418 error_at (first_token
->location
, "expected string literal");
38420 /* Skip to the end of the pragma. */
38421 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38422 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38424 /* Now actually load the PCH file. */
38426 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38428 /* Read one more token to return to our caller. We have to do this
38429 after reading the PCH file in, since its pointers have to be
38431 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38434 /* Normal parsing of a pragma token. Here we can (and must) use the
38438 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38440 cp_token
*pragma_tok
;
38445 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38446 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38447 parser
->lexer
->in_pragma
= true;
38449 id
= cp_parser_pragma_kind (pragma_tok
);
38450 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38451 cp_ensure_no_omp_declare_simd (parser
);
38454 case PRAGMA_GCC_PCH_PREPROCESS
:
38455 error_at (pragma_tok
->location
,
38456 "%<#pragma GCC pch_preprocess%> must be first");
38459 case PRAGMA_OMP_BARRIER
:
38462 case pragma_compound
:
38463 cp_parser_omp_barrier (parser
, pragma_tok
);
38466 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38467 "used in compound statements", "omp barrier");
38474 case PRAGMA_OMP_FLUSH
:
38477 case pragma_compound
:
38478 cp_parser_omp_flush (parser
, pragma_tok
);
38481 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38482 "used in compound statements", "omp flush");
38489 case PRAGMA_OMP_TASKWAIT
:
38492 case pragma_compound
:
38493 cp_parser_omp_taskwait (parser
, pragma_tok
);
38496 error_at (pragma_tok
->location
,
38497 "%<#pragma %s%> may only be used in compound statements",
38505 case PRAGMA_OMP_TASKYIELD
:
38508 case pragma_compound
:
38509 cp_parser_omp_taskyield (parser
, pragma_tok
);
38512 error_at (pragma_tok
->location
,
38513 "%<#pragma %s%> may only be used in compound statements",
38521 case PRAGMA_OMP_CANCEL
:
38524 case pragma_compound
:
38525 cp_parser_omp_cancel (parser
, pragma_tok
);
38528 error_at (pragma_tok
->location
,
38529 "%<#pragma %s%> may only be used in compound statements",
38537 case PRAGMA_OMP_CANCELLATION_POINT
:
38538 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38541 case PRAGMA_OMP_THREADPRIVATE
:
38542 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38545 case PRAGMA_OMP_DECLARE
:
38546 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38548 case PRAGMA_OACC_DECLARE
:
38549 cp_parser_oacc_declare (parser
, pragma_tok
);
38552 case PRAGMA_OACC_ENTER_DATA
:
38553 if (context
== pragma_stmt
)
38555 error_at (pragma_tok
->location
,
38556 "%<#pragma %s%> may only be used in compound statements",
38560 else if (context
!= pragma_compound
)
38562 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38565 case PRAGMA_OACC_EXIT_DATA
:
38566 if (context
== pragma_stmt
)
38568 error_at (pragma_tok
->location
,
38569 "%<#pragma %s%> may only be used in compound statements",
38573 else if (context
!= pragma_compound
)
38575 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38578 case PRAGMA_OACC_ROUTINE
:
38579 if (context
!= pragma_external
)
38581 error_at (pragma_tok
->location
,
38582 "%<#pragma acc routine%> must be at file scope");
38585 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38588 case PRAGMA_OACC_UPDATE
:
38589 if (context
== pragma_stmt
)
38591 error_at (pragma_tok
->location
,
38592 "%<#pragma %s%> may only be used in compound statements",
38596 else if (context
!= pragma_compound
)
38598 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38601 case PRAGMA_OACC_WAIT
:
38602 if (context
== pragma_stmt
)
38604 error_at (pragma_tok
->location
,
38605 "%<#pragma %s%> may only be used in compound statements",
38609 else if (context
!= pragma_compound
)
38611 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38614 case PRAGMA_OACC_ATOMIC
:
38615 case PRAGMA_OACC_CACHE
:
38616 case PRAGMA_OACC_DATA
:
38617 case PRAGMA_OACC_HOST_DATA
:
38618 case PRAGMA_OACC_KERNELS
:
38619 case PRAGMA_OACC_PARALLEL
:
38620 case PRAGMA_OACC_LOOP
:
38621 case PRAGMA_OMP_ATOMIC
:
38622 case PRAGMA_OMP_CRITICAL
:
38623 case PRAGMA_OMP_DISTRIBUTE
:
38624 case PRAGMA_OMP_FOR
:
38625 case PRAGMA_OMP_MASTER
:
38626 case PRAGMA_OMP_PARALLEL
:
38627 case PRAGMA_OMP_SECTIONS
:
38628 case PRAGMA_OMP_SIMD
:
38629 case PRAGMA_OMP_SINGLE
:
38630 case PRAGMA_OMP_TASK
:
38631 case PRAGMA_OMP_TASKGROUP
:
38632 case PRAGMA_OMP_TASKLOOP
:
38633 case PRAGMA_OMP_TEAMS
:
38634 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38636 stmt
= push_omp_privatization_clauses (false);
38637 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38638 pop_omp_privatization_clauses (stmt
);
38641 case PRAGMA_OMP_ORDERED
:
38642 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38644 stmt
= push_omp_privatization_clauses (false);
38645 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38646 pop_omp_privatization_clauses (stmt
);
38649 case PRAGMA_OMP_TARGET
:
38650 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38652 stmt
= push_omp_privatization_clauses (false);
38653 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38654 pop_omp_privatization_clauses (stmt
);
38657 case PRAGMA_OMP_END_DECLARE_TARGET
:
38658 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38661 case PRAGMA_OMP_SECTION
:
38662 error_at (pragma_tok
->location
,
38663 "%<#pragma omp section%> may only be used in "
38664 "%<#pragma omp sections%> construct");
38669 if (context
== pragma_external
)
38671 error_at (pragma_tok
->location
,
38672 "%<#pragma GCC ivdep%> must be inside a function");
38675 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38677 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38678 if (tok
->type
!= CPP_KEYWORD
38679 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
38680 && tok
->keyword
!= RID_DO
))
38682 cp_parser_error (parser
, "for, while or do statement expected");
38685 cp_parser_iteration_statement (parser
, if_p
, true);
38690 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38691 c_invoke_pragma_handler (id
);
38695 cp_parser_error (parser
, "expected declaration specifiers");
38699 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38703 /* The interface the pragma parsers have to the lexer. */
38706 pragma_lex (tree
*value
, location_t
*loc
)
38708 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38709 enum cpp_ttype ret
= tok
->type
;
38711 *value
= tok
->u
.value
;
38713 *loc
= tok
->location
;
38715 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38717 else if (ret
== CPP_STRING
)
38718 *value
= cp_parser_string_literal (the_parser
, false, false);
38721 if (ret
== CPP_KEYWORD
)
38723 cp_lexer_consume_token (the_parser
->lexer
);
38730 /* External interface. */
38732 /* Parse one entire translation unit. */
38735 c_parse_file (void)
38737 static bool already_called
= false;
38739 if (already_called
)
38740 fatal_error (input_location
,
38741 "inter-module optimizations not implemented for C++");
38742 already_called
= true;
38744 the_parser
= cp_parser_new ();
38745 push_deferring_access_checks (flag_access_control
38746 ? dk_no_deferred
: dk_no_check
);
38747 cp_parser_translation_unit (the_parser
);
38751 /* Create an identifier for a generic parameter type (a synthesized
38752 template parameter implied by `auto' or a concept identifier). */
38754 static GTY(()) int generic_parm_count
;
38756 make_generic_type_name ()
38759 sprintf (buf
, "auto:%d", ++generic_parm_count
);
38760 return get_identifier (buf
);
38763 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38764 (creating a new template parameter list if necessary). Returns the newly
38765 created template type parm. */
38768 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
38770 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
38772 /* Before committing to modifying any scope, if we're in an
38773 implicit template scope, and we're trying to synthesize a
38774 constrained parameter, try to find a previous parameter with
38775 the same name. This is the same-type rule for abbreviated
38776 function templates.
38778 NOTE: We can generate implicit parameters when tentatively
38779 parsing a nested name specifier, only to reject that parse
38780 later. However, matching the same template-id as part of a
38781 direct-declarator should generate an identical template
38782 parameter, so this rule will merge them. */
38783 if (parser
->implicit_template_scope
&& constr
)
38785 tree t
= parser
->implicit_template_parms
;
38788 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
38790 tree d
= TREE_VALUE (t
);
38791 if (TREE_CODE (d
) == PARM_DECL
)
38792 /* Return the TEMPLATE_PARM_INDEX. */
38793 d
= DECL_INITIAL (d
);
38796 t
= TREE_CHAIN (t
);
38800 /* We are either continuing a function template that already contains implicit
38801 template parameters, creating a new fully-implicit function template, or
38802 extending an existing explicit function template with implicit template
38805 cp_binding_level
*const entry_scope
= current_binding_level
;
38807 bool become_template
= false;
38808 cp_binding_level
*parent_scope
= 0;
38810 if (parser
->implicit_template_scope
)
38812 gcc_assert (parser
->implicit_template_parms
);
38814 current_binding_level
= parser
->implicit_template_scope
;
38818 /* Roll back to the existing template parameter scope (in the case of
38819 extending an explicit function template) or introduce a new template
38820 parameter scope ahead of the function parameter scope (or class scope
38821 in the case of out-of-line member definitions). The function scope is
38822 added back after template parameter synthesis below. */
38824 cp_binding_level
*scope
= entry_scope
;
38826 while (scope
->kind
== sk_function_parms
)
38828 parent_scope
= scope
;
38829 scope
= scope
->level_chain
;
38831 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
38833 /* If not defining a class, then any class scope is a scope level in
38834 an out-of-line member definition. In this case simply wind back
38835 beyond the first such scope to inject the template parameter list.
38836 Otherwise wind back to the class being defined. The latter can
38837 occur in class member friend declarations such as:
38843 friend void A::foo (auto);
38846 The template parameter list synthesized for the friend declaration
38847 must be injected in the scope of 'B'. This can also occur in
38848 erroneous cases such as:
38854 void B::foo (auto) {}
38857 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38858 but, nevertheless, the template parameter list synthesized for the
38859 declarator should be injected into the scope of 'A' as if the
38860 ill-formed template was specified explicitly. */
38862 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
38864 parent_scope
= scope
;
38865 scope
= scope
->level_chain
;
38869 current_binding_level
= scope
;
38871 if (scope
->kind
!= sk_template_parms
38872 || !function_being_declared_is_template_p (parser
))
38874 /* Introduce a new template parameter list for implicit template
38877 become_template
= true;
38879 parser
->implicit_template_scope
38880 = begin_scope (sk_template_parms
, NULL
);
38882 ++processing_template_decl
;
38884 parser
->fully_implicit_function_template_p
= true;
38885 ++parser
->num_template_parameter_lists
;
38889 /* Synthesize implicit template parameters at the end of the explicit
38890 template parameter list. */
38892 gcc_assert (current_template_parms
);
38894 parser
->implicit_template_scope
= scope
;
38896 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38897 parser
->implicit_template_parms
38898 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
38902 /* Synthesize a new template parameter and track the current template
38903 parameter chain with implicit_template_parms. */
38905 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
38906 tree synth_id
= make_generic_type_name ();
38907 tree synth_tmpl_parm
;
38908 bool non_type
= false;
38910 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
38912 = finish_template_type_parm (class_type_node
, synth_id
);
38913 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
38915 = finish_constrained_template_template_parm (proto
, synth_id
);
38918 synth_tmpl_parm
= copy_decl (proto
);
38919 DECL_NAME (synth_tmpl_parm
) = synth_id
;
38923 // Attach the constraint to the parm before processing.
38924 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
38925 TREE_TYPE (node
) = constr
;
38927 = process_template_parm (parser
->implicit_template_parms
,
38930 /*non_type=*/non_type
,
38931 /*param_pack=*/false);
38933 // Chain the new parameter to the list of implicit parameters.
38934 if (parser
->implicit_template_parms
)
38935 parser
->implicit_template_parms
38936 = TREE_CHAIN (parser
->implicit_template_parms
);
38938 parser
->implicit_template_parms
= new_parm
;
38940 tree new_decl
= get_local_decls ();
38942 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38943 new_decl
= DECL_INITIAL (new_decl
);
38945 /* If creating a fully implicit function template, start the new implicit
38946 template parameter list with this synthesized type, otherwise grow the
38947 current template parameter list. */
38949 if (become_template
)
38951 parent_scope
->level_chain
= current_binding_level
;
38953 tree new_parms
= make_tree_vec (1);
38954 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
38955 current_template_parms
= tree_cons (size_int (processing_template_decl
),
38956 new_parms
, current_template_parms
);
38960 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38961 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
38962 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
38963 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
38966 // If the new parameter was constrained, we need to add that to the
38967 // constraints in the template parameter list.
38968 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
38970 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
38971 reqs
= conjoin_constraints (reqs
, req
);
38972 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
38975 current_binding_level
= entry_scope
;
38980 /* Finish the declaration of a fully implicit function template. Such a
38981 template has no explicit template parameter list so has not been through the
38982 normal template head and tail processing. synthesize_implicit_template_parm
38983 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38984 provided if the declaration is a class member such that its template
38985 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38986 form is returned. Otherwise NULL_TREE is returned. */
38989 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
38991 gcc_assert (parser
->fully_implicit_function_template_p
);
38993 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
38994 && DECL_VIRTUAL_P (member_decl_opt
))
38996 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
38997 "implicit templates may not be %<virtual%>");
38998 DECL_VIRTUAL_P (member_decl_opt
) = false;
39001 if (member_decl_opt
)
39002 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39003 end_template_decl ();
39005 parser
->fully_implicit_function_template_p
= false;
39006 --parser
->num_template_parameter_lists
;
39008 return member_decl_opt
;
39011 /* Helper function for diagnostics that have complained about things
39012 being used with 'extern "C"' linkage.
39014 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39017 maybe_show_extern_c_location (void)
39019 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39020 inform (the_parser
->innermost_linkage_specification_location
,
39021 "%<extern \"C\"%> linkage started here");
39024 #include "gt-cp-parser.h"