2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
560 parser
->implicit_extern_c
);
561 cp_debug_print_flag (file
, "String expressions should be translated "
562 "to execution character set",
563 parser
->translate_strings_p
);
564 cp_debug_print_flag (file
, "Parsing function body outside of a "
565 "local class", parser
->in_function_body
);
566 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
567 parser
->colon_corrects_to_scope_p
);
568 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
569 parser
->colon_doesnt_start_class_def_p
);
570 if (parser
->type_definition_forbidden_message
)
571 fprintf (file
, "Error message for forbidden type definitions: %s\n",
572 parser
->type_definition_forbidden_message
);
573 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
574 fprintf (file
, "Number of class definitions in progress: %u\n",
575 parser
->num_classes_being_defined
);
576 fprintf (file
, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser
->num_template_parameter_lists
);
578 cp_debug_parser_tokens (file
, parser
, window_size
);
579 token
= parser
->lexer
->next_token
;
580 fprintf (file
, "Next token to parse:\n");
581 fprintf (file
, "\tToken: ");
582 cp_lexer_print_token (file
, token
);
583 eloc
= expand_location (token
->location
);
584 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
585 fprintf (file
, "\tLine: %d\n", eloc
.line
);
586 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
590 debug (cp_parser
&ref
)
592 cp_debug_parser (stderr
, &ref
);
596 debug (cp_parser
*ptr
)
601 fprintf (stderr
, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
616 /* Initially we are not debugging. */
617 lexer
->debugging_p
= false;
619 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
621 /* Create the buffer. */
622 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token
);
642 lexer
= cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer
->buffer
->quick_push (token
);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token
.type
!= CPP_EOF
)
650 cp_lexer_get_preprocessor_token (lexer
, &token
);
651 vec_safe_push (lexer
->buffer
, token
);
654 lexer
->last_token
= lexer
->buffer
->address ()
655 + lexer
->buffer
->length ()
657 lexer
->next_token
= lexer
->buffer
->length ()
658 ? lexer
->buffer
->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
681 lexer
->next_token
= first
== last
? &eof_token
: first
;
682 lexer
->last_token
= last
;
684 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
686 /* Initially we are not debugging. */
687 lexer
->debugging_p
= false;
689 gcc_assert (!lexer
->next_token
->purged_p
);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer
*lexer
)
698 vec_free (lexer
->buffer
);
699 lexer
->saved_tokens
.release ();
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
713 cp_lexer_debugging_p (cp_lexer
*lexer
)
715 if (!LEXER_DEBUGGING_ENABLED_P
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 /* Skip past purged tokens. */
759 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
763 return cp_lexer_token_at (lexer
, tp
);
766 /* nonzero if we are presently saving tokens. */
769 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
771 return lexer
->saved_tokens
.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
780 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
782 static int is_extern_c
= 0;
784 /* Get a new token from the preprocessor. */
786 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
787 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
788 token
->keyword
= RID_MAX
;
789 token
->purged_p
= false;
790 token
->error_reported
= false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c
+= pending_lang_change
;
796 pending_lang_change
= 0;
797 token
->implicit_extern_c
= is_extern_c
> 0;
799 /* Check to see if this token is a keyword. */
800 if (token
->type
== CPP_NAME
)
802 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
804 /* Mark this token as a keyword. */
805 token
->type
= CPP_KEYWORD
;
806 /* Record which keyword. */
807 token
->keyword
= C_RID_CODE (token
->u
.value
);
811 if (warn_cxx11_compat
812 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
813 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
815 /* Warn about the C++0x keyword (but still treat it as
817 warning (OPT_Wc__11_compat
,
818 "identifier %qE is a keyword in C++11",
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
826 token
->keyword
= RID_MAX
;
829 else if (token
->type
== CPP_AT_NAME
)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token
->type
= CPP_KEYWORD
;
833 switch (C_RID_CODE (token
->u
.value
))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
843 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
844 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
845 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
846 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
847 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
848 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
849 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
850 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
851 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if KEYWORD can start a decl-specifier. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE
:
979 if (keyword
>= RID_FIRST_INT_N
980 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
981 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
994 token
= cp_lexer_peek_token (lexer
);
995 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
998 /* Returns TRUE iff the token T begins a decltype type. */
1001 token_is_decltype (cp_token
*t
)
1003 return (t
->keyword
== RID_DECLTYPE
1004 || t
->type
== CPP_DECLTYPE
);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1010 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1012 cp_token
*t
= cp_lexer_peek_token (lexer
);
1013 return token_is_decltype (t
);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1020 saved_checks_value (struct tree_check
*check_value
)
1022 /* Perform any access checks that were deferred. */
1023 vec
<deferred_access_check
, va_gc
> *checks
;
1024 deferred_access_check
*chk
;
1025 checks
= check_value
->checks
;
1029 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1030 perform_or_defer_access_check (chk
->binfo
,
1032 chk
->diag_decl
, tf_warning_or_error
);
1034 /* Return the stored value. */
1035 return check_value
->value
;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1045 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer
))
1053 fprintf (cp_lexer_debug_stream
,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1057 token
= lexer
->next_token
;
1058 gcc_assert (!n
|| token
!= &eof_token
);
1062 if (token
== lexer
->last_token
)
1068 if (!token
->purged_p
)
1072 if (cp_lexer_debugging_p (lexer
))
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1085 cp_lexer_consume_token (cp_lexer
* lexer
)
1087 cp_token
*token
= lexer
->next_token
;
1089 gcc_assert (token
!= &eof_token
);
1090 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1094 lexer
->next_token
++;
1095 if (lexer
->next_token
== lexer
->last_token
)
1097 lexer
->next_token
= &eof_token
;
1102 while (lexer
->next_token
->purged_p
);
1104 cp_lexer_set_source_position_from_token (token
);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer
))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1110 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1111 putc ('\n', cp_lexer_debug_stream
);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1122 cp_lexer_purge_token (cp_lexer
*lexer
)
1124 cp_token
*tok
= lexer
->next_token
;
1126 gcc_assert (tok
!= &eof_token
);
1127 tok
->purged_p
= true;
1128 tok
->location
= UNKNOWN_LOCATION
;
1129 tok
->u
.value
= NULL_TREE
;
1130 tok
->keyword
= RID_MAX
;
1135 if (tok
== lexer
->last_token
)
1141 while (tok
->purged_p
);
1142 lexer
->next_token
= tok
;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1150 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1152 cp_token
*peek
= lexer
->next_token
;
1154 if (peek
== &eof_token
)
1155 peek
= lexer
->last_token
;
1157 gcc_assert (tok
< peek
);
1159 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1161 tok
->purged_p
= true;
1162 tok
->location
= UNKNOWN_LOCATION
;
1163 tok
->u
.value
= NULL_TREE
;
1164 tok
->keyword
= RID_MAX
;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1172 cp_lexer_save_tokens (cp_lexer
* lexer
)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer
))
1176 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1178 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1181 /* Commit to the portion of the token stream most recently saved. */
1184 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer
))
1188 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1190 lexer
->saved_tokens
.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1197 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer
))
1201 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1203 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1218 len
= lexer
->saved_tokens
.length ();
1219 cp_lexer_save_tokens (lexer
);
1223 cp_lexer_rollback_tokens (lexer
);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer
);
1230 gcc_assert (lexer
->saved_tokens
.length () == len
);
1234 /* Print a representation of the TOKEN on the STREAM. */
1237 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names
[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token
->type
)
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token
->u
.value
))
1264 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1271 case CPP_UTF8STRING
:
1272 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1276 print_generic_expr (stream
, token
->u
.value
);
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token
->type
< ARRAY_SIZE(token_names
))
1283 fputs (token_names
[token
->type
], stream
);
1285 fprintf (stream
, "[%d]", token
->type
);
1291 debug (cp_token
&ref
)
1293 cp_lexer_print_token (stderr
, &ref
);
1294 fprintf (stderr
, "\n");
1298 debug (cp_token
*ptr
)
1303 fprintf (stderr
, "<nil>\n");
1307 /* Start emitting debugging information. */
1310 cp_lexer_start_debugging (cp_lexer
* lexer
)
1312 if (!LEXER_DEBUGGING_ENABLED_P
)
1313 fatal_error (input_location
,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer
->debugging_p
= true;
1317 cp_lexer_debug_stream
= stderr
;
1320 /* Stop emitting debugging information. */
1323 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1325 if (!LEXER_DEBUGGING_ENABLED_P
)
1326 fatal_error (input_location
,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer
->debugging_p
= false;
1330 cp_lexer_debug_stream
= NULL
;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache
*
1336 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1338 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1339 cache
->first
= first
;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1348 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1350 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser
->omp_declare_simd
= NULL
;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1362 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1364 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1366 if (fndecl
== error_mark_node
)
1368 parser
->omp_declare_simd
= NULL
;
1371 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1373 cp_ensure_no_omp_declare_simd (parser
);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1383 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1385 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1387 error_at (parser
->oacc_routine
->loc
,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser
->oacc_routine
= NULL
;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1399 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1401 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator
*make_call_declarator
1412 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1413 static cp_declarator
*make_array_declarator
1414 (cp_declarator
*, tree
);
1415 static cp_declarator
*make_pointer_declarator
1416 (cp_cv_quals
, cp_declarator
*, tree
);
1417 static cp_declarator
*make_reference_declarator
1418 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1419 static cp_declarator
*make_ptrmem_declarator
1420 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1422 /* An erroneous declarator. */
1423 static cp_declarator
*cp_error_declarator
;
1425 /* The obstack on which declarators and related data structures are
1427 static struct obstack declarator_obstack
;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes
)
1434 return obstack_alloc (&declarator_obstack
, bytes
);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator
*
1441 make_declarator (cp_declarator_kind kind
)
1443 cp_declarator
*declarator
;
1445 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1446 declarator
->kind
= kind
;
1447 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1448 declarator
->attributes
= NULL_TREE
;
1449 declarator
->std_attributes
= NULL_TREE
;
1450 declarator
->declarator
= NULL
;
1451 declarator
->parameter_pack_p
= false;
1452 declarator
->id_loc
= UNKNOWN_LOCATION
;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1463 static cp_declarator
*
1464 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1465 special_function_kind sfk
)
1467 cp_declarator
*declarator
;
1469 /* It is valid to write:
1471 class C { void f(); };
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1479 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1480 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1482 gcc_assert (identifier_p (unqualified_name
)
1483 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1486 declarator
= make_declarator (cdk_id
);
1487 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1488 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1489 declarator
->u
.id
.sfk
= sfk
;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_pointer
);
1506 declarator
->declarator
= target
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1511 declarator
->id_loc
= target
->id_loc
;
1512 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1513 target
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1528 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1529 bool rvalue_ref
, tree attributes
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_reference
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1536 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1539 declarator
->id_loc
= target
->id_loc
;
1540 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1541 target
->parameter_pack_p
= false;
1544 declarator
->parameter_pack_p
= false;
1546 declarator
->std_attributes
= attributes
;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1557 cp_declarator
*pointee
,
1560 cp_declarator
*declarator
;
1562 declarator
= make_declarator (cdk_ptrmem
);
1563 declarator
->declarator
= pointee
;
1564 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1565 declarator
->u
.pointer
.class_type
= class_type
;
1569 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1570 pointee
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1575 declarator
->std_attributes
= attributes
;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1586 make_call_declarator (cp_declarator
*target
,
1588 cp_cv_quals cv_qualifiers
,
1589 cp_virt_specifiers virt_specifiers
,
1590 cp_ref_qualifier ref_qualifier
,
1592 tree exception_specification
,
1593 tree late_return_type
,
1594 tree requires_clause
)
1596 cp_declarator
*declarator
;
1598 declarator
= make_declarator (cdk_function
);
1599 declarator
->declarator
= target
;
1600 declarator
->u
.function
.parameters
= parms
;
1601 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1603 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1604 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1605 declarator
->u
.function
.exception_specification
= exception_specification
;
1606 declarator
->u
.function
.late_return_type
= late_return_type
;
1607 declarator
->u
.function
.requires_clause
= requires_clause
;
1610 declarator
->id_loc
= target
->id_loc
;
1611 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1612 target
->parameter_pack_p
= false;
1615 declarator
->parameter_pack_p
= false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1624 make_array_declarator (cp_declarator
*element
, tree bounds
)
1626 cp_declarator
*declarator
;
1628 declarator
= make_declarator (cdk_array
);
1629 declarator
->declarator
= element
;
1630 declarator
->u
.array
.bounds
= bounds
;
1633 declarator
->id_loc
= element
->id_loc
;
1634 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1635 element
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1648 if (declarator
&& declarator
->parameter_pack_p
)
1649 /* We already saw an ellipsis. */
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1657 while (declarator
&& !found
)
1659 switch ((int)declarator
->kind
)
1671 declarator
= declarator
->declarator
;
1679 cp_parameter_declarator
*no_parameters
;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator
*
1685 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1686 cp_declarator
*declarator
,
1687 tree default_argument
,
1689 bool template_parameter_pack_p
= false)
1691 cp_parameter_declarator
*parameter
;
1693 parameter
= ((cp_parameter_declarator
*)
1694 alloc_declarator (sizeof (cp_parameter_declarator
)));
1695 parameter
->next
= NULL
;
1696 if (decl_specifiers
)
1697 parameter
->decl_specifiers
= *decl_specifiers
;
1699 clear_decl_specs (¶meter
->decl_specifiers
);
1700 parameter
->declarator
= declarator
;
1701 parameter
->default_argument
= default_argument
;
1702 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1703 parameter
->loc
= loc
;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator
*declarator
)
1715 if (declarator
->kind
== cdk_function
1716 && declarator
->declarator
->kind
== cdk_id
)
1718 if (declarator
->kind
== cdk_id
1719 || declarator
->kind
== cdk_decomp
1720 || declarator
->kind
== cdk_error
)
1722 declarator
= declarator
->declarator
;
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1788 CP_PARSER_FLAGS_NONE
= 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser
*, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser
*, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser
*, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser
*, tree
, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2049 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2050 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser
*, tree
, tree
*, tree
*);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2060 static tree cp_parser_new_expression
2062 static vec
<tree
, va_gc
> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser
*, tree
*);
2066 static cp_declarator
*cp_parser_new_declarator_opt
2068 static cp_declarator
*cp_parser_direct_new_declarator
2070 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser
*, cp_expr
);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2084 static cp_expr cp_parser_expression
2085 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2090 static cp_expr cp_parser_lambda_expression
2092 static void cp_parser_lambda_introducer
2093 (cp_parser
*, tree
);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser
*, tree
);
2096 static void cp_parser_lambda_body
2097 (cp_parser
*, tree
);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_expression_statement
2106 (cp_parser
*, tree
);
2107 static tree cp_parser_compound_statement
2108 (cp_parser
*, tree
, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_selection_statement
2112 (cp_parser
*, bool *, vec
<tree
> *);
2113 static tree cp_parser_condition
2115 static tree cp_parser_iteration_statement
2116 (cp_parser
*, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser
*, tree
*decl
);
2119 static tree cp_parser_for
2120 (cp_parser
*, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser
*, tree
, tree
, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree
, tree
*, tree
*);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser
*, bool *, const token_indent_info
&);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2145 static void cp_parser_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_type_name
2166 static tree cp_parser_nonclass_name
2167 (cp_parser
* parser
);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser
*, bool, bool);
2170 static tree cp_parser_enum_specifier
2172 static void cp_parser_enumerator_list
2173 (cp_parser
*, tree
);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser
*, tree
);
2176 static tree cp_parser_namespace_name
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser
*, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser
*, bool);
2198 static tree cp_parser_decltype
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2207 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2224 static tree cp_parser_declarator_id
2225 (cp_parser
*, bool);
2226 static tree cp_parser_type_id
2228 static tree cp_parser_template_type_arg
2230 static tree
cp_parser_trailing_type_id (cp_parser
*);
2231 static tree cp_parser_type_id_1
2232 (cp_parser
*, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2235 static tree cp_parser_parameter_declaration_clause
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser
*, bool *);
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2274 static tree cp_parser_class_head
2275 (cp_parser
*, bool *);
2276 static enum tag_types cp_parser_class_key
2278 static void cp_parser_type_parameter_key
2279 (cp_parser
* parser
);
2280 static void cp_parser_member_specification_opt
2282 static void cp_parser_member_declaration
2284 static tree cp_parser_pure_specifier
2286 static tree cp_parser_constant_initializer
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2293 static tree cp_parser_base_specifier
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2300 static tree cp_parser_conversion_type_id
2302 static cp_declarator
*cp_parser_conversion_declarator_opt
2304 static void cp_parser_ctor_initializer_opt
2306 static void cp_parser_mem_initializer_list
2308 static tree cp_parser_mem_initializer
2310 static tree cp_parser_mem_initializer_id
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2317 static cp_expr cp_parser_operator
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser
*, bool);
2324 static tree cp_parser_template_parameter_list
2326 static tree cp_parser_template_parameter
2327 (cp_parser
*, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser
*, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser
*, bool, bool, enum tag_types
, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2334 static tree cp_parser_template_argument_list
2336 static tree cp_parser_template_argument
2338 static void cp_parser_explicit_instantiation
2340 static void cp_parser_explicit_specialization
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2347 static void cp_parser_function_try_block
2349 static void cp_parser_handler_seq
2351 static void cp_parser_handler
2353 static tree cp_parser_exception_declaration
2355 static tree cp_parser_throw_expression
2357 static tree cp_parser_exception_specification_opt
2359 static tree cp_parser_type_id_list
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2366 static tree cp_parser_asm_operand_list
2368 static tree cp_parser_asm_clobber_list
2370 static tree cp_parser_asm_label_list
2372 static bool cp_next_tokens_can_be_attribute_p
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2376 static bool cp_next_tokens_can_be_std_attribute_p
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser
*, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser
*, size_t);
2384 static tree cp_parser_attributes_opt
2386 static tree cp_parser_gnu_attributes_opt
2388 static tree cp_parser_gnu_attribute_list
2390 static tree cp_parser_std_attribute
2391 (cp_parser
*, tree
);
2392 static tree cp_parser_std_attribute_spec
2394 static tree cp_parser_std_attribute_spec_seq
2396 static bool cp_parser_extension_opt
2397 (cp_parser
*, int *);
2398 static void cp_parser_label_declaration
2401 /* Concept Extensions */
2403 static tree cp_parser_requires_clause
2405 static tree cp_parser_requires_clause_opt
2407 static tree cp_parser_requires_expression
2409 static tree cp_parser_requirement_parameter_list
2411 static tree cp_parser_requirement_body
2413 static tree cp_parser_requirement_list
2415 static tree cp_parser_requirement
2417 static tree cp_parser_simple_requirement
2419 static tree cp_parser_compound_requirement
2421 static tree cp_parser_type_requirement
2423 static tree cp_parser_nested_requirement
2426 /* Transactional Memory Extensions */
2428 static tree cp_parser_transaction
2429 (cp_parser
*, cp_token
*);
2430 static tree cp_parser_transaction_expression
2431 (cp_parser
*, enum rid
);
2432 static void cp_parser_function_transaction
2433 (cp_parser
*, enum rid
);
2434 static tree cp_parser_transaction_cancel
2437 enum pragma_context
{
2444 static bool cp_parser_pragma
2445 (cp_parser
*, enum pragma_context
, bool *);
2447 /* Objective-C++ Productions */
2449 static tree cp_parser_objc_message_receiver
2451 static tree cp_parser_objc_message_args
2453 static tree cp_parser_objc_message_expression
2455 static cp_expr cp_parser_objc_encode_expression
2457 static tree cp_parser_objc_defs_expression
2459 static tree cp_parser_objc_protocol_expression
2461 static tree cp_parser_objc_selector_expression
2463 static cp_expr cp_parser_objc_expression
2465 static bool cp_parser_objc_selector_p
2467 static tree cp_parser_objc_selector
2469 static tree cp_parser_objc_protocol_refs_opt
2471 static void cp_parser_objc_declaration
2472 (cp_parser
*, tree
);
2473 static tree cp_parser_objc_statement
2475 static bool cp_parser_objc_valid_prefix_attributes
2476 (cp_parser
*, tree
*);
2477 static void cp_parser_objc_at_property_declaration
2479 static void cp_parser_objc_at_synthesize_declaration
2481 static void cp_parser_objc_at_dynamic_declaration
2483 static tree cp_parser_objc_struct_declaration
2486 /* Utility Routines */
2488 static cp_expr cp_parser_lookup_name
2489 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2490 static tree cp_parser_lookup_name_simple
2491 (cp_parser
*, tree
, location_t
);
2492 static tree cp_parser_maybe_treat_template_as_class
2494 static bool cp_parser_check_declarator_template_parameters
2495 (cp_parser
*, cp_declarator
*, location_t
);
2496 static bool cp_parser_check_template_parameters
2497 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2498 static cp_expr cp_parser_simple_cast_expression
2500 static tree cp_parser_global_scope_opt
2501 (cp_parser
*, bool);
2502 static bool cp_parser_constructor_declarator_p
2503 (cp_parser
*, bool);
2504 static tree cp_parser_function_definition_from_specifiers_and_declarator
2505 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2506 static tree cp_parser_function_definition_after_declarator
2507 (cp_parser
*, bool);
2508 static bool cp_parser_template_declaration_after_export
2509 (cp_parser
*, bool);
2510 static void cp_parser_perform_template_parameter_access_checks
2511 (vec
<deferred_access_check
, va_gc
> *);
2512 static tree cp_parser_single_declaration
2513 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2514 static cp_expr cp_parser_functional_cast
2515 (cp_parser
*, tree
);
2516 static tree cp_parser_save_member_function_body
2517 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2518 static tree cp_parser_save_nsdmi
2520 static tree cp_parser_enclosed_template_argument_list
2522 static void cp_parser_save_default_args
2523 (cp_parser
*, tree
);
2524 static void cp_parser_late_parsing_for_member
2525 (cp_parser
*, tree
);
2526 static tree cp_parser_late_parse_one_default_arg
2527 (cp_parser
*, tree
, tree
, tree
);
2528 static void cp_parser_late_parsing_nsdmi
2529 (cp_parser
*, tree
);
2530 static void cp_parser_late_parsing_default_args
2531 (cp_parser
*, tree
);
2532 static tree cp_parser_sizeof_operand
2533 (cp_parser
*, enum rid
);
2534 static cp_expr cp_parser_trait_expr
2535 (cp_parser
*, enum rid
);
2536 static bool cp_parser_declares_only_class_p
2538 static void cp_parser_set_storage_class
2539 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2540 static void cp_parser_set_decl_spec_type
2541 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2542 static void set_and_check_decl_spec_loc
2543 (cp_decl_specifier_seq
*decl_specs
,
2544 cp_decl_spec ds
, cp_token
*);
2545 static bool cp_parser_friend_p
2546 (const cp_decl_specifier_seq
*);
2547 static void cp_parser_required_error
2548 (cp_parser
*, required_token
, bool, location_t
);
2549 static cp_token
*cp_parser_require
2550 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2551 static cp_token
*cp_parser_require_keyword
2552 (cp_parser
*, enum rid
, required_token
);
2553 static bool cp_parser_token_starts_function_definition_p
2555 static bool cp_parser_next_token_starts_class_definition_p
2557 static bool cp_parser_next_token_ends_template_argument_p
2559 static bool cp_parser_nth_token_starts_template_argument_list_p
2560 (cp_parser
*, size_t);
2561 static enum tag_types cp_parser_token_is_class_key
2563 static enum tag_types cp_parser_token_is_type_parameter_key
2565 static void cp_parser_check_class_key
2566 (enum tag_types
, tree type
);
2567 static void cp_parser_check_access_in_redeclaration
2568 (tree type
, location_t location
);
2569 static bool cp_parser_optional_template_keyword
2571 static void cp_parser_pre_parsed_nested_name_specifier
2573 static bool cp_parser_cache_group
2574 (cp_parser
*, enum cpp_ttype
, unsigned);
2575 static tree cp_parser_cache_defarg
2576 (cp_parser
*parser
, bool nsdmi
);
2577 static void cp_parser_parse_tentatively
2579 static void cp_parser_commit_to_tentative_parse
2581 static void cp_parser_commit_to_topmost_tentative_parse
2583 static void cp_parser_abort_tentative_parse
2585 static bool cp_parser_parse_definitely
2587 static inline bool cp_parser_parsing_tentatively
2589 static bool cp_parser_uncommitted_to_tentative_parse_p
2591 static void cp_parser_error
2592 (cp_parser
*, const char *);
2593 static void cp_parser_name_lookup_error
2594 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2595 static bool cp_parser_simulate_error
2597 static bool cp_parser_check_type_definition
2599 static void cp_parser_check_for_definition_in_return_type
2600 (cp_declarator
*, tree
, location_t type_location
);
2601 static void cp_parser_check_for_invalid_template_id
2602 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2603 static bool cp_parser_non_integral_constant_expression
2604 (cp_parser
*, non_integral_constant
);
2605 static void cp_parser_diagnose_invalid_type_name
2606 (cp_parser
*, tree
, location_t
);
2607 static bool cp_parser_parse_and_diagnose_invalid_type_name
2609 static int cp_parser_skip_to_closing_parenthesis
2610 (cp_parser
*, bool, bool, bool);
2611 static void cp_parser_skip_to_end_of_statement
2613 static void cp_parser_consume_semicolon_at_end_of_statement
2615 static void cp_parser_skip_to_end_of_block_or_statement
2617 static bool cp_parser_skip_to_closing_brace
2619 static void cp_parser_skip_to_end_of_template_parameter_list
2621 static void cp_parser_skip_to_pragma_eol
2622 (cp_parser
*, cp_token
*);
2623 static bool cp_parser_error_occurred
2625 static bool cp_parser_allow_gnu_extensions_p
2627 static bool cp_parser_is_pure_string_literal
2629 static bool cp_parser_is_string_literal
2631 static bool cp_parser_is_keyword
2632 (cp_token
*, enum rid
);
2633 static tree cp_parser_make_typename_type
2634 (cp_parser
*, tree
, location_t location
);
2635 static cp_declarator
* cp_parser_make_indirect_declarator
2636 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2637 static bool cp_parser_compound_literal_p
2639 static bool cp_parser_array_designator_p
2641 static bool cp_parser_init_statement_p
2643 static bool cp_parser_skip_to_closing_square_bracket
2646 /* Concept-related syntactic transformations */
2648 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2649 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2651 // -------------------------------------------------------------------------- //
2652 // Unevaluated Operand Guard
2654 // Implementation of an RAII helper for unevaluated operand parsing.
2655 cp_unevaluated::cp_unevaluated ()
2657 ++cp_unevaluated_operand
;
2658 ++c_inhibit_evaluation_warnings
;
2661 cp_unevaluated::~cp_unevaluated ()
2663 --c_inhibit_evaluation_warnings
;
2664 --cp_unevaluated_operand
;
2667 // -------------------------------------------------------------------------- //
2668 // Tentative Parsing
2670 /* Returns nonzero if we are parsing tentatively. */
2673 cp_parser_parsing_tentatively (cp_parser
* parser
)
2675 return parser
->context
->next
!= NULL
;
2678 /* Returns nonzero if TOKEN is a string literal. */
2681 cp_parser_is_pure_string_literal (cp_token
* token
)
2683 return (token
->type
== CPP_STRING
||
2684 token
->type
== CPP_STRING16
||
2685 token
->type
== CPP_STRING32
||
2686 token
->type
== CPP_WSTRING
||
2687 token
->type
== CPP_UTF8STRING
);
2690 /* Returns nonzero if TOKEN is a string literal
2691 of a user-defined string literal. */
2694 cp_parser_is_string_literal (cp_token
* token
)
2696 return (cp_parser_is_pure_string_literal (token
) ||
2697 token
->type
== CPP_STRING_USERDEF
||
2698 token
->type
== CPP_STRING16_USERDEF
||
2699 token
->type
== CPP_STRING32_USERDEF
||
2700 token
->type
== CPP_WSTRING_USERDEF
||
2701 token
->type
== CPP_UTF8STRING_USERDEF
);
2704 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2707 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2709 return token
->keyword
== keyword
;
2712 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2715 static enum pragma_kind
2716 cp_parser_pragma_kind (cp_token
*token
)
2718 if (token
->type
!= CPP_PRAGMA
)
2720 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2721 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2724 /* Helper function for cp_parser_error.
2725 Having peeked a token of kind TOK1_KIND that might signify
2726 a conflict marker, peek successor tokens to determine
2727 if we actually do have a conflict marker.
2728 Specifically, we consider a run of 7 '<', '=' or '>' characters
2729 at the start of a line as a conflict marker.
2730 These come through the lexer as three pairs and a single,
2731 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2732 If it returns true, *OUT_LOC is written to with the location/range
2736 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2737 location_t
*out_loc
)
2739 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2740 if (token2
->type
!= tok1_kind
)
2742 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2743 if (token3
->type
!= tok1_kind
)
2745 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2746 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2749 /* It must be at the start of the line. */
2750 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2751 if (LOCATION_COLUMN (start_loc
) != 1)
2754 /* We have a conflict marker. Construct a location of the form:
2757 with start == caret, finishing at the end of the marker. */
2758 location_t finish_loc
= get_finish (token4
->location
);
2759 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2764 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2768 get_matching_symbol (required_token token_desc
)
2775 case RT_CLOSE_BRACE
:
2777 case RT_CLOSE_PAREN
:
2782 /* Attempt to convert TOKEN_DESC from a required_token to an
2783 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2785 static enum cpp_ttype
2786 get_required_cpp_ttype (required_token token_desc
)
2791 return CPP_SEMICOLON
;
2793 return CPP_OPEN_PAREN
;
2794 case RT_CLOSE_BRACE
:
2795 return CPP_CLOSE_BRACE
;
2797 return CPP_OPEN_BRACE
;
2798 case RT_CLOSE_SQUARE
:
2799 return CPP_CLOSE_SQUARE
;
2800 case RT_OPEN_SQUARE
:
2801 return CPP_OPEN_SQUARE
;
2806 case RT_CLOSE_PAREN
:
2807 return CPP_CLOSE_PAREN
;
2810 /* Use CPP_EOF as a "no completions possible" code. */
2816 /* Subroutine of cp_parser_error and cp_parser_required_error.
2818 Issue a diagnostic of the form
2819 FILE:LINE: MESSAGE before TOKEN
2820 where TOKEN is the next token in the input stream. MESSAGE
2821 (specified by the caller) is usually of the form "expected
2824 This bypasses the check for tentative passing, and potentially
2825 adds material needed by cp_parser_required_error.
2827 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2828 suggesting insertion of the missing token.
2830 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2831 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2835 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2836 required_token missing_token_desc
,
2837 location_t matching_location
)
2839 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2840 /* This diagnostic makes more sense if it is tagged to the line
2841 of the token we just peeked at. */
2842 cp_lexer_set_source_position_from_token (token
);
2844 if (token
->type
== CPP_PRAGMA
)
2846 error_at (token
->location
,
2847 "%<#pragma%> is not allowed here");
2848 cp_parser_skip_to_pragma_eol (parser
, token
);
2852 /* If this is actually a conflict marker, report it as such. */
2853 if (token
->type
== CPP_LSHIFT
2854 || token
->type
== CPP_RSHIFT
2855 || token
->type
== CPP_EQ_EQ
)
2858 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2860 error_at (loc
, "version control conflict marker in file");
2865 gcc_rich_location
richloc (input_location
);
2867 bool added_matching_location
= false;
2869 if (missing_token_desc
!= RT_NONE
)
2871 /* Potentially supply a fix-it hint, suggesting to add the
2872 missing token immediately after the *previous* token.
2873 This may move the primary location within richloc. */
2874 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2875 location_t prev_token_loc
2876 = cp_lexer_previous_token (parser
->lexer
)->location
;
2877 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2879 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2880 Attempt to consolidate diagnostics by printing it as a
2881 secondary range within the main diagnostic. */
2882 if (matching_location
!= UNKNOWN_LOCATION
)
2883 added_matching_location
2884 = richloc
.add_location_if_nearby (matching_location
);
2887 /* Actually emit the error. */
2888 c_parse_error (gmsgid
,
2889 /* Because c_parser_error does not understand
2890 CPP_KEYWORD, keywords are treated like
2892 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2893 token
->u
.value
, token
->flags
, &richloc
);
2895 if (missing_token_desc
!= RT_NONE
)
2897 /* If we weren't able to consolidate matching_location, then
2898 print it as a secondary diagnostic. */
2899 if (matching_location
!= UNKNOWN_LOCATION
2900 && !added_matching_location
)
2901 inform (matching_location
, "to match this %qs",
2902 get_matching_symbol (missing_token_desc
));
2906 /* If not parsing tentatively, issue a diagnostic of the form
2907 FILE:LINE: MESSAGE before TOKEN
2908 where TOKEN is the next token in the input stream. MESSAGE
2909 (specified by the caller) is usually of the form "expected
2913 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2915 if (!cp_parser_simulate_error (parser
))
2916 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2919 /* Issue an error about name-lookup failing. NAME is the
2920 IDENTIFIER_NODE DECL is the result of
2921 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2922 the thing that we hoped to find. */
2925 cp_parser_name_lookup_error (cp_parser
* parser
,
2928 name_lookup_error desired
,
2929 location_t location
)
2931 /* If name lookup completely failed, tell the user that NAME was not
2933 if (decl
== error_mark_node
)
2935 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2936 error_at (location
, "%<%E::%E%> has not been declared",
2937 parser
->scope
, name
);
2938 else if (parser
->scope
== global_namespace
)
2939 error_at (location
, "%<::%E%> has not been declared", name
);
2940 else if (parser
->object_scope
2941 && !CLASS_TYPE_P (parser
->object_scope
))
2942 error_at (location
, "request for member %qE in non-class type %qT",
2943 name
, parser
->object_scope
);
2944 else if (parser
->object_scope
)
2945 error_at (location
, "%<%T::%E%> has not been declared",
2946 parser
->object_scope
, name
);
2948 error_at (location
, "%qE has not been declared", name
);
2950 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2955 error_at (location
, "%<%E::%E%> is not a type",
2956 parser
->scope
, name
);
2959 error_at (location
, "%<%E::%E%> is not a class or namespace",
2960 parser
->scope
, name
);
2964 "%<%E::%E%> is not a class, namespace, or enumeration",
2965 parser
->scope
, name
);
2972 else if (parser
->scope
== global_namespace
)
2977 error_at (location
, "%<::%E%> is not a type", name
);
2980 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2984 "%<::%E%> is not a class, namespace, or enumeration",
2996 error_at (location
, "%qE is not a type", name
);
2999 error_at (location
, "%qE is not a class or namespace", name
);
3003 "%qE is not a class, namespace, or enumeration", name
);
3011 /* If we are parsing tentatively, remember that an error has occurred
3012 during this tentative parse. Returns true if the error was
3013 simulated; false if a message should be issued by the caller. */
3016 cp_parser_simulate_error (cp_parser
* parser
)
3018 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3020 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3026 /* This function is called when a type is defined. If type
3027 definitions are forbidden at this point, an error message is
3031 cp_parser_check_type_definition (cp_parser
* parser
)
3033 /* If types are forbidden here, issue a message. */
3034 if (parser
->type_definition_forbidden_message
)
3036 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3037 in the message need to be interpreted. */
3038 error (parser
->type_definition_forbidden_message
);
3044 /* This function is called when the DECLARATOR is processed. The TYPE
3045 was a type defined in the decl-specifiers. If it is invalid to
3046 define a type in the decl-specifiers for DECLARATOR, an error is
3047 issued. TYPE_LOCATION is the location of TYPE and is used
3048 for error reporting. */
3051 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3052 tree type
, location_t type_location
)
3054 /* [dcl.fct] forbids type definitions in return types.
3055 Unfortunately, it's not easy to know whether or not we are
3056 processing a return type until after the fact. */
3058 && (declarator
->kind
== cdk_pointer
3059 || declarator
->kind
== cdk_reference
3060 || declarator
->kind
== cdk_ptrmem
))
3061 declarator
= declarator
->declarator
;
3063 && declarator
->kind
== cdk_function
)
3065 error_at (type_location
,
3066 "new types may not be defined in a return type");
3067 inform (type_location
,
3068 "(perhaps a semicolon is missing after the definition of %qT)",
3073 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3074 "<" in any valid C++ program. If the next token is indeed "<",
3075 issue a message warning the user about what appears to be an
3076 invalid attempt to form a template-id. LOCATION is the location
3077 of the type-specifier (TYPE) */
3080 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3082 enum tag_types tag_type
,
3083 location_t location
)
3085 cp_token_position start
= 0;
3087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3089 if (TREE_CODE (type
) == TYPE_DECL
)
3090 type
= TREE_TYPE (type
);
3091 if (TYPE_P (type
) && !template_placeholder_p (type
))
3092 error_at (location
, "%qT is not a template", type
);
3093 else if (identifier_p (type
))
3095 if (tag_type
!= none_type
)
3096 error_at (location
, "%qE is not a class template", type
);
3098 error_at (location
, "%qE is not a template", type
);
3101 error_at (location
, "invalid template-id");
3102 /* Remember the location of the invalid "<". */
3103 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3104 start
= cp_lexer_token_position (parser
->lexer
, true);
3105 /* Consume the "<". */
3106 cp_lexer_consume_token (parser
->lexer
);
3107 /* Parse the template arguments. */
3108 cp_parser_enclosed_template_argument_list (parser
);
3109 /* Permanently remove the invalid template arguments so that
3110 this error message is not issued again. */
3112 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3116 /* If parsing an integral constant-expression, issue an error message
3117 about the fact that THING appeared and return true. Otherwise,
3118 return false. In either case, set
3119 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3122 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3123 non_integral_constant thing
)
3125 parser
->non_integral_constant_expression_p
= true;
3126 if (parser
->integral_constant_expression_p
)
3128 if (!parser
->allow_non_integral_constant_expression_p
)
3130 const char *msg
= NULL
;
3134 pedwarn (input_location
, OPT_Wpedantic
,
3135 "ISO C++ forbids using a floating-point literal "
3136 "in a constant-expression");
3139 error ("a cast to a type other than an integral or "
3140 "enumeration type cannot appear in a "
3141 "constant-expression");
3144 error ("%<typeid%> operator "
3145 "cannot appear in a constant-expression");
3148 error ("non-constant compound literals "
3149 "cannot appear in a constant-expression");
3152 error ("a function call "
3153 "cannot appear in a constant-expression");
3156 error ("an increment "
3157 "cannot appear in a constant-expression");
3160 error ("an decrement "
3161 "cannot appear in a constant-expression");
3164 error ("an array reference "
3165 "cannot appear in a constant-expression");
3167 case NIC_ADDR_LABEL
:
3168 error ("the address of a label "
3169 "cannot appear in a constant-expression");
3171 case NIC_OVERLOADED
:
3172 error ("calls to overloaded operators "
3173 "cannot appear in a constant-expression");
3175 case NIC_ASSIGNMENT
:
3176 error ("an assignment cannot appear in a constant-expression");
3179 error ("a comma operator "
3180 "cannot appear in a constant-expression");
3182 case NIC_CONSTRUCTOR
:
3183 error ("a call to a constructor "
3184 "cannot appear in a constant-expression");
3186 case NIC_TRANSACTION
:
3187 error ("a transaction expression "
3188 "cannot appear in a constant-expression");
3194 msg
= "__FUNCTION__";
3196 case NIC_PRETTY_FUNC
:
3197 msg
= "__PRETTY_FUNCTION__";
3217 case NIC_PREINCREMENT
:
3220 case NIC_PREDECREMENT
:
3233 error ("%qs cannot appear in a constant-expression", msg
);
3240 /* Emit a diagnostic for an invalid type name. This function commits
3241 to the current active tentative parse, if any. (Otherwise, the
3242 problematic construct might be encountered again later, resulting
3243 in duplicate error messages.) LOCATION is the location of ID. */
3246 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3247 location_t location
)
3249 tree decl
, ambiguous_decls
;
3250 cp_parser_commit_to_tentative_parse (parser
);
3251 /* Try to lookup the identifier. */
3252 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3253 /*is_template=*/false,
3254 /*is_namespace=*/false,
3255 /*check_dependency=*/true,
3256 &ambiguous_decls
, location
);
3257 if (ambiguous_decls
)
3258 /* If the lookup was ambiguous, an error will already have
3261 /* If the lookup found a template-name, it means that the user forgot
3262 to specify an argument list. Emit a useful error message. */
3263 if (DECL_TYPE_TEMPLATE_P (decl
))
3266 "invalid use of template-name %qE without an argument list",
3268 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3269 inform (location
, "class template argument deduction is only available "
3270 "with -std=c++17 or -std=gnu++17");
3271 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3273 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3274 error_at (location
, "invalid use of destructor %qD as a type", id
);
3275 else if (TREE_CODE (decl
) == TYPE_DECL
)
3276 /* Something like 'unsigned A a;' */
3277 error_at (location
, "invalid combination of multiple type-specifiers");
3278 else if (!parser
->scope
)
3280 /* Issue an error message. */
3282 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3283 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3286 gcc_rich_location
richloc (location
);
3287 richloc
.add_fixit_replace (hint
.suggestion ());
3289 "%qE does not name a type; did you mean %qs?",
3290 id
, hint
.suggestion ());
3293 error_at (location
, "%qE does not name a type", id
);
3294 /* If we're in a template class, it's possible that the user was
3295 referring to a type from a base class. For example:
3297 template <typename T> struct A { typedef T X; };
3298 template <typename T> struct B : public A<T> { X x; };
3300 The user should have said "typename A<T>::X". */
3301 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3302 inform (location
, "C++11 %<constexpr%> only available with "
3303 "-std=c++11 or -std=gnu++11");
3304 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3305 inform (location
, "C++11 %<noexcept%> only available with "
3306 "-std=c++11 or -std=gnu++11");
3307 else if (cxx_dialect
< cxx11
3308 && TREE_CODE (id
) == IDENTIFIER_NODE
3309 && id_equal (id
, "thread_local"))
3310 inform (location
, "C++11 %<thread_local%> only available with "
3311 "-std=c++11 or -std=gnu++11");
3312 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3313 inform (location
, "%<concept%> only available with -fconcepts");
3314 else if (processing_template_decl
&& current_class_type
3315 && TYPE_BINFO (current_class_type
))
3319 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3323 tree base_type
= BINFO_TYPE (b
);
3324 if (CLASS_TYPE_P (base_type
)
3325 && dependent_type_p (base_type
))
3328 /* Go from a particular instantiation of the
3329 template (which will have an empty TYPE_FIELDs),
3330 to the main version. */
3331 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3332 for (field
= TYPE_FIELDS (base_type
);
3334 field
= DECL_CHAIN (field
))
3335 if (TREE_CODE (field
) == TYPE_DECL
3336 && DECL_NAME (field
) == id
)
3339 "(perhaps %<typename %T::%E%> was intended)",
3340 BINFO_TYPE (b
), id
);
3349 /* Here we diagnose qualified-ids where the scope is actually correct,
3350 but the identifier does not resolve to a valid type name. */
3351 else if (parser
->scope
!= error_mark_node
)
3353 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3356 error_at (location_of (id
),
3357 "%qE in namespace %qE does not name a template type",
3359 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3360 error_at (location_of (id
),
3361 "%qE in namespace %qE does not name a template type",
3362 TREE_OPERAND (id
, 0), parser
->scope
);
3364 error_at (location_of (id
),
3365 "%qE in namespace %qE does not name a type",
3368 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3369 else if (decl
== error_mark_node
)
3370 suggest_alternative_in_explicit_scope (location
, id
,
3373 else if (CLASS_TYPE_P (parser
->scope
)
3374 && constructor_name_p (id
, parser
->scope
))
3377 error_at (location
, "%<%T::%E%> names the constructor, not"
3378 " the type", parser
->scope
, id
);
3379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3380 error_at (location
, "and %qT has no template constructors",
3383 else if (TYPE_P (parser
->scope
)
3384 && dependent_scope_p (parser
->scope
))
3386 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3388 "need %<typename%> before %<%T::%D::%E%> because "
3389 "%<%T::%D%> is a dependent scope",
3390 TYPE_CONTEXT (parser
->scope
),
3391 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3393 TYPE_CONTEXT (parser
->scope
),
3394 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3396 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3397 "%qT is a dependent scope",
3398 parser
->scope
, id
, parser
->scope
);
3400 else if (TYPE_P (parser
->scope
))
3402 if (!COMPLETE_TYPE_P (parser
->scope
))
3403 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3405 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3406 error_at (location_of (id
),
3407 "%qE in %q#T does not name a template type",
3409 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3410 error_at (location_of (id
),
3411 "%qE in %q#T does not name a template type",
3412 TREE_OPERAND (id
, 0), parser
->scope
);
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a type",
3418 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3425 /* Check for a common situation where a type-name should be present,
3426 but is not, and issue a sensible error message. Returns true if an
3427 invalid type-name was detected.
3429 The situation handled by this function are variable declarations of the
3430 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3431 Usually, `ID' should name a type, but if we got here it means that it
3432 does not. We try to emit the best possible error message depending on
3433 how exactly the id-expression looks like. */
3436 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3439 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3441 /* Avoid duplicate error about ambiguous lookup. */
3442 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3444 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3445 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3449 cp_parser_parse_tentatively (parser
);
3450 id
= cp_parser_id_expression (parser
,
3451 /*template_keyword_p=*/false,
3452 /*check_dependency_p=*/true,
3453 /*template_p=*/NULL
,
3454 /*declarator_p=*/true,
3455 /*optional_p=*/false);
3456 /* If the next token is a (, this is a function with no explicit return
3457 type, i.e. constructor, destructor or conversion op. */
3458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3459 || TREE_CODE (id
) == TYPE_DECL
)
3461 cp_parser_abort_tentative_parse (parser
);
3464 if (!cp_parser_parse_definitely (parser
))
3467 /* Emit a diagnostic for the invalid type. */
3468 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3470 /* If we aren't in the middle of a declarator (i.e. in a
3471 parameter-declaration-clause), skip to the end of the declaration;
3472 there's no point in trying to process it. */
3473 if (!parser
->in_declarator_p
)
3474 cp_parser_skip_to_end_of_block_or_statement (parser
);
3478 /* Consume tokens up to, and including, the next non-nested closing `)'.
3479 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3480 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3481 found an unnested token of that type. */
3484 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3489 unsigned paren_depth
= 0;
3490 unsigned brace_depth
= 0;
3491 unsigned square_depth
= 0;
3493 if (recovering
&& or_ttype
== CPP_EOF
3494 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3499 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3501 /* Have we found what we're looking for before the closing paren? */
3502 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3503 && !brace_depth
&& !paren_depth
&& !square_depth
)
3506 switch (token
->type
)
3509 case CPP_PRAGMA_EOL
:
3510 /* If we've run out of tokens, then there is no closing `)'. */
3513 /* This is good for lambda expression capture-lists. */
3514 case CPP_OPEN_SQUARE
:
3517 case CPP_CLOSE_SQUARE
:
3518 if (!square_depth
--)
3523 /* This matches the processing in skip_to_end_of_statement. */
3528 case CPP_OPEN_BRACE
:
3531 case CPP_CLOSE_BRACE
:
3536 case CPP_OPEN_PAREN
:
3541 case CPP_CLOSE_PAREN
:
3542 if (!brace_depth
&& !paren_depth
--)
3545 cp_lexer_consume_token (parser
->lexer
);
3554 /* Consume the token. */
3555 cp_lexer_consume_token (parser
->lexer
);
3559 /* Consume tokens up to, and including, the next non-nested closing `)'.
3560 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3561 are doing error recovery. Returns -1 if OR_COMMA is true and we
3562 found an unnested token of that type. */
3565 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3570 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3571 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3572 ttype
, consume_paren
);
3575 /* Consume tokens until we reach the end of the current statement.
3576 Normally, that will be just before consuming a `;'. However, if a
3577 non-nested `}' comes first, then we stop before consuming that. */
3580 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3582 unsigned nesting_depth
= 0;
3584 /* Unwind generic function template scope if necessary. */
3585 if (parser
->fully_implicit_function_template_p
)
3586 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3592 switch (token
->type
)
3595 case CPP_PRAGMA_EOL
:
3596 /* If we've run out of tokens, stop. */
3600 /* If the next token is a `;', we have reached the end of the
3606 case CPP_CLOSE_BRACE
:
3607 /* If this is a non-nested '}', stop before consuming it.
3608 That way, when confronted with something like:
3612 we stop before consuming the closing '}', even though we
3613 have not yet reached a `;'. */
3614 if (nesting_depth
== 0)
3617 /* If it is the closing '}' for a block that we have
3618 scanned, stop -- but only after consuming the token.
3624 we will stop after the body of the erroneously declared
3625 function, but before consuming the following `typedef'
3627 if (--nesting_depth
== 0)
3629 cp_lexer_consume_token (parser
->lexer
);
3634 case CPP_OPEN_BRACE
:
3642 /* Consume the token. */
3643 cp_lexer_consume_token (parser
->lexer
);
3647 /* This function is called at the end of a statement or declaration.
3648 If the next token is a semicolon, it is consumed; otherwise, error
3649 recovery is attempted. */
3652 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3654 /* Look for the trailing `;'. */
3655 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3657 /* If there is additional (erroneous) input, skip to the end of
3659 cp_parser_skip_to_end_of_statement (parser
);
3660 /* If the next token is now a `;', consume it. */
3661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3662 cp_lexer_consume_token (parser
->lexer
);
3666 /* Skip tokens until we have consumed an entire block, or until we
3667 have consumed a non-nested `;'. */
3670 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3672 int nesting_depth
= 0;
3674 /* Unwind generic function template scope if necessary. */
3675 if (parser
->fully_implicit_function_template_p
)
3676 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3678 while (nesting_depth
>= 0)
3680 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3682 switch (token
->type
)
3685 case CPP_PRAGMA_EOL
:
3686 /* If we've run out of tokens, stop. */
3690 /* Stop if this is an unnested ';'. */
3695 case CPP_CLOSE_BRACE
:
3696 /* Stop if this is an unnested '}', or closes the outermost
3699 if (nesting_depth
< 0)
3705 case CPP_OPEN_BRACE
:
3714 /* Consume the token. */
3715 cp_lexer_consume_token (parser
->lexer
);
3719 /* Skip tokens until a non-nested closing curly brace is the next
3720 token, or there are no more tokens. Return true in the first case,
3724 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3726 unsigned nesting_depth
= 0;
3730 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3732 switch (token
->type
)
3735 case CPP_PRAGMA_EOL
:
3736 /* If we've run out of tokens, stop. */
3739 case CPP_CLOSE_BRACE
:
3740 /* If the next token is a non-nested `}', then we have reached
3741 the end of the current block. */
3742 if (nesting_depth
-- == 0)
3746 case CPP_OPEN_BRACE
:
3747 /* If it the next token is a `{', then we are entering a new
3748 block. Consume the entire block. */
3756 /* Consume the token. */
3757 cp_lexer_consume_token (parser
->lexer
);
3761 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3762 parameter is the PRAGMA token, allowing us to purge the entire pragma
3766 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3770 parser
->lexer
->in_pragma
= false;
3773 token
= cp_lexer_consume_token (parser
->lexer
);
3774 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3776 /* Ensure that the pragma is not parsed again. */
3777 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3780 /* Require pragma end of line, resyncing with it as necessary. The
3781 arguments are as for cp_parser_skip_to_pragma_eol. */
3784 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3786 parser
->lexer
->in_pragma
= false;
3787 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3788 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3791 /* This is a simple wrapper around make_typename_type. When the id is
3792 an unresolved identifier node, we can provide a superior diagnostic
3793 using cp_parser_diagnose_invalid_type_name. */
3796 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3797 location_t id_location
)
3800 if (identifier_p (id
))
3802 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3803 /*complain=*/tf_none
);
3804 if (result
== error_mark_node
)
3805 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3808 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3811 /* This is a wrapper around the
3812 make_{pointer,ptrmem,reference}_declarator functions that decides
3813 which one to call based on the CODE and CLASS_TYPE arguments. The
3814 CODE argument should be one of the values returned by
3815 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3816 appertain to the pointer or reference. */
3818 static cp_declarator
*
3819 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3820 cp_cv_quals cv_qualifiers
,
3821 cp_declarator
*target
,
3824 if (code
== ERROR_MARK
)
3825 return cp_error_declarator
;
3827 if (code
== INDIRECT_REF
)
3828 if (class_type
== NULL_TREE
)
3829 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3831 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3832 target
, attributes
);
3833 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3834 return make_reference_declarator (cv_qualifiers
, target
,
3836 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3837 return make_reference_declarator (cv_qualifiers
, target
,
3842 /* Create a new C++ parser. */
3845 cp_parser_new (void)
3851 /* cp_lexer_new_main is called before doing GC allocation because
3852 cp_lexer_new_main might load a PCH file. */
3853 lexer
= cp_lexer_new_main ();
3855 /* Initialize the binops_by_token so that we can get the tree
3856 directly from the token. */
3857 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3858 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3860 parser
= ggc_cleared_alloc
<cp_parser
> ();
3861 parser
->lexer
= lexer
;
3862 parser
->context
= cp_parser_context_new (NULL
);
3864 /* For now, we always accept GNU extensions. */
3865 parser
->allow_gnu_extensions_p
= 1;
3867 /* The `>' token is a greater-than operator, not the end of a
3869 parser
->greater_than_is_operator_p
= true;
3871 parser
->default_arg_ok_p
= true;
3873 /* We are not parsing a constant-expression. */
3874 parser
->integral_constant_expression_p
= false;
3875 parser
->allow_non_integral_constant_expression_p
= false;
3876 parser
->non_integral_constant_expression_p
= false;
3878 /* Local variable names are not forbidden. */
3879 parser
->local_variables_forbidden_p
= false;
3881 /* We are not processing an `extern "C"' declaration. */
3882 parser
->in_unbraced_linkage_specification_p
= false;
3884 /* We are not processing a declarator. */
3885 parser
->in_declarator_p
= false;
3887 /* We are not processing a template-argument-list. */
3888 parser
->in_template_argument_list_p
= false;
3890 /* We are not in an iteration statement. */
3891 parser
->in_statement
= 0;
3893 /* We are not in a switch statement. */
3894 parser
->in_switch_statement_p
= false;
3896 /* We are not parsing a type-id inside an expression. */
3897 parser
->in_type_id_in_expr_p
= false;
3899 /* Declarations aren't implicitly extern "C". */
3900 parser
->implicit_extern_c
= false;
3902 /* String literals should be translated to the execution character set. */
3903 parser
->translate_strings_p
= true;
3905 /* We are not parsing a function body. */
3906 parser
->in_function_body
= false;
3908 /* We can correct until told otherwise. */
3909 parser
->colon_corrects_to_scope_p
= true;
3911 /* The unparsed function queue is empty. */
3912 push_unparsed_function_queues (parser
);
3914 /* There are no classes being defined. */
3915 parser
->num_classes_being_defined
= 0;
3917 /* No template parameters apply. */
3918 parser
->num_template_parameter_lists
= 0;
3920 /* Special parsing data structures. */
3921 parser
->omp_declare_simd
= NULL
;
3922 parser
->oacc_routine
= NULL
;
3924 /* Not declaring an implicit function template. */
3925 parser
->auto_is_implicit_function_template_parm_p
= false;
3926 parser
->fully_implicit_function_template_p
= false;
3927 parser
->implicit_template_parms
= 0;
3928 parser
->implicit_template_scope
= 0;
3930 /* Allow constrained-type-specifiers. */
3931 parser
->prevent_constrained_type_specifiers
= 0;
3933 /* We haven't yet seen an 'extern "C"'. */
3934 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3939 /* Create a cp_lexer structure which will emit the tokens in CACHE
3940 and push it onto the parser's lexer stack. This is used for delayed
3941 parsing of in-class method bodies and default arguments, and should
3942 not be confused with tentative parsing. */
3944 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3946 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3947 lexer
->next
= parser
->lexer
;
3948 parser
->lexer
= lexer
;
3950 /* Move the current source position to that of the first token in the
3952 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3955 /* Pop the top lexer off the parser stack. This is never used for the
3956 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3958 cp_parser_pop_lexer (cp_parser
*parser
)
3960 cp_lexer
*lexer
= parser
->lexer
;
3961 parser
->lexer
= lexer
->next
;
3962 cp_lexer_destroy (lexer
);
3964 /* Put the current source position back where it was before this
3965 lexer was pushed. */
3966 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3969 /* Lexical conventions [gram.lex] */
3971 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3975 cp_parser_identifier (cp_parser
* parser
)
3979 /* Look for the identifier. */
3980 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3981 /* Return the value. */
3983 return cp_expr (token
->u
.value
, token
->location
);
3985 return error_mark_node
;
3988 /* Parse a sequence of adjacent string constants. Returns a
3989 TREE_STRING representing the combined, nul-terminated string
3990 constant. If TRANSLATE is true, translate the string to the
3991 execution character set. If WIDE_OK is true, a wide string is
3994 C++98 [lex.string] says that if a narrow string literal token is
3995 adjacent to a wide string literal token, the behavior is undefined.
3996 However, C99 6.4.5p4 says that this results in a wide string literal.
3997 We follow C99 here, for consistency with the C front end.
3999 This code is largely lifted from lex_string() in c-lex.c.
4001 FUTURE: ObjC++ will need to handle @-strings here. */
4003 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4004 bool lookup_udlit
= true)
4008 struct obstack str_ob
;
4009 cpp_string str
, istr
, *strs
;
4011 enum cpp_ttype type
, curr_type
;
4012 int have_suffix_p
= 0;
4014 tree suffix_id
= NULL_TREE
;
4015 bool curr_tok_is_userdef_p
= false;
4017 tok
= cp_lexer_peek_token (parser
->lexer
);
4018 if (!cp_parser_is_string_literal (tok
))
4020 cp_parser_error (parser
, "expected string-literal");
4021 return error_mark_node
;
4024 location_t loc
= tok
->location
;
4026 if (cpp_userdef_string_p (tok
->type
))
4028 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4029 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4030 curr_tok_is_userdef_p
= true;
4034 string_tree
= tok
->u
.value
;
4035 curr_type
= tok
->type
;
4039 /* Try to avoid the overhead of creating and destroying an obstack
4040 for the common case of just one string. */
4041 if (!cp_parser_is_string_literal
4042 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4044 cp_lexer_consume_token (parser
->lexer
);
4046 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4047 str
.len
= TREE_STRING_LENGTH (string_tree
);
4050 if (curr_tok_is_userdef_p
)
4052 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4054 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4057 curr_type
= tok
->type
;
4063 location_t last_tok_loc
= tok
->location
;
4064 gcc_obstack_init (&str_ob
);
4069 cp_lexer_consume_token (parser
->lexer
);
4071 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4072 str
.len
= TREE_STRING_LENGTH (string_tree
);
4074 if (curr_tok_is_userdef_p
)
4076 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4077 if (have_suffix_p
== 0)
4079 suffix_id
= curr_suffix_id
;
4082 else if (have_suffix_p
== 1
4083 && curr_suffix_id
!= suffix_id
)
4085 error ("inconsistent user-defined literal suffixes"
4086 " %qD and %qD in string literal",
4087 suffix_id
, curr_suffix_id
);
4090 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4093 curr_type
= tok
->type
;
4095 if (type
!= curr_type
)
4097 if (type
== CPP_STRING
)
4099 else if (curr_type
!= CPP_STRING
)
4101 rich_location
rich_loc (line_table
, tok
->location
);
4102 rich_loc
.add_range (last_tok_loc
, false);
4103 error_at (&rich_loc
,
4104 "unsupported non-standard concatenation "
4105 "of string literals");
4109 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4111 last_tok_loc
= tok
->location
;
4113 tok
= cp_lexer_peek_token (parser
->lexer
);
4114 if (cpp_userdef_string_p (tok
->type
))
4116 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4117 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4118 curr_tok_is_userdef_p
= true;
4122 string_tree
= tok
->u
.value
;
4123 curr_type
= tok
->type
;
4124 curr_tok_is_userdef_p
= false;
4127 while (cp_parser_is_string_literal (tok
));
4129 /* A string literal built by concatenation has its caret=start at
4130 the start of the initial string, and its finish at the finish of
4131 the final string literal. */
4132 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4134 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4137 if (type
!= CPP_STRING
&& !wide_ok
)
4139 cp_parser_error (parser
, "a wide string is invalid in this context");
4143 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4144 (parse_in
, strs
, count
, &istr
, type
))
4146 value
= build_string (istr
.len
, (const char *)istr
.text
);
4147 free (CONST_CAST (unsigned char *, istr
.text
));
4153 case CPP_UTF8STRING
:
4154 TREE_TYPE (value
) = char_array_type_node
;
4157 TREE_TYPE (value
) = char16_array_type_node
;
4160 TREE_TYPE (value
) = char32_array_type_node
;
4163 TREE_TYPE (value
) = wchar_array_type_node
;
4167 value
= fix_string_type (value
);
4171 tree literal
= build_userdef_literal (suffix_id
, value
,
4172 OT_NONE
, NULL_TREE
);
4174 value
= cp_parser_userdef_string_literal (literal
);
4180 /* cpp_interpret_string has issued an error. */
4181 value
= error_mark_node
;
4184 obstack_free (&str_ob
, 0);
4186 return cp_expr (value
, loc
);
4189 /* Look up a literal operator with the name and the exact arguments. */
4192 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4195 decl
= lookup_name (name
);
4196 if (!decl
|| !is_overloaded_fn (decl
))
4197 return error_mark_node
;
4199 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4204 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4205 if (parmtypes
!= NULL_TREE
)
4207 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4208 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4210 tree tparm
= TREE_VALUE (parmtypes
);
4211 tree targ
= TREE_TYPE ((*args
)[ix
]);
4212 bool ptr
= TYPE_PTR_P (tparm
);
4213 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4214 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4216 || !same_type_p (TREE_TYPE (tparm
),
4221 && ix
== vec_safe_length (args
)
4222 /* May be this should be sufficient_parms_p instead,
4223 depending on how exactly should user-defined literals
4224 work in presence of default arguments on the literal
4225 operator parameters. */
4226 && parmtypes
== void_list_node
)
4231 return error_mark_node
;
4234 /* Parse a user-defined char constant. Returns a call to a user-defined
4235 literal operator taking the character as an argument. */
4238 cp_parser_userdef_char_literal (cp_parser
*parser
)
4240 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4241 tree literal
= token
->u
.value
;
4242 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4243 tree value
= USERDEF_LITERAL_VALUE (literal
);
4244 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4247 /* Build up a call to the user-defined operator */
4248 /* Lookup the name we got back from the id-expression. */
4249 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4250 vec_safe_push (args
, value
);
4251 decl
= lookup_literal_operator (name
, args
);
4252 if (!decl
|| decl
== error_mark_node
)
4254 error ("unable to find character literal operator %qD with %qT argument",
4255 name
, TREE_TYPE (value
));
4256 release_tree_vector (args
);
4257 return error_mark_node
;
4259 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4260 release_tree_vector (args
);
4264 /* A subroutine of cp_parser_userdef_numeric_literal to
4265 create a char... template parameter pack from a string node. */
4268 make_char_string_pack (tree value
)
4271 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4272 const char *str
= TREE_STRING_POINTER (value
);
4273 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4274 tree argvec
= make_tree_vec (1);
4276 /* Fill in CHARVEC with all of the parameters. */
4277 charvec
= make_tree_vec (len
);
4278 for (i
= 0; i
< len
; ++i
)
4279 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4281 /* Build the argument packs. */
4282 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4284 TREE_VEC_ELT (argvec
, 0) = argpack
;
4289 /* A subroutine of cp_parser_userdef_numeric_literal to
4290 create a char... template parameter pack from a string node. */
4293 make_string_pack (tree value
)
4296 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4297 const unsigned char *str
4298 = (const unsigned char *) TREE_STRING_POINTER (value
);
4299 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4300 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4301 tree argvec
= make_tree_vec (2);
4303 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4304 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4306 /* First template parm is character type. */
4307 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4309 /* Fill in CHARVEC with all of the parameters. */
4310 charvec
= make_tree_vec (len
);
4311 for (int i
= 0; i
< len
; ++i
)
4312 TREE_VEC_ELT (charvec
, i
)
4313 = double_int_to_tree (str_char_type_node
,
4314 double_int::from_buffer (str
+ i
* sz
, sz
));
4316 /* Build the argument packs. */
4317 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4319 TREE_VEC_ELT (argvec
, 1) = argpack
;
4324 /* Parse a user-defined numeric constant. returns a call to a user-defined
4325 literal operator. */
4328 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4330 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4331 tree literal
= token
->u
.value
;
4332 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4333 tree value
= USERDEF_LITERAL_VALUE (literal
);
4334 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4335 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4336 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4338 vec
<tree
, va_gc
> *args
;
4340 /* Look for a literal operator taking the exact type of numeric argument
4341 as the literal value. */
4342 args
= make_tree_vector ();
4343 vec_safe_push (args
, value
);
4344 decl
= lookup_literal_operator (name
, args
);
4345 if (decl
&& decl
!= error_mark_node
)
4347 result
= finish_call_expr (decl
, &args
, false, true,
4348 tf_warning_or_error
);
4350 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4352 warning_at (token
->location
, OPT_Woverflow
,
4353 "integer literal exceeds range of %qT type",
4354 long_long_unsigned_type_node
);
4359 warning_at (token
->location
, OPT_Woverflow
,
4360 "floating literal exceeds range of %qT type",
4361 long_double_type_node
);
4362 else if (overflow
< 0)
4363 warning_at (token
->location
, OPT_Woverflow
,
4364 "floating literal truncated to zero");
4367 release_tree_vector (args
);
4370 release_tree_vector (args
);
4372 /* If the numeric argument didn't work, look for a raw literal
4373 operator taking a const char* argument consisting of the number
4374 in string format. */
4375 args
= make_tree_vector ();
4376 vec_safe_push (args
, num_string
);
4377 decl
= lookup_literal_operator (name
, args
);
4378 if (decl
&& decl
!= error_mark_node
)
4380 result
= finish_call_expr (decl
, &args
, false, true,
4381 tf_warning_or_error
);
4382 release_tree_vector (args
);
4385 release_tree_vector (args
);
4387 /* If the raw literal didn't work, look for a non-type template
4388 function with parameter pack char.... Call the function with
4389 template parameter characters representing the number. */
4390 args
= make_tree_vector ();
4391 decl
= lookup_literal_operator (name
, args
);
4392 if (decl
&& decl
!= error_mark_node
)
4394 tree tmpl_args
= make_char_string_pack (num_string
);
4395 decl
= lookup_template_function (decl
, tmpl_args
);
4396 result
= finish_call_expr (decl
, &args
, false, true,
4397 tf_warning_or_error
);
4398 release_tree_vector (args
);
4402 release_tree_vector (args
);
4404 /* In C++14 the standard library defines complex number suffixes that
4405 conflict with GNU extensions. Prefer them if <complex> is #included. */
4406 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4407 bool i14
= (cxx_dialect
> cxx11
4408 && (id_equal (suffix_id
, "i")
4409 || id_equal (suffix_id
, "if")
4410 || id_equal (suffix_id
, "il")));
4411 diagnostic_t kind
= DK_ERROR
;
4416 tree cxlit
= lookup_qualified_name (std_node
,
4417 get_identifier ("complex_literals"),
4419 if (cxlit
== error_mark_node
)
4421 /* No <complex>, so pedwarn and use GNU semantics. */
4423 opt
= OPT_Wpedantic
;
4428 = emit_diagnostic (kind
, input_location
, opt
,
4429 "unable to find numeric literal operator %qD", name
);
4432 /* Don't inform either. */;
4435 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4436 "(from <complex>) to enable the C++14 user-defined literal "
4439 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4440 "GNU built-in suffix");
4443 inform (token
->location
, "use -fext-numeric-literals "
4444 "to enable more built-in suffixes");
4446 if (kind
== DK_ERROR
)
4447 value
= error_mark_node
;
4450 /* Use the built-in semantics. */
4452 if (id_equal (suffix_id
, "i"))
4454 if (TREE_CODE (value
) == INTEGER_CST
)
4455 type
= integer_type_node
;
4457 type
= double_type_node
;
4459 else if (id_equal (suffix_id
, "if"))
4460 type
= float_type_node
;
4461 else /* if (id_equal (suffix_id, "il")) */
4462 type
= long_double_type_node
;
4464 value
= build_complex (build_complex_type (type
),
4465 fold_convert (type
, integer_zero_node
),
4466 fold_convert (type
, value
));
4469 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4470 /* Avoid repeated diagnostics. */
4471 token
->u
.value
= value
;
4475 /* Parse a user-defined string constant. Returns a call to a user-defined
4476 literal operator taking a character pointer and the length of the string
4480 cp_parser_userdef_string_literal (tree literal
)
4482 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4483 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4484 tree value
= USERDEF_LITERAL_VALUE (literal
);
4485 int len
= TREE_STRING_LENGTH (value
)
4486 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4488 vec
<tree
, va_gc
> *args
;
4490 /* Build up a call to the user-defined operator. */
4491 /* Lookup the name we got back from the id-expression. */
4492 args
= make_tree_vector ();
4493 vec_safe_push (args
, value
);
4494 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4495 decl
= lookup_literal_operator (name
, args
);
4497 if (decl
&& decl
!= error_mark_node
)
4499 result
= finish_call_expr (decl
, &args
, false, true,
4500 tf_warning_or_error
);
4501 release_tree_vector (args
);
4504 release_tree_vector (args
);
4506 /* Look for a template function with typename parameter CharT
4507 and parameter pack CharT... Call the function with
4508 template parameter characters representing the string. */
4509 args
= make_tree_vector ();
4510 decl
= lookup_literal_operator (name
, args
);
4511 if (decl
&& decl
!= error_mark_node
)
4513 tree tmpl_args
= make_string_pack (value
);
4514 decl
= lookup_template_function (decl
, tmpl_args
);
4515 result
= finish_call_expr (decl
, &args
, false, true,
4516 tf_warning_or_error
);
4517 release_tree_vector (args
);
4520 release_tree_vector (args
);
4522 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4523 name
, TREE_TYPE (value
), size_type_node
);
4524 return error_mark_node
;
4528 /* Basic concepts [gram.basic] */
4530 /* Parse a translation-unit.
4533 declaration-seq [opt]
4535 Returns TRUE if all went well. */
4538 cp_parser_translation_unit (cp_parser
* parser
)
4540 /* The address of the first non-permanent object on the declarator
4542 static void *declarator_obstack_base
;
4546 /* Create the declarator obstack, if necessary. */
4547 if (!cp_error_declarator
)
4549 gcc_obstack_init (&declarator_obstack
);
4550 /* Create the error declarator. */
4551 cp_error_declarator
= make_declarator (cdk_error
);
4552 /* Create the empty parameter list. */
4553 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4555 /* Remember where the base of the declarator obstack lies. */
4556 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4559 cp_parser_declaration_seq_opt (parser
);
4561 /* If there are no tokens left then all went well. */
4562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4564 /* Get rid of the token array; we don't need it any more. */
4565 cp_lexer_destroy (parser
->lexer
);
4566 parser
->lexer
= NULL
;
4568 /* This file might have been a context that's implicitly extern
4569 "C". If so, pop the lang context. (Only relevant for PCH.) */
4570 if (parser
->implicit_extern_c
)
4572 pop_lang_context ();
4573 parser
->implicit_extern_c
= false;
4577 finish_translation_unit ();
4583 cp_parser_error (parser
, "expected declaration");
4587 /* Make sure the declarator obstack was fully cleaned up. */
4588 gcc_assert (obstack_next_free (&declarator_obstack
)
4589 == declarator_obstack_base
);
4591 /* All went well. */
4595 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4596 decltype context. */
4598 static inline tsubst_flags_t
4599 complain_flags (bool decltype_p
)
4601 tsubst_flags_t complain
= tf_warning_or_error
;
4603 complain
|= tf_decltype
;
4607 /* We're about to parse a collection of statements. If we're currently
4608 parsing tentatively, set up a firewall so that any nested
4609 cp_parser_commit_to_tentative_parse won't affect the current context. */
4611 static cp_token_position
4612 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4614 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4617 cp_parser_parse_tentatively (parser
);
4618 cp_parser_commit_to_topmost_tentative_parse (parser
);
4619 return cp_lexer_token_position (parser
->lexer
, false);
4622 /* We've finished parsing the collection of statements. Wrap up the
4623 firewall and replace the relevant tokens with the parsed form. */
4626 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4632 /* Finish the firewall level. */
4633 cp_parser_parse_definitely (parser
);
4634 /* And remember the result of the parse for when we try again. */
4635 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4636 token
->type
= CPP_PREPARSED_EXPR
;
4637 token
->u
.value
= expr
;
4638 token
->keyword
= RID_MAX
;
4639 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4642 /* Like the above functions, but let the user modify the tokens. Used by
4643 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4644 later parses, so it makes sense to localize the effects of
4645 cp_parser_commit_to_tentative_parse. */
4647 struct tentative_firewall
4652 tentative_firewall (cp_parser
*p
): parser(p
)
4654 /* If we're currently parsing tentatively, start a committed level as a
4655 firewall and then an inner tentative parse. */
4656 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4658 cp_parser_parse_tentatively (parser
);
4659 cp_parser_commit_to_topmost_tentative_parse (parser
);
4660 cp_parser_parse_tentatively (parser
);
4664 ~tentative_firewall()
4668 /* Finish the inner tentative parse and the firewall, propagating any
4669 uncommitted error state to the outer tentative parse. */
4670 bool err
= cp_parser_error_occurred (parser
);
4671 cp_parser_parse_definitely (parser
);
4672 cp_parser_parse_definitely (parser
);
4674 cp_parser_simulate_error (parser
);
4679 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4680 This class is for tracking such a matching pair of symbols.
4681 In particular, it tracks the location of the first token,
4682 so that if the second token is missing, we can highlight the
4683 location of the first token when notifying the user about the
4686 template <typename traits_t
>
4690 /* token_pair's ctor. */
4691 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4693 /* If the next token is the opening symbol for this pair, consume it and
4695 Otherwise, issue an error and return false.
4696 In either case, record the location of the opening token. */
4698 bool require_open (cp_parser
*parser
)
4700 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4701 return cp_parser_require (parser
, traits_t::open_token_type
,
4702 traits_t::required_token_open
);
4705 /* Consume the next token from PARSER, recording its location as
4706 that of the opening token within the pair. */
4708 cp_token
* consume_open (cp_parser
*parser
)
4710 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4711 gcc_assert (tok
->type
== traits_t::open_token_type
);
4712 m_open_loc
= tok
->location
;
4716 /* If the next token is the closing symbol for this pair, consume it
4718 Otherwise, issue an error, highlighting the location of the
4719 corresponding opening token, and return NULL. */
4721 cp_token
*require_close (cp_parser
*parser
) const
4723 return cp_parser_require (parser
, traits_t::close_token_type
,
4724 traits_t::required_token_close
,
4729 location_t m_open_loc
;
4732 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4734 struct matching_paren_traits
4736 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4737 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4738 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4739 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4742 /* "matching_parens" is a token_pair<T> class for tracking matching
4743 pairs of parentheses. */
4745 typedef token_pair
<matching_paren_traits
> matching_parens
;
4747 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4749 struct matching_brace_traits
4751 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4752 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4753 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4754 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4757 /* "matching_braces" is a token_pair<T> class for tracking matching
4760 typedef token_pair
<matching_brace_traits
> matching_braces
;
4763 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4764 enclosing parentheses. */
4767 cp_parser_statement_expr (cp_parser
*parser
)
4769 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4771 /* Consume the '('. */
4772 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4773 matching_parens parens
;
4774 parens
.consume_open (parser
);
4775 /* Start the statement-expression. */
4776 tree expr
= begin_stmt_expr ();
4777 /* Parse the compound-statement. */
4778 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4780 expr
= finish_stmt_expr (expr
, false);
4781 /* Consume the ')'. */
4782 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4783 if (!parens
.require_close (parser
))
4784 cp_parser_skip_to_end_of_statement (parser
);
4786 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4787 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4788 return cp_expr (expr
, combined_loc
);
4791 /* Expressions [gram.expr] */
4793 /* Parse a fold-operator.
4796 - * / % ^ & | = < > << >>
4797 = -= *= /= %= ^= &= |= <<= >>=
4798 == != <= >= && || , .* ->*
4800 This returns the tree code corresponding to the matched operator
4801 as an int. When the current token matches a compound assignment
4802 opertor, the resulting tree code is the negative value of the
4803 non-assignment operator. */
4806 cp_parser_fold_operator (cp_token
*token
)
4808 switch (token
->type
)
4810 case CPP_PLUS
: return PLUS_EXPR
;
4811 case CPP_MINUS
: return MINUS_EXPR
;
4812 case CPP_MULT
: return MULT_EXPR
;
4813 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4814 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4815 case CPP_XOR
: return BIT_XOR_EXPR
;
4816 case CPP_AND
: return BIT_AND_EXPR
;
4817 case CPP_OR
: return BIT_IOR_EXPR
;
4818 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4819 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4821 case CPP_EQ
: return -NOP_EXPR
;
4822 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4823 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4824 case CPP_MULT_EQ
: return -MULT_EXPR
;
4825 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4826 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4827 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4828 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4829 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4830 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4831 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4833 case CPP_EQ_EQ
: return EQ_EXPR
;
4834 case CPP_NOT_EQ
: return NE_EXPR
;
4835 case CPP_LESS
: return LT_EXPR
;
4836 case CPP_GREATER
: return GT_EXPR
;
4837 case CPP_LESS_EQ
: return LE_EXPR
;
4838 case CPP_GREATER_EQ
: return GE_EXPR
;
4840 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4841 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4843 case CPP_COMMA
: return COMPOUND_EXPR
;
4845 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4846 case CPP_DEREF_STAR
: return MEMBER_REF
;
4848 default: return ERROR_MARK
;
4852 /* Returns true if CODE indicates a binary expression, which is not allowed in
4853 the LHS of a fold-expression. More codes will need to be added to use this
4854 function in other contexts. */
4857 is_binary_op (tree_code code
)
4862 case POINTER_PLUS_EXPR
:
4865 case TRUNC_DIV_EXPR
:
4866 case TRUNC_MOD_EXPR
:
4882 case TRUTH_ANDIF_EXPR
:
4883 case TRUTH_ORIF_EXPR
:
4896 /* If the next token is a suitable fold operator, consume it and return as
4897 the function above. */
4900 cp_parser_fold_operator (cp_parser
*parser
)
4902 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4903 int code
= cp_parser_fold_operator (token
);
4904 if (code
!= ERROR_MARK
)
4905 cp_lexer_consume_token (parser
->lexer
);
4909 /* Parse a fold-expression.
4912 ( ... folding-operator cast-expression)
4913 ( cast-expression folding-operator ... )
4914 ( cast-expression folding operator ... folding-operator cast-expression)
4916 Note that the '(' and ')' are matched in primary expression. */
4919 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4926 cp_lexer_consume_token (parser
->lexer
);
4927 int op
= cp_parser_fold_operator (parser
);
4928 if (op
== ERROR_MARK
)
4930 cp_parser_error (parser
, "expected binary operator");
4931 return error_mark_node
;
4934 tree expr
= cp_parser_cast_expression (parser
, false, false,
4936 if (expr
== error_mark_node
)
4937 return error_mark_node
;
4938 return finish_left_unary_fold_expr (expr
, op
);
4941 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4942 int op
= cp_parser_fold_operator (parser
);
4943 if (op
== ERROR_MARK
)
4945 cp_parser_error (parser
, "expected binary operator");
4946 return error_mark_node
;
4949 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4951 cp_parser_error (parser
, "expected ...");
4952 return error_mark_node
;
4954 cp_lexer_consume_token (parser
->lexer
);
4956 /* The operands of a fold-expression are cast-expressions, so binary or
4957 conditional expressions are not allowed. We check this here to avoid
4958 tentative parsing. */
4959 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4960 /* OK, the expression was parenthesized. */;
4961 else if (is_binary_op (TREE_CODE (expr1
)))
4962 error_at (location_of (expr1
),
4963 "binary expression in operand of fold-expression");
4964 else if (TREE_CODE (expr1
) == COND_EXPR
)
4965 error_at (location_of (expr1
),
4966 "conditional expression in operand of fold-expression");
4969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4970 return finish_right_unary_fold_expr (expr1
, op
);
4972 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4974 cp_parser_error (parser
, "mismatched operator in fold-expression");
4975 return error_mark_node
;
4977 cp_lexer_consume_token (parser
->lexer
);
4979 // Binary left or right fold.
4980 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4981 if (expr2
== error_mark_node
)
4982 return error_mark_node
;
4983 return finish_binary_fold_expr (expr1
, expr2
, op
);
4986 /* Parse a primary-expression.
4993 lambda-expression (C++11)
4998 ( compound-statement )
4999 __builtin_va_arg ( assignment-expression , type-id )
5000 __builtin_offsetof ( type-id , offsetof-expression )
5003 __has_nothrow_assign ( type-id )
5004 __has_nothrow_constructor ( type-id )
5005 __has_nothrow_copy ( type-id )
5006 __has_trivial_assign ( type-id )
5007 __has_trivial_constructor ( type-id )
5008 __has_trivial_copy ( type-id )
5009 __has_trivial_destructor ( type-id )
5010 __has_virtual_destructor ( type-id )
5011 __is_abstract ( type-id )
5012 __is_base_of ( type-id , type-id )
5013 __is_class ( type-id )
5014 __is_empty ( type-id )
5015 __is_enum ( type-id )
5016 __is_final ( type-id )
5017 __is_literal_type ( type-id )
5018 __is_pod ( type-id )
5019 __is_polymorphic ( type-id )
5020 __is_std_layout ( type-id )
5021 __is_trivial ( type-id )
5022 __is_union ( type-id )
5024 Objective-C++ Extension:
5032 ADDRESS_P is true iff this expression was immediately preceded by
5033 "&" and therefore might denote a pointer-to-member. CAST_P is true
5034 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5035 true iff this expression is a template argument.
5037 Returns a representation of the expression. Upon return, *IDK
5038 indicates what kind of id-expression (if any) was present. */
5041 cp_parser_primary_expression (cp_parser
*parser
,
5044 bool template_arg_p
,
5048 cp_token
*token
= NULL
;
5050 /* Assume the primary expression is not an id-expression. */
5051 *idk
= CP_ID_KIND_NONE
;
5053 /* Peek at the next token. */
5054 token
= cp_lexer_peek_token (parser
->lexer
);
5055 switch ((int) token
->type
)
5064 user-defined-literal */
5071 case CPP_PREPARSED_EXPR
:
5072 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5073 return cp_parser_userdef_numeric_literal (parser
);
5074 token
= cp_lexer_consume_token (parser
->lexer
);
5075 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5077 error_at (token
->location
,
5078 "fixed-point types not supported in C++");
5079 return error_mark_node
;
5081 /* Floating-point literals are only allowed in an integral
5082 constant expression if they are cast to an integral or
5083 enumeration type. */
5084 if (TREE_CODE (token
->u
.value
) == REAL_CST
5085 && parser
->integral_constant_expression_p
5088 /* CAST_P will be set even in invalid code like "int(2.7 +
5089 ...)". Therefore, we have to check that the next token
5090 is sure to end the cast. */
5093 cp_token
*next_token
;
5095 next_token
= cp_lexer_peek_token (parser
->lexer
);
5096 if (/* The comma at the end of an
5097 enumerator-definition. */
5098 next_token
->type
!= CPP_COMMA
5099 /* The curly brace at the end of an enum-specifier. */
5100 && next_token
->type
!= CPP_CLOSE_BRACE
5101 /* The end of a statement. */
5102 && next_token
->type
!= CPP_SEMICOLON
5103 /* The end of the cast-expression. */
5104 && next_token
->type
!= CPP_CLOSE_PAREN
5105 /* The end of an array bound. */
5106 && next_token
->type
!= CPP_CLOSE_SQUARE
5107 /* The closing ">" in a template-argument-list. */
5108 && (next_token
->type
!= CPP_GREATER
5109 || parser
->greater_than_is_operator_p
)
5110 /* C++0x only: A ">>" treated like two ">" tokens,
5111 in a template-argument-list. */
5112 && (next_token
->type
!= CPP_RSHIFT
5113 || (cxx_dialect
== cxx98
)
5114 || parser
->greater_than_is_operator_p
))
5118 /* If we are within a cast, then the constraint that the
5119 cast is to an integral or enumeration type will be
5120 checked at that point. If we are not within a cast, then
5121 this code is invalid. */
5123 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5125 return cp_expr (token
->u
.value
, token
->location
);
5127 case CPP_CHAR_USERDEF
:
5128 case CPP_CHAR16_USERDEF
:
5129 case CPP_CHAR32_USERDEF
:
5130 case CPP_WCHAR_USERDEF
:
5131 case CPP_UTF8CHAR_USERDEF
:
5132 return cp_parser_userdef_char_literal (parser
);
5138 case CPP_UTF8STRING
:
5139 case CPP_STRING_USERDEF
:
5140 case CPP_STRING16_USERDEF
:
5141 case CPP_STRING32_USERDEF
:
5142 case CPP_WSTRING_USERDEF
:
5143 case CPP_UTF8STRING_USERDEF
:
5144 /* ??? Should wide strings be allowed when parser->translate_strings_p
5145 is false (i.e. in attributes)? If not, we can kill the third
5146 argument to cp_parser_string_literal. */
5147 return cp_parser_string_literal (parser
,
5148 parser
->translate_strings_p
,
5151 case CPP_OPEN_PAREN
:
5152 /* If we see `( { ' then we are looking at the beginning of
5153 a GNU statement-expression. */
5154 if (cp_parser_allow_gnu_extensions_p (parser
)
5155 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5157 /* Statement-expressions are not allowed by the standard. */
5158 pedwarn (token
->location
, OPT_Wpedantic
,
5159 "ISO C++ forbids braced-groups within expressions");
5161 /* And they're not allowed outside of a function-body; you
5162 cannot, for example, write:
5164 int i = ({ int j = 3; j + 1; });
5166 at class or namespace scope. */
5167 if (!parser
->in_function_body
5168 || parser
->in_template_argument_list_p
)
5170 error_at (token
->location
,
5171 "statement-expressions are not allowed outside "
5172 "functions nor in template-argument lists");
5173 cp_parser_skip_to_end_of_block_or_statement (parser
);
5174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5175 cp_lexer_consume_token (parser
->lexer
);
5176 return error_mark_node
;
5179 return cp_parser_statement_expr (parser
);
5181 /* Otherwise it's a normal parenthesized expression. */
5184 bool saved_greater_than_is_operator_p
;
5186 location_t open_paren_loc
= token
->location
;
5188 /* Consume the `('. */
5189 matching_parens parens
;
5190 parens
.consume_open (parser
);
5191 /* Within a parenthesized expression, a `>' token is always
5192 the greater-than operator. */
5193 saved_greater_than_is_operator_p
5194 = parser
->greater_than_is_operator_p
;
5195 parser
->greater_than_is_operator_p
= true;
5197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5198 /* Left fold expression. */
5201 /* Parse the parenthesized expression. */
5202 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5204 token
= cp_lexer_peek_token (parser
->lexer
);
5205 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5207 expr
= cp_parser_fold_expression (parser
, expr
);
5208 if (expr
!= error_mark_node
5209 && cxx_dialect
< cxx17
5210 && !in_system_header_at (input_location
))
5211 pedwarn (input_location
, 0, "fold-expressions only available "
5212 "with -std=c++17 or -std=gnu++17");
5215 /* Let the front end know that this expression was
5216 enclosed in parentheses. This matters in case, for
5217 example, the expression is of the form `A::B', since
5218 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5220 expr
= finish_parenthesized_expr (expr
);
5222 /* DR 705: Wrapping an unqualified name in parentheses
5223 suppresses arg-dependent lookup. We want to pass back
5224 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5225 (c++/37862), but none of the others. */
5226 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5227 *idk
= CP_ID_KIND_NONE
;
5229 /* The `>' token might be the end of a template-id or
5230 template-parameter-list now. */
5231 parser
->greater_than_is_operator_p
5232 = saved_greater_than_is_operator_p
;
5234 /* Consume the `)'. */
5235 token
= cp_lexer_peek_token (parser
->lexer
);
5236 location_t close_paren_loc
= token
->location
;
5237 expr
.set_range (open_paren_loc
, close_paren_loc
);
5238 if (!parens
.require_close (parser
)
5239 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5240 cp_parser_skip_to_end_of_statement (parser
);
5245 case CPP_OPEN_SQUARE
:
5247 if (c_dialect_objc ())
5249 /* We might have an Objective-C++ message. */
5250 cp_parser_parse_tentatively (parser
);
5251 tree msg
= cp_parser_objc_message_expression (parser
);
5252 /* If that works out, we're done ... */
5253 if (cp_parser_parse_definitely (parser
))
5255 /* ... else, fall though to see if it's a lambda. */
5257 cp_expr lam
= cp_parser_lambda_expression (parser
);
5258 /* Don't warn about a failed tentative parse. */
5259 if (cp_parser_error_occurred (parser
))
5260 return error_mark_node
;
5261 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5265 case CPP_OBJC_STRING
:
5266 if (c_dialect_objc ())
5267 /* We have an Objective-C++ string literal. */
5268 return cp_parser_objc_expression (parser
);
5269 cp_parser_error (parser
, "expected primary-expression");
5270 return error_mark_node
;
5273 switch (token
->keyword
)
5275 /* These two are the boolean literals. */
5277 cp_lexer_consume_token (parser
->lexer
);
5278 return cp_expr (boolean_true_node
, token
->location
);
5280 cp_lexer_consume_token (parser
->lexer
);
5281 return cp_expr (boolean_false_node
, token
->location
);
5283 /* The `__null' literal. */
5285 cp_lexer_consume_token (parser
->lexer
);
5286 return cp_expr (null_node
, token
->location
);
5288 /* The `nullptr' literal. */
5290 cp_lexer_consume_token (parser
->lexer
);
5291 return cp_expr (nullptr_node
, token
->location
);
5293 /* Recognize the `this' keyword. */
5295 cp_lexer_consume_token (parser
->lexer
);
5296 if (parser
->local_variables_forbidden_p
)
5298 error_at (token
->location
,
5299 "%<this%> may not be used in this context");
5300 return error_mark_node
;
5302 /* Pointers cannot appear in constant-expressions. */
5303 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5304 return error_mark_node
;
5305 return cp_expr (finish_this_expr (), token
->location
);
5307 /* The `operator' keyword can be the beginning of an
5312 case RID_FUNCTION_NAME
:
5313 case RID_PRETTY_FUNCTION_NAME
:
5314 case RID_C99_FUNCTION_NAME
:
5316 non_integral_constant name
;
5318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5319 __func__ are the names of variables -- but they are
5320 treated specially. Therefore, they are handled here,
5321 rather than relying on the generic id-expression logic
5322 below. Grammatically, these names are id-expressions.
5324 Consume the token. */
5325 token
= cp_lexer_consume_token (parser
->lexer
);
5327 switch (token
->keyword
)
5329 case RID_FUNCTION_NAME
:
5330 name
= NIC_FUNC_NAME
;
5332 case RID_PRETTY_FUNCTION_NAME
:
5333 name
= NIC_PRETTY_FUNC
;
5335 case RID_C99_FUNCTION_NAME
:
5336 name
= NIC_C99_FUNC
;
5342 if (cp_parser_non_integral_constant_expression (parser
, name
))
5343 return error_mark_node
;
5345 /* Look up the name. */
5346 return finish_fname (token
->u
.value
);
5353 source_location type_location
;
5354 location_t start_loc
5355 = cp_lexer_peek_token (parser
->lexer
)->location
;
5356 /* The `__builtin_va_arg' construct is used to handle
5357 `va_arg'. Consume the `__builtin_va_arg' token. */
5358 cp_lexer_consume_token (parser
->lexer
);
5359 /* Look for the opening `('. */
5360 matching_parens parens
;
5361 parens
.require_open (parser
);
5362 /* Now, parse the assignment-expression. */
5363 expression
= cp_parser_assignment_expression (parser
);
5364 /* Look for the `,'. */
5365 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5366 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5367 /* Parse the type-id. */
5369 type_id_in_expr_sentinel
s (parser
);
5370 type
= cp_parser_type_id (parser
);
5372 /* Look for the closing `)'. */
5373 location_t finish_loc
5374 = cp_lexer_peek_token (parser
->lexer
)->location
;
5375 parens
.require_close (parser
);
5376 /* Using `va_arg' in a constant-expression is not
5378 if (cp_parser_non_integral_constant_expression (parser
,
5380 return error_mark_node
;
5381 /* Construct a location of the form:
5382 __builtin_va_arg (v, int)
5383 ~~~~~~~~~~~~~~~~~~~~~^~~~
5384 with the caret at the type, ranging from the start of the
5385 "__builtin_va_arg" token to the close paren. */
5386 location_t combined_loc
5387 = make_location (type_location
, start_loc
, finish_loc
);
5388 return build_x_va_arg (combined_loc
, expression
, type
);
5392 return cp_parser_builtin_offsetof (parser
);
5394 case RID_HAS_NOTHROW_ASSIGN
:
5395 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5396 case RID_HAS_NOTHROW_COPY
:
5397 case RID_HAS_TRIVIAL_ASSIGN
:
5398 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5399 case RID_HAS_TRIVIAL_COPY
:
5400 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5401 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5402 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5403 case RID_IS_ABSTRACT
:
5404 case RID_IS_AGGREGATE
:
5405 case RID_IS_BASE_OF
:
5410 case RID_IS_LITERAL_TYPE
:
5412 case RID_IS_POLYMORPHIC
:
5413 case RID_IS_SAME_AS
:
5414 case RID_IS_STD_LAYOUT
:
5415 case RID_IS_TRIVIAL
:
5416 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5417 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5418 case RID_IS_TRIVIALLY_COPYABLE
:
5420 case RID_IS_ASSIGNABLE
:
5421 case RID_IS_CONSTRUCTIBLE
:
5422 return cp_parser_trait_expr (parser
, token
->keyword
);
5426 return cp_parser_requires_expression (parser
);
5428 /* Objective-C++ expressions. */
5430 case RID_AT_PROTOCOL
:
5431 case RID_AT_SELECTOR
:
5432 return cp_parser_objc_expression (parser
);
5435 if (parser
->in_function_body
5436 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5439 error_at (token
->location
,
5440 "a template declaration cannot appear at block scope");
5441 cp_parser_skip_to_end_of_block_or_statement (parser
);
5442 return error_mark_node
;
5446 cp_parser_error (parser
, "expected primary-expression");
5447 return error_mark_node
;
5450 /* An id-expression can start with either an identifier, a
5451 `::' as the beginning of a qualified-id, or the "operator"
5455 case CPP_TEMPLATE_ID
:
5456 case CPP_NESTED_NAME_SPECIFIER
:
5459 cp_expr id_expression
;
5461 const char *error_msg
;
5464 cp_token
*id_expr_token
;
5466 /* Parse the id-expression. */
5468 = cp_parser_id_expression (parser
,
5469 /*template_keyword_p=*/false,
5470 /*check_dependency_p=*/true,
5472 /*declarator_p=*/false,
5473 /*optional_p=*/false);
5474 if (id_expression
== error_mark_node
)
5475 return error_mark_node
;
5476 id_expr_token
= token
;
5477 token
= cp_lexer_peek_token (parser
->lexer
);
5478 done
= (token
->type
!= CPP_OPEN_SQUARE
5479 && token
->type
!= CPP_OPEN_PAREN
5480 && token
->type
!= CPP_DOT
5481 && token
->type
!= CPP_DEREF
5482 && token
->type
!= CPP_PLUS_PLUS
5483 && token
->type
!= CPP_MINUS_MINUS
);
5484 /* If we have a template-id, then no further lookup is
5485 required. If the template-id was for a template-class, we
5486 will sometimes have a TYPE_DECL at this point. */
5487 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5488 || TREE_CODE (id_expression
) == TYPE_DECL
)
5489 decl
= id_expression
;
5490 /* Look up the name. */
5493 tree ambiguous_decls
;
5495 /* If we already know that this lookup is ambiguous, then
5496 we've already issued an error message; there's no reason
5498 if (id_expr_token
->type
== CPP_NAME
5499 && id_expr_token
->error_reported
)
5501 cp_parser_simulate_error (parser
);
5502 return error_mark_node
;
5505 decl
= cp_parser_lookup_name (parser
, id_expression
,
5508 /*is_namespace=*/false,
5509 /*check_dependency=*/true,
5511 id_expr_token
->location
);
5512 /* If the lookup was ambiguous, an error will already have
5514 if (ambiguous_decls
)
5515 return error_mark_node
;
5517 /* In Objective-C++, we may have an Objective-C 2.0
5518 dot-syntax for classes here. */
5519 if (c_dialect_objc ()
5520 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5521 && TREE_CODE (decl
) == TYPE_DECL
5522 && objc_is_class_name (decl
))
5525 cp_lexer_consume_token (parser
->lexer
);
5526 component
= cp_parser_identifier (parser
);
5527 if (component
== error_mark_node
)
5528 return error_mark_node
;
5530 tree result
= objc_build_class_component_ref (id_expression
,
5532 /* Build a location of the form:
5535 with caret at the start of the component name (at
5536 input_location), ranging from the start of the id_expression
5537 to the end of the component name. */
5538 location_t combined_loc
5539 = make_location (input_location
, id_expression
.get_start (),
5540 get_finish (input_location
));
5541 protected_set_expr_location (result
, combined_loc
);
5545 /* In Objective-C++, an instance variable (ivar) may be preferred
5546 to whatever cp_parser_lookup_name() found.
5547 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5548 rest of c-family, we have to do a little extra work to preserve
5549 any location information in cp_expr "decl". Given that
5550 objc_lookup_ivar is implemented in "c-family" and "objc", we
5551 have a trip through the pure "tree" type, rather than cp_expr.
5552 Naively copying it back to "decl" would implicitly give the
5553 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5554 store an EXPR_LOCATION. Hence we only update "decl" (and
5555 hence its location_t) if we get back a different tree node. */
5556 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5558 if (decl_tree
!= decl
.get_value ())
5559 decl
= cp_expr (decl_tree
);
5561 /* If name lookup gives us a SCOPE_REF, then the
5562 qualifying scope was dependent. */
5563 if (TREE_CODE (decl
) == SCOPE_REF
)
5565 /* At this point, we do not know if DECL is a valid
5566 integral constant expression. We assume that it is
5567 in fact such an expression, so that code like:
5569 template <int N> struct A {
5573 is accepted. At template-instantiation time, we
5574 will check that B<N>::i is actually a constant. */
5577 /* Check to see if DECL is a local variable in a context
5578 where that is forbidden. */
5579 if (parser
->local_variables_forbidden_p
5580 && local_variable_p (decl
))
5582 /* It might be that we only found DECL because we are
5583 trying to be generous with pre-ISO scoping rules.
5584 For example, consider:
5588 for (int i = 0; i < 10; ++i) {}
5589 extern void f(int j = i);
5592 Here, name look up will originally find the out
5593 of scope `i'. We need to issue a warning message,
5594 but then use the global `i'. */
5595 decl
= check_for_out_of_scope_variable (decl
);
5596 if (local_variable_p (decl
))
5598 error_at (id_expr_token
->location
,
5599 "local variable %qD may not appear in this context",
5601 return error_mark_node
;
5606 decl
= (finish_id_expression
5607 (id_expression
, decl
, parser
->scope
,
5609 parser
->integral_constant_expression_p
,
5610 parser
->allow_non_integral_constant_expression_p
,
5611 &parser
->non_integral_constant_expression_p
,
5612 template_p
, done
, address_p
,
5615 id_expression
.get_location ()));
5617 cp_parser_error (parser
, error_msg
);
5618 decl
.set_location (id_expr_token
->location
);
5622 /* Anything else is an error. */
5624 cp_parser_error (parser
, "expected primary-expression");
5625 return error_mark_node
;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser
*parser
,
5633 bool template_arg_p
,
5636 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5637 /*decltype*/false, idk
);
5640 /* Parse an id-expression.
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5649 :: operator-function-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5675 cp_parser_id_expression (cp_parser
*parser
,
5676 bool template_keyword_p
,
5677 bool check_dependency_p
,
5682 bool global_scope_p
;
5683 bool nested_name_specifier_p
;
5685 /* Assume the `template' keyword was not used. */
5687 *template_p
= template_keyword_p
;
5689 /* Look for the optional `::' operator. */
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser
,
5693 /*current_scope_valid_p=*/false)
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser
,
5699 /*typename_keyword_p=*/false,
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p
)
5711 tree saved_object_scope
;
5712 tree saved_qualifying_scope
;
5713 cp_expr unqualified_id
;
5716 /* See if the next token is the `template' keyword. */
5718 template_p
= &is_template
;
5719 *template_p
= cp_parser_optional_template_keyword (parser
);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope
= parser
->scope
;
5723 saved_object_scope
= parser
->object_scope
;
5724 saved_qualifying_scope
= parser
->qualifying_scope
;
5725 /* Process the final unqualified-id. */
5726 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser
->scope
= saved_scope
;
5732 parser
->object_scope
= saved_object_scope
;
5733 parser
->qualifying_scope
= saved_qualifying_scope
;
5735 return unqualified_id
;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p
)
5744 /* Peek at the next token. */
5745 token
= cp_lexer_peek_token (parser
->lexer
);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token
->type
== CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5753 return cp_parser_identifier (parser
);
5755 cp_parser_parse_tentatively (parser
);
5756 /* Try a template-id. */
5757 id
= cp_parser_template_id (parser
,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser
))
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token
= cp_lexer_peek_token (parser
->lexer
);
5770 switch (token
->type
)
5773 return cp_parser_identifier (parser
);
5776 if (token
->keyword
== RID_OPERATOR
)
5777 return cp_parser_operator_function_id (parser
);
5781 cp_parser_error (parser
, "expected id-expression");
5782 return error_mark_node
;
5786 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5787 /*check_dependency_p=*/true,
5792 /* Parse an unqualified-id.
5796 operator-function-id
5797 conversion-function-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5815 cp_parser_unqualified_id (cp_parser
* parser
,
5816 bool template_keyword_p
,
5817 bool check_dependency_p
,
5823 /* Peek at the next token. */
5824 token
= cp_lexer_peek_token (parser
->lexer
);
5826 switch ((int) token
->type
)
5832 /* We don't know yet whether or not this will be a
5834 cp_parser_parse_tentatively (parser
);
5835 /* Try a template-id. */
5836 id
= cp_parser_template_id (parser
, template_keyword_p
,
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser
))
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser
);
5847 case CPP_TEMPLATE_ID
:
5848 return cp_parser_template_id (parser
, template_keyword_p
,
5856 tree qualifying_scope
;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser
->lexer
);
5863 /* Parse the class-name. The standard, as written, seems to
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope
= parser
->scope
;
5899 object_scope
= parser
->object_scope
;
5900 qualifying_scope
= parser
->qualifying_scope
;
5902 /* Check for invalid scopes. */
5903 if (scope
== error_mark_node
)
5905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5906 cp_lexer_consume_token (parser
->lexer
);
5907 return error_mark_node
;
5909 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5912 error_at (token
->location
,
5913 "scope %qT before %<~%> is not a class-name",
5915 cp_parser_simulate_error (parser
);
5916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5917 cp_lexer_consume_token (parser
->lexer
);
5918 return error_mark_node
;
5920 gcc_assert (!scope
|| TYPE_P (scope
));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5924 token
= cp_lexer_peek_token (parser
->lexer
);
5926 && token
->type
== CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5929 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5930 || (CLASS_TYPE_P (scope
)
5931 && constructor_name_p (token
->u
.value
, scope
))))
5933 cp_lexer_consume_token (parser
->lexer
);
5934 return build_nt (BIT_NOT_EXPR
, scope
);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token
, RID_AUTO
))
5940 if (cxx_dialect
< cxx14
)
5941 pedwarn (input_location
, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser
->lexer
);
5945 return build_nt (BIT_NOT_EXPR
, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5955 type_decl
= NULL_TREE
;
5958 cp_parser_parse_tentatively (parser
);
5959 type_decl
= cp_parser_class_name (parser
,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5966 if (cp_parser_parse_definitely (parser
))
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done
&& scope
&& qualifying_scope
)
5972 cp_parser_parse_tentatively (parser
);
5973 parser
->scope
= qualifying_scope
;
5974 parser
->object_scope
= NULL_TREE
;
5975 parser
->qualifying_scope
= NULL_TREE
;
5977 = cp_parser_class_name (parser
,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5984 if (cp_parser_parse_definitely (parser
))
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done
&& object_scope
)
5990 cp_parser_parse_tentatively (parser
);
5991 parser
->scope
= object_scope
;
5992 parser
->object_scope
= NULL_TREE
;
5993 parser
->qualifying_scope
= NULL_TREE
;
5995 = cp_parser_class_name (parser
,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6002 if (cp_parser_parse_definitely (parser
))
6005 /* Look in the surrounding context. */
6008 parser
->scope
= NULL_TREE
;
6009 parser
->object_scope
= NULL_TREE
;
6010 parser
->qualifying_scope
= NULL_TREE
;
6011 if (processing_template_decl
)
6012 cp_parser_parse_tentatively (parser
);
6014 = cp_parser_class_name (parser
,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser
))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6028 cp_parser_simulate_error (parser
);
6029 return error_mark_node
;
6031 /* Otherwise, accept it and check for a match at instantiation
6033 type_decl
= cp_parser_identifier (parser
);
6034 if (type_decl
!= error_mark_node
)
6035 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl
== error_mark_node
&& scope
)
6044 return build_nt (BIT_NOT_EXPR
, scope
);
6045 else if (type_decl
== error_mark_node
)
6046 return error_mark_node
;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6052 error_at (token
->location
,
6053 "declaration of %<~%T%> as member of %qT",
6055 cp_parser_simulate_error (parser
);
6056 return error_mark_node
;
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6065 && !DECL_SELF_REFERENCE_P (type_decl
)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6067 error_at (token
->location
,
6068 "typedef-name %qD used as destructor declarator",
6071 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6075 if (token
->keyword
== RID_OPERATOR
)
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser
);
6081 /* Try a template-id. */
6082 id
= cp_parser_template_id (parser
, template_keyword_p
,
6083 /*check_dependency_p=*/true,
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser
))
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser
);
6092 /* Try an operator-function-id. */
6093 id
= cp_parser_operator_function_id (parser
);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser
))
6096 id
= cp_parser_conversion_function_id (parser
);
6097 else if (UDLIT_OPER_P (id
))
6100 const char *name
= UDLIT_OP_SUFFIX (id
);
6101 if (name
[0] != '_' && !in_system_header_at (input_location
)
6103 warning (OPT_Wliteral_suffix
,
6104 "literal operator suffixes not preceded by %<_%>"
6105 " are reserved for future standardization");
6115 cp_parser_error (parser
, "expected unqualified-id");
6116 return error_mark_node
;
6120 /* Parse an (optional) nested-name-specifier.
6122 nested-name-specifier: [C++98]
6123 class-or-namespace-name :: nested-name-specifier [opt]
6124 class-or-namespace-name :: template nested-name-specifier [opt]
6126 nested-name-specifier: [C++0x]
6129 nested-name-specifier identifier ::
6130 nested-name-specifier template [opt] simple-template-id ::
6132 PARSER->SCOPE should be set appropriately before this function is
6133 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6134 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6137 Sets PARSER->SCOPE to the class (TYPE) or namespace
6138 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6139 it unchanged if there is no nested-name-specifier. Returns the new
6140 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6142 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6143 part of a declaration and/or decl-specifier. */
6146 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6147 bool typename_keyword_p
,
6148 bool check_dependency_p
,
6150 bool is_declaration
,
6151 bool template_keyword_p
/* = false */)
6153 bool success
= false;
6154 cp_token_position start
= 0;
6157 /* Remember where the nested-name-specifier starts. */
6158 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6160 start
= cp_lexer_token_position (parser
->lexer
, false);
6161 push_deferring_access_checks (dk_deferred
);
6168 tree saved_qualifying_scope
;
6170 /* Spot cases that cannot be the beginning of a
6171 nested-name-specifier. */
6172 token
= cp_lexer_peek_token (parser
->lexer
);
6174 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6175 the already parsed nested-name-specifier. */
6176 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6178 /* Grab the nested-name-specifier and continue the loop. */
6179 cp_parser_pre_parsed_nested_name_specifier (parser
);
6180 /* If we originally encountered this nested-name-specifier
6181 with IS_DECLARATION set to false, we will not have
6182 resolved TYPENAME_TYPEs, so we must do so here. */
6184 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6186 new_scope
= resolve_typename_type (parser
->scope
,
6187 /*only_current_p=*/false);
6188 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6189 parser
->scope
= new_scope
;
6195 /* Spot cases that cannot be the beginning of a
6196 nested-name-specifier. On the second and subsequent times
6197 through the loop, we look for the `template' keyword. */
6198 if (success
&& token
->keyword
== RID_TEMPLATE
)
6200 /* A template-id can start a nested-name-specifier. */
6201 else if (token
->type
== CPP_TEMPLATE_ID
)
6203 /* DR 743: decltype can be used in a nested-name-specifier. */
6204 else if (token_is_decltype (token
))
6208 /* If the next token is not an identifier, then it is
6209 definitely not a type-name or namespace-name. */
6210 if (token
->type
!= CPP_NAME
)
6212 /* If the following token is neither a `<' (to begin a
6213 template-id), nor a `::', then we are not looking at a
6214 nested-name-specifier. */
6215 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6217 if (token
->type
== CPP_COLON
6218 && parser
->colon_corrects_to_scope_p
6219 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6221 gcc_rich_location
richloc (token
->location
);
6222 richloc
.add_fixit_replace ("::");
6224 "found %<:%> in nested-name-specifier, "
6226 token
->type
= CPP_SCOPE
;
6229 if (token
->type
!= CPP_SCOPE
6230 && !cp_parser_nth_token_starts_template_argument_list_p
6235 /* The nested-name-specifier is optional, so we parse
6237 cp_parser_parse_tentatively (parser
);
6239 /* Look for the optional `template' keyword, if this isn't the
6240 first time through the loop. */
6242 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6244 /* Save the old scope since the name lookup we are about to do
6245 might destroy it. */
6246 old_scope
= parser
->scope
;
6247 saved_qualifying_scope
= parser
->qualifying_scope
;
6248 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6249 look up names in "X<T>::I" in order to determine that "Y" is
6250 a template. So, if we have a typename at this point, we make
6251 an effort to look through it. */
6253 && !typename_keyword_p
6255 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6256 parser
->scope
= resolve_typename_type (parser
->scope
,
6257 /*only_current_p=*/false);
6258 /* Parse the qualifying entity. */
6260 = cp_parser_qualifying_entity (parser
,
6266 /* Look for the `::' token. */
6267 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6269 /* If we found what we wanted, we keep going; otherwise, we're
6271 if (!cp_parser_parse_definitely (parser
))
6273 bool error_p
= false;
6275 /* Restore the OLD_SCOPE since it was valid before the
6276 failed attempt at finding the last
6277 class-or-namespace-name. */
6278 parser
->scope
= old_scope
;
6279 parser
->qualifying_scope
= saved_qualifying_scope
;
6281 /* If the next token is a decltype, and the one after that is a
6282 `::', then the decltype has failed to resolve to a class or
6283 enumeration type. Give this error even when parsing
6284 tentatively since it can't possibly be valid--and we're going
6285 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6286 won't get another chance.*/
6287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6288 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6291 token
= cp_lexer_consume_token (parser
->lexer
);
6292 error_at (token
->location
, "decltype evaluates to %qT, "
6293 "which is not a class or enumeration type",
6294 token
->u
.tree_check_value
->value
);
6295 parser
->scope
= error_mark_node
;
6299 cp_lexer_consume_token (parser
->lexer
);
6302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6303 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6305 /* If we have a non-type template-id followed by ::, it can't
6306 possibly be valid. */
6307 token
= cp_lexer_peek_token (parser
->lexer
);
6308 tree tid
= token
->u
.tree_check_value
->value
;
6309 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6310 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6312 tree tmpl
= NULL_TREE
;
6313 if (is_overloaded_fn (tid
))
6315 tree fns
= get_fns (tid
);
6316 if (OVL_SINGLE_P (fns
))
6317 tmpl
= OVL_FIRST (fns
);
6318 error_at (token
->location
, "function template-id %qD "
6319 "in nested-name-specifier", tid
);
6323 /* Variable template. */
6324 tmpl
= TREE_OPERAND (tid
, 0);
6325 gcc_assert (variable_template_p (tmpl
));
6326 error_at (token
->location
, "variable template-id %qD "
6327 "in nested-name-specifier", tid
);
6330 inform (DECL_SOURCE_LOCATION (tmpl
),
6331 "%qD declared here", tmpl
);
6333 parser
->scope
= error_mark_node
;
6337 cp_lexer_consume_token (parser
->lexer
);
6338 cp_lexer_consume_token (parser
->lexer
);
6342 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6344 /* If the next token is an identifier, and the one after
6345 that is a `::', then any valid interpretation would have
6346 found a class-or-namespace-name. */
6347 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6348 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6350 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6353 token
= cp_lexer_consume_token (parser
->lexer
);
6356 if (!token
->error_reported
)
6359 tree ambiguous_decls
;
6361 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6363 /*is_template=*/false,
6364 /*is_namespace=*/false,
6365 /*check_dependency=*/true,
6368 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6369 error_at (token
->location
,
6370 "%qD used without template parameters",
6372 else if (ambiguous_decls
)
6374 // cp_parser_lookup_name has the same diagnostic,
6375 // thus make sure to emit it at most once.
6376 if (cp_parser_uncommitted_to_tentative_parse_p
6379 error_at (token
->location
,
6380 "reference to %qD is ambiguous",
6382 print_candidates (ambiguous_decls
);
6384 decl
= error_mark_node
;
6388 if (cxx_dialect
!= cxx98
)
6389 cp_parser_name_lookup_error
6390 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6393 cp_parser_name_lookup_error
6394 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6398 parser
->scope
= error_mark_node
;
6400 /* Treat this as a successful nested-name-specifier
6405 If the name found is not a class-name (clause
6406 _class_) or namespace-name (_namespace.def_), the
6407 program is ill-formed. */
6410 cp_lexer_consume_token (parser
->lexer
);
6414 /* We've found one valid nested-name-specifier. */
6416 /* Name lookup always gives us a DECL. */
6417 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6418 new_scope
= TREE_TYPE (new_scope
);
6419 /* Uses of "template" must be followed by actual templates. */
6420 if (template_keyword_p
6421 && !(CLASS_TYPE_P (new_scope
)
6422 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6423 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6424 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6425 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6426 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6427 == TEMPLATE_ID_EXPR
)))
6428 permerror (input_location
, TYPE_P (new_scope
)
6429 ? G_("%qT is not a template")
6430 : G_("%qD is not a template"),
6432 /* If it is a class scope, try to complete it; we are about to
6433 be looking up names inside the class. */
6434 if (TYPE_P (new_scope
)
6435 /* Since checking types for dependency can be expensive,
6436 avoid doing it if the type is already complete. */
6437 && !COMPLETE_TYPE_P (new_scope
)
6438 /* Do not try to complete dependent types. */
6439 && !dependent_type_p (new_scope
))
6441 new_scope
= complete_type (new_scope
);
6442 /* If it is a typedef to current class, use the current
6443 class instead, as the typedef won't have any names inside
6445 if (!COMPLETE_TYPE_P (new_scope
)
6446 && currently_open_class (new_scope
))
6447 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6449 /* Make sure we look in the right scope the next time through
6451 parser
->scope
= new_scope
;
6454 /* If parsing tentatively, replace the sequence of tokens that makes
6455 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6456 token. That way, should we re-parse the token stream, we will
6457 not have to repeat the effort required to do the parse, nor will
6458 we issue duplicate error messages. */
6459 if (success
&& start
)
6463 token
= cp_lexer_token_at (parser
->lexer
, start
);
6464 /* Reset the contents of the START token. */
6465 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6466 /* Retrieve any deferred checks. Do not pop this access checks yet
6467 so the memory will not be reclaimed during token replacing below. */
6468 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6469 token
->u
.tree_check_value
->value
= parser
->scope
;
6470 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6471 token
->u
.tree_check_value
->qualifying_scope
=
6472 parser
->qualifying_scope
;
6473 token
->keyword
= RID_MAX
;
6475 /* Purge all subsequent tokens. */
6476 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6480 pop_to_parent_deferring_access_checks ();
6482 return success
? parser
->scope
: NULL_TREE
;
6485 /* Parse a nested-name-specifier. See
6486 cp_parser_nested_name_specifier_opt for details. This function
6487 behaves identically, except that it will an issue an error if no
6488 nested-name-specifier is present. */
6491 cp_parser_nested_name_specifier (cp_parser
*parser
,
6492 bool typename_keyword_p
,
6493 bool check_dependency_p
,
6495 bool is_declaration
)
6499 /* Look for the nested-name-specifier. */
6500 scope
= cp_parser_nested_name_specifier_opt (parser
,
6505 /* If it was not present, issue an error message. */
6508 cp_parser_error (parser
, "expected nested-name-specifier");
6509 parser
->scope
= NULL_TREE
;
6515 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6516 this is either a class-name or a namespace-name (which corresponds
6517 to the class-or-namespace-name production in the grammar). For
6518 C++0x, it can also be a type-name that refers to an enumeration
6519 type or a simple-template-id.
6521 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6522 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6523 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6524 TYPE_P is TRUE iff the next name should be taken as a class-name,
6525 even the same name is declared to be another entity in the same
6528 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6529 specified by the class-or-namespace-name. If neither is found the
6530 ERROR_MARK_NODE is returned. */
6533 cp_parser_qualifying_entity (cp_parser
*parser
,
6534 bool typename_keyword_p
,
6535 bool template_keyword_p
,
6536 bool check_dependency_p
,
6538 bool is_declaration
)
6541 tree saved_qualifying_scope
;
6542 tree saved_object_scope
;
6545 bool successful_parse_p
;
6547 /* DR 743: decltype can appear in a nested-name-specifier. */
6548 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6550 scope
= cp_parser_decltype (parser
);
6551 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6552 && !MAYBE_CLASS_TYPE_P (scope
))
6554 cp_parser_simulate_error (parser
);
6555 return error_mark_node
;
6557 if (TYPE_NAME (scope
))
6558 scope
= TYPE_NAME (scope
);
6562 /* Before we try to parse the class-name, we must save away the
6563 current PARSER->SCOPE since cp_parser_class_name will destroy
6565 saved_scope
= parser
->scope
;
6566 saved_qualifying_scope
= parser
->qualifying_scope
;
6567 saved_object_scope
= parser
->object_scope
;
6568 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6569 there is no need to look for a namespace-name. */
6570 only_class_p
= template_keyword_p
6571 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6573 cp_parser_parse_tentatively (parser
);
6574 scope
= cp_parser_class_name (parser
,
6577 type_p
? class_type
: none_type
,
6579 /*class_head_p=*/false,
6581 /*enum_ok=*/cxx_dialect
> cxx98
);
6582 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6583 /* If that didn't work, try for a namespace-name. */
6584 if (!only_class_p
&& !successful_parse_p
)
6586 /* Restore the saved scope. */
6587 parser
->scope
= saved_scope
;
6588 parser
->qualifying_scope
= saved_qualifying_scope
;
6589 parser
->object_scope
= saved_object_scope
;
6590 /* If we are not looking at an identifier followed by the scope
6591 resolution operator, then this is not part of a
6592 nested-name-specifier. (Note that this function is only used
6593 to parse the components of a nested-name-specifier.) */
6594 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6595 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6596 return error_mark_node
;
6597 scope
= cp_parser_namespace_name (parser
);
6603 /* Return true if we are looking at a compound-literal, false otherwise. */
6606 cp_parser_compound_literal_p (cp_parser
*parser
)
6608 cp_lexer_save_tokens (parser
->lexer
);
6610 /* Skip tokens until the next token is a closing parenthesis.
6611 If we find the closing `)', and the next token is a `{', then
6612 we are looking at a compound-literal. */
6613 bool compound_literal_p
6614 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6615 /*consume_paren=*/true)
6616 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6618 /* Roll back the tokens we skipped. */
6619 cp_lexer_rollback_tokens (parser
->lexer
);
6621 return compound_literal_p
;
6624 /* Return true if EXPR is the integer constant zero or a complex constant
6625 of zero, without any folding, but ignoring location wrappers. */
6628 literal_integer_zerop (const_tree expr
)
6630 STRIP_ANY_LOCATION_WRAPPER (expr
);
6631 return integer_zerop (expr
);
6634 /* Parse a postfix-expression.
6638 postfix-expression [ expression ]
6639 postfix-expression ( expression-list [opt] )
6640 simple-type-specifier ( expression-list [opt] )
6641 typename :: [opt] nested-name-specifier identifier
6642 ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier template [opt] template-id
6644 ( expression-list [opt] )
6645 postfix-expression . template [opt] id-expression
6646 postfix-expression -> template [opt] id-expression
6647 postfix-expression . pseudo-destructor-name
6648 postfix-expression -> pseudo-destructor-name
6649 postfix-expression ++
6650 postfix-expression --
6651 dynamic_cast < type-id > ( expression )
6652 static_cast < type-id > ( expression )
6653 reinterpret_cast < type-id > ( expression )
6654 const_cast < type-id > ( expression )
6655 typeid ( expression )
6661 ( type-id ) { initializer-list , [opt] }
6663 This extension is a GNU version of the C99 compound-literal
6664 construct. (The C99 grammar uses `type-name' instead of `type-id',
6665 but they are essentially the same concept.)
6667 If ADDRESS_P is true, the postfix expression is the operand of the
6668 `&' operator. CAST_P is true if this expression is the target of a
6671 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6672 class member access expressions [expr.ref].
6674 Returns a representation of the expression. */
6677 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6678 bool member_access_only_p
, bool decltype_p
,
6679 cp_id_kind
* pidk_return
)
6684 cp_id_kind idk
= CP_ID_KIND_NONE
;
6685 cp_expr postfix_expression
= NULL_TREE
;
6686 bool is_member_access
= false;
6688 /* Peek at the next token. */
6689 token
= cp_lexer_peek_token (parser
->lexer
);
6690 loc
= token
->location
;
6691 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6693 /* Some of the productions are determined by keywords. */
6694 keyword
= token
->keyword
;
6704 const char *saved_message
;
6705 bool saved_in_type_id_in_expr_p
;
6707 /* All of these can be handled in the same way from the point
6708 of view of parsing. Begin by consuming the token
6709 identifying the cast. */
6710 cp_lexer_consume_token (parser
->lexer
);
6712 /* New types cannot be defined in the cast. */
6713 saved_message
= parser
->type_definition_forbidden_message
;
6714 parser
->type_definition_forbidden_message
6715 = G_("types may not be defined in casts");
6717 /* Look for the opening `<'. */
6718 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6719 /* Parse the type to which we are casting. */
6720 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6721 parser
->in_type_id_in_expr_p
= true;
6722 type
= cp_parser_type_id (parser
);
6723 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6724 /* Look for the closing `>'. */
6725 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6726 /* Restore the old message. */
6727 parser
->type_definition_forbidden_message
= saved_message
;
6729 bool saved_greater_than_is_operator_p
6730 = parser
->greater_than_is_operator_p
;
6731 parser
->greater_than_is_operator_p
= true;
6733 /* And the expression which is being cast. */
6734 matching_parens parens
;
6735 parens
.require_open (parser
);
6736 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6737 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6739 location_t end_loc
= close_paren
?
6740 close_paren
->location
: UNKNOWN_LOCATION
;
6742 parser
->greater_than_is_operator_p
6743 = saved_greater_than_is_operator_p
;
6745 /* Only type conversions to integral or enumeration types
6746 can be used in constant-expressions. */
6747 if (!cast_valid_in_integral_constant_expression_p (type
)
6748 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6750 postfix_expression
= error_mark_node
;
6758 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6762 = build_static_cast (type
, expression
, tf_warning_or_error
);
6766 = build_reinterpret_cast (type
, expression
,
6767 tf_warning_or_error
);
6771 = build_const_cast (type
, expression
, tf_warning_or_error
);
6777 /* Construct a location e.g. :
6778 reinterpret_cast <int *> (expr)
6779 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6780 ranging from the start of the "*_cast" token to the final closing
6781 paren, with the caret at the start. */
6782 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6783 postfix_expression
.set_location (cp_cast_loc
);
6790 const char *saved_message
;
6791 bool saved_in_type_id_in_expr_p
;
6793 /* Consume the `typeid' token. */
6794 cp_lexer_consume_token (parser
->lexer
);
6795 /* Look for the `(' token. */
6796 matching_parens parens
;
6797 parens
.require_open (parser
);
6798 /* Types cannot be defined in a `typeid' expression. */
6799 saved_message
= parser
->type_definition_forbidden_message
;
6800 parser
->type_definition_forbidden_message
6801 = G_("types may not be defined in a %<typeid%> expression");
6802 /* We can't be sure yet whether we're looking at a type-id or an
6804 cp_parser_parse_tentatively (parser
);
6805 /* Try a type-id first. */
6806 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6807 parser
->in_type_id_in_expr_p
= true;
6808 type
= cp_parser_type_id (parser
);
6809 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6810 /* Look for the `)' token. Otherwise, we can't be sure that
6811 we're not looking at an expression: consider `typeid (int
6812 (3))', for example. */
6813 cp_token
*close_paren
= parens
.require_close (parser
);
6814 /* If all went well, simply lookup the type-id. */
6815 if (cp_parser_parse_definitely (parser
))
6816 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6817 /* Otherwise, fall back to the expression variant. */
6822 /* Look for an expression. */
6823 expression
= cp_parser_expression (parser
, & idk
);
6824 /* Compute its typeid. */
6825 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6826 /* Look for the `)' token. */
6827 close_paren
= parens
.require_close (parser
);
6829 /* Restore the saved message. */
6830 parser
->type_definition_forbidden_message
= saved_message
;
6831 /* `typeid' may not appear in an integral constant expression. */
6832 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6833 postfix_expression
= error_mark_node
;
6835 /* Construct a location e.g. :
6838 ranging from the start of the "typeid" token to the final closing
6839 paren, with the caret at the start. */
6842 location_t typeid_loc
6843 = make_location (start_loc
, start_loc
, close_paren
->location
);
6844 postfix_expression
.set_location (typeid_loc
);
6845 postfix_expression
.maybe_add_location_wrapper ();
6853 /* The syntax permitted here is the same permitted for an
6854 elaborated-type-specifier. */
6855 ++parser
->prevent_constrained_type_specifiers
;
6856 type
= cp_parser_elaborated_type_specifier (parser
,
6857 /*is_friend=*/false,
6858 /*is_declaration=*/false);
6859 --parser
->prevent_constrained_type_specifiers
;
6860 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6865 case RID_BUILTIN_SHUFFLE
:
6866 case RID_BUILTIN_LAUNDER
:
6868 vec
<tree
, va_gc
> *vec
;
6872 cp_lexer_consume_token (parser
->lexer
);
6873 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6874 /*cast_p=*/false, /*allow_expansion_p=*/true,
6875 /*non_constant_p=*/NULL
);
6878 postfix_expression
= error_mark_node
;
6882 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6888 if (vec
->length () == 1)
6890 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6893 error_at (loc
, "wrong number of arguments to "
6894 "%<__builtin_addressof%>");
6895 postfix_expression
= error_mark_node
;
6899 case RID_BUILTIN_LAUNDER
:
6900 if (vec
->length () == 1)
6901 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6902 tf_warning_or_error
);
6905 error_at (loc
, "wrong number of arguments to "
6906 "%<__builtin_launder%>");
6907 postfix_expression
= error_mark_node
;
6911 case RID_BUILTIN_SHUFFLE
:
6912 if (vec
->length () == 2)
6914 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6915 (*vec
)[1], tf_warning_or_error
);
6916 else if (vec
->length () == 3)
6918 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6919 (*vec
)[2], tf_warning_or_error
);
6922 error_at (loc
, "wrong number of arguments to "
6923 "%<__builtin_shuffle%>");
6924 postfix_expression
= error_mark_node
;
6938 /* If the next thing is a simple-type-specifier, we may be
6939 looking at a functional cast. We could also be looking at
6940 an id-expression. So, we try the functional cast, and if
6941 that doesn't work we fall back to the primary-expression. */
6942 cp_parser_parse_tentatively (parser
);
6943 /* Look for the simple-type-specifier. */
6944 ++parser
->prevent_constrained_type_specifiers
;
6945 type
= cp_parser_simple_type_specifier (parser
,
6946 /*decl_specs=*/NULL
,
6947 CP_PARSER_FLAGS_NONE
);
6948 --parser
->prevent_constrained_type_specifiers
;
6949 /* Parse the cast itself. */
6950 if (!cp_parser_error_occurred (parser
))
6952 = cp_parser_functional_cast (parser
, type
);
6953 /* If that worked, we're done. */
6954 if (cp_parser_parse_definitely (parser
))
6957 /* If the functional-cast didn't work out, try a
6958 compound-literal. */
6959 if (cp_parser_allow_gnu_extensions_p (parser
)
6960 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6962 cp_expr initializer
= NULL_TREE
;
6964 cp_parser_parse_tentatively (parser
);
6966 matching_parens parens
;
6967 parens
.consume_open (parser
);
6969 /* Avoid calling cp_parser_type_id pointlessly, see comment
6970 in cp_parser_cast_expression about c++/29234. */
6971 if (!cp_parser_compound_literal_p (parser
))
6972 cp_parser_simulate_error (parser
);
6975 /* Parse the type. */
6976 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6977 parser
->in_type_id_in_expr_p
= true;
6978 type
= cp_parser_type_id (parser
);
6979 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6980 parens
.require_close (parser
);
6983 /* If things aren't going well, there's no need to
6985 if (!cp_parser_error_occurred (parser
))
6987 bool non_constant_p
;
6988 /* Parse the brace-enclosed initializer list. */
6989 initializer
= cp_parser_braced_list (parser
,
6992 /* If that worked, we're definitely looking at a
6993 compound-literal expression. */
6994 if (cp_parser_parse_definitely (parser
))
6996 /* Warn the user that a compound literal is not
6997 allowed in standard C++. */
6998 pedwarn (input_location
, OPT_Wpedantic
,
6999 "ISO C++ forbids compound-literals");
7000 /* For simplicity, we disallow compound literals in
7001 constant-expressions. We could
7002 allow compound literals of integer type, whose
7003 initializer was a constant, in constant
7004 expressions. Permitting that usage, as a further
7005 extension, would not change the meaning of any
7006 currently accepted programs. (Of course, as
7007 compound literals are not part of ISO C++, the
7008 standard has nothing to say.) */
7009 if (cp_parser_non_integral_constant_expression (parser
,
7012 postfix_expression
= error_mark_node
;
7015 /* Form the representation of the compound-literal. */
7017 = finish_compound_literal (type
, initializer
,
7018 tf_warning_or_error
, fcl_c99
);
7019 postfix_expression
.set_location (initializer
.get_location ());
7024 /* It must be a primary-expression. */
7026 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7027 /*template_arg_p=*/false,
7034 /* Note that we don't need to worry about calling build_cplus_new on a
7035 class-valued CALL_EXPR in decltype when it isn't the end of the
7036 postfix-expression; unary_complex_lvalue will take care of that for
7039 /* Keep looping until the postfix-expression is complete. */
7042 if (idk
== CP_ID_KIND_UNQUALIFIED
7043 && identifier_p (postfix_expression
)
7044 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7045 /* It is not a Koenig lookup function call. */
7047 = unqualified_name_lookup_error (postfix_expression
);
7049 /* Peek at the next token. */
7050 token
= cp_lexer_peek_token (parser
->lexer
);
7052 switch (token
->type
)
7054 case CPP_OPEN_SQUARE
:
7055 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7057 cp_parser_error (parser
,
7058 "two consecutive %<[%> shall "
7059 "only introduce an attribute");
7060 return error_mark_node
;
7063 = cp_parser_postfix_open_square_expression (parser
,
7067 postfix_expression
.set_range (start_loc
,
7068 postfix_expression
.get_location ());
7070 idk
= CP_ID_KIND_NONE
;
7071 is_member_access
= false;
7074 case CPP_OPEN_PAREN
:
7075 /* postfix-expression ( expression-list [opt] ) */
7078 bool is_builtin_constant_p
;
7079 bool saved_integral_constant_expression_p
= false;
7080 bool saved_non_integral_constant_expression_p
= false;
7081 tsubst_flags_t complain
= complain_flags (decltype_p
);
7082 vec
<tree
, va_gc
> *args
;
7083 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7085 is_member_access
= false;
7087 is_builtin_constant_p
7088 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7089 if (is_builtin_constant_p
)
7091 /* The whole point of __builtin_constant_p is to allow
7092 non-constant expressions to appear as arguments. */
7093 saved_integral_constant_expression_p
7094 = parser
->integral_constant_expression_p
;
7095 saved_non_integral_constant_expression_p
7096 = parser
->non_integral_constant_expression_p
;
7097 parser
->integral_constant_expression_p
= false;
7099 args
= (cp_parser_parenthesized_expression_list
7101 /*cast_p=*/false, /*allow_expansion_p=*/true,
7102 /*non_constant_p=*/NULL
,
7103 /*close_paren_loc=*/&close_paren_loc
,
7104 /*wrap_locations_p=*/true));
7105 if (is_builtin_constant_p
)
7107 parser
->integral_constant_expression_p
7108 = saved_integral_constant_expression_p
;
7109 parser
->non_integral_constant_expression_p
7110 = saved_non_integral_constant_expression_p
;
7115 postfix_expression
= error_mark_node
;
7119 /* Function calls are not permitted in
7120 constant-expressions. */
7121 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7122 && cp_parser_non_integral_constant_expression (parser
,
7125 postfix_expression
= error_mark_node
;
7126 release_tree_vector (args
);
7131 if (idk
== CP_ID_KIND_UNQUALIFIED
7132 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7134 if (identifier_p (postfix_expression
))
7136 if (!args
->is_empty ())
7139 if (!any_type_dependent_arguments_p (args
))
7141 = perform_koenig_lookup (postfix_expression
, args
,
7146 = unqualified_fn_lookup_error (postfix_expression
);
7148 /* We do not perform argument-dependent lookup if
7149 normal lookup finds a non-function, in accordance
7150 with the expected resolution of DR 218. */
7151 else if (!args
->is_empty ()
7152 && is_overloaded_fn (postfix_expression
))
7154 tree fn
= get_first_fn (postfix_expression
);
7155 fn
= STRIP_TEMPLATE (fn
);
7157 /* Do not do argument dependent lookup if regular
7158 lookup finds a member function or a block-scope
7159 function declaration. [basic.lookup.argdep]/3 */
7160 if (!DECL_FUNCTION_MEMBER_P (fn
)
7161 && !DECL_LOCAL_FUNCTION_P (fn
))
7164 if (!any_type_dependent_arguments_p (args
))
7166 = perform_koenig_lookup (postfix_expression
, args
,
7172 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7173 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7174 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7175 && vec_safe_length (args
) == 3)
7177 tree arg0
= (*args
)[0];
7178 tree arg1
= (*args
)[1];
7179 tree arg2
= (*args
)[2];
7180 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7181 | (literal_integer_zerop (arg2
) << 2));
7182 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7185 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7187 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7188 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7190 if (processing_template_decl
7191 && (type_dependent_object_expression_p (instance
)
7192 || (!BASELINK_P (fn
)
7193 && TREE_CODE (fn
) != FIELD_DECL
)
7194 || type_dependent_expression_p (fn
)
7195 || any_type_dependent_arguments_p (args
)))
7197 maybe_generic_this_capture (instance
, fn
);
7199 = build_min_nt_call_vec (postfix_expression
, args
);
7200 release_tree_vector (args
);
7204 if (BASELINK_P (fn
))
7207 = (build_new_method_call
7208 (instance
, fn
, &args
, NULL_TREE
,
7209 (idk
== CP_ID_KIND_QUALIFIED
7210 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7217 = finish_call_expr (postfix_expression
, &args
,
7218 /*disallow_virtual=*/false,
7222 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7223 || TREE_CODE (postfix_expression
) == MEMBER_REF
7224 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7225 postfix_expression
= (build_offset_ref_call_from_tree
7226 (postfix_expression
, &args
,
7228 else if (idk
== CP_ID_KIND_QUALIFIED
)
7229 /* A call to a static class member, or a namespace-scope
7232 = finish_call_expr (postfix_expression
, &args
,
7233 /*disallow_virtual=*/true,
7237 /* All other function calls. */
7239 = finish_call_expr (postfix_expression
, &args
,
7240 /*disallow_virtual=*/false,
7244 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7246 location_t combined_loc
= make_location (token
->location
,
7249 postfix_expression
.set_location (combined_loc
);
7252 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7253 idk
= CP_ID_KIND_NONE
;
7255 release_tree_vector (args
);
7261 /* postfix-expression . template [opt] id-expression
7262 postfix-expression . pseudo-destructor-name
7263 postfix-expression -> template [opt] id-expression
7264 postfix-expression -> pseudo-destructor-name */
7266 /* Consume the `.' or `->' operator. */
7267 cp_lexer_consume_token (parser
->lexer
);
7270 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7274 is_member_access
= true;
7278 /* postfix-expression ++ */
7279 /* Consume the `++' token. */
7280 cp_lexer_consume_token (parser
->lexer
);
7281 /* Generate a representation for the complete expression. */
7283 = finish_increment_expr (postfix_expression
,
7284 POSTINCREMENT_EXPR
);
7285 /* Increments may not appear in constant-expressions. */
7286 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7287 postfix_expression
= error_mark_node
;
7288 idk
= CP_ID_KIND_NONE
;
7289 is_member_access
= false;
7292 case CPP_MINUS_MINUS
:
7293 /* postfix-expression -- */
7294 /* Consume the `--' token. */
7295 cp_lexer_consume_token (parser
->lexer
);
7296 /* Generate a representation for the complete expression. */
7298 = finish_increment_expr (postfix_expression
,
7299 POSTDECREMENT_EXPR
);
7300 /* Decrements may not appear in constant-expressions. */
7301 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7302 postfix_expression
= error_mark_node
;
7303 idk
= CP_ID_KIND_NONE
;
7304 is_member_access
= false;
7308 if (pidk_return
!= NULL
)
7309 * pidk_return
= idk
;
7310 if (member_access_only_p
)
7311 return is_member_access
7312 ? postfix_expression
7313 : cp_expr (error_mark_node
);
7315 return postfix_expression
;
7319 /* We should never get here. */
7321 return error_mark_node
;
7324 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7325 by cp_parser_builtin_offsetof. We're looking for
7327 postfix-expression [ expression ]
7328 postfix-expression [ braced-init-list ] (C++11)
7330 FOR_OFFSETOF is set if we're being called in that context, which
7331 changes how we deal with integer constant expressions. */
7334 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7335 tree postfix_expression
,
7339 tree index
= NULL_TREE
;
7340 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7341 bool saved_greater_than_is_operator_p
;
7343 /* Consume the `[' token. */
7344 cp_lexer_consume_token (parser
->lexer
);
7346 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7347 parser
->greater_than_is_operator_p
= true;
7349 /* Parse the index expression. */
7350 /* ??? For offsetof, there is a question of what to allow here. If
7351 offsetof is not being used in an integral constant expression context,
7352 then we *could* get the right answer by computing the value at runtime.
7353 If we are in an integral constant expression context, then we might
7354 could accept any constant expression; hard to say without analysis.
7355 Rather than open the barn door too wide right away, allow only integer
7356 constant expressions here. */
7358 index
= cp_parser_constant_expression (parser
);
7361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7363 bool expr_nonconst_p
;
7364 cp_lexer_set_source_position (parser
->lexer
);
7365 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7366 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7369 index
= cp_parser_expression (parser
);
7372 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7374 /* Look for the closing `]'. */
7375 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7377 /* Build the ARRAY_REF. */
7378 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7381 /* When not doing offsetof, array references are not permitted in
7382 constant-expressions. */
7384 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7385 postfix_expression
= error_mark_node
;
7387 return postfix_expression
;
7390 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7391 by cp_parser_builtin_offsetof. We're looking for
7393 postfix-expression . template [opt] id-expression
7394 postfix-expression . pseudo-destructor-name
7395 postfix-expression -> template [opt] id-expression
7396 postfix-expression -> pseudo-destructor-name
7398 FOR_OFFSETOF is set if we're being called in that context. That sorta
7399 limits what of the above we'll actually accept, but nevermind.
7400 TOKEN_TYPE is the "." or "->" token, which will already have been
7401 removed from the stream. */
7404 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7405 enum cpp_ttype token_type
,
7406 cp_expr postfix_expression
,
7407 bool for_offsetof
, cp_id_kind
*idk
,
7408 location_t location
)
7412 bool pseudo_destructor_p
;
7413 tree scope
= NULL_TREE
;
7414 location_t start_loc
= postfix_expression
.get_start ();
7416 /* If this is a `->' operator, dereference the pointer. */
7417 if (token_type
== CPP_DEREF
)
7418 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7419 tf_warning_or_error
);
7420 /* Check to see whether or not the expression is type-dependent and
7421 not the current instantiation. */
7422 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7423 /* The identifier following the `->' or `.' is not qualified. */
7424 parser
->scope
= NULL_TREE
;
7425 parser
->qualifying_scope
= NULL_TREE
;
7426 parser
->object_scope
= NULL_TREE
;
7427 *idk
= CP_ID_KIND_NONE
;
7429 /* Enter the scope corresponding to the type of the object
7430 given by the POSTFIX_EXPRESSION. */
7433 scope
= TREE_TYPE (postfix_expression
);
7434 /* According to the standard, no expression should ever have
7435 reference type. Unfortunately, we do not currently match
7436 the standard in this respect in that our internal representation
7437 of an expression may have reference type even when the standard
7438 says it does not. Therefore, we have to manually obtain the
7439 underlying type here. */
7440 scope
= non_reference (scope
);
7441 /* The type of the POSTFIX_EXPRESSION must be complete. */
7442 /* Unlike the object expression in other contexts, *this is not
7443 required to be of complete type for purposes of class member
7444 access (5.2.5) outside the member function body. */
7445 if (postfix_expression
!= current_class_ref
7446 && scope
!= error_mark_node
7447 && !(processing_template_decl
7448 && current_class_type
7449 && (same_type_ignoring_top_level_qualifiers_p
7450 (scope
, current_class_type
))))
7452 scope
= complete_type (scope
);
7453 if (!COMPLETE_TYPE_P (scope
)
7454 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7455 && EXPR_P (postfix_expression
))
7457 /* In a template, be permissive by treating an object expression
7458 of incomplete type as dependent (after a pedwarn). */
7459 diagnostic_t kind
= (processing_template_decl
7460 && MAYBE_CLASS_TYPE_P (scope
)
7463 cxx_incomplete_type_diagnostic
7464 (location_of (postfix_expression
),
7465 postfix_expression
, scope
, kind
);
7466 if (!MAYBE_CLASS_TYPE_P (scope
))
7467 return error_mark_node
;
7468 if (processing_template_decl
)
7471 scope
= TREE_TYPE (postfix_expression
) = NULL_TREE
;
7478 /* Let the name lookup machinery know that we are processing a
7479 class member access expression. */
7480 parser
->context
->object_type
= scope
;
7481 /* If something went wrong, we want to be able to discern that case,
7482 as opposed to the case where there was no SCOPE due to the type
7483 of expression being dependent. */
7485 scope
= error_mark_node
;
7486 /* If the SCOPE was erroneous, make the various semantic analysis
7487 functions exit quickly -- and without issuing additional error
7489 if (scope
== error_mark_node
)
7490 postfix_expression
= error_mark_node
;
7495 /* Tell cp_parser_lookup_name that there was an object, even though it's
7497 parser
->context
->object_type
= unknown_type_node
;
7499 /* Assume this expression is not a pseudo-destructor access. */
7500 pseudo_destructor_p
= false;
7502 /* If the SCOPE is a scalar type, then, if this is a valid program,
7503 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7504 is type dependent, it can be pseudo-destructor-name or something else.
7505 Try to parse it as pseudo-destructor-name first. */
7506 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7511 cp_parser_parse_tentatively (parser
);
7512 /* Parse the pseudo-destructor-name. */
7514 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7517 && (cp_parser_error_occurred (parser
)
7518 || !SCALAR_TYPE_P (type
)))
7519 cp_parser_abort_tentative_parse (parser
);
7520 else if (cp_parser_parse_definitely (parser
))
7522 pseudo_destructor_p
= true;
7524 = finish_pseudo_destructor_expr (postfix_expression
,
7529 if (!pseudo_destructor_p
)
7531 /* If the SCOPE is not a scalar type, we are looking at an
7532 ordinary class member access expression, rather than a
7533 pseudo-destructor-name. */
7535 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7536 /* Parse the id-expression. */
7537 name
= (cp_parser_id_expression
7539 cp_parser_optional_template_keyword (parser
),
7540 /*check_dependency_p=*/true,
7542 /*declarator_p=*/false,
7543 /*optional_p=*/false));
7544 /* In general, build a SCOPE_REF if the member name is qualified.
7545 However, if the name was not dependent and has already been
7546 resolved; there is no need to build the SCOPE_REF. For example;
7548 struct X { void f(); };
7549 template <typename T> void f(T* t) { t->X::f(); }
7551 Even though "t" is dependent, "X::f" is not and has been resolved
7552 to a BASELINK; there is no need to include scope information. */
7554 /* But we do need to remember that there was an explicit scope for
7555 virtual function calls. */
7557 *idk
= CP_ID_KIND_QUALIFIED
;
7559 /* If the name is a template-id that names a type, we will get a
7560 TYPE_DECL here. That is invalid code. */
7561 if (TREE_CODE (name
) == TYPE_DECL
)
7563 error_at (token
->location
, "invalid use of %qD", name
);
7564 postfix_expression
= error_mark_node
;
7568 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7570 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7572 error_at (token
->location
, "%<%D::%D%> is not a class member",
7573 parser
->scope
, name
);
7574 postfix_expression
= error_mark_node
;
7577 name
= build_qualified_name (/*type=*/NULL_TREE
,
7581 parser
->scope
= NULL_TREE
;
7582 parser
->qualifying_scope
= NULL_TREE
;
7583 parser
->object_scope
= NULL_TREE
;
7585 if (parser
->scope
&& name
&& BASELINK_P (name
))
7586 adjust_result_of_qualified_name_lookup
7587 (name
, parser
->scope
, scope
);
7589 = finish_class_member_access_expr (postfix_expression
, name
,
7591 tf_warning_or_error
);
7592 /* Build a location e.g.:
7595 where the caret is at the deref token, ranging from
7596 the start of postfix_expression to the end of the access expr. */
7598 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7599 location_t combined_loc
7600 = make_location (input_location
, start_loc
, end_loc
);
7601 protected_set_expr_location (postfix_expression
, combined_loc
);
7605 /* We no longer need to look up names in the scope of the object on
7606 the left-hand side of the `.' or `->' operator. */
7607 parser
->context
->object_type
= NULL_TREE
;
7609 /* Outside of offsetof, these operators may not appear in
7610 constant-expressions. */
7612 && (cp_parser_non_integral_constant_expression
7613 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7614 postfix_expression
= error_mark_node
;
7616 return postfix_expression
;
7619 /* Parse a parenthesized expression-list.
7622 assignment-expression
7623 expression-list, assignment-expression
7628 identifier, expression-list
7630 CAST_P is true if this expression is the target of a cast.
7632 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7635 WRAP_LOCATIONS_P is true if expressions within this list for which
7636 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7637 their source locations.
7639 Returns a vector of trees. Each element is a representation of an
7640 assignment-expression. NULL is returned if the ( and or ) are
7641 missing. An empty, but allocated, vector is returned on no
7642 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7643 if we are parsing an attribute list for an attribute that wants a
7644 plain identifier argument, normal_attr for an attribute that wants
7645 an expression, or non_attr if we aren't parsing an attribute list. If
7646 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7647 not all of the expressions in the list were constant.
7648 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7649 will be written to with the location of the closing parenthesis. If
7650 an error occurs, it may or may not be written to. */
7652 static vec
<tree
, va_gc
> *
7653 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7654 int is_attribute_list
,
7656 bool allow_expansion_p
,
7657 bool *non_constant_p
,
7658 location_t
*close_paren_loc
,
7659 bool wrap_locations_p
)
7661 vec
<tree
, va_gc
> *expression_list
;
7662 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7663 tree identifier
= NULL_TREE
;
7664 bool saved_greater_than_is_operator_p
;
7666 /* Assume all the expressions will be constant. */
7668 *non_constant_p
= false;
7670 matching_parens parens
;
7671 if (!parens
.require_open (parser
))
7674 expression_list
= make_tree_vector ();
7676 /* Within a parenthesized expression, a `>' token is always
7677 the greater-than operator. */
7678 saved_greater_than_is_operator_p
7679 = parser
->greater_than_is_operator_p
;
7680 parser
->greater_than_is_operator_p
= true;
7682 cp_expr
expr (NULL_TREE
);
7684 /* Consume expressions until there are no more. */
7685 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7688 /* At the beginning of attribute lists, check to see if the
7689 next token is an identifier. */
7690 if (is_attribute_list
== id_attr
7691 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7695 /* Consume the identifier. */
7696 token
= cp_lexer_consume_token (parser
->lexer
);
7697 /* Save the identifier. */
7698 identifier
= token
->u
.value
;
7702 bool expr_non_constant_p
;
7704 /* Parse the next assignment-expression. */
7705 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7707 /* A braced-init-list. */
7708 cp_lexer_set_source_position (parser
->lexer
);
7709 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7710 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7711 if (non_constant_p
&& expr_non_constant_p
)
7712 *non_constant_p
= true;
7714 else if (non_constant_p
)
7716 expr
= (cp_parser_constant_expression
7717 (parser
, /*allow_non_constant_p=*/true,
7718 &expr_non_constant_p
));
7719 if (expr_non_constant_p
)
7720 *non_constant_p
= true;
7723 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7727 expr
= instantiate_non_dependent_expr (expr
);
7729 /* If we have an ellipsis, then this is an expression
7731 if (allow_expansion_p
7732 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7734 /* Consume the `...'. */
7735 cp_lexer_consume_token (parser
->lexer
);
7737 /* Build the argument pack. */
7738 expr
= make_pack_expansion (expr
);
7741 if (wrap_locations_p
)
7742 expr
.maybe_add_location_wrapper ();
7744 /* Add it to the list. We add error_mark_node
7745 expressions to the list, so that we can still tell if
7746 the correct form for a parenthesized expression-list
7747 is found. That gives better errors. */
7748 vec_safe_push (expression_list
, expr
.get_value ());
7750 if (expr
== error_mark_node
)
7754 /* After the first item, attribute lists look the same as
7755 expression lists. */
7756 is_attribute_list
= non_attr
;
7759 /* If the next token isn't a `,', then we are done. */
7760 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7763 /* Otherwise, consume the `,' and keep going. */
7764 cp_lexer_consume_token (parser
->lexer
);
7767 if (close_paren_loc
)
7768 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7770 if (!parens
.require_close (parser
))
7775 /* We try and resync to an unnested comma, as that will give the
7776 user better diagnostics. */
7777 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7778 /*recovering=*/true,
7780 /*consume_paren=*/true);
7785 parser
->greater_than_is_operator_p
7786 = saved_greater_than_is_operator_p
;
7791 parser
->greater_than_is_operator_p
7792 = saved_greater_than_is_operator_p
;
7795 vec_safe_insert (expression_list
, 0, identifier
);
7797 return expression_list
;
7800 /* Parse a pseudo-destructor-name.
7802 pseudo-destructor-name:
7803 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7804 :: [opt] nested-name-specifier template template-id :: ~ type-name
7805 :: [opt] nested-name-specifier [opt] ~ type-name
7807 If either of the first two productions is used, sets *SCOPE to the
7808 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7809 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7810 or ERROR_MARK_NODE if the parse fails. */
7813 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7818 bool nested_name_specifier_p
;
7821 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7822 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7823 && !type_dependent_expression_p (object
))
7825 if (cxx_dialect
< cxx14
)
7826 pedwarn (input_location
, 0,
7827 "%<~auto%> only available with "
7828 "-std=c++14 or -std=gnu++14");
7829 cp_lexer_consume_token (parser
->lexer
);
7830 cp_lexer_consume_token (parser
->lexer
);
7832 *type
= TREE_TYPE (object
);
7836 /* Assume that things will not work out. */
7837 *type
= error_mark_node
;
7839 /* Look for the optional `::' operator. */
7840 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7841 /* Look for the optional nested-name-specifier. */
7842 nested_name_specifier_p
7843 = (cp_parser_nested_name_specifier_opt (parser
,
7844 /*typename_keyword_p=*/false,
7845 /*check_dependency_p=*/true,
7847 /*is_declaration=*/false)
7849 /* Now, if we saw a nested-name-specifier, we might be doing the
7850 second production. */
7851 if (nested_name_specifier_p
7852 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7854 /* Consume the `template' keyword. */
7855 cp_lexer_consume_token (parser
->lexer
);
7856 /* Parse the template-id. */
7857 cp_parser_template_id (parser
,
7858 /*template_keyword_p=*/true,
7859 /*check_dependency_p=*/false,
7861 /*is_declaration=*/true);
7862 /* Look for the `::' token. */
7863 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7865 /* If the next token is not a `~', then there might be some
7866 additional qualification. */
7867 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7869 /* At this point, we're looking for "type-name :: ~". The type-name
7870 must not be a class-name, since this is a pseudo-destructor. So,
7871 it must be either an enum-name, or a typedef-name -- both of which
7872 are just identifiers. So, we peek ahead to check that the "::"
7873 and "~" tokens are present; if they are not, then we can avoid
7874 calling type_name. */
7875 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7876 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7877 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7879 cp_parser_error (parser
, "non-scalar type");
7883 /* Look for the type-name. */
7884 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7885 if (*scope
== error_mark_node
)
7888 /* Look for the `::' token. */
7889 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7894 /* Look for the `~'. */
7895 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7897 /* Once we see the ~, this has to be a pseudo-destructor. */
7898 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7899 cp_parser_commit_to_topmost_tentative_parse (parser
);
7901 /* Look for the type-name again. We are not responsible for
7902 checking that it matches the first type-name. */
7903 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7906 /* Parse a unary-expression.
7912 unary-operator cast-expression
7913 sizeof unary-expression
7915 alignof ( type-id ) [C++0x]
7922 __extension__ cast-expression
7923 __alignof__ unary-expression
7924 __alignof__ ( type-id )
7925 alignof unary-expression [C++0x]
7926 __real__ cast-expression
7927 __imag__ cast-expression
7929 sizeof ( type-id ) { initializer-list , [opt] }
7930 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7931 __alignof__ ( type-id ) { initializer-list , [opt] }
7933 ADDRESS_P is true iff the unary-expression is appearing as the
7934 operand of the `&' operator. CAST_P is true if this expression is
7935 the target of a cast.
7937 Returns a representation of the expression. */
7940 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7941 bool address_p
, bool cast_p
, bool decltype_p
)
7944 enum tree_code unary_operator
;
7946 /* Peek at the next token. */
7947 token
= cp_lexer_peek_token (parser
->lexer
);
7948 /* Some keywords give away the kind of expression. */
7949 if (token
->type
== CPP_KEYWORD
)
7951 enum rid keyword
= token
->keyword
;
7960 location_t start_loc
= token
->location
;
7962 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7963 /* Consume the token. */
7964 cp_lexer_consume_token (parser
->lexer
);
7965 /* Parse the operand. */
7966 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7968 if (TYPE_P (operand
))
7969 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7972 /* ISO C++ defines alignof only with types, not with
7973 expressions. So pedwarn if alignof is used with a non-
7974 type expression. However, __alignof__ is ok. */
7975 if (id_equal (token
->u
.value
, "alignof"))
7976 pedwarn (token
->location
, OPT_Wpedantic
,
7977 "ISO C++ does not allow %<alignof%> "
7980 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7982 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7983 SIZEOF_EXPR with the original operand. */
7984 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7986 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7988 if (!processing_template_decl
&& TYPE_P (operand
))
7990 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7991 build1 (NOP_EXPR
, operand
,
7993 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7996 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7997 TREE_SIDE_EFFECTS (ret
) = 0;
7998 TREE_READONLY (ret
) = 1;
8002 /* Construct a location e.g. :
8005 with start == caret at the start of the "alignof"/"sizeof"
8006 token, with the endpoint at the final closing paren. */
8007 location_t finish_loc
8008 = cp_lexer_previous_token (parser
->lexer
)->location
;
8009 location_t compound_loc
8010 = make_location (start_loc
, start_loc
, finish_loc
);
8012 cp_expr
ret_expr (ret
);
8013 ret_expr
.set_location (compound_loc
);
8014 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8019 return cp_parser_new_expression (parser
);
8022 return cp_parser_delete_expression (parser
);
8026 /* The saved value of the PEDANTIC flag. */
8030 /* Save away the PEDANTIC flag. */
8031 cp_parser_extension_opt (parser
, &saved_pedantic
);
8032 /* Parse the cast-expression. */
8033 expr
= cp_parser_simple_cast_expression (parser
);
8034 /* Restore the PEDANTIC flag. */
8035 pedantic
= saved_pedantic
;
8045 /* Consume the `__real__' or `__imag__' token. */
8046 cp_lexer_consume_token (parser
->lexer
);
8047 /* Parse the cast-expression. */
8048 expression
= cp_parser_simple_cast_expression (parser
);
8049 /* Create the complete representation. */
8050 return build_x_unary_op (token
->location
,
8051 (keyword
== RID_REALPART
8052 ? REALPART_EXPR
: IMAGPART_EXPR
),
8054 tf_warning_or_error
);
8058 case RID_TRANSACTION_ATOMIC
:
8059 case RID_TRANSACTION_RELAXED
:
8060 return cp_parser_transaction_expression (parser
, keyword
);
8065 const char *saved_message
;
8066 bool saved_integral_constant_expression_p
;
8067 bool saved_non_integral_constant_expression_p
;
8068 bool saved_greater_than_is_operator_p
;
8070 location_t start_loc
= token
->location
;
8072 cp_lexer_consume_token (parser
->lexer
);
8073 matching_parens parens
;
8074 parens
.require_open (parser
);
8076 saved_message
= parser
->type_definition_forbidden_message
;
8077 parser
->type_definition_forbidden_message
8078 = G_("types may not be defined in %<noexcept%> expressions");
8080 saved_integral_constant_expression_p
8081 = parser
->integral_constant_expression_p
;
8082 saved_non_integral_constant_expression_p
8083 = parser
->non_integral_constant_expression_p
;
8084 parser
->integral_constant_expression_p
= false;
8086 saved_greater_than_is_operator_p
8087 = parser
->greater_than_is_operator_p
;
8088 parser
->greater_than_is_operator_p
= true;
8090 ++cp_unevaluated_operand
;
8091 ++c_inhibit_evaluation_warnings
;
8092 ++cp_noexcept_operand
;
8093 expr
= cp_parser_expression (parser
);
8094 --cp_noexcept_operand
;
8095 --c_inhibit_evaluation_warnings
;
8096 --cp_unevaluated_operand
;
8098 parser
->greater_than_is_operator_p
8099 = saved_greater_than_is_operator_p
;
8101 parser
->integral_constant_expression_p
8102 = saved_integral_constant_expression_p
;
8103 parser
->non_integral_constant_expression_p
8104 = saved_non_integral_constant_expression_p
;
8106 parser
->type_definition_forbidden_message
= saved_message
;
8108 location_t finish_loc
8109 = cp_lexer_peek_token (parser
->lexer
)->location
;
8110 parens
.require_close (parser
);
8112 /* Construct a location of the form:
8115 with start == caret, finishing at the close-paren. */
8116 location_t noexcept_loc
8117 = make_location (start_loc
, start_loc
, finish_loc
);
8119 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8128 /* Look for the `:: new' and `:: delete', which also signal the
8129 beginning of a new-expression, or delete-expression,
8130 respectively. If the next token is `::', then it might be one of
8132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8136 /* See if the token after the `::' is one of the keywords in
8137 which we're interested. */
8138 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8139 /* If it's `new', we have a new-expression. */
8140 if (keyword
== RID_NEW
)
8141 return cp_parser_new_expression (parser
);
8142 /* Similarly, for `delete'. */
8143 else if (keyword
== RID_DELETE
)
8144 return cp_parser_delete_expression (parser
);
8147 /* Look for a unary operator. */
8148 unary_operator
= cp_parser_unary_operator (token
);
8149 /* The `++' and `--' operators can be handled similarly, even though
8150 they are not technically unary-operators in the grammar. */
8151 if (unary_operator
== ERROR_MARK
)
8153 if (token
->type
== CPP_PLUS_PLUS
)
8154 unary_operator
= PREINCREMENT_EXPR
;
8155 else if (token
->type
== CPP_MINUS_MINUS
)
8156 unary_operator
= PREDECREMENT_EXPR
;
8157 /* Handle the GNU address-of-label extension. */
8158 else if (cp_parser_allow_gnu_extensions_p (parser
)
8159 && token
->type
== CPP_AND_AND
)
8163 location_t start_loc
= token
->location
;
8165 /* Consume the '&&' token. */
8166 cp_lexer_consume_token (parser
->lexer
);
8167 /* Look for the identifier. */
8168 location_t finish_loc
8169 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8170 identifier
= cp_parser_identifier (parser
);
8171 /* Construct a location of the form:
8174 with caret==start at the "&&", finish at the end of the label. */
8175 location_t combined_loc
8176 = make_location (start_loc
, start_loc
, finish_loc
);
8177 /* Create an expression representing the address. */
8178 expression
= finish_label_address_expr (identifier
, combined_loc
);
8179 if (cp_parser_non_integral_constant_expression (parser
,
8181 expression
= error_mark_node
;
8185 if (unary_operator
!= ERROR_MARK
)
8187 cp_expr cast_expression
;
8188 cp_expr expression
= error_mark_node
;
8189 non_integral_constant non_constant_p
= NIC_NONE
;
8190 location_t loc
= token
->location
;
8191 tsubst_flags_t complain
= complain_flags (decltype_p
);
8193 /* Consume the operator token. */
8194 token
= cp_lexer_consume_token (parser
->lexer
);
8195 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8197 /* Parse the cast-expression. */
8199 = cp_parser_cast_expression (parser
,
8200 unary_operator
== ADDR_EXPR
,
8206 OP_TOKEN CAST_EXPRESSION
8207 ^~~~~~~~~~~~~~~~~~~~~~~~~
8208 with start==caret at the operator token, and
8209 extending to the end of the cast_expression. */
8210 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8212 /* Now, build an appropriate representation. */
8213 switch (unary_operator
)
8216 non_constant_p
= NIC_STAR
;
8217 expression
= build_x_indirect_ref (loc
, cast_expression
,
8220 /* TODO: build_x_indirect_ref does not always honor the
8221 location, so ensure it is set. */
8222 expression
.set_location (loc
);
8226 non_constant_p
= NIC_ADDR
;
8229 expression
= build_x_unary_op (loc
, unary_operator
,
8232 /* TODO: build_x_unary_op does not always honor the location,
8233 so ensure it is set. */
8234 expression
.set_location (loc
);
8237 case PREINCREMENT_EXPR
:
8238 case PREDECREMENT_EXPR
:
8239 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8240 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8243 /* Immediately fold negation of a constant, unless the constant is 0
8244 (since -0 == 0) or it would overflow. */
8245 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8246 && CONSTANT_CLASS_P (cast_expression
)
8247 && !integer_zerop (cast_expression
)
8248 && !TREE_OVERFLOW (cast_expression
))
8250 tree folded
= fold_build1 (unary_operator
,
8251 TREE_TYPE (cast_expression
),
8253 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8255 expression
= cp_expr (folded
, loc
);
8260 case UNARY_PLUS_EXPR
:
8261 case TRUTH_NOT_EXPR
:
8262 expression
= finish_unary_op_expr (loc
, unary_operator
,
8263 cast_expression
, complain
);
8270 if (non_constant_p
!= NIC_NONE
8271 && cp_parser_non_integral_constant_expression (parser
,
8273 expression
= error_mark_node
;
8278 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8279 /*member_access_only_p=*/false,
8284 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8285 unary-operator, the corresponding tree code is returned. */
8287 static enum tree_code
8288 cp_parser_unary_operator (cp_token
* token
)
8290 switch (token
->type
)
8293 return INDIRECT_REF
;
8299 return UNARY_PLUS_EXPR
;
8305 return TRUTH_NOT_EXPR
;
8308 return BIT_NOT_EXPR
;
8315 /* Parse a new-expression.
8318 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8319 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8321 Returns a representation of the expression. */
8324 cp_parser_new_expression (cp_parser
* parser
)
8326 bool global_scope_p
;
8327 vec
<tree
, va_gc
> *placement
;
8329 vec
<tree
, va_gc
> *initializer
;
8330 tree nelts
= NULL_TREE
;
8333 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8335 /* Look for the optional `::' operator. */
8337 = (cp_parser_global_scope_opt (parser
,
8338 /*current_scope_valid_p=*/false)
8340 /* Look for the `new' operator. */
8341 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8342 /* There's no easy way to tell a new-placement from the
8343 `( type-id )' construct. */
8344 cp_parser_parse_tentatively (parser
);
8345 /* Look for a new-placement. */
8346 placement
= cp_parser_new_placement (parser
);
8347 /* If that didn't work out, there's no new-placement. */
8348 if (!cp_parser_parse_definitely (parser
))
8350 if (placement
!= NULL
)
8351 release_tree_vector (placement
);
8355 /* If the next token is a `(', then we have a parenthesized
8357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8360 const char *saved_message
= parser
->type_definition_forbidden_message
;
8362 /* Consume the `('. */
8363 matching_parens parens
;
8364 parens
.consume_open (parser
);
8366 /* Parse the type-id. */
8367 parser
->type_definition_forbidden_message
8368 = G_("types may not be defined in a new-expression");
8370 type_id_in_expr_sentinel
s (parser
);
8371 type
= cp_parser_type_id (parser
);
8373 parser
->type_definition_forbidden_message
= saved_message
;
8375 /* Look for the closing `)'. */
8376 parens
.require_close (parser
);
8377 token
= cp_lexer_peek_token (parser
->lexer
);
8378 /* There should not be a direct-new-declarator in this production,
8379 but GCC used to allowed this, so we check and emit a sensible error
8380 message for this case. */
8381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8383 error_at (token
->location
,
8384 "array bound forbidden after parenthesized type-id");
8385 inform (token
->location
,
8386 "try removing the parentheses around the type-id");
8387 cp_parser_direct_new_declarator (parser
);
8390 /* Otherwise, there must be a new-type-id. */
8392 type
= cp_parser_new_type_id (parser
, &nelts
);
8394 /* If the next token is a `(' or '{', then we have a new-initializer. */
8395 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8396 if (token
->type
== CPP_OPEN_PAREN
8397 || token
->type
== CPP_OPEN_BRACE
)
8398 initializer
= cp_parser_new_initializer (parser
);
8402 /* A new-expression may not appear in an integral constant
8404 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8405 ret
= error_mark_node
;
8406 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8407 of a new-type-id or type-id of a new-expression, the new-expression shall
8408 contain a new-initializer of the form ( assignment-expression )".
8409 Additionally, consistently with the spirit of DR 1467, we want to accept
8410 'new auto { 2 }' too. */
8411 else if ((ret
= type_uses_auto (type
))
8412 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8413 && (vec_safe_length (initializer
) != 1
8414 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8415 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8417 error_at (token
->location
,
8418 "initialization of new-expression for type %<auto%> "
8419 "requires exactly one element");
8420 ret
= error_mark_node
;
8424 /* Construct a location e.g.:
8427 with caret == start at the start of the "new" token, and the end
8428 at the end of the final token we consumed. */
8429 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8430 location_t end_loc
= get_finish (end_tok
->location
);
8431 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8433 /* Create a representation of the new-expression. */
8434 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8435 tf_warning_or_error
);
8436 protected_set_expr_location (ret
, combined_loc
);
8439 if (placement
!= NULL
)
8440 release_tree_vector (placement
);
8441 if (initializer
!= NULL
)
8442 release_tree_vector (initializer
);
8447 /* Parse a new-placement.
8452 Returns the same representation as for an expression-list. */
8454 static vec
<tree
, va_gc
> *
8455 cp_parser_new_placement (cp_parser
* parser
)
8457 vec
<tree
, va_gc
> *expression_list
;
8459 /* Parse the expression-list. */
8460 expression_list
= (cp_parser_parenthesized_expression_list
8461 (parser
, non_attr
, /*cast_p=*/false,
8462 /*allow_expansion_p=*/true,
8463 /*non_constant_p=*/NULL
));
8465 if (expression_list
&& expression_list
->is_empty ())
8466 error ("expected expression-list or type-id");
8468 return expression_list
;
8471 /* Parse a new-type-id.
8474 type-specifier-seq new-declarator [opt]
8476 Returns the TYPE allocated. If the new-type-id indicates an array
8477 type, *NELTS is set to the number of elements in the last array
8478 bound; the TYPE will not include the last array bound. */
8481 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8483 cp_decl_specifier_seq type_specifier_seq
;
8484 cp_declarator
*new_declarator
;
8485 cp_declarator
*declarator
;
8486 cp_declarator
*outer_declarator
;
8487 const char *saved_message
;
8489 /* The type-specifier sequence must not contain type definitions.
8490 (It cannot contain declarations of new types either, but if they
8491 are not definitions we will catch that because they are not
8493 saved_message
= parser
->type_definition_forbidden_message
;
8494 parser
->type_definition_forbidden_message
8495 = G_("types may not be defined in a new-type-id");
8496 /* Parse the type-specifier-seq. */
8497 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8498 /*is_trailing_return=*/false,
8499 &type_specifier_seq
);
8500 /* Restore the old message. */
8501 parser
->type_definition_forbidden_message
= saved_message
;
8503 if (type_specifier_seq
.type
== error_mark_node
)
8504 return error_mark_node
;
8506 /* Parse the new-declarator. */
8507 new_declarator
= cp_parser_new_declarator_opt (parser
);
8509 /* Determine the number of elements in the last array dimension, if
8512 /* Skip down to the last array dimension. */
8513 declarator
= new_declarator
;
8514 outer_declarator
= NULL
;
8515 while (declarator
&& (declarator
->kind
== cdk_pointer
8516 || declarator
->kind
== cdk_ptrmem
))
8518 outer_declarator
= declarator
;
8519 declarator
= declarator
->declarator
;
8522 && declarator
->kind
== cdk_array
8523 && declarator
->declarator
8524 && declarator
->declarator
->kind
== cdk_array
)
8526 outer_declarator
= declarator
;
8527 declarator
= declarator
->declarator
;
8530 if (declarator
&& declarator
->kind
== cdk_array
)
8532 *nelts
= declarator
->u
.array
.bounds
;
8533 if (*nelts
== error_mark_node
)
8534 *nelts
= integer_one_node
;
8536 if (outer_declarator
)
8537 outer_declarator
->declarator
= declarator
->declarator
;
8539 new_declarator
= NULL
;
8542 return groktypename (&type_specifier_seq
, new_declarator
, false);
8545 /* Parse an (optional) new-declarator.
8548 ptr-operator new-declarator [opt]
8549 direct-new-declarator
8551 Returns the declarator. */
8553 static cp_declarator
*
8554 cp_parser_new_declarator_opt (cp_parser
* parser
)
8556 enum tree_code code
;
8557 tree type
, std_attributes
= NULL_TREE
;
8558 cp_cv_quals cv_quals
;
8560 /* We don't know if there's a ptr-operator next, or not. */
8561 cp_parser_parse_tentatively (parser
);
8562 /* Look for a ptr-operator. */
8563 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8564 /* If that worked, look for more new-declarators. */
8565 if (cp_parser_parse_definitely (parser
))
8567 cp_declarator
*declarator
;
8569 /* Parse another optional declarator. */
8570 declarator
= cp_parser_new_declarator_opt (parser
);
8572 declarator
= cp_parser_make_indirect_declarator
8573 (code
, type
, cv_quals
, declarator
, std_attributes
);
8578 /* If the next token is a `[', there is a direct-new-declarator. */
8579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8580 return cp_parser_direct_new_declarator (parser
);
8585 /* Parse a direct-new-declarator.
8587 direct-new-declarator:
8589 direct-new-declarator [constant-expression]
8593 static cp_declarator
*
8594 cp_parser_direct_new_declarator (cp_parser
* parser
)
8596 cp_declarator
*declarator
= NULL
;
8603 /* Look for the opening `['. */
8604 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8606 token
= cp_lexer_peek_token (parser
->lexer
);
8607 expression
= cp_parser_expression (parser
);
8608 /* The standard requires that the expression have integral
8609 type. DR 74 adds enumeration types. We believe that the
8610 real intent is that these expressions be handled like the
8611 expression in a `switch' condition, which also allows
8612 classes with a single conversion to integral or
8613 enumeration type. */
8614 if (!processing_template_decl
)
8617 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8622 error_at (token
->location
,
8623 "expression in new-declarator must have integral "
8624 "or enumeration type");
8625 expression
= error_mark_node
;
8629 /* Look for the closing `]'. */
8630 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8632 /* Add this bound to the declarator. */
8633 declarator
= make_array_declarator (declarator
, expression
);
8635 /* If the next token is not a `[', then there are no more
8637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8644 /* Parse a new-initializer.
8647 ( expression-list [opt] )
8650 Returns a representation of the expression-list. */
8652 static vec
<tree
, va_gc
> *
8653 cp_parser_new_initializer (cp_parser
* parser
)
8655 vec
<tree
, va_gc
> *expression_list
;
8657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8660 bool expr_non_constant_p
;
8661 cp_lexer_set_source_position (parser
->lexer
);
8662 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8663 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8664 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8665 expression_list
= make_tree_vector_single (t
);
8668 expression_list
= (cp_parser_parenthesized_expression_list
8669 (parser
, non_attr
, /*cast_p=*/false,
8670 /*allow_expansion_p=*/true,
8671 /*non_constant_p=*/NULL
));
8673 return expression_list
;
8676 /* Parse a delete-expression.
8679 :: [opt] delete cast-expression
8680 :: [opt] delete [ ] cast-expression
8682 Returns a representation of the expression. */
8685 cp_parser_delete_expression (cp_parser
* parser
)
8687 bool global_scope_p
;
8691 /* Look for the optional `::' operator. */
8693 = (cp_parser_global_scope_opt (parser
,
8694 /*current_scope_valid_p=*/false)
8696 /* Look for the `delete' keyword. */
8697 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8698 /* See if the array syntax is in use. */
8699 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8701 /* Consume the `[' token. */
8702 cp_lexer_consume_token (parser
->lexer
);
8703 /* Look for the `]' token. */
8704 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8705 /* Remember that this is the `[]' construct. */
8711 /* Parse the cast-expression. */
8712 expression
= cp_parser_simple_cast_expression (parser
);
8714 /* A delete-expression may not appear in an integral constant
8716 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8717 return error_mark_node
;
8719 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8720 tf_warning_or_error
);
8723 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8724 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8728 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8730 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8731 switch (token
->type
)
8737 case CPP_CLOSE_SQUARE
:
8738 case CPP_CLOSE_PAREN
:
8739 case CPP_CLOSE_BRACE
:
8740 case CPP_OPEN_BRACE
:
8744 case CPP_DEREF_STAR
:
8752 case CPP_GREATER_EQ
:
8773 case CPP_OPEN_PAREN
:
8774 /* In ((type ()) () the last () isn't a valid cast-expression,
8775 so the whole must be parsed as postfix-expression. */
8776 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8779 case CPP_OPEN_SQUARE
:
8780 /* '[' may start a primary-expression in obj-c++ and in C++11,
8781 as a lambda-expression, eg, '(void)[]{}'. */
8782 if (cxx_dialect
>= cxx11
)
8784 return c_dialect_objc ();
8787 case CPP_MINUS_MINUS
:
8788 /* '++' and '--' may or may not start a cast-expression:
8790 struct T { void operator++(int); };
8791 void f() { (T())++; }
8804 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8805 in the order: const_cast, static_cast, reinterpret_cast.
8807 Don't suggest dynamic_cast.
8809 Return the first legal cast kind found, or NULL otherwise. */
8812 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8816 /* Reuse the parser logic by attempting to build the various kinds of
8817 cast, with "complain" disabled.
8818 Identify the first such cast that is valid. */
8820 /* Don't attempt to run such logic within template processing. */
8821 if (processing_template_decl
)
8824 /* First try const_cast. */
8825 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8826 if (trial
!= error_mark_node
)
8827 return "const_cast";
8829 /* If that fails, try static_cast. */
8830 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8831 if (trial
!= error_mark_node
)
8832 return "static_cast";
8834 /* Finally, try reinterpret_cast. */
8835 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8836 if (trial
!= error_mark_node
)
8837 return "reinterpret_cast";
8839 /* No such cast possible. */
8843 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8844 suggesting how to convert a C-style cast of the form:
8848 to a C++-style cast.
8850 The primary range of RICHLOC is asssumed to be that of the original
8851 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8852 of the parens in the C-style cast. */
8855 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8856 location_t close_paren_loc
, tree orig_expr
,
8859 /* This function is non-trivial, so bail out now if the warning isn't
8860 going to be emitted. */
8861 if (!warn_old_style_cast
)
8864 /* Try to find a legal C++ cast, trying them in order:
8865 const_cast, static_cast, reinterpret_cast. */
8866 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8867 if (!cast_suggestion
)
8870 /* Replace the open paren with "CAST_SUGGESTION<". */
8872 pp_printf (&pp
, "%s<", cast_suggestion
);
8873 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8875 /* Replace the close paren with "> (". */
8876 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8878 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8879 rich_loc
->add_fixit_insert_after (")");
8883 /* Parse a cast-expression.
8887 ( type-id ) cast-expression
8889 ADDRESS_P is true iff the unary-expression is appearing as the
8890 operand of the `&' operator. CAST_P is true if this expression is
8891 the target of a cast.
8893 Returns a representation of the expression. */
8896 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8897 bool decltype_p
, cp_id_kind
* pidk
)
8899 /* If it's a `(', then we might be looking at a cast. */
8900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8902 tree type
= NULL_TREE
;
8903 cp_expr
expr (NULL_TREE
);
8904 int cast_expression
= 0;
8905 const char *saved_message
;
8907 /* There's no way to know yet whether or not this is a cast.
8908 For example, `(int (3))' is a unary-expression, while `(int)
8909 3' is a cast. So, we resort to parsing tentatively. */
8910 cp_parser_parse_tentatively (parser
);
8911 /* Types may not be defined in a cast. */
8912 saved_message
= parser
->type_definition_forbidden_message
;
8913 parser
->type_definition_forbidden_message
8914 = G_("types may not be defined in casts");
8915 /* Consume the `('. */
8916 matching_parens parens
;
8917 cp_token
*open_paren
= parens
.consume_open (parser
);
8918 location_t open_paren_loc
= open_paren
->location
;
8919 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8921 /* A very tricky bit is that `(struct S) { 3 }' is a
8922 compound-literal (which we permit in C++ as an extension).
8923 But, that construct is not a cast-expression -- it is a
8924 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8925 is legal; if the compound-literal were a cast-expression,
8926 you'd need an extra set of parentheses.) But, if we parse
8927 the type-id, and it happens to be a class-specifier, then we
8928 will commit to the parse at that point, because we cannot
8929 undo the action that is done when creating a new class. So,
8930 then we cannot back up and do a postfix-expression.
8932 Another tricky case is the following (c++/29234):
8934 struct S { void operator () (); };
8941 As a type-id we parse the parenthesized S()() as a function
8942 returning a function, groktypename complains and we cannot
8943 back up in this case either.
8945 Therefore, we scan ahead to the closing `)', and check to see
8946 if the tokens after the `)' can start a cast-expression. Otherwise
8947 we are dealing with an unary-expression, a postfix-expression
8950 Yet another tricky case, in C++11, is the following (c++/54891):
8954 The issue is that usually, besides the case of lambda-expressions,
8955 the parenthesized type-id cannot be followed by '[', and, eg, we
8956 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8957 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8958 we don't commit, we try a cast-expression, then an unary-expression.
8960 Save tokens so that we can put them back. */
8961 cp_lexer_save_tokens (parser
->lexer
);
8963 /* We may be looking at a cast-expression. */
8964 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8965 /*consume_paren=*/true))
8967 = cp_parser_tokens_start_cast_expression (parser
);
8969 /* Roll back the tokens we skipped. */
8970 cp_lexer_rollback_tokens (parser
->lexer
);
8971 /* If we aren't looking at a cast-expression, simulate an error so
8972 that the call to cp_parser_error_occurred below returns true. */
8973 if (!cast_expression
)
8974 cp_parser_simulate_error (parser
);
8977 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8978 parser
->in_type_id_in_expr_p
= true;
8979 /* Look for the type-id. */
8980 type
= cp_parser_type_id (parser
);
8981 /* Look for the closing `)'. */
8982 cp_token
*close_paren
= parens
.require_close (parser
);
8984 close_paren_loc
= close_paren
->location
;
8985 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8988 /* Restore the saved message. */
8989 parser
->type_definition_forbidden_message
= saved_message
;
8991 /* At this point this can only be either a cast or a
8992 parenthesized ctor such as `(T ())' that looks like a cast to
8993 function returning T. */
8994 if (!cp_parser_error_occurred (parser
))
8996 /* Only commit if the cast-expression doesn't start with
8997 '++', '--', or '[' in C++11. */
8998 if (cast_expression
> 0)
8999 cp_parser_commit_to_topmost_tentative_parse (parser
);
9001 expr
= cp_parser_cast_expression (parser
,
9002 /*address_p=*/false,
9004 /*decltype_p=*/false,
9007 if (cp_parser_parse_definitely (parser
))
9009 /* Warn about old-style casts, if so requested. */
9010 if (warn_old_style_cast
9011 && !in_system_header_at (input_location
)
9012 && !VOID_TYPE_P (type
)
9013 && current_lang_name
!= lang_name_c
)
9015 gcc_rich_location
rich_loc (input_location
);
9016 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9018 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9019 "use of old-style cast to %q#T", type
);
9022 /* Only type conversions to integral or enumeration types
9023 can be used in constant-expressions. */
9024 if (!cast_valid_in_integral_constant_expression_p (type
)
9025 && cp_parser_non_integral_constant_expression (parser
,
9027 return error_mark_node
;
9029 /* Perform the cast. */
9033 with start==caret at the open paren, extending to the
9035 location_t cast_loc
= make_location (open_paren_loc
,
9037 expr
.get_finish ());
9038 expr
= build_c_cast (cast_loc
, type
, expr
);
9043 cp_parser_abort_tentative_parse (parser
);
9046 /* If we get here, then it's not a cast, so it must be a
9047 unary-expression. */
9048 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9049 cast_p
, decltype_p
);
9052 /* Parse a binary expression of the general form:
9056 pm-expression .* cast-expression
9057 pm-expression ->* cast-expression
9059 multiplicative-expression:
9061 multiplicative-expression * pm-expression
9062 multiplicative-expression / pm-expression
9063 multiplicative-expression % pm-expression
9065 additive-expression:
9066 multiplicative-expression
9067 additive-expression + multiplicative-expression
9068 additive-expression - multiplicative-expression
9072 shift-expression << additive-expression
9073 shift-expression >> additive-expression
9075 relational-expression:
9077 relational-expression < shift-expression
9078 relational-expression > shift-expression
9079 relational-expression <= shift-expression
9080 relational-expression >= shift-expression
9084 relational-expression:
9085 relational-expression <? shift-expression
9086 relational-expression >? shift-expression
9088 equality-expression:
9089 relational-expression
9090 equality-expression == relational-expression
9091 equality-expression != relational-expression
9095 and-expression & equality-expression
9097 exclusive-or-expression:
9099 exclusive-or-expression ^ and-expression
9101 inclusive-or-expression:
9102 exclusive-or-expression
9103 inclusive-or-expression | exclusive-or-expression
9105 logical-and-expression:
9106 inclusive-or-expression
9107 logical-and-expression && inclusive-or-expression
9109 logical-or-expression:
9110 logical-and-expression
9111 logical-or-expression || logical-and-expression
9113 All these are implemented with a single function like:
9116 simple-cast-expression
9117 binary-expression <token> binary-expression
9119 CAST_P is true if this expression is the target of a cast.
9121 The binops_by_token map is used to get the tree codes for each <token> type.
9122 binary-expressions are associated according to a precedence table. */
9124 #define TOKEN_PRECEDENCE(token) \
9125 (((token->type == CPP_GREATER \
9126 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9127 && !parser->greater_than_is_operator_p) \
9128 ? PREC_NOT_OPERATOR \
9129 : binops_by_token[token->type].prec)
9132 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9133 bool no_toplevel_fold_p
,
9135 enum cp_parser_prec prec
,
9138 cp_parser_expression_stack stack
;
9139 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9140 cp_parser_expression_stack_entry current
;
9143 enum tree_code rhs_type
;
9144 enum cp_parser_prec new_prec
, lookahead_prec
;
9147 /* Parse the first expression. */
9148 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9149 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9150 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9151 cast_p
, decltype_p
, pidk
);
9152 current
.prec
= prec
;
9154 if (cp_parser_error_occurred (parser
))
9155 return error_mark_node
;
9159 /* Get an operator token. */
9160 token
= cp_lexer_peek_token (parser
->lexer
);
9162 if (warn_cxx11_compat
9163 && token
->type
== CPP_RSHIFT
9164 && !parser
->greater_than_is_operator_p
)
9166 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9167 "%<>>%> operator is treated"
9168 " as two right angle brackets in C++11"))
9169 inform (token
->location
,
9170 "suggest parentheses around %<>>%> expression");
9173 new_prec
= TOKEN_PRECEDENCE (token
);
9174 if (new_prec
!= PREC_NOT_OPERATOR
9175 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9176 /* This is a fold-expression; handle it later. */
9177 new_prec
= PREC_NOT_OPERATOR
;
9179 /* Popping an entry off the stack means we completed a subexpression:
9180 - either we found a token which is not an operator (`>' where it is not
9181 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9182 will happen repeatedly;
9183 - or, we found an operator which has lower priority. This is the case
9184 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9186 if (new_prec
<= current
.prec
)
9195 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9196 current
.loc
= token
->location
;
9198 /* We used the operator token. */
9199 cp_lexer_consume_token (parser
->lexer
);
9201 /* For "false && x" or "true || x", x will never be executed;
9202 disable warnings while evaluating it. */
9203 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9204 c_inhibit_evaluation_warnings
+=
9205 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9206 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9207 c_inhibit_evaluation_warnings
+=
9208 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9210 /* Extract another operand. It may be the RHS of this expression
9211 or the LHS of a new, higher priority expression. */
9212 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9213 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9214 rhs
= cp_parser_simple_cast_expression (parser
);
9216 /* Get another operator token. Look up its precedence to avoid
9217 building a useless (immediately popped) stack entry for common
9218 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9219 token
= cp_lexer_peek_token (parser
->lexer
);
9220 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9221 if (lookahead_prec
!= PREC_NOT_OPERATOR
9222 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9223 lookahead_prec
= PREC_NOT_OPERATOR
;
9224 if (lookahead_prec
> new_prec
)
9226 /* ... and prepare to parse the RHS of the new, higher priority
9227 expression. Since precedence levels on the stack are
9228 monotonically increasing, we do not have to care about
9233 current
.lhs_type
= rhs_type
;
9234 current
.prec
= new_prec
;
9235 new_prec
= lookahead_prec
;
9239 lookahead_prec
= new_prec
;
9240 /* If the stack is not empty, we have parsed into LHS the right side
9241 (`4' in the example above) of an expression we had suspended.
9242 We can use the information on the stack to recover the LHS (`3')
9243 from the stack together with the tree code (`MULT_EXPR'), and
9244 the precedence of the higher level subexpression
9245 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9246 which will be used to actually build the additive expression. */
9248 rhs_type
= current
.lhs_type
;
9253 /* Undo the disabling of warnings done above. */
9254 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9255 c_inhibit_evaluation_warnings
-=
9256 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9257 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9258 c_inhibit_evaluation_warnings
-=
9259 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9261 if (warn_logical_not_paren
9262 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9263 && current
.lhs_type
== TRUTH_NOT_EXPR
9264 /* Avoid warning for !!x == y. */
9265 && (TREE_CODE (current
.lhs
) != NE_EXPR
9266 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9267 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9268 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9269 /* Avoid warning for !b == y where b is boolean. */
9270 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9271 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9273 /* Avoid warning for !!b == y where b is boolean. */
9274 && (!DECL_P (current
.lhs
)
9275 || TREE_TYPE (current
.lhs
) == NULL_TREE
9276 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9277 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9278 current
.lhs
, maybe_constant_value (rhs
));
9282 location_t combined_loc
= make_location (current
.loc
,
9283 current
.lhs
.get_start (),
9286 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9287 ERROR_MARK for everything that is not a binary expression.
9288 This makes warn_about_parentheses miss some warnings that
9289 involve unary operators. For unary expressions we should
9290 pass the correct tree_code unless the unary expression was
9291 surrounded by parentheses.
9293 if (no_toplevel_fold_p
9294 && lookahead_prec
<= current
.prec
9296 current
.lhs
= build2_loc (combined_loc
,
9298 TREE_CODE_CLASS (current
.tree_type
)
9300 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9304 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9305 current
.lhs
, current
.lhs_type
,
9306 rhs
, rhs_type
, &overload
,
9307 complain_flags (decltype_p
));
9308 /* TODO: build_x_binary_op doesn't always honor the location. */
9309 current
.lhs
.set_location (combined_loc
);
9311 current
.lhs_type
= current
.tree_type
;
9313 /* If the binary operator required the use of an overloaded operator,
9314 then this expression cannot be an integral constant-expression.
9315 An overloaded operator can be used even if both operands are
9316 otherwise permissible in an integral constant-expression if at
9317 least one of the operands is of enumeration type. */
9320 && cp_parser_non_integral_constant_expression (parser
,
9322 return error_mark_node
;
9329 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9330 bool no_toplevel_fold_p
,
9331 enum cp_parser_prec prec
,
9334 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9335 /*decltype*/false, prec
, pidk
);
9338 /* Parse the `? expression : assignment-expression' part of a
9339 conditional-expression. The LOGICAL_OR_EXPR is the
9340 logical-or-expression that started the conditional-expression.
9341 Returns a representation of the entire conditional-expression.
9343 This routine is used by cp_parser_assignment_expression.
9345 ? expression : assignment-expression
9349 ? : assignment-expression */
9352 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9354 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9355 cp_expr assignment_expr
;
9356 struct cp_token
*token
;
9357 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9359 /* Consume the `?' token. */
9360 cp_lexer_consume_token (parser
->lexer
);
9361 token
= cp_lexer_peek_token (parser
->lexer
);
9362 if (cp_parser_allow_gnu_extensions_p (parser
)
9363 && token
->type
== CPP_COLON
)
9365 pedwarn (token
->location
, OPT_Wpedantic
,
9366 "ISO C++ does not allow ?: with omitted middle operand");
9367 /* Implicit true clause. */
9369 c_inhibit_evaluation_warnings
+=
9370 folded_logical_or_expr
== truthvalue_true_node
;
9371 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9375 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9376 parser
->colon_corrects_to_scope_p
= false;
9377 /* Parse the expression. */
9378 c_inhibit_evaluation_warnings
+=
9379 folded_logical_or_expr
== truthvalue_false_node
;
9380 expr
= cp_parser_expression (parser
);
9381 c_inhibit_evaluation_warnings
+=
9382 ((folded_logical_or_expr
== truthvalue_true_node
)
9383 - (folded_logical_or_expr
== truthvalue_false_node
));
9384 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9387 /* The next token should be a `:'. */
9388 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9389 /* Parse the assignment-expression. */
9390 assignment_expr
= cp_parser_assignment_expression (parser
);
9391 c_inhibit_evaluation_warnings
-=
9392 folded_logical_or_expr
== truthvalue_true_node
;
9395 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9396 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9397 with the caret at the "?", ranging from the start of
9398 the logical_or_expr to the end of the assignment_expr. */
9399 loc
= make_location (loc
,
9400 logical_or_expr
.get_start (),
9401 assignment_expr
.get_finish ());
9403 /* Build the conditional-expression. */
9404 return build_x_conditional_expr (loc
, logical_or_expr
,
9407 tf_warning_or_error
);
9410 /* Parse an assignment-expression.
9412 assignment-expression:
9413 conditional-expression
9414 logical-or-expression assignment-operator assignment_expression
9417 CAST_P is true if this expression is the target of a cast.
9418 DECLTYPE_P is true if this expression is the operand of decltype.
9420 Returns a representation for the expression. */
9423 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9424 bool cast_p
, bool decltype_p
)
9428 /* If the next token is the `throw' keyword, then we're looking at
9429 a throw-expression. */
9430 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9431 expr
= cp_parser_throw_expression (parser
);
9432 /* Otherwise, it must be that we are looking at a
9433 logical-or-expression. */
9436 /* Parse the binary expressions (logical-or-expression). */
9437 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9439 PREC_NOT_OPERATOR
, pidk
);
9440 /* If the next token is a `?' then we're actually looking at a
9441 conditional-expression. */
9442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9443 return cp_parser_question_colon_clause (parser
, expr
);
9446 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9448 /* If it's an assignment-operator, we're using the second
9450 enum tree_code assignment_operator
9451 = cp_parser_assignment_operator_opt (parser
);
9452 if (assignment_operator
!= ERROR_MARK
)
9454 bool non_constant_p
;
9456 /* Parse the right-hand side of the assignment. */
9457 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9460 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9461 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9463 /* An assignment may not appear in a
9464 constant-expression. */
9465 if (cp_parser_non_integral_constant_expression (parser
,
9467 return error_mark_node
;
9468 /* Build the assignment expression. Its default
9472 is the location of the '=' token as the
9473 caret, ranging from the start of the lhs to the
9475 loc
= make_location (loc
,
9478 expr
= build_x_modify_expr (loc
, expr
,
9479 assignment_operator
,
9481 complain_flags (decltype_p
));
9482 /* TODO: build_x_modify_expr doesn't honor the location,
9483 so we must set it here. */
9484 expr
.set_location (loc
);
9492 /* Parse an (optional) assignment-operator.
9494 assignment-operator: one of
9495 = *= /= %= += -= >>= <<= &= ^= |=
9499 assignment-operator: one of
9502 If the next token is an assignment operator, the corresponding tree
9503 code is returned, and the token is consumed. For example, for
9504 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9505 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9506 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9507 operator, ERROR_MARK is returned. */
9509 static enum tree_code
9510 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9515 /* Peek at the next token. */
9516 token
= cp_lexer_peek_token (parser
->lexer
);
9518 switch (token
->type
)
9529 op
= TRUNC_DIV_EXPR
;
9533 op
= TRUNC_MOD_EXPR
;
9565 /* Nothing else is an assignment operator. */
9569 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9570 if (op
!= ERROR_MARK
9571 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9574 /* If it was an assignment operator, consume it. */
9575 if (op
!= ERROR_MARK
)
9576 cp_lexer_consume_token (parser
->lexer
);
9581 /* Parse an expression.
9584 assignment-expression
9585 expression , assignment-expression
9587 CAST_P is true if this expression is the target of a cast.
9588 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9589 except possibly parenthesized or on the RHS of a comma (N3276).
9591 Returns a representation of the expression. */
9594 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9595 bool cast_p
, bool decltype_p
)
9597 cp_expr expression
= NULL_TREE
;
9598 location_t loc
= UNKNOWN_LOCATION
;
9602 cp_expr assignment_expression
;
9604 /* Parse the next assignment-expression. */
9605 assignment_expression
9606 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9608 /* We don't create a temporary for a call that is the immediate operand
9609 of decltype or on the RHS of a comma. But when we see a comma, we
9610 need to create a temporary for a call on the LHS. */
9611 if (decltype_p
&& !processing_template_decl
9612 && TREE_CODE (assignment_expression
) == CALL_EXPR
9613 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9614 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9615 assignment_expression
9616 = build_cplus_new (TREE_TYPE (assignment_expression
),
9617 assignment_expression
, tf_warning_or_error
);
9619 /* If this is the first assignment-expression, we can just
9622 expression
= assignment_expression
;
9625 /* Create a location with caret at the comma, ranging
9626 from the start of the LHS to the end of the RHS. */
9627 loc
= make_location (loc
,
9628 expression
.get_start (),
9629 assignment_expression
.get_finish ());
9630 expression
= build_x_compound_expr (loc
, expression
,
9631 assignment_expression
,
9632 complain_flags (decltype_p
));
9633 expression
.set_location (loc
);
9635 /* If the next token is not a comma, or we're in a fold-expression, then
9636 we are done with the expression. */
9637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9638 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9640 /* Consume the `,'. */
9641 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9642 cp_lexer_consume_token (parser
->lexer
);
9643 /* A comma operator cannot appear in a constant-expression. */
9644 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9645 expression
= error_mark_node
;
9651 /* Parse a constant-expression.
9653 constant-expression:
9654 conditional-expression
9656 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9657 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9658 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9659 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9660 only parse a conditional-expression, otherwise parse an
9661 assignment-expression. See below for rationale. */
9664 cp_parser_constant_expression (cp_parser
* parser
,
9665 bool allow_non_constant_p
,
9666 bool *non_constant_p
,
9669 bool saved_integral_constant_expression_p
;
9670 bool saved_allow_non_integral_constant_expression_p
;
9671 bool saved_non_integral_constant_expression_p
;
9674 /* It might seem that we could simply parse the
9675 conditional-expression, and then check to see if it were
9676 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9677 one that the compiler can figure out is constant, possibly after
9678 doing some simplifications or optimizations. The standard has a
9679 precise definition of constant-expression, and we must honor
9680 that, even though it is somewhat more restrictive.
9686 is not a legal declaration, because `(2, 3)' is not a
9687 constant-expression. The `,' operator is forbidden in a
9688 constant-expression. However, GCC's constant-folding machinery
9689 will fold this operation to an INTEGER_CST for `3'. */
9691 /* Save the old settings. */
9692 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9693 saved_allow_non_integral_constant_expression_p
9694 = parser
->allow_non_integral_constant_expression_p
;
9695 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9696 /* We are now parsing a constant-expression. */
9697 parser
->integral_constant_expression_p
= true;
9698 parser
->allow_non_integral_constant_expression_p
9699 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9700 parser
->non_integral_constant_expression_p
= false;
9701 /* Although the grammar says "conditional-expression", when not STRICT_P,
9702 we parse an "assignment-expression", which also permits
9703 "throw-expression" and the use of assignment operators. In the case
9704 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9705 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9706 actually essential that we look for an assignment-expression.
9707 For example, cp_parser_initializer_clauses uses this function to
9708 determine whether a particular assignment-expression is in fact
9712 /* Parse the binary expressions (logical-or-expression). */
9713 expression
= cp_parser_binary_expression (parser
, false, false, false,
9714 PREC_NOT_OPERATOR
, NULL
);
9715 /* If the next token is a `?' then we're actually looking at
9716 a conditional-expression; otherwise we're done. */
9717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9718 expression
= cp_parser_question_colon_clause (parser
, expression
);
9721 expression
= cp_parser_assignment_expression (parser
);
9722 /* Restore the old settings. */
9723 parser
->integral_constant_expression_p
9724 = saved_integral_constant_expression_p
;
9725 parser
->allow_non_integral_constant_expression_p
9726 = saved_allow_non_integral_constant_expression_p
;
9727 if (cxx_dialect
>= cxx11
)
9729 /* Require an rvalue constant expression here; that's what our
9730 callers expect. Reference constant expressions are handled
9731 separately in e.g. cp_parser_template_argument. */
9732 tree decay
= expression
;
9733 if (TREE_TYPE (expression
)
9734 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9735 decay
= build_address (expression
);
9736 bool is_const
= potential_rvalue_constant_expression (decay
);
9737 parser
->non_integral_constant_expression_p
= !is_const
;
9738 if (!is_const
&& !allow_non_constant_p
)
9739 require_potential_rvalue_constant_expression (decay
);
9741 if (allow_non_constant_p
)
9742 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9743 parser
->non_integral_constant_expression_p
9744 = saved_non_integral_constant_expression_p
;
9749 /* Parse __builtin_offsetof.
9751 offsetof-expression:
9752 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9754 offsetof-member-designator:
9756 | offsetof-member-designator "." id-expression
9757 | offsetof-member-designator "[" expression "]"
9758 | offsetof-member-designator "->" id-expression */
9761 cp_parser_builtin_offsetof (cp_parser
*parser
)
9763 int save_ice_p
, save_non_ice_p
;
9768 location_t finish_loc
;
9770 /* We're about to accept non-integral-constant things, but will
9771 definitely yield an integral constant expression. Save and
9772 restore these values around our local parsing. */
9773 save_ice_p
= parser
->integral_constant_expression_p
;
9774 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9776 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9778 /* Consume the "__builtin_offsetof" token. */
9779 cp_lexer_consume_token (parser
->lexer
);
9780 /* Consume the opening `('. */
9781 matching_parens parens
;
9782 parens
.require_open (parser
);
9783 /* Parse the type-id. */
9784 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9785 type
= cp_parser_type_id (parser
);
9786 /* Look for the `,'. */
9787 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9788 token
= cp_lexer_peek_token (parser
->lexer
);
9790 /* Build the (type *)null that begins the traditional offsetof macro. */
9792 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9793 tf_warning_or_error
);
9795 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9796 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9797 true, &dummy
, token
->location
);
9800 token
= cp_lexer_peek_token (parser
->lexer
);
9801 switch (token
->type
)
9803 case CPP_OPEN_SQUARE
:
9804 /* offsetof-member-designator "[" expression "]" */
9805 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9810 /* offsetof-member-designator "->" identifier */
9811 expr
= grok_array_decl (token
->location
, expr
,
9812 integer_zero_node
, false);
9816 /* offsetof-member-designator "." identifier */
9817 cp_lexer_consume_token (parser
->lexer
);
9818 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9823 case CPP_CLOSE_PAREN
:
9824 /* Consume the ")" token. */
9825 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9826 cp_lexer_consume_token (parser
->lexer
);
9830 /* Error. We know the following require will fail, but
9831 that gives the proper error message. */
9832 parens
.require_close (parser
);
9833 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9834 expr
= error_mark_node
;
9840 /* Make a location of the form:
9841 __builtin_offsetof (struct s, f)
9842 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9843 with caret at the type-id, ranging from the start of the
9844 "_builtin_offsetof" token to the close paren. */
9845 loc
= make_location (loc
, start_loc
, finish_loc
);
9846 /* The result will be an INTEGER_CST, so we need to explicitly
9847 preserve the location. */
9848 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9851 parser
->integral_constant_expression_p
= save_ice_p
;
9852 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9854 expr
= expr
.maybe_add_location_wrapper ();
9858 /* Parse a trait expression.
9860 Returns a representation of the expression, the underlying type
9861 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9864 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9867 tree type1
, type2
= NULL_TREE
;
9868 bool binary
= false;
9869 bool variadic
= false;
9873 case RID_HAS_NOTHROW_ASSIGN
:
9874 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9876 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9877 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9879 case RID_HAS_NOTHROW_COPY
:
9880 kind
= CPTK_HAS_NOTHROW_COPY
;
9882 case RID_HAS_TRIVIAL_ASSIGN
:
9883 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9885 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9886 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9888 case RID_HAS_TRIVIAL_COPY
:
9889 kind
= CPTK_HAS_TRIVIAL_COPY
;
9891 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9892 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9894 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9895 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9897 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9898 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9900 case RID_IS_ABSTRACT
:
9901 kind
= CPTK_IS_ABSTRACT
;
9903 case RID_IS_AGGREGATE
:
9904 kind
= CPTK_IS_AGGREGATE
;
9906 case RID_IS_BASE_OF
:
9907 kind
= CPTK_IS_BASE_OF
;
9911 kind
= CPTK_IS_CLASS
;
9914 kind
= CPTK_IS_EMPTY
;
9917 kind
= CPTK_IS_ENUM
;
9920 kind
= CPTK_IS_FINAL
;
9922 case RID_IS_LITERAL_TYPE
:
9923 kind
= CPTK_IS_LITERAL_TYPE
;
9928 case RID_IS_POLYMORPHIC
:
9929 kind
= CPTK_IS_POLYMORPHIC
;
9931 case RID_IS_SAME_AS
:
9932 kind
= CPTK_IS_SAME_AS
;
9935 case RID_IS_STD_LAYOUT
:
9936 kind
= CPTK_IS_STD_LAYOUT
;
9938 case RID_IS_TRIVIAL
:
9939 kind
= CPTK_IS_TRIVIAL
;
9941 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9942 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9945 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9946 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9949 case RID_IS_TRIVIALLY_COPYABLE
:
9950 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9953 kind
= CPTK_IS_UNION
;
9955 case RID_UNDERLYING_TYPE
:
9956 kind
= CPTK_UNDERLYING_TYPE
;
9961 case RID_DIRECT_BASES
:
9962 kind
= CPTK_DIRECT_BASES
;
9964 case RID_IS_ASSIGNABLE
:
9965 kind
= CPTK_IS_ASSIGNABLE
;
9968 case RID_IS_CONSTRUCTIBLE
:
9969 kind
= CPTK_IS_CONSTRUCTIBLE
;
9976 /* Get location of initial token. */
9977 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9979 /* Consume the token. */
9980 cp_lexer_consume_token (parser
->lexer
);
9982 matching_parens parens
;
9983 parens
.require_open (parser
);
9986 type_id_in_expr_sentinel
s (parser
);
9987 type1
= cp_parser_type_id (parser
);
9990 if (type1
== error_mark_node
)
9991 return error_mark_node
;
9995 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9998 type_id_in_expr_sentinel
s (parser
);
9999 type2
= cp_parser_type_id (parser
);
10002 if (type2
== error_mark_node
)
10003 return error_mark_node
;
10007 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10009 cp_lexer_consume_token (parser
->lexer
);
10010 tree elt
= cp_parser_type_id (parser
);
10011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10013 cp_lexer_consume_token (parser
->lexer
);
10014 elt
= make_pack_expansion (elt
);
10016 if (elt
== error_mark_node
)
10017 return error_mark_node
;
10018 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10022 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10023 parens
.require_close (parser
);
10025 /* Construct a location of the form:
10026 __is_trivially_copyable(_Tp)
10027 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10028 with start == caret, finishing at the close-paren. */
10029 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10031 /* Complete the trait expression, which may mean either processing
10032 the trait expr now or saving it for template instantiation. */
10035 case CPTK_UNDERLYING_TYPE
:
10036 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10038 return cp_expr (finish_bases (type1
, false), trait_loc
);
10039 case CPTK_DIRECT_BASES
:
10040 return cp_expr (finish_bases (type1
, true), trait_loc
);
10042 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10046 /* Parse a lambda expression.
10049 lambda-introducer lambda-declarator [opt] compound-statement
10051 Returns a representation of the expression. */
10054 cp_parser_lambda_expression (cp_parser
* parser
)
10056 tree lambda_expr
= build_lambda_expr ();
10059 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10060 cp_token_position start
= 0;
10062 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10064 if (cp_unevaluated_operand
)
10066 if (!token
->error_reported
)
10068 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10069 "lambda-expression in unevaluated context");
10070 token
->error_reported
= true;
10074 else if (parser
->in_template_argument_list_p
)
10076 if (!token
->error_reported
)
10078 error_at (token
->location
, "lambda-expression in template-argument");
10079 token
->error_reported
= true;
10084 /* We may be in the middle of deferred access check. Disable
10086 push_deferring_access_checks (dk_no_deferred
);
10088 cp_parser_lambda_introducer (parser
, lambda_expr
);
10090 type
= begin_lambda_type (lambda_expr
);
10091 if (type
== error_mark_node
)
10092 return error_mark_node
;
10094 record_lambda_scope (lambda_expr
);
10096 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10097 determine_visibility (TYPE_NAME (type
));
10099 /* Now that we've started the type, add the capture fields for any
10100 explicit captures. */
10101 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10104 /* Inside the class, surrounding template-parameter-lists do not apply. */
10105 unsigned int saved_num_template_parameter_lists
10106 = parser
->num_template_parameter_lists
;
10107 unsigned char in_statement
= parser
->in_statement
;
10108 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10109 bool fully_implicit_function_template_p
10110 = parser
->fully_implicit_function_template_p
;
10111 tree implicit_template_parms
= parser
->implicit_template_parms
;
10112 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10113 bool auto_is_implicit_function_template_parm_p
10114 = parser
->auto_is_implicit_function_template_parm_p
;
10116 parser
->num_template_parameter_lists
= 0;
10117 parser
->in_statement
= 0;
10118 parser
->in_switch_statement_p
= false;
10119 parser
->fully_implicit_function_template_p
= false;
10120 parser
->implicit_template_parms
= 0;
10121 parser
->implicit_template_scope
= 0;
10122 parser
->auto_is_implicit_function_template_parm_p
= false;
10124 /* By virtue of defining a local class, a lambda expression has access to
10125 the private variables of enclosing classes. */
10127 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10129 if (ok
&& cp_parser_error_occurred (parser
))
10134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10135 && cp_parser_start_tentative_firewall (parser
))
10137 cp_parser_lambda_body (parser
, lambda_expr
);
10139 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10141 if (cp_parser_skip_to_closing_brace (parser
))
10142 cp_lexer_consume_token (parser
->lexer
);
10145 /* The capture list was built up in reverse order; fix that now. */
10146 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10147 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10150 maybe_add_lambda_conv_op (type
);
10152 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10154 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10155 parser
->in_statement
= in_statement
;
10156 parser
->in_switch_statement_p
= in_switch_statement_p
;
10157 parser
->fully_implicit_function_template_p
10158 = fully_implicit_function_template_p
;
10159 parser
->implicit_template_parms
= implicit_template_parms
;
10160 parser
->implicit_template_scope
= implicit_template_scope
;
10161 parser
->auto_is_implicit_function_template_parm_p
10162 = auto_is_implicit_function_template_parm_p
;
10165 /* This field is only used during parsing of the lambda. */
10166 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10168 /* This lambda shouldn't have any proxies left at this point. */
10169 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10170 /* And now that we're done, push proxies for an enclosing lambda. */
10171 insert_pending_capture_proxies ();
10174 lambda_expr
= build_lambda_object (lambda_expr
);
10176 lambda_expr
= error_mark_node
;
10178 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10180 pop_deferring_access_checks ();
10182 return lambda_expr
;
10185 /* Parse the beginning of a lambda expression.
10188 [ lambda-capture [opt] ]
10190 LAMBDA_EXPR is the current representation of the lambda expression. */
10193 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10195 /* Need commas after the first capture. */
10198 /* Eat the leading `['. */
10199 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10201 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10203 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10204 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10205 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10206 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10208 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10210 cp_lexer_consume_token (parser
->lexer
);
10214 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10216 cp_token
* capture_token
;
10218 tree capture_init_expr
;
10219 cp_id_kind idk
= CP_ID_KIND_NONE
;
10220 bool explicit_init_p
= false;
10222 enum capture_kind_type
10227 enum capture_kind_type capture_kind
= BY_COPY
;
10229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10231 error ("expected end of capture-list");
10238 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10240 /* Possibly capture `this'. */
10241 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10243 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10244 if (cxx_dialect
< cxx2a
10245 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10246 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10247 "with by-copy capture default");
10248 cp_lexer_consume_token (parser
->lexer
);
10249 add_capture (lambda_expr
,
10250 /*id=*/this_identifier
,
10251 /*initializer=*/finish_this_expr (),
10252 /*by_reference_p=*/true,
10257 /* Possibly capture `*this'. */
10258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10259 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10261 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10262 if (cxx_dialect
< cxx17
)
10263 pedwarn (loc
, 0, "%<*this%> capture only available with "
10264 "-std=c++17 or -std=gnu++17");
10265 cp_lexer_consume_token (parser
->lexer
);
10266 cp_lexer_consume_token (parser
->lexer
);
10267 add_capture (lambda_expr
,
10268 /*id=*/this_identifier
,
10269 /*initializer=*/finish_this_expr (),
10270 /*by_reference_p=*/false,
10275 /* Remember whether we want to capture as a reference or not. */
10276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10278 capture_kind
= BY_REFERENCE
;
10279 cp_lexer_consume_token (parser
->lexer
);
10282 /* Get the identifier. */
10283 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10284 capture_id
= cp_parser_identifier (parser
);
10286 if (capture_id
== error_mark_node
)
10287 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10288 delimiters, but I modified this to stop on unnested ']' as well. It
10289 was already changed to stop on unnested '}', so the
10290 "closing_parenthesis" name is no more misleading with my change. */
10292 cp_parser_skip_to_closing_parenthesis (parser
,
10293 /*recovering=*/true,
10295 /*consume_paren=*/true);
10299 /* Find the initializer for this capture. */
10300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10301 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10302 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10304 bool direct
, non_constant
;
10305 /* An explicit initializer exists. */
10306 if (cxx_dialect
< cxx14
)
10307 pedwarn (input_location
, 0,
10308 "lambda capture initializers "
10309 "only available with -std=c++14 or -std=gnu++14");
10310 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10312 explicit_init_p
= true;
10313 if (capture_init_expr
== NULL_TREE
)
10315 error ("empty initializer for lambda init-capture");
10316 capture_init_expr
= error_mark_node
;
10321 const char* error_msg
;
10323 /* Turn the identifier into an id-expression. */
10325 = cp_parser_lookup_name_simple (parser
, capture_id
,
10326 capture_token
->location
);
10328 if (capture_init_expr
== error_mark_node
)
10330 unqualified_name_lookup_error (capture_id
);
10333 else if (DECL_P (capture_init_expr
)
10334 && (!VAR_P (capture_init_expr
)
10335 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10337 error_at (capture_token
->location
,
10338 "capture of non-variable %qD ",
10339 capture_init_expr
);
10340 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10341 "%q#D declared here", capture_init_expr
);
10344 if (VAR_P (capture_init_expr
)
10345 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10347 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10348 "%qD with non-automatic storage duration",
10349 capture_init_expr
))
10350 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10351 "%q#D declared here", capture_init_expr
);
10356 = finish_id_expression
10361 /*integral_constant_expression_p=*/false,
10362 /*allow_non_integral_constant_expression_p=*/false,
10363 /*non_integral_constant_expression_p=*/NULL
,
10364 /*template_p=*/false,
10366 /*address_p=*/false,
10367 /*template_arg_p=*/false,
10369 capture_token
->location
);
10371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10373 cp_lexer_consume_token (parser
->lexer
);
10374 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10377 check_for_bare_parameter_packs (capture_init_expr
);
10380 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10381 && !explicit_init_p
)
10383 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10384 && capture_kind
== BY_COPY
)
10385 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10386 "of %qD redundant with by-copy capture default",
10388 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10389 && capture_kind
== BY_REFERENCE
)
10390 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10391 "capture of %qD redundant with by-reference capture "
10392 "default", capture_id
);
10395 add_capture (lambda_expr
,
10398 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10402 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10405 /* Parse the (optional) middle of a lambda expression.
10408 < template-parameter-list [opt] >
10409 ( parameter-declaration-clause [opt] )
10410 attribute-specifier [opt]
10411 decl-specifier-seq [opt]
10412 exception-specification [opt]
10413 lambda-return-type-clause [opt]
10415 LAMBDA_EXPR is the current representation of the lambda expression. */
10418 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10420 /* 5.1.1.4 of the standard says:
10421 If a lambda-expression does not include a lambda-declarator, it is as if
10422 the lambda-declarator were ().
10423 This means an empty parameter list, no attributes, and no exception
10425 tree param_list
= void_list_node
;
10426 tree attributes
= NULL_TREE
;
10427 tree exception_spec
= NULL_TREE
;
10428 tree template_param_list
= NULL_TREE
;
10429 tree tx_qual
= NULL_TREE
;
10430 tree return_type
= NULL_TREE
;
10431 cp_decl_specifier_seq lambda_specs
;
10432 clear_decl_specs (&lambda_specs
);
10434 /* The template-parameter-list is optional, but must begin with
10435 an opening angle if present. */
10436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10438 if (cxx_dialect
< cxx14
)
10439 pedwarn (parser
->lexer
->next_token
->location
, 0,
10440 "lambda templates are only available with "
10441 "-std=c++14 or -std=gnu++14");
10442 else if (cxx_dialect
< cxx2a
)
10443 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10444 "lambda templates are only available with "
10445 "-std=c++2a or -std=gnu++2a");
10447 cp_lexer_consume_token (parser
->lexer
);
10449 template_param_list
= cp_parser_template_parameter_list (parser
);
10451 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10453 /* We just processed one more parameter list. */
10454 ++parser
->num_template_parameter_lists
;
10457 /* The parameter-declaration-clause is optional (unless
10458 template-parameter-list was given), but must begin with an
10459 opening parenthesis if present. */
10460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10462 matching_parens parens
;
10463 parens
.consume_open (parser
);
10465 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10467 /* Parse parameters. */
10468 param_list
= cp_parser_parameter_declaration_clause (parser
);
10470 /* Default arguments shall not be specified in the
10471 parameter-declaration-clause of a lambda-declarator. */
10472 if (cxx_dialect
< cxx14
)
10473 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10474 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10475 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10476 "default argument specified for lambda parameter");
10478 parens
.require_close (parser
);
10480 attributes
= cp_parser_attributes_opt (parser
);
10482 /* In the decl-specifier-seq of the lambda-declarator, each
10483 decl-specifier shall either be mutable or constexpr. */
10484 int declares_class_or_enum
;
10485 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10486 cp_parser_decl_specifier_seq (parser
,
10487 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10488 &lambda_specs
, &declares_class_or_enum
);
10489 if (lambda_specs
.storage_class
== sc_mutable
)
10491 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10492 if (lambda_specs
.conflicting_specifiers_p
)
10493 error_at (lambda_specs
.locations
[ds_storage_class
],
10494 "duplicate %<mutable%>");
10497 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10499 /* Parse optional exception specification. */
10500 exception_spec
= cp_parser_exception_specification_opt (parser
);
10502 /* Parse optional trailing return type. */
10503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10505 cp_lexer_consume_token (parser
->lexer
);
10506 return_type
= cp_parser_trailing_type_id (parser
);
10509 /* The function parameters must be in scope all the way until after the
10510 trailing-return-type in case of decltype. */
10511 pop_bindings_and_leave_scope ();
10513 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10514 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10516 /* Create the function call operator.
10518 Messing with declarators like this is no uglier than building up the
10519 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10522 cp_decl_specifier_seq return_type_specs
;
10523 cp_declarator
* declarator
;
10528 clear_decl_specs (&return_type_specs
);
10530 return_type_specs
.type
= return_type
;
10532 /* Maybe we will deduce the return type later. */
10533 return_type_specs
.type
= make_auto ();
10535 if (lambda_specs
.locations
[ds_constexpr
])
10537 if (cxx_dialect
>= cxx17
)
10538 return_type_specs
.locations
[ds_constexpr
]
10539 = lambda_specs
.locations
[ds_constexpr
];
10541 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10542 "lambda only available with -std=c++17 or -std=gnu++17");
10545 p
= obstack_alloc (&declarator_obstack
, 0);
10547 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10549 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10550 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10551 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10552 VIRT_SPEC_UNSPECIFIED
,
10556 /*late_return_type=*/NULL_TREE
,
10557 /*requires_clause*/NULL_TREE
);
10558 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10560 fco
= grokmethod (&return_type_specs
,
10563 if (fco
!= error_mark_node
)
10565 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10566 DECL_ARTIFICIAL (fco
) = 1;
10567 /* Give the object parameter a different name. */
10568 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10570 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10572 if (template_param_list
)
10574 fco
= finish_member_template_decl (fco
);
10575 finish_template_decl (template_param_list
);
10576 --parser
->num_template_parameter_lists
;
10578 else if (parser
->fully_implicit_function_template_p
)
10579 fco
= finish_fully_implicit_template (parser
, fco
);
10581 finish_member_declaration (fco
);
10583 obstack_free (&declarator_obstack
, p
);
10585 return (fco
!= error_mark_node
);
10589 /* Parse the body of a lambda expression, which is simply
10593 but which requires special handling.
10594 LAMBDA_EXPR is the current representation of the lambda expression. */
10597 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10599 bool nested
= (current_function_decl
!= NULL_TREE
);
10600 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10601 bool in_function_body
= parser
->in_function_body
;
10604 push_function_context ();
10606 /* Still increment function_depth so that we don't GC in the
10607 middle of an expression. */
10610 vec
<tree
> omp_privatization_save
;
10611 save_omp_privatization_clauses (omp_privatization_save
);
10612 /* Clear this in case we're in the middle of a default argument. */
10613 parser
->local_variables_forbidden_p
= false;
10614 parser
->in_function_body
= true;
10617 local_specialization_stack
s (lss_copy
);
10618 tree fco
= lambda_function (lambda_expr
);
10619 tree body
= start_lambda_function (fco
, lambda_expr
);
10620 matching_braces braces
;
10622 if (braces
.require_open (parser
))
10624 tree compound_stmt
= begin_compound_stmt (0);
10626 /* Originally C++11 required us to peek for 'return expr'; and
10627 process it specially here to deduce the return type. N3638
10628 removed the need for that. */
10630 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10631 cp_parser_label_declaration (parser
);
10632 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10633 braces
.require_close (parser
);
10635 finish_compound_stmt (compound_stmt
);
10638 finish_lambda_function (body
);
10641 restore_omp_privatization_clauses (omp_privatization_save
);
10642 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10643 parser
->in_function_body
= in_function_body
;
10645 pop_function_context();
10650 /* Statements [gram.stmt.stmt] */
10652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10655 add_debug_begin_stmt (location_t loc
)
10657 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10660 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10661 SET_EXPR_LOCATION (stmt
, loc
);
10665 /* Parse a statement.
10669 expression-statement
10671 selection-statement
10672 iteration-statement
10674 declaration-statement
10681 attribute-specifier-seq (opt) expression-statement
10682 attribute-specifier-seq (opt) compound-statement
10683 attribute-specifier-seq (opt) selection-statement
10684 attribute-specifier-seq (opt) iteration-statement
10685 attribute-specifier-seq (opt) jump-statement
10686 declaration-statement
10687 attribute-specifier-seq (opt) try-block
10690 expression-statement
10698 IN_COMPOUND is true when the statement is nested inside a
10699 cp_parser_compound_statement; this matters for certain pragmas.
10701 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10702 is a (possibly labeled) if statement which is not enclosed in braces
10703 and has an else clause. This is used to implement -Wparentheses.
10705 CHAIN is a vector of if-else-if conditions. */
10708 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10709 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10710 location_t
*loc_after_labels
)
10712 tree statement
, std_attrs
= NULL_TREE
;
10714 location_t statement_location
, attrs_location
;
10719 /* There is no statement yet. */
10720 statement
= NULL_TREE
;
10722 saved_token_sentinel
saved_tokens (parser
->lexer
);
10723 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10724 if (c_dialect_objc ())
10725 /* In obj-c++, seeing '[[' might be the either the beginning of
10726 c++11 attributes, or a nested objc-message-expression. So
10727 let's parse the c++11 attributes tentatively. */
10728 cp_parser_parse_tentatively (parser
);
10729 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10730 if (c_dialect_objc ())
10732 if (!cp_parser_parse_definitely (parser
))
10733 std_attrs
= NULL_TREE
;
10736 /* Peek at the next token. */
10737 token
= cp_lexer_peek_token (parser
->lexer
);
10738 /* Remember the location of the first token in the statement. */
10739 statement_location
= token
->location
;
10740 add_debug_begin_stmt (statement_location
);
10741 /* If this is a keyword, then that will often determine what kind of
10742 statement we have. */
10743 if (token
->type
== CPP_KEYWORD
)
10745 enum rid keyword
= token
->keyword
;
10751 /* Looks like a labeled-statement with a case label.
10752 Parse the label, and then use tail recursion to parse
10754 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10755 in_compound
= false;
10760 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10766 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10773 statement
= cp_parser_jump_statement (parser
);
10776 /* Objective-C++ exception-handling constructs. */
10779 case RID_AT_FINALLY
:
10780 case RID_AT_SYNCHRONIZED
:
10782 statement
= cp_parser_objc_statement (parser
);
10786 statement
= cp_parser_try_block (parser
);
10789 case RID_NAMESPACE
:
10790 /* This must be a namespace alias definition. */
10791 cp_parser_declaration_statement (parser
);
10794 case RID_TRANSACTION_ATOMIC
:
10795 case RID_TRANSACTION_RELAXED
:
10796 case RID_SYNCHRONIZED
:
10797 case RID_ATOMIC_NOEXCEPT
:
10798 case RID_ATOMIC_CANCEL
:
10799 statement
= cp_parser_transaction (parser
, token
);
10801 case RID_TRANSACTION_CANCEL
:
10802 statement
= cp_parser_transaction_cancel (parser
);
10806 /* It might be a keyword like `int' that can start a
10807 declaration-statement. */
10811 else if (token
->type
== CPP_NAME
)
10813 /* If the next token is a `:', then we are looking at a
10814 labeled-statement. */
10815 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10816 if (token
->type
== CPP_COLON
)
10818 /* Looks like a labeled-statement with an ordinary label.
10819 Parse the label, and then use tail recursion to parse
10822 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10823 in_compound
= false;
10827 /* Anything that starts with a `{' must be a compound-statement. */
10828 else if (token
->type
== CPP_OPEN_BRACE
)
10829 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10830 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10831 a statement all its own. */
10832 else if (token
->type
== CPP_PRAGMA
)
10834 /* Only certain OpenMP pragmas are attached to statements, and thus
10835 are considered statements themselves. All others are not. In
10836 the context of a compound, accept the pragma as a "statement" and
10837 return so that we can check for a close brace. Otherwise we
10838 require a real statement and must go back and read one. */
10840 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10841 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10845 else if (token
->type
== CPP_EOF
)
10847 cp_parser_error (parser
, "expected statement");
10851 /* Everything else must be a declaration-statement or an
10852 expression-statement. Try for the declaration-statement
10853 first, unless we are looking at a `;', in which case we know that
10854 we have an expression-statement. */
10857 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10859 if (std_attrs
!= NULL_TREE
)
10861 /* Attributes should be parsed as part of the the
10862 declaration, so let's un-parse them. */
10863 saved_tokens
.rollback();
10864 std_attrs
= NULL_TREE
;
10867 cp_parser_parse_tentatively (parser
);
10868 /* Try to parse the declaration-statement. */
10869 cp_parser_declaration_statement (parser
);
10870 /* If that worked, we're done. */
10871 if (cp_parser_parse_definitely (parser
))
10874 /* All preceding labels have been parsed at this point. */
10875 if (loc_after_labels
!= NULL
)
10876 *loc_after_labels
= statement_location
;
10878 /* Look for an expression-statement instead. */
10879 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10881 /* Handle [[fallthrough]];. */
10882 if (attribute_fallthrough_p (std_attrs
))
10884 /* The next token after the fallthrough attribute is ';'. */
10885 if (statement
== NULL_TREE
)
10887 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10888 statement
= build_call_expr_internal_loc (statement_location
,
10890 void_type_node
, 0);
10891 finish_expr_stmt (statement
);
10894 warning_at (statement_location
, OPT_Wattributes
,
10895 "%<fallthrough%> attribute not followed by %<;%>");
10896 std_attrs
= NULL_TREE
;
10900 /* Set the line number for the statement. */
10901 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10902 SET_EXPR_LOCATION (statement
, statement_location
);
10904 /* Allow "[[fallthrough]];", but warn otherwise. */
10905 if (std_attrs
!= NULL_TREE
)
10906 warning_at (attrs_location
,
10908 "attributes at the beginning of statement are ignored");
10911 /* Parse the label for a labeled-statement, i.e.
10914 case constant-expression :
10918 case constant-expression ... constant-expression : statement
10920 When a label is parsed without errors, the label is added to the
10921 parse tree by the finish_* functions, so this function doesn't
10922 have to return the label. */
10925 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10928 tree label
= NULL_TREE
;
10929 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10931 /* The next token should be an identifier. */
10932 token
= cp_lexer_peek_token (parser
->lexer
);
10933 if (token
->type
!= CPP_NAME
10934 && token
->type
!= CPP_KEYWORD
)
10936 cp_parser_error (parser
, "expected labeled-statement");
10940 /* Remember whether this case or a user-defined label is allowed to fall
10942 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10944 parser
->colon_corrects_to_scope_p
= false;
10945 switch (token
->keyword
)
10949 tree expr
, expr_hi
;
10950 cp_token
*ellipsis
;
10952 /* Consume the `case' token. */
10953 cp_lexer_consume_token (parser
->lexer
);
10954 /* Parse the constant-expression. */
10955 expr
= cp_parser_constant_expression (parser
);
10956 if (check_for_bare_parameter_packs (expr
))
10957 expr
= error_mark_node
;
10959 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10960 if (ellipsis
->type
== CPP_ELLIPSIS
)
10962 /* Consume the `...' token. */
10963 cp_lexer_consume_token (parser
->lexer
);
10964 expr_hi
= cp_parser_constant_expression (parser
);
10965 if (check_for_bare_parameter_packs (expr_hi
))
10966 expr_hi
= error_mark_node
;
10968 /* We don't need to emit warnings here, as the common code
10969 will do this for us. */
10972 expr_hi
= NULL_TREE
;
10974 if (parser
->in_switch_statement_p
)
10976 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
10977 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10978 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10981 error_at (token
->location
,
10982 "case label %qE not within a switch statement",
10988 /* Consume the `default' token. */
10989 cp_lexer_consume_token (parser
->lexer
);
10991 if (parser
->in_switch_statement_p
)
10993 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
10994 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10995 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10998 error_at (token
->location
, "case label not within a switch statement");
11002 /* Anything else must be an ordinary label. */
11003 label
= finish_label_stmt (cp_parser_identifier (parser
));
11004 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11005 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11009 /* Require the `:' token. */
11010 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11012 /* An ordinary label may optionally be followed by attributes.
11013 However, this is only permitted if the attributes are then
11014 followed by a semicolon. This is because, for backward
11015 compatibility, when parsing
11016 lab: __attribute__ ((unused)) int i;
11017 we want the attribute to attach to "i", not "lab". */
11018 if (label
!= NULL_TREE
11019 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11022 cp_parser_parse_tentatively (parser
);
11023 attrs
= cp_parser_gnu_attributes_opt (parser
);
11024 if (attrs
== NULL_TREE
11025 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11026 cp_parser_abort_tentative_parse (parser
);
11027 else if (!cp_parser_parse_definitely (parser
))
11030 attributes
= chainon (attributes
, attrs
);
11033 if (attributes
!= NULL_TREE
)
11034 cplus_decl_attributes (&label
, attributes
, 0);
11036 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11039 /* Parse an expression-statement.
11041 expression-statement:
11044 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11045 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11046 indicates whether this expression-statement is part of an
11047 expression statement. */
11050 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11052 tree statement
= NULL_TREE
;
11053 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11054 location_t loc
= token
->location
;
11056 /* There might be attribute fallthrough. */
11057 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11059 /* If the next token is a ';', then there is no expression
11061 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11063 statement
= cp_parser_expression (parser
);
11064 if (statement
== error_mark_node
11065 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11067 cp_parser_skip_to_end_of_block_or_statement (parser
);
11068 return error_mark_node
;
11072 /* Handle [[fallthrough]];. */
11073 if (attribute_fallthrough_p (attr
))
11075 /* The next token after the fallthrough attribute is ';'. */
11076 if (statement
== NULL_TREE
)
11077 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11078 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11079 void_type_node
, 0);
11081 warning_at (loc
, OPT_Wattributes
,
11082 "%<fallthrough%> attribute not followed by %<;%>");
11086 /* Allow "[[fallthrough]];", but warn otherwise. */
11087 if (attr
!= NULL_TREE
)
11088 warning_at (loc
, OPT_Wattributes
,
11089 "attributes at the beginning of statement are ignored");
11091 /* Give a helpful message for "A<T>::type t;" and the like. */
11092 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11093 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11095 if (TREE_CODE (statement
) == SCOPE_REF
)
11096 error_at (token
->location
, "need %<typename%> before %qE because "
11097 "%qT is a dependent scope",
11098 statement
, TREE_OPERAND (statement
, 0));
11099 else if (is_overloaded_fn (statement
)
11100 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11103 tree fn
= get_first_fn (statement
);
11104 error_at (token
->location
,
11105 "%<%T::%D%> names the constructor, not the type",
11106 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11110 /* Consume the final `;'. */
11111 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11113 if (in_statement_expr
11114 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11115 /* This is the final expression statement of a statement
11117 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11118 else if (statement
)
11119 statement
= finish_expr_stmt (statement
);
11124 /* Parse a compound-statement.
11126 compound-statement:
11127 { statement-seq [opt] }
11131 compound-statement:
11132 { label-declaration-seq [opt] statement-seq [opt] }
11134 label-declaration-seq:
11136 label-declaration-seq label-declaration
11138 Returns a tree representing the statement. */
11141 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11142 int bcs_flags
, bool function_body
)
11144 tree compound_stmt
;
11145 matching_braces braces
;
11147 /* Consume the `{'. */
11148 if (!braces
.require_open (parser
))
11149 return error_mark_node
;
11150 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11151 && !function_body
&& cxx_dialect
< cxx14
)
11152 pedwarn (input_location
, OPT_Wpedantic
,
11153 "compound-statement in %<constexpr%> function");
11154 /* Begin the compound-statement. */
11155 compound_stmt
= begin_compound_stmt (bcs_flags
);
11156 /* If the next keyword is `__label__' we have a label declaration. */
11157 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11158 cp_parser_label_declaration (parser
);
11159 /* Parse an (optional) statement-seq. */
11160 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11161 /* Finish the compound-statement. */
11162 finish_compound_stmt (compound_stmt
);
11163 /* Consume the `}'. */
11164 braces
.require_close (parser
);
11166 return compound_stmt
;
11169 /* Parse an (optional) statement-seq.
11173 statement-seq [opt] statement */
11176 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11178 /* Scan statements until there aren't any more. */
11181 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11183 /* If we are looking at a `}', then we have run out of
11184 statements; the same is true if we have reached the end
11185 of file, or have stumbled upon a stray '@end'. */
11186 if (token
->type
== CPP_CLOSE_BRACE
11187 || token
->type
== CPP_EOF
11188 || token
->type
== CPP_PRAGMA_EOL
11189 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11192 /* If we are in a compound statement and find 'else' then
11193 something went wrong. */
11194 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11196 if (parser
->in_statement
& IN_IF_STMT
)
11200 token
= cp_lexer_consume_token (parser
->lexer
);
11201 error_at (token
->location
, "%<else%> without a previous %<if%>");
11205 /* Parse the statement. */
11206 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11210 /* Return true if we're looking at (init; cond), false otherwise. */
11213 cp_parser_init_statement_p (cp_parser
*parser
)
11215 /* Save tokens so that we can put them back. */
11216 cp_lexer_save_tokens (parser
->lexer
);
11218 /* Look for ';' that is not nested in () or {}. */
11219 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11220 /*recovering=*/false,
11222 /*consume_paren=*/false);
11224 /* Roll back the tokens we skipped. */
11225 cp_lexer_rollback_tokens (parser
->lexer
);
11230 /* Parse a selection-statement.
11232 selection-statement:
11233 if ( init-statement [opt] condition ) statement
11234 if ( init-statement [opt] condition ) statement else statement
11235 switch ( init-statement [opt] condition ) statement
11237 Returns the new IF_STMT or SWITCH_STMT.
11239 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11240 is a (possibly labeled) if statement which is not enclosed in
11241 braces and has an else clause. This is used to implement
11244 CHAIN is a vector of if-else-if conditions. This is used to implement
11245 -Wduplicated-cond. */
11248 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11253 token_indent_info guard_tinfo
;
11258 /* Peek at the next token. */
11259 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11260 guard_tinfo
= get_token_indent_info (token
);
11262 /* See what kind of keyword it is. */
11263 keyword
= token
->keyword
;
11273 if (keyword
== RID_IF
11274 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11278 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11279 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11280 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11281 "with -std=c++17 or -std=gnu++17");
11284 /* Look for the `('. */
11285 matching_parens parens
;
11286 if (!parens
.require_open (parser
))
11288 cp_parser_skip_to_end_of_statement (parser
);
11289 return error_mark_node
;
11292 /* Begin the selection-statement. */
11293 if (keyword
== RID_IF
)
11295 statement
= begin_if_stmt ();
11296 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11299 statement
= begin_switch_stmt ();
11301 /* Parse the optional init-statement. */
11302 if (cp_parser_init_statement_p (parser
))
11305 if (cxx_dialect
< cxx17
)
11306 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11307 "init-statement in selection statements only available "
11308 "with -std=c++17 or -std=gnu++17");
11309 cp_parser_init_statement (parser
, &decl
);
11312 /* Parse the condition. */
11313 condition
= cp_parser_condition (parser
);
11314 /* Look for the `)'. */
11315 if (!parens
.require_close (parser
))
11316 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11317 /*consume_paren=*/true);
11319 if (keyword
== RID_IF
)
11322 unsigned char in_statement
;
11324 /* Add the condition. */
11325 condition
= finish_if_stmt_cond (condition
, statement
);
11327 if (warn_duplicated_cond
)
11328 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11331 /* Parse the then-clause. */
11332 in_statement
= parser
->in_statement
;
11333 parser
->in_statement
|= IN_IF_STMT
;
11335 /* Outside a template, the non-selected branch of a constexpr
11336 if is a 'discarded statement', i.e. unevaluated. */
11337 bool was_discarded
= in_discarded_stmt
;
11338 bool discard_then
= (cx
&& !processing_template_decl
11339 && integer_zerop (condition
));
11342 in_discarded_stmt
= true;
11343 ++c_inhibit_evaluation_warnings
;
11346 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11349 parser
->in_statement
= in_statement
;
11351 finish_then_clause (statement
);
11355 THEN_CLAUSE (statement
) = NULL_TREE
;
11356 in_discarded_stmt
= was_discarded
;
11357 --c_inhibit_evaluation_warnings
;
11360 /* If the next token is `else', parse the else-clause. */
11361 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11364 bool discard_else
= (cx
&& !processing_template_decl
11365 && integer_nonzerop (condition
));
11368 in_discarded_stmt
= true;
11369 ++c_inhibit_evaluation_warnings
;
11373 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11374 /* Consume the `else' keyword. */
11375 cp_lexer_consume_token (parser
->lexer
);
11376 if (warn_duplicated_cond
)
11378 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11382 /* We've got "if (COND) else if (COND2)". Start
11383 the condition chain and add COND as the first
11385 chain
= new vec
<tree
> ();
11386 if (!CONSTANT_CLASS_P (condition
)
11387 && !TREE_SIDE_EFFECTS (condition
))
11389 /* Wrap it in a NOP_EXPR so that we can set the
11390 location of the condition. */
11391 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11393 SET_EXPR_LOCATION (e
, token
->location
);
11394 chain
->safe_push (e
);
11397 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11400 /* This is if-else without subsequent if. Zap the
11401 condition chain; we would have already warned at
11407 begin_else_clause (statement
);
11408 /* Parse the else-clause. */
11409 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11410 guard_tinfo
, chain
);
11412 finish_else_clause (statement
);
11414 /* If we are currently parsing a then-clause, then
11415 IF_P will not be NULL. We set it to true to
11416 indicate that this if statement has an else clause.
11417 This may trigger the Wparentheses warning below
11418 when we get back up to the parent if statement. */
11424 ELSE_CLAUSE (statement
) = NULL_TREE
;
11425 in_discarded_stmt
= was_discarded
;
11426 --c_inhibit_evaluation_warnings
;
11431 /* This if statement does not have an else clause. If
11432 NESTED_IF is true, then the then-clause has an if
11433 statement which does have an else clause. We warn
11434 about the potential ambiguity. */
11436 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11437 "suggest explicit braces to avoid ambiguous"
11439 if (warn_duplicated_cond
)
11441 /* We don't need the condition chain anymore. */
11447 /* Now we're all done with the if-statement. */
11448 finish_if_stmt (statement
);
11452 bool in_switch_statement_p
;
11453 unsigned char in_statement
;
11455 /* Add the condition. */
11456 finish_switch_cond (condition
, statement
);
11458 /* Parse the body of the switch-statement. */
11459 in_switch_statement_p
= parser
->in_switch_statement_p
;
11460 in_statement
= parser
->in_statement
;
11461 parser
->in_switch_statement_p
= true;
11462 parser
->in_statement
|= IN_SWITCH_STMT
;
11463 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11465 parser
->in_switch_statement_p
= in_switch_statement_p
;
11466 parser
->in_statement
= in_statement
;
11468 /* Now we're all done with the switch-statement. */
11469 finish_switch_stmt (statement
);
11477 cp_parser_error (parser
, "expected selection-statement");
11478 return error_mark_node
;
11482 /* Parse a condition.
11486 type-specifier-seq declarator = initializer-clause
11487 type-specifier-seq declarator braced-init-list
11492 type-specifier-seq declarator asm-specification [opt]
11493 attributes [opt] = assignment-expression
11495 Returns the expression that should be tested. */
11498 cp_parser_condition (cp_parser
* parser
)
11500 cp_decl_specifier_seq type_specifiers
;
11501 const char *saved_message
;
11502 int declares_class_or_enum
;
11504 /* Try the declaration first. */
11505 cp_parser_parse_tentatively (parser
);
11506 /* New types are not allowed in the type-specifier-seq for a
11508 saved_message
= parser
->type_definition_forbidden_message
;
11509 parser
->type_definition_forbidden_message
11510 = G_("types may not be defined in conditions");
11511 /* Parse the type-specifier-seq. */
11512 cp_parser_decl_specifier_seq (parser
,
11513 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11515 &declares_class_or_enum
);
11516 /* Restore the saved message. */
11517 parser
->type_definition_forbidden_message
= saved_message
;
11518 /* If all is well, we might be looking at a declaration. */
11519 if (!cp_parser_error_occurred (parser
))
11522 tree asm_specification
;
11524 cp_declarator
*declarator
;
11525 tree initializer
= NULL_TREE
;
11527 /* Parse the declarator. */
11528 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11529 /*ctor_dtor_or_conv_p=*/NULL
,
11530 /*parenthesized_p=*/NULL
,
11531 /*member_p=*/false,
11532 /*friend_p=*/false);
11533 /* Parse the attributes. */
11534 attributes
= cp_parser_attributes_opt (parser
);
11535 /* Parse the asm-specification. */
11536 asm_specification
= cp_parser_asm_specification_opt (parser
);
11537 /* If the next token is not an `=' or '{', then we might still be
11538 looking at an expression. For example:
11542 looks like a decl-specifier-seq and a declarator -- but then
11543 there is no `=', so this is an expression. */
11544 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11545 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11546 cp_parser_simulate_error (parser
);
11548 /* If we did see an `=' or '{', then we are looking at a declaration
11550 if (cp_parser_parse_definitely (parser
))
11553 bool non_constant_p
;
11554 int flags
= LOOKUP_ONLYCONVERTING
;
11556 /* Create the declaration. */
11557 decl
= start_decl (declarator
, &type_specifiers
,
11558 /*initialized_p=*/true,
11559 attributes
, /*prefix_attributes=*/NULL_TREE
,
11562 /* Parse the initializer. */
11563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11565 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11566 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11571 /* Consume the `='. */
11572 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11573 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11575 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11576 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11578 /* Process the initializer. */
11579 cp_finish_decl (decl
,
11580 initializer
, !non_constant_p
,
11585 pop_scope (pushed_scope
);
11587 return convert_from_reference (decl
);
11590 /* If we didn't even get past the declarator successfully, we are
11591 definitely not looking at a declaration. */
11593 cp_parser_abort_tentative_parse (parser
);
11595 /* Otherwise, we are looking at an expression. */
11596 return cp_parser_expression (parser
);
11599 /* Parses a for-statement or range-for-statement until the closing ')',
11603 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11605 tree init
, scope
, decl
;
11608 /* Begin the for-statement. */
11609 scope
= begin_for_scope (&init
);
11611 /* Parse the initialization. */
11612 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11615 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11617 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11621 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11622 unsigned short unroll
)
11624 /* Normal for loop */
11625 tree condition
= NULL_TREE
;
11626 tree expression
= NULL_TREE
;
11629 stmt
= begin_for_stmt (scope
, init
);
11630 /* The init-statement has already been parsed in
11631 cp_parser_init_statement, so no work is needed here. */
11632 finish_init_stmt (stmt
);
11634 /* If there's a condition, process it. */
11635 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11636 condition
= cp_parser_condition (parser
);
11639 cp_parser_error (parser
, "missing loop condition in loop with "
11640 "%<GCC ivdep%> pragma");
11641 condition
= error_mark_node
;
11645 cp_parser_error (parser
, "missing loop condition in loop with "
11646 "%<GCC unroll%> pragma");
11647 condition
= error_mark_node
;
11649 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11650 /* Look for the `;'. */
11651 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11653 /* If there's an expression, process it. */
11654 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11655 expression
= cp_parser_expression (parser
);
11656 finish_for_expr (expression
, stmt
);
11661 /* Tries to parse a range-based for-statement:
11664 decl-specifier-seq declarator : expression
11666 The decl-specifier-seq declarator and the `:' are already parsed by
11667 cp_parser_init_statement. If processing_template_decl it returns a
11668 newly created RANGE_FOR_STMT; if not, it is converted to a
11669 regular FOR_STMT. */
11672 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11673 bool ivdep
, unsigned short unroll
)
11675 tree stmt
, range_expr
;
11676 auto_vec
<cxx_binding
*, 16> bindings
;
11677 auto_vec
<tree
, 16> names
;
11678 tree decomp_first_name
= NULL_TREE
;
11679 unsigned int decomp_cnt
= 0;
11681 /* Get the range declaration momentarily out of the way so that
11682 the range expression doesn't clash with it. */
11683 if (range_decl
!= error_mark_node
)
11685 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11687 tree v
= DECL_VALUE_EXPR (range_decl
);
11688 /* For decomposition declaration get all of the corresponding
11689 declarations out of the way. */
11690 if (TREE_CODE (v
) == ARRAY_REF
11691 && VAR_P (TREE_OPERAND (v
, 0))
11692 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11694 tree d
= range_decl
;
11695 range_decl
= TREE_OPERAND (v
, 0);
11696 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11697 decomp_first_name
= d
;
11698 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11700 tree name
= DECL_NAME (d
);
11701 names
.safe_push (name
);
11702 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11703 IDENTIFIER_BINDING (name
)
11704 = IDENTIFIER_BINDING (name
)->previous
;
11708 if (names
.is_empty ())
11710 tree name
= DECL_NAME (range_decl
);
11711 names
.safe_push (name
);
11712 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11713 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11719 bool expr_non_constant_p
;
11720 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11723 range_expr
= cp_parser_expression (parser
);
11725 /* Put the range declaration(s) back into scope. */
11726 for (unsigned int i
= 0; i
< names
.length (); i
++)
11728 cxx_binding
*binding
= bindings
[i
];
11729 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11730 IDENTIFIER_BINDING (names
[i
]) = binding
;
11733 /* If in template, STMT is converted to a normal for-statement
11734 at instantiation. If not, it is done just ahead. */
11735 if (processing_template_decl
)
11737 if (check_for_bare_parameter_packs (range_expr
))
11738 range_expr
= error_mark_node
;
11739 stmt
= begin_range_for_stmt (scope
, init
);
11741 RANGE_FOR_IVDEP (stmt
) = 1;
11743 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11744 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11745 if (!type_dependent_expression_p (range_expr
)
11746 /* do_auto_deduction doesn't mess with template init-lists. */
11747 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11748 do_range_for_auto_deduction (range_decl
, range_expr
);
11752 stmt
= begin_for_stmt (scope
, init
);
11753 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11754 decomp_first_name
, decomp_cnt
, ivdep
,
11760 /* Subroutine of cp_convert_range_for: given the initializer expression,
11761 builds up the range temporary. */
11764 build_range_temp (tree range_expr
)
11766 tree range_type
, range_temp
;
11768 /* Find out the type deduced by the declaration
11769 `auto &&__range = range_expr'. */
11770 range_type
= cp_build_reference_type (make_auto (), true);
11771 range_type
= do_auto_deduction (range_type
, range_expr
,
11772 type_uses_auto (range_type
));
11774 /* Create the __range variable. */
11775 range_temp
= build_decl (input_location
, VAR_DECL
,
11776 get_identifier ("__for_range"), range_type
);
11777 TREE_USED (range_temp
) = 1;
11778 DECL_ARTIFICIAL (range_temp
) = 1;
11783 /* Used by cp_parser_range_for in template context: we aren't going to
11784 do a full conversion yet, but we still need to resolve auto in the
11785 type of the for-range-declaration if present. This is basically
11786 a shortcut version of cp_convert_range_for. */
11789 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11791 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11794 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11795 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11796 iter_type
= (cp_parser_perform_range_for_lookup
11797 (range_temp
, &begin_dummy
, &end_dummy
));
11800 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11802 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11804 tf_warning_or_error
);
11805 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11806 iter_decl
, auto_node
);
11811 /* Converts a range-based for-statement into a normal
11812 for-statement, as per the definition.
11814 for (RANGE_DECL : RANGE_EXPR)
11817 should be equivalent to:
11820 auto &&__range = RANGE_EXPR;
11821 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11825 RANGE_DECL = *__begin;
11830 If RANGE_EXPR is an array:
11831 BEGIN_EXPR = __range
11832 END_EXPR = __range + ARRAY_SIZE(__range)
11833 Else if RANGE_EXPR has a member 'begin' or 'end':
11834 BEGIN_EXPR = __range.begin()
11835 END_EXPR = __range.end()
11837 BEGIN_EXPR = begin(__range)
11838 END_EXPR = end(__range);
11840 If __range has a member 'begin' but not 'end', or vice versa, we must
11841 still use the second alternative (it will surely fail, however).
11842 When calling begin()/end() in the third alternative we must use
11843 argument dependent lookup, but always considering 'std' as an associated
11847 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11848 tree decomp_first_name
, unsigned int decomp_cnt
,
11849 bool ivdep
, unsigned short unroll
)
11852 tree iter_type
, begin_expr
, end_expr
;
11853 tree condition
, expression
;
11855 range_expr
= mark_lvalue_use (range_expr
);
11857 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11858 /* If an error happened previously do nothing or else a lot of
11859 unhelpful errors would be issued. */
11860 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11865 if (VAR_P (range_expr
)
11866 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11867 /* Can't bind a reference to an array of runtime bound. */
11868 range_temp
= range_expr
;
11871 range_temp
= build_range_temp (range_expr
);
11872 pushdecl (range_temp
);
11873 cp_finish_decl (range_temp
, range_expr
,
11874 /*is_constant_init*/false, NULL_TREE
,
11875 LOOKUP_ONLYCONVERTING
);
11876 range_temp
= convert_from_reference (range_temp
);
11878 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11879 &begin_expr
, &end_expr
);
11882 /* The new for initialization statement. */
11883 begin
= build_decl (input_location
, VAR_DECL
,
11884 get_identifier ("__for_begin"), iter_type
);
11885 TREE_USED (begin
) = 1;
11886 DECL_ARTIFICIAL (begin
) = 1;
11888 cp_finish_decl (begin
, begin_expr
,
11889 /*is_constant_init*/false, NULL_TREE
,
11890 LOOKUP_ONLYCONVERTING
);
11892 if (cxx_dialect
>= cxx17
)
11893 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11894 end
= build_decl (input_location
, VAR_DECL
,
11895 get_identifier ("__for_end"), iter_type
);
11896 TREE_USED (end
) = 1;
11897 DECL_ARTIFICIAL (end
) = 1;
11899 cp_finish_decl (end
, end_expr
,
11900 /*is_constant_init*/false, NULL_TREE
,
11901 LOOKUP_ONLYCONVERTING
);
11903 finish_init_stmt (statement
);
11905 /* The new for condition. */
11906 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11909 NULL
, tf_warning_or_error
);
11910 finish_for_cond (condition
, statement
, ivdep
, unroll
);
11912 /* The new increment expression. */
11913 expression
= finish_unary_op_expr (input_location
,
11914 PREINCREMENT_EXPR
, begin
,
11915 tf_warning_or_error
);
11916 finish_for_expr (expression
, statement
);
11918 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11919 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11921 /* The declaration is initialized with *__begin inside the loop body. */
11922 cp_finish_decl (range_decl
,
11923 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11924 tf_warning_or_error
),
11925 /*is_constant_init*/false, NULL_TREE
,
11926 LOOKUP_ONLYCONVERTING
);
11927 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11928 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11933 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11934 We need to solve both at the same time because the method used
11935 depends on the existence of members begin or end.
11936 Returns the type deduced for the iterator expression. */
11939 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11941 if (error_operand_p (range
))
11943 *begin
= *end
= error_mark_node
;
11944 return error_mark_node
;
11947 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11949 error ("range-based %<for%> expression of type %qT "
11950 "has incomplete type", TREE_TYPE (range
));
11951 *begin
= *end
= error_mark_node
;
11952 return error_mark_node
;
11954 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11956 /* If RANGE is an array, we will use pointer arithmetic. */
11957 *begin
= decay_conversion (range
, tf_warning_or_error
);
11958 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11960 array_type_nelts_top (TREE_TYPE (range
)),
11962 return TREE_TYPE (*begin
);
11966 /* If it is not an array, we must do a bit of magic. */
11967 tree id_begin
, id_end
;
11968 tree member_begin
, member_end
;
11970 *begin
= *end
= error_mark_node
;
11972 id_begin
= get_identifier ("begin");
11973 id_end
= get_identifier ("end");
11974 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11975 /*protect=*/2, /*want_type=*/false,
11976 tf_warning_or_error
);
11977 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11978 /*protect=*/2, /*want_type=*/false,
11979 tf_warning_or_error
);
11981 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11983 /* Use the member functions. */
11984 if (member_begin
!= NULL_TREE
)
11985 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11987 error ("range-based %<for%> expression of type %qT has an "
11988 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11990 if (member_end
!= NULL_TREE
)
11991 *end
= cp_parser_range_for_member_function (range
, id_end
);
11993 error ("range-based %<for%> expression of type %qT has a "
11994 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11998 /* Use global functions with ADL. */
11999 vec
<tree
, va_gc
> *vec
;
12000 vec
= make_tree_vector ();
12002 vec_safe_push (vec
, range
);
12004 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12005 tf_warning_or_error
);
12006 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12007 tf_warning_or_error
);
12008 member_end
= perform_koenig_lookup (id_end
, vec
,
12009 tf_warning_or_error
);
12010 *end
= finish_call_expr (member_end
, &vec
, false, true,
12011 tf_warning_or_error
);
12013 release_tree_vector (vec
);
12016 /* Last common checks. */
12017 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12019 /* If one of the expressions is an error do no more checks. */
12020 *begin
= *end
= error_mark_node
;
12021 return error_mark_node
;
12023 else if (type_dependent_expression_p (*begin
)
12024 || type_dependent_expression_p (*end
))
12025 /* Can happen, when, eg, in a template context, Koenig lookup
12026 can't resolve begin/end (c++/58503). */
12030 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12031 /* The unqualified type of the __begin and __end temporaries should
12032 be the same, as required by the multiple auto declaration. */
12033 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12035 if (cxx_dialect
>= cxx17
12036 && (build_x_binary_op (input_location
, NE_EXPR
,
12037 *begin
, ERROR_MARK
,
12040 != error_mark_node
))
12041 /* P0184R0 allows __begin and __end to have different types,
12042 but make sure they are comparable so we can give a better
12045 error ("inconsistent begin/end types in range-based %<for%> "
12046 "statement: %qT and %qT",
12047 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12054 /* Helper function for cp_parser_perform_range_for_lookup.
12055 Builds a tree for RANGE.IDENTIFIER(). */
12058 cp_parser_range_for_member_function (tree range
, tree identifier
)
12061 vec
<tree
, va_gc
> *vec
;
12063 member
= finish_class_member_access_expr (range
, identifier
,
12064 false, tf_warning_or_error
);
12065 if (member
== error_mark_node
)
12066 return error_mark_node
;
12068 vec
= make_tree_vector ();
12069 res
= finish_call_expr (member
, &vec
,
12070 /*disallow_virtual=*/false,
12071 /*koenig_p=*/false,
12072 tf_warning_or_error
);
12073 release_tree_vector (vec
);
12077 /* Parse an iteration-statement.
12079 iteration-statement:
12080 while ( condition ) statement
12081 do statement while ( expression ) ;
12082 for ( init-statement condition [opt] ; expression [opt] )
12085 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12088 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12089 unsigned short unroll
)
12094 unsigned char in_statement
;
12095 token_indent_info guard_tinfo
;
12097 /* Peek at the next token. */
12098 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12100 return error_mark_node
;
12102 guard_tinfo
= get_token_indent_info (token
);
12104 /* Remember whether or not we are already within an iteration
12106 in_statement
= parser
->in_statement
;
12108 /* See what kind of keyword it is. */
12109 keyword
= token
->keyword
;
12116 /* Begin the while-statement. */
12117 statement
= begin_while_stmt ();
12118 /* Look for the `('. */
12119 matching_parens parens
;
12120 parens
.require_open (parser
);
12121 /* Parse the condition. */
12122 condition
= cp_parser_condition (parser
);
12123 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12124 /* Look for the `)'. */
12125 parens
.require_close (parser
);
12126 /* Parse the dependent statement. */
12127 parser
->in_statement
= IN_ITERATION_STMT
;
12128 bool prev
= note_iteration_stmt_body_start ();
12129 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12130 note_iteration_stmt_body_end (prev
);
12131 parser
->in_statement
= in_statement
;
12132 /* We're done with the while-statement. */
12133 finish_while_stmt (statement
);
12141 /* Begin the do-statement. */
12142 statement
= begin_do_stmt ();
12143 /* Parse the body of the do-statement. */
12144 parser
->in_statement
= IN_ITERATION_STMT
;
12145 bool prev
= note_iteration_stmt_body_start ();
12146 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12147 note_iteration_stmt_body_end (prev
);
12148 parser
->in_statement
= in_statement
;
12149 finish_do_body (statement
);
12150 /* Look for the `while' keyword. */
12151 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12152 /* Look for the `('. */
12153 matching_parens parens
;
12154 parens
.require_open (parser
);
12155 /* Parse the expression. */
12156 expression
= cp_parser_expression (parser
);
12157 /* We're done with the do-statement. */
12158 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12159 /* Look for the `)'. */
12160 parens
.require_close (parser
);
12161 /* Look for the `;'. */
12162 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12168 /* Look for the `('. */
12169 matching_parens parens
;
12170 parens
.require_open (parser
);
12172 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12174 /* Look for the `)'. */
12175 parens
.require_close (parser
);
12177 /* Parse the body of the for-statement. */
12178 parser
->in_statement
= IN_ITERATION_STMT
;
12179 bool prev
= note_iteration_stmt_body_start ();
12180 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12181 note_iteration_stmt_body_end (prev
);
12182 parser
->in_statement
= in_statement
;
12184 /* We're done with the for-statement. */
12185 finish_for_stmt (statement
);
12190 cp_parser_error (parser
, "expected iteration-statement");
12191 statement
= error_mark_node
;
12198 /* Parse a init-statement or the declarator of a range-based-for.
12199 Returns true if a range-based-for declaration is seen.
12202 expression-statement
12203 simple-declaration */
12206 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12208 /* If the next token is a `;', then we have an empty
12209 expression-statement. Grammatically, this is also a
12210 simple-declaration, but an invalid one, because it does not
12211 declare anything. Therefore, if we did not handle this case
12212 specially, we would issue an error message about an invalid
12214 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12216 bool is_range_for
= false;
12217 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12219 /* A colon is used in range-based for. */
12220 parser
->colon_corrects_to_scope_p
= false;
12222 /* We're going to speculatively look for a declaration, falling back
12223 to an expression, if necessary. */
12224 cp_parser_parse_tentatively (parser
);
12225 /* Parse the declaration. */
12226 cp_parser_simple_declaration (parser
,
12227 /*function_definition_allowed_p=*/false,
12229 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12232 /* It is a range-for, consume the ':' */
12233 cp_lexer_consume_token (parser
->lexer
);
12234 is_range_for
= true;
12235 if (cxx_dialect
< cxx11
)
12237 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12238 "range-based %<for%> loops only available with "
12239 "-std=c++11 or -std=gnu++11");
12240 *decl
= error_mark_node
;
12244 /* The ';' is not consumed yet because we told
12245 cp_parser_simple_declaration not to. */
12246 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12248 if (cp_parser_parse_definitely (parser
))
12249 return is_range_for
;
12250 /* If the tentative parse failed, then we shall need to look for an
12251 expression-statement. */
12253 /* If we are here, it is an expression-statement. */
12254 cp_parser_expression_statement (parser
, NULL_TREE
);
12258 /* Parse a jump-statement.
12263 return expression [opt] ;
12264 return braced-init-list ;
12270 goto * expression ;
12272 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12275 cp_parser_jump_statement (cp_parser
* parser
)
12277 tree statement
= error_mark_node
;
12280 unsigned char in_statement
;
12282 /* Peek at the next token. */
12283 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12285 return error_mark_node
;
12287 /* See what kind of keyword it is. */
12288 keyword
= token
->keyword
;
12292 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12293 switch (in_statement
)
12296 error_at (token
->location
, "break statement not within loop or switch");
12299 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12300 || in_statement
== IN_ITERATION_STMT
);
12301 statement
= finish_break_stmt ();
12302 if (in_statement
== IN_ITERATION_STMT
)
12303 break_maybe_infinite_loop ();
12306 error_at (token
->location
, "invalid exit from OpenMP structured block");
12309 error_at (token
->location
, "break statement used with OpenMP for loop");
12312 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12316 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12319 error_at (token
->location
, "continue statement not within a loop");
12321 /* Fall through. */
12322 case IN_ITERATION_STMT
:
12324 statement
= finish_continue_stmt ();
12327 error_at (token
->location
, "invalid exit from OpenMP structured block");
12330 gcc_unreachable ();
12332 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12338 bool expr_non_constant_p
;
12340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12342 cp_lexer_set_source_position (parser
->lexer
);
12343 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12344 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12346 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12347 expr
= cp_parser_expression (parser
);
12349 /* If the next token is a `;', then there is no
12352 /* Build the return-statement. */
12353 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12354 /* Don't deduce from a discarded return statement. */;
12356 statement
= finish_return_stmt (expr
);
12357 /* Look for the final `;'. */
12358 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12363 if (parser
->in_function_body
12364 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12366 error ("%<goto%> in %<constexpr%> function");
12367 cp_function_chain
->invalid_constexpr
= true;
12370 /* Create the goto-statement. */
12371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12373 /* Issue a warning about this use of a GNU extension. */
12374 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12375 /* Consume the '*' token. */
12376 cp_lexer_consume_token (parser
->lexer
);
12377 /* Parse the dependent expression. */
12378 finish_goto_stmt (cp_parser_expression (parser
));
12381 finish_goto_stmt (cp_parser_identifier (parser
));
12382 /* Look for the final `;'. */
12383 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12387 cp_parser_error (parser
, "expected jump-statement");
12394 /* Parse a declaration-statement.
12396 declaration-statement:
12397 block-declaration */
12400 cp_parser_declaration_statement (cp_parser
* parser
)
12404 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12405 p
= obstack_alloc (&declarator_obstack
, 0);
12407 /* Parse the block-declaration. */
12408 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12410 /* Free any declarators allocated. */
12411 obstack_free (&declarator_obstack
, p
);
12414 /* Some dependent statements (like `if (cond) statement'), are
12415 implicitly in their own scope. In other words, if the statement is
12416 a single statement (as opposed to a compound-statement), it is
12417 none-the-less treated as if it were enclosed in braces. Any
12418 declarations appearing in the dependent statement are out of scope
12419 after control passes that point. This function parses a statement,
12420 but ensures that is in its own scope, even if it is not a
12421 compound-statement.
12423 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12424 is a (possibly labeled) if statement which is not enclosed in
12425 braces and has an else clause. This is used to implement
12428 CHAIN is a vector of if-else-if conditions. This is used to implement
12431 Returns the new statement. */
12434 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12435 const token_indent_info
&guard_tinfo
,
12439 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12440 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12441 token_indent_info body_tinfo
12442 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12447 /* Mark if () ; with a special NOP_EXPR. */
12448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12450 cp_lexer_consume_token (parser
->lexer
);
12451 statement
= add_stmt (build_empty_stmt (body_loc
));
12453 if (guard_tinfo
.keyword
== RID_IF
12454 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12455 warning_at (body_loc
, OPT_Wempty_body
,
12456 "suggest braces around empty body in an %<if%> statement");
12457 else if (guard_tinfo
.keyword
== RID_ELSE
)
12458 warning_at (body_loc
, OPT_Wempty_body
,
12459 "suggest braces around empty body in an %<else%> statement");
12461 /* if a compound is opened, we simply parse the statement directly. */
12462 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12463 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12464 /* If the token is not a `{', then we must take special action. */
12467 /* Create a compound-statement. */
12468 statement
= begin_compound_stmt (0);
12469 /* Parse the dependent-statement. */
12470 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12471 &body_loc_after_labels
);
12472 /* Finish the dummy compound-statement. */
12473 finish_compound_stmt (statement
);
12476 token_indent_info next_tinfo
12477 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12478 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12480 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12481 && next_tinfo
.type
!= CPP_SEMICOLON
)
12482 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12483 guard_tinfo
.location
, guard_tinfo
.keyword
);
12485 /* Return the statement. */
12489 /* For some dependent statements (like `while (cond) statement'), we
12490 have already created a scope. Therefore, even if the dependent
12491 statement is a compound-statement, we do not want to create another
12495 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12496 const token_indent_info
&guard_tinfo
)
12498 /* If the token is a `{', then we must take special action. */
12499 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12501 token_indent_info body_tinfo
12502 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12503 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12505 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12506 &loc_after_labels
);
12507 token_indent_info next_tinfo
12508 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12509 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12511 if (loc_after_labels
!= UNKNOWN_LOCATION
12512 && next_tinfo
.type
!= CPP_SEMICOLON
)
12513 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12514 guard_tinfo
.location
,
12515 guard_tinfo
.keyword
);
12519 /* Avoid calling cp_parser_compound_statement, so that we
12520 don't create a new scope. Do everything else by hand. */
12521 matching_braces braces
;
12522 braces
.require_open (parser
);
12523 /* If the next keyword is `__label__' we have a label declaration. */
12524 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12525 cp_parser_label_declaration (parser
);
12526 /* Parse an (optional) statement-seq. */
12527 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12528 braces
.require_close (parser
);
12532 /* Declarations [gram.dcl.dcl] */
12534 /* Parse an optional declaration-sequence.
12538 declaration-seq declaration */
12541 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12547 token
= cp_lexer_peek_token (parser
->lexer
);
12549 if (token
->type
== CPP_CLOSE_BRACE
12550 || token
->type
== CPP_EOF
12551 || token
->type
== CPP_PRAGMA_EOL
)
12554 if (token
->type
== CPP_SEMICOLON
)
12556 /* A declaration consisting of a single semicolon is
12557 invalid. Allow it unless we're being pedantic. */
12558 cp_lexer_consume_token (parser
->lexer
);
12559 if (!in_system_header_at (input_location
))
12560 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12564 /* If we're entering or exiting a region that's implicitly
12565 extern "C", modify the lang context appropriately. */
12566 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12568 push_lang_context (lang_name_c
);
12569 parser
->implicit_extern_c
= true;
12571 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12573 pop_lang_context ();
12574 parser
->implicit_extern_c
= false;
12577 if (token
->type
== CPP_PRAGMA
)
12579 /* A top-level declaration can consist solely of a #pragma.
12580 A nested declaration cannot, so this is done here and not
12581 in cp_parser_declaration. (A #pragma at block scope is
12582 handled in cp_parser_statement.) */
12583 cp_parser_pragma (parser
, pragma_external
, NULL
);
12587 /* Parse the declaration itself. */
12588 cp_parser_declaration (parser
);
12592 /* Parse a declaration.
12596 function-definition
12597 template-declaration
12598 explicit-instantiation
12599 explicit-specialization
12600 linkage-specification
12601 namespace-definition
12609 __extension__ declaration */
12612 cp_parser_declaration (cp_parser
* parser
)
12616 int saved_pedantic
;
12618 tree attributes
= NULL_TREE
;
12620 /* Check for the `__extension__' keyword. */
12621 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12623 /* Parse the qualified declaration. */
12624 cp_parser_declaration (parser
);
12625 /* Restore the PEDANTIC flag. */
12626 pedantic
= saved_pedantic
;
12631 /* Try to figure out what kind of declaration is present. */
12632 token1
= *cp_lexer_peek_token (parser
->lexer
);
12634 if (token1
.type
!= CPP_EOF
)
12635 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12638 token2
.type
= CPP_EOF
;
12639 token2
.keyword
= RID_MAX
;
12642 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12643 p
= obstack_alloc (&declarator_obstack
, 0);
12645 /* If the next token is `extern' and the following token is a string
12646 literal, then we have a linkage specification. */
12647 if (token1
.keyword
== RID_EXTERN
12648 && cp_parser_is_pure_string_literal (&token2
))
12649 cp_parser_linkage_specification (parser
);
12650 /* If the next token is `template', then we have either a template
12651 declaration, an explicit instantiation, or an explicit
12653 else if (token1
.keyword
== RID_TEMPLATE
)
12655 /* `template <>' indicates a template specialization. */
12656 if (token2
.type
== CPP_LESS
12657 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12658 cp_parser_explicit_specialization (parser
);
12659 /* `template <' indicates a template declaration. */
12660 else if (token2
.type
== CPP_LESS
)
12661 cp_parser_template_declaration (parser
, /*member_p=*/false);
12662 /* Anything else must be an explicit instantiation. */
12664 cp_parser_explicit_instantiation (parser
);
12666 /* If the next token is `export', then we have a template
12668 else if (token1
.keyword
== RID_EXPORT
)
12669 cp_parser_template_declaration (parser
, /*member_p=*/false);
12670 /* If the next token is `extern', 'static' or 'inline' and the one
12671 after that is `template', we have a GNU extended explicit
12672 instantiation directive. */
12673 else if (cp_parser_allow_gnu_extensions_p (parser
)
12674 && (token1
.keyword
== RID_EXTERN
12675 || token1
.keyword
== RID_STATIC
12676 || token1
.keyword
== RID_INLINE
)
12677 && token2
.keyword
== RID_TEMPLATE
)
12678 cp_parser_explicit_instantiation (parser
);
12679 /* If the next token is `namespace', check for a named or unnamed
12680 namespace definition. */
12681 else if (token1
.keyword
== RID_NAMESPACE
12682 && (/* A named namespace definition. */
12683 (token2
.type
== CPP_NAME
12684 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12686 || (token2
.type
== CPP_OPEN_SQUARE
12687 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12688 == CPP_OPEN_SQUARE
)
12689 /* An unnamed namespace definition. */
12690 || token2
.type
== CPP_OPEN_BRACE
12691 || token2
.keyword
== RID_ATTRIBUTE
))
12692 cp_parser_namespace_definition (parser
);
12693 /* An inline (associated) namespace definition. */
12694 else if (token1
.keyword
== RID_INLINE
12695 && token2
.keyword
== RID_NAMESPACE
)
12696 cp_parser_namespace_definition (parser
);
12697 /* Objective-C++ declaration/definition. */
12698 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12699 cp_parser_objc_declaration (parser
, NULL_TREE
);
12700 else if (c_dialect_objc ()
12701 && token1
.keyword
== RID_ATTRIBUTE
12702 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12703 cp_parser_objc_declaration (parser
, attributes
);
12704 /* At this point we may have a template declared by a concept
12706 else if (flag_concepts
12707 && cp_parser_template_declaration_after_export (parser
,
12708 /*member_p=*/false))
12711 /* Try to parse a block-declaration, or a function-definition. */
12712 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12714 /* Free any declarators allocated. */
12715 obstack_free (&declarator_obstack
, p
);
12718 /* Parse a block-declaration.
12723 namespace-alias-definition
12730 __extension__ block-declaration
12735 static_assert-declaration
12737 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12738 part of a declaration-statement. */
12741 cp_parser_block_declaration (cp_parser
*parser
,
12745 int saved_pedantic
;
12747 /* Check for the `__extension__' keyword. */
12748 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12750 /* Parse the qualified declaration. */
12751 cp_parser_block_declaration (parser
, statement_p
);
12752 /* Restore the PEDANTIC flag. */
12753 pedantic
= saved_pedantic
;
12758 /* Peek at the next token to figure out which kind of declaration is
12760 token1
= cp_lexer_peek_token (parser
->lexer
);
12762 /* If the next keyword is `asm', we have an asm-definition. */
12763 if (token1
->keyword
== RID_ASM
)
12766 cp_parser_commit_to_tentative_parse (parser
);
12767 cp_parser_asm_definition (parser
);
12769 /* If the next keyword is `namespace', we have a
12770 namespace-alias-definition. */
12771 else if (token1
->keyword
== RID_NAMESPACE
)
12772 cp_parser_namespace_alias_definition (parser
);
12773 /* If the next keyword is `using', we have a
12774 using-declaration, a using-directive, or an alias-declaration. */
12775 else if (token1
->keyword
== RID_USING
)
12780 cp_parser_commit_to_tentative_parse (parser
);
12781 /* If the token after `using' is `namespace', then we have a
12782 using-directive. */
12783 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12784 if (token2
->keyword
== RID_NAMESPACE
)
12785 cp_parser_using_directive (parser
);
12786 /* If the second token after 'using' is '=', then we have an
12787 alias-declaration. */
12788 else if (cxx_dialect
>= cxx11
12789 && token2
->type
== CPP_NAME
12790 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12791 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12792 cp_parser_alias_declaration (parser
);
12793 /* Otherwise, it's a using-declaration. */
12795 cp_parser_using_declaration (parser
,
12796 /*access_declaration_p=*/false);
12798 /* If the next keyword is `__label__' we have a misplaced label
12800 else if (token1
->keyword
== RID_LABEL
)
12802 cp_lexer_consume_token (parser
->lexer
);
12803 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12804 cp_parser_skip_to_end_of_statement (parser
);
12805 /* If the next token is now a `;', consume it. */
12806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12807 cp_lexer_consume_token (parser
->lexer
);
12809 /* If the next token is `static_assert' we have a static assertion. */
12810 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12811 cp_parser_static_assert (parser
, /*member_p=*/false);
12812 /* Anything else must be a simple-declaration. */
12814 cp_parser_simple_declaration (parser
, !statement_p
,
12815 /*maybe_range_for_decl*/NULL
);
12818 /* Parse a simple-declaration.
12820 simple-declaration:
12821 decl-specifier-seq [opt] init-declarator-list [opt] ;
12822 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12823 brace-or-equal-initializer ;
12825 init-declarator-list:
12827 init-declarator-list , init-declarator
12829 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12830 function-definition as a simple-declaration.
12832 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12833 parsed declaration if it is an uninitialized single declarator not followed
12834 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12835 if present, will not be consumed. */
12838 cp_parser_simple_declaration (cp_parser
* parser
,
12839 bool function_definition_allowed_p
,
12840 tree
*maybe_range_for_decl
)
12842 cp_decl_specifier_seq decl_specifiers
;
12843 int declares_class_or_enum
;
12844 bool saw_declarator
;
12845 location_t comma_loc
= UNKNOWN_LOCATION
;
12846 location_t init_loc
= UNKNOWN_LOCATION
;
12848 if (maybe_range_for_decl
)
12849 *maybe_range_for_decl
= NULL_TREE
;
12851 /* Defer access checks until we know what is being declared; the
12852 checks for names appearing in the decl-specifier-seq should be
12853 done as if we were in the scope of the thing being declared. */
12854 push_deferring_access_checks (dk_deferred
);
12856 /* Parse the decl-specifier-seq. We have to keep track of whether
12857 or not the decl-specifier-seq declares a named class or
12858 enumeration type, since that is the only case in which the
12859 init-declarator-list is allowed to be empty.
12863 In a simple-declaration, the optional init-declarator-list can be
12864 omitted only when declaring a class or enumeration, that is when
12865 the decl-specifier-seq contains either a class-specifier, an
12866 elaborated-type-specifier, or an enum-specifier. */
12867 cp_parser_decl_specifier_seq (parser
,
12868 CP_PARSER_FLAGS_OPTIONAL
,
12870 &declares_class_or_enum
);
12871 /* We no longer need to defer access checks. */
12872 stop_deferring_access_checks ();
12874 /* In a block scope, a valid declaration must always have a
12875 decl-specifier-seq. By not trying to parse declarators, we can
12876 resolve the declaration/expression ambiguity more quickly. */
12877 if (!function_definition_allowed_p
12878 && !decl_specifiers
.any_specifiers_p
)
12880 cp_parser_error (parser
, "expected declaration");
12884 /* If the next two tokens are both identifiers, the code is
12885 erroneous. The usual cause of this situation is code like:
12889 where "T" should name a type -- but does not. */
12890 if (!decl_specifiers
.any_type_specifiers_p
12891 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12893 /* If parsing tentatively, we should commit; we really are
12894 looking at a declaration. */
12895 cp_parser_commit_to_tentative_parse (parser
);
12900 /* If we have seen at least one decl-specifier, and the next token
12901 is not a parenthesis, then we must be looking at a declaration.
12902 (After "int (" we might be looking at a functional cast.) */
12903 if (decl_specifiers
.any_specifiers_p
12904 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12905 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12906 && !cp_parser_error_occurred (parser
))
12907 cp_parser_commit_to_tentative_parse (parser
);
12909 /* Look for C++17 decomposition declaration. */
12910 for (size_t n
= 1; ; n
++)
12911 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12912 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12914 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12915 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12916 && decl_specifiers
.any_specifiers_p
)
12919 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12920 maybe_range_for_decl
,
12923 /* The next token should be either a `,' or a `;'. */
12924 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12925 /* If it's a `;', we are done. */
12926 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12928 /* Anything else is an error. */
12931 /* If we have already issued an error message we don't need
12932 to issue another one. */
12933 if ((decl
!= error_mark_node
12934 && DECL_INITIAL (decl
) != error_mark_node
)
12935 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12936 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12937 /* Skip tokens until we reach the end of the statement. */
12938 cp_parser_skip_to_end_of_statement (parser
);
12939 /* If the next token is now a `;', consume it. */
12940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12941 cp_lexer_consume_token (parser
->lexer
);
12949 bool auto_specifier_p
;
12950 /* NULL_TREE if both variable and function declaration are allowed,
12951 error_mark_node if function declaration are not allowed and
12952 a FUNCTION_DECL that should be diagnosed if it is followed by
12953 variable declarations. */
12954 tree auto_function_declaration
;
12956 last_type
= NULL_TREE
;
12958 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
12959 auto_function_declaration
= NULL_TREE
;
12961 /* Keep going until we hit the `;' at the end of the simple
12963 saw_declarator
= false;
12964 while (cp_lexer_next_token_is_not (parser
->lexer
,
12968 bool function_definition_p
;
12970 tree auto_result
= NULL_TREE
;
12972 if (saw_declarator
)
12974 /* If we are processing next declarator, comma is expected */
12975 token
= cp_lexer_peek_token (parser
->lexer
);
12976 gcc_assert (token
->type
== CPP_COMMA
);
12977 cp_lexer_consume_token (parser
->lexer
);
12978 if (maybe_range_for_decl
)
12980 *maybe_range_for_decl
= error_mark_node
;
12981 if (comma_loc
== UNKNOWN_LOCATION
)
12982 comma_loc
= token
->location
;
12986 saw_declarator
= true;
12988 /* Parse the init-declarator. */
12989 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
12991 function_definition_allowed_p
,
12992 /*member_p=*/false,
12993 declares_class_or_enum
,
12994 &function_definition_p
,
12995 maybe_range_for_decl
,
12998 /* If an error occurred while parsing tentatively, exit quickly.
12999 (That usually happens when in the body of a function; each
13000 statement is treated as a declaration-statement until proven
13002 if (cp_parser_error_occurred (parser
))
13005 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13007 /* If the init-declarator-list contains more than one
13008 init-declarator, they shall all form declarations of
13010 if (auto_function_declaration
== NULL_TREE
)
13011 auto_function_declaration
13012 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13013 else if (TREE_CODE (decl
) == FUNCTION_DECL
13014 || auto_function_declaration
!= error_mark_node
)
13016 error_at (decl_specifiers
.locations
[ds_type_spec
],
13017 "non-variable %qD in declaration with more than one "
13018 "declarator with placeholder type",
13019 TREE_CODE (decl
) == FUNCTION_DECL
13020 ? decl
: auto_function_declaration
);
13021 auto_function_declaration
= error_mark_node
;
13026 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13029 && last_type
!= error_mark_node
13030 && !same_type_p (auto_result
, last_type
))
13032 /* If the list of declarators contains more than one declarator,
13033 the type of each declared variable is determined as described
13034 above. If the type deduced for the template parameter U is not
13035 the same in each deduction, the program is ill-formed. */
13036 error_at (decl_specifiers
.locations
[ds_type_spec
],
13037 "inconsistent deduction for %qT: %qT and then %qT",
13038 decl_specifiers
.type
, last_type
, auto_result
);
13039 last_type
= error_mark_node
;
13042 last_type
= auto_result
;
13045 /* Handle function definitions specially. */
13046 if (function_definition_p
)
13048 /* If the next token is a `,', then we are probably
13049 processing something like:
13053 which is erroneous. */
13054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13056 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13057 error_at (token
->location
,
13059 " declarations and function-definitions is forbidden");
13061 /* Otherwise, we're done with the list of declarators. */
13064 pop_deferring_access_checks ();
13068 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13069 *maybe_range_for_decl
= decl
;
13070 /* The next token should be either a `,' or a `;'. */
13071 token
= cp_lexer_peek_token (parser
->lexer
);
13072 /* If it's a `,', there are more declarators to come. */
13073 if (token
->type
== CPP_COMMA
)
13074 /* will be consumed next time around */;
13075 /* If it's a `;', we are done. */
13076 else if (token
->type
== CPP_SEMICOLON
)
13078 else if (maybe_range_for_decl
)
13080 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13081 permerror (decl_specifiers
.locations
[ds_type_spec
],
13082 "types may not be defined in a for-range-declaration");
13085 /* Anything else is an error. */
13088 /* If we have already issued an error message we don't need
13089 to issue another one. */
13090 if ((decl
!= error_mark_node
13091 && DECL_INITIAL (decl
) != error_mark_node
)
13092 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13093 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13094 /* Skip tokens until we reach the end of the statement. */
13095 cp_parser_skip_to_end_of_statement (parser
);
13096 /* If the next token is now a `;', consume it. */
13097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13098 cp_lexer_consume_token (parser
->lexer
);
13101 /* After the first time around, a function-definition is not
13102 allowed -- even if it was OK at first. For example:
13107 function_definition_allowed_p
= false;
13110 /* Issue an error message if no declarators are present, and the
13111 decl-specifier-seq does not itself declare a class or
13112 enumeration: [dcl.dcl]/3. */
13113 if (!saw_declarator
)
13115 if (cp_parser_declares_only_class_p (parser
))
13117 if (!declares_class_or_enum
13118 && decl_specifiers
.type
13119 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13120 /* Ensure an error is issued anyway when finish_decltype_type,
13121 called via cp_parser_decl_specifier_seq, returns a class or
13122 an enumeration (c++/51786). */
13123 decl_specifiers
.type
= NULL_TREE
;
13124 shadow_tag (&decl_specifiers
);
13126 /* Perform any deferred access checks. */
13127 perform_deferred_access_checks (tf_warning_or_error
);
13130 /* Consume the `;'. */
13132 if (!maybe_range_for_decl
)
13133 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13134 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13136 if (init_loc
!= UNKNOWN_LOCATION
)
13137 error_at (init_loc
, "initializer in range-based %<for%> loop");
13138 if (comma_loc
!= UNKNOWN_LOCATION
)
13139 error_at (comma_loc
,
13140 "multiple declarations in range-based %<for%> loop");
13144 pop_deferring_access_checks ();
13147 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13148 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13152 cp_parser_decomposition_declaration (cp_parser
*parser
,
13153 cp_decl_specifier_seq
*decl_specifiers
,
13154 tree
*maybe_range_for_decl
,
13155 location_t
*init_loc
)
13157 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13158 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13159 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13161 /* Parse the identifier-list. */
13162 auto_vec
<cp_expr
, 10> v
;
13163 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13166 cp_expr e
= cp_parser_identifier (parser
);
13167 if (e
.get_value () == error_mark_node
)
13170 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13172 cp_lexer_consume_token (parser
->lexer
);
13175 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13176 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13178 end_loc
= UNKNOWN_LOCATION
;
13179 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13182 cp_lexer_consume_token (parser
->lexer
);
13185 cp_parser_skip_to_end_of_statement (parser
);
13186 return error_mark_node
;
13190 if (cxx_dialect
< cxx17
)
13191 pedwarn (loc
, 0, "structured bindings only available with "
13192 "-std=c++17 or -std=gnu++17");
13195 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13196 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13197 declarator
->id_loc
= loc
;
13198 if (ref_qual
!= REF_QUAL_NONE
)
13199 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13200 ref_qual
== REF_QUAL_RVALUE
,
13202 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13203 NULL_TREE
, decl_specifiers
->attributes
,
13205 tree orig_decl
= decl
;
13209 cp_decl_specifier_seq decl_specs
;
13210 clear_decl_specs (&decl_specs
);
13211 decl_specs
.type
= make_auto ();
13213 FOR_EACH_VEC_ELT (v
, i
, e
)
13216 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13218 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13219 declarator
->id_loc
= e
.get_location ();
13220 tree elt_pushed_scope
;
13221 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13222 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13223 if (decl2
== error_mark_node
)
13224 decl
= error_mark_node
;
13225 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13227 /* Ensure we've diagnosed redeclaration if we aren't creating
13229 gcc_assert (errorcount
);
13230 decl
= error_mark_node
;
13234 if (elt_pushed_scope
)
13235 pop_scope (elt_pushed_scope
);
13240 error_at (loc
, "empty structured binding declaration");
13241 decl
= error_mark_node
;
13244 if (maybe_range_for_decl
== NULL
13245 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13247 bool non_constant_p
= false, is_direct_init
= false;
13248 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13249 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13251 if (initializer
== NULL_TREE
13252 || (TREE_CODE (initializer
) == TREE_LIST
13253 && TREE_CHAIN (initializer
))
13255 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13256 && CONSTRUCTOR_NELTS (initializer
) != 1))
13258 error_at (loc
, "invalid initializer for structured binding "
13260 initializer
= error_mark_node
;
13263 if (decl
!= error_mark_node
)
13265 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13266 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13267 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13268 cp_finish_decomp (decl
, prev
, v
.length ());
13271 else if (decl
!= error_mark_node
)
13273 *maybe_range_for_decl
= prev
;
13274 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13275 the underlying DECL. */
13276 cp_finish_decomp (decl
, prev
, v
.length ());
13280 pop_scope (pushed_scope
);
13282 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13284 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13285 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13291 /* Parse a decl-specifier-seq.
13293 decl-specifier-seq:
13294 decl-specifier-seq [opt] decl-specifier
13295 decl-specifier attribute-specifier-seq [opt] (C++11)
13298 storage-class-specifier
13309 Concepts Extension:
13314 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13316 The parser flags FLAGS is used to control type-specifier parsing.
13318 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13321 1: one of the decl-specifiers is an elaborated-type-specifier
13322 (i.e., a type declaration)
13323 2: one of the decl-specifiers is an enum-specifier or a
13324 class-specifier (i.e., a type definition)
13329 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13330 cp_parser_flags flags
,
13331 cp_decl_specifier_seq
*decl_specs
,
13332 int* declares_class_or_enum
)
13334 bool constructor_possible_p
= !parser
->in_declarator_p
;
13335 bool found_decl_spec
= false;
13336 cp_token
*start_token
= NULL
;
13339 /* Clear DECL_SPECS. */
13340 clear_decl_specs (decl_specs
);
13342 /* Assume no class or enumeration type is declared. */
13343 *declares_class_or_enum
= 0;
13345 /* Keep reading specifiers until there are no more to read. */
13348 bool constructor_p
;
13352 /* Peek at the next token. */
13353 token
= cp_lexer_peek_token (parser
->lexer
);
13355 /* Save the first token of the decl spec list for error
13358 start_token
= token
;
13359 /* Handle attributes. */
13360 if (cp_next_tokens_can_be_attribute_p (parser
))
13362 /* Parse the attributes. */
13363 tree attrs
= cp_parser_attributes_opt (parser
);
13365 /* In a sequence of declaration specifiers, c++11 attributes
13366 appertain to the type that precede them. In that case
13369 The attribute-specifier-seq affects the type only for
13370 the declaration it appears in, not other declarations
13371 involving the same type.
13373 But for now let's force the user to position the
13374 attribute either at the beginning of the declaration or
13375 after the declarator-id, which would clearly mean that it
13376 applies to the declarator. */
13377 if (cxx11_attribute_p (attrs
))
13379 if (!found_decl_spec
)
13380 /* The c++11 attribute is at the beginning of the
13381 declaration. It appertains to the entity being
13385 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13387 /* This is an attribute following a
13388 class-specifier. */
13389 if (decl_specs
->type_definition_p
)
13390 warn_misplaced_attr_for_class_type (token
->location
,
13396 decl_specs
->std_attributes
13397 = chainon (decl_specs
->std_attributes
,
13399 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13400 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13406 decl_specs
->attributes
13407 = chainon (decl_specs
->attributes
,
13409 if (decl_specs
->locations
[ds_attribute
] == 0)
13410 decl_specs
->locations
[ds_attribute
] = token
->location
;
13413 /* Assume we will find a decl-specifier keyword. */
13414 found_decl_spec
= true;
13415 /* If the next token is an appropriate keyword, we can simply
13416 add it to the list. */
13417 switch (token
->keyword
)
13423 if (!at_class_scope_p ())
13425 gcc_rich_location
richloc (token
->location
);
13426 richloc
.add_fixit_remove ();
13427 error_at (&richloc
, "%<friend%> used outside of class");
13428 cp_lexer_purge_token (parser
->lexer
);
13433 /* Consume the token. */
13434 cp_lexer_consume_token (parser
->lexer
);
13438 case RID_CONSTEXPR
:
13440 cp_lexer_consume_token (parser
->lexer
);
13445 cp_lexer_consume_token (parser
->lexer
);
13448 /* function-specifier:
13455 cp_parser_function_specifier_opt (parser
, decl_specs
);
13462 /* Consume the token. */
13463 cp_lexer_consume_token (parser
->lexer
);
13464 /* A constructor declarator cannot appear in a typedef. */
13465 constructor_possible_p
= false;
13466 /* The "typedef" keyword can only occur in a declaration; we
13467 may as well commit at this point. */
13468 cp_parser_commit_to_tentative_parse (parser
);
13470 if (decl_specs
->storage_class
!= sc_none
)
13471 decl_specs
->conflicting_specifiers_p
= true;
13474 /* storage-class-specifier:
13484 if (cxx_dialect
== cxx98
)
13486 /* Consume the token. */
13487 cp_lexer_consume_token (parser
->lexer
);
13489 /* Complain about `auto' as a storage specifier, if
13490 we're complaining about C++0x compatibility. */
13491 gcc_rich_location
richloc (token
->location
);
13492 richloc
.add_fixit_remove ();
13493 warning_at (&richloc
, OPT_Wc__11_compat
,
13494 "%<auto%> changes meaning in C++11; "
13495 "please remove it");
13497 /* Set the storage class anyway. */
13498 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13502 /* C++0x auto type-specifier. */
13503 found_decl_spec
= false;
13510 /* Consume the token. */
13511 cp_lexer_consume_token (parser
->lexer
);
13512 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13516 /* Consume the token. */
13518 cp_lexer_consume_token (parser
->lexer
);
13522 /* We did not yet find a decl-specifier yet. */
13523 found_decl_spec
= false;
13527 if (found_decl_spec
13528 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13529 && token
->keyword
!= RID_CONSTEXPR
)
13530 error ("decl-specifier invalid in condition");
13532 if (found_decl_spec
13533 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13534 && token
->keyword
!= RID_MUTABLE
13535 && token
->keyword
!= RID_CONSTEXPR
)
13536 error_at (token
->location
, "%qD invalid in lambda",
13537 ridpointers
[token
->keyword
]);
13540 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13542 /* Constructors are a special case. The `S' in `S()' is not a
13543 decl-specifier; it is the beginning of the declarator. */
13545 = (!found_decl_spec
13546 && constructor_possible_p
13547 && (cp_parser_constructor_declarator_p
13548 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13550 /* If we don't have a DECL_SPEC yet, then we must be looking at
13551 a type-specifier. */
13552 if (!found_decl_spec
&& !constructor_p
)
13554 int decl_spec_declares_class_or_enum
;
13555 bool is_cv_qualifier
;
13559 = cp_parser_type_specifier (parser
, flags
,
13561 /*is_declaration=*/true,
13562 &decl_spec_declares_class_or_enum
,
13564 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13566 /* If this type-specifier referenced a user-defined type
13567 (a typedef, class-name, etc.), then we can't allow any
13568 more such type-specifiers henceforth.
13572 The longest sequence of decl-specifiers that could
13573 possibly be a type name is taken as the
13574 decl-specifier-seq of a declaration. The sequence shall
13575 be self-consistent as described below.
13579 As a general rule, at most one type-specifier is allowed
13580 in the complete decl-specifier-seq of a declaration. The
13581 only exceptions are the following:
13583 -- const or volatile can be combined with any other
13586 -- signed or unsigned can be combined with char, long,
13594 void g (const int Pc);
13596 Here, Pc is *not* part of the decl-specifier seq; it's
13597 the declarator. Therefore, once we see a type-specifier
13598 (other than a cv-qualifier), we forbid any additional
13599 user-defined types. We *do* still allow things like `int
13600 int' to be considered a decl-specifier-seq, and issue the
13601 error message later. */
13602 if (type_spec
&& !is_cv_qualifier
)
13603 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13604 /* A constructor declarator cannot follow a type-specifier. */
13607 constructor_possible_p
= false;
13608 found_decl_spec
= true;
13609 if (!is_cv_qualifier
)
13610 decl_specs
->any_type_specifiers_p
= true;
13614 /* If we still do not have a DECL_SPEC, then there are no more
13615 decl-specifiers. */
13616 if (!found_decl_spec
)
13619 decl_specs
->any_specifiers_p
= true;
13620 /* After we see one decl-specifier, further decl-specifiers are
13621 always optional. */
13622 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13625 /* Don't allow a friend specifier with a class definition. */
13626 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13627 && (*declares_class_or_enum
& 2))
13628 error_at (decl_specs
->locations
[ds_friend
],
13629 "class definition may not be declared a friend");
13632 /* Parse an (optional) storage-class-specifier.
13634 storage-class-specifier:
13643 storage-class-specifier:
13646 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13649 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13651 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13654 if (cxx_dialect
!= cxx98
)
13656 /* Fall through for C++98. */
13657 gcc_fallthrough ();
13664 /* Consume the token. */
13665 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13672 /* Parse an (optional) function-specifier.
13674 function-specifier:
13679 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13680 Updates DECL_SPECS, if it is non-NULL. */
13683 cp_parser_function_specifier_opt (cp_parser
* parser
,
13684 cp_decl_specifier_seq
*decl_specs
)
13686 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13687 switch (token
->keyword
)
13690 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13694 /* 14.5.2.3 [temp.mem]
13696 A member function template shall not be virtual. */
13697 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13698 && current_class_type
)
13699 error_at (token
->location
, "templates may not be %<virtual%>");
13701 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13705 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13712 /* Consume the token. */
13713 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13716 /* Parse a linkage-specification.
13718 linkage-specification:
13719 extern string-literal { declaration-seq [opt] }
13720 extern string-literal declaration */
13723 cp_parser_linkage_specification (cp_parser
* parser
)
13727 /* Look for the `extern' keyword. */
13728 cp_token
*extern_token
13729 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13731 /* Look for the string-literal. */
13732 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13733 linkage
= cp_parser_string_literal (parser
, false, false);
13735 /* Transform the literal into an identifier. If the literal is a
13736 wide-character string, or contains embedded NULs, then we can't
13737 handle it as the user wants. */
13738 if (strlen (TREE_STRING_POINTER (linkage
))
13739 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13741 cp_parser_error (parser
, "invalid linkage-specification");
13742 /* Assume C++ linkage. */
13743 linkage
= lang_name_cplusplus
;
13746 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13748 /* We're now using the new linkage. */
13749 push_lang_context (linkage
);
13751 /* Preserve the location of the the innermost linkage specification,
13752 tracking the locations of nested specifications via a local. */
13753 location_t saved_location
13754 = parser
->innermost_linkage_specification_location
;
13755 /* Construct a location ranging from the start of the "extern" to
13756 the end of the string-literal, with the caret at the start, e.g.:
13760 parser
->innermost_linkage_specification_location
13761 = make_location (extern_token
->location
,
13762 extern_token
->location
,
13763 get_finish (string_token
->location
));
13765 /* If the next token is a `{', then we're using the first
13767 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13769 cp_ensure_no_omp_declare_simd (parser
);
13770 cp_ensure_no_oacc_routine (parser
);
13772 /* Consume the `{' token. */
13773 matching_braces braces
;
13774 braces
.consume_open (parser
)->location
;
13775 /* Parse the declarations. */
13776 cp_parser_declaration_seq_opt (parser
);
13777 /* Look for the closing `}'. */
13778 braces
.require_close (parser
);
13780 /* Otherwise, there's just one declaration. */
13783 bool saved_in_unbraced_linkage_specification_p
;
13785 saved_in_unbraced_linkage_specification_p
13786 = parser
->in_unbraced_linkage_specification_p
;
13787 parser
->in_unbraced_linkage_specification_p
= true;
13788 cp_parser_declaration (parser
);
13789 parser
->in_unbraced_linkage_specification_p
13790 = saved_in_unbraced_linkage_specification_p
;
13793 /* We're done with the linkage-specification. */
13794 pop_lang_context ();
13796 /* Restore location of parent linkage specification, if any. */
13797 parser
->innermost_linkage_specification_location
= saved_location
;
13800 /* Parse a static_assert-declaration.
13802 static_assert-declaration:
13803 static_assert ( constant-expression , string-literal ) ;
13804 static_assert ( constant-expression ) ; (C++17)
13806 If MEMBER_P, this static_assert is a class member. */
13809 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13812 location_t token_loc
;
13816 /* Peek at the `static_assert' token so we can keep track of exactly
13817 where the static assertion started. */
13818 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13820 /* Look for the `static_assert' keyword. */
13821 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13825 /* We know we are in a static assertion; commit to any tentative
13827 if (cp_parser_parsing_tentatively (parser
))
13828 cp_parser_commit_to_tentative_parse (parser
);
13830 /* Parse the `(' starting the static assertion condition. */
13831 matching_parens parens
;
13832 parens
.require_open (parser
);
13834 /* Parse the constant-expression. Allow a non-constant expression
13835 here in order to give better diagnostics in finish_static_assert. */
13837 cp_parser_constant_expression (parser
,
13838 /*allow_non_constant_p=*/true,
13839 /*non_constant_p=*/&dummy
);
13841 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13843 if (cxx_dialect
< cxx17
)
13844 pedwarn (input_location
, OPT_Wpedantic
,
13845 "static_assert without a message "
13846 "only available with -std=c++17 or -std=gnu++17");
13848 cp_lexer_consume_token (parser
->lexer
);
13849 message
= build_string (1, "");
13850 TREE_TYPE (message
) = char_array_type_node
;
13851 fix_string_type (message
);
13855 /* Parse the separating `,'. */
13856 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13858 /* Parse the string-literal message. */
13859 message
= cp_parser_string_literal (parser
,
13860 /*translate=*/false,
13863 /* A `)' completes the static assertion. */
13864 if (!parens
.require_close (parser
))
13865 cp_parser_skip_to_closing_parenthesis (parser
,
13866 /*recovering=*/true,
13867 /*or_comma=*/false,
13868 /*consume_paren=*/true);
13871 /* A semicolon terminates the declaration. */
13872 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13874 /* Get the location for the static assertion. Use that of the
13875 condition if available, otherwise, use that of the "static_assert"
13877 location_t assert_loc
= condition
.get_location ();
13878 if (assert_loc
== UNKNOWN_LOCATION
)
13879 assert_loc
= token_loc
;
13881 /* Complete the static assertion, which may mean either processing
13882 the static assert now or saving it for template instantiation. */
13883 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13886 /* Parse the expression in decltype ( expression ). */
13889 cp_parser_decltype_expr (cp_parser
*parser
,
13890 bool &id_expression_or_member_access_p
)
13892 cp_token
*id_expr_start_token
;
13895 /* Since we're going to preserve any side-effects from this parse, set up a
13896 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13897 in the expression. */
13898 tentative_firewall
firewall (parser
);
13900 /* First, try parsing an id-expression. */
13901 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13902 cp_parser_parse_tentatively (parser
);
13903 expr
= cp_parser_id_expression (parser
,
13904 /*template_keyword_p=*/false,
13905 /*check_dependency_p=*/true,
13906 /*template_p=*/NULL
,
13907 /*declarator_p=*/false,
13908 /*optional_p=*/false);
13910 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13912 bool non_integral_constant_expression_p
= false;
13913 tree id_expression
= expr
;
13915 const char *error_msg
;
13917 if (identifier_p (expr
))
13918 /* Lookup the name we got back from the id-expression. */
13919 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13920 id_expr_start_token
->location
);
13923 && expr
!= error_mark_node
13924 && TREE_CODE (expr
) != TYPE_DECL
13925 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13926 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13927 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13929 /* Complete lookup of the id-expression. */
13930 expr
= (finish_id_expression
13931 (id_expression
, expr
, parser
->scope
, &idk
,
13932 /*integral_constant_expression_p=*/false,
13933 /*allow_non_integral_constant_expression_p=*/true,
13934 &non_integral_constant_expression_p
,
13935 /*template_p=*/false,
13937 /*address_p=*/false,
13938 /*template_arg_p=*/false,
13940 id_expr_start_token
->location
));
13942 if (expr
== error_mark_node
)
13943 /* We found an id-expression, but it was something that we
13944 should not have found. This is an error, not something
13945 we can recover from, so note that we found an
13946 id-expression and we'll recover as gracefully as
13948 id_expression_or_member_access_p
= true;
13952 && expr
!= error_mark_node
13953 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13954 /* We have an id-expression. */
13955 id_expression_or_member_access_p
= true;
13958 if (!id_expression_or_member_access_p
)
13960 /* Abort the id-expression parse. */
13961 cp_parser_abort_tentative_parse (parser
);
13963 /* Parsing tentatively, again. */
13964 cp_parser_parse_tentatively (parser
);
13966 /* Parse a class member access. */
13967 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
13968 /*cast_p=*/false, /*decltype*/true,
13969 /*member_access_only_p=*/true, NULL
);
13972 && expr
!= error_mark_node
13973 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13974 /* We have an id-expression. */
13975 id_expression_or_member_access_p
= true;
13978 if (id_expression_or_member_access_p
)
13979 /* We have parsed the complete id-expression or member access. */
13980 cp_parser_parse_definitely (parser
);
13983 /* Abort our attempt to parse an id-expression or member access
13985 cp_parser_abort_tentative_parse (parser
);
13987 /* Parse a full expression. */
13988 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
13989 /*decltype_p=*/true);
13995 /* Parse a `decltype' type. Returns the type.
13997 simple-type-specifier:
13998 decltype ( expression )
14000 decltype ( auto ) */
14003 cp_parser_decltype (cp_parser
*parser
)
14006 bool id_expression_or_member_access_p
= false;
14007 const char *saved_message
;
14008 bool saved_integral_constant_expression_p
;
14009 bool saved_non_integral_constant_expression_p
;
14010 bool saved_greater_than_is_operator_p
;
14011 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14013 if (start_token
->type
== CPP_DECLTYPE
)
14015 /* Already parsed. */
14016 cp_lexer_consume_token (parser
->lexer
);
14017 return saved_checks_value (start_token
->u
.tree_check_value
);
14020 /* Look for the `decltype' token. */
14021 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14022 return error_mark_node
;
14024 /* Parse the opening `('. */
14025 matching_parens parens
;
14026 if (!parens
.require_open (parser
))
14027 return error_mark_node
;
14029 /* decltype (auto) */
14030 if (cxx_dialect
>= cxx14
14031 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14033 cp_lexer_consume_token (parser
->lexer
);
14034 if (!parens
.require_close (parser
))
14035 return error_mark_node
;
14036 expr
= make_decltype_auto ();
14037 AUTO_IS_DECLTYPE (expr
) = true;
14041 /* Types cannot be defined in a `decltype' expression. Save away the
14043 saved_message
= parser
->type_definition_forbidden_message
;
14045 /* And create the new one. */
14046 parser
->type_definition_forbidden_message
14047 = G_("types may not be defined in %<decltype%> expressions");
14049 /* The restrictions on constant-expressions do not apply inside
14050 decltype expressions. */
14051 saved_integral_constant_expression_p
14052 = parser
->integral_constant_expression_p
;
14053 saved_non_integral_constant_expression_p
14054 = parser
->non_integral_constant_expression_p
;
14055 parser
->integral_constant_expression_p
= false;
14057 /* Within a parenthesized expression, a `>' token is always
14058 the greater-than operator. */
14059 saved_greater_than_is_operator_p
14060 = parser
->greater_than_is_operator_p
;
14061 parser
->greater_than_is_operator_p
= true;
14063 /* Do not actually evaluate the expression. */
14064 ++cp_unevaluated_operand
;
14066 /* Do not warn about problems with the expression. */
14067 ++c_inhibit_evaluation_warnings
;
14069 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14071 /* Go back to evaluating expressions. */
14072 --cp_unevaluated_operand
;
14073 --c_inhibit_evaluation_warnings
;
14075 /* The `>' token might be the end of a template-id or
14076 template-parameter-list now. */
14077 parser
->greater_than_is_operator_p
14078 = saved_greater_than_is_operator_p
;
14080 /* Restore the old message and the integral constant expression
14082 parser
->type_definition_forbidden_message
= saved_message
;
14083 parser
->integral_constant_expression_p
14084 = saved_integral_constant_expression_p
;
14085 parser
->non_integral_constant_expression_p
14086 = saved_non_integral_constant_expression_p
;
14088 /* Parse to the closing `)'. */
14089 if (!parens
.require_close (parser
))
14091 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14092 /*consume_paren=*/true);
14093 return error_mark_node
;
14096 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14097 tf_warning_or_error
);
14100 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14102 start_token
->type
= CPP_DECLTYPE
;
14103 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14104 start_token
->u
.tree_check_value
->value
= expr
;
14105 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14106 start_token
->keyword
= RID_MAX
;
14107 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14112 /* Special member functions [gram.special] */
14114 /* Parse a conversion-function-id.
14116 conversion-function-id:
14117 operator conversion-type-id
14119 Returns an IDENTIFIER_NODE representing the operator. */
14122 cp_parser_conversion_function_id (cp_parser
* parser
)
14126 tree saved_qualifying_scope
;
14127 tree saved_object_scope
;
14128 tree pushed_scope
= NULL_TREE
;
14130 /* Look for the `operator' token. */
14131 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14132 return error_mark_node
;
14133 /* When we parse the conversion-type-id, the current scope will be
14134 reset. However, we need that information in able to look up the
14135 conversion function later, so we save it here. */
14136 saved_scope
= parser
->scope
;
14137 saved_qualifying_scope
= parser
->qualifying_scope
;
14138 saved_object_scope
= parser
->object_scope
;
14139 /* We must enter the scope of the class so that the names of
14140 entities declared within the class are available in the
14141 conversion-type-id. For example, consider:
14148 S::operator I() { ... }
14150 In order to see that `I' is a type-name in the definition, we
14151 must be in the scope of `S'. */
14153 pushed_scope
= push_scope (saved_scope
);
14154 /* Parse the conversion-type-id. */
14155 type
= cp_parser_conversion_type_id (parser
);
14156 /* Leave the scope of the class, if any. */
14158 pop_scope (pushed_scope
);
14159 /* Restore the saved scope. */
14160 parser
->scope
= saved_scope
;
14161 parser
->qualifying_scope
= saved_qualifying_scope
;
14162 parser
->object_scope
= saved_object_scope
;
14163 /* If the TYPE is invalid, indicate failure. */
14164 if (type
== error_mark_node
)
14165 return error_mark_node
;
14166 return make_conv_op_name (type
);
14169 /* Parse a conversion-type-id:
14171 conversion-type-id:
14172 type-specifier-seq conversion-declarator [opt]
14174 Returns the TYPE specified. */
14177 cp_parser_conversion_type_id (cp_parser
* parser
)
14180 cp_decl_specifier_seq type_specifiers
;
14181 cp_declarator
*declarator
;
14182 tree type_specified
;
14183 const char *saved_message
;
14185 /* Parse the attributes. */
14186 attributes
= cp_parser_attributes_opt (parser
);
14188 saved_message
= parser
->type_definition_forbidden_message
;
14189 parser
->type_definition_forbidden_message
14190 = G_("types may not be defined in a conversion-type-id");
14192 /* Parse the type-specifiers. */
14193 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14194 /*is_trailing_return=*/false,
14197 parser
->type_definition_forbidden_message
= saved_message
;
14199 /* If that didn't work, stop. */
14200 if (type_specifiers
.type
== error_mark_node
)
14201 return error_mark_node
;
14202 /* Parse the conversion-declarator. */
14203 declarator
= cp_parser_conversion_declarator_opt (parser
);
14205 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14206 /*initialized=*/0, &attributes
);
14208 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14210 /* Don't give this error when parsing tentatively. This happens to
14211 work because we always parse this definitively once. */
14212 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14213 && type_uses_auto (type_specified
))
14215 if (cxx_dialect
< cxx14
)
14217 error ("invalid use of %<auto%> in conversion operator");
14218 return error_mark_node
;
14220 else if (template_parm_scope_p ())
14221 warning (0, "use of %<auto%> in member template "
14222 "conversion operator can never be deduced");
14225 return type_specified
;
14228 /* Parse an (optional) conversion-declarator.
14230 conversion-declarator:
14231 ptr-operator conversion-declarator [opt]
14235 static cp_declarator
*
14236 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14238 enum tree_code code
;
14239 tree class_type
, std_attributes
= NULL_TREE
;
14240 cp_cv_quals cv_quals
;
14242 /* We don't know if there's a ptr-operator next, or not. */
14243 cp_parser_parse_tentatively (parser
);
14244 /* Try the ptr-operator. */
14245 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14247 /* If it worked, look for more conversion-declarators. */
14248 if (cp_parser_parse_definitely (parser
))
14250 cp_declarator
*declarator
;
14252 /* Parse another optional declarator. */
14253 declarator
= cp_parser_conversion_declarator_opt (parser
);
14255 declarator
= cp_parser_make_indirect_declarator
14256 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14264 /* Parse an (optional) ctor-initializer.
14267 : mem-initializer-list */
14270 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14272 /* If the next token is not a `:', then there is no
14273 ctor-initializer. */
14274 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14276 /* Do default initialization of any bases and members. */
14277 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14278 finish_mem_initializers (NULL_TREE
);
14282 /* Consume the `:' token. */
14283 cp_lexer_consume_token (parser
->lexer
);
14284 /* And the mem-initializer-list. */
14285 cp_parser_mem_initializer_list (parser
);
14288 /* Parse a mem-initializer-list.
14290 mem-initializer-list:
14291 mem-initializer ... [opt]
14292 mem-initializer ... [opt] , mem-initializer-list */
14295 cp_parser_mem_initializer_list (cp_parser
* parser
)
14297 tree mem_initializer_list
= NULL_TREE
;
14298 tree target_ctor
= error_mark_node
;
14299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14301 /* Let the semantic analysis code know that we are starting the
14302 mem-initializer-list. */
14303 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14304 error_at (token
->location
,
14305 "only constructors take member initializers");
14307 /* Loop through the list. */
14310 tree mem_initializer
;
14312 token
= cp_lexer_peek_token (parser
->lexer
);
14313 /* Parse the mem-initializer. */
14314 mem_initializer
= cp_parser_mem_initializer (parser
);
14315 /* If the next token is a `...', we're expanding member initializers. */
14316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14318 /* Consume the `...'. */
14319 cp_lexer_consume_token (parser
->lexer
);
14321 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14322 can be expanded but members cannot. */
14323 if (mem_initializer
!= error_mark_node
14324 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14326 error_at (token
->location
,
14327 "cannot expand initializer for member %qD",
14328 TREE_PURPOSE (mem_initializer
));
14329 mem_initializer
= error_mark_node
;
14332 /* Construct the pack expansion type. */
14333 if (mem_initializer
!= error_mark_node
)
14334 mem_initializer
= make_pack_expansion (mem_initializer
);
14336 if (target_ctor
!= error_mark_node
14337 && mem_initializer
!= error_mark_node
)
14339 error ("mem-initializer for %qD follows constructor delegation",
14340 TREE_PURPOSE (mem_initializer
));
14341 mem_initializer
= error_mark_node
;
14343 /* Look for a target constructor. */
14344 if (mem_initializer
!= error_mark_node
14345 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14346 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14348 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14349 if (mem_initializer_list
)
14351 error ("constructor delegation follows mem-initializer for %qD",
14352 TREE_PURPOSE (mem_initializer_list
));
14353 mem_initializer
= error_mark_node
;
14355 target_ctor
= mem_initializer
;
14357 /* Add it to the list, unless it was erroneous. */
14358 if (mem_initializer
!= error_mark_node
)
14360 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14361 mem_initializer_list
= mem_initializer
;
14363 /* If the next token is not a `,', we're done. */
14364 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14366 /* Consume the `,' token. */
14367 cp_lexer_consume_token (parser
->lexer
);
14370 /* Perform semantic analysis. */
14371 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14372 finish_mem_initializers (mem_initializer_list
);
14375 /* Parse a mem-initializer.
14378 mem-initializer-id ( expression-list [opt] )
14379 mem-initializer-id braced-init-list
14384 ( expression-list [opt] )
14386 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14387 class) or FIELD_DECL (for a non-static data member) to initialize;
14388 the TREE_VALUE is the expression-list. An empty initialization
14389 list is represented by void_list_node. */
14392 cp_parser_mem_initializer (cp_parser
* parser
)
14394 tree mem_initializer_id
;
14395 tree expression_list
;
14397 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14399 /* Find out what is being initialized. */
14400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14402 permerror (token
->location
,
14403 "anachronistic old-style base class initializer");
14404 mem_initializer_id
= NULL_TREE
;
14408 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14409 if (mem_initializer_id
== error_mark_node
)
14410 return mem_initializer_id
;
14412 member
= expand_member_init (mem_initializer_id
);
14413 if (member
&& !DECL_P (member
))
14414 in_base_initializer
= 1;
14416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14418 bool expr_non_constant_p
;
14419 cp_lexer_set_source_position (parser
->lexer
);
14420 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14421 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14422 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14423 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14427 vec
<tree
, va_gc
> *vec
;
14428 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14430 /*allow_expansion_p=*/true,
14431 /*non_constant_p=*/NULL
);
14433 return error_mark_node
;
14434 expression_list
= build_tree_list_vec (vec
);
14435 release_tree_vector (vec
);
14438 if (expression_list
== error_mark_node
)
14439 return error_mark_node
;
14440 if (!expression_list
)
14441 expression_list
= void_type_node
;
14443 in_base_initializer
= 0;
14445 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14448 /* Parse a mem-initializer-id.
14450 mem-initializer-id:
14451 :: [opt] nested-name-specifier [opt] class-name
14452 decltype-specifier (C++11)
14455 Returns a TYPE indicating the class to be initialized for the first
14456 production (and the second in C++11). Returns an IDENTIFIER_NODE
14457 indicating the data member to be initialized for the last production. */
14460 cp_parser_mem_initializer_id (cp_parser
* parser
)
14462 bool global_scope_p
;
14463 bool nested_name_specifier_p
;
14464 bool template_p
= false;
14467 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14469 /* `typename' is not allowed in this context ([temp.res]). */
14470 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14472 error_at (token
->location
,
14473 "keyword %<typename%> not allowed in this context (a qualified "
14474 "member initializer is implicitly a type)");
14475 cp_lexer_consume_token (parser
->lexer
);
14477 /* Look for the optional `::' operator. */
14479 = (cp_parser_global_scope_opt (parser
,
14480 /*current_scope_valid_p=*/false)
14482 /* Look for the optional nested-name-specifier. The simplest way to
14487 The keyword `typename' is not permitted in a base-specifier or
14488 mem-initializer; in these contexts a qualified name that
14489 depends on a template-parameter is implicitly assumed to be a
14492 is to assume that we have seen the `typename' keyword at this
14494 nested_name_specifier_p
14495 = (cp_parser_nested_name_specifier_opt (parser
,
14496 /*typename_keyword_p=*/true,
14497 /*check_dependency_p=*/true,
14499 /*is_declaration=*/true)
14501 if (nested_name_specifier_p
)
14502 template_p
= cp_parser_optional_template_keyword (parser
);
14503 /* If there is a `::' operator or a nested-name-specifier, then we
14504 are definitely looking for a class-name. */
14505 if (global_scope_p
|| nested_name_specifier_p
)
14506 return cp_parser_class_name (parser
,
14507 /*typename_keyword_p=*/true,
14508 /*template_keyword_p=*/template_p
,
14510 /*check_dependency_p=*/true,
14511 /*class_head_p=*/false,
14512 /*is_declaration=*/true);
14513 /* Otherwise, we could also be looking for an ordinary identifier. */
14514 cp_parser_parse_tentatively (parser
);
14515 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14516 /* Try a decltype-specifier. */
14517 id
= cp_parser_decltype (parser
);
14519 /* Otherwise, try a class-name. */
14520 id
= cp_parser_class_name (parser
,
14521 /*typename_keyword_p=*/true,
14522 /*template_keyword_p=*/false,
14524 /*check_dependency_p=*/true,
14525 /*class_head_p=*/false,
14526 /*is_declaration=*/true);
14527 /* If we found one, we're done. */
14528 if (cp_parser_parse_definitely (parser
))
14530 /* Otherwise, look for an ordinary identifier. */
14531 return cp_parser_identifier (parser
);
14534 /* Overloading [gram.over] */
14536 /* Parse an operator-function-id.
14538 operator-function-id:
14541 Returns an IDENTIFIER_NODE for the operator which is a
14542 human-readable spelling of the identifier, e.g., `operator +'. */
14545 cp_parser_operator_function_id (cp_parser
* parser
)
14547 /* Look for the `operator' keyword. */
14548 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14549 return error_mark_node
;
14550 /* And then the name of the operator itself. */
14551 return cp_parser_operator (parser
);
14554 /* Return an identifier node for a user-defined literal operator.
14555 The suffix identifier is chained to the operator name identifier. */
14558 cp_literal_operator_id (const char* name
)
14561 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14562 + strlen (name
) + 10);
14563 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14564 identifier
= get_identifier (buffer
);
14569 /* Parse an operator.
14572 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14573 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14574 || ++ -- , ->* -> () []
14581 Returns an IDENTIFIER_NODE for the operator which is a
14582 human-readable spelling of the identifier, e.g., `operator +'. */
14585 cp_parser_operator (cp_parser
* parser
)
14587 tree id
= NULL_TREE
;
14591 /* Peek at the next token. */
14592 token
= cp_lexer_peek_token (parser
->lexer
);
14594 location_t start_loc
= token
->location
;
14596 /* Figure out which operator we have. */
14597 enum tree_code op
= ERROR_MARK
;
14598 bool assop
= false;
14599 bool consumed
= false;
14600 switch (token
->type
)
14604 /* The keyword should be either `new' or `delete'. */
14605 if (token
->keyword
== RID_NEW
)
14607 else if (token
->keyword
== RID_DELETE
)
14612 /* Consume the `new' or `delete' token. */
14613 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14615 /* Peek at the next token. */
14616 token
= cp_lexer_peek_token (parser
->lexer
);
14617 /* If it's a `[' token then this is the array variant of the
14619 if (token
->type
== CPP_OPEN_SQUARE
)
14621 /* Consume the `[' token. */
14622 cp_lexer_consume_token (parser
->lexer
);
14623 /* Look for the `]' token. */
14624 if (cp_token
*close_token
14625 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14626 end_loc
= close_token
->location
;
14627 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14629 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14647 op
= TRUNC_DIV_EXPR
;
14651 op
= TRUNC_MOD_EXPR
;
14671 op
= TRUTH_NOT_EXPR
;
14704 op
= TRUNC_DIV_EXPR
;
14709 op
= TRUNC_MOD_EXPR
;
14735 case CPP_LSHIFT_EQ
:
14740 case CPP_RSHIFT_EQ
:
14757 case CPP_GREATER_EQ
:
14762 op
= TRUTH_ANDIF_EXPR
;
14766 op
= TRUTH_ORIF_EXPR
;
14769 case CPP_PLUS_PLUS
:
14770 op
= POSTINCREMENT_EXPR
;
14773 case CPP_MINUS_MINUS
:
14774 op
= PREDECREMENT_EXPR
;
14778 op
= COMPOUND_EXPR
;
14781 case CPP_DEREF_STAR
:
14786 op
= COMPONENT_REF
;
14789 case CPP_OPEN_PAREN
:
14791 /* Consume the `('. */
14792 matching_parens parens
;
14793 parens
.consume_open (parser
);
14794 /* Look for the matching `)'. */
14795 parens
.require_close (parser
);
14801 case CPP_OPEN_SQUARE
:
14802 /* Consume the `['. */
14803 cp_lexer_consume_token (parser
->lexer
);
14804 /* Look for the matching `]'. */
14805 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14810 case CPP_UTF8STRING
:
14811 case CPP_UTF8STRING_USERDEF
:
14818 case CPP_STRING_USERDEF
:
14819 case CPP_WSTRING_USERDEF
:
14820 case CPP_STRING16_USERDEF
:
14821 case CPP_STRING32_USERDEF
:
14823 tree str
, string_tree
;
14826 if (cxx_dialect
== cxx98
)
14827 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14829 /* Consume the string. */
14830 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14831 /*wide_ok=*/true, /*lookup_udlit=*/false);
14832 if (str
== error_mark_node
)
14833 return error_mark_node
;
14834 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14836 string_tree
= USERDEF_LITERAL_VALUE (str
);
14837 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14842 /* Look for the suffix identifier. */
14843 token
= cp_lexer_peek_token (parser
->lexer
);
14844 if (token
->type
== CPP_NAME
)
14845 id
= cp_parser_identifier (parser
);
14846 else if (token
->type
== CPP_KEYWORD
)
14848 error ("unexpected keyword;"
14849 " remove space between quotes and suffix identifier");
14850 return error_mark_node
;
14854 error ("expected suffix identifier");
14855 return error_mark_node
;
14858 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14859 (TREE_TYPE (TREE_TYPE (string_tree
))));
14860 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14863 error ("expected empty string after %<operator%> keyword");
14864 return error_mark_node
;
14866 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14869 error ("invalid encoding prefix in literal operator");
14870 return error_mark_node
;
14872 if (id
!= error_mark_node
)
14874 const char *name
= IDENTIFIER_POINTER (id
);
14875 id
= cp_literal_operator_id (name
);
14881 /* Anything else is an error. */
14885 /* If we have selected an identifier, we need to consume the
14887 if (op
!= ERROR_MARK
)
14889 id
= ovl_op_identifier (assop
, op
);
14891 cp_lexer_consume_token (parser
->lexer
);
14893 /* Otherwise, no valid operator name was present. */
14896 cp_parser_error (parser
, "expected operator");
14897 id
= error_mark_node
;
14900 return cp_expr (id
, start_loc
);
14903 /* Parse a template-declaration.
14905 template-declaration:
14906 export [opt] template < template-parameter-list > declaration
14908 If MEMBER_P is TRUE, this template-declaration occurs within a
14911 The grammar rule given by the standard isn't correct. What
14912 is really meant is:
14914 template-declaration:
14915 export [opt] template-parameter-list-seq
14916 decl-specifier-seq [opt] init-declarator [opt] ;
14917 export [opt] template-parameter-list-seq
14918 function-definition
14920 template-parameter-list-seq:
14921 template-parameter-list-seq [opt]
14922 template < template-parameter-list >
14924 Concept Extensions:
14926 template-parameter-list-seq:
14927 template < template-parameter-list > requires-clause [opt]
14930 requires logical-or-expression */
14933 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14935 /* Check for `export'. */
14936 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
14938 /* Consume the `export' token. */
14939 cp_lexer_consume_token (parser
->lexer
);
14940 /* Warn that we do not support `export'. */
14941 warning (0, "keyword %<export%> not implemented, and will be ignored");
14944 cp_parser_template_declaration_after_export (parser
, member_p
);
14947 /* Parse a template-parameter-list.
14949 template-parameter-list:
14951 template-parameter-list , template-parameter
14953 Returns a TREE_LIST. Each node represents a template parameter.
14954 The nodes are connected via their TREE_CHAINs. */
14957 cp_parser_template_parameter_list (cp_parser
* parser
)
14959 tree parameter_list
= NULL_TREE
;
14961 begin_template_parm_list ();
14963 /* The loop below parses the template parms. We first need to know
14964 the total number of template parms to be able to compute proper
14965 canonical types of each dependent type. So after the loop, when
14966 we know the total number of template parms,
14967 end_template_parm_list computes the proper canonical types and
14968 fixes up the dependent types accordingly. */
14973 bool is_parameter_pack
;
14974 location_t parm_loc
;
14976 /* Parse the template-parameter. */
14977 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14978 parameter
= cp_parser_template_parameter (parser
,
14980 &is_parameter_pack
);
14981 /* Add it to the list. */
14982 if (parameter
!= error_mark_node
)
14983 parameter_list
= process_template_parm (parameter_list
,
14987 is_parameter_pack
);
14990 tree err_parm
= build_tree_list (parameter
, parameter
);
14991 parameter_list
= chainon (parameter_list
, err_parm
);
14994 /* If the next token is not a `,', we're done. */
14995 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14997 /* Otherwise, consume the `,' token. */
14998 cp_lexer_consume_token (parser
->lexer
);
15001 return end_template_parm_list (parameter_list
);
15004 /* Parse a introduction-list.
15007 introduced-parameter
15008 introduction-list , introduced-parameter
15010 introduced-parameter:
15011 ...[opt] identifier
15013 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15014 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15015 WILDCARD_DECL will also have DECL_NAME set and token location in
15016 DECL_SOURCE_LOCATION. */
15019 cp_parser_introduction_list (cp_parser
*parser
)
15021 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15025 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15027 cp_lexer_consume_token (parser
->lexer
);
15029 /* Build placeholder. */
15030 tree parm
= build_nt (WILDCARD_DECL
);
15031 DECL_SOURCE_LOCATION (parm
)
15032 = cp_lexer_peek_token (parser
->lexer
)->location
;
15033 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15034 WILDCARD_PACK_P (parm
) = is_pack
;
15035 vec_safe_push (introduction_vec
, parm
);
15037 /* If the next token is not a `,', we're done. */
15038 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15040 /* Otherwise, consume the `,' token. */
15041 cp_lexer_consume_token (parser
->lexer
);
15044 /* Convert the vec into a TREE_VEC. */
15045 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15048 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15049 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15051 release_tree_vector (introduction_vec
);
15052 return introduction_list
;
15055 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15056 is an abstract declarator. */
15058 static inline cp_declarator
*
15059 get_id_declarator (cp_declarator
*declarator
)
15061 cp_declarator
*d
= declarator
;
15062 while (d
&& d
->kind
!= cdk_id
)
15067 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15068 is an abstract declarator. */
15071 get_unqualified_id (cp_declarator
*declarator
)
15073 declarator
= get_id_declarator (declarator
);
15075 return declarator
->u
.id
.unqualified_name
;
15080 /* Returns true if DECL represents a constrained-parameter. */
15083 is_constrained_parameter (tree decl
)
15086 && TREE_CODE (decl
) == TYPE_DECL
15087 && CONSTRAINED_PARM_CONCEPT (decl
)
15088 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15091 /* Returns true if PARM declares a constrained-parameter. */
15094 is_constrained_parameter (cp_parameter_declarator
*parm
)
15096 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15099 /* Check that the type parameter is only a declarator-id, and that its
15100 type is not cv-qualified. */
15103 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15104 cp_parameter_declarator
*parm
)
15106 if (!parm
->declarator
)
15109 if (parm
->declarator
->kind
!= cdk_id
)
15111 cp_parser_error (parser
, "invalid constrained type parameter");
15115 /* Don't allow cv-qualified type parameters. */
15116 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15117 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15119 cp_parser_error (parser
, "cv-qualified type parameter");
15126 /* Finish parsing/processing a template type parameter and checking
15127 various restrictions. */
15130 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15132 cp_parameter_declarator
* parmdecl
)
15134 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15135 return finish_template_type_parm (class_type_node
, id
);
15137 return error_mark_node
;
15141 finish_constrained_template_template_parm (tree proto
, tree id
)
15143 /* FIXME: This should probably be copied, and we may need to adjust
15144 the template parameter depths. */
15145 tree saved_parms
= current_template_parms
;
15146 begin_template_parm_list ();
15147 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15148 end_template_parm_list ();
15150 tree parm
= finish_template_template_parm (class_type_node
, id
);
15151 current_template_parms
= saved_parms
;
15156 /* Finish parsing/processing a template template parameter by borrowing
15157 the template parameter list from the prototype parameter. */
15160 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15163 cp_parameter_declarator
*parmdecl
)
15165 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15166 return error_mark_node
;
15167 return finish_constrained_template_template_parm (proto
, id
);
15170 /* Create a new non-type template parameter from the given PARM
15174 constrained_non_type_template_parm (bool *is_non_type
,
15175 cp_parameter_declarator
*parm
)
15177 *is_non_type
= true;
15178 cp_declarator
*decl
= parm
->declarator
;
15179 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15180 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15181 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15184 /* Build a constrained template parameter based on the PARMDECL
15185 declarator. The type of PARMDECL is the constrained type, which
15186 refers to the prototype template parameter that ultimately
15187 specifies the type of the declared parameter. */
15190 finish_constrained_parameter (cp_parser
*parser
,
15191 cp_parameter_declarator
*parmdecl
,
15193 bool *is_parameter_pack
)
15195 tree decl
= parmdecl
->decl_specifiers
.type
;
15196 tree id
= get_unqualified_id (parmdecl
->declarator
);
15197 tree def
= parmdecl
->default_argument
;
15198 tree proto
= DECL_INITIAL (decl
);
15200 /* A template parameter constrained by a variadic concept shall also
15201 be declared as a template parameter pack. */
15202 bool is_variadic
= template_parameter_pack_p (proto
);
15203 if (is_variadic
&& !*is_parameter_pack
)
15204 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15206 /* Build the parameter. Return an error if the declarator was invalid. */
15208 if (TREE_CODE (proto
) == TYPE_DECL
)
15209 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15210 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15211 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15214 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15215 if (parm
== error_mark_node
)
15216 return error_mark_node
;
15218 /* Finish the parameter decl and create a node attaching the
15219 default argument and constraint. */
15220 parm
= build_tree_list (def
, parm
);
15221 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15226 /* Returns true if the parsed type actually represents the declaration
15227 of a type template-parameter. */
15230 declares_constrained_type_template_parameter (tree type
)
15232 return (is_constrained_parameter (type
)
15233 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15237 /* Returns true if the parsed type actually represents the declaration of
15238 a template template-parameter. */
15241 declares_constrained_template_template_parameter (tree type
)
15243 return (is_constrained_parameter (type
)
15244 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15247 /* Parse a default argument for a type template-parameter.
15248 Note that diagnostics are handled in cp_parser_template_parameter. */
15251 cp_parser_default_type_template_argument (cp_parser
*parser
)
15253 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15255 /* Consume the `=' token. */
15256 cp_lexer_consume_token (parser
->lexer
);
15258 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15260 /* Parse the default-argument. */
15261 push_deferring_access_checks (dk_no_deferred
);
15262 tree default_argument
= cp_parser_type_id (parser
);
15263 pop_deferring_access_checks ();
15265 if (flag_concepts
&& type_uses_auto (default_argument
))
15267 error_at (token
->location
,
15268 "invalid use of %<auto%> in default template argument");
15269 return error_mark_node
;
15272 return default_argument
;
15275 /* Parse a default argument for a template template-parameter. */
15278 cp_parser_default_template_template_argument (cp_parser
*parser
)
15280 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15284 /* Consume the `='. */
15285 cp_lexer_consume_token (parser
->lexer
);
15286 /* Parse the id-expression. */
15287 push_deferring_access_checks (dk_no_deferred
);
15288 /* save token before parsing the id-expression, for error
15290 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15291 tree default_argument
15292 = cp_parser_id_expression (parser
,
15293 /*template_keyword_p=*/false,
15294 /*check_dependency_p=*/true,
15295 /*template_p=*/&is_template
,
15296 /*declarator_p=*/false,
15297 /*optional_p=*/false);
15298 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15299 /* If the id-expression was a template-id that refers to
15300 a template-class, we already have the declaration here,
15301 so no further lookup is needed. */
15304 /* Look up the name. */
15306 = cp_parser_lookup_name (parser
, default_argument
,
15308 /*is_template=*/is_template
,
15309 /*is_namespace=*/false,
15310 /*check_dependency=*/true,
15311 /*ambiguous_decls=*/NULL
,
15313 /* See if the default argument is valid. */
15314 default_argument
= check_template_template_default_arg (default_argument
);
15315 pop_deferring_access_checks ();
15316 return default_argument
;
15319 /* Parse a template-parameter.
15321 template-parameter:
15323 parameter-declaration
15325 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15326 the parameter. The TREE_PURPOSE is the default value, if any.
15327 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15328 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15329 set to true iff this parameter is a parameter pack. */
15332 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15333 bool *is_parameter_pack
)
15336 cp_parameter_declarator
*parameter_declarator
;
15339 /* Assume it is a type parameter or a template parameter. */
15340 *is_non_type
= false;
15341 /* Assume it not a parameter pack. */
15342 *is_parameter_pack
= false;
15343 /* Peek at the next token. */
15344 token
= cp_lexer_peek_token (parser
->lexer
);
15345 /* If it is `template', we have a type-parameter. */
15346 if (token
->keyword
== RID_TEMPLATE
)
15347 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15348 /* If it is `class' or `typename' we do not know yet whether it is a
15349 type parameter or a non-type parameter. Consider:
15351 template <typename T, typename T::X X> ...
15355 template <class C, class D*> ...
15357 Here, the first parameter is a type parameter, and the second is
15358 a non-type parameter. We can tell by looking at the token after
15359 the identifier -- if it is a `,', `=', or `>' then we have a type
15361 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15363 /* Peek at the token after `class' or `typename'. */
15364 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15365 /* If it's an ellipsis, we have a template type parameter
15367 if (token
->type
== CPP_ELLIPSIS
)
15368 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15369 /* If it's an identifier, skip it. */
15370 if (token
->type
== CPP_NAME
)
15371 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15372 /* Now, see if the token looks like the end of a template
15374 if (token
->type
== CPP_COMMA
15375 || token
->type
== CPP_EQ
15376 || token
->type
== CPP_GREATER
)
15377 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15380 /* Otherwise, it is a non-type parameter or a constrained parameter.
15384 When parsing a default template-argument for a non-type
15385 template-parameter, the first non-nested `>' is taken as the end
15386 of the template parameter-list rather than a greater-than
15388 parameter_declarator
15389 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15390 /*parenthesized_p=*/NULL
);
15392 if (!parameter_declarator
)
15393 return error_mark_node
;
15395 /* If the parameter declaration is marked as a parameter pack, set
15396 *IS_PARAMETER_PACK to notify the caller. */
15397 if (parameter_declarator
->template_parameter_pack_p
)
15398 *is_parameter_pack
= true;
15400 if (parameter_declarator
->default_argument
)
15402 /* Can happen in some cases of erroneous input (c++/34892). */
15403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15404 /* Consume the `...' for better error recovery. */
15405 cp_lexer_consume_token (parser
->lexer
);
15408 // The parameter may have been constrained.
15409 if (is_constrained_parameter (parameter_declarator
))
15410 return finish_constrained_parameter (parser
,
15411 parameter_declarator
,
15413 is_parameter_pack
);
15415 // Now we're sure that the parameter is a non-type parameter.
15416 *is_non_type
= true;
15418 parm
= grokdeclarator (parameter_declarator
->declarator
,
15419 ¶meter_declarator
->decl_specifiers
,
15420 TPARM
, /*initialized=*/0,
15421 /*attrlist=*/NULL
);
15422 if (parm
== error_mark_node
)
15423 return error_mark_node
;
15425 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15428 /* Parse a type-parameter.
15431 class identifier [opt]
15432 class identifier [opt] = type-id
15433 typename identifier [opt]
15434 typename identifier [opt] = type-id
15435 template < template-parameter-list > class identifier [opt]
15436 template < template-parameter-list > class identifier [opt]
15439 GNU Extension (variadic templates):
15442 class ... identifier [opt]
15443 typename ... identifier [opt]
15445 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15446 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15447 the declaration of the parameter.
15449 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15452 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15457 /* Look for a keyword to tell us what kind of parameter this is. */
15458 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15460 return error_mark_node
;
15462 switch (token
->keyword
)
15468 tree default_argument
;
15470 /* If the next token is an ellipsis, we have a template
15472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15474 /* Consume the `...' token. */
15475 cp_lexer_consume_token (parser
->lexer
);
15476 maybe_warn_variadic_templates ();
15478 *is_parameter_pack
= true;
15481 /* If the next token is an identifier, then it names the
15483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15484 identifier
= cp_parser_identifier (parser
);
15486 identifier
= NULL_TREE
;
15488 /* Create the parameter. */
15489 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15491 /* If the next token is an `=', we have a default argument. */
15492 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15495 = cp_parser_default_type_template_argument (parser
);
15497 /* Template parameter packs cannot have default
15499 if (*is_parameter_pack
)
15502 error_at (token
->location
,
15503 "template parameter pack %qD cannot have a "
15504 "default argument", identifier
);
15506 error_at (token
->location
,
15507 "template parameter packs cannot have "
15508 "default arguments");
15509 default_argument
= NULL_TREE
;
15511 else if (check_for_bare_parameter_packs (default_argument
))
15512 default_argument
= error_mark_node
;
15515 default_argument
= NULL_TREE
;
15517 /* Create the combined representation of the parameter and the
15518 default argument. */
15519 parameter
= build_tree_list (default_argument
, parameter
);
15526 tree default_argument
;
15528 /* Look for the `<'. */
15529 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15530 /* Parse the template-parameter-list. */
15531 cp_parser_template_parameter_list (parser
);
15532 /* Look for the `>'. */
15533 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15535 // If template requirements are present, parse them.
15538 tree reqs
= get_shorthand_constraints (current_template_parms
);
15539 if (tree r
= cp_parser_requires_clause_opt (parser
))
15540 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15541 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15544 /* Look for the `class' or 'typename' keywords. */
15545 cp_parser_type_parameter_key (parser
);
15546 /* If the next token is an ellipsis, we have a template
15548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15550 /* Consume the `...' token. */
15551 cp_lexer_consume_token (parser
->lexer
);
15552 maybe_warn_variadic_templates ();
15554 *is_parameter_pack
= true;
15556 /* If the next token is an `=', then there is a
15557 default-argument. If the next token is a `>', we are at
15558 the end of the parameter-list. If the next token is a `,',
15559 then we are at the end of this parameter. */
15560 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15561 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15562 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15564 identifier
= cp_parser_identifier (parser
);
15565 /* Treat invalid names as if the parameter were nameless. */
15566 if (identifier
== error_mark_node
)
15567 identifier
= NULL_TREE
;
15570 identifier
= NULL_TREE
;
15572 /* Create the template parameter. */
15573 parameter
= finish_template_template_parm (class_type_node
,
15576 /* If the next token is an `=', then there is a
15577 default-argument. */
15578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15581 = cp_parser_default_template_template_argument (parser
);
15583 /* Template parameter packs cannot have default
15585 if (*is_parameter_pack
)
15588 error_at (token
->location
,
15589 "template parameter pack %qD cannot "
15590 "have a default argument",
15593 error_at (token
->location
, "template parameter packs cannot "
15594 "have default arguments");
15595 default_argument
= NULL_TREE
;
15599 default_argument
= NULL_TREE
;
15601 /* Create the combined representation of the parameter and the
15602 default argument. */
15603 parameter
= build_tree_list (default_argument
, parameter
);
15608 gcc_unreachable ();
15615 /* Parse a template-id.
15618 template-name < template-argument-list [opt] >
15620 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15621 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15622 returned. Otherwise, if the template-name names a function, or set
15623 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15624 names a class, returns a TYPE_DECL for the specialization.
15626 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15627 uninstantiated templates. */
15630 cp_parser_template_id (cp_parser
*parser
,
15631 bool template_keyword_p
,
15632 bool check_dependency_p
,
15633 enum tag_types tag_type
,
15634 bool is_declaration
)
15639 cp_token_position start_of_id
= 0;
15640 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15641 bool is_identifier
;
15643 /* If the next token corresponds to a template-id, there is no need
15645 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15646 if (token
->type
== CPP_TEMPLATE_ID
)
15648 cp_lexer_consume_token (parser
->lexer
);
15649 return saved_checks_value (token
->u
.tree_check_value
);
15652 /* Avoid performing name lookup if there is no possibility of
15653 finding a template-id. */
15654 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15655 || (token
->type
== CPP_NAME
15656 && !cp_parser_nth_token_starts_template_argument_list_p
15659 cp_parser_error (parser
, "expected template-id");
15660 return error_mark_node
;
15663 /* Remember where the template-id starts. */
15664 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15665 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15667 push_deferring_access_checks (dk_deferred
);
15669 /* Parse the template-name. */
15670 is_identifier
= false;
15671 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15672 check_dependency_p
,
15676 if (templ
== error_mark_node
|| is_identifier
)
15678 pop_deferring_access_checks ();
15682 /* Since we're going to preserve any side-effects from this parse, set up a
15683 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15684 in the template arguments. */
15685 tentative_firewall
firewall (parser
);
15687 /* If we find the sequence `[:' after a template-name, it's probably
15688 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15689 parse correctly the argument list. */
15690 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15691 == CPP_OPEN_SQUARE
)
15692 && next_token
->flags
& DIGRAPH
15693 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15695 && !(next_token_2
->flags
& PREV_WHITE
))
15697 cp_parser_parse_tentatively (parser
);
15698 /* Change `:' into `::'. */
15699 next_token_2
->type
= CPP_SCOPE
;
15700 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15702 cp_lexer_consume_token (parser
->lexer
);
15704 /* Parse the arguments. */
15705 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15706 if (!cp_parser_parse_definitely (parser
))
15708 /* If we couldn't parse an argument list, then we revert our changes
15709 and return simply an error. Maybe this is not a template-id
15711 next_token_2
->type
= CPP_COLON
;
15712 cp_parser_error (parser
, "expected %<<%>");
15713 pop_deferring_access_checks ();
15714 return error_mark_node
;
15716 /* Otherwise, emit an error about the invalid digraph, but continue
15717 parsing because we got our argument list. */
15718 if (permerror (next_token
->location
,
15719 "%<<::%> cannot begin a template-argument list"))
15721 static bool hint
= false;
15722 inform (next_token
->location
,
15723 "%<<:%> is an alternate spelling for %<[%>."
15724 " Insert whitespace between %<<%> and %<::%>");
15725 if (!hint
&& !flag_permissive
)
15727 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15728 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15729 "accept your code)");
15736 /* Look for the `<' that starts the template-argument-list. */
15737 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15739 pop_deferring_access_checks ();
15740 return error_mark_node
;
15742 /* Parse the arguments. */
15743 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15746 /* Set the location to be of the form:
15747 template-name < template-argument-list [opt] >
15748 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15749 with caret == start at the start of the template-name,
15750 ranging until the closing '>'. */
15751 location_t finish_loc
15752 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15753 location_t combined_loc
15754 = make_location (token
->location
, token
->location
, finish_loc
);
15756 /* Build a representation of the specialization. */
15757 if (identifier_p (templ
))
15758 template_id
= build_min_nt_loc (combined_loc
,
15761 else if (DECL_TYPE_TEMPLATE_P (templ
)
15762 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15764 bool entering_scope
;
15765 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15766 template (rather than some instantiation thereof) only if
15767 is not nested within some other construct. For example, in
15768 "template <typename T> void f(T) { A<T>::", A<T> is just an
15769 instantiation of A. */
15770 entering_scope
= (template_parm_scope_p ()
15771 && cp_lexer_next_token_is (parser
->lexer
,
15774 = finish_template_type (templ
, arguments
, entering_scope
);
15776 /* A template-like identifier may be a partial concept id. */
15777 else if (flag_concepts
15778 && (template_id
= (cp_parser_maybe_partial_concept_id
15779 (parser
, templ
, arguments
))))
15780 return template_id
;
15781 else if (variable_template_p (templ
))
15783 template_id
= lookup_template_variable (templ
, arguments
);
15784 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15785 SET_EXPR_LOCATION (template_id
, combined_loc
);
15789 /* If it's not a class-template or a template-template, it should be
15790 a function-template. */
15791 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15792 || TREE_CODE (templ
) == OVERLOAD
15793 || BASELINK_P (templ
)));
15795 template_id
= lookup_template_function (templ
, arguments
);
15796 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15797 SET_EXPR_LOCATION (template_id
, combined_loc
);
15800 /* If parsing tentatively, replace the sequence of tokens that makes
15801 up the template-id with a CPP_TEMPLATE_ID token. That way,
15802 should we re-parse the token stream, we will not have to repeat
15803 the effort required to do the parse, nor will we issue duplicate
15804 error messages about problems during instantiation of the
15807 /* Don't do this if we had a parse error in a declarator; re-parsing
15808 might succeed if a name changes meaning (60361). */
15809 && !(cp_parser_error_occurred (parser
)
15810 && cp_parser_parsing_tentatively (parser
)
15811 && parser
->in_declarator_p
))
15813 /* Reset the contents of the START_OF_ID token. */
15814 token
->type
= CPP_TEMPLATE_ID
;
15815 token
->location
= combined_loc
;
15817 /* We must mark the lookup as kept, so we don't throw it away on
15818 the first parse. */
15819 if (is_overloaded_fn (template_id
))
15820 lookup_keep (get_fns (template_id
), true);
15822 /* Retrieve any deferred checks. Do not pop this access checks yet
15823 so the memory will not be reclaimed during token replacing below. */
15824 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15825 token
->u
.tree_check_value
->value
= template_id
;
15826 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15827 token
->keyword
= RID_MAX
;
15829 /* Purge all subsequent tokens. */
15830 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15832 /* ??? Can we actually assume that, if template_id ==
15833 error_mark_node, we will have issued a diagnostic to the
15834 user, as opposed to simply marking the tentative parse as
15836 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15837 error_at (token
->location
, "parse error in template argument list");
15840 pop_to_parent_deferring_access_checks ();
15841 return template_id
;
15844 /* Parse a template-name.
15849 The standard should actually say:
15853 operator-function-id
15855 A defect report has been filed about this issue.
15857 A conversion-function-id cannot be a template name because they cannot
15858 be part of a template-id. In fact, looking at this code:
15860 a.operator K<int>()
15862 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15863 It is impossible to call a templated conversion-function-id with an
15864 explicit argument list, since the only allowed template parameter is
15865 the type to which it is converting.
15867 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15868 `template' keyword, in a construction like:
15872 In that case `f' is taken to be a template-name, even though there
15873 is no way of knowing for sure.
15875 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15876 name refers to a set of overloaded functions, at least one of which
15877 is a template, or an IDENTIFIER_NODE with the name of the template,
15878 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15879 names are looked up inside uninstantiated templates. */
15882 cp_parser_template_name (cp_parser
* parser
,
15883 bool template_keyword_p
,
15884 bool check_dependency_p
,
15885 bool is_declaration
,
15886 enum tag_types tag_type
,
15887 bool *is_identifier
)
15891 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15893 /* If the next token is `operator', then we have either an
15894 operator-function-id or a conversion-function-id. */
15895 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15897 /* We don't know whether we're looking at an
15898 operator-function-id or a conversion-function-id. */
15899 cp_parser_parse_tentatively (parser
);
15900 /* Try an operator-function-id. */
15901 identifier
= cp_parser_operator_function_id (parser
);
15902 /* If that didn't work, try a conversion-function-id. */
15903 if (!cp_parser_parse_definitely (parser
))
15905 cp_parser_error (parser
, "expected template-name");
15906 return error_mark_node
;
15909 /* Look for the identifier. */
15911 identifier
= cp_parser_identifier (parser
);
15913 /* If we didn't find an identifier, we don't have a template-id. */
15914 if (identifier
== error_mark_node
)
15915 return error_mark_node
;
15917 /* If the name immediately followed the `template' keyword, then it
15918 is a template-name. However, if the next token is not `<', then
15919 we do not treat it as a template-name, since it is not being used
15920 as part of a template-id. This enables us to handle constructs
15923 template <typename T> struct S { S(); };
15924 template <typename T> S<T>::S();
15926 correctly. We would treat `S' as a template -- if it were `S<T>'
15927 -- but we do not if there is no `<'. */
15929 if (processing_template_decl
15930 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15932 /* In a declaration, in a dependent context, we pretend that the
15933 "template" keyword was present in order to improve error
15934 recovery. For example, given:
15936 template <typename T> void f(T::X<int>);
15938 we want to treat "X<int>" as a template-id. */
15940 && !template_keyword_p
15941 && parser
->scope
&& TYPE_P (parser
->scope
)
15942 && check_dependency_p
15943 && dependent_scope_p (parser
->scope
)
15944 /* Do not do this for dtors (or ctors), since they never
15945 need the template keyword before their name. */
15946 && !constructor_name_p (identifier
, parser
->scope
))
15948 cp_token_position start
= 0;
15950 /* Explain what went wrong. */
15951 error_at (token
->location
, "non-template %qD used as template",
15953 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
15954 parser
->scope
, identifier
);
15955 /* If parsing tentatively, find the location of the "<" token. */
15956 if (cp_parser_simulate_error (parser
))
15957 start
= cp_lexer_token_position (parser
->lexer
, true);
15958 /* Parse the template arguments so that we can issue error
15959 messages about them. */
15960 cp_lexer_consume_token (parser
->lexer
);
15961 cp_parser_enclosed_template_argument_list (parser
);
15962 /* Skip tokens until we find a good place from which to
15963 continue parsing. */
15964 cp_parser_skip_to_closing_parenthesis (parser
,
15965 /*recovering=*/true,
15967 /*consume_paren=*/false);
15968 /* If parsing tentatively, permanently remove the
15969 template argument list. That will prevent duplicate
15970 error messages from being issued about the missing
15971 "template" keyword. */
15973 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
15975 *is_identifier
= true;
15976 parser
->context
->object_type
= NULL_TREE
;
15980 /* If the "template" keyword is present, then there is generally
15981 no point in doing name-lookup, so we just return IDENTIFIER.
15982 But, if the qualifying scope is non-dependent then we can
15983 (and must) do name-lookup normally. */
15984 if (template_keyword_p
)
15986 tree scope
= (parser
->scope
? parser
->scope
15987 : parser
->context
->object_type
);
15988 if (scope
&& TYPE_P (scope
)
15989 && (!CLASS_TYPE_P (scope
)
15990 || (check_dependency_p
&& dependent_type_p (scope
))))
15992 /* We're optimizing away the call to cp_parser_lookup_name, but
15993 we still need to do this. */
15994 parser
->context
->object_type
= NULL_TREE
;
16000 /* Look up the name. */
16001 decl
= cp_parser_lookup_name (parser
, identifier
,
16003 /*is_template=*/true,
16004 /*is_namespace=*/false,
16005 check_dependency_p
,
16006 /*ambiguous_decls=*/NULL
,
16009 decl
= strip_using_decl (decl
);
16011 /* If DECL is a template, then the name was a template-name. */
16012 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16014 if (TREE_DEPRECATED (decl
)
16015 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16016 warn_deprecated_use (decl
, NULL_TREE
);
16020 /* The standard does not explicitly indicate whether a name that
16021 names a set of overloaded declarations, some of which are
16022 templates, is a template-name. However, such a name should
16023 be a template-name; otherwise, there is no way to form a
16024 template-id for the overloaded templates. */
16025 bool found
= false;
16027 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16028 !found
&& iter
; ++iter
)
16029 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16034 /* The name does not name a template. */
16035 cp_parser_error (parser
, "expected template-name");
16036 return error_mark_node
;
16040 /* If DECL is dependent, and refers to a function, then just return
16041 its name; we will look it up again during template instantiation. */
16042 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16044 tree scope
= ovl_scope (decl
);
16045 if (TYPE_P (scope
) && dependent_type_p (scope
))
16052 /* Parse a template-argument-list.
16054 template-argument-list:
16055 template-argument ... [opt]
16056 template-argument-list , template-argument ... [opt]
16058 Returns a TREE_VEC containing the arguments. */
16061 cp_parser_template_argument_list (cp_parser
* parser
)
16063 tree fixed_args
[10];
16064 unsigned n_args
= 0;
16065 unsigned alloced
= 10;
16066 tree
*arg_ary
= fixed_args
;
16068 bool saved_in_template_argument_list_p
;
16070 bool saved_non_ice_p
;
16072 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16073 parser
->in_template_argument_list_p
= true;
16074 /* Even if the template-id appears in an integral
16075 constant-expression, the contents of the argument list do
16077 saved_ice_p
= parser
->integral_constant_expression_p
;
16078 parser
->integral_constant_expression_p
= false;
16079 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16080 parser
->non_integral_constant_expression_p
= false;
16082 /* Parse the arguments. */
16088 /* Consume the comma. */
16089 cp_lexer_consume_token (parser
->lexer
);
16091 /* Parse the template-argument. */
16092 argument
= cp_parser_template_argument (parser
);
16094 /* If the next token is an ellipsis, we're expanding a template
16096 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16098 if (argument
== error_mark_node
)
16100 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16101 error_at (token
->location
,
16102 "expected parameter pack before %<...%>");
16104 /* Consume the `...' token. */
16105 cp_lexer_consume_token (parser
->lexer
);
16107 /* Make the argument into a TYPE_PACK_EXPANSION or
16108 EXPR_PACK_EXPANSION. */
16109 argument
= make_pack_expansion (argument
);
16112 if (n_args
== alloced
)
16116 if (arg_ary
== fixed_args
)
16118 arg_ary
= XNEWVEC (tree
, alloced
);
16119 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16122 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16124 arg_ary
[n_args
++] = argument
;
16126 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16128 vec
= make_tree_vec (n_args
);
16131 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16133 if (arg_ary
!= fixed_args
)
16135 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16136 parser
->integral_constant_expression_p
= saved_ice_p
;
16137 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16139 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16143 /* Parse a template-argument.
16146 assignment-expression
16150 The representation is that of an assignment-expression, type-id, or
16151 id-expression -- except that the qualified id-expression is
16152 evaluated, so that the value returned is either a DECL or an
16155 Although the standard says "assignment-expression", it forbids
16156 throw-expressions or assignments in the template argument.
16157 Therefore, we use "conditional-expression" instead. */
16160 cp_parser_template_argument (cp_parser
* parser
)
16165 bool maybe_type_id
= false;
16166 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16167 location_t loc
= 0;
16170 /* There's really no way to know what we're looking at, so we just
16171 try each alternative in order.
16175 In a template-argument, an ambiguity between a type-id and an
16176 expression is resolved to a type-id, regardless of the form of
16177 the corresponding template-parameter.
16179 Therefore, we try a type-id first. */
16180 cp_parser_parse_tentatively (parser
);
16181 argument
= cp_parser_template_type_arg (parser
);
16182 /* If there was no error parsing the type-id but the next token is a
16183 '>>', our behavior depends on which dialect of C++ we're
16184 parsing. In C++98, we probably found a typo for '> >'. But there
16185 are type-id which are also valid expressions. For instance:
16187 struct X { int operator >> (int); };
16188 template <int V> struct Foo {};
16191 Here 'X()' is a valid type-id of a function type, but the user just
16192 wanted to write the expression "X() >> 5". Thus, we remember that we
16193 found a valid type-id, but we still try to parse the argument as an
16194 expression to see what happens.
16196 In C++0x, the '>>' will be considered two separate '>'
16198 if (!cp_parser_error_occurred (parser
)
16199 && cxx_dialect
== cxx98
16200 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16202 maybe_type_id
= true;
16203 cp_parser_abort_tentative_parse (parser
);
16207 /* If the next token isn't a `,' or a `>', then this argument wasn't
16208 really finished. This means that the argument is not a valid
16210 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16211 cp_parser_error (parser
, "expected template-argument");
16212 /* If that worked, we're done. */
16213 if (cp_parser_parse_definitely (parser
))
16216 /* We're still not sure what the argument will be. */
16217 cp_parser_parse_tentatively (parser
);
16218 /* Try a template. */
16219 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16220 argument
= cp_parser_id_expression (parser
,
16221 /*template_keyword_p=*/false,
16222 /*check_dependency_p=*/true,
16224 /*declarator_p=*/false,
16225 /*optional_p=*/false);
16226 /* If the next token isn't a `,' or a `>', then this argument wasn't
16227 really finished. */
16228 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16229 cp_parser_error (parser
, "expected template-argument");
16230 if (!cp_parser_error_occurred (parser
))
16232 /* Figure out what is being referred to. If the id-expression
16233 was for a class template specialization, then we will have a
16234 TYPE_DECL at this point. There is no need to do name lookup
16235 at this point in that case. */
16236 if (TREE_CODE (argument
) != TYPE_DECL
)
16237 argument
= cp_parser_lookup_name (parser
, argument
,
16239 /*is_template=*/template_p
,
16240 /*is_namespace=*/false,
16241 /*check_dependency=*/true,
16242 /*ambiguous_decls=*/NULL
,
16243 argument_start_token
->location
);
16244 /* Handle a constrained-type-specifier for a non-type template
16246 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16248 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16249 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16250 cp_parser_error (parser
, "expected template-name");
16252 if (cp_parser_parse_definitely (parser
))
16254 if (TREE_DEPRECATED (argument
))
16255 warn_deprecated_use (argument
, NULL_TREE
);
16258 /* It must be a non-type argument. In C++17 any constant-expression is
16260 if (cxx_dialect
> cxx14
)
16263 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16265 -- an integral constant-expression of integral or enumeration
16268 -- the name of a non-type template-parameter; or
16270 -- the name of an object or function with external linkage...
16272 -- the address of an object or function with external linkage...
16274 -- a pointer to member... */
16275 /* Look for a non-type template parameter. */
16276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16278 cp_parser_parse_tentatively (parser
);
16279 argument
= cp_parser_primary_expression (parser
,
16280 /*address_p=*/false,
16282 /*template_arg_p=*/true,
16284 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16285 || !cp_parser_next_token_ends_template_argument_p (parser
))
16286 cp_parser_simulate_error (parser
);
16287 if (cp_parser_parse_definitely (parser
))
16291 /* If the next token is "&", the argument must be the address of an
16292 object or function with external linkage. */
16293 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16296 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16297 cp_lexer_consume_token (parser
->lexer
);
16299 /* See if we might have an id-expression. */
16300 token
= cp_lexer_peek_token (parser
->lexer
);
16301 if (token
->type
== CPP_NAME
16302 || token
->keyword
== RID_OPERATOR
16303 || token
->type
== CPP_SCOPE
16304 || token
->type
== CPP_TEMPLATE_ID
16305 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16307 cp_parser_parse_tentatively (parser
);
16308 argument
= cp_parser_primary_expression (parser
,
16311 /*template_arg_p=*/true,
16313 if (cp_parser_error_occurred (parser
)
16314 || !cp_parser_next_token_ends_template_argument_p (parser
))
16315 cp_parser_abort_tentative_parse (parser
);
16320 if (INDIRECT_REF_P (argument
))
16322 /* Strip the dereference temporarily. */
16323 gcc_assert (REFERENCE_REF_P (argument
));
16324 argument
= TREE_OPERAND (argument
, 0);
16327 /* If we're in a template, we represent a qualified-id referring
16328 to a static data member as a SCOPE_REF even if the scope isn't
16329 dependent so that we can check access control later. */
16331 if (TREE_CODE (probe
) == SCOPE_REF
)
16332 probe
= TREE_OPERAND (probe
, 1);
16335 /* A variable without external linkage might still be a
16336 valid constant-expression, so no error is issued here
16337 if the external-linkage check fails. */
16338 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16339 cp_parser_simulate_error (parser
);
16341 else if (is_overloaded_fn (argument
))
16342 /* All overloaded functions are allowed; if the external
16343 linkage test does not pass, an error will be issued
16347 && (TREE_CODE (argument
) == OFFSET_REF
16348 || TREE_CODE (argument
) == SCOPE_REF
))
16349 /* A pointer-to-member. */
16351 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16354 cp_parser_simulate_error (parser
);
16356 if (cp_parser_parse_definitely (parser
))
16359 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16360 tf_warning_or_error
);
16362 argument
= convert_from_reference (argument
);
16367 /* If the argument started with "&", there are no other valid
16368 alternatives at this point. */
16371 cp_parser_error (parser
, "invalid non-type template argument");
16372 return error_mark_node
;
16376 /* If the argument wasn't successfully parsed as a type-id followed
16377 by '>>', the argument can only be a constant expression now.
16378 Otherwise, we try parsing the constant-expression tentatively,
16379 because the argument could really be a type-id. */
16381 cp_parser_parse_tentatively (parser
);
16383 if (cxx_dialect
<= cxx14
)
16384 argument
= cp_parser_constant_expression (parser
);
16387 /* With C++17 generalized non-type template arguments we need to handle
16388 lvalue constant expressions, too. */
16389 argument
= cp_parser_assignment_expression (parser
);
16390 require_potential_constant_expression (argument
);
16393 if (!maybe_type_id
)
16395 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16396 cp_parser_error (parser
, "expected template-argument");
16397 if (cp_parser_parse_definitely (parser
))
16399 /* We did our best to parse the argument as a non type-id, but that
16400 was the only alternative that matched (albeit with a '>' after
16401 it). We can assume it's just a typo from the user, and a
16402 diagnostic will then be issued. */
16403 return cp_parser_template_type_arg (parser
);
16406 /* Parse an explicit-instantiation.
16408 explicit-instantiation:
16409 template declaration
16411 Although the standard says `declaration', what it really means is:
16413 explicit-instantiation:
16414 template decl-specifier-seq [opt] declarator [opt] ;
16416 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16417 supposed to be allowed. A defect report has been filed about this
16422 explicit-instantiation:
16423 storage-class-specifier template
16424 decl-specifier-seq [opt] declarator [opt] ;
16425 function-specifier template
16426 decl-specifier-seq [opt] declarator [opt] ; */
16429 cp_parser_explicit_instantiation (cp_parser
* parser
)
16431 int declares_class_or_enum
;
16432 cp_decl_specifier_seq decl_specifiers
;
16433 tree extension_specifier
= NULL_TREE
;
16435 timevar_push (TV_TEMPLATE_INST
);
16437 /* Look for an (optional) storage-class-specifier or
16438 function-specifier. */
16439 if (cp_parser_allow_gnu_extensions_p (parser
))
16441 extension_specifier
16442 = cp_parser_storage_class_specifier_opt (parser
);
16443 if (!extension_specifier
)
16444 extension_specifier
16445 = cp_parser_function_specifier_opt (parser
,
16446 /*decl_specs=*/NULL
);
16449 /* Look for the `template' keyword. */
16450 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16451 /* Let the front end know that we are processing an explicit
16453 begin_explicit_instantiation ();
16454 /* [temp.explicit] says that we are supposed to ignore access
16455 control while processing explicit instantiation directives. */
16456 push_deferring_access_checks (dk_no_check
);
16457 /* Parse a decl-specifier-seq. */
16458 cp_parser_decl_specifier_seq (parser
,
16459 CP_PARSER_FLAGS_OPTIONAL
,
16461 &declares_class_or_enum
);
16462 /* If there was exactly one decl-specifier, and it declared a class,
16463 and there's no declarator, then we have an explicit type
16465 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16469 type
= check_tag_decl (&decl_specifiers
,
16470 /*explicit_type_instantiation_p=*/true);
16471 /* Turn access control back on for names used during
16472 template instantiation. */
16473 pop_deferring_access_checks ();
16475 do_type_instantiation (type
, extension_specifier
,
16476 /*complain=*/tf_error
);
16480 cp_declarator
*declarator
;
16483 /* Parse the declarator. */
16485 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16486 /*ctor_dtor_or_conv_p=*/NULL
,
16487 /*parenthesized_p=*/NULL
,
16488 /*member_p=*/false,
16489 /*friend_p=*/false);
16490 if (declares_class_or_enum
& 2)
16491 cp_parser_check_for_definition_in_return_type (declarator
,
16492 decl_specifiers
.type
,
16493 decl_specifiers
.locations
[ds_type_spec
]);
16494 if (declarator
!= cp_error_declarator
)
16496 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16497 permerror (decl_specifiers
.locations
[ds_inline
],
16498 "explicit instantiation shall not use"
16499 " %<inline%> specifier");
16500 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16501 permerror (decl_specifiers
.locations
[ds_constexpr
],
16502 "explicit instantiation shall not use"
16503 " %<constexpr%> specifier");
16505 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16506 NORMAL
, 0, &decl_specifiers
.attributes
);
16507 /* Turn access control back on for names used during
16508 template instantiation. */
16509 pop_deferring_access_checks ();
16510 /* Do the explicit instantiation. */
16511 do_decl_instantiation (decl
, extension_specifier
);
16515 pop_deferring_access_checks ();
16516 /* Skip the body of the explicit instantiation. */
16517 cp_parser_skip_to_end_of_statement (parser
);
16520 /* We're done with the instantiation. */
16521 end_explicit_instantiation ();
16523 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16525 timevar_pop (TV_TEMPLATE_INST
);
16528 /* Parse an explicit-specialization.
16530 explicit-specialization:
16531 template < > declaration
16533 Although the standard says `declaration', what it really means is:
16535 explicit-specialization:
16536 template <> decl-specifier [opt] init-declarator [opt] ;
16537 template <> function-definition
16538 template <> explicit-specialization
16539 template <> template-declaration */
16542 cp_parser_explicit_specialization (cp_parser
* parser
)
16544 bool need_lang_pop
;
16545 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16547 /* Look for the `template' keyword. */
16548 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16549 /* Look for the `<'. */
16550 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16551 /* Look for the `>'. */
16552 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16553 /* We have processed another parameter list. */
16554 ++parser
->num_template_parameter_lists
;
16557 A template ... explicit specialization ... shall not have C
16559 if (current_lang_name
== lang_name_c
)
16561 error_at (token
->location
, "template specialization with C linkage");
16562 maybe_show_extern_c_location ();
16563 /* Give it C++ linkage to avoid confusing other parts of the
16565 push_lang_context (lang_name_cplusplus
);
16566 need_lang_pop
= true;
16569 need_lang_pop
= false;
16570 /* Let the front end know that we are beginning a specialization. */
16571 if (!begin_specialization ())
16573 end_specialization ();
16577 /* If the next keyword is `template', we need to figure out whether
16578 or not we're looking a template-declaration. */
16579 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16581 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16582 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16583 cp_parser_template_declaration_after_export (parser
,
16584 /*member_p=*/false);
16586 cp_parser_explicit_specialization (parser
);
16589 /* Parse the dependent declaration. */
16590 cp_parser_single_declaration (parser
,
16592 /*member_p=*/false,
16593 /*explicit_specialization_p=*/true,
16594 /*friend_p=*/NULL
);
16595 /* We're done with the specialization. */
16596 end_specialization ();
16597 /* For the erroneous case of a template with C linkage, we pushed an
16598 implicit C++ linkage scope; exit that scope now. */
16600 pop_lang_context ();
16601 /* We're done with this parameter list. */
16602 --parser
->num_template_parameter_lists
;
16605 /* Parse a type-specifier.
16608 simple-type-specifier
16611 elaborated-type-specifier
16619 Returns a representation of the type-specifier. For a
16620 class-specifier, enum-specifier, or elaborated-type-specifier, a
16621 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16623 The parser flags FLAGS is used to control type-specifier parsing.
16625 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16626 in a decl-specifier-seq.
16628 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16629 class-specifier, enum-specifier, or elaborated-type-specifier, then
16630 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16631 if a type is declared; 2 if it is defined. Otherwise, it is set to
16634 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16635 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16636 is set to FALSE. */
16639 cp_parser_type_specifier (cp_parser
* parser
,
16640 cp_parser_flags flags
,
16641 cp_decl_specifier_seq
*decl_specs
,
16642 bool is_declaration
,
16643 int* declares_class_or_enum
,
16644 bool* is_cv_qualifier
)
16646 tree type_spec
= NULL_TREE
;
16649 cp_decl_spec ds
= ds_last
;
16651 /* Assume this type-specifier does not declare a new type. */
16652 if (declares_class_or_enum
)
16653 *declares_class_or_enum
= 0;
16654 /* And that it does not specify a cv-qualifier. */
16655 if (is_cv_qualifier
)
16656 *is_cv_qualifier
= false;
16657 /* Peek at the next token. */
16658 token
= cp_lexer_peek_token (parser
->lexer
);
16660 /* If we're looking at a keyword, we can use that to guide the
16661 production we choose. */
16662 keyword
= token
->keyword
;
16666 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16667 goto elaborated_type_specifier
;
16669 /* Look for the enum-specifier. */
16670 type_spec
= cp_parser_enum_specifier (parser
);
16671 /* If that worked, we're done. */
16674 if (declares_class_or_enum
)
16675 *declares_class_or_enum
= 2;
16677 cp_parser_set_decl_spec_type (decl_specs
,
16680 /*type_definition_p=*/true);
16684 goto elaborated_type_specifier
;
16686 /* Any of these indicate either a class-specifier, or an
16687 elaborated-type-specifier. */
16691 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16692 goto elaborated_type_specifier
;
16694 /* Parse tentatively so that we can back up if we don't find a
16695 class-specifier. */
16696 cp_parser_parse_tentatively (parser
);
16697 /* Look for the class-specifier. */
16698 type_spec
= cp_parser_class_specifier (parser
);
16699 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16700 /* If that worked, we're done. */
16701 if (cp_parser_parse_definitely (parser
))
16703 if (declares_class_or_enum
)
16704 *declares_class_or_enum
= 2;
16706 cp_parser_set_decl_spec_type (decl_specs
,
16709 /*type_definition_p=*/true);
16713 /* Fall through. */
16714 elaborated_type_specifier
:
16715 /* We're declaring (not defining) a class or enum. */
16716 if (declares_class_or_enum
)
16717 *declares_class_or_enum
= 1;
16719 /* Fall through. */
16721 /* Look for an elaborated-type-specifier. */
16723 = (cp_parser_elaborated_type_specifier
16725 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16728 cp_parser_set_decl_spec_type (decl_specs
,
16731 /*type_definition_p=*/false);
16736 if (is_cv_qualifier
)
16737 *is_cv_qualifier
= true;
16742 if (is_cv_qualifier
)
16743 *is_cv_qualifier
= true;
16748 if (is_cv_qualifier
)
16749 *is_cv_qualifier
= true;
16753 /* The `__complex__' keyword is a GNU extension. */
16761 /* Handle simple keywords. */
16766 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16767 decl_specs
->any_specifiers_p
= true;
16769 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16772 /* If we do not already have a type-specifier, assume we are looking
16773 at a simple-type-specifier. */
16774 type_spec
= cp_parser_simple_type_specifier (parser
,
16778 /* If we didn't find a type-specifier, and a type-specifier was not
16779 optional in this context, issue an error message. */
16780 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16782 cp_parser_error (parser
, "expected type specifier");
16783 return error_mark_node
;
16789 /* Parse a simple-type-specifier.
16791 simple-type-specifier:
16792 :: [opt] nested-name-specifier [opt] type-name
16793 :: [opt] nested-name-specifier template template-id
16808 simple-type-specifier:
16810 decltype ( expression )
16813 __underlying_type ( type-id )
16817 nested-name-specifier(opt) template-name
16821 simple-type-specifier:
16823 __typeof__ unary-expression
16824 __typeof__ ( type-id )
16825 __typeof__ ( type-id ) { initializer-list , [opt] }
16827 Concepts Extension:
16829 simple-type-specifier:
16830 constrained-type-specifier
16832 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16833 appropriately updated. */
16836 cp_parser_simple_type_specifier (cp_parser
* parser
,
16837 cp_decl_specifier_seq
*decl_specs
,
16838 cp_parser_flags flags
)
16840 tree type
= NULL_TREE
;
16844 /* Peek at the next token. */
16845 token
= cp_lexer_peek_token (parser
->lexer
);
16847 /* If we're looking at a keyword, things are easy. */
16848 switch (token
->keyword
)
16852 decl_specs
->explicit_char_p
= true;
16853 type
= char_type_node
;
16856 type
= char16_type_node
;
16859 type
= char32_type_node
;
16862 type
= wchar_type_node
;
16865 type
= boolean_type_node
;
16868 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16869 type
= short_integer_type_node
;
16873 decl_specs
->explicit_int_p
= true;
16874 type
= integer_type_node
;
16880 idx
= token
->keyword
- RID_INT_N_0
;
16881 if (! int_n_enabled_p
[idx
])
16885 decl_specs
->explicit_intN_p
= true;
16886 decl_specs
->int_n_idx
= idx
;
16888 type
= int_n_trees
[idx
].signed_type
;
16892 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16893 type
= long_integer_type_node
;
16896 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16897 type
= integer_type_node
;
16900 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16901 type
= unsigned_type_node
;
16904 type
= float_type_node
;
16907 type
= double_type_node
;
16910 type
= void_type_node
;
16914 maybe_warn_cpp0x (CPP0X_AUTO
);
16915 if (parser
->auto_is_implicit_function_template_parm_p
)
16917 /* The 'auto' might be the placeholder return type for a function decl
16918 with trailing return type. */
16919 bool have_trailing_return_fn_decl
= false;
16921 cp_parser_parse_tentatively (parser
);
16922 cp_lexer_consume_token (parser
->lexer
);
16923 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16924 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16925 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16926 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16930 cp_lexer_consume_token (parser
->lexer
);
16931 cp_parser_skip_to_closing_parenthesis (parser
,
16932 /*recovering*/false,
16934 /*consume_paren*/true);
16938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
16940 have_trailing_return_fn_decl
= true;
16944 cp_lexer_consume_token (parser
->lexer
);
16946 cp_parser_abort_tentative_parse (parser
);
16948 if (have_trailing_return_fn_decl
)
16950 type
= make_auto ();
16954 if (cxx_dialect
>= cxx14
)
16956 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
16957 type
= TREE_TYPE (type
);
16960 type
= error_mark_node
;
16962 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
16964 if (cxx_dialect
< cxx14
)
16965 error_at (token
->location
,
16966 "use of %<auto%> in lambda parameter declaration "
16967 "only available with "
16968 "-std=c++14 or -std=gnu++14");
16970 else if (cxx_dialect
< cxx14
)
16971 error_at (token
->location
,
16972 "use of %<auto%> in parameter declaration "
16973 "only available with "
16974 "-std=c++14 or -std=gnu++14");
16975 else if (!flag_concepts
)
16976 pedwarn (token
->location
, OPT_Wpedantic
,
16977 "ISO C++ forbids use of %<auto%> in parameter "
16981 type
= make_auto ();
16985 /* Since DR 743, decltype can either be a simple-type-specifier by
16986 itself or begin a nested-name-specifier. Parsing it will replace
16987 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16988 handling below decide what to do. */
16989 cp_parser_decltype (parser
);
16990 cp_lexer_set_token_position (parser
->lexer
, token
);
16994 /* Consume the `typeof' token. */
16995 cp_lexer_consume_token (parser
->lexer
);
16996 /* Parse the operand to `typeof'. */
16997 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
16998 /* If it is not already a TYPE, take its type. */
16999 if (!TYPE_P (type
))
17000 type
= finish_typeof (type
);
17003 cp_parser_set_decl_spec_type (decl_specs
, type
,
17005 /*type_definition_p=*/false);
17009 case RID_UNDERLYING_TYPE
:
17010 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17012 cp_parser_set_decl_spec_type (decl_specs
, type
,
17014 /*type_definition_p=*/false);
17019 case RID_DIRECT_BASES
:
17020 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17022 cp_parser_set_decl_spec_type (decl_specs
, type
,
17024 /*type_definition_p=*/false);
17030 /* If token is an already-parsed decltype not followed by ::,
17031 it's a simple-type-specifier. */
17032 if (token
->type
== CPP_DECLTYPE
17033 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17035 type
= saved_checks_value (token
->u
.tree_check_value
);
17038 cp_parser_set_decl_spec_type (decl_specs
, type
,
17040 /*type_definition_p=*/false);
17041 /* Remember that we are handling a decltype in order to
17042 implement the resolution of DR 1510 when the argument
17043 isn't instantiation dependent. */
17044 decl_specs
->decltype_p
= true;
17046 cp_lexer_consume_token (parser
->lexer
);
17050 /* If the type-specifier was for a built-in type, we're done. */
17053 /* Record the type. */
17055 && (token
->keyword
!= RID_SIGNED
17056 && token
->keyword
!= RID_UNSIGNED
17057 && token
->keyword
!= RID_SHORT
17058 && token
->keyword
!= RID_LONG
))
17059 cp_parser_set_decl_spec_type (decl_specs
,
17062 /*type_definition_p=*/false);
17064 decl_specs
->any_specifiers_p
= true;
17066 /* Consume the token. */
17067 cp_lexer_consume_token (parser
->lexer
);
17069 if (type
== error_mark_node
)
17070 return error_mark_node
;
17072 /* There is no valid C++ program where a non-template type is
17073 followed by a "<". That usually indicates that the user thought
17074 that the type was a template. */
17075 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17078 return TYPE_NAME (type
);
17081 /* The type-specifier must be a user-defined type. */
17082 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17087 /* Don't gobble tokens or issue error messages if this is an
17088 optional type-specifier. */
17089 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17090 cp_parser_parse_tentatively (parser
);
17092 token
= cp_lexer_peek_token (parser
->lexer
);
17094 /* Look for the optional `::' operator. */
17096 = (cp_parser_global_scope_opt (parser
,
17097 /*current_scope_valid_p=*/false)
17099 /* Look for the nested-name specifier. */
17101 = (cp_parser_nested_name_specifier_opt (parser
,
17102 /*typename_keyword_p=*/false,
17103 /*check_dependency_p=*/true,
17105 /*is_declaration=*/false)
17107 /* If we have seen a nested-name-specifier, and the next token
17108 is `template', then we are using the template-id production. */
17110 && cp_parser_optional_template_keyword (parser
))
17112 /* Look for the template-id. */
17113 type
= cp_parser_template_id (parser
,
17114 /*template_keyword_p=*/true,
17115 /*check_dependency_p=*/true,
17117 /*is_declaration=*/false);
17118 /* If the template-id did not name a type, we are out of
17120 if (TREE_CODE (type
) != TYPE_DECL
)
17122 cp_parser_error (parser
, "expected template-id for type");
17126 /* Otherwise, look for a type-name. */
17128 type
= cp_parser_type_name (parser
);
17129 /* Keep track of all name-lookups performed in class scopes. */
17133 && TREE_CODE (type
) == TYPE_DECL
17134 && identifier_p (DECL_NAME (type
)))
17135 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17136 /* If it didn't work out, we don't have a TYPE. */
17137 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17138 && !cp_parser_parse_definitely (parser
))
17140 if (!type
&& cxx_dialect
>= cxx17
)
17142 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17143 cp_parser_parse_tentatively (parser
);
17145 cp_parser_global_scope_opt (parser
,
17146 /*current_scope_valid_p=*/false);
17147 cp_parser_nested_name_specifier_opt (parser
,
17148 /*typename_keyword_p=*/false,
17149 /*check_dependency_p=*/true,
17151 /*is_declaration=*/false);
17152 tree name
= cp_parser_identifier (parser
);
17153 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17154 && parser
->scope
!= error_mark_node
)
17156 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17158 /*is_template=*/false,
17159 /*is_namespace=*/false,
17160 /*check_dependency=*/true,
17161 /*ambiguous_decls=*/NULL
,
17163 if (tmpl
&& tmpl
!= error_mark_node
17164 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17165 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17166 type
= make_template_placeholder (tmpl
);
17169 type
= error_mark_node
;
17170 if (!cp_parser_simulate_error (parser
))
17171 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17172 NLE_TYPE
, token
->location
);
17176 type
= error_mark_node
;
17178 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17179 && !cp_parser_parse_definitely (parser
))
17182 if (type
&& decl_specs
)
17183 cp_parser_set_decl_spec_type (decl_specs
, type
,
17185 /*type_definition_p=*/false);
17188 /* If we didn't get a type-name, issue an error message. */
17189 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17191 cp_parser_error (parser
, "expected type-name");
17192 return error_mark_node
;
17195 if (type
&& type
!= error_mark_node
)
17197 /* See if TYPE is an Objective-C type, and if so, parse and
17198 accept any protocol references following it. Do this before
17199 the cp_parser_check_for_invalid_template_id() call, because
17200 Objective-C types can be followed by '<...>' which would
17201 enclose protocol names rather than template arguments, and so
17202 everything is fine. */
17203 if (c_dialect_objc () && !parser
->scope
17204 && (objc_is_id (type
) || objc_is_class_name (type
)))
17206 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17207 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17209 /* Clobber the "unqualified" type previously entered into
17210 DECL_SPECS with the new, improved protocol-qualified version. */
17212 decl_specs
->type
= qual_type
;
17217 /* There is no valid C++ program where a non-template type is
17218 followed by a "<". That usually indicates that the user
17219 thought that the type was a template. */
17220 cp_parser_check_for_invalid_template_id (parser
, type
,
17228 /* Parse a type-name.
17234 simple-template-id [in c++0x]
17251 Returns a TYPE_DECL for the type. */
17254 cp_parser_type_name (cp_parser
* parser
)
17256 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17261 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17265 /* We can't know yet whether it is a class-name or not. */
17266 cp_parser_parse_tentatively (parser
);
17267 /* Try a class-name. */
17268 type_decl
= cp_parser_class_name (parser
,
17269 typename_keyword_p
,
17270 /*template_keyword_p=*/false,
17272 /*check_dependency_p=*/true,
17273 /*class_head_p=*/false,
17274 /*is_declaration=*/false);
17275 /* If it's not a class-name, keep looking. */
17276 if (!cp_parser_parse_definitely (parser
))
17278 if (cxx_dialect
< cxx11
)
17279 /* It must be a typedef-name or an enum-name. */
17280 return cp_parser_nonclass_name (parser
);
17282 cp_parser_parse_tentatively (parser
);
17283 /* It is either a simple-template-id representing an
17284 instantiation of an alias template... */
17285 type_decl
= cp_parser_template_id (parser
,
17286 /*template_keyword_p=*/false,
17287 /*check_dependency_p=*/true,
17289 /*is_declaration=*/false);
17290 /* Note that this must be an instantiation of an alias template
17291 because [temp.names]/6 says:
17293 A template-id that names an alias template specialization
17296 Whereas [temp.names]/7 says:
17298 A simple-template-id that names a class template
17299 specialization is a class-name.
17301 With concepts, this could also be a partial-concept-id that
17302 declares a non-type template parameter. */
17303 if (type_decl
!= NULL_TREE
17304 && TREE_CODE (type_decl
) == TYPE_DECL
17305 && TYPE_DECL_ALIAS_P (type_decl
))
17306 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17307 else if (is_constrained_parameter (type_decl
))
17308 /* Don't do anything. */ ;
17310 cp_parser_simulate_error (parser
);
17312 if (!cp_parser_parse_definitely (parser
))
17313 /* ... Or a typedef-name or an enum-name. */
17314 return cp_parser_nonclass_name (parser
);
17320 /* Check if DECL and ARGS can form a constrained-type-specifier.
17321 If ARGS is non-null, we try to form a concept check of the
17322 form DECL<?, ARGS> where ? is a wildcard that matches any
17323 kind of template argument. If ARGS is NULL, then we try to
17324 form a concept check of the form DECL<?>. */
17327 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17328 tree decl
, tree args
)
17330 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17332 /* If we a constrained-type-specifier cannot be deduced. */
17333 if (parser
->prevent_constrained_type_specifiers
)
17336 /* A constrained type specifier can only be found in an
17337 overload set or as a reference to a template declaration.
17339 FIXME: This might be masking a bug. It's possible that
17340 that the deduction below is causing template specializations
17341 to be formed with the wildcard as an argument. */
17342 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17345 /* Try to build a call expression that evaluates the
17346 concept. This can fail if the overload set refers
17347 only to non-templates. */
17348 tree placeholder
= build_nt (WILDCARD_DECL
);
17349 tree check
= build_concept_check (decl
, placeholder
, args
);
17350 if (check
== error_mark_node
)
17353 /* Deduce the checked constraint and the prototype parameter.
17355 FIXME: In certain cases, failure to deduce should be a
17356 diagnosable error. */
17359 if (!deduce_constrained_parameter (check
, conc
, proto
))
17362 /* In template parameter scope, this results in a constrained
17363 parameter. Return a descriptor of that parm. */
17364 if (processing_template_parmlist
)
17365 return build_constrained_parameter (conc
, proto
, args
);
17367 /* In a parameter-declaration-clause, constrained-type
17368 specifiers result in invented template parameters. */
17369 if (parser
->auto_is_implicit_function_template_parm_p
)
17371 tree x
= build_constrained_parameter (conc
, proto
, args
);
17372 return synthesize_implicit_template_parm (parser
, x
);
17376 /* Otherwise, we're in a context where the constrained
17377 type name is deduced and the constraint applies
17378 after deduction. */
17379 return make_constrained_auto (conc
, args
);
17385 /* If DECL refers to a concept, return a TYPE_DECL representing
17386 the result of using the constrained type specifier in the
17387 current context. DECL refers to a concept if
17389 - it is an overload set containing a function concept taking a single
17392 - it is a variable concept taking a single type argument. */
17395 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17398 && (TREE_CODE (decl
) == OVERLOAD
17399 || BASELINK_P (decl
)
17400 || variable_concept_p (decl
)))
17401 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17406 /* Check if DECL and ARGS form a partial-concept-id. If so,
17407 assign ID to the resulting constrained placeholder.
17409 Returns true if the partial-concept-id designates a placeholder
17410 and false otherwise. Note that *id is set to NULL_TREE in
17414 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17416 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17419 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17431 Returns a TYPE_DECL for the type. */
17434 cp_parser_nonclass_name (cp_parser
* parser
)
17439 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17440 identifier
= cp_parser_identifier (parser
);
17441 if (identifier
== error_mark_node
)
17442 return error_mark_node
;
17444 /* Look up the type-name. */
17445 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17447 type_decl
= strip_using_decl (type_decl
);
17449 /* If we found an overload set, then it may refer to a concept-name. */
17450 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17453 if (TREE_CODE (type_decl
) != TYPE_DECL
17454 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17456 /* See if this is an Objective-C type. */
17457 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17458 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17460 type_decl
= TYPE_NAME (type
);
17463 /* Issue an error if we did not find a type-name. */
17464 if (TREE_CODE (type_decl
) != TYPE_DECL
17465 /* In Objective-C, we have the complication that class names are
17466 normally type names and start declarations (eg, the
17467 "NSObject" in "NSObject *object;"), but can be used in an
17468 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17469 is an expression. So, a classname followed by a dot is not a
17470 valid type-name. */
17471 || (objc_is_class_name (TREE_TYPE (type_decl
))
17472 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17474 if (!cp_parser_simulate_error (parser
))
17475 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17476 NLE_TYPE
, token
->location
);
17477 return error_mark_node
;
17479 /* Remember that the name was used in the definition of the
17480 current class so that we can check later to see if the
17481 meaning would have been different after the class was
17482 entirely defined. */
17483 else if (type_decl
!= error_mark_node
17485 maybe_note_name_used_in_class (identifier
, type_decl
);
17490 /* Parse an elaborated-type-specifier. Note that the grammar given
17491 here incorporates the resolution to DR68.
17493 elaborated-type-specifier:
17494 class-key :: [opt] nested-name-specifier [opt] identifier
17495 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17496 enum-key :: [opt] nested-name-specifier [opt] identifier
17497 typename :: [opt] nested-name-specifier identifier
17498 typename :: [opt] nested-name-specifier template [opt]
17503 elaborated-type-specifier:
17504 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17505 class-key attributes :: [opt] nested-name-specifier [opt]
17506 template [opt] template-id
17507 enum attributes :: [opt] nested-name-specifier [opt] identifier
17509 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17510 declared `friend'. If IS_DECLARATION is TRUE, then this
17511 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17512 something is being declared.
17514 Returns the TYPE specified. */
17517 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17519 bool is_declaration
)
17521 enum tag_types tag_type
;
17523 tree type
= NULL_TREE
;
17524 tree attributes
= NULL_TREE
;
17526 cp_token
*token
= NULL
;
17528 /* See if we're looking at the `enum' keyword. */
17529 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17531 /* Consume the `enum' token. */
17532 cp_lexer_consume_token (parser
->lexer
);
17533 /* Remember that it's an enumeration type. */
17534 tag_type
= enum_type
;
17535 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17536 enums) is used here. */
17537 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17538 if (cp_parser_is_keyword (token
, RID_CLASS
)
17539 || cp_parser_is_keyword (token
, RID_STRUCT
))
17541 gcc_rich_location
richloc (token
->location
);
17542 richloc
.add_range (input_location
, false);
17543 richloc
.add_fixit_remove ();
17544 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17545 "a scoped enum must not use the %qD keyword",
17547 /* Consume the `struct' or `class' and parse it anyway. */
17548 cp_lexer_consume_token (parser
->lexer
);
17550 /* Parse the attributes. */
17551 attributes
= cp_parser_attributes_opt (parser
);
17553 /* Or, it might be `typename'. */
17554 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17557 /* Consume the `typename' token. */
17558 cp_lexer_consume_token (parser
->lexer
);
17559 /* Remember that it's a `typename' type. */
17560 tag_type
= typename_type
;
17562 /* Otherwise it must be a class-key. */
17565 tag_type
= cp_parser_class_key (parser
);
17566 if (tag_type
== none_type
)
17567 return error_mark_node
;
17568 /* Parse the attributes. */
17569 attributes
= cp_parser_attributes_opt (parser
);
17572 /* Look for the `::' operator. */
17573 globalscope
= cp_parser_global_scope_opt (parser
,
17574 /*current_scope_valid_p=*/false);
17575 /* Look for the nested-name-specifier. */
17576 tree nested_name_specifier
;
17577 if (tag_type
== typename_type
&& !globalscope
)
17579 nested_name_specifier
17580 = cp_parser_nested_name_specifier (parser
,
17581 /*typename_keyword_p=*/true,
17582 /*check_dependency_p=*/true,
17585 if (!nested_name_specifier
)
17586 return error_mark_node
;
17589 /* Even though `typename' is not present, the proposed resolution
17590 to Core Issue 180 says that in `class A<T>::B', `B' should be
17591 considered a type-name, even if `A<T>' is dependent. */
17592 nested_name_specifier
17593 = cp_parser_nested_name_specifier_opt (parser
,
17594 /*typename_keyword_p=*/true,
17595 /*check_dependency_p=*/true,
17598 /* For everything but enumeration types, consider a template-id.
17599 For an enumeration type, consider only a plain identifier. */
17600 if (tag_type
!= enum_type
)
17602 bool template_p
= false;
17605 /* Allow the `template' keyword. */
17606 template_p
= cp_parser_optional_template_keyword (parser
);
17607 /* If we didn't see `template', we don't know if there's a
17608 template-id or not. */
17610 cp_parser_parse_tentatively (parser
);
17611 /* Parse the template-id. */
17612 token
= cp_lexer_peek_token (parser
->lexer
);
17613 decl
= cp_parser_template_id (parser
, template_p
,
17614 /*check_dependency_p=*/true,
17617 /* If we didn't find a template-id, look for an ordinary
17619 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17621 /* We can get here when cp_parser_template_id, called by
17622 cp_parser_class_name with tag_type == none_type, succeeds
17623 and caches a BASELINK. Then, when called again here,
17624 instead of failing and returning an error_mark_node
17625 returns it (see template/typename17.C in C++11).
17626 ??? Could we diagnose this earlier? */
17627 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17629 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17630 type
= error_mark_node
;
17632 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17633 in effect, then we must assume that, upon instantiation, the
17634 template will correspond to a class. */
17635 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17636 && tag_type
== typename_type
)
17637 type
= make_typename_type (parser
->scope
, decl
,
17639 /*complain=*/tf_error
);
17640 /* If the `typename' keyword is in effect and DECL is not a type
17641 decl, then type is non existent. */
17642 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17644 else if (TREE_CODE (decl
) == TYPE_DECL
)
17646 type
= check_elaborated_type_specifier (tag_type
, decl
,
17647 /*allow_template_p=*/true);
17649 /* If the next token is a semicolon, this must be a specialization,
17650 instantiation, or friend declaration. Check the scope while we
17651 still know whether or not we had a nested-name-specifier. */
17652 if (type
!= error_mark_node
17653 && !nested_name_specifier
&& !is_friend
17654 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17655 check_unqualified_spec_or_inst (type
, token
->location
);
17657 else if (decl
== error_mark_node
)
17658 type
= error_mark_node
;
17663 token
= cp_lexer_peek_token (parser
->lexer
);
17664 identifier
= cp_parser_identifier (parser
);
17666 if (identifier
== error_mark_node
)
17668 parser
->scope
= NULL_TREE
;
17669 return error_mark_node
;
17672 /* For a `typename', we needn't call xref_tag. */
17673 if (tag_type
== typename_type
17674 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17675 return cp_parser_make_typename_type (parser
, identifier
,
17678 /* Template parameter lists apply only if we are not within a
17679 function parameter list. */
17680 bool template_parm_lists_apply
17681 = parser
->num_template_parameter_lists
;
17682 if (template_parm_lists_apply
)
17683 for (cp_binding_level
*s
= current_binding_level
;
17684 s
&& s
->kind
!= sk_template_parms
;
17685 s
= s
->level_chain
)
17686 if (s
->kind
== sk_function_parms
)
17687 template_parm_lists_apply
= false;
17689 /* Look up a qualified name in the usual way. */
17693 tree ambiguous_decls
;
17695 decl
= cp_parser_lookup_name (parser
, identifier
,
17697 /*is_template=*/false,
17698 /*is_namespace=*/false,
17699 /*check_dependency=*/true,
17703 /* If the lookup was ambiguous, an error will already have been
17705 if (ambiguous_decls
)
17706 return error_mark_node
;
17708 /* If we are parsing friend declaration, DECL may be a
17709 TEMPLATE_DECL tree node here. However, we need to check
17710 whether this TEMPLATE_DECL results in valid code. Consider
17711 the following example:
17714 template <class T> class C {};
17717 template <class T> friend class N::C; // #1, valid code
17719 template <class T> class Y {
17720 friend class N::C; // #2, invalid code
17723 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17724 name lookup of `N::C'. We see that friend declaration must
17725 be template for the code to be valid. Note that
17726 processing_template_decl does not work here since it is
17727 always 1 for the above two cases. */
17729 decl
= (cp_parser_maybe_treat_template_as_class
17730 (decl
, /*tag_name_p=*/is_friend
17731 && template_parm_lists_apply
));
17733 if (TREE_CODE (decl
) != TYPE_DECL
)
17735 cp_parser_diagnose_invalid_type_name (parser
,
17738 return error_mark_node
;
17741 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17743 bool allow_template
= (template_parm_lists_apply
17744 || DECL_SELF_REFERENCE_P (decl
));
17745 type
= check_elaborated_type_specifier (tag_type
, decl
,
17748 if (type
== error_mark_node
)
17749 return error_mark_node
;
17752 /* Forward declarations of nested types, such as
17757 are invalid unless all components preceding the final '::'
17758 are complete. If all enclosing types are complete, these
17759 declarations become merely pointless.
17761 Invalid forward declarations of nested types are errors
17762 caught elsewhere in parsing. Those that are pointless arrive
17765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17766 && !is_friend
&& !processing_explicit_instantiation
)
17767 warning (0, "declaration %qD does not declare anything", decl
);
17769 type
= TREE_TYPE (decl
);
17773 /* An elaborated-type-specifier sometimes introduces a new type and
17774 sometimes names an existing type. Normally, the rule is that it
17775 introduces a new type only if there is not an existing type of
17776 the same name already in scope. For example, given:
17779 void f() { struct S s; }
17781 the `struct S' in the body of `f' is the same `struct S' as in
17782 the global scope; the existing definition is used. However, if
17783 there were no global declaration, this would introduce a new
17784 local class named `S'.
17786 An exception to this rule applies to the following code:
17788 namespace N { struct S; }
17790 Here, the elaborated-type-specifier names a new type
17791 unconditionally; even if there is already an `S' in the
17792 containing scope this declaration names a new type.
17793 This exception only applies if the elaborated-type-specifier
17794 forms the complete declaration:
17798 A declaration consisting solely of `class-key identifier ;' is
17799 either a redeclaration of the name in the current scope or a
17800 forward declaration of the identifier as a class name. It
17801 introduces the name into the current scope.
17803 We are in this situation precisely when the next token is a `;'.
17805 An exception to the exception is that a `friend' declaration does
17806 *not* name a new type; i.e., given:
17808 struct S { friend struct T; };
17810 `T' is not a new type in the scope of `S'.
17812 Also, `new struct S' or `sizeof (struct S)' never results in the
17813 definition of a new type; a new type can only be declared in a
17814 declaration context. */
17820 /* Friends have special name lookup rules. */
17821 ts
= ts_within_enclosing_non_class
;
17822 else if (is_declaration
17823 && cp_lexer_next_token_is (parser
->lexer
,
17825 /* This is a `class-key identifier ;' */
17831 (template_parm_lists_apply
17832 && (cp_parser_next_token_starts_class_definition_p (parser
)
17833 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17834 /* An unqualified name was used to reference this type, so
17835 there were no qualifying templates. */
17836 if (template_parm_lists_apply
17837 && !cp_parser_check_template_parameters (parser
,
17838 /*num_templates=*/0,
17840 /*declarator=*/NULL
))
17841 return error_mark_node
;
17842 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17846 if (type
== error_mark_node
)
17847 return error_mark_node
;
17849 /* Allow attributes on forward declarations of classes. */
17852 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17853 warning (OPT_Wattributes
,
17854 "attributes ignored on uninstantiated type");
17855 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17856 && ! processing_explicit_instantiation
)
17857 warning (OPT_Wattributes
,
17858 "attributes ignored on template instantiation");
17859 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17860 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17862 warning (OPT_Wattributes
,
17863 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17866 if (tag_type
!= enum_type
)
17868 /* Indicate whether this class was declared as a `class' or as a
17870 if (CLASS_TYPE_P (type
))
17871 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17872 cp_parser_check_class_key (tag_type
, type
);
17875 /* A "<" cannot follow an elaborated type specifier. If that
17876 happens, the user was probably trying to form a template-id. */
17877 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17883 /* Parse an enum-specifier.
17886 enum-head { enumerator-list [opt] }
17887 enum-head { enumerator-list , } [C++0x]
17890 enum-key identifier [opt] enum-base [opt]
17891 enum-key nested-name-specifier identifier enum-base [opt]
17896 enum struct [C++0x]
17899 : type-specifier-seq
17901 opaque-enum-specifier:
17902 enum-key identifier enum-base [opt] ;
17905 enum-key attributes[opt] identifier [opt] enum-base [opt]
17906 { enumerator-list [opt] }attributes[opt]
17907 enum-key attributes[opt] identifier [opt] enum-base [opt]
17908 { enumerator-list, }attributes[opt] [C++0x]
17910 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17911 if the token stream isn't an enum-specifier after all. */
17914 cp_parser_enum_specifier (cp_parser
* parser
)
17917 tree type
= NULL_TREE
;
17919 tree nested_name_specifier
= NULL_TREE
;
17921 bool scoped_enum_p
= false;
17922 bool has_underlying_type
= false;
17923 bool nested_being_defined
= false;
17924 bool new_value_list
= false;
17925 bool is_new_type
= false;
17926 bool is_unnamed
= false;
17927 tree underlying_type
= NULL_TREE
;
17928 cp_token
*type_start_token
= NULL
;
17929 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17931 parser
->colon_corrects_to_scope_p
= false;
17933 /* Parse tentatively so that we can back up if we don't find a
17935 cp_parser_parse_tentatively (parser
);
17937 /* Caller guarantees that the current token is 'enum', an identifier
17938 possibly follows, and the token after that is an opening brace.
17939 If we don't have an identifier, fabricate an anonymous name for
17940 the enumeration being defined. */
17941 cp_lexer_consume_token (parser
->lexer
);
17943 /* Parse the "class" or "struct", which indicates a scoped
17944 enumeration type in C++0x. */
17945 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17946 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17948 if (cxx_dialect
< cxx11
)
17949 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17951 /* Consume the `struct' or `class' token. */
17952 cp_lexer_consume_token (parser
->lexer
);
17954 scoped_enum_p
= true;
17957 attributes
= cp_parser_attributes_opt (parser
);
17959 /* Clear the qualification. */
17960 parser
->scope
= NULL_TREE
;
17961 parser
->qualifying_scope
= NULL_TREE
;
17962 parser
->object_scope
= NULL_TREE
;
17964 /* Figure out in what scope the declaration is being placed. */
17965 prev_scope
= current_scope ();
17967 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17969 push_deferring_access_checks (dk_no_check
);
17970 nested_name_specifier
17971 = cp_parser_nested_name_specifier_opt (parser
,
17972 /*typename_keyword_p=*/true,
17973 /*check_dependency_p=*/false,
17975 /*is_declaration=*/false);
17977 if (nested_name_specifier
)
17981 identifier
= cp_parser_identifier (parser
);
17982 name
= cp_parser_lookup_name (parser
, identifier
,
17984 /*is_template=*/false,
17985 /*is_namespace=*/false,
17986 /*check_dependency=*/true,
17987 /*ambiguous_decls=*/NULL
,
17989 if (name
&& name
!= error_mark_node
)
17991 type
= TREE_TYPE (name
);
17992 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17994 /* Are template enums allowed in ISO? */
17995 if (template_parm_scope_p ())
17996 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17997 "%qD is an enumeration template", name
);
17998 /* ignore a typename reference, for it will be solved by name
18003 else if (nested_name_specifier
== error_mark_node
)
18004 /* We already issued an error. */;
18007 error_at (type_start_token
->location
,
18008 "%qD does not name an enumeration in %qT",
18009 identifier
, nested_name_specifier
);
18010 nested_name_specifier
= error_mark_node
;
18015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18016 identifier
= cp_parser_identifier (parser
);
18019 identifier
= make_anon_name ();
18022 error_at (type_start_token
->location
,
18023 "unnamed scoped enum is not allowed");
18026 pop_deferring_access_checks ();
18028 /* Check for the `:' that denotes a specified underlying type in C++0x.
18029 Note that a ':' could also indicate a bitfield width, however. */
18030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18032 cp_decl_specifier_seq type_specifiers
;
18034 /* Consume the `:'. */
18035 cp_lexer_consume_token (parser
->lexer
);
18037 /* Parse the type-specifier-seq. */
18038 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18039 /*is_trailing_return=*/false,
18042 /* At this point this is surely not elaborated type specifier. */
18043 if (!cp_parser_parse_definitely (parser
))
18046 if (cxx_dialect
< cxx11
)
18047 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18049 has_underlying_type
= true;
18051 /* If that didn't work, stop. */
18052 if (type_specifiers
.type
!= error_mark_node
)
18054 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18055 /*initialized=*/0, NULL
);
18056 if (underlying_type
== error_mark_node
18057 || check_for_bare_parameter_packs (underlying_type
))
18058 underlying_type
= NULL_TREE
;
18062 /* Look for the `{' but don't consume it yet. */
18063 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18065 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18067 cp_parser_error (parser
, "expected %<{%>");
18068 if (has_underlying_type
)
18074 /* An opaque-enum-specifier must have a ';' here. */
18075 if ((scoped_enum_p
|| underlying_type
)
18076 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18078 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18079 if (has_underlying_type
)
18087 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18090 if (nested_name_specifier
)
18092 if (CLASS_TYPE_P (nested_name_specifier
))
18094 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18095 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18096 push_scope (nested_name_specifier
);
18098 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18100 push_nested_namespace (nested_name_specifier
);
18104 /* Issue an error message if type-definitions are forbidden here. */
18105 if (!cp_parser_check_type_definition (parser
))
18106 type
= error_mark_node
;
18108 /* Create the new type. We do this before consuming the opening
18109 brace so the enum will be recorded as being on the line of its
18110 tag (or the 'enum' keyword, if there is no tag). */
18111 type
= start_enum (identifier
, type
, underlying_type
,
18112 attributes
, scoped_enum_p
, &is_new_type
);
18114 /* If the next token is not '{' it is an opaque-enum-specifier or an
18115 elaborated-type-specifier. */
18116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18118 timevar_push (TV_PARSE_ENUM
);
18119 if (nested_name_specifier
18120 && nested_name_specifier
!= error_mark_node
)
18122 /* The following catches invalid code such as:
18123 enum class S<int>::E { A, B, C }; */
18124 if (!processing_specialization
18125 && CLASS_TYPE_P (nested_name_specifier
)
18126 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18127 error_at (type_start_token
->location
, "cannot add an enumerator "
18128 "list to a template instantiation");
18130 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18132 error_at (type_start_token
->location
,
18133 "%<%T::%E%> has not been declared",
18134 TYPE_CONTEXT (nested_name_specifier
),
18135 nested_name_specifier
);
18136 type
= error_mark_node
;
18138 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18139 && !CLASS_TYPE_P (nested_name_specifier
))
18141 error_at (type_start_token
->location
, "nested name specifier "
18142 "%qT for enum declaration does not name a class "
18143 "or namespace", nested_name_specifier
);
18144 type
= error_mark_node
;
18146 /* If that scope does not contain the scope in which the
18147 class was originally declared, the program is invalid. */
18148 else if (prev_scope
&& !is_ancestor (prev_scope
,
18149 nested_name_specifier
))
18151 if (at_namespace_scope_p ())
18152 error_at (type_start_token
->location
,
18153 "declaration of %qD in namespace %qD which does not "
18155 type
, prev_scope
, nested_name_specifier
);
18157 error_at (type_start_token
->location
,
18158 "declaration of %qD in %qD which does not "
18160 type
, prev_scope
, nested_name_specifier
);
18161 type
= error_mark_node
;
18163 /* If that scope is the scope where the declaration is being placed
18164 the program is invalid. */
18165 else if (CLASS_TYPE_P (nested_name_specifier
)
18166 && CLASS_TYPE_P (prev_scope
)
18167 && same_type_p (nested_name_specifier
, prev_scope
))
18169 permerror (type_start_token
->location
,
18170 "extra qualification not allowed");
18171 nested_name_specifier
= NULL_TREE
;
18176 begin_scope (sk_scoped_enum
, type
);
18178 /* Consume the opening brace. */
18179 matching_braces braces
;
18180 braces
.consume_open (parser
);
18182 if (type
== error_mark_node
)
18183 ; /* Nothing to add */
18184 else if (OPAQUE_ENUM_P (type
)
18185 || (cxx_dialect
> cxx98
&& processing_specialization
))
18187 new_value_list
= true;
18188 SET_OPAQUE_ENUM_P (type
, false);
18189 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18193 error_at (type_start_token
->location
,
18194 "multiple definition of %q#T", type
);
18195 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18196 "previous definition here");
18197 type
= error_mark_node
;
18200 if (type
== error_mark_node
)
18201 cp_parser_skip_to_end_of_block_or_statement (parser
);
18202 /* If the next token is not '}', then there are some enumerators. */
18203 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18205 if (is_unnamed
&& !scoped_enum_p
)
18206 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18207 "ISO C++ forbids empty unnamed enum");
18210 cp_parser_enumerator_list (parser
, type
);
18212 /* Consume the final '}'. */
18213 braces
.require_close (parser
);
18217 timevar_pop (TV_PARSE_ENUM
);
18221 /* If a ';' follows, then it is an opaque-enum-specifier
18222 and additional restrictions apply. */
18223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18226 error_at (type_start_token
->location
,
18227 "opaque-enum-specifier without name");
18228 else if (nested_name_specifier
)
18229 error_at (type_start_token
->location
,
18230 "opaque-enum-specifier must use a simple identifier");
18234 /* Look for trailing attributes to apply to this enumeration, and
18235 apply them if appropriate. */
18236 if (cp_parser_allow_gnu_extensions_p (parser
))
18238 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18239 cplus_decl_attributes (&type
,
18241 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18244 /* Finish up the enumeration. */
18245 if (type
!= error_mark_node
)
18247 if (new_value_list
)
18248 finish_enum_value_list (type
);
18250 finish_enum (type
);
18253 if (nested_name_specifier
)
18255 if (CLASS_TYPE_P (nested_name_specifier
))
18257 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18258 pop_scope (nested_name_specifier
);
18260 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18262 pop_nested_namespace (nested_name_specifier
);
18266 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18270 /* Parse an enumerator-list. The enumerators all have the indicated
18274 enumerator-definition
18275 enumerator-list , enumerator-definition */
18278 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18282 /* Parse an enumerator-definition. */
18283 cp_parser_enumerator_definition (parser
, type
);
18285 /* If the next token is not a ',', we've reached the end of
18287 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18289 /* Otherwise, consume the `,' and keep going. */
18290 cp_lexer_consume_token (parser
->lexer
);
18291 /* If the next token is a `}', there is a trailing comma. */
18292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18294 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18295 pedwarn (input_location
, OPT_Wpedantic
,
18296 "comma at end of enumerator list");
18302 /* Parse an enumerator-definition. The enumerator has the indicated
18305 enumerator-definition:
18307 enumerator = constant-expression
18314 enumerator-definition:
18315 enumerator attributes [opt]
18316 enumerator attributes [opt] = constant-expression */
18319 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18325 /* Save the input location because we are interested in the location
18326 of the identifier and not the location of the explicit value. */
18327 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18329 /* Look for the identifier. */
18330 identifier
= cp_parser_identifier (parser
);
18331 if (identifier
== error_mark_node
)
18334 /* Parse any specified attributes. */
18335 tree attrs
= cp_parser_attributes_opt (parser
);
18337 /* If the next token is an '=', then there is an explicit value. */
18338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18340 /* Consume the `=' token. */
18341 cp_lexer_consume_token (parser
->lexer
);
18342 /* Parse the value. */
18343 value
= cp_parser_constant_expression (parser
);
18348 /* If we are processing a template, make sure the initializer of the
18349 enumerator doesn't contain any bare template parameter pack. */
18350 if (check_for_bare_parameter_packs (value
))
18351 value
= error_mark_node
;
18353 /* Create the enumerator. */
18354 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18357 /* Parse a namespace-name.
18360 original-namespace-name
18363 Returns the NAMESPACE_DECL for the namespace. */
18366 cp_parser_namespace_name (cp_parser
* parser
)
18369 tree namespace_decl
;
18371 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18373 /* Get the name of the namespace. */
18374 identifier
= cp_parser_identifier (parser
);
18375 if (identifier
== error_mark_node
)
18376 return error_mark_node
;
18378 /* Look up the identifier in the currently active scope. Look only
18379 for namespaces, due to:
18381 [basic.lookup.udir]
18383 When looking up a namespace-name in a using-directive or alias
18384 definition, only namespace names are considered.
18388 [basic.lookup.qual]
18390 During the lookup of a name preceding the :: scope resolution
18391 operator, object, function, and enumerator names are ignored.
18393 (Note that cp_parser_qualifying_entity only calls this
18394 function if the token after the name is the scope resolution
18396 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18398 /*is_template=*/false,
18399 /*is_namespace=*/true,
18400 /*check_dependency=*/true,
18401 /*ambiguous_decls=*/NULL
,
18403 /* If it's not a namespace, issue an error. */
18404 if (namespace_decl
== error_mark_node
18405 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18407 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18409 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18410 if (namespace_decl
== error_mark_node
18411 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18412 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18415 cp_parser_error (parser
, "expected namespace-name");
18416 namespace_decl
= error_mark_node
;
18419 return namespace_decl
;
18422 /* Parse a namespace-definition.
18424 namespace-definition:
18425 named-namespace-definition
18426 unnamed-namespace-definition
18428 named-namespace-definition:
18429 original-namespace-definition
18430 extension-namespace-definition
18432 original-namespace-definition:
18433 namespace identifier { namespace-body }
18435 extension-namespace-definition:
18436 namespace original-namespace-name { namespace-body }
18438 unnamed-namespace-definition:
18439 namespace { namespace-body } */
18442 cp_parser_namespace_definition (cp_parser
* parser
)
18445 int nested_definition_count
= 0;
18447 cp_ensure_no_omp_declare_simd (parser
);
18448 cp_ensure_no_oacc_routine (parser
);
18450 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18454 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18455 cp_lexer_consume_token (parser
->lexer
);
18458 /* Look for the `namespace' keyword. */
18460 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18462 /* Parse any specified attributes before the identifier. */
18463 tree attribs
= cp_parser_attributes_opt (parser
);
18467 identifier
= NULL_TREE
;
18469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18471 identifier
= cp_parser_identifier (parser
);
18473 /* Parse any attributes specified after the identifier. */
18474 attribs
= chainon (attribs
, cp_parser_attributes_opt (parser
));
18477 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18480 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18481 pedwarn (input_location
, OPT_Wpedantic
,
18482 "nested namespace definitions only available with "
18483 "-std=c++17 or -std=gnu++17");
18485 /* Nested namespace names can create new namespaces (unlike
18486 other qualified-ids). */
18487 if (int count
= identifier
? push_namespace (identifier
) : 0)
18488 nested_definition_count
+= count
;
18490 cp_parser_error (parser
, "nested namespace name required");
18491 cp_lexer_consume_token (parser
->lexer
);
18494 if (nested_definition_count
&& !identifier
)
18495 cp_parser_error (parser
, "namespace name required");
18497 if (nested_definition_count
&& attribs
)
18498 error_at (token
->location
,
18499 "a nested namespace definition cannot have attributes");
18500 if (nested_definition_count
&& is_inline
)
18501 error_at (token
->location
,
18502 "a nested namespace definition cannot be inline");
18504 /* Start the namespace. */
18505 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18507 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18509 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18511 /* Look for the `{' to validate starting the namespace. */
18512 matching_braces braces
;
18513 if (braces
.require_open (parser
))
18515 /* Parse the body of the namespace. */
18516 cp_parser_namespace_body (parser
);
18518 /* Look for the final `}'. */
18519 braces
.require_close (parser
);
18522 if (has_visibility
)
18523 pop_visibility (1);
18525 /* Pop the nested namespace definitions. */
18526 while (nested_definition_count
--)
18530 /* Parse a namespace-body.
18533 declaration-seq [opt] */
18536 cp_parser_namespace_body (cp_parser
* parser
)
18538 cp_parser_declaration_seq_opt (parser
);
18541 /* Parse a namespace-alias-definition.
18543 namespace-alias-definition:
18544 namespace identifier = qualified-namespace-specifier ; */
18547 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18550 tree namespace_specifier
;
18552 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18554 /* Look for the `namespace' keyword. */
18555 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18556 /* Look for the identifier. */
18557 identifier
= cp_parser_identifier (parser
);
18558 if (identifier
== error_mark_node
)
18560 /* Look for the `=' token. */
18561 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18562 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18564 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18565 /* Skip the definition. */
18566 cp_lexer_consume_token (parser
->lexer
);
18567 if (cp_parser_skip_to_closing_brace (parser
))
18568 cp_lexer_consume_token (parser
->lexer
);
18571 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18572 /* Look for the qualified-namespace-specifier. */
18573 namespace_specifier
18574 = cp_parser_qualified_namespace_specifier (parser
);
18575 /* Look for the `;' token. */
18576 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18578 /* Register the alias in the symbol table. */
18579 do_namespace_alias (identifier
, namespace_specifier
);
18582 /* Parse a qualified-namespace-specifier.
18584 qualified-namespace-specifier:
18585 :: [opt] nested-name-specifier [opt] namespace-name
18587 Returns a NAMESPACE_DECL corresponding to the specified
18591 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18593 /* Look for the optional `::'. */
18594 cp_parser_global_scope_opt (parser
,
18595 /*current_scope_valid_p=*/false);
18597 /* Look for the optional nested-name-specifier. */
18598 cp_parser_nested_name_specifier_opt (parser
,
18599 /*typename_keyword_p=*/false,
18600 /*check_dependency_p=*/true,
18602 /*is_declaration=*/true);
18604 return cp_parser_namespace_name (parser
);
18607 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18608 access declaration.
18611 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18612 using :: unqualified-id ;
18614 access-declaration:
18620 cp_parser_using_declaration (cp_parser
* parser
,
18621 bool access_declaration_p
)
18624 bool typename_p
= false;
18625 bool global_scope_p
;
18629 int oldcount
= errorcount
;
18630 cp_token
*diag_token
= NULL
;
18632 if (access_declaration_p
)
18634 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18635 cp_parser_parse_tentatively (parser
);
18639 /* Look for the `using' keyword. */
18640 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18643 /* Peek at the next token. */
18644 token
= cp_lexer_peek_token (parser
->lexer
);
18645 /* See if it's `typename'. */
18646 if (token
->keyword
== RID_TYPENAME
)
18648 /* Remember that we've seen it. */
18650 /* Consume the `typename' token. */
18651 cp_lexer_consume_token (parser
->lexer
);
18655 /* Look for the optional global scope qualification. */
18657 = (cp_parser_global_scope_opt (parser
,
18658 /*current_scope_valid_p=*/false)
18661 /* If we saw `typename', or didn't see `::', then there must be a
18662 nested-name-specifier present. */
18663 if (typename_p
|| !global_scope_p
)
18665 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18666 /*check_dependency_p=*/true,
18668 /*is_declaration=*/true);
18669 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18671 cp_parser_skip_to_end_of_block_or_statement (parser
);
18675 /* Otherwise, we could be in either of the two productions. In that
18676 case, treat the nested-name-specifier as optional. */
18678 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18679 /*typename_keyword_p=*/false,
18680 /*check_dependency_p=*/true,
18682 /*is_declaration=*/true);
18684 qscope
= global_namespace
;
18685 else if (UNSCOPED_ENUM_P (qscope
))
18686 qscope
= CP_TYPE_CONTEXT (qscope
);
18688 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18689 /* Something has already gone wrong; there's no need to parse
18690 further. Since an error has occurred, the return value of
18691 cp_parser_parse_definitely will be false, as required. */
18692 return cp_parser_parse_definitely (parser
);
18694 token
= cp_lexer_peek_token (parser
->lexer
);
18695 /* Parse the unqualified-id. */
18696 identifier
= cp_parser_unqualified_id (parser
,
18697 /*template_keyword_p=*/false,
18698 /*check_dependency_p=*/true,
18699 /*declarator_p=*/true,
18700 /*optional_p=*/false);
18702 if (access_declaration_p
)
18704 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18705 cp_parser_simulate_error (parser
);
18706 if (!cp_parser_parse_definitely (parser
))
18709 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18711 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18712 if (cxx_dialect
< cxx17
18713 && !in_system_header_at (ell
->location
))
18714 pedwarn (ell
->location
, 0,
18715 "pack expansion in using-declaration only available "
18716 "with -std=c++17 or -std=gnu++17");
18717 qscope
= make_pack_expansion (qscope
);
18720 /* The function we call to handle a using-declaration is different
18721 depending on what scope we are in. */
18722 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18724 else if (!identifier_p (identifier
)
18725 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18726 /* [namespace.udecl]
18728 A using declaration shall not name a template-id. */
18729 error_at (token
->location
,
18730 "a template-id may not appear in a using-declaration");
18733 if (at_class_scope_p ())
18735 /* Create the USING_DECL. */
18736 decl
= do_class_using_decl (qscope
, identifier
);
18738 if (decl
&& typename_p
)
18739 USING_DECL_TYPENAME_P (decl
) = 1;
18741 if (check_for_bare_parameter_packs (decl
))
18743 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18747 /* Add it to the list of members in this class. */
18748 finish_member_declaration (decl
);
18752 decl
= cp_parser_lookup_name_simple (parser
,
18755 if (decl
== error_mark_node
)
18756 cp_parser_name_lookup_error (parser
, identifier
,
18759 else if (check_for_bare_parameter_packs (decl
))
18761 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18764 else if (!at_namespace_scope_p ())
18765 finish_local_using_decl (decl
, qscope
, identifier
);
18767 finish_namespace_using_decl (decl
, qscope
, identifier
);
18771 if (!access_declaration_p
18772 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18774 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18775 if (cxx_dialect
< cxx17
)
18776 pedwarn (comma
->location
, 0,
18777 "comma-separated list in using-declaration only available "
18778 "with -std=c++17 or -std=gnu++17");
18782 /* Look for the final `;'. */
18783 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18785 if (access_declaration_p
&& errorcount
== oldcount
)
18786 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18787 "access declarations are deprecated "
18788 "in favour of using-declarations; "
18789 "suggestion: add the %<using%> keyword");
18794 /* Parse an alias-declaration.
18797 using identifier attribute-specifier-seq [opt] = type-id */
18800 cp_parser_alias_declaration (cp_parser
* parser
)
18802 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18803 location_t id_location
;
18804 cp_declarator
*declarator
;
18805 cp_decl_specifier_seq decl_specs
;
18807 const char *saved_message
= NULL
;
18809 /* Look for the `using' keyword. */
18810 cp_token
*using_token
18811 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18812 if (using_token
== NULL
)
18813 return error_mark_node
;
18815 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18816 id
= cp_parser_identifier (parser
);
18817 if (id
== error_mark_node
)
18818 return error_mark_node
;
18820 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18821 attributes
= cp_parser_attributes_opt (parser
);
18822 if (attributes
== error_mark_node
)
18823 return error_mark_node
;
18825 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18827 if (cp_parser_error_occurred (parser
))
18828 return error_mark_node
;
18830 cp_parser_commit_to_tentative_parse (parser
);
18832 /* Now we are going to parse the type-id of the declaration. */
18837 "A type-specifier-seq shall not define a class or enumeration
18838 unless it appears in the type-id of an alias-declaration (7.1.3) that
18839 is not the declaration of a template-declaration."
18841 In other words, if we currently are in an alias template, the
18842 type-id should not define a type.
18844 So let's set parser->type_definition_forbidden_message in that
18845 case; cp_parser_check_type_definition (called by
18846 cp_parser_class_specifier) will then emit an error if a type is
18847 defined in the type-id. */
18848 if (parser
->num_template_parameter_lists
)
18850 saved_message
= parser
->type_definition_forbidden_message
;
18851 parser
->type_definition_forbidden_message
=
18852 G_("types may not be defined in alias template declarations");
18855 type
= cp_parser_type_id (parser
);
18857 /* Restore the error message if need be. */
18858 if (parser
->num_template_parameter_lists
)
18859 parser
->type_definition_forbidden_message
= saved_message
;
18861 if (type
== error_mark_node
18862 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18864 cp_parser_skip_to_end_of_block_or_statement (parser
);
18865 return error_mark_node
;
18868 /* A typedef-name can also be introduced by an alias-declaration. The
18869 identifier following the using keyword becomes a typedef-name. It has
18870 the same semantics as if it were introduced by the typedef
18871 specifier. In particular, it does not define a new type and it shall
18872 not appear in the type-id. */
18874 clear_decl_specs (&decl_specs
);
18875 decl_specs
.type
= type
;
18876 if (attributes
!= NULL_TREE
)
18878 decl_specs
.attributes
= attributes
;
18879 set_and_check_decl_spec_loc (&decl_specs
,
18883 set_and_check_decl_spec_loc (&decl_specs
,
18886 set_and_check_decl_spec_loc (&decl_specs
,
18890 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18891 declarator
->id_loc
= id_location
;
18893 member_p
= at_class_scope_p ();
18895 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18896 NULL_TREE
, attributes
);
18898 decl
= start_decl (declarator
, &decl_specs
, 0,
18899 attributes
, NULL_TREE
, &pushed_scope
);
18900 if (decl
== error_mark_node
)
18903 // Attach constraints to the alias declaration.
18904 if (flag_concepts
&& current_template_parms
)
18906 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18907 tree constr
= build_constraints (reqs
, NULL_TREE
);
18908 set_constraints (decl
, constr
);
18911 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18914 pop_scope (pushed_scope
);
18916 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18917 added into the symbol table; otherwise, return the TYPE_DECL. */
18918 if (DECL_LANG_SPECIFIC (decl
)
18919 && DECL_TEMPLATE_INFO (decl
)
18920 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18922 decl
= DECL_TI_TEMPLATE (decl
);
18924 check_member_template (decl
);
18930 /* Parse a using-directive.
18933 using namespace :: [opt] nested-name-specifier [opt]
18934 namespace-name ; */
18937 cp_parser_using_directive (cp_parser
* parser
)
18939 tree namespace_decl
;
18942 /* Look for the `using' keyword. */
18943 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18944 /* And the `namespace' keyword. */
18945 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18946 /* Look for the optional `::' operator. */
18947 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18948 /* And the optional nested-name-specifier. */
18949 cp_parser_nested_name_specifier_opt (parser
,
18950 /*typename_keyword_p=*/false,
18951 /*check_dependency_p=*/true,
18953 /*is_declaration=*/true);
18954 /* Get the namespace being used. */
18955 namespace_decl
= cp_parser_namespace_name (parser
);
18956 /* And any specified attributes. */
18957 attribs
= cp_parser_attributes_opt (parser
);
18959 /* Update the symbol table. */
18960 if (namespace_bindings_p ())
18961 finish_namespace_using_directive (namespace_decl
, attribs
);
18963 finish_local_using_directive (namespace_decl
, attribs
);
18965 /* Look for the final `;'. */
18966 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18969 /* Parse an asm-definition.
18972 asm ( string-literal ) ;
18977 asm volatile [opt] ( string-literal ) ;
18978 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18979 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18980 : asm-operand-list [opt] ) ;
18981 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18982 : asm-operand-list [opt]
18983 : asm-clobber-list [opt] ) ;
18984 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18985 : asm-clobber-list [opt]
18986 : asm-goto-list ) ; */
18989 cp_parser_asm_definition (cp_parser
* parser
)
18992 tree outputs
= NULL_TREE
;
18993 tree inputs
= NULL_TREE
;
18994 tree clobbers
= NULL_TREE
;
18995 tree labels
= NULL_TREE
;
18997 bool volatile_p
= false;
18998 bool extended_p
= false;
18999 bool invalid_inputs_p
= false;
19000 bool invalid_outputs_p
= false;
19001 bool goto_p
= false;
19002 required_token missing
= RT_NONE
;
19004 /* Look for the `asm' keyword. */
19005 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19007 if (parser
->in_function_body
19008 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19010 error ("%<asm%> in %<constexpr%> function");
19011 cp_function_chain
->invalid_constexpr
= true;
19014 /* See if the next token is `volatile'. */
19015 if (cp_parser_allow_gnu_extensions_p (parser
)
19016 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19018 /* Remember that we saw the `volatile' keyword. */
19020 /* Consume the token. */
19021 cp_lexer_consume_token (parser
->lexer
);
19023 if (cp_parser_allow_gnu_extensions_p (parser
)
19024 && parser
->in_function_body
19025 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19027 /* Remember that we saw the `goto' keyword. */
19029 /* Consume the token. */
19030 cp_lexer_consume_token (parser
->lexer
);
19032 /* Look for the opening `('. */
19033 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19035 /* Look for the string. */
19036 string
= cp_parser_string_literal (parser
, false, false);
19037 if (string
== error_mark_node
)
19039 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19040 /*consume_paren=*/true);
19044 /* If we're allowing GNU extensions, check for the extended assembly
19045 syntax. Unfortunately, the `:' tokens need not be separated by
19046 a space in C, and so, for compatibility, we tolerate that here
19047 too. Doing that means that we have to treat the `::' operator as
19049 if (cp_parser_allow_gnu_extensions_p (parser
)
19050 && parser
->in_function_body
19051 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19052 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19054 bool inputs_p
= false;
19055 bool clobbers_p
= false;
19056 bool labels_p
= false;
19058 /* The extended syntax was used. */
19061 /* Look for outputs. */
19062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19064 /* Consume the `:'. */
19065 cp_lexer_consume_token (parser
->lexer
);
19066 /* Parse the output-operands. */
19067 if (cp_lexer_next_token_is_not (parser
->lexer
,
19069 && cp_lexer_next_token_is_not (parser
->lexer
,
19071 && cp_lexer_next_token_is_not (parser
->lexer
,
19075 outputs
= cp_parser_asm_operand_list (parser
);
19076 if (outputs
== error_mark_node
)
19077 invalid_outputs_p
= true;
19080 /* If the next token is `::', there are no outputs, and the
19081 next token is the beginning of the inputs. */
19082 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19083 /* The inputs are coming next. */
19086 /* Look for inputs. */
19088 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19090 /* Consume the `:' or `::'. */
19091 cp_lexer_consume_token (parser
->lexer
);
19092 /* Parse the output-operands. */
19093 if (cp_lexer_next_token_is_not (parser
->lexer
,
19095 && cp_lexer_next_token_is_not (parser
->lexer
,
19097 && cp_lexer_next_token_is_not (parser
->lexer
,
19100 inputs
= cp_parser_asm_operand_list (parser
);
19101 if (inputs
== error_mark_node
)
19102 invalid_inputs_p
= true;
19105 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19106 /* The clobbers are coming next. */
19109 /* Look for clobbers. */
19111 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19114 /* Consume the `:' or `::'. */
19115 cp_lexer_consume_token (parser
->lexer
);
19116 /* Parse the clobbers. */
19117 if (cp_lexer_next_token_is_not (parser
->lexer
,
19119 && cp_lexer_next_token_is_not (parser
->lexer
,
19121 clobbers
= cp_parser_asm_clobber_list (parser
);
19124 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19125 /* The labels are coming next. */
19128 /* Look for labels. */
19130 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19133 /* Consume the `:' or `::'. */
19134 cp_lexer_consume_token (parser
->lexer
);
19135 /* Parse the labels. */
19136 labels
= cp_parser_asm_label_list (parser
);
19139 if (goto_p
&& !labels_p
)
19140 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19143 missing
= RT_COLON_SCOPE
;
19145 /* Look for the closing `)'. */
19146 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19147 missing
? missing
: RT_CLOSE_PAREN
))
19148 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19149 /*consume_paren=*/true);
19150 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19152 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19154 /* Create the ASM_EXPR. */
19155 if (parser
->in_function_body
)
19157 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19158 inputs
, clobbers
, labels
);
19159 /* If the extended syntax was not used, mark the ASM_EXPR. */
19162 tree temp
= asm_stmt
;
19163 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19164 temp
= TREE_OPERAND (temp
, 0);
19166 ASM_INPUT_P (temp
) = 1;
19170 symtab
->finalize_toplevel_asm (string
);
19174 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19175 type that comes from the decl-specifier-seq. */
19178 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19180 for (cp_declarator
*d
= declarator
; d
;)
19190 if (TYPE_PTRMEMFUNC_P (type
))
19191 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19192 type
= TREE_TYPE (type
);
19200 /* Declarators [gram.dcl.decl] */
19202 /* Parse an init-declarator.
19205 declarator initializer [opt]
19210 declarator asm-specification [opt] attributes [opt] initializer [opt]
19212 function-definition:
19213 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19215 decl-specifier-seq [opt] declarator function-try-block
19219 function-definition:
19220 __extension__ function-definition
19224 function-definition:
19225 decl-specifier-seq [opt] declarator function-transaction-block
19227 The DECL_SPECIFIERS apply to this declarator. Returns a
19228 representation of the entity declared. If MEMBER_P is TRUE, then
19229 this declarator appears in a class scope. The new DECL created by
19230 this declarator is returned.
19232 The CHECKS are access checks that should be performed once we know
19233 what entity is being declared (and, therefore, what classes have
19236 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19237 for a function-definition here as well. If the declarator is a
19238 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19239 be TRUE upon return. By that point, the function-definition will
19240 have been completely parsed.
19242 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19245 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19246 parsed declaration if it is an uninitialized single declarator not followed
19247 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19248 if present, will not be consumed. If returned, this declarator will be
19249 created with SD_INITIALIZED but will not call cp_finish_decl.
19251 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19252 and there is an initializer, the pointed location_t is set to the
19253 location of the '=' or `(', or '{' in C++11 token introducing the
19257 cp_parser_init_declarator (cp_parser
* parser
,
19258 cp_decl_specifier_seq
*decl_specifiers
,
19259 vec
<deferred_access_check
, va_gc
> *checks
,
19260 bool function_definition_allowed_p
,
19262 int declares_class_or_enum
,
19263 bool* function_definition_p
,
19264 tree
* maybe_range_for_decl
,
19265 location_t
* init_loc
,
19268 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19269 *attributes_start_token
= NULL
;
19270 cp_declarator
*declarator
;
19271 tree prefix_attributes
;
19272 tree attributes
= NULL
;
19273 tree asm_specification
;
19275 tree decl
= NULL_TREE
;
19277 int is_initialized
;
19278 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19279 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19281 enum cpp_ttype initialization_kind
;
19282 bool is_direct_init
= false;
19283 bool is_non_constant_init
;
19284 int ctor_dtor_or_conv_p
;
19285 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19286 tree pushed_scope
= NULL_TREE
;
19287 bool range_for_decl_p
= false;
19288 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19289 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19291 /* Gather the attributes that were provided with the
19292 decl-specifiers. */
19293 prefix_attributes
= decl_specifiers
->attributes
;
19295 /* Assume that this is not the declarator for a function
19297 if (function_definition_p
)
19298 *function_definition_p
= false;
19300 /* Default arguments are only permitted for function parameters. */
19301 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19302 parser
->default_arg_ok_p
= false;
19304 /* Defer access checks while parsing the declarator; we cannot know
19305 what names are accessible until we know what is being
19307 resume_deferring_access_checks ();
19309 token
= cp_lexer_peek_token (parser
->lexer
);
19311 /* Parse the declarator. */
19313 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19314 &ctor_dtor_or_conv_p
,
19315 /*parenthesized_p=*/NULL
,
19316 member_p
, friend_p
);
19317 /* Gather up the deferred checks. */
19318 stop_deferring_access_checks ();
19320 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19322 /* If the DECLARATOR was erroneous, there's no need to go
19324 if (declarator
== cp_error_declarator
)
19325 return error_mark_node
;
19327 /* Check that the number of template-parameter-lists is OK. */
19328 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19330 return error_mark_node
;
19332 if (declares_class_or_enum
& 2)
19333 cp_parser_check_for_definition_in_return_type (declarator
,
19334 decl_specifiers
->type
,
19335 decl_specifiers
->locations
[ds_type_spec
]);
19337 /* Figure out what scope the entity declared by the DECLARATOR is
19338 located in. `grokdeclarator' sometimes changes the scope, so
19339 we compute it now. */
19340 scope
= get_scope_of_declarator (declarator
);
19342 /* Perform any lookups in the declared type which were thought to be
19343 dependent, but are not in the scope of the declarator. */
19344 decl_specifiers
->type
19345 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19347 /* If we're allowing GNU extensions, look for an
19348 asm-specification. */
19349 if (cp_parser_allow_gnu_extensions_p (parser
))
19351 /* Look for an asm-specification. */
19352 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19353 asm_specification
= cp_parser_asm_specification_opt (parser
);
19356 asm_specification
= NULL_TREE
;
19358 /* Look for attributes. */
19359 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19360 attributes
= cp_parser_attributes_opt (parser
);
19362 /* Peek at the next token. */
19363 token
= cp_lexer_peek_token (parser
->lexer
);
19365 bool bogus_implicit_tmpl
= false;
19367 if (function_declarator_p (declarator
))
19369 /* Handle C++17 deduction guides. */
19370 if (!decl_specifiers
->type
19371 && ctor_dtor_or_conv_p
<= 0
19372 && cxx_dialect
>= cxx17
)
19374 cp_declarator
*id
= get_id_declarator (declarator
);
19375 tree name
= id
->u
.id
.unqualified_name
;
19376 parser
->scope
= id
->u
.id
.qualifying_scope
;
19377 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19379 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19380 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19382 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19383 id
->u
.id
.sfk
= sfk_deduction_guide
;
19384 ctor_dtor_or_conv_p
= 1;
19388 /* Check to see if the token indicates the start of a
19389 function-definition. */
19390 if (cp_parser_token_starts_function_definition_p (token
))
19392 if (!function_definition_allowed_p
)
19394 /* If a function-definition should not appear here, issue an
19396 cp_parser_error (parser
,
19397 "a function-definition is not allowed here");
19398 return error_mark_node
;
19401 location_t func_brace_location
19402 = cp_lexer_peek_token (parser
->lexer
)->location
;
19404 /* Neither attributes nor an asm-specification are allowed
19405 on a function-definition. */
19406 if (asm_specification
)
19407 error_at (asm_spec_start_token
->location
,
19408 "an asm-specification is not allowed "
19409 "on a function-definition");
19411 error_at (attributes_start_token
->location
,
19412 "attributes are not allowed "
19413 "on a function-definition");
19414 /* This is a function-definition. */
19415 *function_definition_p
= true;
19417 /* Parse the function definition. */
19419 decl
= cp_parser_save_member_function_body (parser
,
19422 prefix_attributes
);
19425 (cp_parser_function_definition_from_specifiers_and_declarator
19426 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19428 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19430 /* This is where the prologue starts... */
19431 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19432 = func_brace_location
;
19438 else if (parser
->fully_implicit_function_template_p
)
19440 /* A non-template declaration involving a function parameter list
19441 containing an implicit template parameter will be made into a
19442 template. If the resulting declaration is not going to be an
19443 actual function then finish the template scope here to prevent it.
19444 An error message will be issued once we have a decl to talk about.
19446 FIXME probably we should do type deduction rather than create an
19447 implicit template, but the standard currently doesn't allow it. */
19448 bogus_implicit_tmpl
= true;
19449 finish_fully_implicit_template (parser
, NULL_TREE
);
19454 Only in function declarations for constructors, destructors, type
19455 conversions, and deduction guides can the decl-specifier-seq be omitted.
19457 We explicitly postpone this check past the point where we handle
19458 function-definitions because we tolerate function-definitions
19459 that are missing their return types in some modes. */
19460 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19462 cp_parser_error (parser
,
19463 "expected constructor, destructor, or type conversion");
19464 return error_mark_node
;
19467 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19468 if (token
->type
== CPP_EQ
19469 || token
->type
== CPP_OPEN_PAREN
19470 || token
->type
== CPP_OPEN_BRACE
)
19472 is_initialized
= SD_INITIALIZED
;
19473 initialization_kind
= token
->type
;
19474 if (maybe_range_for_decl
)
19475 *maybe_range_for_decl
= error_mark_node
;
19476 tmp_init_loc
= token
->location
;
19477 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19478 *init_loc
= tmp_init_loc
;
19480 if (token
->type
== CPP_EQ
19481 && function_declarator_p (declarator
))
19483 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19484 if (t2
->keyword
== RID_DEFAULT
)
19485 is_initialized
= SD_DEFAULTED
;
19486 else if (t2
->keyword
== RID_DELETE
)
19487 is_initialized
= SD_DELETED
;
19492 /* If the init-declarator isn't initialized and isn't followed by a
19493 `,' or `;', it's not a valid init-declarator. */
19494 if (token
->type
!= CPP_COMMA
19495 && token
->type
!= CPP_SEMICOLON
)
19497 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19498 range_for_decl_p
= true;
19501 if (!maybe_range_for_decl
)
19502 cp_parser_error (parser
, "expected initializer");
19503 return error_mark_node
;
19506 is_initialized
= SD_UNINITIALIZED
;
19507 initialization_kind
= CPP_EOF
;
19510 /* Because start_decl has side-effects, we should only call it if we
19511 know we're going ahead. By this point, we know that we cannot
19512 possibly be looking at any other construct. */
19513 cp_parser_commit_to_tentative_parse (parser
);
19515 /* Enter the newly declared entry in the symbol table. If we're
19516 processing a declaration in a class-specifier, we wait until
19517 after processing the initializer. */
19520 if (parser
->in_unbraced_linkage_specification_p
)
19521 decl_specifiers
->storage_class
= sc_extern
;
19522 decl
= start_decl (declarator
, decl_specifiers
,
19523 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19524 attributes
, prefix_attributes
, &pushed_scope
);
19525 cp_finalize_omp_declare_simd (parser
, decl
);
19526 cp_finalize_oacc_routine (parser
, decl
, false);
19527 /* Adjust location of decl if declarator->id_loc is more appropriate:
19528 set, and decl wasn't merged with another decl, in which case its
19529 location would be different from input_location, and more accurate. */
19531 && declarator
->id_loc
!= UNKNOWN_LOCATION
19532 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19533 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19536 /* Enter the SCOPE. That way unqualified names appearing in the
19537 initializer will be looked up in SCOPE. */
19538 pushed_scope
= push_scope (scope
);
19540 /* Perform deferred access control checks, now that we know in which
19541 SCOPE the declared entity resides. */
19542 if (!member_p
&& decl
)
19544 tree saved_current_function_decl
= NULL_TREE
;
19546 /* If the entity being declared is a function, pretend that we
19547 are in its scope. If it is a `friend', it may have access to
19548 things that would not otherwise be accessible. */
19549 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19551 saved_current_function_decl
= current_function_decl
;
19552 current_function_decl
= decl
;
19555 /* Perform access checks for template parameters. */
19556 cp_parser_perform_template_parameter_access_checks (checks
);
19558 /* Perform the access control checks for the declarator and the
19559 decl-specifiers. */
19560 perform_deferred_access_checks (tf_warning_or_error
);
19562 /* Restore the saved value. */
19563 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19564 current_function_decl
= saved_current_function_decl
;
19567 /* Parse the initializer. */
19568 initializer
= NULL_TREE
;
19569 is_direct_init
= false;
19570 is_non_constant_init
= true;
19571 if (is_initialized
)
19573 if (function_declarator_p (declarator
))
19575 if (initialization_kind
== CPP_EQ
)
19576 initializer
= cp_parser_pure_specifier (parser
);
19579 /* If the declaration was erroneous, we don't really
19580 know what the user intended, so just silently
19581 consume the initializer. */
19582 if (decl
!= error_mark_node
)
19583 error_at (tmp_init_loc
, "initializer provided for function");
19584 cp_parser_skip_to_closing_parenthesis (parser
,
19585 /*recovering=*/true,
19586 /*or_comma=*/false,
19587 /*consume_paren=*/true);
19592 /* We want to record the extra mangling scope for in-class
19593 initializers of class members and initializers of static data
19594 member templates. The former involves deferring
19595 parsing of the initializer until end of class as with default
19596 arguments. So right here we only handle the latter. */
19597 if (!member_p
&& processing_template_decl
)
19598 start_lambda_scope (decl
);
19599 initializer
= cp_parser_initializer (parser
,
19601 &is_non_constant_init
);
19602 if (!member_p
&& processing_template_decl
)
19603 finish_lambda_scope ();
19604 if (initializer
== error_mark_node
)
19605 cp_parser_skip_to_end_of_statement (parser
);
19609 /* The old parser allows attributes to appear after a parenthesized
19610 initializer. Mark Mitchell proposed removing this functionality
19611 on the GCC mailing lists on 2002-08-13. This parser accepts the
19612 attributes -- but ignores them. */
19613 if (cp_parser_allow_gnu_extensions_p (parser
)
19614 && initialization_kind
== CPP_OPEN_PAREN
)
19615 if (cp_parser_attributes_opt (parser
))
19616 warning (OPT_Wattributes
,
19617 "attributes after parenthesized initializer ignored");
19619 /* And now complain about a non-function implicit template. */
19620 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19621 error_at (DECL_SOURCE_LOCATION (decl
),
19622 "non-function %qD declared as implicit template", decl
);
19624 /* For an in-class declaration, use `grokfield' to create the
19630 pop_scope (pushed_scope
);
19631 pushed_scope
= NULL_TREE
;
19633 decl
= grokfield (declarator
, decl_specifiers
,
19634 initializer
, !is_non_constant_init
,
19635 /*asmspec=*/NULL_TREE
,
19636 chainon (attributes
, prefix_attributes
));
19637 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19638 cp_parser_save_default_args (parser
, decl
);
19639 cp_finalize_omp_declare_simd (parser
, decl
);
19640 cp_finalize_oacc_routine (parser
, decl
, false);
19643 /* Finish processing the declaration. But, skip member
19645 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19647 cp_finish_decl (decl
,
19648 initializer
, !is_non_constant_init
,
19650 /* If the initializer is in parentheses, then this is
19651 a direct-initialization, which means that an
19652 `explicit' constructor is OK. Otherwise, an
19653 `explicit' constructor cannot be used. */
19654 ((is_direct_init
|| !is_initialized
)
19655 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19657 else if ((cxx_dialect
!= cxx98
) && friend_p
19658 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19659 /* Core issue #226 (C++0x only): A default template-argument
19660 shall not be specified in a friend class template
19662 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19663 /*is_partial=*/false, /*is_friend_decl=*/1);
19665 if (!friend_p
&& pushed_scope
)
19666 pop_scope (pushed_scope
);
19668 if (function_declarator_p (declarator
)
19669 && parser
->fully_implicit_function_template_p
)
19672 decl
= finish_fully_implicit_template (parser
, decl
);
19674 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19677 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19678 && type_uses_auto (decl_specifiers
->type
))
19679 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19684 /* Parse a declarator.
19688 ptr-operator declarator
19690 abstract-declarator:
19691 ptr-operator abstract-declarator [opt]
19692 direct-abstract-declarator
19697 attributes [opt] direct-declarator
19698 attributes [opt] ptr-operator declarator
19700 abstract-declarator:
19701 attributes [opt] ptr-operator abstract-declarator [opt]
19702 attributes [opt] direct-abstract-declarator
19704 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19705 detect constructors, destructors, deduction guides, or conversion operators.
19706 It is set to -1 if the declarator is a name, and +1 if it is a
19707 function. Otherwise it is set to zero. Usually you just want to
19708 test for >0, but internally the negative value is used.
19710 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19711 a decl-specifier-seq unless it declares a constructor, destructor,
19712 or conversion. It might seem that we could check this condition in
19713 semantic analysis, rather than parsing, but that makes it difficult
19714 to handle something like `f()'. We want to notice that there are
19715 no decl-specifiers, and therefore realize that this is an
19716 expression, not a declaration.)
19718 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19719 the declarator is a direct-declarator of the form "(...)".
19721 MEMBER_P is true iff this declarator is a member-declarator.
19723 FRIEND_P is true iff this declarator is a friend. */
19725 static cp_declarator
*
19726 cp_parser_declarator (cp_parser
* parser
,
19727 cp_parser_declarator_kind dcl_kind
,
19728 int* ctor_dtor_or_conv_p
,
19729 bool* parenthesized_p
,
19730 bool member_p
, bool friend_p
)
19732 cp_declarator
*declarator
;
19733 enum tree_code code
;
19734 cp_cv_quals cv_quals
;
19736 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19738 /* Assume this is not a constructor, destructor, or type-conversion
19740 if (ctor_dtor_or_conv_p
)
19741 *ctor_dtor_or_conv_p
= 0;
19743 if (cp_parser_allow_gnu_extensions_p (parser
))
19744 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19746 /* Check for the ptr-operator production. */
19747 cp_parser_parse_tentatively (parser
);
19748 /* Parse the ptr-operator. */
19749 code
= cp_parser_ptr_operator (parser
,
19754 /* If that worked, then we have a ptr-operator. */
19755 if (cp_parser_parse_definitely (parser
))
19757 /* If a ptr-operator was found, then this declarator was not
19759 if (parenthesized_p
)
19760 *parenthesized_p
= true;
19761 /* The dependent declarator is optional if we are parsing an
19762 abstract-declarator. */
19763 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19764 cp_parser_parse_tentatively (parser
);
19766 /* Parse the dependent declarator. */
19767 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19768 /*ctor_dtor_or_conv_p=*/NULL
,
19769 /*parenthesized_p=*/NULL
,
19770 /*member_p=*/false,
19773 /* If we are parsing an abstract-declarator, we must handle the
19774 case where the dependent declarator is absent. */
19775 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19776 && !cp_parser_parse_definitely (parser
))
19779 declarator
= cp_parser_make_indirect_declarator
19780 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19782 /* Everything else is a direct-declarator. */
19785 if (parenthesized_p
)
19786 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19788 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19789 ctor_dtor_or_conv_p
,
19790 member_p
, friend_p
);
19793 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19794 declarator
->attributes
= gnu_attributes
;
19798 /* Parse a direct-declarator or direct-abstract-declarator.
19802 direct-declarator ( parameter-declaration-clause )
19803 cv-qualifier-seq [opt]
19804 ref-qualifier [opt]
19805 exception-specification [opt]
19806 direct-declarator [ constant-expression [opt] ]
19809 direct-abstract-declarator:
19810 direct-abstract-declarator [opt]
19811 ( parameter-declaration-clause )
19812 cv-qualifier-seq [opt]
19813 ref-qualifier [opt]
19814 exception-specification [opt]
19815 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19816 ( abstract-declarator )
19818 Returns a representation of the declarator. DCL_KIND is
19819 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19820 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19821 we are parsing a direct-declarator. It is
19822 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19823 of ambiguity we prefer an abstract declarator, as per
19824 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19825 as for cp_parser_declarator. */
19827 static cp_declarator
*
19828 cp_parser_direct_declarator (cp_parser
* parser
,
19829 cp_parser_declarator_kind dcl_kind
,
19830 int* ctor_dtor_or_conv_p
,
19831 bool member_p
, bool friend_p
)
19834 cp_declarator
*declarator
= NULL
;
19835 tree scope
= NULL_TREE
;
19836 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19837 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19839 tree pushed_scope
= NULL_TREE
;
19840 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19844 /* Peek at the next token. */
19845 token
= cp_lexer_peek_token (parser
->lexer
);
19846 if (token
->type
== CPP_OPEN_PAREN
)
19848 /* This is either a parameter-declaration-clause, or a
19849 parenthesized declarator. When we know we are parsing a
19850 named declarator, it must be a parenthesized declarator
19851 if FIRST is true. For instance, `(int)' is a
19852 parameter-declaration-clause, with an omitted
19853 direct-abstract-declarator. But `((*))', is a
19854 parenthesized abstract declarator. Finally, when T is a
19855 template parameter `(T)' is a
19856 parameter-declaration-clause, and not a parenthesized
19859 We first try and parse a parameter-declaration-clause,
19860 and then try a nested declarator (if FIRST is true).
19862 It is not an error for it not to be a
19863 parameter-declaration-clause, even when FIRST is
19869 The first is the declaration of a function while the
19870 second is the definition of a variable, including its
19873 Having seen only the parenthesis, we cannot know which of
19874 these two alternatives should be selected. Even more
19875 complex are examples like:
19880 The former is a function-declaration; the latter is a
19881 variable initialization.
19883 Thus again, we try a parameter-declaration-clause, and if
19884 that fails, we back out and return. */
19886 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19889 bool is_declarator
= false;
19893 /* In a member-declarator, the only valid interpretation
19894 of a parenthesis is the start of a
19895 parameter-declaration-clause. (It is invalid to
19896 initialize a static data member with a parenthesized
19897 initializer; only the "=" form of initialization is
19900 cp_parser_parse_tentatively (parser
);
19902 /* Consume the `('. */
19903 matching_parens parens
;
19904 parens
.consume_open (parser
);
19907 /* If this is going to be an abstract declarator, we're
19908 in a declarator and we can't have default args. */
19909 parser
->default_arg_ok_p
= false;
19910 parser
->in_declarator_p
= true;
19913 begin_scope (sk_function_parms
, NULL_TREE
);
19915 /* Parse the parameter-declaration-clause. */
19916 params
= cp_parser_parameter_declaration_clause (parser
);
19918 /* Consume the `)'. */
19919 parens
.require_close (parser
);
19921 /* If all went well, parse the cv-qualifier-seq,
19922 ref-qualifier and the exception-specification. */
19923 if (member_p
|| cp_parser_parse_definitely (parser
))
19925 cp_cv_quals cv_quals
;
19926 cp_virt_specifiers virt_specifiers
;
19927 cp_ref_qualifier ref_qual
;
19928 tree exception_specification
;
19931 bool memfn
= (member_p
|| (pushed_scope
19932 && CLASS_TYPE_P (pushed_scope
)));
19934 is_declarator
= true;
19936 if (ctor_dtor_or_conv_p
)
19937 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
19940 /* Parse the cv-qualifier-seq. */
19941 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19942 /* Parse the ref-qualifier. */
19943 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
19944 /* Parse the tx-qualifier. */
19945 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
19946 /* And the exception-specification. */
19947 exception_specification
19948 = cp_parser_exception_specification_opt (parser
);
19950 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19952 /* In here, we handle cases where attribute is used after
19953 the function declaration. For example:
19954 void func (int x) __attribute__((vector(..))); */
19955 tree gnu_attrs
= NULL_TREE
;
19956 tree requires_clause
= NULL_TREE
;
19957 late_return
= (cp_parser_late_return_type_opt
19958 (parser
, declarator
, requires_clause
,
19959 memfn
? cv_quals
: -1));
19961 /* Parse the virt-specifier-seq. */
19962 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19964 /* Create the function-declarator. */
19965 declarator
= make_call_declarator (declarator
,
19971 exception_specification
,
19974 declarator
->std_attributes
= attrs
;
19975 declarator
->attributes
= gnu_attrs
;
19976 /* Any subsequent parameter lists are to do with
19977 return type, so are not those of the declared
19979 parser
->default_arg_ok_p
= false;
19982 /* Remove the function parms from scope. */
19983 pop_bindings_and_leave_scope ();
19986 /* Repeat the main loop. */
19990 /* If this is the first, we can try a parenthesized
19994 bool saved_in_type_id_in_expr_p
;
19996 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19997 parser
->in_declarator_p
= saved_in_declarator_p
;
19999 open_paren
= token
;
20000 /* Consume the `('. */
20001 matching_parens parens
;
20002 parens
.consume_open (parser
);
20003 /* Parse the nested declarator. */
20004 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20005 parser
->in_type_id_in_expr_p
= true;
20007 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20008 /*parenthesized_p=*/NULL
,
20009 member_p
, friend_p
);
20010 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20012 /* Expect a `)'. */
20013 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20014 if (!parens
.require_close (parser
))
20015 declarator
= cp_error_declarator
;
20016 if (declarator
== cp_error_declarator
)
20019 goto handle_declarator
;
20021 /* Otherwise, we must be done. */
20025 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20026 && token
->type
== CPP_OPEN_SQUARE
20027 && !cp_next_tokens_can_be_attribute_p (parser
))
20029 /* Parse an array-declarator. */
20030 tree bounds
, attrs
;
20032 if (ctor_dtor_or_conv_p
)
20033 *ctor_dtor_or_conv_p
= 0;
20037 parser
->default_arg_ok_p
= false;
20038 parser
->in_declarator_p
= true;
20039 /* Consume the `['. */
20040 cp_lexer_consume_token (parser
->lexer
);
20041 /* Peek at the next token. */
20042 token
= cp_lexer_peek_token (parser
->lexer
);
20043 /* If the next token is `]', then there is no
20044 constant-expression. */
20045 if (token
->type
!= CPP_CLOSE_SQUARE
)
20047 bool non_constant_p
;
20049 = cp_parser_constant_expression (parser
,
20050 /*allow_non_constant=*/true,
20052 if (!non_constant_p
)
20054 else if (error_operand_p (bounds
))
20055 /* Already gave an error. */;
20056 else if (!parser
->in_function_body
20057 || current_binding_level
->kind
== sk_function_parms
)
20059 /* Normally, the array bound must be an integral constant
20060 expression. However, as an extension, we allow VLAs
20061 in function scopes as long as they aren't part of a
20062 parameter declaration. */
20063 cp_parser_error (parser
,
20064 "array bound is not an integer constant");
20065 bounds
= error_mark_node
;
20067 else if (processing_template_decl
20068 && !type_dependent_expression_p (bounds
))
20070 /* Remember this wasn't a constant-expression. */
20071 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20072 TREE_SIDE_EFFECTS (bounds
) = 1;
20076 bounds
= NULL_TREE
;
20077 /* Look for the closing `]'. */
20078 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20080 declarator
= cp_error_declarator
;
20084 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20085 declarator
= make_array_declarator (declarator
, bounds
);
20086 declarator
->std_attributes
= attrs
;
20088 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20091 tree qualifying_scope
;
20092 tree unqualified_name
;
20094 special_function_kind sfk
;
20096 bool pack_expansion_p
= false;
20097 cp_token
*declarator_id_start_token
;
20099 /* Parse a declarator-id */
20100 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20103 cp_parser_parse_tentatively (parser
);
20105 /* If we see an ellipsis, we should be looking at a
20107 if (token
->type
== CPP_ELLIPSIS
)
20109 /* Consume the `...' */
20110 cp_lexer_consume_token (parser
->lexer
);
20112 pack_expansion_p
= true;
20116 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20118 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20119 qualifying_scope
= parser
->scope
;
20124 if (!unqualified_name
&& pack_expansion_p
)
20126 /* Check whether an error occurred. */
20127 okay
= !cp_parser_error_occurred (parser
);
20129 /* We already consumed the ellipsis to mark a
20130 parameter pack, but we have no way to report it,
20131 so abort the tentative parse. We will be exiting
20132 immediately anyway. */
20133 cp_parser_abort_tentative_parse (parser
);
20136 okay
= cp_parser_parse_definitely (parser
);
20139 unqualified_name
= error_mark_node
;
20140 else if (unqualified_name
20141 && (qualifying_scope
20142 || (!identifier_p (unqualified_name
))))
20144 cp_parser_error (parser
, "expected unqualified-id");
20145 unqualified_name
= error_mark_node
;
20149 if (!unqualified_name
)
20151 if (unqualified_name
== error_mark_node
)
20153 declarator
= cp_error_declarator
;
20154 pack_expansion_p
= false;
20155 declarator
->parameter_pack_p
= false;
20159 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20161 if (qualifying_scope
&& at_namespace_scope_p ()
20162 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20164 /* In the declaration of a member of a template class
20165 outside of the class itself, the SCOPE will sometimes
20166 be a TYPENAME_TYPE. For example, given:
20168 template <typename T>
20169 int S<T>::R::i = 3;
20171 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20172 this context, we must resolve S<T>::R to an ordinary
20173 type, rather than a typename type.
20175 The reason we normally avoid resolving TYPENAME_TYPEs
20176 is that a specialization of `S' might render
20177 `S<T>::R' not a type. However, if `S' is
20178 specialized, then this `i' will not be used, so there
20179 is no harm in resolving the types here. */
20182 /* Resolve the TYPENAME_TYPE. */
20183 type
= resolve_typename_type (qualifying_scope
,
20184 /*only_current_p=*/false);
20185 /* If that failed, the declarator is invalid. */
20186 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20188 if (typedef_variant_p (type
))
20189 error_at (declarator_id_start_token
->location
,
20190 "cannot define member of dependent typedef "
20193 error_at (declarator_id_start_token
->location
,
20194 "%<%T::%E%> is not a type",
20195 TYPE_CONTEXT (qualifying_scope
),
20196 TYPE_IDENTIFIER (qualifying_scope
));
20198 qualifying_scope
= type
;
20203 if (unqualified_name
)
20207 if (qualifying_scope
20208 && CLASS_TYPE_P (qualifying_scope
))
20209 class_type
= qualifying_scope
;
20211 class_type
= current_class_type
;
20213 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20215 tree name_type
= TREE_TYPE (unqualified_name
);
20217 if (!class_type
|| !same_type_p (name_type
, class_type
))
20219 /* We do not attempt to print the declarator
20220 here because we do not have enough
20221 information about its original syntactic
20223 cp_parser_error (parser
, "invalid declarator");
20224 declarator
= cp_error_declarator
;
20227 else if (qualifying_scope
20228 && CLASSTYPE_USE_TEMPLATE (name_type
))
20230 error_at (declarator_id_start_token
->location
,
20231 "invalid use of constructor as a template");
20232 inform (declarator_id_start_token
->location
,
20233 "use %<%T::%D%> instead of %<%T::%D%> to "
20234 "name the constructor in a qualified name",
20236 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20237 class_type
, name_type
);
20238 declarator
= cp_error_declarator
;
20241 unqualified_name
= constructor_name (class_type
);
20246 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20247 sfk
= sfk_destructor
;
20248 else if (identifier_p (unqualified_name
)
20249 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20250 sfk
= sfk_conversion
;
20251 else if (/* There's no way to declare a constructor
20252 for an unnamed type, even if the type
20253 got a name for linkage purposes. */
20254 !TYPE_WAS_UNNAMED (class_type
)
20255 /* Handle correctly (c++/19200):
20269 friend void N::S();
20271 && (!friend_p
|| class_type
== qualifying_scope
)
20272 && constructor_name_p (unqualified_name
,
20274 sfk
= sfk_constructor
;
20275 else if (is_overloaded_fn (unqualified_name
)
20276 && DECL_CONSTRUCTOR_P (get_first_fn
20277 (unqualified_name
)))
20278 sfk
= sfk_constructor
;
20280 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20281 *ctor_dtor_or_conv_p
= -1;
20284 declarator
= make_id_declarator (qualifying_scope
,
20287 declarator
->std_attributes
= attrs
;
20288 declarator
->id_loc
= token
->location
;
20289 declarator
->parameter_pack_p
= pack_expansion_p
;
20291 if (pack_expansion_p
)
20292 maybe_warn_variadic_templates ();
20295 handle_declarator
:;
20296 scope
= get_scope_of_declarator (declarator
);
20299 /* Any names that appear after the declarator-id for a
20300 member are looked up in the containing scope. */
20301 if (at_function_scope_p ())
20303 /* But declarations with qualified-ids can't appear in a
20305 cp_parser_error (parser
, "qualified-id in declaration");
20306 declarator
= cp_error_declarator
;
20309 pushed_scope
= push_scope (scope
);
20311 parser
->in_declarator_p
= true;
20312 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20313 || (declarator
&& declarator
->kind
== cdk_id
))
20314 /* Default args are only allowed on function
20316 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20318 parser
->default_arg_ok_p
= false;
20327 /* For an abstract declarator, we might wind up with nothing at this
20328 point. That's an error; the declarator is not optional. */
20330 cp_parser_error (parser
, "expected declarator");
20331 else if (open_paren
)
20333 /* Record overly parenthesized declarator so we can give a
20334 diagnostic about confusing decl/expr disambiguation. */
20335 if (declarator
->kind
== cdk_array
)
20337 /* If the open and close parens are on different lines, this
20338 is probably a formatting thing, so ignore. */
20339 expanded_location open
= expand_location (open_paren
->location
);
20340 expanded_location close
= expand_location (close_paren
->location
);
20341 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20345 declarator
->parenthesized
= open_paren
->location
;
20348 /* If we entered a scope, we must exit it now. */
20350 pop_scope (pushed_scope
);
20352 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20353 parser
->in_declarator_p
= saved_in_declarator_p
;
20358 /* Parse a ptr-operator.
20361 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20362 * cv-qualifier-seq [opt]
20364 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20365 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20370 & cv-qualifier-seq [opt]
20372 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20373 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20374 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20375 filled in with the TYPE containing the member. *CV_QUALS is
20376 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20377 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20378 Note that the tree codes returned by this function have nothing
20379 to do with the types of trees that will be eventually be created
20380 to represent the pointer or reference type being parsed. They are
20381 just constants with suggestive names. */
20382 static enum tree_code
20383 cp_parser_ptr_operator (cp_parser
* parser
,
20385 cp_cv_quals
*cv_quals
,
20388 enum tree_code code
= ERROR_MARK
;
20390 tree attrs
= NULL_TREE
;
20392 /* Assume that it's not a pointer-to-member. */
20394 /* And that there are no cv-qualifiers. */
20395 *cv_quals
= TYPE_UNQUALIFIED
;
20397 /* Peek at the next token. */
20398 token
= cp_lexer_peek_token (parser
->lexer
);
20400 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20401 if (token
->type
== CPP_MULT
)
20402 code
= INDIRECT_REF
;
20403 else if (token
->type
== CPP_AND
)
20405 else if ((cxx_dialect
!= cxx98
) &&
20406 token
->type
== CPP_AND_AND
) /* C++0x only */
20407 code
= NON_LVALUE_EXPR
;
20409 if (code
!= ERROR_MARK
)
20411 /* Consume the `*', `&' or `&&'. */
20412 cp_lexer_consume_token (parser
->lexer
);
20414 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20415 `&', if we are allowing GNU extensions. (The only qualifier
20416 that can legally appear after `&' is `restrict', but that is
20417 enforced during semantic analysis. */
20418 if (code
== INDIRECT_REF
20419 || cp_parser_allow_gnu_extensions_p (parser
))
20420 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20422 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20423 if (attributes
!= NULL
)
20424 *attributes
= attrs
;
20428 /* Try the pointer-to-member case. */
20429 cp_parser_parse_tentatively (parser
);
20430 /* Look for the optional `::' operator. */
20431 cp_parser_global_scope_opt (parser
,
20432 /*current_scope_valid_p=*/false);
20433 /* Look for the nested-name specifier. */
20434 token
= cp_lexer_peek_token (parser
->lexer
);
20435 cp_parser_nested_name_specifier (parser
,
20436 /*typename_keyword_p=*/false,
20437 /*check_dependency_p=*/true,
20439 /*is_declaration=*/false);
20440 /* If we found it, and the next token is a `*', then we are
20441 indeed looking at a pointer-to-member operator. */
20442 if (!cp_parser_error_occurred (parser
)
20443 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20445 /* Indicate that the `*' operator was used. */
20446 code
= INDIRECT_REF
;
20448 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20449 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20450 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20451 error_at (token
->location
, "cannot form pointer to member of "
20452 "non-class %q#T", parser
->scope
);
20455 /* The type of which the member is a member is given by the
20457 *type
= parser
->scope
;
20458 /* The next name will not be qualified. */
20459 parser
->scope
= NULL_TREE
;
20460 parser
->qualifying_scope
= NULL_TREE
;
20461 parser
->object_scope
= NULL_TREE
;
20462 /* Look for optional c++11 attributes. */
20463 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20464 if (attributes
!= NULL
)
20465 *attributes
= attrs
;
20466 /* Look for the optional cv-qualifier-seq. */
20467 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20470 /* If that didn't work we don't have a ptr-operator. */
20471 if (!cp_parser_parse_definitely (parser
))
20472 cp_parser_error (parser
, "expected ptr-operator");
20478 /* Parse an (optional) cv-qualifier-seq.
20481 cv-qualifier cv-qualifier-seq [opt]
20492 Returns a bitmask representing the cv-qualifiers. */
20495 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20497 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20502 cp_cv_quals cv_qualifier
;
20504 /* Peek at the next token. */
20505 token
= cp_lexer_peek_token (parser
->lexer
);
20506 /* See if it's a cv-qualifier. */
20507 switch (token
->keyword
)
20510 cv_qualifier
= TYPE_QUAL_CONST
;
20514 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20518 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20522 cv_qualifier
= TYPE_UNQUALIFIED
;
20529 if (cv_quals
& cv_qualifier
)
20531 gcc_rich_location
richloc (token
->location
);
20532 richloc
.add_fixit_remove ();
20533 error_at (&richloc
, "duplicate cv-qualifier");
20534 cp_lexer_purge_token (parser
->lexer
);
20538 cp_lexer_consume_token (parser
->lexer
);
20539 cv_quals
|= cv_qualifier
;
20546 /* Parse an (optional) ref-qualifier
20552 Returns cp_ref_qualifier representing ref-qualifier. */
20554 static cp_ref_qualifier
20555 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20557 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20559 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20560 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20565 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20566 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20568 switch (token
->type
)
20571 curr_ref_qual
= REF_QUAL_LVALUE
;
20575 curr_ref_qual
= REF_QUAL_RVALUE
;
20579 curr_ref_qual
= REF_QUAL_NONE
;
20583 if (!curr_ref_qual
)
20587 error_at (token
->location
, "multiple ref-qualifiers");
20588 cp_lexer_purge_token (parser
->lexer
);
20592 ref_qual
= curr_ref_qual
;
20593 cp_lexer_consume_token (parser
->lexer
);
20600 /* Parse an optional tx-qualifier.
20604 transaction_safe_dynamic */
20607 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20609 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20610 if (token
->type
== CPP_NAME
)
20612 tree name
= token
->u
.value
;
20613 const char *p
= IDENTIFIER_POINTER (name
);
20614 const int len
= strlen ("transaction_safe");
20615 if (!strncmp (p
, "transaction_safe", len
))
20619 || !strcmp (p
, "_dynamic"))
20621 cp_lexer_consume_token (parser
->lexer
);
20624 error ("%qE requires %<-fgnu-tm%>", name
);
20635 /* Parse an (optional) virt-specifier-seq.
20637 virt-specifier-seq:
20638 virt-specifier virt-specifier-seq [opt]
20644 Returns a bitmask representing the virt-specifiers. */
20646 static cp_virt_specifiers
20647 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20649 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20654 cp_virt_specifiers virt_specifier
;
20656 /* Peek at the next token. */
20657 token
= cp_lexer_peek_token (parser
->lexer
);
20658 /* See if it's a virt-specifier-qualifier. */
20659 if (token
->type
!= CPP_NAME
)
20661 if (id_equal (token
->u
.value
, "override"))
20663 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20664 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20666 else if (id_equal (token
->u
.value
, "final"))
20668 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20669 virt_specifier
= VIRT_SPEC_FINAL
;
20671 else if (id_equal (token
->u
.value
, "__final"))
20673 virt_specifier
= VIRT_SPEC_FINAL
;
20678 if (virt_specifiers
& virt_specifier
)
20680 gcc_rich_location
richloc (token
->location
);
20681 richloc
.add_fixit_remove ();
20682 error_at (&richloc
, "duplicate virt-specifier");
20683 cp_lexer_purge_token (parser
->lexer
);
20687 cp_lexer_consume_token (parser
->lexer
);
20688 virt_specifiers
|= virt_specifier
;
20691 return virt_specifiers
;
20694 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20695 is in scope even though it isn't real. */
20698 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20702 if (current_class_ptr
)
20704 /* We don't clear this between NSDMIs. Is it already what we want? */
20705 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20706 if (DECL_P (current_class_ptr
)
20707 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20708 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20709 && cp_type_quals (type
) == quals
)
20713 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20714 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20715 current_class_ptr
= NULL_TREE
;
20717 = cp_build_fold_indirect_ref (this_parm
);
20718 current_class_ptr
= this_parm
;
20721 /* Return true iff our current scope is a non-static data member
20725 parsing_nsdmi (void)
20727 /* We recognize NSDMI context by the context-less 'this' pointer set up
20728 by the function above. */
20729 if (current_class_ptr
20730 && TREE_CODE (current_class_ptr
) == PARM_DECL
20731 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20736 /* Parse a late-specified return type, if any. This is not a separate
20737 non-terminal, but part of a function declarator, which looks like
20739 -> trailing-type-specifier-seq abstract-declarator(opt)
20741 Returns the type indicated by the type-id.
20743 In addition to this, parse any queued up #pragma omp declare simd
20744 clauses, and #pragma acc routine clauses.
20746 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20750 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20751 tree
& requires_clause
, cp_cv_quals quals
)
20754 tree type
= NULL_TREE
;
20755 bool declare_simd_p
= (parser
->omp_declare_simd
20757 && declarator
->kind
== cdk_id
);
20759 bool oacc_routine_p
= (parser
->oacc_routine
20761 && declarator
->kind
== cdk_id
);
20763 /* Peek at the next token. */
20764 token
= cp_lexer_peek_token (parser
->lexer
);
20765 /* A late-specified return type is indicated by an initial '->'. */
20766 if (token
->type
!= CPP_DEREF
20767 && token
->keyword
!= RID_REQUIRES
20768 && !(token
->type
== CPP_NAME
20769 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20770 && !(declare_simd_p
|| oacc_routine_p
))
20773 tree save_ccp
= current_class_ptr
;
20774 tree save_ccr
= current_class_ref
;
20777 /* DR 1207: 'this' is in scope in the trailing return type. */
20778 inject_this_parameter (current_class_type
, quals
);
20781 if (token
->type
== CPP_DEREF
)
20783 /* Consume the ->. */
20784 cp_lexer_consume_token (parser
->lexer
);
20786 type
= cp_parser_trailing_type_id (parser
);
20789 /* Function declarations may be followed by a trailing
20790 requires-clause. */
20791 requires_clause
= cp_parser_requires_clause_opt (parser
);
20793 if (declare_simd_p
)
20794 declarator
->attributes
20795 = cp_parser_late_parsing_omp_declare_simd (parser
,
20796 declarator
->attributes
);
20797 if (oacc_routine_p
)
20798 declarator
->attributes
20799 = cp_parser_late_parsing_oacc_routine (parser
,
20800 declarator
->attributes
);
20804 current_class_ptr
= save_ccp
;
20805 current_class_ref
= save_ccr
;
20811 /* Parse a declarator-id.
20815 :: [opt] nested-name-specifier [opt] type-name
20817 In the `id-expression' case, the value returned is as for
20818 cp_parser_id_expression if the id-expression was an unqualified-id.
20819 If the id-expression was a qualified-id, then a SCOPE_REF is
20820 returned. The first operand is the scope (either a NAMESPACE_DECL
20821 or TREE_TYPE), but the second is still just a representation of an
20825 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20828 /* The expression must be an id-expression. Assume that qualified
20829 names are the names of types so that:
20832 int S<T>::R::i = 3;
20834 will work; we must treat `S<T>::R' as the name of a type.
20835 Similarly, assume that qualified names are templates, where
20839 int S<T>::R<T>::i = 3;
20842 id
= cp_parser_id_expression (parser
,
20843 /*template_keyword_p=*/false,
20844 /*check_dependency_p=*/false,
20845 /*template_p=*/NULL
,
20846 /*declarator_p=*/true,
20848 if (id
&& BASELINK_P (id
))
20849 id
= BASELINK_FUNCTIONS (id
);
20853 /* Parse a type-id.
20856 type-specifier-seq abstract-declarator [opt]
20858 Returns the TYPE specified. */
20861 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20862 bool is_trailing_return
)
20864 cp_decl_specifier_seq type_specifier_seq
;
20865 cp_declarator
*abstract_declarator
;
20867 /* Parse the type-specifier-seq. */
20868 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20869 is_trailing_return
,
20870 &type_specifier_seq
);
20871 if (is_template_arg
&& type_specifier_seq
.type
20872 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20873 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20874 /* A bare template name as a template argument is a template template
20875 argument, not a placeholder, so fail parsing it as a type argument. */
20877 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20878 cp_parser_simulate_error (parser
);
20879 return error_mark_node
;
20881 if (type_specifier_seq
.type
== error_mark_node
)
20882 return error_mark_node
;
20884 /* There might or might not be an abstract declarator. */
20885 cp_parser_parse_tentatively (parser
);
20886 /* Look for the declarator. */
20887 abstract_declarator
20888 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20889 /*parenthesized_p=*/NULL
,
20890 /*member_p=*/false,
20891 /*friend_p=*/false);
20892 /* Check to see if there really was a declarator. */
20893 if (!cp_parser_parse_definitely (parser
))
20894 abstract_declarator
= NULL
;
20896 if (type_specifier_seq
.type
20897 /* The concepts TS allows 'auto' as a type-id. */
20898 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20899 /* None of the valid uses of 'auto' in C++14 involve the type-id
20900 nonterminal, but it is valid in a trailing-return-type. */
20901 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20902 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20904 /* A type-id with type 'auto' is only ok if the abstract declarator
20905 is a function declarator with a late-specified return type.
20907 A type-id with 'auto' is also valid in a trailing-return-type
20908 in a compound-requirement. */
20909 if (abstract_declarator
20910 && abstract_declarator
->kind
== cdk_function
20911 && abstract_declarator
->u
.function
.late_return_type
)
20913 else if (parser
->in_result_type_constraint_p
)
20917 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20918 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20920 error_at (loc
, "missing template arguments after %qT",
20922 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
20926 error_at (loc
, "invalid use of %qT", auto_node
);
20927 return error_mark_node
;
20931 return groktypename (&type_specifier_seq
, abstract_declarator
,
20936 cp_parser_type_id (cp_parser
*parser
)
20938 return cp_parser_type_id_1 (parser
, false, false);
20942 cp_parser_template_type_arg (cp_parser
*parser
)
20945 const char *saved_message
= parser
->type_definition_forbidden_message
;
20946 parser
->type_definition_forbidden_message
20947 = G_("types may not be defined in template arguments");
20948 r
= cp_parser_type_id_1 (parser
, true, false);
20949 parser
->type_definition_forbidden_message
= saved_message
;
20950 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
20952 error ("invalid use of %<auto%> in template argument");
20953 r
= error_mark_node
;
20959 cp_parser_trailing_type_id (cp_parser
*parser
)
20961 return cp_parser_type_id_1 (parser
, false, true);
20964 /* Parse a type-specifier-seq.
20966 type-specifier-seq:
20967 type-specifier type-specifier-seq [opt]
20971 type-specifier-seq:
20972 attributes type-specifier-seq [opt]
20974 If IS_DECLARATION is true, we are at the start of a "condition" or
20975 exception-declaration, so we might be followed by a declarator-id.
20977 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20978 i.e. we've just seen "->".
20980 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20983 cp_parser_type_specifier_seq (cp_parser
* parser
,
20984 bool is_declaration
,
20985 bool is_trailing_return
,
20986 cp_decl_specifier_seq
*type_specifier_seq
)
20988 bool seen_type_specifier
= false;
20989 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
20990 cp_token
*start_token
= NULL
;
20992 /* Clear the TYPE_SPECIFIER_SEQ. */
20993 clear_decl_specs (type_specifier_seq
);
20995 /* In the context of a trailing return type, enum E { } is an
20996 elaborated-type-specifier followed by a function-body, not an
20998 if (is_trailing_return
)
20999 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21001 /* Parse the type-specifiers and attributes. */
21004 tree type_specifier
;
21005 bool is_cv_qualifier
;
21007 /* Check for attributes first. */
21008 if (cp_next_tokens_can_be_attribute_p (parser
))
21010 type_specifier_seq
->attributes
=
21011 chainon (type_specifier_seq
->attributes
,
21012 cp_parser_attributes_opt (parser
));
21016 /* record the token of the beginning of the type specifier seq,
21017 for error reporting purposes*/
21019 start_token
= cp_lexer_peek_token (parser
->lexer
);
21021 /* Look for the type-specifier. */
21022 type_specifier
= cp_parser_type_specifier (parser
,
21024 type_specifier_seq
,
21025 /*is_declaration=*/false,
21028 if (!type_specifier
)
21030 /* If the first type-specifier could not be found, this is not a
21031 type-specifier-seq at all. */
21032 if (!seen_type_specifier
)
21034 /* Set in_declarator_p to avoid skipping to the semicolon. */
21035 int in_decl
= parser
->in_declarator_p
;
21036 parser
->in_declarator_p
= true;
21038 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21039 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21040 cp_parser_error (parser
, "expected type-specifier");
21042 parser
->in_declarator_p
= in_decl
;
21044 type_specifier_seq
->type
= error_mark_node
;
21047 /* If subsequent type-specifiers could not be found, the
21048 type-specifier-seq is complete. */
21052 seen_type_specifier
= true;
21053 /* The standard says that a condition can be:
21055 type-specifier-seq declarator = assignment-expression
21062 we should treat the "S" as a declarator, not as a
21063 type-specifier. The standard doesn't say that explicitly for
21064 type-specifier-seq, but it does say that for
21065 decl-specifier-seq in an ordinary declaration. Perhaps it
21066 would be clearer just to allow a decl-specifier-seq here, and
21067 then add a semantic restriction that if any decl-specifiers
21068 that are not type-specifiers appear, the program is invalid. */
21069 if (is_declaration
&& !is_cv_qualifier
)
21070 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21074 /* Return whether the function currently being declared has an associated
21075 template parameter list. */
21078 function_being_declared_is_template_p (cp_parser
* parser
)
21080 if (!current_template_parms
|| processing_template_parmlist
)
21083 if (parser
->implicit_template_scope
)
21086 if (at_class_scope_p ()
21087 && TYPE_BEING_DEFINED (current_class_type
))
21088 return parser
->num_template_parameter_lists
!= 0;
21090 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21091 (current_class_type
));
21094 /* Parse a parameter-declaration-clause.
21096 parameter-declaration-clause:
21097 parameter-declaration-list [opt] ... [opt]
21098 parameter-declaration-list , ...
21100 Returns a representation for the parameter declarations. A return
21101 value of NULL indicates a parameter-declaration-clause consisting
21102 only of an ellipsis. */
21105 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21114 int auto_is_implicit_function_template_parm_p
;
21116 parser
->auto_is_implicit_function_template_parm_p
21117 = auto_is_implicit_function_template_parm_p
;
21119 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
21123 if (!processing_specialization
21124 && !processing_template_parmlist
21125 && !processing_explicit_instantiation
)
21126 if (!current_function_decl
21127 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21128 parser
->auto_is_implicit_function_template_parm_p
= true;
21130 /* Peek at the next token. */
21131 token
= cp_lexer_peek_token (parser
->lexer
);
21132 /* Check for trivial parameter-declaration-clauses. */
21133 if (token
->type
== CPP_ELLIPSIS
)
21135 /* Consume the `...' token. */
21136 cp_lexer_consume_token (parser
->lexer
);
21139 else if (token
->type
== CPP_CLOSE_PAREN
)
21140 /* There are no parameters. */
21142 #ifndef NO_IMPLICIT_EXTERN_C
21143 if (in_system_header_at (input_location
)
21144 && current_class_type
== NULL
21145 && current_lang_name
== lang_name_c
)
21149 return void_list_node
;
21151 /* Check for `(void)', too, which is a special case. */
21152 else if (token
->keyword
== RID_VOID
21153 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21154 == CPP_CLOSE_PAREN
))
21156 /* Consume the `void' token. */
21157 cp_lexer_consume_token (parser
->lexer
);
21158 /* There are no parameters. */
21159 return void_list_node
;
21162 /* Parse the parameter-declaration-list. */
21163 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21164 /* If a parse error occurred while parsing the
21165 parameter-declaration-list, then the entire
21166 parameter-declaration-clause is erroneous. */
21170 /* Peek at the next token. */
21171 token
= cp_lexer_peek_token (parser
->lexer
);
21172 /* If it's a `,', the clause should terminate with an ellipsis. */
21173 if (token
->type
== CPP_COMMA
)
21175 /* Consume the `,'. */
21176 cp_lexer_consume_token (parser
->lexer
);
21177 /* Expect an ellipsis. */
21179 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21181 /* It might also be `...' if the optional trailing `,' was
21183 else if (token
->type
== CPP_ELLIPSIS
)
21185 /* Consume the `...' token. */
21186 cp_lexer_consume_token (parser
->lexer
);
21187 /* And remember that we saw it. */
21191 ellipsis_p
= false;
21193 /* Finish the parameter list. */
21195 parameters
= chainon (parameters
, void_list_node
);
21200 /* Parse a parameter-declaration-list.
21202 parameter-declaration-list:
21203 parameter-declaration
21204 parameter-declaration-list , parameter-declaration
21206 Returns a representation of the parameter-declaration-list, as for
21207 cp_parser_parameter_declaration_clause. However, the
21208 `void_list_node' is never appended to the list. Upon return,
21209 *IS_ERROR will be true iff an error occurred. */
21212 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21214 tree parameters
= NULL_TREE
;
21215 tree
*tail
= ¶meters
;
21216 bool saved_in_unbraced_linkage_specification_p
;
21219 /* Assume all will go well. */
21221 /* The special considerations that apply to a function within an
21222 unbraced linkage specifications do not apply to the parameters
21223 to the function. */
21224 saved_in_unbraced_linkage_specification_p
21225 = parser
->in_unbraced_linkage_specification_p
;
21226 parser
->in_unbraced_linkage_specification_p
= false;
21228 /* Look for more parameters. */
21231 cp_parameter_declarator
*parameter
;
21232 tree decl
= error_mark_node
;
21233 bool parenthesized_p
= false;
21234 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21235 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21236 (current_template_parms
)) : 0);
21238 /* Parse the parameter. */
21240 = cp_parser_parameter_declaration (parser
,
21241 /*template_parm_p=*/false,
21244 /* We don't know yet if the enclosing context is deprecated, so wait
21245 and warn in grokparms if appropriate. */
21246 deprecated_state
= DEPRECATED_SUPPRESS
;
21250 /* If a function parameter pack was specified and an implicit template
21251 parameter was introduced during cp_parser_parameter_declaration,
21252 change any implicit parameters introduced into packs. */
21253 if (parser
->implicit_template_parms
21254 && parameter
->declarator
21255 && parameter
->declarator
->parameter_pack_p
)
21257 int latest_template_parm_idx
= TREE_VEC_LENGTH
21258 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21260 if (latest_template_parm_idx
!= template_parm_idx
)
21261 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21262 (parameter
->decl_specifiers
.type
,
21263 template_parm_idx
, latest_template_parm_idx
);
21266 decl
= grokdeclarator (parameter
->declarator
,
21267 ¶meter
->decl_specifiers
,
21269 parameter
->default_argument
!= NULL_TREE
,
21270 ¶meter
->decl_specifiers
.attributes
);
21271 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21272 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21275 deprecated_state
= DEPRECATED_NORMAL
;
21277 /* If a parse error occurred parsing the parameter declaration,
21278 then the entire parameter-declaration-list is erroneous. */
21279 if (decl
== error_mark_node
)
21282 parameters
= error_mark_node
;
21286 if (parameter
->decl_specifiers
.attributes
)
21287 cplus_decl_attributes (&decl
,
21288 parameter
->decl_specifiers
.attributes
,
21290 if (DECL_NAME (decl
))
21291 decl
= pushdecl (decl
);
21293 if (decl
!= error_mark_node
)
21295 retrofit_lang_decl (decl
);
21296 DECL_PARM_INDEX (decl
) = ++index
;
21297 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21300 /* Add the new parameter to the list. */
21301 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21302 tail
= &TREE_CHAIN (*tail
);
21304 /* Peek at the next token. */
21305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21306 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21307 /* These are for Objective-C++ */
21308 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21309 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21310 /* The parameter-declaration-list is complete. */
21312 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21316 /* Peek at the next token. */
21317 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21318 /* If it's an ellipsis, then the list is complete. */
21319 if (token
->type
== CPP_ELLIPSIS
)
21321 /* Otherwise, there must be more parameters. Consume the
21323 cp_lexer_consume_token (parser
->lexer
);
21324 /* When parsing something like:
21326 int i(float f, double d)
21328 we can tell after seeing the declaration for "f" that we
21329 are not looking at an initialization of a variable "i",
21330 but rather at the declaration of a function "i".
21332 Due to the fact that the parsing of template arguments
21333 (as specified to a template-id) requires backtracking we
21334 cannot use this technique when inside a template argument
21336 if (!parser
->in_template_argument_list_p
21337 && !parser
->in_type_id_in_expr_p
21338 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21339 /* However, a parameter-declaration of the form
21340 "float(f)" (which is a valid declaration of a
21341 parameter "f") can also be interpreted as an
21342 expression (the conversion of "f" to "float"). */
21343 && !parenthesized_p
)
21344 cp_parser_commit_to_tentative_parse (parser
);
21348 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21349 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21350 cp_parser_skip_to_closing_parenthesis (parser
,
21351 /*recovering=*/true,
21352 /*or_comma=*/false,
21353 /*consume_paren=*/false);
21358 parser
->in_unbraced_linkage_specification_p
21359 = saved_in_unbraced_linkage_specification_p
;
21361 /* Reset implicit_template_scope if we are about to leave the function
21362 parameter list that introduced it. Note that for out-of-line member
21363 definitions, there will be one or more class scopes before we get to
21364 the template parameter scope. */
21366 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21367 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21369 while (maybe_its
->kind
== sk_class
)
21370 maybe_its
= maybe_its
->level_chain
;
21371 if (maybe_its
== its
)
21373 parser
->implicit_template_parms
= 0;
21374 parser
->implicit_template_scope
= 0;
21381 /* Parse a parameter declaration.
21383 parameter-declaration:
21384 decl-specifier-seq ... [opt] declarator
21385 decl-specifier-seq declarator = assignment-expression
21386 decl-specifier-seq ... [opt] abstract-declarator [opt]
21387 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21389 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21390 declares a template parameter. (In that case, a non-nested `>'
21391 token encountered during the parsing of the assignment-expression
21392 is not interpreted as a greater-than operator.)
21394 Returns a representation of the parameter, or NULL if an error
21395 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21396 true iff the declarator is of the form "(p)". */
21398 static cp_parameter_declarator
*
21399 cp_parser_parameter_declaration (cp_parser
*parser
,
21400 bool template_parm_p
,
21401 bool *parenthesized_p
)
21403 int declares_class_or_enum
;
21404 cp_decl_specifier_seq decl_specifiers
;
21405 cp_declarator
*declarator
;
21406 tree default_argument
;
21407 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21408 const char *saved_message
;
21409 bool template_parameter_pack_p
= false;
21411 /* In a template parameter, `>' is not an operator.
21415 When parsing a default template-argument for a non-type
21416 template-parameter, the first non-nested `>' is taken as the end
21417 of the template parameter-list rather than a greater-than
21420 /* Type definitions may not appear in parameter types. */
21421 saved_message
= parser
->type_definition_forbidden_message
;
21422 parser
->type_definition_forbidden_message
21423 = G_("types may not be defined in parameter types");
21425 /* Parse the declaration-specifiers. */
21426 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21427 cp_parser_decl_specifier_seq (parser
,
21428 CP_PARSER_FLAGS_NONE
,
21430 &declares_class_or_enum
);
21432 /* Complain about missing 'typename' or other invalid type names. */
21433 if (!decl_specifiers
.any_type_specifiers_p
21434 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21435 decl_specifiers
.type
= error_mark_node
;
21437 /* If an error occurred, there's no reason to attempt to parse the
21438 rest of the declaration. */
21439 if (cp_parser_error_occurred (parser
))
21441 parser
->type_definition_forbidden_message
= saved_message
;
21445 /* Peek at the next token. */
21446 token
= cp_lexer_peek_token (parser
->lexer
);
21448 /* If the next token is a `)', `,', `=', `>', or `...', then there
21449 is no declarator. However, when variadic templates are enabled,
21450 there may be a declarator following `...'. */
21451 if (token
->type
== CPP_CLOSE_PAREN
21452 || token
->type
== CPP_COMMA
21453 || token
->type
== CPP_EQ
21454 || token
->type
== CPP_GREATER
)
21457 if (parenthesized_p
)
21458 *parenthesized_p
= false;
21460 /* Otherwise, there should be a declarator. */
21463 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21464 parser
->default_arg_ok_p
= false;
21466 /* After seeing a decl-specifier-seq, if the next token is not a
21467 "(", there is no possibility that the code is a valid
21468 expression. Therefore, if parsing tentatively, we commit at
21470 if (!parser
->in_template_argument_list_p
21471 /* In an expression context, having seen:
21475 we cannot be sure whether we are looking at a
21476 function-type (taking a "char" as a parameter) or a cast
21477 of some object of type "char" to "int". */
21478 && !parser
->in_type_id_in_expr_p
21479 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21480 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21481 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21482 cp_parser_commit_to_tentative_parse (parser
);
21483 /* Parse the declarator. */
21484 declarator_token_start
= token
;
21485 declarator
= cp_parser_declarator (parser
,
21486 CP_PARSER_DECLARATOR_EITHER
,
21487 /*ctor_dtor_or_conv_p=*/NULL
,
21489 /*member_p=*/false,
21490 /*friend_p=*/false);
21491 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21492 /* After the declarator, allow more attributes. */
21493 decl_specifiers
.attributes
21494 = chainon (decl_specifiers
.attributes
,
21495 cp_parser_attributes_opt (parser
));
21497 /* If the declarator is a template parameter pack, remember that and
21498 clear the flag in the declarator itself so we don't get errors
21499 from grokdeclarator. */
21500 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21502 declarator
->parameter_pack_p
= false;
21503 template_parameter_pack_p
= true;
21507 /* If the next token is an ellipsis, and we have not seen a declarator
21508 name, and if either the type of the declarator contains parameter
21509 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21510 for, eg, abbreviated integral type names), then we actually have a
21511 parameter pack expansion expression. Otherwise, leave the ellipsis
21512 for a C-style variadic function. */
21513 token
= cp_lexer_peek_token (parser
->lexer
);
21514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21516 tree type
= decl_specifiers
.type
;
21518 if (type
&& DECL_P (type
))
21519 type
= TREE_TYPE (type
);
21522 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21523 && (template_parm_p
|| uses_parameter_packs (type
)))
21524 || (!type
&& template_parm_p
))
21525 && declarator_can_be_parameter_pack (declarator
))
21527 /* Consume the `...'. */
21528 cp_lexer_consume_token (parser
->lexer
);
21529 maybe_warn_variadic_templates ();
21531 /* Build a pack expansion type */
21532 if (template_parm_p
)
21533 template_parameter_pack_p
= true;
21534 else if (declarator
)
21535 declarator
->parameter_pack_p
= true;
21537 decl_specifiers
.type
= make_pack_expansion (type
);
21541 /* The restriction on defining new types applies only to the type
21542 of the parameter, not to the default argument. */
21543 parser
->type_definition_forbidden_message
= saved_message
;
21545 /* If the next token is `=', then process a default argument. */
21546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21548 tree type
= decl_specifiers
.type
;
21549 token
= cp_lexer_peek_token (parser
->lexer
);
21550 /* If we are defining a class, then the tokens that make up the
21551 default argument must be saved and processed later. */
21552 if (!template_parm_p
&& at_class_scope_p ()
21553 && TYPE_BEING_DEFINED (current_class_type
)
21554 && !LAMBDA_TYPE_P (current_class_type
))
21555 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21557 // A constrained-type-specifier may declare a type template-parameter.
21558 else if (declares_constrained_type_template_parameter (type
))
21560 = cp_parser_default_type_template_argument (parser
);
21562 // A constrained-type-specifier may declare a template-template-parameter.
21563 else if (declares_constrained_template_template_parameter (type
))
21565 = cp_parser_default_template_template_argument (parser
);
21567 /* Outside of a class definition, we can just parse the
21568 assignment-expression. */
21571 = cp_parser_default_argument (parser
, template_parm_p
);
21573 if (!parser
->default_arg_ok_p
)
21575 permerror (token
->location
,
21576 "default arguments are only "
21577 "permitted for function parameters");
21579 else if ((declarator
&& declarator
->parameter_pack_p
)
21580 || template_parameter_pack_p
21581 || (decl_specifiers
.type
21582 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21584 /* Find the name of the parameter pack. */
21585 cp_declarator
*id_declarator
= declarator
;
21586 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21587 id_declarator
= id_declarator
->declarator
;
21589 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21590 error_at (declarator_token_start
->location
,
21592 ? G_("template parameter pack %qD "
21593 "cannot have a default argument")
21594 : G_("parameter pack %qD cannot have "
21595 "a default argument"),
21596 id_declarator
->u
.id
.unqualified_name
);
21598 error_at (declarator_token_start
->location
,
21600 ? G_("template parameter pack cannot have "
21601 "a default argument")
21602 : G_("parameter pack cannot have a "
21603 "default argument"));
21605 default_argument
= NULL_TREE
;
21609 default_argument
= NULL_TREE
;
21611 /* Generate a location for the parameter, ranging from the start of the
21612 initial token to the end of the final token (using input_location for
21613 the latter, set up by cp_lexer_set_source_position_from_token when
21616 If we have a identifier, then use it for the caret location, e.g.
21618 extern int callee (int one, int (*two)(int, int), float three);
21619 ~~~~~~^~~~~~~~~~~~~~
21621 otherwise, reuse the start location for the caret location e.g.:
21623 extern int callee (int one, int (*)(int, int), float three);
21627 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21628 ? declarator
->id_loc
21629 : decl_spec_token_start
->location
);
21630 location_t param_loc
= make_location (caret_loc
,
21631 decl_spec_token_start
->location
,
21634 return make_parameter_declarator (&decl_specifiers
,
21638 template_parameter_pack_p
);
21641 /* Parse a default argument and return it.
21643 TEMPLATE_PARM_P is true if this is a default argument for a
21644 non-type template parameter. */
21646 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21648 tree default_argument
= NULL_TREE
;
21649 bool saved_greater_than_is_operator_p
;
21650 bool saved_local_variables_forbidden_p
;
21651 bool non_constant_p
, is_direct_init
;
21653 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21655 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21656 parser
->greater_than_is_operator_p
= !template_parm_p
;
21657 /* Local variable names (and the `this' keyword) may not
21658 appear in a default argument. */
21659 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21660 parser
->local_variables_forbidden_p
= true;
21661 /* Parse the assignment-expression. */
21662 if (template_parm_p
)
21663 push_deferring_access_checks (dk_no_deferred
);
21664 tree saved_class_ptr
= NULL_TREE
;
21665 tree saved_class_ref
= NULL_TREE
;
21666 /* The "this" pointer is not valid in a default argument. */
21669 saved_class_ptr
= current_class_ptr
;
21670 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21671 saved_class_ref
= current_class_ref
;
21672 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21675 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21676 /* Restore the "this" pointer. */
21679 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21680 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21682 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21683 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21684 if (template_parm_p
)
21685 pop_deferring_access_checks ();
21686 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21687 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21689 return default_argument
;
21692 /* Parse a function-body.
21695 compound_statement */
21698 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21700 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21701 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21705 /* Parse a ctor-initializer-opt followed by a function-body. Return
21706 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21707 is true we are parsing a function-try-block. */
21710 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21711 bool in_function_try_block
)
21714 const bool check_body_p
=
21715 DECL_CONSTRUCTOR_P (current_function_decl
)
21716 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21719 /* Begin the function body. */
21720 body
= begin_function_body ();
21721 /* Parse the optional ctor-initializer. */
21722 cp_parser_ctor_initializer_opt (parser
);
21724 /* If we're parsing a constexpr constructor definition, we need
21725 to check that the constructor body is indeed empty. However,
21726 before we get to cp_parser_function_body lot of junk has been
21727 generated, so we can't just check that we have an empty block.
21728 Rather we take a snapshot of the outermost block, and check whether
21729 cp_parser_function_body changed its state. */
21732 list
= cur_stmt_list
;
21733 if (STATEMENT_LIST_TAIL (list
))
21734 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21736 /* Parse the function-body. */
21737 cp_parser_function_body (parser
, in_function_try_block
);
21739 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21740 /* Finish the function body. */
21741 finish_function_body (body
);
21744 /* Parse an initializer.
21747 = initializer-clause
21748 ( expression-list )
21750 Returns an expression representing the initializer. If no
21751 initializer is present, NULL_TREE is returned.
21753 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21754 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21755 set to TRUE if there is no initializer present. If there is an
21756 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21757 is set to true; otherwise it is set to false. */
21760 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21761 bool* non_constant_p
)
21766 /* Peek at the next token. */
21767 token
= cp_lexer_peek_token (parser
->lexer
);
21769 /* Let our caller know whether or not this initializer was
21771 *is_direct_init
= (token
->type
!= CPP_EQ
);
21772 /* Assume that the initializer is constant. */
21773 *non_constant_p
= false;
21775 if (token
->type
== CPP_EQ
)
21777 /* Consume the `='. */
21778 cp_lexer_consume_token (parser
->lexer
);
21779 /* Parse the initializer-clause. */
21780 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21782 else if (token
->type
== CPP_OPEN_PAREN
)
21784 vec
<tree
, va_gc
> *vec
;
21785 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21787 /*allow_expansion_p=*/true,
21790 return error_mark_node
;
21791 init
= build_tree_list_vec (vec
);
21792 release_tree_vector (vec
);
21794 else if (token
->type
== CPP_OPEN_BRACE
)
21796 cp_lexer_set_source_position (parser
->lexer
);
21797 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21798 init
= cp_parser_braced_list (parser
, non_constant_p
);
21799 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21803 /* Anything else is an error. */
21804 cp_parser_error (parser
, "expected initializer");
21805 init
= error_mark_node
;
21808 if (check_for_bare_parameter_packs (init
))
21809 init
= error_mark_node
;
21814 /* Parse an initializer-clause.
21816 initializer-clause:
21817 assignment-expression
21820 Returns an expression representing the initializer.
21822 If the `assignment-expression' production is used the value
21823 returned is simply a representation for the expression.
21825 Otherwise, calls cp_parser_braced_list. */
21828 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21830 cp_expr initializer
;
21832 /* Assume the expression is constant. */
21833 *non_constant_p
= false;
21835 /* If it is not a `{', then we are looking at an
21836 assignment-expression. */
21837 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21840 = cp_parser_constant_expression (parser
,
21841 /*allow_non_constant_p=*/true,
21845 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21847 return initializer
;
21850 /* Parse a brace-enclosed initializer list.
21853 { initializer-list , [opt] }
21854 { designated-initializer-list , [opt] }
21857 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21858 the elements of the initializer-list (or NULL, if the last
21859 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21860 NULL_TREE. There is no way to detect whether or not the optional
21861 trailing `,' was provided. NON_CONSTANT_P is as for
21862 cp_parser_initializer. */
21865 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21868 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21870 /* Consume the `{' token. */
21871 matching_braces braces
;
21872 braces
.consume_open (parser
);
21873 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21874 initializer
= make_node (CONSTRUCTOR
);
21875 /* If it's not a `}', then there is a non-trivial initializer. */
21876 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21878 /* Parse the initializer list. */
21879 CONSTRUCTOR_ELTS (initializer
)
21880 = cp_parser_initializer_list (parser
, non_constant_p
);
21881 /* A trailing `,' token is allowed. */
21882 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21883 cp_lexer_consume_token (parser
->lexer
);
21886 *non_constant_p
= false;
21887 /* Now, there should be a trailing `}'. */
21888 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21889 braces
.require_close (parser
);
21890 TREE_TYPE (initializer
) = init_list_type_node
;
21892 cp_expr
result (initializer
);
21893 /* Build a location of the form:
21896 with caret==start at the open brace, finish at the close brace. */
21897 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21898 result
.set_location (combined_loc
);
21902 /* Consume tokens up to, and including, the next non-nested closing `]'.
21903 Returns true iff we found a closing `]'. */
21906 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21908 unsigned square_depth
= 0;
21912 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21914 switch (token
->type
)
21917 case CPP_PRAGMA_EOL
:
21918 /* If we've run out of tokens, then there is no closing `]'. */
21921 case CPP_OPEN_SQUARE
:
21925 case CPP_CLOSE_SQUARE
:
21926 if (!square_depth
--)
21928 cp_lexer_consume_token (parser
->lexer
);
21937 /* Consume the token. */
21938 cp_lexer_consume_token (parser
->lexer
);
21942 /* Return true if we are looking at an array-designator, false otherwise. */
21945 cp_parser_array_designator_p (cp_parser
*parser
)
21947 /* Consume the `['. */
21948 cp_lexer_consume_token (parser
->lexer
);
21950 cp_lexer_save_tokens (parser
->lexer
);
21952 /* Skip tokens until the next token is a closing square bracket.
21953 If we find the closing `]', and the next token is a `=', then
21954 we are looking at an array designator. */
21955 bool array_designator_p
21956 = (cp_parser_skip_to_closing_square_bracket (parser
)
21957 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
21959 /* Roll back the tokens we skipped. */
21960 cp_lexer_rollback_tokens (parser
->lexer
);
21962 return array_designator_p
;
21965 /* Parse an initializer-list.
21968 initializer-clause ... [opt]
21969 initializer-list , initializer-clause ... [opt]
21973 designated-initializer-list:
21974 designated-initializer-clause
21975 designated-initializer-list , designated-initializer-clause
21977 designated-initializer-clause:
21978 designator brace-or-equal-initializer
21986 designation initializer-clause ...[opt]
21987 initializer-list , designation initializer-clause ...[opt]
21992 [ constant-expression ] =
21994 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21995 for the initializer. If the INDEX of the elt is non-NULL, it is the
21996 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21997 as for cp_parser_initializer. */
21999 static vec
<constructor_elt
, va_gc
> *
22000 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22002 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22003 bool first_p
= true;
22004 tree first_designator
= NULL_TREE
;
22006 /* Assume all of the expressions are constant. */
22007 *non_constant_p
= false;
22009 /* Parse the rest of the list. */
22015 bool clause_non_constant_p
;
22016 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22018 /* Handle the C++2A syntax, '. id ='. */
22019 if ((cxx_dialect
>= cxx2a
22020 || cp_parser_allow_gnu_extensions_p (parser
))
22021 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22022 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22023 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22024 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22025 == CPP_OPEN_BRACE
)))
22027 if (cxx_dialect
< cxx2a
)
22028 pedwarn (loc
, OPT_Wpedantic
,
22029 "C++ designated initializers only available with "
22030 "-std=c++2a or -std=gnu++2a");
22031 /* Consume the `.'. */
22032 cp_lexer_consume_token (parser
->lexer
);
22033 /* Consume the identifier. */
22034 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22036 /* Consume the `='. */
22037 cp_lexer_consume_token (parser
->lexer
);
22039 /* Also, if the next token is an identifier and the following one is a
22040 colon, we are looking at the GNU designated-initializer
22042 else if (cp_parser_allow_gnu_extensions_p (parser
)
22043 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22044 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22047 /* Warn the user that they are using an extension. */
22048 pedwarn (loc
, OPT_Wpedantic
,
22049 "ISO C++ does not allow GNU designated initializers");
22050 /* Consume the identifier. */
22051 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22052 /* Consume the `:'. */
22053 cp_lexer_consume_token (parser
->lexer
);
22055 /* Also handle C99 array designators, '[ const ] ='. */
22056 else if (cp_parser_allow_gnu_extensions_p (parser
)
22057 && !c_dialect_objc ()
22058 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22060 /* In C++11, [ could start a lambda-introducer. */
22061 bool non_const
= false;
22063 cp_parser_parse_tentatively (parser
);
22065 if (!cp_parser_array_designator_p (parser
))
22067 cp_parser_simulate_error (parser
);
22068 designator
= NULL_TREE
;
22072 designator
= cp_parser_constant_expression (parser
, true,
22074 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22075 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22078 if (!cp_parser_parse_definitely (parser
))
22079 designator
= NULL_TREE
;
22081 && (!require_potential_rvalue_constant_expression
22083 designator
= NULL_TREE
;
22085 /* Warn the user that they are using an extension. */
22086 pedwarn (loc
, OPT_Wpedantic
,
22087 "ISO C++ does not allow C99 designated initializers");
22090 designator
= NULL_TREE
;
22094 first_designator
= designator
;
22097 else if (cxx_dialect
>= cxx2a
22098 && first_designator
!= error_mark_node
22099 && (!first_designator
!= !designator
))
22101 error_at (loc
, "either all initializer clauses should be designated "
22102 "or none of them should be");
22103 first_designator
= error_mark_node
;
22105 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22106 first_designator
= designator
;
22108 /* Parse the initializer. */
22109 initializer
= cp_parser_initializer_clause (parser
,
22110 &clause_non_constant_p
);
22111 /* If any clause is non-constant, so is the entire initializer. */
22112 if (clause_non_constant_p
)
22113 *non_constant_p
= true;
22115 /* If we have an ellipsis, this is an initializer pack
22117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22119 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22121 /* Consume the `...'. */
22122 cp_lexer_consume_token (parser
->lexer
);
22124 if (designator
&& cxx_dialect
>= cxx2a
)
22126 "%<...%> not allowed in designated initializer list");
22128 /* Turn the initializer into an initializer expansion. */
22129 initializer
= make_pack_expansion (initializer
);
22132 /* Add it to the vector. */
22133 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22135 /* If the next token is not a comma, we have reached the end of
22137 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22140 /* Peek at the next token. */
22141 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22142 /* If the next token is a `}', then we're still done. An
22143 initializer-clause can have a trailing `,' after the
22144 initializer-list and before the closing `}'. */
22145 if (token
->type
== CPP_CLOSE_BRACE
)
22148 /* Consume the `,' token. */
22149 cp_lexer_consume_token (parser
->lexer
);
22152 /* The same identifier shall not appear in multiple designators
22153 of a designated-initializer-list. */
22154 if (first_designator
)
22157 tree designator
, val
;
22158 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22159 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22161 if (IDENTIFIER_MARKED (designator
))
22163 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22164 "%<.%s%> designator used multiple times in "
22165 "the same initializer list",
22166 IDENTIFIER_POINTER (designator
));
22167 (*v
)[i
].index
= NULL_TREE
;
22170 IDENTIFIER_MARKED (designator
) = 1;
22172 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22173 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22174 IDENTIFIER_MARKED (designator
) = 0;
22180 /* Classes [gram.class] */
22182 /* Parse a class-name.
22188 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22189 to indicate that names looked up in dependent types should be
22190 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22191 keyword has been used to indicate that the name that appears next
22192 is a template. TAG_TYPE indicates the explicit tag given before
22193 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22194 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22195 is the class being defined in a class-head. If ENUM_OK is TRUE,
22196 enum-names are also accepted.
22198 Returns the TYPE_DECL representing the class. */
22201 cp_parser_class_name (cp_parser
*parser
,
22202 bool typename_keyword_p
,
22203 bool template_keyword_p
,
22204 enum tag_types tag_type
,
22205 bool check_dependency_p
,
22207 bool is_declaration
,
22214 tree identifier
= NULL_TREE
;
22216 /* All class-names start with an identifier. */
22217 token
= cp_lexer_peek_token (parser
->lexer
);
22218 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22220 cp_parser_error (parser
, "expected class-name");
22221 return error_mark_node
;
22224 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22225 to a template-id, so we save it here. */
22226 scope
= parser
->scope
;
22227 if (scope
== error_mark_node
)
22228 return error_mark_node
;
22230 /* Any name names a type if we're following the `typename' keyword
22231 in a qualified name where the enclosing scope is type-dependent. */
22232 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22233 && dependent_type_p (scope
));
22234 /* Handle the common case (an identifier, but not a template-id)
22236 if (token
->type
== CPP_NAME
22237 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22239 cp_token
*identifier_token
;
22242 /* Look for the identifier. */
22243 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22244 ambiguous_p
= identifier_token
->error_reported
;
22245 identifier
= cp_parser_identifier (parser
);
22246 /* If the next token isn't an identifier, we are certainly not
22247 looking at a class-name. */
22248 if (identifier
== error_mark_node
)
22249 decl
= error_mark_node
;
22250 /* If we know this is a type-name, there's no need to look it
22252 else if (typename_p
)
22256 tree ambiguous_decls
;
22257 /* If we already know that this lookup is ambiguous, then
22258 we've already issued an error message; there's no reason
22262 cp_parser_simulate_error (parser
);
22263 return error_mark_node
;
22265 /* If the next token is a `::', then the name must be a type
22268 [basic.lookup.qual]
22270 During the lookup for a name preceding the :: scope
22271 resolution operator, object, function, and enumerator
22272 names are ignored. */
22273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22274 tag_type
= scope_type
;
22275 /* Look up the name. */
22276 decl
= cp_parser_lookup_name (parser
, identifier
,
22278 /*is_template=*/false,
22279 /*is_namespace=*/false,
22280 check_dependency_p
,
22282 identifier_token
->location
);
22283 if (ambiguous_decls
)
22285 if (cp_parser_parsing_tentatively (parser
))
22286 cp_parser_simulate_error (parser
);
22287 return error_mark_node
;
22293 /* Try a template-id. */
22294 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22295 check_dependency_p
,
22298 if (decl
== error_mark_node
)
22299 return error_mark_node
;
22302 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22304 /* If this is a typename, create a TYPENAME_TYPE. */
22305 if (typename_p
&& decl
!= error_mark_node
)
22307 decl
= make_typename_type (scope
, decl
, typename_type
,
22308 /*complain=*/tf_error
);
22309 if (decl
!= error_mark_node
)
22310 decl
= TYPE_NAME (decl
);
22313 decl
= strip_using_decl (decl
);
22315 /* Check to see that it is really the name of a class. */
22316 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22317 && identifier_p (TREE_OPERAND (decl
, 0))
22318 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22319 /* Situations like this:
22321 template <typename T> struct A {
22322 typename T::template X<int>::I i;
22325 are problematic. Is `T::template X<int>' a class-name? The
22326 standard does not seem to be definitive, but there is no other
22327 valid interpretation of the following `::'. Therefore, those
22328 names are considered class-names. */
22330 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22331 if (decl
!= error_mark_node
)
22332 decl
= TYPE_NAME (decl
);
22334 else if (TREE_CODE (decl
) != TYPE_DECL
22335 || TREE_TYPE (decl
) == error_mark_node
22336 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22337 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22338 /* In Objective-C 2.0, a classname followed by '.' starts a
22339 dot-syntax expression, and it's not a type-name. */
22340 || (c_dialect_objc ()
22341 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22342 && objc_is_class_name (decl
)))
22343 decl
= error_mark_node
;
22345 if (decl
== error_mark_node
)
22346 cp_parser_error (parser
, "expected class-name");
22347 else if (identifier
&& !parser
->scope
)
22348 maybe_note_name_used_in_class (identifier
, decl
);
22353 /* Parse a class-specifier.
22356 class-head { member-specification [opt] }
22358 Returns the TREE_TYPE representing the class. */
22361 cp_parser_class_specifier_1 (cp_parser
* parser
)
22364 tree attributes
= NULL_TREE
;
22365 bool nested_name_specifier_p
;
22366 unsigned saved_num_template_parameter_lists
;
22367 bool saved_in_function_body
;
22368 unsigned char in_statement
;
22369 bool in_switch_statement_p
;
22370 bool saved_in_unbraced_linkage_specification_p
;
22371 tree old_scope
= NULL_TREE
;
22372 tree scope
= NULL_TREE
;
22373 cp_token
*closing_brace
;
22375 push_deferring_access_checks (dk_no_deferred
);
22377 /* Parse the class-head. */
22378 type
= cp_parser_class_head (parser
,
22379 &nested_name_specifier_p
);
22380 /* If the class-head was a semantic disaster, skip the entire body
22384 cp_parser_skip_to_end_of_block_or_statement (parser
);
22385 pop_deferring_access_checks ();
22386 return error_mark_node
;
22389 /* Look for the `{'. */
22390 matching_braces braces
;
22391 if (!braces
.require_open (parser
))
22393 pop_deferring_access_checks ();
22394 return error_mark_node
;
22397 cp_ensure_no_omp_declare_simd (parser
);
22398 cp_ensure_no_oacc_routine (parser
);
22400 /* Issue an error message if type-definitions are forbidden here. */
22401 cp_parser_check_type_definition (parser
);
22402 /* Remember that we are defining one more class. */
22403 ++parser
->num_classes_being_defined
;
22404 /* Inside the class, surrounding template-parameter-lists do not
22406 saved_num_template_parameter_lists
22407 = parser
->num_template_parameter_lists
;
22408 parser
->num_template_parameter_lists
= 0;
22409 /* We are not in a function body. */
22410 saved_in_function_body
= parser
->in_function_body
;
22411 parser
->in_function_body
= false;
22412 /* Or in a loop. */
22413 in_statement
= parser
->in_statement
;
22414 parser
->in_statement
= 0;
22415 /* Or in a switch. */
22416 in_switch_statement_p
= parser
->in_switch_statement_p
;
22417 parser
->in_switch_statement_p
= false;
22418 /* We are not immediately inside an extern "lang" block. */
22419 saved_in_unbraced_linkage_specification_p
22420 = parser
->in_unbraced_linkage_specification_p
;
22421 parser
->in_unbraced_linkage_specification_p
= false;
22423 // Associate constraints with the type.
22425 type
= associate_classtype_constraints (type
);
22427 /* Start the class. */
22428 if (nested_name_specifier_p
)
22430 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22431 old_scope
= push_inner_scope (scope
);
22433 type
= begin_class_definition (type
);
22435 if (type
== error_mark_node
)
22436 /* If the type is erroneous, skip the entire body of the class. */
22437 cp_parser_skip_to_closing_brace (parser
);
22439 /* Parse the member-specification. */
22440 cp_parser_member_specification_opt (parser
);
22442 /* Look for the trailing `}'. */
22443 closing_brace
= braces
.require_close (parser
);
22444 /* Look for trailing attributes to apply to this class. */
22445 if (cp_parser_allow_gnu_extensions_p (parser
))
22446 attributes
= cp_parser_gnu_attributes_opt (parser
);
22447 if (type
!= error_mark_node
)
22448 type
= finish_struct (type
, attributes
);
22449 if (nested_name_specifier_p
)
22450 pop_inner_scope (old_scope
, scope
);
22452 /* We've finished a type definition. Check for the common syntax
22453 error of forgetting a semicolon after the definition. We need to
22454 be careful, as we can't just check for not-a-semicolon and be done
22455 with it; the user might have typed:
22457 class X { } c = ...;
22458 class X { } *p = ...;
22460 and so forth. Instead, enumerate all the possible tokens that
22461 might follow this production; if we don't see one of them, then
22462 complain and silently insert the semicolon. */
22464 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22465 bool want_semicolon
= true;
22467 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22468 /* Don't try to parse c++11 attributes here. As per the
22469 grammar, that should be a task for
22470 cp_parser_decl_specifier_seq. */
22471 want_semicolon
= false;
22473 switch (token
->type
)
22476 case CPP_SEMICOLON
:
22479 case CPP_OPEN_PAREN
:
22480 case CPP_CLOSE_PAREN
:
22482 want_semicolon
= false;
22485 /* While it's legal for type qualifiers and storage class
22486 specifiers to follow type definitions in the grammar, only
22487 compiler testsuites contain code like that. Assume that if
22488 we see such code, then what we're really seeing is a case
22492 const <type> var = ...;
22497 static <type> func (...) ...
22499 i.e. the qualifier or specifier applies to the next
22500 declaration. To do so, however, we need to look ahead one
22501 more token to see if *that* token is a type specifier.
22503 This code could be improved to handle:
22506 static const <type> var = ...; */
22508 if (keyword_is_decl_specifier (token
->keyword
))
22510 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22512 /* Handling user-defined types here would be nice, but very
22515 = (lookahead
->type
== CPP_KEYWORD
22516 && keyword_begins_type_specifier (lookahead
->keyword
));
22523 /* If we don't have a type, then something is very wrong and we
22524 shouldn't try to do anything clever. Likewise for not seeing the
22526 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22528 /* Locate the closing brace. */
22529 cp_token_position prev
22530 = cp_lexer_previous_token_position (parser
->lexer
);
22531 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22532 location_t loc
= prev_token
->location
;
22534 /* We want to suggest insertion of a ';' immediately *after* the
22535 closing brace, so, if we can, offset the location by 1 column. */
22536 location_t next_loc
= loc
;
22537 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22538 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22540 rich_location
richloc (line_table
, next_loc
);
22542 /* If we successfully offset the location, suggest the fix-it. */
22543 if (next_loc
!= loc
)
22544 richloc
.add_fixit_insert_before (next_loc
, ";");
22546 if (CLASSTYPE_DECLARED_CLASS (type
))
22547 error_at (&richloc
,
22548 "expected %<;%> after class definition");
22549 else if (TREE_CODE (type
) == RECORD_TYPE
)
22550 error_at (&richloc
,
22551 "expected %<;%> after struct definition");
22552 else if (TREE_CODE (type
) == UNION_TYPE
)
22553 error_at (&richloc
,
22554 "expected %<;%> after union definition");
22556 gcc_unreachable ();
22558 /* Unget one token and smash it to look as though we encountered
22559 a semicolon in the input stream. */
22560 cp_lexer_set_token_position (parser
->lexer
, prev
);
22561 token
= cp_lexer_peek_token (parser
->lexer
);
22562 token
->type
= CPP_SEMICOLON
;
22563 token
->keyword
= RID_MAX
;
22567 /* If this class is not itself within the scope of another class,
22568 then we need to parse the bodies of all of the queued function
22569 definitions. Note that the queued functions defined in a class
22570 are not always processed immediately following the
22571 class-specifier for that class. Consider:
22574 struct B { void f() { sizeof (A); } };
22577 If `f' were processed before the processing of `A' were
22578 completed, there would be no way to compute the size of `A'.
22579 Note that the nesting we are interested in here is lexical --
22580 not the semantic nesting given by TYPE_CONTEXT. In particular,
22583 struct A { struct B; };
22584 struct A::B { void f() { } };
22586 there is no need to delay the parsing of `A::B::f'. */
22587 if (--parser
->num_classes_being_defined
== 0)
22590 tree class_type
= NULL_TREE
;
22591 tree pushed_scope
= NULL_TREE
;
22593 cp_default_arg_entry
*e
;
22594 tree save_ccp
, save_ccr
;
22596 /* In a first pass, parse default arguments to the functions.
22597 Then, in a second pass, parse the bodies of the functions.
22598 This two-phased approach handles cases like:
22606 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22609 /* If there are default arguments that have not yet been processed,
22610 take care of them now. */
22611 if (class_type
!= e
->class_type
)
22614 pop_scope (pushed_scope
);
22615 class_type
= e
->class_type
;
22616 pushed_scope
= push_scope (class_type
);
22618 /* Make sure that any template parameters are in scope. */
22619 maybe_begin_member_template_processing (decl
);
22620 /* Parse the default argument expressions. */
22621 cp_parser_late_parsing_default_args (parser
, decl
);
22622 /* Remove any template parameters from the symbol table. */
22623 maybe_end_member_template_processing ();
22625 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22626 /* Now parse any NSDMIs. */
22627 save_ccp
= current_class_ptr
;
22628 save_ccr
= current_class_ref
;
22629 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22631 if (class_type
!= DECL_CONTEXT (decl
))
22634 pop_scope (pushed_scope
);
22635 class_type
= DECL_CONTEXT (decl
);
22636 pushed_scope
= push_scope (class_type
);
22638 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22639 cp_parser_late_parsing_nsdmi (parser
, decl
);
22641 vec_safe_truncate (unparsed_nsdmis
, 0);
22642 current_class_ptr
= save_ccp
;
22643 current_class_ref
= save_ccr
;
22645 pop_scope (pushed_scope
);
22647 /* Now do some post-NSDMI bookkeeping. */
22648 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22649 after_nsdmi_defaulted_late_checks (class_type
);
22650 vec_safe_truncate (unparsed_classes
, 0);
22651 after_nsdmi_defaulted_late_checks (type
);
22653 /* Now parse the body of the functions. */
22656 /* OpenMP UDRs need to be parsed before all other functions. */
22657 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22658 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22659 cp_parser_late_parsing_for_member (parser
, decl
);
22660 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22661 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22662 cp_parser_late_parsing_for_member (parser
, decl
);
22665 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22666 cp_parser_late_parsing_for_member (parser
, decl
);
22667 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22670 vec_safe_push (unparsed_classes
, type
);
22672 /* Put back any saved access checks. */
22673 pop_deferring_access_checks ();
22675 /* Restore saved state. */
22676 parser
->in_switch_statement_p
= in_switch_statement_p
;
22677 parser
->in_statement
= in_statement
;
22678 parser
->in_function_body
= saved_in_function_body
;
22679 parser
->num_template_parameter_lists
22680 = saved_num_template_parameter_lists
;
22681 parser
->in_unbraced_linkage_specification_p
22682 = saved_in_unbraced_linkage_specification_p
;
22688 cp_parser_class_specifier (cp_parser
* parser
)
22691 timevar_push (TV_PARSE_STRUCT
);
22692 ret
= cp_parser_class_specifier_1 (parser
);
22693 timevar_pop (TV_PARSE_STRUCT
);
22697 /* Parse a class-head.
22700 class-key identifier [opt] base-clause [opt]
22701 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22702 class-key nested-name-specifier [opt] template-id
22705 class-virt-specifier:
22709 class-key attributes identifier [opt] base-clause [opt]
22710 class-key attributes nested-name-specifier identifier base-clause [opt]
22711 class-key attributes nested-name-specifier [opt] template-id
22714 Upon return BASES is initialized to the list of base classes (or
22715 NULL, if there are none) in the same form returned by
22716 cp_parser_base_clause.
22718 Returns the TYPE of the indicated class. Sets
22719 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22720 involving a nested-name-specifier was used, and FALSE otherwise.
22722 Returns error_mark_node if this is not a class-head.
22724 Returns NULL_TREE if the class-head is syntactically valid, but
22725 semantically invalid in a way that means we should skip the entire
22726 body of the class. */
22729 cp_parser_class_head (cp_parser
* parser
,
22730 bool* nested_name_specifier_p
)
22732 tree nested_name_specifier
;
22733 enum tag_types class_key
;
22734 tree id
= NULL_TREE
;
22735 tree type
= NULL_TREE
;
22738 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22739 bool template_id_p
= false;
22740 bool qualified_p
= false;
22741 bool invalid_nested_name_p
= false;
22742 bool invalid_explicit_specialization_p
= false;
22743 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22744 tree pushed_scope
= NULL_TREE
;
22745 unsigned num_templates
;
22746 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22747 /* Assume no nested-name-specifier will be present. */
22748 *nested_name_specifier_p
= false;
22749 /* Assume no template parameter lists will be used in defining the
22752 parser
->colon_corrects_to_scope_p
= false;
22754 /* Look for the class-key. */
22755 class_key
= cp_parser_class_key (parser
);
22756 if (class_key
== none_type
)
22757 return error_mark_node
;
22759 location_t class_head_start_location
= input_location
;
22761 /* Parse the attributes. */
22762 attributes
= cp_parser_attributes_opt (parser
);
22764 /* If the next token is `::', that is invalid -- but sometimes
22765 people do try to write:
22769 Handle this gracefully by accepting the extra qualifier, and then
22770 issuing an error about it later if this really is a
22771 class-head. If it turns out just to be an elaborated type
22772 specifier, remain silent. */
22773 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22774 qualified_p
= true;
22776 push_deferring_access_checks (dk_no_check
);
22778 /* Determine the name of the class. Begin by looking for an
22779 optional nested-name-specifier. */
22780 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22781 nested_name_specifier
22782 = cp_parser_nested_name_specifier_opt (parser
,
22783 /*typename_keyword_p=*/false,
22784 /*check_dependency_p=*/false,
22786 /*is_declaration=*/false);
22787 /* If there was a nested-name-specifier, then there *must* be an
22790 cp_token
*bad_template_keyword
= NULL
;
22792 if (nested_name_specifier
)
22794 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22795 /* Although the grammar says `identifier', it really means
22796 `class-name' or `template-name'. You are only allowed to
22797 define a class that has already been declared with this
22800 The proposed resolution for Core Issue 180 says that wherever
22801 you see `class T::X' you should treat `X' as a type-name.
22803 It is OK to define an inaccessible class; for example:
22805 class A { class B; };
22808 We do not know if we will see a class-name, or a
22809 template-name. We look for a class-name first, in case the
22810 class-name is a template-id; if we looked for the
22811 template-name first we would stop after the template-name. */
22812 cp_parser_parse_tentatively (parser
);
22813 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22814 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22815 type
= cp_parser_class_name (parser
,
22816 /*typename_keyword_p=*/false,
22817 /*template_keyword_p=*/false,
22819 /*check_dependency_p=*/false,
22820 /*class_head_p=*/true,
22821 /*is_declaration=*/false);
22822 /* If that didn't work, ignore the nested-name-specifier. */
22823 if (!cp_parser_parse_definitely (parser
))
22825 invalid_nested_name_p
= true;
22826 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22827 id
= cp_parser_identifier (parser
);
22828 if (id
== error_mark_node
)
22831 /* If we could not find a corresponding TYPE, treat this
22832 declaration like an unqualified declaration. */
22833 if (type
== error_mark_node
)
22834 nested_name_specifier
= NULL_TREE
;
22835 /* Otherwise, count the number of templates used in TYPE and its
22836 containing scopes. */
22841 for (scope
= TREE_TYPE (type
);
22842 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22843 scope
= get_containing_scope (scope
))
22845 && CLASS_TYPE_P (scope
)
22846 && CLASSTYPE_TEMPLATE_INFO (scope
)
22847 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22848 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22849 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22853 /* Otherwise, the identifier is optional. */
22856 /* We don't know whether what comes next is a template-id,
22857 an identifier, or nothing at all. */
22858 cp_parser_parse_tentatively (parser
);
22859 /* Check for a template-id. */
22860 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22861 id
= cp_parser_template_id (parser
,
22862 /*template_keyword_p=*/false,
22863 /*check_dependency_p=*/true,
22865 /*is_declaration=*/true);
22866 /* If that didn't work, it could still be an identifier. */
22867 if (!cp_parser_parse_definitely (parser
))
22869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22871 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22872 id
= cp_parser_identifier (parser
);
22879 template_id_p
= true;
22884 pop_deferring_access_checks ();
22888 cp_parser_check_for_invalid_template_id (parser
, id
,
22890 type_start_token
->location
);
22892 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22894 /* If it's not a `:' or a `{' then we can't really be looking at a
22895 class-head, since a class-head only appears as part of a
22896 class-specifier. We have to detect this situation before calling
22897 xref_tag, since that has irreversible side-effects. */
22898 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22900 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22901 type
= error_mark_node
;
22905 /* At this point, we're going ahead with the class-specifier, even
22906 if some other problem occurs. */
22907 cp_parser_commit_to_tentative_parse (parser
);
22908 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22910 cp_parser_error (parser
,
22911 "cannot specify %<override%> for a class");
22912 type
= error_mark_node
;
22915 /* Issue the error about the overly-qualified name now. */
22918 cp_parser_error (parser
,
22919 "global qualification of class name is invalid");
22920 type
= error_mark_node
;
22923 else if (invalid_nested_name_p
)
22925 cp_parser_error (parser
,
22926 "qualified name does not name a class");
22927 type
= error_mark_node
;
22930 else if (nested_name_specifier
)
22934 if (bad_template_keyword
)
22935 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22936 keyword template shall not appear at the top level. */
22937 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
22938 "keyword %<template%> not allowed in class-head-name");
22940 /* Reject typedef-names in class heads. */
22941 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
22943 error_at (type_start_token
->location
,
22944 "invalid class name in declaration of %qD",
22950 /* Figure out in what scope the declaration is being placed. */
22951 scope
= current_scope ();
22952 /* If that scope does not contain the scope in which the
22953 class was originally declared, the program is invalid. */
22954 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
22956 if (at_namespace_scope_p ())
22957 error_at (type_start_token
->location
,
22958 "declaration of %qD in namespace %qD which does not "
22960 type
, scope
, nested_name_specifier
);
22962 error_at (type_start_token
->location
,
22963 "declaration of %qD in %qD which does not enclose %qD",
22964 type
, scope
, nested_name_specifier
);
22970 A declarator-id shall not be qualified except for the
22971 definition of a ... nested class outside of its class
22972 ... [or] the definition or explicit instantiation of a
22973 class member of a namespace outside of its namespace. */
22974 if (scope
== nested_name_specifier
)
22976 permerror (nested_name_specifier_token_start
->location
,
22977 "extra qualification not allowed");
22978 nested_name_specifier
= NULL_TREE
;
22982 /* An explicit-specialization must be preceded by "template <>". If
22983 it is not, try to recover gracefully. */
22984 if (at_namespace_scope_p ()
22985 && parser
->num_template_parameter_lists
== 0
22986 && !processing_template_parmlist
22989 /* Build a location of this form:
22990 struct typename <ARGS>
22991 ^~~~~~~~~~~~~~~~~~~~~~
22992 with caret==start at the start token, and
22993 finishing at the end of the type. */
22994 location_t reported_loc
22995 = make_location (class_head_start_location
,
22996 class_head_start_location
,
22997 get_finish (type_start_token
->location
));
22998 rich_location
richloc (line_table
, reported_loc
);
22999 richloc
.add_fixit_insert_before (class_head_start_location
,
23001 error_at (&richloc
,
23002 "an explicit specialization must be preceded by"
23003 " %<template <>%>");
23004 invalid_explicit_specialization_p
= true;
23005 /* Take the same action that would have been taken by
23006 cp_parser_explicit_specialization. */
23007 ++parser
->num_template_parameter_lists
;
23008 begin_specialization ();
23010 /* There must be no "return" statements between this point and the
23011 end of this function; set "type "to the correct return value and
23012 use "goto done;" to return. */
23013 /* Make sure that the right number of template parameters were
23015 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23016 type_start_token
->location
,
23017 /*declarator=*/NULL
))
23019 /* If something went wrong, there is no point in even trying to
23020 process the class-definition. */
23025 /* Look up the type. */
23028 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23029 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23030 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23032 error_at (type_start_token
->location
,
23033 "function template %qD redeclared as a class template", id
);
23034 type
= error_mark_node
;
23038 type
= TREE_TYPE (id
);
23039 type
= maybe_process_partial_specialization (type
);
23041 /* Check the scope while we still know whether or not we had a
23042 nested-name-specifier. */
23043 if (type
!= error_mark_node
)
23044 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23046 if (nested_name_specifier
)
23047 pushed_scope
= push_scope (nested_name_specifier
);
23049 else if (nested_name_specifier
)
23055 template <typename T> struct S { struct T };
23056 template <typename T> struct S<T>::T { };
23058 we will get a TYPENAME_TYPE when processing the definition of
23059 `S::T'. We need to resolve it to the actual type before we
23060 try to define it. */
23061 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23063 class_type
= resolve_typename_type (TREE_TYPE (type
),
23064 /*only_current_p=*/false);
23065 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23066 type
= TYPE_NAME (class_type
);
23069 cp_parser_error (parser
, "could not resolve typename type");
23070 type
= error_mark_node
;
23074 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23075 == error_mark_node
)
23081 class_type
= current_class_type
;
23082 /* Enter the scope indicated by the nested-name-specifier. */
23083 pushed_scope
= push_scope (nested_name_specifier
);
23084 /* Get the canonical version of this type. */
23085 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23086 /* Call push_template_decl if it seems like we should be defining a
23087 template either from the template headers or the type we're
23088 defining, so that we diagnose both extra and missing headers. */
23089 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23090 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23091 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23093 type
= push_template_decl (type
);
23094 if (type
== error_mark_node
)
23101 type
= TREE_TYPE (type
);
23102 *nested_name_specifier_p
= true;
23104 else /* The name is not a nested name. */
23106 /* If the class was unnamed, create a dummy name. */
23108 id
= make_anon_name ();
23109 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23110 ? ts_within_enclosing_non_class
23112 type
= xref_tag (class_key
, id
, tag_scope
,
23113 parser
->num_template_parameter_lists
);
23116 /* Indicate whether this class was declared as a `class' or as a
23118 if (TREE_CODE (type
) == RECORD_TYPE
)
23119 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23120 cp_parser_check_class_key (class_key
, type
);
23122 /* If this type was already complete, and we see another definition,
23123 that's an error. */
23124 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23126 error_at (type_start_token
->location
, "redefinition of %q#T",
23128 inform (location_of (type
), "previous definition of %q#T",
23133 else if (type
== error_mark_node
)
23138 /* Apply attributes now, before any use of the class as a template
23139 argument in its base list. */
23140 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23141 fixup_attribute_variants (type
);
23144 /* We will have entered the scope containing the class; the names of
23145 base classes should be looked up in that context. For example:
23147 struct A { struct B {}; struct C; };
23148 struct A::C : B {};
23152 /* Get the list of base-classes, if there is one. */
23153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23155 /* PR59482: enter the class scope so that base-specifiers are looked
23159 bases
= cp_parser_base_clause (parser
);
23160 /* PR59482: get out of the previously pushed class scope so that the
23161 subsequent pops pop the right thing. */
23168 /* If we're really defining a class, process the base classes.
23169 If they're invalid, fail. */
23170 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23171 xref_basetypes (type
, bases
);
23174 /* Leave the scope given by the nested-name-specifier. We will
23175 enter the class scope itself while processing the members. */
23177 pop_scope (pushed_scope
);
23179 if (invalid_explicit_specialization_p
)
23181 end_specialization ();
23182 --parser
->num_template_parameter_lists
;
23186 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23187 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23188 CLASSTYPE_FINAL (type
) = 1;
23190 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23194 /* Parse a class-key.
23201 Returns the kind of class-key specified, or none_type to indicate
23204 static enum tag_types
23205 cp_parser_class_key (cp_parser
* parser
)
23208 enum tag_types tag_type
;
23210 /* Look for the class-key. */
23211 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23215 /* Check to see if the TOKEN is a class-key. */
23216 tag_type
= cp_parser_token_is_class_key (token
);
23218 cp_parser_error (parser
, "expected class-key");
23222 /* Parse a type-parameter-key.
23224 type-parameter-key:
23230 cp_parser_type_parameter_key (cp_parser
* parser
)
23232 /* Look for the type-parameter-key. */
23233 enum tag_types tag_type
= none_type
;
23234 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23235 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23237 cp_lexer_consume_token (parser
->lexer
);
23238 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23239 /* typename is not allowed in a template template parameter
23240 by the standard until C++17. */
23241 pedwarn (token
->location
, OPT_Wpedantic
,
23242 "ISO C++ forbids typename key in template template parameter;"
23243 " use -std=c++17 or -std=gnu++17");
23246 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23251 /* Parse an (optional) member-specification.
23253 member-specification:
23254 member-declaration member-specification [opt]
23255 access-specifier : member-specification [opt] */
23258 cp_parser_member_specification_opt (cp_parser
* parser
)
23265 /* Peek at the next token. */
23266 token
= cp_lexer_peek_token (parser
->lexer
);
23267 /* If it's a `}', or EOF then we've seen all the members. */
23268 if (token
->type
== CPP_CLOSE_BRACE
23269 || token
->type
== CPP_EOF
23270 || token
->type
== CPP_PRAGMA_EOL
)
23273 /* See if this token is a keyword. */
23274 keyword
= token
->keyword
;
23278 case RID_PROTECTED
:
23280 /* Consume the access-specifier. */
23281 cp_lexer_consume_token (parser
->lexer
);
23282 /* Remember which access-specifier is active. */
23283 current_access_specifier
= token
->u
.value
;
23284 /* Look for the `:'. */
23285 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23289 /* Accept #pragmas at class scope. */
23290 if (token
->type
== CPP_PRAGMA
)
23292 cp_parser_pragma (parser
, pragma_member
, NULL
);
23296 /* Otherwise, the next construction must be a
23297 member-declaration. */
23298 cp_parser_member_declaration (parser
);
23303 /* Parse a member-declaration.
23305 member-declaration:
23306 decl-specifier-seq [opt] member-declarator-list [opt] ;
23307 function-definition ; [opt]
23308 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23310 template-declaration
23313 member-declarator-list:
23315 member-declarator-list , member-declarator
23318 declarator pure-specifier [opt]
23319 declarator constant-initializer [opt]
23320 identifier [opt] : constant-expression
23324 member-declaration:
23325 __extension__ member-declaration
23328 declarator attributes [opt] pure-specifier [opt]
23329 declarator attributes [opt] constant-initializer [opt]
23330 identifier [opt] attributes [opt] : constant-expression
23334 member-declaration:
23335 static_assert-declaration */
23338 cp_parser_member_declaration (cp_parser
* parser
)
23340 cp_decl_specifier_seq decl_specifiers
;
23341 tree prefix_attributes
;
23343 int declares_class_or_enum
;
23345 cp_token
*token
= NULL
;
23346 cp_token
*decl_spec_token_start
= NULL
;
23347 cp_token
*initializer_token_start
= NULL
;
23348 int saved_pedantic
;
23349 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23351 /* Check for the `__extension__' keyword. */
23352 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23355 cp_parser_member_declaration (parser
);
23356 /* Restore the old value of the PEDANTIC flag. */
23357 pedantic
= saved_pedantic
;
23362 /* Check for a template-declaration. */
23363 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23365 /* An explicit specialization here is an error condition, and we
23366 expect the specialization handler to detect and report this. */
23367 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23368 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23369 cp_parser_explicit_specialization (parser
);
23371 cp_parser_template_declaration (parser
, /*member_p=*/true);
23375 /* Check for a template introduction. */
23376 else if (cp_parser_template_declaration_after_export (parser
, true))
23379 /* Check for a using-declaration. */
23380 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23382 if (cxx_dialect
< cxx11
)
23384 /* Parse the using-declaration. */
23385 cp_parser_using_declaration (parser
,
23386 /*access_declaration_p=*/false);
23392 bool alias_decl_expected
;
23393 cp_parser_parse_tentatively (parser
);
23394 decl
= cp_parser_alias_declaration (parser
);
23395 /* Note that if we actually see the '=' token after the
23396 identifier, cp_parser_alias_declaration commits the
23397 tentative parse. In that case, we really expect an
23398 alias-declaration. Otherwise, we expect a using
23400 alias_decl_expected
=
23401 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23402 cp_parser_parse_definitely (parser
);
23404 if (alias_decl_expected
)
23405 finish_member_declaration (decl
);
23407 cp_parser_using_declaration (parser
,
23408 /*access_declaration_p=*/false);
23413 /* Check for @defs. */
23414 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23417 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23418 ivar
= ivar_chains
;
23422 ivar
= TREE_CHAIN (member
);
23423 TREE_CHAIN (member
) = NULL_TREE
;
23424 finish_member_declaration (member
);
23429 /* If the next token is `static_assert' we have a static assertion. */
23430 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23432 cp_parser_static_assert (parser
, /*member_p=*/true);
23436 parser
->colon_corrects_to_scope_p
= false;
23438 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23441 /* Parse the decl-specifier-seq. */
23442 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23443 cp_parser_decl_specifier_seq (parser
,
23444 CP_PARSER_FLAGS_OPTIONAL
,
23446 &declares_class_or_enum
);
23447 /* Check for an invalid type-name. */
23448 if (!decl_specifiers
.any_type_specifiers_p
23449 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23451 /* If there is no declarator, then the decl-specifier-seq should
23453 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23455 /* If there was no decl-specifier-seq, and the next token is a
23456 `;', then we have something like:
23462 Each member-declaration shall declare at least one member
23463 name of the class. */
23464 if (!decl_specifiers
.any_specifiers_p
)
23466 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23467 if (!in_system_header_at (token
->location
))
23469 gcc_rich_location
richloc (token
->location
);
23470 richloc
.add_fixit_remove ();
23471 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23478 /* See if this declaration is a friend. */
23479 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23480 /* If there were decl-specifiers, check to see if there was
23481 a class-declaration. */
23482 type
= check_tag_decl (&decl_specifiers
,
23483 /*explicit_type_instantiation_p=*/false);
23484 /* Nested classes have already been added to the class, but
23485 a `friend' needs to be explicitly registered. */
23488 /* If the `friend' keyword was present, the friend must
23489 be introduced with a class-key. */
23490 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23491 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23492 "in C++03 a class-key must be used "
23493 "when declaring a friend");
23496 template <typename T> struct A {
23497 friend struct A<T>::B;
23500 A<T>::B will be represented by a TYPENAME_TYPE, and
23501 therefore not recognized by check_tag_decl. */
23504 type
= decl_specifiers
.type
;
23505 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23506 type
= TREE_TYPE (type
);
23508 if (!type
|| !TYPE_P (type
))
23509 error_at (decl_spec_token_start
->location
,
23510 "friend declaration does not name a class or "
23513 make_friend_class (current_class_type
, type
,
23514 /*complain=*/true);
23516 /* If there is no TYPE, an error message will already have
23518 else if (!type
|| type
== error_mark_node
)
23520 /* An anonymous aggregate has to be handled specially; such
23521 a declaration really declares a data member (with a
23522 particular type), as opposed to a nested class. */
23523 else if (ANON_AGGR_TYPE_P (type
))
23526 if (decl_specifiers
.storage_class
!= sc_none
)
23527 error_at (decl_spec_token_start
->location
,
23528 "a storage class on an anonymous aggregate "
23529 "in class scope is not allowed");
23531 /* Remove constructors and such from TYPE, now that we
23532 know it is an anonymous aggregate. */
23533 fixup_anonymous_aggr (type
);
23534 /* And make the corresponding data member. */
23535 decl
= build_decl (decl_spec_token_start
->location
,
23536 FIELD_DECL
, NULL_TREE
, type
);
23537 /* Add it to the class. */
23538 finish_member_declaration (decl
);
23541 cp_parser_check_access_in_redeclaration
23543 decl_spec_token_start
->location
);
23548 bool assume_semicolon
= false;
23550 /* Clear attributes from the decl_specifiers but keep them
23551 around as prefix attributes that apply them to the entity
23553 prefix_attributes
= decl_specifiers
.attributes
;
23554 decl_specifiers
.attributes
= NULL_TREE
;
23556 /* See if these declarations will be friends. */
23557 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23559 /* Keep going until we hit the `;' at the end of the
23561 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23563 tree attributes
= NULL_TREE
;
23564 tree first_attribute
;
23566 bool is_bitfld
= false;
23567 bool named_bitfld
= false;
23569 /* Peek at the next token. */
23570 token
= cp_lexer_peek_token (parser
->lexer
);
23572 /* The following code wants to know early if it is a bit-field
23573 or some other declaration. Attributes can appear before
23574 the `:' token, but are hopefully rare enough that the
23575 simplicity of the tentative lookup pays off. */
23576 if (cp_next_tokens_can_be_attribute_p (parser
)
23577 || (token
->type
== CPP_NAME
23578 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23579 && (named_bitfld
= true)))
23581 cp_parser_parse_tentatively (parser
);
23583 cp_lexer_consume_token (parser
->lexer
);
23584 cp_parser_attributes_opt (parser
);
23585 token
= cp_lexer_peek_token (parser
->lexer
);
23586 is_bitfld
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
23587 cp_parser_abort_tentative_parse (parser
);
23590 /* Check for a bitfield declaration. */
23592 || token
->type
== CPP_COLON
23593 || (token
->type
== CPP_NAME
23594 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23595 && (named_bitfld
= true)))
23599 tree late_attributes
= NULL_TREE
;
23602 identifier
= cp_parser_identifier (parser
);
23604 identifier
= NULL_TREE
;
23606 /* Look for attributes that apply to the bitfield. */
23607 attributes
= cp_parser_attributes_opt (parser
);
23609 /* Consume the `:' token. */
23610 cp_lexer_consume_token (parser
->lexer
);
23612 /* Get the width of the bitfield. */
23613 width
= cp_parser_constant_expression (parser
, false, NULL
,
23614 cxx_dialect
>= cxx11
);
23616 /* In C++2A and as extension for C++11 and above we allow
23617 default member initializers for bit-fields. */
23618 initializer
= NULL_TREE
;
23619 if (cxx_dialect
>= cxx11
23620 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23621 || cp_lexer_next_token_is (parser
->lexer
,
23625 = cp_lexer_peek_token (parser
->lexer
)->location
;
23626 if (cxx_dialect
< cxx2a
23627 && !in_system_header_at (loc
)
23628 && identifier
!= NULL_TREE
)
23630 "default member initializers for bit-fields "
23631 "only available with -std=c++2a or "
23634 initializer
= cp_parser_save_nsdmi (parser
);
23635 if (identifier
== NULL_TREE
)
23637 error_at (loc
, "default member initializer for "
23638 "unnamed bit-field");
23639 initializer
= NULL_TREE
;
23644 /* Look for attributes that apply to the bitfield after
23645 the `:' token and width. This is where GCC used to
23646 parse attributes in the past, pedwarn if there is
23647 a std attribute. */
23648 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23649 pedwarn (input_location
, OPT_Wpedantic
,
23650 "ISO C++ allows bit-field attributes only "
23651 "before the %<:%> token");
23653 late_attributes
= cp_parser_attributes_opt (parser
);
23656 attributes
= chainon (attributes
, late_attributes
);
23658 /* Remember which attributes are prefix attributes and
23660 first_attribute
= attributes
;
23661 /* Combine the attributes. */
23662 attributes
= chainon (prefix_attributes
, attributes
);
23664 /* Create the bitfield declaration. */
23665 decl
= grokbitfield (identifier
23666 ? make_id_declarator (NULL_TREE
,
23671 width
, initializer
,
23676 cp_declarator
*declarator
;
23677 tree asm_specification
;
23678 int ctor_dtor_or_conv_p
;
23680 /* Parse the declarator. */
23682 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23683 &ctor_dtor_or_conv_p
,
23684 /*parenthesized_p=*/NULL
,
23688 /* If something went wrong parsing the declarator, make sure
23689 that we at least consume some tokens. */
23690 if (declarator
== cp_error_declarator
)
23692 /* Skip to the end of the statement. */
23693 cp_parser_skip_to_end_of_statement (parser
);
23694 /* If the next token is not a semicolon, that is
23695 probably because we just skipped over the body of
23696 a function. So, we consume a semicolon if
23697 present, but do not issue an error message if it
23699 if (cp_lexer_next_token_is (parser
->lexer
,
23701 cp_lexer_consume_token (parser
->lexer
);
23705 if (declares_class_or_enum
& 2)
23706 cp_parser_check_for_definition_in_return_type
23707 (declarator
, decl_specifiers
.type
,
23708 decl_specifiers
.locations
[ds_type_spec
]);
23710 /* Look for an asm-specification. */
23711 asm_specification
= cp_parser_asm_specification_opt (parser
);
23712 /* Look for attributes that apply to the declaration. */
23713 attributes
= cp_parser_attributes_opt (parser
);
23714 /* Remember which attributes are prefix attributes and
23716 first_attribute
= attributes
;
23717 /* Combine the attributes. */
23718 attributes
= chainon (prefix_attributes
, attributes
);
23720 /* If it's an `=', then we have a constant-initializer or a
23721 pure-specifier. It is not correct to parse the
23722 initializer before registering the member declaration
23723 since the member declaration should be in scope while
23724 its initializer is processed. However, the rest of the
23725 front end does not yet provide an interface that allows
23726 us to handle this correctly. */
23727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23731 A pure-specifier shall be used only in the declaration of
23732 a virtual function.
23734 A member-declarator can contain a constant-initializer
23735 only if it declares a static member of integral or
23738 Therefore, if the DECLARATOR is for a function, we look
23739 for a pure-specifier; otherwise, we look for a
23740 constant-initializer. When we call `grokfield', it will
23741 perform more stringent semantics checks. */
23742 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23743 if (function_declarator_p (declarator
)
23744 || (decl_specifiers
.type
23745 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23746 && declarator
->kind
== cdk_id
23747 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23748 == FUNCTION_TYPE
)))
23749 initializer
= cp_parser_pure_specifier (parser
);
23750 else if (decl_specifiers
.storage_class
!= sc_static
)
23751 initializer
= cp_parser_save_nsdmi (parser
);
23752 else if (cxx_dialect
>= cxx11
)
23755 /* Don't require a constant rvalue in C++11, since we
23756 might want a reference constant. We'll enforce
23757 constancy later. */
23758 cp_lexer_consume_token (parser
->lexer
);
23759 /* Parse the initializer. */
23760 initializer
= cp_parser_initializer_clause (parser
,
23764 /* Parse the initializer. */
23765 initializer
= cp_parser_constant_initializer (parser
);
23767 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23768 && !function_declarator_p (declarator
))
23771 if (decl_specifiers
.storage_class
!= sc_static
)
23772 initializer
= cp_parser_save_nsdmi (parser
);
23774 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23776 /* Otherwise, there is no initializer. */
23778 initializer
= NULL_TREE
;
23780 /* See if we are probably looking at a function
23781 definition. We are certainly not looking at a
23782 member-declarator. Calling `grokfield' has
23783 side-effects, so we must not do it unless we are sure
23784 that we are looking at a member-declarator. */
23785 if (cp_parser_token_starts_function_definition_p
23786 (cp_lexer_peek_token (parser
->lexer
)))
23788 /* The grammar does not allow a pure-specifier to be
23789 used when a member function is defined. (It is
23790 possible that this fact is an oversight in the
23791 standard, since a pure function may be defined
23792 outside of the class-specifier. */
23793 if (initializer
&& initializer_token_start
)
23794 error_at (initializer_token_start
->location
,
23795 "pure-specifier on function-definition");
23796 decl
= cp_parser_save_member_function_body (parser
,
23800 if (parser
->fully_implicit_function_template_p
)
23801 decl
= finish_fully_implicit_template (parser
, decl
);
23802 /* If the member was not a friend, declare it here. */
23804 finish_member_declaration (decl
);
23805 /* Peek at the next token. */
23806 token
= cp_lexer_peek_token (parser
->lexer
);
23807 /* If the next token is a semicolon, consume it. */
23808 if (token
->type
== CPP_SEMICOLON
)
23810 location_t semicolon_loc
23811 = cp_lexer_consume_token (parser
->lexer
)->location
;
23812 gcc_rich_location
richloc (semicolon_loc
);
23813 richloc
.add_fixit_remove ();
23814 warning_at (&richloc
, OPT_Wextra_semi
,
23815 "extra %<;%> after in-class "
23816 "function definition");
23821 if (declarator
->kind
== cdk_function
)
23822 declarator
->id_loc
= token
->location
;
23823 /* Create the declaration. */
23824 decl
= grokfield (declarator
, &decl_specifiers
,
23825 initializer
, /*init_const_expr_p=*/true,
23826 asm_specification
, attributes
);
23827 if (parser
->fully_implicit_function_template_p
)
23830 finish_fully_implicit_template (parser
, 0);
23832 decl
= finish_fully_implicit_template (parser
, decl
);
23836 cp_finalize_omp_declare_simd (parser
, decl
);
23837 cp_finalize_oacc_routine (parser
, decl
, false);
23839 /* Reset PREFIX_ATTRIBUTES. */
23840 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23841 attributes
= TREE_CHAIN (attributes
);
23843 TREE_CHAIN (attributes
) = NULL_TREE
;
23845 /* If there is any qualification still in effect, clear it
23846 now; we will be starting fresh with the next declarator. */
23847 parser
->scope
= NULL_TREE
;
23848 parser
->qualifying_scope
= NULL_TREE
;
23849 parser
->object_scope
= NULL_TREE
;
23850 /* If it's a `,', then there are more declarators. */
23851 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23853 cp_lexer_consume_token (parser
->lexer
);
23854 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23856 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23857 gcc_rich_location
richloc (token
->location
);
23858 richloc
.add_fixit_remove ();
23859 error_at (&richloc
, "stray %<,%> at end of "
23860 "member declaration");
23863 /* If the next token isn't a `;', then we have a parse error. */
23864 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23867 /* The next token might be a ways away from where the
23868 actual semicolon is missing. Find the previous token
23869 and use that for our error position. */
23870 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23871 gcc_rich_location
richloc (token
->location
);
23872 richloc
.add_fixit_insert_after (";");
23873 error_at (&richloc
, "expected %<;%> at end of "
23874 "member declaration");
23876 /* Assume that the user meant to provide a semicolon. If
23877 we were to cp_parser_skip_to_end_of_statement, we might
23878 skip to a semicolon inside a member function definition
23879 and issue nonsensical error messages. */
23880 assume_semicolon
= true;
23885 /* Add DECL to the list of members. */
23887 /* Explicitly include, eg, NSDMIs, for better error
23888 recovery (c++/58650). */
23889 || !DECL_DECLARES_FUNCTION_P (decl
))
23890 finish_member_declaration (decl
);
23892 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23893 cp_parser_save_default_args (parser
, decl
);
23894 else if (TREE_CODE (decl
) == FIELD_DECL
23895 && DECL_INITIAL (decl
))
23896 /* Add DECL to the queue of NSDMI to be parsed later. */
23897 vec_safe_push (unparsed_nsdmis
, decl
);
23900 if (assume_semicolon
)
23905 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23907 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23910 /* Parse a pure-specifier.
23915 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23916 Otherwise, ERROR_MARK_NODE is returned. */
23919 cp_parser_pure_specifier (cp_parser
* parser
)
23923 /* Look for the `=' token. */
23924 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23925 return error_mark_node
;
23926 /* Look for the `0' token. */
23927 token
= cp_lexer_peek_token (parser
->lexer
);
23929 if (token
->type
== CPP_EOF
23930 || token
->type
== CPP_PRAGMA_EOL
)
23931 return error_mark_node
;
23933 cp_lexer_consume_token (parser
->lexer
);
23935 /* Accept = default or = delete in c++0x mode. */
23936 if (token
->keyword
== RID_DEFAULT
23937 || token
->keyword
== RID_DELETE
)
23939 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
23940 return token
->u
.value
;
23943 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23944 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
23946 cp_parser_error (parser
,
23947 "invalid pure specifier (only %<= 0%> is allowed)");
23948 cp_parser_skip_to_end_of_statement (parser
);
23949 return error_mark_node
;
23951 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23953 error_at (token
->location
, "templates may not be %<virtual%>");
23954 return error_mark_node
;
23957 return integer_zero_node
;
23960 /* Parse a constant-initializer.
23962 constant-initializer:
23963 = constant-expression
23965 Returns a representation of the constant-expression. */
23968 cp_parser_constant_initializer (cp_parser
* parser
)
23970 /* Look for the `=' token. */
23971 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23972 return error_mark_node
;
23974 /* It is invalid to write:
23976 struct S { static const int i = { 7 }; };
23979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23981 cp_parser_error (parser
,
23982 "a brace-enclosed initializer is not allowed here");
23983 /* Consume the opening brace. */
23984 matching_braces braces
;
23985 braces
.consume_open (parser
);
23986 /* Skip the initializer. */
23987 cp_parser_skip_to_closing_brace (parser
);
23988 /* Look for the trailing `}'. */
23989 braces
.require_close (parser
);
23991 return error_mark_node
;
23994 return cp_parser_constant_expression (parser
);
23997 /* Derived classes [gram.class.derived] */
23999 /* Parse a base-clause.
24002 : base-specifier-list
24004 base-specifier-list:
24005 base-specifier ... [opt]
24006 base-specifier-list , base-specifier ... [opt]
24008 Returns a TREE_LIST representing the base-classes, in the order in
24009 which they were declared. The representation of each node is as
24010 described by cp_parser_base_specifier.
24012 In the case that no bases are specified, this function will return
24013 NULL_TREE, not ERROR_MARK_NODE. */
24016 cp_parser_base_clause (cp_parser
* parser
)
24018 tree bases
= NULL_TREE
;
24020 /* Look for the `:' that begins the list. */
24021 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24023 /* Scan the base-specifier-list. */
24028 bool pack_expansion_p
= false;
24030 /* Look for the base-specifier. */
24031 base
= cp_parser_base_specifier (parser
);
24032 /* Look for the (optional) ellipsis. */
24033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24035 /* Consume the `...'. */
24036 cp_lexer_consume_token (parser
->lexer
);
24038 pack_expansion_p
= true;
24041 /* Add BASE to the front of the list. */
24042 if (base
&& base
!= error_mark_node
)
24044 if (pack_expansion_p
)
24045 /* Make this a pack expansion type. */
24046 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24048 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24050 TREE_CHAIN (base
) = bases
;
24054 /* Peek at the next token. */
24055 token
= cp_lexer_peek_token (parser
->lexer
);
24056 /* If it's not a comma, then the list is complete. */
24057 if (token
->type
!= CPP_COMMA
)
24059 /* Consume the `,'. */
24060 cp_lexer_consume_token (parser
->lexer
);
24063 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24064 base class had a qualified name. However, the next name that
24065 appears is certainly not qualified. */
24066 parser
->scope
= NULL_TREE
;
24067 parser
->qualifying_scope
= NULL_TREE
;
24068 parser
->object_scope
= NULL_TREE
;
24070 return nreverse (bases
);
24073 /* Parse a base-specifier.
24076 :: [opt] nested-name-specifier [opt] class-name
24077 virtual access-specifier [opt] :: [opt] nested-name-specifier
24079 access-specifier virtual [opt] :: [opt] nested-name-specifier
24082 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24083 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24084 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24085 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24088 cp_parser_base_specifier (cp_parser
* parser
)
24092 bool virtual_p
= false;
24093 bool duplicate_virtual_error_issued_p
= false;
24094 bool duplicate_access_error_issued_p
= false;
24095 bool class_scope_p
, template_p
;
24096 tree access
= access_default_node
;
24099 /* Process the optional `virtual' and `access-specifier'. */
24102 /* Peek at the next token. */
24103 token
= cp_lexer_peek_token (parser
->lexer
);
24104 /* Process `virtual'. */
24105 switch (token
->keyword
)
24108 /* If `virtual' appears more than once, issue an error. */
24109 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24111 cp_parser_error (parser
,
24112 "%<virtual%> specified more than once in base-specifier");
24113 duplicate_virtual_error_issued_p
= true;
24118 /* Consume the `virtual' token. */
24119 cp_lexer_consume_token (parser
->lexer
);
24124 case RID_PROTECTED
:
24126 /* If more than one access specifier appears, issue an
24128 if (access
!= access_default_node
24129 && !duplicate_access_error_issued_p
)
24131 cp_parser_error (parser
,
24132 "more than one access specifier in base-specifier");
24133 duplicate_access_error_issued_p
= true;
24136 access
= ridpointers
[(int) token
->keyword
];
24138 /* Consume the access-specifier. */
24139 cp_lexer_consume_token (parser
->lexer
);
24148 /* It is not uncommon to see programs mechanically, erroneously, use
24149 the 'typename' keyword to denote (dependent) qualified types
24150 as base classes. */
24151 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24153 token
= cp_lexer_peek_token (parser
->lexer
);
24154 if (!processing_template_decl
)
24155 error_at (token
->location
,
24156 "keyword %<typename%> not allowed outside of templates");
24158 error_at (token
->location
,
24159 "keyword %<typename%> not allowed in this context "
24160 "(the base class is implicitly a type)");
24161 cp_lexer_consume_token (parser
->lexer
);
24164 /* Look for the optional `::' operator. */
24165 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24166 /* Look for the nested-name-specifier. The simplest way to
24171 The keyword `typename' is not permitted in a base-specifier or
24172 mem-initializer; in these contexts a qualified name that
24173 depends on a template-parameter is implicitly assumed to be a
24176 is to pretend that we have seen the `typename' keyword at this
24178 cp_parser_nested_name_specifier_opt (parser
,
24179 /*typename_keyword_p=*/true,
24180 /*check_dependency_p=*/true,
24182 /*is_declaration=*/true);
24183 /* If the base class is given by a qualified name, assume that names
24184 we see are type names or templates, as appropriate. */
24185 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24186 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24189 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24190 /* DR 950 allows decltype as a base-specifier. */
24191 type
= cp_parser_decltype (parser
);
24194 /* Otherwise, look for the class-name. */
24195 type
= cp_parser_class_name (parser
,
24199 /*check_dependency_p=*/true,
24200 /*class_head_p=*/false,
24201 /*is_declaration=*/true);
24202 type
= TREE_TYPE (type
);
24205 if (type
== error_mark_node
)
24206 return error_mark_node
;
24208 return finish_base_specifier (type
, access
, virtual_p
);
24211 /* Exception handling [gram.exception] */
24213 /* Parse an (optional) noexcept-specification.
24215 noexcept-specification:
24216 noexcept ( constant-expression ) [opt]
24218 If no noexcept-specification is present, returns NULL_TREE.
24219 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24220 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24221 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24222 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24223 in which case a boolean condition is returned instead. */
24226 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24227 bool require_constexpr
,
24228 bool* consumed_expr
,
24232 const char *saved_message
;
24234 /* Peek at the next token. */
24235 token
= cp_lexer_peek_token (parser
->lexer
);
24237 /* Is it a noexcept-specification? */
24238 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24241 cp_lexer_consume_token (parser
->lexer
);
24243 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24245 matching_parens parens
;
24246 parens
.consume_open (parser
);
24248 if (require_constexpr
)
24250 /* Types may not be defined in an exception-specification. */
24251 saved_message
= parser
->type_definition_forbidden_message
;
24252 parser
->type_definition_forbidden_message
24253 = G_("types may not be defined in an exception-specification");
24255 expr
= cp_parser_constant_expression (parser
);
24257 /* Restore the saved message. */
24258 parser
->type_definition_forbidden_message
= saved_message
;
24262 expr
= cp_parser_expression (parser
);
24263 *consumed_expr
= true;
24266 parens
.require_close (parser
);
24270 expr
= boolean_true_node
;
24271 if (!require_constexpr
)
24272 *consumed_expr
= false;
24275 /* We cannot build a noexcept-spec right away because this will check
24276 that expr is a constexpr. */
24278 return build_noexcept_spec (expr
, tf_warning_or_error
);
24286 /* Parse an (optional) exception-specification.
24288 exception-specification:
24289 throw ( type-id-list [opt] )
24291 Returns a TREE_LIST representing the exception-specification. The
24292 TREE_VALUE of each node is a type. */
24295 cp_parser_exception_specification_opt (cp_parser
* parser
)
24299 const char *saved_message
;
24301 /* Peek at the next token. */
24302 token
= cp_lexer_peek_token (parser
->lexer
);
24304 /* Is it a noexcept-specification? */
24305 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24307 if (type_id_list
!= NULL_TREE
)
24308 return type_id_list
;
24310 /* If it's not `throw', then there's no exception-specification. */
24311 if (!cp_parser_is_keyword (token
, RID_THROW
))
24314 location_t loc
= token
->location
;
24316 /* Consume the `throw'. */
24317 cp_lexer_consume_token (parser
->lexer
);
24319 /* Look for the `('. */
24320 matching_parens parens
;
24321 parens
.require_open (parser
);
24323 /* Peek at the next token. */
24324 token
= cp_lexer_peek_token (parser
->lexer
);
24325 /* If it's not a `)', then there is a type-id-list. */
24326 if (token
->type
!= CPP_CLOSE_PAREN
)
24328 /* Types may not be defined in an exception-specification. */
24329 saved_message
= parser
->type_definition_forbidden_message
;
24330 parser
->type_definition_forbidden_message
24331 = G_("types may not be defined in an exception-specification");
24332 /* Parse the type-id-list. */
24333 type_id_list
= cp_parser_type_id_list (parser
);
24334 /* Restore the saved message. */
24335 parser
->type_definition_forbidden_message
= saved_message
;
24337 if (cxx_dialect
>= cxx17
)
24339 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24341 type_id_list
= NULL_TREE
;
24343 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24344 warning_at (loc
, OPT_Wdeprecated
,
24345 "dynamic exception specifications are deprecated in "
24348 /* In C++17, throw() is equivalent to noexcept (true). throw()
24349 is deprecated in C++11 and above as well, but is still widely used,
24350 so don't warn about it yet. */
24351 else if (cxx_dialect
>= cxx17
)
24352 type_id_list
= noexcept_true_spec
;
24354 type_id_list
= empty_except_spec
;
24356 /* Look for the `)'. */
24357 parens
.require_close (parser
);
24359 return type_id_list
;
24362 /* Parse an (optional) type-id-list.
24366 type-id-list , type-id ... [opt]
24368 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24369 in the order that the types were presented. */
24372 cp_parser_type_id_list (cp_parser
* parser
)
24374 tree types
= NULL_TREE
;
24381 token
= cp_lexer_peek_token (parser
->lexer
);
24383 /* Get the next type-id. */
24384 type
= cp_parser_type_id (parser
);
24385 /* Check for invalid 'auto'. */
24386 if (flag_concepts
&& type_uses_auto (type
))
24388 error_at (token
->location
,
24389 "invalid use of %<auto%> in exception-specification");
24390 type
= error_mark_node
;
24392 /* Parse the optional ellipsis. */
24393 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24395 /* Consume the `...'. */
24396 cp_lexer_consume_token (parser
->lexer
);
24398 /* Turn the type into a pack expansion expression. */
24399 type
= make_pack_expansion (type
);
24401 /* Add it to the list. */
24402 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24403 /* Peek at the next token. */
24404 token
= cp_lexer_peek_token (parser
->lexer
);
24405 /* If it is not a `,', we are done. */
24406 if (token
->type
!= CPP_COMMA
)
24408 /* Consume the `,'. */
24409 cp_lexer_consume_token (parser
->lexer
);
24412 return nreverse (types
);
24415 /* Parse a try-block.
24418 try compound-statement handler-seq */
24421 cp_parser_try_block (cp_parser
* parser
)
24425 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24426 if (parser
->in_function_body
24427 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24428 error ("%<try%> in %<constexpr%> function");
24430 try_block
= begin_try_block ();
24431 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24432 finish_try_block (try_block
);
24433 cp_parser_handler_seq (parser
);
24434 finish_handler_sequence (try_block
);
24439 /* Parse a function-try-block.
24441 function-try-block:
24442 try ctor-initializer [opt] function-body handler-seq */
24445 cp_parser_function_try_block (cp_parser
* parser
)
24447 tree compound_stmt
;
24450 /* Look for the `try' keyword. */
24451 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24453 /* Let the rest of the front end know where we are. */
24454 try_block
= begin_function_try_block (&compound_stmt
);
24455 /* Parse the function-body. */
24456 cp_parser_ctor_initializer_opt_and_function_body
24457 (parser
, /*in_function_try_block=*/true);
24458 /* We're done with the `try' part. */
24459 finish_function_try_block (try_block
);
24460 /* Parse the handlers. */
24461 cp_parser_handler_seq (parser
);
24462 /* We're done with the handlers. */
24463 finish_function_handler_sequence (try_block
, compound_stmt
);
24466 /* Parse a handler-seq.
24469 handler handler-seq [opt] */
24472 cp_parser_handler_seq (cp_parser
* parser
)
24478 /* Parse the handler. */
24479 cp_parser_handler (parser
);
24480 /* Peek at the next token. */
24481 token
= cp_lexer_peek_token (parser
->lexer
);
24482 /* If it's not `catch' then there are no more handlers. */
24483 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24488 /* Parse a handler.
24491 catch ( exception-declaration ) compound-statement */
24494 cp_parser_handler (cp_parser
* parser
)
24499 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24500 handler
= begin_handler ();
24501 matching_parens parens
;
24502 parens
.require_open (parser
);
24503 declaration
= cp_parser_exception_declaration (parser
);
24504 finish_handler_parms (declaration
, handler
);
24505 parens
.require_close (parser
);
24506 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24507 finish_handler (handler
);
24510 /* Parse an exception-declaration.
24512 exception-declaration:
24513 type-specifier-seq declarator
24514 type-specifier-seq abstract-declarator
24518 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24519 ellipsis variant is used. */
24522 cp_parser_exception_declaration (cp_parser
* parser
)
24524 cp_decl_specifier_seq type_specifiers
;
24525 cp_declarator
*declarator
;
24526 const char *saved_message
;
24528 /* If it's an ellipsis, it's easy to handle. */
24529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24531 /* Consume the `...' token. */
24532 cp_lexer_consume_token (parser
->lexer
);
24536 /* Types may not be defined in exception-declarations. */
24537 saved_message
= parser
->type_definition_forbidden_message
;
24538 parser
->type_definition_forbidden_message
24539 = G_("types may not be defined in exception-declarations");
24541 /* Parse the type-specifier-seq. */
24542 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24543 /*is_trailing_return=*/false,
24545 /* If it's a `)', then there is no declarator. */
24546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24549 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24550 /*ctor_dtor_or_conv_p=*/NULL
,
24551 /*parenthesized_p=*/NULL
,
24552 /*member_p=*/false,
24553 /*friend_p=*/false);
24555 /* Restore the saved message. */
24556 parser
->type_definition_forbidden_message
= saved_message
;
24558 if (!type_specifiers
.any_specifiers_p
)
24559 return error_mark_node
;
24561 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24564 /* Parse a throw-expression.
24567 throw assignment-expression [opt]
24569 Returns a THROW_EXPR representing the throw-expression. */
24572 cp_parser_throw_expression (cp_parser
* parser
)
24577 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24578 token
= cp_lexer_peek_token (parser
->lexer
);
24579 /* Figure out whether or not there is an assignment-expression
24580 following the "throw" keyword. */
24581 if (token
->type
== CPP_COMMA
24582 || token
->type
== CPP_SEMICOLON
24583 || token
->type
== CPP_CLOSE_PAREN
24584 || token
->type
== CPP_CLOSE_SQUARE
24585 || token
->type
== CPP_CLOSE_BRACE
24586 || token
->type
== CPP_COLON
)
24587 expression
= NULL_TREE
;
24589 expression
= cp_parser_assignment_expression (parser
);
24591 return build_throw (expression
);
24594 /* GNU Extensions */
24596 /* Parse an (optional) asm-specification.
24599 asm ( string-literal )
24601 If the asm-specification is present, returns a STRING_CST
24602 corresponding to the string-literal. Otherwise, returns
24606 cp_parser_asm_specification_opt (cp_parser
* parser
)
24609 tree asm_specification
;
24611 /* Peek at the next token. */
24612 token
= cp_lexer_peek_token (parser
->lexer
);
24613 /* If the next token isn't the `asm' keyword, then there's no
24614 asm-specification. */
24615 if (!cp_parser_is_keyword (token
, RID_ASM
))
24618 /* Consume the `asm' token. */
24619 cp_lexer_consume_token (parser
->lexer
);
24620 /* Look for the `('. */
24621 matching_parens parens
;
24622 parens
.require_open (parser
);
24624 /* Look for the string-literal. */
24625 asm_specification
= cp_parser_string_literal (parser
, false, false);
24627 /* Look for the `)'. */
24628 parens
.require_close (parser
);
24630 return asm_specification
;
24633 /* Parse an asm-operand-list.
24637 asm-operand-list , asm-operand
24640 string-literal ( expression )
24641 [ string-literal ] string-literal ( expression )
24643 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24644 each node is the expression. The TREE_PURPOSE is itself a
24645 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24646 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24647 is a STRING_CST for the string literal before the parenthesis. Returns
24648 ERROR_MARK_NODE if any of the operands are invalid. */
24651 cp_parser_asm_operand_list (cp_parser
* parser
)
24653 tree asm_operands
= NULL_TREE
;
24654 bool invalid_operands
= false;
24658 tree string_literal
;
24662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24664 /* Consume the `[' token. */
24665 cp_lexer_consume_token (parser
->lexer
);
24666 /* Read the operand name. */
24667 name
= cp_parser_identifier (parser
);
24668 if (name
!= error_mark_node
)
24669 name
= build_string (IDENTIFIER_LENGTH (name
),
24670 IDENTIFIER_POINTER (name
));
24671 /* Look for the closing `]'. */
24672 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24676 /* Look for the string-literal. */
24677 string_literal
= cp_parser_string_literal (parser
, false, false);
24679 /* Look for the `('. */
24680 matching_parens parens
;
24681 parens
.require_open (parser
);
24682 /* Parse the expression. */
24683 expression
= cp_parser_expression (parser
);
24684 /* Look for the `)'. */
24685 parens
.require_close (parser
);
24687 if (name
== error_mark_node
24688 || string_literal
== error_mark_node
24689 || expression
== error_mark_node
)
24690 invalid_operands
= true;
24692 /* Add this operand to the list. */
24693 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24696 /* If the next token is not a `,', there are no more
24698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24700 /* Consume the `,'. */
24701 cp_lexer_consume_token (parser
->lexer
);
24704 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24707 /* Parse an asm-clobber-list.
24711 asm-clobber-list , string-literal
24713 Returns a TREE_LIST, indicating the clobbers in the order that they
24714 appeared. The TREE_VALUE of each node is a STRING_CST. */
24717 cp_parser_asm_clobber_list (cp_parser
* parser
)
24719 tree clobbers
= NULL_TREE
;
24723 tree string_literal
;
24725 /* Look for the string literal. */
24726 string_literal
= cp_parser_string_literal (parser
, false, false);
24727 /* Add it to the list. */
24728 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24729 /* If the next token is not a `,', then the list is
24731 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24733 /* Consume the `,' token. */
24734 cp_lexer_consume_token (parser
->lexer
);
24740 /* Parse an asm-label-list.
24744 asm-label-list , identifier
24746 Returns a TREE_LIST, indicating the labels in the order that they
24747 appeared. The TREE_VALUE of each node is a label. */
24750 cp_parser_asm_label_list (cp_parser
* parser
)
24752 tree labels
= NULL_TREE
;
24756 tree identifier
, label
, name
;
24758 /* Look for the identifier. */
24759 identifier
= cp_parser_identifier (parser
);
24760 if (!error_operand_p (identifier
))
24762 label
= lookup_label (identifier
);
24763 if (TREE_CODE (label
) == LABEL_DECL
)
24765 TREE_USED (label
) = 1;
24766 check_goto (label
);
24767 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24768 IDENTIFIER_POINTER (identifier
));
24769 labels
= tree_cons (name
, label
, labels
);
24772 /* If the next token is not a `,', then the list is
24774 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24776 /* Consume the `,' token. */
24777 cp_lexer_consume_token (parser
->lexer
);
24780 return nreverse (labels
);
24783 /* Return TRUE iff the next tokens in the stream are possibly the
24784 beginning of a GNU extension attribute. */
24787 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24789 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24792 /* Return TRUE iff the next tokens in the stream are possibly the
24793 beginning of a standard C++-11 attribute specifier. */
24796 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24798 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24801 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24802 beginning of a standard C++-11 attribute specifier. */
24805 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24807 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24809 return (cxx_dialect
>= cxx11
24810 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24811 || (token
->type
== CPP_OPEN_SQUARE
24812 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24813 && token
->type
== CPP_OPEN_SQUARE
)));
24816 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24817 beginning of a GNU extension attribute. */
24820 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24822 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24824 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24827 /* Return true iff the next tokens can be the beginning of either a
24828 GNU attribute list, or a standard C++11 attribute sequence. */
24831 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24833 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24834 || cp_next_tokens_can_be_std_attribute_p (parser
));
24837 /* Return true iff the next Nth tokens can be the beginning of either
24838 a GNU attribute list, or a standard C++11 attribute sequence. */
24841 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24843 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24844 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24847 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24848 of GNU attributes, or return NULL. */
24851 cp_parser_attributes_opt (cp_parser
*parser
)
24853 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24854 return cp_parser_gnu_attributes_opt (parser
);
24855 return cp_parser_std_attribute_spec_seq (parser
);
24858 /* Parse an (optional) series of attributes.
24861 attributes attribute
24864 __attribute__ (( attribute-list [opt] ))
24866 The return value is as for cp_parser_gnu_attribute_list. */
24869 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24871 tree attributes
= NULL_TREE
;
24876 tree attribute_list
;
24879 /* Peek at the next token. */
24880 token
= cp_lexer_peek_token (parser
->lexer
);
24881 /* If it's not `__attribute__', then we're done. */
24882 if (token
->keyword
!= RID_ATTRIBUTE
)
24885 /* Consume the `__attribute__' keyword. */
24886 cp_lexer_consume_token (parser
->lexer
);
24887 /* Look for the two `(' tokens. */
24888 matching_parens outer_parens
;
24889 outer_parens
.require_open (parser
);
24890 matching_parens inner_parens
;
24891 inner_parens
.require_open (parser
);
24893 /* Peek at the next token. */
24894 token
= cp_lexer_peek_token (parser
->lexer
);
24895 if (token
->type
!= CPP_CLOSE_PAREN
)
24896 /* Parse the attribute-list. */
24897 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24899 /* If the next token is a `)', then there is no attribute
24901 attribute_list
= NULL
;
24903 /* Look for the two `)' tokens. */
24904 if (!inner_parens
.require_close (parser
))
24906 if (!outer_parens
.require_close (parser
))
24909 cp_parser_skip_to_end_of_statement (parser
);
24911 /* Add these new attributes to the list. */
24912 attributes
= chainon (attributes
, attribute_list
);
24918 /* Parse a GNU attribute-list.
24922 attribute-list , attribute
24926 identifier ( identifier )
24927 identifier ( identifier , expression-list )
24928 identifier ( expression-list )
24930 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24931 to an attribute. The TREE_PURPOSE of each node is the identifier
24932 indicating which attribute is in use. The TREE_VALUE represents
24933 the arguments, if any. */
24936 cp_parser_gnu_attribute_list (cp_parser
* parser
)
24938 tree attribute_list
= NULL_TREE
;
24939 bool save_translate_strings_p
= parser
->translate_strings_p
;
24941 parser
->translate_strings_p
= false;
24948 /* Look for the identifier. We also allow keywords here; for
24949 example `__attribute__ ((const))' is legal. */
24950 token
= cp_lexer_peek_token (parser
->lexer
);
24951 if (token
->type
== CPP_NAME
24952 || token
->type
== CPP_KEYWORD
)
24954 tree arguments
= NULL_TREE
;
24956 /* Consume the token, but save it since we need it for the
24957 SIMD enabled function parsing. */
24958 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
24960 /* Save away the identifier that indicates which attribute
24962 identifier
= (token
->type
== CPP_KEYWORD
)
24963 /* For keywords, use the canonical spelling, not the
24964 parsed identifier. */
24965 ? ridpointers
[(int) token
->keyword
]
24966 : id_token
->u
.value
;
24968 identifier
= canonicalize_attr_name (identifier
);
24969 attribute
= build_tree_list (identifier
, NULL_TREE
);
24971 /* Peek at the next token. */
24972 token
= cp_lexer_peek_token (parser
->lexer
);
24973 /* If it's an `(', then parse the attribute arguments. */
24974 if (token
->type
== CPP_OPEN_PAREN
)
24976 vec
<tree
, va_gc
> *vec
;
24977 int attr_flag
= (attribute_takes_identifier_p (identifier
)
24978 ? id_attr
: normal_attr
);
24979 vec
= cp_parser_parenthesized_expression_list
24980 (parser
, attr_flag
, /*cast_p=*/false,
24981 /*allow_expansion_p=*/false,
24982 /*non_constant_p=*/NULL
);
24984 arguments
= error_mark_node
;
24987 arguments
= build_tree_list_vec (vec
);
24988 release_tree_vector (vec
);
24990 /* Save the arguments away. */
24991 TREE_VALUE (attribute
) = arguments
;
24994 if (arguments
!= error_mark_node
)
24996 /* Add this attribute to the list. */
24997 TREE_CHAIN (attribute
) = attribute_list
;
24998 attribute_list
= attribute
;
25001 token
= cp_lexer_peek_token (parser
->lexer
);
25003 /* Now, look for more attributes. If the next token isn't a
25004 `,', we're done. */
25005 if (token
->type
!= CPP_COMMA
)
25008 /* Consume the comma and keep going. */
25009 cp_lexer_consume_token (parser
->lexer
);
25011 parser
->translate_strings_p
= save_translate_strings_p
;
25013 /* We built up the list in reverse order. */
25014 return nreverse (attribute_list
);
25017 /* Parse a standard C++11 attribute.
25019 The returned representation is a TREE_LIST which TREE_PURPOSE is
25020 the scoped name of the attribute, and the TREE_VALUE is its
25023 Note that the scoped name of the attribute is itself a TREE_LIST
25024 which TREE_PURPOSE is the namespace of the attribute, and
25025 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25026 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25027 and which TREE_PURPOSE is directly the attribute name.
25029 Clients of the attribute code should use get_attribute_namespace
25030 and get_attribute_name to get the actual namespace and name of
25031 attributes, regardless of their being GNU or C++11 attributes.
25034 attribute-token attribute-argument-clause [opt]
25038 attribute-scoped-token
25040 attribute-scoped-token:
25041 attribute-namespace :: identifier
25043 attribute-namespace:
25046 attribute-argument-clause:
25047 ( balanced-token-seq )
25049 balanced-token-seq:
25050 balanced-token [opt]
25051 balanced-token-seq balanced-token
25054 ( balanced-token-seq )
25055 [ balanced-token-seq ]
25056 { balanced-token-seq }. */
25059 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25061 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25064 /* First, parse name of the attribute, a.k.a attribute-token. */
25066 token
= cp_lexer_peek_token (parser
->lexer
);
25067 if (token
->type
== CPP_NAME
)
25068 attr_id
= token
->u
.value
;
25069 else if (token
->type
== CPP_KEYWORD
)
25070 attr_id
= ridpointers
[(int) token
->keyword
];
25071 else if (token
->flags
& NAMED_OP
)
25072 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25074 if (attr_id
== NULL_TREE
)
25077 cp_lexer_consume_token (parser
->lexer
);
25079 token
= cp_lexer_peek_token (parser
->lexer
);
25080 if (token
->type
== CPP_SCOPE
)
25082 /* We are seeing a scoped attribute token. */
25084 cp_lexer_consume_token (parser
->lexer
);
25086 error_at (token
->location
, "attribute using prefix used together "
25087 "with scoped attribute token");
25090 token
= cp_lexer_consume_token (parser
->lexer
);
25091 if (token
->type
== CPP_NAME
)
25092 attr_id
= token
->u
.value
;
25093 else if (token
->type
== CPP_KEYWORD
)
25094 attr_id
= ridpointers
[(int) token
->keyword
];
25095 else if (token
->flags
& NAMED_OP
)
25096 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25099 error_at (token
->location
,
25100 "expected an identifier for the attribute name");
25101 return error_mark_node
;
25104 attr_id
= canonicalize_attr_name (attr_id
);
25105 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25107 token
= cp_lexer_peek_token (parser
->lexer
);
25110 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25114 attr_id
= canonicalize_attr_name (attr_id
);
25115 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25117 /* C++11 noreturn attribute is equivalent to GNU's. */
25118 if (is_attribute_p ("noreturn", attr_id
))
25119 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25120 /* C++14 deprecated attribute is equivalent to GNU's. */
25121 else if (is_attribute_p ("deprecated", attr_id
))
25122 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25123 /* C++17 fallthrough attribute is equivalent to GNU's. */
25124 else if (is_attribute_p ("fallthrough", attr_id
))
25125 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25126 /* Transactional Memory TS optimize_for_synchronized attribute is
25127 equivalent to GNU transaction_callable. */
25128 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25129 TREE_PURPOSE (attribute
)
25130 = get_identifier ("transaction_callable");
25131 /* Transactional Memory attributes are GNU attributes. */
25132 else if (tm_attr_to_mask (attr_id
))
25133 TREE_PURPOSE (attribute
) = attr_id
;
25136 /* Now parse the optional argument clause of the attribute. */
25138 if (token
->type
!= CPP_OPEN_PAREN
)
25142 vec
<tree
, va_gc
> *vec
;
25143 int attr_flag
= normal_attr
;
25145 if (attr_ns
== get_identifier ("gnu")
25146 && attribute_takes_identifier_p (attr_id
))
25147 /* A GNU attribute that takes an identifier in parameter. */
25148 attr_flag
= id_attr
;
25150 vec
= cp_parser_parenthesized_expression_list
25151 (parser
, attr_flag
, /*cast_p=*/false,
25152 /*allow_expansion_p=*/true,
25153 /*non_constant_p=*/NULL
);
25155 arguments
= error_mark_node
;
25158 arguments
= build_tree_list_vec (vec
);
25159 release_tree_vector (vec
);
25162 if (arguments
== error_mark_node
)
25163 attribute
= error_mark_node
;
25165 TREE_VALUE (attribute
) = arguments
;
25171 /* Check that the attribute ATTRIBUTE appears at most once in the
25172 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25173 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25174 isn't implemented yet in GCC. */
25177 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25181 tree name
= get_attribute_name (attribute
);
25182 if (is_attribute_p ("noreturn", name
)
25183 && lookup_attribute ("noreturn", attributes
))
25184 error ("attribute %<noreturn%> can appear at most once "
25185 "in an attribute-list");
25186 else if (is_attribute_p ("deprecated", name
)
25187 && lookup_attribute ("deprecated", attributes
))
25188 error ("attribute %<deprecated%> can appear at most once "
25189 "in an attribute-list");
25193 /* Parse a list of standard C++-11 attributes.
25197 attribute-list , attribute[opt]
25199 attribute-list , attribute ...
25203 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25205 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25206 cp_token
*token
= NULL
;
25210 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25211 if (attribute
== error_mark_node
)
25213 if (attribute
!= NULL_TREE
)
25215 cp_parser_check_std_attribute (attributes
, attribute
);
25216 TREE_CHAIN (attribute
) = attributes
;
25217 attributes
= attribute
;
25219 token
= cp_lexer_peek_token (parser
->lexer
);
25220 if (token
->type
== CPP_ELLIPSIS
)
25222 cp_lexer_consume_token (parser
->lexer
);
25223 if (attribute
== NULL_TREE
)
25224 error_at (token
->location
,
25225 "expected attribute before %<...%>");
25228 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25229 if (pack
== error_mark_node
)
25230 return error_mark_node
;
25231 TREE_VALUE (attribute
) = pack
;
25233 token
= cp_lexer_peek_token (parser
->lexer
);
25235 if (token
->type
!= CPP_COMMA
)
25237 cp_lexer_consume_token (parser
->lexer
);
25239 attributes
= nreverse (attributes
);
25243 /* Parse a standard C++-11 attribute specifier.
25245 attribute-specifier:
25246 [ [ attribute-using-prefix [opt] attribute-list ] ]
25247 alignment-specifier
25249 attribute-using-prefix:
25250 using attribute-namespace :
25252 alignment-specifier:
25253 alignas ( type-id ... [opt] )
25254 alignas ( alignment-expression ... [opt] ). */
25257 cp_parser_std_attribute_spec (cp_parser
*parser
)
25259 tree attributes
= NULL_TREE
;
25260 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25262 if (token
->type
== CPP_OPEN_SQUARE
25263 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25265 tree attr_ns
= NULL_TREE
;
25267 cp_lexer_consume_token (parser
->lexer
);
25268 cp_lexer_consume_token (parser
->lexer
);
25270 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25272 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25273 if (token
->type
== CPP_NAME
)
25274 attr_ns
= token
->u
.value
;
25275 else if (token
->type
== CPP_KEYWORD
)
25276 attr_ns
= ridpointers
[(int) token
->keyword
];
25277 else if (token
->flags
& NAMED_OP
)
25278 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25281 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25283 if (cxx_dialect
< cxx17
25284 && !in_system_header_at (input_location
))
25285 pedwarn (input_location
, 0,
25286 "attribute using prefix only available "
25287 "with -std=c++17 or -std=gnu++17");
25289 cp_lexer_consume_token (parser
->lexer
);
25290 cp_lexer_consume_token (parser
->lexer
);
25291 cp_lexer_consume_token (parser
->lexer
);
25294 attr_ns
= NULL_TREE
;
25297 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25299 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25300 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25301 cp_parser_skip_to_end_of_statement (parser
);
25303 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25304 when we are sure that we have actually parsed them. */
25305 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25311 /* Look for an alignment-specifier. */
25313 token
= cp_lexer_peek_token (parser
->lexer
);
25315 if (token
->type
!= CPP_KEYWORD
25316 || token
->keyword
!= RID_ALIGNAS
)
25319 cp_lexer_consume_token (parser
->lexer
);
25320 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25322 matching_parens parens
;
25323 if (!parens
.require_open (parser
))
25325 cp_parser_error (parser
, "expected %<(%>");
25326 return error_mark_node
;
25329 cp_parser_parse_tentatively (parser
);
25330 alignas_expr
= cp_parser_type_id (parser
);
25332 if (!cp_parser_parse_definitely (parser
))
25334 alignas_expr
= cp_parser_assignment_expression (parser
);
25335 if (alignas_expr
== error_mark_node
)
25336 cp_parser_skip_to_end_of_statement (parser
);
25337 if (alignas_expr
== NULL_TREE
25338 || alignas_expr
== error_mark_node
)
25339 return alignas_expr
;
25342 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25343 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25345 /* Handle alignas (pack...). */
25346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25348 cp_lexer_consume_token (parser
->lexer
);
25349 alignas_expr
= make_pack_expansion (alignas_expr
);
25352 /* Something went wrong, so don't build the attribute. */
25353 if (alignas_expr
== error_mark_node
)
25354 return error_mark_node
;
25356 if (!parens
.require_close (parser
))
25358 cp_parser_error (parser
, "expected %<)%>");
25359 return error_mark_node
;
25362 /* Build the C++-11 representation of an 'aligned'
25365 build_tree_list (build_tree_list (get_identifier ("gnu"),
25366 get_identifier ("aligned")),
25373 /* Parse a standard C++-11 attribute-specifier-seq.
25375 attribute-specifier-seq:
25376 attribute-specifier-seq [opt] attribute-specifier
25380 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25382 tree attr_specs
= NULL_TREE
;
25383 tree attr_last
= NULL_TREE
;
25387 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25388 if (attr_spec
== NULL_TREE
)
25390 if (attr_spec
== error_mark_node
)
25391 return error_mark_node
;
25394 TREE_CHAIN (attr_last
) = attr_spec
;
25396 attr_specs
= attr_last
= attr_spec
;
25397 attr_last
= tree_last (attr_last
);
25403 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25404 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25405 current value of the PEDANTIC flag, regardless of whether or not
25406 the `__extension__' keyword is present. The caller is responsible
25407 for restoring the value of the PEDANTIC flag. */
25410 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25412 /* Save the old value of the PEDANTIC flag. */
25413 *saved_pedantic
= pedantic
;
25415 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25417 /* Consume the `__extension__' token. */
25418 cp_lexer_consume_token (parser
->lexer
);
25419 /* We're not being pedantic while the `__extension__' keyword is
25429 /* Parse a label declaration.
25432 __label__ label-declarator-seq ;
25434 label-declarator-seq:
25435 identifier , label-declarator-seq
25439 cp_parser_label_declaration (cp_parser
* parser
)
25441 /* Look for the `__label__' keyword. */
25442 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25448 /* Look for an identifier. */
25449 identifier
= cp_parser_identifier (parser
);
25450 /* If we failed, stop. */
25451 if (identifier
== error_mark_node
)
25453 /* Declare it as a label. */
25454 finish_label_decl (identifier
);
25455 /* If the next token is a `;', stop. */
25456 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25458 /* Look for the `,' separating the label declarations. */
25459 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25462 /* Look for the final `;'. */
25463 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25466 // -------------------------------------------------------------------------- //
25469 // Parse a requires clause.
25471 // requires-clause:
25472 // 'requires' logical-or-expression
25474 // The required logical-or-expression must be a constant expression. Note
25475 // that we don't check that the expression is constepxr here. We defer until
25476 // we analyze constraints and then, we only check atomic constraints.
25478 cp_parser_requires_clause (cp_parser
*parser
)
25480 // Parse the requires clause so that it is not automatically folded.
25481 ++processing_template_decl
;
25482 tree expr
= cp_parser_binary_expression (parser
, false, false,
25483 PREC_NOT_OPERATOR
, NULL
);
25484 if (check_for_bare_parameter_packs (expr
))
25485 expr
= error_mark_node
;
25486 --processing_template_decl
;
25490 // Optionally parse a requires clause:
25492 cp_parser_requires_clause_opt (cp_parser
*parser
)
25494 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25495 if (tok
->keyword
!= RID_REQUIRES
)
25497 if (!flag_concepts
&& tok
->type
== CPP_NAME
25498 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25500 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25501 "%<requires%> only available with -fconcepts");
25502 /* Parse and discard the requires-clause. */
25503 cp_lexer_consume_token (parser
->lexer
);
25504 cp_parser_requires_clause (parser
);
25508 cp_lexer_consume_token (parser
->lexer
);
25509 return cp_parser_requires_clause (parser
);
25513 /*---------------------------------------------------------------------------
25514 Requires expressions
25515 ---------------------------------------------------------------------------*/
25517 /* Parse a requires expression
25519 requirement-expression:
25520 'requires' requirement-parameter-list [opt] requirement-body */
25522 cp_parser_requires_expression (cp_parser
*parser
)
25524 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25525 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25527 /* A requires-expression shall appear only within a concept
25528 definition or a requires-clause.
25530 TODO: Implement this diagnostic correctly. */
25531 if (!processing_template_decl
)
25533 error_at (loc
, "a requires expression cannot appear outside a template");
25534 cp_parser_skip_to_end_of_statement (parser
);
25535 return error_mark_node
;
25540 /* Local parameters are delared as variables within the scope
25541 of the expression. They are not visible past the end of
25542 the expression. Expressions within the requires-expression
25543 are unevaluated. */
25544 struct scope_sentinel
25548 ++cp_unevaluated_operand
;
25549 begin_scope (sk_block
, NULL_TREE
);
25554 pop_bindings_and_leave_scope ();
25555 --cp_unevaluated_operand
;
25559 /* Parse the optional parameter list. */
25560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25562 parms
= cp_parser_requirement_parameter_list (parser
);
25563 if (parms
== error_mark_node
)
25564 return error_mark_node
;
25569 /* Parse the requirement body. */
25570 reqs
= cp_parser_requirement_body (parser
);
25571 if (reqs
== error_mark_node
)
25572 return error_mark_node
;
25575 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25577 grokparms (parms
, &parms
);
25578 return finish_requires_expr (parms
, reqs
);
25581 /* Parse a parameterized requirement.
25583 requirement-parameter-list:
25584 '(' parameter-declaration-clause ')' */
25586 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25588 matching_parens parens
;
25589 if (!parens
.require_open (parser
))
25590 return error_mark_node
;
25592 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25594 if (!parens
.require_close (parser
))
25595 return error_mark_node
;
25600 /* Parse the body of a requirement.
25603 '{' requirement-list '}' */
25605 cp_parser_requirement_body (cp_parser
*parser
)
25607 matching_braces braces
;
25608 if (!braces
.require_open (parser
))
25609 return error_mark_node
;
25611 tree reqs
= cp_parser_requirement_list (parser
);
25613 if (!braces
.require_close (parser
))
25614 return error_mark_node
;
25619 /* Parse a list of requirements.
25623 requirement-list ';' requirement[opt] */
25625 cp_parser_requirement_list (cp_parser
*parser
)
25627 tree result
= NULL_TREE
;
25630 tree req
= cp_parser_requirement (parser
);
25631 if (req
== error_mark_node
)
25632 return error_mark_node
;
25634 result
= tree_cons (NULL_TREE
, req
, result
);
25636 /* If we see a semi-colon, consume it. */
25637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25638 cp_lexer_consume_token (parser
->lexer
);
25640 /* Stop processing at the end of the list. */
25641 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25645 /* Reverse the order of requirements so they are analyzed in
25646 declaration order. */
25647 return nreverse (result
);
25650 /* Parse a syntactic requirement or type requirement.
25654 compound-requirement
25656 nested-requirement */
25658 cp_parser_requirement (cp_parser
*parser
)
25660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25661 return cp_parser_compound_requirement (parser
);
25662 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25663 return cp_parser_type_requirement (parser
);
25664 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25665 return cp_parser_nested_requirement (parser
);
25667 return cp_parser_simple_requirement (parser
);
25670 /* Parse a simple requirement.
25672 simple-requirement:
25675 cp_parser_simple_requirement (cp_parser
*parser
)
25677 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25678 if (!expr
|| expr
== error_mark_node
)
25679 return error_mark_node
;
25681 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25682 return error_mark_node
;
25684 return finish_simple_requirement (expr
);
25687 /* Parse a type requirement
25690 nested-name-specifier [opt] required-type-name ';'
25692 required-type-name:
25694 'template' [opt] simple-template-id */
25696 cp_parser_type_requirement (cp_parser
*parser
)
25698 cp_lexer_consume_token (parser
->lexer
);
25700 // Save the scope before parsing name specifiers.
25701 tree saved_scope
= parser
->scope
;
25702 tree saved_object_scope
= parser
->object_scope
;
25703 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25704 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25705 cp_parser_nested_name_specifier_opt (parser
,
25706 /*typename_keyword_p=*/true,
25707 /*check_dependency_p=*/false,
25709 /*is_declaration=*/false);
25712 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25714 cp_lexer_consume_token (parser
->lexer
);
25715 type
= cp_parser_template_id (parser
,
25716 /*template_keyword_p=*/true,
25717 /*check_dependency=*/false,
25718 /*tag_type=*/none_type
,
25719 /*is_declaration=*/false);
25720 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25721 /*complain=*/tf_error
);
25724 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25726 if (TREE_CODE (type
) == TYPE_DECL
)
25727 type
= TREE_TYPE (type
);
25729 parser
->scope
= saved_scope
;
25730 parser
->object_scope
= saved_object_scope
;
25731 parser
->qualifying_scope
= saved_qualifying_scope
;
25733 if (type
== error_mark_node
)
25734 cp_parser_skip_to_end_of_statement (parser
);
25736 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25737 return error_mark_node
;
25738 if (type
== error_mark_node
)
25739 return error_mark_node
;
25741 return finish_type_requirement (type
);
25744 /* Parse a compound requirement
25746 compound-requirement:
25747 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25749 cp_parser_compound_requirement (cp_parser
*parser
)
25751 /* Parse an expression enclosed in '{ }'s. */
25752 matching_braces braces
;
25753 if (!braces
.require_open (parser
))
25754 return error_mark_node
;
25756 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25757 if (!expr
|| expr
== error_mark_node
)
25758 return error_mark_node
;
25760 if (!braces
.require_close (parser
))
25761 return error_mark_node
;
25763 /* Parse the optional noexcept. */
25764 bool noexcept_p
= false;
25765 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25767 cp_lexer_consume_token (parser
->lexer
);
25771 /* Parse the optional trailing return type. */
25772 tree type
= NULL_TREE
;
25773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25775 cp_lexer_consume_token (parser
->lexer
);
25776 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25777 parser
->in_result_type_constraint_p
= true;
25778 type
= cp_parser_trailing_type_id (parser
);
25779 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25780 if (type
== error_mark_node
)
25781 return error_mark_node
;
25784 return finish_compound_requirement (expr
, type
, noexcept_p
);
25787 /* Parse a nested requirement. This is the same as a requires clause.
25789 nested-requirement:
25792 cp_parser_nested_requirement (cp_parser
*parser
)
25794 cp_lexer_consume_token (parser
->lexer
);
25795 tree req
= cp_parser_requires_clause (parser
);
25796 if (req
== error_mark_node
)
25797 return error_mark_node
;
25798 return finish_nested_requirement (req
);
25801 /* Support Functions */
25803 /* Return the appropriate prefer_type argument for lookup_name_real based on
25804 tag_type and template_mem_access. */
25807 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25809 /* DR 141: When looking in the current enclosing context for a template-name
25810 after -> or ., only consider class templates. */
25811 if (template_mem_access
)
25815 case none_type
: return 0; // No preference.
25816 case scope_type
: return 1; // Type or namespace.
25817 default: return 2; // Type only.
25821 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25822 NAME should have one of the representations used for an
25823 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25824 is returned. If PARSER->SCOPE is a dependent type, then a
25825 SCOPE_REF is returned.
25827 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25828 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25829 was formed. Abstractly, such entities should not be passed to this
25830 function, because they do not need to be looked up, but it is
25831 simpler to check for this special case here, rather than at the
25834 In cases not explicitly covered above, this function returns a
25835 DECL, OVERLOAD, or baselink representing the result of the lookup.
25836 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25839 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25840 (e.g., "struct") that was used. In that case bindings that do not
25841 refer to types are ignored.
25843 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25846 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25849 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25852 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25853 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25854 NULL_TREE otherwise. */
25857 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
25858 enum tag_types tag_type
,
25861 bool check_dependency
,
25862 tree
*ambiguous_decls
,
25863 location_t name_location
)
25866 tree object_type
= parser
->context
->object_type
;
25868 /* Assume that the lookup will be unambiguous. */
25869 if (ambiguous_decls
)
25870 *ambiguous_decls
= NULL_TREE
;
25872 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25873 no longer valid. Note that if we are parsing tentatively, and
25874 the parse fails, OBJECT_TYPE will be automatically restored. */
25875 parser
->context
->object_type
= NULL_TREE
;
25877 if (name
== error_mark_node
)
25878 return error_mark_node
;
25880 /* A template-id has already been resolved; there is no lookup to
25882 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
25884 if (BASELINK_P (name
))
25886 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
25887 == TEMPLATE_ID_EXPR
);
25891 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25892 it should already have been checked to make sure that the name
25893 used matches the type being destroyed. */
25894 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
25898 /* Figure out to which type this destructor applies. */
25900 type
= parser
->scope
;
25901 else if (object_type
)
25902 type
= object_type
;
25904 type
= current_class_type
;
25905 /* If that's not a class type, there is no destructor. */
25906 if (!type
|| !CLASS_TYPE_P (type
))
25907 return error_mark_node
;
25909 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
25910 lazily_declare_fn (sfk_destructor
, type
);
25912 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
25915 return error_mark_node
;
25918 /* By this point, the NAME should be an ordinary identifier. If
25919 the id-expression was a qualified name, the qualifying scope is
25920 stored in PARSER->SCOPE at this point. */
25921 gcc_assert (identifier_p (name
));
25923 /* Perform the lookup. */
25928 if (parser
->scope
== error_mark_node
)
25929 return error_mark_node
;
25931 /* If the SCOPE is dependent, the lookup must be deferred until
25932 the template is instantiated -- unless we are explicitly
25933 looking up names in uninstantiated templates. Even then, we
25934 cannot look up the name if the scope is not a class type; it
25935 might, for example, be a template type parameter. */
25936 dependent_p
= (TYPE_P (parser
->scope
)
25937 && dependent_scope_p (parser
->scope
));
25938 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
25940 /* Defer lookup. */
25941 decl
= error_mark_node
;
25944 tree pushed_scope
= NULL_TREE
;
25946 /* If PARSER->SCOPE is a dependent type, then it must be a
25947 class type, and we must not be checking dependencies;
25948 otherwise, we would have processed this lookup above. So
25949 that PARSER->SCOPE is not considered a dependent base by
25950 lookup_member, we must enter the scope here. */
25952 pushed_scope
= push_scope (parser
->scope
);
25954 /* If the PARSER->SCOPE is a template specialization, it
25955 may be instantiated during name lookup. In that case,
25956 errors may be issued. Even if we rollback the current
25957 tentative parse, those errors are valid. */
25958 decl
= lookup_qualified_name (parser
->scope
, name
,
25959 prefer_type_arg (tag_type
),
25960 /*complain=*/true);
25962 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25963 lookup result and the nested-name-specifier nominates a class C:
25964 * if the name specified after the nested-name-specifier, when
25965 looked up in C, is the injected-class-name of C (Clause 9), or
25966 * if the name specified after the nested-name-specifier is the
25967 same as the identifier or the simple-template-id's template-
25968 name in the last component of the nested-name-specifier,
25969 the name is instead considered to name the constructor of
25970 class C. [ Note: for example, the constructor is not an
25971 acceptable lookup result in an elaborated-type-specifier so
25972 the constructor would not be used in place of the
25973 injected-class-name. --end note ] Such a constructor name
25974 shall be used only in the declarator-id of a declaration that
25975 names a constructor or in a using-declaration. */
25976 if (tag_type
== none_type
25977 && DECL_SELF_REFERENCE_P (decl
)
25978 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
25979 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
25980 prefer_type_arg (tag_type
),
25981 /*complain=*/true);
25983 /* If we have a single function from a using decl, pull it out. */
25984 if (TREE_CODE (decl
) == OVERLOAD
25985 && !really_overloaded_fn (decl
))
25986 decl
= OVL_FUNCTION (decl
);
25989 pop_scope (pushed_scope
);
25992 /* If the scope is a dependent type and either we deferred lookup or
25993 we did lookup but didn't find the name, rememeber the name. */
25994 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
25995 && dependent_type_p (parser
->scope
))
26001 /* The resolution to Core Issue 180 says that `struct
26002 A::B' should be considered a type-name, even if `A'
26004 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26005 /*complain=*/tf_error
);
26006 if (type
!= error_mark_node
)
26007 decl
= TYPE_NAME (type
);
26009 else if (is_template
26010 && (cp_parser_next_token_ends_template_argument_p (parser
)
26011 || cp_lexer_next_token_is (parser
->lexer
,
26013 decl
= make_unbound_class_template (parser
->scope
,
26015 /*complain=*/tf_error
);
26017 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26018 parser
->scope
, name
,
26021 parser
->qualifying_scope
= parser
->scope
;
26022 parser
->object_scope
= NULL_TREE
;
26024 else if (object_type
)
26026 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26027 OBJECT_TYPE is not a class. */
26028 if (CLASS_TYPE_P (object_type
))
26029 /* If the OBJECT_TYPE is a template specialization, it may
26030 be instantiated during name lookup. In that case, errors
26031 may be issued. Even if we rollback the current tentative
26032 parse, those errors are valid. */
26033 decl
= lookup_member (object_type
,
26036 prefer_type_arg (tag_type
),
26037 tf_warning_or_error
);
26042 /* Look it up in the enclosing context. DR 141: When looking for a
26043 template-name after -> or ., only consider class templates. */
26044 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26046 /*block_p=*/true, is_namespace
, 0);
26047 if (object_type
== unknown_type_node
)
26048 /* The object is type-dependent, so we can't look anything up; we used
26049 this to get the DR 141 behavior. */
26050 object_type
= NULL_TREE
;
26051 parser
->object_scope
= object_type
;
26052 parser
->qualifying_scope
= NULL_TREE
;
26056 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26058 /*block_p=*/true, is_namespace
, 0);
26059 parser
->qualifying_scope
= NULL_TREE
;
26060 parser
->object_scope
= NULL_TREE
;
26063 /* If the lookup failed, let our caller know. */
26064 if (!decl
|| decl
== error_mark_node
)
26065 return error_mark_node
;
26067 /* Pull out the template from an injected-class-name (or multiple). */
26069 decl
= maybe_get_template_decl_from_type_decl (decl
);
26071 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26072 if (TREE_CODE (decl
) == TREE_LIST
)
26074 if (ambiguous_decls
)
26075 *ambiguous_decls
= decl
;
26076 /* The error message we have to print is too complicated for
26077 cp_parser_error, so we incorporate its actions directly. */
26078 if (!cp_parser_simulate_error (parser
))
26080 error_at (name_location
, "reference to %qD is ambiguous",
26082 print_candidates (decl
);
26084 return error_mark_node
;
26087 gcc_assert (DECL_P (decl
)
26088 || TREE_CODE (decl
) == OVERLOAD
26089 || TREE_CODE (decl
) == SCOPE_REF
26090 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26091 || BASELINK_P (decl
));
26093 /* If we have resolved the name of a member declaration, check to
26094 see if the declaration is accessible. When the name resolves to
26095 set of overloaded functions, accessibility is checked when
26096 overload resolution is done.
26098 During an explicit instantiation, access is not checked at all,
26099 as per [temp.explicit]. */
26101 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26103 maybe_record_typedef_use (decl
);
26105 return cp_expr (decl
, name_location
);
26108 /* Like cp_parser_lookup_name, but for use in the typical case where
26109 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26110 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26113 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26115 return cp_parser_lookup_name (parser
, name
,
26117 /*is_template=*/false,
26118 /*is_namespace=*/false,
26119 /*check_dependency=*/true,
26120 /*ambiguous_decls=*/NULL
,
26124 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26125 the current context, return the TYPE_DECL. If TAG_NAME_P is
26126 true, the DECL indicates the class being defined in a class-head,
26127 or declared in an elaborated-type-specifier.
26129 Otherwise, return DECL. */
26132 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26134 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26135 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26138 template <typename T> struct B;
26141 template <typename T> struct A::B {};
26143 Similarly, in an elaborated-type-specifier:
26145 namespace N { struct X{}; }
26148 template <typename T> friend struct N::X;
26151 However, if the DECL refers to a class type, and we are in
26152 the scope of the class, then the name lookup automatically
26153 finds the TYPE_DECL created by build_self_reference rather
26154 than a TEMPLATE_DECL. For example, in:
26156 template <class T> struct S {
26160 there is no need to handle such case. */
26162 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26163 return DECL_TEMPLATE_RESULT (decl
);
26168 /* If too many, or too few, template-parameter lists apply to the
26169 declarator, issue an error message. Returns TRUE if all went well,
26170 and FALSE otherwise. */
26173 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26174 cp_declarator
*declarator
,
26175 location_t declarator_location
)
26177 switch (declarator
->kind
)
26181 unsigned num_templates
= 0;
26182 tree scope
= declarator
->u
.id
.qualifying_scope
;
26185 num_templates
= num_template_headers_for_class (scope
);
26186 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26187 == TEMPLATE_ID_EXPR
)
26188 /* If the DECLARATOR has the form `X<y>' then it uses one
26189 additional level of template parameters. */
26192 return cp_parser_check_template_parameters
26193 (parser
, num_templates
, declarator_location
, declarator
);
26199 case cdk_reference
:
26201 return (cp_parser_check_declarator_template_parameters
26202 (parser
, declarator
->declarator
, declarator_location
));
26209 gcc_unreachable ();
26214 /* NUM_TEMPLATES were used in the current declaration. If that is
26215 invalid, return FALSE and issue an error messages. Otherwise,
26216 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26217 declarator and we can print more accurate diagnostics. */
26220 cp_parser_check_template_parameters (cp_parser
* parser
,
26221 unsigned num_templates
,
26222 location_t location
,
26223 cp_declarator
*declarator
)
26225 /* If there are the same number of template classes and parameter
26226 lists, that's OK. */
26227 if (parser
->num_template_parameter_lists
== num_templates
)
26229 /* If there are more, but only one more, then we are referring to a
26230 member template. That's OK too. */
26231 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26233 /* If there are more template classes than parameter lists, we have
26236 template <class T> void S<T>::R<T>::f (); */
26237 if (parser
->num_template_parameter_lists
< num_templates
)
26239 if (declarator
&& !current_function_decl
)
26240 error_at (location
, "specializing member %<%T::%E%> "
26241 "requires %<template<>%> syntax",
26242 declarator
->u
.id
.qualifying_scope
,
26243 declarator
->u
.id
.unqualified_name
);
26244 else if (declarator
)
26245 error_at (location
, "invalid declaration of %<%T::%E%>",
26246 declarator
->u
.id
.qualifying_scope
,
26247 declarator
->u
.id
.unqualified_name
);
26249 error_at (location
, "too few template-parameter-lists");
26252 /* Otherwise, there are too many template parameter lists. We have
26255 template <class T> template <class U> void S::f(); */
26256 error_at (location
, "too many template-parameter-lists");
26260 /* Parse an optional `::' token indicating that the following name is
26261 from the global namespace. If so, PARSER->SCOPE is set to the
26262 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26263 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26264 Returns the new value of PARSER->SCOPE, if the `::' token is
26265 present, and NULL_TREE otherwise. */
26268 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26272 /* Peek at the next token. */
26273 token
= cp_lexer_peek_token (parser
->lexer
);
26274 /* If we're looking at a `::' token then we're starting from the
26275 global namespace, not our current location. */
26276 if (token
->type
== CPP_SCOPE
)
26278 /* Consume the `::' token. */
26279 cp_lexer_consume_token (parser
->lexer
);
26280 /* Set the SCOPE so that we know where to start the lookup. */
26281 parser
->scope
= global_namespace
;
26282 parser
->qualifying_scope
= global_namespace
;
26283 parser
->object_scope
= NULL_TREE
;
26285 return parser
->scope
;
26287 else if (!current_scope_valid_p
)
26289 parser
->scope
= NULL_TREE
;
26290 parser
->qualifying_scope
= NULL_TREE
;
26291 parser
->object_scope
= NULL_TREE
;
26297 /* Returns TRUE if the upcoming token sequence is the start of a
26298 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26299 declarator is preceded by the `friend' specifier. */
26302 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26304 bool constructor_p
;
26305 bool outside_class_specifier_p
;
26306 tree nested_name_specifier
;
26307 cp_token
*next_token
;
26309 /* The common case is that this is not a constructor declarator, so
26310 try to avoid doing lots of work if at all possible. It's not
26311 valid declare a constructor at function scope. */
26312 if (parser
->in_function_body
)
26314 /* And only certain tokens can begin a constructor declarator. */
26315 next_token
= cp_lexer_peek_token (parser
->lexer
);
26316 if (next_token
->type
!= CPP_NAME
26317 && next_token
->type
!= CPP_SCOPE
26318 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26319 && next_token
->type
!= CPP_TEMPLATE_ID
)
26322 /* Parse tentatively; we are going to roll back all of the tokens
26324 cp_parser_parse_tentatively (parser
);
26325 /* Assume that we are looking at a constructor declarator. */
26326 constructor_p
= true;
26328 /* Look for the optional `::' operator. */
26329 cp_parser_global_scope_opt (parser
,
26330 /*current_scope_valid_p=*/false);
26331 /* Look for the nested-name-specifier. */
26332 nested_name_specifier
26333 = (cp_parser_nested_name_specifier_opt (parser
,
26334 /*typename_keyword_p=*/false,
26335 /*check_dependency_p=*/false,
26337 /*is_declaration=*/false));
26339 outside_class_specifier_p
= (!at_class_scope_p ()
26340 || !TYPE_BEING_DEFINED (current_class_type
)
26343 /* Outside of a class-specifier, there must be a
26344 nested-name-specifier. Except in C++17 mode, where we
26345 might be declaring a guiding declaration. */
26346 if (!nested_name_specifier
&& outside_class_specifier_p
26347 && cxx_dialect
< cxx17
)
26348 constructor_p
= false;
26349 else if (nested_name_specifier
== error_mark_node
)
26350 constructor_p
= false;
26352 /* If we have a class scope, this is easy; DR 147 says that S::S always
26353 names the constructor, and no other qualified name could. */
26354 if (constructor_p
&& nested_name_specifier
26355 && CLASS_TYPE_P (nested_name_specifier
))
26357 tree id
= cp_parser_unqualified_id (parser
,
26358 /*template_keyword_p=*/false,
26359 /*check_dependency_p=*/false,
26360 /*declarator_p=*/true,
26361 /*optional_p=*/false);
26362 if (is_overloaded_fn (id
))
26363 id
= DECL_NAME (get_first_fn (id
));
26364 if (!constructor_name_p (id
, nested_name_specifier
))
26365 constructor_p
= false;
26367 /* If we still think that this might be a constructor-declarator,
26368 look for a class-name. */
26369 else if (constructor_p
)
26373 template <typename T> struct S {
26377 we must recognize that the nested `S' names a class. */
26378 if (cxx_dialect
>= cxx17
)
26379 cp_parser_parse_tentatively (parser
);
26382 type_decl
= cp_parser_class_name (parser
,
26383 /*typename_keyword_p=*/false,
26384 /*template_keyword_p=*/false,
26386 /*check_dependency_p=*/false,
26387 /*class_head_p=*/false,
26388 /*is_declaration=*/false);
26390 if (cxx_dialect
>= cxx17
26391 && !cp_parser_parse_definitely (parser
))
26393 type_decl
= NULL_TREE
;
26394 tree tmpl
= cp_parser_template_name (parser
,
26395 /*template_keyword*/false,
26396 /*check_dependency_p*/false,
26397 /*is_declaration*/false,
26399 /*is_identifier*/NULL
);
26400 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26401 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26402 /* It's a deduction guide, return true. */;
26404 cp_parser_simulate_error (parser
);
26407 /* If there was no class-name, then this is not a constructor.
26408 Otherwise, if we are in a class-specifier and we aren't
26409 handling a friend declaration, check that its type matches
26410 current_class_type (c++/38313). Note: error_mark_node
26411 is left alone for error recovery purposes. */
26412 constructor_p
= (!cp_parser_error_occurred (parser
)
26413 && (outside_class_specifier_p
26414 || type_decl
== NULL_TREE
26415 || type_decl
== error_mark_node
26416 || same_type_p (current_class_type
,
26417 TREE_TYPE (type_decl
))));
26419 /* If we're still considering a constructor, we have to see a `(',
26420 to begin the parameter-declaration-clause, followed by either a
26421 `)', an `...', or a decl-specifier. We need to check for a
26422 type-specifier to avoid being fooled into thinking that:
26426 is a constructor. (It is actually a function named `f' that
26427 takes one parameter (of type `int') and returns a value of type
26430 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26431 constructor_p
= false;
26434 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26435 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26436 /* A parameter declaration begins with a decl-specifier,
26437 which is either the "attribute" keyword, a storage class
26438 specifier, or (usually) a type-specifier. */
26439 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26442 tree pushed_scope
= NULL_TREE
;
26443 unsigned saved_num_template_parameter_lists
;
26445 /* Names appearing in the type-specifier should be looked up
26446 in the scope of the class. */
26447 if (current_class_type
)
26449 else if (type_decl
)
26451 type
= TREE_TYPE (type_decl
);
26452 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26454 type
= resolve_typename_type (type
,
26455 /*only_current_p=*/false);
26456 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26458 cp_parser_abort_tentative_parse (parser
);
26462 pushed_scope
= push_scope (type
);
26465 /* Inside the constructor parameter list, surrounding
26466 template-parameter-lists do not apply. */
26467 saved_num_template_parameter_lists
26468 = parser
->num_template_parameter_lists
;
26469 parser
->num_template_parameter_lists
= 0;
26471 /* Look for the type-specifier. */
26472 cp_parser_type_specifier (parser
,
26473 CP_PARSER_FLAGS_NONE
,
26474 /*decl_specs=*/NULL
,
26475 /*is_declarator=*/true,
26476 /*declares_class_or_enum=*/NULL
,
26477 /*is_cv_qualifier=*/NULL
);
26479 parser
->num_template_parameter_lists
26480 = saved_num_template_parameter_lists
;
26482 /* Leave the scope of the class. */
26484 pop_scope (pushed_scope
);
26486 constructor_p
= !cp_parser_error_occurred (parser
);
26490 /* We did not really want to consume any tokens. */
26491 cp_parser_abort_tentative_parse (parser
);
26493 return constructor_p
;
26496 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26497 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26498 they must be performed once we are in the scope of the function.
26500 Returns the function defined. */
26503 cp_parser_function_definition_from_specifiers_and_declarator
26504 (cp_parser
* parser
,
26505 cp_decl_specifier_seq
*decl_specifiers
,
26507 const cp_declarator
*declarator
)
26512 /* Begin the function-definition. */
26513 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26515 /* The things we're about to see are not directly qualified by any
26516 template headers we've seen thus far. */
26517 reset_specialization ();
26519 /* If there were names looked up in the decl-specifier-seq that we
26520 did not check, check them now. We must wait until we are in the
26521 scope of the function to perform the checks, since the function
26522 might be a friend. */
26523 perform_deferred_access_checks (tf_warning_or_error
);
26527 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26528 parser
->omp_declare_simd
= NULL
;
26529 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26530 parser
->oacc_routine
= NULL
;
26535 /* Skip the entire function. */
26536 cp_parser_skip_to_end_of_block_or_statement (parser
);
26537 fn
= error_mark_node
;
26539 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26541 /* Seen already, skip it. An error message has already been output. */
26542 cp_parser_skip_to_end_of_block_or_statement (parser
);
26543 fn
= current_function_decl
;
26544 current_function_decl
= NULL_TREE
;
26545 /* If this is a function from a class, pop the nested class. */
26546 if (current_class_name
)
26547 pop_nested_class ();
26552 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26553 tv
= TV_PARSE_INLINE
;
26555 tv
= TV_PARSE_FUNC
;
26557 fn
= cp_parser_function_definition_after_declarator (parser
,
26558 /*inline_p=*/false);
26565 /* Parse the part of a function-definition that follows the
26566 declarator. INLINE_P is TRUE iff this function is an inline
26567 function defined within a class-specifier.
26569 Returns the function defined. */
26572 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26576 bool saved_in_unbraced_linkage_specification_p
;
26577 bool saved_in_function_body
;
26578 unsigned saved_num_template_parameter_lists
;
26580 bool fully_implicit_function_template_p
26581 = parser
->fully_implicit_function_template_p
;
26582 parser
->fully_implicit_function_template_p
= false;
26583 tree implicit_template_parms
26584 = parser
->implicit_template_parms
;
26585 parser
->implicit_template_parms
= 0;
26586 cp_binding_level
* implicit_template_scope
26587 = parser
->implicit_template_scope
;
26588 parser
->implicit_template_scope
= 0;
26590 saved_in_function_body
= parser
->in_function_body
;
26591 parser
->in_function_body
= true;
26592 /* If the next token is `return', then the code may be trying to
26593 make use of the "named return value" extension that G++ used to
26595 token
= cp_lexer_peek_token (parser
->lexer
);
26596 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26598 /* Consume the `return' keyword. */
26599 cp_lexer_consume_token (parser
->lexer
);
26600 /* Look for the identifier that indicates what value is to be
26602 cp_parser_identifier (parser
);
26603 /* Issue an error message. */
26604 error_at (token
->location
,
26605 "named return values are no longer supported");
26606 /* Skip tokens until we reach the start of the function body. */
26609 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26610 if (token
->type
== CPP_OPEN_BRACE
26611 || token
->type
== CPP_EOF
26612 || token
->type
== CPP_PRAGMA_EOL
)
26614 cp_lexer_consume_token (parser
->lexer
);
26617 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26618 anything declared inside `f'. */
26619 saved_in_unbraced_linkage_specification_p
26620 = parser
->in_unbraced_linkage_specification_p
;
26621 parser
->in_unbraced_linkage_specification_p
= false;
26622 /* Inside the function, surrounding template-parameter-lists do not
26624 saved_num_template_parameter_lists
26625 = parser
->num_template_parameter_lists
;
26626 parser
->num_template_parameter_lists
= 0;
26628 /* If the next token is `try', `__transaction_atomic', or
26629 `__transaction_relaxed`, then we are looking at either function-try-block
26630 or function-transaction-block. Note that all of these include the
26632 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26633 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26634 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26635 RID_TRANSACTION_RELAXED
))
26636 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26637 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26638 cp_parser_function_try_block (parser
);
26640 cp_parser_ctor_initializer_opt_and_function_body
26641 (parser
, /*in_function_try_block=*/false);
26643 /* Finish the function. */
26644 fn
= finish_function (inline_p
);
26645 /* Generate code for it, if necessary. */
26646 expand_or_defer_fn (fn
);
26647 /* Restore the saved values. */
26648 parser
->in_unbraced_linkage_specification_p
26649 = saved_in_unbraced_linkage_specification_p
;
26650 parser
->num_template_parameter_lists
26651 = saved_num_template_parameter_lists
;
26652 parser
->in_function_body
= saved_in_function_body
;
26654 parser
->fully_implicit_function_template_p
26655 = fully_implicit_function_template_p
;
26656 parser
->implicit_template_parms
26657 = implicit_template_parms
;
26658 parser
->implicit_template_scope
26659 = implicit_template_scope
;
26661 if (parser
->fully_implicit_function_template_p
)
26662 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26667 /* Parse a template-declaration body (following argument list). */
26670 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26671 tree parameter_list
,
26674 tree decl
= NULL_TREE
;
26675 bool friend_p
= false;
26677 /* We just processed one more parameter list. */
26678 ++parser
->num_template_parameter_lists
;
26680 /* Get the deferred access checks from the parameter list. These
26681 will be checked once we know what is being declared, as for a
26682 member template the checks must be performed in the scope of the
26683 class containing the member. */
26684 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26686 /* Tentatively parse for a new template parameter list, which can either be
26687 the template keyword or a template introduction. */
26688 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26690 else if (cxx_dialect
>= cxx11
26691 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26692 decl
= cp_parser_alias_declaration (parser
);
26695 /* There are no access checks when parsing a template, as we do not
26696 know if a specialization will be a friend. */
26697 push_deferring_access_checks (dk_no_check
);
26698 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26699 decl
= cp_parser_single_declaration (parser
,
26702 /*explicit_specialization_p=*/false,
26704 pop_deferring_access_checks ();
26706 /* If this is a member template declaration, let the front
26708 if (member_p
&& !friend_p
&& decl
)
26710 if (TREE_CODE (decl
) == TYPE_DECL
)
26711 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26713 decl
= finish_member_template_decl (decl
);
26715 else if (friend_p
&& decl
26716 && DECL_DECLARES_TYPE_P (decl
))
26717 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26718 /*complain=*/true);
26720 /* We are done with the current parameter list. */
26721 --parser
->num_template_parameter_lists
;
26723 pop_deferring_access_checks ();
26726 finish_template_decl (parameter_list
);
26728 /* Check the template arguments for a literal operator template. */
26730 && DECL_DECLARES_FUNCTION_P (decl
)
26731 && UDLIT_OPER_P (DECL_NAME (decl
)))
26734 if (parameter_list
== NULL_TREE
)
26738 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26739 if (num_parms
== 1)
26741 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26742 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26743 if (TREE_TYPE (parm
) != char_type_node
26744 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26747 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26749 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26750 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26751 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26752 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26753 if (parm
== error_mark_node
26754 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26755 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26763 if (cxx_dialect
>= cxx14
)
26764 error ("literal operator template %qD has invalid parameter list."
26765 " Expected non-type template argument pack <char...>"
26766 " or <typename CharT, CharT...>",
26769 error ("literal operator template %qD has invalid parameter list."
26770 " Expected non-type template argument pack <char...>",
26775 /* Register member declarations. */
26776 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26777 finish_member_declaration (decl
);
26778 /* If DECL is a function template, we must return to parse it later.
26779 (Even though there is no definition, there might be default
26780 arguments that need handling.) */
26781 if (member_p
&& decl
26782 && DECL_DECLARES_FUNCTION_P (decl
))
26783 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26786 /* Parse a template introduction header for a template-declaration. Returns
26787 false if tentative parse fails. */
26790 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26792 cp_parser_parse_tentatively (parser
);
26794 tree saved_scope
= parser
->scope
;
26795 tree saved_object_scope
= parser
->object_scope
;
26796 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26798 /* Look for the optional `::' operator. */
26799 cp_parser_global_scope_opt (parser
,
26800 /*current_scope_valid_p=*/false);
26801 /* Look for the nested-name-specifier. */
26802 cp_parser_nested_name_specifier_opt (parser
,
26803 /*typename_keyword_p=*/false,
26804 /*check_dependency_p=*/true,
26806 /*is_declaration=*/false);
26808 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26809 tree concept_name
= cp_parser_identifier (parser
);
26811 /* Look up the concept for which we will be matching
26812 template parameters. */
26813 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26815 parser
->scope
= saved_scope
;
26816 parser
->object_scope
= saved_object_scope
;
26817 parser
->qualifying_scope
= saved_qualifying_scope
;
26819 if (concept_name
== error_mark_node
)
26820 cp_parser_simulate_error (parser
);
26822 /* Look for opening brace for introduction. */
26823 matching_braces braces
;
26824 braces
.require_open (parser
);
26826 if (!cp_parser_parse_definitely (parser
))
26829 push_deferring_access_checks (dk_deferred
);
26831 /* Build vector of placeholder parameters and grab
26832 matching identifiers. */
26833 tree introduction_list
= cp_parser_introduction_list (parser
);
26835 /* The introduction-list shall not be empty. */
26836 int nargs
= TREE_VEC_LENGTH (introduction_list
);
26839 error ("empty introduction-list");
26843 /* Look for closing brace for introduction. */
26844 if (!braces
.require_close (parser
))
26847 if (tmpl_decl
== error_mark_node
)
26849 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
26854 /* Build and associate the constraint. */
26855 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
26856 if (parms
&& parms
!= error_mark_node
)
26858 cp_parser_template_declaration_after_parameters (parser
, parms
,
26863 error_at (token
->location
, "no matching concept for template-introduction");
26867 /* Parse a normal template-declaration following the template keyword. */
26870 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
26872 tree parameter_list
;
26873 bool need_lang_pop
;
26874 location_t location
= input_location
;
26876 /* Look for the `<' token. */
26877 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
26879 if (at_class_scope_p () && current_function_decl
)
26881 /* 14.5.2.2 [temp.mem]
26883 A local class shall not have member templates. */
26884 error_at (location
,
26885 "invalid declaration of member template in local class");
26886 cp_parser_skip_to_end_of_block_or_statement (parser
);
26891 A template ... shall not have C linkage. */
26892 if (current_lang_name
== lang_name_c
)
26894 error_at (location
, "template with C linkage");
26895 maybe_show_extern_c_location ();
26896 /* Give it C++ linkage to avoid confusing other parts of the
26898 push_lang_context (lang_name_cplusplus
);
26899 need_lang_pop
= true;
26902 need_lang_pop
= false;
26904 /* We cannot perform access checks on the template parameter
26905 declarations until we know what is being declared, just as we
26906 cannot check the decl-specifier list. */
26907 push_deferring_access_checks (dk_deferred
);
26909 /* If the next token is `>', then we have an invalid
26910 specialization. Rather than complain about an invalid template
26911 parameter, issue an error message here. */
26912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
26914 cp_parser_error (parser
, "invalid explicit specialization");
26915 begin_specialization ();
26916 parameter_list
= NULL_TREE
;
26920 /* Parse the template parameters. */
26921 parameter_list
= cp_parser_template_parameter_list (parser
);
26924 /* Look for the `>'. */
26925 cp_parser_skip_to_end_of_template_parameter_list (parser
);
26927 /* Manage template requirements */
26930 tree reqs
= get_shorthand_constraints (current_template_parms
);
26931 if (tree r
= cp_parser_requires_clause_opt (parser
))
26932 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
26933 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
26936 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
26939 /* For the erroneous case of a template with C linkage, we pushed an
26940 implicit C++ linkage scope; exit that scope now. */
26942 pop_lang_context ();
26945 /* Parse a template-declaration, assuming that the `export' (and
26946 `extern') keywords, if present, has already been scanned. MEMBER_P
26947 is as for cp_parser_template_declaration. */
26950 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
26952 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26954 cp_lexer_consume_token (parser
->lexer
);
26955 cp_parser_explicit_template_declaration (parser
, member_p
);
26958 else if (flag_concepts
)
26959 return cp_parser_template_introduction (parser
, member_p
);
26964 /* Perform the deferred access checks from a template-parameter-list.
26965 CHECKS is a TREE_LIST of access checks, as returned by
26966 get_deferred_access_checks. */
26969 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
26971 ++processing_template_parmlist
;
26972 perform_access_checks (checks
, tf_warning_or_error
);
26973 --processing_template_parmlist
;
26976 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26977 `function-definition' sequence that follows a template header.
26978 If MEMBER_P is true, this declaration appears in a class scope.
26980 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26981 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26984 cp_parser_single_declaration (cp_parser
* parser
,
26985 vec
<deferred_access_check
, va_gc
> *checks
,
26987 bool explicit_specialization_p
,
26990 int declares_class_or_enum
;
26991 tree decl
= NULL_TREE
;
26992 cp_decl_specifier_seq decl_specifiers
;
26993 bool function_definition_p
= false;
26994 cp_token
*decl_spec_token_start
;
26996 /* This function is only used when processing a template
26998 gcc_assert (innermost_scope_kind () == sk_template_parms
26999 || innermost_scope_kind () == sk_template_spec
);
27001 /* Defer access checks until we know what is being declared. */
27002 push_deferring_access_checks (dk_deferred
);
27004 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27006 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27007 cp_parser_decl_specifier_seq (parser
,
27008 CP_PARSER_FLAGS_OPTIONAL
,
27010 &declares_class_or_enum
);
27012 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27014 /* There are no template typedefs. */
27015 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27017 error_at (decl_spec_token_start
->location
,
27018 "template declaration of %<typedef%>");
27019 decl
= error_mark_node
;
27022 /* Gather up the access checks that occurred the
27023 decl-specifier-seq. */
27024 stop_deferring_access_checks ();
27026 /* Check for the declaration of a template class. */
27027 if (declares_class_or_enum
)
27029 if (cp_parser_declares_only_class_p (parser
)
27030 || (declares_class_or_enum
& 2))
27032 // If this is a declaration, but not a definition, associate
27033 // any constraints with the type declaration. Constraints
27034 // are associated with definitions in cp_parser_class_specifier.
27035 if (declares_class_or_enum
== 1)
27036 associate_classtype_constraints (decl_specifiers
.type
);
27038 decl
= shadow_tag (&decl_specifiers
);
27043 friend template <typename T> struct A<T>::B;
27046 A<T>::B will be represented by a TYPENAME_TYPE, and
27047 therefore not recognized by shadow_tag. */
27048 if (friend_p
&& *friend_p
27050 && decl_specifiers
.type
27051 && TYPE_P (decl_specifiers
.type
))
27052 decl
= decl_specifiers
.type
;
27054 if (decl
&& decl
!= error_mark_node
)
27055 decl
= TYPE_NAME (decl
);
27057 decl
= error_mark_node
;
27059 /* Perform access checks for template parameters. */
27060 cp_parser_perform_template_parameter_access_checks (checks
);
27062 /* Give a helpful diagnostic for
27063 template <class T> struct A { } a;
27064 if we aren't already recovering from an error. */
27065 if (!cp_parser_declares_only_class_p (parser
)
27068 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27069 "a class template declaration must not declare "
27071 cp_parser_skip_to_end_of_block_or_statement (parser
);
27077 /* Complain about missing 'typename' or other invalid type names. */
27078 if (!decl_specifiers
.any_type_specifiers_p
27079 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27081 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27082 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27083 the rest of this declaration. */
27084 decl
= error_mark_node
;
27088 /* If it's not a template class, try for a template function. If
27089 the next token is a `;', then this declaration does not declare
27090 anything. But, if there were errors in the decl-specifiers, then
27091 the error might well have come from an attempted class-specifier.
27092 In that case, there's no need to warn about a missing declarator. */
27094 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27095 || decl_specifiers
.type
!= error_mark_node
))
27097 decl
= cp_parser_init_declarator (parser
,
27100 /*function_definition_allowed_p=*/true,
27102 declares_class_or_enum
,
27103 &function_definition_p
,
27106 /* 7.1.1-1 [dcl.stc]
27108 A storage-class-specifier shall not be specified in an explicit
27109 specialization... */
27111 && explicit_specialization_p
27112 && decl_specifiers
.storage_class
!= sc_none
)
27114 error_at (decl_spec_token_start
->location
,
27115 "explicit template specialization cannot have a storage class");
27116 decl
= error_mark_node
;
27119 if (decl
&& VAR_P (decl
))
27120 check_template_variable (decl
);
27123 /* Look for a trailing `;' after the declaration. */
27124 if (!function_definition_p
27125 && (decl
== error_mark_node
27126 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27127 cp_parser_skip_to_end_of_block_or_statement (parser
);
27130 pop_deferring_access_checks ();
27132 /* Clear any current qualification; whatever comes next is the start
27133 of something new. */
27134 parser
->scope
= NULL_TREE
;
27135 parser
->qualifying_scope
= NULL_TREE
;
27136 parser
->object_scope
= NULL_TREE
;
27141 /* Parse a cast-expression that is not the operand of a unary "&". */
27144 cp_parser_simple_cast_expression (cp_parser
*parser
)
27146 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27147 /*cast_p=*/false, /*decltype*/false, NULL
);
27150 /* Parse a functional cast to TYPE. Returns an expression
27151 representing the cast. */
27154 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27156 vec
<tree
, va_gc
> *vec
;
27157 tree expression_list
;
27161 location_t start_loc
= input_location
;
27164 type
= error_mark_node
;
27166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27168 cp_lexer_set_source_position (parser
->lexer
);
27169 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27170 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27171 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27172 if (TREE_CODE (type
) == TYPE_DECL
)
27173 type
= TREE_TYPE (type
);
27175 cast
= finish_compound_literal (type
, expression_list
,
27176 tf_warning_or_error
, fcl_functional
);
27177 /* Create a location of the form:
27180 with caret == start at the start of the type name,
27181 finishing at the closing brace. */
27182 location_t finish_loc
27183 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27184 location_t combined_loc
= make_location (start_loc
, start_loc
,
27186 cast
.set_location (combined_loc
);
27191 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27193 /*allow_expansion_p=*/true,
27194 /*non_constant_p=*/NULL
);
27196 expression_list
= error_mark_node
;
27199 expression_list
= build_tree_list_vec (vec
);
27200 release_tree_vector (vec
);
27203 cast
= build_functional_cast (type
, expression_list
,
27204 tf_warning_or_error
);
27205 /* [expr.const]/1: In an integral constant expression "only type
27206 conversions to integral or enumeration type can be used". */
27207 if (TREE_CODE (type
) == TYPE_DECL
)
27208 type
= TREE_TYPE (type
);
27209 if (cast
!= error_mark_node
27210 && !cast_valid_in_integral_constant_expression_p (type
)
27211 && cp_parser_non_integral_constant_expression (parser
,
27213 return error_mark_node
;
27215 /* Create a location of the form:
27218 with caret == start at the start of the type name,
27219 finishing at the closing paren. */
27220 location_t finish_loc
27221 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27222 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27223 cast
.set_location (combined_loc
);
27227 /* Save the tokens that make up the body of a member function defined
27228 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27229 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27230 specifiers applied to the declaration. Returns the FUNCTION_DECL
27231 for the member function. */
27234 cp_parser_save_member_function_body (cp_parser
* parser
,
27235 cp_decl_specifier_seq
*decl_specifiers
,
27236 cp_declarator
*declarator
,
27242 bool function_try_block
= false;
27244 /* Create the FUNCTION_DECL. */
27245 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27246 cp_finalize_omp_declare_simd (parser
, fn
);
27247 cp_finalize_oacc_routine (parser
, fn
, true);
27248 /* If something went badly wrong, bail out now. */
27249 if (fn
== error_mark_node
)
27251 /* If there's a function-body, skip it. */
27252 if (cp_parser_token_starts_function_definition_p
27253 (cp_lexer_peek_token (parser
->lexer
)))
27254 cp_parser_skip_to_end_of_block_or_statement (parser
);
27255 return error_mark_node
;
27258 /* Remember it, if there default args to post process. */
27259 cp_parser_save_default_args (parser
, fn
);
27261 /* Save away the tokens that make up the body of the
27263 first
= parser
->lexer
->next_token
;
27265 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27266 cp_lexer_consume_token (parser
->lexer
);
27267 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27268 RID_TRANSACTION_ATOMIC
))
27270 cp_lexer_consume_token (parser
->lexer
);
27271 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27273 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27274 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27275 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27276 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27277 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27279 cp_lexer_consume_token (parser
->lexer
);
27280 cp_lexer_consume_token (parser
->lexer
);
27281 cp_lexer_consume_token (parser
->lexer
);
27282 cp_lexer_consume_token (parser
->lexer
);
27283 cp_lexer_consume_token (parser
->lexer
);
27286 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27287 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27289 cp_lexer_consume_token (parser
->lexer
);
27290 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27295 /* Handle function try blocks. */
27296 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27298 cp_lexer_consume_token (parser
->lexer
);
27299 function_try_block
= true;
27301 /* We can have braced-init-list mem-initializers before the fn body. */
27302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27304 cp_lexer_consume_token (parser
->lexer
);
27305 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27307 /* cache_group will stop after an un-nested { } pair, too. */
27308 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27311 /* variadic mem-inits have ... after the ')'. */
27312 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27313 cp_lexer_consume_token (parser
->lexer
);
27316 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27317 /* Handle function try blocks. */
27318 if (function_try_block
)
27319 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27320 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27321 last
= parser
->lexer
->next_token
;
27323 /* Save away the inline definition; we will process it when the
27324 class is complete. */
27325 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27326 DECL_PENDING_INLINE_P (fn
) = 1;
27328 /* We need to know that this was defined in the class, so that
27329 friend templates are handled correctly. */
27330 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27332 /* Add FN to the queue of functions to be parsed later. */
27333 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27338 /* Save the tokens that make up the in-class initializer for a non-static
27339 data member. Returns a DEFAULT_ARG. */
27342 cp_parser_save_nsdmi (cp_parser
* parser
)
27344 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27347 /* Parse a template-argument-list, as well as the trailing ">" (but
27348 not the opening "<"). See cp_parser_template_argument_list for the
27352 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27356 tree saved_qualifying_scope
;
27357 tree saved_object_scope
;
27358 bool saved_greater_than_is_operator_p
;
27359 int saved_unevaluated_operand
;
27360 int saved_inhibit_evaluation_warnings
;
27364 When parsing a template-id, the first non-nested `>' is taken as
27365 the end of the template-argument-list rather than a greater-than
27367 saved_greater_than_is_operator_p
27368 = parser
->greater_than_is_operator_p
;
27369 parser
->greater_than_is_operator_p
= false;
27370 /* Parsing the argument list may modify SCOPE, so we save it
27372 saved_scope
= parser
->scope
;
27373 saved_qualifying_scope
= parser
->qualifying_scope
;
27374 saved_object_scope
= parser
->object_scope
;
27375 /* We need to evaluate the template arguments, even though this
27376 template-id may be nested within a "sizeof". */
27377 saved_unevaluated_operand
= cp_unevaluated_operand
;
27378 cp_unevaluated_operand
= 0;
27379 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27380 c_inhibit_evaluation_warnings
= 0;
27381 /* Parse the template-argument-list itself. */
27382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27383 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27384 arguments
= NULL_TREE
;
27386 arguments
= cp_parser_template_argument_list (parser
);
27387 /* Look for the `>' that ends the template-argument-list. If we find
27388 a '>>' instead, it's probably just a typo. */
27389 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27391 if (cxx_dialect
!= cxx98
)
27393 /* In C++0x, a `>>' in a template argument list or cast
27394 expression is considered to be two separate `>'
27395 tokens. So, change the current token to a `>', but don't
27396 consume it: it will be consumed later when the outer
27397 template argument list (or cast expression) is parsed.
27398 Note that this replacement of `>' for `>>' is necessary
27399 even if we are parsing tentatively: in the tentative
27400 case, after calling
27401 cp_parser_enclosed_template_argument_list we will always
27402 throw away all of the template arguments and the first
27403 closing `>', either because the template argument list
27404 was erroneous or because we are replacing those tokens
27405 with a CPP_TEMPLATE_ID token. The second `>' (which will
27406 not have been thrown away) is needed either to close an
27407 outer template argument list or to complete a new-style
27409 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27410 token
->type
= CPP_GREATER
;
27412 else if (!saved_greater_than_is_operator_p
)
27414 /* If we're in a nested template argument list, the '>>' has
27415 to be a typo for '> >'. We emit the error message, but we
27416 continue parsing and we push a '>' as next token, so that
27417 the argument list will be parsed correctly. Note that the
27418 global source location is still on the token before the
27419 '>>', so we need to say explicitly where we want it. */
27420 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27421 gcc_rich_location
richloc (token
->location
);
27422 richloc
.add_fixit_replace ("> >");
27423 error_at (&richloc
, "%<>>%> should be %<> >%> "
27424 "within a nested template argument list");
27426 token
->type
= CPP_GREATER
;
27430 /* If this is not a nested template argument list, the '>>'
27431 is a typo for '>'. Emit an error message and continue.
27432 Same deal about the token location, but here we can get it
27433 right by consuming the '>>' before issuing the diagnostic. */
27434 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27435 error_at (token
->location
,
27436 "spurious %<>>%>, use %<>%> to terminate "
27437 "a template argument list");
27441 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27442 /* The `>' token might be a greater-than operator again now. */
27443 parser
->greater_than_is_operator_p
27444 = saved_greater_than_is_operator_p
;
27445 /* Restore the SAVED_SCOPE. */
27446 parser
->scope
= saved_scope
;
27447 parser
->qualifying_scope
= saved_qualifying_scope
;
27448 parser
->object_scope
= saved_object_scope
;
27449 cp_unevaluated_operand
= saved_unevaluated_operand
;
27450 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27455 /* MEMBER_FUNCTION is a member function, or a friend. If default
27456 arguments, or the body of the function have not yet been parsed,
27460 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27462 timevar_push (TV_PARSE_INMETH
);
27463 /* If this member is a template, get the underlying
27465 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27466 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27468 /* There should not be any class definitions in progress at this
27469 point; the bodies of members are only parsed outside of all class
27471 gcc_assert (parser
->num_classes_being_defined
== 0);
27472 /* While we're parsing the member functions we might encounter more
27473 classes. We want to handle them right away, but we don't want
27474 them getting mixed up with functions that are currently in the
27476 push_unparsed_function_queues (parser
);
27478 /* Make sure that any template parameters are in scope. */
27479 maybe_begin_member_template_processing (member_function
);
27481 /* If the body of the function has not yet been parsed, parse it
27483 if (DECL_PENDING_INLINE_P (member_function
))
27485 tree function_scope
;
27486 cp_token_cache
*tokens
;
27488 /* The function is no longer pending; we are processing it. */
27489 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27490 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27491 DECL_PENDING_INLINE_P (member_function
) = 0;
27493 /* If this is a local class, enter the scope of the containing
27495 function_scope
= current_function_decl
;
27496 if (function_scope
)
27497 push_function_context ();
27499 /* Push the body of the function onto the lexer stack. */
27500 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27502 /* Let the front end know that we going to be defining this
27504 start_preparsed_function (member_function
, NULL_TREE
,
27505 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27507 /* Don't do access checking if it is a templated function. */
27508 if (processing_template_decl
)
27509 push_deferring_access_checks (dk_no_check
);
27511 /* #pragma omp declare reduction needs special parsing. */
27512 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27514 parser
->lexer
->in_pragma
= true;
27515 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27516 finish_function (/*inline_p=*/true);
27517 cp_check_omp_declare_reduction (member_function
);
27520 /* Now, parse the body of the function. */
27521 cp_parser_function_definition_after_declarator (parser
,
27522 /*inline_p=*/true);
27524 if (processing_template_decl
)
27525 pop_deferring_access_checks ();
27527 /* Leave the scope of the containing function. */
27528 if (function_scope
)
27529 pop_function_context ();
27530 cp_parser_pop_lexer (parser
);
27533 /* Remove any template parameters from the symbol table. */
27534 maybe_end_member_template_processing ();
27536 /* Restore the queue. */
27537 pop_unparsed_function_queues (parser
);
27538 timevar_pop (TV_PARSE_INMETH
);
27541 /* If DECL contains any default args, remember it on the unparsed
27542 functions queue. */
27545 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27549 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27551 probe
= TREE_CHAIN (probe
))
27552 if (TREE_PURPOSE (probe
))
27554 cp_default_arg_entry entry
= {current_class_type
, decl
};
27555 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27560 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27561 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27562 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27563 from the parameter-type-list. */
27566 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27567 tree default_arg
, tree parmtype
)
27569 cp_token_cache
*tokens
;
27573 if (default_arg
== error_mark_node
)
27574 return error_mark_node
;
27576 /* Push the saved tokens for the default argument onto the parser's
27578 tokens
= DEFARG_TOKENS (default_arg
);
27579 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27581 start_lambda_scope (decl
);
27583 /* Parse the default argument. */
27584 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27585 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27586 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27588 finish_lambda_scope ();
27590 if (parsed_arg
== error_mark_node
)
27591 cp_parser_skip_to_end_of_statement (parser
);
27593 if (!processing_template_decl
)
27595 /* In a non-template class, check conversions now. In a template,
27596 we'll wait and instantiate these as needed. */
27597 if (TREE_CODE (decl
) == PARM_DECL
)
27598 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27599 tf_warning_or_error
);
27600 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27601 parsed_arg
= error_mark_node
;
27603 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27606 /* If the token stream has not been completely used up, then
27607 there was extra junk after the end of the default
27609 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27611 if (TREE_CODE (decl
) == PARM_DECL
)
27612 cp_parser_error (parser
, "expected %<,%>");
27614 cp_parser_error (parser
, "expected %<;%>");
27617 /* Revert to the main lexer. */
27618 cp_parser_pop_lexer (parser
);
27623 /* FIELD is a non-static data member with an initializer which we saved for
27624 later; parse it now. */
27627 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27631 maybe_begin_member_template_processing (field
);
27633 push_unparsed_function_queues (parser
);
27634 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27635 DECL_INITIAL (field
),
27637 pop_unparsed_function_queues (parser
);
27639 maybe_end_member_template_processing ();
27641 DECL_INITIAL (field
) = def
;
27644 /* FN is a FUNCTION_DECL which may contains a parameter with an
27645 unparsed DEFAULT_ARG. Parse the default args now. This function
27646 assumes that the current scope is the scope in which the default
27647 argument should be processed. */
27650 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27652 bool saved_local_variables_forbidden_p
;
27653 tree parm
, parmdecl
;
27655 /* While we're parsing the default args, we might (due to the
27656 statement expression extension) encounter more classes. We want
27657 to handle them right away, but we don't want them getting mixed
27658 up with default args that are currently in the queue. */
27659 push_unparsed_function_queues (parser
);
27661 /* Local variable names (and the `this' keyword) may not appear
27662 in a default argument. */
27663 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27664 parser
->local_variables_forbidden_p
= true;
27666 push_defarg_context (fn
);
27668 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27669 parmdecl
= DECL_ARGUMENTS (fn
);
27670 parm
&& parm
!= void_list_node
;
27671 parm
= TREE_CHAIN (parm
),
27672 parmdecl
= DECL_CHAIN (parmdecl
))
27674 tree default_arg
= TREE_PURPOSE (parm
);
27676 vec
<tree
, va_gc
> *insts
;
27683 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27684 /* This can happen for a friend declaration for a function
27685 already declared with default arguments. */
27689 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27691 TREE_VALUE (parm
));
27692 TREE_PURPOSE (parm
) = parsed_arg
;
27694 /* Update any instantiations we've already created. */
27695 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27696 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27697 TREE_PURPOSE (copy
) = parsed_arg
;
27700 pop_defarg_context ();
27702 /* Make sure no default arg is missing. */
27703 check_default_args (fn
);
27705 /* Restore the state of local_variables_forbidden_p. */
27706 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27708 /* Restore the queue. */
27709 pop_unparsed_function_queues (parser
);
27712 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27714 sizeof ... ( identifier )
27716 where the 'sizeof' token has already been consumed. */
27719 cp_parser_sizeof_pack (cp_parser
*parser
)
27721 /* Consume the `...'. */
27722 cp_lexer_consume_token (parser
->lexer
);
27723 maybe_warn_variadic_templates ();
27725 matching_parens parens
;
27726 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27728 parens
.consume_open (parser
);
27730 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27731 "%<sizeof...%> argument must be surrounded by parentheses");
27733 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27734 tree name
= cp_parser_identifier (parser
);
27735 if (name
== error_mark_node
)
27736 return error_mark_node
;
27737 /* The name is not qualified. */
27738 parser
->scope
= NULL_TREE
;
27739 parser
->qualifying_scope
= NULL_TREE
;
27740 parser
->object_scope
= NULL_TREE
;
27741 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27742 if (expr
== error_mark_node
)
27743 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27745 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27746 expr
= TREE_TYPE (expr
);
27747 else if (TREE_CODE (expr
) == CONST_DECL
)
27748 expr
= DECL_INITIAL (expr
);
27749 expr
= make_pack_expansion (expr
);
27750 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27753 parens
.require_close (parser
);
27758 /* Parse the operand of `sizeof' (or a similar operator). Returns
27759 either a TYPE or an expression, depending on the form of the
27760 input. The KEYWORD indicates which kind of expression we have
27764 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27766 tree expr
= NULL_TREE
;
27767 const char *saved_message
;
27769 bool saved_integral_constant_expression_p
;
27770 bool saved_non_integral_constant_expression_p
;
27772 /* If it's a `...', then we are computing the length of a parameter
27774 if (keyword
== RID_SIZEOF
27775 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27776 return cp_parser_sizeof_pack (parser
);
27778 /* Types cannot be defined in a `sizeof' expression. Save away the
27780 saved_message
= parser
->type_definition_forbidden_message
;
27781 /* And create the new one. */
27782 tmp
= concat ("types may not be defined in %<",
27783 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27784 "%> expressions", NULL
);
27785 parser
->type_definition_forbidden_message
= tmp
;
27787 /* The restrictions on constant-expressions do not apply inside
27788 sizeof expressions. */
27789 saved_integral_constant_expression_p
27790 = parser
->integral_constant_expression_p
;
27791 saved_non_integral_constant_expression_p
27792 = parser
->non_integral_constant_expression_p
;
27793 parser
->integral_constant_expression_p
= false;
27795 /* Do not actually evaluate the expression. */
27796 ++cp_unevaluated_operand
;
27797 ++c_inhibit_evaluation_warnings
;
27798 /* If it's a `(', then we might be looking at the type-id
27800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27802 tree type
= NULL_TREE
;
27804 /* We can't be sure yet whether we're looking at a type-id or an
27806 cp_parser_parse_tentatively (parser
);
27808 matching_parens parens
;
27809 parens
.consume_open (parser
);
27811 /* Note: as a GNU Extension, compound literals are considered
27812 postfix-expressions as they are in C99, so they are valid
27813 arguments to sizeof. See comment in cp_parser_cast_expression
27815 if (cp_parser_compound_literal_p (parser
))
27816 cp_parser_simulate_error (parser
);
27819 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27820 parser
->in_type_id_in_expr_p
= true;
27821 /* Look for the type-id. */
27822 type
= cp_parser_type_id (parser
);
27823 /* Look for the closing `)'. */
27824 parens
.require_close (parser
);
27825 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
27828 /* If all went well, then we're done. */
27829 if (cp_parser_parse_definitely (parser
))
27831 cp_decl_specifier_seq decl_specs
;
27833 /* Build a trivial decl-specifier-seq. */
27834 clear_decl_specs (&decl_specs
);
27835 decl_specs
.type
= type
;
27837 /* Call grokdeclarator to figure out what type this is. */
27838 expr
= grokdeclarator (NULL
,
27842 /*attrlist=*/NULL
);
27846 /* If the type-id production did not work out, then we must be
27847 looking at the unary-expression production. */
27849 expr
= cp_parser_unary_expression (parser
);
27851 /* Go back to evaluating expressions. */
27852 --cp_unevaluated_operand
;
27853 --c_inhibit_evaluation_warnings
;
27855 /* Free the message we created. */
27857 /* And restore the old one. */
27858 parser
->type_definition_forbidden_message
= saved_message
;
27859 parser
->integral_constant_expression_p
27860 = saved_integral_constant_expression_p
;
27861 parser
->non_integral_constant_expression_p
27862 = saved_non_integral_constant_expression_p
;
27867 /* If the current declaration has no declarator, return true. */
27870 cp_parser_declares_only_class_p (cp_parser
*parser
)
27872 /* If the next token is a `;' or a `,' then there is no
27874 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27875 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
27878 /* Update the DECL_SPECS to reflect the storage class indicated by
27882 cp_parser_set_storage_class (cp_parser
*parser
,
27883 cp_decl_specifier_seq
*decl_specs
,
27887 cp_storage_class storage_class
;
27889 if (parser
->in_unbraced_linkage_specification_p
)
27891 error_at (token
->location
, "invalid use of %qD in linkage specification",
27892 ridpointers
[keyword
]);
27895 else if (decl_specs
->storage_class
!= sc_none
)
27897 decl_specs
->conflicting_specifiers_p
= true;
27901 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
27902 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
27903 && decl_specs
->gnu_thread_keyword_p
)
27905 pedwarn (decl_specs
->locations
[ds_thread
], 0,
27906 "%<__thread%> before %qD", ridpointers
[keyword
]);
27912 storage_class
= sc_auto
;
27915 storage_class
= sc_register
;
27918 storage_class
= sc_static
;
27921 storage_class
= sc_extern
;
27924 storage_class
= sc_mutable
;
27927 gcc_unreachable ();
27929 decl_specs
->storage_class
= storage_class
;
27930 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
27932 /* A storage class specifier cannot be applied alongside a typedef
27933 specifier. If there is a typedef specifier present then set
27934 conflicting_specifiers_p which will trigger an error later
27935 on in grokdeclarator. */
27936 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
27937 decl_specs
->conflicting_specifiers_p
= true;
27940 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27941 is true, the type is a class or enum definition. */
27944 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
27947 bool type_definition_p
)
27949 decl_specs
->any_specifiers_p
= true;
27951 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27952 (with, for example, in "typedef int wchar_t;") we remember that
27953 this is what happened. In system headers, we ignore these
27954 declarations so that G++ can work with system headers that are not
27956 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
27957 && !type_definition_p
27958 && (type_spec
== boolean_type_node
27959 || type_spec
== char16_type_node
27960 || type_spec
== char32_type_node
27961 || type_spec
== wchar_type_node
)
27962 && (decl_specs
->type
27963 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
27964 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
27965 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
27966 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
27968 decl_specs
->redefined_builtin_type
= type_spec
;
27969 set_and_check_decl_spec_loc (decl_specs
,
27970 ds_redefined_builtin_type_spec
,
27972 if (!decl_specs
->type
)
27974 decl_specs
->type
= type_spec
;
27975 decl_specs
->type_definition_p
= false;
27976 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
27979 else if (decl_specs
->type
)
27980 decl_specs
->multiple_types_p
= true;
27983 decl_specs
->type
= type_spec
;
27984 decl_specs
->type_definition_p
= type_definition_p
;
27985 decl_specs
->redefined_builtin_type
= NULL_TREE
;
27986 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
27990 /* True iff TOKEN is the GNU keyword __thread. */
27993 token_is__thread (cp_token
*token
)
27995 gcc_assert (token
->keyword
== RID_THREAD
);
27996 return id_equal (token
->u
.value
, "__thread");
27999 /* Set the location for a declarator specifier and check if it is
28002 DECL_SPECS is the sequence of declarator specifiers onto which to
28005 DS is the single declarator specifier to set which location is to
28006 be set onto the existing sequence of declarators.
28008 LOCATION is the location for the declarator specifier to
28012 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28013 cp_decl_spec ds
, cp_token
*token
)
28015 gcc_assert (ds
< ds_last
);
28017 if (decl_specs
== NULL
)
28020 source_location location
= token
->location
;
28022 if (decl_specs
->locations
[ds
] == 0)
28024 decl_specs
->locations
[ds
] = location
;
28025 if (ds
== ds_thread
)
28026 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28032 if (decl_specs
->locations
[ds_long_long
] != 0)
28033 error_at (location
,
28034 "%<long long long%> is too long for GCC");
28037 decl_specs
->locations
[ds_long_long
] = location
;
28038 pedwarn_cxx98 (location
,
28040 "ISO C++ 1998 does not support %<long long%>");
28043 else if (ds
== ds_thread
)
28045 bool gnu
= token_is__thread (token
);
28046 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28047 error_at (location
,
28048 "both %<__thread%> and %<thread_local%> specified");
28051 gcc_rich_location
richloc (location
);
28052 richloc
.add_fixit_remove ();
28053 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28058 static const char *const decl_spec_names
[] = {
28075 gcc_rich_location
richloc (location
);
28076 richloc
.add_fixit_remove ();
28077 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28082 /* Return true iff the declarator specifier DS is present in the
28083 sequence of declarator specifiers DECL_SPECS. */
28086 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28089 gcc_assert (ds
< ds_last
);
28091 if (decl_specs
== NULL
)
28094 return decl_specs
->locations
[ds
] != 0;
28097 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28098 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28101 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28103 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28106 /* Issue an error message indicating that TOKEN_DESC was expected.
28107 If KEYWORD is true, it indicated this function is called by
28108 cp_parser_require_keword and the required token can only be
28109 a indicated keyword.
28111 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28112 within any error as the location of an "opening" token matching
28113 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28114 RT_CLOSE_PAREN). */
28117 cp_parser_required_error (cp_parser
*parser
,
28118 required_token token_desc
,
28120 location_t matching_location
)
28122 if (cp_parser_simulate_error (parser
))
28125 const char *gmsgid
= NULL
;
28126 switch (token_desc
)
28129 gmsgid
= G_("expected %<new%>");
28132 gmsgid
= G_("expected %<delete%>");
28135 gmsgid
= G_("expected %<return%>");
28138 gmsgid
= G_("expected %<while%>");
28141 gmsgid
= G_("expected %<extern%>");
28143 case RT_STATIC_ASSERT
:
28144 gmsgid
= G_("expected %<static_assert%>");
28147 gmsgid
= G_("expected %<decltype%>");
28150 gmsgid
= G_("expected %<operator%>");
28153 gmsgid
= G_("expected %<class%>");
28156 gmsgid
= G_("expected %<template%>");
28159 gmsgid
= G_("expected %<namespace%>");
28162 gmsgid
= G_("expected %<using%>");
28165 gmsgid
= G_("expected %<asm%>");
28168 gmsgid
= G_("expected %<try%>");
28171 gmsgid
= G_("expected %<catch%>");
28174 gmsgid
= G_("expected %<throw%>");
28177 gmsgid
= G_("expected %<__label__%>");
28180 gmsgid
= G_("expected %<@try%>");
28182 case RT_AT_SYNCHRONIZED
:
28183 gmsgid
= G_("expected %<@synchronized%>");
28186 gmsgid
= G_("expected %<@throw%>");
28188 case RT_TRANSACTION_ATOMIC
:
28189 gmsgid
= G_("expected %<__transaction_atomic%>");
28191 case RT_TRANSACTION_RELAXED
:
28192 gmsgid
= G_("expected %<__transaction_relaxed%>");
28198 if (!gmsgid
&& !keyword
)
28200 switch (token_desc
)
28203 gmsgid
= G_("expected %<;%>");
28205 case RT_OPEN_PAREN
:
28206 gmsgid
= G_("expected %<(%>");
28208 case RT_CLOSE_BRACE
:
28209 gmsgid
= G_("expected %<}%>");
28211 case RT_OPEN_BRACE
:
28212 gmsgid
= G_("expected %<{%>");
28214 case RT_CLOSE_SQUARE
:
28215 gmsgid
= G_("expected %<]%>");
28217 case RT_OPEN_SQUARE
:
28218 gmsgid
= G_("expected %<[%>");
28221 gmsgid
= G_("expected %<,%>");
28224 gmsgid
= G_("expected %<::%>");
28227 gmsgid
= G_("expected %<<%>");
28230 gmsgid
= G_("expected %<>%>");
28233 gmsgid
= G_("expected %<=%>");
28236 gmsgid
= G_("expected %<...%>");
28239 gmsgid
= G_("expected %<*%>");
28242 gmsgid
= G_("expected %<~%>");
28245 gmsgid
= G_("expected %<:%>");
28247 case RT_COLON_SCOPE
:
28248 gmsgid
= G_("expected %<:%> or %<::%>");
28250 case RT_CLOSE_PAREN
:
28251 gmsgid
= G_("expected %<)%>");
28253 case RT_COMMA_CLOSE_PAREN
:
28254 gmsgid
= G_("expected %<,%> or %<)%>");
28256 case RT_PRAGMA_EOL
:
28257 gmsgid
= G_("expected end of line");
28260 gmsgid
= G_("expected identifier");
28263 gmsgid
= G_("expected selection-statement");
28266 gmsgid
= G_("expected iteration-statement");
28269 gmsgid
= G_("expected jump-statement");
28272 gmsgid
= G_("expected class-key");
28274 case RT_CLASS_TYPENAME_TEMPLATE
:
28275 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28278 gcc_unreachable ();
28283 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28287 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28288 issue an error message indicating that TOKEN_DESC was expected.
28290 Returns the token consumed, if the token had the appropriate type.
28291 Otherwise, returns NULL.
28293 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28294 within any error as the location of an "opening" token matching
28295 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28296 RT_CLOSE_PAREN). */
28299 cp_parser_require (cp_parser
* parser
,
28300 enum cpp_ttype type
,
28301 required_token token_desc
,
28302 location_t matching_location
)
28304 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28305 return cp_lexer_consume_token (parser
->lexer
);
28308 /* Output the MESSAGE -- unless we're parsing tentatively. */
28309 if (!cp_parser_simulate_error (parser
))
28310 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28311 matching_location
);
28316 /* An error message is produced if the next token is not '>'.
28317 All further tokens are skipped until the desired token is
28318 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28321 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28323 /* Current level of '< ... >'. */
28324 unsigned level
= 0;
28325 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28326 unsigned nesting_depth
= 0;
28328 /* Are we ready, yet? If not, issue error message. */
28329 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28332 /* Skip tokens until the desired token is found. */
28335 /* Peek at the next token. */
28336 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28339 if (!nesting_depth
)
28344 if (cxx_dialect
== cxx98
)
28345 /* C++0x views the `>>' operator as two `>' tokens, but
28348 else if (!nesting_depth
&& level
-- == 0)
28350 /* We've hit a `>>' where the first `>' closes the
28351 template argument list, and the second `>' is
28352 spurious. Just consume the `>>' and stop; we've
28353 already produced at least one error. */
28354 cp_lexer_consume_token (parser
->lexer
);
28357 /* Fall through for C++0x, so we handle the second `>' in
28359 gcc_fallthrough ();
28362 if (!nesting_depth
&& level
-- == 0)
28364 /* We've reached the token we want, consume it and stop. */
28365 cp_lexer_consume_token (parser
->lexer
);
28370 case CPP_OPEN_PAREN
:
28371 case CPP_OPEN_SQUARE
:
28375 case CPP_CLOSE_PAREN
:
28376 case CPP_CLOSE_SQUARE
:
28377 if (nesting_depth
-- == 0)
28382 case CPP_PRAGMA_EOL
:
28383 case CPP_SEMICOLON
:
28384 case CPP_OPEN_BRACE
:
28385 case CPP_CLOSE_BRACE
:
28386 /* The '>' was probably forgotten, don't look further. */
28393 /* Consume this token. */
28394 cp_lexer_consume_token (parser
->lexer
);
28398 /* If the next token is the indicated keyword, consume it. Otherwise,
28399 issue an error message indicating that TOKEN_DESC was expected.
28401 Returns the token consumed, if the token had the appropriate type.
28402 Otherwise, returns NULL. */
28405 cp_parser_require_keyword (cp_parser
* parser
,
28407 required_token token_desc
)
28409 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28411 if (token
&& token
->keyword
!= keyword
)
28413 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28421 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28422 function-definition. */
28425 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28427 return (/* An ordinary function-body begins with an `{'. */
28428 token
->type
== CPP_OPEN_BRACE
28429 /* A ctor-initializer begins with a `:'. */
28430 || token
->type
== CPP_COLON
28431 /* A function-try-block begins with `try'. */
28432 || token
->keyword
== RID_TRY
28433 /* A function-transaction-block begins with `__transaction_atomic'
28434 or `__transaction_relaxed'. */
28435 || token
->keyword
== RID_TRANSACTION_ATOMIC
28436 || token
->keyword
== RID_TRANSACTION_RELAXED
28437 /* The named return value extension begins with `return'. */
28438 || token
->keyword
== RID_RETURN
);
28441 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28445 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28449 token
= cp_lexer_peek_token (parser
->lexer
);
28450 return (token
->type
== CPP_OPEN_BRACE
28451 || (token
->type
== CPP_COLON
28452 && !parser
->colon_doesnt_start_class_def_p
));
28455 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28456 C++0x) ending a template-argument. */
28459 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28463 token
= cp_lexer_peek_token (parser
->lexer
);
28464 return (token
->type
== CPP_COMMA
28465 || token
->type
== CPP_GREATER
28466 || token
->type
== CPP_ELLIPSIS
28467 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28470 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28471 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28474 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28479 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28480 if (token
->type
== CPP_LESS
)
28482 /* Check for the sequence `<::' in the original code. It would be lexed as
28483 `[:', where `[' is a digraph, and there is no whitespace before
28485 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28488 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28489 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28495 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28496 or none_type otherwise. */
28498 static enum tag_types
28499 cp_parser_token_is_class_key (cp_token
* token
)
28501 switch (token
->keyword
)
28506 return record_type
;
28515 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28516 or none_type otherwise or if the token is null. */
28518 static enum tag_types
28519 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28524 switch (token
->keyword
)
28529 return typename_type
;
28536 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28539 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28541 if (type
== error_mark_node
)
28543 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28545 if (permerror (input_location
, "%qs tag used in naming %q#T",
28546 class_key
== union_type
? "union"
28547 : class_key
== record_type
? "struct" : "class",
28549 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28550 "%q#T was previously declared here", type
);
28554 /* Issue an error message if DECL is redeclared with different
28555 access than its original declaration [class.access.spec/3].
28556 This applies to nested classes, nested class templates and
28557 enumerations [class.mem/1]. */
28560 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28563 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28564 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28567 if ((TREE_PRIVATE (decl
)
28568 != (current_access_specifier
== access_private_node
))
28569 || (TREE_PROTECTED (decl
)
28570 != (current_access_specifier
== access_protected_node
)))
28571 error_at (location
, "%qD redeclared with different access", decl
);
28574 /* Look for the `template' keyword, as a syntactic disambiguator.
28575 Return TRUE iff it is present, in which case it will be
28579 cp_parser_optional_template_keyword (cp_parser
*parser
)
28581 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28583 /* In C++98 the `template' keyword can only be used within templates;
28584 outside templates the parser can always figure out what is a
28585 template and what is not. In C++11, per the resolution of DR 468,
28586 `template' is allowed in cases where it is not strictly necessary. */
28587 if (!processing_template_decl
28588 && pedantic
&& cxx_dialect
== cxx98
)
28590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28591 pedwarn (token
->location
, OPT_Wpedantic
,
28592 "in C++98 %<template%> (as a disambiguator) is only "
28593 "allowed within templates");
28594 /* If this part of the token stream is rescanned, the same
28595 error message would be generated. So, we purge the token
28596 from the stream. */
28597 cp_lexer_purge_token (parser
->lexer
);
28602 /* Consume the `template' keyword. */
28603 cp_lexer_consume_token (parser
->lexer
);
28610 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28611 set PARSER->SCOPE, and perform other related actions. */
28614 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28616 struct tree_check
*check_value
;
28618 /* Get the stored value. */
28619 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28620 /* Set the scope from the stored value. */
28621 parser
->scope
= saved_checks_value (check_value
);
28622 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28623 parser
->object_scope
= NULL_TREE
;
28626 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28627 encounter the end of a block before what we were looking for. */
28630 cp_parser_cache_group (cp_parser
*parser
,
28631 enum cpp_ttype end
,
28636 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28638 /* Abort a parenthesized expression if we encounter a semicolon. */
28639 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28640 && token
->type
== CPP_SEMICOLON
)
28642 /* If we've reached the end of the file, stop. */
28643 if (token
->type
== CPP_EOF
28644 || (end
!= CPP_PRAGMA_EOL
28645 && token
->type
== CPP_PRAGMA_EOL
))
28647 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28648 /* We've hit the end of an enclosing block, so there's been some
28649 kind of syntax error. */
28652 /* Consume the token. */
28653 cp_lexer_consume_token (parser
->lexer
);
28654 /* See if it starts a new group. */
28655 if (token
->type
== CPP_OPEN_BRACE
)
28657 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28658 /* In theory this should probably check end == '}', but
28659 cp_parser_save_member_function_body needs it to exit
28660 after either '}' or ')' when called with ')'. */
28664 else if (token
->type
== CPP_OPEN_PAREN
)
28666 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28667 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28670 else if (token
->type
== CPP_PRAGMA
)
28671 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28672 else if (token
->type
== end
)
28677 /* Like above, for caching a default argument or NSDMI. Both of these are
28678 terminated by a non-nested comma, but it can be unclear whether or not a
28679 comma is nested in a template argument list unless we do more parsing.
28680 In order to handle this ambiguity, when we encounter a ',' after a '<'
28681 we try to parse what follows as a parameter-declaration-list (in the
28682 case of a default argument) or a member-declarator (in the case of an
28683 NSDMI). If that succeeds, then we stop caching. */
28686 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28688 unsigned depth
= 0;
28689 int maybe_template_id
= 0;
28690 cp_token
*first_token
;
28692 tree default_argument
;
28694 /* Add tokens until we have processed the entire default
28695 argument. We add the range [first_token, token). */
28696 first_token
= cp_lexer_peek_token (parser
->lexer
);
28697 if (first_token
->type
== CPP_OPEN_BRACE
)
28699 /* For list-initialization, this is straightforward. */
28700 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28701 token
= cp_lexer_peek_token (parser
->lexer
);
28707 /* Peek at the next token. */
28708 token
= cp_lexer_peek_token (parser
->lexer
);
28709 /* What we do depends on what token we have. */
28710 switch (token
->type
)
28712 /* In valid code, a default argument must be
28713 immediately followed by a `,' `)', or `...'. */
28715 if (depth
== 0 && maybe_template_id
)
28717 /* If we've seen a '<', we might be in a
28718 template-argument-list. Until Core issue 325 is
28719 resolved, we don't know how this situation ought
28720 to be handled, so try to DTRT. We check whether
28721 what comes after the comma is a valid parameter
28722 declaration list. If it is, then the comma ends
28723 the default argument; otherwise the default
28724 argument continues. */
28725 bool error
= false;
28728 /* Set ITALP so cp_parser_parameter_declaration_list
28729 doesn't decide to commit to this parse. */
28730 bool saved_italp
= parser
->in_template_argument_list_p
;
28731 parser
->in_template_argument_list_p
= true;
28733 cp_parser_parse_tentatively (parser
);
28737 /* Parse declarators until we reach a non-comma or
28738 somthing that cannot be an initializer.
28739 Just checking whether we're looking at a single
28740 declarator is insufficient. Consider:
28741 int var = tuple<T,U>::x;
28742 The template parameter 'U' looks exactly like a
28746 int ctor_dtor_or_conv_p
;
28747 cp_lexer_consume_token (parser
->lexer
);
28748 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28749 &ctor_dtor_or_conv_p
,
28750 /*parenthesized_p=*/NULL
,
28752 /*friend_p=*/false);
28753 peek
= cp_lexer_peek_token (parser
->lexer
);
28754 if (cp_parser_error_occurred (parser
))
28757 while (peek
->type
== CPP_COMMA
);
28758 /* If we met an '=' or ';' then the original comma
28759 was the end of the NSDMI. Otherwise assume
28760 we're still in the NSDMI. */
28761 error
= (peek
->type
!= CPP_EQ
28762 && peek
->type
!= CPP_SEMICOLON
);
28766 cp_lexer_consume_token (parser
->lexer
);
28767 begin_scope (sk_function_parms
, NULL_TREE
);
28768 cp_parser_parameter_declaration_list (parser
, &error
);
28769 pop_bindings_and_leave_scope ();
28771 if (!cp_parser_error_occurred (parser
) && !error
)
28773 cp_parser_abort_tentative_parse (parser
);
28775 parser
->in_template_argument_list_p
= saved_italp
;
28779 case CPP_CLOSE_PAREN
:
28781 /* If we run into a non-nested `;', `}', or `]',
28782 then the code is invalid -- but the default
28783 argument is certainly over. */
28784 case CPP_SEMICOLON
:
28785 case CPP_CLOSE_BRACE
:
28786 case CPP_CLOSE_SQUARE
:
28788 /* Handle correctly int n = sizeof ... ( p ); */
28789 && token
->type
!= CPP_ELLIPSIS
)
28791 /* Update DEPTH, if necessary. */
28792 else if (token
->type
== CPP_CLOSE_PAREN
28793 || token
->type
== CPP_CLOSE_BRACE
28794 || token
->type
== CPP_CLOSE_SQUARE
)
28798 case CPP_OPEN_PAREN
:
28799 case CPP_OPEN_SQUARE
:
28800 case CPP_OPEN_BRACE
:
28806 /* This might be the comparison operator, or it might
28807 start a template argument list. */
28808 ++maybe_template_id
;
28812 if (cxx_dialect
== cxx98
)
28814 /* Fall through for C++0x, which treats the `>>'
28815 operator like two `>' tokens in certain
28817 gcc_fallthrough ();
28822 /* This might be an operator, or it might close a
28823 template argument list. But if a previous '<'
28824 started a template argument list, this will have
28825 closed it, so we can't be in one anymore. */
28826 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
28827 if (maybe_template_id
< 0)
28828 maybe_template_id
= 0;
28832 /* If we run out of tokens, issue an error message. */
28834 case CPP_PRAGMA_EOL
:
28835 error_at (token
->location
, "file ends in default argument");
28836 return error_mark_node
;
28840 /* In these cases, we should look for template-ids.
28841 For example, if the default argument is
28842 `X<int, double>()', we need to do name lookup to
28843 figure out whether or not `X' is a template; if
28844 so, the `,' does not end the default argument.
28846 That is not yet done. */
28853 /* If we've reached the end, stop. */
28857 /* Add the token to the token block. */
28858 token
= cp_lexer_consume_token (parser
->lexer
);
28861 /* Create a DEFAULT_ARG to represent the unparsed default
28863 default_argument
= make_node (DEFAULT_ARG
);
28864 DEFARG_TOKENS (default_argument
)
28865 = cp_token_cache_new (first_token
, token
);
28866 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
28868 return default_argument
;
28871 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28874 defarg_location (tree default_argument
)
28876 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
28877 location_t start
= tokens
->first
->location
;
28878 location_t end
= tokens
->last
->location
;
28879 return make_location (start
, start
, end
);
28882 /* Begin parsing tentatively. We always save tokens while parsing
28883 tentatively so that if the tentative parsing fails we can restore the
28887 cp_parser_parse_tentatively (cp_parser
* parser
)
28889 /* Enter a new parsing context. */
28890 parser
->context
= cp_parser_context_new (parser
->context
);
28891 /* Begin saving tokens. */
28892 cp_lexer_save_tokens (parser
->lexer
);
28893 /* In order to avoid repetitive access control error messages,
28894 access checks are queued up until we are no longer parsing
28896 push_deferring_access_checks (dk_deferred
);
28899 /* Commit to the currently active tentative parse. */
28902 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
28904 cp_parser_context
*context
;
28907 /* Mark all of the levels as committed. */
28908 lexer
= parser
->lexer
;
28909 for (context
= parser
->context
; context
->next
; context
= context
->next
)
28911 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28913 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28914 while (!cp_lexer_saving_tokens (lexer
))
28915 lexer
= lexer
->next
;
28916 cp_lexer_commit_tokens (lexer
);
28920 /* Commit to the topmost currently active tentative parse.
28922 Note that this function shouldn't be called when there are
28923 irreversible side-effects while in a tentative state. For
28924 example, we shouldn't create a permanent entry in the symbol
28925 table, or issue an error message that might not apply if the
28926 tentative parse is aborted. */
28929 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
28931 cp_parser_context
*context
= parser
->context
;
28932 cp_lexer
*lexer
= parser
->lexer
;
28936 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28938 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28940 while (!cp_lexer_saving_tokens (lexer
))
28941 lexer
= lexer
->next
;
28942 cp_lexer_commit_tokens (lexer
);
28946 /* Abort the currently active tentative parse. All consumed tokens
28947 will be rolled back, and no diagnostics will be issued. */
28950 cp_parser_abort_tentative_parse (cp_parser
* parser
)
28952 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
28953 || errorcount
> 0);
28954 cp_parser_simulate_error (parser
);
28955 /* Now, pretend that we want to see if the construct was
28956 successfully parsed. */
28957 cp_parser_parse_definitely (parser
);
28960 /* Stop parsing tentatively. If a parse error has occurred, restore the
28961 token stream. Otherwise, commit to the tokens we have consumed.
28962 Returns true if no error occurred; false otherwise. */
28965 cp_parser_parse_definitely (cp_parser
* parser
)
28967 bool error_occurred
;
28968 cp_parser_context
*context
;
28970 /* Remember whether or not an error occurred, since we are about to
28971 destroy that information. */
28972 error_occurred
= cp_parser_error_occurred (parser
);
28973 /* Remove the topmost context from the stack. */
28974 context
= parser
->context
;
28975 parser
->context
= context
->next
;
28976 /* If no parse errors occurred, commit to the tentative parse. */
28977 if (!error_occurred
)
28979 /* Commit to the tokens read tentatively, unless that was
28981 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
28982 cp_lexer_commit_tokens (parser
->lexer
);
28984 pop_to_parent_deferring_access_checks ();
28986 /* Otherwise, if errors occurred, roll back our state so that things
28987 are just as they were before we began the tentative parse. */
28990 cp_lexer_rollback_tokens (parser
->lexer
);
28991 pop_deferring_access_checks ();
28993 /* Add the context to the front of the free list. */
28994 context
->next
= cp_parser_context_free_list
;
28995 cp_parser_context_free_list
= context
;
28997 return !error_occurred
;
29000 /* Returns true if we are parsing tentatively and are not committed to
29001 this tentative parse. */
29004 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29006 return (cp_parser_parsing_tentatively (parser
)
29007 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29010 /* Returns nonzero iff an error has occurred during the most recent
29011 tentative parse. */
29014 cp_parser_error_occurred (cp_parser
* parser
)
29016 return (cp_parser_parsing_tentatively (parser
)
29017 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29020 /* Returns nonzero if GNU extensions are allowed. */
29023 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29025 return parser
->allow_gnu_extensions_p
;
29028 /* Objective-C++ Productions */
29031 /* Parse an Objective-C expression, which feeds into a primary-expression
29035 objc-message-expression
29036 objc-string-literal
29037 objc-encode-expression
29038 objc-protocol-expression
29039 objc-selector-expression
29041 Returns a tree representation of the expression. */
29044 cp_parser_objc_expression (cp_parser
* parser
)
29046 /* Try to figure out what kind of declaration is present. */
29047 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29051 case CPP_OPEN_SQUARE
:
29052 return cp_parser_objc_message_expression (parser
);
29054 case CPP_OBJC_STRING
:
29055 kwd
= cp_lexer_consume_token (parser
->lexer
);
29056 return objc_build_string_object (kwd
->u
.value
);
29059 switch (kwd
->keyword
)
29061 case RID_AT_ENCODE
:
29062 return cp_parser_objc_encode_expression (parser
);
29064 case RID_AT_PROTOCOL
:
29065 return cp_parser_objc_protocol_expression (parser
);
29067 case RID_AT_SELECTOR
:
29068 return cp_parser_objc_selector_expression (parser
);
29075 error_at (kwd
->location
,
29076 "misplaced %<@%D%> Objective-C++ construct",
29078 cp_parser_skip_to_end_of_block_or_statement (parser
);
29081 return error_mark_node
;
29084 /* Parse an Objective-C message expression.
29086 objc-message-expression:
29087 [ objc-message-receiver objc-message-args ]
29089 Returns a representation of an Objective-C message. */
29092 cp_parser_objc_message_expression (cp_parser
* parser
)
29094 tree receiver
, messageargs
;
29096 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29097 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29098 receiver
= cp_parser_objc_message_receiver (parser
);
29099 messageargs
= cp_parser_objc_message_args (parser
);
29100 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29101 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29103 tree result
= objc_build_message_expr (receiver
, messageargs
);
29105 /* Construct a location e.g.
29108 ranging from the '[' to the ']', with the caret at the start. */
29109 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29110 protected_set_expr_location (result
, combined_loc
);
29115 /* Parse an objc-message-receiver.
29117 objc-message-receiver:
29119 simple-type-specifier
29121 Returns a representation of the type or expression. */
29124 cp_parser_objc_message_receiver (cp_parser
* parser
)
29128 /* An Objective-C message receiver may be either (1) a type
29129 or (2) an expression. */
29130 cp_parser_parse_tentatively (parser
);
29131 rcv
= cp_parser_expression (parser
);
29133 /* If that worked out, fine. */
29134 if (cp_parser_parse_definitely (parser
))
29137 cp_parser_parse_tentatively (parser
);
29138 rcv
= cp_parser_simple_type_specifier (parser
,
29139 /*decl_specs=*/NULL
,
29140 CP_PARSER_FLAGS_NONE
);
29142 if (cp_parser_parse_definitely (parser
))
29143 return objc_get_class_reference (rcv
);
29145 cp_parser_error (parser
, "objective-c++ message receiver expected");
29146 return error_mark_node
;
29149 /* Parse the arguments and selectors comprising an Objective-C message.
29154 objc-selector-args , objc-comma-args
29156 objc-selector-args:
29157 objc-selector [opt] : assignment-expression
29158 objc-selector-args objc-selector [opt] : assignment-expression
29161 assignment-expression
29162 objc-comma-args , assignment-expression
29164 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29165 selector arguments and TREE_VALUE containing a list of comma
29169 cp_parser_objc_message_args (cp_parser
* parser
)
29171 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29172 bool maybe_unary_selector_p
= true;
29173 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29175 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29177 tree selector
= NULL_TREE
, arg
;
29179 if (token
->type
!= CPP_COLON
)
29180 selector
= cp_parser_objc_selector (parser
);
29182 /* Detect if we have a unary selector. */
29183 if (maybe_unary_selector_p
29184 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29185 return build_tree_list (selector
, NULL_TREE
);
29187 maybe_unary_selector_p
= false;
29188 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29189 arg
= cp_parser_assignment_expression (parser
);
29192 = chainon (sel_args
,
29193 build_tree_list (selector
, arg
));
29195 token
= cp_lexer_peek_token (parser
->lexer
);
29198 /* Handle non-selector arguments, if any. */
29199 while (token
->type
== CPP_COMMA
)
29203 cp_lexer_consume_token (parser
->lexer
);
29204 arg
= cp_parser_assignment_expression (parser
);
29207 = chainon (addl_args
,
29208 build_tree_list (NULL_TREE
, arg
));
29210 token
= cp_lexer_peek_token (parser
->lexer
);
29213 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29215 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29216 return build_tree_list (error_mark_node
, error_mark_node
);
29219 return build_tree_list (sel_args
, addl_args
);
29222 /* Parse an Objective-C encode expression.
29224 objc-encode-expression:
29225 @encode objc-typename
29227 Returns an encoded representation of the type argument. */
29230 cp_parser_objc_encode_expression (cp_parser
* parser
)
29234 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29236 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29237 matching_parens parens
;
29238 parens
.require_open (parser
);
29239 token
= cp_lexer_peek_token (parser
->lexer
);
29240 type
= complete_type (cp_parser_type_id (parser
));
29241 parens
.require_close (parser
);
29245 error_at (token
->location
,
29246 "%<@encode%> must specify a type as an argument");
29247 return error_mark_node
;
29250 /* This happens if we find @encode(T) (where T is a template
29251 typename or something dependent on a template typename) when
29252 parsing a template. In that case, we can't compile it
29253 immediately, but we rather create an AT_ENCODE_EXPR which will
29254 need to be instantiated when the template is used.
29256 if (dependent_type_p (type
))
29258 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29259 TREE_READONLY (value
) = 1;
29264 /* Build a location of the form:
29267 with caret==start at the @ token, finishing at the close paren. */
29268 location_t combined_loc
29269 = make_location (start_loc
, start_loc
,
29270 cp_lexer_previous_token (parser
->lexer
)->location
);
29272 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29275 /* Parse an Objective-C @defs expression. */
29278 cp_parser_objc_defs_expression (cp_parser
*parser
)
29282 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29283 matching_parens parens
;
29284 parens
.require_open (parser
);
29285 name
= cp_parser_identifier (parser
);
29286 parens
.require_close (parser
);
29288 return objc_get_class_ivars (name
);
29291 /* Parse an Objective-C protocol expression.
29293 objc-protocol-expression:
29294 @protocol ( identifier )
29296 Returns a representation of the protocol expression. */
29299 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29302 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29304 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29305 matching_parens parens
;
29306 parens
.require_open (parser
);
29307 proto
= cp_parser_identifier (parser
);
29308 parens
.require_close (parser
);
29310 /* Build a location of the form:
29313 with caret==start at the @ token, finishing at the close paren. */
29314 location_t combined_loc
29315 = make_location (start_loc
, start_loc
,
29316 cp_lexer_previous_token (parser
->lexer
)->location
);
29317 tree result
= objc_build_protocol_expr (proto
);
29318 protected_set_expr_location (result
, combined_loc
);
29322 /* Parse an Objective-C selector expression.
29324 objc-selector-expression:
29325 @selector ( objc-method-signature )
29327 objc-method-signature:
29333 objc-selector-seq objc-selector :
29335 Returns a representation of the method selector. */
29338 cp_parser_objc_selector_expression (cp_parser
* parser
)
29340 tree sel_seq
= NULL_TREE
;
29341 bool maybe_unary_selector_p
= true;
29343 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29345 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29346 matching_parens parens
;
29347 parens
.require_open (parser
);
29348 token
= cp_lexer_peek_token (parser
->lexer
);
29350 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29351 || token
->type
== CPP_SCOPE
)
29353 tree selector
= NULL_TREE
;
29355 if (token
->type
!= CPP_COLON
29356 || token
->type
== CPP_SCOPE
)
29357 selector
= cp_parser_objc_selector (parser
);
29359 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29360 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29362 /* Detect if we have a unary selector. */
29363 if (maybe_unary_selector_p
)
29365 sel_seq
= selector
;
29366 goto finish_selector
;
29370 cp_parser_error (parser
, "expected %<:%>");
29373 maybe_unary_selector_p
= false;
29374 token
= cp_lexer_consume_token (parser
->lexer
);
29376 if (token
->type
== CPP_SCOPE
)
29379 = chainon (sel_seq
,
29380 build_tree_list (selector
, NULL_TREE
));
29382 = chainon (sel_seq
,
29383 build_tree_list (NULL_TREE
, NULL_TREE
));
29387 = chainon (sel_seq
,
29388 build_tree_list (selector
, NULL_TREE
));
29390 token
= cp_lexer_peek_token (parser
->lexer
);
29394 parens
.require_close (parser
);
29397 /* Build a location of the form:
29400 with caret==start at the @ token, finishing at the close paren. */
29401 location_t combined_loc
29402 = make_location (loc
, loc
,
29403 cp_lexer_previous_token (parser
->lexer
)->location
);
29404 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29405 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29406 protected_set_expr_location (result
, combined_loc
);
29410 /* Parse a list of identifiers.
29412 objc-identifier-list:
29414 objc-identifier-list , identifier
29416 Returns a TREE_LIST of identifier nodes. */
29419 cp_parser_objc_identifier_list (cp_parser
* parser
)
29425 identifier
= cp_parser_identifier (parser
);
29426 if (identifier
== error_mark_node
)
29427 return error_mark_node
;
29429 list
= build_tree_list (NULL_TREE
, identifier
);
29430 sep
= cp_lexer_peek_token (parser
->lexer
);
29432 while (sep
->type
== CPP_COMMA
)
29434 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29435 identifier
= cp_parser_identifier (parser
);
29436 if (identifier
== error_mark_node
)
29439 list
= chainon (list
, build_tree_list (NULL_TREE
,
29441 sep
= cp_lexer_peek_token (parser
->lexer
);
29447 /* Parse an Objective-C alias declaration.
29449 objc-alias-declaration:
29450 @compatibility_alias identifier identifier ;
29452 This function registers the alias mapping with the Objective-C front end.
29453 It returns nothing. */
29456 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29460 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29461 alias
= cp_parser_identifier (parser
);
29462 orig
= cp_parser_identifier (parser
);
29463 objc_declare_alias (alias
, orig
);
29464 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29467 /* Parse an Objective-C class forward-declaration.
29469 objc-class-declaration:
29470 @class objc-identifier-list ;
29472 The function registers the forward declarations with the Objective-C
29473 front end. It returns nothing. */
29476 cp_parser_objc_class_declaration (cp_parser
* parser
)
29478 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29483 id
= cp_parser_identifier (parser
);
29484 if (id
== error_mark_node
)
29487 objc_declare_class (id
);
29489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29490 cp_lexer_consume_token (parser
->lexer
);
29494 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29497 /* Parse a list of Objective-C protocol references.
29499 objc-protocol-refs-opt:
29500 objc-protocol-refs [opt]
29502 objc-protocol-refs:
29503 < objc-identifier-list >
29505 Returns a TREE_LIST of identifiers, if any. */
29508 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29510 tree protorefs
= NULL_TREE
;
29512 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29514 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29515 protorefs
= cp_parser_objc_identifier_list (parser
);
29516 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29522 /* Parse a Objective-C visibility specification. */
29525 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29527 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29529 switch (vis
->keyword
)
29531 case RID_AT_PRIVATE
:
29532 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29534 case RID_AT_PROTECTED
:
29535 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29537 case RID_AT_PUBLIC
:
29538 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29540 case RID_AT_PACKAGE
:
29541 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29547 /* Eat '@private'/'@protected'/'@public'. */
29548 cp_lexer_consume_token (parser
->lexer
);
29551 /* Parse an Objective-C method type. Return 'true' if it is a class
29552 (+) method, and 'false' if it is an instance (-) method. */
29555 cp_parser_objc_method_type (cp_parser
* parser
)
29557 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29563 /* Parse an Objective-C protocol qualifier. */
29566 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29568 tree quals
= NULL_TREE
, node
;
29569 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29571 node
= token
->u
.value
;
29573 while (node
&& identifier_p (node
)
29574 && (node
== ridpointers
[(int) RID_IN
]
29575 || node
== ridpointers
[(int) RID_OUT
]
29576 || node
== ridpointers
[(int) RID_INOUT
]
29577 || node
== ridpointers
[(int) RID_BYCOPY
]
29578 || node
== ridpointers
[(int) RID_BYREF
]
29579 || node
== ridpointers
[(int) RID_ONEWAY
]))
29581 quals
= tree_cons (NULL_TREE
, node
, quals
);
29582 cp_lexer_consume_token (parser
->lexer
);
29583 token
= cp_lexer_peek_token (parser
->lexer
);
29584 node
= token
->u
.value
;
29590 /* Parse an Objective-C typename. */
29593 cp_parser_objc_typename (cp_parser
* parser
)
29595 tree type_name
= NULL_TREE
;
29597 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29599 tree proto_quals
, cp_type
= NULL_TREE
;
29601 matching_parens parens
;
29602 parens
.consume_open (parser
); /* Eat '('. */
29603 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29605 /* An ObjC type name may consist of just protocol qualifiers, in which
29606 case the type shall default to 'id'. */
29607 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29609 cp_type
= cp_parser_type_id (parser
);
29611 /* If the type could not be parsed, an error has already
29612 been produced. For error recovery, behave as if it had
29613 not been specified, which will use the default type
29615 if (cp_type
== error_mark_node
)
29617 cp_type
= NULL_TREE
;
29618 /* We need to skip to the closing parenthesis as
29619 cp_parser_type_id() does not seem to do it for
29621 cp_parser_skip_to_closing_parenthesis (parser
,
29622 /*recovering=*/true,
29623 /*or_comma=*/false,
29624 /*consume_paren=*/false);
29628 parens
.require_close (parser
);
29629 type_name
= build_tree_list (proto_quals
, cp_type
);
29635 /* Check to see if TYPE refers to an Objective-C selector name. */
29638 cp_parser_objc_selector_p (enum cpp_ttype type
)
29640 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29641 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29642 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29643 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29644 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29647 /* Parse an Objective-C selector. */
29650 cp_parser_objc_selector (cp_parser
* parser
)
29652 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29654 if (!cp_parser_objc_selector_p (token
->type
))
29656 error_at (token
->location
, "invalid Objective-C++ selector name");
29657 return error_mark_node
;
29660 /* C++ operator names are allowed to appear in ObjC selectors. */
29661 switch (token
->type
)
29663 case CPP_AND_AND
: return get_identifier ("and");
29664 case CPP_AND_EQ
: return get_identifier ("and_eq");
29665 case CPP_AND
: return get_identifier ("bitand");
29666 case CPP_OR
: return get_identifier ("bitor");
29667 case CPP_COMPL
: return get_identifier ("compl");
29668 case CPP_NOT
: return get_identifier ("not");
29669 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29670 case CPP_OR_OR
: return get_identifier ("or");
29671 case CPP_OR_EQ
: return get_identifier ("or_eq");
29672 case CPP_XOR
: return get_identifier ("xor");
29673 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29674 default: return token
->u
.value
;
29678 /* Parse an Objective-C params list. */
29681 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29683 tree params
= NULL_TREE
;
29684 bool maybe_unary_selector_p
= true;
29685 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29687 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29689 tree selector
= NULL_TREE
, type_name
, identifier
;
29690 tree parm_attr
= NULL_TREE
;
29692 if (token
->keyword
== RID_ATTRIBUTE
)
29695 if (token
->type
!= CPP_COLON
)
29696 selector
= cp_parser_objc_selector (parser
);
29698 /* Detect if we have a unary selector. */
29699 if (maybe_unary_selector_p
29700 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29702 params
= selector
; /* Might be followed by attributes. */
29706 maybe_unary_selector_p
= false;
29707 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29709 /* Something went quite wrong. There should be a colon
29710 here, but there is not. Stop parsing parameters. */
29713 type_name
= cp_parser_objc_typename (parser
);
29714 /* New ObjC allows attributes on parameters too. */
29715 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29716 parm_attr
= cp_parser_attributes_opt (parser
);
29717 identifier
= cp_parser_identifier (parser
);
29721 objc_build_keyword_decl (selector
,
29726 token
= cp_lexer_peek_token (parser
->lexer
);
29729 if (params
== NULL_TREE
)
29731 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29732 return error_mark_node
;
29735 /* We allow tail attributes for the method. */
29736 if (token
->keyword
== RID_ATTRIBUTE
)
29738 *attributes
= cp_parser_attributes_opt (parser
);
29739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29740 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29742 cp_parser_error (parser
,
29743 "method attributes must be specified at the end");
29744 return error_mark_node
;
29747 if (params
== NULL_TREE
)
29749 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29750 return error_mark_node
;
29755 /* Parse the non-keyword Objective-C params. */
29758 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29761 tree params
= make_node (TREE_LIST
);
29762 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29763 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29765 while (token
->type
== CPP_COMMA
)
29767 cp_parameter_declarator
*parmdecl
;
29770 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29771 token
= cp_lexer_peek_token (parser
->lexer
);
29773 if (token
->type
== CPP_ELLIPSIS
)
29775 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29777 token
= cp_lexer_peek_token (parser
->lexer
);
29781 /* TODO: parse attributes for tail parameters. */
29782 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29783 parm
= grokdeclarator (parmdecl
->declarator
,
29784 &parmdecl
->decl_specifiers
,
29785 PARM
, /*initialized=*/0,
29786 /*attrlist=*/NULL
);
29788 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29789 token
= cp_lexer_peek_token (parser
->lexer
);
29792 /* We allow tail attributes for the method. */
29793 if (token
->keyword
== RID_ATTRIBUTE
)
29795 if (*attributes
== NULL_TREE
)
29797 *attributes
= cp_parser_attributes_opt (parser
);
29798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29799 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29803 /* We have an error, but parse the attributes, so that we can
29805 *attributes
= cp_parser_attributes_opt (parser
);
29807 cp_parser_error (parser
,
29808 "method attributes must be specified at the end");
29809 return error_mark_node
;
29815 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29818 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29820 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29822 /* If the next token is `extern' and the following token is a string
29823 literal, then we have a linkage specification. */
29824 if (token
->keyword
== RID_EXTERN
29825 && cp_parser_is_pure_string_literal
29826 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
29827 cp_parser_linkage_specification (parser
);
29828 /* Handle #pragma, if any. */
29829 else if (token
->type
== CPP_PRAGMA
)
29830 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
29831 /* Allow stray semicolons. */
29832 else if (token
->type
== CPP_SEMICOLON
)
29833 cp_lexer_consume_token (parser
->lexer
);
29834 /* Mark methods as optional or required, when building protocols. */
29835 else if (token
->keyword
== RID_AT_OPTIONAL
)
29837 cp_lexer_consume_token (parser
->lexer
);
29838 objc_set_method_opt (true);
29840 else if (token
->keyword
== RID_AT_REQUIRED
)
29842 cp_lexer_consume_token (parser
->lexer
);
29843 objc_set_method_opt (false);
29845 else if (token
->keyword
== RID_NAMESPACE
)
29846 cp_parser_namespace_definition (parser
);
29847 /* Other stray characters must generate errors. */
29848 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
29850 cp_lexer_consume_token (parser
->lexer
);
29851 error ("stray %qs between Objective-C++ methods",
29852 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
29854 /* Finally, try to parse a block-declaration, or a function-definition. */
29856 cp_parser_block_declaration (parser
, /*statement_p=*/false);
29859 /* Parse a method signature. */
29862 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
29864 tree rettype
, kwdparms
, optparms
;
29865 bool ellipsis
= false;
29866 bool is_class_method
;
29868 is_class_method
= cp_parser_objc_method_type (parser
);
29869 rettype
= cp_parser_objc_typename (parser
);
29870 *attributes
= NULL_TREE
;
29871 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
29872 if (kwdparms
== error_mark_node
)
29873 return error_mark_node
;
29874 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
29875 if (optparms
== error_mark_node
)
29876 return error_mark_node
;
29878 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
29882 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
29885 cp_lexer_save_tokens (parser
->lexer
);
29886 tattr
= cp_parser_attributes_opt (parser
);
29887 gcc_assert (tattr
) ;
29889 /* If the attributes are followed by a method introducer, this is not allowed.
29890 Dump the attributes and flag the situation. */
29891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
29892 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
29895 /* Otherwise, the attributes introduce some interstitial code, possibly so
29896 rewind to allow that check. */
29897 cp_lexer_rollback_tokens (parser
->lexer
);
29901 /* Parse an Objective-C method prototype list. */
29904 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
29906 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29908 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29910 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29912 tree attributes
, sig
;
29913 bool is_class_method
;
29914 if (token
->type
== CPP_PLUS
)
29915 is_class_method
= true;
29917 is_class_method
= false;
29918 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
29919 if (sig
== error_mark_node
)
29921 cp_parser_skip_to_end_of_block_or_statement (parser
);
29922 token
= cp_lexer_peek_token (parser
->lexer
);
29925 objc_add_method_declaration (is_class_method
, sig
, attributes
);
29926 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29928 else if (token
->keyword
== RID_AT_PROPERTY
)
29929 cp_parser_objc_at_property_declaration (parser
);
29930 else if (token
->keyword
== RID_ATTRIBUTE
29931 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29932 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29934 "prefix attributes are ignored for methods");
29936 /* Allow for interspersed non-ObjC++ code. */
29937 cp_parser_objc_interstitial_code (parser
);
29939 token
= cp_lexer_peek_token (parser
->lexer
);
29942 if (token
->type
!= CPP_EOF
)
29943 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29945 cp_parser_error (parser
, "expected %<@end%>");
29947 objc_finish_interface ();
29950 /* Parse an Objective-C method definition list. */
29953 cp_parser_objc_method_definition_list (cp_parser
* parser
)
29955 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29957 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29961 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29964 tree sig
, attribute
;
29965 bool is_class_method
;
29966 if (token
->type
== CPP_PLUS
)
29967 is_class_method
= true;
29969 is_class_method
= false;
29970 push_deferring_access_checks (dk_deferred
);
29971 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
29972 if (sig
== error_mark_node
)
29974 cp_parser_skip_to_end_of_block_or_statement (parser
);
29975 token
= cp_lexer_peek_token (parser
->lexer
);
29978 objc_start_method_definition (is_class_method
, sig
, attribute
,
29981 /* For historical reasons, we accept an optional semicolon. */
29982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29983 cp_lexer_consume_token (parser
->lexer
);
29985 ptk
= cp_lexer_peek_token (parser
->lexer
);
29986 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
29987 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
29989 perform_deferred_access_checks (tf_warning_or_error
);
29990 stop_deferring_access_checks ();
29991 meth
= cp_parser_function_definition_after_declarator (parser
,
29993 pop_deferring_access_checks ();
29994 objc_finish_method_definition (meth
);
29997 /* The following case will be removed once @synthesize is
29998 completely implemented. */
29999 else if (token
->keyword
== RID_AT_PROPERTY
)
30000 cp_parser_objc_at_property_declaration (parser
);
30001 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30002 cp_parser_objc_at_synthesize_declaration (parser
);
30003 else if (token
->keyword
== RID_AT_DYNAMIC
)
30004 cp_parser_objc_at_dynamic_declaration (parser
);
30005 else if (token
->keyword
== RID_ATTRIBUTE
30006 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30007 warning_at (token
->location
, OPT_Wattributes
,
30008 "prefix attributes are ignored for methods");
30010 /* Allow for interspersed non-ObjC++ code. */
30011 cp_parser_objc_interstitial_code (parser
);
30013 token
= cp_lexer_peek_token (parser
->lexer
);
30016 if (token
->type
!= CPP_EOF
)
30017 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30019 cp_parser_error (parser
, "expected %<@end%>");
30021 objc_finish_implementation ();
30024 /* Parse Objective-C ivars. */
30027 cp_parser_objc_class_ivars (cp_parser
* parser
)
30029 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30031 if (token
->type
!= CPP_OPEN_BRACE
)
30032 return; /* No ivars specified. */
30034 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30035 token
= cp_lexer_peek_token (parser
->lexer
);
30037 while (token
->type
!= CPP_CLOSE_BRACE
30038 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30040 cp_decl_specifier_seq declspecs
;
30041 int decl_class_or_enum_p
;
30042 tree prefix_attributes
;
30044 cp_parser_objc_visibility_spec (parser
);
30046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30049 cp_parser_decl_specifier_seq (parser
,
30050 CP_PARSER_FLAGS_OPTIONAL
,
30052 &decl_class_or_enum_p
);
30054 /* auto, register, static, extern, mutable. */
30055 if (declspecs
.storage_class
!= sc_none
)
30057 cp_parser_error (parser
, "invalid type for instance variable");
30058 declspecs
.storage_class
= sc_none
;
30061 /* thread_local. */
30062 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30064 cp_parser_error (parser
, "invalid type for instance variable");
30065 declspecs
.locations
[ds_thread
] = 0;
30069 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30071 cp_parser_error (parser
, "invalid type for instance variable");
30072 declspecs
.locations
[ds_typedef
] = 0;
30075 prefix_attributes
= declspecs
.attributes
;
30076 declspecs
.attributes
= NULL_TREE
;
30078 /* Keep going until we hit the `;' at the end of the
30080 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30082 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30083 cp_declarator
*declarator
= NULL
;
30084 int ctor_dtor_or_conv_p
;
30086 /* Check for a (possibly unnamed) bitfield declaration. */
30087 token
= cp_lexer_peek_token (parser
->lexer
);
30088 if (token
->type
== CPP_COLON
)
30091 if (token
->type
== CPP_NAME
30092 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30095 /* Get the name of the bitfield. */
30096 declarator
= make_id_declarator (NULL_TREE
,
30097 cp_parser_identifier (parser
),
30101 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30102 /* Get the width of the bitfield. */
30104 = cp_parser_constant_expression (parser
);
30108 /* Parse the declarator. */
30110 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30111 &ctor_dtor_or_conv_p
,
30112 /*parenthesized_p=*/NULL
,
30113 /*member_p=*/false,
30114 /*friend_p=*/false);
30117 /* Look for attributes that apply to the ivar. */
30118 attributes
= cp_parser_attributes_opt (parser
);
30119 /* Remember which attributes are prefix attributes and
30121 first_attribute
= attributes
;
30122 /* Combine the attributes. */
30123 attributes
= chainon (prefix_attributes
, attributes
);
30126 /* Create the bitfield declaration. */
30127 decl
= grokbitfield (declarator
, &declspecs
,
30128 width
, NULL_TREE
, attributes
);
30130 decl
= grokfield (declarator
, &declspecs
,
30131 NULL_TREE
, /*init_const_expr_p=*/false,
30132 NULL_TREE
, attributes
);
30134 /* Add the instance variable. */
30135 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30136 objc_add_instance_variable (decl
);
30138 /* Reset PREFIX_ATTRIBUTES. */
30139 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30140 attributes
= TREE_CHAIN (attributes
);
30142 TREE_CHAIN (attributes
) = NULL_TREE
;
30144 token
= cp_lexer_peek_token (parser
->lexer
);
30146 if (token
->type
== CPP_COMMA
)
30148 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30154 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30155 token
= cp_lexer_peek_token (parser
->lexer
);
30158 if (token
->keyword
== RID_AT_END
)
30159 cp_parser_error (parser
, "expected %<}%>");
30161 /* Do not consume the RID_AT_END, so it will be read again as terminating
30162 the @interface of @implementation. */
30163 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30164 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30166 /* For historical reasons, we accept an optional semicolon. */
30167 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30168 cp_lexer_consume_token (parser
->lexer
);
30171 /* Parse an Objective-C protocol declaration. */
30174 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30176 tree proto
, protorefs
;
30179 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30180 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30182 tok
= cp_lexer_peek_token (parser
->lexer
);
30183 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30184 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30188 /* See if we have a forward declaration or a definition. */
30189 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30191 /* Try a forward declaration first. */
30192 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30198 id
= cp_parser_identifier (parser
);
30199 if (id
== error_mark_node
)
30202 objc_declare_protocol (id
, attributes
);
30204 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30205 cp_lexer_consume_token (parser
->lexer
);
30209 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30212 /* Ok, we got a full-fledged definition (or at least should). */
30215 proto
= cp_parser_identifier (parser
);
30216 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30217 objc_start_protocol (proto
, protorefs
, attributes
);
30218 cp_parser_objc_method_prototype_list (parser
);
30222 /* Parse an Objective-C superclass or category. */
30225 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30228 tree
*categ
, bool *is_class_extension
)
30230 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30232 *super
= *categ
= NULL_TREE
;
30233 *is_class_extension
= false;
30234 if (next
->type
== CPP_COLON
)
30236 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30237 *super
= cp_parser_identifier (parser
);
30239 else if (next
->type
== CPP_OPEN_PAREN
)
30241 matching_parens parens
;
30242 parens
.consume_open (parser
); /* Eat '('. */
30244 /* If there is no category name, and this is an @interface, we
30245 have a class extension. */
30246 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30248 *categ
= NULL_TREE
;
30249 *is_class_extension
= true;
30252 *categ
= cp_parser_identifier (parser
);
30254 parens
.require_close (parser
);
30258 /* Parse an Objective-C class interface. */
30261 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30263 tree name
, super
, categ
, protos
;
30264 bool is_class_extension
;
30266 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30267 name
= cp_parser_identifier (parser
);
30268 if (name
== error_mark_node
)
30270 /* It's hard to recover because even if valid @interface stuff
30271 is to follow, we can't compile it (or validate it) if we
30272 don't even know which class it refers to. Let's assume this
30273 was a stray '@interface' token in the stream and skip it.
30277 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30278 &is_class_extension
);
30279 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30281 /* We have either a class or a category on our hands. */
30282 if (categ
|| is_class_extension
)
30283 objc_start_category_interface (name
, categ
, protos
, attributes
);
30286 objc_start_class_interface (name
, super
, protos
, attributes
);
30287 /* Handle instance variable declarations, if any. */
30288 cp_parser_objc_class_ivars (parser
);
30289 objc_continue_interface ();
30292 cp_parser_objc_method_prototype_list (parser
);
30295 /* Parse an Objective-C class implementation. */
30298 cp_parser_objc_class_implementation (cp_parser
* parser
)
30300 tree name
, super
, categ
;
30301 bool is_class_extension
;
30303 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30304 name
= cp_parser_identifier (parser
);
30305 if (name
== error_mark_node
)
30307 /* It's hard to recover because even if valid @implementation
30308 stuff is to follow, we can't compile it (or validate it) if
30309 we don't even know which class it refers to. Let's assume
30310 this was a stray '@implementation' token in the stream and
30315 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30316 &is_class_extension
);
30318 /* We have either a class or a category on our hands. */
30320 objc_start_category_implementation (name
, categ
);
30323 objc_start_class_implementation (name
, super
);
30324 /* Handle instance variable declarations, if any. */
30325 cp_parser_objc_class_ivars (parser
);
30326 objc_continue_implementation ();
30329 cp_parser_objc_method_definition_list (parser
);
30332 /* Consume the @end token and finish off the implementation. */
30335 cp_parser_objc_end_implementation (cp_parser
* parser
)
30337 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30338 objc_finish_implementation ();
30341 /* Parse an Objective-C declaration. */
30344 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30346 /* Try to figure out what kind of declaration is present. */
30347 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30350 switch (kwd
->keyword
)
30355 error_at (kwd
->location
, "attributes may not be specified before"
30356 " the %<@%D%> Objective-C++ keyword",
30360 case RID_AT_IMPLEMENTATION
:
30361 warning_at (kwd
->location
, OPT_Wattributes
,
30362 "prefix attributes are ignored before %<@%D%>",
30369 switch (kwd
->keyword
)
30372 cp_parser_objc_alias_declaration (parser
);
30375 cp_parser_objc_class_declaration (parser
);
30377 case RID_AT_PROTOCOL
:
30378 cp_parser_objc_protocol_declaration (parser
, attributes
);
30380 case RID_AT_INTERFACE
:
30381 cp_parser_objc_class_interface (parser
, attributes
);
30383 case RID_AT_IMPLEMENTATION
:
30384 cp_parser_objc_class_implementation (parser
);
30387 cp_parser_objc_end_implementation (parser
);
30390 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30392 cp_parser_skip_to_end_of_block_or_statement (parser
);
30396 /* Parse an Objective-C try-catch-finally statement.
30398 objc-try-catch-finally-stmt:
30399 @try compound-statement objc-catch-clause-seq [opt]
30400 objc-finally-clause [opt]
30402 objc-catch-clause-seq:
30403 objc-catch-clause objc-catch-clause-seq [opt]
30406 @catch ( objc-exception-declaration ) compound-statement
30408 objc-finally-clause:
30409 @finally compound-statement
30411 objc-exception-declaration:
30412 parameter-declaration
30415 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30419 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30420 for C. Keep them in sync. */
30423 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30425 location_t location
;
30428 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30429 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30430 objc_maybe_warn_exceptions (location
);
30431 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30432 node, lest it get absorbed into the surrounding block. */
30433 stmt
= push_stmt_list ();
30434 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30435 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30437 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30439 cp_parameter_declarator
*parm
;
30440 tree parameter_declaration
= error_mark_node
;
30441 bool seen_open_paren
= false;
30442 matching_parens parens
;
30444 cp_lexer_consume_token (parser
->lexer
);
30445 if (parens
.require_open (parser
))
30446 seen_open_paren
= true;
30447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30449 /* We have "@catch (...)" (where the '...' are literally
30450 what is in the code). Skip the '...'.
30451 parameter_declaration is set to NULL_TREE, and
30452 objc_being_catch_clauses() knows that that means
30454 cp_lexer_consume_token (parser
->lexer
);
30455 parameter_declaration
= NULL_TREE
;
30459 /* We have "@catch (NSException *exception)" or something
30460 like that. Parse the parameter declaration. */
30461 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30463 parameter_declaration
= error_mark_node
;
30465 parameter_declaration
= grokdeclarator (parm
->declarator
,
30466 &parm
->decl_specifiers
,
30467 PARM
, /*initialized=*/0,
30468 /*attrlist=*/NULL
);
30470 if (seen_open_paren
)
30471 parens
.require_close (parser
);
30474 /* If there was no open parenthesis, we are recovering from
30475 an error, and we are trying to figure out what mistake
30476 the user has made. */
30478 /* If there is an immediate closing parenthesis, the user
30479 probably forgot the opening one (ie, they typed "@catch
30480 NSException *e)". Parse the closing parenthesis and keep
30482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30483 cp_lexer_consume_token (parser
->lexer
);
30485 /* If these is no immediate closing parenthesis, the user
30486 probably doesn't know that parenthesis are required at
30487 all (ie, they typed "@catch NSException *e"). So, just
30488 forget about the closing parenthesis and keep going. */
30490 objc_begin_catch_clause (parameter_declaration
);
30491 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30492 objc_finish_catch_clause ();
30494 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30496 cp_lexer_consume_token (parser
->lexer
);
30497 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30498 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30499 node, lest it get absorbed into the surrounding block. */
30500 stmt
= push_stmt_list ();
30501 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30502 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30505 return objc_finish_try_stmt ();
30508 /* Parse an Objective-C synchronized statement.
30510 objc-synchronized-stmt:
30511 @synchronized ( expression ) compound-statement
30513 Returns NULL_TREE. */
30516 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30518 location_t location
;
30521 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30523 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30524 objc_maybe_warn_exceptions (location
);
30525 matching_parens parens
;
30526 parens
.require_open (parser
);
30527 lock
= cp_parser_expression (parser
);
30528 parens
.require_close (parser
);
30530 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30531 node, lest it get absorbed into the surrounding block. */
30532 stmt
= push_stmt_list ();
30533 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30535 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30538 /* Parse an Objective-C throw statement.
30541 @throw assignment-expression [opt] ;
30543 Returns a constructed '@throw' statement. */
30546 cp_parser_objc_throw_statement (cp_parser
*parser
)
30548 tree expr
= NULL_TREE
;
30549 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30551 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30553 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30554 expr
= cp_parser_expression (parser
);
30556 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30558 return objc_build_throw_stmt (loc
, expr
);
30561 /* Parse an Objective-C statement. */
30564 cp_parser_objc_statement (cp_parser
* parser
)
30566 /* Try to figure out what kind of declaration is present. */
30567 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30569 switch (kwd
->keyword
)
30572 return cp_parser_objc_try_catch_finally_statement (parser
);
30573 case RID_AT_SYNCHRONIZED
:
30574 return cp_parser_objc_synchronized_statement (parser
);
30576 return cp_parser_objc_throw_statement (parser
);
30578 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30580 cp_parser_skip_to_end_of_block_or_statement (parser
);
30583 return error_mark_node
;
30586 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30587 look ahead to see if an objc keyword follows the attributes. This
30588 is to detect the use of prefix attributes on ObjC @interface and
30592 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30594 cp_lexer_save_tokens (parser
->lexer
);
30595 *attrib
= cp_parser_attributes_opt (parser
);
30596 gcc_assert (*attrib
);
30597 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30599 cp_lexer_commit_tokens (parser
->lexer
);
30602 cp_lexer_rollback_tokens (parser
->lexer
);
30606 /* This routine is a minimal replacement for
30607 c_parser_struct_declaration () used when parsing the list of
30608 types/names or ObjC++ properties. For example, when parsing the
30611 @property (readonly) int a, b, c;
30613 this function is responsible for parsing "int a, int b, int c" and
30614 returning the declarations as CHAIN of DECLs.
30616 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30617 similar parsing. */
30619 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30621 tree decls
= NULL_TREE
;
30622 cp_decl_specifier_seq declspecs
;
30623 int decl_class_or_enum_p
;
30624 tree prefix_attributes
;
30626 cp_parser_decl_specifier_seq (parser
,
30627 CP_PARSER_FLAGS_NONE
,
30629 &decl_class_or_enum_p
);
30631 if (declspecs
.type
== error_mark_node
)
30632 return error_mark_node
;
30634 /* auto, register, static, extern, mutable. */
30635 if (declspecs
.storage_class
!= sc_none
)
30637 cp_parser_error (parser
, "invalid type for property");
30638 declspecs
.storage_class
= sc_none
;
30641 /* thread_local. */
30642 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30644 cp_parser_error (parser
, "invalid type for property");
30645 declspecs
.locations
[ds_thread
] = 0;
30649 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30651 cp_parser_error (parser
, "invalid type for property");
30652 declspecs
.locations
[ds_typedef
] = 0;
30655 prefix_attributes
= declspecs
.attributes
;
30656 declspecs
.attributes
= NULL_TREE
;
30658 /* Keep going until we hit the `;' at the end of the declaration. */
30659 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30661 tree attributes
, first_attribute
, decl
;
30662 cp_declarator
*declarator
;
30665 /* Parse the declarator. */
30666 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30667 NULL
, NULL
, false, false);
30669 /* Look for attributes that apply to the ivar. */
30670 attributes
= cp_parser_attributes_opt (parser
);
30671 /* Remember which attributes are prefix attributes and
30673 first_attribute
= attributes
;
30674 /* Combine the attributes. */
30675 attributes
= chainon (prefix_attributes
, attributes
);
30677 decl
= grokfield (declarator
, &declspecs
,
30678 NULL_TREE
, /*init_const_expr_p=*/false,
30679 NULL_TREE
, attributes
);
30681 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30682 return error_mark_node
;
30684 /* Reset PREFIX_ATTRIBUTES. */
30685 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30686 attributes
= TREE_CHAIN (attributes
);
30688 TREE_CHAIN (attributes
) = NULL_TREE
;
30690 DECL_CHAIN (decl
) = decls
;
30693 token
= cp_lexer_peek_token (parser
->lexer
);
30694 if (token
->type
== CPP_COMMA
)
30696 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30705 /* Parse an Objective-C @property declaration. The syntax is:
30707 objc-property-declaration:
30708 '@property' objc-property-attributes[opt] struct-declaration ;
30710 objc-property-attributes:
30711 '(' objc-property-attribute-list ')'
30713 objc-property-attribute-list:
30714 objc-property-attribute
30715 objc-property-attribute-list, objc-property-attribute
30717 objc-property-attribute
30718 'getter' = identifier
30719 'setter' = identifier
30728 @property NSString *name;
30729 @property (readonly) id object;
30730 @property (retain, nonatomic, getter=getTheName) id name;
30731 @property int a, b, c;
30733 PS: This function is identical to
30734 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30736 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30738 /* The following variables hold the attributes of the properties as
30739 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30740 seen. When we see an attribute, we set them to 'true' (if they
30741 are boolean properties) or to the identifier (if they have an
30742 argument, ie, for getter and setter). Note that here we only
30743 parse the list of attributes, check the syntax and accumulate the
30744 attributes that we find. objc_add_property_declaration() will
30745 then process the information. */
30746 bool property_assign
= false;
30747 bool property_copy
= false;
30748 tree property_getter_ident
= NULL_TREE
;
30749 bool property_nonatomic
= false;
30750 bool property_readonly
= false;
30751 bool property_readwrite
= false;
30752 bool property_retain
= false;
30753 tree property_setter_ident
= NULL_TREE
;
30755 /* 'properties' is the list of properties that we read. Usually a
30756 single one, but maybe more (eg, in "@property int a, b, c;" there
30761 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30763 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30765 /* Parse the optional attribute list... */
30766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30769 matching_parens parens
;
30770 parens
.consume_open (parser
);
30774 bool syntax_error
= false;
30775 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30778 if (token
->type
!= CPP_NAME
)
30780 cp_parser_error (parser
, "expected identifier");
30783 keyword
= C_RID_CODE (token
->u
.value
);
30784 cp_lexer_consume_token (parser
->lexer
);
30787 case RID_ASSIGN
: property_assign
= true; break;
30788 case RID_COPY
: property_copy
= true; break;
30789 case RID_NONATOMIC
: property_nonatomic
= true; break;
30790 case RID_READONLY
: property_readonly
= true; break;
30791 case RID_READWRITE
: property_readwrite
= true; break;
30792 case RID_RETAIN
: property_retain
= true; break;
30796 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30798 if (keyword
== RID_GETTER
)
30799 cp_parser_error (parser
,
30800 "missing %<=%> (after %<getter%> attribute)");
30802 cp_parser_error (parser
,
30803 "missing %<=%> (after %<setter%> attribute)");
30804 syntax_error
= true;
30807 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30808 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30810 cp_parser_error (parser
, "expected identifier");
30811 syntax_error
= true;
30814 if (keyword
== RID_SETTER
)
30816 if (property_setter_ident
!= NULL_TREE
)
30818 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
30819 cp_lexer_consume_token (parser
->lexer
);
30822 property_setter_ident
= cp_parser_objc_selector (parser
);
30823 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30824 cp_parser_error (parser
, "setter name must terminate with %<:%>");
30826 cp_lexer_consume_token (parser
->lexer
);
30830 if (property_getter_ident
!= NULL_TREE
)
30832 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
30833 cp_lexer_consume_token (parser
->lexer
);
30836 property_getter_ident
= cp_parser_objc_selector (parser
);
30840 cp_parser_error (parser
, "unknown property attribute");
30841 syntax_error
= true;
30848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30849 cp_lexer_consume_token (parser
->lexer
);
30854 /* FIXME: "@property (setter, assign);" will generate a spurious
30855 "error: expected ‘)’ before ‘,’ token". This is because
30856 cp_parser_require, unlike the C counterpart, will produce an
30857 error even if we are in error recovery. */
30858 if (!parens
.require_close (parser
))
30860 cp_parser_skip_to_closing_parenthesis (parser
,
30861 /*recovering=*/true,
30862 /*or_comma=*/false,
30863 /*consume_paren=*/true);
30867 /* ... and the property declaration(s). */
30868 properties
= cp_parser_objc_struct_declaration (parser
);
30870 if (properties
== error_mark_node
)
30872 cp_parser_skip_to_end_of_statement (parser
);
30873 /* If the next token is now a `;', consume it. */
30874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30875 cp_lexer_consume_token (parser
->lexer
);
30879 if (properties
== NULL_TREE
)
30880 cp_parser_error (parser
, "expected identifier");
30883 /* Comma-separated properties are chained together in
30884 reverse order; add them one by one. */
30885 properties
= nreverse (properties
);
30887 for (; properties
; properties
= TREE_CHAIN (properties
))
30888 objc_add_property_declaration (loc
, copy_node (properties
),
30889 property_readonly
, property_readwrite
,
30890 property_assign
, property_retain
,
30891 property_copy
, property_nonatomic
,
30892 property_getter_ident
, property_setter_ident
);
30895 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30898 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30900 objc-synthesize-declaration:
30901 @synthesize objc-synthesize-identifier-list ;
30903 objc-synthesize-identifier-list:
30904 objc-synthesize-identifier
30905 objc-synthesize-identifier-list, objc-synthesize-identifier
30907 objc-synthesize-identifier
30909 identifier = identifier
30912 @synthesize MyProperty;
30913 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30915 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30916 for C. Keep them in sync.
30919 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
30921 tree list
= NULL_TREE
;
30923 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30925 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
30928 tree property
, ivar
;
30929 property
= cp_parser_identifier (parser
);
30930 if (property
== error_mark_node
)
30932 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
30937 cp_lexer_consume_token (parser
->lexer
);
30938 ivar
= cp_parser_identifier (parser
);
30939 if (ivar
== error_mark_node
)
30941 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30947 list
= chainon (list
, build_tree_list (ivar
, property
));
30948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30949 cp_lexer_consume_token (parser
->lexer
);
30953 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30954 objc_add_synthesize_declaration (loc
, list
);
30957 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30959 objc-dynamic-declaration:
30960 @dynamic identifier-list ;
30963 @dynamic MyProperty;
30964 @dynamic MyProperty, AnotherProperty;
30966 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30967 for C. Keep them in sync.
30970 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
30972 tree list
= NULL_TREE
;
30974 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30976 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
30980 property
= cp_parser_identifier (parser
);
30981 if (property
== error_mark_node
)
30983 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30986 list
= chainon (list
, build_tree_list (NULL
, property
));
30987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30988 cp_lexer_consume_token (parser
->lexer
);
30992 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30993 objc_add_dynamic_declaration (loc
, list
);
30997 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30999 /* Returns name of the next clause.
31000 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31001 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31002 returned and the token is consumed. */
31004 static pragma_omp_clause
31005 cp_parser_omp_clause_name (cp_parser
*parser
)
31007 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31009 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31010 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31011 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31012 result
= PRAGMA_OMP_CLAUSE_IF
;
31013 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31014 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31015 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31016 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31017 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31018 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31019 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31020 result
= PRAGMA_OMP_CLAUSE_FOR
;
31021 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31023 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31024 const char *p
= IDENTIFIER_POINTER (id
);
31029 if (!strcmp ("aligned", p
))
31030 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31031 else if (!strcmp ("async", p
))
31032 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31035 if (!strcmp ("collapse", p
))
31036 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31037 else if (!strcmp ("copy", p
))
31038 result
= PRAGMA_OACC_CLAUSE_COPY
;
31039 else if (!strcmp ("copyin", p
))
31040 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31041 else if (!strcmp ("copyout", p
))
31042 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31043 else if (!strcmp ("copyprivate", p
))
31044 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31045 else if (!strcmp ("create", p
))
31046 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31049 if (!strcmp ("defaultmap", p
))
31050 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31051 else if (!strcmp ("depend", p
))
31052 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31053 else if (!strcmp ("device", p
))
31054 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31055 else if (!strcmp ("deviceptr", p
))
31056 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31057 else if (!strcmp ("device_resident", p
))
31058 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31059 else if (!strcmp ("dist_schedule", p
))
31060 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31063 if (!strcmp ("final", p
))
31064 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31065 else if (!strcmp ("firstprivate", p
))
31066 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31067 else if (!strcmp ("from", p
))
31068 result
= PRAGMA_OMP_CLAUSE_FROM
;
31071 if (!strcmp ("gang", p
))
31072 result
= PRAGMA_OACC_CLAUSE_GANG
;
31073 else if (!strcmp ("grainsize", p
))
31074 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31077 if (!strcmp ("hint", p
))
31078 result
= PRAGMA_OMP_CLAUSE_HINT
;
31079 else if (!strcmp ("host", p
))
31080 result
= PRAGMA_OACC_CLAUSE_HOST
;
31083 if (!strcmp ("inbranch", p
))
31084 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31085 else if (!strcmp ("independent", p
))
31086 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31087 else if (!strcmp ("is_device_ptr", p
))
31088 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31091 if (!strcmp ("lastprivate", p
))
31092 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31093 else if (!strcmp ("linear", p
))
31094 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31095 else if (!strcmp ("link", p
))
31096 result
= PRAGMA_OMP_CLAUSE_LINK
;
31099 if (!strcmp ("map", p
))
31100 result
= PRAGMA_OMP_CLAUSE_MAP
;
31101 else if (!strcmp ("mergeable", p
))
31102 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31105 if (!strcmp ("nogroup", p
))
31106 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31107 else if (!strcmp ("notinbranch", p
))
31108 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31109 else if (!strcmp ("nowait", p
))
31110 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31111 else if (!strcmp ("num_gangs", p
))
31112 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31113 else if (!strcmp ("num_tasks", p
))
31114 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31115 else if (!strcmp ("num_teams", p
))
31116 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31117 else if (!strcmp ("num_threads", p
))
31118 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31119 else if (!strcmp ("num_workers", p
))
31120 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31123 if (!strcmp ("ordered", p
))
31124 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31127 if (!strcmp ("parallel", p
))
31128 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31129 else if (!strcmp ("present", p
))
31130 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31131 else if (!strcmp ("present_or_copy", p
)
31132 || !strcmp ("pcopy", p
))
31133 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31134 else if (!strcmp ("present_or_copyin", p
)
31135 || !strcmp ("pcopyin", p
))
31136 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31137 else if (!strcmp ("present_or_copyout", p
)
31138 || !strcmp ("pcopyout", p
))
31139 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31140 else if (!strcmp ("present_or_create", p
)
31141 || !strcmp ("pcreate", p
))
31142 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31143 else if (!strcmp ("priority", p
))
31144 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31145 else if (!strcmp ("proc_bind", p
))
31146 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31149 if (!strcmp ("reduction", p
))
31150 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31153 if (!strcmp ("safelen", p
))
31154 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31155 else if (!strcmp ("schedule", p
))
31156 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31157 else if (!strcmp ("sections", p
))
31158 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31159 else if (!strcmp ("self", p
))
31160 result
= PRAGMA_OACC_CLAUSE_SELF
;
31161 else if (!strcmp ("seq", p
))
31162 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31163 else if (!strcmp ("shared", p
))
31164 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31165 else if (!strcmp ("simd", p
))
31166 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31167 else if (!strcmp ("simdlen", p
))
31168 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31171 if (!strcmp ("taskgroup", p
))
31172 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31173 else if (!strcmp ("thread_limit", p
))
31174 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31175 else if (!strcmp ("threads", p
))
31176 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31177 else if (!strcmp ("tile", p
))
31178 result
= PRAGMA_OACC_CLAUSE_TILE
;
31179 else if (!strcmp ("to", p
))
31180 result
= PRAGMA_OMP_CLAUSE_TO
;
31183 if (!strcmp ("uniform", p
))
31184 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31185 else if (!strcmp ("untied", p
))
31186 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31187 else if (!strcmp ("use_device", p
))
31188 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31189 else if (!strcmp ("use_device_ptr", p
))
31190 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31193 if (!strcmp ("vector", p
))
31194 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31195 else if (!strcmp ("vector_length", p
))
31196 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31199 if (!strcmp ("wait", p
))
31200 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31201 else if (!strcmp ("worker", p
))
31202 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31207 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31208 cp_lexer_consume_token (parser
->lexer
);
31213 /* Validate that a clause of the given type does not already exist. */
31216 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31217 const char *name
, location_t location
)
31221 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31222 if (OMP_CLAUSE_CODE (c
) == code
)
31224 error_at (location
, "too many %qs clauses", name
);
31232 variable-list , identifier
31234 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31235 colon). An opening parenthesis will have been consumed by the caller.
31237 If KIND is nonzero, create the appropriate node and install the decl
31238 in OMP_CLAUSE_DECL and add the node to the head of the list.
31240 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31241 return the list created.
31243 COLON can be NULL if only closing parenthesis should end the list,
31244 or pointer to bool which will receive false if the list is terminated
31245 by closing parenthesis or true if the list is terminated by colon. */
31248 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31249 tree list
, bool *colon
)
31252 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31255 parser
->colon_corrects_to_scope_p
= false;
31262 token
= cp_lexer_peek_token (parser
->lexer
);
31264 && current_class_ptr
31265 && cp_parser_is_keyword (token
, RID_THIS
))
31267 decl
= finish_this_expr ();
31268 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31269 || CONVERT_EXPR_P (decl
))
31270 decl
= TREE_OPERAND (decl
, 0);
31271 cp_lexer_consume_token (parser
->lexer
);
31275 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31276 /*check_dependency_p=*/true,
31277 /*template_p=*/NULL
,
31278 /*declarator_p=*/false,
31279 /*optional_p=*/false);
31280 if (name
== error_mark_node
)
31283 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31284 if (decl
== error_mark_node
)
31285 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31288 if (decl
== error_mark_node
)
31290 else if (kind
!= 0)
31294 case OMP_CLAUSE__CACHE_
:
31295 /* The OpenACC cache directive explicitly only allows "array
31296 elements or subarrays". */
31297 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31299 error_at (token
->location
, "expected %<[%>");
31300 decl
= error_mark_node
;
31304 case OMP_CLAUSE_MAP
:
31305 case OMP_CLAUSE_FROM
:
31306 case OMP_CLAUSE_TO
:
31307 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31310 = cp_lexer_peek_token (parser
->lexer
)->location
;
31311 cp_id_kind idk
= CP_ID_KIND_NONE
;
31312 cp_lexer_consume_token (parser
->lexer
);
31313 decl
= convert_from_reference (decl
);
31315 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31320 case OMP_CLAUSE_DEPEND
:
31321 case OMP_CLAUSE_REDUCTION
:
31322 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31324 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31326 parser
->colon_corrects_to_scope_p
= false;
31327 cp_lexer_consume_token (parser
->lexer
);
31328 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31329 low_bound
= cp_parser_expression (parser
);
31331 parser
->colon_corrects_to_scope_p
31332 = saved_colon_corrects_to_scope_p
;
31333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31334 length
= integer_one_node
;
31337 /* Look for `:'. */
31338 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31340 if (!cp_lexer_next_token_is (parser
->lexer
,
31342 length
= cp_parser_expression (parser
);
31344 /* Look for the closing `]'. */
31345 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31349 decl
= tree_cons (low_bound
, length
, decl
);
31356 tree u
= build_omp_clause (token
->location
, kind
);
31357 OMP_CLAUSE_DECL (u
) = decl
;
31358 OMP_CLAUSE_CHAIN (u
) = list
;
31362 list
= tree_cons (decl
, NULL_TREE
, list
);
31365 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31367 cp_lexer_consume_token (parser
->lexer
);
31371 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31373 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31376 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31380 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31384 /* Try to resync to an unnested comma. Copied from
31385 cp_parser_parenthesized_expression_list. */
31388 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31389 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31390 /*recovering=*/true,
31392 /*consume_paren=*/true);
31400 /* Similarly, but expect leading and trailing parenthesis. This is a very
31401 common case for omp clauses. */
31404 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31406 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31407 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31412 copy ( variable-list )
31413 copyin ( variable-list )
31414 copyout ( variable-list )
31415 create ( variable-list )
31416 delete ( variable-list )
31417 present ( variable-list )
31418 present_or_copy ( variable-list )
31419 pcopy ( variable-list )
31420 present_or_copyin ( variable-list )
31421 pcopyin ( variable-list )
31422 present_or_copyout ( variable-list )
31423 pcopyout ( variable-list )
31424 present_or_create ( variable-list )
31425 pcreate ( variable-list ) */
31428 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31431 enum gomp_map_kind kind
;
31434 case PRAGMA_OACC_CLAUSE_COPY
:
31435 kind
= GOMP_MAP_FORCE_TOFROM
;
31437 case PRAGMA_OACC_CLAUSE_COPYIN
:
31438 kind
= GOMP_MAP_FORCE_TO
;
31440 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31441 kind
= GOMP_MAP_FORCE_FROM
;
31443 case PRAGMA_OACC_CLAUSE_CREATE
:
31444 kind
= GOMP_MAP_FORCE_ALLOC
;
31446 case PRAGMA_OACC_CLAUSE_DELETE
:
31447 kind
= GOMP_MAP_DELETE
;
31449 case PRAGMA_OACC_CLAUSE_DEVICE
:
31450 kind
= GOMP_MAP_FORCE_TO
;
31452 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31453 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31455 case PRAGMA_OACC_CLAUSE_HOST
:
31456 case PRAGMA_OACC_CLAUSE_SELF
:
31457 kind
= GOMP_MAP_FORCE_FROM
;
31459 case PRAGMA_OACC_CLAUSE_LINK
:
31460 kind
= GOMP_MAP_LINK
;
31462 case PRAGMA_OACC_CLAUSE_PRESENT
:
31463 kind
= GOMP_MAP_FORCE_PRESENT
;
31465 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31466 kind
= GOMP_MAP_TOFROM
;
31468 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31469 kind
= GOMP_MAP_TO
;
31471 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31472 kind
= GOMP_MAP_FROM
;
31474 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31475 kind
= GOMP_MAP_ALLOC
;
31478 gcc_unreachable ();
31481 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31483 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31484 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31490 deviceptr ( variable-list ) */
31493 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31495 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31498 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31499 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31500 variable-list must only allow for pointer variables. */
31501 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31502 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31504 tree v
= TREE_PURPOSE (t
);
31505 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31506 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31507 OMP_CLAUSE_DECL (u
) = v
;
31508 OMP_CLAUSE_CHAIN (u
) = list
;
31522 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31523 enum omp_clause_code code
,
31524 tree list
, location_t location
)
31526 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31527 tree c
= build_omp_clause (location
, code
);
31528 OMP_CLAUSE_CHAIN (c
) = list
;
31533 num_gangs ( expression )
31534 num_workers ( expression )
31535 vector_length ( expression ) */
31538 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31539 const char *str
, tree list
)
31541 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31543 matching_parens parens
;
31544 if (!parens
.require_open (parser
))
31547 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31549 if (t
== error_mark_node
31550 || !parens
.require_close (parser
))
31552 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31553 /*or_comma=*/false,
31554 /*consume_paren=*/true);
31558 check_no_duplicate_clause (list
, code
, str
, loc
);
31560 tree c
= build_omp_clause (loc
, code
);
31561 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31562 OMP_CLAUSE_CHAIN (c
) = list
;
31568 gang [( gang-arg-list )]
31569 worker [( [num:] int-expr )]
31570 vector [( [length:] int-expr )]
31572 where gang-arg is one of:
31577 and size-expr may be:
31584 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31585 const char *str
, tree list
)
31587 const char *id
= "num";
31588 cp_lexer
*lexer
= parser
->lexer
;
31589 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31590 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31592 if (kind
== OMP_CLAUSE_VECTOR
)
31595 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31597 matching_parens parens
;
31598 parens
.consume_open (parser
);
31602 cp_token
*next
= cp_lexer_peek_token (lexer
);
31605 /* Gang static argument. */
31606 if (kind
== OMP_CLAUSE_GANG
31607 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31609 cp_lexer_consume_token (lexer
);
31611 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31612 goto cleanup_error
;
31615 if (ops
[idx
] != NULL
)
31617 cp_parser_error (parser
, "too many %<static%> arguments");
31618 goto cleanup_error
;
31621 /* Check for the '*' argument. */
31622 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31623 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31624 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31627 cp_lexer_consume_token (lexer
);
31628 ops
[idx
] = integer_minus_one_node
;
31630 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31632 cp_lexer_consume_token (lexer
);
31638 /* Worker num: argument and vector length: arguments. */
31639 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31640 && id_equal (next
->u
.value
, id
)
31641 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31643 cp_lexer_consume_token (lexer
); /* id */
31644 cp_lexer_consume_token (lexer
); /* ':' */
31647 /* Now collect the actual argument. */
31648 if (ops
[idx
] != NULL_TREE
)
31650 cp_parser_error (parser
, "unexpected argument");
31651 goto cleanup_error
;
31654 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31656 if (expr
== error_mark_node
)
31657 goto cleanup_error
;
31659 mark_exp_read (expr
);
31662 if (kind
== OMP_CLAUSE_GANG
31663 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31665 cp_lexer_consume_token (lexer
);
31672 if (!parens
.require_close (parser
))
31673 goto cleanup_error
;
31676 check_no_duplicate_clause (list
, kind
, str
, loc
);
31678 c
= build_omp_clause (loc
, kind
);
31681 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31683 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31684 OMP_CLAUSE_CHAIN (c
) = list
;
31689 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31694 tile ( size-expr-list ) */
31697 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31699 tree c
, expr
= error_mark_node
;
31700 tree tile
= NULL_TREE
;
31702 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31703 so, but the spec authors never considered such a case and have
31704 differing opinions on what it might mean, including 'not
31706 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31707 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31710 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31715 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31719 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31720 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31722 cp_lexer_consume_token (parser
->lexer
);
31723 expr
= integer_zero_node
;
31726 expr
= cp_parser_constant_expression (parser
);
31728 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31730 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31732 /* Consume the trailing ')'. */
31733 cp_lexer_consume_token (parser
->lexer
);
31735 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31736 tile
= nreverse (tile
);
31737 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31738 OMP_CLAUSE_CHAIN (c
) = list
;
31743 Parse wait clause or directive parameters. */
31746 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31748 vec
<tree
, va_gc
> *args
;
31751 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31753 /*allow_expansion_p=*/true,
31754 /*non_constant_p=*/NULL
);
31756 if (args
== NULL
|| args
->length () == 0)
31758 cp_parser_error (parser
, "expected integer expression before ')'");
31760 release_tree_vector (args
);
31764 args_tree
= build_tree_list_vec (args
);
31766 release_tree_vector (args
);
31768 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31770 tree targ
= TREE_VALUE (t
);
31772 if (targ
!= error_mark_node
)
31774 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31775 error ("%<wait%> expression must be integral");
31778 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31780 targ
= mark_rvalue_use (targ
);
31781 OMP_CLAUSE_DECL (c
) = targ
;
31782 OMP_CLAUSE_CHAIN (c
) = list
;
31792 wait ( int-expr-list ) */
31795 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31797 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31799 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31802 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31808 collapse ( constant-expression ) */
31811 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
31817 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31818 matching_parens parens
;
31819 if (!parens
.require_open (parser
))
31822 num
= cp_parser_constant_expression (parser
);
31824 if (!parens
.require_close (parser
))
31825 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31826 /*or_comma=*/false,
31827 /*consume_paren=*/true);
31829 if (num
== error_mark_node
)
31831 num
= fold_non_dependent_expr (num
);
31832 if (!tree_fits_shwi_p (num
)
31833 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31834 || (n
= tree_to_shwi (num
)) <= 0
31837 error_at (loc
, "collapse argument needs positive constant integer expression");
31841 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
31842 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
31843 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
31844 OMP_CLAUSE_CHAIN (c
) = list
;
31845 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
31851 default ( none | shared )
31854 default ( none | present ) */
31857 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
31858 location_t location
, bool is_oacc
)
31860 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
31863 matching_parens parens
;
31864 if (!parens
.require_open (parser
))
31866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31868 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31869 const char *p
= IDENTIFIER_POINTER (id
);
31874 if (strcmp ("none", p
) != 0)
31876 kind
= OMP_CLAUSE_DEFAULT_NONE
;
31880 if (strcmp ("present", p
) != 0 || !is_oacc
)
31882 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
31886 if (strcmp ("shared", p
) != 0 || is_oacc
)
31888 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
31895 cp_lexer_consume_token (parser
->lexer
);
31901 cp_parser_error (parser
, "expected %<none%> or %<present%>");
31903 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
31906 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
31907 || !parens
.require_close (parser
))
31908 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31909 /*or_comma=*/false,
31910 /*consume_paren=*/true);
31912 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
31915 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
31916 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
31917 OMP_CLAUSE_CHAIN (c
) = list
;
31918 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
31924 final ( expression ) */
31927 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
31931 matching_parens parens
;
31932 if (!parens
.require_open (parser
))
31935 t
= cp_parser_condition (parser
);
31937 if (t
== error_mark_node
31938 || !parens
.require_close (parser
))
31939 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31940 /*or_comma=*/false,
31941 /*consume_paren=*/true);
31943 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
31945 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
31946 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
31947 OMP_CLAUSE_CHAIN (c
) = list
;
31956 if ( directive-name-modifier : expression )
31958 directive-name-modifier:
31959 parallel | task | taskloop | target data | target | target update
31960 | target enter data | target exit data */
31963 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
31967 enum tree_code if_modifier
= ERROR_MARK
;
31969 matching_parens parens
;
31970 if (!parens
.require_open (parser
))
31973 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31975 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31976 const char *p
= IDENTIFIER_POINTER (id
);
31979 if (strcmp ("parallel", p
) == 0)
31980 if_modifier
= OMP_PARALLEL
;
31981 else if (strcmp ("task", p
) == 0)
31982 if_modifier
= OMP_TASK
;
31983 else if (strcmp ("taskloop", p
) == 0)
31984 if_modifier
= OMP_TASKLOOP
;
31985 else if (strcmp ("target", p
) == 0)
31987 if_modifier
= OMP_TARGET
;
31988 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
31990 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
31991 p
= IDENTIFIER_POINTER (id
);
31992 if (strcmp ("data", p
) == 0)
31993 if_modifier
= OMP_TARGET_DATA
;
31994 else if (strcmp ("update", p
) == 0)
31995 if_modifier
= OMP_TARGET_UPDATE
;
31996 else if (strcmp ("enter", p
) == 0)
31997 if_modifier
= OMP_TARGET_ENTER_DATA
;
31998 else if (strcmp ("exit", p
) == 0)
31999 if_modifier
= OMP_TARGET_EXIT_DATA
;
32000 if (if_modifier
!= OMP_TARGET
)
32005 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32006 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32008 if_modifier
= ERROR_MARK
;
32010 if (if_modifier
== OMP_TARGET_ENTER_DATA
32011 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32013 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32015 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32016 p
= IDENTIFIER_POINTER (id
);
32017 if (strcmp ("data", p
) == 0)
32023 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32024 error_at (loc
, "expected %<data%>");
32025 if_modifier
= ERROR_MARK
;
32030 if (if_modifier
!= ERROR_MARK
)
32032 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32035 cp_lexer_consume_token (parser
->lexer
);
32042 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32043 error_at (loc
, "expected %<:%>");
32045 if_modifier
= ERROR_MARK
;
32050 t
= cp_parser_condition (parser
);
32052 if (t
== error_mark_node
32053 || !parens
.require_close (parser
))
32054 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32055 /*or_comma=*/false,
32056 /*consume_paren=*/true);
32058 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32059 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32061 if (if_modifier
!= ERROR_MARK
32062 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32064 const char *p
= NULL
;
32065 switch (if_modifier
)
32067 case OMP_PARALLEL
: p
= "parallel"; break;
32068 case OMP_TASK
: p
= "task"; break;
32069 case OMP_TASKLOOP
: p
= "taskloop"; break;
32070 case OMP_TARGET_DATA
: p
= "target data"; break;
32071 case OMP_TARGET
: p
= "target"; break;
32072 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32073 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32074 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32075 default: gcc_unreachable ();
32077 error_at (location
, "too many %<if%> clauses with %qs modifier",
32081 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32084 error_at (location
, "too many %<if%> clauses");
32086 error_at (location
, "too many %<if%> clauses without modifier");
32089 else if (if_modifier
== ERROR_MARK
32090 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32092 error_at (location
, "if any %<if%> clause has modifier, then all "
32093 "%<if%> clauses have to use modifier");
32098 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32099 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32100 OMP_CLAUSE_IF_EXPR (c
) = t
;
32101 OMP_CLAUSE_CHAIN (c
) = list
;
32110 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32111 tree list
, location_t location
)
32115 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32118 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32119 OMP_CLAUSE_CHAIN (c
) = list
;
32127 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32128 tree list
, location_t location
)
32132 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32134 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32135 OMP_CLAUSE_CHAIN (c
) = list
;
32140 num_threads ( expression ) */
32143 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32144 location_t location
)
32148 matching_parens parens
;
32149 if (!parens
.require_open (parser
))
32152 t
= cp_parser_expression (parser
);
32154 if (t
== error_mark_node
32155 || !parens
.require_close (parser
))
32156 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32157 /*or_comma=*/false,
32158 /*consume_paren=*/true);
32160 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32161 "num_threads", location
);
32163 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32164 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32165 OMP_CLAUSE_CHAIN (c
) = list
;
32171 num_tasks ( expression ) */
32174 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32175 location_t location
)
32179 matching_parens parens
;
32180 if (!parens
.require_open (parser
))
32183 t
= cp_parser_expression (parser
);
32185 if (t
== error_mark_node
32186 || !parens
.require_close (parser
))
32187 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32188 /*or_comma=*/false,
32189 /*consume_paren=*/true);
32191 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32192 "num_tasks", location
);
32194 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32195 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32196 OMP_CLAUSE_CHAIN (c
) = list
;
32202 grainsize ( expression ) */
32205 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32206 location_t location
)
32210 matching_parens parens
;
32211 if (!parens
.require_open (parser
))
32214 t
= cp_parser_expression (parser
);
32216 if (t
== error_mark_node
32217 || !parens
.require_close (parser
))
32218 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32219 /*or_comma=*/false,
32220 /*consume_paren=*/true);
32222 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32223 "grainsize", location
);
32225 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32226 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32227 OMP_CLAUSE_CHAIN (c
) = list
;
32233 priority ( expression ) */
32236 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32237 location_t location
)
32241 matching_parens parens
;
32242 if (!parens
.require_open (parser
))
32245 t
= cp_parser_expression (parser
);
32247 if (t
== error_mark_node
32248 || !parens
.require_close (parser
))
32249 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32250 /*or_comma=*/false,
32251 /*consume_paren=*/true);
32253 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32254 "priority", location
);
32256 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32257 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32258 OMP_CLAUSE_CHAIN (c
) = list
;
32264 hint ( expression ) */
32267 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32268 location_t location
)
32272 matching_parens parens
;
32273 if (!parens
.require_open (parser
))
32276 t
= cp_parser_expression (parser
);
32278 if (t
== error_mark_node
32279 || !parens
.require_close (parser
))
32280 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32281 /*or_comma=*/false,
32282 /*consume_paren=*/true);
32284 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32286 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32287 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32288 OMP_CLAUSE_CHAIN (c
) = list
;
32294 defaultmap ( tofrom : scalar ) */
32297 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32298 location_t location
)
32303 matching_parens parens
;
32304 if (!parens
.require_open (parser
))
32307 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32309 cp_parser_error (parser
, "expected %<tofrom%>");
32312 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32313 p
= IDENTIFIER_POINTER (id
);
32314 if (strcmp (p
, "tofrom") != 0)
32316 cp_parser_error (parser
, "expected %<tofrom%>");
32319 cp_lexer_consume_token (parser
->lexer
);
32320 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32323 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32325 cp_parser_error (parser
, "expected %<scalar%>");
32328 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32329 p
= IDENTIFIER_POINTER (id
);
32330 if (strcmp (p
, "scalar") != 0)
32332 cp_parser_error (parser
, "expected %<scalar%>");
32335 cp_lexer_consume_token (parser
->lexer
);
32336 if (!parens
.require_close (parser
))
32339 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32342 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32343 OMP_CLAUSE_CHAIN (c
) = list
;
32347 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32348 /*or_comma=*/false,
32349 /*consume_paren=*/true);
32357 ordered ( constant-expression ) */
32360 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32361 tree list
, location_t location
)
32363 tree c
, num
= NULL_TREE
;
32366 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32367 "ordered", location
);
32369 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32371 matching_parens parens
;
32372 parens
.consume_open (parser
);
32374 num
= cp_parser_constant_expression (parser
);
32376 if (!parens
.require_close (parser
))
32377 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32378 /*or_comma=*/false,
32379 /*consume_paren=*/true);
32381 if (num
== error_mark_node
)
32383 num
= fold_non_dependent_expr (num
);
32384 if (!tree_fits_shwi_p (num
)
32385 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32386 || (n
= tree_to_shwi (num
)) <= 0
32389 error_at (location
,
32390 "ordered argument needs positive constant integer "
32396 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32397 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32398 OMP_CLAUSE_CHAIN (c
) = list
;
32403 reduction ( reduction-operator : variable-list )
32405 reduction-operator:
32406 One of: + * - & ^ | && ||
32410 reduction-operator:
32411 One of: + * - & ^ | && || min max
32415 reduction-operator:
32416 One of: + * - & ^ | && ||
32420 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32422 enum tree_code code
= ERROR_MARK
;
32423 tree nlist
, c
, id
= NULL_TREE
;
32425 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32428 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32430 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32431 case CPP_MULT
: code
= MULT_EXPR
; break;
32432 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32433 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32434 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32435 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32436 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32437 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32441 if (code
!= ERROR_MARK
)
32442 cp_lexer_consume_token (parser
->lexer
);
32445 bool saved_colon_corrects_to_scope_p
;
32446 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32447 parser
->colon_corrects_to_scope_p
= false;
32448 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32449 /*check_dependency_p=*/true,
32450 /*template_p=*/NULL
,
32451 /*declarator_p=*/false,
32452 /*optional_p=*/false);
32453 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32454 if (identifier_p (id
))
32456 const char *p
= IDENTIFIER_POINTER (id
);
32458 if (strcmp (p
, "min") == 0)
32460 else if (strcmp (p
, "max") == 0)
32462 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32464 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32466 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32468 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32469 code
= BIT_AND_EXPR
;
32470 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32471 code
= BIT_IOR_EXPR
;
32472 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32473 code
= BIT_XOR_EXPR
;
32474 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32475 code
= TRUTH_ANDIF_EXPR
;
32476 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32477 code
= TRUTH_ORIF_EXPR
;
32478 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32479 tree scope
= parser
->scope
;
32481 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32482 parser
->scope
= NULL_TREE
;
32483 parser
->qualifying_scope
= NULL_TREE
;
32484 parser
->object_scope
= NULL_TREE
;
32488 error ("invalid reduction-identifier");
32490 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32491 /*or_comma=*/false,
32492 /*consume_paren=*/true);
32497 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32500 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32502 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32504 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32505 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32512 schedule ( schedule-kind )
32513 schedule ( schedule-kind , expression )
32516 static | dynamic | guided | runtime | auto
32519 schedule ( schedule-modifier : schedule-kind )
32520 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32528 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32531 int modifiers
= 0, nmodifiers
= 0;
32533 matching_parens parens
;
32534 if (!parens
.require_open (parser
))
32537 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32539 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32541 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32542 const char *p
= IDENTIFIER_POINTER (id
);
32543 if (strcmp ("simd", p
) == 0)
32544 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32545 else if (strcmp ("monotonic", p
) == 0)
32546 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32547 else if (strcmp ("nonmonotonic", p
) == 0)
32548 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32551 cp_lexer_consume_token (parser
->lexer
);
32552 if (nmodifiers
++ == 0
32553 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32554 cp_lexer_consume_token (parser
->lexer
);
32557 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32564 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32565 const char *p
= IDENTIFIER_POINTER (id
);
32570 if (strcmp ("dynamic", p
) != 0)
32572 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32576 if (strcmp ("guided", p
) != 0)
32578 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32582 if (strcmp ("runtime", p
) != 0)
32584 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32591 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32592 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32593 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32594 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32597 cp_lexer_consume_token (parser
->lexer
);
32599 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32600 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32601 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32602 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32604 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32612 cp_lexer_consume_token (parser
->lexer
);
32614 token
= cp_lexer_peek_token (parser
->lexer
);
32615 t
= cp_parser_assignment_expression (parser
);
32617 if (t
== error_mark_node
)
32619 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32620 error_at (token
->location
, "schedule %<runtime%> does not take "
32621 "a %<chunk_size%> parameter");
32622 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32623 error_at (token
->location
, "schedule %<auto%> does not take "
32624 "a %<chunk_size%> parameter");
32626 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32628 if (!parens
.require_close (parser
))
32631 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32634 OMP_CLAUSE_SCHEDULE_KIND (c
)
32635 = (enum omp_clause_schedule_kind
)
32636 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32638 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32639 OMP_CLAUSE_CHAIN (c
) = list
;
32643 cp_parser_error (parser
, "invalid schedule kind");
32645 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32646 /*or_comma=*/false,
32647 /*consume_paren=*/true);
32655 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32656 tree list
, location_t location
)
32660 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32662 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32663 OMP_CLAUSE_CHAIN (c
) = list
;
32672 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32673 tree list
, location_t location
)
32675 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32676 tree c
= build_omp_clause (location
, code
);
32677 OMP_CLAUSE_CHAIN (c
) = list
;
32688 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32689 enum omp_clause_code code
,
32690 tree list
, location_t location
)
32692 tree c
= build_omp_clause (location
, code
);
32693 OMP_CLAUSE_CHAIN (c
) = list
;
32701 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32702 tree list
, location_t location
)
32704 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32705 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32706 OMP_CLAUSE_CHAIN (c
) = list
;
32715 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32716 enum omp_clause_code code
,
32717 tree list
, location_t location
)
32719 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32720 tree c
= build_omp_clause (location
, code
);
32721 OMP_CLAUSE_CHAIN (c
) = list
;
32726 num_teams ( expression ) */
32729 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32730 location_t location
)
32734 matching_parens parens
;
32735 if (!parens
.require_open (parser
))
32738 t
= cp_parser_expression (parser
);
32740 if (t
== error_mark_node
32741 || !parens
.require_close (parser
))
32742 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32743 /*or_comma=*/false,
32744 /*consume_paren=*/true);
32746 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32747 "num_teams", location
);
32749 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32750 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32751 OMP_CLAUSE_CHAIN (c
) = list
;
32757 thread_limit ( expression ) */
32760 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32761 location_t location
)
32765 matching_parens parens
;
32766 if (!parens
.require_open (parser
))
32769 t
= cp_parser_expression (parser
);
32771 if (t
== error_mark_node
32772 || !parens
.require_close (parser
))
32773 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32774 /*or_comma=*/false,
32775 /*consume_paren=*/true);
32777 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32778 "thread_limit", location
);
32780 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32781 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32782 OMP_CLAUSE_CHAIN (c
) = list
;
32788 aligned ( variable-list )
32789 aligned ( variable-list : constant-expression ) */
32792 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32794 tree nlist
, c
, alignment
= NULL_TREE
;
32797 matching_parens parens
;
32798 if (!parens
.require_open (parser
))
32801 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32806 alignment
= cp_parser_constant_expression (parser
);
32808 if (!parens
.require_close (parser
))
32809 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32810 /*or_comma=*/false,
32811 /*consume_paren=*/true);
32813 if (alignment
== error_mark_node
)
32814 alignment
= NULL_TREE
;
32817 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32818 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
32824 linear ( variable-list )
32825 linear ( variable-list : expression )
32828 linear ( modifier ( variable-list ) )
32829 linear ( modifier ( variable-list ) : expression ) */
32832 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
32835 tree nlist
, c
, step
= integer_one_node
;
32837 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32839 matching_parens parens
;
32840 if (!parens
.require_open (parser
))
32843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32845 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32846 const char *p
= IDENTIFIER_POINTER (id
);
32848 if (strcmp ("ref", p
) == 0)
32849 kind
= OMP_CLAUSE_LINEAR_REF
;
32850 else if (strcmp ("val", p
) == 0)
32851 kind
= OMP_CLAUSE_LINEAR_VAL
;
32852 else if (strcmp ("uval", p
) == 0)
32853 kind
= OMP_CLAUSE_LINEAR_UVAL
;
32854 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
32855 cp_lexer_consume_token (parser
->lexer
);
32857 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32860 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
32861 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
32865 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
32866 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
32868 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32869 else if (!parens
.require_close (parser
))
32870 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32871 /*or_comma=*/false,
32872 /*consume_paren=*/true);
32879 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32880 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
32882 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32883 cp_parser_parse_tentatively (parser
);
32884 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
32885 /*check_dependency_p=*/true,
32886 /*template_p=*/NULL
,
32887 /*declarator_p=*/false,
32888 /*optional_p=*/false);
32889 if (step
!= error_mark_node
)
32890 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
32891 if (step
== error_mark_node
)
32894 cp_parser_abort_tentative_parse (parser
);
32896 else if (!cp_parser_parse_definitely (parser
))
32900 step
= cp_parser_expression (parser
);
32902 if (!parens
.require_close (parser
))
32903 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32904 /*or_comma=*/false,
32905 /*consume_paren=*/true);
32907 if (step
== error_mark_node
)
32911 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32913 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
32914 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
32921 safelen ( constant-expression ) */
32924 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
32925 location_t location
)
32929 matching_parens parens
;
32930 if (!parens
.require_open (parser
))
32933 t
= cp_parser_constant_expression (parser
);
32935 if (t
== error_mark_node
32936 || !parens
.require_close (parser
))
32937 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32938 /*or_comma=*/false,
32939 /*consume_paren=*/true);
32941 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
32943 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
32944 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
32945 OMP_CLAUSE_CHAIN (c
) = list
;
32951 simdlen ( constant-expression ) */
32954 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
32955 location_t location
)
32959 matching_parens parens
;
32960 if (!parens
.require_open (parser
))
32963 t
= cp_parser_constant_expression (parser
);
32965 if (t
== error_mark_node
32966 || !parens
.require_close (parser
))
32967 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32968 /*or_comma=*/false,
32969 /*consume_paren=*/true);
32971 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
32973 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
32974 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
32975 OMP_CLAUSE_CHAIN (c
) = list
;
32982 identifier [+/- integer]
32983 vec , identifier [+/- integer]
32987 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
32992 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32994 cp_parser_error (parser
, "expected identifier");
32998 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33000 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33001 tree t
, identifier
= cp_parser_identifier (parser
);
33002 tree addend
= NULL
;
33004 if (identifier
== error_mark_node
)
33005 t
= error_mark_node
;
33008 t
= cp_parser_lookup_name_simple
33009 (parser
, identifier
,
33010 cp_lexer_peek_token (parser
->lexer
)->location
);
33011 if (t
== error_mark_node
)
33012 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33019 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33021 addend
= integer_zero_node
;
33022 goto add_to_vector
;
33024 cp_lexer_consume_token (parser
->lexer
);
33026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33028 cp_parser_error (parser
, "expected integer");
33032 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33033 if (TREE_CODE (addend
) != INTEGER_CST
)
33035 cp_parser_error (parser
, "expected integer");
33038 cp_lexer_consume_token (parser
->lexer
);
33041 if (t
!= error_mark_node
)
33043 vec
= tree_cons (addend
, t
, vec
);
33045 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33048 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33051 cp_lexer_consume_token (parser
->lexer
);
33054 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33056 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33057 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33058 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33059 OMP_CLAUSE_CHAIN (u
) = list
;
33066 depend ( depend-kind : variable-list )
33074 depend ( sink : vec ) */
33077 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33080 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33082 matching_parens parens
;
33083 if (!parens
.require_open (parser
))
33086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33088 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33089 const char *p
= IDENTIFIER_POINTER (id
);
33091 if (strcmp ("in", p
) == 0)
33092 kind
= OMP_CLAUSE_DEPEND_IN
;
33093 else if (strcmp ("inout", p
) == 0)
33094 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33095 else if (strcmp ("out", p
) == 0)
33096 kind
= OMP_CLAUSE_DEPEND_OUT
;
33097 else if (strcmp ("source", p
) == 0)
33098 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33099 else if (strcmp ("sink", p
) == 0)
33100 kind
= OMP_CLAUSE_DEPEND_SINK
;
33107 cp_lexer_consume_token (parser
->lexer
);
33109 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33111 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33112 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33113 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33114 OMP_CLAUSE_CHAIN (c
) = list
;
33115 if (!parens
.require_close (parser
))
33116 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33117 /*or_comma=*/false,
33118 /*consume_paren=*/true);
33122 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33125 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33126 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33129 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33132 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33133 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33138 cp_parser_error (parser
, "invalid depend kind");
33140 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33141 /*or_comma=*/false,
33142 /*consume_paren=*/true);
33147 map ( map-kind : variable-list )
33148 map ( variable-list )
33151 alloc | to | from | tofrom
33155 alloc | to | from | tofrom | release | delete
33157 map ( always [,] map-kind: variable-list ) */
33160 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33163 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33164 bool always
= false;
33166 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33171 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33172 const char *p
= IDENTIFIER_POINTER (id
);
33174 if (strcmp ("always", p
) == 0)
33177 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33179 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33180 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33182 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33186 cp_lexer_consume_token (parser
->lexer
);
33188 cp_lexer_consume_token (parser
->lexer
);
33193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33194 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33196 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33197 const char *p
= IDENTIFIER_POINTER (id
);
33199 if (strcmp ("alloc", p
) == 0)
33200 kind
= GOMP_MAP_ALLOC
;
33201 else if (strcmp ("to", p
) == 0)
33202 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33203 else if (strcmp ("from", p
) == 0)
33204 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33205 else if (strcmp ("tofrom", p
) == 0)
33206 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33207 else if (strcmp ("release", p
) == 0)
33208 kind
= GOMP_MAP_RELEASE
;
33211 cp_parser_error (parser
, "invalid map kind");
33212 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33213 /*or_comma=*/false,
33214 /*consume_paren=*/true);
33217 cp_lexer_consume_token (parser
->lexer
);
33218 cp_lexer_consume_token (parser
->lexer
);
33220 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33221 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33223 kind
= GOMP_MAP_DELETE
;
33224 cp_lexer_consume_token (parser
->lexer
);
33225 cp_lexer_consume_token (parser
->lexer
);
33228 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33231 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33232 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33238 device ( expression ) */
33241 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33242 location_t location
)
33246 matching_parens parens
;
33247 if (!parens
.require_open (parser
))
33250 t
= cp_parser_expression (parser
);
33252 if (t
== error_mark_node
33253 || !parens
.require_close (parser
))
33254 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33255 /*or_comma=*/false,
33256 /*consume_paren=*/true);
33258 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33259 "device", location
);
33261 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33262 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33263 OMP_CLAUSE_CHAIN (c
) = list
;
33269 dist_schedule ( static )
33270 dist_schedule ( static , expression ) */
33273 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33274 location_t location
)
33278 matching_parens parens
;
33279 if (!parens
.require_open (parser
))
33282 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33284 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33286 cp_lexer_consume_token (parser
->lexer
);
33288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33290 cp_lexer_consume_token (parser
->lexer
);
33292 t
= cp_parser_assignment_expression (parser
);
33294 if (t
== error_mark_node
)
33296 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33298 if (!parens
.require_close (parser
))
33301 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33304 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33306 OMP_CLAUSE_CHAIN (c
) = list
;
33310 cp_parser_error (parser
, "invalid dist_schedule kind");
33312 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33313 /*or_comma=*/false,
33314 /*consume_paren=*/true);
33319 proc_bind ( proc-bind-kind )
33322 master | close | spread */
33325 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33326 location_t location
)
33329 enum omp_clause_proc_bind_kind kind
;
33331 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33336 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33337 const char *p
= IDENTIFIER_POINTER (id
);
33339 if (strcmp ("master", p
) == 0)
33340 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33341 else if (strcmp ("close", p
) == 0)
33342 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33343 else if (strcmp ("spread", p
) == 0)
33344 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33351 cp_lexer_consume_token (parser
->lexer
);
33352 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33355 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33356 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33358 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33359 OMP_CLAUSE_CHAIN (c
) = list
;
33363 cp_parser_error (parser
, "invalid depend kind");
33365 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33366 /*or_comma=*/false,
33367 /*consume_paren=*/true);
33372 async [( int-expr )] */
33375 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33378 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33380 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33382 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33384 matching_parens parens
;
33385 parens
.consume_open (parser
);
33387 t
= cp_parser_expression (parser
);
33388 if (t
== error_mark_node
33389 || !parens
.require_close (parser
))
33390 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33391 /*or_comma=*/false,
33392 /*consume_paren=*/true);
33395 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33397 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33398 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33399 OMP_CLAUSE_CHAIN (c
) = list
;
33405 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33406 is a bitmask in MASK. Return the list of clauses found. */
33409 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33410 const char *where
, cp_token
*pragma_tok
,
33411 bool finish_p
= true)
33413 tree clauses
= NULL
;
33416 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33419 pragma_omp_clause c_kind
;
33420 omp_clause_code code
;
33421 const char *c_name
;
33422 tree prev
= clauses
;
33424 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33425 cp_lexer_consume_token (parser
->lexer
);
33427 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33428 c_kind
= cp_parser_omp_clause_name (parser
);
33432 case PRAGMA_OACC_CLAUSE_ASYNC
:
33433 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33436 case PRAGMA_OACC_CLAUSE_AUTO
:
33437 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33441 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33442 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33443 c_name
= "collapse";
33445 case PRAGMA_OACC_CLAUSE_COPY
:
33446 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33449 case PRAGMA_OACC_CLAUSE_COPYIN
:
33450 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33453 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33454 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33455 c_name
= "copyout";
33457 case PRAGMA_OACC_CLAUSE_CREATE
:
33458 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33461 case PRAGMA_OACC_CLAUSE_DELETE
:
33462 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33465 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33466 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33467 c_name
= "default";
33469 case PRAGMA_OACC_CLAUSE_DEVICE
:
33470 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33473 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33474 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33475 c_name
= "deviceptr";
33477 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33478 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33479 c_name
= "device_resident";
33481 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33482 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33484 c_name
= "firstprivate";
33486 case PRAGMA_OACC_CLAUSE_GANG
:
33488 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33491 case PRAGMA_OACC_CLAUSE_HOST
:
33492 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33495 case PRAGMA_OACC_CLAUSE_IF
:
33496 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33499 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33500 clauses
= cp_parser_oacc_simple_clause (parser
,
33501 OMP_CLAUSE_INDEPENDENT
,
33503 c_name
= "independent";
33505 case PRAGMA_OACC_CLAUSE_LINK
:
33506 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33509 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33510 code
= OMP_CLAUSE_NUM_GANGS
;
33511 c_name
= "num_gangs";
33512 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33515 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33516 c_name
= "num_workers";
33517 code
= OMP_CLAUSE_NUM_WORKERS
;
33518 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33521 case PRAGMA_OACC_CLAUSE_PRESENT
:
33522 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33523 c_name
= "present";
33525 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33526 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33527 c_name
= "present_or_copy";
33529 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33530 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33531 c_name
= "present_or_copyin";
33533 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33534 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33535 c_name
= "present_or_copyout";
33537 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33538 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33539 c_name
= "present_or_create";
33541 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33542 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33544 c_name
= "private";
33546 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33547 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33548 c_name
= "reduction";
33550 case PRAGMA_OACC_CLAUSE_SELF
:
33551 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33554 case PRAGMA_OACC_CLAUSE_SEQ
:
33555 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33559 case PRAGMA_OACC_CLAUSE_TILE
:
33560 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33563 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33564 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33566 c_name
= "use_device";
33568 case PRAGMA_OACC_CLAUSE_VECTOR
:
33570 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33573 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33574 c_name
= "vector_length";
33575 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33576 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33579 case PRAGMA_OACC_CLAUSE_WAIT
:
33580 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33583 case PRAGMA_OACC_CLAUSE_WORKER
:
33585 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33589 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33595 if (((mask
>> c_kind
) & 1) == 0)
33597 /* Remove the invalid clause(s) from the list to avoid
33598 confusing the rest of the compiler. */
33600 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33605 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33608 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33613 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33614 is a bitmask in MASK. Return the list of clauses found; the result
33615 of clause default goes in *pdefault. */
33618 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33619 const char *where
, cp_token
*pragma_tok
,
33620 bool finish_p
= true)
33622 tree clauses
= NULL
;
33624 cp_token
*token
= NULL
;
33626 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33628 pragma_omp_clause c_kind
;
33629 const char *c_name
;
33630 tree prev
= clauses
;
33632 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33633 cp_lexer_consume_token (parser
->lexer
);
33635 token
= cp_lexer_peek_token (parser
->lexer
);
33636 c_kind
= cp_parser_omp_clause_name (parser
);
33640 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33641 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33643 c_name
= "collapse";
33645 case PRAGMA_OMP_CLAUSE_COPYIN
:
33646 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33649 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33650 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33652 c_name
= "copyprivate";
33654 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33655 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33656 token
->location
, false);
33657 c_name
= "default";
33659 case PRAGMA_OMP_CLAUSE_FINAL
:
33660 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33663 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33664 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33666 c_name
= "firstprivate";
33668 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33669 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33671 c_name
= "grainsize";
33673 case PRAGMA_OMP_CLAUSE_HINT
:
33674 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33678 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33679 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33681 c_name
= "defaultmap";
33683 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33684 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33686 c_name
= "use_device_ptr";
33688 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33689 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33691 c_name
= "is_device_ptr";
33693 case PRAGMA_OMP_CLAUSE_IF
:
33694 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33698 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33699 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33701 c_name
= "lastprivate";
33703 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33704 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33706 c_name
= "mergeable";
33708 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33709 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33712 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33713 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33715 c_name
= "num_tasks";
33717 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33718 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33720 c_name
= "num_threads";
33722 case PRAGMA_OMP_CLAUSE_ORDERED
:
33723 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33725 c_name
= "ordered";
33727 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33728 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33730 c_name
= "priority";
33732 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33733 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33735 c_name
= "private";
33737 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33738 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33739 c_name
= "reduction";
33741 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33742 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33744 c_name
= "schedule";
33746 case PRAGMA_OMP_CLAUSE_SHARED
:
33747 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33751 case PRAGMA_OMP_CLAUSE_UNTIED
:
33752 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33756 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33757 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33758 clauses
, token
->location
);
33759 c_name
= "inbranch";
33761 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33762 clauses
= cp_parser_omp_clause_branch (parser
,
33763 OMP_CLAUSE_NOTINBRANCH
,
33764 clauses
, token
->location
);
33765 c_name
= "notinbranch";
33767 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33768 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33769 clauses
, token
->location
);
33770 c_name
= "parallel";
33774 error_at (token
->location
, "%qs must be the first clause of %qs",
33779 case PRAGMA_OMP_CLAUSE_FOR
:
33780 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33781 clauses
, token
->location
);
33784 goto clause_not_first
;
33786 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33787 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33788 clauses
, token
->location
);
33789 c_name
= "sections";
33791 goto clause_not_first
;
33793 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33794 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33795 clauses
, token
->location
);
33796 c_name
= "taskgroup";
33798 goto clause_not_first
;
33800 case PRAGMA_OMP_CLAUSE_LINK
:
33801 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33804 case PRAGMA_OMP_CLAUSE_TO
:
33805 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33806 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33809 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33812 case PRAGMA_OMP_CLAUSE_FROM
:
33813 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
33816 case PRAGMA_OMP_CLAUSE_UNIFORM
:
33817 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
33819 c_name
= "uniform";
33821 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
33822 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
33824 c_name
= "num_teams";
33826 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
33827 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
33829 c_name
= "thread_limit";
33831 case PRAGMA_OMP_CLAUSE_ALIGNED
:
33832 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
33833 c_name
= "aligned";
33835 case PRAGMA_OMP_CLAUSE_LINEAR
:
33837 bool declare_simd
= false;
33838 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
33839 declare_simd
= true;
33840 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
33844 case PRAGMA_OMP_CLAUSE_DEPEND
:
33845 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
33849 case PRAGMA_OMP_CLAUSE_MAP
:
33850 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
33853 case PRAGMA_OMP_CLAUSE_DEVICE
:
33854 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
33858 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
33859 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
33861 c_name
= "dist_schedule";
33863 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
33864 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
33866 c_name
= "proc_bind";
33868 case PRAGMA_OMP_CLAUSE_SAFELEN
:
33869 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
33871 c_name
= "safelen";
33873 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
33874 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
33876 c_name
= "simdlen";
33878 case PRAGMA_OMP_CLAUSE_NOGROUP
:
33879 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
33881 c_name
= "nogroup";
33883 case PRAGMA_OMP_CLAUSE_THREADS
:
33885 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
33886 clauses
, token
->location
);
33887 c_name
= "threads";
33889 case PRAGMA_OMP_CLAUSE_SIMD
:
33891 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
33892 clauses
, token
->location
);
33896 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
33902 if (((mask
>> c_kind
) & 1) == 0)
33904 /* Remove the invalid clause(s) from the list to avoid
33905 confusing the rest of the compiler. */
33907 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
33911 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33914 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
33915 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
33917 return finish_omp_clauses (clauses
, C_ORT_OMP
);
33926 In practice, we're also interested in adding the statement to an
33927 outer node. So it is convenient if we work around the fact that
33928 cp_parser_statement calls add_stmt. */
33931 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
33933 unsigned save
= parser
->in_statement
;
33935 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33936 This preserves the "not within loop or switch" style error messages
33937 for nonsense cases like
33943 if (parser
->in_statement
)
33944 parser
->in_statement
= IN_OMP_BLOCK
;
33950 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
33952 parser
->in_statement
= save
;
33956 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
33958 tree stmt
= begin_omp_structured_block ();
33959 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
33961 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
33963 cp_parser_end_omp_structured_block (parser
, save
);
33964 return finish_omp_structured_block (stmt
);
33968 # pragma omp atomic new-line
33972 x binop= expr | x++ | ++x | x-- | --x
33974 +, *, -, /, &, ^, |, <<, >>
33976 where x is an lvalue expression with scalar type.
33979 # pragma omp atomic new-line
33982 # pragma omp atomic read new-line
33985 # pragma omp atomic write new-line
33988 # pragma omp atomic update new-line
33991 # pragma omp atomic capture new-line
33994 # pragma omp atomic capture new-line
34002 expression-stmt | x = x binop expr
34004 v = expression-stmt
34006 { v = x; update-stmt; } | { update-stmt; v = x; }
34010 expression-stmt | x = x binop expr | x = expr binop x
34014 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34016 where x and v are lvalue expressions with scalar type. */
34019 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34021 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34022 tree rhs1
= NULL_TREE
, orig_lhs
;
34023 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34024 bool structured_block
= false;
34025 bool seq_cst
= false;
34027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34029 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34030 const char *p
= IDENTIFIER_POINTER (id
);
34032 if (!strcmp (p
, "seq_cst"))
34035 cp_lexer_consume_token (parser
->lexer
);
34036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34037 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34038 cp_lexer_consume_token (parser
->lexer
);
34041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34043 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34044 const char *p
= IDENTIFIER_POINTER (id
);
34046 if (!strcmp (p
, "read"))
34047 code
= OMP_ATOMIC_READ
;
34048 else if (!strcmp (p
, "write"))
34050 else if (!strcmp (p
, "update"))
34052 else if (!strcmp (p
, "capture"))
34053 code
= OMP_ATOMIC_CAPTURE_NEW
;
34057 cp_lexer_consume_token (parser
->lexer
);
34061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34062 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34063 cp_lexer_consume_token (parser
->lexer
);
34065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34067 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34068 const char *p
= IDENTIFIER_POINTER (id
);
34070 if (!strcmp (p
, "seq_cst"))
34073 cp_lexer_consume_token (parser
->lexer
);
34077 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34081 case OMP_ATOMIC_READ
:
34082 case NOP_EXPR
: /* atomic write */
34083 v
= cp_parser_unary_expression (parser
);
34084 if (v
== error_mark_node
)
34086 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34088 if (code
== NOP_EXPR
)
34089 lhs
= cp_parser_expression (parser
);
34091 lhs
= cp_parser_unary_expression (parser
);
34092 if (lhs
== error_mark_node
)
34094 if (code
== NOP_EXPR
)
34096 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34104 case OMP_ATOMIC_CAPTURE_NEW
:
34105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34107 cp_lexer_consume_token (parser
->lexer
);
34108 structured_block
= true;
34112 v
= cp_parser_unary_expression (parser
);
34113 if (v
== error_mark_node
)
34115 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34123 lhs
= cp_parser_unary_expression (parser
);
34125 switch (TREE_CODE (lhs
))
34130 case POSTINCREMENT_EXPR
:
34131 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34132 code
= OMP_ATOMIC_CAPTURE_OLD
;
34134 case PREINCREMENT_EXPR
:
34135 lhs
= TREE_OPERAND (lhs
, 0);
34136 opcode
= PLUS_EXPR
;
34137 rhs
= integer_one_node
;
34140 case POSTDECREMENT_EXPR
:
34141 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34142 code
= OMP_ATOMIC_CAPTURE_OLD
;
34144 case PREDECREMENT_EXPR
:
34145 lhs
= TREE_OPERAND (lhs
, 0);
34146 opcode
= MINUS_EXPR
;
34147 rhs
= integer_one_node
;
34150 case COMPOUND_EXPR
:
34151 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34152 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34153 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34154 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34155 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34156 (TREE_OPERAND (lhs
, 1), 0), 0)))
34158 /* Undo effects of boolean_increment for post {in,de}crement. */
34159 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34162 if (TREE_CODE (lhs
) == MODIFY_EXPR
34163 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34165 /* Undo effects of boolean_increment. */
34166 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34168 /* This is pre or post increment. */
34169 rhs
= TREE_OPERAND (lhs
, 1);
34170 lhs
= TREE_OPERAND (lhs
, 0);
34172 if (code
== OMP_ATOMIC_CAPTURE_NEW
34173 && !structured_block
34174 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34175 code
= OMP_ATOMIC_CAPTURE_OLD
;
34181 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34184 opcode
= MULT_EXPR
;
34187 opcode
= TRUNC_DIV_EXPR
;
34190 opcode
= PLUS_EXPR
;
34193 opcode
= MINUS_EXPR
;
34195 case CPP_LSHIFT_EQ
:
34196 opcode
= LSHIFT_EXPR
;
34198 case CPP_RSHIFT_EQ
:
34199 opcode
= RSHIFT_EXPR
;
34202 opcode
= BIT_AND_EXPR
;
34205 opcode
= BIT_IOR_EXPR
;
34208 opcode
= BIT_XOR_EXPR
;
34211 enum cp_parser_prec oprec
;
34213 cp_lexer_consume_token (parser
->lexer
);
34214 cp_parser_parse_tentatively (parser
);
34215 rhs1
= cp_parser_simple_cast_expression (parser
);
34216 if (rhs1
== error_mark_node
)
34218 cp_parser_abort_tentative_parse (parser
);
34219 cp_parser_simple_cast_expression (parser
);
34222 token
= cp_lexer_peek_token (parser
->lexer
);
34223 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34225 cp_parser_abort_tentative_parse (parser
);
34226 cp_parser_parse_tentatively (parser
);
34227 rhs
= cp_parser_binary_expression (parser
, false, true,
34228 PREC_NOT_OPERATOR
, NULL
);
34229 if (rhs
== error_mark_node
)
34231 cp_parser_abort_tentative_parse (parser
);
34232 cp_parser_binary_expression (parser
, false, true,
34233 PREC_NOT_OPERATOR
, NULL
);
34236 switch (TREE_CODE (rhs
))
34239 case TRUNC_DIV_EXPR
:
34248 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34250 if (cp_parser_parse_definitely (parser
))
34252 opcode
= TREE_CODE (rhs
);
34253 rhs1
= TREE_OPERAND (rhs
, 0);
34254 rhs
= TREE_OPERAND (rhs
, 1);
34264 cp_parser_abort_tentative_parse (parser
);
34265 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34267 rhs
= cp_parser_expression (parser
);
34268 if (rhs
== error_mark_node
)
34274 cp_parser_error (parser
,
34275 "invalid form of %<#pragma omp atomic%>");
34278 if (!cp_parser_parse_definitely (parser
))
34280 switch (token
->type
)
34282 case CPP_SEMICOLON
:
34283 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34285 code
= OMP_ATOMIC_CAPTURE_OLD
;
34290 cp_lexer_consume_token (parser
->lexer
);
34293 else if (structured_block
)
34300 cp_parser_error (parser
,
34301 "invalid form of %<#pragma omp atomic%>");
34304 opcode
= MULT_EXPR
;
34307 opcode
= TRUNC_DIV_EXPR
;
34310 opcode
= PLUS_EXPR
;
34313 opcode
= MINUS_EXPR
;
34316 opcode
= LSHIFT_EXPR
;
34319 opcode
= RSHIFT_EXPR
;
34322 opcode
= BIT_AND_EXPR
;
34325 opcode
= BIT_IOR_EXPR
;
34328 opcode
= BIT_XOR_EXPR
;
34331 cp_parser_error (parser
,
34332 "invalid operator for %<#pragma omp atomic%>");
34335 oprec
= TOKEN_PRECEDENCE (token
);
34336 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34337 if (commutative_tree_code (opcode
))
34338 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34339 cp_lexer_consume_token (parser
->lexer
);
34340 rhs
= cp_parser_binary_expression (parser
, false, false,
34342 if (rhs
== error_mark_node
)
34347 cp_parser_error (parser
,
34348 "invalid operator for %<#pragma omp atomic%>");
34351 cp_lexer_consume_token (parser
->lexer
);
34353 rhs
= cp_parser_expression (parser
);
34354 if (rhs
== error_mark_node
)
34359 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34361 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34363 v
= cp_parser_unary_expression (parser
);
34364 if (v
== error_mark_node
)
34366 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34368 lhs1
= cp_parser_unary_expression (parser
);
34369 if (lhs1
== error_mark_node
)
34372 if (structured_block
)
34374 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34375 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34378 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34379 if (!structured_block
)
34380 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34384 cp_parser_skip_to_end_of_block_or_statement (parser
);
34385 if (structured_block
)
34387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34388 cp_lexer_consume_token (parser
->lexer
);
34389 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34391 cp_parser_skip_to_end_of_block_or_statement (parser
);
34392 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34393 cp_lexer_consume_token (parser
->lexer
);
34400 # pragma omp barrier new-line */
34403 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34405 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34406 finish_omp_barrier ();
34410 # pragma omp critical [(name)] new-line
34414 # pragma omp critical [(name) [hint(expression)]] new-line
34415 structured-block */
34417 #define OMP_CRITICAL_CLAUSE_MASK \
34418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34421 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34423 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34425 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34427 matching_parens parens
;
34428 parens
.consume_open (parser
);
34430 name
= cp_parser_identifier (parser
);
34432 if (name
== error_mark_node
34433 || !parens
.require_close (parser
))
34434 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34435 /*or_comma=*/false,
34436 /*consume_paren=*/true);
34437 if (name
== error_mark_node
)
34440 clauses
= cp_parser_omp_all_clauses (parser
,
34441 OMP_CRITICAL_CLAUSE_MASK
,
34442 "#pragma omp critical", pragma_tok
);
34445 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34447 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34448 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34452 # pragma omp flush flush-vars[opt] new-line
34455 ( variable-list ) */
34458 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34461 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34462 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34464 finish_omp_flush ();
34467 /* Helper function, to parse omp for increment expression. */
34470 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34472 tree cond
= cp_parser_binary_expression (parser
, false, true,
34473 PREC_NOT_OPERATOR
, NULL
);
34474 if (cond
== error_mark_node
34475 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34477 cp_parser_skip_to_end_of_statement (parser
);
34478 return error_mark_node
;
34481 switch (TREE_CODE (cond
))
34489 /* Fall through: OpenMP disallows NE_EXPR. */
34490 gcc_fallthrough ();
34492 return error_mark_node
;
34495 /* If decl is an iterator, preserve LHS and RHS of the relational
34496 expr until finish_omp_for. */
34498 && (type_dependent_expression_p (decl
)
34499 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34502 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34504 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34505 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34506 /*overload=*/NULL
, tf_warning_or_error
);
34509 /* Helper function, to parse omp for increment expression. */
34512 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34514 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34520 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34522 op
= (token
->type
== CPP_PLUS_PLUS
34523 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34524 cp_lexer_consume_token (parser
->lexer
);
34525 lhs
= cp_parser_simple_cast_expression (parser
);
34527 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34528 return error_mark_node
;
34529 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34532 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34534 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34535 return error_mark_node
;
34537 token
= cp_lexer_peek_token (parser
->lexer
);
34538 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34540 op
= (token
->type
== CPP_PLUS_PLUS
34541 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34542 cp_lexer_consume_token (parser
->lexer
);
34543 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34546 op
= cp_parser_assignment_operator_opt (parser
);
34547 if (op
== ERROR_MARK
)
34548 return error_mark_node
;
34550 if (op
!= NOP_EXPR
)
34552 rhs
= cp_parser_assignment_expression (parser
);
34553 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34554 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34557 lhs
= cp_parser_binary_expression (parser
, false, false,
34558 PREC_ADDITIVE_EXPRESSION
, NULL
);
34559 token
= cp_lexer_peek_token (parser
->lexer
);
34560 decl_first
= (lhs
== decl
34561 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34564 if (token
->type
!= CPP_PLUS
34565 && token
->type
!= CPP_MINUS
)
34566 return error_mark_node
;
34570 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34571 cp_lexer_consume_token (parser
->lexer
);
34572 rhs
= cp_parser_binary_expression (parser
, false, false,
34573 PREC_ADDITIVE_EXPRESSION
, NULL
);
34574 token
= cp_lexer_peek_token (parser
->lexer
);
34575 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34577 if (lhs
== NULL_TREE
)
34579 if (op
== PLUS_EXPR
)
34582 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34583 tf_warning_or_error
);
34586 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34587 ERROR_MARK
, NULL
, tf_warning_or_error
);
34590 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34595 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34596 || op
== MINUS_EXPR
)
34597 return error_mark_node
;
34598 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34601 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34603 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34606 /* Parse the initialization statement of an OpenMP for loop.
34608 Return true if the resulting construct should have an
34609 OMP_CLAUSE_PRIVATE added to it. */
34612 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34613 tree
&this_pre_body
,
34614 vec
<tree
, va_gc
> *for_block
,
34620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34623 tree add_private_clause
= NULL_TREE
;
34625 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34629 integer-type var = lb
34630 random-access-iterator-type var = lb
34631 pointer-type var = lb
34633 cp_decl_specifier_seq type_specifiers
;
34635 /* First, try to parse as an initialized declaration. See
34636 cp_parser_condition, from whence the bulk of this is copied. */
34638 cp_parser_parse_tentatively (parser
);
34639 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34640 /*is_trailing_return=*/false,
34642 if (cp_parser_parse_definitely (parser
))
34644 /* If parsing a type specifier seq succeeded, then this
34645 MUST be a initialized declaration. */
34646 tree asm_specification
, attributes
;
34647 cp_declarator
*declarator
;
34649 declarator
= cp_parser_declarator (parser
,
34650 CP_PARSER_DECLARATOR_NAMED
,
34651 /*ctor_dtor_or_conv_p=*/NULL
,
34652 /*parenthesized_p=*/NULL
,
34653 /*member_p=*/false,
34654 /*friend_p=*/false);
34655 attributes
= cp_parser_attributes_opt (parser
);
34656 asm_specification
= cp_parser_asm_specification_opt (parser
);
34658 if (declarator
== cp_error_declarator
)
34659 cp_parser_skip_to_end_of_statement (parser
);
34663 tree pushed_scope
, auto_node
;
34665 decl
= start_decl (declarator
, &type_specifiers
,
34666 SD_INITIALIZED
, attributes
,
34667 /*prefix_attributes=*/NULL_TREE
,
34670 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34671 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34673 if (cp_lexer_next_token_is (parser
->lexer
,
34675 error ("parenthesized initialization is not allowed in "
34676 "OpenMP %<for%> loop");
34678 /* Trigger an error. */
34679 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34681 init
= error_mark_node
;
34682 cp_parser_skip_to_end_of_statement (parser
);
34684 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34685 || type_dependent_expression_p (decl
)
34688 bool is_direct_init
, is_non_constant_init
;
34690 init
= cp_parser_initializer (parser
,
34692 &is_non_constant_init
);
34697 = do_auto_deduction (TREE_TYPE (decl
), init
,
34700 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34701 && !type_dependent_expression_p (decl
))
34705 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34707 LOOKUP_ONLYCONVERTING
);
34709 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34711 vec_safe_push (for_block
, this_pre_body
);
34716 init
= pop_stmt_list (this_pre_body
);
34717 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34719 tree_stmt_iterator i
= tsi_start (init
);
34720 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34721 while (!tsi_end_p (i
))
34723 tree t
= tsi_stmt (i
);
34724 if (TREE_CODE (t
) == DECL_EXPR
34725 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34728 vec_safe_push (for_block
, t
);
34733 if (tsi_one_before_end_p (i
))
34735 tree t
= tsi_stmt (i
);
34737 free_stmt_list (init
);
34742 this_pre_body
= NULL_TREE
;
34747 cp_lexer_consume_token (parser
->lexer
);
34748 init
= cp_parser_assignment_expression (parser
);
34751 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34752 init
= error_mark_node
;
34754 cp_finish_decl (decl
, NULL_TREE
,
34755 /*init_const_expr_p=*/false,
34757 LOOKUP_ONLYCONVERTING
);
34761 pop_scope (pushed_scope
);
34767 /* If parsing a type specifier sequence failed, then
34768 this MUST be a simple expression. */
34769 cp_parser_parse_tentatively (parser
);
34770 decl
= cp_parser_primary_expression (parser
, false, false,
34772 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34773 if (!cp_parser_error_occurred (parser
)
34775 && (TREE_CODE (decl
) == COMPONENT_REF
34776 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34778 cp_parser_abort_tentative_parse (parser
);
34779 cp_parser_parse_tentatively (parser
);
34780 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34781 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34782 /*check_dependency_p=*/true,
34783 /*template_p=*/NULL
,
34784 /*declarator_p=*/false,
34785 /*optional_p=*/false);
34786 if (name
!= error_mark_node
34787 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34789 decl
= cp_parser_lookup_name_simple (parser
, name
,
34791 if (TREE_CODE (decl
) == FIELD_DECL
)
34792 add_private_clause
= omp_privatize_field (decl
, false);
34794 cp_parser_abort_tentative_parse (parser
);
34795 cp_parser_parse_tentatively (parser
);
34796 decl
= cp_parser_primary_expression (parser
, false, false,
34799 if (!cp_parser_error_occurred (parser
)
34802 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34806 cp_parser_parse_definitely (parser
);
34807 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34808 rhs
= cp_parser_assignment_expression (parser
);
34810 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34813 tf_warning_or_error
));
34814 if (!add_private_clause
)
34815 add_private_clause
= decl
;
34820 cp_parser_abort_tentative_parse (parser
);
34821 init
= cp_parser_expression (parser
);
34824 if (TREE_CODE (init
) == MODIFY_EXPR
34825 || TREE_CODE (init
) == MODOP_EXPR
)
34826 real_decl
= TREE_OPERAND (init
, 0);
34830 return add_private_clause
;
34833 /* Parse the restricted form of the for statement allowed by OpenMP. */
34836 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
34837 tree
*cclauses
, bool *if_p
)
34839 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
34840 tree real_decl
, initv
, condv
, incrv
, declv
;
34841 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
34842 location_t loc_first
;
34843 bool collapse_err
= false;
34844 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
34845 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
34846 auto_vec
<tree
, 4> orig_inits
;
34847 bool tiling
= false;
34849 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
34850 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
34851 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
34852 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
34855 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
34857 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
34858 && OMP_CLAUSE_ORDERED_EXPR (cl
))
34861 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
34864 if (ordered
&& ordered
< collapse
)
34866 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
34867 "%<ordered%> clause parameter is less than %<collapse%>");
34868 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
34869 = build_int_cst (NULL_TREE
, collapse
);
34870 ordered
= collapse
;
34874 for (tree
*pc
= &clauses
; *pc
; )
34875 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
34877 error_at (OMP_CLAUSE_LOCATION (*pc
),
34878 "%<linear%> clause may not be specified together "
34879 "with %<ordered%> clause with a parameter");
34880 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34883 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34886 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
34887 count
= ordered
? ordered
: collapse
;
34889 declv
= make_tree_vec (count
);
34890 initv
= make_tree_vec (count
);
34891 condv
= make_tree_vec (count
);
34892 incrv
= make_tree_vec (count
);
34894 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
34896 for (i
= 0; i
< count
; i
++)
34898 int bracecount
= 0;
34899 tree add_private_clause
= NULL_TREE
;
34902 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34905 cp_parser_error (parser
, "for statement expected");
34908 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
34910 matching_parens parens
;
34911 if (!parens
.require_open (parser
))
34914 init
= orig_init
= decl
= real_decl
= NULL
;
34915 this_pre_body
= push_stmt_list ();
34918 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
34919 init
, orig_init
, decl
, real_decl
);
34921 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34924 this_pre_body
= pop_stmt_list (this_pre_body
);
34928 pre_body
= push_stmt_list ();
34930 add_stmt (this_pre_body
);
34931 pre_body
= pop_stmt_list (pre_body
);
34934 pre_body
= this_pre_body
;
34939 if (cclauses
!= NULL
34940 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
34941 && real_decl
!= NULL_TREE
)
34944 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
34945 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
34946 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34948 error_at (loc
, "iteration variable %qD"
34949 " should not be firstprivate", real_decl
);
34950 *c
= OMP_CLAUSE_CHAIN (*c
);
34952 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
34953 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34955 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34957 *c
= OMP_CLAUSE_CHAIN (*c
);
34958 if (code
== OMP_SIMD
)
34960 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34961 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
34965 OMP_CLAUSE_CHAIN (l
) = clauses
;
34968 add_private_clause
= NULL_TREE
;
34972 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
34973 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34974 add_private_clause
= NULL_TREE
;
34975 c
= &OMP_CLAUSE_CHAIN (*c
);
34979 if (add_private_clause
)
34982 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
34984 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
34985 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
34986 && OMP_CLAUSE_DECL (c
) == decl
)
34988 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
34989 && OMP_CLAUSE_DECL (c
) == decl
)
34990 error_at (loc
, "iteration variable %qD "
34991 "should not be firstprivate",
34993 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
34994 && OMP_CLAUSE_DECL (c
) == decl
)
34995 error_at (loc
, "iteration variable %qD should not be reduction",
35000 if (code
!= OMP_SIMD
)
35001 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35002 else if (collapse
== 1)
35003 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35005 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35006 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35007 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35010 OMP_CLAUSE_CHAIN (c
) = clauses
;
35012 /* For linear, signal that we need to fill up
35013 the so far unknown linear step. */
35014 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35015 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35021 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35022 cond
= cp_parser_omp_for_cond (parser
, decl
);
35023 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35028 /* If decl is an iterator, preserve the operator on decl
35029 until finish_omp_for. */
35031 && ((processing_template_decl
35032 && (TREE_TYPE (real_decl
) == NULL_TREE
35033 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35034 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35035 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35037 incr
= cp_parser_expression (parser
);
35038 if (!EXPR_HAS_LOCATION (incr
))
35039 protected_set_expr_location (incr
, input_location
);
35042 if (!parens
.require_close (parser
))
35043 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35044 /*or_comma=*/false,
35045 /*consume_paren=*/true);
35047 TREE_VEC_ELT (declv
, i
) = decl
;
35048 TREE_VEC_ELT (initv
, i
) = init
;
35049 TREE_VEC_ELT (condv
, i
) = cond
;
35050 TREE_VEC_ELT (incrv
, i
) = incr
;
35053 orig_inits
.safe_grow_cleared (i
+ 1);
35054 orig_inits
[i
] = orig_init
;
35057 if (i
== count
- 1)
35060 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35061 in between the collapsed for loops to be still considered perfectly
35062 nested. Hopefully the final version clarifies this.
35063 For now handle (multiple) {'s and empty statements. */
35064 cp_parser_parse_tentatively (parser
);
35067 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35069 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35071 cp_lexer_consume_token (parser
->lexer
);
35074 else if (bracecount
35075 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35076 cp_lexer_consume_token (parser
->lexer
);
35079 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35080 error_at (loc
, "not enough for loops to collapse");
35081 collapse_err
= true;
35082 cp_parser_abort_tentative_parse (parser
);
35090 cp_parser_parse_definitely (parser
);
35091 nbraces
+= bracecount
;
35098 /* Note that we saved the original contents of this flag when we entered
35099 the structured block, and so we don't need to re-save it here. */
35100 parser
->in_statement
= IN_OMP_FOR
;
35102 /* Note that the grammar doesn't call for a structured block here,
35103 though the loop as a whole is a structured block. */
35104 body
= push_stmt_list ();
35105 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35106 body
= pop_stmt_list (body
);
35108 if (declv
== NULL_TREE
)
35111 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35112 body
, pre_body
, &orig_inits
, clauses
);
35116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35118 cp_lexer_consume_token (parser
->lexer
);
35121 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35122 cp_lexer_consume_token (parser
->lexer
);
35127 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35128 "collapsed loops not perfectly nested");
35130 collapse_err
= true;
35131 cp_parser_statement_seq_opt (parser
, NULL
);
35132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35137 while (!for_block
->is_empty ())
35139 tree t
= for_block
->pop ();
35140 if (TREE_CODE (t
) == STATEMENT_LIST
)
35141 add_stmt (pop_stmt_list (t
));
35145 release_tree_vector (for_block
);
35150 /* Helper function for OpenMP parsing, split clauses and call
35151 finish_omp_clauses on each of the set of clauses afterwards. */
35154 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35155 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35158 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35159 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35161 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35165 #pragma omp simd simd-clause[optseq] new-line
35168 #define OMP_SIMD_CLAUSE_MASK \
35169 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35179 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35180 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35183 tree clauses
, sb
, ret
;
35185 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35187 strcat (p_name
, " simd");
35188 mask
|= OMP_SIMD_CLAUSE_MASK
;
35190 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35194 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35195 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35196 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35197 OMP_CLAUSE_ORDERED
);
35198 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35200 error_at (OMP_CLAUSE_LOCATION (c
),
35201 "%<ordered%> clause with parameter may not be specified "
35202 "on %qs construct", p_name
);
35203 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35207 sb
= begin_omp_structured_block ();
35208 save
= cp_parser_begin_omp_structured_block (parser
);
35210 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35212 cp_parser_end_omp_structured_block (parser
, save
);
35213 add_stmt (finish_omp_structured_block (sb
));
35219 #pragma omp for for-clause[optseq] new-line
35223 #pragma omp for simd for-simd-clause[optseq] new-line
35226 #define OMP_FOR_CLAUSE_MASK \
35227 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35238 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35239 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35242 tree clauses
, sb
, ret
;
35244 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35246 strcat (p_name
, " for");
35247 mask
|= OMP_FOR_CLAUSE_MASK
;
35248 /* parallel for{, simd} disallows nowait clause, but for
35249 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35250 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35251 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35252 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35253 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35254 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35258 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35259 const char *p
= IDENTIFIER_POINTER (id
);
35261 if (strcmp (p
, "simd") == 0)
35263 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35264 if (cclauses
== NULL
)
35265 cclauses
= cclauses_buf
;
35267 cp_lexer_consume_token (parser
->lexer
);
35268 if (!flag_openmp
) /* flag_openmp_simd */
35269 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35271 sb
= begin_omp_structured_block ();
35272 save
= cp_parser_begin_omp_structured_block (parser
);
35273 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35275 cp_parser_end_omp_structured_block (parser
, save
);
35276 tree body
= finish_omp_structured_block (sb
);
35279 ret
= make_node (OMP_FOR
);
35280 TREE_TYPE (ret
) = void_type_node
;
35281 OMP_FOR_BODY (ret
) = body
;
35282 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35283 SET_EXPR_LOCATION (ret
, loc
);
35288 if (!flag_openmp
) /* flag_openmp_simd */
35290 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35294 /* Composite distribute parallel for disallows linear clause. */
35295 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35296 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35298 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35302 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35303 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35306 sb
= begin_omp_structured_block ();
35307 save
= cp_parser_begin_omp_structured_block (parser
);
35309 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35311 cp_parser_end_omp_structured_block (parser
, save
);
35312 add_stmt (finish_omp_structured_block (sb
));
35318 # pragma omp master new-line
35319 structured-block */
35322 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35324 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35325 return c_finish_omp_master (input_location
,
35326 cp_parser_omp_structured_block (parser
, if_p
));
35330 # pragma omp ordered new-line
35334 # pragma omp ordered ordered-clauses new-line
35335 structured-block */
35337 #define OMP_ORDERED_CLAUSE_MASK \
35338 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35341 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35342 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35345 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35346 enum pragma_context context
, bool *if_p
)
35348 location_t loc
= pragma_tok
->location
;
35350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35352 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35353 const char *p
= IDENTIFIER_POINTER (id
);
35355 if (strcmp (p
, "depend") == 0)
35357 if (!flag_openmp
) /* flag_openmp_simd */
35359 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35362 if (context
== pragma_stmt
)
35364 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35365 "%<depend%> clause may only be used in compound "
35367 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35371 = cp_parser_omp_all_clauses (parser
,
35372 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35373 "#pragma omp ordered", pragma_tok
);
35374 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35380 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35381 "#pragma omp ordered", pragma_tok
);
35383 if (!flag_openmp
/* flag_openmp_simd */
35384 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35387 c_finish_omp_ordered (loc
, clauses
,
35388 cp_parser_omp_structured_block (parser
, if_p
));
35395 { section-sequence }
35398 section-directive[opt] structured-block
35399 section-sequence section-directive structured-block */
35402 cp_parser_omp_sections_scope (cp_parser
*parser
)
35404 tree stmt
, substmt
;
35405 bool error_suppress
= false;
35408 matching_braces braces
;
35409 if (!braces
.require_open (parser
))
35412 stmt
= push_stmt_list ();
35414 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35415 != PRAGMA_OMP_SECTION
)
35417 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35418 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35419 add_stmt (substmt
);
35424 tok
= cp_lexer_peek_token (parser
->lexer
);
35425 if (tok
->type
== CPP_CLOSE_BRACE
)
35427 if (tok
->type
== CPP_EOF
)
35430 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35432 cp_lexer_consume_token (parser
->lexer
);
35433 cp_parser_require_pragma_eol (parser
, tok
);
35434 error_suppress
= false;
35436 else if (!error_suppress
)
35438 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35439 error_suppress
= true;
35442 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35443 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35444 add_stmt (substmt
);
35446 braces
.require_close (parser
);
35448 substmt
= pop_stmt_list (stmt
);
35450 stmt
= make_node (OMP_SECTIONS
);
35451 TREE_TYPE (stmt
) = void_type_node
;
35452 OMP_SECTIONS_BODY (stmt
) = substmt
;
35459 # pragma omp sections sections-clause[optseq] newline
35462 #define OMP_SECTIONS_CLAUSE_MASK \
35463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35470 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35471 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35474 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35476 strcat (p_name
, " sections");
35477 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35479 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35481 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35485 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35486 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35489 ret
= cp_parser_omp_sections_scope (parser
);
35491 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35497 # pragma omp parallel parallel-clause[optseq] new-line
35499 # pragma omp parallel for parallel-for-clause[optseq] new-line
35501 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35505 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35506 structured-block */
35508 #define OMP_PARALLEL_CLAUSE_MASK \
35509 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35520 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35521 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35524 tree stmt
, clauses
, block
;
35526 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35528 strcat (p_name
, " parallel");
35529 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35530 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35531 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35532 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35533 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35535 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35537 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35538 if (cclauses
== NULL
)
35539 cclauses
= cclauses_buf
;
35541 cp_lexer_consume_token (parser
->lexer
);
35542 if (!flag_openmp
) /* flag_openmp_simd */
35543 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35545 block
= begin_omp_parallel ();
35546 save
= cp_parser_begin_omp_structured_block (parser
);
35547 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35549 cp_parser_end_omp_structured_block (parser
, save
);
35550 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35552 if (ret
== NULL_TREE
)
35554 OMP_PARALLEL_COMBINED (stmt
) = 1;
35557 /* When combined with distribute, parallel has to be followed by for.
35558 #pragma omp target parallel is allowed though. */
35560 && (mask
& (OMP_CLAUSE_MASK_1
35561 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35563 error_at (loc
, "expected %<for%> after %qs", p_name
);
35564 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35567 else if (!flag_openmp
) /* flag_openmp_simd */
35569 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35572 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35574 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35575 const char *p
= IDENTIFIER_POINTER (id
);
35576 if (strcmp (p
, "sections") == 0)
35578 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35579 cclauses
= cclauses_buf
;
35581 cp_lexer_consume_token (parser
->lexer
);
35582 block
= begin_omp_parallel ();
35583 save
= cp_parser_begin_omp_structured_block (parser
);
35584 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35585 cp_parser_end_omp_structured_block (parser
, save
);
35586 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35588 OMP_PARALLEL_COMBINED (stmt
) = 1;
35593 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35597 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35598 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35601 block
= begin_omp_parallel ();
35602 save
= cp_parser_begin_omp_structured_block (parser
);
35603 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35604 cp_parser_end_omp_structured_block (parser
, save
);
35605 stmt
= finish_omp_parallel (clauses
, block
);
35610 # pragma omp single single-clause[optseq] new-line
35611 structured-block */
35613 #define OMP_SINGLE_CLAUSE_MASK \
35614 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35620 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35622 tree stmt
= make_node (OMP_SINGLE
);
35623 TREE_TYPE (stmt
) = void_type_node
;
35625 OMP_SINGLE_CLAUSES (stmt
)
35626 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35627 "#pragma omp single", pragma_tok
);
35628 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35630 return add_stmt (stmt
);
35634 # pragma omp task task-clause[optseq] new-line
35635 structured-block */
35637 #define OMP_TASK_CLAUSE_MASK \
35638 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35650 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35652 tree clauses
, block
;
35655 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35656 "#pragma omp task", pragma_tok
);
35657 block
= begin_omp_task ();
35658 save
= cp_parser_begin_omp_structured_block (parser
);
35659 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35660 cp_parser_end_omp_structured_block (parser
, save
);
35661 return finish_omp_task (clauses
, block
);
35665 # pragma omp taskwait new-line */
35668 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35670 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35671 finish_omp_taskwait ();
35675 # pragma omp taskyield new-line */
35678 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35680 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35681 finish_omp_taskyield ();
35685 # pragma omp taskgroup new-line
35686 structured-block */
35689 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35691 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35692 return c_finish_omp_taskgroup (input_location
,
35693 cp_parser_omp_structured_block (parser
,
35699 # pragma omp threadprivate (variable-list) */
35702 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35706 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35707 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35709 finish_omp_threadprivate (vars
);
35713 # pragma omp cancel cancel-clause[optseq] new-line */
35715 #define OMP_CANCEL_CLAUSE_MASK \
35716 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35723 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35725 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35726 "#pragma omp cancel", pragma_tok
);
35727 finish_omp_cancel (clauses
);
35731 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35733 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35740 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35741 enum pragma_context context
)
35744 bool point_seen
= false;
35746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35748 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35749 const char *p
= IDENTIFIER_POINTER (id
);
35751 if (strcmp (p
, "point") == 0)
35753 cp_lexer_consume_token (parser
->lexer
);
35759 cp_parser_error (parser
, "expected %<point%>");
35760 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35764 if (context
!= pragma_compound
)
35766 if (context
== pragma_stmt
)
35767 error_at (pragma_tok
->location
,
35768 "%<#pragma %s%> may only be used in compound statements",
35769 "omp cancellation point");
35771 cp_parser_error (parser
, "expected declaration specifiers");
35772 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35776 clauses
= cp_parser_omp_all_clauses (parser
,
35777 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35778 "#pragma omp cancellation point",
35780 finish_omp_cancellation_point (clauses
);
35784 #pragma omp distribute distribute-clause[optseq] new-line
35787 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35788 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35795 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35796 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35799 tree clauses
, sb
, ret
;
35801 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35803 strcat (p_name
, " distribute");
35804 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
35806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35808 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35809 const char *p
= IDENTIFIER_POINTER (id
);
35811 bool parallel
= false;
35813 if (strcmp (p
, "simd") == 0)
35816 parallel
= strcmp (p
, "parallel") == 0;
35817 if (parallel
|| simd
)
35819 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35820 if (cclauses
== NULL
)
35821 cclauses
= cclauses_buf
;
35822 cp_lexer_consume_token (parser
->lexer
);
35823 if (!flag_openmp
) /* flag_openmp_simd */
35826 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35829 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35832 sb
= begin_omp_structured_block ();
35833 save
= cp_parser_begin_omp_structured_block (parser
);
35835 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35838 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35840 cp_parser_end_omp_structured_block (parser
, save
);
35841 tree body
= finish_omp_structured_block (sb
);
35844 ret
= make_node (OMP_DISTRIBUTE
);
35845 TREE_TYPE (ret
) = void_type_node
;
35846 OMP_FOR_BODY (ret
) = body
;
35847 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35848 SET_EXPR_LOCATION (ret
, loc
);
35853 if (!flag_openmp
) /* flag_openmp_simd */
35855 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35859 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35863 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
35864 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35867 sb
= begin_omp_structured_block ();
35868 save
= cp_parser_begin_omp_structured_block (parser
);
35870 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
35872 cp_parser_end_omp_structured_block (parser
, save
);
35873 add_stmt (finish_omp_structured_block (sb
));
35879 # pragma omp teams teams-clause[optseq] new-line
35880 structured-block */
35882 #define OMP_TEAMS_CLAUSE_MASK \
35883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35892 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
35893 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35896 tree clauses
, sb
, ret
;
35898 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35900 strcat (p_name
, " teams");
35901 mask
|= OMP_TEAMS_CLAUSE_MASK
;
35903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35905 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35906 const char *p
= IDENTIFIER_POINTER (id
);
35907 if (strcmp (p
, "distribute") == 0)
35909 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35910 if (cclauses
== NULL
)
35911 cclauses
= cclauses_buf
;
35913 cp_lexer_consume_token (parser
->lexer
);
35914 if (!flag_openmp
) /* flag_openmp_simd */
35915 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35917 sb
= begin_omp_structured_block ();
35918 save
= cp_parser_begin_omp_structured_block (parser
);
35919 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35921 cp_parser_end_omp_structured_block (parser
, save
);
35922 tree body
= finish_omp_structured_block (sb
);
35925 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35926 ret
= make_node (OMP_TEAMS
);
35927 TREE_TYPE (ret
) = void_type_node
;
35928 OMP_TEAMS_CLAUSES (ret
) = clauses
;
35929 OMP_TEAMS_BODY (ret
) = body
;
35930 OMP_TEAMS_COMBINED (ret
) = 1;
35931 SET_EXPR_LOCATION (ret
, loc
);
35932 return add_stmt (ret
);
35935 if (!flag_openmp
) /* flag_openmp_simd */
35937 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35941 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35945 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
35946 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35949 tree stmt
= make_node (OMP_TEAMS
);
35950 TREE_TYPE (stmt
) = void_type_node
;
35951 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
35952 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35953 SET_EXPR_LOCATION (stmt
, loc
);
35955 return add_stmt (stmt
);
35959 # pragma omp target data target-data-clause[optseq] new-line
35960 structured-block */
35962 #define OMP_TARGET_DATA_CLAUSE_MASK \
35963 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35969 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35972 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
35973 "#pragma omp target data", pragma_tok
);
35975 for (tree
*pc
= &clauses
; *pc
;)
35977 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35978 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35981 case GOMP_MAP_ALWAYS_TO
:
35982 case GOMP_MAP_FROM
:
35983 case GOMP_MAP_ALWAYS_FROM
:
35984 case GOMP_MAP_TOFROM
:
35985 case GOMP_MAP_ALWAYS_TOFROM
:
35986 case GOMP_MAP_ALLOC
:
35989 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35990 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35991 case GOMP_MAP_ALWAYS_POINTER
:
35995 error_at (OMP_CLAUSE_LOCATION (*pc
),
35996 "%<#pragma omp target data%> with map-type other "
35997 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35998 "on %<map%> clause");
35999 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36002 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36008 error_at (pragma_tok
->location
,
36009 "%<#pragma omp target data%> must contain at least "
36010 "one %<map%> clause");
36014 tree stmt
= make_node (OMP_TARGET_DATA
);
36015 TREE_TYPE (stmt
) = void_type_node
;
36016 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36018 keep_next_level (true);
36019 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36021 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36022 return add_stmt (stmt
);
36026 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36027 structured-block */
36029 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36030 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36037 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36038 enum pragma_context context
)
36040 bool data_seen
= false;
36041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36043 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36044 const char *p
= IDENTIFIER_POINTER (id
);
36046 if (strcmp (p
, "data") == 0)
36048 cp_lexer_consume_token (parser
->lexer
);
36054 cp_parser_error (parser
, "expected %<data%>");
36055 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36059 if (context
== pragma_stmt
)
36061 error_at (pragma_tok
->location
,
36062 "%<#pragma %s%> may only be used in compound statements",
36063 "omp target enter data");
36064 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36069 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36070 "#pragma omp target enter data", pragma_tok
);
36072 for (tree
*pc
= &clauses
; *pc
;)
36074 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36075 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36078 case GOMP_MAP_ALWAYS_TO
:
36079 case GOMP_MAP_ALLOC
:
36082 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36083 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36084 case GOMP_MAP_ALWAYS_POINTER
:
36088 error_at (OMP_CLAUSE_LOCATION (*pc
),
36089 "%<#pragma omp target enter data%> with map-type other "
36090 "than %<to%> or %<alloc%> on %<map%> clause");
36091 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36094 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36100 error_at (pragma_tok
->location
,
36101 "%<#pragma omp target enter data%> must contain at least "
36102 "one %<map%> clause");
36106 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36107 TREE_TYPE (stmt
) = void_type_node
;
36108 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36109 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36110 return add_stmt (stmt
);
36114 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36115 structured-block */
36117 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36125 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36126 enum pragma_context context
)
36128 bool data_seen
= false;
36129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36131 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36132 const char *p
= IDENTIFIER_POINTER (id
);
36134 if (strcmp (p
, "data") == 0)
36136 cp_lexer_consume_token (parser
->lexer
);
36142 cp_parser_error (parser
, "expected %<data%>");
36143 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36147 if (context
== pragma_stmt
)
36149 error_at (pragma_tok
->location
,
36150 "%<#pragma %s%> may only be used in compound statements",
36151 "omp target exit data");
36152 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36157 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36158 "#pragma omp target exit data", pragma_tok
);
36160 for (tree
*pc
= &clauses
; *pc
;)
36162 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36163 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36165 case GOMP_MAP_FROM
:
36166 case GOMP_MAP_ALWAYS_FROM
:
36167 case GOMP_MAP_RELEASE
:
36168 case GOMP_MAP_DELETE
:
36171 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36172 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36173 case GOMP_MAP_ALWAYS_POINTER
:
36177 error_at (OMP_CLAUSE_LOCATION (*pc
),
36178 "%<#pragma omp target exit data%> with map-type other "
36179 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36181 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36184 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36190 error_at (pragma_tok
->location
,
36191 "%<#pragma omp target exit data%> must contain at least "
36192 "one %<map%> clause");
36196 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36197 TREE_TYPE (stmt
) = void_type_node
;
36198 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36199 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36200 return add_stmt (stmt
);
36204 # pragma omp target update target-update-clause[optseq] new-line */
36206 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36215 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36216 enum pragma_context context
)
36218 if (context
== pragma_stmt
)
36220 error_at (pragma_tok
->location
,
36221 "%<#pragma %s%> may only be used in compound statements",
36222 "omp target update");
36223 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36228 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36229 "#pragma omp target update", pragma_tok
);
36230 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36231 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36233 error_at (pragma_tok
->location
,
36234 "%<#pragma omp target update%> must contain at least one "
36235 "%<from%> or %<to%> clauses");
36239 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36240 TREE_TYPE (stmt
) = void_type_node
;
36241 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36242 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36248 # pragma omp target target-clause[optseq] new-line
36249 structured-block */
36251 #define OMP_TARGET_CLAUSE_MASK \
36252 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36263 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36264 enum pragma_context context
, bool *if_p
)
36266 tree
*pc
= NULL
, stmt
;
36268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36270 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36271 const char *p
= IDENTIFIER_POINTER (id
);
36272 enum tree_code ccode
= ERROR_MARK
;
36274 if (strcmp (p
, "teams") == 0)
36276 else if (strcmp (p
, "parallel") == 0)
36277 ccode
= OMP_PARALLEL
;
36278 else if (strcmp (p
, "simd") == 0)
36280 if (ccode
!= ERROR_MARK
)
36282 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36283 char p_name
[sizeof ("#pragma omp target teams distribute "
36284 "parallel for simd")];
36286 cp_lexer_consume_token (parser
->lexer
);
36287 strcpy (p_name
, "#pragma omp target");
36288 if (!flag_openmp
) /* flag_openmp_simd */
36294 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36295 OMP_TARGET_CLAUSE_MASK
,
36299 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36300 OMP_TARGET_CLAUSE_MASK
,
36304 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36305 OMP_TARGET_CLAUSE_MASK
,
36309 gcc_unreachable ();
36311 return stmt
!= NULL_TREE
;
36313 keep_next_level (true);
36314 tree sb
= begin_omp_structured_block (), ret
;
36315 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36319 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36320 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36324 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36325 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36329 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36330 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36334 gcc_unreachable ();
36336 cp_parser_end_omp_structured_block (parser
, save
);
36337 tree body
= finish_omp_structured_block (sb
);
36338 if (ret
== NULL_TREE
)
36340 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36342 /* For combined target teams, ensure the num_teams and
36343 thread_limit clause expressions are evaluated on the host,
36344 before entering the target construct. */
36346 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36347 c
; c
= OMP_CLAUSE_CHAIN (c
))
36348 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36349 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36350 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36352 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36353 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36354 if (expr
== error_mark_node
)
36356 tree tmp
= TARGET_EXPR_SLOT (expr
);
36358 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36359 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36360 OMP_CLAUSE_FIRSTPRIVATE
);
36361 OMP_CLAUSE_DECL (tc
) = tmp
;
36362 OMP_CLAUSE_CHAIN (tc
)
36363 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36364 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36367 tree stmt
= make_node (OMP_TARGET
);
36368 TREE_TYPE (stmt
) = void_type_node
;
36369 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36370 OMP_TARGET_BODY (stmt
) = body
;
36371 OMP_TARGET_COMBINED (stmt
) = 1;
36372 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36374 pc
= &OMP_TARGET_CLAUSES (stmt
);
36375 goto check_clauses
;
36377 else if (!flag_openmp
) /* flag_openmp_simd */
36379 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36382 else if (strcmp (p
, "data") == 0)
36384 cp_lexer_consume_token (parser
->lexer
);
36385 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36388 else if (strcmp (p
, "enter") == 0)
36390 cp_lexer_consume_token (parser
->lexer
);
36391 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36394 else if (strcmp (p
, "exit") == 0)
36396 cp_lexer_consume_token (parser
->lexer
);
36397 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36400 else if (strcmp (p
, "update") == 0)
36402 cp_lexer_consume_token (parser
->lexer
);
36403 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36406 if (!flag_openmp
) /* flag_openmp_simd */
36408 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36412 stmt
= make_node (OMP_TARGET
);
36413 TREE_TYPE (stmt
) = void_type_node
;
36415 OMP_TARGET_CLAUSES (stmt
)
36416 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36417 "#pragma omp target", pragma_tok
);
36418 pc
= &OMP_TARGET_CLAUSES (stmt
);
36419 keep_next_level (true);
36420 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36422 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36428 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36429 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36432 case GOMP_MAP_ALWAYS_TO
:
36433 case GOMP_MAP_FROM
:
36434 case GOMP_MAP_ALWAYS_FROM
:
36435 case GOMP_MAP_TOFROM
:
36436 case GOMP_MAP_ALWAYS_TOFROM
:
36437 case GOMP_MAP_ALLOC
:
36438 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36439 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36440 case GOMP_MAP_ALWAYS_POINTER
:
36443 error_at (OMP_CLAUSE_LOCATION (*pc
),
36444 "%<#pragma omp target%> with map-type other "
36445 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36446 "on %<map%> clause");
36447 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36450 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36456 # pragma acc cache (variable-list) new-line
36460 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36462 tree stmt
, clauses
;
36464 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36465 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36467 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36469 stmt
= make_node (OACC_CACHE
);
36470 TREE_TYPE (stmt
) = void_type_node
;
36471 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36472 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36479 # pragma acc data oacc-data-clause[optseq] new-line
36480 structured-block */
36482 #define OACC_DATA_CLAUSE_MASK \
36483 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36496 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36498 tree stmt
, clauses
, block
;
36501 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36502 "#pragma acc data", pragma_tok
);
36504 block
= begin_omp_parallel ();
36505 save
= cp_parser_begin_omp_structured_block (parser
);
36506 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36507 cp_parser_end_omp_structured_block (parser
, save
);
36508 stmt
= finish_oacc_data (clauses
, block
);
36513 # pragma acc host_data <clauses> new-line
36514 structured-block */
36516 #define OACC_HOST_DATA_CLAUSE_MASK \
36517 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36520 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36522 tree stmt
, clauses
, block
;
36525 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36526 "#pragma acc host_data", pragma_tok
);
36528 block
= begin_omp_parallel ();
36529 save
= cp_parser_begin_omp_structured_block (parser
);
36530 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36531 cp_parser_end_omp_structured_block (parser
, save
);
36532 stmt
= finish_oacc_host_data (clauses
, block
);
36537 # pragma acc declare oacc-data-clause[optseq] new-line
36540 #define OACC_DECLARE_CLAUSE_MASK \
36541 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36555 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36557 tree clauses
, stmt
;
36558 bool error
= false;
36560 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36561 "#pragma acc declare", pragma_tok
, true);
36564 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36566 error_at (pragma_tok
->location
,
36567 "no valid clauses specified in %<#pragma acc declare%>");
36571 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36573 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36574 tree decl
= OMP_CLAUSE_DECL (t
);
36575 if (!DECL_P (decl
))
36577 error_at (loc
, "array section in %<#pragma acc declare%>");
36581 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36582 switch (OMP_CLAUSE_MAP_KIND (t
))
36584 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36585 case GOMP_MAP_FORCE_ALLOC
:
36586 case GOMP_MAP_FORCE_TO
:
36587 case GOMP_MAP_FORCE_DEVICEPTR
:
36588 case GOMP_MAP_DEVICE_RESIDENT
:
36591 case GOMP_MAP_LINK
:
36592 if (!global_bindings_p ()
36593 && (TREE_STATIC (decl
)
36594 || !DECL_EXTERNAL (decl
)))
36597 "%qD must be a global variable in "
36598 "%<#pragma acc declare link%>",
36606 if (global_bindings_p ())
36608 error_at (loc
, "invalid OpenACC clause at file scope");
36612 if (DECL_EXTERNAL (decl
))
36615 "invalid use of %<extern%> variable %qD "
36616 "in %<#pragma acc declare%>", decl
);
36620 else if (TREE_PUBLIC (decl
))
36623 "invalid use of %<global%> variable %qD "
36624 "in %<#pragma acc declare%>", decl
);
36631 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36632 || lookup_attribute ("omp declare target link",
36633 DECL_ATTRIBUTES (decl
)))
36635 error_at (loc
, "variable %qD used more than once with "
36636 "%<#pragma acc declare%>", decl
);
36645 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36646 id
= get_identifier ("omp declare target link");
36648 id
= get_identifier ("omp declare target");
36650 DECL_ATTRIBUTES (decl
)
36651 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36652 if (global_bindings_p ())
36654 symtab_node
*node
= symtab_node::get (decl
);
36657 node
->offloadable
= 1;
36658 if (ENABLE_OFFLOADING
)
36660 g
->have_offload
= true;
36661 if (is_a
<varpool_node
*> (node
))
36662 vec_safe_push (offload_vars
, decl
);
36669 if (error
|| global_bindings_p ())
36672 stmt
= make_node (OACC_DECLARE
);
36673 TREE_TYPE (stmt
) = void_type_node
;
36674 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36675 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36683 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36687 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36689 LOC is the location of the #pragma token.
36692 #define OACC_ENTER_DATA_CLAUSE_MASK \
36693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36701 #define OACC_EXIT_DATA_CLAUSE_MASK \
36702 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36709 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36712 location_t loc
= pragma_tok
->location
;
36713 tree stmt
, clauses
;
36714 const char *p
= "";
36716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36717 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36719 if (strcmp (p
, "data") != 0)
36721 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36722 enter
? "enter" : "exit");
36723 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36727 cp_lexer_consume_token (parser
->lexer
);
36730 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36731 "#pragma acc enter data", pragma_tok
);
36733 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36734 "#pragma acc exit data", pragma_tok
);
36736 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36738 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36739 enter
? "enter" : "exit");
36743 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36744 TREE_TYPE (stmt
) = void_type_node
;
36745 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36746 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36752 # pragma acc loop oacc-loop-clause[optseq] new-line
36753 structured-block */
36755 #define OACC_LOOP_CLAUSE_MASK \
36756 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36768 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36769 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36771 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36773 strcat (p_name
, " loop");
36774 mask
|= OACC_LOOP_CLAUSE_MASK
;
36776 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36780 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36782 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36784 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36787 tree block
= begin_omp_structured_block ();
36788 int save
= cp_parser_begin_omp_structured_block (parser
);
36789 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36790 cp_parser_end_omp_structured_block (parser
, save
);
36791 add_stmt (finish_omp_structured_block (block
));
36797 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36802 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36806 #define OACC_KERNELS_CLAUSE_MASK \
36807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36825 #define OACC_PARALLEL_CLAUSE_MASK \
36826 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36848 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
36849 char *p_name
, bool *if_p
)
36851 omp_clause_mask mask
;
36852 enum tree_code code
;
36853 switch (cp_parser_pragma_kind (pragma_tok
))
36855 case PRAGMA_OACC_KERNELS
:
36856 strcat (p_name
, " kernels");
36857 mask
= OACC_KERNELS_CLAUSE_MASK
;
36858 code
= OACC_KERNELS
;
36860 case PRAGMA_OACC_PARALLEL
:
36861 strcat (p_name
, " parallel");
36862 mask
= OACC_PARALLEL_CLAUSE_MASK
;
36863 code
= OACC_PARALLEL
;
36866 gcc_unreachable ();
36869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36872 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36873 if (strcmp (p
, "loop") == 0)
36875 cp_lexer_consume_token (parser
->lexer
);
36876 tree block
= begin_omp_parallel ();
36878 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
36880 return finish_omp_construct (code
, block
, clauses
);
36884 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
36886 tree block
= begin_omp_parallel ();
36887 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
36888 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36889 cp_parser_end_omp_structured_block (parser
, save
);
36890 return finish_omp_construct (code
, block
, clauses
);
36894 # pragma acc update oacc-update-clause[optseq] new-line
36897 #define OACC_UPDATE_CLAUSE_MASK \
36898 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36906 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
36908 tree stmt
, clauses
;
36910 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
36911 "#pragma acc update", pragma_tok
);
36913 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36915 error_at (pragma_tok
->location
,
36916 "%<#pragma acc update%> must contain at least one "
36917 "%<device%> or %<host%> or %<self%> clause");
36921 stmt
= make_node (OACC_UPDATE
);
36922 TREE_TYPE (stmt
) = void_type_node
;
36923 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
36924 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36930 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36932 LOC is the location of the #pragma token.
36935 #define OACC_WAIT_CLAUSE_MASK \
36936 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36939 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
36941 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
36942 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36944 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36945 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
36947 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
36948 "#pragma acc wait", pragma_tok
);
36950 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
36951 stmt
= finish_expr_stmt (stmt
);
36957 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36959 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36960 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36968 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36969 enum pragma_context context
)
36971 bool first_p
= parser
->omp_declare_simd
== NULL
;
36972 cp_omp_declare_simd_data data
;
36975 data
.error_seen
= false;
36976 data
.fndecl_seen
= false;
36977 data
.tokens
= vNULL
;
36978 data
.clauses
= NULL_TREE
;
36979 /* It is safe to take the address of a local variable; it will only be
36980 used while this scope is live. */
36981 parser
->omp_declare_simd
= &data
;
36984 /* Store away all pragma tokens. */
36985 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36986 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36987 cp_lexer_consume_token (parser
->lexer
);
36988 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36989 parser
->omp_declare_simd
->error_seen
= true;
36990 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36991 struct cp_token_cache
*cp
36992 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
36993 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
36997 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
36998 cp_parser_pragma (parser
, context
, NULL
);
37001 case pragma_external
:
37002 cp_parser_declaration (parser
);
37004 case pragma_member
:
37005 cp_parser_member_declaration (parser
);
37007 case pragma_objc_icode
:
37008 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37011 cp_parser_declaration_statement (parser
);
37014 if (parser
->omp_declare_simd
37015 && !parser
->omp_declare_simd
->error_seen
37016 && !parser
->omp_declare_simd
->fndecl_seen
)
37017 error_at (pragma_tok
->location
,
37018 "%<#pragma omp declare simd%> not immediately followed by "
37019 "function declaration or definition");
37020 data
.tokens
.release ();
37021 parser
->omp_declare_simd
= NULL
;
37025 /* Finalize #pragma omp declare simd clauses after direct declarator has
37026 been parsed, and put that into "omp declare simd" attribute. */
37029 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37031 struct cp_token_cache
*ce
;
37032 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37035 if (!data
->error_seen
&& data
->fndecl_seen
)
37037 error ("%<#pragma omp declare simd%> not immediately followed by "
37038 "a single function declaration or definition");
37039 data
->error_seen
= true;
37041 if (data
->error_seen
)
37044 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37048 cp_parser_push_lexer_for_tokens (parser
, ce
);
37049 parser
->lexer
->in_pragma
= true;
37050 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37051 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37052 cp_lexer_consume_token (parser
->lexer
);
37053 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37054 "#pragma omp declare simd", pragma_tok
);
37055 cp_parser_pop_lexer (parser
);
37057 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37058 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37059 TREE_CHAIN (c
) = attrs
;
37060 if (processing_template_decl
)
37061 ATTR_IS_DEPENDENT (c
) = 1;
37065 data
->fndecl_seen
= true;
37071 # pragma omp declare target new-line
37072 declarations and definitions
37073 # pragma omp end declare target new-line
37076 # pragma omp declare target ( extended-list ) new-line
37078 # pragma omp declare target declare-target-clauses[seq] new-line */
37080 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37081 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37085 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37087 tree clauses
= NULL_TREE
;
37088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37090 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37091 "#pragma omp declare target", pragma_tok
);
37092 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37094 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37096 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37097 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37101 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37102 scope_chain
->omp_declare_target_attribute
++;
37105 if (scope_chain
->omp_declare_target_attribute
)
37106 error_at (pragma_tok
->location
,
37107 "%<#pragma omp declare target%> with clauses in between "
37108 "%<#pragma omp declare target%> without clauses and "
37109 "%<#pragma omp end declare target%>");
37110 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37112 tree t
= OMP_CLAUSE_DECL (c
), id
;
37113 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37114 tree at2
= lookup_attribute ("omp declare target link",
37115 DECL_ATTRIBUTES (t
));
37116 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37118 id
= get_identifier ("omp declare target link");
37119 std::swap (at1
, at2
);
37122 id
= get_identifier ("omp declare target");
37125 error_at (OMP_CLAUSE_LOCATION (c
),
37126 "%qD specified both in declare target %<link%> and %<to%>"
37132 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37133 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37136 symtab_node
*node
= symtab_node::get (t
);
37139 node
->offloadable
= 1;
37140 if (ENABLE_OFFLOADING
)
37142 g
->have_offload
= true;
37143 if (is_a
<varpool_node
*> (node
))
37144 vec_safe_push (offload_vars
, t
);
37152 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37154 const char *p
= "";
37155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37157 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37158 p
= IDENTIFIER_POINTER (id
);
37160 if (strcmp (p
, "declare") == 0)
37162 cp_lexer_consume_token (parser
->lexer
);
37164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37166 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37167 p
= IDENTIFIER_POINTER (id
);
37169 if (strcmp (p
, "target") == 0)
37170 cp_lexer_consume_token (parser
->lexer
);
37173 cp_parser_error (parser
, "expected %<target%>");
37174 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37180 cp_parser_error (parser
, "expected %<declare%>");
37181 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37184 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37185 if (!scope_chain
->omp_declare_target_attribute
)
37186 error_at (pragma_tok
->location
,
37187 "%<#pragma omp end declare target%> without corresponding "
37188 "%<#pragma omp declare target%>");
37190 scope_chain
->omp_declare_target_attribute
--;
37193 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37194 expression and optional initializer clause of
37195 #pragma omp declare reduction. We store the expression(s) as
37196 either 3, 6 or 7 special statements inside of the artificial function's
37197 body. The first two statements are DECL_EXPRs for the artificial
37198 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37199 expression that uses those variables.
37200 If there was any INITIALIZER clause, this is followed by further statements,
37201 the fourth and fifth statements are DECL_EXPRs for the artificial
37202 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37203 constructor variant (first token after open paren is not omp_priv),
37204 then the sixth statement is a statement with the function call expression
37205 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37206 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37207 to initialize the OMP_PRIV artificial variable and there is seventh
37208 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37211 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37213 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37214 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37215 type
= TREE_TYPE (type
);
37216 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37217 DECL_ARTIFICIAL (omp_out
) = 1;
37218 pushdecl (omp_out
);
37219 add_decl_expr (omp_out
);
37220 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37221 DECL_ARTIFICIAL (omp_in
) = 1;
37223 add_decl_expr (omp_in
);
37225 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37227 keep_next_level (true);
37228 tree block
= begin_omp_structured_block ();
37229 combiner
= cp_parser_expression (parser
);
37230 finish_expr_stmt (combiner
);
37231 block
= finish_omp_structured_block (block
);
37234 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37237 const char *p
= "";
37238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37240 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37241 p
= IDENTIFIER_POINTER (id
);
37244 if (strcmp (p
, "initializer") == 0)
37246 cp_lexer_consume_token (parser
->lexer
);
37247 matching_parens parens
;
37248 if (!parens
.require_open (parser
))
37252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37254 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37255 p
= IDENTIFIER_POINTER (id
);
37258 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37259 DECL_ARTIFICIAL (omp_priv
) = 1;
37260 pushdecl (omp_priv
);
37261 add_decl_expr (omp_priv
);
37262 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37263 DECL_ARTIFICIAL (omp_orig
) = 1;
37264 pushdecl (omp_orig
);
37265 add_decl_expr (omp_orig
);
37267 keep_next_level (true);
37268 block
= begin_omp_structured_block ();
37271 if (strcmp (p
, "omp_priv") == 0)
37273 bool is_direct_init
, is_non_constant_init
;
37275 cp_lexer_consume_token (parser
->lexer
);
37276 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37278 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37279 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37281 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37282 == CPP_CLOSE_PAREN
))
37284 finish_omp_structured_block (block
);
37285 error ("invalid initializer clause");
37288 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37289 &is_non_constant_init
);
37290 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37291 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37295 cp_parser_parse_tentatively (parser
);
37296 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37297 /*check_dependency_p=*/true,
37298 /*template_p=*/NULL
,
37299 /*declarator_p=*/false,
37300 /*optional_p=*/false);
37301 vec
<tree
, va_gc
> *args
;
37302 if (fn_name
== error_mark_node
37303 || cp_parser_error_occurred (parser
)
37304 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37305 || ((args
= cp_parser_parenthesized_expression_list
37306 (parser
, non_attr
, /*cast_p=*/false,
37307 /*allow_expansion_p=*/true,
37308 /*non_constant_p=*/NULL
)),
37309 cp_parser_error_occurred (parser
)))
37311 finish_omp_structured_block (block
);
37312 cp_parser_abort_tentative_parse (parser
);
37313 cp_parser_error (parser
, "expected id-expression (arguments)");
37318 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37319 if (arg
== omp_priv
37320 || (TREE_CODE (arg
) == ADDR_EXPR
37321 && TREE_OPERAND (arg
, 0) == omp_priv
))
37323 cp_parser_abort_tentative_parse (parser
);
37324 if (arg
== NULL_TREE
)
37325 error ("one of the initializer call arguments should be %<omp_priv%>"
37326 " or %<&omp_priv%>");
37327 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37329 finish_expr_stmt (initializer
);
37332 block
= finish_omp_structured_block (block
);
37333 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37337 add_decl_expr (omp_orig
);
37339 if (!parens
.require_close (parser
))
37343 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37344 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37351 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37352 initializer-clause[opt] new-line
37354 initializer-clause:
37355 initializer (omp_priv initializer)
37356 initializer (function-name (argument-list)) */
37359 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37360 enum pragma_context
)
37362 auto_vec
<tree
> types
;
37363 enum tree_code reduc_code
= ERROR_MARK
;
37364 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37366 cp_token
*first_token
;
37367 cp_token_cache
*cp
;
37371 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37372 p
= obstack_alloc (&declarator_obstack
, 0);
37374 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37377 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37380 reduc_code
= PLUS_EXPR
;
37383 reduc_code
= MULT_EXPR
;
37386 reduc_code
= MINUS_EXPR
;
37389 reduc_code
= BIT_AND_EXPR
;
37392 reduc_code
= BIT_XOR_EXPR
;
37395 reduc_code
= BIT_IOR_EXPR
;
37398 reduc_code
= TRUTH_ANDIF_EXPR
;
37401 reduc_code
= TRUTH_ORIF_EXPR
;
37404 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37407 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37408 "%<|%>, %<&&%>, %<||%> or identifier");
37412 if (reduc_code
!= ERROR_MARK
)
37413 cp_lexer_consume_token (parser
->lexer
);
37415 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37416 if (reduc_id
== error_mark_node
)
37419 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37422 /* Types may not be defined in declare reduction type list. */
37423 const char *saved_message
;
37424 saved_message
= parser
->type_definition_forbidden_message
;
37425 parser
->type_definition_forbidden_message
37426 = G_("types may not be defined in declare reduction type list");
37427 bool saved_colon_corrects_to_scope_p
;
37428 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37429 parser
->colon_corrects_to_scope_p
= false;
37430 bool saved_colon_doesnt_start_class_def_p
;
37431 saved_colon_doesnt_start_class_def_p
37432 = parser
->colon_doesnt_start_class_def_p
;
37433 parser
->colon_doesnt_start_class_def_p
= true;
37437 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37438 type
= cp_parser_type_id (parser
);
37439 if (type
== error_mark_node
)
37441 else if (ARITHMETIC_TYPE_P (type
)
37442 && (orig_reduc_id
== NULL_TREE
37443 || (TREE_CODE (type
) != COMPLEX_TYPE
37444 && (id_equal (orig_reduc_id
, "min")
37445 || id_equal (orig_reduc_id
, "max")))))
37446 error_at (loc
, "predeclared arithmetic type %qT in "
37447 "%<#pragma omp declare reduction%>", type
);
37448 else if (TREE_CODE (type
) == FUNCTION_TYPE
37449 || TREE_CODE (type
) == METHOD_TYPE
37450 || TREE_CODE (type
) == ARRAY_TYPE
)
37451 error_at (loc
, "function or array type %qT in "
37452 "%<#pragma omp declare reduction%>", type
);
37453 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37454 error_at (loc
, "reference type %qT in "
37455 "%<#pragma omp declare reduction%>", type
);
37456 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37457 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37458 "%<#pragma omp declare reduction%>", type
);
37460 types
.safe_push (type
);
37462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37463 cp_lexer_consume_token (parser
->lexer
);
37468 /* Restore the saved message. */
37469 parser
->type_definition_forbidden_message
= saved_message
;
37470 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37471 parser
->colon_doesnt_start_class_def_p
37472 = saved_colon_doesnt_start_class_def_p
;
37474 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37475 || types
.is_empty ())
37478 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37482 first_token
= cp_lexer_peek_token (parser
->lexer
);
37485 FOR_EACH_VEC_ELT (types
, i
, type
)
37488 = build_function_type_list (void_type_node
,
37489 cp_build_reference_type (type
, false),
37491 tree this_reduc_id
= reduc_id
;
37492 if (!dependent_type_p (type
))
37493 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37494 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37495 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37496 DECL_ARTIFICIAL (fndecl
) = 1;
37497 DECL_EXTERNAL (fndecl
) = 1;
37498 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37499 DECL_IGNORED_P (fndecl
) = 1;
37500 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37501 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37502 DECL_ATTRIBUTES (fndecl
)
37503 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37504 DECL_ATTRIBUTES (fndecl
));
37505 if (processing_template_decl
)
37506 fndecl
= push_template_decl (fndecl
);
37507 bool block_scope
= false;
37508 tree block
= NULL_TREE
;
37509 if (current_function_decl
)
37511 block_scope
= true;
37512 DECL_CONTEXT (fndecl
) = global_namespace
;
37513 if (!processing_template_decl
)
37516 else if (current_class_type
)
37520 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37521 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37522 cp_lexer_consume_token (parser
->lexer
);
37523 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37525 cp
= cp_token_cache_new (first_token
,
37526 cp_lexer_peek_nth_token (parser
->lexer
,
37529 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37530 finish_member_declaration (fndecl
);
37531 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37532 DECL_PENDING_INLINE_P (fndecl
) = 1;
37533 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37538 DECL_CONTEXT (fndecl
) = current_namespace
;
37542 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37544 block
= begin_omp_structured_block ();
37547 cp_parser_push_lexer_for_tokens (parser
, cp
);
37548 parser
->lexer
->in_pragma
= true;
37550 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37553 finish_function (/*inline_p=*/false);
37555 DECL_CONTEXT (fndecl
) = current_function_decl
;
37557 cp_parser_pop_lexer (parser
);
37561 cp_parser_pop_lexer (parser
);
37563 finish_function (/*inline_p=*/false);
37566 DECL_CONTEXT (fndecl
) = current_function_decl
;
37567 block
= finish_omp_structured_block (block
);
37568 if (TREE_CODE (block
) == BIND_EXPR
)
37569 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37570 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37571 DECL_SAVED_TREE (fndecl
) = block
;
37572 if (processing_template_decl
)
37573 add_decl_expr (fndecl
);
37575 cp_check_omp_declare_reduction (fndecl
);
37576 if (cp
== NULL
&& types
.length () > 1)
37577 cp
= cp_token_cache_new (first_token
,
37578 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37579 if (errs
!= errorcount
)
37583 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37586 /* Free any declarators allocated. */
37587 obstack_free (&declarator_obstack
, p
);
37591 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37592 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37593 initializer-clause[opt] new-line
37594 #pragma omp declare target new-line */
37597 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37598 enum pragma_context context
)
37600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37602 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37603 const char *p
= IDENTIFIER_POINTER (id
);
37605 if (strcmp (p
, "simd") == 0)
37607 cp_lexer_consume_token (parser
->lexer
);
37608 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37612 cp_ensure_no_omp_declare_simd (parser
);
37613 if (strcmp (p
, "reduction") == 0)
37615 cp_lexer_consume_token (parser
->lexer
);
37616 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37620 if (!flag_openmp
) /* flag_openmp_simd */
37622 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37625 if (strcmp (p
, "target") == 0)
37627 cp_lexer_consume_token (parser
->lexer
);
37628 cp_parser_omp_declare_target (parser
, pragma_tok
);
37632 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37634 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37639 #pragma omp taskloop taskloop-clause[optseq] new-line
37642 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37645 #define OMP_TASKLOOP_CLAUSE_MASK \
37646 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37662 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37663 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37666 tree clauses
, sb
, ret
;
37668 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37670 strcat (p_name
, " taskloop");
37671 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37675 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37676 const char *p
= IDENTIFIER_POINTER (id
);
37678 if (strcmp (p
, "simd") == 0)
37680 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37681 if (cclauses
== NULL
)
37682 cclauses
= cclauses_buf
;
37684 cp_lexer_consume_token (parser
->lexer
);
37685 if (!flag_openmp
) /* flag_openmp_simd */
37686 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37688 sb
= begin_omp_structured_block ();
37689 save
= cp_parser_begin_omp_structured_block (parser
);
37690 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37692 cp_parser_end_omp_structured_block (parser
, save
);
37693 tree body
= finish_omp_structured_block (sb
);
37696 ret
= make_node (OMP_TASKLOOP
);
37697 TREE_TYPE (ret
) = void_type_node
;
37698 OMP_FOR_BODY (ret
) = body
;
37699 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37700 SET_EXPR_LOCATION (ret
, loc
);
37705 if (!flag_openmp
) /* flag_openmp_simd */
37707 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37711 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37715 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37716 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37719 sb
= begin_omp_structured_block ();
37720 save
= cp_parser_begin_omp_structured_block (parser
);
37722 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37725 cp_parser_end_omp_structured_block (parser
, save
);
37726 add_stmt (finish_omp_structured_block (sb
));
37733 # pragma acc routine oacc-routine-clause[optseq] new-line
37734 function-definition
37736 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37739 #define OACC_ROUTINE_CLAUSE_MASK \
37740 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37746 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37747 component, which must resolve to a declared namespace-scope
37748 function. The clauses are either processed directly (for a named
37749 function), or defered until the immediatley following declaration
37753 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37754 enum pragma_context context
)
37756 gcc_checking_assert (context
== pragma_external
);
37757 /* The checking for "another pragma following this one" in the "no optional
37758 '( name )'" case makes sure that we dont re-enter. */
37759 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37761 cp_oacc_routine_data data
;
37762 data
.error_seen
= false;
37763 data
.fndecl_seen
= false;
37764 data
.tokens
= vNULL
;
37765 data
.clauses
= NULL_TREE
;
37766 data
.loc
= pragma_tok
->location
;
37767 /* It is safe to take the address of a local variable; it will only be
37768 used while this scope is live. */
37769 parser
->oacc_routine
= &data
;
37771 /* Look for optional '( name )'. */
37772 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37774 matching_parens parens
;
37775 parens
.consume_open (parser
); /* '(' */
37777 /* We parse the name as an id-expression. If it resolves to
37778 anything other than a non-overloaded function at namespace
37779 scope, it's an error. */
37780 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37781 tree name
= cp_parser_id_expression (parser
,
37782 /*template_keyword_p=*/false,
37783 /*check_dependency_p=*/false,
37784 /*template_p=*/NULL
,
37785 /*declarator_p=*/false,
37786 /*optional_p=*/false);
37787 tree decl
= cp_parser_lookup_name_simple (parser
, name
, name_loc
);
37788 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37789 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37791 if (decl
== error_mark_node
37792 || !parens
.require_close (parser
))
37794 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37795 parser
->oacc_routine
= NULL
;
37800 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37801 "#pragma acc routine",
37802 cp_lexer_peek_token (parser
->lexer
));
37804 if (decl
&& is_overloaded_fn (decl
)
37805 && (TREE_CODE (decl
) != FUNCTION_DECL
37806 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37808 error_at (name_loc
,
37809 "%<#pragma acc routine%> names a set of overloads");
37810 parser
->oacc_routine
= NULL
;
37814 /* Perhaps we should use the same rule as declarations in different
37816 if (!DECL_NAMESPACE_SCOPE_P (decl
))
37818 error_at (name_loc
,
37819 "%qD does not refer to a namespace scope function", decl
);
37820 parser
->oacc_routine
= NULL
;
37824 if (TREE_CODE (decl
) != FUNCTION_DECL
)
37826 error_at (name_loc
, "%qD does not refer to a function", decl
);
37827 parser
->oacc_routine
= NULL
;
37831 cp_finalize_oacc_routine (parser
, decl
, false);
37832 parser
->oacc_routine
= NULL
;
37834 else /* No optional '( name )'. */
37836 /* Store away all pragma tokens. */
37837 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37838 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37839 cp_lexer_consume_token (parser
->lexer
);
37840 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37841 parser
->oacc_routine
->error_seen
= true;
37842 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37843 struct cp_token_cache
*cp
37844 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37845 parser
->oacc_routine
->tokens
.safe_push (cp
);
37847 /* Emit a helpful diagnostic if there's another pragma following this
37849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37851 cp_ensure_no_oacc_routine (parser
);
37852 data
.tokens
.release ();
37853 /* ..., and then just keep going. */
37857 /* We only have to consider the pragma_external case here. */
37858 cp_parser_declaration (parser
);
37859 if (parser
->oacc_routine
37860 && !parser
->oacc_routine
->fndecl_seen
)
37861 cp_ensure_no_oacc_routine (parser
);
37863 parser
->oacc_routine
= NULL
;
37864 data
.tokens
.release ();
37868 /* Finalize #pragma acc routine clauses after direct declarator has
37872 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
37874 struct cp_token_cache
*ce
;
37875 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
37877 if (!data
->error_seen
&& data
->fndecl_seen
)
37879 error_at (data
->loc
,
37880 "%<#pragma acc routine%> not immediately followed by "
37881 "a single function declaration or definition");
37882 data
->error_seen
= true;
37884 if (data
->error_seen
)
37887 gcc_checking_assert (data
->tokens
.length () == 1);
37888 ce
= data
->tokens
[0];
37890 cp_parser_push_lexer_for_tokens (parser
, ce
);
37891 parser
->lexer
->in_pragma
= true;
37892 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37894 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37895 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
37896 parser
->oacc_routine
->clauses
37897 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37898 "#pragma acc routine", pragma_tok
);
37899 cp_parser_pop_lexer (parser
);
37900 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37906 /* Apply any saved OpenACC routine clauses to a just-parsed
37910 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
37912 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
37914 /* Keep going if we're in error reporting mode. */
37915 if (parser
->oacc_routine
->error_seen
37916 || fndecl
== error_mark_node
)
37919 if (parser
->oacc_routine
->fndecl_seen
)
37921 error_at (parser
->oacc_routine
->loc
,
37922 "%<#pragma acc routine%> not immediately followed by"
37923 " a single function declaration or definition");
37924 parser
->oacc_routine
= NULL
;
37927 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
37929 cp_ensure_no_oacc_routine (parser
);
37933 if (oacc_get_fn_attrib (fndecl
))
37935 error_at (parser
->oacc_routine
->loc
,
37936 "%<#pragma acc routine%> already applied to %qD", fndecl
);
37937 parser
->oacc_routine
= NULL
;
37941 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
37943 error_at (parser
->oacc_routine
->loc
,
37945 ? G_("%<#pragma acc routine%> must be applied before use")
37946 : G_("%<#pragma acc routine%> must be applied before "
37948 parser
->oacc_routine
= NULL
;
37952 /* Process the routine's dimension clauses. */
37953 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
37954 oacc_replace_fn_attrib (fndecl
, dims
);
37956 /* Add an "omp declare target" attribute. */
37957 DECL_ATTRIBUTES (fndecl
)
37958 = tree_cons (get_identifier ("omp declare target"),
37959 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
37961 /* Don't unset parser->oacc_routine here: we may still need it to
37962 diagnose wrong usage. But, remember that we've used this "#pragma acc
37964 parser
->oacc_routine
->fndecl_seen
= true;
37968 /* Main entry point to OpenMP statement pragmas. */
37971 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37974 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
37975 omp_clause_mask
mask (0);
37977 switch (cp_parser_pragma_kind (pragma_tok
))
37979 case PRAGMA_OACC_ATOMIC
:
37980 cp_parser_omp_atomic (parser
, pragma_tok
);
37982 case PRAGMA_OACC_CACHE
:
37983 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
37985 case PRAGMA_OACC_DATA
:
37986 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
37988 case PRAGMA_OACC_ENTER_DATA
:
37989 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
37991 case PRAGMA_OACC_EXIT_DATA
:
37992 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
37994 case PRAGMA_OACC_HOST_DATA
:
37995 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
37997 case PRAGMA_OACC_KERNELS
:
37998 case PRAGMA_OACC_PARALLEL
:
37999 strcpy (p_name
, "#pragma acc");
38000 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38003 case PRAGMA_OACC_LOOP
:
38004 strcpy (p_name
, "#pragma acc");
38005 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38008 case PRAGMA_OACC_UPDATE
:
38009 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38011 case PRAGMA_OACC_WAIT
:
38012 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38014 case PRAGMA_OMP_ATOMIC
:
38015 cp_parser_omp_atomic (parser
, pragma_tok
);
38017 case PRAGMA_OMP_CRITICAL
:
38018 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38020 case PRAGMA_OMP_DISTRIBUTE
:
38021 strcpy (p_name
, "#pragma omp");
38022 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38025 case PRAGMA_OMP_FOR
:
38026 strcpy (p_name
, "#pragma omp");
38027 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38030 case PRAGMA_OMP_MASTER
:
38031 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38033 case PRAGMA_OMP_PARALLEL
:
38034 strcpy (p_name
, "#pragma omp");
38035 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38038 case PRAGMA_OMP_SECTIONS
:
38039 strcpy (p_name
, "#pragma omp");
38040 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38042 case PRAGMA_OMP_SIMD
:
38043 strcpy (p_name
, "#pragma omp");
38044 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38047 case PRAGMA_OMP_SINGLE
:
38048 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38050 case PRAGMA_OMP_TASK
:
38051 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38053 case PRAGMA_OMP_TASKGROUP
:
38054 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38056 case PRAGMA_OMP_TASKLOOP
:
38057 strcpy (p_name
, "#pragma omp");
38058 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38061 case PRAGMA_OMP_TEAMS
:
38062 strcpy (p_name
, "#pragma omp");
38063 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38067 gcc_unreachable ();
38070 protected_set_expr_location (stmt
, pragma_tok
->location
);
38073 /* Transactional Memory parsing routines. */
38075 /* Parse a transaction attribute.
38081 We use this instead of cp_parser_attributes_opt for transactions to avoid
38082 the pedwarn in C++98 mode. */
38085 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38088 tree attr_name
, attr
= NULL
;
38090 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38091 return cp_parser_attributes_opt (parser
);
38093 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38095 cp_lexer_consume_token (parser
->lexer
);
38096 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38099 token
= cp_lexer_peek_token (parser
->lexer
);
38100 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38102 token
= cp_lexer_consume_token (parser
->lexer
);
38104 attr_name
= (token
->type
== CPP_KEYWORD
38105 /* For keywords, use the canonical spelling,
38106 not the parsed identifier. */
38107 ? ridpointers
[(int) token
->keyword
]
38109 attr
= build_tree_list (attr_name
, NULL_TREE
);
38112 cp_parser_error (parser
, "expected identifier");
38114 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38116 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38120 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38122 transaction-statement:
38123 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38125 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38129 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38131 unsigned char old_in
= parser
->in_transaction
;
38132 unsigned char this_in
= 1, new_in
;
38133 enum rid keyword
= token
->keyword
;
38134 tree stmt
, attrs
, noex
;
38136 cp_lexer_consume_token (parser
->lexer
);
38138 if (keyword
== RID_TRANSACTION_RELAXED
38139 || keyword
== RID_SYNCHRONIZED
)
38140 this_in
|= TM_STMT_ATTR_RELAXED
;
38143 attrs
= cp_parser_txn_attribute_opt (parser
);
38145 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38148 /* Parse a noexcept specification. */
38149 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38150 noex
= boolean_true_node
;
38151 else if (keyword
== RID_ATOMIC_CANCEL
)
38153 /* cancel-and-throw is unimplemented. */
38154 sorry ("atomic_cancel");
38158 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38160 /* Keep track if we're in the lexical scope of an outer transaction. */
38161 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38163 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38165 parser
->in_transaction
= new_in
;
38166 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38167 parser
->in_transaction
= old_in
;
38169 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38174 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38176 transaction-expression:
38177 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38178 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38182 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38184 unsigned char old_in
= parser
->in_transaction
;
38185 unsigned char this_in
= 1;
38189 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38191 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38192 || keyword
== RID_TRANSACTION_RELAXED
);
38196 keyword
== RID_TRANSACTION_RELAXED
38197 ? G_("%<__transaction_relaxed%> without transactional memory "
38199 : G_("%<__transaction_atomic%> without transactional memory "
38200 "support enabled"));
38202 token
= cp_parser_require_keyword (parser
, keyword
,
38203 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38204 : RT_TRANSACTION_RELAXED
));
38205 gcc_assert (token
!= NULL
);
38207 if (keyword
== RID_TRANSACTION_RELAXED
)
38208 this_in
|= TM_STMT_ATTR_RELAXED
;
38210 /* Set this early. This might mean that we allow transaction_cancel in
38211 an expression that we find out later actually has to be a constexpr.
38212 However, we expect that cxx_constant_value will be able to deal with
38213 this; also, if the noexcept has no constexpr, then what we parse next
38214 really is a transaction's body. */
38215 parser
->in_transaction
= this_in
;
38217 /* Parse a noexcept specification. */
38218 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38221 if (!noex
|| !noex_expr
38222 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38224 matching_parens parens
;
38225 parens
.require_open (parser
);
38227 expr
= cp_parser_expression (parser
);
38228 expr
= finish_parenthesized_expr (expr
);
38230 parens
.require_close (parser
);
38234 /* The only expression that is available got parsed for the noexcept
38235 already. noexcept is true then. */
38237 noex
= boolean_true_node
;
38240 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38241 parser
->in_transaction
= old_in
;
38243 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38244 return error_mark_node
;
38246 return (flag_tm
? expr
: error_mark_node
);
38249 /* Parse a function-transaction-block.
38251 function-transaction-block:
38252 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38254 __transaction_atomic txn-attribute[opt] function-try-block
38255 __transaction_relaxed ctor-initializer[opt] function-body
38256 __transaction_relaxed function-try-block
38260 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38262 unsigned char old_in
= parser
->in_transaction
;
38263 unsigned char new_in
= 1;
38264 tree compound_stmt
, stmt
, attrs
;
38267 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38268 || keyword
== RID_TRANSACTION_RELAXED
);
38269 token
= cp_parser_require_keyword (parser
, keyword
,
38270 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38271 : RT_TRANSACTION_RELAXED
));
38272 gcc_assert (token
!= NULL
);
38274 if (keyword
== RID_TRANSACTION_RELAXED
)
38275 new_in
|= TM_STMT_ATTR_RELAXED
;
38278 attrs
= cp_parser_txn_attribute_opt (parser
);
38280 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38283 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38285 parser
->in_transaction
= new_in
;
38287 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38288 cp_parser_function_try_block (parser
);
38290 cp_parser_ctor_initializer_opt_and_function_body
38291 (parser
, /*in_function_try_block=*/false);
38293 parser
->in_transaction
= old_in
;
38295 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38298 /* Parse a __transaction_cancel statement.
38301 __transaction_cancel txn-attribute[opt] ;
38302 __transaction_cancel txn-attribute[opt] throw-expression ;
38304 ??? Cancel and throw is not yet implemented. */
38307 cp_parser_transaction_cancel (cp_parser
*parser
)
38310 bool is_outer
= false;
38313 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38314 RT_TRANSACTION_CANCEL
);
38315 gcc_assert (token
!= NULL
);
38317 attrs
= cp_parser_txn_attribute_opt (parser
);
38319 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38321 /* ??? Parse cancel-and-throw here. */
38323 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38327 error_at (token
->location
, "%<__transaction_cancel%> without "
38328 "transactional memory support enabled");
38329 return error_mark_node
;
38331 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38333 error_at (token
->location
, "%<__transaction_cancel%> within a "
38334 "%<__transaction_relaxed%>");
38335 return error_mark_node
;
38339 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38340 && !is_tm_may_cancel_outer (current_function_decl
))
38342 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38343 "within outer %<__transaction_atomic%>");
38344 error_at (token
->location
,
38345 " or a %<transaction_may_cancel_outer%> function");
38346 return error_mark_node
;
38349 else if (parser
->in_transaction
== 0)
38351 error_at (token
->location
, "%<__transaction_cancel%> not within "
38352 "%<__transaction_atomic%>");
38353 return error_mark_node
;
38356 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38364 static GTY (()) cp_parser
*the_parser
;
38367 /* Special handling for the first token or line in the file. The first
38368 thing in the file might be #pragma GCC pch_preprocess, which loads a
38369 PCH file, which is a GC collection point. So we need to handle this
38370 first pragma without benefit of an existing lexer structure.
38372 Always returns one token to the caller in *FIRST_TOKEN. This is
38373 either the true first token of the file, or the first token after
38374 the initial pragma. */
38377 cp_parser_initial_pragma (cp_token
*first_token
)
38381 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38382 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38385 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38386 if (first_token
->type
== CPP_STRING
)
38388 name
= first_token
->u
.value
;
38390 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38391 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38392 error_at (first_token
->location
,
38393 "junk at end of %<#pragma GCC pch_preprocess%>");
38396 error_at (first_token
->location
, "expected string literal");
38398 /* Skip to the end of the pragma. */
38399 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38400 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38402 /* Now actually load the PCH file. */
38404 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38406 /* Read one more token to return to our caller. We have to do this
38407 after reading the PCH file in, since its pointers have to be
38409 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38412 /* Parse a pragma GCC ivdep. */
38415 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38417 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38421 /* Parse a pragma GCC unroll. */
38423 static unsigned short
38424 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38426 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38427 tree expr
= cp_parser_constant_expression (parser
);
38428 unsigned short unroll
;
38429 expr
= maybe_constant_value (expr
);
38430 HOST_WIDE_INT lunroll
= 0;
38431 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38432 || TREE_CODE (expr
) != INTEGER_CST
38433 || (lunroll
= tree_to_shwi (expr
)) < 0
38434 || lunroll
>= USHRT_MAX
)
38436 error_at (location
, "%<#pragma GCC unroll%> requires an"
38437 " assignment-expression that evaluates to a non-negative"
38438 " integral constant less than %u", USHRT_MAX
);
38443 unroll
= (unsigned short)lunroll
;
38447 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38451 /* Normal parsing of a pragma token. Here we can (and must) use the
38455 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38457 cp_token
*pragma_tok
;
38462 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38463 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38464 parser
->lexer
->in_pragma
= true;
38466 id
= cp_parser_pragma_kind (pragma_tok
);
38467 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38468 cp_ensure_no_omp_declare_simd (parser
);
38471 case PRAGMA_GCC_PCH_PREPROCESS
:
38472 error_at (pragma_tok
->location
,
38473 "%<#pragma GCC pch_preprocess%> must be first");
38476 case PRAGMA_OMP_BARRIER
:
38479 case pragma_compound
:
38480 cp_parser_omp_barrier (parser
, pragma_tok
);
38483 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38484 "used in compound statements", "omp barrier");
38491 case PRAGMA_OMP_FLUSH
:
38494 case pragma_compound
:
38495 cp_parser_omp_flush (parser
, pragma_tok
);
38498 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38499 "used in compound statements", "omp flush");
38506 case PRAGMA_OMP_TASKWAIT
:
38509 case pragma_compound
:
38510 cp_parser_omp_taskwait (parser
, pragma_tok
);
38513 error_at (pragma_tok
->location
,
38514 "%<#pragma %s%> may only be used in compound statements",
38522 case PRAGMA_OMP_TASKYIELD
:
38525 case pragma_compound
:
38526 cp_parser_omp_taskyield (parser
, pragma_tok
);
38529 error_at (pragma_tok
->location
,
38530 "%<#pragma %s%> may only be used in compound statements",
38538 case PRAGMA_OMP_CANCEL
:
38541 case pragma_compound
:
38542 cp_parser_omp_cancel (parser
, pragma_tok
);
38545 error_at (pragma_tok
->location
,
38546 "%<#pragma %s%> may only be used in compound statements",
38554 case PRAGMA_OMP_CANCELLATION_POINT
:
38555 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38558 case PRAGMA_OMP_THREADPRIVATE
:
38559 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38562 case PRAGMA_OMP_DECLARE
:
38563 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38565 case PRAGMA_OACC_DECLARE
:
38566 cp_parser_oacc_declare (parser
, pragma_tok
);
38569 case PRAGMA_OACC_ENTER_DATA
:
38570 if (context
== pragma_stmt
)
38572 error_at (pragma_tok
->location
,
38573 "%<#pragma %s%> may only be used in compound statements",
38577 else if (context
!= pragma_compound
)
38579 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38582 case PRAGMA_OACC_EXIT_DATA
:
38583 if (context
== pragma_stmt
)
38585 error_at (pragma_tok
->location
,
38586 "%<#pragma %s%> may only be used in compound statements",
38590 else if (context
!= pragma_compound
)
38592 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38595 case PRAGMA_OACC_ROUTINE
:
38596 if (context
!= pragma_external
)
38598 error_at (pragma_tok
->location
,
38599 "%<#pragma acc routine%> must be at file scope");
38602 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38605 case PRAGMA_OACC_UPDATE
:
38606 if (context
== pragma_stmt
)
38608 error_at (pragma_tok
->location
,
38609 "%<#pragma %s%> may only be used in compound statements",
38613 else if (context
!= pragma_compound
)
38615 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38618 case PRAGMA_OACC_WAIT
:
38619 if (context
== pragma_stmt
)
38621 error_at (pragma_tok
->location
,
38622 "%<#pragma %s%> may only be used in compound statements",
38626 else if (context
!= pragma_compound
)
38628 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38631 case PRAGMA_OACC_ATOMIC
:
38632 case PRAGMA_OACC_CACHE
:
38633 case PRAGMA_OACC_DATA
:
38634 case PRAGMA_OACC_HOST_DATA
:
38635 case PRAGMA_OACC_KERNELS
:
38636 case PRAGMA_OACC_PARALLEL
:
38637 case PRAGMA_OACC_LOOP
:
38638 case PRAGMA_OMP_ATOMIC
:
38639 case PRAGMA_OMP_CRITICAL
:
38640 case PRAGMA_OMP_DISTRIBUTE
:
38641 case PRAGMA_OMP_FOR
:
38642 case PRAGMA_OMP_MASTER
:
38643 case PRAGMA_OMP_PARALLEL
:
38644 case PRAGMA_OMP_SECTIONS
:
38645 case PRAGMA_OMP_SIMD
:
38646 case PRAGMA_OMP_SINGLE
:
38647 case PRAGMA_OMP_TASK
:
38648 case PRAGMA_OMP_TASKGROUP
:
38649 case PRAGMA_OMP_TASKLOOP
:
38650 case PRAGMA_OMP_TEAMS
:
38651 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38653 stmt
= push_omp_privatization_clauses (false);
38654 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38655 pop_omp_privatization_clauses (stmt
);
38658 case PRAGMA_OMP_ORDERED
:
38659 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38661 stmt
= push_omp_privatization_clauses (false);
38662 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38663 pop_omp_privatization_clauses (stmt
);
38666 case PRAGMA_OMP_TARGET
:
38667 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38669 stmt
= push_omp_privatization_clauses (false);
38670 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38671 pop_omp_privatization_clauses (stmt
);
38674 case PRAGMA_OMP_END_DECLARE_TARGET
:
38675 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38678 case PRAGMA_OMP_SECTION
:
38679 error_at (pragma_tok
->location
,
38680 "%<#pragma omp section%> may only be used in "
38681 "%<#pragma omp sections%> construct");
38686 if (context
== pragma_external
)
38688 error_at (pragma_tok
->location
,
38689 "%<#pragma GCC ivdep%> must be inside a function");
38692 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38693 unsigned short unroll
;
38694 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38695 if (tok
->type
== CPP_PRAGMA
38696 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38698 tok
= cp_lexer_consume_token (parser
->lexer
);
38699 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38700 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38704 if (tok
->type
!= CPP_KEYWORD
38705 || (tok
->keyword
!= RID_FOR
38706 && tok
->keyword
!= RID_WHILE
38707 && tok
->keyword
!= RID_DO
))
38709 cp_parser_error (parser
, "for, while or do statement expected");
38712 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38716 case PRAGMA_UNROLL
:
38718 if (context
== pragma_external
)
38720 error_at (pragma_tok
->location
,
38721 "%<#pragma GCC unroll%> must be inside a function");
38724 const unsigned short unroll
38725 = cp_parser_pragma_unroll (parser
, pragma_tok
);
38727 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38728 if (tok
->type
== CPP_PRAGMA
38729 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
38731 tok
= cp_lexer_consume_token (parser
->lexer
);
38732 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
38733 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38737 if (tok
->type
!= CPP_KEYWORD
38738 || (tok
->keyword
!= RID_FOR
38739 && tok
->keyword
!= RID_WHILE
38740 && tok
->keyword
!= RID_DO
))
38742 cp_parser_error (parser
, "for, while or do statement expected");
38745 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38750 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38751 c_invoke_pragma_handler (id
);
38755 cp_parser_error (parser
, "expected declaration specifiers");
38759 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38763 /* The interface the pragma parsers have to the lexer. */
38766 pragma_lex (tree
*value
, location_t
*loc
)
38768 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38769 enum cpp_ttype ret
= tok
->type
;
38771 *value
= tok
->u
.value
;
38773 *loc
= tok
->location
;
38775 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38777 else if (ret
== CPP_STRING
)
38778 *value
= cp_parser_string_literal (the_parser
, false, false);
38781 if (ret
== CPP_KEYWORD
)
38783 cp_lexer_consume_token (the_parser
->lexer
);
38790 /* External interface. */
38792 /* Parse one entire translation unit. */
38795 c_parse_file (void)
38797 static bool already_called
= false;
38799 if (already_called
)
38800 fatal_error (input_location
,
38801 "inter-module optimizations not implemented for C++");
38802 already_called
= true;
38804 the_parser
= cp_parser_new ();
38805 push_deferring_access_checks (flag_access_control
38806 ? dk_no_deferred
: dk_no_check
);
38807 cp_parser_translation_unit (the_parser
);
38811 /* Create an identifier for a generic parameter type (a synthesized
38812 template parameter implied by `auto' or a concept identifier). */
38814 static GTY(()) int generic_parm_count
;
38816 make_generic_type_name ()
38819 sprintf (buf
, "auto:%d", ++generic_parm_count
);
38820 return get_identifier (buf
);
38823 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38824 (creating a new template parameter list if necessary). Returns the newly
38825 created template type parm. */
38828 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
38830 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
38832 /* Before committing to modifying any scope, if we're in an
38833 implicit template scope, and we're trying to synthesize a
38834 constrained parameter, try to find a previous parameter with
38835 the same name. This is the same-type rule for abbreviated
38836 function templates.
38838 NOTE: We can generate implicit parameters when tentatively
38839 parsing a nested name specifier, only to reject that parse
38840 later. However, matching the same template-id as part of a
38841 direct-declarator should generate an identical template
38842 parameter, so this rule will merge them. */
38843 if (parser
->implicit_template_scope
&& constr
)
38845 tree t
= parser
->implicit_template_parms
;
38848 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
38850 tree d
= TREE_VALUE (t
);
38851 if (TREE_CODE (d
) == PARM_DECL
)
38852 /* Return the TEMPLATE_PARM_INDEX. */
38853 d
= DECL_INITIAL (d
);
38856 t
= TREE_CHAIN (t
);
38860 /* We are either continuing a function template that already contains implicit
38861 template parameters, creating a new fully-implicit function template, or
38862 extending an existing explicit function template with implicit template
38865 cp_binding_level
*const entry_scope
= current_binding_level
;
38867 bool become_template
= false;
38868 cp_binding_level
*parent_scope
= 0;
38870 if (parser
->implicit_template_scope
)
38872 gcc_assert (parser
->implicit_template_parms
);
38874 current_binding_level
= parser
->implicit_template_scope
;
38878 /* Roll back to the existing template parameter scope (in the case of
38879 extending an explicit function template) or introduce a new template
38880 parameter scope ahead of the function parameter scope (or class scope
38881 in the case of out-of-line member definitions). The function scope is
38882 added back after template parameter synthesis below. */
38884 cp_binding_level
*scope
= entry_scope
;
38886 while (scope
->kind
== sk_function_parms
)
38888 parent_scope
= scope
;
38889 scope
= scope
->level_chain
;
38891 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
38893 /* If not defining a class, then any class scope is a scope level in
38894 an out-of-line member definition. In this case simply wind back
38895 beyond the first such scope to inject the template parameter list.
38896 Otherwise wind back to the class being defined. The latter can
38897 occur in class member friend declarations such as:
38903 friend void A::foo (auto);
38906 The template parameter list synthesized for the friend declaration
38907 must be injected in the scope of 'B'. This can also occur in
38908 erroneous cases such as:
38914 void B::foo (auto) {}
38917 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38918 but, nevertheless, the template parameter list synthesized for the
38919 declarator should be injected into the scope of 'A' as if the
38920 ill-formed template was specified explicitly. */
38922 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
38924 parent_scope
= scope
;
38925 scope
= scope
->level_chain
;
38929 current_binding_level
= scope
;
38931 if (scope
->kind
!= sk_template_parms
38932 || !function_being_declared_is_template_p (parser
))
38934 /* Introduce a new template parameter list for implicit template
38937 become_template
= true;
38939 parser
->implicit_template_scope
38940 = begin_scope (sk_template_parms
, NULL
);
38942 ++processing_template_decl
;
38944 parser
->fully_implicit_function_template_p
= true;
38945 ++parser
->num_template_parameter_lists
;
38949 /* Synthesize implicit template parameters at the end of the explicit
38950 template parameter list. */
38952 gcc_assert (current_template_parms
);
38954 parser
->implicit_template_scope
= scope
;
38956 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38957 parser
->implicit_template_parms
38958 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
38962 /* Synthesize a new template parameter and track the current template
38963 parameter chain with implicit_template_parms. */
38965 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
38966 tree synth_id
= make_generic_type_name ();
38967 tree synth_tmpl_parm
;
38968 bool non_type
= false;
38970 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
38972 = finish_template_type_parm (class_type_node
, synth_id
);
38973 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
38975 = finish_constrained_template_template_parm (proto
, synth_id
);
38978 synth_tmpl_parm
= copy_decl (proto
);
38979 DECL_NAME (synth_tmpl_parm
) = synth_id
;
38983 // Attach the constraint to the parm before processing.
38984 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
38985 TREE_TYPE (node
) = constr
;
38987 = process_template_parm (parser
->implicit_template_parms
,
38990 /*non_type=*/non_type
,
38991 /*param_pack=*/false);
38993 // Chain the new parameter to the list of implicit parameters.
38994 if (parser
->implicit_template_parms
)
38995 parser
->implicit_template_parms
38996 = TREE_CHAIN (parser
->implicit_template_parms
);
38998 parser
->implicit_template_parms
= new_parm
;
39000 tree new_decl
= get_local_decls ();
39002 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39003 new_decl
= DECL_INITIAL (new_decl
);
39005 /* If creating a fully implicit function template, start the new implicit
39006 template parameter list with this synthesized type, otherwise grow the
39007 current template parameter list. */
39009 if (become_template
)
39011 parent_scope
->level_chain
= current_binding_level
;
39013 tree new_parms
= make_tree_vec (1);
39014 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39015 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39016 new_parms
, current_template_parms
);
39020 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39021 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39022 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39023 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39026 // If the new parameter was constrained, we need to add that to the
39027 // constraints in the template parameter list.
39028 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39030 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39031 reqs
= conjoin_constraints (reqs
, req
);
39032 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39035 current_binding_level
= entry_scope
;
39040 /* Finish the declaration of a fully implicit function template. Such a
39041 template has no explicit template parameter list so has not been through the
39042 normal template head and tail processing. synthesize_implicit_template_parm
39043 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39044 provided if the declaration is a class member such that its template
39045 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39046 form is returned. Otherwise NULL_TREE is returned. */
39049 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39051 gcc_assert (parser
->fully_implicit_function_template_p
);
39053 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39054 && DECL_VIRTUAL_P (member_decl_opt
))
39056 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39057 "implicit templates may not be %<virtual%>");
39058 DECL_VIRTUAL_P (member_decl_opt
) = false;
39061 if (member_decl_opt
)
39062 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39063 end_template_decl ();
39065 parser
->fully_implicit_function_template_p
= false;
39066 --parser
->num_template_parameter_lists
;
39068 return member_decl_opt
;
39071 /* Helper function for diagnostics that have complained about things
39072 being used with 'extern "C"' linkage.
39074 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39077 maybe_show_extern_c_location (void)
39079 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39080 inform (the_parser
->innermost_linkage_specification_location
,
39081 "%<extern \"C\"%> linkage started here");
39084 #include "gt-cp-parser.h"