2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
560 parser
->implicit_extern_c
);
561 cp_debug_print_flag (file
, "String expressions should be translated "
562 "to execution character set",
563 parser
->translate_strings_p
);
564 cp_debug_print_flag (file
, "Parsing function body outside of a "
565 "local class", parser
->in_function_body
);
566 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
567 parser
->colon_corrects_to_scope_p
);
568 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
569 parser
->colon_doesnt_start_class_def_p
);
570 if (parser
->type_definition_forbidden_message
)
571 fprintf (file
, "Error message for forbidden type definitions: %s\n",
572 parser
->type_definition_forbidden_message
);
573 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
574 fprintf (file
, "Number of class definitions in progress: %u\n",
575 parser
->num_classes_being_defined
);
576 fprintf (file
, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser
->num_template_parameter_lists
);
578 cp_debug_parser_tokens (file
, parser
, window_size
);
579 token
= parser
->lexer
->next_token
;
580 fprintf (file
, "Next token to parse:\n");
581 fprintf (file
, "\tToken: ");
582 cp_lexer_print_token (file
, token
);
583 eloc
= expand_location (token
->location
);
584 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
585 fprintf (file
, "\tLine: %d\n", eloc
.line
);
586 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
590 debug (cp_parser
&ref
)
592 cp_debug_parser (stderr
, &ref
);
596 debug (cp_parser
*ptr
)
601 fprintf (stderr
, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
616 /* Initially we are not debugging. */
617 lexer
->debugging_p
= false;
619 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
621 /* Create the buffer. */
622 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token
);
642 lexer
= cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer
->buffer
->quick_push (token
);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token
.type
!= CPP_EOF
)
650 cp_lexer_get_preprocessor_token (lexer
, &token
);
651 vec_safe_push (lexer
->buffer
, token
);
654 lexer
->last_token
= lexer
->buffer
->address ()
655 + lexer
->buffer
->length ()
657 lexer
->next_token
= lexer
->buffer
->length ()
658 ? lexer
->buffer
->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
681 lexer
->next_token
= first
== last
? &eof_token
: first
;
682 lexer
->last_token
= last
;
684 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
686 /* Initially we are not debugging. */
687 lexer
->debugging_p
= false;
689 gcc_assert (!lexer
->next_token
->purged_p
);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer
*lexer
)
698 vec_free (lexer
->buffer
);
699 lexer
->saved_tokens
.release ();
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
713 cp_lexer_debugging_p (cp_lexer
*lexer
)
715 if (!LEXER_DEBUGGING_ENABLED_P
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 /* Skip past purged tokens. */
759 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
763 return cp_lexer_token_at (lexer
, tp
);
766 /* nonzero if we are presently saving tokens. */
769 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
771 return lexer
->saved_tokens
.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
780 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
782 static int is_extern_c
= 0;
784 /* Get a new token from the preprocessor. */
786 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
787 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
788 token
->keyword
= RID_MAX
;
789 token
->purged_p
= false;
790 token
->error_reported
= false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c
+= pending_lang_change
;
796 pending_lang_change
= 0;
797 token
->implicit_extern_c
= is_extern_c
> 0;
799 /* Check to see if this token is a keyword. */
800 if (token
->type
== CPP_NAME
)
802 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
804 /* Mark this token as a keyword. */
805 token
->type
= CPP_KEYWORD
;
806 /* Record which keyword. */
807 token
->keyword
= C_RID_CODE (token
->u
.value
);
811 if (warn_cxx11_compat
812 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
813 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
815 /* Warn about the C++0x keyword (but still treat it as
817 warning (OPT_Wc__11_compat
,
818 "identifier %qE is a keyword in C++11",
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
826 token
->keyword
= RID_MAX
;
829 else if (token
->type
== CPP_AT_NAME
)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token
->type
= CPP_KEYWORD
;
833 switch (C_RID_CODE (token
->u
.value
))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
843 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
844 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
845 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
846 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
847 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
848 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
849 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
850 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
851 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if KEYWORD can start a decl-specifier. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE
:
979 if (keyword
>= RID_FIRST_INT_N
980 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
981 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
994 token
= cp_lexer_peek_token (lexer
);
995 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
998 /* Returns TRUE iff the token T begins a decltype type. */
1001 token_is_decltype (cp_token
*t
)
1003 return (t
->keyword
== RID_DECLTYPE
1004 || t
->type
== CPP_DECLTYPE
);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1010 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1012 cp_token
*t
= cp_lexer_peek_token (lexer
);
1013 return token_is_decltype (t
);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1020 saved_checks_value (struct tree_check
*check_value
)
1022 /* Perform any access checks that were deferred. */
1023 vec
<deferred_access_check
, va_gc
> *checks
;
1024 deferred_access_check
*chk
;
1025 checks
= check_value
->checks
;
1029 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1030 perform_or_defer_access_check (chk
->binfo
,
1032 chk
->diag_decl
, tf_warning_or_error
);
1034 /* Return the stored value. */
1035 return check_value
->value
;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1045 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer
))
1053 fprintf (cp_lexer_debug_stream
,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1057 token
= lexer
->next_token
;
1058 gcc_assert (!n
|| token
!= &eof_token
);
1062 if (token
== lexer
->last_token
)
1068 if (!token
->purged_p
)
1072 if (cp_lexer_debugging_p (lexer
))
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1085 cp_lexer_consume_token (cp_lexer
* lexer
)
1087 cp_token
*token
= lexer
->next_token
;
1089 gcc_assert (token
!= &eof_token
);
1090 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1094 lexer
->next_token
++;
1095 if (lexer
->next_token
== lexer
->last_token
)
1097 lexer
->next_token
= &eof_token
;
1102 while (lexer
->next_token
->purged_p
);
1104 cp_lexer_set_source_position_from_token (token
);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer
))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1110 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1111 putc ('\n', cp_lexer_debug_stream
);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1122 cp_lexer_purge_token (cp_lexer
*lexer
)
1124 cp_token
*tok
= lexer
->next_token
;
1126 gcc_assert (tok
!= &eof_token
);
1127 tok
->purged_p
= true;
1128 tok
->location
= UNKNOWN_LOCATION
;
1129 tok
->u
.value
= NULL_TREE
;
1130 tok
->keyword
= RID_MAX
;
1135 if (tok
== lexer
->last_token
)
1141 while (tok
->purged_p
);
1142 lexer
->next_token
= tok
;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1150 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1152 cp_token
*peek
= lexer
->next_token
;
1154 if (peek
== &eof_token
)
1155 peek
= lexer
->last_token
;
1157 gcc_assert (tok
< peek
);
1159 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1161 tok
->purged_p
= true;
1162 tok
->location
= UNKNOWN_LOCATION
;
1163 tok
->u
.value
= NULL_TREE
;
1164 tok
->keyword
= RID_MAX
;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1172 cp_lexer_save_tokens (cp_lexer
* lexer
)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer
))
1176 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1178 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1181 /* Commit to the portion of the token stream most recently saved. */
1184 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer
))
1188 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1190 lexer
->saved_tokens
.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1197 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer
))
1201 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1203 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1218 len
= lexer
->saved_tokens
.length ();
1219 cp_lexer_save_tokens (lexer
);
1223 cp_lexer_rollback_tokens (lexer
);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer
);
1230 gcc_assert (lexer
->saved_tokens
.length () == len
);
1234 /* Print a representation of the TOKEN on the STREAM. */
1237 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names
[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token
->type
)
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token
->u
.value
))
1264 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1271 case CPP_UTF8STRING
:
1272 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1276 print_generic_expr (stream
, token
->u
.value
);
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token
->type
< ARRAY_SIZE(token_names
))
1283 fputs (token_names
[token
->type
], stream
);
1285 fprintf (stream
, "[%d]", token
->type
);
1291 debug (cp_token
&ref
)
1293 cp_lexer_print_token (stderr
, &ref
);
1294 fprintf (stderr
, "\n");
1298 debug (cp_token
*ptr
)
1303 fprintf (stderr
, "<nil>\n");
1307 /* Start emitting debugging information. */
1310 cp_lexer_start_debugging (cp_lexer
* lexer
)
1312 if (!LEXER_DEBUGGING_ENABLED_P
)
1313 fatal_error (input_location
,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer
->debugging_p
= true;
1317 cp_lexer_debug_stream
= stderr
;
1320 /* Stop emitting debugging information. */
1323 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1325 if (!LEXER_DEBUGGING_ENABLED_P
)
1326 fatal_error (input_location
,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer
->debugging_p
= false;
1330 cp_lexer_debug_stream
= NULL
;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache
*
1336 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1338 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1339 cache
->first
= first
;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1348 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1350 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser
->omp_declare_simd
= NULL
;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1362 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1364 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1366 if (fndecl
== error_mark_node
)
1368 parser
->omp_declare_simd
= NULL
;
1371 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1373 cp_ensure_no_omp_declare_simd (parser
);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1383 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1385 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1387 error_at (parser
->oacc_routine
->loc
,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser
->oacc_routine
= NULL
;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1399 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1401 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator
*make_call_declarator
1412 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1413 static cp_declarator
*make_array_declarator
1414 (cp_declarator
*, tree
);
1415 static cp_declarator
*make_pointer_declarator
1416 (cp_cv_quals
, cp_declarator
*, tree
);
1417 static cp_declarator
*make_reference_declarator
1418 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1419 static cp_declarator
*make_ptrmem_declarator
1420 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1422 /* An erroneous declarator. */
1423 static cp_declarator
*cp_error_declarator
;
1425 /* The obstack on which declarators and related data structures are
1427 static struct obstack declarator_obstack
;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes
)
1434 return obstack_alloc (&declarator_obstack
, bytes
);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator
*
1441 make_declarator (cp_declarator_kind kind
)
1443 cp_declarator
*declarator
;
1445 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1446 declarator
->kind
= kind
;
1447 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1448 declarator
->attributes
= NULL_TREE
;
1449 declarator
->std_attributes
= NULL_TREE
;
1450 declarator
->declarator
= NULL
;
1451 declarator
->parameter_pack_p
= false;
1452 declarator
->id_loc
= UNKNOWN_LOCATION
;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1463 static cp_declarator
*
1464 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1465 special_function_kind sfk
)
1467 cp_declarator
*declarator
;
1469 /* It is valid to write:
1471 class C { void f(); };
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1479 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1480 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1482 gcc_assert (identifier_p (unqualified_name
)
1483 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1486 declarator
= make_declarator (cdk_id
);
1487 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1488 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1489 declarator
->u
.id
.sfk
= sfk
;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_pointer
);
1506 declarator
->declarator
= target
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1511 declarator
->id_loc
= target
->id_loc
;
1512 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1513 target
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1528 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1529 bool rvalue_ref
, tree attributes
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_reference
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1536 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1539 declarator
->id_loc
= target
->id_loc
;
1540 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1541 target
->parameter_pack_p
= false;
1544 declarator
->parameter_pack_p
= false;
1546 declarator
->std_attributes
= attributes
;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1557 cp_declarator
*pointee
,
1560 cp_declarator
*declarator
;
1562 declarator
= make_declarator (cdk_ptrmem
);
1563 declarator
->declarator
= pointee
;
1564 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1565 declarator
->u
.pointer
.class_type
= class_type
;
1569 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1570 pointee
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1575 declarator
->std_attributes
= attributes
;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1586 make_call_declarator (cp_declarator
*target
,
1588 cp_cv_quals cv_qualifiers
,
1589 cp_virt_specifiers virt_specifiers
,
1590 cp_ref_qualifier ref_qualifier
,
1592 tree exception_specification
,
1593 tree late_return_type
,
1594 tree requires_clause
)
1596 cp_declarator
*declarator
;
1598 declarator
= make_declarator (cdk_function
);
1599 declarator
->declarator
= target
;
1600 declarator
->u
.function
.parameters
= parms
;
1601 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1603 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1604 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1605 declarator
->u
.function
.exception_specification
= exception_specification
;
1606 declarator
->u
.function
.late_return_type
= late_return_type
;
1607 declarator
->u
.function
.requires_clause
= requires_clause
;
1610 declarator
->id_loc
= target
->id_loc
;
1611 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1612 target
->parameter_pack_p
= false;
1615 declarator
->parameter_pack_p
= false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1624 make_array_declarator (cp_declarator
*element
, tree bounds
)
1626 cp_declarator
*declarator
;
1628 declarator
= make_declarator (cdk_array
);
1629 declarator
->declarator
= element
;
1630 declarator
->u
.array
.bounds
= bounds
;
1633 declarator
->id_loc
= element
->id_loc
;
1634 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1635 element
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1648 if (declarator
&& declarator
->parameter_pack_p
)
1649 /* We already saw an ellipsis. */
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1657 while (declarator
&& !found
)
1659 switch ((int)declarator
->kind
)
1671 declarator
= declarator
->declarator
;
1679 cp_parameter_declarator
*no_parameters
;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator
*
1685 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1686 cp_declarator
*declarator
,
1687 tree default_argument
,
1689 bool template_parameter_pack_p
= false)
1691 cp_parameter_declarator
*parameter
;
1693 parameter
= ((cp_parameter_declarator
*)
1694 alloc_declarator (sizeof (cp_parameter_declarator
)));
1695 parameter
->next
= NULL
;
1696 if (decl_specifiers
)
1697 parameter
->decl_specifiers
= *decl_specifiers
;
1699 clear_decl_specs (¶meter
->decl_specifiers
);
1700 parameter
->declarator
= declarator
;
1701 parameter
->default_argument
= default_argument
;
1702 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1703 parameter
->loc
= loc
;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator
*declarator
)
1715 if (declarator
->kind
== cdk_function
1716 && declarator
->declarator
->kind
== cdk_id
)
1718 if (declarator
->kind
== cdk_id
1719 || declarator
->kind
== cdk_decomp
1720 || declarator
->kind
== cdk_error
)
1722 declarator
= declarator
->declarator
;
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1788 CP_PARSER_FLAGS_NONE
= 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser
*, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser
*, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser
*, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser
*, tree
, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2049 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2050 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser
*, tree
, tree
*, tree
*);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2060 static tree cp_parser_new_expression
2062 static vec
<tree
, va_gc
> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser
*, tree
*);
2066 static cp_declarator
*cp_parser_new_declarator_opt
2068 static cp_declarator
*cp_parser_direct_new_declarator
2070 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser
*, cp_expr
);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2084 static cp_expr cp_parser_expression
2085 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2090 static cp_expr cp_parser_lambda_expression
2092 static void cp_parser_lambda_introducer
2093 (cp_parser
*, tree
);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser
*, tree
);
2096 static void cp_parser_lambda_body
2097 (cp_parser
*, tree
);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_expression_statement
2106 (cp_parser
*, tree
);
2107 static tree cp_parser_compound_statement
2108 (cp_parser
*, tree
, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_selection_statement
2112 (cp_parser
*, bool *, vec
<tree
> *);
2113 static tree cp_parser_condition
2115 static tree cp_parser_iteration_statement
2116 (cp_parser
*, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser
*, tree
*decl
);
2119 static tree cp_parser_for
2120 (cp_parser
*, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser
*, tree
, tree
, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree
, tree
*, tree
*);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser
*, bool *, const token_indent_info
&);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2145 static void cp_parser_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_type_name
2166 static tree cp_parser_nonclass_name
2167 (cp_parser
* parser
);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser
*, bool, bool);
2170 static tree cp_parser_enum_specifier
2172 static void cp_parser_enumerator_list
2173 (cp_parser
*, tree
);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser
*, tree
);
2176 static tree cp_parser_namespace_name
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser
*, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser
*, bool);
2198 static tree cp_parser_decltype
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2207 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2224 static tree cp_parser_declarator_id
2225 (cp_parser
*, bool);
2226 static tree cp_parser_type_id
2228 static tree cp_parser_template_type_arg
2230 static tree
cp_parser_trailing_type_id (cp_parser
*);
2231 static tree cp_parser_type_id_1
2232 (cp_parser
*, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2235 static tree cp_parser_parameter_declaration_clause
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser
*, bool *);
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2274 static tree cp_parser_class_head
2275 (cp_parser
*, bool *);
2276 static enum tag_types cp_parser_class_key
2278 static void cp_parser_type_parameter_key
2279 (cp_parser
* parser
);
2280 static void cp_parser_member_specification_opt
2282 static void cp_parser_member_declaration
2284 static tree cp_parser_pure_specifier
2286 static tree cp_parser_constant_initializer
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2293 static tree cp_parser_base_specifier
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2300 static tree cp_parser_conversion_type_id
2302 static cp_declarator
*cp_parser_conversion_declarator_opt
2304 static void cp_parser_ctor_initializer_opt
2306 static void cp_parser_mem_initializer_list
2308 static tree cp_parser_mem_initializer
2310 static tree cp_parser_mem_initializer_id
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2317 static cp_expr cp_parser_operator
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser
*, bool);
2324 static tree cp_parser_template_parameter_list
2326 static tree cp_parser_template_parameter
2327 (cp_parser
*, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser
*, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser
*, bool, bool, enum tag_types
, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2334 static tree cp_parser_template_argument_list
2336 static tree cp_parser_template_argument
2338 static void cp_parser_explicit_instantiation
2340 static void cp_parser_explicit_specialization
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2347 static void cp_parser_function_try_block
2349 static void cp_parser_handler_seq
2351 static void cp_parser_handler
2353 static tree cp_parser_exception_declaration
2355 static tree cp_parser_throw_expression
2357 static tree cp_parser_exception_specification_opt
2359 static tree cp_parser_type_id_list
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2366 static tree cp_parser_asm_operand_list
2368 static tree cp_parser_asm_clobber_list
2370 static tree cp_parser_asm_label_list
2372 static bool cp_next_tokens_can_be_attribute_p
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2376 static bool cp_next_tokens_can_be_std_attribute_p
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser
*, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser
*, size_t);
2384 static tree cp_parser_attributes_opt
2386 static tree cp_parser_gnu_attributes_opt
2388 static tree cp_parser_gnu_attribute_list
2390 static tree cp_parser_std_attribute
2391 (cp_parser
*, tree
);
2392 static tree cp_parser_std_attribute_spec
2394 static tree cp_parser_std_attribute_spec_seq
2396 static size_t cp_parser_skip_attributes_opt
2397 (cp_parser
*, size_t);
2398 static bool cp_parser_extension_opt
2399 (cp_parser
*, int *);
2400 static void cp_parser_label_declaration
2403 /* Concept Extensions */
2405 static tree cp_parser_requires_clause
2407 static tree cp_parser_requires_clause_opt
2409 static tree cp_parser_requires_expression
2411 static tree cp_parser_requirement_parameter_list
2413 static tree cp_parser_requirement_body
2415 static tree cp_parser_requirement_list
2417 static tree cp_parser_requirement
2419 static tree cp_parser_simple_requirement
2421 static tree cp_parser_compound_requirement
2423 static tree cp_parser_type_requirement
2425 static tree cp_parser_nested_requirement
2428 /* Transactional Memory Extensions */
2430 static tree cp_parser_transaction
2431 (cp_parser
*, cp_token
*);
2432 static tree cp_parser_transaction_expression
2433 (cp_parser
*, enum rid
);
2434 static void cp_parser_function_transaction
2435 (cp_parser
*, enum rid
);
2436 static tree cp_parser_transaction_cancel
2439 enum pragma_context
{
2446 static bool cp_parser_pragma
2447 (cp_parser
*, enum pragma_context
, bool *);
2449 /* Objective-C++ Productions */
2451 static tree cp_parser_objc_message_receiver
2453 static tree cp_parser_objc_message_args
2455 static tree cp_parser_objc_message_expression
2457 static cp_expr cp_parser_objc_encode_expression
2459 static tree cp_parser_objc_defs_expression
2461 static tree cp_parser_objc_protocol_expression
2463 static tree cp_parser_objc_selector_expression
2465 static cp_expr cp_parser_objc_expression
2467 static bool cp_parser_objc_selector_p
2469 static tree cp_parser_objc_selector
2471 static tree cp_parser_objc_protocol_refs_opt
2473 static void cp_parser_objc_declaration
2474 (cp_parser
*, tree
);
2475 static tree cp_parser_objc_statement
2477 static bool cp_parser_objc_valid_prefix_attributes
2478 (cp_parser
*, tree
*);
2479 static void cp_parser_objc_at_property_declaration
2481 static void cp_parser_objc_at_synthesize_declaration
2483 static void cp_parser_objc_at_dynamic_declaration
2485 static tree cp_parser_objc_struct_declaration
2488 /* Utility Routines */
2490 static cp_expr cp_parser_lookup_name
2491 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2492 static tree cp_parser_lookup_name_simple
2493 (cp_parser
*, tree
, location_t
);
2494 static tree cp_parser_maybe_treat_template_as_class
2496 static bool cp_parser_check_declarator_template_parameters
2497 (cp_parser
*, cp_declarator
*, location_t
);
2498 static bool cp_parser_check_template_parameters
2499 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2500 static cp_expr cp_parser_simple_cast_expression
2502 static tree cp_parser_global_scope_opt
2503 (cp_parser
*, bool);
2504 static bool cp_parser_constructor_declarator_p
2505 (cp_parser
*, bool);
2506 static tree cp_parser_function_definition_from_specifiers_and_declarator
2507 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2508 static tree cp_parser_function_definition_after_declarator
2509 (cp_parser
*, bool);
2510 static bool cp_parser_template_declaration_after_export
2511 (cp_parser
*, bool);
2512 static void cp_parser_perform_template_parameter_access_checks
2513 (vec
<deferred_access_check
, va_gc
> *);
2514 static tree cp_parser_single_declaration
2515 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2516 static cp_expr cp_parser_functional_cast
2517 (cp_parser
*, tree
);
2518 static tree cp_parser_save_member_function_body
2519 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2520 static tree cp_parser_save_nsdmi
2522 static tree cp_parser_enclosed_template_argument_list
2524 static void cp_parser_save_default_args
2525 (cp_parser
*, tree
);
2526 static void cp_parser_late_parsing_for_member
2527 (cp_parser
*, tree
);
2528 static tree cp_parser_late_parse_one_default_arg
2529 (cp_parser
*, tree
, tree
, tree
);
2530 static void cp_parser_late_parsing_nsdmi
2531 (cp_parser
*, tree
);
2532 static void cp_parser_late_parsing_default_args
2533 (cp_parser
*, tree
);
2534 static tree cp_parser_sizeof_operand
2535 (cp_parser
*, enum rid
);
2536 static cp_expr cp_parser_trait_expr
2537 (cp_parser
*, enum rid
);
2538 static bool cp_parser_declares_only_class_p
2540 static void cp_parser_set_storage_class
2541 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2542 static void cp_parser_set_decl_spec_type
2543 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2544 static void set_and_check_decl_spec_loc
2545 (cp_decl_specifier_seq
*decl_specs
,
2546 cp_decl_spec ds
, cp_token
*);
2547 static bool cp_parser_friend_p
2548 (const cp_decl_specifier_seq
*);
2549 static void cp_parser_required_error
2550 (cp_parser
*, required_token
, bool, location_t
);
2551 static cp_token
*cp_parser_require
2552 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2553 static cp_token
*cp_parser_require_keyword
2554 (cp_parser
*, enum rid
, required_token
);
2555 static bool cp_parser_token_starts_function_definition_p
2557 static bool cp_parser_next_token_starts_class_definition_p
2559 static bool cp_parser_next_token_ends_template_argument_p
2561 static bool cp_parser_nth_token_starts_template_argument_list_p
2562 (cp_parser
*, size_t);
2563 static enum tag_types cp_parser_token_is_class_key
2565 static enum tag_types cp_parser_token_is_type_parameter_key
2567 static void cp_parser_check_class_key
2568 (enum tag_types
, tree type
);
2569 static void cp_parser_check_access_in_redeclaration
2570 (tree type
, location_t location
);
2571 static bool cp_parser_optional_template_keyword
2573 static void cp_parser_pre_parsed_nested_name_specifier
2575 static bool cp_parser_cache_group
2576 (cp_parser
*, enum cpp_ttype
, unsigned);
2577 static tree cp_parser_cache_defarg
2578 (cp_parser
*parser
, bool nsdmi
);
2579 static void cp_parser_parse_tentatively
2581 static void cp_parser_commit_to_tentative_parse
2583 static void cp_parser_commit_to_topmost_tentative_parse
2585 static void cp_parser_abort_tentative_parse
2587 static bool cp_parser_parse_definitely
2589 static inline bool cp_parser_parsing_tentatively
2591 static bool cp_parser_uncommitted_to_tentative_parse_p
2593 static void cp_parser_error
2594 (cp_parser
*, const char *);
2595 static void cp_parser_name_lookup_error
2596 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2597 static bool cp_parser_simulate_error
2599 static bool cp_parser_check_type_definition
2601 static void cp_parser_check_for_definition_in_return_type
2602 (cp_declarator
*, tree
, location_t type_location
);
2603 static void cp_parser_check_for_invalid_template_id
2604 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2605 static bool cp_parser_non_integral_constant_expression
2606 (cp_parser
*, non_integral_constant
);
2607 static void cp_parser_diagnose_invalid_type_name
2608 (cp_parser
*, tree
, location_t
);
2609 static bool cp_parser_parse_and_diagnose_invalid_type_name
2611 static int cp_parser_skip_to_closing_parenthesis
2612 (cp_parser
*, bool, bool, bool);
2613 static void cp_parser_skip_to_end_of_statement
2615 static void cp_parser_consume_semicolon_at_end_of_statement
2617 static void cp_parser_skip_to_end_of_block_or_statement
2619 static bool cp_parser_skip_to_closing_brace
2621 static void cp_parser_skip_to_end_of_template_parameter_list
2623 static void cp_parser_skip_to_pragma_eol
2624 (cp_parser
*, cp_token
*);
2625 static bool cp_parser_error_occurred
2627 static bool cp_parser_allow_gnu_extensions_p
2629 static bool cp_parser_is_pure_string_literal
2631 static bool cp_parser_is_string_literal
2633 static bool cp_parser_is_keyword
2634 (cp_token
*, enum rid
);
2635 static tree cp_parser_make_typename_type
2636 (cp_parser
*, tree
, location_t location
);
2637 static cp_declarator
* cp_parser_make_indirect_declarator
2638 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2639 static bool cp_parser_compound_literal_p
2641 static bool cp_parser_array_designator_p
2643 static bool cp_parser_init_statement_p
2645 static bool cp_parser_skip_to_closing_square_bracket
2648 /* Concept-related syntactic transformations */
2650 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2651 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2653 // -------------------------------------------------------------------------- //
2654 // Unevaluated Operand Guard
2656 // Implementation of an RAII helper for unevaluated operand parsing.
2657 cp_unevaluated::cp_unevaluated ()
2659 ++cp_unevaluated_operand
;
2660 ++c_inhibit_evaluation_warnings
;
2663 cp_unevaluated::~cp_unevaluated ()
2665 --c_inhibit_evaluation_warnings
;
2666 --cp_unevaluated_operand
;
2669 // -------------------------------------------------------------------------- //
2670 // Tentative Parsing
2672 /* Returns nonzero if we are parsing tentatively. */
2675 cp_parser_parsing_tentatively (cp_parser
* parser
)
2677 return parser
->context
->next
!= NULL
;
2680 /* Returns nonzero if TOKEN is a string literal. */
2683 cp_parser_is_pure_string_literal (cp_token
* token
)
2685 return (token
->type
== CPP_STRING
||
2686 token
->type
== CPP_STRING16
||
2687 token
->type
== CPP_STRING32
||
2688 token
->type
== CPP_WSTRING
||
2689 token
->type
== CPP_UTF8STRING
);
2692 /* Returns nonzero if TOKEN is a string literal
2693 of a user-defined string literal. */
2696 cp_parser_is_string_literal (cp_token
* token
)
2698 return (cp_parser_is_pure_string_literal (token
) ||
2699 token
->type
== CPP_STRING_USERDEF
||
2700 token
->type
== CPP_STRING16_USERDEF
||
2701 token
->type
== CPP_STRING32_USERDEF
||
2702 token
->type
== CPP_WSTRING_USERDEF
||
2703 token
->type
== CPP_UTF8STRING_USERDEF
);
2706 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2709 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2711 return token
->keyword
== keyword
;
2714 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 static enum pragma_kind
2718 cp_parser_pragma_kind (cp_token
*token
)
2720 if (token
->type
!= CPP_PRAGMA
)
2722 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2723 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2726 /* Helper function for cp_parser_error.
2727 Having peeked a token of kind TOK1_KIND that might signify
2728 a conflict marker, peek successor tokens to determine
2729 if we actually do have a conflict marker.
2730 Specifically, we consider a run of 7 '<', '=' or '>' characters
2731 at the start of a line as a conflict marker.
2732 These come through the lexer as three pairs and a single,
2733 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2734 If it returns true, *OUT_LOC is written to with the location/range
2738 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2739 location_t
*out_loc
)
2741 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2742 if (token2
->type
!= tok1_kind
)
2744 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2745 if (token3
->type
!= tok1_kind
)
2747 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2748 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2751 /* It must be at the start of the line. */
2752 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2753 if (LOCATION_COLUMN (start_loc
) != 1)
2756 /* We have a conflict marker. Construct a location of the form:
2759 with start == caret, finishing at the end of the marker. */
2760 location_t finish_loc
= get_finish (token4
->location
);
2761 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2766 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2770 get_matching_symbol (required_token token_desc
)
2777 case RT_CLOSE_BRACE
:
2779 case RT_CLOSE_PAREN
:
2784 /* Attempt to convert TOKEN_DESC from a required_token to an
2785 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2787 static enum cpp_ttype
2788 get_required_cpp_ttype (required_token token_desc
)
2793 return CPP_SEMICOLON
;
2795 return CPP_OPEN_PAREN
;
2796 case RT_CLOSE_BRACE
:
2797 return CPP_CLOSE_BRACE
;
2799 return CPP_OPEN_BRACE
;
2800 case RT_CLOSE_SQUARE
:
2801 return CPP_CLOSE_SQUARE
;
2802 case RT_OPEN_SQUARE
:
2803 return CPP_OPEN_SQUARE
;
2808 case RT_CLOSE_PAREN
:
2809 return CPP_CLOSE_PAREN
;
2812 /* Use CPP_EOF as a "no completions possible" code. */
2818 /* Subroutine of cp_parser_error and cp_parser_required_error.
2820 Issue a diagnostic of the form
2821 FILE:LINE: MESSAGE before TOKEN
2822 where TOKEN is the next token in the input stream. MESSAGE
2823 (specified by the caller) is usually of the form "expected
2826 This bypasses the check for tentative passing, and potentially
2827 adds material needed by cp_parser_required_error.
2829 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2830 suggesting insertion of the missing token.
2832 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2833 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2837 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2838 required_token missing_token_desc
,
2839 location_t matching_location
)
2841 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2842 /* This diagnostic makes more sense if it is tagged to the line
2843 of the token we just peeked at. */
2844 cp_lexer_set_source_position_from_token (token
);
2846 if (token
->type
== CPP_PRAGMA
)
2848 error_at (token
->location
,
2849 "%<#pragma%> is not allowed here");
2850 cp_parser_skip_to_pragma_eol (parser
, token
);
2854 /* If this is actually a conflict marker, report it as such. */
2855 if (token
->type
== CPP_LSHIFT
2856 || token
->type
== CPP_RSHIFT
2857 || token
->type
== CPP_EQ_EQ
)
2860 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2862 error_at (loc
, "version control conflict marker in file");
2867 gcc_rich_location
richloc (input_location
);
2869 bool added_matching_location
= false;
2871 if (missing_token_desc
!= RT_NONE
)
2873 /* Potentially supply a fix-it hint, suggesting to add the
2874 missing token immediately after the *previous* token.
2875 This may move the primary location within richloc. */
2876 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2877 location_t prev_token_loc
2878 = cp_lexer_previous_token (parser
->lexer
)->location
;
2879 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2881 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2882 Attempt to consolidate diagnostics by printing it as a
2883 secondary range within the main diagnostic. */
2884 if (matching_location
!= UNKNOWN_LOCATION
)
2885 added_matching_location
2886 = richloc
.add_location_if_nearby (matching_location
);
2889 /* Actually emit the error. */
2890 c_parse_error (gmsgid
,
2891 /* Because c_parser_error does not understand
2892 CPP_KEYWORD, keywords are treated like
2894 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2895 token
->u
.value
, token
->flags
, &richloc
);
2897 if (missing_token_desc
!= RT_NONE
)
2899 /* If we weren't able to consolidate matching_location, then
2900 print it as a secondary diagnostic. */
2901 if (matching_location
!= UNKNOWN_LOCATION
2902 && !added_matching_location
)
2903 inform (matching_location
, "to match this %qs",
2904 get_matching_symbol (missing_token_desc
));
2908 /* If not parsing tentatively, issue a diagnostic of the form
2909 FILE:LINE: MESSAGE before TOKEN
2910 where TOKEN is the next token in the input stream. MESSAGE
2911 (specified by the caller) is usually of the form "expected
2915 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2917 if (!cp_parser_simulate_error (parser
))
2918 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2921 /* Issue an error about name-lookup failing. NAME is the
2922 IDENTIFIER_NODE DECL is the result of
2923 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2924 the thing that we hoped to find. */
2927 cp_parser_name_lookup_error (cp_parser
* parser
,
2930 name_lookup_error desired
,
2931 location_t location
)
2933 /* If name lookup completely failed, tell the user that NAME was not
2935 if (decl
== error_mark_node
)
2937 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2938 error_at (location
, "%<%E::%E%> has not been declared",
2939 parser
->scope
, name
);
2940 else if (parser
->scope
== global_namespace
)
2941 error_at (location
, "%<::%E%> has not been declared", name
);
2942 else if (parser
->object_scope
2943 && !CLASS_TYPE_P (parser
->object_scope
))
2944 error_at (location
, "request for member %qE in non-class type %qT",
2945 name
, parser
->object_scope
);
2946 else if (parser
->object_scope
)
2947 error_at (location
, "%<%T::%E%> has not been declared",
2948 parser
->object_scope
, name
);
2950 error_at (location
, "%qE has not been declared", name
);
2952 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2957 error_at (location
, "%<%E::%E%> is not a type",
2958 parser
->scope
, name
);
2961 error_at (location
, "%<%E::%E%> is not a class or namespace",
2962 parser
->scope
, name
);
2966 "%<%E::%E%> is not a class, namespace, or enumeration",
2967 parser
->scope
, name
);
2974 else if (parser
->scope
== global_namespace
)
2979 error_at (location
, "%<::%E%> is not a type", name
);
2982 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2986 "%<::%E%> is not a class, namespace, or enumeration",
2998 error_at (location
, "%qE is not a type", name
);
3001 error_at (location
, "%qE is not a class or namespace", name
);
3005 "%qE is not a class, namespace, or enumeration", name
);
3013 /* If we are parsing tentatively, remember that an error has occurred
3014 during this tentative parse. Returns true if the error was
3015 simulated; false if a message should be issued by the caller. */
3018 cp_parser_simulate_error (cp_parser
* parser
)
3020 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3022 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3028 /* This function is called when a type is defined. If type
3029 definitions are forbidden at this point, an error message is
3033 cp_parser_check_type_definition (cp_parser
* parser
)
3035 /* If types are forbidden here, issue a message. */
3036 if (parser
->type_definition_forbidden_message
)
3038 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3039 in the message need to be interpreted. */
3040 error (parser
->type_definition_forbidden_message
);
3046 /* This function is called when the DECLARATOR is processed. The TYPE
3047 was a type defined in the decl-specifiers. If it is invalid to
3048 define a type in the decl-specifiers for DECLARATOR, an error is
3049 issued. TYPE_LOCATION is the location of TYPE and is used
3050 for error reporting. */
3053 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3054 tree type
, location_t type_location
)
3056 /* [dcl.fct] forbids type definitions in return types.
3057 Unfortunately, it's not easy to know whether or not we are
3058 processing a return type until after the fact. */
3060 && (declarator
->kind
== cdk_pointer
3061 || declarator
->kind
== cdk_reference
3062 || declarator
->kind
== cdk_ptrmem
))
3063 declarator
= declarator
->declarator
;
3065 && declarator
->kind
== cdk_function
)
3067 error_at (type_location
,
3068 "new types may not be defined in a return type");
3069 inform (type_location
,
3070 "(perhaps a semicolon is missing after the definition of %qT)",
3075 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3076 "<" in any valid C++ program. If the next token is indeed "<",
3077 issue a message warning the user about what appears to be an
3078 invalid attempt to form a template-id. LOCATION is the location
3079 of the type-specifier (TYPE) */
3082 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3084 enum tag_types tag_type
,
3085 location_t location
)
3087 cp_token_position start
= 0;
3089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3091 if (TREE_CODE (type
) == TYPE_DECL
)
3092 type
= TREE_TYPE (type
);
3093 if (TYPE_P (type
) && !template_placeholder_p (type
))
3094 error_at (location
, "%qT is not a template", type
);
3095 else if (identifier_p (type
))
3097 if (tag_type
!= none_type
)
3098 error_at (location
, "%qE is not a class template", type
);
3100 error_at (location
, "%qE is not a template", type
);
3103 error_at (location
, "invalid template-id");
3104 /* Remember the location of the invalid "<". */
3105 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3106 start
= cp_lexer_token_position (parser
->lexer
, true);
3107 /* Consume the "<". */
3108 cp_lexer_consume_token (parser
->lexer
);
3109 /* Parse the template arguments. */
3110 cp_parser_enclosed_template_argument_list (parser
);
3111 /* Permanently remove the invalid template arguments so that
3112 this error message is not issued again. */
3114 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3118 /* If parsing an integral constant-expression, issue an error message
3119 about the fact that THING appeared and return true. Otherwise,
3120 return false. In either case, set
3121 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3124 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3125 non_integral_constant thing
)
3127 parser
->non_integral_constant_expression_p
= true;
3128 if (parser
->integral_constant_expression_p
)
3130 if (!parser
->allow_non_integral_constant_expression_p
)
3132 const char *msg
= NULL
;
3136 pedwarn (input_location
, OPT_Wpedantic
,
3137 "ISO C++ forbids using a floating-point literal "
3138 "in a constant-expression");
3141 error ("a cast to a type other than an integral or "
3142 "enumeration type cannot appear in a "
3143 "constant-expression");
3146 error ("%<typeid%> operator "
3147 "cannot appear in a constant-expression");
3150 error ("non-constant compound literals "
3151 "cannot appear in a constant-expression");
3154 error ("a function call "
3155 "cannot appear in a constant-expression");
3158 error ("an increment "
3159 "cannot appear in a constant-expression");
3162 error ("an decrement "
3163 "cannot appear in a constant-expression");
3166 error ("an array reference "
3167 "cannot appear in a constant-expression");
3169 case NIC_ADDR_LABEL
:
3170 error ("the address of a label "
3171 "cannot appear in a constant-expression");
3173 case NIC_OVERLOADED
:
3174 error ("calls to overloaded operators "
3175 "cannot appear in a constant-expression");
3177 case NIC_ASSIGNMENT
:
3178 error ("an assignment cannot appear in a constant-expression");
3181 error ("a comma operator "
3182 "cannot appear in a constant-expression");
3184 case NIC_CONSTRUCTOR
:
3185 error ("a call to a constructor "
3186 "cannot appear in a constant-expression");
3188 case NIC_TRANSACTION
:
3189 error ("a transaction expression "
3190 "cannot appear in a constant-expression");
3196 msg
= "__FUNCTION__";
3198 case NIC_PRETTY_FUNC
:
3199 msg
= "__PRETTY_FUNCTION__";
3219 case NIC_PREINCREMENT
:
3222 case NIC_PREDECREMENT
:
3235 error ("%qs cannot appear in a constant-expression", msg
);
3242 /* Emit a diagnostic for an invalid type name. This function commits
3243 to the current active tentative parse, if any. (Otherwise, the
3244 problematic construct might be encountered again later, resulting
3245 in duplicate error messages.) LOCATION is the location of ID. */
3248 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3249 location_t location
)
3251 tree decl
, ambiguous_decls
;
3252 cp_parser_commit_to_tentative_parse (parser
);
3253 /* Try to lookup the identifier. */
3254 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3255 /*is_template=*/false,
3256 /*is_namespace=*/false,
3257 /*check_dependency=*/true,
3258 &ambiguous_decls
, location
);
3259 if (ambiguous_decls
)
3260 /* If the lookup was ambiguous, an error will already have
3263 /* If the lookup found a template-name, it means that the user forgot
3264 to specify an argument list. Emit a useful error message. */
3265 if (DECL_TYPE_TEMPLATE_P (decl
))
3268 "invalid use of template-name %qE without an argument list",
3270 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3271 inform (location
, "class template argument deduction is only available "
3272 "with -std=c++17 or -std=gnu++17");
3273 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3275 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3276 error_at (location
, "invalid use of destructor %qD as a type", id
);
3277 else if (TREE_CODE (decl
) == TYPE_DECL
)
3278 /* Something like 'unsigned A a;' */
3279 error_at (location
, "invalid combination of multiple type-specifiers");
3280 else if (!parser
->scope
)
3282 /* Issue an error message. */
3284 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3285 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3288 gcc_rich_location
richloc (location
);
3289 richloc
.add_fixit_replace (hint
.suggestion ());
3291 "%qE does not name a type; did you mean %qs?",
3292 id
, hint
.suggestion ());
3295 error_at (location
, "%qE does not name a type", id
);
3296 /* If we're in a template class, it's possible that the user was
3297 referring to a type from a base class. For example:
3299 template <typename T> struct A { typedef T X; };
3300 template <typename T> struct B : public A<T> { X x; };
3302 The user should have said "typename A<T>::X". */
3303 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3304 inform (location
, "C++11 %<constexpr%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3307 inform (location
, "C++11 %<noexcept%> only available with "
3308 "-std=c++11 or -std=gnu++11");
3309 else if (cxx_dialect
< cxx11
3310 && TREE_CODE (id
) == IDENTIFIER_NODE
3311 && id_equal (id
, "thread_local"))
3312 inform (location
, "C++11 %<thread_local%> only available with "
3313 "-std=c++11 or -std=gnu++11");
3314 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3315 inform (location
, "%<concept%> only available with -fconcepts");
3316 else if (processing_template_decl
&& current_class_type
3317 && TYPE_BINFO (current_class_type
))
3321 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3325 tree base_type
= BINFO_TYPE (b
);
3326 if (CLASS_TYPE_P (base_type
)
3327 && dependent_type_p (base_type
))
3330 /* Go from a particular instantiation of the
3331 template (which will have an empty TYPE_FIELDs),
3332 to the main version. */
3333 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3334 for (field
= TYPE_FIELDS (base_type
);
3336 field
= DECL_CHAIN (field
))
3337 if (TREE_CODE (field
) == TYPE_DECL
3338 && DECL_NAME (field
) == id
)
3341 "(perhaps %<typename %T::%E%> was intended)",
3342 BINFO_TYPE (b
), id
);
3351 /* Here we diagnose qualified-ids where the scope is actually correct,
3352 but the identifier does not resolve to a valid type name. */
3353 else if (parser
->scope
!= error_mark_node
)
3355 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3358 error_at (location_of (id
),
3359 "%qE in namespace %qE does not name a template type",
3361 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3362 error_at (location_of (id
),
3363 "%qE in namespace %qE does not name a template type",
3364 TREE_OPERAND (id
, 0), parser
->scope
);
3366 error_at (location_of (id
),
3367 "%qE in namespace %qE does not name a type",
3370 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3371 else if (decl
== error_mark_node
)
3372 suggest_alternative_in_explicit_scope (location
, id
,
3375 else if (CLASS_TYPE_P (parser
->scope
)
3376 && constructor_name_p (id
, parser
->scope
))
3379 error_at (location
, "%<%T::%E%> names the constructor, not"
3380 " the type", parser
->scope
, id
);
3381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3382 error_at (location
, "and %qT has no template constructors",
3385 else if (TYPE_P (parser
->scope
)
3386 && dependent_scope_p (parser
->scope
))
3388 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3390 "need %<typename%> before %<%T::%D::%E%> because "
3391 "%<%T::%D%> is a dependent scope",
3392 TYPE_CONTEXT (parser
->scope
),
3393 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3395 TYPE_CONTEXT (parser
->scope
),
3396 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3398 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3399 "%qT is a dependent scope",
3400 parser
->scope
, id
, parser
->scope
);
3402 else if (TYPE_P (parser
->scope
))
3404 if (!COMPLETE_TYPE_P (parser
->scope
))
3405 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3407 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3408 error_at (location_of (id
),
3409 "%qE in %q#T does not name a template type",
3411 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3412 error_at (location_of (id
),
3413 "%qE in %q#T does not name a template type",
3414 TREE_OPERAND (id
, 0), parser
->scope
);
3416 error_at (location_of (id
),
3417 "%qE in %q#T does not name a type",
3420 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3427 /* Check for a common situation where a type-name should be present,
3428 but is not, and issue a sensible error message. Returns true if an
3429 invalid type-name was detected.
3431 The situation handled by this function are variable declarations of the
3432 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3433 Usually, `ID' should name a type, but if we got here it means that it
3434 does not. We try to emit the best possible error message depending on
3435 how exactly the id-expression looks like. */
3438 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3441 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3443 /* Avoid duplicate error about ambiguous lookup. */
3444 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3446 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3447 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3451 cp_parser_parse_tentatively (parser
);
3452 id
= cp_parser_id_expression (parser
,
3453 /*template_keyword_p=*/false,
3454 /*check_dependency_p=*/true,
3455 /*template_p=*/NULL
,
3456 /*declarator_p=*/true,
3457 /*optional_p=*/false);
3458 /* If the next token is a (, this is a function with no explicit return
3459 type, i.e. constructor, destructor or conversion op. */
3460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3461 || TREE_CODE (id
) == TYPE_DECL
)
3463 cp_parser_abort_tentative_parse (parser
);
3466 if (!cp_parser_parse_definitely (parser
))
3469 /* Emit a diagnostic for the invalid type. */
3470 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3472 /* If we aren't in the middle of a declarator (i.e. in a
3473 parameter-declaration-clause), skip to the end of the declaration;
3474 there's no point in trying to process it. */
3475 if (!parser
->in_declarator_p
)
3476 cp_parser_skip_to_end_of_block_or_statement (parser
);
3480 /* Consume tokens up to, and including, the next non-nested closing `)'.
3481 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3482 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3483 found an unnested token of that type. */
3486 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3491 unsigned paren_depth
= 0;
3492 unsigned brace_depth
= 0;
3493 unsigned square_depth
= 0;
3495 if (recovering
&& or_ttype
== CPP_EOF
3496 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3501 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3503 /* Have we found what we're looking for before the closing paren? */
3504 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3505 && !brace_depth
&& !paren_depth
&& !square_depth
)
3508 switch (token
->type
)
3511 case CPP_PRAGMA_EOL
:
3512 /* If we've run out of tokens, then there is no closing `)'. */
3515 /* This is good for lambda expression capture-lists. */
3516 case CPP_OPEN_SQUARE
:
3519 case CPP_CLOSE_SQUARE
:
3520 if (!square_depth
--)
3525 /* This matches the processing in skip_to_end_of_statement. */
3530 case CPP_OPEN_BRACE
:
3533 case CPP_CLOSE_BRACE
:
3538 case CPP_OPEN_PAREN
:
3543 case CPP_CLOSE_PAREN
:
3544 if (!brace_depth
&& !paren_depth
--)
3547 cp_lexer_consume_token (parser
->lexer
);
3556 /* Consume the token. */
3557 cp_lexer_consume_token (parser
->lexer
);
3561 /* Consume tokens up to, and including, the next non-nested closing `)'.
3562 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3563 are doing error recovery. Returns -1 if OR_COMMA is true and we
3564 found an unnested token of that type. */
3567 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3572 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3573 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3574 ttype
, consume_paren
);
3577 /* Consume tokens until we reach the end of the current statement.
3578 Normally, that will be just before consuming a `;'. However, if a
3579 non-nested `}' comes first, then we stop before consuming that. */
3582 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3584 unsigned nesting_depth
= 0;
3586 /* Unwind generic function template scope if necessary. */
3587 if (parser
->fully_implicit_function_template_p
)
3588 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3592 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3594 switch (token
->type
)
3597 case CPP_PRAGMA_EOL
:
3598 /* If we've run out of tokens, stop. */
3602 /* If the next token is a `;', we have reached the end of the
3608 case CPP_CLOSE_BRACE
:
3609 /* If this is a non-nested '}', stop before consuming it.
3610 That way, when confronted with something like:
3614 we stop before consuming the closing '}', even though we
3615 have not yet reached a `;'. */
3616 if (nesting_depth
== 0)
3619 /* If it is the closing '}' for a block that we have
3620 scanned, stop -- but only after consuming the token.
3626 we will stop after the body of the erroneously declared
3627 function, but before consuming the following `typedef'
3629 if (--nesting_depth
== 0)
3631 cp_lexer_consume_token (parser
->lexer
);
3636 case CPP_OPEN_BRACE
:
3644 /* Consume the token. */
3645 cp_lexer_consume_token (parser
->lexer
);
3649 /* This function is called at the end of a statement or declaration.
3650 If the next token is a semicolon, it is consumed; otherwise, error
3651 recovery is attempted. */
3654 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3656 /* Look for the trailing `;'. */
3657 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3659 /* If there is additional (erroneous) input, skip to the end of
3661 cp_parser_skip_to_end_of_statement (parser
);
3662 /* If the next token is now a `;', consume it. */
3663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3664 cp_lexer_consume_token (parser
->lexer
);
3668 /* Skip tokens until we have consumed an entire block, or until we
3669 have consumed a non-nested `;'. */
3672 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3674 int nesting_depth
= 0;
3676 /* Unwind generic function template scope if necessary. */
3677 if (parser
->fully_implicit_function_template_p
)
3678 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3680 while (nesting_depth
>= 0)
3682 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3684 switch (token
->type
)
3687 case CPP_PRAGMA_EOL
:
3688 /* If we've run out of tokens, stop. */
3692 /* Stop if this is an unnested ';'. */
3697 case CPP_CLOSE_BRACE
:
3698 /* Stop if this is an unnested '}', or closes the outermost
3701 if (nesting_depth
< 0)
3707 case CPP_OPEN_BRACE
:
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser
->lexer
);
3721 /* Skip tokens until a non-nested closing curly brace is the next
3722 token, or there are no more tokens. Return true in the first case,
3726 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3728 unsigned nesting_depth
= 0;
3732 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3734 switch (token
->type
)
3737 case CPP_PRAGMA_EOL
:
3738 /* If we've run out of tokens, stop. */
3741 case CPP_CLOSE_BRACE
:
3742 /* If the next token is a non-nested `}', then we have reached
3743 the end of the current block. */
3744 if (nesting_depth
-- == 0)
3748 case CPP_OPEN_BRACE
:
3749 /* If it the next token is a `{', then we are entering a new
3750 block. Consume the entire block. */
3758 /* Consume the token. */
3759 cp_lexer_consume_token (parser
->lexer
);
3763 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3764 parameter is the PRAGMA token, allowing us to purge the entire pragma
3768 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3772 parser
->lexer
->in_pragma
= false;
3775 token
= cp_lexer_consume_token (parser
->lexer
);
3776 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3778 /* Ensure that the pragma is not parsed again. */
3779 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3782 /* Require pragma end of line, resyncing with it as necessary. The
3783 arguments are as for cp_parser_skip_to_pragma_eol. */
3786 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3788 parser
->lexer
->in_pragma
= false;
3789 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3790 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3793 /* This is a simple wrapper around make_typename_type. When the id is
3794 an unresolved identifier node, we can provide a superior diagnostic
3795 using cp_parser_diagnose_invalid_type_name. */
3798 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3799 location_t id_location
)
3802 if (identifier_p (id
))
3804 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3805 /*complain=*/tf_none
);
3806 if (result
== error_mark_node
)
3807 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3810 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3813 /* This is a wrapper around the
3814 make_{pointer,ptrmem,reference}_declarator functions that decides
3815 which one to call based on the CODE and CLASS_TYPE arguments. The
3816 CODE argument should be one of the values returned by
3817 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3818 appertain to the pointer or reference. */
3820 static cp_declarator
*
3821 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3822 cp_cv_quals cv_qualifiers
,
3823 cp_declarator
*target
,
3826 if (code
== ERROR_MARK
)
3827 return cp_error_declarator
;
3829 if (code
== INDIRECT_REF
)
3830 if (class_type
== NULL_TREE
)
3831 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3833 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3834 target
, attributes
);
3835 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3836 return make_reference_declarator (cv_qualifiers
, target
,
3838 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3839 return make_reference_declarator (cv_qualifiers
, target
,
3844 /* Create a new C++ parser. */
3847 cp_parser_new (void)
3853 /* cp_lexer_new_main is called before doing GC allocation because
3854 cp_lexer_new_main might load a PCH file. */
3855 lexer
= cp_lexer_new_main ();
3857 /* Initialize the binops_by_token so that we can get the tree
3858 directly from the token. */
3859 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3860 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3862 parser
= ggc_cleared_alloc
<cp_parser
> ();
3863 parser
->lexer
= lexer
;
3864 parser
->context
= cp_parser_context_new (NULL
);
3866 /* For now, we always accept GNU extensions. */
3867 parser
->allow_gnu_extensions_p
= 1;
3869 /* The `>' token is a greater-than operator, not the end of a
3871 parser
->greater_than_is_operator_p
= true;
3873 parser
->default_arg_ok_p
= true;
3875 /* We are not parsing a constant-expression. */
3876 parser
->integral_constant_expression_p
= false;
3877 parser
->allow_non_integral_constant_expression_p
= false;
3878 parser
->non_integral_constant_expression_p
= false;
3880 /* Local variable names are not forbidden. */
3881 parser
->local_variables_forbidden_p
= false;
3883 /* We are not processing an `extern "C"' declaration. */
3884 parser
->in_unbraced_linkage_specification_p
= false;
3886 /* We are not processing a declarator. */
3887 parser
->in_declarator_p
= false;
3889 /* We are not processing a template-argument-list. */
3890 parser
->in_template_argument_list_p
= false;
3892 /* We are not in an iteration statement. */
3893 parser
->in_statement
= 0;
3895 /* We are not in a switch statement. */
3896 parser
->in_switch_statement_p
= false;
3898 /* We are not parsing a type-id inside an expression. */
3899 parser
->in_type_id_in_expr_p
= false;
3901 /* Declarations aren't implicitly extern "C". */
3902 parser
->implicit_extern_c
= false;
3904 /* String literals should be translated to the execution character set. */
3905 parser
->translate_strings_p
= true;
3907 /* We are not parsing a function body. */
3908 parser
->in_function_body
= false;
3910 /* We can correct until told otherwise. */
3911 parser
->colon_corrects_to_scope_p
= true;
3913 /* The unparsed function queue is empty. */
3914 push_unparsed_function_queues (parser
);
3916 /* There are no classes being defined. */
3917 parser
->num_classes_being_defined
= 0;
3919 /* No template parameters apply. */
3920 parser
->num_template_parameter_lists
= 0;
3922 /* Special parsing data structures. */
3923 parser
->omp_declare_simd
= NULL
;
3924 parser
->oacc_routine
= NULL
;
3926 /* Not declaring an implicit function template. */
3927 parser
->auto_is_implicit_function_template_parm_p
= false;
3928 parser
->fully_implicit_function_template_p
= false;
3929 parser
->implicit_template_parms
= 0;
3930 parser
->implicit_template_scope
= 0;
3932 /* Allow constrained-type-specifiers. */
3933 parser
->prevent_constrained_type_specifiers
= 0;
3935 /* We haven't yet seen an 'extern "C"'. */
3936 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3941 /* Create a cp_lexer structure which will emit the tokens in CACHE
3942 and push it onto the parser's lexer stack. This is used for delayed
3943 parsing of in-class method bodies and default arguments, and should
3944 not be confused with tentative parsing. */
3946 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3948 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3949 lexer
->next
= parser
->lexer
;
3950 parser
->lexer
= lexer
;
3952 /* Move the current source position to that of the first token in the
3954 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3957 /* Pop the top lexer off the parser stack. This is never used for the
3958 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3960 cp_parser_pop_lexer (cp_parser
*parser
)
3962 cp_lexer
*lexer
= parser
->lexer
;
3963 parser
->lexer
= lexer
->next
;
3964 cp_lexer_destroy (lexer
);
3966 /* Put the current source position back where it was before this
3967 lexer was pushed. */
3968 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3971 /* Lexical conventions [gram.lex] */
3973 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3977 cp_parser_identifier (cp_parser
* parser
)
3981 /* Look for the identifier. */
3982 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3983 /* Return the value. */
3985 return cp_expr (token
->u
.value
, token
->location
);
3987 return error_mark_node
;
3990 /* Parse a sequence of adjacent string constants. Returns a
3991 TREE_STRING representing the combined, nul-terminated string
3992 constant. If TRANSLATE is true, translate the string to the
3993 execution character set. If WIDE_OK is true, a wide string is
3996 C++98 [lex.string] says that if a narrow string literal token is
3997 adjacent to a wide string literal token, the behavior is undefined.
3998 However, C99 6.4.5p4 says that this results in a wide string literal.
3999 We follow C99 here, for consistency with the C front end.
4001 This code is largely lifted from lex_string() in c-lex.c.
4003 FUTURE: ObjC++ will need to handle @-strings here. */
4005 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4006 bool lookup_udlit
= true)
4010 struct obstack str_ob
;
4011 cpp_string str
, istr
, *strs
;
4013 enum cpp_ttype type
, curr_type
;
4014 int have_suffix_p
= 0;
4016 tree suffix_id
= NULL_TREE
;
4017 bool curr_tok_is_userdef_p
= false;
4019 tok
= cp_lexer_peek_token (parser
->lexer
);
4020 if (!cp_parser_is_string_literal (tok
))
4022 cp_parser_error (parser
, "expected string-literal");
4023 return error_mark_node
;
4026 location_t loc
= tok
->location
;
4028 if (cpp_userdef_string_p (tok
->type
))
4030 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4031 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4032 curr_tok_is_userdef_p
= true;
4036 string_tree
= tok
->u
.value
;
4037 curr_type
= tok
->type
;
4041 /* Try to avoid the overhead of creating and destroying an obstack
4042 for the common case of just one string. */
4043 if (!cp_parser_is_string_literal
4044 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4046 cp_lexer_consume_token (parser
->lexer
);
4048 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4049 str
.len
= TREE_STRING_LENGTH (string_tree
);
4052 if (curr_tok_is_userdef_p
)
4054 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4056 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4059 curr_type
= tok
->type
;
4065 location_t last_tok_loc
= tok
->location
;
4066 gcc_obstack_init (&str_ob
);
4071 cp_lexer_consume_token (parser
->lexer
);
4073 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4074 str
.len
= TREE_STRING_LENGTH (string_tree
);
4076 if (curr_tok_is_userdef_p
)
4078 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4079 if (have_suffix_p
== 0)
4081 suffix_id
= curr_suffix_id
;
4084 else if (have_suffix_p
== 1
4085 && curr_suffix_id
!= suffix_id
)
4087 error ("inconsistent user-defined literal suffixes"
4088 " %qD and %qD in string literal",
4089 suffix_id
, curr_suffix_id
);
4092 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4095 curr_type
= tok
->type
;
4097 if (type
!= curr_type
)
4099 if (type
== CPP_STRING
)
4101 else if (curr_type
!= CPP_STRING
)
4103 rich_location
rich_loc (line_table
, tok
->location
);
4104 rich_loc
.add_range (last_tok_loc
, false);
4105 error_at (&rich_loc
,
4106 "unsupported non-standard concatenation "
4107 "of string literals");
4111 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4113 last_tok_loc
= tok
->location
;
4115 tok
= cp_lexer_peek_token (parser
->lexer
);
4116 if (cpp_userdef_string_p (tok
->type
))
4118 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4119 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4120 curr_tok_is_userdef_p
= true;
4124 string_tree
= tok
->u
.value
;
4125 curr_type
= tok
->type
;
4126 curr_tok_is_userdef_p
= false;
4129 while (cp_parser_is_string_literal (tok
));
4131 /* A string literal built by concatenation has its caret=start at
4132 the start of the initial string, and its finish at the finish of
4133 the final string literal. */
4134 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4136 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4139 if (type
!= CPP_STRING
&& !wide_ok
)
4141 cp_parser_error (parser
, "a wide string is invalid in this context");
4145 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4146 (parse_in
, strs
, count
, &istr
, type
))
4148 value
= build_string (istr
.len
, (const char *)istr
.text
);
4149 free (CONST_CAST (unsigned char *, istr
.text
));
4155 case CPP_UTF8STRING
:
4156 TREE_TYPE (value
) = char_array_type_node
;
4159 TREE_TYPE (value
) = char16_array_type_node
;
4162 TREE_TYPE (value
) = char32_array_type_node
;
4165 TREE_TYPE (value
) = wchar_array_type_node
;
4169 value
= fix_string_type (value
);
4173 tree literal
= build_userdef_literal (suffix_id
, value
,
4174 OT_NONE
, NULL_TREE
);
4176 value
= cp_parser_userdef_string_literal (literal
);
4182 /* cpp_interpret_string has issued an error. */
4183 value
= error_mark_node
;
4186 obstack_free (&str_ob
, 0);
4188 return cp_expr (value
, loc
);
4191 /* Look up a literal operator with the name and the exact arguments. */
4194 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4197 decl
= lookup_name (name
);
4198 if (!decl
|| !is_overloaded_fn (decl
))
4199 return error_mark_node
;
4201 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4206 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4207 if (parmtypes
!= NULL_TREE
)
4209 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4210 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4212 tree tparm
= TREE_VALUE (parmtypes
);
4213 tree targ
= TREE_TYPE ((*args
)[ix
]);
4214 bool ptr
= TYPE_PTR_P (tparm
);
4215 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4216 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4218 || !same_type_p (TREE_TYPE (tparm
),
4223 && ix
== vec_safe_length (args
)
4224 /* May be this should be sufficient_parms_p instead,
4225 depending on how exactly should user-defined literals
4226 work in presence of default arguments on the literal
4227 operator parameters. */
4228 && parmtypes
== void_list_node
)
4233 return error_mark_node
;
4236 /* Parse a user-defined char constant. Returns a call to a user-defined
4237 literal operator taking the character as an argument. */
4240 cp_parser_userdef_char_literal (cp_parser
*parser
)
4242 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4243 tree literal
= token
->u
.value
;
4244 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4245 tree value
= USERDEF_LITERAL_VALUE (literal
);
4246 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4249 /* Build up a call to the user-defined operator */
4250 /* Lookup the name we got back from the id-expression. */
4251 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4252 vec_safe_push (args
, value
);
4253 decl
= lookup_literal_operator (name
, args
);
4254 if (!decl
|| decl
== error_mark_node
)
4256 error ("unable to find character literal operator %qD with %qT argument",
4257 name
, TREE_TYPE (value
));
4258 release_tree_vector (args
);
4259 return error_mark_node
;
4261 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4262 release_tree_vector (args
);
4266 /* A subroutine of cp_parser_userdef_numeric_literal to
4267 create a char... template parameter pack from a string node. */
4270 make_char_string_pack (tree value
)
4273 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4274 const char *str
= TREE_STRING_POINTER (value
);
4275 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4276 tree argvec
= make_tree_vec (1);
4278 /* Fill in CHARVEC with all of the parameters. */
4279 charvec
= make_tree_vec (len
);
4280 for (i
= 0; i
< len
; ++i
)
4281 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4283 /* Build the argument packs. */
4284 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4286 TREE_VEC_ELT (argvec
, 0) = argpack
;
4291 /* A subroutine of cp_parser_userdef_numeric_literal to
4292 create a char... template parameter pack from a string node. */
4295 make_string_pack (tree value
)
4298 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4299 const unsigned char *str
4300 = (const unsigned char *) TREE_STRING_POINTER (value
);
4301 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4302 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4303 tree argvec
= make_tree_vec (2);
4305 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4306 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4308 /* First template parm is character type. */
4309 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4311 /* Fill in CHARVEC with all of the parameters. */
4312 charvec
= make_tree_vec (len
);
4313 for (int i
= 0; i
< len
; ++i
)
4314 TREE_VEC_ELT (charvec
, i
)
4315 = double_int_to_tree (str_char_type_node
,
4316 double_int::from_buffer (str
+ i
* sz
, sz
));
4318 /* Build the argument packs. */
4319 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4321 TREE_VEC_ELT (argvec
, 1) = argpack
;
4326 /* Parse a user-defined numeric constant. returns a call to a user-defined
4327 literal operator. */
4330 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4332 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4333 tree literal
= token
->u
.value
;
4334 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4335 tree value
= USERDEF_LITERAL_VALUE (literal
);
4336 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4337 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4338 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4340 vec
<tree
, va_gc
> *args
;
4342 /* Look for a literal operator taking the exact type of numeric argument
4343 as the literal value. */
4344 args
= make_tree_vector ();
4345 vec_safe_push (args
, value
);
4346 decl
= lookup_literal_operator (name
, args
);
4347 if (decl
&& decl
!= error_mark_node
)
4349 result
= finish_call_expr (decl
, &args
, false, true,
4350 tf_warning_or_error
);
4352 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4354 warning_at (token
->location
, OPT_Woverflow
,
4355 "integer literal exceeds range of %qT type",
4356 long_long_unsigned_type_node
);
4361 warning_at (token
->location
, OPT_Woverflow
,
4362 "floating literal exceeds range of %qT type",
4363 long_double_type_node
);
4364 else if (overflow
< 0)
4365 warning_at (token
->location
, OPT_Woverflow
,
4366 "floating literal truncated to zero");
4369 release_tree_vector (args
);
4372 release_tree_vector (args
);
4374 /* If the numeric argument didn't work, look for a raw literal
4375 operator taking a const char* argument consisting of the number
4376 in string format. */
4377 args
= make_tree_vector ();
4378 vec_safe_push (args
, num_string
);
4379 decl
= lookup_literal_operator (name
, args
);
4380 if (decl
&& decl
!= error_mark_node
)
4382 result
= finish_call_expr (decl
, &args
, false, true,
4383 tf_warning_or_error
);
4384 release_tree_vector (args
);
4387 release_tree_vector (args
);
4389 /* If the raw literal didn't work, look for a non-type template
4390 function with parameter pack char.... Call the function with
4391 template parameter characters representing the number. */
4392 args
= make_tree_vector ();
4393 decl
= lookup_literal_operator (name
, args
);
4394 if (decl
&& decl
!= error_mark_node
)
4396 tree tmpl_args
= make_char_string_pack (num_string
);
4397 decl
= lookup_template_function (decl
, tmpl_args
);
4398 result
= finish_call_expr (decl
, &args
, false, true,
4399 tf_warning_or_error
);
4400 release_tree_vector (args
);
4404 release_tree_vector (args
);
4406 /* In C++14 the standard library defines complex number suffixes that
4407 conflict with GNU extensions. Prefer them if <complex> is #included. */
4408 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4409 bool i14
= (cxx_dialect
> cxx11
4410 && (id_equal (suffix_id
, "i")
4411 || id_equal (suffix_id
, "if")
4412 || id_equal (suffix_id
, "il")));
4413 diagnostic_t kind
= DK_ERROR
;
4418 tree cxlit
= lookup_qualified_name (std_node
,
4419 get_identifier ("complex_literals"),
4421 if (cxlit
== error_mark_node
)
4423 /* No <complex>, so pedwarn and use GNU semantics. */
4425 opt
= OPT_Wpedantic
;
4430 = emit_diagnostic (kind
, input_location
, opt
,
4431 "unable to find numeric literal operator %qD", name
);
4434 /* Don't inform either. */;
4437 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4438 "(from <complex>) to enable the C++14 user-defined literal "
4441 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4442 "GNU built-in suffix");
4445 inform (token
->location
, "use -fext-numeric-literals "
4446 "to enable more built-in suffixes");
4448 if (kind
== DK_ERROR
)
4449 value
= error_mark_node
;
4452 /* Use the built-in semantics. */
4454 if (id_equal (suffix_id
, "i"))
4456 if (TREE_CODE (value
) == INTEGER_CST
)
4457 type
= integer_type_node
;
4459 type
= double_type_node
;
4461 else if (id_equal (suffix_id
, "if"))
4462 type
= float_type_node
;
4463 else /* if (id_equal (suffix_id, "il")) */
4464 type
= long_double_type_node
;
4466 value
= build_complex (build_complex_type (type
),
4467 fold_convert (type
, integer_zero_node
),
4468 fold_convert (type
, value
));
4471 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4472 /* Avoid repeated diagnostics. */
4473 token
->u
.value
= value
;
4477 /* Parse a user-defined string constant. Returns a call to a user-defined
4478 literal operator taking a character pointer and the length of the string
4482 cp_parser_userdef_string_literal (tree literal
)
4484 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4485 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4486 tree value
= USERDEF_LITERAL_VALUE (literal
);
4487 int len
= TREE_STRING_LENGTH (value
)
4488 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4490 vec
<tree
, va_gc
> *args
;
4492 /* Build up a call to the user-defined operator. */
4493 /* Lookup the name we got back from the id-expression. */
4494 args
= make_tree_vector ();
4495 vec_safe_push (args
, value
);
4496 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4497 decl
= lookup_literal_operator (name
, args
);
4499 if (decl
&& decl
!= error_mark_node
)
4501 result
= finish_call_expr (decl
, &args
, false, true,
4502 tf_warning_or_error
);
4503 release_tree_vector (args
);
4506 release_tree_vector (args
);
4508 /* Look for a template function with typename parameter CharT
4509 and parameter pack CharT... Call the function with
4510 template parameter characters representing the string. */
4511 args
= make_tree_vector ();
4512 decl
= lookup_literal_operator (name
, args
);
4513 if (decl
&& decl
!= error_mark_node
)
4515 tree tmpl_args
= make_string_pack (value
);
4516 decl
= lookup_template_function (decl
, tmpl_args
);
4517 result
= finish_call_expr (decl
, &args
, false, true,
4518 tf_warning_or_error
);
4519 release_tree_vector (args
);
4522 release_tree_vector (args
);
4524 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4525 name
, TREE_TYPE (value
), size_type_node
);
4526 return error_mark_node
;
4530 /* Basic concepts [gram.basic] */
4532 /* Parse a translation-unit.
4535 declaration-seq [opt]
4537 Returns TRUE if all went well. */
4540 cp_parser_translation_unit (cp_parser
* parser
)
4542 /* The address of the first non-permanent object on the declarator
4544 static void *declarator_obstack_base
;
4548 /* Create the declarator obstack, if necessary. */
4549 if (!cp_error_declarator
)
4551 gcc_obstack_init (&declarator_obstack
);
4552 /* Create the error declarator. */
4553 cp_error_declarator
= make_declarator (cdk_error
);
4554 /* Create the empty parameter list. */
4555 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4557 /* Remember where the base of the declarator obstack lies. */
4558 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4561 cp_parser_declaration_seq_opt (parser
);
4563 /* If there are no tokens left then all went well. */
4564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4566 /* Get rid of the token array; we don't need it any more. */
4567 cp_lexer_destroy (parser
->lexer
);
4568 parser
->lexer
= NULL
;
4570 /* This file might have been a context that's implicitly extern
4571 "C". If so, pop the lang context. (Only relevant for PCH.) */
4572 if (parser
->implicit_extern_c
)
4574 pop_lang_context ();
4575 parser
->implicit_extern_c
= false;
4579 finish_translation_unit ();
4585 cp_parser_error (parser
, "expected declaration");
4589 /* Make sure the declarator obstack was fully cleaned up. */
4590 gcc_assert (obstack_next_free (&declarator_obstack
)
4591 == declarator_obstack_base
);
4593 /* All went well. */
4597 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4598 decltype context. */
4600 static inline tsubst_flags_t
4601 complain_flags (bool decltype_p
)
4603 tsubst_flags_t complain
= tf_warning_or_error
;
4605 complain
|= tf_decltype
;
4609 /* We're about to parse a collection of statements. If we're currently
4610 parsing tentatively, set up a firewall so that any nested
4611 cp_parser_commit_to_tentative_parse won't affect the current context. */
4613 static cp_token_position
4614 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4619 cp_parser_parse_tentatively (parser
);
4620 cp_parser_commit_to_topmost_tentative_parse (parser
);
4621 return cp_lexer_token_position (parser
->lexer
, false);
4624 /* We've finished parsing the collection of statements. Wrap up the
4625 firewall and replace the relevant tokens with the parsed form. */
4628 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4634 /* Finish the firewall level. */
4635 cp_parser_parse_definitely (parser
);
4636 /* And remember the result of the parse for when we try again. */
4637 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4638 token
->type
= CPP_PREPARSED_EXPR
;
4639 token
->u
.value
= expr
;
4640 token
->keyword
= RID_MAX
;
4641 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4644 /* Like the above functions, but let the user modify the tokens. Used by
4645 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4646 later parses, so it makes sense to localize the effects of
4647 cp_parser_commit_to_tentative_parse. */
4649 struct tentative_firewall
4654 tentative_firewall (cp_parser
*p
): parser(p
)
4656 /* If we're currently parsing tentatively, start a committed level as a
4657 firewall and then an inner tentative parse. */
4658 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4660 cp_parser_parse_tentatively (parser
);
4661 cp_parser_commit_to_topmost_tentative_parse (parser
);
4662 cp_parser_parse_tentatively (parser
);
4666 ~tentative_firewall()
4670 /* Finish the inner tentative parse and the firewall, propagating any
4671 uncommitted error state to the outer tentative parse. */
4672 bool err
= cp_parser_error_occurred (parser
);
4673 cp_parser_parse_definitely (parser
);
4674 cp_parser_parse_definitely (parser
);
4676 cp_parser_simulate_error (parser
);
4681 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4682 This class is for tracking such a matching pair of symbols.
4683 In particular, it tracks the location of the first token,
4684 so that if the second token is missing, we can highlight the
4685 location of the first token when notifying the user about the
4688 template <typename traits_t
>
4692 /* token_pair's ctor. */
4693 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4695 /* If the next token is the opening symbol for this pair, consume it and
4697 Otherwise, issue an error and return false.
4698 In either case, record the location of the opening token. */
4700 bool require_open (cp_parser
*parser
)
4702 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4703 return cp_parser_require (parser
, traits_t::open_token_type
,
4704 traits_t::required_token_open
);
4707 /* Consume the next token from PARSER, recording its location as
4708 that of the opening token within the pair. */
4710 cp_token
* consume_open (cp_parser
*parser
)
4712 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4713 gcc_assert (tok
->type
== traits_t::open_token_type
);
4714 m_open_loc
= tok
->location
;
4718 /* If the next token is the closing symbol for this pair, consume it
4720 Otherwise, issue an error, highlighting the location of the
4721 corresponding opening token, and return NULL. */
4723 cp_token
*require_close (cp_parser
*parser
) const
4725 return cp_parser_require (parser
, traits_t::close_token_type
,
4726 traits_t::required_token_close
,
4731 location_t m_open_loc
;
4734 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4736 struct matching_paren_traits
4738 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4739 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4740 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4741 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4744 /* "matching_parens" is a token_pair<T> class for tracking matching
4745 pairs of parentheses. */
4747 typedef token_pair
<matching_paren_traits
> matching_parens
;
4749 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4751 struct matching_brace_traits
4753 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4754 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4755 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4756 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4759 /* "matching_braces" is a token_pair<T> class for tracking matching
4762 typedef token_pair
<matching_brace_traits
> matching_braces
;
4765 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4766 enclosing parentheses. */
4769 cp_parser_statement_expr (cp_parser
*parser
)
4771 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4773 /* Consume the '('. */
4774 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4775 matching_parens parens
;
4776 parens
.consume_open (parser
);
4777 /* Start the statement-expression. */
4778 tree expr
= begin_stmt_expr ();
4779 /* Parse the compound-statement. */
4780 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4782 expr
= finish_stmt_expr (expr
, false);
4783 /* Consume the ')'. */
4784 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4785 if (!parens
.require_close (parser
))
4786 cp_parser_skip_to_end_of_statement (parser
);
4788 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4789 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4790 return cp_expr (expr
, combined_loc
);
4793 /* Expressions [gram.expr] */
4795 /* Parse a fold-operator.
4798 - * / % ^ & | = < > << >>
4799 = -= *= /= %= ^= &= |= <<= >>=
4800 == != <= >= && || , .* ->*
4802 This returns the tree code corresponding to the matched operator
4803 as an int. When the current token matches a compound assignment
4804 opertor, the resulting tree code is the negative value of the
4805 non-assignment operator. */
4808 cp_parser_fold_operator (cp_token
*token
)
4810 switch (token
->type
)
4812 case CPP_PLUS
: return PLUS_EXPR
;
4813 case CPP_MINUS
: return MINUS_EXPR
;
4814 case CPP_MULT
: return MULT_EXPR
;
4815 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4816 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4817 case CPP_XOR
: return BIT_XOR_EXPR
;
4818 case CPP_AND
: return BIT_AND_EXPR
;
4819 case CPP_OR
: return BIT_IOR_EXPR
;
4820 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4821 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4823 case CPP_EQ
: return -NOP_EXPR
;
4824 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4825 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4826 case CPP_MULT_EQ
: return -MULT_EXPR
;
4827 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4828 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4829 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4830 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4831 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4832 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4833 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4835 case CPP_EQ_EQ
: return EQ_EXPR
;
4836 case CPP_NOT_EQ
: return NE_EXPR
;
4837 case CPP_LESS
: return LT_EXPR
;
4838 case CPP_GREATER
: return GT_EXPR
;
4839 case CPP_LESS_EQ
: return LE_EXPR
;
4840 case CPP_GREATER_EQ
: return GE_EXPR
;
4842 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4843 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4845 case CPP_COMMA
: return COMPOUND_EXPR
;
4847 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4848 case CPP_DEREF_STAR
: return MEMBER_REF
;
4850 default: return ERROR_MARK
;
4854 /* Returns true if CODE indicates a binary expression, which is not allowed in
4855 the LHS of a fold-expression. More codes will need to be added to use this
4856 function in other contexts. */
4859 is_binary_op (tree_code code
)
4864 case POINTER_PLUS_EXPR
:
4867 case TRUNC_DIV_EXPR
:
4868 case TRUNC_MOD_EXPR
:
4884 case TRUTH_ANDIF_EXPR
:
4885 case TRUTH_ORIF_EXPR
:
4898 /* If the next token is a suitable fold operator, consume it and return as
4899 the function above. */
4902 cp_parser_fold_operator (cp_parser
*parser
)
4904 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4905 int code
= cp_parser_fold_operator (token
);
4906 if (code
!= ERROR_MARK
)
4907 cp_lexer_consume_token (parser
->lexer
);
4911 /* Parse a fold-expression.
4914 ( ... folding-operator cast-expression)
4915 ( cast-expression folding-operator ... )
4916 ( cast-expression folding operator ... folding-operator cast-expression)
4918 Note that the '(' and ')' are matched in primary expression. */
4921 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4928 cp_lexer_consume_token (parser
->lexer
);
4929 int op
= cp_parser_fold_operator (parser
);
4930 if (op
== ERROR_MARK
)
4932 cp_parser_error (parser
, "expected binary operator");
4933 return error_mark_node
;
4936 tree expr
= cp_parser_cast_expression (parser
, false, false,
4938 if (expr
== error_mark_node
)
4939 return error_mark_node
;
4940 return finish_left_unary_fold_expr (expr
, op
);
4943 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4944 int op
= cp_parser_fold_operator (parser
);
4945 if (op
== ERROR_MARK
)
4947 cp_parser_error (parser
, "expected binary operator");
4948 return error_mark_node
;
4951 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4953 cp_parser_error (parser
, "expected ...");
4954 return error_mark_node
;
4956 cp_lexer_consume_token (parser
->lexer
);
4958 /* The operands of a fold-expression are cast-expressions, so binary or
4959 conditional expressions are not allowed. We check this here to avoid
4960 tentative parsing. */
4961 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4962 /* OK, the expression was parenthesized. */;
4963 else if (is_binary_op (TREE_CODE (expr1
)))
4964 error_at (location_of (expr1
),
4965 "binary expression in operand of fold-expression");
4966 else if (TREE_CODE (expr1
) == COND_EXPR
)
4967 error_at (location_of (expr1
),
4968 "conditional expression in operand of fold-expression");
4971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4972 return finish_right_unary_fold_expr (expr1
, op
);
4974 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4976 cp_parser_error (parser
, "mismatched operator in fold-expression");
4977 return error_mark_node
;
4979 cp_lexer_consume_token (parser
->lexer
);
4981 // Binary left or right fold.
4982 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4983 if (expr2
== error_mark_node
)
4984 return error_mark_node
;
4985 return finish_binary_fold_expr (expr1
, expr2
, op
);
4988 /* Parse a primary-expression.
4995 lambda-expression (C++11)
5000 ( compound-statement )
5001 __builtin_va_arg ( assignment-expression , type-id )
5002 __builtin_offsetof ( type-id , offsetof-expression )
5005 __has_nothrow_assign ( type-id )
5006 __has_nothrow_constructor ( type-id )
5007 __has_nothrow_copy ( type-id )
5008 __has_trivial_assign ( type-id )
5009 __has_trivial_constructor ( type-id )
5010 __has_trivial_copy ( type-id )
5011 __has_trivial_destructor ( type-id )
5012 __has_virtual_destructor ( type-id )
5013 __is_abstract ( type-id )
5014 __is_base_of ( type-id , type-id )
5015 __is_class ( type-id )
5016 __is_empty ( type-id )
5017 __is_enum ( type-id )
5018 __is_final ( type-id )
5019 __is_literal_type ( type-id )
5020 __is_pod ( type-id )
5021 __is_polymorphic ( type-id )
5022 __is_std_layout ( type-id )
5023 __is_trivial ( type-id )
5024 __is_union ( type-id )
5026 Objective-C++ Extension:
5034 ADDRESS_P is true iff this expression was immediately preceded by
5035 "&" and therefore might denote a pointer-to-member. CAST_P is true
5036 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5037 true iff this expression is a template argument.
5039 Returns a representation of the expression. Upon return, *IDK
5040 indicates what kind of id-expression (if any) was present. */
5043 cp_parser_primary_expression (cp_parser
*parser
,
5046 bool template_arg_p
,
5050 cp_token
*token
= NULL
;
5052 /* Assume the primary expression is not an id-expression. */
5053 *idk
= CP_ID_KIND_NONE
;
5055 /* Peek at the next token. */
5056 token
= cp_lexer_peek_token (parser
->lexer
);
5057 switch ((int) token
->type
)
5066 user-defined-literal */
5073 case CPP_PREPARSED_EXPR
:
5074 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5075 return cp_parser_userdef_numeric_literal (parser
);
5076 token
= cp_lexer_consume_token (parser
->lexer
);
5077 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5079 error_at (token
->location
,
5080 "fixed-point types not supported in C++");
5081 return error_mark_node
;
5083 /* Floating-point literals are only allowed in an integral
5084 constant expression if they are cast to an integral or
5085 enumeration type. */
5086 if (TREE_CODE (token
->u
.value
) == REAL_CST
5087 && parser
->integral_constant_expression_p
5090 /* CAST_P will be set even in invalid code like "int(2.7 +
5091 ...)". Therefore, we have to check that the next token
5092 is sure to end the cast. */
5095 cp_token
*next_token
;
5097 next_token
= cp_lexer_peek_token (parser
->lexer
);
5098 if (/* The comma at the end of an
5099 enumerator-definition. */
5100 next_token
->type
!= CPP_COMMA
5101 /* The curly brace at the end of an enum-specifier. */
5102 && next_token
->type
!= CPP_CLOSE_BRACE
5103 /* The end of a statement. */
5104 && next_token
->type
!= CPP_SEMICOLON
5105 /* The end of the cast-expression. */
5106 && next_token
->type
!= CPP_CLOSE_PAREN
5107 /* The end of an array bound. */
5108 && next_token
->type
!= CPP_CLOSE_SQUARE
5109 /* The closing ">" in a template-argument-list. */
5110 && (next_token
->type
!= CPP_GREATER
5111 || parser
->greater_than_is_operator_p
)
5112 /* C++0x only: A ">>" treated like two ">" tokens,
5113 in a template-argument-list. */
5114 && (next_token
->type
!= CPP_RSHIFT
5115 || (cxx_dialect
== cxx98
)
5116 || parser
->greater_than_is_operator_p
))
5120 /* If we are within a cast, then the constraint that the
5121 cast is to an integral or enumeration type will be
5122 checked at that point. If we are not within a cast, then
5123 this code is invalid. */
5125 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5127 return cp_expr (token
->u
.value
, token
->location
);
5129 case CPP_CHAR_USERDEF
:
5130 case CPP_CHAR16_USERDEF
:
5131 case CPP_CHAR32_USERDEF
:
5132 case CPP_WCHAR_USERDEF
:
5133 case CPP_UTF8CHAR_USERDEF
:
5134 return cp_parser_userdef_char_literal (parser
);
5140 case CPP_UTF8STRING
:
5141 case CPP_STRING_USERDEF
:
5142 case CPP_STRING16_USERDEF
:
5143 case CPP_STRING32_USERDEF
:
5144 case CPP_WSTRING_USERDEF
:
5145 case CPP_UTF8STRING_USERDEF
:
5146 /* ??? Should wide strings be allowed when parser->translate_strings_p
5147 is false (i.e. in attributes)? If not, we can kill the third
5148 argument to cp_parser_string_literal. */
5149 return cp_parser_string_literal (parser
,
5150 parser
->translate_strings_p
,
5153 case CPP_OPEN_PAREN
:
5154 /* If we see `( { ' then we are looking at the beginning of
5155 a GNU statement-expression. */
5156 if (cp_parser_allow_gnu_extensions_p (parser
)
5157 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5159 /* Statement-expressions are not allowed by the standard. */
5160 pedwarn (token
->location
, OPT_Wpedantic
,
5161 "ISO C++ forbids braced-groups within expressions");
5163 /* And they're not allowed outside of a function-body; you
5164 cannot, for example, write:
5166 int i = ({ int j = 3; j + 1; });
5168 at class or namespace scope. */
5169 if (!parser
->in_function_body
5170 || parser
->in_template_argument_list_p
)
5172 error_at (token
->location
,
5173 "statement-expressions are not allowed outside "
5174 "functions nor in template-argument lists");
5175 cp_parser_skip_to_end_of_block_or_statement (parser
);
5176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5177 cp_lexer_consume_token (parser
->lexer
);
5178 return error_mark_node
;
5181 return cp_parser_statement_expr (parser
);
5183 /* Otherwise it's a normal parenthesized expression. */
5186 bool saved_greater_than_is_operator_p
;
5188 location_t open_paren_loc
= token
->location
;
5190 /* Consume the `('. */
5191 matching_parens parens
;
5192 parens
.consume_open (parser
);
5193 /* Within a parenthesized expression, a `>' token is always
5194 the greater-than operator. */
5195 saved_greater_than_is_operator_p
5196 = parser
->greater_than_is_operator_p
;
5197 parser
->greater_than_is_operator_p
= true;
5199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5200 /* Left fold expression. */
5203 /* Parse the parenthesized expression. */
5204 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5206 token
= cp_lexer_peek_token (parser
->lexer
);
5207 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5209 expr
= cp_parser_fold_expression (parser
, expr
);
5210 if (expr
!= error_mark_node
5211 && cxx_dialect
< cxx17
5212 && !in_system_header_at (input_location
))
5213 pedwarn (input_location
, 0, "fold-expressions only available "
5214 "with -std=c++17 or -std=gnu++17");
5217 /* Let the front end know that this expression was
5218 enclosed in parentheses. This matters in case, for
5219 example, the expression is of the form `A::B', since
5220 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5222 expr
= finish_parenthesized_expr (expr
);
5224 /* DR 705: Wrapping an unqualified name in parentheses
5225 suppresses arg-dependent lookup. We want to pass back
5226 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5227 (c++/37862), but none of the others. */
5228 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5229 *idk
= CP_ID_KIND_NONE
;
5231 /* The `>' token might be the end of a template-id or
5232 template-parameter-list now. */
5233 parser
->greater_than_is_operator_p
5234 = saved_greater_than_is_operator_p
;
5236 /* Consume the `)'. */
5237 token
= cp_lexer_peek_token (parser
->lexer
);
5238 location_t close_paren_loc
= token
->location
;
5239 expr
.set_range (open_paren_loc
, close_paren_loc
);
5240 if (!parens
.require_close (parser
)
5241 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5242 cp_parser_skip_to_end_of_statement (parser
);
5247 case CPP_OPEN_SQUARE
:
5249 if (c_dialect_objc ())
5251 /* We might have an Objective-C++ message. */
5252 cp_parser_parse_tentatively (parser
);
5253 tree msg
= cp_parser_objc_message_expression (parser
);
5254 /* If that works out, we're done ... */
5255 if (cp_parser_parse_definitely (parser
))
5257 /* ... else, fall though to see if it's a lambda. */
5259 cp_expr lam
= cp_parser_lambda_expression (parser
);
5260 /* Don't warn about a failed tentative parse. */
5261 if (cp_parser_error_occurred (parser
))
5262 return error_mark_node
;
5263 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5267 case CPP_OBJC_STRING
:
5268 if (c_dialect_objc ())
5269 /* We have an Objective-C++ string literal. */
5270 return cp_parser_objc_expression (parser
);
5271 cp_parser_error (parser
, "expected primary-expression");
5272 return error_mark_node
;
5275 switch (token
->keyword
)
5277 /* These two are the boolean literals. */
5279 cp_lexer_consume_token (parser
->lexer
);
5280 return cp_expr (boolean_true_node
, token
->location
);
5282 cp_lexer_consume_token (parser
->lexer
);
5283 return cp_expr (boolean_false_node
, token
->location
);
5285 /* The `__null' literal. */
5287 cp_lexer_consume_token (parser
->lexer
);
5288 return cp_expr (null_node
, token
->location
);
5290 /* The `nullptr' literal. */
5292 cp_lexer_consume_token (parser
->lexer
);
5293 return cp_expr (nullptr_node
, token
->location
);
5295 /* Recognize the `this' keyword. */
5297 cp_lexer_consume_token (parser
->lexer
);
5298 if (parser
->local_variables_forbidden_p
)
5300 error_at (token
->location
,
5301 "%<this%> may not be used in this context");
5302 return error_mark_node
;
5304 /* Pointers cannot appear in constant-expressions. */
5305 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5306 return error_mark_node
;
5307 return cp_expr (finish_this_expr (), token
->location
);
5309 /* The `operator' keyword can be the beginning of an
5314 case RID_FUNCTION_NAME
:
5315 case RID_PRETTY_FUNCTION_NAME
:
5316 case RID_C99_FUNCTION_NAME
:
5318 non_integral_constant name
;
5320 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5321 __func__ are the names of variables -- but they are
5322 treated specially. Therefore, they are handled here,
5323 rather than relying on the generic id-expression logic
5324 below. Grammatically, these names are id-expressions.
5326 Consume the token. */
5327 token
= cp_lexer_consume_token (parser
->lexer
);
5329 switch (token
->keyword
)
5331 case RID_FUNCTION_NAME
:
5332 name
= NIC_FUNC_NAME
;
5334 case RID_PRETTY_FUNCTION_NAME
:
5335 name
= NIC_PRETTY_FUNC
;
5337 case RID_C99_FUNCTION_NAME
:
5338 name
= NIC_C99_FUNC
;
5344 if (cp_parser_non_integral_constant_expression (parser
, name
))
5345 return error_mark_node
;
5347 /* Look up the name. */
5348 return finish_fname (token
->u
.value
);
5355 source_location type_location
;
5356 location_t start_loc
5357 = cp_lexer_peek_token (parser
->lexer
)->location
;
5358 /* The `__builtin_va_arg' construct is used to handle
5359 `va_arg'. Consume the `__builtin_va_arg' token. */
5360 cp_lexer_consume_token (parser
->lexer
);
5361 /* Look for the opening `('. */
5362 matching_parens parens
;
5363 parens
.require_open (parser
);
5364 /* Now, parse the assignment-expression. */
5365 expression
= cp_parser_assignment_expression (parser
);
5366 /* Look for the `,'. */
5367 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5368 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5369 /* Parse the type-id. */
5371 type_id_in_expr_sentinel
s (parser
);
5372 type
= cp_parser_type_id (parser
);
5374 /* Look for the closing `)'. */
5375 location_t finish_loc
5376 = cp_lexer_peek_token (parser
->lexer
)->location
;
5377 parens
.require_close (parser
);
5378 /* Using `va_arg' in a constant-expression is not
5380 if (cp_parser_non_integral_constant_expression (parser
,
5382 return error_mark_node
;
5383 /* Construct a location of the form:
5384 __builtin_va_arg (v, int)
5385 ~~~~~~~~~~~~~~~~~~~~~^~~~
5386 with the caret at the type, ranging from the start of the
5387 "__builtin_va_arg" token to the close paren. */
5388 location_t combined_loc
5389 = make_location (type_location
, start_loc
, finish_loc
);
5390 return build_x_va_arg (combined_loc
, expression
, type
);
5394 return cp_parser_builtin_offsetof (parser
);
5396 case RID_HAS_NOTHROW_ASSIGN
:
5397 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5398 case RID_HAS_NOTHROW_COPY
:
5399 case RID_HAS_TRIVIAL_ASSIGN
:
5400 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5401 case RID_HAS_TRIVIAL_COPY
:
5402 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5403 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5404 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5405 case RID_IS_ABSTRACT
:
5406 case RID_IS_AGGREGATE
:
5407 case RID_IS_BASE_OF
:
5412 case RID_IS_LITERAL_TYPE
:
5414 case RID_IS_POLYMORPHIC
:
5415 case RID_IS_SAME_AS
:
5416 case RID_IS_STD_LAYOUT
:
5417 case RID_IS_TRIVIAL
:
5418 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5419 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5420 case RID_IS_TRIVIALLY_COPYABLE
:
5422 case RID_IS_ASSIGNABLE
:
5423 case RID_IS_CONSTRUCTIBLE
:
5424 return cp_parser_trait_expr (parser
, token
->keyword
);
5428 return cp_parser_requires_expression (parser
);
5430 /* Objective-C++ expressions. */
5432 case RID_AT_PROTOCOL
:
5433 case RID_AT_SELECTOR
:
5434 return cp_parser_objc_expression (parser
);
5437 if (parser
->in_function_body
5438 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5441 error_at (token
->location
,
5442 "a template declaration cannot appear at block scope");
5443 cp_parser_skip_to_end_of_block_or_statement (parser
);
5444 return error_mark_node
;
5448 cp_parser_error (parser
, "expected primary-expression");
5449 return error_mark_node
;
5452 /* An id-expression can start with either an identifier, a
5453 `::' as the beginning of a qualified-id, or the "operator"
5457 case CPP_TEMPLATE_ID
:
5458 case CPP_NESTED_NAME_SPECIFIER
:
5461 cp_expr id_expression
;
5463 const char *error_msg
;
5466 cp_token
*id_expr_token
;
5468 /* Parse the id-expression. */
5470 = cp_parser_id_expression (parser
,
5471 /*template_keyword_p=*/false,
5472 /*check_dependency_p=*/true,
5474 /*declarator_p=*/false,
5475 /*optional_p=*/false);
5476 if (id_expression
== error_mark_node
)
5477 return error_mark_node
;
5478 id_expr_token
= token
;
5479 token
= cp_lexer_peek_token (parser
->lexer
);
5480 done
= (token
->type
!= CPP_OPEN_SQUARE
5481 && token
->type
!= CPP_OPEN_PAREN
5482 && token
->type
!= CPP_DOT
5483 && token
->type
!= CPP_DEREF
5484 && token
->type
!= CPP_PLUS_PLUS
5485 && token
->type
!= CPP_MINUS_MINUS
);
5486 /* If we have a template-id, then no further lookup is
5487 required. If the template-id was for a template-class, we
5488 will sometimes have a TYPE_DECL at this point. */
5489 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5490 || TREE_CODE (id_expression
) == TYPE_DECL
)
5491 decl
= id_expression
;
5492 /* Look up the name. */
5495 tree ambiguous_decls
;
5497 /* If we already know that this lookup is ambiguous, then
5498 we've already issued an error message; there's no reason
5500 if (id_expr_token
->type
== CPP_NAME
5501 && id_expr_token
->error_reported
)
5503 cp_parser_simulate_error (parser
);
5504 return error_mark_node
;
5507 decl
= cp_parser_lookup_name (parser
, id_expression
,
5510 /*is_namespace=*/false,
5511 /*check_dependency=*/true,
5513 id_expr_token
->location
);
5514 /* If the lookup was ambiguous, an error will already have
5516 if (ambiguous_decls
)
5517 return error_mark_node
;
5519 /* In Objective-C++, we may have an Objective-C 2.0
5520 dot-syntax for classes here. */
5521 if (c_dialect_objc ()
5522 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5523 && TREE_CODE (decl
) == TYPE_DECL
5524 && objc_is_class_name (decl
))
5527 cp_lexer_consume_token (parser
->lexer
);
5528 component
= cp_parser_identifier (parser
);
5529 if (component
== error_mark_node
)
5530 return error_mark_node
;
5532 tree result
= objc_build_class_component_ref (id_expression
,
5534 /* Build a location of the form:
5537 with caret at the start of the component name (at
5538 input_location), ranging from the start of the id_expression
5539 to the end of the component name. */
5540 location_t combined_loc
5541 = make_location (input_location
, id_expression
.get_start (),
5542 get_finish (input_location
));
5543 protected_set_expr_location (result
, combined_loc
);
5547 /* In Objective-C++, an instance variable (ivar) may be preferred
5548 to whatever cp_parser_lookup_name() found.
5549 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5550 rest of c-family, we have to do a little extra work to preserve
5551 any location information in cp_expr "decl". Given that
5552 objc_lookup_ivar is implemented in "c-family" and "objc", we
5553 have a trip through the pure "tree" type, rather than cp_expr.
5554 Naively copying it back to "decl" would implicitly give the
5555 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5556 store an EXPR_LOCATION. Hence we only update "decl" (and
5557 hence its location_t) if we get back a different tree node. */
5558 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5560 if (decl_tree
!= decl
.get_value ())
5561 decl
= cp_expr (decl_tree
);
5563 /* If name lookup gives us a SCOPE_REF, then the
5564 qualifying scope was dependent. */
5565 if (TREE_CODE (decl
) == SCOPE_REF
)
5567 /* At this point, we do not know if DECL is a valid
5568 integral constant expression. We assume that it is
5569 in fact such an expression, so that code like:
5571 template <int N> struct A {
5575 is accepted. At template-instantiation time, we
5576 will check that B<N>::i is actually a constant. */
5579 /* Check to see if DECL is a local variable in a context
5580 where that is forbidden. */
5581 if (parser
->local_variables_forbidden_p
5582 && local_variable_p (decl
))
5584 /* It might be that we only found DECL because we are
5585 trying to be generous with pre-ISO scoping rules.
5586 For example, consider:
5590 for (int i = 0; i < 10; ++i) {}
5591 extern void f(int j = i);
5594 Here, name look up will originally find the out
5595 of scope `i'. We need to issue a warning message,
5596 but then use the global `i'. */
5597 decl
= check_for_out_of_scope_variable (decl
);
5598 if (local_variable_p (decl
))
5600 error_at (id_expr_token
->location
,
5601 "local variable %qD may not appear in this context",
5603 return error_mark_node
;
5608 decl
= (finish_id_expression
5609 (id_expression
, decl
, parser
->scope
,
5611 parser
->integral_constant_expression_p
,
5612 parser
->allow_non_integral_constant_expression_p
,
5613 &parser
->non_integral_constant_expression_p
,
5614 template_p
, done
, address_p
,
5617 id_expression
.get_location ()));
5619 cp_parser_error (parser
, error_msg
);
5620 decl
.set_location (id_expr_token
->location
);
5624 /* Anything else is an error. */
5626 cp_parser_error (parser
, "expected primary-expression");
5627 return error_mark_node
;
5631 static inline cp_expr
5632 cp_parser_primary_expression (cp_parser
*parser
,
5635 bool template_arg_p
,
5638 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5639 /*decltype*/false, idk
);
5642 /* Parse an id-expression.
5649 :: [opt] nested-name-specifier template [opt] unqualified-id
5651 :: operator-function-id
5654 Return a representation of the unqualified portion of the
5655 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5656 a `::' or nested-name-specifier.
5658 Often, if the id-expression was a qualified-id, the caller will
5659 want to make a SCOPE_REF to represent the qualified-id. This
5660 function does not do this in order to avoid wastefully creating
5661 SCOPE_REFs when they are not required.
5663 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5666 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5667 uninstantiated templates.
5669 If *TEMPLATE_P is non-NULL, it is set to true iff the
5670 `template' keyword is used to explicitly indicate that the entity
5671 named is a template.
5673 If DECLARATOR_P is true, the id-expression is appearing as part of
5674 a declarator, rather than as part of an expression. */
5677 cp_parser_id_expression (cp_parser
*parser
,
5678 bool template_keyword_p
,
5679 bool check_dependency_p
,
5684 bool global_scope_p
;
5685 bool nested_name_specifier_p
;
5687 /* Assume the `template' keyword was not used. */
5689 *template_p
= template_keyword_p
;
5691 /* Look for the optional `::' operator. */
5693 = (!template_keyword_p
5694 && (cp_parser_global_scope_opt (parser
,
5695 /*current_scope_valid_p=*/false)
5698 /* Look for the optional nested-name-specifier. */
5699 nested_name_specifier_p
5700 = (cp_parser_nested_name_specifier_opt (parser
,
5701 /*typename_keyword_p=*/false,
5708 /* If there is a nested-name-specifier, then we are looking at
5709 the first qualified-id production. */
5710 if (nested_name_specifier_p
)
5713 tree saved_object_scope
;
5714 tree saved_qualifying_scope
;
5715 cp_expr unqualified_id
;
5718 /* See if the next token is the `template' keyword. */
5720 template_p
= &is_template
;
5721 *template_p
= cp_parser_optional_template_keyword (parser
);
5722 /* Name lookup we do during the processing of the
5723 unqualified-id might obliterate SCOPE. */
5724 saved_scope
= parser
->scope
;
5725 saved_object_scope
= parser
->object_scope
;
5726 saved_qualifying_scope
= parser
->qualifying_scope
;
5727 /* Process the final unqualified-id. */
5728 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5731 /*optional_p=*/false);
5732 /* Restore the SAVED_SCOPE for our caller. */
5733 parser
->scope
= saved_scope
;
5734 parser
->object_scope
= saved_object_scope
;
5735 parser
->qualifying_scope
= saved_qualifying_scope
;
5737 return unqualified_id
;
5739 /* Otherwise, if we are in global scope, then we are looking at one
5740 of the other qualified-id productions. */
5741 else if (global_scope_p
)
5746 /* Peek at the next token. */
5747 token
= cp_lexer_peek_token (parser
->lexer
);
5749 /* If it's an identifier, and the next token is not a "<", then
5750 we can avoid the template-id case. This is an optimization
5751 for this common case. */
5752 if (token
->type
== CPP_NAME
5753 && !cp_parser_nth_token_starts_template_argument_list_p
5755 return cp_parser_identifier (parser
);
5757 cp_parser_parse_tentatively (parser
);
5758 /* Try a template-id. */
5759 id
= cp_parser_template_id (parser
,
5760 /*template_keyword_p=*/false,
5761 /*check_dependency_p=*/true,
5764 /* If that worked, we're done. */
5765 if (cp_parser_parse_definitely (parser
))
5768 /* Peek at the next token. (Changes in the token buffer may
5769 have invalidated the pointer obtained above.) */
5770 token
= cp_lexer_peek_token (parser
->lexer
);
5772 switch (token
->type
)
5775 return cp_parser_identifier (parser
);
5778 if (token
->keyword
== RID_OPERATOR
)
5779 return cp_parser_operator_function_id (parser
);
5783 cp_parser_error (parser
, "expected id-expression");
5784 return error_mark_node
;
5788 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5789 /*check_dependency_p=*/true,
5794 /* Parse an unqualified-id.
5798 operator-function-id
5799 conversion-function-id
5803 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5804 keyword, in a construct like `A::template ...'.
5806 Returns a representation of unqualified-id. For the `identifier'
5807 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5808 production a BIT_NOT_EXPR is returned; the operand of the
5809 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5810 other productions, see the documentation accompanying the
5811 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5812 names are looked up in uninstantiated templates. If DECLARATOR_P
5813 is true, the unqualified-id is appearing as part of a declarator,
5814 rather than as part of an expression. */
5817 cp_parser_unqualified_id (cp_parser
* parser
,
5818 bool template_keyword_p
,
5819 bool check_dependency_p
,
5825 /* Peek at the next token. */
5826 token
= cp_lexer_peek_token (parser
->lexer
);
5828 switch ((int) token
->type
)
5834 /* We don't know yet whether or not this will be a
5836 cp_parser_parse_tentatively (parser
);
5837 /* Try a template-id. */
5838 id
= cp_parser_template_id (parser
, template_keyword_p
,
5842 /* If it worked, we're done. */
5843 if (cp_parser_parse_definitely (parser
))
5845 /* Otherwise, it's an ordinary identifier. */
5846 return cp_parser_identifier (parser
);
5849 case CPP_TEMPLATE_ID
:
5850 return cp_parser_template_id (parser
, template_keyword_p
,
5858 tree qualifying_scope
;
5863 /* Consume the `~' token. */
5864 cp_lexer_consume_token (parser
->lexer
);
5865 /* Parse the class-name. The standard, as written, seems to
5868 template <typename T> struct S { ~S (); };
5869 template <typename T> S<T>::~S() {}
5871 is invalid, since `~' must be followed by a class-name, but
5872 `S<T>' is dependent, and so not known to be a class.
5873 That's not right; we need to look in uninstantiated
5874 templates. A further complication arises from:
5876 template <typename T> void f(T t) {
5880 Here, it is not possible to look up `T' in the scope of `T'
5881 itself. We must look in both the current scope, and the
5882 scope of the containing complete expression.
5884 Yet another issue is:
5893 The standard does not seem to say that the `S' in `~S'
5894 should refer to the type `S' and not the data member
5897 /* DR 244 says that we look up the name after the "~" in the
5898 same scope as we looked up the qualifying name. That idea
5899 isn't fully worked out; it's more complicated than that. */
5900 scope
= parser
->scope
;
5901 object_scope
= parser
->object_scope
;
5902 qualifying_scope
= parser
->qualifying_scope
;
5904 /* Check for invalid scopes. */
5905 if (scope
== error_mark_node
)
5907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5908 cp_lexer_consume_token (parser
->lexer
);
5909 return error_mark_node
;
5911 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5913 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5914 error_at (token
->location
,
5915 "scope %qT before %<~%> is not a class-name",
5917 cp_parser_simulate_error (parser
);
5918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5919 cp_lexer_consume_token (parser
->lexer
);
5920 return error_mark_node
;
5922 gcc_assert (!scope
|| TYPE_P (scope
));
5924 /* If the name is of the form "X::~X" it's OK even if X is a
5926 token
= cp_lexer_peek_token (parser
->lexer
);
5928 && token
->type
== CPP_NAME
5929 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5931 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5932 || (CLASS_TYPE_P (scope
)
5933 && constructor_name_p (token
->u
.value
, scope
))))
5935 cp_lexer_consume_token (parser
->lexer
);
5936 return build_nt (BIT_NOT_EXPR
, scope
);
5939 /* ~auto means the destructor of whatever the object is. */
5940 if (cp_parser_is_keyword (token
, RID_AUTO
))
5942 if (cxx_dialect
< cxx14
)
5943 pedwarn (input_location
, 0,
5944 "%<~auto%> only available with "
5945 "-std=c++14 or -std=gnu++14");
5946 cp_lexer_consume_token (parser
->lexer
);
5947 return build_nt (BIT_NOT_EXPR
, make_auto ());
5950 /* If there was an explicit qualification (S::~T), first look
5951 in the scope given by the qualification (i.e., S).
5953 Note: in the calls to cp_parser_class_name below we pass
5954 typename_type so that lookup finds the injected-class-name
5955 rather than the constructor. */
5957 type_decl
= NULL_TREE
;
5960 cp_parser_parse_tentatively (parser
);
5961 type_decl
= cp_parser_class_name (parser
,
5962 /*typename_keyword_p=*/false,
5963 /*template_keyword_p=*/false,
5965 /*check_dependency=*/false,
5966 /*class_head_p=*/false,
5968 if (cp_parser_parse_definitely (parser
))
5971 /* In "N::S::~S", look in "N" as well. */
5972 if (!done
&& scope
&& qualifying_scope
)
5974 cp_parser_parse_tentatively (parser
);
5975 parser
->scope
= qualifying_scope
;
5976 parser
->object_scope
= NULL_TREE
;
5977 parser
->qualifying_scope
= NULL_TREE
;
5979 = cp_parser_class_name (parser
,
5980 /*typename_keyword_p=*/false,
5981 /*template_keyword_p=*/false,
5983 /*check_dependency=*/false,
5984 /*class_head_p=*/false,
5986 if (cp_parser_parse_definitely (parser
))
5989 /* In "p->S::~T", look in the scope given by "*p" as well. */
5990 else if (!done
&& object_scope
)
5992 cp_parser_parse_tentatively (parser
);
5993 parser
->scope
= object_scope
;
5994 parser
->object_scope
= NULL_TREE
;
5995 parser
->qualifying_scope
= NULL_TREE
;
5997 = cp_parser_class_name (parser
,
5998 /*typename_keyword_p=*/false,
5999 /*template_keyword_p=*/false,
6001 /*check_dependency=*/false,
6002 /*class_head_p=*/false,
6004 if (cp_parser_parse_definitely (parser
))
6007 /* Look in the surrounding context. */
6010 parser
->scope
= NULL_TREE
;
6011 parser
->object_scope
= NULL_TREE
;
6012 parser
->qualifying_scope
= NULL_TREE
;
6013 if (processing_template_decl
)
6014 cp_parser_parse_tentatively (parser
);
6016 = cp_parser_class_name (parser
,
6017 /*typename_keyword_p=*/false,
6018 /*template_keyword_p=*/false,
6020 /*check_dependency=*/false,
6021 /*class_head_p=*/false,
6023 if (processing_template_decl
6024 && ! cp_parser_parse_definitely (parser
))
6026 /* We couldn't find a type with this name. If we're parsing
6027 tentatively, fail and try something else. */
6028 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6030 cp_parser_simulate_error (parser
);
6031 return error_mark_node
;
6033 /* Otherwise, accept it and check for a match at instantiation
6035 type_decl
= cp_parser_identifier (parser
);
6036 if (type_decl
!= error_mark_node
)
6037 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6041 /* If an error occurred, assume that the name of the
6042 destructor is the same as the name of the qualifying
6043 class. That allows us to keep parsing after running
6044 into ill-formed destructor names. */
6045 if (type_decl
== error_mark_node
&& scope
)
6046 return build_nt (BIT_NOT_EXPR
, scope
);
6047 else if (type_decl
== error_mark_node
)
6048 return error_mark_node
;
6050 /* Check that destructor name and scope match. */
6051 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6054 error_at (token
->location
,
6055 "declaration of %<~%T%> as member of %qT",
6057 cp_parser_simulate_error (parser
);
6058 return error_mark_node
;
6063 A typedef-name that names a class shall not be used as the
6064 identifier in the declarator for a destructor declaration. */
6066 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6067 && !DECL_SELF_REFERENCE_P (type_decl
)
6068 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6069 error_at (token
->location
,
6070 "typedef-name %qD used as destructor declarator",
6073 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6077 if (token
->keyword
== RID_OPERATOR
)
6081 /* This could be a template-id, so we try that first. */
6082 cp_parser_parse_tentatively (parser
);
6083 /* Try a template-id. */
6084 id
= cp_parser_template_id (parser
, template_keyword_p
,
6085 /*check_dependency_p=*/true,
6088 /* If that worked, we're done. */
6089 if (cp_parser_parse_definitely (parser
))
6091 /* We still don't know whether we're looking at an
6092 operator-function-id or a conversion-function-id. */
6093 cp_parser_parse_tentatively (parser
);
6094 /* Try an operator-function-id. */
6095 id
= cp_parser_operator_function_id (parser
);
6096 /* If that didn't work, try a conversion-function-id. */
6097 if (!cp_parser_parse_definitely (parser
))
6098 id
= cp_parser_conversion_function_id (parser
);
6099 else if (UDLIT_OPER_P (id
))
6102 const char *name
= UDLIT_OP_SUFFIX (id
);
6103 if (name
[0] != '_' && !in_system_header_at (input_location
)
6105 warning (OPT_Wliteral_suffix
,
6106 "literal operator suffixes not preceded by %<_%>"
6107 " are reserved for future standardization");
6117 cp_parser_error (parser
, "expected unqualified-id");
6118 return error_mark_node
;
6122 /* Parse an (optional) nested-name-specifier.
6124 nested-name-specifier: [C++98]
6125 class-or-namespace-name :: nested-name-specifier [opt]
6126 class-or-namespace-name :: template nested-name-specifier [opt]
6128 nested-name-specifier: [C++0x]
6131 nested-name-specifier identifier ::
6132 nested-name-specifier template [opt] simple-template-id ::
6134 PARSER->SCOPE should be set appropriately before this function is
6135 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6136 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6139 Sets PARSER->SCOPE to the class (TYPE) or namespace
6140 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6141 it unchanged if there is no nested-name-specifier. Returns the new
6142 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6144 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6145 part of a declaration and/or decl-specifier. */
6148 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6149 bool typename_keyword_p
,
6150 bool check_dependency_p
,
6152 bool is_declaration
,
6153 bool template_keyword_p
/* = false */)
6155 bool success
= false;
6156 cp_token_position start
= 0;
6159 /* Remember where the nested-name-specifier starts. */
6160 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6162 start
= cp_lexer_token_position (parser
->lexer
, false);
6163 push_deferring_access_checks (dk_deferred
);
6170 tree saved_qualifying_scope
;
6172 /* Spot cases that cannot be the beginning of a
6173 nested-name-specifier. */
6174 token
= cp_lexer_peek_token (parser
->lexer
);
6176 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6177 the already parsed nested-name-specifier. */
6178 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6180 /* Grab the nested-name-specifier and continue the loop. */
6181 cp_parser_pre_parsed_nested_name_specifier (parser
);
6182 /* If we originally encountered this nested-name-specifier
6183 with IS_DECLARATION set to false, we will not have
6184 resolved TYPENAME_TYPEs, so we must do so here. */
6186 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6188 new_scope
= resolve_typename_type (parser
->scope
,
6189 /*only_current_p=*/false);
6190 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6191 parser
->scope
= new_scope
;
6197 /* Spot cases that cannot be the beginning of a
6198 nested-name-specifier. On the second and subsequent times
6199 through the loop, we look for the `template' keyword. */
6200 if (success
&& token
->keyword
== RID_TEMPLATE
)
6202 /* A template-id can start a nested-name-specifier. */
6203 else if (token
->type
== CPP_TEMPLATE_ID
)
6205 /* DR 743: decltype can be used in a nested-name-specifier. */
6206 else if (token_is_decltype (token
))
6210 /* If the next token is not an identifier, then it is
6211 definitely not a type-name or namespace-name. */
6212 if (token
->type
!= CPP_NAME
)
6214 /* If the following token is neither a `<' (to begin a
6215 template-id), nor a `::', then we are not looking at a
6216 nested-name-specifier. */
6217 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6219 if (token
->type
== CPP_COLON
6220 && parser
->colon_corrects_to_scope_p
6221 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6223 gcc_rich_location
richloc (token
->location
);
6224 richloc
.add_fixit_replace ("::");
6226 "found %<:%> in nested-name-specifier, "
6228 token
->type
= CPP_SCOPE
;
6231 if (token
->type
!= CPP_SCOPE
6232 && !cp_parser_nth_token_starts_template_argument_list_p
6237 /* The nested-name-specifier is optional, so we parse
6239 cp_parser_parse_tentatively (parser
);
6241 /* Look for the optional `template' keyword, if this isn't the
6242 first time through the loop. */
6244 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6246 /* Save the old scope since the name lookup we are about to do
6247 might destroy it. */
6248 old_scope
= parser
->scope
;
6249 saved_qualifying_scope
= parser
->qualifying_scope
;
6250 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6251 look up names in "X<T>::I" in order to determine that "Y" is
6252 a template. So, if we have a typename at this point, we make
6253 an effort to look through it. */
6255 && !typename_keyword_p
6257 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6258 parser
->scope
= resolve_typename_type (parser
->scope
,
6259 /*only_current_p=*/false);
6260 /* Parse the qualifying entity. */
6262 = cp_parser_qualifying_entity (parser
,
6268 /* Look for the `::' token. */
6269 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6271 /* If we found what we wanted, we keep going; otherwise, we're
6273 if (!cp_parser_parse_definitely (parser
))
6275 bool error_p
= false;
6277 /* Restore the OLD_SCOPE since it was valid before the
6278 failed attempt at finding the last
6279 class-or-namespace-name. */
6280 parser
->scope
= old_scope
;
6281 parser
->qualifying_scope
= saved_qualifying_scope
;
6283 /* If the next token is a decltype, and the one after that is a
6284 `::', then the decltype has failed to resolve to a class or
6285 enumeration type. Give this error even when parsing
6286 tentatively since it can't possibly be valid--and we're going
6287 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6288 won't get another chance.*/
6289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6290 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6293 token
= cp_lexer_consume_token (parser
->lexer
);
6294 error_at (token
->location
, "decltype evaluates to %qT, "
6295 "which is not a class or enumeration type",
6296 token
->u
.tree_check_value
->value
);
6297 parser
->scope
= error_mark_node
;
6301 cp_lexer_consume_token (parser
->lexer
);
6304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6305 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6307 /* If we have a non-type template-id followed by ::, it can't
6308 possibly be valid. */
6309 token
= cp_lexer_peek_token (parser
->lexer
);
6310 tree tid
= token
->u
.tree_check_value
->value
;
6311 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6312 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6314 tree tmpl
= NULL_TREE
;
6315 if (is_overloaded_fn (tid
))
6317 tree fns
= get_fns (tid
);
6318 if (OVL_SINGLE_P (fns
))
6319 tmpl
= OVL_FIRST (fns
);
6320 error_at (token
->location
, "function template-id %qD "
6321 "in nested-name-specifier", tid
);
6325 /* Variable template. */
6326 tmpl
= TREE_OPERAND (tid
, 0);
6327 gcc_assert (variable_template_p (tmpl
));
6328 error_at (token
->location
, "variable template-id %qD "
6329 "in nested-name-specifier", tid
);
6332 inform (DECL_SOURCE_LOCATION (tmpl
),
6333 "%qD declared here", tmpl
);
6335 parser
->scope
= error_mark_node
;
6339 cp_lexer_consume_token (parser
->lexer
);
6340 cp_lexer_consume_token (parser
->lexer
);
6344 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6346 /* If the next token is an identifier, and the one after
6347 that is a `::', then any valid interpretation would have
6348 found a class-or-namespace-name. */
6349 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6350 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6352 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6355 token
= cp_lexer_consume_token (parser
->lexer
);
6358 if (!token
->error_reported
)
6361 tree ambiguous_decls
;
6363 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6365 /*is_template=*/false,
6366 /*is_namespace=*/false,
6367 /*check_dependency=*/true,
6370 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6371 error_at (token
->location
,
6372 "%qD used without template parameters",
6374 else if (ambiguous_decls
)
6376 // cp_parser_lookup_name has the same diagnostic,
6377 // thus make sure to emit it at most once.
6378 if (cp_parser_uncommitted_to_tentative_parse_p
6381 error_at (token
->location
,
6382 "reference to %qD is ambiguous",
6384 print_candidates (ambiguous_decls
);
6386 decl
= error_mark_node
;
6390 if (cxx_dialect
!= cxx98
)
6391 cp_parser_name_lookup_error
6392 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6395 cp_parser_name_lookup_error
6396 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6400 parser
->scope
= error_mark_node
;
6402 /* Treat this as a successful nested-name-specifier
6407 If the name found is not a class-name (clause
6408 _class_) or namespace-name (_namespace.def_), the
6409 program is ill-formed. */
6412 cp_lexer_consume_token (parser
->lexer
);
6416 /* We've found one valid nested-name-specifier. */
6418 /* Name lookup always gives us a DECL. */
6419 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6420 new_scope
= TREE_TYPE (new_scope
);
6421 /* Uses of "template" must be followed by actual templates. */
6422 if (template_keyword_p
6423 && !(CLASS_TYPE_P (new_scope
)
6424 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6425 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6426 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6427 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6428 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6429 == TEMPLATE_ID_EXPR
)))
6430 permerror (input_location
, TYPE_P (new_scope
)
6431 ? G_("%qT is not a template")
6432 : G_("%qD is not a template"),
6434 /* If it is a class scope, try to complete it; we are about to
6435 be looking up names inside the class. */
6436 if (TYPE_P (new_scope
)
6437 /* Since checking types for dependency can be expensive,
6438 avoid doing it if the type is already complete. */
6439 && !COMPLETE_TYPE_P (new_scope
)
6440 /* Do not try to complete dependent types. */
6441 && !dependent_type_p (new_scope
))
6443 new_scope
= complete_type (new_scope
);
6444 /* If it is a typedef to current class, use the current
6445 class instead, as the typedef won't have any names inside
6447 if (!COMPLETE_TYPE_P (new_scope
)
6448 && currently_open_class (new_scope
))
6449 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6451 /* Make sure we look in the right scope the next time through
6453 parser
->scope
= new_scope
;
6456 /* If parsing tentatively, replace the sequence of tokens that makes
6457 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6458 token. That way, should we re-parse the token stream, we will
6459 not have to repeat the effort required to do the parse, nor will
6460 we issue duplicate error messages. */
6461 if (success
&& start
)
6465 token
= cp_lexer_token_at (parser
->lexer
, start
);
6466 /* Reset the contents of the START token. */
6467 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6468 /* Retrieve any deferred checks. Do not pop this access checks yet
6469 so the memory will not be reclaimed during token replacing below. */
6470 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6471 token
->u
.tree_check_value
->value
= parser
->scope
;
6472 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6473 token
->u
.tree_check_value
->qualifying_scope
=
6474 parser
->qualifying_scope
;
6475 token
->keyword
= RID_MAX
;
6477 /* Purge all subsequent tokens. */
6478 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6482 pop_to_parent_deferring_access_checks ();
6484 return success
? parser
->scope
: NULL_TREE
;
6487 /* Parse a nested-name-specifier. See
6488 cp_parser_nested_name_specifier_opt for details. This function
6489 behaves identically, except that it will an issue an error if no
6490 nested-name-specifier is present. */
6493 cp_parser_nested_name_specifier (cp_parser
*parser
,
6494 bool typename_keyword_p
,
6495 bool check_dependency_p
,
6497 bool is_declaration
)
6501 /* Look for the nested-name-specifier. */
6502 scope
= cp_parser_nested_name_specifier_opt (parser
,
6507 /* If it was not present, issue an error message. */
6510 cp_parser_error (parser
, "expected nested-name-specifier");
6511 parser
->scope
= NULL_TREE
;
6517 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6518 this is either a class-name or a namespace-name (which corresponds
6519 to the class-or-namespace-name production in the grammar). For
6520 C++0x, it can also be a type-name that refers to an enumeration
6521 type or a simple-template-id.
6523 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6524 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6525 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6526 TYPE_P is TRUE iff the next name should be taken as a class-name,
6527 even the same name is declared to be another entity in the same
6530 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6531 specified by the class-or-namespace-name. If neither is found the
6532 ERROR_MARK_NODE is returned. */
6535 cp_parser_qualifying_entity (cp_parser
*parser
,
6536 bool typename_keyword_p
,
6537 bool template_keyword_p
,
6538 bool check_dependency_p
,
6540 bool is_declaration
)
6543 tree saved_qualifying_scope
;
6544 tree saved_object_scope
;
6547 bool successful_parse_p
;
6549 /* DR 743: decltype can appear in a nested-name-specifier. */
6550 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6552 scope
= cp_parser_decltype (parser
);
6553 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6554 && !MAYBE_CLASS_TYPE_P (scope
))
6556 cp_parser_simulate_error (parser
);
6557 return error_mark_node
;
6559 if (TYPE_NAME (scope
))
6560 scope
= TYPE_NAME (scope
);
6564 /* Before we try to parse the class-name, we must save away the
6565 current PARSER->SCOPE since cp_parser_class_name will destroy
6567 saved_scope
= parser
->scope
;
6568 saved_qualifying_scope
= parser
->qualifying_scope
;
6569 saved_object_scope
= parser
->object_scope
;
6570 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6571 there is no need to look for a namespace-name. */
6572 only_class_p
= template_keyword_p
6573 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6575 cp_parser_parse_tentatively (parser
);
6576 scope
= cp_parser_class_name (parser
,
6579 type_p
? class_type
: none_type
,
6581 /*class_head_p=*/false,
6583 /*enum_ok=*/cxx_dialect
> cxx98
);
6584 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6585 /* If that didn't work, try for a namespace-name. */
6586 if (!only_class_p
&& !successful_parse_p
)
6588 /* Restore the saved scope. */
6589 parser
->scope
= saved_scope
;
6590 parser
->qualifying_scope
= saved_qualifying_scope
;
6591 parser
->object_scope
= saved_object_scope
;
6592 /* If we are not looking at an identifier followed by the scope
6593 resolution operator, then this is not part of a
6594 nested-name-specifier. (Note that this function is only used
6595 to parse the components of a nested-name-specifier.) */
6596 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6597 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6598 return error_mark_node
;
6599 scope
= cp_parser_namespace_name (parser
);
6605 /* Return true if we are looking at a compound-literal, false otherwise. */
6608 cp_parser_compound_literal_p (cp_parser
*parser
)
6610 cp_lexer_save_tokens (parser
->lexer
);
6612 /* Skip tokens until the next token is a closing parenthesis.
6613 If we find the closing `)', and the next token is a `{', then
6614 we are looking at a compound-literal. */
6615 bool compound_literal_p
6616 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6617 /*consume_paren=*/true)
6618 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6620 /* Roll back the tokens we skipped. */
6621 cp_lexer_rollback_tokens (parser
->lexer
);
6623 return compound_literal_p
;
6626 /* Return true if EXPR is the integer constant zero or a complex constant
6627 of zero, without any folding, but ignoring location wrappers. */
6630 literal_integer_zerop (const_tree expr
)
6632 STRIP_ANY_LOCATION_WRAPPER (expr
);
6633 return integer_zerop (expr
);
6636 /* Parse a postfix-expression.
6640 postfix-expression [ expression ]
6641 postfix-expression ( expression-list [opt] )
6642 simple-type-specifier ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier identifier
6644 ( expression-list [opt] )
6645 typename :: [opt] nested-name-specifier template [opt] template-id
6646 ( expression-list [opt] )
6647 postfix-expression . template [opt] id-expression
6648 postfix-expression -> template [opt] id-expression
6649 postfix-expression . pseudo-destructor-name
6650 postfix-expression -> pseudo-destructor-name
6651 postfix-expression ++
6652 postfix-expression --
6653 dynamic_cast < type-id > ( expression )
6654 static_cast < type-id > ( expression )
6655 reinterpret_cast < type-id > ( expression )
6656 const_cast < type-id > ( expression )
6657 typeid ( expression )
6663 ( type-id ) { initializer-list , [opt] }
6665 This extension is a GNU version of the C99 compound-literal
6666 construct. (The C99 grammar uses `type-name' instead of `type-id',
6667 but they are essentially the same concept.)
6669 If ADDRESS_P is true, the postfix expression is the operand of the
6670 `&' operator. CAST_P is true if this expression is the target of a
6673 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6674 class member access expressions [expr.ref].
6676 Returns a representation of the expression. */
6679 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6680 bool member_access_only_p
, bool decltype_p
,
6681 cp_id_kind
* pidk_return
)
6686 cp_id_kind idk
= CP_ID_KIND_NONE
;
6687 cp_expr postfix_expression
= NULL_TREE
;
6688 bool is_member_access
= false;
6690 /* Peek at the next token. */
6691 token
= cp_lexer_peek_token (parser
->lexer
);
6692 loc
= token
->location
;
6693 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6695 /* Some of the productions are determined by keywords. */
6696 keyword
= token
->keyword
;
6706 const char *saved_message
;
6707 bool saved_in_type_id_in_expr_p
;
6709 /* All of these can be handled in the same way from the point
6710 of view of parsing. Begin by consuming the token
6711 identifying the cast. */
6712 cp_lexer_consume_token (parser
->lexer
);
6714 /* New types cannot be defined in the cast. */
6715 saved_message
= parser
->type_definition_forbidden_message
;
6716 parser
->type_definition_forbidden_message
6717 = G_("types may not be defined in casts");
6719 /* Look for the opening `<'. */
6720 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6721 /* Parse the type to which we are casting. */
6722 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6723 parser
->in_type_id_in_expr_p
= true;
6724 type
= cp_parser_type_id (parser
);
6725 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6726 /* Look for the closing `>'. */
6727 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6728 /* Restore the old message. */
6729 parser
->type_definition_forbidden_message
= saved_message
;
6731 bool saved_greater_than_is_operator_p
6732 = parser
->greater_than_is_operator_p
;
6733 parser
->greater_than_is_operator_p
= true;
6735 /* And the expression which is being cast. */
6736 matching_parens parens
;
6737 parens
.require_open (parser
);
6738 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6739 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6741 location_t end_loc
= close_paren
?
6742 close_paren
->location
: UNKNOWN_LOCATION
;
6744 parser
->greater_than_is_operator_p
6745 = saved_greater_than_is_operator_p
;
6747 /* Only type conversions to integral or enumeration types
6748 can be used in constant-expressions. */
6749 if (!cast_valid_in_integral_constant_expression_p (type
)
6750 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6752 postfix_expression
= error_mark_node
;
6760 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6764 = build_static_cast (type
, expression
, tf_warning_or_error
);
6768 = build_reinterpret_cast (type
, expression
,
6769 tf_warning_or_error
);
6773 = build_const_cast (type
, expression
, tf_warning_or_error
);
6779 /* Construct a location e.g. :
6780 reinterpret_cast <int *> (expr)
6781 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6782 ranging from the start of the "*_cast" token to the final closing
6783 paren, with the caret at the start. */
6784 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6785 postfix_expression
.set_location (cp_cast_loc
);
6792 const char *saved_message
;
6793 bool saved_in_type_id_in_expr_p
;
6795 /* Consume the `typeid' token. */
6796 cp_lexer_consume_token (parser
->lexer
);
6797 /* Look for the `(' token. */
6798 matching_parens parens
;
6799 parens
.require_open (parser
);
6800 /* Types cannot be defined in a `typeid' expression. */
6801 saved_message
= parser
->type_definition_forbidden_message
;
6802 parser
->type_definition_forbidden_message
6803 = G_("types may not be defined in a %<typeid%> expression");
6804 /* We can't be sure yet whether we're looking at a type-id or an
6806 cp_parser_parse_tentatively (parser
);
6807 /* Try a type-id first. */
6808 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6809 parser
->in_type_id_in_expr_p
= true;
6810 type
= cp_parser_type_id (parser
);
6811 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6812 /* Look for the `)' token. Otherwise, we can't be sure that
6813 we're not looking at an expression: consider `typeid (int
6814 (3))', for example. */
6815 cp_token
*close_paren
= parens
.require_close (parser
);
6816 /* If all went well, simply lookup the type-id. */
6817 if (cp_parser_parse_definitely (parser
))
6818 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6819 /* Otherwise, fall back to the expression variant. */
6824 /* Look for an expression. */
6825 expression
= cp_parser_expression (parser
, & idk
);
6826 /* Compute its typeid. */
6827 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6828 /* Look for the `)' token. */
6829 close_paren
= parens
.require_close (parser
);
6831 /* Restore the saved message. */
6832 parser
->type_definition_forbidden_message
= saved_message
;
6833 /* `typeid' may not appear in an integral constant expression. */
6834 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6835 postfix_expression
= error_mark_node
;
6837 /* Construct a location e.g. :
6840 ranging from the start of the "typeid" token to the final closing
6841 paren, with the caret at the start. */
6844 location_t typeid_loc
6845 = make_location (start_loc
, start_loc
, close_paren
->location
);
6846 postfix_expression
.set_location (typeid_loc
);
6847 postfix_expression
.maybe_add_location_wrapper ();
6855 /* The syntax permitted here is the same permitted for an
6856 elaborated-type-specifier. */
6857 ++parser
->prevent_constrained_type_specifiers
;
6858 type
= cp_parser_elaborated_type_specifier (parser
,
6859 /*is_friend=*/false,
6860 /*is_declaration=*/false);
6861 --parser
->prevent_constrained_type_specifiers
;
6862 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6867 case RID_BUILTIN_SHUFFLE
:
6868 case RID_BUILTIN_LAUNDER
:
6870 vec
<tree
, va_gc
> *vec
;
6874 cp_lexer_consume_token (parser
->lexer
);
6875 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6876 /*cast_p=*/false, /*allow_expansion_p=*/true,
6877 /*non_constant_p=*/NULL
);
6880 postfix_expression
= error_mark_node
;
6884 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6890 if (vec
->length () == 1)
6892 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6895 error_at (loc
, "wrong number of arguments to "
6896 "%<__builtin_addressof%>");
6897 postfix_expression
= error_mark_node
;
6901 case RID_BUILTIN_LAUNDER
:
6902 if (vec
->length () == 1)
6903 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6904 tf_warning_or_error
);
6907 error_at (loc
, "wrong number of arguments to "
6908 "%<__builtin_launder%>");
6909 postfix_expression
= error_mark_node
;
6913 case RID_BUILTIN_SHUFFLE
:
6914 if (vec
->length () == 2)
6916 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6917 (*vec
)[1], tf_warning_or_error
);
6918 else if (vec
->length () == 3)
6920 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6921 (*vec
)[2], tf_warning_or_error
);
6924 error_at (loc
, "wrong number of arguments to "
6925 "%<__builtin_shuffle%>");
6926 postfix_expression
= error_mark_node
;
6940 /* If the next thing is a simple-type-specifier, we may be
6941 looking at a functional cast. We could also be looking at
6942 an id-expression. So, we try the functional cast, and if
6943 that doesn't work we fall back to the primary-expression. */
6944 cp_parser_parse_tentatively (parser
);
6945 /* Look for the simple-type-specifier. */
6946 ++parser
->prevent_constrained_type_specifiers
;
6947 type
= cp_parser_simple_type_specifier (parser
,
6948 /*decl_specs=*/NULL
,
6949 CP_PARSER_FLAGS_NONE
);
6950 --parser
->prevent_constrained_type_specifiers
;
6951 /* Parse the cast itself. */
6952 if (!cp_parser_error_occurred (parser
))
6954 = cp_parser_functional_cast (parser
, type
);
6955 /* If that worked, we're done. */
6956 if (cp_parser_parse_definitely (parser
))
6959 /* If the functional-cast didn't work out, try a
6960 compound-literal. */
6961 if (cp_parser_allow_gnu_extensions_p (parser
)
6962 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6964 cp_expr initializer
= NULL_TREE
;
6966 cp_parser_parse_tentatively (parser
);
6968 matching_parens parens
;
6969 parens
.consume_open (parser
);
6971 /* Avoid calling cp_parser_type_id pointlessly, see comment
6972 in cp_parser_cast_expression about c++/29234. */
6973 if (!cp_parser_compound_literal_p (parser
))
6974 cp_parser_simulate_error (parser
);
6977 /* Parse the type. */
6978 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6979 parser
->in_type_id_in_expr_p
= true;
6980 type
= cp_parser_type_id (parser
);
6981 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6982 parens
.require_close (parser
);
6985 /* If things aren't going well, there's no need to
6987 if (!cp_parser_error_occurred (parser
))
6989 bool non_constant_p
;
6990 /* Parse the brace-enclosed initializer list. */
6991 initializer
= cp_parser_braced_list (parser
,
6994 /* If that worked, we're definitely looking at a
6995 compound-literal expression. */
6996 if (cp_parser_parse_definitely (parser
))
6998 /* Warn the user that a compound literal is not
6999 allowed in standard C++. */
7000 pedwarn (input_location
, OPT_Wpedantic
,
7001 "ISO C++ forbids compound-literals");
7002 /* For simplicity, we disallow compound literals in
7003 constant-expressions. We could
7004 allow compound literals of integer type, whose
7005 initializer was a constant, in constant
7006 expressions. Permitting that usage, as a further
7007 extension, would not change the meaning of any
7008 currently accepted programs. (Of course, as
7009 compound literals are not part of ISO C++, the
7010 standard has nothing to say.) */
7011 if (cp_parser_non_integral_constant_expression (parser
,
7014 postfix_expression
= error_mark_node
;
7017 /* Form the representation of the compound-literal. */
7019 = finish_compound_literal (type
, initializer
,
7020 tf_warning_or_error
, fcl_c99
);
7021 postfix_expression
.set_location (initializer
.get_location ());
7026 /* It must be a primary-expression. */
7028 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7029 /*template_arg_p=*/false,
7036 /* Note that we don't need to worry about calling build_cplus_new on a
7037 class-valued CALL_EXPR in decltype when it isn't the end of the
7038 postfix-expression; unary_complex_lvalue will take care of that for
7041 /* Keep looping until the postfix-expression is complete. */
7044 if (idk
== CP_ID_KIND_UNQUALIFIED
7045 && identifier_p (postfix_expression
)
7046 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7047 /* It is not a Koenig lookup function call. */
7049 = unqualified_name_lookup_error (postfix_expression
);
7051 /* Peek at the next token. */
7052 token
= cp_lexer_peek_token (parser
->lexer
);
7054 switch (token
->type
)
7056 case CPP_OPEN_SQUARE
:
7057 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7059 cp_parser_error (parser
,
7060 "two consecutive %<[%> shall "
7061 "only introduce an attribute");
7062 return error_mark_node
;
7065 = cp_parser_postfix_open_square_expression (parser
,
7069 postfix_expression
.set_range (start_loc
,
7070 postfix_expression
.get_location ());
7072 idk
= CP_ID_KIND_NONE
;
7073 is_member_access
= false;
7076 case CPP_OPEN_PAREN
:
7077 /* postfix-expression ( expression-list [opt] ) */
7080 bool is_builtin_constant_p
;
7081 bool saved_integral_constant_expression_p
= false;
7082 bool saved_non_integral_constant_expression_p
= false;
7083 tsubst_flags_t complain
= complain_flags (decltype_p
);
7084 vec
<tree
, va_gc
> *args
;
7085 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7087 is_member_access
= false;
7089 is_builtin_constant_p
7090 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7091 if (is_builtin_constant_p
)
7093 /* The whole point of __builtin_constant_p is to allow
7094 non-constant expressions to appear as arguments. */
7095 saved_integral_constant_expression_p
7096 = parser
->integral_constant_expression_p
;
7097 saved_non_integral_constant_expression_p
7098 = parser
->non_integral_constant_expression_p
;
7099 parser
->integral_constant_expression_p
= false;
7101 args
= (cp_parser_parenthesized_expression_list
7103 /*cast_p=*/false, /*allow_expansion_p=*/true,
7104 /*non_constant_p=*/NULL
,
7105 /*close_paren_loc=*/&close_paren_loc
,
7106 /*wrap_locations_p=*/true));
7107 if (is_builtin_constant_p
)
7109 parser
->integral_constant_expression_p
7110 = saved_integral_constant_expression_p
;
7111 parser
->non_integral_constant_expression_p
7112 = saved_non_integral_constant_expression_p
;
7117 postfix_expression
= error_mark_node
;
7121 /* Function calls are not permitted in
7122 constant-expressions. */
7123 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7124 && cp_parser_non_integral_constant_expression (parser
,
7127 postfix_expression
= error_mark_node
;
7128 release_tree_vector (args
);
7133 if (idk
== CP_ID_KIND_UNQUALIFIED
7134 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7136 if (identifier_p (postfix_expression
))
7138 if (!args
->is_empty ())
7141 if (!any_type_dependent_arguments_p (args
))
7143 = perform_koenig_lookup (postfix_expression
, args
,
7148 = unqualified_fn_lookup_error (postfix_expression
);
7150 /* We do not perform argument-dependent lookup if
7151 normal lookup finds a non-function, in accordance
7152 with the expected resolution of DR 218. */
7153 else if (!args
->is_empty ()
7154 && is_overloaded_fn (postfix_expression
))
7156 tree fn
= get_first_fn (postfix_expression
);
7157 fn
= STRIP_TEMPLATE (fn
);
7159 /* Do not do argument dependent lookup if regular
7160 lookup finds a member function or a block-scope
7161 function declaration. [basic.lookup.argdep]/3 */
7162 if (!DECL_FUNCTION_MEMBER_P (fn
)
7163 && !DECL_LOCAL_FUNCTION_P (fn
))
7166 if (!any_type_dependent_arguments_p (args
))
7168 = perform_koenig_lookup (postfix_expression
, args
,
7174 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7175 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7176 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7177 && vec_safe_length (args
) == 3)
7179 tree arg0
= (*args
)[0];
7180 tree arg1
= (*args
)[1];
7181 tree arg2
= (*args
)[2];
7182 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7183 | (literal_integer_zerop (arg2
) << 2));
7184 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7187 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7189 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7190 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7192 if (processing_template_decl
7193 && (type_dependent_object_expression_p (instance
)
7194 || (!BASELINK_P (fn
)
7195 && TREE_CODE (fn
) != FIELD_DECL
)
7196 || type_dependent_expression_p (fn
)
7197 || any_type_dependent_arguments_p (args
)))
7199 maybe_generic_this_capture (instance
, fn
);
7201 = build_min_nt_call_vec (postfix_expression
, args
);
7202 release_tree_vector (args
);
7206 if (BASELINK_P (fn
))
7209 = (build_new_method_call
7210 (instance
, fn
, &args
, NULL_TREE
,
7211 (idk
== CP_ID_KIND_QUALIFIED
7212 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7219 = finish_call_expr (postfix_expression
, &args
,
7220 /*disallow_virtual=*/false,
7224 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7225 || TREE_CODE (postfix_expression
) == MEMBER_REF
7226 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7227 postfix_expression
= (build_offset_ref_call_from_tree
7228 (postfix_expression
, &args
,
7230 else if (idk
== CP_ID_KIND_QUALIFIED
)
7231 /* A call to a static class member, or a namespace-scope
7234 = finish_call_expr (postfix_expression
, &args
,
7235 /*disallow_virtual=*/true,
7239 /* All other function calls. */
7241 = finish_call_expr (postfix_expression
, &args
,
7242 /*disallow_virtual=*/false,
7246 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7248 location_t combined_loc
= make_location (token
->location
,
7251 postfix_expression
.set_location (combined_loc
);
7254 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7255 idk
= CP_ID_KIND_NONE
;
7257 release_tree_vector (args
);
7263 /* postfix-expression . template [opt] id-expression
7264 postfix-expression . pseudo-destructor-name
7265 postfix-expression -> template [opt] id-expression
7266 postfix-expression -> pseudo-destructor-name */
7268 /* Consume the `.' or `->' operator. */
7269 cp_lexer_consume_token (parser
->lexer
);
7272 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7276 is_member_access
= true;
7280 /* postfix-expression ++ */
7281 /* Consume the `++' token. */
7282 cp_lexer_consume_token (parser
->lexer
);
7283 /* Generate a representation for the complete expression. */
7285 = finish_increment_expr (postfix_expression
,
7286 POSTINCREMENT_EXPR
);
7287 /* Increments may not appear in constant-expressions. */
7288 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7289 postfix_expression
= error_mark_node
;
7290 idk
= CP_ID_KIND_NONE
;
7291 is_member_access
= false;
7294 case CPP_MINUS_MINUS
:
7295 /* postfix-expression -- */
7296 /* Consume the `--' token. */
7297 cp_lexer_consume_token (parser
->lexer
);
7298 /* Generate a representation for the complete expression. */
7300 = finish_increment_expr (postfix_expression
,
7301 POSTDECREMENT_EXPR
);
7302 /* Decrements may not appear in constant-expressions. */
7303 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7304 postfix_expression
= error_mark_node
;
7305 idk
= CP_ID_KIND_NONE
;
7306 is_member_access
= false;
7310 if (pidk_return
!= NULL
)
7311 * pidk_return
= idk
;
7312 if (member_access_only_p
)
7313 return is_member_access
7314 ? postfix_expression
7315 : cp_expr (error_mark_node
);
7317 return postfix_expression
;
7321 /* We should never get here. */
7323 return error_mark_node
;
7326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7327 by cp_parser_builtin_offsetof. We're looking for
7329 postfix-expression [ expression ]
7330 postfix-expression [ braced-init-list ] (C++11)
7332 FOR_OFFSETOF is set if we're being called in that context, which
7333 changes how we deal with integer constant expressions. */
7336 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7337 tree postfix_expression
,
7341 tree index
= NULL_TREE
;
7342 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7343 bool saved_greater_than_is_operator_p
;
7345 /* Consume the `[' token. */
7346 cp_lexer_consume_token (parser
->lexer
);
7348 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7349 parser
->greater_than_is_operator_p
= true;
7351 /* Parse the index expression. */
7352 /* ??? For offsetof, there is a question of what to allow here. If
7353 offsetof is not being used in an integral constant expression context,
7354 then we *could* get the right answer by computing the value at runtime.
7355 If we are in an integral constant expression context, then we might
7356 could accept any constant expression; hard to say without analysis.
7357 Rather than open the barn door too wide right away, allow only integer
7358 constant expressions here. */
7360 index
= cp_parser_constant_expression (parser
);
7363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7365 bool expr_nonconst_p
;
7366 cp_lexer_set_source_position (parser
->lexer
);
7367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7368 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7371 index
= cp_parser_expression (parser
);
7374 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7376 /* Look for the closing `]'. */
7377 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7379 /* Build the ARRAY_REF. */
7380 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7383 /* When not doing offsetof, array references are not permitted in
7384 constant-expressions. */
7386 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7387 postfix_expression
= error_mark_node
;
7389 return postfix_expression
;
7392 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7393 dereference of incomplete type, returns true if error_mark_node should
7394 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7395 and *DEPENDENT_P. */
7398 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7401 /* In a template, be permissive by treating an object expression
7402 of incomplete type as dependent (after a pedwarn). */
7403 diagnostic_t kind
= (processing_template_decl
7404 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7406 switch (TREE_CODE (*postfix_expression
))
7409 case REINTERPRET_CAST_EXPR
:
7410 case CONST_CAST_EXPR
:
7411 case STATIC_CAST_EXPR
:
7412 case DYNAMIC_CAST_EXPR
:
7413 case IMPLICIT_CONV_EXPR
:
7414 case VIEW_CONVERT_EXPR
:
7415 case NON_LVALUE_EXPR
:
7419 /* Don't emit any diagnostic for OVERLOADs. */
7423 /* Avoid clobbering e.g. DECLs. */
7424 if (!EXPR_P (*postfix_expression
))
7429 if (kind
== DK_IGNORED
)
7432 location_t exploc
= location_of (*postfix_expression
);
7433 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7434 if (!MAYBE_CLASS_TYPE_P (*scope
))
7436 if (kind
== DK_ERROR
)
7437 *scope
= *postfix_expression
= error_mark_node
;
7438 else if (processing_template_decl
)
7440 *dependent_p
= true;
7441 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7446 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7447 by cp_parser_builtin_offsetof. We're looking for
7449 postfix-expression . template [opt] id-expression
7450 postfix-expression . pseudo-destructor-name
7451 postfix-expression -> template [opt] id-expression
7452 postfix-expression -> pseudo-destructor-name
7454 FOR_OFFSETOF is set if we're being called in that context. That sorta
7455 limits what of the above we'll actually accept, but nevermind.
7456 TOKEN_TYPE is the "." or "->" token, which will already have been
7457 removed from the stream. */
7460 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7461 enum cpp_ttype token_type
,
7462 cp_expr postfix_expression
,
7463 bool for_offsetof
, cp_id_kind
*idk
,
7464 location_t location
)
7468 bool pseudo_destructor_p
;
7469 tree scope
= NULL_TREE
;
7470 location_t start_loc
= postfix_expression
.get_start ();
7472 /* If this is a `->' operator, dereference the pointer. */
7473 if (token_type
== CPP_DEREF
)
7474 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7475 tf_warning_or_error
);
7476 /* Check to see whether or not the expression is type-dependent and
7477 not the current instantiation. */
7478 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7479 /* The identifier following the `->' or `.' is not qualified. */
7480 parser
->scope
= NULL_TREE
;
7481 parser
->qualifying_scope
= NULL_TREE
;
7482 parser
->object_scope
= NULL_TREE
;
7483 *idk
= CP_ID_KIND_NONE
;
7485 /* Enter the scope corresponding to the type of the object
7486 given by the POSTFIX_EXPRESSION. */
7489 scope
= TREE_TYPE (postfix_expression
);
7490 /* According to the standard, no expression should ever have
7491 reference type. Unfortunately, we do not currently match
7492 the standard in this respect in that our internal representation
7493 of an expression may have reference type even when the standard
7494 says it does not. Therefore, we have to manually obtain the
7495 underlying type here. */
7496 scope
= non_reference (scope
);
7497 /* The type of the POSTFIX_EXPRESSION must be complete. */
7498 /* Unlike the object expression in other contexts, *this is not
7499 required to be of complete type for purposes of class member
7500 access (5.2.5) outside the member function body. */
7501 if (postfix_expression
!= current_class_ref
7502 && scope
!= error_mark_node
7503 && !(processing_template_decl
7504 && current_class_type
7505 && (same_type_ignoring_top_level_qualifiers_p
7506 (scope
, current_class_type
))))
7508 scope
= complete_type (scope
);
7509 if (!COMPLETE_TYPE_P (scope
)
7510 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7512 return error_mark_node
;
7517 /* Let the name lookup machinery know that we are processing a
7518 class member access expression. */
7519 parser
->context
->object_type
= scope
;
7520 /* If something went wrong, we want to be able to discern that case,
7521 as opposed to the case where there was no SCOPE due to the type
7522 of expression being dependent. */
7524 scope
= error_mark_node
;
7525 /* If the SCOPE was erroneous, make the various semantic analysis
7526 functions exit quickly -- and without issuing additional error
7528 if (scope
== error_mark_node
)
7529 postfix_expression
= error_mark_node
;
7534 /* Tell cp_parser_lookup_name that there was an object, even though it's
7536 parser
->context
->object_type
= unknown_type_node
;
7538 /* Assume this expression is not a pseudo-destructor access. */
7539 pseudo_destructor_p
= false;
7541 /* If the SCOPE is a scalar type, then, if this is a valid program,
7542 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7543 is type dependent, it can be pseudo-destructor-name or something else.
7544 Try to parse it as pseudo-destructor-name first. */
7545 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7550 cp_parser_parse_tentatively (parser
);
7551 /* Parse the pseudo-destructor-name. */
7553 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7556 && (cp_parser_error_occurred (parser
)
7557 || !SCALAR_TYPE_P (type
)))
7558 cp_parser_abort_tentative_parse (parser
);
7559 else if (cp_parser_parse_definitely (parser
))
7561 pseudo_destructor_p
= true;
7563 = finish_pseudo_destructor_expr (postfix_expression
,
7568 if (!pseudo_destructor_p
)
7570 /* If the SCOPE is not a scalar type, we are looking at an
7571 ordinary class member access expression, rather than a
7572 pseudo-destructor-name. */
7574 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7575 /* Parse the id-expression. */
7576 name
= (cp_parser_id_expression
7578 cp_parser_optional_template_keyword (parser
),
7579 /*check_dependency_p=*/true,
7581 /*declarator_p=*/false,
7582 /*optional_p=*/false));
7583 /* In general, build a SCOPE_REF if the member name is qualified.
7584 However, if the name was not dependent and has already been
7585 resolved; there is no need to build the SCOPE_REF. For example;
7587 struct X { void f(); };
7588 template <typename T> void f(T* t) { t->X::f(); }
7590 Even though "t" is dependent, "X::f" is not and has been resolved
7591 to a BASELINK; there is no need to include scope information. */
7593 /* But we do need to remember that there was an explicit scope for
7594 virtual function calls. */
7596 *idk
= CP_ID_KIND_QUALIFIED
;
7598 /* If the name is a template-id that names a type, we will get a
7599 TYPE_DECL here. That is invalid code. */
7600 if (TREE_CODE (name
) == TYPE_DECL
)
7602 error_at (token
->location
, "invalid use of %qD", name
);
7603 postfix_expression
= error_mark_node
;
7607 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7609 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7611 error_at (token
->location
, "%<%D::%D%> is not a class member",
7612 parser
->scope
, name
);
7613 postfix_expression
= error_mark_node
;
7616 name
= build_qualified_name (/*type=*/NULL_TREE
,
7620 parser
->scope
= NULL_TREE
;
7621 parser
->qualifying_scope
= NULL_TREE
;
7622 parser
->object_scope
= NULL_TREE
;
7624 if (parser
->scope
&& name
&& BASELINK_P (name
))
7625 adjust_result_of_qualified_name_lookup
7626 (name
, parser
->scope
, scope
);
7628 = finish_class_member_access_expr (postfix_expression
, name
,
7630 tf_warning_or_error
);
7631 /* Build a location e.g.:
7634 where the caret is at the deref token, ranging from
7635 the start of postfix_expression to the end of the access expr. */
7637 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7638 location_t combined_loc
7639 = make_location (input_location
, start_loc
, end_loc
);
7640 protected_set_expr_location (postfix_expression
, combined_loc
);
7644 /* We no longer need to look up names in the scope of the object on
7645 the left-hand side of the `.' or `->' operator. */
7646 parser
->context
->object_type
= NULL_TREE
;
7648 /* Outside of offsetof, these operators may not appear in
7649 constant-expressions. */
7651 && (cp_parser_non_integral_constant_expression
7652 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7653 postfix_expression
= error_mark_node
;
7655 return postfix_expression
;
7658 /* Parse a parenthesized expression-list.
7661 assignment-expression
7662 expression-list, assignment-expression
7667 identifier, expression-list
7669 CAST_P is true if this expression is the target of a cast.
7671 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7674 WRAP_LOCATIONS_P is true if expressions within this list for which
7675 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7676 their source locations.
7678 Returns a vector of trees. Each element is a representation of an
7679 assignment-expression. NULL is returned if the ( and or ) are
7680 missing. An empty, but allocated, vector is returned on no
7681 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7682 if we are parsing an attribute list for an attribute that wants a
7683 plain identifier argument, normal_attr for an attribute that wants
7684 an expression, or non_attr if we aren't parsing an attribute list. If
7685 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7686 not all of the expressions in the list were constant.
7687 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7688 will be written to with the location of the closing parenthesis. If
7689 an error occurs, it may or may not be written to. */
7691 static vec
<tree
, va_gc
> *
7692 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7693 int is_attribute_list
,
7695 bool allow_expansion_p
,
7696 bool *non_constant_p
,
7697 location_t
*close_paren_loc
,
7698 bool wrap_locations_p
)
7700 vec
<tree
, va_gc
> *expression_list
;
7701 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7702 tree identifier
= NULL_TREE
;
7703 bool saved_greater_than_is_operator_p
;
7705 /* Assume all the expressions will be constant. */
7707 *non_constant_p
= false;
7709 matching_parens parens
;
7710 if (!parens
.require_open (parser
))
7713 expression_list
= make_tree_vector ();
7715 /* Within a parenthesized expression, a `>' token is always
7716 the greater-than operator. */
7717 saved_greater_than_is_operator_p
7718 = parser
->greater_than_is_operator_p
;
7719 parser
->greater_than_is_operator_p
= true;
7721 cp_expr
expr (NULL_TREE
);
7723 /* Consume expressions until there are no more. */
7724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7727 /* At the beginning of attribute lists, check to see if the
7728 next token is an identifier. */
7729 if (is_attribute_list
== id_attr
7730 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7734 /* Consume the identifier. */
7735 token
= cp_lexer_consume_token (parser
->lexer
);
7736 /* Save the identifier. */
7737 identifier
= token
->u
.value
;
7741 bool expr_non_constant_p
;
7743 /* Parse the next assignment-expression. */
7744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7746 /* A braced-init-list. */
7747 cp_lexer_set_source_position (parser
->lexer
);
7748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7749 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7750 if (non_constant_p
&& expr_non_constant_p
)
7751 *non_constant_p
= true;
7753 else if (non_constant_p
)
7755 expr
= (cp_parser_constant_expression
7756 (parser
, /*allow_non_constant_p=*/true,
7757 &expr_non_constant_p
));
7758 if (expr_non_constant_p
)
7759 *non_constant_p
= true;
7762 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7766 expr
= instantiate_non_dependent_expr (expr
);
7768 /* If we have an ellipsis, then this is an expression
7770 if (allow_expansion_p
7771 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7773 /* Consume the `...'. */
7774 cp_lexer_consume_token (parser
->lexer
);
7776 /* Build the argument pack. */
7777 expr
= make_pack_expansion (expr
);
7780 if (wrap_locations_p
)
7781 expr
.maybe_add_location_wrapper ();
7783 /* Add it to the list. We add error_mark_node
7784 expressions to the list, so that we can still tell if
7785 the correct form for a parenthesized expression-list
7786 is found. That gives better errors. */
7787 vec_safe_push (expression_list
, expr
.get_value ());
7789 if (expr
== error_mark_node
)
7793 /* After the first item, attribute lists look the same as
7794 expression lists. */
7795 is_attribute_list
= non_attr
;
7798 /* If the next token isn't a `,', then we are done. */
7799 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7802 /* Otherwise, consume the `,' and keep going. */
7803 cp_lexer_consume_token (parser
->lexer
);
7806 if (close_paren_loc
)
7807 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7809 if (!parens
.require_close (parser
))
7814 /* We try and resync to an unnested comma, as that will give the
7815 user better diagnostics. */
7816 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7817 /*recovering=*/true,
7819 /*consume_paren=*/true);
7824 parser
->greater_than_is_operator_p
7825 = saved_greater_than_is_operator_p
;
7830 parser
->greater_than_is_operator_p
7831 = saved_greater_than_is_operator_p
;
7834 vec_safe_insert (expression_list
, 0, identifier
);
7836 return expression_list
;
7839 /* Parse a pseudo-destructor-name.
7841 pseudo-destructor-name:
7842 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7843 :: [opt] nested-name-specifier template template-id :: ~ type-name
7844 :: [opt] nested-name-specifier [opt] ~ type-name
7846 If either of the first two productions is used, sets *SCOPE to the
7847 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7848 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7849 or ERROR_MARK_NODE if the parse fails. */
7852 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7857 bool nested_name_specifier_p
;
7860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7861 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7862 && !type_dependent_expression_p (object
))
7864 if (cxx_dialect
< cxx14
)
7865 pedwarn (input_location
, 0,
7866 "%<~auto%> only available with "
7867 "-std=c++14 or -std=gnu++14");
7868 cp_lexer_consume_token (parser
->lexer
);
7869 cp_lexer_consume_token (parser
->lexer
);
7871 *type
= TREE_TYPE (object
);
7875 /* Assume that things will not work out. */
7876 *type
= error_mark_node
;
7878 /* Look for the optional `::' operator. */
7879 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7880 /* Look for the optional nested-name-specifier. */
7881 nested_name_specifier_p
7882 = (cp_parser_nested_name_specifier_opt (parser
,
7883 /*typename_keyword_p=*/false,
7884 /*check_dependency_p=*/true,
7886 /*is_declaration=*/false)
7888 /* Now, if we saw a nested-name-specifier, we might be doing the
7889 second production. */
7890 if (nested_name_specifier_p
7891 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7893 /* Consume the `template' keyword. */
7894 cp_lexer_consume_token (parser
->lexer
);
7895 /* Parse the template-id. */
7896 cp_parser_template_id (parser
,
7897 /*template_keyword_p=*/true,
7898 /*check_dependency_p=*/false,
7900 /*is_declaration=*/true);
7901 /* Look for the `::' token. */
7902 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7904 /* If the next token is not a `~', then there might be some
7905 additional qualification. */
7906 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7908 /* At this point, we're looking for "type-name :: ~". The type-name
7909 must not be a class-name, since this is a pseudo-destructor. So,
7910 it must be either an enum-name, or a typedef-name -- both of which
7911 are just identifiers. So, we peek ahead to check that the "::"
7912 and "~" tokens are present; if they are not, then we can avoid
7913 calling type_name. */
7914 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7915 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7916 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7918 cp_parser_error (parser
, "non-scalar type");
7922 /* Look for the type-name. */
7923 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7924 if (*scope
== error_mark_node
)
7927 /* Look for the `::' token. */
7928 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7933 /* Look for the `~'. */
7934 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7936 /* Once we see the ~, this has to be a pseudo-destructor. */
7937 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7938 cp_parser_commit_to_topmost_tentative_parse (parser
);
7940 /* Look for the type-name again. We are not responsible for
7941 checking that it matches the first type-name. */
7942 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7945 /* Parse a unary-expression.
7951 unary-operator cast-expression
7952 sizeof unary-expression
7954 alignof ( type-id ) [C++0x]
7961 __extension__ cast-expression
7962 __alignof__ unary-expression
7963 __alignof__ ( type-id )
7964 alignof unary-expression [C++0x]
7965 __real__ cast-expression
7966 __imag__ cast-expression
7968 sizeof ( type-id ) { initializer-list , [opt] }
7969 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7970 __alignof__ ( type-id ) { initializer-list , [opt] }
7972 ADDRESS_P is true iff the unary-expression is appearing as the
7973 operand of the `&' operator. CAST_P is true if this expression is
7974 the target of a cast.
7976 Returns a representation of the expression. */
7979 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7980 bool address_p
, bool cast_p
, bool decltype_p
)
7983 enum tree_code unary_operator
;
7985 /* Peek at the next token. */
7986 token
= cp_lexer_peek_token (parser
->lexer
);
7987 /* Some keywords give away the kind of expression. */
7988 if (token
->type
== CPP_KEYWORD
)
7990 enum rid keyword
= token
->keyword
;
7999 location_t start_loc
= token
->location
;
8001 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8002 /* Consume the token. */
8003 cp_lexer_consume_token (parser
->lexer
);
8004 /* Parse the operand. */
8005 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8007 if (TYPE_P (operand
))
8008 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
8011 /* ISO C++ defines alignof only with types, not with
8012 expressions. So pedwarn if alignof is used with a non-
8013 type expression. However, __alignof__ is ok. */
8014 if (id_equal (token
->u
.value
, "alignof"))
8015 pedwarn (token
->location
, OPT_Wpedantic
,
8016 "ISO C++ does not allow %<alignof%> "
8019 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8021 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8022 SIZEOF_EXPR with the original operand. */
8023 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8025 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8027 if (!processing_template_decl
&& TYPE_P (operand
))
8029 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8030 build1 (NOP_EXPR
, operand
,
8032 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8035 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8036 TREE_SIDE_EFFECTS (ret
) = 0;
8037 TREE_READONLY (ret
) = 1;
8041 /* Construct a location e.g. :
8044 with start == caret at the start of the "alignof"/"sizeof"
8045 token, with the endpoint at the final closing paren. */
8046 location_t finish_loc
8047 = cp_lexer_previous_token (parser
->lexer
)->location
;
8048 location_t compound_loc
8049 = make_location (start_loc
, start_loc
, finish_loc
);
8051 cp_expr
ret_expr (ret
);
8052 ret_expr
.set_location (compound_loc
);
8053 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8058 return cp_parser_new_expression (parser
);
8061 return cp_parser_delete_expression (parser
);
8065 /* The saved value of the PEDANTIC flag. */
8069 /* Save away the PEDANTIC flag. */
8070 cp_parser_extension_opt (parser
, &saved_pedantic
);
8071 /* Parse the cast-expression. */
8072 expr
= cp_parser_simple_cast_expression (parser
);
8073 /* Restore the PEDANTIC flag. */
8074 pedantic
= saved_pedantic
;
8084 /* Consume the `__real__' or `__imag__' token. */
8085 cp_lexer_consume_token (parser
->lexer
);
8086 /* Parse the cast-expression. */
8087 expression
= cp_parser_simple_cast_expression (parser
);
8088 /* Create the complete representation. */
8089 return build_x_unary_op (token
->location
,
8090 (keyword
== RID_REALPART
8091 ? REALPART_EXPR
: IMAGPART_EXPR
),
8093 tf_warning_or_error
);
8097 case RID_TRANSACTION_ATOMIC
:
8098 case RID_TRANSACTION_RELAXED
:
8099 return cp_parser_transaction_expression (parser
, keyword
);
8104 const char *saved_message
;
8105 bool saved_integral_constant_expression_p
;
8106 bool saved_non_integral_constant_expression_p
;
8107 bool saved_greater_than_is_operator_p
;
8109 location_t start_loc
= token
->location
;
8111 cp_lexer_consume_token (parser
->lexer
);
8112 matching_parens parens
;
8113 parens
.require_open (parser
);
8115 saved_message
= parser
->type_definition_forbidden_message
;
8116 parser
->type_definition_forbidden_message
8117 = G_("types may not be defined in %<noexcept%> expressions");
8119 saved_integral_constant_expression_p
8120 = parser
->integral_constant_expression_p
;
8121 saved_non_integral_constant_expression_p
8122 = parser
->non_integral_constant_expression_p
;
8123 parser
->integral_constant_expression_p
= false;
8125 saved_greater_than_is_operator_p
8126 = parser
->greater_than_is_operator_p
;
8127 parser
->greater_than_is_operator_p
= true;
8129 ++cp_unevaluated_operand
;
8130 ++c_inhibit_evaluation_warnings
;
8131 ++cp_noexcept_operand
;
8132 expr
= cp_parser_expression (parser
);
8133 --cp_noexcept_operand
;
8134 --c_inhibit_evaluation_warnings
;
8135 --cp_unevaluated_operand
;
8137 parser
->greater_than_is_operator_p
8138 = saved_greater_than_is_operator_p
;
8140 parser
->integral_constant_expression_p
8141 = saved_integral_constant_expression_p
;
8142 parser
->non_integral_constant_expression_p
8143 = saved_non_integral_constant_expression_p
;
8145 parser
->type_definition_forbidden_message
= saved_message
;
8147 location_t finish_loc
8148 = cp_lexer_peek_token (parser
->lexer
)->location
;
8149 parens
.require_close (parser
);
8151 /* Construct a location of the form:
8154 with start == caret, finishing at the close-paren. */
8155 location_t noexcept_loc
8156 = make_location (start_loc
, start_loc
, finish_loc
);
8158 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8167 /* Look for the `:: new' and `:: delete', which also signal the
8168 beginning of a new-expression, or delete-expression,
8169 respectively. If the next token is `::', then it might be one of
8171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8175 /* See if the token after the `::' is one of the keywords in
8176 which we're interested. */
8177 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8178 /* If it's `new', we have a new-expression. */
8179 if (keyword
== RID_NEW
)
8180 return cp_parser_new_expression (parser
);
8181 /* Similarly, for `delete'. */
8182 else if (keyword
== RID_DELETE
)
8183 return cp_parser_delete_expression (parser
);
8186 /* Look for a unary operator. */
8187 unary_operator
= cp_parser_unary_operator (token
);
8188 /* The `++' and `--' operators can be handled similarly, even though
8189 they are not technically unary-operators in the grammar. */
8190 if (unary_operator
== ERROR_MARK
)
8192 if (token
->type
== CPP_PLUS_PLUS
)
8193 unary_operator
= PREINCREMENT_EXPR
;
8194 else if (token
->type
== CPP_MINUS_MINUS
)
8195 unary_operator
= PREDECREMENT_EXPR
;
8196 /* Handle the GNU address-of-label extension. */
8197 else if (cp_parser_allow_gnu_extensions_p (parser
)
8198 && token
->type
== CPP_AND_AND
)
8202 location_t start_loc
= token
->location
;
8204 /* Consume the '&&' token. */
8205 cp_lexer_consume_token (parser
->lexer
);
8206 /* Look for the identifier. */
8207 location_t finish_loc
8208 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8209 identifier
= cp_parser_identifier (parser
);
8210 /* Construct a location of the form:
8213 with caret==start at the "&&", finish at the end of the label. */
8214 location_t combined_loc
8215 = make_location (start_loc
, start_loc
, finish_loc
);
8216 /* Create an expression representing the address. */
8217 expression
= finish_label_address_expr (identifier
, combined_loc
);
8218 if (cp_parser_non_integral_constant_expression (parser
,
8220 expression
= error_mark_node
;
8224 if (unary_operator
!= ERROR_MARK
)
8226 cp_expr cast_expression
;
8227 cp_expr expression
= error_mark_node
;
8228 non_integral_constant non_constant_p
= NIC_NONE
;
8229 location_t loc
= token
->location
;
8230 tsubst_flags_t complain
= complain_flags (decltype_p
);
8232 /* Consume the operator token. */
8233 token
= cp_lexer_consume_token (parser
->lexer
);
8234 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8236 /* Parse the cast-expression. */
8238 = cp_parser_cast_expression (parser
,
8239 unary_operator
== ADDR_EXPR
,
8245 OP_TOKEN CAST_EXPRESSION
8246 ^~~~~~~~~~~~~~~~~~~~~~~~~
8247 with start==caret at the operator token, and
8248 extending to the end of the cast_expression. */
8249 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8251 /* Now, build an appropriate representation. */
8252 switch (unary_operator
)
8255 non_constant_p
= NIC_STAR
;
8256 expression
= build_x_indirect_ref (loc
, cast_expression
,
8259 /* TODO: build_x_indirect_ref does not always honor the
8260 location, so ensure it is set. */
8261 expression
.set_location (loc
);
8265 non_constant_p
= NIC_ADDR
;
8268 expression
= build_x_unary_op (loc
, unary_operator
,
8271 /* TODO: build_x_unary_op does not always honor the location,
8272 so ensure it is set. */
8273 expression
.set_location (loc
);
8276 case PREINCREMENT_EXPR
:
8277 case PREDECREMENT_EXPR
:
8278 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8279 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8282 /* Immediately fold negation of a constant, unless the constant is 0
8283 (since -0 == 0) or it would overflow. */
8284 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8285 && CONSTANT_CLASS_P (cast_expression
)
8286 && !integer_zerop (cast_expression
)
8287 && !TREE_OVERFLOW (cast_expression
))
8289 tree folded
= fold_build1 (unary_operator
,
8290 TREE_TYPE (cast_expression
),
8292 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8294 expression
= cp_expr (folded
, loc
);
8299 case UNARY_PLUS_EXPR
:
8300 case TRUTH_NOT_EXPR
:
8301 expression
= finish_unary_op_expr (loc
, unary_operator
,
8302 cast_expression
, complain
);
8309 if (non_constant_p
!= NIC_NONE
8310 && cp_parser_non_integral_constant_expression (parser
,
8312 expression
= error_mark_node
;
8317 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8318 /*member_access_only_p=*/false,
8323 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8324 unary-operator, the corresponding tree code is returned. */
8326 static enum tree_code
8327 cp_parser_unary_operator (cp_token
* token
)
8329 switch (token
->type
)
8332 return INDIRECT_REF
;
8338 return UNARY_PLUS_EXPR
;
8344 return TRUTH_NOT_EXPR
;
8347 return BIT_NOT_EXPR
;
8354 /* Parse a new-expression.
8357 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8358 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8360 Returns a representation of the expression. */
8363 cp_parser_new_expression (cp_parser
* parser
)
8365 bool global_scope_p
;
8366 vec
<tree
, va_gc
> *placement
;
8368 vec
<tree
, va_gc
> *initializer
;
8369 tree nelts
= NULL_TREE
;
8372 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8374 /* Look for the optional `::' operator. */
8376 = (cp_parser_global_scope_opt (parser
,
8377 /*current_scope_valid_p=*/false)
8379 /* Look for the `new' operator. */
8380 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8381 /* There's no easy way to tell a new-placement from the
8382 `( type-id )' construct. */
8383 cp_parser_parse_tentatively (parser
);
8384 /* Look for a new-placement. */
8385 placement
= cp_parser_new_placement (parser
);
8386 /* If that didn't work out, there's no new-placement. */
8387 if (!cp_parser_parse_definitely (parser
))
8389 if (placement
!= NULL
)
8390 release_tree_vector (placement
);
8394 /* If the next token is a `(', then we have a parenthesized
8396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8399 const char *saved_message
= parser
->type_definition_forbidden_message
;
8401 /* Consume the `('. */
8402 matching_parens parens
;
8403 parens
.consume_open (parser
);
8405 /* Parse the type-id. */
8406 parser
->type_definition_forbidden_message
8407 = G_("types may not be defined in a new-expression");
8409 type_id_in_expr_sentinel
s (parser
);
8410 type
= cp_parser_type_id (parser
);
8412 parser
->type_definition_forbidden_message
= saved_message
;
8414 /* Look for the closing `)'. */
8415 parens
.require_close (parser
);
8416 token
= cp_lexer_peek_token (parser
->lexer
);
8417 /* There should not be a direct-new-declarator in this production,
8418 but GCC used to allowed this, so we check and emit a sensible error
8419 message for this case. */
8420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8422 error_at (token
->location
,
8423 "array bound forbidden after parenthesized type-id");
8424 inform (token
->location
,
8425 "try removing the parentheses around the type-id");
8426 cp_parser_direct_new_declarator (parser
);
8429 /* Otherwise, there must be a new-type-id. */
8431 type
= cp_parser_new_type_id (parser
, &nelts
);
8433 /* If the next token is a `(' or '{', then we have a new-initializer. */
8434 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8435 if (token
->type
== CPP_OPEN_PAREN
8436 || token
->type
== CPP_OPEN_BRACE
)
8437 initializer
= cp_parser_new_initializer (parser
);
8441 /* A new-expression may not appear in an integral constant
8443 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8444 ret
= error_mark_node
;
8445 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8446 of a new-type-id or type-id of a new-expression, the new-expression shall
8447 contain a new-initializer of the form ( assignment-expression )".
8448 Additionally, consistently with the spirit of DR 1467, we want to accept
8449 'new auto { 2 }' too. */
8450 else if ((ret
= type_uses_auto (type
))
8451 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8452 && (vec_safe_length (initializer
) != 1
8453 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8454 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8456 error_at (token
->location
,
8457 "initialization of new-expression for type %<auto%> "
8458 "requires exactly one element");
8459 ret
= error_mark_node
;
8463 /* Construct a location e.g.:
8466 with caret == start at the start of the "new" token, and the end
8467 at the end of the final token we consumed. */
8468 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8469 location_t end_loc
= get_finish (end_tok
->location
);
8470 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8472 /* Create a representation of the new-expression. */
8473 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8474 tf_warning_or_error
);
8475 protected_set_expr_location (ret
, combined_loc
);
8478 if (placement
!= NULL
)
8479 release_tree_vector (placement
);
8480 if (initializer
!= NULL
)
8481 release_tree_vector (initializer
);
8486 /* Parse a new-placement.
8491 Returns the same representation as for an expression-list. */
8493 static vec
<tree
, va_gc
> *
8494 cp_parser_new_placement (cp_parser
* parser
)
8496 vec
<tree
, va_gc
> *expression_list
;
8498 /* Parse the expression-list. */
8499 expression_list
= (cp_parser_parenthesized_expression_list
8500 (parser
, non_attr
, /*cast_p=*/false,
8501 /*allow_expansion_p=*/true,
8502 /*non_constant_p=*/NULL
));
8504 if (expression_list
&& expression_list
->is_empty ())
8505 error ("expected expression-list or type-id");
8507 return expression_list
;
8510 /* Parse a new-type-id.
8513 type-specifier-seq new-declarator [opt]
8515 Returns the TYPE allocated. If the new-type-id indicates an array
8516 type, *NELTS is set to the number of elements in the last array
8517 bound; the TYPE will not include the last array bound. */
8520 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8522 cp_decl_specifier_seq type_specifier_seq
;
8523 cp_declarator
*new_declarator
;
8524 cp_declarator
*declarator
;
8525 cp_declarator
*outer_declarator
;
8526 const char *saved_message
;
8528 /* The type-specifier sequence must not contain type definitions.
8529 (It cannot contain declarations of new types either, but if they
8530 are not definitions we will catch that because they are not
8532 saved_message
= parser
->type_definition_forbidden_message
;
8533 parser
->type_definition_forbidden_message
8534 = G_("types may not be defined in a new-type-id");
8535 /* Parse the type-specifier-seq. */
8536 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8537 /*is_trailing_return=*/false,
8538 &type_specifier_seq
);
8539 /* Restore the old message. */
8540 parser
->type_definition_forbidden_message
= saved_message
;
8542 if (type_specifier_seq
.type
== error_mark_node
)
8543 return error_mark_node
;
8545 /* Parse the new-declarator. */
8546 new_declarator
= cp_parser_new_declarator_opt (parser
);
8548 /* Determine the number of elements in the last array dimension, if
8551 /* Skip down to the last array dimension. */
8552 declarator
= new_declarator
;
8553 outer_declarator
= NULL
;
8554 while (declarator
&& (declarator
->kind
== cdk_pointer
8555 || declarator
->kind
== cdk_ptrmem
))
8557 outer_declarator
= declarator
;
8558 declarator
= declarator
->declarator
;
8561 && declarator
->kind
== cdk_array
8562 && declarator
->declarator
8563 && declarator
->declarator
->kind
== cdk_array
)
8565 outer_declarator
= declarator
;
8566 declarator
= declarator
->declarator
;
8569 if (declarator
&& declarator
->kind
== cdk_array
)
8571 *nelts
= declarator
->u
.array
.bounds
;
8572 if (*nelts
== error_mark_node
)
8573 *nelts
= integer_one_node
;
8575 if (outer_declarator
)
8576 outer_declarator
->declarator
= declarator
->declarator
;
8578 new_declarator
= NULL
;
8581 return groktypename (&type_specifier_seq
, new_declarator
, false);
8584 /* Parse an (optional) new-declarator.
8587 ptr-operator new-declarator [opt]
8588 direct-new-declarator
8590 Returns the declarator. */
8592 static cp_declarator
*
8593 cp_parser_new_declarator_opt (cp_parser
* parser
)
8595 enum tree_code code
;
8596 tree type
, std_attributes
= NULL_TREE
;
8597 cp_cv_quals cv_quals
;
8599 /* We don't know if there's a ptr-operator next, or not. */
8600 cp_parser_parse_tentatively (parser
);
8601 /* Look for a ptr-operator. */
8602 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8603 /* If that worked, look for more new-declarators. */
8604 if (cp_parser_parse_definitely (parser
))
8606 cp_declarator
*declarator
;
8608 /* Parse another optional declarator. */
8609 declarator
= cp_parser_new_declarator_opt (parser
);
8611 declarator
= cp_parser_make_indirect_declarator
8612 (code
, type
, cv_quals
, declarator
, std_attributes
);
8617 /* If the next token is a `[', there is a direct-new-declarator. */
8618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8619 return cp_parser_direct_new_declarator (parser
);
8624 /* Parse a direct-new-declarator.
8626 direct-new-declarator:
8628 direct-new-declarator [constant-expression]
8632 static cp_declarator
*
8633 cp_parser_direct_new_declarator (cp_parser
* parser
)
8635 cp_declarator
*declarator
= NULL
;
8642 /* Look for the opening `['. */
8643 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8645 token
= cp_lexer_peek_token (parser
->lexer
);
8646 expression
= cp_parser_expression (parser
);
8647 /* The standard requires that the expression have integral
8648 type. DR 74 adds enumeration types. We believe that the
8649 real intent is that these expressions be handled like the
8650 expression in a `switch' condition, which also allows
8651 classes with a single conversion to integral or
8652 enumeration type. */
8653 if (!processing_template_decl
)
8656 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8661 error_at (token
->location
,
8662 "expression in new-declarator must have integral "
8663 "or enumeration type");
8664 expression
= error_mark_node
;
8668 /* Look for the closing `]'. */
8669 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8671 /* Add this bound to the declarator. */
8672 declarator
= make_array_declarator (declarator
, expression
);
8674 /* If the next token is not a `[', then there are no more
8676 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8683 /* Parse a new-initializer.
8686 ( expression-list [opt] )
8689 Returns a representation of the expression-list. */
8691 static vec
<tree
, va_gc
> *
8692 cp_parser_new_initializer (cp_parser
* parser
)
8694 vec
<tree
, va_gc
> *expression_list
;
8696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8699 bool expr_non_constant_p
;
8700 cp_lexer_set_source_position (parser
->lexer
);
8701 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8702 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8703 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8704 expression_list
= make_tree_vector_single (t
);
8707 expression_list
= (cp_parser_parenthesized_expression_list
8708 (parser
, non_attr
, /*cast_p=*/false,
8709 /*allow_expansion_p=*/true,
8710 /*non_constant_p=*/NULL
));
8712 return expression_list
;
8715 /* Parse a delete-expression.
8718 :: [opt] delete cast-expression
8719 :: [opt] delete [ ] cast-expression
8721 Returns a representation of the expression. */
8724 cp_parser_delete_expression (cp_parser
* parser
)
8726 bool global_scope_p
;
8730 /* Look for the optional `::' operator. */
8732 = (cp_parser_global_scope_opt (parser
,
8733 /*current_scope_valid_p=*/false)
8735 /* Look for the `delete' keyword. */
8736 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8737 /* See if the array syntax is in use. */
8738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8740 /* Consume the `[' token. */
8741 cp_lexer_consume_token (parser
->lexer
);
8742 /* Look for the `]' token. */
8743 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8744 /* Remember that this is the `[]' construct. */
8750 /* Parse the cast-expression. */
8751 expression
= cp_parser_simple_cast_expression (parser
);
8753 /* A delete-expression may not appear in an integral constant
8755 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8756 return error_mark_node
;
8758 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8759 tf_warning_or_error
);
8762 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8763 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8767 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8769 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8770 switch (token
->type
)
8776 case CPP_CLOSE_SQUARE
:
8777 case CPP_CLOSE_PAREN
:
8778 case CPP_CLOSE_BRACE
:
8779 case CPP_OPEN_BRACE
:
8783 case CPP_DEREF_STAR
:
8791 case CPP_GREATER_EQ
:
8812 case CPP_OPEN_PAREN
:
8813 /* In ((type ()) () the last () isn't a valid cast-expression,
8814 so the whole must be parsed as postfix-expression. */
8815 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8818 case CPP_OPEN_SQUARE
:
8819 /* '[' may start a primary-expression in obj-c++ and in C++11,
8820 as a lambda-expression, eg, '(void)[]{}'. */
8821 if (cxx_dialect
>= cxx11
)
8823 return c_dialect_objc ();
8826 case CPP_MINUS_MINUS
:
8827 /* '++' and '--' may or may not start a cast-expression:
8829 struct T { void operator++(int); };
8830 void f() { (T())++; }
8843 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8844 in the order: const_cast, static_cast, reinterpret_cast.
8846 Don't suggest dynamic_cast.
8848 Return the first legal cast kind found, or NULL otherwise. */
8851 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8855 /* Reuse the parser logic by attempting to build the various kinds of
8856 cast, with "complain" disabled.
8857 Identify the first such cast that is valid. */
8859 /* Don't attempt to run such logic within template processing. */
8860 if (processing_template_decl
)
8863 /* First try const_cast. */
8864 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8865 if (trial
!= error_mark_node
)
8866 return "const_cast";
8868 /* If that fails, try static_cast. */
8869 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8870 if (trial
!= error_mark_node
)
8871 return "static_cast";
8873 /* Finally, try reinterpret_cast. */
8874 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8875 if (trial
!= error_mark_node
)
8876 return "reinterpret_cast";
8878 /* No such cast possible. */
8882 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8883 suggesting how to convert a C-style cast of the form:
8887 to a C++-style cast.
8889 The primary range of RICHLOC is asssumed to be that of the original
8890 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8891 of the parens in the C-style cast. */
8894 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8895 location_t close_paren_loc
, tree orig_expr
,
8898 /* This function is non-trivial, so bail out now if the warning isn't
8899 going to be emitted. */
8900 if (!warn_old_style_cast
)
8903 /* Try to find a legal C++ cast, trying them in order:
8904 const_cast, static_cast, reinterpret_cast. */
8905 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8906 if (!cast_suggestion
)
8909 /* Replace the open paren with "CAST_SUGGESTION<". */
8911 pp_printf (&pp
, "%s<", cast_suggestion
);
8912 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8914 /* Replace the close paren with "> (". */
8915 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8917 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8918 rich_loc
->add_fixit_insert_after (")");
8922 /* Parse a cast-expression.
8926 ( type-id ) cast-expression
8928 ADDRESS_P is true iff the unary-expression is appearing as the
8929 operand of the `&' operator. CAST_P is true if this expression is
8930 the target of a cast.
8932 Returns a representation of the expression. */
8935 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8936 bool decltype_p
, cp_id_kind
* pidk
)
8938 /* If it's a `(', then we might be looking at a cast. */
8939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8941 tree type
= NULL_TREE
;
8942 cp_expr
expr (NULL_TREE
);
8943 int cast_expression
= 0;
8944 const char *saved_message
;
8946 /* There's no way to know yet whether or not this is a cast.
8947 For example, `(int (3))' is a unary-expression, while `(int)
8948 3' is a cast. So, we resort to parsing tentatively. */
8949 cp_parser_parse_tentatively (parser
);
8950 /* Types may not be defined in a cast. */
8951 saved_message
= parser
->type_definition_forbidden_message
;
8952 parser
->type_definition_forbidden_message
8953 = G_("types may not be defined in casts");
8954 /* Consume the `('. */
8955 matching_parens parens
;
8956 cp_token
*open_paren
= parens
.consume_open (parser
);
8957 location_t open_paren_loc
= open_paren
->location
;
8958 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8960 /* A very tricky bit is that `(struct S) { 3 }' is a
8961 compound-literal (which we permit in C++ as an extension).
8962 But, that construct is not a cast-expression -- it is a
8963 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8964 is legal; if the compound-literal were a cast-expression,
8965 you'd need an extra set of parentheses.) But, if we parse
8966 the type-id, and it happens to be a class-specifier, then we
8967 will commit to the parse at that point, because we cannot
8968 undo the action that is done when creating a new class. So,
8969 then we cannot back up and do a postfix-expression.
8971 Another tricky case is the following (c++/29234):
8973 struct S { void operator () (); };
8980 As a type-id we parse the parenthesized S()() as a function
8981 returning a function, groktypename complains and we cannot
8982 back up in this case either.
8984 Therefore, we scan ahead to the closing `)', and check to see
8985 if the tokens after the `)' can start a cast-expression. Otherwise
8986 we are dealing with an unary-expression, a postfix-expression
8989 Yet another tricky case, in C++11, is the following (c++/54891):
8993 The issue is that usually, besides the case of lambda-expressions,
8994 the parenthesized type-id cannot be followed by '[', and, eg, we
8995 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8996 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8997 we don't commit, we try a cast-expression, then an unary-expression.
8999 Save tokens so that we can put them back. */
9000 cp_lexer_save_tokens (parser
->lexer
);
9002 /* We may be looking at a cast-expression. */
9003 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9004 /*consume_paren=*/true))
9006 = cp_parser_tokens_start_cast_expression (parser
);
9008 /* Roll back the tokens we skipped. */
9009 cp_lexer_rollback_tokens (parser
->lexer
);
9010 /* If we aren't looking at a cast-expression, simulate an error so
9011 that the call to cp_parser_error_occurred below returns true. */
9012 if (!cast_expression
)
9013 cp_parser_simulate_error (parser
);
9016 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9017 parser
->in_type_id_in_expr_p
= true;
9018 /* Look for the type-id. */
9019 type
= cp_parser_type_id (parser
);
9020 /* Look for the closing `)'. */
9021 cp_token
*close_paren
= parens
.require_close (parser
);
9023 close_paren_loc
= close_paren
->location
;
9024 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9027 /* Restore the saved message. */
9028 parser
->type_definition_forbidden_message
= saved_message
;
9030 /* At this point this can only be either a cast or a
9031 parenthesized ctor such as `(T ())' that looks like a cast to
9032 function returning T. */
9033 if (!cp_parser_error_occurred (parser
))
9035 /* Only commit if the cast-expression doesn't start with
9036 '++', '--', or '[' in C++11. */
9037 if (cast_expression
> 0)
9038 cp_parser_commit_to_topmost_tentative_parse (parser
);
9040 expr
= cp_parser_cast_expression (parser
,
9041 /*address_p=*/false,
9043 /*decltype_p=*/false,
9046 if (cp_parser_parse_definitely (parser
))
9048 /* Warn about old-style casts, if so requested. */
9049 if (warn_old_style_cast
9050 && !in_system_header_at (input_location
)
9051 && !VOID_TYPE_P (type
)
9052 && current_lang_name
!= lang_name_c
)
9054 gcc_rich_location
rich_loc (input_location
);
9055 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9057 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9058 "use of old-style cast to %q#T", type
);
9061 /* Only type conversions to integral or enumeration types
9062 can be used in constant-expressions. */
9063 if (!cast_valid_in_integral_constant_expression_p (type
)
9064 && cp_parser_non_integral_constant_expression (parser
,
9066 return error_mark_node
;
9068 /* Perform the cast. */
9072 with start==caret at the open paren, extending to the
9074 location_t cast_loc
= make_location (open_paren_loc
,
9076 expr
.get_finish ());
9077 expr
= build_c_cast (cast_loc
, type
, expr
);
9082 cp_parser_abort_tentative_parse (parser
);
9085 /* If we get here, then it's not a cast, so it must be a
9086 unary-expression. */
9087 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9088 cast_p
, decltype_p
);
9091 /* Parse a binary expression of the general form:
9095 pm-expression .* cast-expression
9096 pm-expression ->* cast-expression
9098 multiplicative-expression:
9100 multiplicative-expression * pm-expression
9101 multiplicative-expression / pm-expression
9102 multiplicative-expression % pm-expression
9104 additive-expression:
9105 multiplicative-expression
9106 additive-expression + multiplicative-expression
9107 additive-expression - multiplicative-expression
9111 shift-expression << additive-expression
9112 shift-expression >> additive-expression
9114 relational-expression:
9116 relational-expression < shift-expression
9117 relational-expression > shift-expression
9118 relational-expression <= shift-expression
9119 relational-expression >= shift-expression
9123 relational-expression:
9124 relational-expression <? shift-expression
9125 relational-expression >? shift-expression
9127 equality-expression:
9128 relational-expression
9129 equality-expression == relational-expression
9130 equality-expression != relational-expression
9134 and-expression & equality-expression
9136 exclusive-or-expression:
9138 exclusive-or-expression ^ and-expression
9140 inclusive-or-expression:
9141 exclusive-or-expression
9142 inclusive-or-expression | exclusive-or-expression
9144 logical-and-expression:
9145 inclusive-or-expression
9146 logical-and-expression && inclusive-or-expression
9148 logical-or-expression:
9149 logical-and-expression
9150 logical-or-expression || logical-and-expression
9152 All these are implemented with a single function like:
9155 simple-cast-expression
9156 binary-expression <token> binary-expression
9158 CAST_P is true if this expression is the target of a cast.
9160 The binops_by_token map is used to get the tree codes for each <token> type.
9161 binary-expressions are associated according to a precedence table. */
9163 #define TOKEN_PRECEDENCE(token) \
9164 (((token->type == CPP_GREATER \
9165 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9166 && !parser->greater_than_is_operator_p) \
9167 ? PREC_NOT_OPERATOR \
9168 : binops_by_token[token->type].prec)
9171 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9172 bool no_toplevel_fold_p
,
9174 enum cp_parser_prec prec
,
9177 cp_parser_expression_stack stack
;
9178 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9179 cp_parser_expression_stack_entry current
;
9182 enum tree_code rhs_type
;
9183 enum cp_parser_prec new_prec
, lookahead_prec
;
9186 /* Parse the first expression. */
9187 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9188 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9189 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9190 cast_p
, decltype_p
, pidk
);
9191 current
.prec
= prec
;
9193 if (cp_parser_error_occurred (parser
))
9194 return error_mark_node
;
9198 /* Get an operator token. */
9199 token
= cp_lexer_peek_token (parser
->lexer
);
9201 if (warn_cxx11_compat
9202 && token
->type
== CPP_RSHIFT
9203 && !parser
->greater_than_is_operator_p
)
9205 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9206 "%<>>%> operator is treated"
9207 " as two right angle brackets in C++11"))
9208 inform (token
->location
,
9209 "suggest parentheses around %<>>%> expression");
9212 new_prec
= TOKEN_PRECEDENCE (token
);
9213 if (new_prec
!= PREC_NOT_OPERATOR
9214 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9215 /* This is a fold-expression; handle it later. */
9216 new_prec
= PREC_NOT_OPERATOR
;
9218 /* Popping an entry off the stack means we completed a subexpression:
9219 - either we found a token which is not an operator (`>' where it is not
9220 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9221 will happen repeatedly;
9222 - or, we found an operator which has lower priority. This is the case
9223 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9225 if (new_prec
<= current
.prec
)
9234 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9235 current
.loc
= token
->location
;
9237 /* We used the operator token. */
9238 cp_lexer_consume_token (parser
->lexer
);
9240 /* For "false && x" or "true || x", x will never be executed;
9241 disable warnings while evaluating it. */
9242 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9243 c_inhibit_evaluation_warnings
+=
9244 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9245 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9246 c_inhibit_evaluation_warnings
+=
9247 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9249 /* Extract another operand. It may be the RHS of this expression
9250 or the LHS of a new, higher priority expression. */
9251 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9252 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9253 rhs
= cp_parser_simple_cast_expression (parser
);
9255 /* Get another operator token. Look up its precedence to avoid
9256 building a useless (immediately popped) stack entry for common
9257 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9258 token
= cp_lexer_peek_token (parser
->lexer
);
9259 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9260 if (lookahead_prec
!= PREC_NOT_OPERATOR
9261 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9262 lookahead_prec
= PREC_NOT_OPERATOR
;
9263 if (lookahead_prec
> new_prec
)
9265 /* ... and prepare to parse the RHS of the new, higher priority
9266 expression. Since precedence levels on the stack are
9267 monotonically increasing, we do not have to care about
9272 current
.lhs_type
= rhs_type
;
9273 current
.prec
= new_prec
;
9274 new_prec
= lookahead_prec
;
9278 lookahead_prec
= new_prec
;
9279 /* If the stack is not empty, we have parsed into LHS the right side
9280 (`4' in the example above) of an expression we had suspended.
9281 We can use the information on the stack to recover the LHS (`3')
9282 from the stack together with the tree code (`MULT_EXPR'), and
9283 the precedence of the higher level subexpression
9284 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9285 which will be used to actually build the additive expression. */
9287 rhs_type
= current
.lhs_type
;
9292 /* Undo the disabling of warnings done above. */
9293 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9294 c_inhibit_evaluation_warnings
-=
9295 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9296 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9297 c_inhibit_evaluation_warnings
-=
9298 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9300 if (warn_logical_not_paren
9301 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9302 && current
.lhs_type
== TRUTH_NOT_EXPR
9303 /* Avoid warning for !!x == y. */
9304 && (TREE_CODE (current
.lhs
) != NE_EXPR
9305 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9306 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9307 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9308 /* Avoid warning for !b == y where b is boolean. */
9309 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9310 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9312 /* Avoid warning for !!b == y where b is boolean. */
9313 && (!DECL_P (current
.lhs
)
9314 || TREE_TYPE (current
.lhs
) == NULL_TREE
9315 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9316 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9317 current
.lhs
, maybe_constant_value (rhs
));
9321 location_t combined_loc
= make_location (current
.loc
,
9322 current
.lhs
.get_start (),
9325 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9326 ERROR_MARK for everything that is not a binary expression.
9327 This makes warn_about_parentheses miss some warnings that
9328 involve unary operators. For unary expressions we should
9329 pass the correct tree_code unless the unary expression was
9330 surrounded by parentheses.
9332 if (no_toplevel_fold_p
9333 && lookahead_prec
<= current
.prec
9336 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9337 current
.lhs
= error_mark_node
;
9341 = build_min (current
.tree_type
,
9342 TREE_CODE_CLASS (current
.tree_type
)
9344 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9345 current
.lhs
.get_value (), rhs
.get_value ());
9346 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9351 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9352 current
.lhs
, current
.lhs_type
,
9353 rhs
, rhs_type
, &overload
,
9354 complain_flags (decltype_p
));
9355 /* TODO: build_x_binary_op doesn't always honor the location. */
9356 current
.lhs
.set_location (combined_loc
);
9358 current
.lhs_type
= current
.tree_type
;
9360 /* If the binary operator required the use of an overloaded operator,
9361 then this expression cannot be an integral constant-expression.
9362 An overloaded operator can be used even if both operands are
9363 otherwise permissible in an integral constant-expression if at
9364 least one of the operands is of enumeration type. */
9367 && cp_parser_non_integral_constant_expression (parser
,
9369 return error_mark_node
;
9376 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9377 bool no_toplevel_fold_p
,
9378 enum cp_parser_prec prec
,
9381 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9382 /*decltype*/false, prec
, pidk
);
9385 /* Parse the `? expression : assignment-expression' part of a
9386 conditional-expression. The LOGICAL_OR_EXPR is the
9387 logical-or-expression that started the conditional-expression.
9388 Returns a representation of the entire conditional-expression.
9390 This routine is used by cp_parser_assignment_expression.
9392 ? expression : assignment-expression
9396 ? : assignment-expression */
9399 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9401 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9402 cp_expr assignment_expr
;
9403 struct cp_token
*token
;
9404 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9406 /* Consume the `?' token. */
9407 cp_lexer_consume_token (parser
->lexer
);
9408 token
= cp_lexer_peek_token (parser
->lexer
);
9409 if (cp_parser_allow_gnu_extensions_p (parser
)
9410 && token
->type
== CPP_COLON
)
9412 pedwarn (token
->location
, OPT_Wpedantic
,
9413 "ISO C++ does not allow ?: with omitted middle operand");
9414 /* Implicit true clause. */
9416 c_inhibit_evaluation_warnings
+=
9417 folded_logical_or_expr
== truthvalue_true_node
;
9418 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9422 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9423 parser
->colon_corrects_to_scope_p
= false;
9424 /* Parse the expression. */
9425 c_inhibit_evaluation_warnings
+=
9426 folded_logical_or_expr
== truthvalue_false_node
;
9427 expr
= cp_parser_expression (parser
);
9428 c_inhibit_evaluation_warnings
+=
9429 ((folded_logical_or_expr
== truthvalue_true_node
)
9430 - (folded_logical_or_expr
== truthvalue_false_node
));
9431 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9434 /* The next token should be a `:'. */
9435 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9436 /* Parse the assignment-expression. */
9437 assignment_expr
= cp_parser_assignment_expression (parser
);
9438 c_inhibit_evaluation_warnings
-=
9439 folded_logical_or_expr
== truthvalue_true_node
;
9442 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9443 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9444 with the caret at the "?", ranging from the start of
9445 the logical_or_expr to the end of the assignment_expr. */
9446 loc
= make_location (loc
,
9447 logical_or_expr
.get_start (),
9448 assignment_expr
.get_finish ());
9450 /* Build the conditional-expression. */
9451 return build_x_conditional_expr (loc
, logical_or_expr
,
9454 tf_warning_or_error
);
9457 /* Parse an assignment-expression.
9459 assignment-expression:
9460 conditional-expression
9461 logical-or-expression assignment-operator assignment_expression
9464 CAST_P is true if this expression is the target of a cast.
9465 DECLTYPE_P is true if this expression is the operand of decltype.
9467 Returns a representation for the expression. */
9470 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9471 bool cast_p
, bool decltype_p
)
9475 /* If the next token is the `throw' keyword, then we're looking at
9476 a throw-expression. */
9477 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9478 expr
= cp_parser_throw_expression (parser
);
9479 /* Otherwise, it must be that we are looking at a
9480 logical-or-expression. */
9483 /* Parse the binary expressions (logical-or-expression). */
9484 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9486 PREC_NOT_OPERATOR
, pidk
);
9487 /* If the next token is a `?' then we're actually looking at a
9488 conditional-expression. */
9489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9490 return cp_parser_question_colon_clause (parser
, expr
);
9493 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9495 /* If it's an assignment-operator, we're using the second
9497 enum tree_code assignment_operator
9498 = cp_parser_assignment_operator_opt (parser
);
9499 if (assignment_operator
!= ERROR_MARK
)
9501 bool non_constant_p
;
9503 /* Parse the right-hand side of the assignment. */
9504 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9507 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9508 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9510 /* An assignment may not appear in a
9511 constant-expression. */
9512 if (cp_parser_non_integral_constant_expression (parser
,
9514 return error_mark_node
;
9515 /* Build the assignment expression. Its default
9519 is the location of the '=' token as the
9520 caret, ranging from the start of the lhs to the
9522 loc
= make_location (loc
,
9525 expr
= build_x_modify_expr (loc
, expr
,
9526 assignment_operator
,
9528 complain_flags (decltype_p
));
9529 /* TODO: build_x_modify_expr doesn't honor the location,
9530 so we must set it here. */
9531 expr
.set_location (loc
);
9539 /* Parse an (optional) assignment-operator.
9541 assignment-operator: one of
9542 = *= /= %= += -= >>= <<= &= ^= |=
9546 assignment-operator: one of
9549 If the next token is an assignment operator, the corresponding tree
9550 code is returned, and the token is consumed. For example, for
9551 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9552 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9553 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9554 operator, ERROR_MARK is returned. */
9556 static enum tree_code
9557 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9562 /* Peek at the next token. */
9563 token
= cp_lexer_peek_token (parser
->lexer
);
9565 switch (token
->type
)
9576 op
= TRUNC_DIV_EXPR
;
9580 op
= TRUNC_MOD_EXPR
;
9612 /* Nothing else is an assignment operator. */
9616 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9617 if (op
!= ERROR_MARK
9618 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9621 /* If it was an assignment operator, consume it. */
9622 if (op
!= ERROR_MARK
)
9623 cp_lexer_consume_token (parser
->lexer
);
9628 /* Parse an expression.
9631 assignment-expression
9632 expression , assignment-expression
9634 CAST_P is true if this expression is the target of a cast.
9635 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9636 except possibly parenthesized or on the RHS of a comma (N3276).
9638 Returns a representation of the expression. */
9641 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9642 bool cast_p
, bool decltype_p
)
9644 cp_expr expression
= NULL_TREE
;
9645 location_t loc
= UNKNOWN_LOCATION
;
9649 cp_expr assignment_expression
;
9651 /* Parse the next assignment-expression. */
9652 assignment_expression
9653 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9655 /* We don't create a temporary for a call that is the immediate operand
9656 of decltype or on the RHS of a comma. But when we see a comma, we
9657 need to create a temporary for a call on the LHS. */
9658 if (decltype_p
&& !processing_template_decl
9659 && TREE_CODE (assignment_expression
) == CALL_EXPR
9660 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9661 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9662 assignment_expression
9663 = build_cplus_new (TREE_TYPE (assignment_expression
),
9664 assignment_expression
, tf_warning_or_error
);
9666 /* If this is the first assignment-expression, we can just
9669 expression
= assignment_expression
;
9672 /* Create a location with caret at the comma, ranging
9673 from the start of the LHS to the end of the RHS. */
9674 loc
= make_location (loc
,
9675 expression
.get_start (),
9676 assignment_expression
.get_finish ());
9677 expression
= build_x_compound_expr (loc
, expression
,
9678 assignment_expression
,
9679 complain_flags (decltype_p
));
9680 expression
.set_location (loc
);
9682 /* If the next token is not a comma, or we're in a fold-expression, then
9683 we are done with the expression. */
9684 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9685 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9687 /* Consume the `,'. */
9688 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9689 cp_lexer_consume_token (parser
->lexer
);
9690 /* A comma operator cannot appear in a constant-expression. */
9691 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9692 expression
= error_mark_node
;
9698 /* Parse a constant-expression.
9700 constant-expression:
9701 conditional-expression
9703 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9704 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9705 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9706 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9707 only parse a conditional-expression, otherwise parse an
9708 assignment-expression. See below for rationale. */
9711 cp_parser_constant_expression (cp_parser
* parser
,
9712 bool allow_non_constant_p
,
9713 bool *non_constant_p
,
9716 bool saved_integral_constant_expression_p
;
9717 bool saved_allow_non_integral_constant_expression_p
;
9718 bool saved_non_integral_constant_expression_p
;
9721 /* It might seem that we could simply parse the
9722 conditional-expression, and then check to see if it were
9723 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9724 one that the compiler can figure out is constant, possibly after
9725 doing some simplifications or optimizations. The standard has a
9726 precise definition of constant-expression, and we must honor
9727 that, even though it is somewhat more restrictive.
9733 is not a legal declaration, because `(2, 3)' is not a
9734 constant-expression. The `,' operator is forbidden in a
9735 constant-expression. However, GCC's constant-folding machinery
9736 will fold this operation to an INTEGER_CST for `3'. */
9738 /* Save the old settings. */
9739 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9740 saved_allow_non_integral_constant_expression_p
9741 = parser
->allow_non_integral_constant_expression_p
;
9742 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9743 /* We are now parsing a constant-expression. */
9744 parser
->integral_constant_expression_p
= true;
9745 parser
->allow_non_integral_constant_expression_p
9746 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9747 parser
->non_integral_constant_expression_p
= false;
9748 /* Although the grammar says "conditional-expression", when not STRICT_P,
9749 we parse an "assignment-expression", which also permits
9750 "throw-expression" and the use of assignment operators. In the case
9751 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9752 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9753 actually essential that we look for an assignment-expression.
9754 For example, cp_parser_initializer_clauses uses this function to
9755 determine whether a particular assignment-expression is in fact
9759 /* Parse the binary expressions (logical-or-expression). */
9760 expression
= cp_parser_binary_expression (parser
, false, false, false,
9761 PREC_NOT_OPERATOR
, NULL
);
9762 /* If the next token is a `?' then we're actually looking at
9763 a conditional-expression; otherwise we're done. */
9764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9765 expression
= cp_parser_question_colon_clause (parser
, expression
);
9768 expression
= cp_parser_assignment_expression (parser
);
9769 /* Restore the old settings. */
9770 parser
->integral_constant_expression_p
9771 = saved_integral_constant_expression_p
;
9772 parser
->allow_non_integral_constant_expression_p
9773 = saved_allow_non_integral_constant_expression_p
;
9774 if (cxx_dialect
>= cxx11
)
9776 /* Require an rvalue constant expression here; that's what our
9777 callers expect. Reference constant expressions are handled
9778 separately in e.g. cp_parser_template_argument. */
9779 tree decay
= expression
;
9780 if (TREE_TYPE (expression
)
9781 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9782 decay
= build_address (expression
);
9783 bool is_const
= potential_rvalue_constant_expression (decay
);
9784 parser
->non_integral_constant_expression_p
= !is_const
;
9785 if (!is_const
&& !allow_non_constant_p
)
9786 require_potential_rvalue_constant_expression (decay
);
9788 if (allow_non_constant_p
)
9789 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9790 parser
->non_integral_constant_expression_p
9791 = saved_non_integral_constant_expression_p
;
9796 /* Parse __builtin_offsetof.
9798 offsetof-expression:
9799 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9801 offsetof-member-designator:
9803 | offsetof-member-designator "." id-expression
9804 | offsetof-member-designator "[" expression "]"
9805 | offsetof-member-designator "->" id-expression */
9808 cp_parser_builtin_offsetof (cp_parser
*parser
)
9810 int save_ice_p
, save_non_ice_p
;
9815 location_t finish_loc
;
9817 /* We're about to accept non-integral-constant things, but will
9818 definitely yield an integral constant expression. Save and
9819 restore these values around our local parsing. */
9820 save_ice_p
= parser
->integral_constant_expression_p
;
9821 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9823 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9825 /* Consume the "__builtin_offsetof" token. */
9826 cp_lexer_consume_token (parser
->lexer
);
9827 /* Consume the opening `('. */
9828 matching_parens parens
;
9829 parens
.require_open (parser
);
9830 /* Parse the type-id. */
9831 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9832 type
= cp_parser_type_id (parser
);
9833 /* Look for the `,'. */
9834 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9835 token
= cp_lexer_peek_token (parser
->lexer
);
9837 /* Build the (type *)null that begins the traditional offsetof macro. */
9839 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9840 tf_warning_or_error
);
9842 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9843 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9844 true, &dummy
, token
->location
);
9847 token
= cp_lexer_peek_token (parser
->lexer
);
9848 switch (token
->type
)
9850 case CPP_OPEN_SQUARE
:
9851 /* offsetof-member-designator "[" expression "]" */
9852 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9857 /* offsetof-member-designator "->" identifier */
9858 expr
= grok_array_decl (token
->location
, expr
,
9859 integer_zero_node
, false);
9863 /* offsetof-member-designator "." identifier */
9864 cp_lexer_consume_token (parser
->lexer
);
9865 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9870 case CPP_CLOSE_PAREN
:
9871 /* Consume the ")" token. */
9872 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9873 cp_lexer_consume_token (parser
->lexer
);
9877 /* Error. We know the following require will fail, but
9878 that gives the proper error message. */
9879 parens
.require_close (parser
);
9880 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9881 expr
= error_mark_node
;
9887 /* Make a location of the form:
9888 __builtin_offsetof (struct s, f)
9889 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9890 with caret at the type-id, ranging from the start of the
9891 "_builtin_offsetof" token to the close paren. */
9892 loc
= make_location (loc
, start_loc
, finish_loc
);
9893 /* The result will be an INTEGER_CST, so we need to explicitly
9894 preserve the location. */
9895 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9898 parser
->integral_constant_expression_p
= save_ice_p
;
9899 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9901 expr
= expr
.maybe_add_location_wrapper ();
9905 /* Parse a trait expression.
9907 Returns a representation of the expression, the underlying type
9908 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9911 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9914 tree type1
, type2
= NULL_TREE
;
9915 bool binary
= false;
9916 bool variadic
= false;
9920 case RID_HAS_NOTHROW_ASSIGN
:
9921 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9923 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9924 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9926 case RID_HAS_NOTHROW_COPY
:
9927 kind
= CPTK_HAS_NOTHROW_COPY
;
9929 case RID_HAS_TRIVIAL_ASSIGN
:
9930 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9932 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9933 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9935 case RID_HAS_TRIVIAL_COPY
:
9936 kind
= CPTK_HAS_TRIVIAL_COPY
;
9938 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9939 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9941 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9942 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9944 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9945 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9947 case RID_IS_ABSTRACT
:
9948 kind
= CPTK_IS_ABSTRACT
;
9950 case RID_IS_AGGREGATE
:
9951 kind
= CPTK_IS_AGGREGATE
;
9953 case RID_IS_BASE_OF
:
9954 kind
= CPTK_IS_BASE_OF
;
9958 kind
= CPTK_IS_CLASS
;
9961 kind
= CPTK_IS_EMPTY
;
9964 kind
= CPTK_IS_ENUM
;
9967 kind
= CPTK_IS_FINAL
;
9969 case RID_IS_LITERAL_TYPE
:
9970 kind
= CPTK_IS_LITERAL_TYPE
;
9975 case RID_IS_POLYMORPHIC
:
9976 kind
= CPTK_IS_POLYMORPHIC
;
9978 case RID_IS_SAME_AS
:
9979 kind
= CPTK_IS_SAME_AS
;
9982 case RID_IS_STD_LAYOUT
:
9983 kind
= CPTK_IS_STD_LAYOUT
;
9985 case RID_IS_TRIVIAL
:
9986 kind
= CPTK_IS_TRIVIAL
;
9988 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9989 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9992 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9993 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9996 case RID_IS_TRIVIALLY_COPYABLE
:
9997 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10000 kind
= CPTK_IS_UNION
;
10002 case RID_UNDERLYING_TYPE
:
10003 kind
= CPTK_UNDERLYING_TYPE
;
10008 case RID_DIRECT_BASES
:
10009 kind
= CPTK_DIRECT_BASES
;
10011 case RID_IS_ASSIGNABLE
:
10012 kind
= CPTK_IS_ASSIGNABLE
;
10015 case RID_IS_CONSTRUCTIBLE
:
10016 kind
= CPTK_IS_CONSTRUCTIBLE
;
10020 gcc_unreachable ();
10023 /* Get location of initial token. */
10024 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10026 /* Consume the token. */
10027 cp_lexer_consume_token (parser
->lexer
);
10029 matching_parens parens
;
10030 parens
.require_open (parser
);
10033 type_id_in_expr_sentinel
s (parser
);
10034 type1
= cp_parser_type_id (parser
);
10037 if (type1
== error_mark_node
)
10038 return error_mark_node
;
10042 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10045 type_id_in_expr_sentinel
s (parser
);
10046 type2
= cp_parser_type_id (parser
);
10049 if (type2
== error_mark_node
)
10050 return error_mark_node
;
10054 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10056 cp_lexer_consume_token (parser
->lexer
);
10057 tree elt
= cp_parser_type_id (parser
);
10058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10060 cp_lexer_consume_token (parser
->lexer
);
10061 elt
= make_pack_expansion (elt
);
10063 if (elt
== error_mark_node
)
10064 return error_mark_node
;
10065 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10069 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10070 parens
.require_close (parser
);
10072 /* Construct a location of the form:
10073 __is_trivially_copyable(_Tp)
10074 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10075 with start == caret, finishing at the close-paren. */
10076 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10078 /* Complete the trait expression, which may mean either processing
10079 the trait expr now or saving it for template instantiation. */
10082 case CPTK_UNDERLYING_TYPE
:
10083 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10085 return cp_expr (finish_bases (type1
, false), trait_loc
);
10086 case CPTK_DIRECT_BASES
:
10087 return cp_expr (finish_bases (type1
, true), trait_loc
);
10089 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10093 /* Parse a lambda expression.
10096 lambda-introducer lambda-declarator [opt] compound-statement
10098 Returns a representation of the expression. */
10101 cp_parser_lambda_expression (cp_parser
* parser
)
10103 tree lambda_expr
= build_lambda_expr ();
10106 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10107 cp_token_position start
= 0;
10109 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10111 if (cp_unevaluated_operand
)
10113 if (!token
->error_reported
)
10115 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10116 "lambda-expression in unevaluated context");
10117 token
->error_reported
= true;
10121 else if (parser
->in_template_argument_list_p
)
10123 if (!token
->error_reported
)
10125 error_at (token
->location
, "lambda-expression in template-argument");
10126 token
->error_reported
= true;
10131 /* We may be in the middle of deferred access check. Disable
10133 push_deferring_access_checks (dk_no_deferred
);
10135 cp_parser_lambda_introducer (parser
, lambda_expr
);
10137 type
= begin_lambda_type (lambda_expr
);
10138 if (type
== error_mark_node
)
10139 return error_mark_node
;
10141 record_lambda_scope (lambda_expr
);
10143 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10144 determine_visibility (TYPE_NAME (type
));
10146 /* Now that we've started the type, add the capture fields for any
10147 explicit captures. */
10148 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10151 /* Inside the class, surrounding template-parameter-lists do not apply. */
10152 unsigned int saved_num_template_parameter_lists
10153 = parser
->num_template_parameter_lists
;
10154 unsigned char in_statement
= parser
->in_statement
;
10155 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10156 bool fully_implicit_function_template_p
10157 = parser
->fully_implicit_function_template_p
;
10158 tree implicit_template_parms
= parser
->implicit_template_parms
;
10159 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10160 bool auto_is_implicit_function_template_parm_p
10161 = parser
->auto_is_implicit_function_template_parm_p
;
10163 parser
->num_template_parameter_lists
= 0;
10164 parser
->in_statement
= 0;
10165 parser
->in_switch_statement_p
= false;
10166 parser
->fully_implicit_function_template_p
= false;
10167 parser
->implicit_template_parms
= 0;
10168 parser
->implicit_template_scope
= 0;
10169 parser
->auto_is_implicit_function_template_parm_p
= false;
10171 /* By virtue of defining a local class, a lambda expression has access to
10172 the private variables of enclosing classes. */
10174 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10176 if (ok
&& cp_parser_error_occurred (parser
))
10181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10182 && cp_parser_start_tentative_firewall (parser
))
10184 cp_parser_lambda_body (parser
, lambda_expr
);
10186 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10188 if (cp_parser_skip_to_closing_brace (parser
))
10189 cp_lexer_consume_token (parser
->lexer
);
10192 /* The capture list was built up in reverse order; fix that now. */
10193 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10194 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10197 maybe_add_lambda_conv_op (type
);
10199 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10201 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10202 parser
->in_statement
= in_statement
;
10203 parser
->in_switch_statement_p
= in_switch_statement_p
;
10204 parser
->fully_implicit_function_template_p
10205 = fully_implicit_function_template_p
;
10206 parser
->implicit_template_parms
= implicit_template_parms
;
10207 parser
->implicit_template_scope
= implicit_template_scope
;
10208 parser
->auto_is_implicit_function_template_parm_p
10209 = auto_is_implicit_function_template_parm_p
;
10212 /* This field is only used during parsing of the lambda. */
10213 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10215 /* This lambda shouldn't have any proxies left at this point. */
10216 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10217 /* And now that we're done, push proxies for an enclosing lambda. */
10218 insert_pending_capture_proxies ();
10221 lambda_expr
= build_lambda_object (lambda_expr
);
10223 lambda_expr
= error_mark_node
;
10225 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10227 pop_deferring_access_checks ();
10229 return lambda_expr
;
10232 /* Parse the beginning of a lambda expression.
10235 [ lambda-capture [opt] ]
10237 LAMBDA_EXPR is the current representation of the lambda expression. */
10240 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10242 /* Need commas after the first capture. */
10245 /* Eat the leading `['. */
10246 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10248 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10250 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10251 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10252 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10257 cp_lexer_consume_token (parser
->lexer
);
10261 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10263 cp_token
* capture_token
;
10265 tree capture_init_expr
;
10266 cp_id_kind idk
= CP_ID_KIND_NONE
;
10267 bool explicit_init_p
= false;
10269 enum capture_kind_type
10274 enum capture_kind_type capture_kind
= BY_COPY
;
10276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10278 error ("expected end of capture-list");
10285 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10287 /* Possibly capture `this'. */
10288 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10290 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10291 if (cxx_dialect
< cxx2a
10292 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10293 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10294 "with by-copy capture default");
10295 cp_lexer_consume_token (parser
->lexer
);
10296 add_capture (lambda_expr
,
10297 /*id=*/this_identifier
,
10298 /*initializer=*/finish_this_expr (),
10299 /*by_reference_p=*/true,
10304 /* Possibly capture `*this'. */
10305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10306 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10308 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10309 if (cxx_dialect
< cxx17
)
10310 pedwarn (loc
, 0, "%<*this%> capture only available with "
10311 "-std=c++17 or -std=gnu++17");
10312 cp_lexer_consume_token (parser
->lexer
);
10313 cp_lexer_consume_token (parser
->lexer
);
10314 add_capture (lambda_expr
,
10315 /*id=*/this_identifier
,
10316 /*initializer=*/finish_this_expr (),
10317 /*by_reference_p=*/false,
10322 /* Remember whether we want to capture as a reference or not. */
10323 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10325 capture_kind
= BY_REFERENCE
;
10326 cp_lexer_consume_token (parser
->lexer
);
10329 /* Get the identifier. */
10330 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10331 capture_id
= cp_parser_identifier (parser
);
10333 if (capture_id
== error_mark_node
)
10334 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10335 delimiters, but I modified this to stop on unnested ']' as well. It
10336 was already changed to stop on unnested '}', so the
10337 "closing_parenthesis" name is no more misleading with my change. */
10339 cp_parser_skip_to_closing_parenthesis (parser
,
10340 /*recovering=*/true,
10342 /*consume_paren=*/true);
10346 /* Find the initializer for this capture. */
10347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10348 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10349 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10351 bool direct
, non_constant
;
10352 /* An explicit initializer exists. */
10353 if (cxx_dialect
< cxx14
)
10354 pedwarn (input_location
, 0,
10355 "lambda capture initializers "
10356 "only available with -std=c++14 or -std=gnu++14");
10357 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10359 explicit_init_p
= true;
10360 if (capture_init_expr
== NULL_TREE
)
10362 error ("empty initializer for lambda init-capture");
10363 capture_init_expr
= error_mark_node
;
10368 const char* error_msg
;
10370 /* Turn the identifier into an id-expression. */
10372 = cp_parser_lookup_name_simple (parser
, capture_id
,
10373 capture_token
->location
);
10375 if (capture_init_expr
== error_mark_node
)
10377 unqualified_name_lookup_error (capture_id
);
10380 else if (DECL_P (capture_init_expr
)
10381 && (!VAR_P (capture_init_expr
)
10382 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10384 error_at (capture_token
->location
,
10385 "capture of non-variable %qD ",
10386 capture_init_expr
);
10387 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10388 "%q#D declared here", capture_init_expr
);
10391 if (VAR_P (capture_init_expr
)
10392 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10394 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10395 "%qD with non-automatic storage duration",
10396 capture_init_expr
))
10397 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10398 "%q#D declared here", capture_init_expr
);
10403 = finish_id_expression
10408 /*integral_constant_expression_p=*/false,
10409 /*allow_non_integral_constant_expression_p=*/false,
10410 /*non_integral_constant_expression_p=*/NULL
,
10411 /*template_p=*/false,
10413 /*address_p=*/false,
10414 /*template_arg_p=*/false,
10416 capture_token
->location
);
10418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10420 cp_lexer_consume_token (parser
->lexer
);
10421 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10425 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10426 && !explicit_init_p
)
10428 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10429 && capture_kind
== BY_COPY
)
10430 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10431 "of %qD redundant with by-copy capture default",
10433 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10434 && capture_kind
== BY_REFERENCE
)
10435 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10436 "capture of %qD redundant with by-reference capture "
10437 "default", capture_id
);
10440 add_capture (lambda_expr
,
10443 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10446 /* If there is any qualification still in effect, clear it
10447 now; we will be starting fresh with the next capture. */
10448 parser
->scope
= NULL_TREE
;
10449 parser
->qualifying_scope
= NULL_TREE
;
10450 parser
->object_scope
= NULL_TREE
;
10453 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10456 /* Parse the (optional) middle of a lambda expression.
10459 < template-parameter-list [opt] >
10460 ( parameter-declaration-clause [opt] )
10461 attribute-specifier [opt]
10462 decl-specifier-seq [opt]
10463 exception-specification [opt]
10464 lambda-return-type-clause [opt]
10466 LAMBDA_EXPR is the current representation of the lambda expression. */
10469 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10471 /* 5.1.1.4 of the standard says:
10472 If a lambda-expression does not include a lambda-declarator, it is as if
10473 the lambda-declarator were ().
10474 This means an empty parameter list, no attributes, and no exception
10476 tree param_list
= void_list_node
;
10477 tree attributes
= NULL_TREE
;
10478 tree exception_spec
= NULL_TREE
;
10479 tree template_param_list
= NULL_TREE
;
10480 tree tx_qual
= NULL_TREE
;
10481 tree return_type
= NULL_TREE
;
10482 cp_decl_specifier_seq lambda_specs
;
10483 clear_decl_specs (&lambda_specs
);
10485 /* The template-parameter-list is optional, but must begin with
10486 an opening angle if present. */
10487 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10489 if (cxx_dialect
< cxx14
)
10490 pedwarn (parser
->lexer
->next_token
->location
, 0,
10491 "lambda templates are only available with "
10492 "-std=c++14 or -std=gnu++14");
10493 else if (cxx_dialect
< cxx2a
)
10494 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10495 "lambda templates are only available with "
10496 "-std=c++2a or -std=gnu++2a");
10498 cp_lexer_consume_token (parser
->lexer
);
10500 template_param_list
= cp_parser_template_parameter_list (parser
);
10502 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10504 /* We just processed one more parameter list. */
10505 ++parser
->num_template_parameter_lists
;
10508 /* The parameter-declaration-clause is optional (unless
10509 template-parameter-list was given), but must begin with an
10510 opening parenthesis if present. */
10511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10513 matching_parens parens
;
10514 parens
.consume_open (parser
);
10516 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10518 /* Parse parameters. */
10519 param_list
= cp_parser_parameter_declaration_clause (parser
);
10521 /* Default arguments shall not be specified in the
10522 parameter-declaration-clause of a lambda-declarator. */
10523 if (cxx_dialect
< cxx14
)
10524 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10525 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10526 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10527 "default argument specified for lambda parameter");
10529 parens
.require_close (parser
);
10531 attributes
= cp_parser_attributes_opt (parser
);
10533 /* In the decl-specifier-seq of the lambda-declarator, each
10534 decl-specifier shall either be mutable or constexpr. */
10535 int declares_class_or_enum
;
10536 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10537 cp_parser_decl_specifier_seq (parser
,
10538 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10539 &lambda_specs
, &declares_class_or_enum
);
10540 if (lambda_specs
.storage_class
== sc_mutable
)
10542 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10543 if (lambda_specs
.conflicting_specifiers_p
)
10544 error_at (lambda_specs
.locations
[ds_storage_class
],
10545 "duplicate %<mutable%>");
10548 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10550 /* Parse optional exception specification. */
10551 exception_spec
= cp_parser_exception_specification_opt (parser
);
10553 /* Parse optional trailing return type. */
10554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10556 cp_lexer_consume_token (parser
->lexer
);
10557 return_type
= cp_parser_trailing_type_id (parser
);
10560 /* The function parameters must be in scope all the way until after the
10561 trailing-return-type in case of decltype. */
10562 pop_bindings_and_leave_scope ();
10564 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10565 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10567 /* Create the function call operator.
10569 Messing with declarators like this is no uglier than building up the
10570 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10573 cp_decl_specifier_seq return_type_specs
;
10574 cp_declarator
* declarator
;
10579 clear_decl_specs (&return_type_specs
);
10581 return_type_specs
.type
= return_type
;
10583 /* Maybe we will deduce the return type later. */
10584 return_type_specs
.type
= make_auto ();
10586 if (lambda_specs
.locations
[ds_constexpr
])
10588 if (cxx_dialect
>= cxx17
)
10589 return_type_specs
.locations
[ds_constexpr
]
10590 = lambda_specs
.locations
[ds_constexpr
];
10592 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10593 "lambda only available with -std=c++17 or -std=gnu++17");
10596 p
= obstack_alloc (&declarator_obstack
, 0);
10598 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10600 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10601 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10602 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10603 VIRT_SPEC_UNSPECIFIED
,
10607 /*late_return_type=*/NULL_TREE
,
10608 /*requires_clause*/NULL_TREE
);
10609 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10611 fco
= grokmethod (&return_type_specs
,
10614 if (fco
!= error_mark_node
)
10616 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10617 DECL_ARTIFICIAL (fco
) = 1;
10618 /* Give the object parameter a different name. */
10619 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10621 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10623 if (template_param_list
)
10625 fco
= finish_member_template_decl (fco
);
10626 finish_template_decl (template_param_list
);
10627 --parser
->num_template_parameter_lists
;
10629 else if (parser
->fully_implicit_function_template_p
)
10630 fco
= finish_fully_implicit_template (parser
, fco
);
10632 finish_member_declaration (fco
);
10634 obstack_free (&declarator_obstack
, p
);
10636 return (fco
!= error_mark_node
);
10640 /* Parse the body of a lambda expression, which is simply
10644 but which requires special handling.
10645 LAMBDA_EXPR is the current representation of the lambda expression. */
10648 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10650 bool nested
= (current_function_decl
!= NULL_TREE
);
10651 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10652 bool in_function_body
= parser
->in_function_body
;
10655 push_function_context ();
10657 /* Still increment function_depth so that we don't GC in the
10658 middle of an expression. */
10661 vec
<tree
> omp_privatization_save
;
10662 save_omp_privatization_clauses (omp_privatization_save
);
10663 /* Clear this in case we're in the middle of a default argument. */
10664 parser
->local_variables_forbidden_p
= false;
10665 parser
->in_function_body
= true;
10668 local_specialization_stack
s (lss_copy
);
10669 tree fco
= lambda_function (lambda_expr
);
10670 tree body
= start_lambda_function (fco
, lambda_expr
);
10671 matching_braces braces
;
10673 if (braces
.require_open (parser
))
10675 tree compound_stmt
= begin_compound_stmt (0);
10677 /* Originally C++11 required us to peek for 'return expr'; and
10678 process it specially here to deduce the return type. N3638
10679 removed the need for that. */
10681 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10682 cp_parser_label_declaration (parser
);
10683 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10684 braces
.require_close (parser
);
10686 finish_compound_stmt (compound_stmt
);
10689 finish_lambda_function (body
);
10692 restore_omp_privatization_clauses (omp_privatization_save
);
10693 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10694 parser
->in_function_body
= in_function_body
;
10696 pop_function_context();
10701 /* Statements [gram.stmt.stmt] */
10703 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10706 add_debug_begin_stmt (location_t loc
)
10708 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10710 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10711 /* A concept is never expanded normally. */
10714 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10715 SET_EXPR_LOCATION (stmt
, loc
);
10719 /* Parse a statement.
10723 expression-statement
10725 selection-statement
10726 iteration-statement
10728 declaration-statement
10735 attribute-specifier-seq (opt) expression-statement
10736 attribute-specifier-seq (opt) compound-statement
10737 attribute-specifier-seq (opt) selection-statement
10738 attribute-specifier-seq (opt) iteration-statement
10739 attribute-specifier-seq (opt) jump-statement
10740 declaration-statement
10741 attribute-specifier-seq (opt) try-block
10744 expression-statement
10752 IN_COMPOUND is true when the statement is nested inside a
10753 cp_parser_compound_statement; this matters for certain pragmas.
10755 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10756 is a (possibly labeled) if statement which is not enclosed in braces
10757 and has an else clause. This is used to implement -Wparentheses.
10759 CHAIN is a vector of if-else-if conditions. */
10762 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10763 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10764 location_t
*loc_after_labels
)
10766 tree statement
, std_attrs
= NULL_TREE
;
10768 location_t statement_location
, attrs_location
;
10773 /* There is no statement yet. */
10774 statement
= NULL_TREE
;
10776 saved_token_sentinel
saved_tokens (parser
->lexer
);
10777 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10778 if (c_dialect_objc ())
10779 /* In obj-c++, seeing '[[' might be the either the beginning of
10780 c++11 attributes, or a nested objc-message-expression. So
10781 let's parse the c++11 attributes tentatively. */
10782 cp_parser_parse_tentatively (parser
);
10783 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10784 if (c_dialect_objc ())
10786 if (!cp_parser_parse_definitely (parser
))
10787 std_attrs
= NULL_TREE
;
10790 /* Peek at the next token. */
10791 token
= cp_lexer_peek_token (parser
->lexer
);
10792 /* Remember the location of the first token in the statement. */
10793 statement_location
= token
->location
;
10794 add_debug_begin_stmt (statement_location
);
10795 /* If this is a keyword, then that will often determine what kind of
10796 statement we have. */
10797 if (token
->type
== CPP_KEYWORD
)
10799 enum rid keyword
= token
->keyword
;
10805 /* Looks like a labeled-statement with a case label.
10806 Parse the label, and then use tail recursion to parse
10808 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10809 in_compound
= false;
10814 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10820 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10827 statement
= cp_parser_jump_statement (parser
);
10830 /* Objective-C++ exception-handling constructs. */
10833 case RID_AT_FINALLY
:
10834 case RID_AT_SYNCHRONIZED
:
10836 statement
= cp_parser_objc_statement (parser
);
10840 statement
= cp_parser_try_block (parser
);
10843 case RID_NAMESPACE
:
10844 /* This must be a namespace alias definition. */
10845 cp_parser_declaration_statement (parser
);
10848 case RID_TRANSACTION_ATOMIC
:
10849 case RID_TRANSACTION_RELAXED
:
10850 case RID_SYNCHRONIZED
:
10851 case RID_ATOMIC_NOEXCEPT
:
10852 case RID_ATOMIC_CANCEL
:
10853 statement
= cp_parser_transaction (parser
, token
);
10855 case RID_TRANSACTION_CANCEL
:
10856 statement
= cp_parser_transaction_cancel (parser
);
10860 /* It might be a keyword like `int' that can start a
10861 declaration-statement. */
10865 else if (token
->type
== CPP_NAME
)
10867 /* If the next token is a `:', then we are looking at a
10868 labeled-statement. */
10869 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10870 if (token
->type
== CPP_COLON
)
10872 /* Looks like a labeled-statement with an ordinary label.
10873 Parse the label, and then use tail recursion to parse
10876 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10877 in_compound
= false;
10881 /* Anything that starts with a `{' must be a compound-statement. */
10882 else if (token
->type
== CPP_OPEN_BRACE
)
10883 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10884 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10885 a statement all its own. */
10886 else if (token
->type
== CPP_PRAGMA
)
10888 /* Only certain OpenMP pragmas are attached to statements, and thus
10889 are considered statements themselves. All others are not. In
10890 the context of a compound, accept the pragma as a "statement" and
10891 return so that we can check for a close brace. Otherwise we
10892 require a real statement and must go back and read one. */
10894 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10895 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10899 else if (token
->type
== CPP_EOF
)
10901 cp_parser_error (parser
, "expected statement");
10905 /* Everything else must be a declaration-statement or an
10906 expression-statement. Try for the declaration-statement
10907 first, unless we are looking at a `;', in which case we know that
10908 we have an expression-statement. */
10911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10913 if (std_attrs
!= NULL_TREE
)
10915 /* Attributes should be parsed as part of the the
10916 declaration, so let's un-parse them. */
10917 saved_tokens
.rollback();
10918 std_attrs
= NULL_TREE
;
10921 cp_parser_parse_tentatively (parser
);
10922 /* Try to parse the declaration-statement. */
10923 cp_parser_declaration_statement (parser
);
10924 /* If that worked, we're done. */
10925 if (cp_parser_parse_definitely (parser
))
10928 /* All preceding labels have been parsed at this point. */
10929 if (loc_after_labels
!= NULL
)
10930 *loc_after_labels
= statement_location
;
10932 /* Look for an expression-statement instead. */
10933 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10935 /* Handle [[fallthrough]];. */
10936 if (attribute_fallthrough_p (std_attrs
))
10938 /* The next token after the fallthrough attribute is ';'. */
10939 if (statement
== NULL_TREE
)
10941 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10942 statement
= build_call_expr_internal_loc (statement_location
,
10944 void_type_node
, 0);
10945 finish_expr_stmt (statement
);
10948 warning_at (statement_location
, OPT_Wattributes
,
10949 "%<fallthrough%> attribute not followed by %<;%>");
10950 std_attrs
= NULL_TREE
;
10954 /* Set the line number for the statement. */
10955 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10956 SET_EXPR_LOCATION (statement
, statement_location
);
10958 /* Allow "[[fallthrough]];", but warn otherwise. */
10959 if (std_attrs
!= NULL_TREE
)
10960 warning_at (attrs_location
,
10962 "attributes at the beginning of statement are ignored");
10965 /* Append ATTR to attribute list ATTRS. */
10968 attr_chainon (tree attrs
, tree attr
)
10970 if (attrs
== error_mark_node
)
10971 return error_mark_node
;
10972 if (attr
== error_mark_node
)
10973 return error_mark_node
;
10974 return chainon (attrs
, attr
);
10977 /* Parse the label for a labeled-statement, i.e.
10980 case constant-expression :
10984 case constant-expression ... constant-expression : statement
10986 When a label is parsed without errors, the label is added to the
10987 parse tree by the finish_* functions, so this function doesn't
10988 have to return the label. */
10991 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10994 tree label
= NULL_TREE
;
10995 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10997 /* The next token should be an identifier. */
10998 token
= cp_lexer_peek_token (parser
->lexer
);
10999 if (token
->type
!= CPP_NAME
11000 && token
->type
!= CPP_KEYWORD
)
11002 cp_parser_error (parser
, "expected labeled-statement");
11006 /* Remember whether this case or a user-defined label is allowed to fall
11008 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11010 parser
->colon_corrects_to_scope_p
= false;
11011 switch (token
->keyword
)
11015 tree expr
, expr_hi
;
11016 cp_token
*ellipsis
;
11018 /* Consume the `case' token. */
11019 cp_lexer_consume_token (parser
->lexer
);
11020 /* Parse the constant-expression. */
11021 expr
= cp_parser_constant_expression (parser
);
11022 if (check_for_bare_parameter_packs (expr
))
11023 expr
= error_mark_node
;
11025 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11026 if (ellipsis
->type
== CPP_ELLIPSIS
)
11028 /* Consume the `...' token. */
11029 cp_lexer_consume_token (parser
->lexer
);
11030 expr_hi
= cp_parser_constant_expression (parser
);
11031 if (check_for_bare_parameter_packs (expr_hi
))
11032 expr_hi
= error_mark_node
;
11034 /* We don't need to emit warnings here, as the common code
11035 will do this for us. */
11038 expr_hi
= NULL_TREE
;
11040 if (parser
->in_switch_statement_p
)
11042 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11043 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11044 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11047 error_at (token
->location
,
11048 "case label %qE not within a switch statement",
11054 /* Consume the `default' token. */
11055 cp_lexer_consume_token (parser
->lexer
);
11057 if (parser
->in_switch_statement_p
)
11059 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11060 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11061 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11064 error_at (token
->location
, "case label not within a switch statement");
11068 /* Anything else must be an ordinary label. */
11069 label
= finish_label_stmt (cp_parser_identifier (parser
));
11070 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11071 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11075 /* Require the `:' token. */
11076 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11078 /* An ordinary label may optionally be followed by attributes.
11079 However, this is only permitted if the attributes are then
11080 followed by a semicolon. This is because, for backward
11081 compatibility, when parsing
11082 lab: __attribute__ ((unused)) int i;
11083 we want the attribute to attach to "i", not "lab". */
11084 if (label
!= NULL_TREE
11085 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11088 cp_parser_parse_tentatively (parser
);
11089 attrs
= cp_parser_gnu_attributes_opt (parser
);
11090 if (attrs
== NULL_TREE
11091 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11092 cp_parser_abort_tentative_parse (parser
);
11093 else if (!cp_parser_parse_definitely (parser
))
11096 attributes
= attr_chainon (attributes
, attrs
);
11099 if (attributes
!= NULL_TREE
)
11100 cplus_decl_attributes (&label
, attributes
, 0);
11102 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11105 /* Parse an expression-statement.
11107 expression-statement:
11110 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11111 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11112 indicates whether this expression-statement is part of an
11113 expression statement. */
11116 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11118 tree statement
= NULL_TREE
;
11119 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11120 location_t loc
= token
->location
;
11122 /* There might be attribute fallthrough. */
11123 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11125 /* If the next token is a ';', then there is no expression
11127 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11129 statement
= cp_parser_expression (parser
);
11130 if (statement
== error_mark_node
11131 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11133 cp_parser_skip_to_end_of_block_or_statement (parser
);
11134 return error_mark_node
;
11138 /* Handle [[fallthrough]];. */
11139 if (attribute_fallthrough_p (attr
))
11141 /* The next token after the fallthrough attribute is ';'. */
11142 if (statement
== NULL_TREE
)
11143 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11144 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11145 void_type_node
, 0);
11147 warning_at (loc
, OPT_Wattributes
,
11148 "%<fallthrough%> attribute not followed by %<;%>");
11152 /* Allow "[[fallthrough]];", but warn otherwise. */
11153 if (attr
!= NULL_TREE
)
11154 warning_at (loc
, OPT_Wattributes
,
11155 "attributes at the beginning of statement are ignored");
11157 /* Give a helpful message for "A<T>::type t;" and the like. */
11158 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11159 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11161 if (TREE_CODE (statement
) == SCOPE_REF
)
11162 error_at (token
->location
, "need %<typename%> before %qE because "
11163 "%qT is a dependent scope",
11164 statement
, TREE_OPERAND (statement
, 0));
11165 else if (is_overloaded_fn (statement
)
11166 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11169 tree fn
= get_first_fn (statement
);
11170 error_at (token
->location
,
11171 "%<%T::%D%> names the constructor, not the type",
11172 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11176 /* Consume the final `;'. */
11177 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11179 if (in_statement_expr
11180 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11181 /* This is the final expression statement of a statement
11183 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11184 else if (statement
)
11185 statement
= finish_expr_stmt (statement
);
11190 /* Parse a compound-statement.
11192 compound-statement:
11193 { statement-seq [opt] }
11197 compound-statement:
11198 { label-declaration-seq [opt] statement-seq [opt] }
11200 label-declaration-seq:
11202 label-declaration-seq label-declaration
11204 Returns a tree representing the statement. */
11207 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11208 int bcs_flags
, bool function_body
)
11210 tree compound_stmt
;
11211 matching_braces braces
;
11213 /* Consume the `{'. */
11214 if (!braces
.require_open (parser
))
11215 return error_mark_node
;
11216 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11217 && !function_body
&& cxx_dialect
< cxx14
)
11218 pedwarn (input_location
, OPT_Wpedantic
,
11219 "compound-statement in %<constexpr%> function");
11220 /* Begin the compound-statement. */
11221 compound_stmt
= begin_compound_stmt (bcs_flags
);
11222 /* If the next keyword is `__label__' we have a label declaration. */
11223 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11224 cp_parser_label_declaration (parser
);
11225 /* Parse an (optional) statement-seq. */
11226 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11227 /* Finish the compound-statement. */
11228 finish_compound_stmt (compound_stmt
);
11229 /* Consume the `}'. */
11230 braces
.require_close (parser
);
11232 return compound_stmt
;
11235 /* Parse an (optional) statement-seq.
11239 statement-seq [opt] statement */
11242 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11244 /* Scan statements until there aren't any more. */
11247 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11249 /* If we are looking at a `}', then we have run out of
11250 statements; the same is true if we have reached the end
11251 of file, or have stumbled upon a stray '@end'. */
11252 if (token
->type
== CPP_CLOSE_BRACE
11253 || token
->type
== CPP_EOF
11254 || token
->type
== CPP_PRAGMA_EOL
11255 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11258 /* If we are in a compound statement and find 'else' then
11259 something went wrong. */
11260 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11262 if (parser
->in_statement
& IN_IF_STMT
)
11266 token
= cp_lexer_consume_token (parser
->lexer
);
11267 error_at (token
->location
, "%<else%> without a previous %<if%>");
11271 /* Parse the statement. */
11272 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11276 /* Return true if we're looking at (init; cond), false otherwise. */
11279 cp_parser_init_statement_p (cp_parser
*parser
)
11281 /* Save tokens so that we can put them back. */
11282 cp_lexer_save_tokens (parser
->lexer
);
11284 /* Look for ';' that is not nested in () or {}. */
11285 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11286 /*recovering=*/false,
11288 /*consume_paren=*/false);
11290 /* Roll back the tokens we skipped. */
11291 cp_lexer_rollback_tokens (parser
->lexer
);
11296 /* Parse a selection-statement.
11298 selection-statement:
11299 if ( init-statement [opt] condition ) statement
11300 if ( init-statement [opt] condition ) statement else statement
11301 switch ( init-statement [opt] condition ) statement
11303 Returns the new IF_STMT or SWITCH_STMT.
11305 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11306 is a (possibly labeled) if statement which is not enclosed in
11307 braces and has an else clause. This is used to implement
11310 CHAIN is a vector of if-else-if conditions. This is used to implement
11311 -Wduplicated-cond. */
11314 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11319 token_indent_info guard_tinfo
;
11324 /* Peek at the next token. */
11325 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11326 guard_tinfo
= get_token_indent_info (token
);
11328 /* See what kind of keyword it is. */
11329 keyword
= token
->keyword
;
11339 if (keyword
== RID_IF
11340 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11344 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11345 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11346 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11347 "with -std=c++17 or -std=gnu++17");
11350 /* Look for the `('. */
11351 matching_parens parens
;
11352 if (!parens
.require_open (parser
))
11354 cp_parser_skip_to_end_of_statement (parser
);
11355 return error_mark_node
;
11358 /* Begin the selection-statement. */
11359 if (keyword
== RID_IF
)
11361 statement
= begin_if_stmt ();
11362 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11365 statement
= begin_switch_stmt ();
11367 /* Parse the optional init-statement. */
11368 if (cp_parser_init_statement_p (parser
))
11371 if (cxx_dialect
< cxx17
)
11372 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11373 "init-statement in selection statements only available "
11374 "with -std=c++17 or -std=gnu++17");
11375 cp_parser_init_statement (parser
, &decl
);
11378 /* Parse the condition. */
11379 condition
= cp_parser_condition (parser
);
11380 /* Look for the `)'. */
11381 if (!parens
.require_close (parser
))
11382 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11383 /*consume_paren=*/true);
11385 if (keyword
== RID_IF
)
11388 unsigned char in_statement
;
11390 /* Add the condition. */
11391 condition
= finish_if_stmt_cond (condition
, statement
);
11393 if (warn_duplicated_cond
)
11394 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11397 /* Parse the then-clause. */
11398 in_statement
= parser
->in_statement
;
11399 parser
->in_statement
|= IN_IF_STMT
;
11401 /* Outside a template, the non-selected branch of a constexpr
11402 if is a 'discarded statement', i.e. unevaluated. */
11403 bool was_discarded
= in_discarded_stmt
;
11404 bool discard_then
= (cx
&& !processing_template_decl
11405 && integer_zerop (condition
));
11408 in_discarded_stmt
= true;
11409 ++c_inhibit_evaluation_warnings
;
11412 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11415 parser
->in_statement
= in_statement
;
11417 finish_then_clause (statement
);
11421 THEN_CLAUSE (statement
) = NULL_TREE
;
11422 in_discarded_stmt
= was_discarded
;
11423 --c_inhibit_evaluation_warnings
;
11426 /* If the next token is `else', parse the else-clause. */
11427 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11430 bool discard_else
= (cx
&& !processing_template_decl
11431 && integer_nonzerop (condition
));
11434 in_discarded_stmt
= true;
11435 ++c_inhibit_evaluation_warnings
;
11439 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11440 /* Consume the `else' keyword. */
11441 cp_lexer_consume_token (parser
->lexer
);
11442 if (warn_duplicated_cond
)
11444 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11448 /* We've got "if (COND) else if (COND2)". Start
11449 the condition chain and add COND as the first
11451 chain
= new vec
<tree
> ();
11452 if (!CONSTANT_CLASS_P (condition
)
11453 && !TREE_SIDE_EFFECTS (condition
))
11455 /* Wrap it in a NOP_EXPR so that we can set the
11456 location of the condition. */
11457 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11459 SET_EXPR_LOCATION (e
, token
->location
);
11460 chain
->safe_push (e
);
11463 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11466 /* This is if-else without subsequent if. Zap the
11467 condition chain; we would have already warned at
11473 begin_else_clause (statement
);
11474 /* Parse the else-clause. */
11475 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11476 guard_tinfo
, chain
);
11478 finish_else_clause (statement
);
11480 /* If we are currently parsing a then-clause, then
11481 IF_P will not be NULL. We set it to true to
11482 indicate that this if statement has an else clause.
11483 This may trigger the Wparentheses warning below
11484 when we get back up to the parent if statement. */
11490 ELSE_CLAUSE (statement
) = NULL_TREE
;
11491 in_discarded_stmt
= was_discarded
;
11492 --c_inhibit_evaluation_warnings
;
11497 /* This if statement does not have an else clause. If
11498 NESTED_IF is true, then the then-clause has an if
11499 statement which does have an else clause. We warn
11500 about the potential ambiguity. */
11502 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11503 "suggest explicit braces to avoid ambiguous"
11505 if (warn_duplicated_cond
)
11507 /* We don't need the condition chain anymore. */
11513 /* Now we're all done with the if-statement. */
11514 finish_if_stmt (statement
);
11518 bool in_switch_statement_p
;
11519 unsigned char in_statement
;
11521 /* Add the condition. */
11522 finish_switch_cond (condition
, statement
);
11524 /* Parse the body of the switch-statement. */
11525 in_switch_statement_p
= parser
->in_switch_statement_p
;
11526 in_statement
= parser
->in_statement
;
11527 parser
->in_switch_statement_p
= true;
11528 parser
->in_statement
|= IN_SWITCH_STMT
;
11529 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11531 parser
->in_switch_statement_p
= in_switch_statement_p
;
11532 parser
->in_statement
= in_statement
;
11534 /* Now we're all done with the switch-statement. */
11535 finish_switch_stmt (statement
);
11543 cp_parser_error (parser
, "expected selection-statement");
11544 return error_mark_node
;
11548 /* Parse a condition.
11552 type-specifier-seq declarator = initializer-clause
11553 type-specifier-seq declarator braced-init-list
11558 type-specifier-seq declarator asm-specification [opt]
11559 attributes [opt] = assignment-expression
11561 Returns the expression that should be tested. */
11564 cp_parser_condition (cp_parser
* parser
)
11566 cp_decl_specifier_seq type_specifiers
;
11567 const char *saved_message
;
11568 int declares_class_or_enum
;
11570 /* Try the declaration first. */
11571 cp_parser_parse_tentatively (parser
);
11572 /* New types are not allowed in the type-specifier-seq for a
11574 saved_message
= parser
->type_definition_forbidden_message
;
11575 parser
->type_definition_forbidden_message
11576 = G_("types may not be defined in conditions");
11577 /* Parse the type-specifier-seq. */
11578 cp_parser_decl_specifier_seq (parser
,
11579 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11581 &declares_class_or_enum
);
11582 /* Restore the saved message. */
11583 parser
->type_definition_forbidden_message
= saved_message
;
11584 /* If all is well, we might be looking at a declaration. */
11585 if (!cp_parser_error_occurred (parser
))
11588 tree asm_specification
;
11590 cp_declarator
*declarator
;
11591 tree initializer
= NULL_TREE
;
11593 /* Parse the declarator. */
11594 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11595 /*ctor_dtor_or_conv_p=*/NULL
,
11596 /*parenthesized_p=*/NULL
,
11597 /*member_p=*/false,
11598 /*friend_p=*/false);
11599 /* Parse the attributes. */
11600 attributes
= cp_parser_attributes_opt (parser
);
11601 /* Parse the asm-specification. */
11602 asm_specification
= cp_parser_asm_specification_opt (parser
);
11603 /* If the next token is not an `=' or '{', then we might still be
11604 looking at an expression. For example:
11608 looks like a decl-specifier-seq and a declarator -- but then
11609 there is no `=', so this is an expression. */
11610 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11611 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11612 cp_parser_simulate_error (parser
);
11614 /* If we did see an `=' or '{', then we are looking at a declaration
11616 if (cp_parser_parse_definitely (parser
))
11619 bool non_constant_p
;
11620 int flags
= LOOKUP_ONLYCONVERTING
;
11622 /* Create the declaration. */
11623 decl
= start_decl (declarator
, &type_specifiers
,
11624 /*initialized_p=*/true,
11625 attributes
, /*prefix_attributes=*/NULL_TREE
,
11628 /* Parse the initializer. */
11629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11631 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11632 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11637 /* Consume the `='. */
11638 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11639 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11641 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11644 /* Process the initializer. */
11645 cp_finish_decl (decl
,
11646 initializer
, !non_constant_p
,
11651 pop_scope (pushed_scope
);
11653 return convert_from_reference (decl
);
11656 /* If we didn't even get past the declarator successfully, we are
11657 definitely not looking at a declaration. */
11659 cp_parser_abort_tentative_parse (parser
);
11661 /* Otherwise, we are looking at an expression. */
11662 return cp_parser_expression (parser
);
11665 /* Parses a for-statement or range-for-statement until the closing ')',
11669 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11671 tree init
, scope
, decl
;
11674 /* Begin the for-statement. */
11675 scope
= begin_for_scope (&init
);
11677 /* Parse the initialization. */
11678 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11681 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11683 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11687 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11688 unsigned short unroll
)
11690 /* Normal for loop */
11691 tree condition
= NULL_TREE
;
11692 tree expression
= NULL_TREE
;
11695 stmt
= begin_for_stmt (scope
, init
);
11696 /* The init-statement has already been parsed in
11697 cp_parser_init_statement, so no work is needed here. */
11698 finish_init_stmt (stmt
);
11700 /* If there's a condition, process it. */
11701 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11702 condition
= cp_parser_condition (parser
);
11705 cp_parser_error (parser
, "missing loop condition in loop with "
11706 "%<GCC ivdep%> pragma");
11707 condition
= error_mark_node
;
11711 cp_parser_error (parser
, "missing loop condition in loop with "
11712 "%<GCC unroll%> pragma");
11713 condition
= error_mark_node
;
11715 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11716 /* Look for the `;'. */
11717 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11719 /* If there's an expression, process it. */
11720 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11721 expression
= cp_parser_expression (parser
);
11722 finish_for_expr (expression
, stmt
);
11727 /* Tries to parse a range-based for-statement:
11730 decl-specifier-seq declarator : expression
11732 The decl-specifier-seq declarator and the `:' are already parsed by
11733 cp_parser_init_statement. If processing_template_decl it returns a
11734 newly created RANGE_FOR_STMT; if not, it is converted to a
11735 regular FOR_STMT. */
11738 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11739 bool ivdep
, unsigned short unroll
)
11741 tree stmt
, range_expr
;
11742 auto_vec
<cxx_binding
*, 16> bindings
;
11743 auto_vec
<tree
, 16> names
;
11744 tree decomp_first_name
= NULL_TREE
;
11745 unsigned int decomp_cnt
= 0;
11747 /* Get the range declaration momentarily out of the way so that
11748 the range expression doesn't clash with it. */
11749 if (range_decl
!= error_mark_node
)
11751 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11753 tree v
= DECL_VALUE_EXPR (range_decl
);
11754 /* For decomposition declaration get all of the corresponding
11755 declarations out of the way. */
11756 if (TREE_CODE (v
) == ARRAY_REF
11757 && VAR_P (TREE_OPERAND (v
, 0))
11758 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11760 tree d
= range_decl
;
11761 range_decl
= TREE_OPERAND (v
, 0);
11762 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11763 decomp_first_name
= d
;
11764 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11766 tree name
= DECL_NAME (d
);
11767 names
.safe_push (name
);
11768 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11769 IDENTIFIER_BINDING (name
)
11770 = IDENTIFIER_BINDING (name
)->previous
;
11774 if (names
.is_empty ())
11776 tree name
= DECL_NAME (range_decl
);
11777 names
.safe_push (name
);
11778 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11779 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11785 bool expr_non_constant_p
;
11786 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11789 range_expr
= cp_parser_expression (parser
);
11791 /* Put the range declaration(s) back into scope. */
11792 for (unsigned int i
= 0; i
< names
.length (); i
++)
11794 cxx_binding
*binding
= bindings
[i
];
11795 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11796 IDENTIFIER_BINDING (names
[i
]) = binding
;
11799 /* If in template, STMT is converted to a normal for-statement
11800 at instantiation. If not, it is done just ahead. */
11801 if (processing_template_decl
)
11803 if (check_for_bare_parameter_packs (range_expr
))
11804 range_expr
= error_mark_node
;
11805 stmt
= begin_range_for_stmt (scope
, init
);
11807 RANGE_FOR_IVDEP (stmt
) = 1;
11809 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11810 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11811 if (!type_dependent_expression_p (range_expr
)
11812 /* do_auto_deduction doesn't mess with template init-lists. */
11813 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11814 do_range_for_auto_deduction (range_decl
, range_expr
);
11818 stmt
= begin_for_stmt (scope
, init
);
11819 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11820 decomp_first_name
, decomp_cnt
, ivdep
,
11826 /* Subroutine of cp_convert_range_for: given the initializer expression,
11827 builds up the range temporary. */
11830 build_range_temp (tree range_expr
)
11832 tree range_type
, range_temp
;
11834 /* Find out the type deduced by the declaration
11835 `auto &&__range = range_expr'. */
11836 range_type
= cp_build_reference_type (make_auto (), true);
11837 range_type
= do_auto_deduction (range_type
, range_expr
,
11838 type_uses_auto (range_type
));
11840 /* Create the __range variable. */
11841 range_temp
= build_decl (input_location
, VAR_DECL
,
11842 get_identifier ("__for_range"), range_type
);
11843 TREE_USED (range_temp
) = 1;
11844 DECL_ARTIFICIAL (range_temp
) = 1;
11849 /* Used by cp_parser_range_for in template context: we aren't going to
11850 do a full conversion yet, but we still need to resolve auto in the
11851 type of the for-range-declaration if present. This is basically
11852 a shortcut version of cp_convert_range_for. */
11855 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11857 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11860 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11861 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11862 iter_type
= (cp_parser_perform_range_for_lookup
11863 (range_temp
, &begin_dummy
, &end_dummy
));
11866 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11868 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11870 tf_warning_or_error
);
11871 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11872 iter_decl
, auto_node
);
11877 /* Converts a range-based for-statement into a normal
11878 for-statement, as per the definition.
11880 for (RANGE_DECL : RANGE_EXPR)
11883 should be equivalent to:
11886 auto &&__range = RANGE_EXPR;
11887 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11891 RANGE_DECL = *__begin;
11896 If RANGE_EXPR is an array:
11897 BEGIN_EXPR = __range
11898 END_EXPR = __range + ARRAY_SIZE(__range)
11899 Else if RANGE_EXPR has a member 'begin' or 'end':
11900 BEGIN_EXPR = __range.begin()
11901 END_EXPR = __range.end()
11903 BEGIN_EXPR = begin(__range)
11904 END_EXPR = end(__range);
11906 If __range has a member 'begin' but not 'end', or vice versa, we must
11907 still use the second alternative (it will surely fail, however).
11908 When calling begin()/end() in the third alternative we must use
11909 argument dependent lookup, but always considering 'std' as an associated
11913 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11914 tree decomp_first_name
, unsigned int decomp_cnt
,
11915 bool ivdep
, unsigned short unroll
)
11918 tree iter_type
, begin_expr
, end_expr
;
11919 tree condition
, expression
;
11921 range_expr
= mark_lvalue_use (range_expr
);
11923 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11924 /* If an error happened previously do nothing or else a lot of
11925 unhelpful errors would be issued. */
11926 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11931 if (VAR_P (range_expr
)
11932 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11933 /* Can't bind a reference to an array of runtime bound. */
11934 range_temp
= range_expr
;
11937 range_temp
= build_range_temp (range_expr
);
11938 pushdecl (range_temp
);
11939 cp_finish_decl (range_temp
, range_expr
,
11940 /*is_constant_init*/false, NULL_TREE
,
11941 LOOKUP_ONLYCONVERTING
);
11942 range_temp
= convert_from_reference (range_temp
);
11944 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11945 &begin_expr
, &end_expr
);
11948 /* The new for initialization statement. */
11949 begin
= build_decl (input_location
, VAR_DECL
,
11950 get_identifier ("__for_begin"), iter_type
);
11951 TREE_USED (begin
) = 1;
11952 DECL_ARTIFICIAL (begin
) = 1;
11954 cp_finish_decl (begin
, begin_expr
,
11955 /*is_constant_init*/false, NULL_TREE
,
11956 LOOKUP_ONLYCONVERTING
);
11958 if (cxx_dialect
>= cxx17
)
11959 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11960 end
= build_decl (input_location
, VAR_DECL
,
11961 get_identifier ("__for_end"), iter_type
);
11962 TREE_USED (end
) = 1;
11963 DECL_ARTIFICIAL (end
) = 1;
11965 cp_finish_decl (end
, end_expr
,
11966 /*is_constant_init*/false, NULL_TREE
,
11967 LOOKUP_ONLYCONVERTING
);
11969 finish_init_stmt (statement
);
11971 /* The new for condition. */
11972 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11975 NULL
, tf_warning_or_error
);
11976 finish_for_cond (condition
, statement
, ivdep
, unroll
);
11978 /* The new increment expression. */
11979 expression
= finish_unary_op_expr (input_location
,
11980 PREINCREMENT_EXPR
, begin
,
11981 tf_warning_or_error
);
11982 finish_for_expr (expression
, statement
);
11984 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11985 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11987 /* The declaration is initialized with *__begin inside the loop body. */
11988 cp_finish_decl (range_decl
,
11989 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11990 tf_warning_or_error
),
11991 /*is_constant_init*/false, NULL_TREE
,
11992 LOOKUP_ONLYCONVERTING
);
11993 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11994 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11999 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12000 We need to solve both at the same time because the method used
12001 depends on the existence of members begin or end.
12002 Returns the type deduced for the iterator expression. */
12005 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12007 if (error_operand_p (range
))
12009 *begin
= *end
= error_mark_node
;
12010 return error_mark_node
;
12013 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12015 error ("range-based %<for%> expression of type %qT "
12016 "has incomplete type", TREE_TYPE (range
));
12017 *begin
= *end
= error_mark_node
;
12018 return error_mark_node
;
12020 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12022 /* If RANGE is an array, we will use pointer arithmetic. */
12023 *begin
= decay_conversion (range
, tf_warning_or_error
);
12024 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12026 array_type_nelts_top (TREE_TYPE (range
)),
12028 return TREE_TYPE (*begin
);
12032 /* If it is not an array, we must do a bit of magic. */
12033 tree id_begin
, id_end
;
12034 tree member_begin
, member_end
;
12036 *begin
= *end
= error_mark_node
;
12038 id_begin
= get_identifier ("begin");
12039 id_end
= get_identifier ("end");
12040 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12041 /*protect=*/2, /*want_type=*/false,
12042 tf_warning_or_error
);
12043 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12044 /*protect=*/2, /*want_type=*/false,
12045 tf_warning_or_error
);
12047 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
12049 /* Use the member functions. */
12050 if (member_begin
!= NULL_TREE
)
12051 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12053 error ("range-based %<for%> expression of type %qT has an "
12054 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
12056 if (member_end
!= NULL_TREE
)
12057 *end
= cp_parser_range_for_member_function (range
, id_end
);
12059 error ("range-based %<for%> expression of type %qT has a "
12060 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
12064 /* Use global functions with ADL. */
12065 vec
<tree
, va_gc
> *vec
;
12066 vec
= make_tree_vector ();
12068 vec_safe_push (vec
, range
);
12070 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12071 tf_warning_or_error
);
12072 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12073 tf_warning_or_error
);
12074 member_end
= perform_koenig_lookup (id_end
, vec
,
12075 tf_warning_or_error
);
12076 *end
= finish_call_expr (member_end
, &vec
, false, true,
12077 tf_warning_or_error
);
12079 release_tree_vector (vec
);
12082 /* Last common checks. */
12083 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12085 /* If one of the expressions is an error do no more checks. */
12086 *begin
= *end
= error_mark_node
;
12087 return error_mark_node
;
12089 else if (type_dependent_expression_p (*begin
)
12090 || type_dependent_expression_p (*end
))
12091 /* Can happen, when, eg, in a template context, Koenig lookup
12092 can't resolve begin/end (c++/58503). */
12096 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12097 /* The unqualified type of the __begin and __end temporaries should
12098 be the same, as required by the multiple auto declaration. */
12099 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12101 if (cxx_dialect
>= cxx17
12102 && (build_x_binary_op (input_location
, NE_EXPR
,
12103 *begin
, ERROR_MARK
,
12106 != error_mark_node
))
12107 /* P0184R0 allows __begin and __end to have different types,
12108 but make sure they are comparable so we can give a better
12111 error ("inconsistent begin/end types in range-based %<for%> "
12112 "statement: %qT and %qT",
12113 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12120 /* Helper function for cp_parser_perform_range_for_lookup.
12121 Builds a tree for RANGE.IDENTIFIER(). */
12124 cp_parser_range_for_member_function (tree range
, tree identifier
)
12127 vec
<tree
, va_gc
> *vec
;
12129 member
= finish_class_member_access_expr (range
, identifier
,
12130 false, tf_warning_or_error
);
12131 if (member
== error_mark_node
)
12132 return error_mark_node
;
12134 vec
= make_tree_vector ();
12135 res
= finish_call_expr (member
, &vec
,
12136 /*disallow_virtual=*/false,
12137 /*koenig_p=*/false,
12138 tf_warning_or_error
);
12139 release_tree_vector (vec
);
12143 /* Parse an iteration-statement.
12145 iteration-statement:
12146 while ( condition ) statement
12147 do statement while ( expression ) ;
12148 for ( init-statement condition [opt] ; expression [opt] )
12151 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12154 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12155 unsigned short unroll
)
12160 unsigned char in_statement
;
12161 token_indent_info guard_tinfo
;
12163 /* Peek at the next token. */
12164 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12166 return error_mark_node
;
12168 guard_tinfo
= get_token_indent_info (token
);
12170 /* Remember whether or not we are already within an iteration
12172 in_statement
= parser
->in_statement
;
12174 /* See what kind of keyword it is. */
12175 keyword
= token
->keyword
;
12182 /* Begin the while-statement. */
12183 statement
= begin_while_stmt ();
12184 /* Look for the `('. */
12185 matching_parens parens
;
12186 parens
.require_open (parser
);
12187 /* Parse the condition. */
12188 condition
= cp_parser_condition (parser
);
12189 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12190 /* Look for the `)'. */
12191 parens
.require_close (parser
);
12192 /* Parse the dependent statement. */
12193 parser
->in_statement
= IN_ITERATION_STMT
;
12194 bool prev
= note_iteration_stmt_body_start ();
12195 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12196 note_iteration_stmt_body_end (prev
);
12197 parser
->in_statement
= in_statement
;
12198 /* We're done with the while-statement. */
12199 finish_while_stmt (statement
);
12207 /* Begin the do-statement. */
12208 statement
= begin_do_stmt ();
12209 /* Parse the body of the do-statement. */
12210 parser
->in_statement
= IN_ITERATION_STMT
;
12211 bool prev
= note_iteration_stmt_body_start ();
12212 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12213 note_iteration_stmt_body_end (prev
);
12214 parser
->in_statement
= in_statement
;
12215 finish_do_body (statement
);
12216 /* Look for the `while' keyword. */
12217 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12218 /* Look for the `('. */
12219 matching_parens parens
;
12220 parens
.require_open (parser
);
12221 /* Parse the expression. */
12222 expression
= cp_parser_expression (parser
);
12223 /* We're done with the do-statement. */
12224 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12225 /* Look for the `)'. */
12226 parens
.require_close (parser
);
12227 /* Look for the `;'. */
12228 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12234 /* Look for the `('. */
12235 matching_parens parens
;
12236 parens
.require_open (parser
);
12238 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12240 /* Look for the `)'. */
12241 parens
.require_close (parser
);
12243 /* Parse the body of the for-statement. */
12244 parser
->in_statement
= IN_ITERATION_STMT
;
12245 bool prev
= note_iteration_stmt_body_start ();
12246 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12247 note_iteration_stmt_body_end (prev
);
12248 parser
->in_statement
= in_statement
;
12250 /* We're done with the for-statement. */
12251 finish_for_stmt (statement
);
12256 cp_parser_error (parser
, "expected iteration-statement");
12257 statement
= error_mark_node
;
12264 /* Parse a init-statement or the declarator of a range-based-for.
12265 Returns true if a range-based-for declaration is seen.
12268 expression-statement
12269 simple-declaration */
12272 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12274 /* If the next token is a `;', then we have an empty
12275 expression-statement. Grammatically, this is also a
12276 simple-declaration, but an invalid one, because it does not
12277 declare anything. Therefore, if we did not handle this case
12278 specially, we would issue an error message about an invalid
12280 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12282 bool is_range_for
= false;
12283 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12285 /* A colon is used in range-based for. */
12286 parser
->colon_corrects_to_scope_p
= false;
12288 /* We're going to speculatively look for a declaration, falling back
12289 to an expression, if necessary. */
12290 cp_parser_parse_tentatively (parser
);
12291 /* Parse the declaration. */
12292 cp_parser_simple_declaration (parser
,
12293 /*function_definition_allowed_p=*/false,
12295 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12298 /* It is a range-for, consume the ':' */
12299 cp_lexer_consume_token (parser
->lexer
);
12300 is_range_for
= true;
12301 if (cxx_dialect
< cxx11
)
12303 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12304 "range-based %<for%> loops only available with "
12305 "-std=c++11 or -std=gnu++11");
12306 *decl
= error_mark_node
;
12310 /* The ';' is not consumed yet because we told
12311 cp_parser_simple_declaration not to. */
12312 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12314 if (cp_parser_parse_definitely (parser
))
12315 return is_range_for
;
12316 /* If the tentative parse failed, then we shall need to look for an
12317 expression-statement. */
12319 /* If we are here, it is an expression-statement. */
12320 cp_parser_expression_statement (parser
, NULL_TREE
);
12324 /* Parse a jump-statement.
12329 return expression [opt] ;
12330 return braced-init-list ;
12336 goto * expression ;
12338 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12341 cp_parser_jump_statement (cp_parser
* parser
)
12343 tree statement
= error_mark_node
;
12346 unsigned char in_statement
;
12348 /* Peek at the next token. */
12349 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12351 return error_mark_node
;
12353 /* See what kind of keyword it is. */
12354 keyword
= token
->keyword
;
12358 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12359 switch (in_statement
)
12362 error_at (token
->location
, "break statement not within loop or switch");
12365 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12366 || in_statement
== IN_ITERATION_STMT
);
12367 statement
= finish_break_stmt ();
12368 if (in_statement
== IN_ITERATION_STMT
)
12369 break_maybe_infinite_loop ();
12372 error_at (token
->location
, "invalid exit from OpenMP structured block");
12375 error_at (token
->location
, "break statement used with OpenMP for loop");
12378 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12382 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12385 error_at (token
->location
, "continue statement not within a loop");
12387 /* Fall through. */
12388 case IN_ITERATION_STMT
:
12390 statement
= finish_continue_stmt ();
12393 error_at (token
->location
, "invalid exit from OpenMP structured block");
12396 gcc_unreachable ();
12398 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12404 bool expr_non_constant_p
;
12406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12408 cp_lexer_set_source_position (parser
->lexer
);
12409 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12410 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12412 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12413 expr
= cp_parser_expression (parser
);
12415 /* If the next token is a `;', then there is no
12418 /* Build the return-statement. */
12419 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12420 /* Don't deduce from a discarded return statement. */;
12422 statement
= finish_return_stmt (expr
);
12423 /* Look for the final `;'. */
12424 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12429 if (parser
->in_function_body
12430 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12432 error ("%<goto%> in %<constexpr%> function");
12433 cp_function_chain
->invalid_constexpr
= true;
12436 /* Create the goto-statement. */
12437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12439 /* Issue a warning about this use of a GNU extension. */
12440 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12441 /* Consume the '*' token. */
12442 cp_lexer_consume_token (parser
->lexer
);
12443 /* Parse the dependent expression. */
12444 finish_goto_stmt (cp_parser_expression (parser
));
12447 finish_goto_stmt (cp_parser_identifier (parser
));
12448 /* Look for the final `;'. */
12449 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12453 cp_parser_error (parser
, "expected jump-statement");
12460 /* Parse a declaration-statement.
12462 declaration-statement:
12463 block-declaration */
12466 cp_parser_declaration_statement (cp_parser
* parser
)
12470 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12471 p
= obstack_alloc (&declarator_obstack
, 0);
12473 /* Parse the block-declaration. */
12474 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12476 /* Free any declarators allocated. */
12477 obstack_free (&declarator_obstack
, p
);
12480 /* Some dependent statements (like `if (cond) statement'), are
12481 implicitly in their own scope. In other words, if the statement is
12482 a single statement (as opposed to a compound-statement), it is
12483 none-the-less treated as if it were enclosed in braces. Any
12484 declarations appearing in the dependent statement are out of scope
12485 after control passes that point. This function parses a statement,
12486 but ensures that is in its own scope, even if it is not a
12487 compound-statement.
12489 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12490 is a (possibly labeled) if statement which is not enclosed in
12491 braces and has an else clause. This is used to implement
12494 CHAIN is a vector of if-else-if conditions. This is used to implement
12497 Returns the new statement. */
12500 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12501 const token_indent_info
&guard_tinfo
,
12505 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12506 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12507 token_indent_info body_tinfo
12508 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12513 /* Mark if () ; with a special NOP_EXPR. */
12514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12516 cp_lexer_consume_token (parser
->lexer
);
12517 statement
= add_stmt (build_empty_stmt (body_loc
));
12519 if (guard_tinfo
.keyword
== RID_IF
12520 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12521 warning_at (body_loc
, OPT_Wempty_body
,
12522 "suggest braces around empty body in an %<if%> statement");
12523 else if (guard_tinfo
.keyword
== RID_ELSE
)
12524 warning_at (body_loc
, OPT_Wempty_body
,
12525 "suggest braces around empty body in an %<else%> statement");
12527 /* if a compound is opened, we simply parse the statement directly. */
12528 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12529 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12530 /* If the token is not a `{', then we must take special action. */
12533 /* Create a compound-statement. */
12534 statement
= begin_compound_stmt (0);
12535 /* Parse the dependent-statement. */
12536 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12537 &body_loc_after_labels
);
12538 /* Finish the dummy compound-statement. */
12539 finish_compound_stmt (statement
);
12542 token_indent_info next_tinfo
12543 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12544 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12546 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12547 && next_tinfo
.type
!= CPP_SEMICOLON
)
12548 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12549 guard_tinfo
.location
, guard_tinfo
.keyword
);
12551 /* Return the statement. */
12555 /* For some dependent statements (like `while (cond) statement'), we
12556 have already created a scope. Therefore, even if the dependent
12557 statement is a compound-statement, we do not want to create another
12561 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12562 const token_indent_info
&guard_tinfo
)
12564 /* If the token is a `{', then we must take special action. */
12565 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12567 token_indent_info body_tinfo
12568 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12569 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12571 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12572 &loc_after_labels
);
12573 token_indent_info next_tinfo
12574 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12575 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12577 if (loc_after_labels
!= UNKNOWN_LOCATION
12578 && next_tinfo
.type
!= CPP_SEMICOLON
)
12579 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12580 guard_tinfo
.location
,
12581 guard_tinfo
.keyword
);
12585 /* Avoid calling cp_parser_compound_statement, so that we
12586 don't create a new scope. Do everything else by hand. */
12587 matching_braces braces
;
12588 braces
.require_open (parser
);
12589 /* If the next keyword is `__label__' we have a label declaration. */
12590 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12591 cp_parser_label_declaration (parser
);
12592 /* Parse an (optional) statement-seq. */
12593 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12594 braces
.require_close (parser
);
12598 /* Declarations [gram.dcl.dcl] */
12600 /* Parse an optional declaration-sequence.
12604 declaration-seq declaration */
12607 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12613 token
= cp_lexer_peek_token (parser
->lexer
);
12615 if (token
->type
== CPP_CLOSE_BRACE
12616 || token
->type
== CPP_EOF
12617 || token
->type
== CPP_PRAGMA_EOL
)
12620 if (token
->type
== CPP_SEMICOLON
)
12622 /* A declaration consisting of a single semicolon is
12623 invalid. Allow it unless we're being pedantic. */
12624 cp_lexer_consume_token (parser
->lexer
);
12625 if (!in_system_header_at (input_location
))
12626 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12630 /* If we're entering or exiting a region that's implicitly
12631 extern "C", modify the lang context appropriately. */
12632 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12634 push_lang_context (lang_name_c
);
12635 parser
->implicit_extern_c
= true;
12637 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12639 pop_lang_context ();
12640 parser
->implicit_extern_c
= false;
12643 if (token
->type
== CPP_PRAGMA
)
12645 /* A top-level declaration can consist solely of a #pragma.
12646 A nested declaration cannot, so this is done here and not
12647 in cp_parser_declaration. (A #pragma at block scope is
12648 handled in cp_parser_statement.) */
12649 cp_parser_pragma (parser
, pragma_external
, NULL
);
12653 /* Parse the declaration itself. */
12654 cp_parser_declaration (parser
);
12658 /* Parse a declaration.
12662 function-definition
12663 template-declaration
12664 explicit-instantiation
12665 explicit-specialization
12666 linkage-specification
12667 namespace-definition
12675 __extension__ declaration */
12678 cp_parser_declaration (cp_parser
* parser
)
12682 int saved_pedantic
;
12684 tree attributes
= NULL_TREE
;
12686 /* Check for the `__extension__' keyword. */
12687 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12689 /* Parse the qualified declaration. */
12690 cp_parser_declaration (parser
);
12691 /* Restore the PEDANTIC flag. */
12692 pedantic
= saved_pedantic
;
12697 /* Try to figure out what kind of declaration is present. */
12698 token1
= *cp_lexer_peek_token (parser
->lexer
);
12700 if (token1
.type
!= CPP_EOF
)
12701 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12704 token2
.type
= CPP_EOF
;
12705 token2
.keyword
= RID_MAX
;
12708 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12709 p
= obstack_alloc (&declarator_obstack
, 0);
12711 /* If the next token is `extern' and the following token is a string
12712 literal, then we have a linkage specification. */
12713 if (token1
.keyword
== RID_EXTERN
12714 && cp_parser_is_pure_string_literal (&token2
))
12715 cp_parser_linkage_specification (parser
);
12716 /* If the next token is `template', then we have either a template
12717 declaration, an explicit instantiation, or an explicit
12719 else if (token1
.keyword
== RID_TEMPLATE
)
12721 /* `template <>' indicates a template specialization. */
12722 if (token2
.type
== CPP_LESS
12723 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12724 cp_parser_explicit_specialization (parser
);
12725 /* `template <' indicates a template declaration. */
12726 else if (token2
.type
== CPP_LESS
)
12727 cp_parser_template_declaration (parser
, /*member_p=*/false);
12728 /* Anything else must be an explicit instantiation. */
12730 cp_parser_explicit_instantiation (parser
);
12732 /* If the next token is `export', then we have a template
12734 else if (token1
.keyword
== RID_EXPORT
)
12735 cp_parser_template_declaration (parser
, /*member_p=*/false);
12736 /* If the next token is `extern', 'static' or 'inline' and the one
12737 after that is `template', we have a GNU extended explicit
12738 instantiation directive. */
12739 else if (cp_parser_allow_gnu_extensions_p (parser
)
12740 && (token1
.keyword
== RID_EXTERN
12741 || token1
.keyword
== RID_STATIC
12742 || token1
.keyword
== RID_INLINE
)
12743 && token2
.keyword
== RID_TEMPLATE
)
12744 cp_parser_explicit_instantiation (parser
);
12745 /* If the next token is `namespace', check for a named or unnamed
12746 namespace definition. */
12747 else if (token1
.keyword
== RID_NAMESPACE
12748 && (/* A named namespace definition. */
12749 (token2
.type
== CPP_NAME
12750 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12752 || (token2
.type
== CPP_OPEN_SQUARE
12753 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12754 == CPP_OPEN_SQUARE
)
12755 /* An unnamed namespace definition. */
12756 || token2
.type
== CPP_OPEN_BRACE
12757 || token2
.keyword
== RID_ATTRIBUTE
))
12758 cp_parser_namespace_definition (parser
);
12759 /* An inline (associated) namespace definition. */
12760 else if (token1
.keyword
== RID_INLINE
12761 && token2
.keyword
== RID_NAMESPACE
)
12762 cp_parser_namespace_definition (parser
);
12763 /* Objective-C++ declaration/definition. */
12764 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12765 cp_parser_objc_declaration (parser
, NULL_TREE
);
12766 else if (c_dialect_objc ()
12767 && token1
.keyword
== RID_ATTRIBUTE
12768 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12769 cp_parser_objc_declaration (parser
, attributes
);
12770 /* At this point we may have a template declared by a concept
12772 else if (flag_concepts
12773 && cp_parser_template_declaration_after_export (parser
,
12774 /*member_p=*/false))
12777 /* Try to parse a block-declaration, or a function-definition. */
12778 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12780 /* Free any declarators allocated. */
12781 obstack_free (&declarator_obstack
, p
);
12784 /* Parse a block-declaration.
12789 namespace-alias-definition
12796 __extension__ block-declaration
12801 static_assert-declaration
12803 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12804 part of a declaration-statement. */
12807 cp_parser_block_declaration (cp_parser
*parser
,
12811 int saved_pedantic
;
12813 /* Check for the `__extension__' keyword. */
12814 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12816 /* Parse the qualified declaration. */
12817 cp_parser_block_declaration (parser
, statement_p
);
12818 /* Restore the PEDANTIC flag. */
12819 pedantic
= saved_pedantic
;
12824 /* Peek at the next token to figure out which kind of declaration is
12826 token1
= cp_lexer_peek_token (parser
->lexer
);
12828 /* If the next keyword is `asm', we have an asm-definition. */
12829 if (token1
->keyword
== RID_ASM
)
12832 cp_parser_commit_to_tentative_parse (parser
);
12833 cp_parser_asm_definition (parser
);
12835 /* If the next keyword is `namespace', we have a
12836 namespace-alias-definition. */
12837 else if (token1
->keyword
== RID_NAMESPACE
)
12838 cp_parser_namespace_alias_definition (parser
);
12839 /* If the next keyword is `using', we have a
12840 using-declaration, a using-directive, or an alias-declaration. */
12841 else if (token1
->keyword
== RID_USING
)
12846 cp_parser_commit_to_tentative_parse (parser
);
12847 /* If the token after `using' is `namespace', then we have a
12848 using-directive. */
12849 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12850 if (token2
->keyword
== RID_NAMESPACE
)
12851 cp_parser_using_directive (parser
);
12852 /* If the second token after 'using' is '=', then we have an
12853 alias-declaration. */
12854 else if (cxx_dialect
>= cxx11
12855 && token2
->type
== CPP_NAME
12856 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12857 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12858 cp_parser_alias_declaration (parser
);
12859 /* Otherwise, it's a using-declaration. */
12861 cp_parser_using_declaration (parser
,
12862 /*access_declaration_p=*/false);
12864 /* If the next keyword is `__label__' we have a misplaced label
12866 else if (token1
->keyword
== RID_LABEL
)
12868 cp_lexer_consume_token (parser
->lexer
);
12869 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12870 cp_parser_skip_to_end_of_statement (parser
);
12871 /* If the next token is now a `;', consume it. */
12872 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12873 cp_lexer_consume_token (parser
->lexer
);
12875 /* If the next token is `static_assert' we have a static assertion. */
12876 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12877 cp_parser_static_assert (parser
, /*member_p=*/false);
12878 /* Anything else must be a simple-declaration. */
12880 cp_parser_simple_declaration (parser
, !statement_p
,
12881 /*maybe_range_for_decl*/NULL
);
12884 /* Parse a simple-declaration.
12886 simple-declaration:
12887 decl-specifier-seq [opt] init-declarator-list [opt] ;
12888 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12889 brace-or-equal-initializer ;
12891 init-declarator-list:
12893 init-declarator-list , init-declarator
12895 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12896 function-definition as a simple-declaration.
12898 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12899 parsed declaration if it is an uninitialized single declarator not followed
12900 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12901 if present, will not be consumed. */
12904 cp_parser_simple_declaration (cp_parser
* parser
,
12905 bool function_definition_allowed_p
,
12906 tree
*maybe_range_for_decl
)
12908 cp_decl_specifier_seq decl_specifiers
;
12909 int declares_class_or_enum
;
12910 bool saw_declarator
;
12911 location_t comma_loc
= UNKNOWN_LOCATION
;
12912 location_t init_loc
= UNKNOWN_LOCATION
;
12914 if (maybe_range_for_decl
)
12915 *maybe_range_for_decl
= NULL_TREE
;
12917 /* Defer access checks until we know what is being declared; the
12918 checks for names appearing in the decl-specifier-seq should be
12919 done as if we were in the scope of the thing being declared. */
12920 push_deferring_access_checks (dk_deferred
);
12922 /* Parse the decl-specifier-seq. We have to keep track of whether
12923 or not the decl-specifier-seq declares a named class or
12924 enumeration type, since that is the only case in which the
12925 init-declarator-list is allowed to be empty.
12929 In a simple-declaration, the optional init-declarator-list can be
12930 omitted only when declaring a class or enumeration, that is when
12931 the decl-specifier-seq contains either a class-specifier, an
12932 elaborated-type-specifier, or an enum-specifier. */
12933 cp_parser_decl_specifier_seq (parser
,
12934 CP_PARSER_FLAGS_OPTIONAL
,
12936 &declares_class_or_enum
);
12937 /* We no longer need to defer access checks. */
12938 stop_deferring_access_checks ();
12940 /* In a block scope, a valid declaration must always have a
12941 decl-specifier-seq. By not trying to parse declarators, we can
12942 resolve the declaration/expression ambiguity more quickly. */
12943 if (!function_definition_allowed_p
12944 && !decl_specifiers
.any_specifiers_p
)
12946 cp_parser_error (parser
, "expected declaration");
12950 /* If the next two tokens are both identifiers, the code is
12951 erroneous. The usual cause of this situation is code like:
12955 where "T" should name a type -- but does not. */
12956 if (!decl_specifiers
.any_type_specifiers_p
12957 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12959 /* If parsing tentatively, we should commit; we really are
12960 looking at a declaration. */
12961 cp_parser_commit_to_tentative_parse (parser
);
12966 /* If we have seen at least one decl-specifier, and the next token
12967 is not a parenthesis, then we must be looking at a declaration.
12968 (After "int (" we might be looking at a functional cast.) */
12969 if (decl_specifiers
.any_specifiers_p
12970 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12971 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12972 && !cp_parser_error_occurred (parser
))
12973 cp_parser_commit_to_tentative_parse (parser
);
12975 /* Look for C++17 decomposition declaration. */
12976 for (size_t n
= 1; ; n
++)
12977 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12978 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12980 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12981 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12982 && decl_specifiers
.any_specifiers_p
)
12985 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12986 maybe_range_for_decl
,
12989 /* The next token should be either a `,' or a `;'. */
12990 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12991 /* If it's a `;', we are done. */
12992 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12994 /* Anything else is an error. */
12997 /* If we have already issued an error message we don't need
12998 to issue another one. */
12999 if ((decl
!= error_mark_node
13000 && DECL_INITIAL (decl
) != error_mark_node
)
13001 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13002 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13003 /* Skip tokens until we reach the end of the statement. */
13004 cp_parser_skip_to_end_of_statement (parser
);
13005 /* If the next token is now a `;', consume it. */
13006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13007 cp_lexer_consume_token (parser
->lexer
);
13015 bool auto_specifier_p
;
13016 /* NULL_TREE if both variable and function declaration are allowed,
13017 error_mark_node if function declaration are not allowed and
13018 a FUNCTION_DECL that should be diagnosed if it is followed by
13019 variable declarations. */
13020 tree auto_function_declaration
;
13022 last_type
= NULL_TREE
;
13024 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13025 auto_function_declaration
= NULL_TREE
;
13027 /* Keep going until we hit the `;' at the end of the simple
13029 saw_declarator
= false;
13030 while (cp_lexer_next_token_is_not (parser
->lexer
,
13034 bool function_definition_p
;
13036 tree auto_result
= NULL_TREE
;
13038 if (saw_declarator
)
13040 /* If we are processing next declarator, comma is expected */
13041 token
= cp_lexer_peek_token (parser
->lexer
);
13042 gcc_assert (token
->type
== CPP_COMMA
);
13043 cp_lexer_consume_token (parser
->lexer
);
13044 if (maybe_range_for_decl
)
13046 *maybe_range_for_decl
= error_mark_node
;
13047 if (comma_loc
== UNKNOWN_LOCATION
)
13048 comma_loc
= token
->location
;
13052 saw_declarator
= true;
13054 /* Parse the init-declarator. */
13055 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13057 function_definition_allowed_p
,
13058 /*member_p=*/false,
13059 declares_class_or_enum
,
13060 &function_definition_p
,
13061 maybe_range_for_decl
,
13064 /* If an error occurred while parsing tentatively, exit quickly.
13065 (That usually happens when in the body of a function; each
13066 statement is treated as a declaration-statement until proven
13068 if (cp_parser_error_occurred (parser
))
13071 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13073 /* If the init-declarator-list contains more than one
13074 init-declarator, they shall all form declarations of
13076 if (auto_function_declaration
== NULL_TREE
)
13077 auto_function_declaration
13078 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13079 else if (TREE_CODE (decl
) == FUNCTION_DECL
13080 || auto_function_declaration
!= error_mark_node
)
13082 error_at (decl_specifiers
.locations
[ds_type_spec
],
13083 "non-variable %qD in declaration with more than one "
13084 "declarator with placeholder type",
13085 TREE_CODE (decl
) == FUNCTION_DECL
13086 ? decl
: auto_function_declaration
);
13087 auto_function_declaration
= error_mark_node
;
13092 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13095 && last_type
!= error_mark_node
13096 && !same_type_p (auto_result
, last_type
))
13098 /* If the list of declarators contains more than one declarator,
13099 the type of each declared variable is determined as described
13100 above. If the type deduced for the template parameter U is not
13101 the same in each deduction, the program is ill-formed. */
13102 error_at (decl_specifiers
.locations
[ds_type_spec
],
13103 "inconsistent deduction for %qT: %qT and then %qT",
13104 decl_specifiers
.type
, last_type
, auto_result
);
13105 last_type
= error_mark_node
;
13108 last_type
= auto_result
;
13111 /* Handle function definitions specially. */
13112 if (function_definition_p
)
13114 /* If the next token is a `,', then we are probably
13115 processing something like:
13119 which is erroneous. */
13120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13122 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13123 error_at (token
->location
,
13125 " declarations and function-definitions is forbidden");
13127 /* Otherwise, we're done with the list of declarators. */
13130 pop_deferring_access_checks ();
13134 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13135 *maybe_range_for_decl
= decl
;
13136 /* The next token should be either a `,' or a `;'. */
13137 token
= cp_lexer_peek_token (parser
->lexer
);
13138 /* If it's a `,', there are more declarators to come. */
13139 if (token
->type
== CPP_COMMA
)
13140 /* will be consumed next time around */;
13141 /* If it's a `;', we are done. */
13142 else if (token
->type
== CPP_SEMICOLON
)
13144 else if (maybe_range_for_decl
)
13146 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13147 permerror (decl_specifiers
.locations
[ds_type_spec
],
13148 "types may not be defined in a for-range-declaration");
13151 /* Anything else is an error. */
13154 /* If we have already issued an error message we don't need
13155 to issue another one. */
13156 if ((decl
!= error_mark_node
13157 && DECL_INITIAL (decl
) != error_mark_node
)
13158 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13159 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13160 /* Skip tokens until we reach the end of the statement. */
13161 cp_parser_skip_to_end_of_statement (parser
);
13162 /* If the next token is now a `;', consume it. */
13163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13164 cp_lexer_consume_token (parser
->lexer
);
13167 /* After the first time around, a function-definition is not
13168 allowed -- even if it was OK at first. For example:
13173 function_definition_allowed_p
= false;
13176 /* Issue an error message if no declarators are present, and the
13177 decl-specifier-seq does not itself declare a class or
13178 enumeration: [dcl.dcl]/3. */
13179 if (!saw_declarator
)
13181 if (cp_parser_declares_only_class_p (parser
))
13183 if (!declares_class_or_enum
13184 && decl_specifiers
.type
13185 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13186 /* Ensure an error is issued anyway when finish_decltype_type,
13187 called via cp_parser_decl_specifier_seq, returns a class or
13188 an enumeration (c++/51786). */
13189 decl_specifiers
.type
= NULL_TREE
;
13190 shadow_tag (&decl_specifiers
);
13192 /* Perform any deferred access checks. */
13193 perform_deferred_access_checks (tf_warning_or_error
);
13196 /* Consume the `;'. */
13198 if (!maybe_range_for_decl
)
13199 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13200 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13202 if (init_loc
!= UNKNOWN_LOCATION
)
13203 error_at (init_loc
, "initializer in range-based %<for%> loop");
13204 if (comma_loc
!= UNKNOWN_LOCATION
)
13205 error_at (comma_loc
,
13206 "multiple declarations in range-based %<for%> loop");
13210 pop_deferring_access_checks ();
13213 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13214 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13218 cp_parser_decomposition_declaration (cp_parser
*parser
,
13219 cp_decl_specifier_seq
*decl_specifiers
,
13220 tree
*maybe_range_for_decl
,
13221 location_t
*init_loc
)
13223 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13224 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13225 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13227 /* Parse the identifier-list. */
13228 auto_vec
<cp_expr
, 10> v
;
13229 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13232 cp_expr e
= cp_parser_identifier (parser
);
13233 if (e
.get_value () == error_mark_node
)
13236 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13238 cp_lexer_consume_token (parser
->lexer
);
13241 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13242 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13244 end_loc
= UNKNOWN_LOCATION
;
13245 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13248 cp_lexer_consume_token (parser
->lexer
);
13251 cp_parser_skip_to_end_of_statement (parser
);
13252 return error_mark_node
;
13256 if (cxx_dialect
< cxx17
)
13257 pedwarn (loc
, 0, "structured bindings only available with "
13258 "-std=c++17 or -std=gnu++17");
13261 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13262 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13263 declarator
->id_loc
= loc
;
13264 if (ref_qual
!= REF_QUAL_NONE
)
13265 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13266 ref_qual
== REF_QUAL_RVALUE
,
13268 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13269 NULL_TREE
, decl_specifiers
->attributes
,
13271 tree orig_decl
= decl
;
13275 cp_decl_specifier_seq decl_specs
;
13276 clear_decl_specs (&decl_specs
);
13277 decl_specs
.type
= make_auto ();
13279 FOR_EACH_VEC_ELT (v
, i
, e
)
13282 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13284 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13285 declarator
->id_loc
= e
.get_location ();
13286 tree elt_pushed_scope
;
13287 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13288 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13289 if (decl2
== error_mark_node
)
13290 decl
= error_mark_node
;
13291 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13293 /* Ensure we've diagnosed redeclaration if we aren't creating
13295 gcc_assert (errorcount
);
13296 decl
= error_mark_node
;
13300 if (elt_pushed_scope
)
13301 pop_scope (elt_pushed_scope
);
13306 error_at (loc
, "empty structured binding declaration");
13307 decl
= error_mark_node
;
13310 if (maybe_range_for_decl
== NULL
13311 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13313 bool non_constant_p
= false, is_direct_init
= false;
13314 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13315 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13317 if (initializer
== NULL_TREE
13318 || (TREE_CODE (initializer
) == TREE_LIST
13319 && TREE_CHAIN (initializer
))
13321 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13322 && CONSTRUCTOR_NELTS (initializer
) != 1))
13324 error_at (loc
, "invalid initializer for structured binding "
13326 initializer
= error_mark_node
;
13329 if (decl
!= error_mark_node
)
13331 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13332 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13333 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13334 cp_finish_decomp (decl
, prev
, v
.length ());
13337 else if (decl
!= error_mark_node
)
13339 *maybe_range_for_decl
= prev
;
13340 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13341 the underlying DECL. */
13342 cp_finish_decomp (decl
, prev
, v
.length ());
13346 pop_scope (pushed_scope
);
13348 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13350 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13351 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13357 /* Parse a decl-specifier-seq.
13359 decl-specifier-seq:
13360 decl-specifier-seq [opt] decl-specifier
13361 decl-specifier attribute-specifier-seq [opt] (C++11)
13364 storage-class-specifier
13375 Concepts Extension:
13380 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13382 The parser flags FLAGS is used to control type-specifier parsing.
13384 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13387 1: one of the decl-specifiers is an elaborated-type-specifier
13388 (i.e., a type declaration)
13389 2: one of the decl-specifiers is an enum-specifier or a
13390 class-specifier (i.e., a type definition)
13395 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13396 cp_parser_flags flags
,
13397 cp_decl_specifier_seq
*decl_specs
,
13398 int* declares_class_or_enum
)
13400 bool constructor_possible_p
= !parser
->in_declarator_p
;
13401 bool found_decl_spec
= false;
13402 cp_token
*start_token
= NULL
;
13405 /* Clear DECL_SPECS. */
13406 clear_decl_specs (decl_specs
);
13408 /* Assume no class or enumeration type is declared. */
13409 *declares_class_or_enum
= 0;
13411 /* Keep reading specifiers until there are no more to read. */
13414 bool constructor_p
;
13418 /* Peek at the next token. */
13419 token
= cp_lexer_peek_token (parser
->lexer
);
13421 /* Save the first token of the decl spec list for error
13424 start_token
= token
;
13425 /* Handle attributes. */
13426 if (cp_next_tokens_can_be_attribute_p (parser
))
13428 /* Parse the attributes. */
13429 tree attrs
= cp_parser_attributes_opt (parser
);
13431 /* In a sequence of declaration specifiers, c++11 attributes
13432 appertain to the type that precede them. In that case
13435 The attribute-specifier-seq affects the type only for
13436 the declaration it appears in, not other declarations
13437 involving the same type.
13439 But for now let's force the user to position the
13440 attribute either at the beginning of the declaration or
13441 after the declarator-id, which would clearly mean that it
13442 applies to the declarator. */
13443 if (cxx11_attribute_p (attrs
))
13445 if (!found_decl_spec
)
13446 /* The c++11 attribute is at the beginning of the
13447 declaration. It appertains to the entity being
13451 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13453 /* This is an attribute following a
13454 class-specifier. */
13455 if (decl_specs
->type_definition_p
)
13456 warn_misplaced_attr_for_class_type (token
->location
,
13462 decl_specs
->std_attributes
13463 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13464 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13465 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13471 decl_specs
->attributes
13472 = attr_chainon (decl_specs
->attributes
, attrs
);
13473 if (decl_specs
->locations
[ds_attribute
] == 0)
13474 decl_specs
->locations
[ds_attribute
] = token
->location
;
13477 /* Assume we will find a decl-specifier keyword. */
13478 found_decl_spec
= true;
13479 /* If the next token is an appropriate keyword, we can simply
13480 add it to the list. */
13481 switch (token
->keyword
)
13487 if (!at_class_scope_p ())
13489 gcc_rich_location
richloc (token
->location
);
13490 richloc
.add_fixit_remove ();
13491 error_at (&richloc
, "%<friend%> used outside of class");
13492 cp_lexer_purge_token (parser
->lexer
);
13497 /* Consume the token. */
13498 cp_lexer_consume_token (parser
->lexer
);
13502 case RID_CONSTEXPR
:
13504 cp_lexer_consume_token (parser
->lexer
);
13509 cp_lexer_consume_token (parser
->lexer
);
13512 /* function-specifier:
13519 cp_parser_function_specifier_opt (parser
, decl_specs
);
13526 /* Consume the token. */
13527 cp_lexer_consume_token (parser
->lexer
);
13528 /* A constructor declarator cannot appear in a typedef. */
13529 constructor_possible_p
= false;
13530 /* The "typedef" keyword can only occur in a declaration; we
13531 may as well commit at this point. */
13532 cp_parser_commit_to_tentative_parse (parser
);
13534 if (decl_specs
->storage_class
!= sc_none
)
13535 decl_specs
->conflicting_specifiers_p
= true;
13538 /* storage-class-specifier:
13548 if (cxx_dialect
== cxx98
)
13550 /* Consume the token. */
13551 cp_lexer_consume_token (parser
->lexer
);
13553 /* Complain about `auto' as a storage specifier, if
13554 we're complaining about C++0x compatibility. */
13555 gcc_rich_location
richloc (token
->location
);
13556 richloc
.add_fixit_remove ();
13557 warning_at (&richloc
, OPT_Wc__11_compat
,
13558 "%<auto%> changes meaning in C++11; "
13559 "please remove it");
13561 /* Set the storage class anyway. */
13562 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13566 /* C++0x auto type-specifier. */
13567 found_decl_spec
= false;
13574 /* Consume the token. */
13575 cp_lexer_consume_token (parser
->lexer
);
13576 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13580 /* Consume the token. */
13582 cp_lexer_consume_token (parser
->lexer
);
13586 /* We did not yet find a decl-specifier yet. */
13587 found_decl_spec
= false;
13591 if (found_decl_spec
13592 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13593 && token
->keyword
!= RID_CONSTEXPR
)
13594 error ("decl-specifier invalid in condition");
13596 if (found_decl_spec
13597 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13598 && token
->keyword
!= RID_MUTABLE
13599 && token
->keyword
!= RID_CONSTEXPR
)
13600 error_at (token
->location
, "%qD invalid in lambda",
13601 ridpointers
[token
->keyword
]);
13604 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13606 /* Constructors are a special case. The `S' in `S()' is not a
13607 decl-specifier; it is the beginning of the declarator. */
13609 = (!found_decl_spec
13610 && constructor_possible_p
13611 && (cp_parser_constructor_declarator_p
13612 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13614 /* If we don't have a DECL_SPEC yet, then we must be looking at
13615 a type-specifier. */
13616 if (!found_decl_spec
&& !constructor_p
)
13618 int decl_spec_declares_class_or_enum
;
13619 bool is_cv_qualifier
;
13623 = cp_parser_type_specifier (parser
, flags
,
13625 /*is_declaration=*/true,
13626 &decl_spec_declares_class_or_enum
,
13628 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13630 /* If this type-specifier referenced a user-defined type
13631 (a typedef, class-name, etc.), then we can't allow any
13632 more such type-specifiers henceforth.
13636 The longest sequence of decl-specifiers that could
13637 possibly be a type name is taken as the
13638 decl-specifier-seq of a declaration. The sequence shall
13639 be self-consistent as described below.
13643 As a general rule, at most one type-specifier is allowed
13644 in the complete decl-specifier-seq of a declaration. The
13645 only exceptions are the following:
13647 -- const or volatile can be combined with any other
13650 -- signed or unsigned can be combined with char, long,
13658 void g (const int Pc);
13660 Here, Pc is *not* part of the decl-specifier seq; it's
13661 the declarator. Therefore, once we see a type-specifier
13662 (other than a cv-qualifier), we forbid any additional
13663 user-defined types. We *do* still allow things like `int
13664 int' to be considered a decl-specifier-seq, and issue the
13665 error message later. */
13666 if (type_spec
&& !is_cv_qualifier
)
13667 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13668 /* A constructor declarator cannot follow a type-specifier. */
13671 constructor_possible_p
= false;
13672 found_decl_spec
= true;
13673 if (!is_cv_qualifier
)
13674 decl_specs
->any_type_specifiers_p
= true;
13678 /* If we still do not have a DECL_SPEC, then there are no more
13679 decl-specifiers. */
13680 if (!found_decl_spec
)
13683 decl_specs
->any_specifiers_p
= true;
13684 /* After we see one decl-specifier, further decl-specifiers are
13685 always optional. */
13686 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13689 /* Don't allow a friend specifier with a class definition. */
13690 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13691 && (*declares_class_or_enum
& 2))
13692 error_at (decl_specs
->locations
[ds_friend
],
13693 "class definition may not be declared a friend");
13696 /* Parse an (optional) storage-class-specifier.
13698 storage-class-specifier:
13707 storage-class-specifier:
13710 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13713 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13715 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13718 if (cxx_dialect
!= cxx98
)
13720 /* Fall through for C++98. */
13721 gcc_fallthrough ();
13728 /* Consume the token. */
13729 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13736 /* Parse an (optional) function-specifier.
13738 function-specifier:
13743 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13744 Updates DECL_SPECS, if it is non-NULL. */
13747 cp_parser_function_specifier_opt (cp_parser
* parser
,
13748 cp_decl_specifier_seq
*decl_specs
)
13750 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13751 switch (token
->keyword
)
13754 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13758 /* 14.5.2.3 [temp.mem]
13760 A member function template shall not be virtual. */
13761 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13762 && current_class_type
)
13763 error_at (token
->location
, "templates may not be %<virtual%>");
13765 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13769 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13776 /* Consume the token. */
13777 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13780 /* Parse a linkage-specification.
13782 linkage-specification:
13783 extern string-literal { declaration-seq [opt] }
13784 extern string-literal declaration */
13787 cp_parser_linkage_specification (cp_parser
* parser
)
13791 /* Look for the `extern' keyword. */
13792 cp_token
*extern_token
13793 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13795 /* Look for the string-literal. */
13796 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13797 linkage
= cp_parser_string_literal (parser
, false, false);
13799 /* Transform the literal into an identifier. If the literal is a
13800 wide-character string, or contains embedded NULs, then we can't
13801 handle it as the user wants. */
13802 if (strlen (TREE_STRING_POINTER (linkage
))
13803 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13805 cp_parser_error (parser
, "invalid linkage-specification");
13806 /* Assume C++ linkage. */
13807 linkage
= lang_name_cplusplus
;
13810 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13812 /* We're now using the new linkage. */
13813 push_lang_context (linkage
);
13815 /* Preserve the location of the the innermost linkage specification,
13816 tracking the locations of nested specifications via a local. */
13817 location_t saved_location
13818 = parser
->innermost_linkage_specification_location
;
13819 /* Construct a location ranging from the start of the "extern" to
13820 the end of the string-literal, with the caret at the start, e.g.:
13824 parser
->innermost_linkage_specification_location
13825 = make_location (extern_token
->location
,
13826 extern_token
->location
,
13827 get_finish (string_token
->location
));
13829 /* If the next token is a `{', then we're using the first
13831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13833 cp_ensure_no_omp_declare_simd (parser
);
13834 cp_ensure_no_oacc_routine (parser
);
13836 /* Consume the `{' token. */
13837 matching_braces braces
;
13838 braces
.consume_open (parser
)->location
;
13839 /* Parse the declarations. */
13840 cp_parser_declaration_seq_opt (parser
);
13841 /* Look for the closing `}'. */
13842 braces
.require_close (parser
);
13844 /* Otherwise, there's just one declaration. */
13847 bool saved_in_unbraced_linkage_specification_p
;
13849 saved_in_unbraced_linkage_specification_p
13850 = parser
->in_unbraced_linkage_specification_p
;
13851 parser
->in_unbraced_linkage_specification_p
= true;
13852 cp_parser_declaration (parser
);
13853 parser
->in_unbraced_linkage_specification_p
13854 = saved_in_unbraced_linkage_specification_p
;
13857 /* We're done with the linkage-specification. */
13858 pop_lang_context ();
13860 /* Restore location of parent linkage specification, if any. */
13861 parser
->innermost_linkage_specification_location
= saved_location
;
13864 /* Parse a static_assert-declaration.
13866 static_assert-declaration:
13867 static_assert ( constant-expression , string-literal ) ;
13868 static_assert ( constant-expression ) ; (C++17)
13870 If MEMBER_P, this static_assert is a class member. */
13873 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13876 location_t token_loc
;
13880 /* Peek at the `static_assert' token so we can keep track of exactly
13881 where the static assertion started. */
13882 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13884 /* Look for the `static_assert' keyword. */
13885 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13889 /* We know we are in a static assertion; commit to any tentative
13891 if (cp_parser_parsing_tentatively (parser
))
13892 cp_parser_commit_to_tentative_parse (parser
);
13894 /* Parse the `(' starting the static assertion condition. */
13895 matching_parens parens
;
13896 parens
.require_open (parser
);
13898 /* Parse the constant-expression. Allow a non-constant expression
13899 here in order to give better diagnostics in finish_static_assert. */
13901 cp_parser_constant_expression (parser
,
13902 /*allow_non_constant_p=*/true,
13903 /*non_constant_p=*/&dummy
);
13905 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13907 if (cxx_dialect
< cxx17
)
13908 pedwarn (input_location
, OPT_Wpedantic
,
13909 "static_assert without a message "
13910 "only available with -std=c++17 or -std=gnu++17");
13912 cp_lexer_consume_token (parser
->lexer
);
13913 message
= build_string (1, "");
13914 TREE_TYPE (message
) = char_array_type_node
;
13915 fix_string_type (message
);
13919 /* Parse the separating `,'. */
13920 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13922 /* Parse the string-literal message. */
13923 message
= cp_parser_string_literal (parser
,
13924 /*translate=*/false,
13927 /* A `)' completes the static assertion. */
13928 if (!parens
.require_close (parser
))
13929 cp_parser_skip_to_closing_parenthesis (parser
,
13930 /*recovering=*/true,
13931 /*or_comma=*/false,
13932 /*consume_paren=*/true);
13935 /* A semicolon terminates the declaration. */
13936 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13938 /* Get the location for the static assertion. Use that of the
13939 condition if available, otherwise, use that of the "static_assert"
13941 location_t assert_loc
= condition
.get_location ();
13942 if (assert_loc
== UNKNOWN_LOCATION
)
13943 assert_loc
= token_loc
;
13945 /* Complete the static assertion, which may mean either processing
13946 the static assert now or saving it for template instantiation. */
13947 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13950 /* Parse the expression in decltype ( expression ). */
13953 cp_parser_decltype_expr (cp_parser
*parser
,
13954 bool &id_expression_or_member_access_p
)
13956 cp_token
*id_expr_start_token
;
13959 /* Since we're going to preserve any side-effects from this parse, set up a
13960 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13961 in the expression. */
13962 tentative_firewall
firewall (parser
);
13964 /* First, try parsing an id-expression. */
13965 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13966 cp_parser_parse_tentatively (parser
);
13967 expr
= cp_parser_id_expression (parser
,
13968 /*template_keyword_p=*/false,
13969 /*check_dependency_p=*/true,
13970 /*template_p=*/NULL
,
13971 /*declarator_p=*/false,
13972 /*optional_p=*/false);
13974 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13976 bool non_integral_constant_expression_p
= false;
13977 tree id_expression
= expr
;
13979 const char *error_msg
;
13981 if (identifier_p (expr
))
13982 /* Lookup the name we got back from the id-expression. */
13983 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13984 id_expr_start_token
->location
);
13987 && expr
!= error_mark_node
13988 && TREE_CODE (expr
) != TYPE_DECL
13989 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13990 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13991 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13993 /* Complete lookup of the id-expression. */
13994 expr
= (finish_id_expression
13995 (id_expression
, expr
, parser
->scope
, &idk
,
13996 /*integral_constant_expression_p=*/false,
13997 /*allow_non_integral_constant_expression_p=*/true,
13998 &non_integral_constant_expression_p
,
13999 /*template_p=*/false,
14001 /*address_p=*/false,
14002 /*template_arg_p=*/false,
14004 id_expr_start_token
->location
));
14006 if (expr
== error_mark_node
)
14007 /* We found an id-expression, but it was something that we
14008 should not have found. This is an error, not something
14009 we can recover from, so note that we found an
14010 id-expression and we'll recover as gracefully as
14012 id_expression_or_member_access_p
= true;
14016 && expr
!= error_mark_node
14017 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14018 /* We have an id-expression. */
14019 id_expression_or_member_access_p
= true;
14022 if (!id_expression_or_member_access_p
)
14024 /* Abort the id-expression parse. */
14025 cp_parser_abort_tentative_parse (parser
);
14027 /* Parsing tentatively, again. */
14028 cp_parser_parse_tentatively (parser
);
14030 /* Parse a class member access. */
14031 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14032 /*cast_p=*/false, /*decltype*/true,
14033 /*member_access_only_p=*/true, NULL
);
14036 && expr
!= error_mark_node
14037 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14038 /* We have an id-expression. */
14039 id_expression_or_member_access_p
= true;
14042 if (id_expression_or_member_access_p
)
14043 /* We have parsed the complete id-expression or member access. */
14044 cp_parser_parse_definitely (parser
);
14047 /* Abort our attempt to parse an id-expression or member access
14049 cp_parser_abort_tentative_parse (parser
);
14051 /* Parse a full expression. */
14052 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14053 /*decltype_p=*/true);
14059 /* Parse a `decltype' type. Returns the type.
14061 simple-type-specifier:
14062 decltype ( expression )
14064 decltype ( auto ) */
14067 cp_parser_decltype (cp_parser
*parser
)
14069 bool id_expression_or_member_access_p
= false;
14070 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14072 if (start_token
->type
== CPP_DECLTYPE
)
14074 /* Already parsed. */
14075 cp_lexer_consume_token (parser
->lexer
);
14076 return saved_checks_value (start_token
->u
.tree_check_value
);
14079 /* Look for the `decltype' token. */
14080 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14081 return error_mark_node
;
14083 /* Parse the opening `('. */
14084 matching_parens parens
;
14085 if (!parens
.require_open (parser
))
14086 return error_mark_node
;
14088 push_deferring_access_checks (dk_deferred
);
14090 tree expr
= NULL_TREE
;
14092 if (cxx_dialect
>= cxx14
14093 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14094 /* decltype (auto) */
14095 cp_lexer_consume_token (parser
->lexer
);
14098 /* decltype (expression) */
14100 /* Types cannot be defined in a `decltype' expression. Save away the
14101 old message and set the new one. */
14102 const char *saved_message
= parser
->type_definition_forbidden_message
;
14103 parser
->type_definition_forbidden_message
14104 = G_("types may not be defined in %<decltype%> expressions");
14106 /* The restrictions on constant-expressions do not apply inside
14107 decltype expressions. */
14108 bool saved_integral_constant_expression_p
14109 = parser
->integral_constant_expression_p
;
14110 bool saved_non_integral_constant_expression_p
14111 = parser
->non_integral_constant_expression_p
;
14112 parser
->integral_constant_expression_p
= false;
14114 /* Within a parenthesized expression, a `>' token is always
14115 the greater-than operator. */
14116 bool saved_greater_than_is_operator_p
14117 = parser
->greater_than_is_operator_p
;
14118 parser
->greater_than_is_operator_p
= true;
14120 /* Do not actually evaluate the expression. */
14121 ++cp_unevaluated_operand
;
14123 /* Do not warn about problems with the expression. */
14124 ++c_inhibit_evaluation_warnings
;
14126 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14128 /* Go back to evaluating expressions. */
14129 --cp_unevaluated_operand
;
14130 --c_inhibit_evaluation_warnings
;
14132 /* The `>' token might be the end of a template-id or
14133 template-parameter-list now. */
14134 parser
->greater_than_is_operator_p
14135 = saved_greater_than_is_operator_p
;
14137 /* Restore the old message and the integral constant expression
14139 parser
->type_definition_forbidden_message
= saved_message
;
14140 parser
->integral_constant_expression_p
14141 = saved_integral_constant_expression_p
;
14142 parser
->non_integral_constant_expression_p
14143 = saved_non_integral_constant_expression_p
;
14146 /* Parse to the closing `)'. */
14147 if (!parens
.require_close (parser
))
14149 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14150 /*consume_paren=*/true);
14151 pop_deferring_access_checks ();
14152 return error_mark_node
;
14158 expr
= make_decltype_auto ();
14159 AUTO_IS_DECLTYPE (expr
) = true;
14162 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14163 tf_warning_or_error
);
14165 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14167 start_token
->type
= CPP_DECLTYPE
;
14168 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14169 start_token
->u
.tree_check_value
->value
= expr
;
14170 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14171 start_token
->keyword
= RID_MAX
;
14172 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14174 pop_to_parent_deferring_access_checks ();
14179 /* Special member functions [gram.special] */
14181 /* Parse a conversion-function-id.
14183 conversion-function-id:
14184 operator conversion-type-id
14186 Returns an IDENTIFIER_NODE representing the operator. */
14189 cp_parser_conversion_function_id (cp_parser
* parser
)
14193 tree saved_qualifying_scope
;
14194 tree saved_object_scope
;
14195 tree pushed_scope
= NULL_TREE
;
14197 /* Look for the `operator' token. */
14198 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14199 return error_mark_node
;
14200 /* When we parse the conversion-type-id, the current scope will be
14201 reset. However, we need that information in able to look up the
14202 conversion function later, so we save it here. */
14203 saved_scope
= parser
->scope
;
14204 saved_qualifying_scope
= parser
->qualifying_scope
;
14205 saved_object_scope
= parser
->object_scope
;
14206 /* We must enter the scope of the class so that the names of
14207 entities declared within the class are available in the
14208 conversion-type-id. For example, consider:
14215 S::operator I() { ... }
14217 In order to see that `I' is a type-name in the definition, we
14218 must be in the scope of `S'. */
14220 pushed_scope
= push_scope (saved_scope
);
14221 /* Parse the conversion-type-id. */
14222 type
= cp_parser_conversion_type_id (parser
);
14223 /* Leave the scope of the class, if any. */
14225 pop_scope (pushed_scope
);
14226 /* Restore the saved scope. */
14227 parser
->scope
= saved_scope
;
14228 parser
->qualifying_scope
= saved_qualifying_scope
;
14229 parser
->object_scope
= saved_object_scope
;
14230 /* If the TYPE is invalid, indicate failure. */
14231 if (type
== error_mark_node
)
14232 return error_mark_node
;
14233 return make_conv_op_name (type
);
14236 /* Parse a conversion-type-id:
14238 conversion-type-id:
14239 type-specifier-seq conversion-declarator [opt]
14241 Returns the TYPE specified. */
14244 cp_parser_conversion_type_id (cp_parser
* parser
)
14247 cp_decl_specifier_seq type_specifiers
;
14248 cp_declarator
*declarator
;
14249 tree type_specified
;
14250 const char *saved_message
;
14252 /* Parse the attributes. */
14253 attributes
= cp_parser_attributes_opt (parser
);
14255 saved_message
= parser
->type_definition_forbidden_message
;
14256 parser
->type_definition_forbidden_message
14257 = G_("types may not be defined in a conversion-type-id");
14259 /* Parse the type-specifiers. */
14260 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14261 /*is_trailing_return=*/false,
14264 parser
->type_definition_forbidden_message
= saved_message
;
14266 /* If that didn't work, stop. */
14267 if (type_specifiers
.type
== error_mark_node
)
14268 return error_mark_node
;
14269 /* Parse the conversion-declarator. */
14270 declarator
= cp_parser_conversion_declarator_opt (parser
);
14272 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14273 /*initialized=*/0, &attributes
);
14275 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14277 /* Don't give this error when parsing tentatively. This happens to
14278 work because we always parse this definitively once. */
14279 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14280 && type_uses_auto (type_specified
))
14282 if (cxx_dialect
< cxx14
)
14284 error ("invalid use of %<auto%> in conversion operator");
14285 return error_mark_node
;
14287 else if (template_parm_scope_p ())
14288 warning (0, "use of %<auto%> in member template "
14289 "conversion operator can never be deduced");
14292 return type_specified
;
14295 /* Parse an (optional) conversion-declarator.
14297 conversion-declarator:
14298 ptr-operator conversion-declarator [opt]
14302 static cp_declarator
*
14303 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14305 enum tree_code code
;
14306 tree class_type
, std_attributes
= NULL_TREE
;
14307 cp_cv_quals cv_quals
;
14309 /* We don't know if there's a ptr-operator next, or not. */
14310 cp_parser_parse_tentatively (parser
);
14311 /* Try the ptr-operator. */
14312 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14314 /* If it worked, look for more conversion-declarators. */
14315 if (cp_parser_parse_definitely (parser
))
14317 cp_declarator
*declarator
;
14319 /* Parse another optional declarator. */
14320 declarator
= cp_parser_conversion_declarator_opt (parser
);
14322 declarator
= cp_parser_make_indirect_declarator
14323 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14331 /* Parse an (optional) ctor-initializer.
14334 : mem-initializer-list */
14337 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14339 /* If the next token is not a `:', then there is no
14340 ctor-initializer. */
14341 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14343 /* Do default initialization of any bases and members. */
14344 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14345 finish_mem_initializers (NULL_TREE
);
14349 /* Consume the `:' token. */
14350 cp_lexer_consume_token (parser
->lexer
);
14351 /* And the mem-initializer-list. */
14352 cp_parser_mem_initializer_list (parser
);
14355 /* Parse a mem-initializer-list.
14357 mem-initializer-list:
14358 mem-initializer ... [opt]
14359 mem-initializer ... [opt] , mem-initializer-list */
14362 cp_parser_mem_initializer_list (cp_parser
* parser
)
14364 tree mem_initializer_list
= NULL_TREE
;
14365 tree target_ctor
= error_mark_node
;
14366 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14368 /* Let the semantic analysis code know that we are starting the
14369 mem-initializer-list. */
14370 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14371 error_at (token
->location
,
14372 "only constructors take member initializers");
14374 /* Loop through the list. */
14377 tree mem_initializer
;
14379 token
= cp_lexer_peek_token (parser
->lexer
);
14380 /* Parse the mem-initializer. */
14381 mem_initializer
= cp_parser_mem_initializer (parser
);
14382 /* If the next token is a `...', we're expanding member initializers. */
14383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14385 /* Consume the `...'. */
14386 cp_lexer_consume_token (parser
->lexer
);
14388 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14389 can be expanded but members cannot. */
14390 if (mem_initializer
!= error_mark_node
14391 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14393 error_at (token
->location
,
14394 "cannot expand initializer for member %qD",
14395 TREE_PURPOSE (mem_initializer
));
14396 mem_initializer
= error_mark_node
;
14399 /* Construct the pack expansion type. */
14400 if (mem_initializer
!= error_mark_node
)
14401 mem_initializer
= make_pack_expansion (mem_initializer
);
14403 if (target_ctor
!= error_mark_node
14404 && mem_initializer
!= error_mark_node
)
14406 error ("mem-initializer for %qD follows constructor delegation",
14407 TREE_PURPOSE (mem_initializer
));
14408 mem_initializer
= error_mark_node
;
14410 /* Look for a target constructor. */
14411 if (mem_initializer
!= error_mark_node
14412 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14413 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14415 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14416 if (mem_initializer_list
)
14418 error ("constructor delegation follows mem-initializer for %qD",
14419 TREE_PURPOSE (mem_initializer_list
));
14420 mem_initializer
= error_mark_node
;
14422 target_ctor
= mem_initializer
;
14424 /* Add it to the list, unless it was erroneous. */
14425 if (mem_initializer
!= error_mark_node
)
14427 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14428 mem_initializer_list
= mem_initializer
;
14430 /* If the next token is not a `,', we're done. */
14431 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14433 /* Consume the `,' token. */
14434 cp_lexer_consume_token (parser
->lexer
);
14437 /* Perform semantic analysis. */
14438 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14439 finish_mem_initializers (mem_initializer_list
);
14442 /* Parse a mem-initializer.
14445 mem-initializer-id ( expression-list [opt] )
14446 mem-initializer-id braced-init-list
14451 ( expression-list [opt] )
14453 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14454 class) or FIELD_DECL (for a non-static data member) to initialize;
14455 the TREE_VALUE is the expression-list. An empty initialization
14456 list is represented by void_list_node. */
14459 cp_parser_mem_initializer (cp_parser
* parser
)
14461 tree mem_initializer_id
;
14462 tree expression_list
;
14464 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14466 /* Find out what is being initialized. */
14467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14469 permerror (token
->location
,
14470 "anachronistic old-style base class initializer");
14471 mem_initializer_id
= NULL_TREE
;
14475 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14476 if (mem_initializer_id
== error_mark_node
)
14477 return mem_initializer_id
;
14479 member
= expand_member_init (mem_initializer_id
);
14480 if (member
&& !DECL_P (member
))
14481 in_base_initializer
= 1;
14483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14485 bool expr_non_constant_p
;
14486 cp_lexer_set_source_position (parser
->lexer
);
14487 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14488 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14489 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14490 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14494 vec
<tree
, va_gc
> *vec
;
14495 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14497 /*allow_expansion_p=*/true,
14498 /*non_constant_p=*/NULL
);
14500 return error_mark_node
;
14501 expression_list
= build_tree_list_vec (vec
);
14502 release_tree_vector (vec
);
14505 if (expression_list
== error_mark_node
)
14506 return error_mark_node
;
14507 if (!expression_list
)
14508 expression_list
= void_type_node
;
14510 in_base_initializer
= 0;
14512 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14515 /* Parse a mem-initializer-id.
14517 mem-initializer-id:
14518 :: [opt] nested-name-specifier [opt] class-name
14519 decltype-specifier (C++11)
14522 Returns a TYPE indicating the class to be initialized for the first
14523 production (and the second in C++11). Returns an IDENTIFIER_NODE
14524 indicating the data member to be initialized for the last production. */
14527 cp_parser_mem_initializer_id (cp_parser
* parser
)
14529 bool global_scope_p
;
14530 bool nested_name_specifier_p
;
14531 bool template_p
= false;
14534 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14536 /* `typename' is not allowed in this context ([temp.res]). */
14537 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14539 error_at (token
->location
,
14540 "keyword %<typename%> not allowed in this context (a qualified "
14541 "member initializer is implicitly a type)");
14542 cp_lexer_consume_token (parser
->lexer
);
14544 /* Look for the optional `::' operator. */
14546 = (cp_parser_global_scope_opt (parser
,
14547 /*current_scope_valid_p=*/false)
14549 /* Look for the optional nested-name-specifier. The simplest way to
14554 The keyword `typename' is not permitted in a base-specifier or
14555 mem-initializer; in these contexts a qualified name that
14556 depends on a template-parameter is implicitly assumed to be a
14559 is to assume that we have seen the `typename' keyword at this
14561 nested_name_specifier_p
14562 = (cp_parser_nested_name_specifier_opt (parser
,
14563 /*typename_keyword_p=*/true,
14564 /*check_dependency_p=*/true,
14566 /*is_declaration=*/true)
14568 if (nested_name_specifier_p
)
14569 template_p
= cp_parser_optional_template_keyword (parser
);
14570 /* If there is a `::' operator or a nested-name-specifier, then we
14571 are definitely looking for a class-name. */
14572 if (global_scope_p
|| nested_name_specifier_p
)
14573 return cp_parser_class_name (parser
,
14574 /*typename_keyword_p=*/true,
14575 /*template_keyword_p=*/template_p
,
14577 /*check_dependency_p=*/true,
14578 /*class_head_p=*/false,
14579 /*is_declaration=*/true);
14580 /* Otherwise, we could also be looking for an ordinary identifier. */
14581 cp_parser_parse_tentatively (parser
);
14582 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14583 /* Try a decltype-specifier. */
14584 id
= cp_parser_decltype (parser
);
14586 /* Otherwise, try a class-name. */
14587 id
= cp_parser_class_name (parser
,
14588 /*typename_keyword_p=*/true,
14589 /*template_keyword_p=*/false,
14591 /*check_dependency_p=*/true,
14592 /*class_head_p=*/false,
14593 /*is_declaration=*/true);
14594 /* If we found one, we're done. */
14595 if (cp_parser_parse_definitely (parser
))
14597 /* Otherwise, look for an ordinary identifier. */
14598 return cp_parser_identifier (parser
);
14601 /* Overloading [gram.over] */
14603 /* Parse an operator-function-id.
14605 operator-function-id:
14608 Returns an IDENTIFIER_NODE for the operator which is a
14609 human-readable spelling of the identifier, e.g., `operator +'. */
14612 cp_parser_operator_function_id (cp_parser
* parser
)
14614 /* Look for the `operator' keyword. */
14615 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14616 return error_mark_node
;
14617 /* And then the name of the operator itself. */
14618 return cp_parser_operator (parser
);
14621 /* Return an identifier node for a user-defined literal operator.
14622 The suffix identifier is chained to the operator name identifier. */
14625 cp_literal_operator_id (const char* name
)
14628 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14629 + strlen (name
) + 10);
14630 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14631 identifier
= get_identifier (buffer
);
14636 /* Parse an operator.
14639 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14640 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14641 || ++ -- , ->* -> () []
14648 Returns an IDENTIFIER_NODE for the operator which is a
14649 human-readable spelling of the identifier, e.g., `operator +'. */
14652 cp_parser_operator (cp_parser
* parser
)
14654 tree id
= NULL_TREE
;
14658 /* Peek at the next token. */
14659 token
= cp_lexer_peek_token (parser
->lexer
);
14661 location_t start_loc
= token
->location
;
14663 /* Figure out which operator we have. */
14664 enum tree_code op
= ERROR_MARK
;
14665 bool assop
= false;
14666 bool consumed
= false;
14667 switch (token
->type
)
14671 /* The keyword should be either `new' or `delete'. */
14672 if (token
->keyword
== RID_NEW
)
14674 else if (token
->keyword
== RID_DELETE
)
14679 /* Consume the `new' or `delete' token. */
14680 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14682 /* Peek at the next token. */
14683 token
= cp_lexer_peek_token (parser
->lexer
);
14684 /* If it's a `[' token then this is the array variant of the
14686 if (token
->type
== CPP_OPEN_SQUARE
)
14688 /* Consume the `[' token. */
14689 cp_lexer_consume_token (parser
->lexer
);
14690 /* Look for the `]' token. */
14691 if (cp_token
*close_token
14692 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14693 end_loc
= close_token
->location
;
14694 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14696 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14714 op
= TRUNC_DIV_EXPR
;
14718 op
= TRUNC_MOD_EXPR
;
14738 op
= TRUTH_NOT_EXPR
;
14771 op
= TRUNC_DIV_EXPR
;
14776 op
= TRUNC_MOD_EXPR
;
14802 case CPP_LSHIFT_EQ
:
14807 case CPP_RSHIFT_EQ
:
14824 case CPP_GREATER_EQ
:
14829 op
= TRUTH_ANDIF_EXPR
;
14833 op
= TRUTH_ORIF_EXPR
;
14836 case CPP_PLUS_PLUS
:
14837 op
= POSTINCREMENT_EXPR
;
14840 case CPP_MINUS_MINUS
:
14841 op
= PREDECREMENT_EXPR
;
14845 op
= COMPOUND_EXPR
;
14848 case CPP_DEREF_STAR
:
14853 op
= COMPONENT_REF
;
14856 case CPP_OPEN_PAREN
:
14858 /* Consume the `('. */
14859 matching_parens parens
;
14860 parens
.consume_open (parser
);
14861 /* Look for the matching `)'. */
14862 parens
.require_close (parser
);
14868 case CPP_OPEN_SQUARE
:
14869 /* Consume the `['. */
14870 cp_lexer_consume_token (parser
->lexer
);
14871 /* Look for the matching `]'. */
14872 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14877 case CPP_UTF8STRING
:
14878 case CPP_UTF8STRING_USERDEF
:
14885 case CPP_STRING_USERDEF
:
14886 case CPP_WSTRING_USERDEF
:
14887 case CPP_STRING16_USERDEF
:
14888 case CPP_STRING32_USERDEF
:
14890 tree str
, string_tree
;
14893 if (cxx_dialect
== cxx98
)
14894 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14896 /* Consume the string. */
14897 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14898 /*wide_ok=*/true, /*lookup_udlit=*/false);
14899 if (str
== error_mark_node
)
14900 return error_mark_node
;
14901 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14903 string_tree
= USERDEF_LITERAL_VALUE (str
);
14904 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14909 /* Look for the suffix identifier. */
14910 token
= cp_lexer_peek_token (parser
->lexer
);
14911 if (token
->type
== CPP_NAME
)
14912 id
= cp_parser_identifier (parser
);
14913 else if (token
->type
== CPP_KEYWORD
)
14915 error ("unexpected keyword;"
14916 " remove space between quotes and suffix identifier");
14917 return error_mark_node
;
14921 error ("expected suffix identifier");
14922 return error_mark_node
;
14925 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14926 (TREE_TYPE (TREE_TYPE (string_tree
))));
14927 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14930 error ("expected empty string after %<operator%> keyword");
14931 return error_mark_node
;
14933 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14936 error ("invalid encoding prefix in literal operator");
14937 return error_mark_node
;
14939 if (id
!= error_mark_node
)
14941 const char *name
= IDENTIFIER_POINTER (id
);
14942 id
= cp_literal_operator_id (name
);
14948 /* Anything else is an error. */
14952 /* If we have selected an identifier, we need to consume the
14954 if (op
!= ERROR_MARK
)
14956 id
= ovl_op_identifier (assop
, op
);
14958 cp_lexer_consume_token (parser
->lexer
);
14960 /* Otherwise, no valid operator name was present. */
14963 cp_parser_error (parser
, "expected operator");
14964 id
= error_mark_node
;
14967 return cp_expr (id
, start_loc
);
14970 /* Parse a template-declaration.
14972 template-declaration:
14973 export [opt] template < template-parameter-list > declaration
14975 If MEMBER_P is TRUE, this template-declaration occurs within a
14978 The grammar rule given by the standard isn't correct. What
14979 is really meant is:
14981 template-declaration:
14982 export [opt] template-parameter-list-seq
14983 decl-specifier-seq [opt] init-declarator [opt] ;
14984 export [opt] template-parameter-list-seq
14985 function-definition
14987 template-parameter-list-seq:
14988 template-parameter-list-seq [opt]
14989 template < template-parameter-list >
14991 Concept Extensions:
14993 template-parameter-list-seq:
14994 template < template-parameter-list > requires-clause [opt]
14997 requires logical-or-expression */
15000 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15002 /* Check for `export'. */
15003 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15005 /* Consume the `export' token. */
15006 cp_lexer_consume_token (parser
->lexer
);
15007 /* Warn that we do not support `export'. */
15008 warning (0, "keyword %<export%> not implemented, and will be ignored");
15011 cp_parser_template_declaration_after_export (parser
, member_p
);
15014 /* Parse a template-parameter-list.
15016 template-parameter-list:
15018 template-parameter-list , template-parameter
15020 Returns a TREE_LIST. Each node represents a template parameter.
15021 The nodes are connected via their TREE_CHAINs. */
15024 cp_parser_template_parameter_list (cp_parser
* parser
)
15026 tree parameter_list
= NULL_TREE
;
15028 begin_template_parm_list ();
15030 /* The loop below parses the template parms. We first need to know
15031 the total number of template parms to be able to compute proper
15032 canonical types of each dependent type. So after the loop, when
15033 we know the total number of template parms,
15034 end_template_parm_list computes the proper canonical types and
15035 fixes up the dependent types accordingly. */
15040 bool is_parameter_pack
;
15041 location_t parm_loc
;
15043 /* Parse the template-parameter. */
15044 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15045 parameter
= cp_parser_template_parameter (parser
,
15047 &is_parameter_pack
);
15048 /* Add it to the list. */
15049 if (parameter
!= error_mark_node
)
15050 parameter_list
= process_template_parm (parameter_list
,
15054 is_parameter_pack
);
15057 tree err_parm
= build_tree_list (parameter
, parameter
);
15058 parameter_list
= chainon (parameter_list
, err_parm
);
15061 /* If the next token is not a `,', we're done. */
15062 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15064 /* Otherwise, consume the `,' token. */
15065 cp_lexer_consume_token (parser
->lexer
);
15068 return end_template_parm_list (parameter_list
);
15071 /* Parse a introduction-list.
15074 introduced-parameter
15075 introduction-list , introduced-parameter
15077 introduced-parameter:
15078 ...[opt] identifier
15080 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15081 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15082 WILDCARD_DECL will also have DECL_NAME set and token location in
15083 DECL_SOURCE_LOCATION. */
15086 cp_parser_introduction_list (cp_parser
*parser
)
15088 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15092 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15094 cp_lexer_consume_token (parser
->lexer
);
15096 /* Build placeholder. */
15097 tree parm
= build_nt (WILDCARD_DECL
);
15098 DECL_SOURCE_LOCATION (parm
)
15099 = cp_lexer_peek_token (parser
->lexer
)->location
;
15100 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15101 WILDCARD_PACK_P (parm
) = is_pack
;
15102 vec_safe_push (introduction_vec
, parm
);
15104 /* If the next token is not a `,', we're done. */
15105 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15107 /* Otherwise, consume the `,' token. */
15108 cp_lexer_consume_token (parser
->lexer
);
15111 /* Convert the vec into a TREE_VEC. */
15112 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15115 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15116 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15118 release_tree_vector (introduction_vec
);
15119 return introduction_list
;
15122 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15123 is an abstract declarator. */
15125 static inline cp_declarator
*
15126 get_id_declarator (cp_declarator
*declarator
)
15128 cp_declarator
*d
= declarator
;
15129 while (d
&& d
->kind
!= cdk_id
)
15134 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15135 is an abstract declarator. */
15138 get_unqualified_id (cp_declarator
*declarator
)
15140 declarator
= get_id_declarator (declarator
);
15142 return declarator
->u
.id
.unqualified_name
;
15147 /* Returns true if DECL represents a constrained-parameter. */
15150 is_constrained_parameter (tree decl
)
15153 && TREE_CODE (decl
) == TYPE_DECL
15154 && CONSTRAINED_PARM_CONCEPT (decl
)
15155 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15158 /* Returns true if PARM declares a constrained-parameter. */
15161 is_constrained_parameter (cp_parameter_declarator
*parm
)
15163 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15166 /* Check that the type parameter is only a declarator-id, and that its
15167 type is not cv-qualified. */
15170 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15171 cp_parameter_declarator
*parm
)
15173 if (!parm
->declarator
)
15176 if (parm
->declarator
->kind
!= cdk_id
)
15178 cp_parser_error (parser
, "invalid constrained type parameter");
15182 /* Don't allow cv-qualified type parameters. */
15183 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15184 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15186 cp_parser_error (parser
, "cv-qualified type parameter");
15193 /* Finish parsing/processing a template type parameter and checking
15194 various restrictions. */
15197 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15199 cp_parameter_declarator
* parmdecl
)
15201 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15202 return finish_template_type_parm (class_type_node
, id
);
15204 return error_mark_node
;
15208 finish_constrained_template_template_parm (tree proto
, tree id
)
15210 /* FIXME: This should probably be copied, and we may need to adjust
15211 the template parameter depths. */
15212 tree saved_parms
= current_template_parms
;
15213 begin_template_parm_list ();
15214 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15215 end_template_parm_list ();
15217 tree parm
= finish_template_template_parm (class_type_node
, id
);
15218 current_template_parms
= saved_parms
;
15223 /* Finish parsing/processing a template template parameter by borrowing
15224 the template parameter list from the prototype parameter. */
15227 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15230 cp_parameter_declarator
*parmdecl
)
15232 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15233 return error_mark_node
;
15234 return finish_constrained_template_template_parm (proto
, id
);
15237 /* Create a new non-type template parameter from the given PARM
15241 constrained_non_type_template_parm (bool *is_non_type
,
15242 cp_parameter_declarator
*parm
)
15244 *is_non_type
= true;
15245 cp_declarator
*decl
= parm
->declarator
;
15246 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15247 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15248 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15251 /* Build a constrained template parameter based on the PARMDECL
15252 declarator. The type of PARMDECL is the constrained type, which
15253 refers to the prototype template parameter that ultimately
15254 specifies the type of the declared parameter. */
15257 finish_constrained_parameter (cp_parser
*parser
,
15258 cp_parameter_declarator
*parmdecl
,
15260 bool *is_parameter_pack
)
15262 tree decl
= parmdecl
->decl_specifiers
.type
;
15263 tree id
= get_unqualified_id (parmdecl
->declarator
);
15264 tree def
= parmdecl
->default_argument
;
15265 tree proto
= DECL_INITIAL (decl
);
15267 /* A template parameter constrained by a variadic concept shall also
15268 be declared as a template parameter pack. */
15269 bool is_variadic
= template_parameter_pack_p (proto
);
15270 if (is_variadic
&& !*is_parameter_pack
)
15271 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15273 /* Build the parameter. Return an error if the declarator was invalid. */
15275 if (TREE_CODE (proto
) == TYPE_DECL
)
15276 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15277 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15278 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15281 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15282 if (parm
== error_mark_node
)
15283 return error_mark_node
;
15285 /* Finish the parameter decl and create a node attaching the
15286 default argument and constraint. */
15287 parm
= build_tree_list (def
, parm
);
15288 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15293 /* Returns true if the parsed type actually represents the declaration
15294 of a type template-parameter. */
15297 declares_constrained_type_template_parameter (tree type
)
15299 return (is_constrained_parameter (type
)
15300 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15304 /* Returns true if the parsed type actually represents the declaration of
15305 a template template-parameter. */
15308 declares_constrained_template_template_parameter (tree type
)
15310 return (is_constrained_parameter (type
)
15311 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15314 /* Parse a default argument for a type template-parameter.
15315 Note that diagnostics are handled in cp_parser_template_parameter. */
15318 cp_parser_default_type_template_argument (cp_parser
*parser
)
15320 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15322 /* Consume the `=' token. */
15323 cp_lexer_consume_token (parser
->lexer
);
15325 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15327 /* Parse the default-argument. */
15328 push_deferring_access_checks (dk_no_deferred
);
15329 tree default_argument
= cp_parser_type_id (parser
);
15330 pop_deferring_access_checks ();
15332 if (flag_concepts
&& type_uses_auto (default_argument
))
15334 error_at (token
->location
,
15335 "invalid use of %<auto%> in default template argument");
15336 return error_mark_node
;
15339 return default_argument
;
15342 /* Parse a default argument for a template template-parameter. */
15345 cp_parser_default_template_template_argument (cp_parser
*parser
)
15347 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15351 /* Consume the `='. */
15352 cp_lexer_consume_token (parser
->lexer
);
15353 /* Parse the id-expression. */
15354 push_deferring_access_checks (dk_no_deferred
);
15355 /* save token before parsing the id-expression, for error
15357 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15358 tree default_argument
15359 = cp_parser_id_expression (parser
,
15360 /*template_keyword_p=*/false,
15361 /*check_dependency_p=*/true,
15362 /*template_p=*/&is_template
,
15363 /*declarator_p=*/false,
15364 /*optional_p=*/false);
15365 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15366 /* If the id-expression was a template-id that refers to
15367 a template-class, we already have the declaration here,
15368 so no further lookup is needed. */
15371 /* Look up the name. */
15373 = cp_parser_lookup_name (parser
, default_argument
,
15375 /*is_template=*/is_template
,
15376 /*is_namespace=*/false,
15377 /*check_dependency=*/true,
15378 /*ambiguous_decls=*/NULL
,
15380 /* See if the default argument is valid. */
15381 default_argument
= check_template_template_default_arg (default_argument
);
15382 pop_deferring_access_checks ();
15383 return default_argument
;
15386 /* Parse a template-parameter.
15388 template-parameter:
15390 parameter-declaration
15392 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15393 the parameter. The TREE_PURPOSE is the default value, if any.
15394 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15395 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15396 set to true iff this parameter is a parameter pack. */
15399 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15400 bool *is_parameter_pack
)
15403 cp_parameter_declarator
*parameter_declarator
;
15406 /* Assume it is a type parameter or a template parameter. */
15407 *is_non_type
= false;
15408 /* Assume it not a parameter pack. */
15409 *is_parameter_pack
= false;
15410 /* Peek at the next token. */
15411 token
= cp_lexer_peek_token (parser
->lexer
);
15412 /* If it is `template', we have a type-parameter. */
15413 if (token
->keyword
== RID_TEMPLATE
)
15414 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15415 /* If it is `class' or `typename' we do not know yet whether it is a
15416 type parameter or a non-type parameter. Consider:
15418 template <typename T, typename T::X X> ...
15422 template <class C, class D*> ...
15424 Here, the first parameter is a type parameter, and the second is
15425 a non-type parameter. We can tell by looking at the token after
15426 the identifier -- if it is a `,', `=', or `>' then we have a type
15428 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15430 /* Peek at the token after `class' or `typename'. */
15431 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15432 /* If it's an ellipsis, we have a template type parameter
15434 if (token
->type
== CPP_ELLIPSIS
)
15435 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15436 /* If it's an identifier, skip it. */
15437 if (token
->type
== CPP_NAME
)
15438 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15439 /* Now, see if the token looks like the end of a template
15441 if (token
->type
== CPP_COMMA
15442 || token
->type
== CPP_EQ
15443 || token
->type
== CPP_GREATER
)
15444 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15447 /* Otherwise, it is a non-type parameter or a constrained parameter.
15451 When parsing a default template-argument for a non-type
15452 template-parameter, the first non-nested `>' is taken as the end
15453 of the template parameter-list rather than a greater-than
15455 parameter_declarator
15456 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15457 /*parenthesized_p=*/NULL
);
15459 if (!parameter_declarator
)
15460 return error_mark_node
;
15462 /* If the parameter declaration is marked as a parameter pack, set
15463 *IS_PARAMETER_PACK to notify the caller. */
15464 if (parameter_declarator
->template_parameter_pack_p
)
15465 *is_parameter_pack
= true;
15467 if (parameter_declarator
->default_argument
)
15469 /* Can happen in some cases of erroneous input (c++/34892). */
15470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15471 /* Consume the `...' for better error recovery. */
15472 cp_lexer_consume_token (parser
->lexer
);
15475 // The parameter may have been constrained.
15476 if (is_constrained_parameter (parameter_declarator
))
15477 return finish_constrained_parameter (parser
,
15478 parameter_declarator
,
15480 is_parameter_pack
);
15482 // Now we're sure that the parameter is a non-type parameter.
15483 *is_non_type
= true;
15485 parm
= grokdeclarator (parameter_declarator
->declarator
,
15486 ¶meter_declarator
->decl_specifiers
,
15487 TPARM
, /*initialized=*/0,
15488 /*attrlist=*/NULL
);
15489 if (parm
== error_mark_node
)
15490 return error_mark_node
;
15492 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15495 /* Parse a type-parameter.
15498 class identifier [opt]
15499 class identifier [opt] = type-id
15500 typename identifier [opt]
15501 typename identifier [opt] = type-id
15502 template < template-parameter-list > class identifier [opt]
15503 template < template-parameter-list > class identifier [opt]
15506 GNU Extension (variadic templates):
15509 class ... identifier [opt]
15510 typename ... identifier [opt]
15512 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15513 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15514 the declaration of the parameter.
15516 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15519 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15524 /* Look for a keyword to tell us what kind of parameter this is. */
15525 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15527 return error_mark_node
;
15529 switch (token
->keyword
)
15535 tree default_argument
;
15537 /* If the next token is an ellipsis, we have a template
15539 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15541 /* Consume the `...' token. */
15542 cp_lexer_consume_token (parser
->lexer
);
15543 maybe_warn_variadic_templates ();
15545 *is_parameter_pack
= true;
15548 /* If the next token is an identifier, then it names the
15550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15551 identifier
= cp_parser_identifier (parser
);
15553 identifier
= NULL_TREE
;
15555 /* Create the parameter. */
15556 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15558 /* If the next token is an `=', we have a default argument. */
15559 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15562 = cp_parser_default_type_template_argument (parser
);
15564 /* Template parameter packs cannot have default
15566 if (*is_parameter_pack
)
15569 error_at (token
->location
,
15570 "template parameter pack %qD cannot have a "
15571 "default argument", identifier
);
15573 error_at (token
->location
,
15574 "template parameter packs cannot have "
15575 "default arguments");
15576 default_argument
= NULL_TREE
;
15578 else if (check_for_bare_parameter_packs (default_argument
))
15579 default_argument
= error_mark_node
;
15582 default_argument
= NULL_TREE
;
15584 /* Create the combined representation of the parameter and the
15585 default argument. */
15586 parameter
= build_tree_list (default_argument
, parameter
);
15593 tree default_argument
;
15595 /* Look for the `<'. */
15596 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15597 /* Parse the template-parameter-list. */
15598 cp_parser_template_parameter_list (parser
);
15599 /* Look for the `>'. */
15600 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15602 // If template requirements are present, parse them.
15605 tree reqs
= get_shorthand_constraints (current_template_parms
);
15606 if (tree r
= cp_parser_requires_clause_opt (parser
))
15607 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15608 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15611 /* Look for the `class' or 'typename' keywords. */
15612 cp_parser_type_parameter_key (parser
);
15613 /* If the next token is an ellipsis, we have a template
15615 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15617 /* Consume the `...' token. */
15618 cp_lexer_consume_token (parser
->lexer
);
15619 maybe_warn_variadic_templates ();
15621 *is_parameter_pack
= true;
15623 /* If the next token is an `=', then there is a
15624 default-argument. If the next token is a `>', we are at
15625 the end of the parameter-list. If the next token is a `,',
15626 then we are at the end of this parameter. */
15627 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15628 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15629 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15631 identifier
= cp_parser_identifier (parser
);
15632 /* Treat invalid names as if the parameter were nameless. */
15633 if (identifier
== error_mark_node
)
15634 identifier
= NULL_TREE
;
15637 identifier
= NULL_TREE
;
15639 /* Create the template parameter. */
15640 parameter
= finish_template_template_parm (class_type_node
,
15643 /* If the next token is an `=', then there is a
15644 default-argument. */
15645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15648 = cp_parser_default_template_template_argument (parser
);
15650 /* Template parameter packs cannot have default
15652 if (*is_parameter_pack
)
15655 error_at (token
->location
,
15656 "template parameter pack %qD cannot "
15657 "have a default argument",
15660 error_at (token
->location
, "template parameter packs cannot "
15661 "have default arguments");
15662 default_argument
= NULL_TREE
;
15666 default_argument
= NULL_TREE
;
15668 /* Create the combined representation of the parameter and the
15669 default argument. */
15670 parameter
= build_tree_list (default_argument
, parameter
);
15675 gcc_unreachable ();
15682 /* Parse a template-id.
15685 template-name < template-argument-list [opt] >
15687 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15688 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15689 returned. Otherwise, if the template-name names a function, or set
15690 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15691 names a class, returns a TYPE_DECL for the specialization.
15693 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15694 uninstantiated templates. */
15697 cp_parser_template_id (cp_parser
*parser
,
15698 bool template_keyword_p
,
15699 bool check_dependency_p
,
15700 enum tag_types tag_type
,
15701 bool is_declaration
)
15706 cp_token_position start_of_id
= 0;
15707 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15708 bool is_identifier
;
15710 /* If the next token corresponds to a template-id, there is no need
15712 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15713 if (token
->type
== CPP_TEMPLATE_ID
)
15715 cp_lexer_consume_token (parser
->lexer
);
15716 return saved_checks_value (token
->u
.tree_check_value
);
15719 /* Avoid performing name lookup if there is no possibility of
15720 finding a template-id. */
15721 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15722 || (token
->type
== CPP_NAME
15723 && !cp_parser_nth_token_starts_template_argument_list_p
15726 cp_parser_error (parser
, "expected template-id");
15727 return error_mark_node
;
15730 /* Remember where the template-id starts. */
15731 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15732 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15734 push_deferring_access_checks (dk_deferred
);
15736 /* Parse the template-name. */
15737 is_identifier
= false;
15738 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15739 check_dependency_p
,
15743 if (templ
== error_mark_node
|| is_identifier
)
15745 pop_deferring_access_checks ();
15749 /* Since we're going to preserve any side-effects from this parse, set up a
15750 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15751 in the template arguments. */
15752 tentative_firewall
firewall (parser
);
15754 /* If we find the sequence `[:' after a template-name, it's probably
15755 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15756 parse correctly the argument list. */
15757 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15758 == CPP_OPEN_SQUARE
)
15759 && next_token
->flags
& DIGRAPH
15760 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15762 && !(next_token_2
->flags
& PREV_WHITE
))
15764 cp_parser_parse_tentatively (parser
);
15765 /* Change `:' into `::'. */
15766 next_token_2
->type
= CPP_SCOPE
;
15767 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15769 cp_lexer_consume_token (parser
->lexer
);
15771 /* Parse the arguments. */
15772 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15773 if (!cp_parser_parse_definitely (parser
))
15775 /* If we couldn't parse an argument list, then we revert our changes
15776 and return simply an error. Maybe this is not a template-id
15778 next_token_2
->type
= CPP_COLON
;
15779 cp_parser_error (parser
, "expected %<<%>");
15780 pop_deferring_access_checks ();
15781 return error_mark_node
;
15783 /* Otherwise, emit an error about the invalid digraph, but continue
15784 parsing because we got our argument list. */
15785 if (permerror (next_token
->location
,
15786 "%<<::%> cannot begin a template-argument list"))
15788 static bool hint
= false;
15789 inform (next_token
->location
,
15790 "%<<:%> is an alternate spelling for %<[%>."
15791 " Insert whitespace between %<<%> and %<::%>");
15792 if (!hint
&& !flag_permissive
)
15794 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15795 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15796 "accept your code)");
15803 /* Look for the `<' that starts the template-argument-list. */
15804 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15806 pop_deferring_access_checks ();
15807 return error_mark_node
;
15809 /* Parse the arguments. */
15810 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15813 /* Set the location to be of the form:
15814 template-name < template-argument-list [opt] >
15815 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15816 with caret == start at the start of the template-name,
15817 ranging until the closing '>'. */
15818 location_t finish_loc
15819 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15820 location_t combined_loc
15821 = make_location (token
->location
, token
->location
, finish_loc
);
15823 /* Build a representation of the specialization. */
15824 if (identifier_p (templ
))
15825 template_id
= build_min_nt_loc (combined_loc
,
15828 else if (DECL_TYPE_TEMPLATE_P (templ
)
15829 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15831 bool entering_scope
;
15832 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15833 template (rather than some instantiation thereof) only if
15834 is not nested within some other construct. For example, in
15835 "template <typename T> void f(T) { A<T>::", A<T> is just an
15836 instantiation of A. */
15837 entering_scope
= (template_parm_scope_p ()
15838 && cp_lexer_next_token_is (parser
->lexer
,
15841 = finish_template_type (templ
, arguments
, entering_scope
);
15843 /* A template-like identifier may be a partial concept id. */
15844 else if (flag_concepts
15845 && (template_id
= (cp_parser_maybe_partial_concept_id
15846 (parser
, templ
, arguments
))))
15847 return template_id
;
15848 else if (variable_template_p (templ
))
15850 template_id
= lookup_template_variable (templ
, arguments
);
15851 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15852 SET_EXPR_LOCATION (template_id
, combined_loc
);
15856 /* If it's not a class-template or a template-template, it should be
15857 a function-template. */
15858 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15859 || TREE_CODE (templ
) == OVERLOAD
15860 || BASELINK_P (templ
)));
15862 template_id
= lookup_template_function (templ
, arguments
);
15863 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15864 SET_EXPR_LOCATION (template_id
, combined_loc
);
15867 /* If parsing tentatively, replace the sequence of tokens that makes
15868 up the template-id with a CPP_TEMPLATE_ID token. That way,
15869 should we re-parse the token stream, we will not have to repeat
15870 the effort required to do the parse, nor will we issue duplicate
15871 error messages about problems during instantiation of the
15874 /* Don't do this if we had a parse error in a declarator; re-parsing
15875 might succeed if a name changes meaning (60361). */
15876 && !(cp_parser_error_occurred (parser
)
15877 && cp_parser_parsing_tentatively (parser
)
15878 && parser
->in_declarator_p
))
15880 /* Reset the contents of the START_OF_ID token. */
15881 token
->type
= CPP_TEMPLATE_ID
;
15882 token
->location
= combined_loc
;
15884 /* We must mark the lookup as kept, so we don't throw it away on
15885 the first parse. */
15886 if (is_overloaded_fn (template_id
))
15887 lookup_keep (get_fns (template_id
), true);
15889 /* Retrieve any deferred checks. Do not pop this access checks yet
15890 so the memory will not be reclaimed during token replacing below. */
15891 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15892 token
->u
.tree_check_value
->value
= template_id
;
15893 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15894 token
->keyword
= RID_MAX
;
15896 /* Purge all subsequent tokens. */
15897 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15899 /* ??? Can we actually assume that, if template_id ==
15900 error_mark_node, we will have issued a diagnostic to the
15901 user, as opposed to simply marking the tentative parse as
15903 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15904 error_at (token
->location
, "parse error in template argument list");
15907 pop_to_parent_deferring_access_checks ();
15908 return template_id
;
15911 /* Parse a template-name.
15916 The standard should actually say:
15920 operator-function-id
15922 A defect report has been filed about this issue.
15924 A conversion-function-id cannot be a template name because they cannot
15925 be part of a template-id. In fact, looking at this code:
15927 a.operator K<int>()
15929 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15930 It is impossible to call a templated conversion-function-id with an
15931 explicit argument list, since the only allowed template parameter is
15932 the type to which it is converting.
15934 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15935 `template' keyword, in a construction like:
15939 In that case `f' is taken to be a template-name, even though there
15940 is no way of knowing for sure.
15942 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15943 name refers to a set of overloaded functions, at least one of which
15944 is a template, or an IDENTIFIER_NODE with the name of the template,
15945 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15946 names are looked up inside uninstantiated templates. */
15949 cp_parser_template_name (cp_parser
* parser
,
15950 bool template_keyword_p
,
15951 bool check_dependency_p
,
15952 bool is_declaration
,
15953 enum tag_types tag_type
,
15954 bool *is_identifier
)
15958 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15960 /* If the next token is `operator', then we have either an
15961 operator-function-id or a conversion-function-id. */
15962 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15964 /* We don't know whether we're looking at an
15965 operator-function-id or a conversion-function-id. */
15966 cp_parser_parse_tentatively (parser
);
15967 /* Try an operator-function-id. */
15968 identifier
= cp_parser_operator_function_id (parser
);
15969 /* If that didn't work, try a conversion-function-id. */
15970 if (!cp_parser_parse_definitely (parser
))
15972 cp_parser_error (parser
, "expected template-name");
15973 return error_mark_node
;
15976 /* Look for the identifier. */
15978 identifier
= cp_parser_identifier (parser
);
15980 /* If we didn't find an identifier, we don't have a template-id. */
15981 if (identifier
== error_mark_node
)
15982 return error_mark_node
;
15984 /* If the name immediately followed the `template' keyword, then it
15985 is a template-name. However, if the next token is not `<', then
15986 we do not treat it as a template-name, since it is not being used
15987 as part of a template-id. This enables us to handle constructs
15990 template <typename T> struct S { S(); };
15991 template <typename T> S<T>::S();
15993 correctly. We would treat `S' as a template -- if it were `S<T>'
15994 -- but we do not if there is no `<'. */
15996 if (processing_template_decl
15997 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15999 /* In a declaration, in a dependent context, we pretend that the
16000 "template" keyword was present in order to improve error
16001 recovery. For example, given:
16003 template <typename T> void f(T::X<int>);
16005 we want to treat "X<int>" as a template-id. */
16007 && !template_keyword_p
16008 && parser
->scope
&& TYPE_P (parser
->scope
)
16009 && check_dependency_p
16010 && dependent_scope_p (parser
->scope
)
16011 /* Do not do this for dtors (or ctors), since they never
16012 need the template keyword before their name. */
16013 && !constructor_name_p (identifier
, parser
->scope
))
16015 cp_token_position start
= 0;
16017 /* Explain what went wrong. */
16018 error_at (token
->location
, "non-template %qD used as template",
16020 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16021 parser
->scope
, identifier
);
16022 /* If parsing tentatively, find the location of the "<" token. */
16023 if (cp_parser_simulate_error (parser
))
16024 start
= cp_lexer_token_position (parser
->lexer
, true);
16025 /* Parse the template arguments so that we can issue error
16026 messages about them. */
16027 cp_lexer_consume_token (parser
->lexer
);
16028 cp_parser_enclosed_template_argument_list (parser
);
16029 /* Skip tokens until we find a good place from which to
16030 continue parsing. */
16031 cp_parser_skip_to_closing_parenthesis (parser
,
16032 /*recovering=*/true,
16034 /*consume_paren=*/false);
16035 /* If parsing tentatively, permanently remove the
16036 template argument list. That will prevent duplicate
16037 error messages from being issued about the missing
16038 "template" keyword. */
16040 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16042 *is_identifier
= true;
16043 parser
->context
->object_type
= NULL_TREE
;
16047 /* If the "template" keyword is present, then there is generally
16048 no point in doing name-lookup, so we just return IDENTIFIER.
16049 But, if the qualifying scope is non-dependent then we can
16050 (and must) do name-lookup normally. */
16051 if (template_keyword_p
)
16053 tree scope
= (parser
->scope
? parser
->scope
16054 : parser
->context
->object_type
);
16055 if (scope
&& TYPE_P (scope
)
16056 && (!CLASS_TYPE_P (scope
)
16057 || (check_dependency_p
&& dependent_type_p (scope
))))
16059 /* We're optimizing away the call to cp_parser_lookup_name, but
16060 we still need to do this. */
16061 parser
->context
->object_type
= NULL_TREE
;
16067 /* Look up the name. */
16068 decl
= cp_parser_lookup_name (parser
, identifier
,
16070 /*is_template=*/true,
16071 /*is_namespace=*/false,
16072 check_dependency_p
,
16073 /*ambiguous_decls=*/NULL
,
16076 decl
= strip_using_decl (decl
);
16078 /* If DECL is a template, then the name was a template-name. */
16079 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16081 if (TREE_DEPRECATED (decl
)
16082 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16083 warn_deprecated_use (decl
, NULL_TREE
);
16087 /* The standard does not explicitly indicate whether a name that
16088 names a set of overloaded declarations, some of which are
16089 templates, is a template-name. However, such a name should
16090 be a template-name; otherwise, there is no way to form a
16091 template-id for the overloaded templates. */
16092 bool found
= false;
16094 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16095 !found
&& iter
; ++iter
)
16096 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16101 /* The name does not name a template. */
16102 cp_parser_error (parser
, "expected template-name");
16103 return error_mark_node
;
16107 /* If DECL is dependent, and refers to a function, then just return
16108 its name; we will look it up again during template instantiation. */
16109 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16111 tree scope
= ovl_scope (decl
);
16112 if (TYPE_P (scope
) && dependent_type_p (scope
))
16119 /* Parse a template-argument-list.
16121 template-argument-list:
16122 template-argument ... [opt]
16123 template-argument-list , template-argument ... [opt]
16125 Returns a TREE_VEC containing the arguments. */
16128 cp_parser_template_argument_list (cp_parser
* parser
)
16130 tree fixed_args
[10];
16131 unsigned n_args
= 0;
16132 unsigned alloced
= 10;
16133 tree
*arg_ary
= fixed_args
;
16135 bool saved_in_template_argument_list_p
;
16137 bool saved_non_ice_p
;
16139 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16140 parser
->in_template_argument_list_p
= true;
16141 /* Even if the template-id appears in an integral
16142 constant-expression, the contents of the argument list do
16144 saved_ice_p
= parser
->integral_constant_expression_p
;
16145 parser
->integral_constant_expression_p
= false;
16146 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16147 parser
->non_integral_constant_expression_p
= false;
16149 /* Parse the arguments. */
16155 /* Consume the comma. */
16156 cp_lexer_consume_token (parser
->lexer
);
16158 /* Parse the template-argument. */
16159 argument
= cp_parser_template_argument (parser
);
16161 /* If the next token is an ellipsis, we're expanding a template
16163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16165 if (argument
== error_mark_node
)
16167 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16168 error_at (token
->location
,
16169 "expected parameter pack before %<...%>");
16171 /* Consume the `...' token. */
16172 cp_lexer_consume_token (parser
->lexer
);
16174 /* Make the argument into a TYPE_PACK_EXPANSION or
16175 EXPR_PACK_EXPANSION. */
16176 argument
= make_pack_expansion (argument
);
16179 if (n_args
== alloced
)
16183 if (arg_ary
== fixed_args
)
16185 arg_ary
= XNEWVEC (tree
, alloced
);
16186 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16189 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16191 arg_ary
[n_args
++] = argument
;
16193 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16195 vec
= make_tree_vec (n_args
);
16198 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16200 if (arg_ary
!= fixed_args
)
16202 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16203 parser
->integral_constant_expression_p
= saved_ice_p
;
16204 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16206 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16210 /* Parse a template-argument.
16213 assignment-expression
16217 The representation is that of an assignment-expression, type-id, or
16218 id-expression -- except that the qualified id-expression is
16219 evaluated, so that the value returned is either a DECL or an
16222 Although the standard says "assignment-expression", it forbids
16223 throw-expressions or assignments in the template argument.
16224 Therefore, we use "conditional-expression" instead. */
16227 cp_parser_template_argument (cp_parser
* parser
)
16232 bool maybe_type_id
= false;
16233 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16234 location_t loc
= 0;
16237 /* There's really no way to know what we're looking at, so we just
16238 try each alternative in order.
16242 In a template-argument, an ambiguity between a type-id and an
16243 expression is resolved to a type-id, regardless of the form of
16244 the corresponding template-parameter.
16246 Therefore, we try a type-id first. */
16247 cp_parser_parse_tentatively (parser
);
16248 argument
= cp_parser_template_type_arg (parser
);
16249 /* If there was no error parsing the type-id but the next token is a
16250 '>>', our behavior depends on which dialect of C++ we're
16251 parsing. In C++98, we probably found a typo for '> >'. But there
16252 are type-id which are also valid expressions. For instance:
16254 struct X { int operator >> (int); };
16255 template <int V> struct Foo {};
16258 Here 'X()' is a valid type-id of a function type, but the user just
16259 wanted to write the expression "X() >> 5". Thus, we remember that we
16260 found a valid type-id, but we still try to parse the argument as an
16261 expression to see what happens.
16263 In C++0x, the '>>' will be considered two separate '>'
16265 if (!cp_parser_error_occurred (parser
)
16266 && cxx_dialect
== cxx98
16267 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16269 maybe_type_id
= true;
16270 cp_parser_abort_tentative_parse (parser
);
16274 /* If the next token isn't a `,' or a `>', then this argument wasn't
16275 really finished. This means that the argument is not a valid
16277 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16278 cp_parser_error (parser
, "expected template-argument");
16279 /* If that worked, we're done. */
16280 if (cp_parser_parse_definitely (parser
))
16283 /* We're still not sure what the argument will be. */
16284 cp_parser_parse_tentatively (parser
);
16285 /* Try a template. */
16286 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16287 argument
= cp_parser_id_expression (parser
,
16288 /*template_keyword_p=*/false,
16289 /*check_dependency_p=*/true,
16291 /*declarator_p=*/false,
16292 /*optional_p=*/false);
16293 /* If the next token isn't a `,' or a `>', then this argument wasn't
16294 really finished. */
16295 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16296 cp_parser_error (parser
, "expected template-argument");
16297 if (!cp_parser_error_occurred (parser
))
16299 /* Figure out what is being referred to. If the id-expression
16300 was for a class template specialization, then we will have a
16301 TYPE_DECL at this point. There is no need to do name lookup
16302 at this point in that case. */
16303 if (TREE_CODE (argument
) != TYPE_DECL
)
16304 argument
= cp_parser_lookup_name (parser
, argument
,
16306 /*is_template=*/template_p
,
16307 /*is_namespace=*/false,
16308 /*check_dependency=*/true,
16309 /*ambiguous_decls=*/NULL
,
16310 argument_start_token
->location
);
16311 /* Handle a constrained-type-specifier for a non-type template
16313 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16315 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16316 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16317 cp_parser_error (parser
, "expected template-name");
16319 if (cp_parser_parse_definitely (parser
))
16321 if (TREE_DEPRECATED (argument
))
16322 warn_deprecated_use (argument
, NULL_TREE
);
16325 /* It must be a non-type argument. In C++17 any constant-expression is
16327 if (cxx_dialect
> cxx14
)
16330 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16332 -- an integral constant-expression of integral or enumeration
16335 -- the name of a non-type template-parameter; or
16337 -- the name of an object or function with external linkage...
16339 -- the address of an object or function with external linkage...
16341 -- a pointer to member... */
16342 /* Look for a non-type template parameter. */
16343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16345 cp_parser_parse_tentatively (parser
);
16346 argument
= cp_parser_primary_expression (parser
,
16347 /*address_p=*/false,
16349 /*template_arg_p=*/true,
16351 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16352 || !cp_parser_next_token_ends_template_argument_p (parser
))
16353 cp_parser_simulate_error (parser
);
16354 if (cp_parser_parse_definitely (parser
))
16358 /* If the next token is "&", the argument must be the address of an
16359 object or function with external linkage. */
16360 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16363 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16364 cp_lexer_consume_token (parser
->lexer
);
16366 /* See if we might have an id-expression. */
16367 token
= cp_lexer_peek_token (parser
->lexer
);
16368 if (token
->type
== CPP_NAME
16369 || token
->keyword
== RID_OPERATOR
16370 || token
->type
== CPP_SCOPE
16371 || token
->type
== CPP_TEMPLATE_ID
16372 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16374 cp_parser_parse_tentatively (parser
);
16375 argument
= cp_parser_primary_expression (parser
,
16378 /*template_arg_p=*/true,
16380 if (cp_parser_error_occurred (parser
)
16381 || !cp_parser_next_token_ends_template_argument_p (parser
))
16382 cp_parser_abort_tentative_parse (parser
);
16387 if (INDIRECT_REF_P (argument
))
16389 /* Strip the dereference temporarily. */
16390 gcc_assert (REFERENCE_REF_P (argument
));
16391 argument
= TREE_OPERAND (argument
, 0);
16394 /* If we're in a template, we represent a qualified-id referring
16395 to a static data member as a SCOPE_REF even if the scope isn't
16396 dependent so that we can check access control later. */
16398 if (TREE_CODE (probe
) == SCOPE_REF
)
16399 probe
= TREE_OPERAND (probe
, 1);
16402 /* A variable without external linkage might still be a
16403 valid constant-expression, so no error is issued here
16404 if the external-linkage check fails. */
16405 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16406 cp_parser_simulate_error (parser
);
16408 else if (is_overloaded_fn (argument
))
16409 /* All overloaded functions are allowed; if the external
16410 linkage test does not pass, an error will be issued
16414 && (TREE_CODE (argument
) == OFFSET_REF
16415 || TREE_CODE (argument
) == SCOPE_REF
))
16416 /* A pointer-to-member. */
16418 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16421 cp_parser_simulate_error (parser
);
16423 if (cp_parser_parse_definitely (parser
))
16426 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16427 tf_warning_or_error
);
16429 argument
= convert_from_reference (argument
);
16434 /* If the argument started with "&", there are no other valid
16435 alternatives at this point. */
16438 cp_parser_error (parser
, "invalid non-type template argument");
16439 return error_mark_node
;
16443 /* If the argument wasn't successfully parsed as a type-id followed
16444 by '>>', the argument can only be a constant expression now.
16445 Otherwise, we try parsing the constant-expression tentatively,
16446 because the argument could really be a type-id. */
16448 cp_parser_parse_tentatively (parser
);
16450 if (cxx_dialect
<= cxx14
)
16451 argument
= cp_parser_constant_expression (parser
);
16454 /* With C++17 generalized non-type template arguments we need to handle
16455 lvalue constant expressions, too. */
16456 argument
= cp_parser_assignment_expression (parser
);
16457 require_potential_constant_expression (argument
);
16460 if (!maybe_type_id
)
16462 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16463 cp_parser_error (parser
, "expected template-argument");
16464 if (cp_parser_parse_definitely (parser
))
16466 /* We did our best to parse the argument as a non type-id, but that
16467 was the only alternative that matched (albeit with a '>' after
16468 it). We can assume it's just a typo from the user, and a
16469 diagnostic will then be issued. */
16470 return cp_parser_template_type_arg (parser
);
16473 /* Parse an explicit-instantiation.
16475 explicit-instantiation:
16476 template declaration
16478 Although the standard says `declaration', what it really means is:
16480 explicit-instantiation:
16481 template decl-specifier-seq [opt] declarator [opt] ;
16483 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16484 supposed to be allowed. A defect report has been filed about this
16489 explicit-instantiation:
16490 storage-class-specifier template
16491 decl-specifier-seq [opt] declarator [opt] ;
16492 function-specifier template
16493 decl-specifier-seq [opt] declarator [opt] ; */
16496 cp_parser_explicit_instantiation (cp_parser
* parser
)
16498 int declares_class_or_enum
;
16499 cp_decl_specifier_seq decl_specifiers
;
16500 tree extension_specifier
= NULL_TREE
;
16502 timevar_push (TV_TEMPLATE_INST
);
16504 /* Look for an (optional) storage-class-specifier or
16505 function-specifier. */
16506 if (cp_parser_allow_gnu_extensions_p (parser
))
16508 extension_specifier
16509 = cp_parser_storage_class_specifier_opt (parser
);
16510 if (!extension_specifier
)
16511 extension_specifier
16512 = cp_parser_function_specifier_opt (parser
,
16513 /*decl_specs=*/NULL
);
16516 /* Look for the `template' keyword. */
16517 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16518 /* Let the front end know that we are processing an explicit
16520 begin_explicit_instantiation ();
16521 /* [temp.explicit] says that we are supposed to ignore access
16522 control while processing explicit instantiation directives. */
16523 push_deferring_access_checks (dk_no_check
);
16524 /* Parse a decl-specifier-seq. */
16525 cp_parser_decl_specifier_seq (parser
,
16526 CP_PARSER_FLAGS_OPTIONAL
,
16528 &declares_class_or_enum
);
16529 /* If there was exactly one decl-specifier, and it declared a class,
16530 and there's no declarator, then we have an explicit type
16532 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16536 type
= check_tag_decl (&decl_specifiers
,
16537 /*explicit_type_instantiation_p=*/true);
16538 /* Turn access control back on for names used during
16539 template instantiation. */
16540 pop_deferring_access_checks ();
16542 do_type_instantiation (type
, extension_specifier
,
16543 /*complain=*/tf_error
);
16547 cp_declarator
*declarator
;
16550 /* Parse the declarator. */
16552 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16553 /*ctor_dtor_or_conv_p=*/NULL
,
16554 /*parenthesized_p=*/NULL
,
16555 /*member_p=*/false,
16556 /*friend_p=*/false);
16557 if (declares_class_or_enum
& 2)
16558 cp_parser_check_for_definition_in_return_type (declarator
,
16559 decl_specifiers
.type
,
16560 decl_specifiers
.locations
[ds_type_spec
]);
16561 if (declarator
!= cp_error_declarator
)
16563 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16564 permerror (decl_specifiers
.locations
[ds_inline
],
16565 "explicit instantiation shall not use"
16566 " %<inline%> specifier");
16567 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16568 permerror (decl_specifiers
.locations
[ds_constexpr
],
16569 "explicit instantiation shall not use"
16570 " %<constexpr%> specifier");
16572 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16573 NORMAL
, 0, &decl_specifiers
.attributes
);
16574 /* Turn access control back on for names used during
16575 template instantiation. */
16576 pop_deferring_access_checks ();
16577 /* Do the explicit instantiation. */
16578 do_decl_instantiation (decl
, extension_specifier
);
16582 pop_deferring_access_checks ();
16583 /* Skip the body of the explicit instantiation. */
16584 cp_parser_skip_to_end_of_statement (parser
);
16587 /* We're done with the instantiation. */
16588 end_explicit_instantiation ();
16590 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16592 timevar_pop (TV_TEMPLATE_INST
);
16595 /* Parse an explicit-specialization.
16597 explicit-specialization:
16598 template < > declaration
16600 Although the standard says `declaration', what it really means is:
16602 explicit-specialization:
16603 template <> decl-specifier [opt] init-declarator [opt] ;
16604 template <> function-definition
16605 template <> explicit-specialization
16606 template <> template-declaration */
16609 cp_parser_explicit_specialization (cp_parser
* parser
)
16611 bool need_lang_pop
;
16612 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16614 /* Look for the `template' keyword. */
16615 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16616 /* Look for the `<'. */
16617 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16618 /* Look for the `>'. */
16619 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16620 /* We have processed another parameter list. */
16621 ++parser
->num_template_parameter_lists
;
16624 A template ... explicit specialization ... shall not have C
16626 if (current_lang_name
== lang_name_c
)
16628 error_at (token
->location
, "template specialization with C linkage");
16629 maybe_show_extern_c_location ();
16630 /* Give it C++ linkage to avoid confusing other parts of the
16632 push_lang_context (lang_name_cplusplus
);
16633 need_lang_pop
= true;
16636 need_lang_pop
= false;
16637 /* Let the front end know that we are beginning a specialization. */
16638 if (!begin_specialization ())
16640 end_specialization ();
16644 /* If the next keyword is `template', we need to figure out whether
16645 or not we're looking a template-declaration. */
16646 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16648 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16649 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16650 cp_parser_template_declaration_after_export (parser
,
16651 /*member_p=*/false);
16653 cp_parser_explicit_specialization (parser
);
16656 /* Parse the dependent declaration. */
16657 cp_parser_single_declaration (parser
,
16659 /*member_p=*/false,
16660 /*explicit_specialization_p=*/true,
16661 /*friend_p=*/NULL
);
16662 /* We're done with the specialization. */
16663 end_specialization ();
16664 /* For the erroneous case of a template with C linkage, we pushed an
16665 implicit C++ linkage scope; exit that scope now. */
16667 pop_lang_context ();
16668 /* We're done with this parameter list. */
16669 --parser
->num_template_parameter_lists
;
16672 /* Parse a type-specifier.
16675 simple-type-specifier
16678 elaborated-type-specifier
16686 Returns a representation of the type-specifier. For a
16687 class-specifier, enum-specifier, or elaborated-type-specifier, a
16688 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16690 The parser flags FLAGS is used to control type-specifier parsing.
16692 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16693 in a decl-specifier-seq.
16695 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16696 class-specifier, enum-specifier, or elaborated-type-specifier, then
16697 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16698 if a type is declared; 2 if it is defined. Otherwise, it is set to
16701 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16702 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16703 is set to FALSE. */
16706 cp_parser_type_specifier (cp_parser
* parser
,
16707 cp_parser_flags flags
,
16708 cp_decl_specifier_seq
*decl_specs
,
16709 bool is_declaration
,
16710 int* declares_class_or_enum
,
16711 bool* is_cv_qualifier
)
16713 tree type_spec
= NULL_TREE
;
16716 cp_decl_spec ds
= ds_last
;
16718 /* Assume this type-specifier does not declare a new type. */
16719 if (declares_class_or_enum
)
16720 *declares_class_or_enum
= 0;
16721 /* And that it does not specify a cv-qualifier. */
16722 if (is_cv_qualifier
)
16723 *is_cv_qualifier
= false;
16724 /* Peek at the next token. */
16725 token
= cp_lexer_peek_token (parser
->lexer
);
16727 /* If we're looking at a keyword, we can use that to guide the
16728 production we choose. */
16729 keyword
= token
->keyword
;
16733 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16734 goto elaborated_type_specifier
;
16736 /* Look for the enum-specifier. */
16737 type_spec
= cp_parser_enum_specifier (parser
);
16738 /* If that worked, we're done. */
16741 if (declares_class_or_enum
)
16742 *declares_class_or_enum
= 2;
16744 cp_parser_set_decl_spec_type (decl_specs
,
16747 /*type_definition_p=*/true);
16751 goto elaborated_type_specifier
;
16753 /* Any of these indicate either a class-specifier, or an
16754 elaborated-type-specifier. */
16758 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16759 goto elaborated_type_specifier
;
16761 /* Parse tentatively so that we can back up if we don't find a
16762 class-specifier. */
16763 cp_parser_parse_tentatively (parser
);
16764 /* Look for the class-specifier. */
16765 type_spec
= cp_parser_class_specifier (parser
);
16766 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16767 /* If that worked, we're done. */
16768 if (cp_parser_parse_definitely (parser
))
16770 if (declares_class_or_enum
)
16771 *declares_class_or_enum
= 2;
16773 cp_parser_set_decl_spec_type (decl_specs
,
16776 /*type_definition_p=*/true);
16780 /* Fall through. */
16781 elaborated_type_specifier
:
16782 /* We're declaring (not defining) a class or enum. */
16783 if (declares_class_or_enum
)
16784 *declares_class_or_enum
= 1;
16786 /* Fall through. */
16788 /* Look for an elaborated-type-specifier. */
16790 = (cp_parser_elaborated_type_specifier
16792 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16795 cp_parser_set_decl_spec_type (decl_specs
,
16798 /*type_definition_p=*/false);
16803 if (is_cv_qualifier
)
16804 *is_cv_qualifier
= true;
16809 if (is_cv_qualifier
)
16810 *is_cv_qualifier
= true;
16815 if (is_cv_qualifier
)
16816 *is_cv_qualifier
= true;
16820 /* The `__complex__' keyword is a GNU extension. */
16828 /* Handle simple keywords. */
16833 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16834 decl_specs
->any_specifiers_p
= true;
16836 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16839 /* If we do not already have a type-specifier, assume we are looking
16840 at a simple-type-specifier. */
16841 type_spec
= cp_parser_simple_type_specifier (parser
,
16845 /* If we didn't find a type-specifier, and a type-specifier was not
16846 optional in this context, issue an error message. */
16847 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16849 cp_parser_error (parser
, "expected type specifier");
16850 return error_mark_node
;
16856 /* Parse a simple-type-specifier.
16858 simple-type-specifier:
16859 :: [opt] nested-name-specifier [opt] type-name
16860 :: [opt] nested-name-specifier template template-id
16875 simple-type-specifier:
16877 decltype ( expression )
16880 __underlying_type ( type-id )
16884 nested-name-specifier(opt) template-name
16888 simple-type-specifier:
16890 __typeof__ unary-expression
16891 __typeof__ ( type-id )
16892 __typeof__ ( type-id ) { initializer-list , [opt] }
16894 Concepts Extension:
16896 simple-type-specifier:
16897 constrained-type-specifier
16899 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16900 appropriately updated. */
16903 cp_parser_simple_type_specifier (cp_parser
* parser
,
16904 cp_decl_specifier_seq
*decl_specs
,
16905 cp_parser_flags flags
)
16907 tree type
= NULL_TREE
;
16911 /* Peek at the next token. */
16912 token
= cp_lexer_peek_token (parser
->lexer
);
16914 /* If we're looking at a keyword, things are easy. */
16915 switch (token
->keyword
)
16919 decl_specs
->explicit_char_p
= true;
16920 type
= char_type_node
;
16923 type
= char16_type_node
;
16926 type
= char32_type_node
;
16929 type
= wchar_type_node
;
16932 type
= boolean_type_node
;
16935 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16936 type
= short_integer_type_node
;
16940 decl_specs
->explicit_int_p
= true;
16941 type
= integer_type_node
;
16947 idx
= token
->keyword
- RID_INT_N_0
;
16948 if (! int_n_enabled_p
[idx
])
16952 decl_specs
->explicit_intN_p
= true;
16953 decl_specs
->int_n_idx
= idx
;
16955 type
= int_n_trees
[idx
].signed_type
;
16959 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16960 type
= long_integer_type_node
;
16963 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16964 type
= integer_type_node
;
16967 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16968 type
= unsigned_type_node
;
16971 type
= float_type_node
;
16974 type
= double_type_node
;
16977 type
= void_type_node
;
16981 maybe_warn_cpp0x (CPP0X_AUTO
);
16982 if (parser
->auto_is_implicit_function_template_parm_p
)
16984 /* The 'auto' might be the placeholder return type for a function decl
16985 with trailing return type. */
16986 bool have_trailing_return_fn_decl
= false;
16988 cp_parser_parse_tentatively (parser
);
16989 cp_lexer_consume_token (parser
->lexer
);
16990 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16991 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16992 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16993 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16997 cp_lexer_consume_token (parser
->lexer
);
16998 cp_parser_skip_to_closing_parenthesis (parser
,
16999 /*recovering*/false,
17001 /*consume_paren*/true);
17005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17007 have_trailing_return_fn_decl
= true;
17011 cp_lexer_consume_token (parser
->lexer
);
17013 cp_parser_abort_tentative_parse (parser
);
17015 if (have_trailing_return_fn_decl
)
17017 type
= make_auto ();
17021 if (cxx_dialect
>= cxx14
)
17023 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17024 type
= TREE_TYPE (type
);
17027 type
= error_mark_node
;
17029 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17031 if (cxx_dialect
< cxx14
)
17032 error_at (token
->location
,
17033 "use of %<auto%> in lambda parameter declaration "
17034 "only available with "
17035 "-std=c++14 or -std=gnu++14");
17037 else if (cxx_dialect
< cxx14
)
17038 error_at (token
->location
,
17039 "use of %<auto%> in parameter declaration "
17040 "only available with "
17041 "-std=c++14 or -std=gnu++14");
17042 else if (!flag_concepts
)
17043 pedwarn (token
->location
, OPT_Wpedantic
,
17044 "ISO C++ forbids use of %<auto%> in parameter "
17048 type
= make_auto ();
17052 /* Since DR 743, decltype can either be a simple-type-specifier by
17053 itself or begin a nested-name-specifier. Parsing it will replace
17054 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17055 handling below decide what to do. */
17056 cp_parser_decltype (parser
);
17057 cp_lexer_set_token_position (parser
->lexer
, token
);
17061 /* Consume the `typeof' token. */
17062 cp_lexer_consume_token (parser
->lexer
);
17063 /* Parse the operand to `typeof'. */
17064 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17065 /* If it is not already a TYPE, take its type. */
17066 if (!TYPE_P (type
))
17067 type
= finish_typeof (type
);
17070 cp_parser_set_decl_spec_type (decl_specs
, type
,
17072 /*type_definition_p=*/false);
17076 case RID_UNDERLYING_TYPE
:
17077 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17079 cp_parser_set_decl_spec_type (decl_specs
, type
,
17081 /*type_definition_p=*/false);
17086 case RID_DIRECT_BASES
:
17087 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17089 cp_parser_set_decl_spec_type (decl_specs
, type
,
17091 /*type_definition_p=*/false);
17097 /* If token is an already-parsed decltype not followed by ::,
17098 it's a simple-type-specifier. */
17099 if (token
->type
== CPP_DECLTYPE
17100 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17102 type
= saved_checks_value (token
->u
.tree_check_value
);
17105 cp_parser_set_decl_spec_type (decl_specs
, type
,
17107 /*type_definition_p=*/false);
17108 /* Remember that we are handling a decltype in order to
17109 implement the resolution of DR 1510 when the argument
17110 isn't instantiation dependent. */
17111 decl_specs
->decltype_p
= true;
17113 cp_lexer_consume_token (parser
->lexer
);
17117 /* If the type-specifier was for a built-in type, we're done. */
17120 /* Record the type. */
17122 && (token
->keyword
!= RID_SIGNED
17123 && token
->keyword
!= RID_UNSIGNED
17124 && token
->keyword
!= RID_SHORT
17125 && token
->keyword
!= RID_LONG
))
17126 cp_parser_set_decl_spec_type (decl_specs
,
17129 /*type_definition_p=*/false);
17131 decl_specs
->any_specifiers_p
= true;
17133 /* Consume the token. */
17134 cp_lexer_consume_token (parser
->lexer
);
17136 if (type
== error_mark_node
)
17137 return error_mark_node
;
17139 /* There is no valid C++ program where a non-template type is
17140 followed by a "<". That usually indicates that the user thought
17141 that the type was a template. */
17142 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17145 return TYPE_NAME (type
);
17148 /* The type-specifier must be a user-defined type. */
17149 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17154 /* Don't gobble tokens or issue error messages if this is an
17155 optional type-specifier. */
17156 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17157 cp_parser_parse_tentatively (parser
);
17159 token
= cp_lexer_peek_token (parser
->lexer
);
17161 /* Look for the optional `::' operator. */
17163 = (cp_parser_global_scope_opt (parser
,
17164 /*current_scope_valid_p=*/false)
17166 /* Look for the nested-name specifier. */
17168 = (cp_parser_nested_name_specifier_opt (parser
,
17169 /*typename_keyword_p=*/false,
17170 /*check_dependency_p=*/true,
17172 /*is_declaration=*/false)
17174 /* If we have seen a nested-name-specifier, and the next token
17175 is `template', then we are using the template-id production. */
17177 && cp_parser_optional_template_keyword (parser
))
17179 /* Look for the template-id. */
17180 type
= cp_parser_template_id (parser
,
17181 /*template_keyword_p=*/true,
17182 /*check_dependency_p=*/true,
17184 /*is_declaration=*/false);
17185 /* If the template-id did not name a type, we are out of
17187 if (TREE_CODE (type
) != TYPE_DECL
)
17189 cp_parser_error (parser
, "expected template-id for type");
17193 /* Otherwise, look for a type-name. */
17195 type
= cp_parser_type_name (parser
);
17196 /* Keep track of all name-lookups performed in class scopes. */
17200 && TREE_CODE (type
) == TYPE_DECL
17201 && identifier_p (DECL_NAME (type
)))
17202 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17203 /* If it didn't work out, we don't have a TYPE. */
17204 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17205 && !cp_parser_parse_definitely (parser
))
17207 if (!type
&& cxx_dialect
>= cxx17
)
17209 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17210 cp_parser_parse_tentatively (parser
);
17212 cp_parser_global_scope_opt (parser
,
17213 /*current_scope_valid_p=*/false);
17214 cp_parser_nested_name_specifier_opt (parser
,
17215 /*typename_keyword_p=*/false,
17216 /*check_dependency_p=*/true,
17218 /*is_declaration=*/false);
17219 tree name
= cp_parser_identifier (parser
);
17220 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17221 && parser
->scope
!= error_mark_node
)
17223 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17225 /*is_template=*/false,
17226 /*is_namespace=*/false,
17227 /*check_dependency=*/true,
17228 /*ambiguous_decls=*/NULL
,
17230 if (tmpl
&& tmpl
!= error_mark_node
17231 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17232 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17233 type
= make_template_placeholder (tmpl
);
17236 type
= error_mark_node
;
17237 if (!cp_parser_simulate_error (parser
))
17238 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17239 NLE_TYPE
, token
->location
);
17243 type
= error_mark_node
;
17245 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17246 && !cp_parser_parse_definitely (parser
))
17249 if (type
&& decl_specs
)
17250 cp_parser_set_decl_spec_type (decl_specs
, type
,
17252 /*type_definition_p=*/false);
17255 /* If we didn't get a type-name, issue an error message. */
17256 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17258 cp_parser_error (parser
, "expected type-name");
17259 return error_mark_node
;
17262 if (type
&& type
!= error_mark_node
)
17264 /* See if TYPE is an Objective-C type, and if so, parse and
17265 accept any protocol references following it. Do this before
17266 the cp_parser_check_for_invalid_template_id() call, because
17267 Objective-C types can be followed by '<...>' which would
17268 enclose protocol names rather than template arguments, and so
17269 everything is fine. */
17270 if (c_dialect_objc () && !parser
->scope
17271 && (objc_is_id (type
) || objc_is_class_name (type
)))
17273 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17274 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17276 /* Clobber the "unqualified" type previously entered into
17277 DECL_SPECS with the new, improved protocol-qualified version. */
17279 decl_specs
->type
= qual_type
;
17284 /* There is no valid C++ program where a non-template type is
17285 followed by a "<". That usually indicates that the user
17286 thought that the type was a template. */
17287 cp_parser_check_for_invalid_template_id (parser
, type
,
17295 /* Parse a type-name.
17301 simple-template-id [in c++0x]
17318 Returns a TYPE_DECL for the type. */
17321 cp_parser_type_name (cp_parser
* parser
)
17323 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17328 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17332 /* We can't know yet whether it is a class-name or not. */
17333 cp_parser_parse_tentatively (parser
);
17334 /* Try a class-name. */
17335 type_decl
= cp_parser_class_name (parser
,
17336 typename_keyword_p
,
17337 /*template_keyword_p=*/false,
17339 /*check_dependency_p=*/true,
17340 /*class_head_p=*/false,
17341 /*is_declaration=*/false);
17342 /* If it's not a class-name, keep looking. */
17343 if (!cp_parser_parse_definitely (parser
))
17345 if (cxx_dialect
< cxx11
)
17346 /* It must be a typedef-name or an enum-name. */
17347 return cp_parser_nonclass_name (parser
);
17349 cp_parser_parse_tentatively (parser
);
17350 /* It is either a simple-template-id representing an
17351 instantiation of an alias template... */
17352 type_decl
= cp_parser_template_id (parser
,
17353 /*template_keyword_p=*/false,
17354 /*check_dependency_p=*/true,
17356 /*is_declaration=*/false);
17357 /* Note that this must be an instantiation of an alias template
17358 because [temp.names]/6 says:
17360 A template-id that names an alias template specialization
17363 Whereas [temp.names]/7 says:
17365 A simple-template-id that names a class template
17366 specialization is a class-name.
17368 With concepts, this could also be a partial-concept-id that
17369 declares a non-type template parameter. */
17370 if (type_decl
!= NULL_TREE
17371 && TREE_CODE (type_decl
) == TYPE_DECL
17372 && TYPE_DECL_ALIAS_P (type_decl
))
17373 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17374 else if (is_constrained_parameter (type_decl
))
17375 /* Don't do anything. */ ;
17377 cp_parser_simulate_error (parser
);
17379 if (!cp_parser_parse_definitely (parser
))
17380 /* ... Or a typedef-name or an enum-name. */
17381 return cp_parser_nonclass_name (parser
);
17387 /* Check if DECL and ARGS can form a constrained-type-specifier.
17388 If ARGS is non-null, we try to form a concept check of the
17389 form DECL<?, ARGS> where ? is a wildcard that matches any
17390 kind of template argument. If ARGS is NULL, then we try to
17391 form a concept check of the form DECL<?>. */
17394 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17395 tree decl
, tree args
)
17397 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17399 /* If we a constrained-type-specifier cannot be deduced. */
17400 if (parser
->prevent_constrained_type_specifiers
)
17403 /* A constrained type specifier can only be found in an
17404 overload set or as a reference to a template declaration.
17406 FIXME: This might be masking a bug. It's possible that
17407 that the deduction below is causing template specializations
17408 to be formed with the wildcard as an argument. */
17409 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17412 /* Try to build a call expression that evaluates the
17413 concept. This can fail if the overload set refers
17414 only to non-templates. */
17415 tree placeholder
= build_nt (WILDCARD_DECL
);
17416 tree check
= build_concept_check (decl
, placeholder
, args
);
17417 if (check
== error_mark_node
)
17420 /* Deduce the checked constraint and the prototype parameter.
17422 FIXME: In certain cases, failure to deduce should be a
17423 diagnosable error. */
17426 if (!deduce_constrained_parameter (check
, conc
, proto
))
17429 /* In template parameter scope, this results in a constrained
17430 parameter. Return a descriptor of that parm. */
17431 if (processing_template_parmlist
)
17432 return build_constrained_parameter (conc
, proto
, args
);
17434 /* In a parameter-declaration-clause, constrained-type
17435 specifiers result in invented template parameters. */
17436 if (parser
->auto_is_implicit_function_template_parm_p
)
17438 tree x
= build_constrained_parameter (conc
, proto
, args
);
17439 return synthesize_implicit_template_parm (parser
, x
);
17443 /* Otherwise, we're in a context where the constrained
17444 type name is deduced and the constraint applies
17445 after deduction. */
17446 return make_constrained_auto (conc
, args
);
17452 /* If DECL refers to a concept, return a TYPE_DECL representing
17453 the result of using the constrained type specifier in the
17454 current context. DECL refers to a concept if
17456 - it is an overload set containing a function concept taking a single
17459 - it is a variable concept taking a single type argument. */
17462 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17465 && (TREE_CODE (decl
) == OVERLOAD
17466 || BASELINK_P (decl
)
17467 || variable_concept_p (decl
)))
17468 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17473 /* Check if DECL and ARGS form a partial-concept-id. If so,
17474 assign ID to the resulting constrained placeholder.
17476 Returns true if the partial-concept-id designates a placeholder
17477 and false otherwise. Note that *id is set to NULL_TREE in
17481 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17483 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17486 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17498 Returns a TYPE_DECL for the type. */
17501 cp_parser_nonclass_name (cp_parser
* parser
)
17506 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17507 identifier
= cp_parser_identifier (parser
);
17508 if (identifier
== error_mark_node
)
17509 return error_mark_node
;
17511 /* Look up the type-name. */
17512 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17514 type_decl
= strip_using_decl (type_decl
);
17516 /* If we found an overload set, then it may refer to a concept-name. */
17517 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17520 if (TREE_CODE (type_decl
) != TYPE_DECL
17521 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17523 /* See if this is an Objective-C type. */
17524 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17525 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17527 type_decl
= TYPE_NAME (type
);
17530 /* Issue an error if we did not find a type-name. */
17531 if (TREE_CODE (type_decl
) != TYPE_DECL
17532 /* In Objective-C, we have the complication that class names are
17533 normally type names and start declarations (eg, the
17534 "NSObject" in "NSObject *object;"), but can be used in an
17535 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17536 is an expression. So, a classname followed by a dot is not a
17537 valid type-name. */
17538 || (objc_is_class_name (TREE_TYPE (type_decl
))
17539 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17541 if (!cp_parser_simulate_error (parser
))
17542 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17543 NLE_TYPE
, token
->location
);
17544 return error_mark_node
;
17546 /* Remember that the name was used in the definition of the
17547 current class so that we can check later to see if the
17548 meaning would have been different after the class was
17549 entirely defined. */
17550 else if (type_decl
!= error_mark_node
17552 maybe_note_name_used_in_class (identifier
, type_decl
);
17557 /* Parse an elaborated-type-specifier. Note that the grammar given
17558 here incorporates the resolution to DR68.
17560 elaborated-type-specifier:
17561 class-key :: [opt] nested-name-specifier [opt] identifier
17562 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17563 enum-key :: [opt] nested-name-specifier [opt] identifier
17564 typename :: [opt] nested-name-specifier identifier
17565 typename :: [opt] nested-name-specifier template [opt]
17570 elaborated-type-specifier:
17571 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17572 class-key attributes :: [opt] nested-name-specifier [opt]
17573 template [opt] template-id
17574 enum attributes :: [opt] nested-name-specifier [opt] identifier
17576 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17577 declared `friend'. If IS_DECLARATION is TRUE, then this
17578 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17579 something is being declared.
17581 Returns the TYPE specified. */
17584 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17586 bool is_declaration
)
17588 enum tag_types tag_type
;
17590 tree type
= NULL_TREE
;
17591 tree attributes
= NULL_TREE
;
17593 cp_token
*token
= NULL
;
17595 /* See if we're looking at the `enum' keyword. */
17596 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17598 /* Consume the `enum' token. */
17599 cp_lexer_consume_token (parser
->lexer
);
17600 /* Remember that it's an enumeration type. */
17601 tag_type
= enum_type
;
17602 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17603 enums) is used here. */
17604 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17605 if (cp_parser_is_keyword (token
, RID_CLASS
)
17606 || cp_parser_is_keyword (token
, RID_STRUCT
))
17608 gcc_rich_location
richloc (token
->location
);
17609 richloc
.add_range (input_location
, false);
17610 richloc
.add_fixit_remove ();
17611 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17612 "a scoped enum must not use the %qD keyword",
17614 /* Consume the `struct' or `class' and parse it anyway. */
17615 cp_lexer_consume_token (parser
->lexer
);
17617 /* Parse the attributes. */
17618 attributes
= cp_parser_attributes_opt (parser
);
17620 /* Or, it might be `typename'. */
17621 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17624 /* Consume the `typename' token. */
17625 cp_lexer_consume_token (parser
->lexer
);
17626 /* Remember that it's a `typename' type. */
17627 tag_type
= typename_type
;
17629 /* Otherwise it must be a class-key. */
17632 tag_type
= cp_parser_class_key (parser
);
17633 if (tag_type
== none_type
)
17634 return error_mark_node
;
17635 /* Parse the attributes. */
17636 attributes
= cp_parser_attributes_opt (parser
);
17639 /* Look for the `::' operator. */
17640 globalscope
= cp_parser_global_scope_opt (parser
,
17641 /*current_scope_valid_p=*/false);
17642 /* Look for the nested-name-specifier. */
17643 tree nested_name_specifier
;
17644 if (tag_type
== typename_type
&& !globalscope
)
17646 nested_name_specifier
17647 = cp_parser_nested_name_specifier (parser
,
17648 /*typename_keyword_p=*/true,
17649 /*check_dependency_p=*/true,
17652 if (!nested_name_specifier
)
17653 return error_mark_node
;
17656 /* Even though `typename' is not present, the proposed resolution
17657 to Core Issue 180 says that in `class A<T>::B', `B' should be
17658 considered a type-name, even if `A<T>' is dependent. */
17659 nested_name_specifier
17660 = cp_parser_nested_name_specifier_opt (parser
,
17661 /*typename_keyword_p=*/true,
17662 /*check_dependency_p=*/true,
17665 /* For everything but enumeration types, consider a template-id.
17666 For an enumeration type, consider only a plain identifier. */
17667 if (tag_type
!= enum_type
)
17669 bool template_p
= false;
17672 /* Allow the `template' keyword. */
17673 template_p
= cp_parser_optional_template_keyword (parser
);
17674 /* If we didn't see `template', we don't know if there's a
17675 template-id or not. */
17677 cp_parser_parse_tentatively (parser
);
17678 /* Parse the template-id. */
17679 token
= cp_lexer_peek_token (parser
->lexer
);
17680 decl
= cp_parser_template_id (parser
, template_p
,
17681 /*check_dependency_p=*/true,
17684 /* If we didn't find a template-id, look for an ordinary
17686 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17688 /* We can get here when cp_parser_template_id, called by
17689 cp_parser_class_name with tag_type == none_type, succeeds
17690 and caches a BASELINK. Then, when called again here,
17691 instead of failing and returning an error_mark_node
17692 returns it (see template/typename17.C in C++11).
17693 ??? Could we diagnose this earlier? */
17694 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17696 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17697 type
= error_mark_node
;
17699 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17700 in effect, then we must assume that, upon instantiation, the
17701 template will correspond to a class. */
17702 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17703 && tag_type
== typename_type
)
17704 type
= make_typename_type (parser
->scope
, decl
,
17706 /*complain=*/tf_error
);
17707 /* If the `typename' keyword is in effect and DECL is not a type
17708 decl, then type is non existent. */
17709 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17711 else if (TREE_CODE (decl
) == TYPE_DECL
)
17713 type
= check_elaborated_type_specifier (tag_type
, decl
,
17714 /*allow_template_p=*/true);
17716 /* If the next token is a semicolon, this must be a specialization,
17717 instantiation, or friend declaration. Check the scope while we
17718 still know whether or not we had a nested-name-specifier. */
17719 if (type
!= error_mark_node
17720 && !nested_name_specifier
&& !is_friend
17721 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17722 check_unqualified_spec_or_inst (type
, token
->location
);
17724 else if (decl
== error_mark_node
)
17725 type
= error_mark_node
;
17730 token
= cp_lexer_peek_token (parser
->lexer
);
17731 identifier
= cp_parser_identifier (parser
);
17733 if (identifier
== error_mark_node
)
17735 parser
->scope
= NULL_TREE
;
17736 return error_mark_node
;
17739 /* For a `typename', we needn't call xref_tag. */
17740 if (tag_type
== typename_type
17741 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17742 return cp_parser_make_typename_type (parser
, identifier
,
17745 /* Template parameter lists apply only if we are not within a
17746 function parameter list. */
17747 bool template_parm_lists_apply
17748 = parser
->num_template_parameter_lists
;
17749 if (template_parm_lists_apply
)
17750 for (cp_binding_level
*s
= current_binding_level
;
17751 s
&& s
->kind
!= sk_template_parms
;
17752 s
= s
->level_chain
)
17753 if (s
->kind
== sk_function_parms
)
17754 template_parm_lists_apply
= false;
17756 /* Look up a qualified name in the usual way. */
17760 tree ambiguous_decls
;
17762 decl
= cp_parser_lookup_name (parser
, identifier
,
17764 /*is_template=*/false,
17765 /*is_namespace=*/false,
17766 /*check_dependency=*/true,
17770 /* If the lookup was ambiguous, an error will already have been
17772 if (ambiguous_decls
)
17773 return error_mark_node
;
17775 /* If we are parsing friend declaration, DECL may be a
17776 TEMPLATE_DECL tree node here. However, we need to check
17777 whether this TEMPLATE_DECL results in valid code. Consider
17778 the following example:
17781 template <class T> class C {};
17784 template <class T> friend class N::C; // #1, valid code
17786 template <class T> class Y {
17787 friend class N::C; // #2, invalid code
17790 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17791 name lookup of `N::C'. We see that friend declaration must
17792 be template for the code to be valid. Note that
17793 processing_template_decl does not work here since it is
17794 always 1 for the above two cases. */
17796 decl
= (cp_parser_maybe_treat_template_as_class
17797 (decl
, /*tag_name_p=*/is_friend
17798 && template_parm_lists_apply
));
17800 if (TREE_CODE (decl
) != TYPE_DECL
)
17802 cp_parser_diagnose_invalid_type_name (parser
,
17805 return error_mark_node
;
17808 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17810 bool allow_template
= (template_parm_lists_apply
17811 || DECL_SELF_REFERENCE_P (decl
));
17812 type
= check_elaborated_type_specifier (tag_type
, decl
,
17815 if (type
== error_mark_node
)
17816 return error_mark_node
;
17819 /* Forward declarations of nested types, such as
17824 are invalid unless all components preceding the final '::'
17825 are complete. If all enclosing types are complete, these
17826 declarations become merely pointless.
17828 Invalid forward declarations of nested types are errors
17829 caught elsewhere in parsing. Those that are pointless arrive
17832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17833 && !is_friend
&& !processing_explicit_instantiation
)
17834 warning (0, "declaration %qD does not declare anything", decl
);
17836 type
= TREE_TYPE (decl
);
17840 /* An elaborated-type-specifier sometimes introduces a new type and
17841 sometimes names an existing type. Normally, the rule is that it
17842 introduces a new type only if there is not an existing type of
17843 the same name already in scope. For example, given:
17846 void f() { struct S s; }
17848 the `struct S' in the body of `f' is the same `struct S' as in
17849 the global scope; the existing definition is used. However, if
17850 there were no global declaration, this would introduce a new
17851 local class named `S'.
17853 An exception to this rule applies to the following code:
17855 namespace N { struct S; }
17857 Here, the elaborated-type-specifier names a new type
17858 unconditionally; even if there is already an `S' in the
17859 containing scope this declaration names a new type.
17860 This exception only applies if the elaborated-type-specifier
17861 forms the complete declaration:
17865 A declaration consisting solely of `class-key identifier ;' is
17866 either a redeclaration of the name in the current scope or a
17867 forward declaration of the identifier as a class name. It
17868 introduces the name into the current scope.
17870 We are in this situation precisely when the next token is a `;'.
17872 An exception to the exception is that a `friend' declaration does
17873 *not* name a new type; i.e., given:
17875 struct S { friend struct T; };
17877 `T' is not a new type in the scope of `S'.
17879 Also, `new struct S' or `sizeof (struct S)' never results in the
17880 definition of a new type; a new type can only be declared in a
17881 declaration context. */
17887 /* Friends have special name lookup rules. */
17888 ts
= ts_within_enclosing_non_class
;
17889 else if (is_declaration
17890 && cp_lexer_next_token_is (parser
->lexer
,
17892 /* This is a `class-key identifier ;' */
17898 (template_parm_lists_apply
17899 && (cp_parser_next_token_starts_class_definition_p (parser
)
17900 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17901 /* An unqualified name was used to reference this type, so
17902 there were no qualifying templates. */
17903 if (template_parm_lists_apply
17904 && !cp_parser_check_template_parameters (parser
,
17905 /*num_templates=*/0,
17907 /*declarator=*/NULL
))
17908 return error_mark_node
;
17909 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17913 if (type
== error_mark_node
)
17914 return error_mark_node
;
17916 /* Allow attributes on forward declarations of classes. */
17919 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17920 warning (OPT_Wattributes
,
17921 "attributes ignored on uninstantiated type");
17922 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17923 && ! processing_explicit_instantiation
)
17924 warning (OPT_Wattributes
,
17925 "attributes ignored on template instantiation");
17926 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17927 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17929 warning (OPT_Wattributes
,
17930 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17933 if (tag_type
!= enum_type
)
17935 /* Indicate whether this class was declared as a `class' or as a
17937 if (CLASS_TYPE_P (type
))
17938 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17939 cp_parser_check_class_key (tag_type
, type
);
17942 /* A "<" cannot follow an elaborated type specifier. If that
17943 happens, the user was probably trying to form a template-id. */
17944 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17950 /* Parse an enum-specifier.
17953 enum-head { enumerator-list [opt] }
17954 enum-head { enumerator-list , } [C++0x]
17957 enum-key identifier [opt] enum-base [opt]
17958 enum-key nested-name-specifier identifier enum-base [opt]
17963 enum struct [C++0x]
17966 : type-specifier-seq
17968 opaque-enum-specifier:
17969 enum-key identifier enum-base [opt] ;
17972 enum-key attributes[opt] identifier [opt] enum-base [opt]
17973 { enumerator-list [opt] }attributes[opt]
17974 enum-key attributes[opt] identifier [opt] enum-base [opt]
17975 { enumerator-list, }attributes[opt] [C++0x]
17977 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17978 if the token stream isn't an enum-specifier after all. */
17981 cp_parser_enum_specifier (cp_parser
* parser
)
17984 tree type
= NULL_TREE
;
17986 tree nested_name_specifier
= NULL_TREE
;
17988 bool scoped_enum_p
= false;
17989 bool has_underlying_type
= false;
17990 bool nested_being_defined
= false;
17991 bool new_value_list
= false;
17992 bool is_new_type
= false;
17993 bool is_unnamed
= false;
17994 tree underlying_type
= NULL_TREE
;
17995 cp_token
*type_start_token
= NULL
;
17996 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17998 parser
->colon_corrects_to_scope_p
= false;
18000 /* Parse tentatively so that we can back up if we don't find a
18002 cp_parser_parse_tentatively (parser
);
18004 /* Caller guarantees that the current token is 'enum', an identifier
18005 possibly follows, and the token after that is an opening brace.
18006 If we don't have an identifier, fabricate an anonymous name for
18007 the enumeration being defined. */
18008 cp_lexer_consume_token (parser
->lexer
);
18010 /* Parse the "class" or "struct", which indicates a scoped
18011 enumeration type in C++0x. */
18012 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18013 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18015 if (cxx_dialect
< cxx11
)
18016 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18018 /* Consume the `struct' or `class' token. */
18019 cp_lexer_consume_token (parser
->lexer
);
18021 scoped_enum_p
= true;
18024 attributes
= cp_parser_attributes_opt (parser
);
18026 /* Clear the qualification. */
18027 parser
->scope
= NULL_TREE
;
18028 parser
->qualifying_scope
= NULL_TREE
;
18029 parser
->object_scope
= NULL_TREE
;
18031 /* Figure out in what scope the declaration is being placed. */
18032 prev_scope
= current_scope ();
18034 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18036 push_deferring_access_checks (dk_no_check
);
18037 nested_name_specifier
18038 = cp_parser_nested_name_specifier_opt (parser
,
18039 /*typename_keyword_p=*/true,
18040 /*check_dependency_p=*/false,
18042 /*is_declaration=*/false);
18044 if (nested_name_specifier
)
18048 identifier
= cp_parser_identifier (parser
);
18049 name
= cp_parser_lookup_name (parser
, identifier
,
18051 /*is_template=*/false,
18052 /*is_namespace=*/false,
18053 /*check_dependency=*/true,
18054 /*ambiguous_decls=*/NULL
,
18056 if (name
&& name
!= error_mark_node
)
18058 type
= TREE_TYPE (name
);
18059 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18061 /* Are template enums allowed in ISO? */
18062 if (template_parm_scope_p ())
18063 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18064 "%qD is an enumeration template", name
);
18065 /* ignore a typename reference, for it will be solved by name
18070 else if (nested_name_specifier
== error_mark_node
)
18071 /* We already issued an error. */;
18074 error_at (type_start_token
->location
,
18075 "%qD does not name an enumeration in %qT",
18076 identifier
, nested_name_specifier
);
18077 nested_name_specifier
= error_mark_node
;
18082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18083 identifier
= cp_parser_identifier (parser
);
18086 identifier
= make_anon_name ();
18089 error_at (type_start_token
->location
,
18090 "unnamed scoped enum is not allowed");
18093 pop_deferring_access_checks ();
18095 /* Check for the `:' that denotes a specified underlying type in C++0x.
18096 Note that a ':' could also indicate a bitfield width, however. */
18097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18099 cp_decl_specifier_seq type_specifiers
;
18101 /* Consume the `:'. */
18102 cp_lexer_consume_token (parser
->lexer
);
18104 /* Parse the type-specifier-seq. */
18105 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18106 /*is_trailing_return=*/false,
18109 /* At this point this is surely not elaborated type specifier. */
18110 if (!cp_parser_parse_definitely (parser
))
18113 if (cxx_dialect
< cxx11
)
18114 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18116 has_underlying_type
= true;
18118 /* If that didn't work, stop. */
18119 if (type_specifiers
.type
!= error_mark_node
)
18121 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18122 /*initialized=*/0, NULL
);
18123 if (underlying_type
== error_mark_node
18124 || check_for_bare_parameter_packs (underlying_type
))
18125 underlying_type
= NULL_TREE
;
18129 /* Look for the `{' but don't consume it yet. */
18130 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18132 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18134 cp_parser_error (parser
, "expected %<{%>");
18135 if (has_underlying_type
)
18141 /* An opaque-enum-specifier must have a ';' here. */
18142 if ((scoped_enum_p
|| underlying_type
)
18143 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18145 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18146 if (has_underlying_type
)
18154 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18157 if (nested_name_specifier
)
18159 if (CLASS_TYPE_P (nested_name_specifier
))
18161 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18162 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18163 push_scope (nested_name_specifier
);
18165 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18167 push_nested_namespace (nested_name_specifier
);
18171 /* Issue an error message if type-definitions are forbidden here. */
18172 if (!cp_parser_check_type_definition (parser
))
18173 type
= error_mark_node
;
18175 /* Create the new type. We do this before consuming the opening
18176 brace so the enum will be recorded as being on the line of its
18177 tag (or the 'enum' keyword, if there is no tag). */
18178 type
= start_enum (identifier
, type
, underlying_type
,
18179 attributes
, scoped_enum_p
, &is_new_type
);
18181 /* If the next token is not '{' it is an opaque-enum-specifier or an
18182 elaborated-type-specifier. */
18183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18185 timevar_push (TV_PARSE_ENUM
);
18186 if (nested_name_specifier
18187 && nested_name_specifier
!= error_mark_node
)
18189 /* The following catches invalid code such as:
18190 enum class S<int>::E { A, B, C }; */
18191 if (!processing_specialization
18192 && CLASS_TYPE_P (nested_name_specifier
)
18193 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18194 error_at (type_start_token
->location
, "cannot add an enumerator "
18195 "list to a template instantiation");
18197 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18199 error_at (type_start_token
->location
,
18200 "%<%T::%E%> has not been declared",
18201 TYPE_CONTEXT (nested_name_specifier
),
18202 nested_name_specifier
);
18203 type
= error_mark_node
;
18205 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18206 && !CLASS_TYPE_P (nested_name_specifier
))
18208 error_at (type_start_token
->location
, "nested name specifier "
18209 "%qT for enum declaration does not name a class "
18210 "or namespace", nested_name_specifier
);
18211 type
= error_mark_node
;
18213 /* If that scope does not contain the scope in which the
18214 class was originally declared, the program is invalid. */
18215 else if (prev_scope
&& !is_ancestor (prev_scope
,
18216 nested_name_specifier
))
18218 if (at_namespace_scope_p ())
18219 error_at (type_start_token
->location
,
18220 "declaration of %qD in namespace %qD which does not "
18222 type
, prev_scope
, nested_name_specifier
);
18224 error_at (type_start_token
->location
,
18225 "declaration of %qD in %qD which does not "
18227 type
, prev_scope
, nested_name_specifier
);
18228 type
= error_mark_node
;
18230 /* If that scope is the scope where the declaration is being placed
18231 the program is invalid. */
18232 else if (CLASS_TYPE_P (nested_name_specifier
)
18233 && CLASS_TYPE_P (prev_scope
)
18234 && same_type_p (nested_name_specifier
, prev_scope
))
18236 permerror (type_start_token
->location
,
18237 "extra qualification not allowed");
18238 nested_name_specifier
= NULL_TREE
;
18243 begin_scope (sk_scoped_enum
, type
);
18245 /* Consume the opening brace. */
18246 matching_braces braces
;
18247 braces
.consume_open (parser
);
18249 if (type
== error_mark_node
)
18250 ; /* Nothing to add */
18251 else if (OPAQUE_ENUM_P (type
)
18252 || (cxx_dialect
> cxx98
&& processing_specialization
))
18254 new_value_list
= true;
18255 SET_OPAQUE_ENUM_P (type
, false);
18256 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18260 error_at (type_start_token
->location
,
18261 "multiple definition of %q#T", type
);
18262 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18263 "previous definition here");
18264 type
= error_mark_node
;
18267 if (type
== error_mark_node
)
18268 cp_parser_skip_to_end_of_block_or_statement (parser
);
18269 /* If the next token is not '}', then there are some enumerators. */
18270 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18272 if (is_unnamed
&& !scoped_enum_p
)
18273 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18274 "ISO C++ forbids empty unnamed enum");
18277 cp_parser_enumerator_list (parser
, type
);
18279 /* Consume the final '}'. */
18280 braces
.require_close (parser
);
18284 timevar_pop (TV_PARSE_ENUM
);
18288 /* If a ';' follows, then it is an opaque-enum-specifier
18289 and additional restrictions apply. */
18290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18293 error_at (type_start_token
->location
,
18294 "opaque-enum-specifier without name");
18295 else if (nested_name_specifier
)
18296 error_at (type_start_token
->location
,
18297 "opaque-enum-specifier must use a simple identifier");
18301 /* Look for trailing attributes to apply to this enumeration, and
18302 apply them if appropriate. */
18303 if (cp_parser_allow_gnu_extensions_p (parser
))
18305 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18306 cplus_decl_attributes (&type
,
18308 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18311 /* Finish up the enumeration. */
18312 if (type
!= error_mark_node
)
18314 if (new_value_list
)
18315 finish_enum_value_list (type
);
18317 finish_enum (type
);
18320 if (nested_name_specifier
)
18322 if (CLASS_TYPE_P (nested_name_specifier
))
18324 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18325 pop_scope (nested_name_specifier
);
18327 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18329 pop_nested_namespace (nested_name_specifier
);
18333 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18337 /* Parse an enumerator-list. The enumerators all have the indicated
18341 enumerator-definition
18342 enumerator-list , enumerator-definition */
18345 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18349 /* Parse an enumerator-definition. */
18350 cp_parser_enumerator_definition (parser
, type
);
18352 /* If the next token is not a ',', we've reached the end of
18354 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18356 /* Otherwise, consume the `,' and keep going. */
18357 cp_lexer_consume_token (parser
->lexer
);
18358 /* If the next token is a `}', there is a trailing comma. */
18359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18361 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18362 pedwarn (input_location
, OPT_Wpedantic
,
18363 "comma at end of enumerator list");
18369 /* Parse an enumerator-definition. The enumerator has the indicated
18372 enumerator-definition:
18374 enumerator = constant-expression
18381 enumerator-definition:
18382 enumerator attributes [opt]
18383 enumerator attributes [opt] = constant-expression */
18386 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18392 /* Save the input location because we are interested in the location
18393 of the identifier and not the location of the explicit value. */
18394 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18396 /* Look for the identifier. */
18397 identifier
= cp_parser_identifier (parser
);
18398 if (identifier
== error_mark_node
)
18401 /* Parse any specified attributes. */
18402 tree attrs
= cp_parser_attributes_opt (parser
);
18404 /* If the next token is an '=', then there is an explicit value. */
18405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18407 /* Consume the `=' token. */
18408 cp_lexer_consume_token (parser
->lexer
);
18409 /* Parse the value. */
18410 value
= cp_parser_constant_expression (parser
);
18415 /* If we are processing a template, make sure the initializer of the
18416 enumerator doesn't contain any bare template parameter pack. */
18417 if (check_for_bare_parameter_packs (value
))
18418 value
= error_mark_node
;
18420 /* Create the enumerator. */
18421 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18424 /* Parse a namespace-name.
18427 original-namespace-name
18430 Returns the NAMESPACE_DECL for the namespace. */
18433 cp_parser_namespace_name (cp_parser
* parser
)
18436 tree namespace_decl
;
18438 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18440 /* Get the name of the namespace. */
18441 identifier
= cp_parser_identifier (parser
);
18442 if (identifier
== error_mark_node
)
18443 return error_mark_node
;
18445 /* Look up the identifier in the currently active scope. Look only
18446 for namespaces, due to:
18448 [basic.lookup.udir]
18450 When looking up a namespace-name in a using-directive or alias
18451 definition, only namespace names are considered.
18455 [basic.lookup.qual]
18457 During the lookup of a name preceding the :: scope resolution
18458 operator, object, function, and enumerator names are ignored.
18460 (Note that cp_parser_qualifying_entity only calls this
18461 function if the token after the name is the scope resolution
18463 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18465 /*is_template=*/false,
18466 /*is_namespace=*/true,
18467 /*check_dependency=*/true,
18468 /*ambiguous_decls=*/NULL
,
18470 /* If it's not a namespace, issue an error. */
18471 if (namespace_decl
== error_mark_node
18472 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18474 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18476 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18477 if (namespace_decl
== error_mark_node
18478 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18479 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18482 cp_parser_error (parser
, "expected namespace-name");
18483 namespace_decl
= error_mark_node
;
18486 return namespace_decl
;
18489 /* Parse a namespace-definition.
18491 namespace-definition:
18492 named-namespace-definition
18493 unnamed-namespace-definition
18495 named-namespace-definition:
18496 original-namespace-definition
18497 extension-namespace-definition
18499 original-namespace-definition:
18500 namespace identifier { namespace-body }
18502 extension-namespace-definition:
18503 namespace original-namespace-name { namespace-body }
18505 unnamed-namespace-definition:
18506 namespace { namespace-body } */
18509 cp_parser_namespace_definition (cp_parser
* parser
)
18512 int nested_definition_count
= 0;
18514 cp_ensure_no_omp_declare_simd (parser
);
18515 cp_ensure_no_oacc_routine (parser
);
18517 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18521 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18522 cp_lexer_consume_token (parser
->lexer
);
18525 /* Look for the `namespace' keyword. */
18527 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18529 /* Parse any specified attributes before the identifier. */
18530 tree attribs
= cp_parser_attributes_opt (parser
);
18534 identifier
= NULL_TREE
;
18536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18538 identifier
= cp_parser_identifier (parser
);
18540 /* Parse any attributes specified after the identifier. */
18541 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18544 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18547 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18548 pedwarn (input_location
, OPT_Wpedantic
,
18549 "nested namespace definitions only available with "
18550 "-std=c++17 or -std=gnu++17");
18552 /* Nested namespace names can create new namespaces (unlike
18553 other qualified-ids). */
18554 if (int count
= identifier
? push_namespace (identifier
) : 0)
18555 nested_definition_count
+= count
;
18557 cp_parser_error (parser
, "nested namespace name required");
18558 cp_lexer_consume_token (parser
->lexer
);
18561 if (nested_definition_count
&& !identifier
)
18562 cp_parser_error (parser
, "namespace name required");
18564 if (nested_definition_count
&& attribs
)
18565 error_at (token
->location
,
18566 "a nested namespace definition cannot have attributes");
18567 if (nested_definition_count
&& is_inline
)
18568 error_at (token
->location
,
18569 "a nested namespace definition cannot be inline");
18571 /* Start the namespace. */
18572 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18574 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18576 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18578 /* Look for the `{' to validate starting the namespace. */
18579 matching_braces braces
;
18580 if (braces
.require_open (parser
))
18582 /* Parse the body of the namespace. */
18583 cp_parser_namespace_body (parser
);
18585 /* Look for the final `}'. */
18586 braces
.require_close (parser
);
18589 if (has_visibility
)
18590 pop_visibility (1);
18592 /* Pop the nested namespace definitions. */
18593 while (nested_definition_count
--)
18597 /* Parse a namespace-body.
18600 declaration-seq [opt] */
18603 cp_parser_namespace_body (cp_parser
* parser
)
18605 cp_parser_declaration_seq_opt (parser
);
18608 /* Parse a namespace-alias-definition.
18610 namespace-alias-definition:
18611 namespace identifier = qualified-namespace-specifier ; */
18614 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18617 tree namespace_specifier
;
18619 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18621 /* Look for the `namespace' keyword. */
18622 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18623 /* Look for the identifier. */
18624 identifier
= cp_parser_identifier (parser
);
18625 if (identifier
== error_mark_node
)
18627 /* Look for the `=' token. */
18628 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18629 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18631 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18632 /* Skip the definition. */
18633 cp_lexer_consume_token (parser
->lexer
);
18634 if (cp_parser_skip_to_closing_brace (parser
))
18635 cp_lexer_consume_token (parser
->lexer
);
18638 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18639 /* Look for the qualified-namespace-specifier. */
18640 namespace_specifier
18641 = cp_parser_qualified_namespace_specifier (parser
);
18642 /* Look for the `;' token. */
18643 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18645 /* Register the alias in the symbol table. */
18646 do_namespace_alias (identifier
, namespace_specifier
);
18649 /* Parse a qualified-namespace-specifier.
18651 qualified-namespace-specifier:
18652 :: [opt] nested-name-specifier [opt] namespace-name
18654 Returns a NAMESPACE_DECL corresponding to the specified
18658 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18660 /* Look for the optional `::'. */
18661 cp_parser_global_scope_opt (parser
,
18662 /*current_scope_valid_p=*/false);
18664 /* Look for the optional nested-name-specifier. */
18665 cp_parser_nested_name_specifier_opt (parser
,
18666 /*typename_keyword_p=*/false,
18667 /*check_dependency_p=*/true,
18669 /*is_declaration=*/true);
18671 return cp_parser_namespace_name (parser
);
18674 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18675 access declaration.
18678 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18679 using :: unqualified-id ;
18681 access-declaration:
18687 cp_parser_using_declaration (cp_parser
* parser
,
18688 bool access_declaration_p
)
18691 bool typename_p
= false;
18692 bool global_scope_p
;
18696 int oldcount
= errorcount
;
18697 cp_token
*diag_token
= NULL
;
18699 if (access_declaration_p
)
18701 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18702 cp_parser_parse_tentatively (parser
);
18706 /* Look for the `using' keyword. */
18707 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18710 /* Peek at the next token. */
18711 token
= cp_lexer_peek_token (parser
->lexer
);
18712 /* See if it's `typename'. */
18713 if (token
->keyword
== RID_TYPENAME
)
18715 /* Remember that we've seen it. */
18717 /* Consume the `typename' token. */
18718 cp_lexer_consume_token (parser
->lexer
);
18722 /* Look for the optional global scope qualification. */
18724 = (cp_parser_global_scope_opt (parser
,
18725 /*current_scope_valid_p=*/false)
18728 /* If we saw `typename', or didn't see `::', then there must be a
18729 nested-name-specifier present. */
18730 if (typename_p
|| !global_scope_p
)
18732 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18733 /*check_dependency_p=*/true,
18735 /*is_declaration=*/true);
18736 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18738 cp_parser_skip_to_end_of_block_or_statement (parser
);
18742 /* Otherwise, we could be in either of the two productions. In that
18743 case, treat the nested-name-specifier as optional. */
18745 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18746 /*typename_keyword_p=*/false,
18747 /*check_dependency_p=*/true,
18749 /*is_declaration=*/true);
18751 qscope
= global_namespace
;
18752 else if (UNSCOPED_ENUM_P (qscope
))
18753 qscope
= CP_TYPE_CONTEXT (qscope
);
18755 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18756 /* Something has already gone wrong; there's no need to parse
18757 further. Since an error has occurred, the return value of
18758 cp_parser_parse_definitely will be false, as required. */
18759 return cp_parser_parse_definitely (parser
);
18761 token
= cp_lexer_peek_token (parser
->lexer
);
18762 /* Parse the unqualified-id. */
18763 identifier
= cp_parser_unqualified_id (parser
,
18764 /*template_keyword_p=*/false,
18765 /*check_dependency_p=*/true,
18766 /*declarator_p=*/true,
18767 /*optional_p=*/false);
18769 if (access_declaration_p
)
18771 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18772 cp_parser_simulate_error (parser
);
18773 if (!cp_parser_parse_definitely (parser
))
18776 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18778 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18779 if (cxx_dialect
< cxx17
18780 && !in_system_header_at (ell
->location
))
18781 pedwarn (ell
->location
, 0,
18782 "pack expansion in using-declaration only available "
18783 "with -std=c++17 or -std=gnu++17");
18784 qscope
= make_pack_expansion (qscope
);
18787 /* The function we call to handle a using-declaration is different
18788 depending on what scope we are in. */
18789 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18791 else if (!identifier_p (identifier
)
18792 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18793 /* [namespace.udecl]
18795 A using declaration shall not name a template-id. */
18796 error_at (token
->location
,
18797 "a template-id may not appear in a using-declaration");
18800 if (at_class_scope_p ())
18802 /* Create the USING_DECL. */
18803 decl
= do_class_using_decl (qscope
, identifier
);
18805 if (decl
&& typename_p
)
18806 USING_DECL_TYPENAME_P (decl
) = 1;
18808 if (check_for_bare_parameter_packs (decl
))
18810 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18814 /* Add it to the list of members in this class. */
18815 finish_member_declaration (decl
);
18819 decl
= cp_parser_lookup_name_simple (parser
,
18822 if (decl
== error_mark_node
)
18823 cp_parser_name_lookup_error (parser
, identifier
,
18826 else if (check_for_bare_parameter_packs (decl
))
18828 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18831 else if (!at_namespace_scope_p ())
18832 finish_local_using_decl (decl
, qscope
, identifier
);
18834 finish_namespace_using_decl (decl
, qscope
, identifier
);
18838 if (!access_declaration_p
18839 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18841 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18842 if (cxx_dialect
< cxx17
)
18843 pedwarn (comma
->location
, 0,
18844 "comma-separated list in using-declaration only available "
18845 "with -std=c++17 or -std=gnu++17");
18849 /* Look for the final `;'. */
18850 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18852 if (access_declaration_p
&& errorcount
== oldcount
)
18853 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18854 "access declarations are deprecated "
18855 "in favour of using-declarations; "
18856 "suggestion: add the %<using%> keyword");
18861 /* Parse an alias-declaration.
18864 using identifier attribute-specifier-seq [opt] = type-id */
18867 cp_parser_alias_declaration (cp_parser
* parser
)
18869 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18870 location_t id_location
;
18871 cp_declarator
*declarator
;
18872 cp_decl_specifier_seq decl_specs
;
18874 const char *saved_message
= NULL
;
18876 /* Look for the `using' keyword. */
18877 cp_token
*using_token
18878 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18879 if (using_token
== NULL
)
18880 return error_mark_node
;
18882 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18883 id
= cp_parser_identifier (parser
);
18884 if (id
== error_mark_node
)
18885 return error_mark_node
;
18887 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18888 attributes
= cp_parser_attributes_opt (parser
);
18889 if (attributes
== error_mark_node
)
18890 return error_mark_node
;
18892 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18894 if (cp_parser_error_occurred (parser
))
18895 return error_mark_node
;
18897 cp_parser_commit_to_tentative_parse (parser
);
18899 /* Now we are going to parse the type-id of the declaration. */
18904 "A type-specifier-seq shall not define a class or enumeration
18905 unless it appears in the type-id of an alias-declaration (7.1.3) that
18906 is not the declaration of a template-declaration."
18908 In other words, if we currently are in an alias template, the
18909 type-id should not define a type.
18911 So let's set parser->type_definition_forbidden_message in that
18912 case; cp_parser_check_type_definition (called by
18913 cp_parser_class_specifier) will then emit an error if a type is
18914 defined in the type-id. */
18915 if (parser
->num_template_parameter_lists
)
18917 saved_message
= parser
->type_definition_forbidden_message
;
18918 parser
->type_definition_forbidden_message
=
18919 G_("types may not be defined in alias template declarations");
18922 type
= cp_parser_type_id (parser
);
18924 /* Restore the error message if need be. */
18925 if (parser
->num_template_parameter_lists
)
18926 parser
->type_definition_forbidden_message
= saved_message
;
18928 if (type
== error_mark_node
18929 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18931 cp_parser_skip_to_end_of_block_or_statement (parser
);
18932 return error_mark_node
;
18935 /* A typedef-name can also be introduced by an alias-declaration. The
18936 identifier following the using keyword becomes a typedef-name. It has
18937 the same semantics as if it were introduced by the typedef
18938 specifier. In particular, it does not define a new type and it shall
18939 not appear in the type-id. */
18941 clear_decl_specs (&decl_specs
);
18942 decl_specs
.type
= type
;
18943 if (attributes
!= NULL_TREE
)
18945 decl_specs
.attributes
= attributes
;
18946 set_and_check_decl_spec_loc (&decl_specs
,
18950 set_and_check_decl_spec_loc (&decl_specs
,
18953 set_and_check_decl_spec_loc (&decl_specs
,
18957 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18958 declarator
->id_loc
= id_location
;
18960 member_p
= at_class_scope_p ();
18962 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18963 NULL_TREE
, attributes
);
18965 decl
= start_decl (declarator
, &decl_specs
, 0,
18966 attributes
, NULL_TREE
, &pushed_scope
);
18967 if (decl
== error_mark_node
)
18970 // Attach constraints to the alias declaration.
18971 if (flag_concepts
&& current_template_parms
)
18973 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18974 tree constr
= build_constraints (reqs
, NULL_TREE
);
18975 set_constraints (decl
, constr
);
18978 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18981 pop_scope (pushed_scope
);
18983 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18984 added into the symbol table; otherwise, return the TYPE_DECL. */
18985 if (DECL_LANG_SPECIFIC (decl
)
18986 && DECL_TEMPLATE_INFO (decl
)
18987 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18989 decl
= DECL_TI_TEMPLATE (decl
);
18991 check_member_template (decl
);
18997 /* Parse a using-directive.
19000 using namespace :: [opt] nested-name-specifier [opt]
19001 namespace-name ; */
19004 cp_parser_using_directive (cp_parser
* parser
)
19006 tree namespace_decl
;
19009 /* Look for the `using' keyword. */
19010 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19011 /* And the `namespace' keyword. */
19012 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19013 /* Look for the optional `::' operator. */
19014 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19015 /* And the optional nested-name-specifier. */
19016 cp_parser_nested_name_specifier_opt (parser
,
19017 /*typename_keyword_p=*/false,
19018 /*check_dependency_p=*/true,
19020 /*is_declaration=*/true);
19021 /* Get the namespace being used. */
19022 namespace_decl
= cp_parser_namespace_name (parser
);
19023 /* And any specified attributes. */
19024 attribs
= cp_parser_attributes_opt (parser
);
19026 /* Update the symbol table. */
19027 if (namespace_bindings_p ())
19028 finish_namespace_using_directive (namespace_decl
, attribs
);
19030 finish_local_using_directive (namespace_decl
, attribs
);
19032 /* Look for the final `;'. */
19033 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19036 /* Parse an asm-definition.
19039 asm ( string-literal ) ;
19044 asm volatile [opt] ( string-literal ) ;
19045 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19046 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19047 : asm-operand-list [opt] ) ;
19048 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19049 : asm-operand-list [opt]
19050 : asm-clobber-list [opt] ) ;
19051 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19052 : asm-clobber-list [opt]
19053 : asm-goto-list ) ; */
19056 cp_parser_asm_definition (cp_parser
* parser
)
19059 tree outputs
= NULL_TREE
;
19060 tree inputs
= NULL_TREE
;
19061 tree clobbers
= NULL_TREE
;
19062 tree labels
= NULL_TREE
;
19064 bool volatile_p
= false;
19065 bool extended_p
= false;
19066 bool invalid_inputs_p
= false;
19067 bool invalid_outputs_p
= false;
19068 bool goto_p
= false;
19069 required_token missing
= RT_NONE
;
19071 /* Look for the `asm' keyword. */
19072 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19074 if (parser
->in_function_body
19075 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19077 error ("%<asm%> in %<constexpr%> function");
19078 cp_function_chain
->invalid_constexpr
= true;
19081 /* See if the next token is `volatile'. */
19082 if (cp_parser_allow_gnu_extensions_p (parser
)
19083 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19085 /* Remember that we saw the `volatile' keyword. */
19087 /* Consume the token. */
19088 cp_lexer_consume_token (parser
->lexer
);
19090 if (cp_parser_allow_gnu_extensions_p (parser
)
19091 && parser
->in_function_body
19092 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19094 /* Remember that we saw the `goto' keyword. */
19096 /* Consume the token. */
19097 cp_lexer_consume_token (parser
->lexer
);
19099 /* Look for the opening `('. */
19100 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19102 /* Look for the string. */
19103 string
= cp_parser_string_literal (parser
, false, false);
19104 if (string
== error_mark_node
)
19106 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19107 /*consume_paren=*/true);
19111 /* If we're allowing GNU extensions, check for the extended assembly
19112 syntax. Unfortunately, the `:' tokens need not be separated by
19113 a space in C, and so, for compatibility, we tolerate that here
19114 too. Doing that means that we have to treat the `::' operator as
19116 if (cp_parser_allow_gnu_extensions_p (parser
)
19117 && parser
->in_function_body
19118 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19119 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19121 bool inputs_p
= false;
19122 bool clobbers_p
= false;
19123 bool labels_p
= false;
19125 /* The extended syntax was used. */
19128 /* Look for outputs. */
19129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19131 /* Consume the `:'. */
19132 cp_lexer_consume_token (parser
->lexer
);
19133 /* Parse the output-operands. */
19134 if (cp_lexer_next_token_is_not (parser
->lexer
,
19136 && cp_lexer_next_token_is_not (parser
->lexer
,
19138 && cp_lexer_next_token_is_not (parser
->lexer
,
19142 outputs
= cp_parser_asm_operand_list (parser
);
19143 if (outputs
== error_mark_node
)
19144 invalid_outputs_p
= true;
19147 /* If the next token is `::', there are no outputs, and the
19148 next token is the beginning of the inputs. */
19149 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19150 /* The inputs are coming next. */
19153 /* Look for inputs. */
19155 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19157 /* Consume the `:' or `::'. */
19158 cp_lexer_consume_token (parser
->lexer
);
19159 /* Parse the output-operands. */
19160 if (cp_lexer_next_token_is_not (parser
->lexer
,
19162 && cp_lexer_next_token_is_not (parser
->lexer
,
19164 && cp_lexer_next_token_is_not (parser
->lexer
,
19167 inputs
= cp_parser_asm_operand_list (parser
);
19168 if (inputs
== error_mark_node
)
19169 invalid_inputs_p
= true;
19172 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19173 /* The clobbers are coming next. */
19176 /* Look for clobbers. */
19178 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19181 /* Consume the `:' or `::'. */
19182 cp_lexer_consume_token (parser
->lexer
);
19183 /* Parse the clobbers. */
19184 if (cp_lexer_next_token_is_not (parser
->lexer
,
19186 && cp_lexer_next_token_is_not (parser
->lexer
,
19188 clobbers
= cp_parser_asm_clobber_list (parser
);
19191 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19192 /* The labels are coming next. */
19195 /* Look for labels. */
19197 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19200 /* Consume the `:' or `::'. */
19201 cp_lexer_consume_token (parser
->lexer
);
19202 /* Parse the labels. */
19203 labels
= cp_parser_asm_label_list (parser
);
19206 if (goto_p
&& !labels_p
)
19207 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19210 missing
= RT_COLON_SCOPE
;
19212 /* Look for the closing `)'. */
19213 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19214 missing
? missing
: RT_CLOSE_PAREN
))
19215 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19216 /*consume_paren=*/true);
19217 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19219 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19221 /* Create the ASM_EXPR. */
19222 if (parser
->in_function_body
)
19224 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19225 inputs
, clobbers
, labels
);
19226 /* If the extended syntax was not used, mark the ASM_EXPR. */
19229 tree temp
= asm_stmt
;
19230 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19231 temp
= TREE_OPERAND (temp
, 0);
19233 ASM_INPUT_P (temp
) = 1;
19237 symtab
->finalize_toplevel_asm (string
);
19241 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19242 type that comes from the decl-specifier-seq. */
19245 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19247 for (cp_declarator
*d
= declarator
; d
;)
19257 if (TYPE_PTRMEMFUNC_P (type
))
19258 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19259 type
= TREE_TYPE (type
);
19267 /* Declarators [gram.dcl.decl] */
19269 /* Parse an init-declarator.
19272 declarator initializer [opt]
19277 declarator asm-specification [opt] attributes [opt] initializer [opt]
19279 function-definition:
19280 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19282 decl-specifier-seq [opt] declarator function-try-block
19286 function-definition:
19287 __extension__ function-definition
19291 function-definition:
19292 decl-specifier-seq [opt] declarator function-transaction-block
19294 The DECL_SPECIFIERS apply to this declarator. Returns a
19295 representation of the entity declared. If MEMBER_P is TRUE, then
19296 this declarator appears in a class scope. The new DECL created by
19297 this declarator is returned.
19299 The CHECKS are access checks that should be performed once we know
19300 what entity is being declared (and, therefore, what classes have
19303 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19304 for a function-definition here as well. If the declarator is a
19305 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19306 be TRUE upon return. By that point, the function-definition will
19307 have been completely parsed.
19309 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19312 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19313 parsed declaration if it is an uninitialized single declarator not followed
19314 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19315 if present, will not be consumed. If returned, this declarator will be
19316 created with SD_INITIALIZED but will not call cp_finish_decl.
19318 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19319 and there is an initializer, the pointed location_t is set to the
19320 location of the '=' or `(', or '{' in C++11 token introducing the
19324 cp_parser_init_declarator (cp_parser
* parser
,
19325 cp_decl_specifier_seq
*decl_specifiers
,
19326 vec
<deferred_access_check
, va_gc
> *checks
,
19327 bool function_definition_allowed_p
,
19329 int declares_class_or_enum
,
19330 bool* function_definition_p
,
19331 tree
* maybe_range_for_decl
,
19332 location_t
* init_loc
,
19335 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19336 *attributes_start_token
= NULL
;
19337 cp_declarator
*declarator
;
19338 tree prefix_attributes
;
19339 tree attributes
= NULL
;
19340 tree asm_specification
;
19342 tree decl
= NULL_TREE
;
19344 int is_initialized
;
19345 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19346 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19348 enum cpp_ttype initialization_kind
;
19349 bool is_direct_init
= false;
19350 bool is_non_constant_init
;
19351 int ctor_dtor_or_conv_p
;
19352 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19353 tree pushed_scope
= NULL_TREE
;
19354 bool range_for_decl_p
= false;
19355 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19356 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19358 /* Gather the attributes that were provided with the
19359 decl-specifiers. */
19360 prefix_attributes
= decl_specifiers
->attributes
;
19362 /* Assume that this is not the declarator for a function
19364 if (function_definition_p
)
19365 *function_definition_p
= false;
19367 /* Default arguments are only permitted for function parameters. */
19368 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19369 parser
->default_arg_ok_p
= false;
19371 /* Defer access checks while parsing the declarator; we cannot know
19372 what names are accessible until we know what is being
19374 resume_deferring_access_checks ();
19376 token
= cp_lexer_peek_token (parser
->lexer
);
19378 /* Parse the declarator. */
19380 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19381 &ctor_dtor_or_conv_p
,
19382 /*parenthesized_p=*/NULL
,
19383 member_p
, friend_p
);
19384 /* Gather up the deferred checks. */
19385 stop_deferring_access_checks ();
19387 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19389 /* If the DECLARATOR was erroneous, there's no need to go
19391 if (declarator
== cp_error_declarator
)
19392 return error_mark_node
;
19394 /* Check that the number of template-parameter-lists is OK. */
19395 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19397 return error_mark_node
;
19399 if (declares_class_or_enum
& 2)
19400 cp_parser_check_for_definition_in_return_type (declarator
,
19401 decl_specifiers
->type
,
19402 decl_specifiers
->locations
[ds_type_spec
]);
19404 /* Figure out what scope the entity declared by the DECLARATOR is
19405 located in. `grokdeclarator' sometimes changes the scope, so
19406 we compute it now. */
19407 scope
= get_scope_of_declarator (declarator
);
19409 /* Perform any lookups in the declared type which were thought to be
19410 dependent, but are not in the scope of the declarator. */
19411 decl_specifiers
->type
19412 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19414 /* If we're allowing GNU extensions, look for an
19415 asm-specification. */
19416 if (cp_parser_allow_gnu_extensions_p (parser
))
19418 /* Look for an asm-specification. */
19419 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19420 asm_specification
= cp_parser_asm_specification_opt (parser
);
19423 asm_specification
= NULL_TREE
;
19425 /* Look for attributes. */
19426 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19427 attributes
= cp_parser_attributes_opt (parser
);
19429 /* Peek at the next token. */
19430 token
= cp_lexer_peek_token (parser
->lexer
);
19432 bool bogus_implicit_tmpl
= false;
19434 if (function_declarator_p (declarator
))
19436 /* Handle C++17 deduction guides. */
19437 if (!decl_specifiers
->type
19438 && ctor_dtor_or_conv_p
<= 0
19439 && cxx_dialect
>= cxx17
)
19441 cp_declarator
*id
= get_id_declarator (declarator
);
19442 tree name
= id
->u
.id
.unqualified_name
;
19443 parser
->scope
= id
->u
.id
.qualifying_scope
;
19444 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19446 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19447 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19449 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19450 id
->u
.id
.sfk
= sfk_deduction_guide
;
19451 ctor_dtor_or_conv_p
= 1;
19455 /* Check to see if the token indicates the start of a
19456 function-definition. */
19457 if (cp_parser_token_starts_function_definition_p (token
))
19459 if (!function_definition_allowed_p
)
19461 /* If a function-definition should not appear here, issue an
19463 cp_parser_error (parser
,
19464 "a function-definition is not allowed here");
19465 return error_mark_node
;
19468 location_t func_brace_location
19469 = cp_lexer_peek_token (parser
->lexer
)->location
;
19471 /* Neither attributes nor an asm-specification are allowed
19472 on a function-definition. */
19473 if (asm_specification
)
19474 error_at (asm_spec_start_token
->location
,
19475 "an asm-specification is not allowed "
19476 "on a function-definition");
19478 error_at (attributes_start_token
->location
,
19479 "attributes are not allowed "
19480 "on a function-definition");
19481 /* This is a function-definition. */
19482 *function_definition_p
= true;
19484 /* Parse the function definition. */
19486 decl
= cp_parser_save_member_function_body (parser
,
19489 prefix_attributes
);
19492 (cp_parser_function_definition_from_specifiers_and_declarator
19493 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19495 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19497 /* This is where the prologue starts... */
19498 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19499 = func_brace_location
;
19505 else if (parser
->fully_implicit_function_template_p
)
19507 /* A non-template declaration involving a function parameter list
19508 containing an implicit template parameter will be made into a
19509 template. If the resulting declaration is not going to be an
19510 actual function then finish the template scope here to prevent it.
19511 An error message will be issued once we have a decl to talk about.
19513 FIXME probably we should do type deduction rather than create an
19514 implicit template, but the standard currently doesn't allow it. */
19515 bogus_implicit_tmpl
= true;
19516 finish_fully_implicit_template (parser
, NULL_TREE
);
19521 Only in function declarations for constructors, destructors, type
19522 conversions, and deduction guides can the decl-specifier-seq be omitted.
19524 We explicitly postpone this check past the point where we handle
19525 function-definitions because we tolerate function-definitions
19526 that are missing their return types in some modes. */
19527 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19529 cp_parser_error (parser
,
19530 "expected constructor, destructor, or type conversion");
19531 return error_mark_node
;
19534 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19535 if (token
->type
== CPP_EQ
19536 || token
->type
== CPP_OPEN_PAREN
19537 || token
->type
== CPP_OPEN_BRACE
)
19539 is_initialized
= SD_INITIALIZED
;
19540 initialization_kind
= token
->type
;
19541 if (maybe_range_for_decl
)
19542 *maybe_range_for_decl
= error_mark_node
;
19543 tmp_init_loc
= token
->location
;
19544 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19545 *init_loc
= tmp_init_loc
;
19547 if (token
->type
== CPP_EQ
19548 && function_declarator_p (declarator
))
19550 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19551 if (t2
->keyword
== RID_DEFAULT
)
19552 is_initialized
= SD_DEFAULTED
;
19553 else if (t2
->keyword
== RID_DELETE
)
19554 is_initialized
= SD_DELETED
;
19559 /* If the init-declarator isn't initialized and isn't followed by a
19560 `,' or `;', it's not a valid init-declarator. */
19561 if (token
->type
!= CPP_COMMA
19562 && token
->type
!= CPP_SEMICOLON
)
19564 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19565 range_for_decl_p
= true;
19568 if (!maybe_range_for_decl
)
19569 cp_parser_error (parser
, "expected initializer");
19570 return error_mark_node
;
19573 is_initialized
= SD_UNINITIALIZED
;
19574 initialization_kind
= CPP_EOF
;
19577 /* Because start_decl has side-effects, we should only call it if we
19578 know we're going ahead. By this point, we know that we cannot
19579 possibly be looking at any other construct. */
19580 cp_parser_commit_to_tentative_parse (parser
);
19582 /* Enter the newly declared entry in the symbol table. If we're
19583 processing a declaration in a class-specifier, we wait until
19584 after processing the initializer. */
19587 if (parser
->in_unbraced_linkage_specification_p
)
19588 decl_specifiers
->storage_class
= sc_extern
;
19589 decl
= start_decl (declarator
, decl_specifiers
,
19590 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19591 attributes
, prefix_attributes
, &pushed_scope
);
19592 cp_finalize_omp_declare_simd (parser
, decl
);
19593 cp_finalize_oacc_routine (parser
, decl
, false);
19594 /* Adjust location of decl if declarator->id_loc is more appropriate:
19595 set, and decl wasn't merged with another decl, in which case its
19596 location would be different from input_location, and more accurate. */
19598 && declarator
->id_loc
!= UNKNOWN_LOCATION
19599 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19600 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19603 /* Enter the SCOPE. That way unqualified names appearing in the
19604 initializer will be looked up in SCOPE. */
19605 pushed_scope
= push_scope (scope
);
19607 /* Perform deferred access control checks, now that we know in which
19608 SCOPE the declared entity resides. */
19609 if (!member_p
&& decl
)
19611 tree saved_current_function_decl
= NULL_TREE
;
19613 /* If the entity being declared is a function, pretend that we
19614 are in its scope. If it is a `friend', it may have access to
19615 things that would not otherwise be accessible. */
19616 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19618 saved_current_function_decl
= current_function_decl
;
19619 current_function_decl
= decl
;
19622 /* Perform access checks for template parameters. */
19623 cp_parser_perform_template_parameter_access_checks (checks
);
19625 /* Perform the access control checks for the declarator and the
19626 decl-specifiers. */
19627 perform_deferred_access_checks (tf_warning_or_error
);
19629 /* Restore the saved value. */
19630 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19631 current_function_decl
= saved_current_function_decl
;
19634 /* Parse the initializer. */
19635 initializer
= NULL_TREE
;
19636 is_direct_init
= false;
19637 is_non_constant_init
= true;
19638 if (is_initialized
)
19640 if (function_declarator_p (declarator
))
19642 if (initialization_kind
== CPP_EQ
)
19643 initializer
= cp_parser_pure_specifier (parser
);
19646 /* If the declaration was erroneous, we don't really
19647 know what the user intended, so just silently
19648 consume the initializer. */
19649 if (decl
!= error_mark_node
)
19650 error_at (tmp_init_loc
, "initializer provided for function");
19651 cp_parser_skip_to_closing_parenthesis (parser
,
19652 /*recovering=*/true,
19653 /*or_comma=*/false,
19654 /*consume_paren=*/true);
19659 /* We want to record the extra mangling scope for in-class
19660 initializers of class members and initializers of static data
19661 member templates. The former involves deferring
19662 parsing of the initializer until end of class as with default
19663 arguments. So right here we only handle the latter. */
19664 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19665 start_lambda_scope (decl
);
19666 initializer
= cp_parser_initializer (parser
,
19668 &is_non_constant_init
);
19669 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19670 finish_lambda_scope ();
19671 if (initializer
== error_mark_node
)
19672 cp_parser_skip_to_end_of_statement (parser
);
19676 /* The old parser allows attributes to appear after a parenthesized
19677 initializer. Mark Mitchell proposed removing this functionality
19678 on the GCC mailing lists on 2002-08-13. This parser accepts the
19679 attributes -- but ignores them. */
19680 if (cp_parser_allow_gnu_extensions_p (parser
)
19681 && initialization_kind
== CPP_OPEN_PAREN
)
19682 if (cp_parser_attributes_opt (parser
))
19683 warning (OPT_Wattributes
,
19684 "attributes after parenthesized initializer ignored");
19686 /* And now complain about a non-function implicit template. */
19687 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19688 error_at (DECL_SOURCE_LOCATION (decl
),
19689 "non-function %qD declared as implicit template", decl
);
19691 /* For an in-class declaration, use `grokfield' to create the
19697 pop_scope (pushed_scope
);
19698 pushed_scope
= NULL_TREE
;
19700 decl
= grokfield (declarator
, decl_specifiers
,
19701 initializer
, !is_non_constant_init
,
19702 /*asmspec=*/NULL_TREE
,
19703 attr_chainon (attributes
, prefix_attributes
));
19704 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19705 cp_parser_save_default_args (parser
, decl
);
19706 cp_finalize_omp_declare_simd (parser
, decl
);
19707 cp_finalize_oacc_routine (parser
, decl
, false);
19710 /* Finish processing the declaration. But, skip member
19712 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19714 cp_finish_decl (decl
,
19715 initializer
, !is_non_constant_init
,
19717 /* If the initializer is in parentheses, then this is
19718 a direct-initialization, which means that an
19719 `explicit' constructor is OK. Otherwise, an
19720 `explicit' constructor cannot be used. */
19721 ((is_direct_init
|| !is_initialized
)
19722 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19724 else if ((cxx_dialect
!= cxx98
) && friend_p
19725 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19726 /* Core issue #226 (C++0x only): A default template-argument
19727 shall not be specified in a friend class template
19729 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19730 /*is_partial=*/false, /*is_friend_decl=*/1);
19732 if (!friend_p
&& pushed_scope
)
19733 pop_scope (pushed_scope
);
19735 if (function_declarator_p (declarator
)
19736 && parser
->fully_implicit_function_template_p
)
19739 decl
= finish_fully_implicit_template (parser
, decl
);
19741 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19744 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19745 && type_uses_auto (decl_specifiers
->type
))
19746 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19751 /* Parse a declarator.
19755 ptr-operator declarator
19757 abstract-declarator:
19758 ptr-operator abstract-declarator [opt]
19759 direct-abstract-declarator
19764 attributes [opt] direct-declarator
19765 attributes [opt] ptr-operator declarator
19767 abstract-declarator:
19768 attributes [opt] ptr-operator abstract-declarator [opt]
19769 attributes [opt] direct-abstract-declarator
19771 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19772 detect constructors, destructors, deduction guides, or conversion operators.
19773 It is set to -1 if the declarator is a name, and +1 if it is a
19774 function. Otherwise it is set to zero. Usually you just want to
19775 test for >0, but internally the negative value is used.
19777 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19778 a decl-specifier-seq unless it declares a constructor, destructor,
19779 or conversion. It might seem that we could check this condition in
19780 semantic analysis, rather than parsing, but that makes it difficult
19781 to handle something like `f()'. We want to notice that there are
19782 no decl-specifiers, and therefore realize that this is an
19783 expression, not a declaration.)
19785 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19786 the declarator is a direct-declarator of the form "(...)".
19788 MEMBER_P is true iff this declarator is a member-declarator.
19790 FRIEND_P is true iff this declarator is a friend. */
19792 static cp_declarator
*
19793 cp_parser_declarator (cp_parser
* parser
,
19794 cp_parser_declarator_kind dcl_kind
,
19795 int* ctor_dtor_or_conv_p
,
19796 bool* parenthesized_p
,
19797 bool member_p
, bool friend_p
)
19799 cp_declarator
*declarator
;
19800 enum tree_code code
;
19801 cp_cv_quals cv_quals
;
19803 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19805 /* Assume this is not a constructor, destructor, or type-conversion
19807 if (ctor_dtor_or_conv_p
)
19808 *ctor_dtor_or_conv_p
= 0;
19810 if (cp_parser_allow_gnu_extensions_p (parser
))
19811 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19813 /* Check for the ptr-operator production. */
19814 cp_parser_parse_tentatively (parser
);
19815 /* Parse the ptr-operator. */
19816 code
= cp_parser_ptr_operator (parser
,
19821 /* If that worked, then we have a ptr-operator. */
19822 if (cp_parser_parse_definitely (parser
))
19824 /* If a ptr-operator was found, then this declarator was not
19826 if (parenthesized_p
)
19827 *parenthesized_p
= true;
19828 /* The dependent declarator is optional if we are parsing an
19829 abstract-declarator. */
19830 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19831 cp_parser_parse_tentatively (parser
);
19833 /* Parse the dependent declarator. */
19834 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19835 /*ctor_dtor_or_conv_p=*/NULL
,
19836 /*parenthesized_p=*/NULL
,
19837 /*member_p=*/false,
19840 /* If we are parsing an abstract-declarator, we must handle the
19841 case where the dependent declarator is absent. */
19842 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19843 && !cp_parser_parse_definitely (parser
))
19846 declarator
= cp_parser_make_indirect_declarator
19847 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19849 /* Everything else is a direct-declarator. */
19852 if (parenthesized_p
)
19853 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19855 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19856 ctor_dtor_or_conv_p
,
19857 member_p
, friend_p
);
19860 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19861 declarator
->attributes
= gnu_attributes
;
19865 /* Parse a direct-declarator or direct-abstract-declarator.
19869 direct-declarator ( parameter-declaration-clause )
19870 cv-qualifier-seq [opt]
19871 ref-qualifier [opt]
19872 exception-specification [opt]
19873 direct-declarator [ constant-expression [opt] ]
19876 direct-abstract-declarator:
19877 direct-abstract-declarator [opt]
19878 ( parameter-declaration-clause )
19879 cv-qualifier-seq [opt]
19880 ref-qualifier [opt]
19881 exception-specification [opt]
19882 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19883 ( abstract-declarator )
19885 Returns a representation of the declarator. DCL_KIND is
19886 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19887 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19888 we are parsing a direct-declarator. It is
19889 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19890 of ambiguity we prefer an abstract declarator, as per
19891 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19892 as for cp_parser_declarator. */
19894 static cp_declarator
*
19895 cp_parser_direct_declarator (cp_parser
* parser
,
19896 cp_parser_declarator_kind dcl_kind
,
19897 int* ctor_dtor_or_conv_p
,
19898 bool member_p
, bool friend_p
)
19901 cp_declarator
*declarator
= NULL
;
19902 tree scope
= NULL_TREE
;
19903 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19904 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19906 tree pushed_scope
= NULL_TREE
;
19907 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19911 /* Peek at the next token. */
19912 token
= cp_lexer_peek_token (parser
->lexer
);
19913 if (token
->type
== CPP_OPEN_PAREN
)
19915 /* This is either a parameter-declaration-clause, or a
19916 parenthesized declarator. When we know we are parsing a
19917 named declarator, it must be a parenthesized declarator
19918 if FIRST is true. For instance, `(int)' is a
19919 parameter-declaration-clause, with an omitted
19920 direct-abstract-declarator. But `((*))', is a
19921 parenthesized abstract declarator. Finally, when T is a
19922 template parameter `(T)' is a
19923 parameter-declaration-clause, and not a parenthesized
19926 We first try and parse a parameter-declaration-clause,
19927 and then try a nested declarator (if FIRST is true).
19929 It is not an error for it not to be a
19930 parameter-declaration-clause, even when FIRST is
19936 The first is the declaration of a function while the
19937 second is the definition of a variable, including its
19940 Having seen only the parenthesis, we cannot know which of
19941 these two alternatives should be selected. Even more
19942 complex are examples like:
19947 The former is a function-declaration; the latter is a
19948 variable initialization.
19950 Thus again, we try a parameter-declaration-clause, and if
19951 that fails, we back out and return. */
19953 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19956 bool is_declarator
= false;
19960 /* In a member-declarator, the only valid interpretation
19961 of a parenthesis is the start of a
19962 parameter-declaration-clause. (It is invalid to
19963 initialize a static data member with a parenthesized
19964 initializer; only the "=" form of initialization is
19967 cp_parser_parse_tentatively (parser
);
19969 /* Consume the `('. */
19970 matching_parens parens
;
19971 parens
.consume_open (parser
);
19974 /* If this is going to be an abstract declarator, we're
19975 in a declarator and we can't have default args. */
19976 parser
->default_arg_ok_p
= false;
19977 parser
->in_declarator_p
= true;
19980 begin_scope (sk_function_parms
, NULL_TREE
);
19982 /* Parse the parameter-declaration-clause. */
19983 params
= cp_parser_parameter_declaration_clause (parser
);
19985 /* Consume the `)'. */
19986 parens
.require_close (parser
);
19988 /* If all went well, parse the cv-qualifier-seq,
19989 ref-qualifier and the exception-specification. */
19990 if (member_p
|| cp_parser_parse_definitely (parser
))
19992 cp_cv_quals cv_quals
;
19993 cp_virt_specifiers virt_specifiers
;
19994 cp_ref_qualifier ref_qual
;
19995 tree exception_specification
;
19998 bool memfn
= (member_p
|| (pushed_scope
19999 && CLASS_TYPE_P (pushed_scope
)));
20001 is_declarator
= true;
20003 if (ctor_dtor_or_conv_p
)
20004 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20007 /* Parse the cv-qualifier-seq. */
20008 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20009 /* Parse the ref-qualifier. */
20010 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20011 /* Parse the tx-qualifier. */
20012 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20013 /* And the exception-specification. */
20014 exception_specification
20015 = cp_parser_exception_specification_opt (parser
);
20017 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20019 /* In here, we handle cases where attribute is used after
20020 the function declaration. For example:
20021 void func (int x) __attribute__((vector(..))); */
20022 tree gnu_attrs
= NULL_TREE
;
20023 tree requires_clause
= NULL_TREE
;
20024 late_return
= (cp_parser_late_return_type_opt
20025 (parser
, declarator
, requires_clause
,
20026 memfn
? cv_quals
: -1));
20028 /* Parse the virt-specifier-seq. */
20029 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20031 /* Create the function-declarator. */
20032 declarator
= make_call_declarator (declarator
,
20038 exception_specification
,
20041 declarator
->std_attributes
= attrs
;
20042 declarator
->attributes
= gnu_attrs
;
20043 /* Any subsequent parameter lists are to do with
20044 return type, so are not those of the declared
20046 parser
->default_arg_ok_p
= false;
20049 /* Remove the function parms from scope. */
20050 pop_bindings_and_leave_scope ();
20053 /* Repeat the main loop. */
20057 /* If this is the first, we can try a parenthesized
20061 bool saved_in_type_id_in_expr_p
;
20063 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20064 parser
->in_declarator_p
= saved_in_declarator_p
;
20066 open_paren
= token
;
20067 /* Consume the `('. */
20068 matching_parens parens
;
20069 parens
.consume_open (parser
);
20070 /* Parse the nested declarator. */
20071 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20072 parser
->in_type_id_in_expr_p
= true;
20074 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20075 /*parenthesized_p=*/NULL
,
20076 member_p
, friend_p
);
20077 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20079 /* Expect a `)'. */
20080 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20081 if (!parens
.require_close (parser
))
20082 declarator
= cp_error_declarator
;
20083 if (declarator
== cp_error_declarator
)
20086 goto handle_declarator
;
20088 /* Otherwise, we must be done. */
20092 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20093 && token
->type
== CPP_OPEN_SQUARE
20094 && !cp_next_tokens_can_be_attribute_p (parser
))
20096 /* Parse an array-declarator. */
20097 tree bounds
, attrs
;
20099 if (ctor_dtor_or_conv_p
)
20100 *ctor_dtor_or_conv_p
= 0;
20104 parser
->default_arg_ok_p
= false;
20105 parser
->in_declarator_p
= true;
20106 /* Consume the `['. */
20107 cp_lexer_consume_token (parser
->lexer
);
20108 /* Peek at the next token. */
20109 token
= cp_lexer_peek_token (parser
->lexer
);
20110 /* If the next token is `]', then there is no
20111 constant-expression. */
20112 if (token
->type
!= CPP_CLOSE_SQUARE
)
20114 bool non_constant_p
;
20116 = cp_parser_constant_expression (parser
,
20117 /*allow_non_constant=*/true,
20119 if (!non_constant_p
)
20121 else if (error_operand_p (bounds
))
20122 /* Already gave an error. */;
20123 else if (!parser
->in_function_body
20124 || current_binding_level
->kind
== sk_function_parms
)
20126 /* Normally, the array bound must be an integral constant
20127 expression. However, as an extension, we allow VLAs
20128 in function scopes as long as they aren't part of a
20129 parameter declaration. */
20130 cp_parser_error (parser
,
20131 "array bound is not an integer constant");
20132 bounds
= error_mark_node
;
20134 else if (processing_template_decl
20135 && !type_dependent_expression_p (bounds
))
20137 /* Remember this wasn't a constant-expression. */
20138 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20139 TREE_SIDE_EFFECTS (bounds
) = 1;
20143 bounds
= NULL_TREE
;
20144 /* Look for the closing `]'. */
20145 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20147 declarator
= cp_error_declarator
;
20151 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20152 declarator
= make_array_declarator (declarator
, bounds
);
20153 declarator
->std_attributes
= attrs
;
20155 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20158 tree qualifying_scope
;
20159 tree unqualified_name
;
20161 special_function_kind sfk
;
20163 bool pack_expansion_p
= false;
20164 cp_token
*declarator_id_start_token
;
20166 /* Parse a declarator-id */
20167 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20170 cp_parser_parse_tentatively (parser
);
20172 /* If we see an ellipsis, we should be looking at a
20174 if (token
->type
== CPP_ELLIPSIS
)
20176 /* Consume the `...' */
20177 cp_lexer_consume_token (parser
->lexer
);
20179 pack_expansion_p
= true;
20183 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20185 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20186 qualifying_scope
= parser
->scope
;
20191 if (!unqualified_name
&& pack_expansion_p
)
20193 /* Check whether an error occurred. */
20194 okay
= !cp_parser_error_occurred (parser
);
20196 /* We already consumed the ellipsis to mark a
20197 parameter pack, but we have no way to report it,
20198 so abort the tentative parse. We will be exiting
20199 immediately anyway. */
20200 cp_parser_abort_tentative_parse (parser
);
20203 okay
= cp_parser_parse_definitely (parser
);
20206 unqualified_name
= error_mark_node
;
20207 else if (unqualified_name
20208 && (qualifying_scope
20209 || (!identifier_p (unqualified_name
))))
20211 cp_parser_error (parser
, "expected unqualified-id");
20212 unqualified_name
= error_mark_node
;
20216 if (!unqualified_name
)
20218 if (unqualified_name
== error_mark_node
)
20220 declarator
= cp_error_declarator
;
20221 pack_expansion_p
= false;
20222 declarator
->parameter_pack_p
= false;
20226 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20228 if (qualifying_scope
&& at_namespace_scope_p ()
20229 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20231 /* In the declaration of a member of a template class
20232 outside of the class itself, the SCOPE will sometimes
20233 be a TYPENAME_TYPE. For example, given:
20235 template <typename T>
20236 int S<T>::R::i = 3;
20238 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20239 this context, we must resolve S<T>::R to an ordinary
20240 type, rather than a typename type.
20242 The reason we normally avoid resolving TYPENAME_TYPEs
20243 is that a specialization of `S' might render
20244 `S<T>::R' not a type. However, if `S' is
20245 specialized, then this `i' will not be used, so there
20246 is no harm in resolving the types here. */
20249 /* Resolve the TYPENAME_TYPE. */
20250 type
= resolve_typename_type (qualifying_scope
,
20251 /*only_current_p=*/false);
20252 /* If that failed, the declarator is invalid. */
20253 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20255 if (typedef_variant_p (type
))
20256 error_at (declarator_id_start_token
->location
,
20257 "cannot define member of dependent typedef "
20260 error_at (declarator_id_start_token
->location
,
20261 "%<%T::%E%> is not a type",
20262 TYPE_CONTEXT (qualifying_scope
),
20263 TYPE_IDENTIFIER (qualifying_scope
));
20265 qualifying_scope
= type
;
20270 if (unqualified_name
)
20274 if (qualifying_scope
20275 && CLASS_TYPE_P (qualifying_scope
))
20276 class_type
= qualifying_scope
;
20278 class_type
= current_class_type
;
20280 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20282 tree name_type
= TREE_TYPE (unqualified_name
);
20284 if (!class_type
|| !same_type_p (name_type
, class_type
))
20286 /* We do not attempt to print the declarator
20287 here because we do not have enough
20288 information about its original syntactic
20290 cp_parser_error (parser
, "invalid declarator");
20291 declarator
= cp_error_declarator
;
20294 else if (qualifying_scope
20295 && CLASSTYPE_USE_TEMPLATE (name_type
))
20297 error_at (declarator_id_start_token
->location
,
20298 "invalid use of constructor as a template");
20299 inform (declarator_id_start_token
->location
,
20300 "use %<%T::%D%> instead of %<%T::%D%> to "
20301 "name the constructor in a qualified name",
20303 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20304 class_type
, name_type
);
20305 declarator
= cp_error_declarator
;
20308 unqualified_name
= constructor_name (class_type
);
20313 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20314 sfk
= sfk_destructor
;
20315 else if (identifier_p (unqualified_name
)
20316 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20317 sfk
= sfk_conversion
;
20318 else if (/* There's no way to declare a constructor
20319 for an unnamed type, even if the type
20320 got a name for linkage purposes. */
20321 !TYPE_WAS_UNNAMED (class_type
)
20322 /* Handle correctly (c++/19200):
20336 friend void N::S();
20338 && (!friend_p
|| class_type
== qualifying_scope
)
20339 && constructor_name_p (unqualified_name
,
20341 sfk
= sfk_constructor
;
20342 else if (is_overloaded_fn (unqualified_name
)
20343 && DECL_CONSTRUCTOR_P (get_first_fn
20344 (unqualified_name
)))
20345 sfk
= sfk_constructor
;
20347 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20348 *ctor_dtor_or_conv_p
= -1;
20351 declarator
= make_id_declarator (qualifying_scope
,
20354 declarator
->std_attributes
= attrs
;
20355 declarator
->id_loc
= token
->location
;
20356 declarator
->parameter_pack_p
= pack_expansion_p
;
20358 if (pack_expansion_p
)
20359 maybe_warn_variadic_templates ();
20362 handle_declarator
:;
20363 scope
= get_scope_of_declarator (declarator
);
20366 /* Any names that appear after the declarator-id for a
20367 member are looked up in the containing scope. */
20368 if (at_function_scope_p ())
20370 /* But declarations with qualified-ids can't appear in a
20372 cp_parser_error (parser
, "qualified-id in declaration");
20373 declarator
= cp_error_declarator
;
20376 pushed_scope
= push_scope (scope
);
20378 parser
->in_declarator_p
= true;
20379 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20380 || (declarator
&& declarator
->kind
== cdk_id
))
20381 /* Default args are only allowed on function
20383 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20385 parser
->default_arg_ok_p
= false;
20394 /* For an abstract declarator, we might wind up with nothing at this
20395 point. That's an error; the declarator is not optional. */
20397 cp_parser_error (parser
, "expected declarator");
20398 else if (open_paren
)
20400 /* Record overly parenthesized declarator so we can give a
20401 diagnostic about confusing decl/expr disambiguation. */
20402 if (declarator
->kind
== cdk_array
)
20404 /* If the open and close parens are on different lines, this
20405 is probably a formatting thing, so ignore. */
20406 expanded_location open
= expand_location (open_paren
->location
);
20407 expanded_location close
= expand_location (close_paren
->location
);
20408 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20412 declarator
->parenthesized
= open_paren
->location
;
20415 /* If we entered a scope, we must exit it now. */
20417 pop_scope (pushed_scope
);
20419 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20420 parser
->in_declarator_p
= saved_in_declarator_p
;
20425 /* Parse a ptr-operator.
20428 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20429 * cv-qualifier-seq [opt]
20431 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20432 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20437 & cv-qualifier-seq [opt]
20439 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20440 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20441 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20442 filled in with the TYPE containing the member. *CV_QUALS is
20443 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20444 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20445 Note that the tree codes returned by this function have nothing
20446 to do with the types of trees that will be eventually be created
20447 to represent the pointer or reference type being parsed. They are
20448 just constants with suggestive names. */
20449 static enum tree_code
20450 cp_parser_ptr_operator (cp_parser
* parser
,
20452 cp_cv_quals
*cv_quals
,
20455 enum tree_code code
= ERROR_MARK
;
20457 tree attrs
= NULL_TREE
;
20459 /* Assume that it's not a pointer-to-member. */
20461 /* And that there are no cv-qualifiers. */
20462 *cv_quals
= TYPE_UNQUALIFIED
;
20464 /* Peek at the next token. */
20465 token
= cp_lexer_peek_token (parser
->lexer
);
20467 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20468 if (token
->type
== CPP_MULT
)
20469 code
= INDIRECT_REF
;
20470 else if (token
->type
== CPP_AND
)
20472 else if ((cxx_dialect
!= cxx98
) &&
20473 token
->type
== CPP_AND_AND
) /* C++0x only */
20474 code
= NON_LVALUE_EXPR
;
20476 if (code
!= ERROR_MARK
)
20478 /* Consume the `*', `&' or `&&'. */
20479 cp_lexer_consume_token (parser
->lexer
);
20481 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20482 `&', if we are allowing GNU extensions. (The only qualifier
20483 that can legally appear after `&' is `restrict', but that is
20484 enforced during semantic analysis. */
20485 if (code
== INDIRECT_REF
20486 || cp_parser_allow_gnu_extensions_p (parser
))
20487 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20489 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20490 if (attributes
!= NULL
)
20491 *attributes
= attrs
;
20495 /* Try the pointer-to-member case. */
20496 cp_parser_parse_tentatively (parser
);
20497 /* Look for the optional `::' operator. */
20498 cp_parser_global_scope_opt (parser
,
20499 /*current_scope_valid_p=*/false);
20500 /* Look for the nested-name specifier. */
20501 token
= cp_lexer_peek_token (parser
->lexer
);
20502 cp_parser_nested_name_specifier (parser
,
20503 /*typename_keyword_p=*/false,
20504 /*check_dependency_p=*/true,
20506 /*is_declaration=*/false);
20507 /* If we found it, and the next token is a `*', then we are
20508 indeed looking at a pointer-to-member operator. */
20509 if (!cp_parser_error_occurred (parser
)
20510 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20512 /* Indicate that the `*' operator was used. */
20513 code
= INDIRECT_REF
;
20515 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20516 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20517 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20518 error_at (token
->location
, "cannot form pointer to member of "
20519 "non-class %q#T", parser
->scope
);
20522 /* The type of which the member is a member is given by the
20524 *type
= parser
->scope
;
20525 /* The next name will not be qualified. */
20526 parser
->scope
= NULL_TREE
;
20527 parser
->qualifying_scope
= NULL_TREE
;
20528 parser
->object_scope
= NULL_TREE
;
20529 /* Look for optional c++11 attributes. */
20530 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20531 if (attributes
!= NULL
)
20532 *attributes
= attrs
;
20533 /* Look for the optional cv-qualifier-seq. */
20534 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20537 /* If that didn't work we don't have a ptr-operator. */
20538 if (!cp_parser_parse_definitely (parser
))
20539 cp_parser_error (parser
, "expected ptr-operator");
20545 /* Parse an (optional) cv-qualifier-seq.
20548 cv-qualifier cv-qualifier-seq [opt]
20559 Returns a bitmask representing the cv-qualifiers. */
20562 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20564 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20569 cp_cv_quals cv_qualifier
;
20571 /* Peek at the next token. */
20572 token
= cp_lexer_peek_token (parser
->lexer
);
20573 /* See if it's a cv-qualifier. */
20574 switch (token
->keyword
)
20577 cv_qualifier
= TYPE_QUAL_CONST
;
20581 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20585 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20589 cv_qualifier
= TYPE_UNQUALIFIED
;
20596 if (cv_quals
& cv_qualifier
)
20598 gcc_rich_location
richloc (token
->location
);
20599 richloc
.add_fixit_remove ();
20600 error_at (&richloc
, "duplicate cv-qualifier");
20601 cp_lexer_purge_token (parser
->lexer
);
20605 cp_lexer_consume_token (parser
->lexer
);
20606 cv_quals
|= cv_qualifier
;
20613 /* Parse an (optional) ref-qualifier
20619 Returns cp_ref_qualifier representing ref-qualifier. */
20621 static cp_ref_qualifier
20622 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20624 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20626 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20627 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20632 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20633 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20635 switch (token
->type
)
20638 curr_ref_qual
= REF_QUAL_LVALUE
;
20642 curr_ref_qual
= REF_QUAL_RVALUE
;
20646 curr_ref_qual
= REF_QUAL_NONE
;
20650 if (!curr_ref_qual
)
20654 error_at (token
->location
, "multiple ref-qualifiers");
20655 cp_lexer_purge_token (parser
->lexer
);
20659 ref_qual
= curr_ref_qual
;
20660 cp_lexer_consume_token (parser
->lexer
);
20667 /* Parse an optional tx-qualifier.
20671 transaction_safe_dynamic */
20674 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20676 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20677 if (token
->type
== CPP_NAME
)
20679 tree name
= token
->u
.value
;
20680 const char *p
= IDENTIFIER_POINTER (name
);
20681 const int len
= strlen ("transaction_safe");
20682 if (!strncmp (p
, "transaction_safe", len
))
20686 || !strcmp (p
, "_dynamic"))
20688 cp_lexer_consume_token (parser
->lexer
);
20691 error ("%qE requires %<-fgnu-tm%>", name
);
20702 /* Parse an (optional) virt-specifier-seq.
20704 virt-specifier-seq:
20705 virt-specifier virt-specifier-seq [opt]
20711 Returns a bitmask representing the virt-specifiers. */
20713 static cp_virt_specifiers
20714 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20716 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20721 cp_virt_specifiers virt_specifier
;
20723 /* Peek at the next token. */
20724 token
= cp_lexer_peek_token (parser
->lexer
);
20725 /* See if it's a virt-specifier-qualifier. */
20726 if (token
->type
!= CPP_NAME
)
20728 if (id_equal (token
->u
.value
, "override"))
20730 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20731 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20733 else if (id_equal (token
->u
.value
, "final"))
20735 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20736 virt_specifier
= VIRT_SPEC_FINAL
;
20738 else if (id_equal (token
->u
.value
, "__final"))
20740 virt_specifier
= VIRT_SPEC_FINAL
;
20745 if (virt_specifiers
& virt_specifier
)
20747 gcc_rich_location
richloc (token
->location
);
20748 richloc
.add_fixit_remove ();
20749 error_at (&richloc
, "duplicate virt-specifier");
20750 cp_lexer_purge_token (parser
->lexer
);
20754 cp_lexer_consume_token (parser
->lexer
);
20755 virt_specifiers
|= virt_specifier
;
20758 return virt_specifiers
;
20761 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20762 is in scope even though it isn't real. */
20765 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20769 if (current_class_ptr
)
20771 /* We don't clear this between NSDMIs. Is it already what we want? */
20772 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20773 if (DECL_P (current_class_ptr
)
20774 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20775 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20776 && cp_type_quals (type
) == quals
)
20780 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20781 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20782 current_class_ptr
= NULL_TREE
;
20784 = cp_build_fold_indirect_ref (this_parm
);
20785 current_class_ptr
= this_parm
;
20788 /* Return true iff our current scope is a non-static data member
20792 parsing_nsdmi (void)
20794 /* We recognize NSDMI context by the context-less 'this' pointer set up
20795 by the function above. */
20796 if (current_class_ptr
20797 && TREE_CODE (current_class_ptr
) == PARM_DECL
20798 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20803 /* Parse a late-specified return type, if any. This is not a separate
20804 non-terminal, but part of a function declarator, which looks like
20806 -> trailing-type-specifier-seq abstract-declarator(opt)
20808 Returns the type indicated by the type-id.
20810 In addition to this, parse any queued up #pragma omp declare simd
20811 clauses, and #pragma acc routine clauses.
20813 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20817 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20818 tree
& requires_clause
, cp_cv_quals quals
)
20821 tree type
= NULL_TREE
;
20822 bool declare_simd_p
= (parser
->omp_declare_simd
20824 && declarator
->kind
== cdk_id
);
20826 bool oacc_routine_p
= (parser
->oacc_routine
20828 && declarator
->kind
== cdk_id
);
20830 /* Peek at the next token. */
20831 token
= cp_lexer_peek_token (parser
->lexer
);
20832 /* A late-specified return type is indicated by an initial '->'. */
20833 if (token
->type
!= CPP_DEREF
20834 && token
->keyword
!= RID_REQUIRES
20835 && !(token
->type
== CPP_NAME
20836 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20837 && !(declare_simd_p
|| oacc_routine_p
))
20840 tree save_ccp
= current_class_ptr
;
20841 tree save_ccr
= current_class_ref
;
20844 /* DR 1207: 'this' is in scope in the trailing return type. */
20845 inject_this_parameter (current_class_type
, quals
);
20848 if (token
->type
== CPP_DEREF
)
20850 /* Consume the ->. */
20851 cp_lexer_consume_token (parser
->lexer
);
20853 type
= cp_parser_trailing_type_id (parser
);
20856 /* Function declarations may be followed by a trailing
20857 requires-clause. */
20858 requires_clause
= cp_parser_requires_clause_opt (parser
);
20860 if (declare_simd_p
)
20861 declarator
->attributes
20862 = cp_parser_late_parsing_omp_declare_simd (parser
,
20863 declarator
->attributes
);
20864 if (oacc_routine_p
)
20865 declarator
->attributes
20866 = cp_parser_late_parsing_oacc_routine (parser
,
20867 declarator
->attributes
);
20871 current_class_ptr
= save_ccp
;
20872 current_class_ref
= save_ccr
;
20878 /* Parse a declarator-id.
20882 :: [opt] nested-name-specifier [opt] type-name
20884 In the `id-expression' case, the value returned is as for
20885 cp_parser_id_expression if the id-expression was an unqualified-id.
20886 If the id-expression was a qualified-id, then a SCOPE_REF is
20887 returned. The first operand is the scope (either a NAMESPACE_DECL
20888 or TREE_TYPE), but the second is still just a representation of an
20892 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20895 /* The expression must be an id-expression. Assume that qualified
20896 names are the names of types so that:
20899 int S<T>::R::i = 3;
20901 will work; we must treat `S<T>::R' as the name of a type.
20902 Similarly, assume that qualified names are templates, where
20906 int S<T>::R<T>::i = 3;
20909 id
= cp_parser_id_expression (parser
,
20910 /*template_keyword_p=*/false,
20911 /*check_dependency_p=*/false,
20912 /*template_p=*/NULL
,
20913 /*declarator_p=*/true,
20915 if (id
&& BASELINK_P (id
))
20916 id
= BASELINK_FUNCTIONS (id
);
20920 /* Parse a type-id.
20923 type-specifier-seq abstract-declarator [opt]
20925 Returns the TYPE specified. */
20928 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20929 bool is_trailing_return
)
20931 cp_decl_specifier_seq type_specifier_seq
;
20932 cp_declarator
*abstract_declarator
;
20934 /* Parse the type-specifier-seq. */
20935 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20936 is_trailing_return
,
20937 &type_specifier_seq
);
20938 if (is_template_arg
&& type_specifier_seq
.type
20939 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20940 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20941 /* A bare template name as a template argument is a template template
20942 argument, not a placeholder, so fail parsing it as a type argument. */
20944 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20945 cp_parser_simulate_error (parser
);
20946 return error_mark_node
;
20948 if (type_specifier_seq
.type
== error_mark_node
)
20949 return error_mark_node
;
20951 /* There might or might not be an abstract declarator. */
20952 cp_parser_parse_tentatively (parser
);
20953 /* Look for the declarator. */
20954 abstract_declarator
20955 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20956 /*parenthesized_p=*/NULL
,
20957 /*member_p=*/false,
20958 /*friend_p=*/false);
20959 /* Check to see if there really was a declarator. */
20960 if (!cp_parser_parse_definitely (parser
))
20961 abstract_declarator
= NULL
;
20963 if (type_specifier_seq
.type
20964 /* The concepts TS allows 'auto' as a type-id. */
20965 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20966 /* None of the valid uses of 'auto' in C++14 involve the type-id
20967 nonterminal, but it is valid in a trailing-return-type. */
20968 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20969 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20971 /* A type-id with type 'auto' is only ok if the abstract declarator
20972 is a function declarator with a late-specified return type.
20974 A type-id with 'auto' is also valid in a trailing-return-type
20975 in a compound-requirement. */
20976 if (abstract_declarator
20977 && abstract_declarator
->kind
== cdk_function
20978 && abstract_declarator
->u
.function
.late_return_type
)
20980 else if (parser
->in_result_type_constraint_p
)
20984 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20985 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20987 error_at (loc
, "missing template arguments after %qT",
20989 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
20993 error_at (loc
, "invalid use of %qT", auto_node
);
20994 return error_mark_node
;
20998 return groktypename (&type_specifier_seq
, abstract_declarator
,
21003 cp_parser_type_id (cp_parser
*parser
)
21005 return cp_parser_type_id_1 (parser
, false, false);
21009 cp_parser_template_type_arg (cp_parser
*parser
)
21012 const char *saved_message
= parser
->type_definition_forbidden_message
;
21013 parser
->type_definition_forbidden_message
21014 = G_("types may not be defined in template arguments");
21015 r
= cp_parser_type_id_1 (parser
, true, false);
21016 parser
->type_definition_forbidden_message
= saved_message
;
21017 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21019 error ("invalid use of %<auto%> in template argument");
21020 r
= error_mark_node
;
21026 cp_parser_trailing_type_id (cp_parser
*parser
)
21028 return cp_parser_type_id_1 (parser
, false, true);
21031 /* Parse a type-specifier-seq.
21033 type-specifier-seq:
21034 type-specifier type-specifier-seq [opt]
21038 type-specifier-seq:
21039 attributes type-specifier-seq [opt]
21041 If IS_DECLARATION is true, we are at the start of a "condition" or
21042 exception-declaration, so we might be followed by a declarator-id.
21044 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21045 i.e. we've just seen "->".
21047 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21050 cp_parser_type_specifier_seq (cp_parser
* parser
,
21051 bool is_declaration
,
21052 bool is_trailing_return
,
21053 cp_decl_specifier_seq
*type_specifier_seq
)
21055 bool seen_type_specifier
= false;
21056 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21057 cp_token
*start_token
= NULL
;
21059 /* Clear the TYPE_SPECIFIER_SEQ. */
21060 clear_decl_specs (type_specifier_seq
);
21062 /* In the context of a trailing return type, enum E { } is an
21063 elaborated-type-specifier followed by a function-body, not an
21065 if (is_trailing_return
)
21066 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21068 /* Parse the type-specifiers and attributes. */
21071 tree type_specifier
;
21072 bool is_cv_qualifier
;
21074 /* Check for attributes first. */
21075 if (cp_next_tokens_can_be_attribute_p (parser
))
21077 type_specifier_seq
->attributes
21078 = attr_chainon (type_specifier_seq
->attributes
,
21079 cp_parser_attributes_opt (parser
));
21083 /* record the token of the beginning of the type specifier seq,
21084 for error reporting purposes*/
21086 start_token
= cp_lexer_peek_token (parser
->lexer
);
21088 /* Look for the type-specifier. */
21089 type_specifier
= cp_parser_type_specifier (parser
,
21091 type_specifier_seq
,
21092 /*is_declaration=*/false,
21095 if (!type_specifier
)
21097 /* If the first type-specifier could not be found, this is not a
21098 type-specifier-seq at all. */
21099 if (!seen_type_specifier
)
21101 /* Set in_declarator_p to avoid skipping to the semicolon. */
21102 int in_decl
= parser
->in_declarator_p
;
21103 parser
->in_declarator_p
= true;
21105 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21106 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21107 cp_parser_error (parser
, "expected type-specifier");
21109 parser
->in_declarator_p
= in_decl
;
21111 type_specifier_seq
->type
= error_mark_node
;
21114 /* If subsequent type-specifiers could not be found, the
21115 type-specifier-seq is complete. */
21119 seen_type_specifier
= true;
21120 /* The standard says that a condition can be:
21122 type-specifier-seq declarator = assignment-expression
21129 we should treat the "S" as a declarator, not as a
21130 type-specifier. The standard doesn't say that explicitly for
21131 type-specifier-seq, but it does say that for
21132 decl-specifier-seq in an ordinary declaration. Perhaps it
21133 would be clearer just to allow a decl-specifier-seq here, and
21134 then add a semantic restriction that if any decl-specifiers
21135 that are not type-specifiers appear, the program is invalid. */
21136 if (is_declaration
&& !is_cv_qualifier
)
21137 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21141 /* Return whether the function currently being declared has an associated
21142 template parameter list. */
21145 function_being_declared_is_template_p (cp_parser
* parser
)
21147 if (!current_template_parms
|| processing_template_parmlist
)
21150 if (parser
->implicit_template_scope
)
21153 if (at_class_scope_p ()
21154 && TYPE_BEING_DEFINED (current_class_type
))
21155 return parser
->num_template_parameter_lists
!= 0;
21157 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21158 (current_class_type
));
21161 /* Parse a parameter-declaration-clause.
21163 parameter-declaration-clause:
21164 parameter-declaration-list [opt] ... [opt]
21165 parameter-declaration-list , ...
21167 Returns a representation for the parameter declarations. A return
21168 value of NULL indicates a parameter-declaration-clause consisting
21169 only of an ellipsis. */
21172 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21181 int auto_is_implicit_function_template_parm_p
;
21183 parser
->auto_is_implicit_function_template_parm_p
21184 = auto_is_implicit_function_template_parm_p
;
21186 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
21190 if (!processing_specialization
21191 && !processing_template_parmlist
21192 && !processing_explicit_instantiation
)
21193 if (!current_function_decl
21194 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21195 parser
->auto_is_implicit_function_template_parm_p
= true;
21197 /* Peek at the next token. */
21198 token
= cp_lexer_peek_token (parser
->lexer
);
21199 /* Check for trivial parameter-declaration-clauses. */
21200 if (token
->type
== CPP_ELLIPSIS
)
21202 /* Consume the `...' token. */
21203 cp_lexer_consume_token (parser
->lexer
);
21206 else if (token
->type
== CPP_CLOSE_PAREN
)
21207 /* There are no parameters. */
21209 #ifndef NO_IMPLICIT_EXTERN_C
21210 if (in_system_header_at (input_location
)
21211 && current_class_type
== NULL
21212 && current_lang_name
== lang_name_c
)
21216 return void_list_node
;
21218 /* Check for `(void)', too, which is a special case. */
21219 else if (token
->keyword
== RID_VOID
21220 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21221 == CPP_CLOSE_PAREN
))
21223 /* Consume the `void' token. */
21224 cp_lexer_consume_token (parser
->lexer
);
21225 /* There are no parameters. */
21226 return void_list_node
;
21229 /* Parse the parameter-declaration-list. */
21230 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21231 /* If a parse error occurred while parsing the
21232 parameter-declaration-list, then the entire
21233 parameter-declaration-clause is erroneous. */
21237 /* Peek at the next token. */
21238 token
= cp_lexer_peek_token (parser
->lexer
);
21239 /* If it's a `,', the clause should terminate with an ellipsis. */
21240 if (token
->type
== CPP_COMMA
)
21242 /* Consume the `,'. */
21243 cp_lexer_consume_token (parser
->lexer
);
21244 /* Expect an ellipsis. */
21246 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21248 /* It might also be `...' if the optional trailing `,' was
21250 else if (token
->type
== CPP_ELLIPSIS
)
21252 /* Consume the `...' token. */
21253 cp_lexer_consume_token (parser
->lexer
);
21254 /* And remember that we saw it. */
21258 ellipsis_p
= false;
21260 /* Finish the parameter list. */
21262 parameters
= chainon (parameters
, void_list_node
);
21267 /* Parse a parameter-declaration-list.
21269 parameter-declaration-list:
21270 parameter-declaration
21271 parameter-declaration-list , parameter-declaration
21273 Returns a representation of the parameter-declaration-list, as for
21274 cp_parser_parameter_declaration_clause. However, the
21275 `void_list_node' is never appended to the list. Upon return,
21276 *IS_ERROR will be true iff an error occurred. */
21279 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21281 tree parameters
= NULL_TREE
;
21282 tree
*tail
= ¶meters
;
21283 bool saved_in_unbraced_linkage_specification_p
;
21286 /* Assume all will go well. */
21288 /* The special considerations that apply to a function within an
21289 unbraced linkage specifications do not apply to the parameters
21290 to the function. */
21291 saved_in_unbraced_linkage_specification_p
21292 = parser
->in_unbraced_linkage_specification_p
;
21293 parser
->in_unbraced_linkage_specification_p
= false;
21295 /* Look for more parameters. */
21298 cp_parameter_declarator
*parameter
;
21299 tree decl
= error_mark_node
;
21300 bool parenthesized_p
= false;
21301 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21302 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21303 (current_template_parms
)) : 0);
21305 /* Parse the parameter. */
21307 = cp_parser_parameter_declaration (parser
,
21308 /*template_parm_p=*/false,
21311 /* We don't know yet if the enclosing context is deprecated, so wait
21312 and warn in grokparms if appropriate. */
21313 deprecated_state
= DEPRECATED_SUPPRESS
;
21317 /* If a function parameter pack was specified and an implicit template
21318 parameter was introduced during cp_parser_parameter_declaration,
21319 change any implicit parameters introduced into packs. */
21320 if (parser
->implicit_template_parms
21321 && parameter
->declarator
21322 && parameter
->declarator
->parameter_pack_p
)
21324 int latest_template_parm_idx
= TREE_VEC_LENGTH
21325 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21327 if (latest_template_parm_idx
!= template_parm_idx
)
21328 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21329 (parameter
->decl_specifiers
.type
,
21330 template_parm_idx
, latest_template_parm_idx
);
21333 decl
= grokdeclarator (parameter
->declarator
,
21334 ¶meter
->decl_specifiers
,
21336 parameter
->default_argument
!= NULL_TREE
,
21337 ¶meter
->decl_specifiers
.attributes
);
21338 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21339 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21342 deprecated_state
= DEPRECATED_NORMAL
;
21344 /* If a parse error occurred parsing the parameter declaration,
21345 then the entire parameter-declaration-list is erroneous. */
21346 if (decl
== error_mark_node
)
21349 parameters
= error_mark_node
;
21353 if (parameter
->decl_specifiers
.attributes
)
21354 cplus_decl_attributes (&decl
,
21355 parameter
->decl_specifiers
.attributes
,
21357 if (DECL_NAME (decl
))
21358 decl
= pushdecl (decl
);
21360 if (decl
!= error_mark_node
)
21362 retrofit_lang_decl (decl
);
21363 DECL_PARM_INDEX (decl
) = ++index
;
21364 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21367 /* Add the new parameter to the list. */
21368 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21369 tail
= &TREE_CHAIN (*tail
);
21371 /* Peek at the next token. */
21372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21373 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21374 /* These are for Objective-C++ */
21375 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21376 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21377 /* The parameter-declaration-list is complete. */
21379 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21383 /* Peek at the next token. */
21384 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21385 /* If it's an ellipsis, then the list is complete. */
21386 if (token
->type
== CPP_ELLIPSIS
)
21388 /* Otherwise, there must be more parameters. Consume the
21390 cp_lexer_consume_token (parser
->lexer
);
21391 /* When parsing something like:
21393 int i(float f, double d)
21395 we can tell after seeing the declaration for "f" that we
21396 are not looking at an initialization of a variable "i",
21397 but rather at the declaration of a function "i".
21399 Due to the fact that the parsing of template arguments
21400 (as specified to a template-id) requires backtracking we
21401 cannot use this technique when inside a template argument
21403 if (!parser
->in_template_argument_list_p
21404 && !parser
->in_type_id_in_expr_p
21405 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21406 /* However, a parameter-declaration of the form
21407 "float(f)" (which is a valid declaration of a
21408 parameter "f") can also be interpreted as an
21409 expression (the conversion of "f" to "float"). */
21410 && !parenthesized_p
)
21411 cp_parser_commit_to_tentative_parse (parser
);
21415 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21416 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21417 cp_parser_skip_to_closing_parenthesis (parser
,
21418 /*recovering=*/true,
21419 /*or_comma=*/false,
21420 /*consume_paren=*/false);
21425 parser
->in_unbraced_linkage_specification_p
21426 = saved_in_unbraced_linkage_specification_p
;
21428 /* Reset implicit_template_scope if we are about to leave the function
21429 parameter list that introduced it. Note that for out-of-line member
21430 definitions, there will be one or more class scopes before we get to
21431 the template parameter scope. */
21433 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21434 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21436 while (maybe_its
->kind
== sk_class
)
21437 maybe_its
= maybe_its
->level_chain
;
21438 if (maybe_its
== its
)
21440 parser
->implicit_template_parms
= 0;
21441 parser
->implicit_template_scope
= 0;
21448 /* Parse a parameter declaration.
21450 parameter-declaration:
21451 decl-specifier-seq ... [opt] declarator
21452 decl-specifier-seq declarator = assignment-expression
21453 decl-specifier-seq ... [opt] abstract-declarator [opt]
21454 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21456 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21457 declares a template parameter. (In that case, a non-nested `>'
21458 token encountered during the parsing of the assignment-expression
21459 is not interpreted as a greater-than operator.)
21461 Returns a representation of the parameter, or NULL if an error
21462 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21463 true iff the declarator is of the form "(p)". */
21465 static cp_parameter_declarator
*
21466 cp_parser_parameter_declaration (cp_parser
*parser
,
21467 bool template_parm_p
,
21468 bool *parenthesized_p
)
21470 int declares_class_or_enum
;
21471 cp_decl_specifier_seq decl_specifiers
;
21472 cp_declarator
*declarator
;
21473 tree default_argument
;
21474 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21475 const char *saved_message
;
21476 bool template_parameter_pack_p
= false;
21478 /* In a template parameter, `>' is not an operator.
21482 When parsing a default template-argument for a non-type
21483 template-parameter, the first non-nested `>' is taken as the end
21484 of the template parameter-list rather than a greater-than
21487 /* Type definitions may not appear in parameter types. */
21488 saved_message
= parser
->type_definition_forbidden_message
;
21489 parser
->type_definition_forbidden_message
21490 = G_("types may not be defined in parameter types");
21492 /* Parse the declaration-specifiers. */
21493 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21494 cp_parser_decl_specifier_seq (parser
,
21495 CP_PARSER_FLAGS_NONE
,
21497 &declares_class_or_enum
);
21499 /* Complain about missing 'typename' or other invalid type names. */
21500 if (!decl_specifiers
.any_type_specifiers_p
21501 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21502 decl_specifiers
.type
= error_mark_node
;
21504 /* If an error occurred, there's no reason to attempt to parse the
21505 rest of the declaration. */
21506 if (cp_parser_error_occurred (parser
))
21508 parser
->type_definition_forbidden_message
= saved_message
;
21512 /* Peek at the next token. */
21513 token
= cp_lexer_peek_token (parser
->lexer
);
21515 /* If the next token is a `)', `,', `=', `>', or `...', then there
21516 is no declarator. However, when variadic templates are enabled,
21517 there may be a declarator following `...'. */
21518 if (token
->type
== CPP_CLOSE_PAREN
21519 || token
->type
== CPP_COMMA
21520 || token
->type
== CPP_EQ
21521 || token
->type
== CPP_GREATER
)
21524 if (parenthesized_p
)
21525 *parenthesized_p
= false;
21527 /* Otherwise, there should be a declarator. */
21530 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21531 parser
->default_arg_ok_p
= false;
21533 /* After seeing a decl-specifier-seq, if the next token is not a
21534 "(", there is no possibility that the code is a valid
21535 expression. Therefore, if parsing tentatively, we commit at
21537 if (!parser
->in_template_argument_list_p
21538 /* In an expression context, having seen:
21542 we cannot be sure whether we are looking at a
21543 function-type (taking a "char" as a parameter) or a cast
21544 of some object of type "char" to "int". */
21545 && !parser
->in_type_id_in_expr_p
21546 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21547 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21548 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21549 cp_parser_commit_to_tentative_parse (parser
);
21550 /* Parse the declarator. */
21551 declarator_token_start
= token
;
21552 declarator
= cp_parser_declarator (parser
,
21553 CP_PARSER_DECLARATOR_EITHER
,
21554 /*ctor_dtor_or_conv_p=*/NULL
,
21556 /*member_p=*/false,
21557 /*friend_p=*/false);
21558 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21559 /* After the declarator, allow more attributes. */
21560 decl_specifiers
.attributes
21561 = attr_chainon (decl_specifiers
.attributes
,
21562 cp_parser_attributes_opt (parser
));
21564 /* If the declarator is a template parameter pack, remember that and
21565 clear the flag in the declarator itself so we don't get errors
21566 from grokdeclarator. */
21567 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21569 declarator
->parameter_pack_p
= false;
21570 template_parameter_pack_p
= true;
21574 /* If the next token is an ellipsis, and we have not seen a declarator
21575 name, and if either the type of the declarator contains parameter
21576 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21577 for, eg, abbreviated integral type names), then we actually have a
21578 parameter pack expansion expression. Otherwise, leave the ellipsis
21579 for a C-style variadic function. */
21580 token
= cp_lexer_peek_token (parser
->lexer
);
21581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21583 tree type
= decl_specifiers
.type
;
21585 if (type
&& DECL_P (type
))
21586 type
= TREE_TYPE (type
);
21589 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21590 && (template_parm_p
|| uses_parameter_packs (type
)))
21591 || (!type
&& template_parm_p
))
21592 && declarator_can_be_parameter_pack (declarator
))
21594 /* Consume the `...'. */
21595 cp_lexer_consume_token (parser
->lexer
);
21596 maybe_warn_variadic_templates ();
21598 /* Build a pack expansion type */
21599 if (template_parm_p
)
21600 template_parameter_pack_p
= true;
21601 else if (declarator
)
21602 declarator
->parameter_pack_p
= true;
21604 decl_specifiers
.type
= make_pack_expansion (type
);
21608 /* The restriction on defining new types applies only to the type
21609 of the parameter, not to the default argument. */
21610 parser
->type_definition_forbidden_message
= saved_message
;
21612 /* If the next token is `=', then process a default argument. */
21613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21615 tree type
= decl_specifiers
.type
;
21616 token
= cp_lexer_peek_token (parser
->lexer
);
21617 /* If we are defining a class, then the tokens that make up the
21618 default argument must be saved and processed later. */
21619 if (!template_parm_p
&& at_class_scope_p ()
21620 && TYPE_BEING_DEFINED (current_class_type
)
21621 && !LAMBDA_TYPE_P (current_class_type
))
21622 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21624 // A constrained-type-specifier may declare a type template-parameter.
21625 else if (declares_constrained_type_template_parameter (type
))
21627 = cp_parser_default_type_template_argument (parser
);
21629 // A constrained-type-specifier may declare a template-template-parameter.
21630 else if (declares_constrained_template_template_parameter (type
))
21632 = cp_parser_default_template_template_argument (parser
);
21634 /* Outside of a class definition, we can just parse the
21635 assignment-expression. */
21638 = cp_parser_default_argument (parser
, template_parm_p
);
21640 if (!parser
->default_arg_ok_p
)
21642 permerror (token
->location
,
21643 "default arguments are only "
21644 "permitted for function parameters");
21646 else if ((declarator
&& declarator
->parameter_pack_p
)
21647 || template_parameter_pack_p
21648 || (decl_specifiers
.type
21649 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21651 /* Find the name of the parameter pack. */
21652 cp_declarator
*id_declarator
= declarator
;
21653 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21654 id_declarator
= id_declarator
->declarator
;
21656 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21657 error_at (declarator_token_start
->location
,
21659 ? G_("template parameter pack %qD "
21660 "cannot have a default argument")
21661 : G_("parameter pack %qD cannot have "
21662 "a default argument"),
21663 id_declarator
->u
.id
.unqualified_name
);
21665 error_at (declarator_token_start
->location
,
21667 ? G_("template parameter pack cannot have "
21668 "a default argument")
21669 : G_("parameter pack cannot have a "
21670 "default argument"));
21672 default_argument
= NULL_TREE
;
21676 default_argument
= NULL_TREE
;
21678 /* Generate a location for the parameter, ranging from the start of the
21679 initial token to the end of the final token (using input_location for
21680 the latter, set up by cp_lexer_set_source_position_from_token when
21683 If we have a identifier, then use it for the caret location, e.g.
21685 extern int callee (int one, int (*two)(int, int), float three);
21686 ~~~~~~^~~~~~~~~~~~~~
21688 otherwise, reuse the start location for the caret location e.g.:
21690 extern int callee (int one, int (*)(int, int), float three);
21694 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21695 ? declarator
->id_loc
21696 : decl_spec_token_start
->location
);
21697 location_t param_loc
= make_location (caret_loc
,
21698 decl_spec_token_start
->location
,
21701 return make_parameter_declarator (&decl_specifiers
,
21705 template_parameter_pack_p
);
21708 /* Parse a default argument and return it.
21710 TEMPLATE_PARM_P is true if this is a default argument for a
21711 non-type template parameter. */
21713 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21715 tree default_argument
= NULL_TREE
;
21716 bool saved_greater_than_is_operator_p
;
21717 bool saved_local_variables_forbidden_p
;
21718 bool non_constant_p
, is_direct_init
;
21720 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21722 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21723 parser
->greater_than_is_operator_p
= !template_parm_p
;
21724 /* Local variable names (and the `this' keyword) may not
21725 appear in a default argument. */
21726 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21727 parser
->local_variables_forbidden_p
= true;
21728 /* Parse the assignment-expression. */
21729 if (template_parm_p
)
21730 push_deferring_access_checks (dk_no_deferred
);
21731 tree saved_class_ptr
= NULL_TREE
;
21732 tree saved_class_ref
= NULL_TREE
;
21733 /* The "this" pointer is not valid in a default argument. */
21736 saved_class_ptr
= current_class_ptr
;
21737 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21738 saved_class_ref
= current_class_ref
;
21739 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21742 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21743 /* Restore the "this" pointer. */
21746 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21747 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21749 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21750 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21751 if (template_parm_p
)
21752 pop_deferring_access_checks ();
21753 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21754 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21756 return default_argument
;
21759 /* Parse a function-body.
21762 compound_statement */
21765 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21767 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21768 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21772 /* Parse a ctor-initializer-opt followed by a function-body. Return
21773 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21774 is true we are parsing a function-try-block. */
21777 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21778 bool in_function_try_block
)
21781 const bool check_body_p
=
21782 DECL_CONSTRUCTOR_P (current_function_decl
)
21783 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21786 /* Begin the function body. */
21787 body
= begin_function_body ();
21788 /* Parse the optional ctor-initializer. */
21789 cp_parser_ctor_initializer_opt (parser
);
21791 /* If we're parsing a constexpr constructor definition, we need
21792 to check that the constructor body is indeed empty. However,
21793 before we get to cp_parser_function_body lot of junk has been
21794 generated, so we can't just check that we have an empty block.
21795 Rather we take a snapshot of the outermost block, and check whether
21796 cp_parser_function_body changed its state. */
21799 list
= cur_stmt_list
;
21800 if (STATEMENT_LIST_TAIL (list
))
21801 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21803 /* Parse the function-body. */
21804 cp_parser_function_body (parser
, in_function_try_block
);
21806 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21807 /* Finish the function body. */
21808 finish_function_body (body
);
21811 /* Parse an initializer.
21814 = initializer-clause
21815 ( expression-list )
21817 Returns an expression representing the initializer. If no
21818 initializer is present, NULL_TREE is returned.
21820 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21821 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21822 set to TRUE if there is no initializer present. If there is an
21823 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21824 is set to true; otherwise it is set to false. */
21827 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21828 bool* non_constant_p
)
21833 /* Peek at the next token. */
21834 token
= cp_lexer_peek_token (parser
->lexer
);
21836 /* Let our caller know whether or not this initializer was
21838 *is_direct_init
= (token
->type
!= CPP_EQ
);
21839 /* Assume that the initializer is constant. */
21840 *non_constant_p
= false;
21842 if (token
->type
== CPP_EQ
)
21844 /* Consume the `='. */
21845 cp_lexer_consume_token (parser
->lexer
);
21846 /* Parse the initializer-clause. */
21847 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21849 else if (token
->type
== CPP_OPEN_PAREN
)
21851 vec
<tree
, va_gc
> *vec
;
21852 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21854 /*allow_expansion_p=*/true,
21857 return error_mark_node
;
21858 init
= build_tree_list_vec (vec
);
21859 release_tree_vector (vec
);
21861 else if (token
->type
== CPP_OPEN_BRACE
)
21863 cp_lexer_set_source_position (parser
->lexer
);
21864 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21865 init
= cp_parser_braced_list (parser
, non_constant_p
);
21866 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21870 /* Anything else is an error. */
21871 cp_parser_error (parser
, "expected initializer");
21872 init
= error_mark_node
;
21875 if (check_for_bare_parameter_packs (init
))
21876 init
= error_mark_node
;
21881 /* Parse an initializer-clause.
21883 initializer-clause:
21884 assignment-expression
21887 Returns an expression representing the initializer.
21889 If the `assignment-expression' production is used the value
21890 returned is simply a representation for the expression.
21892 Otherwise, calls cp_parser_braced_list. */
21895 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21897 cp_expr initializer
;
21899 /* Assume the expression is constant. */
21900 *non_constant_p
= false;
21902 /* If it is not a `{', then we are looking at an
21903 assignment-expression. */
21904 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21907 = cp_parser_constant_expression (parser
,
21908 /*allow_non_constant_p=*/true,
21912 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21914 return initializer
;
21917 /* Parse a brace-enclosed initializer list.
21920 { initializer-list , [opt] }
21921 { designated-initializer-list , [opt] }
21924 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21925 the elements of the initializer-list (or NULL, if the last
21926 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21927 NULL_TREE. There is no way to detect whether or not the optional
21928 trailing `,' was provided. NON_CONSTANT_P is as for
21929 cp_parser_initializer. */
21932 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21935 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21937 /* Consume the `{' token. */
21938 matching_braces braces
;
21939 braces
.require_open (parser
);
21940 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21941 initializer
= make_node (CONSTRUCTOR
);
21942 /* If it's not a `}', then there is a non-trivial initializer. */
21943 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21945 /* Parse the initializer list. */
21946 CONSTRUCTOR_ELTS (initializer
)
21947 = cp_parser_initializer_list (parser
, non_constant_p
);
21948 /* A trailing `,' token is allowed. */
21949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21950 cp_lexer_consume_token (parser
->lexer
);
21953 *non_constant_p
= false;
21954 /* Now, there should be a trailing `}'. */
21955 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21956 braces
.require_close (parser
);
21957 TREE_TYPE (initializer
) = init_list_type_node
;
21959 cp_expr
result (initializer
);
21960 /* Build a location of the form:
21963 with caret==start at the open brace, finish at the close brace. */
21964 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21965 result
.set_location (combined_loc
);
21969 /* Consume tokens up to, and including, the next non-nested closing `]'.
21970 Returns true iff we found a closing `]'. */
21973 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21975 unsigned square_depth
= 0;
21979 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21981 switch (token
->type
)
21984 case CPP_PRAGMA_EOL
:
21985 /* If we've run out of tokens, then there is no closing `]'. */
21988 case CPP_OPEN_SQUARE
:
21992 case CPP_CLOSE_SQUARE
:
21993 if (!square_depth
--)
21995 cp_lexer_consume_token (parser
->lexer
);
22004 /* Consume the token. */
22005 cp_lexer_consume_token (parser
->lexer
);
22009 /* Return true if we are looking at an array-designator, false otherwise. */
22012 cp_parser_array_designator_p (cp_parser
*parser
)
22014 /* Consume the `['. */
22015 cp_lexer_consume_token (parser
->lexer
);
22017 cp_lexer_save_tokens (parser
->lexer
);
22019 /* Skip tokens until the next token is a closing square bracket.
22020 If we find the closing `]', and the next token is a `=', then
22021 we are looking at an array designator. */
22022 bool array_designator_p
22023 = (cp_parser_skip_to_closing_square_bracket (parser
)
22024 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22026 /* Roll back the tokens we skipped. */
22027 cp_lexer_rollback_tokens (parser
->lexer
);
22029 return array_designator_p
;
22032 /* Parse an initializer-list.
22035 initializer-clause ... [opt]
22036 initializer-list , initializer-clause ... [opt]
22040 designated-initializer-list:
22041 designated-initializer-clause
22042 designated-initializer-list , designated-initializer-clause
22044 designated-initializer-clause:
22045 designator brace-or-equal-initializer
22053 designation initializer-clause ...[opt]
22054 initializer-list , designation initializer-clause ...[opt]
22059 [ constant-expression ] =
22061 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22062 for the initializer. If the INDEX of the elt is non-NULL, it is the
22063 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22064 as for cp_parser_initializer. */
22066 static vec
<constructor_elt
, va_gc
> *
22067 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22069 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22070 bool first_p
= true;
22071 tree first_designator
= NULL_TREE
;
22073 /* Assume all of the expressions are constant. */
22074 *non_constant_p
= false;
22076 /* Parse the rest of the list. */
22082 bool clause_non_constant_p
;
22083 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22085 /* Handle the C++2A syntax, '. id ='. */
22086 if ((cxx_dialect
>= cxx2a
22087 || cp_parser_allow_gnu_extensions_p (parser
))
22088 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22089 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22090 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22091 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22092 == CPP_OPEN_BRACE
)))
22094 if (cxx_dialect
< cxx2a
)
22095 pedwarn (loc
, OPT_Wpedantic
,
22096 "C++ designated initializers only available with "
22097 "-std=c++2a or -std=gnu++2a");
22098 /* Consume the `.'. */
22099 cp_lexer_consume_token (parser
->lexer
);
22100 /* Consume the identifier. */
22101 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22103 /* Consume the `='. */
22104 cp_lexer_consume_token (parser
->lexer
);
22106 /* Also, if the next token is an identifier and the following one is a
22107 colon, we are looking at the GNU designated-initializer
22109 else if (cp_parser_allow_gnu_extensions_p (parser
)
22110 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22111 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22114 /* Warn the user that they are using an extension. */
22115 pedwarn (loc
, OPT_Wpedantic
,
22116 "ISO C++ does not allow GNU designated initializers");
22117 /* Consume the identifier. */
22118 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22119 /* Consume the `:'. */
22120 cp_lexer_consume_token (parser
->lexer
);
22122 /* Also handle C99 array designators, '[ const ] ='. */
22123 else if (cp_parser_allow_gnu_extensions_p (parser
)
22124 && !c_dialect_objc ()
22125 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22127 /* In C++11, [ could start a lambda-introducer. */
22128 bool non_const
= false;
22130 cp_parser_parse_tentatively (parser
);
22132 if (!cp_parser_array_designator_p (parser
))
22134 cp_parser_simulate_error (parser
);
22135 designator
= NULL_TREE
;
22139 designator
= cp_parser_constant_expression (parser
, true,
22141 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22142 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22145 if (!cp_parser_parse_definitely (parser
))
22146 designator
= NULL_TREE
;
22148 && (!require_potential_rvalue_constant_expression
22150 designator
= NULL_TREE
;
22152 /* Warn the user that they are using an extension. */
22153 pedwarn (loc
, OPT_Wpedantic
,
22154 "ISO C++ does not allow C99 designated initializers");
22157 designator
= NULL_TREE
;
22161 first_designator
= designator
;
22164 else if (cxx_dialect
>= cxx2a
22165 && first_designator
!= error_mark_node
22166 && (!first_designator
!= !designator
))
22168 error_at (loc
, "either all initializer clauses should be designated "
22169 "or none of them should be");
22170 first_designator
= error_mark_node
;
22172 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22173 first_designator
= designator
;
22175 /* Parse the initializer. */
22176 initializer
= cp_parser_initializer_clause (parser
,
22177 &clause_non_constant_p
);
22178 /* If any clause is non-constant, so is the entire initializer. */
22179 if (clause_non_constant_p
)
22180 *non_constant_p
= true;
22182 /* If we have an ellipsis, this is an initializer pack
22184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22186 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22188 /* Consume the `...'. */
22189 cp_lexer_consume_token (parser
->lexer
);
22191 if (designator
&& cxx_dialect
>= cxx2a
)
22193 "%<...%> not allowed in designated initializer list");
22195 /* Turn the initializer into an initializer expansion. */
22196 initializer
= make_pack_expansion (initializer
);
22199 /* Add it to the vector. */
22200 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22202 /* If the next token is not a comma, we have reached the end of
22204 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22207 /* Peek at the next token. */
22208 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22209 /* If the next token is a `}', then we're still done. An
22210 initializer-clause can have a trailing `,' after the
22211 initializer-list and before the closing `}'. */
22212 if (token
->type
== CPP_CLOSE_BRACE
)
22215 /* Consume the `,' token. */
22216 cp_lexer_consume_token (parser
->lexer
);
22219 /* The same identifier shall not appear in multiple designators
22220 of a designated-initializer-list. */
22221 if (first_designator
)
22224 tree designator
, val
;
22225 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22226 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22228 if (IDENTIFIER_MARKED (designator
))
22230 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22231 "%<.%s%> designator used multiple times in "
22232 "the same initializer list",
22233 IDENTIFIER_POINTER (designator
));
22234 (*v
)[i
].index
= NULL_TREE
;
22237 IDENTIFIER_MARKED (designator
) = 1;
22239 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22240 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22241 IDENTIFIER_MARKED (designator
) = 0;
22247 /* Classes [gram.class] */
22249 /* Parse a class-name.
22255 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22256 to indicate that names looked up in dependent types should be
22257 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22258 keyword has been used to indicate that the name that appears next
22259 is a template. TAG_TYPE indicates the explicit tag given before
22260 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22261 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22262 is the class being defined in a class-head. If ENUM_OK is TRUE,
22263 enum-names are also accepted.
22265 Returns the TYPE_DECL representing the class. */
22268 cp_parser_class_name (cp_parser
*parser
,
22269 bool typename_keyword_p
,
22270 bool template_keyword_p
,
22271 enum tag_types tag_type
,
22272 bool check_dependency_p
,
22274 bool is_declaration
,
22281 tree identifier
= NULL_TREE
;
22283 /* All class-names start with an identifier. */
22284 token
= cp_lexer_peek_token (parser
->lexer
);
22285 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22287 cp_parser_error (parser
, "expected class-name");
22288 return error_mark_node
;
22291 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22292 to a template-id, so we save it here. */
22293 scope
= parser
->scope
;
22294 if (scope
== error_mark_node
)
22295 return error_mark_node
;
22297 /* Any name names a type if we're following the `typename' keyword
22298 in a qualified name where the enclosing scope is type-dependent. */
22299 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22300 && dependent_type_p (scope
));
22301 /* Handle the common case (an identifier, but not a template-id)
22303 if (token
->type
== CPP_NAME
22304 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22306 cp_token
*identifier_token
;
22309 /* Look for the identifier. */
22310 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22311 ambiguous_p
= identifier_token
->error_reported
;
22312 identifier
= cp_parser_identifier (parser
);
22313 /* If the next token isn't an identifier, we are certainly not
22314 looking at a class-name. */
22315 if (identifier
== error_mark_node
)
22316 decl
= error_mark_node
;
22317 /* If we know this is a type-name, there's no need to look it
22319 else if (typename_p
)
22323 tree ambiguous_decls
;
22324 /* If we already know that this lookup is ambiguous, then
22325 we've already issued an error message; there's no reason
22329 cp_parser_simulate_error (parser
);
22330 return error_mark_node
;
22332 /* If the next token is a `::', then the name must be a type
22335 [basic.lookup.qual]
22337 During the lookup for a name preceding the :: scope
22338 resolution operator, object, function, and enumerator
22339 names are ignored. */
22340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22341 tag_type
= scope_type
;
22342 /* Look up the name. */
22343 decl
= cp_parser_lookup_name (parser
, identifier
,
22345 /*is_template=*/false,
22346 /*is_namespace=*/false,
22347 check_dependency_p
,
22349 identifier_token
->location
);
22350 if (ambiguous_decls
)
22352 if (cp_parser_parsing_tentatively (parser
))
22353 cp_parser_simulate_error (parser
);
22354 return error_mark_node
;
22360 /* Try a template-id. */
22361 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22362 check_dependency_p
,
22365 if (decl
== error_mark_node
)
22366 return error_mark_node
;
22369 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22371 /* If this is a typename, create a TYPENAME_TYPE. */
22372 if (typename_p
&& decl
!= error_mark_node
)
22374 decl
= make_typename_type (scope
, decl
, typename_type
,
22375 /*complain=*/tf_error
);
22376 if (decl
!= error_mark_node
)
22377 decl
= TYPE_NAME (decl
);
22380 decl
= strip_using_decl (decl
);
22382 /* Check to see that it is really the name of a class. */
22383 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22384 && identifier_p (TREE_OPERAND (decl
, 0))
22385 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22386 /* Situations like this:
22388 template <typename T> struct A {
22389 typename T::template X<int>::I i;
22392 are problematic. Is `T::template X<int>' a class-name? The
22393 standard does not seem to be definitive, but there is no other
22394 valid interpretation of the following `::'. Therefore, those
22395 names are considered class-names. */
22397 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22398 if (decl
!= error_mark_node
)
22399 decl
= TYPE_NAME (decl
);
22401 else if (TREE_CODE (decl
) != TYPE_DECL
22402 || TREE_TYPE (decl
) == error_mark_node
22403 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22404 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22405 /* In Objective-C 2.0, a classname followed by '.' starts a
22406 dot-syntax expression, and it's not a type-name. */
22407 || (c_dialect_objc ()
22408 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22409 && objc_is_class_name (decl
)))
22410 decl
= error_mark_node
;
22412 if (decl
== error_mark_node
)
22413 cp_parser_error (parser
, "expected class-name");
22414 else if (identifier
&& !parser
->scope
)
22415 maybe_note_name_used_in_class (identifier
, decl
);
22420 /* Parse a class-specifier.
22423 class-head { member-specification [opt] }
22425 Returns the TREE_TYPE representing the class. */
22428 cp_parser_class_specifier_1 (cp_parser
* parser
)
22431 tree attributes
= NULL_TREE
;
22432 bool nested_name_specifier_p
;
22433 unsigned saved_num_template_parameter_lists
;
22434 bool saved_in_function_body
;
22435 unsigned char in_statement
;
22436 bool in_switch_statement_p
;
22437 bool saved_in_unbraced_linkage_specification_p
;
22438 tree old_scope
= NULL_TREE
;
22439 tree scope
= NULL_TREE
;
22440 cp_token
*closing_brace
;
22442 push_deferring_access_checks (dk_no_deferred
);
22444 /* Parse the class-head. */
22445 type
= cp_parser_class_head (parser
,
22446 &nested_name_specifier_p
);
22447 /* If the class-head was a semantic disaster, skip the entire body
22451 cp_parser_skip_to_end_of_block_or_statement (parser
);
22452 pop_deferring_access_checks ();
22453 return error_mark_node
;
22456 /* Look for the `{'. */
22457 matching_braces braces
;
22458 if (!braces
.require_open (parser
))
22460 pop_deferring_access_checks ();
22461 return error_mark_node
;
22464 cp_ensure_no_omp_declare_simd (parser
);
22465 cp_ensure_no_oacc_routine (parser
);
22467 /* Issue an error message if type-definitions are forbidden here. */
22468 cp_parser_check_type_definition (parser
);
22469 /* Remember that we are defining one more class. */
22470 ++parser
->num_classes_being_defined
;
22471 /* Inside the class, surrounding template-parameter-lists do not
22473 saved_num_template_parameter_lists
22474 = parser
->num_template_parameter_lists
;
22475 parser
->num_template_parameter_lists
= 0;
22476 /* We are not in a function body. */
22477 saved_in_function_body
= parser
->in_function_body
;
22478 parser
->in_function_body
= false;
22479 /* Or in a loop. */
22480 in_statement
= parser
->in_statement
;
22481 parser
->in_statement
= 0;
22482 /* Or in a switch. */
22483 in_switch_statement_p
= parser
->in_switch_statement_p
;
22484 parser
->in_switch_statement_p
= false;
22485 /* We are not immediately inside an extern "lang" block. */
22486 saved_in_unbraced_linkage_specification_p
22487 = parser
->in_unbraced_linkage_specification_p
;
22488 parser
->in_unbraced_linkage_specification_p
= false;
22490 // Associate constraints with the type.
22492 type
= associate_classtype_constraints (type
);
22494 /* Start the class. */
22495 if (nested_name_specifier_p
)
22497 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22498 old_scope
= push_inner_scope (scope
);
22500 type
= begin_class_definition (type
);
22502 if (type
== error_mark_node
)
22503 /* If the type is erroneous, skip the entire body of the class. */
22504 cp_parser_skip_to_closing_brace (parser
);
22506 /* Parse the member-specification. */
22507 cp_parser_member_specification_opt (parser
);
22509 /* Look for the trailing `}'. */
22510 closing_brace
= braces
.require_close (parser
);
22511 /* Look for trailing attributes to apply to this class. */
22512 if (cp_parser_allow_gnu_extensions_p (parser
))
22513 attributes
= cp_parser_gnu_attributes_opt (parser
);
22514 if (type
!= error_mark_node
)
22515 type
= finish_struct (type
, attributes
);
22516 if (nested_name_specifier_p
)
22517 pop_inner_scope (old_scope
, scope
);
22519 /* We've finished a type definition. Check for the common syntax
22520 error of forgetting a semicolon after the definition. We need to
22521 be careful, as we can't just check for not-a-semicolon and be done
22522 with it; the user might have typed:
22524 class X { } c = ...;
22525 class X { } *p = ...;
22527 and so forth. Instead, enumerate all the possible tokens that
22528 might follow this production; if we don't see one of them, then
22529 complain and silently insert the semicolon. */
22531 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22532 bool want_semicolon
= true;
22534 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22535 /* Don't try to parse c++11 attributes here. As per the
22536 grammar, that should be a task for
22537 cp_parser_decl_specifier_seq. */
22538 want_semicolon
= false;
22540 switch (token
->type
)
22543 case CPP_SEMICOLON
:
22546 case CPP_OPEN_PAREN
:
22547 case CPP_CLOSE_PAREN
:
22549 want_semicolon
= false;
22552 /* While it's legal for type qualifiers and storage class
22553 specifiers to follow type definitions in the grammar, only
22554 compiler testsuites contain code like that. Assume that if
22555 we see such code, then what we're really seeing is a case
22559 const <type> var = ...;
22564 static <type> func (...) ...
22566 i.e. the qualifier or specifier applies to the next
22567 declaration. To do so, however, we need to look ahead one
22568 more token to see if *that* token is a type specifier.
22570 This code could be improved to handle:
22573 static const <type> var = ...; */
22575 if (keyword_is_decl_specifier (token
->keyword
))
22577 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22579 /* Handling user-defined types here would be nice, but very
22582 = (lookahead
->type
== CPP_KEYWORD
22583 && keyword_begins_type_specifier (lookahead
->keyword
));
22590 /* If we don't have a type, then something is very wrong and we
22591 shouldn't try to do anything clever. Likewise for not seeing the
22593 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22595 /* Locate the closing brace. */
22596 cp_token_position prev
22597 = cp_lexer_previous_token_position (parser
->lexer
);
22598 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22599 location_t loc
= prev_token
->location
;
22601 /* We want to suggest insertion of a ';' immediately *after* the
22602 closing brace, so, if we can, offset the location by 1 column. */
22603 location_t next_loc
= loc
;
22604 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22605 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22607 rich_location
richloc (line_table
, next_loc
);
22609 /* If we successfully offset the location, suggest the fix-it. */
22610 if (next_loc
!= loc
)
22611 richloc
.add_fixit_insert_before (next_loc
, ";");
22613 if (CLASSTYPE_DECLARED_CLASS (type
))
22614 error_at (&richloc
,
22615 "expected %<;%> after class definition");
22616 else if (TREE_CODE (type
) == RECORD_TYPE
)
22617 error_at (&richloc
,
22618 "expected %<;%> after struct definition");
22619 else if (TREE_CODE (type
) == UNION_TYPE
)
22620 error_at (&richloc
,
22621 "expected %<;%> after union definition");
22623 gcc_unreachable ();
22625 /* Unget one token and smash it to look as though we encountered
22626 a semicolon in the input stream. */
22627 cp_lexer_set_token_position (parser
->lexer
, prev
);
22628 token
= cp_lexer_peek_token (parser
->lexer
);
22629 token
->type
= CPP_SEMICOLON
;
22630 token
->keyword
= RID_MAX
;
22634 /* If this class is not itself within the scope of another class,
22635 then we need to parse the bodies of all of the queued function
22636 definitions. Note that the queued functions defined in a class
22637 are not always processed immediately following the
22638 class-specifier for that class. Consider:
22641 struct B { void f() { sizeof (A); } };
22644 If `f' were processed before the processing of `A' were
22645 completed, there would be no way to compute the size of `A'.
22646 Note that the nesting we are interested in here is lexical --
22647 not the semantic nesting given by TYPE_CONTEXT. In particular,
22650 struct A { struct B; };
22651 struct A::B { void f() { } };
22653 there is no need to delay the parsing of `A::B::f'. */
22654 if (--parser
->num_classes_being_defined
== 0)
22657 tree class_type
= NULL_TREE
;
22658 tree pushed_scope
= NULL_TREE
;
22660 cp_default_arg_entry
*e
;
22661 tree save_ccp
, save_ccr
;
22663 /* In a first pass, parse default arguments to the functions.
22664 Then, in a second pass, parse the bodies of the functions.
22665 This two-phased approach handles cases like:
22673 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22676 /* If there are default arguments that have not yet been processed,
22677 take care of them now. */
22678 if (class_type
!= e
->class_type
)
22681 pop_scope (pushed_scope
);
22682 class_type
= e
->class_type
;
22683 pushed_scope
= push_scope (class_type
);
22685 /* Make sure that any template parameters are in scope. */
22686 maybe_begin_member_template_processing (decl
);
22687 /* Parse the default argument expressions. */
22688 cp_parser_late_parsing_default_args (parser
, decl
);
22689 /* Remove any template parameters from the symbol table. */
22690 maybe_end_member_template_processing ();
22692 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22693 /* Now parse any NSDMIs. */
22694 save_ccp
= current_class_ptr
;
22695 save_ccr
= current_class_ref
;
22696 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22698 if (class_type
!= DECL_CONTEXT (decl
))
22701 pop_scope (pushed_scope
);
22702 class_type
= DECL_CONTEXT (decl
);
22703 pushed_scope
= push_scope (class_type
);
22705 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22706 cp_parser_late_parsing_nsdmi (parser
, decl
);
22708 vec_safe_truncate (unparsed_nsdmis
, 0);
22709 current_class_ptr
= save_ccp
;
22710 current_class_ref
= save_ccr
;
22712 pop_scope (pushed_scope
);
22714 /* Now do some post-NSDMI bookkeeping. */
22715 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22716 after_nsdmi_defaulted_late_checks (class_type
);
22717 vec_safe_truncate (unparsed_classes
, 0);
22718 after_nsdmi_defaulted_late_checks (type
);
22720 /* Now parse the body of the functions. */
22723 /* OpenMP UDRs need to be parsed before all other functions. */
22724 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22725 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22726 cp_parser_late_parsing_for_member (parser
, decl
);
22727 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22728 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22729 cp_parser_late_parsing_for_member (parser
, decl
);
22732 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22733 cp_parser_late_parsing_for_member (parser
, decl
);
22734 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22737 vec_safe_push (unparsed_classes
, type
);
22739 /* Put back any saved access checks. */
22740 pop_deferring_access_checks ();
22742 /* Restore saved state. */
22743 parser
->in_switch_statement_p
= in_switch_statement_p
;
22744 parser
->in_statement
= in_statement
;
22745 parser
->in_function_body
= saved_in_function_body
;
22746 parser
->num_template_parameter_lists
22747 = saved_num_template_parameter_lists
;
22748 parser
->in_unbraced_linkage_specification_p
22749 = saved_in_unbraced_linkage_specification_p
;
22755 cp_parser_class_specifier (cp_parser
* parser
)
22758 timevar_push (TV_PARSE_STRUCT
);
22759 ret
= cp_parser_class_specifier_1 (parser
);
22760 timevar_pop (TV_PARSE_STRUCT
);
22764 /* Parse a class-head.
22767 class-key identifier [opt] base-clause [opt]
22768 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22769 class-key nested-name-specifier [opt] template-id
22772 class-virt-specifier:
22776 class-key attributes identifier [opt] base-clause [opt]
22777 class-key attributes nested-name-specifier identifier base-clause [opt]
22778 class-key attributes nested-name-specifier [opt] template-id
22781 Upon return BASES is initialized to the list of base classes (or
22782 NULL, if there are none) in the same form returned by
22783 cp_parser_base_clause.
22785 Returns the TYPE of the indicated class. Sets
22786 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22787 involving a nested-name-specifier was used, and FALSE otherwise.
22789 Returns error_mark_node if this is not a class-head.
22791 Returns NULL_TREE if the class-head is syntactically valid, but
22792 semantically invalid in a way that means we should skip the entire
22793 body of the class. */
22796 cp_parser_class_head (cp_parser
* parser
,
22797 bool* nested_name_specifier_p
)
22799 tree nested_name_specifier
;
22800 enum tag_types class_key
;
22801 tree id
= NULL_TREE
;
22802 tree type
= NULL_TREE
;
22805 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22806 bool template_id_p
= false;
22807 bool qualified_p
= false;
22808 bool invalid_nested_name_p
= false;
22809 bool invalid_explicit_specialization_p
= false;
22810 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22811 tree pushed_scope
= NULL_TREE
;
22812 unsigned num_templates
;
22813 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22814 /* Assume no nested-name-specifier will be present. */
22815 *nested_name_specifier_p
= false;
22816 /* Assume no template parameter lists will be used in defining the
22819 parser
->colon_corrects_to_scope_p
= false;
22821 /* Look for the class-key. */
22822 class_key
= cp_parser_class_key (parser
);
22823 if (class_key
== none_type
)
22824 return error_mark_node
;
22826 location_t class_head_start_location
= input_location
;
22828 /* Parse the attributes. */
22829 attributes
= cp_parser_attributes_opt (parser
);
22831 /* If the next token is `::', that is invalid -- but sometimes
22832 people do try to write:
22836 Handle this gracefully by accepting the extra qualifier, and then
22837 issuing an error about it later if this really is a
22838 class-head. If it turns out just to be an elaborated type
22839 specifier, remain silent. */
22840 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22841 qualified_p
= true;
22843 push_deferring_access_checks (dk_no_check
);
22845 /* Determine the name of the class. Begin by looking for an
22846 optional nested-name-specifier. */
22847 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22848 nested_name_specifier
22849 = cp_parser_nested_name_specifier_opt (parser
,
22850 /*typename_keyword_p=*/false,
22851 /*check_dependency_p=*/false,
22853 /*is_declaration=*/false);
22854 /* If there was a nested-name-specifier, then there *must* be an
22857 cp_token
*bad_template_keyword
= NULL
;
22859 if (nested_name_specifier
)
22861 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22862 /* Although the grammar says `identifier', it really means
22863 `class-name' or `template-name'. You are only allowed to
22864 define a class that has already been declared with this
22867 The proposed resolution for Core Issue 180 says that wherever
22868 you see `class T::X' you should treat `X' as a type-name.
22870 It is OK to define an inaccessible class; for example:
22872 class A { class B; };
22875 We do not know if we will see a class-name, or a
22876 template-name. We look for a class-name first, in case the
22877 class-name is a template-id; if we looked for the
22878 template-name first we would stop after the template-name. */
22879 cp_parser_parse_tentatively (parser
);
22880 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22881 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22882 type
= cp_parser_class_name (parser
,
22883 /*typename_keyword_p=*/false,
22884 /*template_keyword_p=*/false,
22886 /*check_dependency_p=*/false,
22887 /*class_head_p=*/true,
22888 /*is_declaration=*/false);
22889 /* If that didn't work, ignore the nested-name-specifier. */
22890 if (!cp_parser_parse_definitely (parser
))
22892 invalid_nested_name_p
= true;
22893 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22894 id
= cp_parser_identifier (parser
);
22895 if (id
== error_mark_node
)
22898 /* If we could not find a corresponding TYPE, treat this
22899 declaration like an unqualified declaration. */
22900 if (type
== error_mark_node
)
22901 nested_name_specifier
= NULL_TREE
;
22902 /* Otherwise, count the number of templates used in TYPE and its
22903 containing scopes. */
22908 for (scope
= TREE_TYPE (type
);
22909 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22910 scope
= get_containing_scope (scope
))
22912 && CLASS_TYPE_P (scope
)
22913 && CLASSTYPE_TEMPLATE_INFO (scope
)
22914 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22915 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22916 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22920 /* Otherwise, the identifier is optional. */
22923 /* We don't know whether what comes next is a template-id,
22924 an identifier, or nothing at all. */
22925 cp_parser_parse_tentatively (parser
);
22926 /* Check for a template-id. */
22927 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22928 id
= cp_parser_template_id (parser
,
22929 /*template_keyword_p=*/false,
22930 /*check_dependency_p=*/true,
22932 /*is_declaration=*/true);
22933 /* If that didn't work, it could still be an identifier. */
22934 if (!cp_parser_parse_definitely (parser
))
22936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22938 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22939 id
= cp_parser_identifier (parser
);
22946 template_id_p
= true;
22951 pop_deferring_access_checks ();
22955 cp_parser_check_for_invalid_template_id (parser
, id
,
22957 type_start_token
->location
);
22959 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22961 /* If it's not a `:' or a `{' then we can't really be looking at a
22962 class-head, since a class-head only appears as part of a
22963 class-specifier. We have to detect this situation before calling
22964 xref_tag, since that has irreversible side-effects. */
22965 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22967 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22968 type
= error_mark_node
;
22972 /* At this point, we're going ahead with the class-specifier, even
22973 if some other problem occurs. */
22974 cp_parser_commit_to_tentative_parse (parser
);
22975 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22977 cp_parser_error (parser
,
22978 "cannot specify %<override%> for a class");
22979 type
= error_mark_node
;
22982 /* Issue the error about the overly-qualified name now. */
22985 cp_parser_error (parser
,
22986 "global qualification of class name is invalid");
22987 type
= error_mark_node
;
22990 else if (invalid_nested_name_p
)
22992 cp_parser_error (parser
,
22993 "qualified name does not name a class");
22994 type
= error_mark_node
;
22997 else if (nested_name_specifier
)
23001 if (bad_template_keyword
)
23002 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23003 keyword template shall not appear at the top level. */
23004 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23005 "keyword %<template%> not allowed in class-head-name");
23007 /* Reject typedef-names in class heads. */
23008 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23010 error_at (type_start_token
->location
,
23011 "invalid class name in declaration of %qD",
23017 /* Figure out in what scope the declaration is being placed. */
23018 scope
= current_scope ();
23019 /* If that scope does not contain the scope in which the
23020 class was originally declared, the program is invalid. */
23021 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23023 if (at_namespace_scope_p ())
23024 error_at (type_start_token
->location
,
23025 "declaration of %qD in namespace %qD which does not "
23027 type
, scope
, nested_name_specifier
);
23029 error_at (type_start_token
->location
,
23030 "declaration of %qD in %qD which does not enclose %qD",
23031 type
, scope
, nested_name_specifier
);
23037 A declarator-id shall not be qualified except for the
23038 definition of a ... nested class outside of its class
23039 ... [or] the definition or explicit instantiation of a
23040 class member of a namespace outside of its namespace. */
23041 if (scope
== nested_name_specifier
)
23043 permerror (nested_name_specifier_token_start
->location
,
23044 "extra qualification not allowed");
23045 nested_name_specifier
= NULL_TREE
;
23049 /* An explicit-specialization must be preceded by "template <>". If
23050 it is not, try to recover gracefully. */
23051 if (at_namespace_scope_p ()
23052 && parser
->num_template_parameter_lists
== 0
23053 && !processing_template_parmlist
23056 /* Build a location of this form:
23057 struct typename <ARGS>
23058 ^~~~~~~~~~~~~~~~~~~~~~
23059 with caret==start at the start token, and
23060 finishing at the end of the type. */
23061 location_t reported_loc
23062 = make_location (class_head_start_location
,
23063 class_head_start_location
,
23064 get_finish (type_start_token
->location
));
23065 rich_location
richloc (line_table
, reported_loc
);
23066 richloc
.add_fixit_insert_before (class_head_start_location
,
23068 error_at (&richloc
,
23069 "an explicit specialization must be preceded by"
23070 " %<template <>%>");
23071 invalid_explicit_specialization_p
= true;
23072 /* Take the same action that would have been taken by
23073 cp_parser_explicit_specialization. */
23074 ++parser
->num_template_parameter_lists
;
23075 begin_specialization ();
23077 /* There must be no "return" statements between this point and the
23078 end of this function; set "type "to the correct return value and
23079 use "goto done;" to return. */
23080 /* Make sure that the right number of template parameters were
23082 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23083 type_start_token
->location
,
23084 /*declarator=*/NULL
))
23086 /* If something went wrong, there is no point in even trying to
23087 process the class-definition. */
23092 /* Look up the type. */
23095 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23096 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23097 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23099 error_at (type_start_token
->location
,
23100 "function template %qD redeclared as a class template", id
);
23101 type
= error_mark_node
;
23105 type
= TREE_TYPE (id
);
23106 type
= maybe_process_partial_specialization (type
);
23108 /* Check the scope while we still know whether or not we had a
23109 nested-name-specifier. */
23110 if (type
!= error_mark_node
)
23111 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23113 if (nested_name_specifier
)
23114 pushed_scope
= push_scope (nested_name_specifier
);
23116 else if (nested_name_specifier
)
23122 template <typename T> struct S { struct T };
23123 template <typename T> struct S<T>::T { };
23125 we will get a TYPENAME_TYPE when processing the definition of
23126 `S::T'. We need to resolve it to the actual type before we
23127 try to define it. */
23128 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23130 class_type
= resolve_typename_type (TREE_TYPE (type
),
23131 /*only_current_p=*/false);
23132 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23133 type
= TYPE_NAME (class_type
);
23136 cp_parser_error (parser
, "could not resolve typename type");
23137 type
= error_mark_node
;
23141 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23142 == error_mark_node
)
23148 class_type
= current_class_type
;
23149 /* Enter the scope indicated by the nested-name-specifier. */
23150 pushed_scope
= push_scope (nested_name_specifier
);
23151 /* Get the canonical version of this type. */
23152 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23153 /* Call push_template_decl if it seems like we should be defining a
23154 template either from the template headers or the type we're
23155 defining, so that we diagnose both extra and missing headers. */
23156 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23157 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23158 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23160 type
= push_template_decl (type
);
23161 if (type
== error_mark_node
)
23168 type
= TREE_TYPE (type
);
23169 *nested_name_specifier_p
= true;
23171 else /* The name is not a nested name. */
23173 /* If the class was unnamed, create a dummy name. */
23175 id
= make_anon_name ();
23176 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23177 ? ts_within_enclosing_non_class
23179 type
= xref_tag (class_key
, id
, tag_scope
,
23180 parser
->num_template_parameter_lists
);
23183 /* Indicate whether this class was declared as a `class' or as a
23185 if (TREE_CODE (type
) == RECORD_TYPE
)
23186 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23187 cp_parser_check_class_key (class_key
, type
);
23189 /* If this type was already complete, and we see another definition,
23190 that's an error. */
23191 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23193 error_at (type_start_token
->location
, "redefinition of %q#T",
23195 inform (location_of (type
), "previous definition of %q#T",
23200 else if (type
== error_mark_node
)
23205 /* Apply attributes now, before any use of the class as a template
23206 argument in its base list. */
23207 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23208 fixup_attribute_variants (type
);
23211 /* We will have entered the scope containing the class; the names of
23212 base classes should be looked up in that context. For example:
23214 struct A { struct B {}; struct C; };
23215 struct A::C : B {};
23219 /* Get the list of base-classes, if there is one. */
23220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23222 /* PR59482: enter the class scope so that base-specifiers are looked
23226 bases
= cp_parser_base_clause (parser
);
23227 /* PR59482: get out of the previously pushed class scope so that the
23228 subsequent pops pop the right thing. */
23235 /* If we're really defining a class, process the base classes.
23236 If they're invalid, fail. */
23237 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23238 xref_basetypes (type
, bases
);
23241 /* Leave the scope given by the nested-name-specifier. We will
23242 enter the class scope itself while processing the members. */
23244 pop_scope (pushed_scope
);
23246 if (invalid_explicit_specialization_p
)
23248 end_specialization ();
23249 --parser
->num_template_parameter_lists
;
23253 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23254 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23255 CLASSTYPE_FINAL (type
) = 1;
23257 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23261 /* Parse a class-key.
23268 Returns the kind of class-key specified, or none_type to indicate
23271 static enum tag_types
23272 cp_parser_class_key (cp_parser
* parser
)
23275 enum tag_types tag_type
;
23277 /* Look for the class-key. */
23278 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23282 /* Check to see if the TOKEN is a class-key. */
23283 tag_type
= cp_parser_token_is_class_key (token
);
23285 cp_parser_error (parser
, "expected class-key");
23289 /* Parse a type-parameter-key.
23291 type-parameter-key:
23297 cp_parser_type_parameter_key (cp_parser
* parser
)
23299 /* Look for the type-parameter-key. */
23300 enum tag_types tag_type
= none_type
;
23301 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23302 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23304 cp_lexer_consume_token (parser
->lexer
);
23305 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23306 /* typename is not allowed in a template template parameter
23307 by the standard until C++17. */
23308 pedwarn (token
->location
, OPT_Wpedantic
,
23309 "ISO C++ forbids typename key in template template parameter;"
23310 " use -std=c++17 or -std=gnu++17");
23313 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23318 /* Parse an (optional) member-specification.
23320 member-specification:
23321 member-declaration member-specification [opt]
23322 access-specifier : member-specification [opt] */
23325 cp_parser_member_specification_opt (cp_parser
* parser
)
23332 /* Peek at the next token. */
23333 token
= cp_lexer_peek_token (parser
->lexer
);
23334 /* If it's a `}', or EOF then we've seen all the members. */
23335 if (token
->type
== CPP_CLOSE_BRACE
23336 || token
->type
== CPP_EOF
23337 || token
->type
== CPP_PRAGMA_EOL
)
23340 /* See if this token is a keyword. */
23341 keyword
= token
->keyword
;
23345 case RID_PROTECTED
:
23347 /* Consume the access-specifier. */
23348 cp_lexer_consume_token (parser
->lexer
);
23349 /* Remember which access-specifier is active. */
23350 current_access_specifier
= token
->u
.value
;
23351 /* Look for the `:'. */
23352 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23356 /* Accept #pragmas at class scope. */
23357 if (token
->type
== CPP_PRAGMA
)
23359 cp_parser_pragma (parser
, pragma_member
, NULL
);
23363 /* Otherwise, the next construction must be a
23364 member-declaration. */
23365 cp_parser_member_declaration (parser
);
23370 /* Parse a member-declaration.
23372 member-declaration:
23373 decl-specifier-seq [opt] member-declarator-list [opt] ;
23374 function-definition ; [opt]
23375 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23377 template-declaration
23380 member-declarator-list:
23382 member-declarator-list , member-declarator
23385 declarator pure-specifier [opt]
23386 declarator constant-initializer [opt]
23387 identifier [opt] : constant-expression
23391 member-declaration:
23392 __extension__ member-declaration
23395 declarator attributes [opt] pure-specifier [opt]
23396 declarator attributes [opt] constant-initializer [opt]
23397 identifier [opt] attributes [opt] : constant-expression
23401 member-declaration:
23402 static_assert-declaration */
23405 cp_parser_member_declaration (cp_parser
* parser
)
23407 cp_decl_specifier_seq decl_specifiers
;
23408 tree prefix_attributes
;
23410 int declares_class_or_enum
;
23412 cp_token
*token
= NULL
;
23413 cp_token
*decl_spec_token_start
= NULL
;
23414 cp_token
*initializer_token_start
= NULL
;
23415 int saved_pedantic
;
23416 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23418 /* Check for the `__extension__' keyword. */
23419 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23422 cp_parser_member_declaration (parser
);
23423 /* Restore the old value of the PEDANTIC flag. */
23424 pedantic
= saved_pedantic
;
23429 /* Check for a template-declaration. */
23430 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23432 /* An explicit specialization here is an error condition, and we
23433 expect the specialization handler to detect and report this. */
23434 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23435 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23436 cp_parser_explicit_specialization (parser
);
23438 cp_parser_template_declaration (parser
, /*member_p=*/true);
23442 /* Check for a template introduction. */
23443 else if (cp_parser_template_declaration_after_export (parser
, true))
23446 /* Check for a using-declaration. */
23447 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23449 if (cxx_dialect
< cxx11
)
23451 /* Parse the using-declaration. */
23452 cp_parser_using_declaration (parser
,
23453 /*access_declaration_p=*/false);
23459 bool alias_decl_expected
;
23460 cp_parser_parse_tentatively (parser
);
23461 decl
= cp_parser_alias_declaration (parser
);
23462 /* Note that if we actually see the '=' token after the
23463 identifier, cp_parser_alias_declaration commits the
23464 tentative parse. In that case, we really expect an
23465 alias-declaration. Otherwise, we expect a using
23467 alias_decl_expected
=
23468 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23469 cp_parser_parse_definitely (parser
);
23471 if (alias_decl_expected
)
23472 finish_member_declaration (decl
);
23474 cp_parser_using_declaration (parser
,
23475 /*access_declaration_p=*/false);
23480 /* Check for @defs. */
23481 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23484 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23485 ivar
= ivar_chains
;
23489 ivar
= TREE_CHAIN (member
);
23490 TREE_CHAIN (member
) = NULL_TREE
;
23491 finish_member_declaration (member
);
23496 /* If the next token is `static_assert' we have a static assertion. */
23497 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23499 cp_parser_static_assert (parser
, /*member_p=*/true);
23503 parser
->colon_corrects_to_scope_p
= false;
23505 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23508 /* Parse the decl-specifier-seq. */
23509 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23510 cp_parser_decl_specifier_seq (parser
,
23511 CP_PARSER_FLAGS_OPTIONAL
,
23513 &declares_class_or_enum
);
23514 /* Check for an invalid type-name. */
23515 if (!decl_specifiers
.any_type_specifiers_p
23516 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23518 /* If there is no declarator, then the decl-specifier-seq should
23520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23522 /* If there was no decl-specifier-seq, and the next token is a
23523 `;', then we have something like:
23529 Each member-declaration shall declare at least one member
23530 name of the class. */
23531 if (!decl_specifiers
.any_specifiers_p
)
23533 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23534 if (!in_system_header_at (token
->location
))
23536 gcc_rich_location
richloc (token
->location
);
23537 richloc
.add_fixit_remove ();
23538 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23545 /* See if this declaration is a friend. */
23546 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23547 /* If there were decl-specifiers, check to see if there was
23548 a class-declaration. */
23549 type
= check_tag_decl (&decl_specifiers
,
23550 /*explicit_type_instantiation_p=*/false);
23551 /* Nested classes have already been added to the class, but
23552 a `friend' needs to be explicitly registered. */
23555 /* If the `friend' keyword was present, the friend must
23556 be introduced with a class-key. */
23557 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23558 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23559 "in C++03 a class-key must be used "
23560 "when declaring a friend");
23563 template <typename T> struct A {
23564 friend struct A<T>::B;
23567 A<T>::B will be represented by a TYPENAME_TYPE, and
23568 therefore not recognized by check_tag_decl. */
23571 type
= decl_specifiers
.type
;
23572 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23573 type
= TREE_TYPE (type
);
23575 if (!type
|| !TYPE_P (type
))
23576 error_at (decl_spec_token_start
->location
,
23577 "friend declaration does not name a class or "
23580 make_friend_class (current_class_type
, type
,
23581 /*complain=*/true);
23583 /* If there is no TYPE, an error message will already have
23585 else if (!type
|| type
== error_mark_node
)
23587 /* An anonymous aggregate has to be handled specially; such
23588 a declaration really declares a data member (with a
23589 particular type), as opposed to a nested class. */
23590 else if (ANON_AGGR_TYPE_P (type
))
23593 if (decl_specifiers
.storage_class
!= sc_none
)
23594 error_at (decl_spec_token_start
->location
,
23595 "a storage class on an anonymous aggregate "
23596 "in class scope is not allowed");
23598 /* Remove constructors and such from TYPE, now that we
23599 know it is an anonymous aggregate. */
23600 fixup_anonymous_aggr (type
);
23601 /* And make the corresponding data member. */
23602 decl
= build_decl (decl_spec_token_start
->location
,
23603 FIELD_DECL
, NULL_TREE
, type
);
23604 /* Add it to the class. */
23605 finish_member_declaration (decl
);
23608 cp_parser_check_access_in_redeclaration
23610 decl_spec_token_start
->location
);
23615 bool assume_semicolon
= false;
23617 /* Clear attributes from the decl_specifiers but keep them
23618 around as prefix attributes that apply them to the entity
23620 prefix_attributes
= decl_specifiers
.attributes
;
23621 decl_specifiers
.attributes
= NULL_TREE
;
23623 /* See if these declarations will be friends. */
23624 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23626 /* Keep going until we hit the `;' at the end of the
23628 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23630 tree attributes
= NULL_TREE
;
23631 tree first_attribute
;
23633 bool named_bitfld
= false;
23635 /* Peek at the next token. */
23636 token
= cp_lexer_peek_token (parser
->lexer
);
23638 /* The following code wants to know early if it is a bit-field
23639 or some other declaration. Attributes can appear before
23640 the `:' token. Skip over them without consuming any tokens
23641 to peek if they are followed by `:'. */
23642 if (cp_next_tokens_can_be_attribute_p (parser
)
23643 || (token
->type
== CPP_NAME
23644 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23645 && (named_bitfld
= true)))
23648 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23649 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23652 /* Check for a bitfield declaration. */
23653 if (token
->type
== CPP_COLON
23654 || (token
->type
== CPP_NAME
23655 && token
== cp_lexer_peek_token (parser
->lexer
)
23656 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23657 && (named_bitfld
= true)))
23661 tree late_attributes
= NULL_TREE
;
23664 identifier
= cp_parser_identifier (parser
);
23666 identifier
= NULL_TREE
;
23668 /* Look for attributes that apply to the bitfield. */
23669 attributes
= cp_parser_attributes_opt (parser
);
23671 /* Consume the `:' token. */
23672 cp_lexer_consume_token (parser
->lexer
);
23674 /* Get the width of the bitfield. */
23675 width
= cp_parser_constant_expression (parser
, false, NULL
,
23676 cxx_dialect
>= cxx11
);
23678 /* In C++2A and as extension for C++11 and above we allow
23679 default member initializers for bit-fields. */
23680 initializer
= NULL_TREE
;
23681 if (cxx_dialect
>= cxx11
23682 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23683 || cp_lexer_next_token_is (parser
->lexer
,
23687 = cp_lexer_peek_token (parser
->lexer
)->location
;
23688 if (cxx_dialect
< cxx2a
23689 && !in_system_header_at (loc
)
23690 && identifier
!= NULL_TREE
)
23692 "default member initializers for bit-fields "
23693 "only available with -std=c++2a or "
23696 initializer
= cp_parser_save_nsdmi (parser
);
23697 if (identifier
== NULL_TREE
)
23699 error_at (loc
, "default member initializer for "
23700 "unnamed bit-field");
23701 initializer
= NULL_TREE
;
23706 /* Look for attributes that apply to the bitfield after
23707 the `:' token and width. This is where GCC used to
23708 parse attributes in the past, pedwarn if there is
23709 a std attribute. */
23710 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23711 pedwarn (input_location
, OPT_Wpedantic
,
23712 "ISO C++ allows bit-field attributes only "
23713 "before the %<:%> token");
23715 late_attributes
= cp_parser_attributes_opt (parser
);
23718 attributes
= attr_chainon (attributes
, late_attributes
);
23720 /* Remember which attributes are prefix attributes and
23722 first_attribute
= attributes
;
23723 /* Combine the attributes. */
23724 attributes
= attr_chainon (prefix_attributes
, attributes
);
23726 /* Create the bitfield declaration. */
23727 decl
= grokbitfield (identifier
23728 ? make_id_declarator (NULL_TREE
,
23733 width
, initializer
,
23738 cp_declarator
*declarator
;
23739 tree asm_specification
;
23740 int ctor_dtor_or_conv_p
;
23742 /* Parse the declarator. */
23744 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23745 &ctor_dtor_or_conv_p
,
23746 /*parenthesized_p=*/NULL
,
23750 /* If something went wrong parsing the declarator, make sure
23751 that we at least consume some tokens. */
23752 if (declarator
== cp_error_declarator
)
23754 /* Skip to the end of the statement. */
23755 cp_parser_skip_to_end_of_statement (parser
);
23756 /* If the next token is not a semicolon, that is
23757 probably because we just skipped over the body of
23758 a function. So, we consume a semicolon if
23759 present, but do not issue an error message if it
23761 if (cp_lexer_next_token_is (parser
->lexer
,
23763 cp_lexer_consume_token (parser
->lexer
);
23767 if (declares_class_or_enum
& 2)
23768 cp_parser_check_for_definition_in_return_type
23769 (declarator
, decl_specifiers
.type
,
23770 decl_specifiers
.locations
[ds_type_spec
]);
23772 /* Look for an asm-specification. */
23773 asm_specification
= cp_parser_asm_specification_opt (parser
);
23774 /* Look for attributes that apply to the declaration. */
23775 attributes
= cp_parser_attributes_opt (parser
);
23776 /* Remember which attributes are prefix attributes and
23778 first_attribute
= attributes
;
23779 /* Combine the attributes. */
23780 attributes
= attr_chainon (prefix_attributes
, attributes
);
23782 /* If it's an `=', then we have a constant-initializer or a
23783 pure-specifier. It is not correct to parse the
23784 initializer before registering the member declaration
23785 since the member declaration should be in scope while
23786 its initializer is processed. However, the rest of the
23787 front end does not yet provide an interface that allows
23788 us to handle this correctly. */
23789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23793 A pure-specifier shall be used only in the declaration of
23794 a virtual function.
23796 A member-declarator can contain a constant-initializer
23797 only if it declares a static member of integral or
23800 Therefore, if the DECLARATOR is for a function, we look
23801 for a pure-specifier; otherwise, we look for a
23802 constant-initializer. When we call `grokfield', it will
23803 perform more stringent semantics checks. */
23804 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23805 if (function_declarator_p (declarator
)
23806 || (decl_specifiers
.type
23807 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23808 && declarator
->kind
== cdk_id
23809 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23810 == FUNCTION_TYPE
)))
23811 initializer
= cp_parser_pure_specifier (parser
);
23812 else if (decl_specifiers
.storage_class
!= sc_static
)
23813 initializer
= cp_parser_save_nsdmi (parser
);
23814 else if (cxx_dialect
>= cxx11
)
23817 /* Don't require a constant rvalue in C++11, since we
23818 might want a reference constant. We'll enforce
23819 constancy later. */
23820 cp_lexer_consume_token (parser
->lexer
);
23821 /* Parse the initializer. */
23822 initializer
= cp_parser_initializer_clause (parser
,
23826 /* Parse the initializer. */
23827 initializer
= cp_parser_constant_initializer (parser
);
23829 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23830 && !function_declarator_p (declarator
))
23833 if (decl_specifiers
.storage_class
!= sc_static
)
23834 initializer
= cp_parser_save_nsdmi (parser
);
23836 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23838 /* Otherwise, there is no initializer. */
23840 initializer
= NULL_TREE
;
23842 /* See if we are probably looking at a function
23843 definition. We are certainly not looking at a
23844 member-declarator. Calling `grokfield' has
23845 side-effects, so we must not do it unless we are sure
23846 that we are looking at a member-declarator. */
23847 if (cp_parser_token_starts_function_definition_p
23848 (cp_lexer_peek_token (parser
->lexer
)))
23850 /* The grammar does not allow a pure-specifier to be
23851 used when a member function is defined. (It is
23852 possible that this fact is an oversight in the
23853 standard, since a pure function may be defined
23854 outside of the class-specifier. */
23855 if (initializer
&& initializer_token_start
)
23856 error_at (initializer_token_start
->location
,
23857 "pure-specifier on function-definition");
23858 decl
= cp_parser_save_member_function_body (parser
,
23862 if (parser
->fully_implicit_function_template_p
)
23863 decl
= finish_fully_implicit_template (parser
, decl
);
23864 /* If the member was not a friend, declare it here. */
23866 finish_member_declaration (decl
);
23867 /* Peek at the next token. */
23868 token
= cp_lexer_peek_token (parser
->lexer
);
23869 /* If the next token is a semicolon, consume it. */
23870 if (token
->type
== CPP_SEMICOLON
)
23872 location_t semicolon_loc
23873 = cp_lexer_consume_token (parser
->lexer
)->location
;
23874 gcc_rich_location
richloc (semicolon_loc
);
23875 richloc
.add_fixit_remove ();
23876 warning_at (&richloc
, OPT_Wextra_semi
,
23877 "extra %<;%> after in-class "
23878 "function definition");
23883 if (declarator
->kind
== cdk_function
)
23884 declarator
->id_loc
= token
->location
;
23885 /* Create the declaration. */
23886 decl
= grokfield (declarator
, &decl_specifiers
,
23887 initializer
, /*init_const_expr_p=*/true,
23888 asm_specification
, attributes
);
23889 if (parser
->fully_implicit_function_template_p
)
23892 finish_fully_implicit_template (parser
, 0);
23894 decl
= finish_fully_implicit_template (parser
, decl
);
23898 cp_finalize_omp_declare_simd (parser
, decl
);
23899 cp_finalize_oacc_routine (parser
, decl
, false);
23901 /* Reset PREFIX_ATTRIBUTES. */
23902 if (attributes
!= error_mark_node
)
23904 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23905 attributes
= TREE_CHAIN (attributes
);
23907 TREE_CHAIN (attributes
) = NULL_TREE
;
23910 /* If there is any qualification still in effect, clear it
23911 now; we will be starting fresh with the next declarator. */
23912 parser
->scope
= NULL_TREE
;
23913 parser
->qualifying_scope
= NULL_TREE
;
23914 parser
->object_scope
= NULL_TREE
;
23915 /* If it's a `,', then there are more declarators. */
23916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23918 cp_lexer_consume_token (parser
->lexer
);
23919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23921 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23922 gcc_rich_location
richloc (token
->location
);
23923 richloc
.add_fixit_remove ();
23924 error_at (&richloc
, "stray %<,%> at end of "
23925 "member declaration");
23928 /* If the next token isn't a `;', then we have a parse error. */
23929 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23932 /* The next token might be a ways away from where the
23933 actual semicolon is missing. Find the previous token
23934 and use that for our error position. */
23935 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23936 gcc_rich_location
richloc (token
->location
);
23937 richloc
.add_fixit_insert_after (";");
23938 error_at (&richloc
, "expected %<;%> at end of "
23939 "member declaration");
23941 /* Assume that the user meant to provide a semicolon. If
23942 we were to cp_parser_skip_to_end_of_statement, we might
23943 skip to a semicolon inside a member function definition
23944 and issue nonsensical error messages. */
23945 assume_semicolon
= true;
23950 /* Add DECL to the list of members. */
23952 /* Explicitly include, eg, NSDMIs, for better error
23953 recovery (c++/58650). */
23954 || !DECL_DECLARES_FUNCTION_P (decl
))
23955 finish_member_declaration (decl
);
23957 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23958 cp_parser_save_default_args (parser
, decl
);
23959 else if (TREE_CODE (decl
) == FIELD_DECL
23960 && DECL_INITIAL (decl
))
23961 /* Add DECL to the queue of NSDMI to be parsed later. */
23962 vec_safe_push (unparsed_nsdmis
, decl
);
23965 if (assume_semicolon
)
23970 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23972 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23975 /* Parse a pure-specifier.
23980 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23981 Otherwise, ERROR_MARK_NODE is returned. */
23984 cp_parser_pure_specifier (cp_parser
* parser
)
23988 /* Look for the `=' token. */
23989 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23990 return error_mark_node
;
23991 /* Look for the `0' token. */
23992 token
= cp_lexer_peek_token (parser
->lexer
);
23994 if (token
->type
== CPP_EOF
23995 || token
->type
== CPP_PRAGMA_EOL
)
23996 return error_mark_node
;
23998 cp_lexer_consume_token (parser
->lexer
);
24000 /* Accept = default or = delete in c++0x mode. */
24001 if (token
->keyword
== RID_DEFAULT
24002 || token
->keyword
== RID_DELETE
)
24004 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24005 return token
->u
.value
;
24008 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24009 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24011 cp_parser_error (parser
,
24012 "invalid pure specifier (only %<= 0%> is allowed)");
24013 cp_parser_skip_to_end_of_statement (parser
);
24014 return error_mark_node
;
24016 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24018 error_at (token
->location
, "templates may not be %<virtual%>");
24019 return error_mark_node
;
24022 return integer_zero_node
;
24025 /* Parse a constant-initializer.
24027 constant-initializer:
24028 = constant-expression
24030 Returns a representation of the constant-expression. */
24033 cp_parser_constant_initializer (cp_parser
* parser
)
24035 /* Look for the `=' token. */
24036 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24037 return error_mark_node
;
24039 /* It is invalid to write:
24041 struct S { static const int i = { 7 }; };
24044 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24046 cp_parser_error (parser
,
24047 "a brace-enclosed initializer is not allowed here");
24048 /* Consume the opening brace. */
24049 matching_braces braces
;
24050 braces
.consume_open (parser
);
24051 /* Skip the initializer. */
24052 cp_parser_skip_to_closing_brace (parser
);
24053 /* Look for the trailing `}'. */
24054 braces
.require_close (parser
);
24056 return error_mark_node
;
24059 return cp_parser_constant_expression (parser
);
24062 /* Derived classes [gram.class.derived] */
24064 /* Parse a base-clause.
24067 : base-specifier-list
24069 base-specifier-list:
24070 base-specifier ... [opt]
24071 base-specifier-list , base-specifier ... [opt]
24073 Returns a TREE_LIST representing the base-classes, in the order in
24074 which they were declared. The representation of each node is as
24075 described by cp_parser_base_specifier.
24077 In the case that no bases are specified, this function will return
24078 NULL_TREE, not ERROR_MARK_NODE. */
24081 cp_parser_base_clause (cp_parser
* parser
)
24083 tree bases
= NULL_TREE
;
24085 /* Look for the `:' that begins the list. */
24086 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24088 /* Scan the base-specifier-list. */
24093 bool pack_expansion_p
= false;
24095 /* Look for the base-specifier. */
24096 base
= cp_parser_base_specifier (parser
);
24097 /* Look for the (optional) ellipsis. */
24098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24100 /* Consume the `...'. */
24101 cp_lexer_consume_token (parser
->lexer
);
24103 pack_expansion_p
= true;
24106 /* Add BASE to the front of the list. */
24107 if (base
&& base
!= error_mark_node
)
24109 if (pack_expansion_p
)
24110 /* Make this a pack expansion type. */
24111 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24113 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24115 TREE_CHAIN (base
) = bases
;
24119 /* Peek at the next token. */
24120 token
= cp_lexer_peek_token (parser
->lexer
);
24121 /* If it's not a comma, then the list is complete. */
24122 if (token
->type
!= CPP_COMMA
)
24124 /* Consume the `,'. */
24125 cp_lexer_consume_token (parser
->lexer
);
24128 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24129 base class had a qualified name. However, the next name that
24130 appears is certainly not qualified. */
24131 parser
->scope
= NULL_TREE
;
24132 parser
->qualifying_scope
= NULL_TREE
;
24133 parser
->object_scope
= NULL_TREE
;
24135 return nreverse (bases
);
24138 /* Parse a base-specifier.
24141 :: [opt] nested-name-specifier [opt] class-name
24142 virtual access-specifier [opt] :: [opt] nested-name-specifier
24144 access-specifier virtual [opt] :: [opt] nested-name-specifier
24147 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24148 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24149 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24150 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24153 cp_parser_base_specifier (cp_parser
* parser
)
24157 bool virtual_p
= false;
24158 bool duplicate_virtual_error_issued_p
= false;
24159 bool duplicate_access_error_issued_p
= false;
24160 bool class_scope_p
, template_p
;
24161 tree access
= access_default_node
;
24164 /* Process the optional `virtual' and `access-specifier'. */
24167 /* Peek at the next token. */
24168 token
= cp_lexer_peek_token (parser
->lexer
);
24169 /* Process `virtual'. */
24170 switch (token
->keyword
)
24173 /* If `virtual' appears more than once, issue an error. */
24174 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24176 cp_parser_error (parser
,
24177 "%<virtual%> specified more than once in base-specifier");
24178 duplicate_virtual_error_issued_p
= true;
24183 /* Consume the `virtual' token. */
24184 cp_lexer_consume_token (parser
->lexer
);
24189 case RID_PROTECTED
:
24191 /* If more than one access specifier appears, issue an
24193 if (access
!= access_default_node
24194 && !duplicate_access_error_issued_p
)
24196 cp_parser_error (parser
,
24197 "more than one access specifier in base-specifier");
24198 duplicate_access_error_issued_p
= true;
24201 access
= ridpointers
[(int) token
->keyword
];
24203 /* Consume the access-specifier. */
24204 cp_lexer_consume_token (parser
->lexer
);
24213 /* It is not uncommon to see programs mechanically, erroneously, use
24214 the 'typename' keyword to denote (dependent) qualified types
24215 as base classes. */
24216 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24218 token
= cp_lexer_peek_token (parser
->lexer
);
24219 if (!processing_template_decl
)
24220 error_at (token
->location
,
24221 "keyword %<typename%> not allowed outside of templates");
24223 error_at (token
->location
,
24224 "keyword %<typename%> not allowed in this context "
24225 "(the base class is implicitly a type)");
24226 cp_lexer_consume_token (parser
->lexer
);
24229 /* Look for the optional `::' operator. */
24230 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24231 /* Look for the nested-name-specifier. The simplest way to
24236 The keyword `typename' is not permitted in a base-specifier or
24237 mem-initializer; in these contexts a qualified name that
24238 depends on a template-parameter is implicitly assumed to be a
24241 is to pretend that we have seen the `typename' keyword at this
24243 cp_parser_nested_name_specifier_opt (parser
,
24244 /*typename_keyword_p=*/true,
24245 /*check_dependency_p=*/true,
24247 /*is_declaration=*/true);
24248 /* If the base class is given by a qualified name, assume that names
24249 we see are type names or templates, as appropriate. */
24250 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24251 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24254 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24255 /* DR 950 allows decltype as a base-specifier. */
24256 type
= cp_parser_decltype (parser
);
24259 /* Otherwise, look for the class-name. */
24260 type
= cp_parser_class_name (parser
,
24264 /*check_dependency_p=*/true,
24265 /*class_head_p=*/false,
24266 /*is_declaration=*/true);
24267 type
= TREE_TYPE (type
);
24270 if (type
== error_mark_node
)
24271 return error_mark_node
;
24273 return finish_base_specifier (type
, access
, virtual_p
);
24276 /* Exception handling [gram.exception] */
24278 /* Parse an (optional) noexcept-specification.
24280 noexcept-specification:
24281 noexcept ( constant-expression ) [opt]
24283 If no noexcept-specification is present, returns NULL_TREE.
24284 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24285 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24286 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24287 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24288 in which case a boolean condition is returned instead. */
24291 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24292 bool require_constexpr
,
24293 bool* consumed_expr
,
24297 const char *saved_message
;
24299 /* Peek at the next token. */
24300 token
= cp_lexer_peek_token (parser
->lexer
);
24302 /* Is it a noexcept-specification? */
24303 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24306 cp_lexer_consume_token (parser
->lexer
);
24308 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24310 matching_parens parens
;
24311 parens
.consume_open (parser
);
24313 if (require_constexpr
)
24315 /* Types may not be defined in an exception-specification. */
24316 saved_message
= parser
->type_definition_forbidden_message
;
24317 parser
->type_definition_forbidden_message
24318 = G_("types may not be defined in an exception-specification");
24320 expr
= cp_parser_constant_expression (parser
);
24322 /* Restore the saved message. */
24323 parser
->type_definition_forbidden_message
= saved_message
;
24327 expr
= cp_parser_expression (parser
);
24328 *consumed_expr
= true;
24331 parens
.require_close (parser
);
24335 expr
= boolean_true_node
;
24336 if (!require_constexpr
)
24337 *consumed_expr
= false;
24340 /* We cannot build a noexcept-spec right away because this will check
24341 that expr is a constexpr. */
24343 return build_noexcept_spec (expr
, tf_warning_or_error
);
24351 /* Parse an (optional) exception-specification.
24353 exception-specification:
24354 throw ( type-id-list [opt] )
24356 Returns a TREE_LIST representing the exception-specification. The
24357 TREE_VALUE of each node is a type. */
24360 cp_parser_exception_specification_opt (cp_parser
* parser
)
24364 const char *saved_message
;
24366 /* Peek at the next token. */
24367 token
= cp_lexer_peek_token (parser
->lexer
);
24369 /* Is it a noexcept-specification? */
24370 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24372 if (type_id_list
!= NULL_TREE
)
24373 return type_id_list
;
24375 /* If it's not `throw', then there's no exception-specification. */
24376 if (!cp_parser_is_keyword (token
, RID_THROW
))
24379 location_t loc
= token
->location
;
24381 /* Consume the `throw'. */
24382 cp_lexer_consume_token (parser
->lexer
);
24384 /* Look for the `('. */
24385 matching_parens parens
;
24386 parens
.require_open (parser
);
24388 /* Peek at the next token. */
24389 token
= cp_lexer_peek_token (parser
->lexer
);
24390 /* If it's not a `)', then there is a type-id-list. */
24391 if (token
->type
!= CPP_CLOSE_PAREN
)
24393 /* Types may not be defined in an exception-specification. */
24394 saved_message
= parser
->type_definition_forbidden_message
;
24395 parser
->type_definition_forbidden_message
24396 = G_("types may not be defined in an exception-specification");
24397 /* Parse the type-id-list. */
24398 type_id_list
= cp_parser_type_id_list (parser
);
24399 /* Restore the saved message. */
24400 parser
->type_definition_forbidden_message
= saved_message
;
24402 if (cxx_dialect
>= cxx17
)
24404 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24406 type_id_list
= NULL_TREE
;
24408 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24409 warning_at (loc
, OPT_Wdeprecated
,
24410 "dynamic exception specifications are deprecated in "
24413 /* In C++17, throw() is equivalent to noexcept (true). throw()
24414 is deprecated in C++11 and above as well, but is still widely used,
24415 so don't warn about it yet. */
24416 else if (cxx_dialect
>= cxx17
)
24417 type_id_list
= noexcept_true_spec
;
24419 type_id_list
= empty_except_spec
;
24421 /* Look for the `)'. */
24422 parens
.require_close (parser
);
24424 return type_id_list
;
24427 /* Parse an (optional) type-id-list.
24431 type-id-list , type-id ... [opt]
24433 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24434 in the order that the types were presented. */
24437 cp_parser_type_id_list (cp_parser
* parser
)
24439 tree types
= NULL_TREE
;
24446 token
= cp_lexer_peek_token (parser
->lexer
);
24448 /* Get the next type-id. */
24449 type
= cp_parser_type_id (parser
);
24450 /* Check for invalid 'auto'. */
24451 if (flag_concepts
&& type_uses_auto (type
))
24453 error_at (token
->location
,
24454 "invalid use of %<auto%> in exception-specification");
24455 type
= error_mark_node
;
24457 /* Parse the optional ellipsis. */
24458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24460 /* Consume the `...'. */
24461 cp_lexer_consume_token (parser
->lexer
);
24463 /* Turn the type into a pack expansion expression. */
24464 type
= make_pack_expansion (type
);
24466 /* Add it to the list. */
24467 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24468 /* Peek at the next token. */
24469 token
= cp_lexer_peek_token (parser
->lexer
);
24470 /* If it is not a `,', we are done. */
24471 if (token
->type
!= CPP_COMMA
)
24473 /* Consume the `,'. */
24474 cp_lexer_consume_token (parser
->lexer
);
24477 return nreverse (types
);
24480 /* Parse a try-block.
24483 try compound-statement handler-seq */
24486 cp_parser_try_block (cp_parser
* parser
)
24490 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24491 if (parser
->in_function_body
24492 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24493 error ("%<try%> in %<constexpr%> function");
24495 try_block
= begin_try_block ();
24496 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24497 finish_try_block (try_block
);
24498 cp_parser_handler_seq (parser
);
24499 finish_handler_sequence (try_block
);
24504 /* Parse a function-try-block.
24506 function-try-block:
24507 try ctor-initializer [opt] function-body handler-seq */
24510 cp_parser_function_try_block (cp_parser
* parser
)
24512 tree compound_stmt
;
24515 /* Look for the `try' keyword. */
24516 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24518 /* Let the rest of the front end know where we are. */
24519 try_block
= begin_function_try_block (&compound_stmt
);
24520 /* Parse the function-body. */
24521 cp_parser_ctor_initializer_opt_and_function_body
24522 (parser
, /*in_function_try_block=*/true);
24523 /* We're done with the `try' part. */
24524 finish_function_try_block (try_block
);
24525 /* Parse the handlers. */
24526 cp_parser_handler_seq (parser
);
24527 /* We're done with the handlers. */
24528 finish_function_handler_sequence (try_block
, compound_stmt
);
24531 /* Parse a handler-seq.
24534 handler handler-seq [opt] */
24537 cp_parser_handler_seq (cp_parser
* parser
)
24543 /* Parse the handler. */
24544 cp_parser_handler (parser
);
24545 /* Peek at the next token. */
24546 token
= cp_lexer_peek_token (parser
->lexer
);
24547 /* If it's not `catch' then there are no more handlers. */
24548 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24553 /* Parse a handler.
24556 catch ( exception-declaration ) compound-statement */
24559 cp_parser_handler (cp_parser
* parser
)
24564 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24565 handler
= begin_handler ();
24566 matching_parens parens
;
24567 parens
.require_open (parser
);
24568 declaration
= cp_parser_exception_declaration (parser
);
24569 finish_handler_parms (declaration
, handler
);
24570 parens
.require_close (parser
);
24571 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24572 finish_handler (handler
);
24575 /* Parse an exception-declaration.
24577 exception-declaration:
24578 type-specifier-seq declarator
24579 type-specifier-seq abstract-declarator
24583 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24584 ellipsis variant is used. */
24587 cp_parser_exception_declaration (cp_parser
* parser
)
24589 cp_decl_specifier_seq type_specifiers
;
24590 cp_declarator
*declarator
;
24591 const char *saved_message
;
24593 /* If it's an ellipsis, it's easy to handle. */
24594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24596 /* Consume the `...' token. */
24597 cp_lexer_consume_token (parser
->lexer
);
24601 /* Types may not be defined in exception-declarations. */
24602 saved_message
= parser
->type_definition_forbidden_message
;
24603 parser
->type_definition_forbidden_message
24604 = G_("types may not be defined in exception-declarations");
24606 /* Parse the type-specifier-seq. */
24607 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24608 /*is_trailing_return=*/false,
24610 /* If it's a `)', then there is no declarator. */
24611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24614 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24615 /*ctor_dtor_or_conv_p=*/NULL
,
24616 /*parenthesized_p=*/NULL
,
24617 /*member_p=*/false,
24618 /*friend_p=*/false);
24620 /* Restore the saved message. */
24621 parser
->type_definition_forbidden_message
= saved_message
;
24623 if (!type_specifiers
.any_specifiers_p
)
24624 return error_mark_node
;
24626 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24629 /* Parse a throw-expression.
24632 throw assignment-expression [opt]
24634 Returns a THROW_EXPR representing the throw-expression. */
24637 cp_parser_throw_expression (cp_parser
* parser
)
24642 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24643 token
= cp_lexer_peek_token (parser
->lexer
);
24644 /* Figure out whether or not there is an assignment-expression
24645 following the "throw" keyword. */
24646 if (token
->type
== CPP_COMMA
24647 || token
->type
== CPP_SEMICOLON
24648 || token
->type
== CPP_CLOSE_PAREN
24649 || token
->type
== CPP_CLOSE_SQUARE
24650 || token
->type
== CPP_CLOSE_BRACE
24651 || token
->type
== CPP_COLON
)
24652 expression
= NULL_TREE
;
24654 expression
= cp_parser_assignment_expression (parser
);
24656 return build_throw (expression
);
24659 /* GNU Extensions */
24661 /* Parse an (optional) asm-specification.
24664 asm ( string-literal )
24666 If the asm-specification is present, returns a STRING_CST
24667 corresponding to the string-literal. Otherwise, returns
24671 cp_parser_asm_specification_opt (cp_parser
* parser
)
24674 tree asm_specification
;
24676 /* Peek at the next token. */
24677 token
= cp_lexer_peek_token (parser
->lexer
);
24678 /* If the next token isn't the `asm' keyword, then there's no
24679 asm-specification. */
24680 if (!cp_parser_is_keyword (token
, RID_ASM
))
24683 /* Consume the `asm' token. */
24684 cp_lexer_consume_token (parser
->lexer
);
24685 /* Look for the `('. */
24686 matching_parens parens
;
24687 parens
.require_open (parser
);
24689 /* Look for the string-literal. */
24690 asm_specification
= cp_parser_string_literal (parser
, false, false);
24692 /* Look for the `)'. */
24693 parens
.require_close (parser
);
24695 return asm_specification
;
24698 /* Parse an asm-operand-list.
24702 asm-operand-list , asm-operand
24705 string-literal ( expression )
24706 [ string-literal ] string-literal ( expression )
24708 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24709 each node is the expression. The TREE_PURPOSE is itself a
24710 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24711 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24712 is a STRING_CST for the string literal before the parenthesis. Returns
24713 ERROR_MARK_NODE if any of the operands are invalid. */
24716 cp_parser_asm_operand_list (cp_parser
* parser
)
24718 tree asm_operands
= NULL_TREE
;
24719 bool invalid_operands
= false;
24723 tree string_literal
;
24727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24729 /* Consume the `[' token. */
24730 cp_lexer_consume_token (parser
->lexer
);
24731 /* Read the operand name. */
24732 name
= cp_parser_identifier (parser
);
24733 if (name
!= error_mark_node
)
24734 name
= build_string (IDENTIFIER_LENGTH (name
),
24735 IDENTIFIER_POINTER (name
));
24736 /* Look for the closing `]'. */
24737 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24741 /* Look for the string-literal. */
24742 string_literal
= cp_parser_string_literal (parser
, false, false);
24744 /* Look for the `('. */
24745 matching_parens parens
;
24746 parens
.require_open (parser
);
24747 /* Parse the expression. */
24748 expression
= cp_parser_expression (parser
);
24749 /* Look for the `)'. */
24750 parens
.require_close (parser
);
24752 if (name
== error_mark_node
24753 || string_literal
== error_mark_node
24754 || expression
== error_mark_node
)
24755 invalid_operands
= true;
24757 /* Add this operand to the list. */
24758 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24761 /* If the next token is not a `,', there are no more
24763 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24765 /* Consume the `,'. */
24766 cp_lexer_consume_token (parser
->lexer
);
24769 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24772 /* Parse an asm-clobber-list.
24776 asm-clobber-list , string-literal
24778 Returns a TREE_LIST, indicating the clobbers in the order that they
24779 appeared. The TREE_VALUE of each node is a STRING_CST. */
24782 cp_parser_asm_clobber_list (cp_parser
* parser
)
24784 tree clobbers
= NULL_TREE
;
24788 tree string_literal
;
24790 /* Look for the string literal. */
24791 string_literal
= cp_parser_string_literal (parser
, false, false);
24792 /* Add it to the list. */
24793 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24794 /* If the next token is not a `,', then the list is
24796 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24798 /* Consume the `,' token. */
24799 cp_lexer_consume_token (parser
->lexer
);
24805 /* Parse an asm-label-list.
24809 asm-label-list , identifier
24811 Returns a TREE_LIST, indicating the labels in the order that they
24812 appeared. The TREE_VALUE of each node is a label. */
24815 cp_parser_asm_label_list (cp_parser
* parser
)
24817 tree labels
= NULL_TREE
;
24821 tree identifier
, label
, name
;
24823 /* Look for the identifier. */
24824 identifier
= cp_parser_identifier (parser
);
24825 if (!error_operand_p (identifier
))
24827 label
= lookup_label (identifier
);
24828 if (TREE_CODE (label
) == LABEL_DECL
)
24830 TREE_USED (label
) = 1;
24831 check_goto (label
);
24832 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24833 IDENTIFIER_POINTER (identifier
));
24834 labels
= tree_cons (name
, label
, labels
);
24837 /* If the next token is not a `,', then the list is
24839 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24841 /* Consume the `,' token. */
24842 cp_lexer_consume_token (parser
->lexer
);
24845 return nreverse (labels
);
24848 /* Return TRUE iff the next tokens in the stream are possibly the
24849 beginning of a GNU extension attribute. */
24852 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24854 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24857 /* Return TRUE iff the next tokens in the stream are possibly the
24858 beginning of a standard C++-11 attribute specifier. */
24861 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24863 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24866 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24867 beginning of a standard C++-11 attribute specifier. */
24870 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24872 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24874 return (cxx_dialect
>= cxx11
24875 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24876 || (token
->type
== CPP_OPEN_SQUARE
24877 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24878 && token
->type
== CPP_OPEN_SQUARE
)));
24881 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24882 beginning of a GNU extension attribute. */
24885 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24887 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24889 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24892 /* Return true iff the next tokens can be the beginning of either a
24893 GNU attribute list, or a standard C++11 attribute sequence. */
24896 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24898 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24899 || cp_next_tokens_can_be_std_attribute_p (parser
));
24902 /* Return true iff the next Nth tokens can be the beginning of either
24903 a GNU attribute list, or a standard C++11 attribute sequence. */
24906 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24908 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24909 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24912 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24913 of GNU attributes, or return NULL. */
24916 cp_parser_attributes_opt (cp_parser
*parser
)
24918 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24919 return cp_parser_gnu_attributes_opt (parser
);
24920 return cp_parser_std_attribute_spec_seq (parser
);
24923 /* Parse an (optional) series of attributes.
24926 attributes attribute
24929 __attribute__ (( attribute-list [opt] ))
24931 The return value is as for cp_parser_gnu_attribute_list. */
24934 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24936 tree attributes
= NULL_TREE
;
24941 tree attribute_list
;
24944 /* Peek at the next token. */
24945 token
= cp_lexer_peek_token (parser
->lexer
);
24946 /* If it's not `__attribute__', then we're done. */
24947 if (token
->keyword
!= RID_ATTRIBUTE
)
24950 /* Consume the `__attribute__' keyword. */
24951 cp_lexer_consume_token (parser
->lexer
);
24952 /* Look for the two `(' tokens. */
24953 matching_parens outer_parens
;
24954 outer_parens
.require_open (parser
);
24955 matching_parens inner_parens
;
24956 inner_parens
.require_open (parser
);
24958 /* Peek at the next token. */
24959 token
= cp_lexer_peek_token (parser
->lexer
);
24960 if (token
->type
!= CPP_CLOSE_PAREN
)
24961 /* Parse the attribute-list. */
24962 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24964 /* If the next token is a `)', then there is no attribute
24966 attribute_list
= NULL
;
24968 /* Look for the two `)' tokens. */
24969 if (!inner_parens
.require_close (parser
))
24971 if (!outer_parens
.require_close (parser
))
24974 cp_parser_skip_to_end_of_statement (parser
);
24976 /* Add these new attributes to the list. */
24977 attributes
= attr_chainon (attributes
, attribute_list
);
24983 /* Parse a GNU attribute-list.
24987 attribute-list , attribute
24991 identifier ( identifier )
24992 identifier ( identifier , expression-list )
24993 identifier ( expression-list )
24995 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24996 to an attribute. The TREE_PURPOSE of each node is the identifier
24997 indicating which attribute is in use. The TREE_VALUE represents
24998 the arguments, if any. */
25001 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25003 tree attribute_list
= NULL_TREE
;
25004 bool save_translate_strings_p
= parser
->translate_strings_p
;
25006 parser
->translate_strings_p
= false;
25013 /* Look for the identifier. We also allow keywords here; for
25014 example `__attribute__ ((const))' is legal. */
25015 token
= cp_lexer_peek_token (parser
->lexer
);
25016 if (token
->type
== CPP_NAME
25017 || token
->type
== CPP_KEYWORD
)
25019 tree arguments
= NULL_TREE
;
25021 /* Consume the token, but save it since we need it for the
25022 SIMD enabled function parsing. */
25023 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25025 /* Save away the identifier that indicates which attribute
25027 identifier
= (token
->type
== CPP_KEYWORD
)
25028 /* For keywords, use the canonical spelling, not the
25029 parsed identifier. */
25030 ? ridpointers
[(int) token
->keyword
]
25031 : id_token
->u
.value
;
25033 identifier
= canonicalize_attr_name (identifier
);
25034 attribute
= build_tree_list (identifier
, NULL_TREE
);
25036 /* Peek at the next token. */
25037 token
= cp_lexer_peek_token (parser
->lexer
);
25038 /* If it's an `(', then parse the attribute arguments. */
25039 if (token
->type
== CPP_OPEN_PAREN
)
25041 vec
<tree
, va_gc
> *vec
;
25042 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25043 ? id_attr
: normal_attr
);
25044 vec
= cp_parser_parenthesized_expression_list
25045 (parser
, attr_flag
, /*cast_p=*/false,
25046 /*allow_expansion_p=*/false,
25047 /*non_constant_p=*/NULL
);
25049 arguments
= error_mark_node
;
25052 arguments
= build_tree_list_vec (vec
);
25053 release_tree_vector (vec
);
25055 /* Save the arguments away. */
25056 TREE_VALUE (attribute
) = arguments
;
25059 if (arguments
!= error_mark_node
)
25061 /* Add this attribute to the list. */
25062 TREE_CHAIN (attribute
) = attribute_list
;
25063 attribute_list
= attribute
;
25066 token
= cp_lexer_peek_token (parser
->lexer
);
25068 /* Now, look for more attributes. If the next token isn't a
25069 `,', we're done. */
25070 if (token
->type
!= CPP_COMMA
)
25073 /* Consume the comma and keep going. */
25074 cp_lexer_consume_token (parser
->lexer
);
25076 parser
->translate_strings_p
= save_translate_strings_p
;
25078 /* We built up the list in reverse order. */
25079 return nreverse (attribute_list
);
25082 /* Parse a standard C++11 attribute.
25084 The returned representation is a TREE_LIST which TREE_PURPOSE is
25085 the scoped name of the attribute, and the TREE_VALUE is its
25088 Note that the scoped name of the attribute is itself a TREE_LIST
25089 which TREE_PURPOSE is the namespace of the attribute, and
25090 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25091 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25092 and which TREE_PURPOSE is directly the attribute name.
25094 Clients of the attribute code should use get_attribute_namespace
25095 and get_attribute_name to get the actual namespace and name of
25096 attributes, regardless of their being GNU or C++11 attributes.
25099 attribute-token attribute-argument-clause [opt]
25103 attribute-scoped-token
25105 attribute-scoped-token:
25106 attribute-namespace :: identifier
25108 attribute-namespace:
25111 attribute-argument-clause:
25112 ( balanced-token-seq )
25114 balanced-token-seq:
25115 balanced-token [opt]
25116 balanced-token-seq balanced-token
25119 ( balanced-token-seq )
25120 [ balanced-token-seq ]
25121 { balanced-token-seq }. */
25124 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25126 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25129 /* First, parse name of the attribute, a.k.a attribute-token. */
25131 token
= cp_lexer_peek_token (parser
->lexer
);
25132 if (token
->type
== CPP_NAME
)
25133 attr_id
= token
->u
.value
;
25134 else if (token
->type
== CPP_KEYWORD
)
25135 attr_id
= ridpointers
[(int) token
->keyword
];
25136 else if (token
->flags
& NAMED_OP
)
25137 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25139 if (attr_id
== NULL_TREE
)
25142 cp_lexer_consume_token (parser
->lexer
);
25144 token
= cp_lexer_peek_token (parser
->lexer
);
25145 if (token
->type
== CPP_SCOPE
)
25147 /* We are seeing a scoped attribute token. */
25149 cp_lexer_consume_token (parser
->lexer
);
25151 error_at (token
->location
, "attribute using prefix used together "
25152 "with scoped attribute token");
25155 token
= cp_lexer_consume_token (parser
->lexer
);
25156 if (token
->type
== CPP_NAME
)
25157 attr_id
= token
->u
.value
;
25158 else if (token
->type
== CPP_KEYWORD
)
25159 attr_id
= ridpointers
[(int) token
->keyword
];
25160 else if (token
->flags
& NAMED_OP
)
25161 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25164 error_at (token
->location
,
25165 "expected an identifier for the attribute name");
25166 return error_mark_node
;
25169 attr_id
= canonicalize_attr_name (attr_id
);
25170 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25172 token
= cp_lexer_peek_token (parser
->lexer
);
25175 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25179 attr_id
= canonicalize_attr_name (attr_id
);
25180 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25182 /* C++11 noreturn attribute is equivalent to GNU's. */
25183 if (is_attribute_p ("noreturn", attr_id
))
25184 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25185 /* C++14 deprecated attribute is equivalent to GNU's. */
25186 else if (is_attribute_p ("deprecated", attr_id
))
25187 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25188 /* C++17 fallthrough attribute is equivalent to GNU's. */
25189 else if (is_attribute_p ("fallthrough", attr_id
))
25190 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25191 /* Transactional Memory TS optimize_for_synchronized attribute is
25192 equivalent to GNU transaction_callable. */
25193 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25194 TREE_PURPOSE (attribute
)
25195 = get_identifier ("transaction_callable");
25196 /* Transactional Memory attributes are GNU attributes. */
25197 else if (tm_attr_to_mask (attr_id
))
25198 TREE_PURPOSE (attribute
) = attr_id
;
25201 /* Now parse the optional argument clause of the attribute. */
25203 if (token
->type
!= CPP_OPEN_PAREN
)
25207 vec
<tree
, va_gc
> *vec
;
25208 int attr_flag
= normal_attr
;
25210 if (attr_ns
== get_identifier ("gnu")
25211 && attribute_takes_identifier_p (attr_id
))
25212 /* A GNU attribute that takes an identifier in parameter. */
25213 attr_flag
= id_attr
;
25215 vec
= cp_parser_parenthesized_expression_list
25216 (parser
, attr_flag
, /*cast_p=*/false,
25217 /*allow_expansion_p=*/true,
25218 /*non_constant_p=*/NULL
);
25220 arguments
= error_mark_node
;
25223 arguments
= build_tree_list_vec (vec
);
25224 release_tree_vector (vec
);
25227 if (arguments
== error_mark_node
)
25228 attribute
= error_mark_node
;
25230 TREE_VALUE (attribute
) = arguments
;
25236 /* Check that the attribute ATTRIBUTE appears at most once in the
25237 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25238 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25239 isn't implemented yet in GCC. */
25242 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25246 tree name
= get_attribute_name (attribute
);
25247 if (is_attribute_p ("noreturn", name
)
25248 && lookup_attribute ("noreturn", attributes
))
25249 error ("attribute %<noreturn%> can appear at most once "
25250 "in an attribute-list");
25251 else if (is_attribute_p ("deprecated", name
)
25252 && lookup_attribute ("deprecated", attributes
))
25253 error ("attribute %<deprecated%> can appear at most once "
25254 "in an attribute-list");
25258 /* Parse a list of standard C++-11 attributes.
25262 attribute-list , attribute[opt]
25264 attribute-list , attribute ...
25268 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25270 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25271 cp_token
*token
= NULL
;
25275 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25276 if (attribute
== error_mark_node
)
25278 if (attribute
!= NULL_TREE
)
25280 cp_parser_check_std_attribute (attributes
, attribute
);
25281 TREE_CHAIN (attribute
) = attributes
;
25282 attributes
= attribute
;
25284 token
= cp_lexer_peek_token (parser
->lexer
);
25285 if (token
->type
== CPP_ELLIPSIS
)
25287 cp_lexer_consume_token (parser
->lexer
);
25288 if (attribute
== NULL_TREE
)
25289 error_at (token
->location
,
25290 "expected attribute before %<...%>");
25293 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25294 if (pack
== error_mark_node
)
25295 return error_mark_node
;
25296 TREE_VALUE (attribute
) = pack
;
25298 token
= cp_lexer_peek_token (parser
->lexer
);
25300 if (token
->type
!= CPP_COMMA
)
25302 cp_lexer_consume_token (parser
->lexer
);
25304 attributes
= nreverse (attributes
);
25308 /* Parse a standard C++-11 attribute specifier.
25310 attribute-specifier:
25311 [ [ attribute-using-prefix [opt] attribute-list ] ]
25312 alignment-specifier
25314 attribute-using-prefix:
25315 using attribute-namespace :
25317 alignment-specifier:
25318 alignas ( type-id ... [opt] )
25319 alignas ( alignment-expression ... [opt] ). */
25322 cp_parser_std_attribute_spec (cp_parser
*parser
)
25324 tree attributes
= NULL_TREE
;
25325 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25327 if (token
->type
== CPP_OPEN_SQUARE
25328 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25330 tree attr_ns
= NULL_TREE
;
25332 cp_lexer_consume_token (parser
->lexer
);
25333 cp_lexer_consume_token (parser
->lexer
);
25335 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25337 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25338 if (token
->type
== CPP_NAME
)
25339 attr_ns
= token
->u
.value
;
25340 else if (token
->type
== CPP_KEYWORD
)
25341 attr_ns
= ridpointers
[(int) token
->keyword
];
25342 else if (token
->flags
& NAMED_OP
)
25343 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25346 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25348 if (cxx_dialect
< cxx17
25349 && !in_system_header_at (input_location
))
25350 pedwarn (input_location
, 0,
25351 "attribute using prefix only available "
25352 "with -std=c++17 or -std=gnu++17");
25354 cp_lexer_consume_token (parser
->lexer
);
25355 cp_lexer_consume_token (parser
->lexer
);
25356 cp_lexer_consume_token (parser
->lexer
);
25359 attr_ns
= NULL_TREE
;
25362 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25364 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25365 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25366 cp_parser_skip_to_end_of_statement (parser
);
25368 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25369 when we are sure that we have actually parsed them. */
25370 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25376 /* Look for an alignment-specifier. */
25378 token
= cp_lexer_peek_token (parser
->lexer
);
25380 if (token
->type
!= CPP_KEYWORD
25381 || token
->keyword
!= RID_ALIGNAS
)
25384 cp_lexer_consume_token (parser
->lexer
);
25385 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25387 matching_parens parens
;
25388 if (!parens
.require_open (parser
))
25389 return error_mark_node
;
25391 cp_parser_parse_tentatively (parser
);
25392 alignas_expr
= cp_parser_type_id (parser
);
25394 if (!cp_parser_parse_definitely (parser
))
25396 alignas_expr
= cp_parser_assignment_expression (parser
);
25397 if (alignas_expr
== error_mark_node
)
25398 cp_parser_skip_to_end_of_statement (parser
);
25399 if (alignas_expr
== NULL_TREE
25400 || alignas_expr
== error_mark_node
)
25401 return alignas_expr
;
25404 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25405 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25407 /* Handle alignas (pack...). */
25408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25410 cp_lexer_consume_token (parser
->lexer
);
25411 alignas_expr
= make_pack_expansion (alignas_expr
);
25414 /* Something went wrong, so don't build the attribute. */
25415 if (alignas_expr
== error_mark_node
)
25416 return error_mark_node
;
25418 if (!parens
.require_close (parser
))
25419 return error_mark_node
;
25421 /* Build the C++-11 representation of an 'aligned'
25424 build_tree_list (build_tree_list (get_identifier ("gnu"),
25425 get_identifier ("aligned")),
25432 /* Parse a standard C++-11 attribute-specifier-seq.
25434 attribute-specifier-seq:
25435 attribute-specifier-seq [opt] attribute-specifier
25439 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25441 tree attr_specs
= NULL_TREE
;
25442 tree attr_last
= NULL_TREE
;
25446 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25447 if (attr_spec
== NULL_TREE
)
25449 if (attr_spec
== error_mark_node
)
25450 return error_mark_node
;
25453 TREE_CHAIN (attr_last
) = attr_spec
;
25455 attr_specs
= attr_last
= attr_spec
;
25456 attr_last
= tree_last (attr_last
);
25462 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25463 return index of the first token after balanced-token, or N on failure. */
25466 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25469 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25471 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25474 case CPP_PRAGMA_EOL
:
25475 /* Ran out of tokens. */
25477 case CPP_OPEN_PAREN
:
25480 case CPP_OPEN_BRACE
:
25483 case CPP_OPEN_SQUARE
:
25486 case CPP_CLOSE_PAREN
:
25489 case CPP_CLOSE_BRACE
:
25492 case CPP_CLOSE_SQUARE
:
25498 while (nparens
|| nbraces
|| nsquares
);
25502 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25503 return index of the first token after the GNU attribute tokens, or N on
25507 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25511 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25512 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25513 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25516 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25519 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25526 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25527 next token), return index of the first token after the standard C++11
25528 attribute tokens, or N on failure. */
25531 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25535 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25536 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25538 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25541 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25545 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25546 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25548 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25559 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25560 as the next token), return index of the first token after the attribute
25561 tokens, or N on failure. */
25564 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25566 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25567 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25568 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25571 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25572 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25573 current value of the PEDANTIC flag, regardless of whether or not
25574 the `__extension__' keyword is present. The caller is responsible
25575 for restoring the value of the PEDANTIC flag. */
25578 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25580 /* Save the old value of the PEDANTIC flag. */
25581 *saved_pedantic
= pedantic
;
25583 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25585 /* Consume the `__extension__' token. */
25586 cp_lexer_consume_token (parser
->lexer
);
25587 /* We're not being pedantic while the `__extension__' keyword is
25597 /* Parse a label declaration.
25600 __label__ label-declarator-seq ;
25602 label-declarator-seq:
25603 identifier , label-declarator-seq
25607 cp_parser_label_declaration (cp_parser
* parser
)
25609 /* Look for the `__label__' keyword. */
25610 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25616 /* Look for an identifier. */
25617 identifier
= cp_parser_identifier (parser
);
25618 /* If we failed, stop. */
25619 if (identifier
== error_mark_node
)
25621 /* Declare it as a label. */
25622 finish_label_decl (identifier
);
25623 /* If the next token is a `;', stop. */
25624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25626 /* Look for the `,' separating the label declarations. */
25627 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25630 /* Look for the final `;'. */
25631 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25634 // -------------------------------------------------------------------------- //
25637 // Parse a requires clause.
25639 // requires-clause:
25640 // 'requires' logical-or-expression
25642 // The required logical-or-expression must be a constant expression. Note
25643 // that we don't check that the expression is constepxr here. We defer until
25644 // we analyze constraints and then, we only check atomic constraints.
25646 cp_parser_requires_clause (cp_parser
*parser
)
25648 // Parse the requires clause so that it is not automatically folded.
25649 ++processing_template_decl
;
25650 tree expr
= cp_parser_binary_expression (parser
, false, false,
25651 PREC_NOT_OPERATOR
, NULL
);
25652 if (check_for_bare_parameter_packs (expr
))
25653 expr
= error_mark_node
;
25654 --processing_template_decl
;
25658 // Optionally parse a requires clause:
25660 cp_parser_requires_clause_opt (cp_parser
*parser
)
25662 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25663 if (tok
->keyword
!= RID_REQUIRES
)
25665 if (!flag_concepts
&& tok
->type
== CPP_NAME
25666 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25668 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25669 "%<requires%> only available with -fconcepts");
25670 /* Parse and discard the requires-clause. */
25671 cp_lexer_consume_token (parser
->lexer
);
25672 cp_parser_requires_clause (parser
);
25676 cp_lexer_consume_token (parser
->lexer
);
25677 return cp_parser_requires_clause (parser
);
25681 /*---------------------------------------------------------------------------
25682 Requires expressions
25683 ---------------------------------------------------------------------------*/
25685 /* Parse a requires expression
25687 requirement-expression:
25688 'requires' requirement-parameter-list [opt] requirement-body */
25690 cp_parser_requires_expression (cp_parser
*parser
)
25692 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25693 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25695 /* A requires-expression shall appear only within a concept
25696 definition or a requires-clause.
25698 TODO: Implement this diagnostic correctly. */
25699 if (!processing_template_decl
)
25701 error_at (loc
, "a requires expression cannot appear outside a template");
25702 cp_parser_skip_to_end_of_statement (parser
);
25703 return error_mark_node
;
25708 /* Local parameters are delared as variables within the scope
25709 of the expression. They are not visible past the end of
25710 the expression. Expressions within the requires-expression
25711 are unevaluated. */
25712 struct scope_sentinel
25716 ++cp_unevaluated_operand
;
25717 begin_scope (sk_block
, NULL_TREE
);
25722 pop_bindings_and_leave_scope ();
25723 --cp_unevaluated_operand
;
25727 /* Parse the optional parameter list. */
25728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25730 parms
= cp_parser_requirement_parameter_list (parser
);
25731 if (parms
== error_mark_node
)
25732 return error_mark_node
;
25737 /* Parse the requirement body. */
25738 reqs
= cp_parser_requirement_body (parser
);
25739 if (reqs
== error_mark_node
)
25740 return error_mark_node
;
25743 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25745 grokparms (parms
, &parms
);
25746 return finish_requires_expr (parms
, reqs
);
25749 /* Parse a parameterized requirement.
25751 requirement-parameter-list:
25752 '(' parameter-declaration-clause ')' */
25754 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25756 matching_parens parens
;
25757 if (!parens
.require_open (parser
))
25758 return error_mark_node
;
25760 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25762 if (!parens
.require_close (parser
))
25763 return error_mark_node
;
25768 /* Parse the body of a requirement.
25771 '{' requirement-list '}' */
25773 cp_parser_requirement_body (cp_parser
*parser
)
25775 matching_braces braces
;
25776 if (!braces
.require_open (parser
))
25777 return error_mark_node
;
25779 tree reqs
= cp_parser_requirement_list (parser
);
25781 if (!braces
.require_close (parser
))
25782 return error_mark_node
;
25787 /* Parse a list of requirements.
25791 requirement-list ';' requirement[opt] */
25793 cp_parser_requirement_list (cp_parser
*parser
)
25795 tree result
= NULL_TREE
;
25798 tree req
= cp_parser_requirement (parser
);
25799 if (req
== error_mark_node
)
25800 return error_mark_node
;
25802 result
= tree_cons (NULL_TREE
, req
, result
);
25804 /* If we see a semi-colon, consume it. */
25805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25806 cp_lexer_consume_token (parser
->lexer
);
25808 /* Stop processing at the end of the list. */
25809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25813 /* Reverse the order of requirements so they are analyzed in
25814 declaration order. */
25815 return nreverse (result
);
25818 /* Parse a syntactic requirement or type requirement.
25822 compound-requirement
25824 nested-requirement */
25826 cp_parser_requirement (cp_parser
*parser
)
25828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25829 return cp_parser_compound_requirement (parser
);
25830 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25831 return cp_parser_type_requirement (parser
);
25832 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25833 return cp_parser_nested_requirement (parser
);
25835 return cp_parser_simple_requirement (parser
);
25838 /* Parse a simple requirement.
25840 simple-requirement:
25843 cp_parser_simple_requirement (cp_parser
*parser
)
25845 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25846 if (!expr
|| expr
== error_mark_node
)
25847 return error_mark_node
;
25849 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25850 return error_mark_node
;
25852 return finish_simple_requirement (expr
);
25855 /* Parse a type requirement
25858 nested-name-specifier [opt] required-type-name ';'
25860 required-type-name:
25862 'template' [opt] simple-template-id */
25864 cp_parser_type_requirement (cp_parser
*parser
)
25866 cp_lexer_consume_token (parser
->lexer
);
25868 // Save the scope before parsing name specifiers.
25869 tree saved_scope
= parser
->scope
;
25870 tree saved_object_scope
= parser
->object_scope
;
25871 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25872 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25873 cp_parser_nested_name_specifier_opt (parser
,
25874 /*typename_keyword_p=*/true,
25875 /*check_dependency_p=*/false,
25877 /*is_declaration=*/false);
25880 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25882 cp_lexer_consume_token (parser
->lexer
);
25883 type
= cp_parser_template_id (parser
,
25884 /*template_keyword_p=*/true,
25885 /*check_dependency=*/false,
25886 /*tag_type=*/none_type
,
25887 /*is_declaration=*/false);
25888 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25889 /*complain=*/tf_error
);
25892 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25894 if (TREE_CODE (type
) == TYPE_DECL
)
25895 type
= TREE_TYPE (type
);
25897 parser
->scope
= saved_scope
;
25898 parser
->object_scope
= saved_object_scope
;
25899 parser
->qualifying_scope
= saved_qualifying_scope
;
25901 if (type
== error_mark_node
)
25902 cp_parser_skip_to_end_of_statement (parser
);
25904 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25905 return error_mark_node
;
25906 if (type
== error_mark_node
)
25907 return error_mark_node
;
25909 return finish_type_requirement (type
);
25912 /* Parse a compound requirement
25914 compound-requirement:
25915 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25917 cp_parser_compound_requirement (cp_parser
*parser
)
25919 /* Parse an expression enclosed in '{ }'s. */
25920 matching_braces braces
;
25921 if (!braces
.require_open (parser
))
25922 return error_mark_node
;
25924 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25925 if (!expr
|| expr
== error_mark_node
)
25926 return error_mark_node
;
25928 if (!braces
.require_close (parser
))
25929 return error_mark_node
;
25931 /* Parse the optional noexcept. */
25932 bool noexcept_p
= false;
25933 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25935 cp_lexer_consume_token (parser
->lexer
);
25939 /* Parse the optional trailing return type. */
25940 tree type
= NULL_TREE
;
25941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25943 cp_lexer_consume_token (parser
->lexer
);
25944 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25945 parser
->in_result_type_constraint_p
= true;
25946 type
= cp_parser_trailing_type_id (parser
);
25947 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25948 if (type
== error_mark_node
)
25949 return error_mark_node
;
25952 return finish_compound_requirement (expr
, type
, noexcept_p
);
25955 /* Parse a nested requirement. This is the same as a requires clause.
25957 nested-requirement:
25960 cp_parser_nested_requirement (cp_parser
*parser
)
25962 cp_lexer_consume_token (parser
->lexer
);
25963 tree req
= cp_parser_requires_clause (parser
);
25964 if (req
== error_mark_node
)
25965 return error_mark_node
;
25966 return finish_nested_requirement (req
);
25969 /* Support Functions */
25971 /* Return the appropriate prefer_type argument for lookup_name_real based on
25972 tag_type and template_mem_access. */
25975 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25977 /* DR 141: When looking in the current enclosing context for a template-name
25978 after -> or ., only consider class templates. */
25979 if (template_mem_access
)
25983 case none_type
: return 0; // No preference.
25984 case scope_type
: return 1; // Type or namespace.
25985 default: return 2; // Type only.
25989 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25990 NAME should have one of the representations used for an
25991 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25992 is returned. If PARSER->SCOPE is a dependent type, then a
25993 SCOPE_REF is returned.
25995 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25996 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25997 was formed. Abstractly, such entities should not be passed to this
25998 function, because they do not need to be looked up, but it is
25999 simpler to check for this special case here, rather than at the
26002 In cases not explicitly covered above, this function returns a
26003 DECL, OVERLOAD, or baselink representing the result of the lookup.
26004 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26007 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26008 (e.g., "struct") that was used. In that case bindings that do not
26009 refer to types are ignored.
26011 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26014 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26017 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26020 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26021 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26022 NULL_TREE otherwise. */
26025 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26026 enum tag_types tag_type
,
26029 bool check_dependency
,
26030 tree
*ambiguous_decls
,
26031 location_t name_location
)
26034 tree object_type
= parser
->context
->object_type
;
26036 /* Assume that the lookup will be unambiguous. */
26037 if (ambiguous_decls
)
26038 *ambiguous_decls
= NULL_TREE
;
26040 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26041 no longer valid. Note that if we are parsing tentatively, and
26042 the parse fails, OBJECT_TYPE will be automatically restored. */
26043 parser
->context
->object_type
= NULL_TREE
;
26045 if (name
== error_mark_node
)
26046 return error_mark_node
;
26048 /* A template-id has already been resolved; there is no lookup to
26050 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26052 if (BASELINK_P (name
))
26054 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26055 == TEMPLATE_ID_EXPR
);
26059 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26060 it should already have been checked to make sure that the name
26061 used matches the type being destroyed. */
26062 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26066 /* Figure out to which type this destructor applies. */
26068 type
= parser
->scope
;
26069 else if (object_type
)
26070 type
= object_type
;
26072 type
= current_class_type
;
26073 /* If that's not a class type, there is no destructor. */
26074 if (!type
|| !CLASS_TYPE_P (type
))
26075 return error_mark_node
;
26077 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26078 lazily_declare_fn (sfk_destructor
, type
);
26080 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26083 return error_mark_node
;
26086 /* By this point, the NAME should be an ordinary identifier. If
26087 the id-expression was a qualified name, the qualifying scope is
26088 stored in PARSER->SCOPE at this point. */
26089 gcc_assert (identifier_p (name
));
26091 /* Perform the lookup. */
26096 if (parser
->scope
== error_mark_node
)
26097 return error_mark_node
;
26099 /* If the SCOPE is dependent, the lookup must be deferred until
26100 the template is instantiated -- unless we are explicitly
26101 looking up names in uninstantiated templates. Even then, we
26102 cannot look up the name if the scope is not a class type; it
26103 might, for example, be a template type parameter. */
26104 dependent_p
= (TYPE_P (parser
->scope
)
26105 && dependent_scope_p (parser
->scope
));
26106 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26108 /* Defer lookup. */
26109 decl
= error_mark_node
;
26112 tree pushed_scope
= NULL_TREE
;
26114 /* If PARSER->SCOPE is a dependent type, then it must be a
26115 class type, and we must not be checking dependencies;
26116 otherwise, we would have processed this lookup above. So
26117 that PARSER->SCOPE is not considered a dependent base by
26118 lookup_member, we must enter the scope here. */
26120 pushed_scope
= push_scope (parser
->scope
);
26122 /* If the PARSER->SCOPE is a template specialization, it
26123 may be instantiated during name lookup. In that case,
26124 errors may be issued. Even if we rollback the current
26125 tentative parse, those errors are valid. */
26126 decl
= lookup_qualified_name (parser
->scope
, name
,
26127 prefer_type_arg (tag_type
),
26128 /*complain=*/true);
26130 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26131 lookup result and the nested-name-specifier nominates a class C:
26132 * if the name specified after the nested-name-specifier, when
26133 looked up in C, is the injected-class-name of C (Clause 9), or
26134 * if the name specified after the nested-name-specifier is the
26135 same as the identifier or the simple-template-id's template-
26136 name in the last component of the nested-name-specifier,
26137 the name is instead considered to name the constructor of
26138 class C. [ Note: for example, the constructor is not an
26139 acceptable lookup result in an elaborated-type-specifier so
26140 the constructor would not be used in place of the
26141 injected-class-name. --end note ] Such a constructor name
26142 shall be used only in the declarator-id of a declaration that
26143 names a constructor or in a using-declaration. */
26144 if (tag_type
== none_type
26145 && DECL_SELF_REFERENCE_P (decl
)
26146 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26147 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26148 prefer_type_arg (tag_type
),
26149 /*complain=*/true);
26151 /* If we have a single function from a using decl, pull it out. */
26152 if (TREE_CODE (decl
) == OVERLOAD
26153 && !really_overloaded_fn (decl
))
26154 decl
= OVL_FUNCTION (decl
);
26157 pop_scope (pushed_scope
);
26160 /* If the scope is a dependent type and either we deferred lookup or
26161 we did lookup but didn't find the name, rememeber the name. */
26162 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26163 && dependent_type_p (parser
->scope
))
26169 /* The resolution to Core Issue 180 says that `struct
26170 A::B' should be considered a type-name, even if `A'
26172 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26173 /*complain=*/tf_error
);
26174 if (type
!= error_mark_node
)
26175 decl
= TYPE_NAME (type
);
26177 else if (is_template
26178 && (cp_parser_next_token_ends_template_argument_p (parser
)
26179 || cp_lexer_next_token_is (parser
->lexer
,
26181 decl
= make_unbound_class_template (parser
->scope
,
26183 /*complain=*/tf_error
);
26185 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26186 parser
->scope
, name
,
26189 parser
->qualifying_scope
= parser
->scope
;
26190 parser
->object_scope
= NULL_TREE
;
26192 else if (object_type
)
26194 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26195 OBJECT_TYPE is not a class. */
26196 if (CLASS_TYPE_P (object_type
))
26197 /* If the OBJECT_TYPE is a template specialization, it may
26198 be instantiated during name lookup. In that case, errors
26199 may be issued. Even if we rollback the current tentative
26200 parse, those errors are valid. */
26201 decl
= lookup_member (object_type
,
26204 prefer_type_arg (tag_type
),
26205 tf_warning_or_error
);
26210 /* Look it up in the enclosing context. DR 141: When looking for a
26211 template-name after -> or ., only consider class templates. */
26212 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26214 /*block_p=*/true, is_namespace
, 0);
26215 if (object_type
== unknown_type_node
)
26216 /* The object is type-dependent, so we can't look anything up; we used
26217 this to get the DR 141 behavior. */
26218 object_type
= NULL_TREE
;
26219 parser
->object_scope
= object_type
;
26220 parser
->qualifying_scope
= NULL_TREE
;
26224 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26226 /*block_p=*/true, is_namespace
, 0);
26227 parser
->qualifying_scope
= NULL_TREE
;
26228 parser
->object_scope
= NULL_TREE
;
26231 /* If the lookup failed, let our caller know. */
26232 if (!decl
|| decl
== error_mark_node
)
26233 return error_mark_node
;
26235 /* Pull out the template from an injected-class-name (or multiple). */
26237 decl
= maybe_get_template_decl_from_type_decl (decl
);
26239 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26240 if (TREE_CODE (decl
) == TREE_LIST
)
26242 if (ambiguous_decls
)
26243 *ambiguous_decls
= decl
;
26244 /* The error message we have to print is too complicated for
26245 cp_parser_error, so we incorporate its actions directly. */
26246 if (!cp_parser_simulate_error (parser
))
26248 error_at (name_location
, "reference to %qD is ambiguous",
26250 print_candidates (decl
);
26252 return error_mark_node
;
26255 gcc_assert (DECL_P (decl
)
26256 || TREE_CODE (decl
) == OVERLOAD
26257 || TREE_CODE (decl
) == SCOPE_REF
26258 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26259 || BASELINK_P (decl
));
26261 /* If we have resolved the name of a member declaration, check to
26262 see if the declaration is accessible. When the name resolves to
26263 set of overloaded functions, accessibility is checked when
26264 overload resolution is done.
26266 During an explicit instantiation, access is not checked at all,
26267 as per [temp.explicit]. */
26269 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26271 maybe_record_typedef_use (decl
);
26273 return cp_expr (decl
, name_location
);
26276 /* Like cp_parser_lookup_name, but for use in the typical case where
26277 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26278 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26281 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26283 return cp_parser_lookup_name (parser
, name
,
26285 /*is_template=*/false,
26286 /*is_namespace=*/false,
26287 /*check_dependency=*/true,
26288 /*ambiguous_decls=*/NULL
,
26292 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26293 the current context, return the TYPE_DECL. If TAG_NAME_P is
26294 true, the DECL indicates the class being defined in a class-head,
26295 or declared in an elaborated-type-specifier.
26297 Otherwise, return DECL. */
26300 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26302 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26303 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26306 template <typename T> struct B;
26309 template <typename T> struct A::B {};
26311 Similarly, in an elaborated-type-specifier:
26313 namespace N { struct X{}; }
26316 template <typename T> friend struct N::X;
26319 However, if the DECL refers to a class type, and we are in
26320 the scope of the class, then the name lookup automatically
26321 finds the TYPE_DECL created by build_self_reference rather
26322 than a TEMPLATE_DECL. For example, in:
26324 template <class T> struct S {
26328 there is no need to handle such case. */
26330 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26331 return DECL_TEMPLATE_RESULT (decl
);
26336 /* If too many, or too few, template-parameter lists apply to the
26337 declarator, issue an error message. Returns TRUE if all went well,
26338 and FALSE otherwise. */
26341 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26342 cp_declarator
*declarator
,
26343 location_t declarator_location
)
26345 switch (declarator
->kind
)
26349 unsigned num_templates
= 0;
26350 tree scope
= declarator
->u
.id
.qualifying_scope
;
26353 num_templates
= num_template_headers_for_class (scope
);
26354 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26355 == TEMPLATE_ID_EXPR
)
26356 /* If the DECLARATOR has the form `X<y>' then it uses one
26357 additional level of template parameters. */
26360 return cp_parser_check_template_parameters
26361 (parser
, num_templates
, declarator_location
, declarator
);
26367 case cdk_reference
:
26369 return (cp_parser_check_declarator_template_parameters
26370 (parser
, declarator
->declarator
, declarator_location
));
26377 gcc_unreachable ();
26382 /* NUM_TEMPLATES were used in the current declaration. If that is
26383 invalid, return FALSE and issue an error messages. Otherwise,
26384 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26385 declarator and we can print more accurate diagnostics. */
26388 cp_parser_check_template_parameters (cp_parser
* parser
,
26389 unsigned num_templates
,
26390 location_t location
,
26391 cp_declarator
*declarator
)
26393 /* If there are the same number of template classes and parameter
26394 lists, that's OK. */
26395 if (parser
->num_template_parameter_lists
== num_templates
)
26397 /* If there are more, but only one more, then we are referring to a
26398 member template. That's OK too. */
26399 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26401 /* If there are more template classes than parameter lists, we have
26404 template <class T> void S<T>::R<T>::f (); */
26405 if (parser
->num_template_parameter_lists
< num_templates
)
26407 if (declarator
&& !current_function_decl
)
26408 error_at (location
, "specializing member %<%T::%E%> "
26409 "requires %<template<>%> syntax",
26410 declarator
->u
.id
.qualifying_scope
,
26411 declarator
->u
.id
.unqualified_name
);
26412 else if (declarator
)
26413 error_at (location
, "invalid declaration of %<%T::%E%>",
26414 declarator
->u
.id
.qualifying_scope
,
26415 declarator
->u
.id
.unqualified_name
);
26417 error_at (location
, "too few template-parameter-lists");
26420 /* Otherwise, there are too many template parameter lists. We have
26423 template <class T> template <class U> void S::f(); */
26424 error_at (location
, "too many template-parameter-lists");
26428 /* Parse an optional `::' token indicating that the following name is
26429 from the global namespace. If so, PARSER->SCOPE is set to the
26430 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26431 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26432 Returns the new value of PARSER->SCOPE, if the `::' token is
26433 present, and NULL_TREE otherwise. */
26436 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26440 /* Peek at the next token. */
26441 token
= cp_lexer_peek_token (parser
->lexer
);
26442 /* If we're looking at a `::' token then we're starting from the
26443 global namespace, not our current location. */
26444 if (token
->type
== CPP_SCOPE
)
26446 /* Consume the `::' token. */
26447 cp_lexer_consume_token (parser
->lexer
);
26448 /* Set the SCOPE so that we know where to start the lookup. */
26449 parser
->scope
= global_namespace
;
26450 parser
->qualifying_scope
= global_namespace
;
26451 parser
->object_scope
= NULL_TREE
;
26453 return parser
->scope
;
26455 else if (!current_scope_valid_p
)
26457 parser
->scope
= NULL_TREE
;
26458 parser
->qualifying_scope
= NULL_TREE
;
26459 parser
->object_scope
= NULL_TREE
;
26465 /* Returns TRUE if the upcoming token sequence is the start of a
26466 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26467 declarator is preceded by the `friend' specifier. */
26470 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26472 bool constructor_p
;
26473 bool outside_class_specifier_p
;
26474 tree nested_name_specifier
;
26475 cp_token
*next_token
;
26477 /* The common case is that this is not a constructor declarator, so
26478 try to avoid doing lots of work if at all possible. It's not
26479 valid declare a constructor at function scope. */
26480 if (parser
->in_function_body
)
26482 /* And only certain tokens can begin a constructor declarator. */
26483 next_token
= cp_lexer_peek_token (parser
->lexer
);
26484 if (next_token
->type
!= CPP_NAME
26485 && next_token
->type
!= CPP_SCOPE
26486 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26487 && next_token
->type
!= CPP_TEMPLATE_ID
)
26490 /* Parse tentatively; we are going to roll back all of the tokens
26492 cp_parser_parse_tentatively (parser
);
26493 /* Assume that we are looking at a constructor declarator. */
26494 constructor_p
= true;
26496 /* Look for the optional `::' operator. */
26497 cp_parser_global_scope_opt (parser
,
26498 /*current_scope_valid_p=*/false);
26499 /* Look for the nested-name-specifier. */
26500 nested_name_specifier
26501 = (cp_parser_nested_name_specifier_opt (parser
,
26502 /*typename_keyword_p=*/false,
26503 /*check_dependency_p=*/false,
26505 /*is_declaration=*/false));
26507 outside_class_specifier_p
= (!at_class_scope_p ()
26508 || !TYPE_BEING_DEFINED (current_class_type
)
26511 /* Outside of a class-specifier, there must be a
26512 nested-name-specifier. Except in C++17 mode, where we
26513 might be declaring a guiding declaration. */
26514 if (!nested_name_specifier
&& outside_class_specifier_p
26515 && cxx_dialect
< cxx17
)
26516 constructor_p
= false;
26517 else if (nested_name_specifier
== error_mark_node
)
26518 constructor_p
= false;
26520 /* If we have a class scope, this is easy; DR 147 says that S::S always
26521 names the constructor, and no other qualified name could. */
26522 if (constructor_p
&& nested_name_specifier
26523 && CLASS_TYPE_P (nested_name_specifier
))
26525 tree id
= cp_parser_unqualified_id (parser
,
26526 /*template_keyword_p=*/false,
26527 /*check_dependency_p=*/false,
26528 /*declarator_p=*/true,
26529 /*optional_p=*/false);
26530 if (is_overloaded_fn (id
))
26531 id
= DECL_NAME (get_first_fn (id
));
26532 if (!constructor_name_p (id
, nested_name_specifier
))
26533 constructor_p
= false;
26535 /* If we still think that this might be a constructor-declarator,
26536 look for a class-name. */
26537 else if (constructor_p
)
26541 template <typename T> struct S {
26545 we must recognize that the nested `S' names a class. */
26546 if (cxx_dialect
>= cxx17
)
26547 cp_parser_parse_tentatively (parser
);
26550 type_decl
= cp_parser_class_name (parser
,
26551 /*typename_keyword_p=*/false,
26552 /*template_keyword_p=*/false,
26554 /*check_dependency_p=*/false,
26555 /*class_head_p=*/false,
26556 /*is_declaration=*/false);
26558 if (cxx_dialect
>= cxx17
26559 && !cp_parser_parse_definitely (parser
))
26561 type_decl
= NULL_TREE
;
26562 tree tmpl
= cp_parser_template_name (parser
,
26563 /*template_keyword*/false,
26564 /*check_dependency_p*/false,
26565 /*is_declaration*/false,
26567 /*is_identifier*/NULL
);
26568 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26569 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26570 /* It's a deduction guide, return true. */;
26572 cp_parser_simulate_error (parser
);
26575 /* If there was no class-name, then this is not a constructor.
26576 Otherwise, if we are in a class-specifier and we aren't
26577 handling a friend declaration, check that its type matches
26578 current_class_type (c++/38313). Note: error_mark_node
26579 is left alone for error recovery purposes. */
26580 constructor_p
= (!cp_parser_error_occurred (parser
)
26581 && (outside_class_specifier_p
26582 || type_decl
== NULL_TREE
26583 || type_decl
== error_mark_node
26584 || same_type_p (current_class_type
,
26585 TREE_TYPE (type_decl
))));
26587 /* If we're still considering a constructor, we have to see a `(',
26588 to begin the parameter-declaration-clause, followed by either a
26589 `)', an `...', or a decl-specifier. We need to check for a
26590 type-specifier to avoid being fooled into thinking that:
26594 is a constructor. (It is actually a function named `f' that
26595 takes one parameter (of type `int') and returns a value of type
26598 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26599 constructor_p
= false;
26602 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26603 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26604 /* A parameter declaration begins with a decl-specifier,
26605 which is either the "attribute" keyword, a storage class
26606 specifier, or (usually) a type-specifier. */
26607 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26610 tree pushed_scope
= NULL_TREE
;
26611 unsigned saved_num_template_parameter_lists
;
26613 /* Names appearing in the type-specifier should be looked up
26614 in the scope of the class. */
26615 if (current_class_type
)
26617 else if (type_decl
)
26619 type
= TREE_TYPE (type_decl
);
26620 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26622 type
= resolve_typename_type (type
,
26623 /*only_current_p=*/false);
26624 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26626 cp_parser_abort_tentative_parse (parser
);
26630 pushed_scope
= push_scope (type
);
26633 /* Inside the constructor parameter list, surrounding
26634 template-parameter-lists do not apply. */
26635 saved_num_template_parameter_lists
26636 = parser
->num_template_parameter_lists
;
26637 parser
->num_template_parameter_lists
= 0;
26639 /* Look for the type-specifier. */
26640 cp_parser_type_specifier (parser
,
26641 CP_PARSER_FLAGS_NONE
,
26642 /*decl_specs=*/NULL
,
26643 /*is_declarator=*/true,
26644 /*declares_class_or_enum=*/NULL
,
26645 /*is_cv_qualifier=*/NULL
);
26647 parser
->num_template_parameter_lists
26648 = saved_num_template_parameter_lists
;
26650 /* Leave the scope of the class. */
26652 pop_scope (pushed_scope
);
26654 constructor_p
= !cp_parser_error_occurred (parser
);
26658 /* We did not really want to consume any tokens. */
26659 cp_parser_abort_tentative_parse (parser
);
26661 return constructor_p
;
26664 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26665 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26666 they must be performed once we are in the scope of the function.
26668 Returns the function defined. */
26671 cp_parser_function_definition_from_specifiers_and_declarator
26672 (cp_parser
* parser
,
26673 cp_decl_specifier_seq
*decl_specifiers
,
26675 const cp_declarator
*declarator
)
26680 /* Begin the function-definition. */
26681 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26683 /* The things we're about to see are not directly qualified by any
26684 template headers we've seen thus far. */
26685 reset_specialization ();
26687 /* If there were names looked up in the decl-specifier-seq that we
26688 did not check, check them now. We must wait until we are in the
26689 scope of the function to perform the checks, since the function
26690 might be a friend. */
26691 perform_deferred_access_checks (tf_warning_or_error
);
26695 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26696 parser
->omp_declare_simd
= NULL
;
26697 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26698 parser
->oacc_routine
= NULL
;
26703 /* Skip the entire function. */
26704 cp_parser_skip_to_end_of_block_or_statement (parser
);
26705 fn
= error_mark_node
;
26707 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26709 /* Seen already, skip it. An error message has already been output. */
26710 cp_parser_skip_to_end_of_block_or_statement (parser
);
26711 fn
= current_function_decl
;
26712 current_function_decl
= NULL_TREE
;
26713 /* If this is a function from a class, pop the nested class. */
26714 if (current_class_name
)
26715 pop_nested_class ();
26720 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26721 tv
= TV_PARSE_INLINE
;
26723 tv
= TV_PARSE_FUNC
;
26725 fn
= cp_parser_function_definition_after_declarator (parser
,
26726 /*inline_p=*/false);
26733 /* Parse the part of a function-definition that follows the
26734 declarator. INLINE_P is TRUE iff this function is an inline
26735 function defined within a class-specifier.
26737 Returns the function defined. */
26740 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26744 bool saved_in_unbraced_linkage_specification_p
;
26745 bool saved_in_function_body
;
26746 unsigned saved_num_template_parameter_lists
;
26748 bool fully_implicit_function_template_p
26749 = parser
->fully_implicit_function_template_p
;
26750 parser
->fully_implicit_function_template_p
= false;
26751 tree implicit_template_parms
26752 = parser
->implicit_template_parms
;
26753 parser
->implicit_template_parms
= 0;
26754 cp_binding_level
* implicit_template_scope
26755 = parser
->implicit_template_scope
;
26756 parser
->implicit_template_scope
= 0;
26758 saved_in_function_body
= parser
->in_function_body
;
26759 parser
->in_function_body
= true;
26760 /* If the next token is `return', then the code may be trying to
26761 make use of the "named return value" extension that G++ used to
26763 token
= cp_lexer_peek_token (parser
->lexer
);
26764 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26766 /* Consume the `return' keyword. */
26767 cp_lexer_consume_token (parser
->lexer
);
26768 /* Look for the identifier that indicates what value is to be
26770 cp_parser_identifier (parser
);
26771 /* Issue an error message. */
26772 error_at (token
->location
,
26773 "named return values are no longer supported");
26774 /* Skip tokens until we reach the start of the function body. */
26777 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26778 if (token
->type
== CPP_OPEN_BRACE
26779 || token
->type
== CPP_EOF
26780 || token
->type
== CPP_PRAGMA_EOL
)
26782 cp_lexer_consume_token (parser
->lexer
);
26785 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26786 anything declared inside `f'. */
26787 saved_in_unbraced_linkage_specification_p
26788 = parser
->in_unbraced_linkage_specification_p
;
26789 parser
->in_unbraced_linkage_specification_p
= false;
26790 /* Inside the function, surrounding template-parameter-lists do not
26792 saved_num_template_parameter_lists
26793 = parser
->num_template_parameter_lists
;
26794 parser
->num_template_parameter_lists
= 0;
26796 /* If the next token is `try', `__transaction_atomic', or
26797 `__transaction_relaxed`, then we are looking at either function-try-block
26798 or function-transaction-block. Note that all of these include the
26800 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26801 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26802 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26803 RID_TRANSACTION_RELAXED
))
26804 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26805 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26806 cp_parser_function_try_block (parser
);
26808 cp_parser_ctor_initializer_opt_and_function_body
26809 (parser
, /*in_function_try_block=*/false);
26811 /* Finish the function. */
26812 fn
= finish_function (inline_p
);
26813 /* Generate code for it, if necessary. */
26814 expand_or_defer_fn (fn
);
26815 /* Restore the saved values. */
26816 parser
->in_unbraced_linkage_specification_p
26817 = saved_in_unbraced_linkage_specification_p
;
26818 parser
->num_template_parameter_lists
26819 = saved_num_template_parameter_lists
;
26820 parser
->in_function_body
= saved_in_function_body
;
26822 parser
->fully_implicit_function_template_p
26823 = fully_implicit_function_template_p
;
26824 parser
->implicit_template_parms
26825 = implicit_template_parms
;
26826 parser
->implicit_template_scope
26827 = implicit_template_scope
;
26829 if (parser
->fully_implicit_function_template_p
)
26830 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26835 /* Parse a template-declaration body (following argument list). */
26838 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26839 tree parameter_list
,
26842 tree decl
= NULL_TREE
;
26843 bool friend_p
= false;
26845 /* We just processed one more parameter list. */
26846 ++parser
->num_template_parameter_lists
;
26848 /* Get the deferred access checks from the parameter list. These
26849 will be checked once we know what is being declared, as for a
26850 member template the checks must be performed in the scope of the
26851 class containing the member. */
26852 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26854 /* Tentatively parse for a new template parameter list, which can either be
26855 the template keyword or a template introduction. */
26856 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26858 else if (cxx_dialect
>= cxx11
26859 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26860 decl
= cp_parser_alias_declaration (parser
);
26863 /* There are no access checks when parsing a template, as we do not
26864 know if a specialization will be a friend. */
26865 push_deferring_access_checks (dk_no_check
);
26866 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26867 decl
= cp_parser_single_declaration (parser
,
26870 /*explicit_specialization_p=*/false,
26872 pop_deferring_access_checks ();
26874 /* If this is a member template declaration, let the front
26876 if (member_p
&& !friend_p
&& decl
)
26878 if (TREE_CODE (decl
) == TYPE_DECL
)
26879 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26881 decl
= finish_member_template_decl (decl
);
26883 else if (friend_p
&& decl
26884 && DECL_DECLARES_TYPE_P (decl
))
26885 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26886 /*complain=*/true);
26888 /* We are done with the current parameter list. */
26889 --parser
->num_template_parameter_lists
;
26891 pop_deferring_access_checks ();
26894 finish_template_decl (parameter_list
);
26896 /* Check the template arguments for a literal operator template. */
26898 && DECL_DECLARES_FUNCTION_P (decl
)
26899 && UDLIT_OPER_P (DECL_NAME (decl
)))
26902 if (parameter_list
== NULL_TREE
)
26906 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26907 if (num_parms
== 1)
26909 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26910 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26911 if (TREE_TYPE (parm
) != char_type_node
26912 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26915 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26917 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26918 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26919 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26920 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26921 if (parm
== error_mark_node
26922 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26923 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26931 if (cxx_dialect
>= cxx14
)
26932 error ("literal operator template %qD has invalid parameter list."
26933 " Expected non-type template argument pack <char...>"
26934 " or <typename CharT, CharT...>",
26937 error ("literal operator template %qD has invalid parameter list."
26938 " Expected non-type template argument pack <char...>",
26943 /* Register member declarations. */
26944 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26945 finish_member_declaration (decl
);
26946 /* If DECL is a function template, we must return to parse it later.
26947 (Even though there is no definition, there might be default
26948 arguments that need handling.) */
26949 if (member_p
&& decl
26950 && DECL_DECLARES_FUNCTION_P (decl
))
26951 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26954 /* Parse a template introduction header for a template-declaration. Returns
26955 false if tentative parse fails. */
26958 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26960 cp_parser_parse_tentatively (parser
);
26962 tree saved_scope
= parser
->scope
;
26963 tree saved_object_scope
= parser
->object_scope
;
26964 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26966 /* Look for the optional `::' operator. */
26967 cp_parser_global_scope_opt (parser
,
26968 /*current_scope_valid_p=*/false);
26969 /* Look for the nested-name-specifier. */
26970 cp_parser_nested_name_specifier_opt (parser
,
26971 /*typename_keyword_p=*/false,
26972 /*check_dependency_p=*/true,
26974 /*is_declaration=*/false);
26976 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26977 tree concept_name
= cp_parser_identifier (parser
);
26979 /* Look up the concept for which we will be matching
26980 template parameters. */
26981 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26983 parser
->scope
= saved_scope
;
26984 parser
->object_scope
= saved_object_scope
;
26985 parser
->qualifying_scope
= saved_qualifying_scope
;
26987 if (concept_name
== error_mark_node
)
26988 cp_parser_simulate_error (parser
);
26990 /* Look for opening brace for introduction. */
26991 matching_braces braces
;
26992 braces
.require_open (parser
);
26994 if (!cp_parser_parse_definitely (parser
))
26997 push_deferring_access_checks (dk_deferred
);
26999 /* Build vector of placeholder parameters and grab
27000 matching identifiers. */
27001 tree introduction_list
= cp_parser_introduction_list (parser
);
27003 /* The introduction-list shall not be empty. */
27004 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27007 error ("empty introduction-list");
27011 /* Look for closing brace for introduction. */
27012 if (!braces
.require_close (parser
))
27015 if (tmpl_decl
== error_mark_node
)
27017 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27022 /* Build and associate the constraint. */
27023 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27024 if (parms
&& parms
!= error_mark_node
)
27026 cp_parser_template_declaration_after_parameters (parser
, parms
,
27031 error_at (token
->location
, "no matching concept for template-introduction");
27035 /* Parse a normal template-declaration following the template keyword. */
27038 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27040 tree parameter_list
;
27041 bool need_lang_pop
;
27042 location_t location
= input_location
;
27044 /* Look for the `<' token. */
27045 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27047 if (at_class_scope_p () && current_function_decl
)
27049 /* 14.5.2.2 [temp.mem]
27051 A local class shall not have member templates. */
27052 error_at (location
,
27053 "invalid declaration of member template in local class");
27054 cp_parser_skip_to_end_of_block_or_statement (parser
);
27059 A template ... shall not have C linkage. */
27060 if (current_lang_name
== lang_name_c
)
27062 error_at (location
, "template with C linkage");
27063 maybe_show_extern_c_location ();
27064 /* Give it C++ linkage to avoid confusing other parts of the
27066 push_lang_context (lang_name_cplusplus
);
27067 need_lang_pop
= true;
27070 need_lang_pop
= false;
27072 /* We cannot perform access checks on the template parameter
27073 declarations until we know what is being declared, just as we
27074 cannot check the decl-specifier list. */
27075 push_deferring_access_checks (dk_deferred
);
27077 /* If the next token is `>', then we have an invalid
27078 specialization. Rather than complain about an invalid template
27079 parameter, issue an error message here. */
27080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27082 cp_parser_error (parser
, "invalid explicit specialization");
27083 begin_specialization ();
27084 parameter_list
= NULL_TREE
;
27088 /* Parse the template parameters. */
27089 parameter_list
= cp_parser_template_parameter_list (parser
);
27092 /* Look for the `>'. */
27093 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27095 /* Manage template requirements */
27098 tree reqs
= get_shorthand_constraints (current_template_parms
);
27099 if (tree r
= cp_parser_requires_clause_opt (parser
))
27100 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27101 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27104 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27107 /* For the erroneous case of a template with C linkage, we pushed an
27108 implicit C++ linkage scope; exit that scope now. */
27110 pop_lang_context ();
27113 /* Parse a template-declaration, assuming that the `export' (and
27114 `extern') keywords, if present, has already been scanned. MEMBER_P
27115 is as for cp_parser_template_declaration. */
27118 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27120 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27122 cp_lexer_consume_token (parser
->lexer
);
27123 cp_parser_explicit_template_declaration (parser
, member_p
);
27126 else if (flag_concepts
)
27127 return cp_parser_template_introduction (parser
, member_p
);
27132 /* Perform the deferred access checks from a template-parameter-list.
27133 CHECKS is a TREE_LIST of access checks, as returned by
27134 get_deferred_access_checks. */
27137 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27139 ++processing_template_parmlist
;
27140 perform_access_checks (checks
, tf_warning_or_error
);
27141 --processing_template_parmlist
;
27144 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27145 `function-definition' sequence that follows a template header.
27146 If MEMBER_P is true, this declaration appears in a class scope.
27148 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27149 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27152 cp_parser_single_declaration (cp_parser
* parser
,
27153 vec
<deferred_access_check
, va_gc
> *checks
,
27155 bool explicit_specialization_p
,
27158 int declares_class_or_enum
;
27159 tree decl
= NULL_TREE
;
27160 cp_decl_specifier_seq decl_specifiers
;
27161 bool function_definition_p
= false;
27162 cp_token
*decl_spec_token_start
;
27164 /* This function is only used when processing a template
27166 gcc_assert (innermost_scope_kind () == sk_template_parms
27167 || innermost_scope_kind () == sk_template_spec
);
27169 /* Defer access checks until we know what is being declared. */
27170 push_deferring_access_checks (dk_deferred
);
27172 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27174 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27175 cp_parser_decl_specifier_seq (parser
,
27176 CP_PARSER_FLAGS_OPTIONAL
,
27178 &declares_class_or_enum
);
27180 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27182 /* There are no template typedefs. */
27183 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27185 error_at (decl_spec_token_start
->location
,
27186 "template declaration of %<typedef%>");
27187 decl
= error_mark_node
;
27190 /* Gather up the access checks that occurred the
27191 decl-specifier-seq. */
27192 stop_deferring_access_checks ();
27194 /* Check for the declaration of a template class. */
27195 if (declares_class_or_enum
)
27197 if (cp_parser_declares_only_class_p (parser
)
27198 || (declares_class_or_enum
& 2))
27200 // If this is a declaration, but not a definition, associate
27201 // any constraints with the type declaration. Constraints
27202 // are associated with definitions in cp_parser_class_specifier.
27203 if (declares_class_or_enum
== 1)
27204 associate_classtype_constraints (decl_specifiers
.type
);
27206 decl
= shadow_tag (&decl_specifiers
);
27211 friend template <typename T> struct A<T>::B;
27214 A<T>::B will be represented by a TYPENAME_TYPE, and
27215 therefore not recognized by shadow_tag. */
27216 if (friend_p
&& *friend_p
27218 && decl_specifiers
.type
27219 && TYPE_P (decl_specifiers
.type
))
27220 decl
= decl_specifiers
.type
;
27222 if (decl
&& decl
!= error_mark_node
)
27223 decl
= TYPE_NAME (decl
);
27225 decl
= error_mark_node
;
27227 /* Perform access checks for template parameters. */
27228 cp_parser_perform_template_parameter_access_checks (checks
);
27230 /* Give a helpful diagnostic for
27231 template <class T> struct A { } a;
27232 if we aren't already recovering from an error. */
27233 if (!cp_parser_declares_only_class_p (parser
)
27236 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27237 "a class template declaration must not declare "
27239 cp_parser_skip_to_end_of_block_or_statement (parser
);
27245 /* Complain about missing 'typename' or other invalid type names. */
27246 if (!decl_specifiers
.any_type_specifiers_p
27247 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27249 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27250 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27251 the rest of this declaration. */
27252 decl
= error_mark_node
;
27256 /* If it's not a template class, try for a template function. If
27257 the next token is a `;', then this declaration does not declare
27258 anything. But, if there were errors in the decl-specifiers, then
27259 the error might well have come from an attempted class-specifier.
27260 In that case, there's no need to warn about a missing declarator. */
27262 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27263 || decl_specifiers
.type
!= error_mark_node
))
27265 decl
= cp_parser_init_declarator (parser
,
27268 /*function_definition_allowed_p=*/true,
27270 declares_class_or_enum
,
27271 &function_definition_p
,
27274 /* 7.1.1-1 [dcl.stc]
27276 A storage-class-specifier shall not be specified in an explicit
27277 specialization... */
27279 && explicit_specialization_p
27280 && decl_specifiers
.storage_class
!= sc_none
)
27282 error_at (decl_spec_token_start
->location
,
27283 "explicit template specialization cannot have a storage class");
27284 decl
= error_mark_node
;
27287 if (decl
&& VAR_P (decl
))
27288 check_template_variable (decl
);
27291 /* Look for a trailing `;' after the declaration. */
27292 if (!function_definition_p
27293 && (decl
== error_mark_node
27294 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27295 cp_parser_skip_to_end_of_block_or_statement (parser
);
27298 pop_deferring_access_checks ();
27300 /* Clear any current qualification; whatever comes next is the start
27301 of something new. */
27302 parser
->scope
= NULL_TREE
;
27303 parser
->qualifying_scope
= NULL_TREE
;
27304 parser
->object_scope
= NULL_TREE
;
27309 /* Parse a cast-expression that is not the operand of a unary "&". */
27312 cp_parser_simple_cast_expression (cp_parser
*parser
)
27314 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27315 /*cast_p=*/false, /*decltype*/false, NULL
);
27318 /* Parse a functional cast to TYPE. Returns an expression
27319 representing the cast. */
27322 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27324 vec
<tree
, va_gc
> *vec
;
27325 tree expression_list
;
27329 location_t start_loc
= input_location
;
27332 type
= error_mark_node
;
27334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27336 cp_lexer_set_source_position (parser
->lexer
);
27337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27338 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27339 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27340 if (TREE_CODE (type
) == TYPE_DECL
)
27341 type
= TREE_TYPE (type
);
27343 cast
= finish_compound_literal (type
, expression_list
,
27344 tf_warning_or_error
, fcl_functional
);
27345 /* Create a location of the form:
27348 with caret == start at the start of the type name,
27349 finishing at the closing brace. */
27350 location_t finish_loc
27351 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27352 location_t combined_loc
= make_location (start_loc
, start_loc
,
27354 cast
.set_location (combined_loc
);
27359 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27361 /*allow_expansion_p=*/true,
27362 /*non_constant_p=*/NULL
);
27364 expression_list
= error_mark_node
;
27367 expression_list
= build_tree_list_vec (vec
);
27368 release_tree_vector (vec
);
27371 cast
= build_functional_cast (type
, expression_list
,
27372 tf_warning_or_error
);
27373 /* [expr.const]/1: In an integral constant expression "only type
27374 conversions to integral or enumeration type can be used". */
27375 if (TREE_CODE (type
) == TYPE_DECL
)
27376 type
= TREE_TYPE (type
);
27377 if (cast
!= error_mark_node
27378 && !cast_valid_in_integral_constant_expression_p (type
)
27379 && cp_parser_non_integral_constant_expression (parser
,
27381 return error_mark_node
;
27383 /* Create a location of the form:
27386 with caret == start at the start of the type name,
27387 finishing at the closing paren. */
27388 location_t finish_loc
27389 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27390 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27391 cast
.set_location (combined_loc
);
27395 /* Save the tokens that make up the body of a member function defined
27396 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27397 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27398 specifiers applied to the declaration. Returns the FUNCTION_DECL
27399 for the member function. */
27402 cp_parser_save_member_function_body (cp_parser
* parser
,
27403 cp_decl_specifier_seq
*decl_specifiers
,
27404 cp_declarator
*declarator
,
27410 bool function_try_block
= false;
27412 /* Create the FUNCTION_DECL. */
27413 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27414 cp_finalize_omp_declare_simd (parser
, fn
);
27415 cp_finalize_oacc_routine (parser
, fn
, true);
27416 /* If something went badly wrong, bail out now. */
27417 if (fn
== error_mark_node
)
27419 /* If there's a function-body, skip it. */
27420 if (cp_parser_token_starts_function_definition_p
27421 (cp_lexer_peek_token (parser
->lexer
)))
27422 cp_parser_skip_to_end_of_block_or_statement (parser
);
27423 return error_mark_node
;
27426 /* Remember it, if there default args to post process. */
27427 cp_parser_save_default_args (parser
, fn
);
27429 /* Save away the tokens that make up the body of the
27431 first
= parser
->lexer
->next_token
;
27433 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27434 cp_lexer_consume_token (parser
->lexer
);
27435 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27436 RID_TRANSACTION_ATOMIC
))
27438 cp_lexer_consume_token (parser
->lexer
);
27439 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27441 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27442 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27443 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27444 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27445 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27447 cp_lexer_consume_token (parser
->lexer
);
27448 cp_lexer_consume_token (parser
->lexer
);
27449 cp_lexer_consume_token (parser
->lexer
);
27450 cp_lexer_consume_token (parser
->lexer
);
27451 cp_lexer_consume_token (parser
->lexer
);
27454 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27455 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27457 cp_lexer_consume_token (parser
->lexer
);
27458 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27463 /* Handle function try blocks. */
27464 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27466 cp_lexer_consume_token (parser
->lexer
);
27467 function_try_block
= true;
27469 /* We can have braced-init-list mem-initializers before the fn body. */
27470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27472 cp_lexer_consume_token (parser
->lexer
);
27473 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27475 /* cache_group will stop after an un-nested { } pair, too. */
27476 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27479 /* variadic mem-inits have ... after the ')'. */
27480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27481 cp_lexer_consume_token (parser
->lexer
);
27484 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27485 /* Handle function try blocks. */
27486 if (function_try_block
)
27487 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27488 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27489 last
= parser
->lexer
->next_token
;
27491 /* Save away the inline definition; we will process it when the
27492 class is complete. */
27493 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27494 DECL_PENDING_INLINE_P (fn
) = 1;
27496 /* We need to know that this was defined in the class, so that
27497 friend templates are handled correctly. */
27498 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27500 /* Add FN to the queue of functions to be parsed later. */
27501 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27506 /* Save the tokens that make up the in-class initializer for a non-static
27507 data member. Returns a DEFAULT_ARG. */
27510 cp_parser_save_nsdmi (cp_parser
* parser
)
27512 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27515 /* Parse a template-argument-list, as well as the trailing ">" (but
27516 not the opening "<"). See cp_parser_template_argument_list for the
27520 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27524 tree saved_qualifying_scope
;
27525 tree saved_object_scope
;
27526 bool saved_greater_than_is_operator_p
;
27527 int saved_unevaluated_operand
;
27528 int saved_inhibit_evaluation_warnings
;
27532 When parsing a template-id, the first non-nested `>' is taken as
27533 the end of the template-argument-list rather than a greater-than
27535 saved_greater_than_is_operator_p
27536 = parser
->greater_than_is_operator_p
;
27537 parser
->greater_than_is_operator_p
= false;
27538 /* Parsing the argument list may modify SCOPE, so we save it
27540 saved_scope
= parser
->scope
;
27541 saved_qualifying_scope
= parser
->qualifying_scope
;
27542 saved_object_scope
= parser
->object_scope
;
27543 /* We need to evaluate the template arguments, even though this
27544 template-id may be nested within a "sizeof". */
27545 saved_unevaluated_operand
= cp_unevaluated_operand
;
27546 cp_unevaluated_operand
= 0;
27547 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27548 c_inhibit_evaluation_warnings
= 0;
27549 /* Parse the template-argument-list itself. */
27550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27551 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27552 arguments
= NULL_TREE
;
27554 arguments
= cp_parser_template_argument_list (parser
);
27555 /* Look for the `>' that ends the template-argument-list. If we find
27556 a '>>' instead, it's probably just a typo. */
27557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27559 if (cxx_dialect
!= cxx98
)
27561 /* In C++0x, a `>>' in a template argument list or cast
27562 expression is considered to be two separate `>'
27563 tokens. So, change the current token to a `>', but don't
27564 consume it: it will be consumed later when the outer
27565 template argument list (or cast expression) is parsed.
27566 Note that this replacement of `>' for `>>' is necessary
27567 even if we are parsing tentatively: in the tentative
27568 case, after calling
27569 cp_parser_enclosed_template_argument_list we will always
27570 throw away all of the template arguments and the first
27571 closing `>', either because the template argument list
27572 was erroneous or because we are replacing those tokens
27573 with a CPP_TEMPLATE_ID token. The second `>' (which will
27574 not have been thrown away) is needed either to close an
27575 outer template argument list or to complete a new-style
27577 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27578 token
->type
= CPP_GREATER
;
27580 else if (!saved_greater_than_is_operator_p
)
27582 /* If we're in a nested template argument list, the '>>' has
27583 to be a typo for '> >'. We emit the error message, but we
27584 continue parsing and we push a '>' as next token, so that
27585 the argument list will be parsed correctly. Note that the
27586 global source location is still on the token before the
27587 '>>', so we need to say explicitly where we want it. */
27588 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27589 gcc_rich_location
richloc (token
->location
);
27590 richloc
.add_fixit_replace ("> >");
27591 error_at (&richloc
, "%<>>%> should be %<> >%> "
27592 "within a nested template argument list");
27594 token
->type
= CPP_GREATER
;
27598 /* If this is not a nested template argument list, the '>>'
27599 is a typo for '>'. Emit an error message and continue.
27600 Same deal about the token location, but here we can get it
27601 right by consuming the '>>' before issuing the diagnostic. */
27602 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27603 error_at (token
->location
,
27604 "spurious %<>>%>, use %<>%> to terminate "
27605 "a template argument list");
27609 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27610 /* The `>' token might be a greater-than operator again now. */
27611 parser
->greater_than_is_operator_p
27612 = saved_greater_than_is_operator_p
;
27613 /* Restore the SAVED_SCOPE. */
27614 parser
->scope
= saved_scope
;
27615 parser
->qualifying_scope
= saved_qualifying_scope
;
27616 parser
->object_scope
= saved_object_scope
;
27617 cp_unevaluated_operand
= saved_unevaluated_operand
;
27618 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27623 /* MEMBER_FUNCTION is a member function, or a friend. If default
27624 arguments, or the body of the function have not yet been parsed,
27628 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27630 timevar_push (TV_PARSE_INMETH
);
27631 /* If this member is a template, get the underlying
27633 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27634 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27636 /* There should not be any class definitions in progress at this
27637 point; the bodies of members are only parsed outside of all class
27639 gcc_assert (parser
->num_classes_being_defined
== 0);
27640 /* While we're parsing the member functions we might encounter more
27641 classes. We want to handle them right away, but we don't want
27642 them getting mixed up with functions that are currently in the
27644 push_unparsed_function_queues (parser
);
27646 /* Make sure that any template parameters are in scope. */
27647 maybe_begin_member_template_processing (member_function
);
27649 /* If the body of the function has not yet been parsed, parse it
27651 if (DECL_PENDING_INLINE_P (member_function
))
27653 tree function_scope
;
27654 cp_token_cache
*tokens
;
27656 /* The function is no longer pending; we are processing it. */
27657 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27658 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27659 DECL_PENDING_INLINE_P (member_function
) = 0;
27661 /* If this is a local class, enter the scope of the containing
27663 function_scope
= current_function_decl
;
27664 if (function_scope
)
27665 push_function_context ();
27667 /* Push the body of the function onto the lexer stack. */
27668 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27670 /* Let the front end know that we going to be defining this
27672 start_preparsed_function (member_function
, NULL_TREE
,
27673 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27675 /* Don't do access checking if it is a templated function. */
27676 if (processing_template_decl
)
27677 push_deferring_access_checks (dk_no_check
);
27679 /* #pragma omp declare reduction needs special parsing. */
27680 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27682 parser
->lexer
->in_pragma
= true;
27683 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27684 finish_function (/*inline_p=*/true);
27685 cp_check_omp_declare_reduction (member_function
);
27688 /* Now, parse the body of the function. */
27689 cp_parser_function_definition_after_declarator (parser
,
27690 /*inline_p=*/true);
27692 if (processing_template_decl
)
27693 pop_deferring_access_checks ();
27695 /* Leave the scope of the containing function. */
27696 if (function_scope
)
27697 pop_function_context ();
27698 cp_parser_pop_lexer (parser
);
27701 /* Remove any template parameters from the symbol table. */
27702 maybe_end_member_template_processing ();
27704 /* Restore the queue. */
27705 pop_unparsed_function_queues (parser
);
27706 timevar_pop (TV_PARSE_INMETH
);
27709 /* If DECL contains any default args, remember it on the unparsed
27710 functions queue. */
27713 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27717 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27719 probe
= TREE_CHAIN (probe
))
27720 if (TREE_PURPOSE (probe
))
27722 cp_default_arg_entry entry
= {current_class_type
, decl
};
27723 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27728 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27729 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27730 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27731 from the parameter-type-list. */
27734 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27735 tree default_arg
, tree parmtype
)
27737 cp_token_cache
*tokens
;
27741 if (default_arg
== error_mark_node
)
27742 return error_mark_node
;
27744 /* Push the saved tokens for the default argument onto the parser's
27746 tokens
= DEFARG_TOKENS (default_arg
);
27747 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27749 start_lambda_scope (decl
);
27751 /* Parse the default argument. */
27752 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27753 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27756 finish_lambda_scope ();
27758 if (parsed_arg
== error_mark_node
)
27759 cp_parser_skip_to_end_of_statement (parser
);
27761 if (!processing_template_decl
)
27763 /* In a non-template class, check conversions now. In a template,
27764 we'll wait and instantiate these as needed. */
27765 if (TREE_CODE (decl
) == PARM_DECL
)
27766 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27767 tf_warning_or_error
);
27768 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27769 parsed_arg
= error_mark_node
;
27771 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27774 /* If the token stream has not been completely used up, then
27775 there was extra junk after the end of the default
27777 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27779 if (TREE_CODE (decl
) == PARM_DECL
)
27780 cp_parser_error (parser
, "expected %<,%>");
27782 cp_parser_error (parser
, "expected %<;%>");
27785 /* Revert to the main lexer. */
27786 cp_parser_pop_lexer (parser
);
27791 /* FIELD is a non-static data member with an initializer which we saved for
27792 later; parse it now. */
27795 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27799 maybe_begin_member_template_processing (field
);
27801 push_unparsed_function_queues (parser
);
27802 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27803 DECL_INITIAL (field
),
27805 pop_unparsed_function_queues (parser
);
27807 maybe_end_member_template_processing ();
27809 DECL_INITIAL (field
) = def
;
27812 /* FN is a FUNCTION_DECL which may contains a parameter with an
27813 unparsed DEFAULT_ARG. Parse the default args now. This function
27814 assumes that the current scope is the scope in which the default
27815 argument should be processed. */
27818 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27820 bool saved_local_variables_forbidden_p
;
27821 tree parm
, parmdecl
;
27823 /* While we're parsing the default args, we might (due to the
27824 statement expression extension) encounter more classes. We want
27825 to handle them right away, but we don't want them getting mixed
27826 up with default args that are currently in the queue. */
27827 push_unparsed_function_queues (parser
);
27829 /* Local variable names (and the `this' keyword) may not appear
27830 in a default argument. */
27831 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27832 parser
->local_variables_forbidden_p
= true;
27834 push_defarg_context (fn
);
27836 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27837 parmdecl
= DECL_ARGUMENTS (fn
);
27838 parm
&& parm
!= void_list_node
;
27839 parm
= TREE_CHAIN (parm
),
27840 parmdecl
= DECL_CHAIN (parmdecl
))
27842 tree default_arg
= TREE_PURPOSE (parm
);
27844 vec
<tree
, va_gc
> *insts
;
27851 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27852 /* This can happen for a friend declaration for a function
27853 already declared with default arguments. */
27857 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27859 TREE_VALUE (parm
));
27860 TREE_PURPOSE (parm
) = parsed_arg
;
27862 /* Update any instantiations we've already created. */
27863 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27864 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27865 TREE_PURPOSE (copy
) = parsed_arg
;
27868 pop_defarg_context ();
27870 /* Make sure no default arg is missing. */
27871 check_default_args (fn
);
27873 /* Restore the state of local_variables_forbidden_p. */
27874 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27876 /* Restore the queue. */
27877 pop_unparsed_function_queues (parser
);
27880 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27882 sizeof ... ( identifier )
27884 where the 'sizeof' token has already been consumed. */
27887 cp_parser_sizeof_pack (cp_parser
*parser
)
27889 /* Consume the `...'. */
27890 cp_lexer_consume_token (parser
->lexer
);
27891 maybe_warn_variadic_templates ();
27893 matching_parens parens
;
27894 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27896 parens
.consume_open (parser
);
27898 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27899 "%<sizeof...%> argument must be surrounded by parentheses");
27901 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27902 tree name
= cp_parser_identifier (parser
);
27903 if (name
== error_mark_node
)
27904 return error_mark_node
;
27905 /* The name is not qualified. */
27906 parser
->scope
= NULL_TREE
;
27907 parser
->qualifying_scope
= NULL_TREE
;
27908 parser
->object_scope
= NULL_TREE
;
27909 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27910 if (expr
== error_mark_node
)
27911 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27913 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27914 expr
= TREE_TYPE (expr
);
27915 else if (TREE_CODE (expr
) == CONST_DECL
)
27916 expr
= DECL_INITIAL (expr
);
27917 expr
= make_pack_expansion (expr
);
27918 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27921 parens
.require_close (parser
);
27926 /* Parse the operand of `sizeof' (or a similar operator). Returns
27927 either a TYPE or an expression, depending on the form of the
27928 input. The KEYWORD indicates which kind of expression we have
27932 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27934 tree expr
= NULL_TREE
;
27935 const char *saved_message
;
27937 bool saved_integral_constant_expression_p
;
27938 bool saved_non_integral_constant_expression_p
;
27940 /* If it's a `...', then we are computing the length of a parameter
27942 if (keyword
== RID_SIZEOF
27943 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27944 return cp_parser_sizeof_pack (parser
);
27946 /* Types cannot be defined in a `sizeof' expression. Save away the
27948 saved_message
= parser
->type_definition_forbidden_message
;
27949 /* And create the new one. */
27950 tmp
= concat ("types may not be defined in %<",
27951 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27952 "%> expressions", NULL
);
27953 parser
->type_definition_forbidden_message
= tmp
;
27955 /* The restrictions on constant-expressions do not apply inside
27956 sizeof expressions. */
27957 saved_integral_constant_expression_p
27958 = parser
->integral_constant_expression_p
;
27959 saved_non_integral_constant_expression_p
27960 = parser
->non_integral_constant_expression_p
;
27961 parser
->integral_constant_expression_p
= false;
27963 /* Do not actually evaluate the expression. */
27964 ++cp_unevaluated_operand
;
27965 ++c_inhibit_evaluation_warnings
;
27966 /* If it's a `(', then we might be looking at the type-id
27968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27970 tree type
= NULL_TREE
;
27972 /* We can't be sure yet whether we're looking at a type-id or an
27974 cp_parser_parse_tentatively (parser
);
27976 matching_parens parens
;
27977 parens
.consume_open (parser
);
27979 /* Note: as a GNU Extension, compound literals are considered
27980 postfix-expressions as they are in C99, so they are valid
27981 arguments to sizeof. See comment in cp_parser_cast_expression
27983 if (cp_parser_compound_literal_p (parser
))
27984 cp_parser_simulate_error (parser
);
27987 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27988 parser
->in_type_id_in_expr_p
= true;
27989 /* Look for the type-id. */
27990 type
= cp_parser_type_id (parser
);
27991 /* Look for the closing `)'. */
27992 parens
.require_close (parser
);
27993 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
27996 /* If all went well, then we're done. */
27997 if (cp_parser_parse_definitely (parser
))
27999 cp_decl_specifier_seq decl_specs
;
28001 /* Build a trivial decl-specifier-seq. */
28002 clear_decl_specs (&decl_specs
);
28003 decl_specs
.type
= type
;
28005 /* Call grokdeclarator to figure out what type this is. */
28006 expr
= grokdeclarator (NULL
,
28010 /*attrlist=*/NULL
);
28014 /* If the type-id production did not work out, then we must be
28015 looking at the unary-expression production. */
28017 expr
= cp_parser_unary_expression (parser
);
28019 /* Go back to evaluating expressions. */
28020 --cp_unevaluated_operand
;
28021 --c_inhibit_evaluation_warnings
;
28023 /* Free the message we created. */
28025 /* And restore the old one. */
28026 parser
->type_definition_forbidden_message
= saved_message
;
28027 parser
->integral_constant_expression_p
28028 = saved_integral_constant_expression_p
;
28029 parser
->non_integral_constant_expression_p
28030 = saved_non_integral_constant_expression_p
;
28035 /* If the current declaration has no declarator, return true. */
28038 cp_parser_declares_only_class_p (cp_parser
*parser
)
28040 /* If the next token is a `;' or a `,' then there is no
28042 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28043 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28046 /* Update the DECL_SPECS to reflect the storage class indicated by
28050 cp_parser_set_storage_class (cp_parser
*parser
,
28051 cp_decl_specifier_seq
*decl_specs
,
28055 cp_storage_class storage_class
;
28057 if (parser
->in_unbraced_linkage_specification_p
)
28059 error_at (token
->location
, "invalid use of %qD in linkage specification",
28060 ridpointers
[keyword
]);
28063 else if (decl_specs
->storage_class
!= sc_none
)
28065 decl_specs
->conflicting_specifiers_p
= true;
28069 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28070 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28071 && decl_specs
->gnu_thread_keyword_p
)
28073 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28074 "%<__thread%> before %qD", ridpointers
[keyword
]);
28080 storage_class
= sc_auto
;
28083 storage_class
= sc_register
;
28086 storage_class
= sc_static
;
28089 storage_class
= sc_extern
;
28092 storage_class
= sc_mutable
;
28095 gcc_unreachable ();
28097 decl_specs
->storage_class
= storage_class
;
28098 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28100 /* A storage class specifier cannot be applied alongside a typedef
28101 specifier. If there is a typedef specifier present then set
28102 conflicting_specifiers_p which will trigger an error later
28103 on in grokdeclarator. */
28104 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28105 decl_specs
->conflicting_specifiers_p
= true;
28108 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28109 is true, the type is a class or enum definition. */
28112 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28115 bool type_definition_p
)
28117 decl_specs
->any_specifiers_p
= true;
28119 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28120 (with, for example, in "typedef int wchar_t;") we remember that
28121 this is what happened. In system headers, we ignore these
28122 declarations so that G++ can work with system headers that are not
28124 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28125 && !type_definition_p
28126 && (type_spec
== boolean_type_node
28127 || type_spec
== char16_type_node
28128 || type_spec
== char32_type_node
28129 || type_spec
== wchar_type_node
)
28130 && (decl_specs
->type
28131 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28132 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28133 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28134 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28136 decl_specs
->redefined_builtin_type
= type_spec
;
28137 set_and_check_decl_spec_loc (decl_specs
,
28138 ds_redefined_builtin_type_spec
,
28140 if (!decl_specs
->type
)
28142 decl_specs
->type
= type_spec
;
28143 decl_specs
->type_definition_p
= false;
28144 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28147 else if (decl_specs
->type
)
28148 decl_specs
->multiple_types_p
= true;
28151 decl_specs
->type
= type_spec
;
28152 decl_specs
->type_definition_p
= type_definition_p
;
28153 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28154 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28158 /* True iff TOKEN is the GNU keyword __thread. */
28161 token_is__thread (cp_token
*token
)
28163 gcc_assert (token
->keyword
== RID_THREAD
);
28164 return id_equal (token
->u
.value
, "__thread");
28167 /* Set the location for a declarator specifier and check if it is
28170 DECL_SPECS is the sequence of declarator specifiers onto which to
28173 DS is the single declarator specifier to set which location is to
28174 be set onto the existing sequence of declarators.
28176 LOCATION is the location for the declarator specifier to
28180 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28181 cp_decl_spec ds
, cp_token
*token
)
28183 gcc_assert (ds
< ds_last
);
28185 if (decl_specs
== NULL
)
28188 source_location location
= token
->location
;
28190 if (decl_specs
->locations
[ds
] == 0)
28192 decl_specs
->locations
[ds
] = location
;
28193 if (ds
== ds_thread
)
28194 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28200 if (decl_specs
->locations
[ds_long_long
] != 0)
28201 error_at (location
,
28202 "%<long long long%> is too long for GCC");
28205 decl_specs
->locations
[ds_long_long
] = location
;
28206 pedwarn_cxx98 (location
,
28208 "ISO C++ 1998 does not support %<long long%>");
28211 else if (ds
== ds_thread
)
28213 bool gnu
= token_is__thread (token
);
28214 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28215 error_at (location
,
28216 "both %<__thread%> and %<thread_local%> specified");
28219 gcc_rich_location
richloc (location
);
28220 richloc
.add_fixit_remove ();
28221 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28226 static const char *const decl_spec_names
[] = {
28243 gcc_rich_location
richloc (location
);
28244 richloc
.add_fixit_remove ();
28245 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28250 /* Return true iff the declarator specifier DS is present in the
28251 sequence of declarator specifiers DECL_SPECS. */
28254 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28257 gcc_assert (ds
< ds_last
);
28259 if (decl_specs
== NULL
)
28262 return decl_specs
->locations
[ds
] != 0;
28265 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28266 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28269 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28271 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28274 /* Issue an error message indicating that TOKEN_DESC was expected.
28275 If KEYWORD is true, it indicated this function is called by
28276 cp_parser_require_keword and the required token can only be
28277 a indicated keyword.
28279 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28280 within any error as the location of an "opening" token matching
28281 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28282 RT_CLOSE_PAREN). */
28285 cp_parser_required_error (cp_parser
*parser
,
28286 required_token token_desc
,
28288 location_t matching_location
)
28290 if (cp_parser_simulate_error (parser
))
28293 const char *gmsgid
= NULL
;
28294 switch (token_desc
)
28297 gmsgid
= G_("expected %<new%>");
28300 gmsgid
= G_("expected %<delete%>");
28303 gmsgid
= G_("expected %<return%>");
28306 gmsgid
= G_("expected %<while%>");
28309 gmsgid
= G_("expected %<extern%>");
28311 case RT_STATIC_ASSERT
:
28312 gmsgid
= G_("expected %<static_assert%>");
28315 gmsgid
= G_("expected %<decltype%>");
28318 gmsgid
= G_("expected %<operator%>");
28321 gmsgid
= G_("expected %<class%>");
28324 gmsgid
= G_("expected %<template%>");
28327 gmsgid
= G_("expected %<namespace%>");
28330 gmsgid
= G_("expected %<using%>");
28333 gmsgid
= G_("expected %<asm%>");
28336 gmsgid
= G_("expected %<try%>");
28339 gmsgid
= G_("expected %<catch%>");
28342 gmsgid
= G_("expected %<throw%>");
28345 gmsgid
= G_("expected %<__label__%>");
28348 gmsgid
= G_("expected %<@try%>");
28350 case RT_AT_SYNCHRONIZED
:
28351 gmsgid
= G_("expected %<@synchronized%>");
28354 gmsgid
= G_("expected %<@throw%>");
28356 case RT_TRANSACTION_ATOMIC
:
28357 gmsgid
= G_("expected %<__transaction_atomic%>");
28359 case RT_TRANSACTION_RELAXED
:
28360 gmsgid
= G_("expected %<__transaction_relaxed%>");
28366 if (!gmsgid
&& !keyword
)
28368 switch (token_desc
)
28371 gmsgid
= G_("expected %<;%>");
28373 case RT_OPEN_PAREN
:
28374 gmsgid
= G_("expected %<(%>");
28376 case RT_CLOSE_BRACE
:
28377 gmsgid
= G_("expected %<}%>");
28379 case RT_OPEN_BRACE
:
28380 gmsgid
= G_("expected %<{%>");
28382 case RT_CLOSE_SQUARE
:
28383 gmsgid
= G_("expected %<]%>");
28385 case RT_OPEN_SQUARE
:
28386 gmsgid
= G_("expected %<[%>");
28389 gmsgid
= G_("expected %<,%>");
28392 gmsgid
= G_("expected %<::%>");
28395 gmsgid
= G_("expected %<<%>");
28398 gmsgid
= G_("expected %<>%>");
28401 gmsgid
= G_("expected %<=%>");
28404 gmsgid
= G_("expected %<...%>");
28407 gmsgid
= G_("expected %<*%>");
28410 gmsgid
= G_("expected %<~%>");
28413 gmsgid
= G_("expected %<:%>");
28415 case RT_COLON_SCOPE
:
28416 gmsgid
= G_("expected %<:%> or %<::%>");
28418 case RT_CLOSE_PAREN
:
28419 gmsgid
= G_("expected %<)%>");
28421 case RT_COMMA_CLOSE_PAREN
:
28422 gmsgid
= G_("expected %<,%> or %<)%>");
28424 case RT_PRAGMA_EOL
:
28425 gmsgid
= G_("expected end of line");
28428 gmsgid
= G_("expected identifier");
28431 gmsgid
= G_("expected selection-statement");
28434 gmsgid
= G_("expected iteration-statement");
28437 gmsgid
= G_("expected jump-statement");
28440 gmsgid
= G_("expected class-key");
28442 case RT_CLASS_TYPENAME_TEMPLATE
:
28443 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28446 gcc_unreachable ();
28451 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28455 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28456 issue an error message indicating that TOKEN_DESC was expected.
28458 Returns the token consumed, if the token had the appropriate type.
28459 Otherwise, returns NULL.
28461 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28462 within any error as the location of an "opening" token matching
28463 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28464 RT_CLOSE_PAREN). */
28467 cp_parser_require (cp_parser
* parser
,
28468 enum cpp_ttype type
,
28469 required_token token_desc
,
28470 location_t matching_location
)
28472 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28473 return cp_lexer_consume_token (parser
->lexer
);
28476 /* Output the MESSAGE -- unless we're parsing tentatively. */
28477 if (!cp_parser_simulate_error (parser
))
28478 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28479 matching_location
);
28484 /* An error message is produced if the next token is not '>'.
28485 All further tokens are skipped until the desired token is
28486 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28489 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28491 /* Current level of '< ... >'. */
28492 unsigned level
= 0;
28493 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28494 unsigned nesting_depth
= 0;
28496 /* Are we ready, yet? If not, issue error message. */
28497 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28500 /* Skip tokens until the desired token is found. */
28503 /* Peek at the next token. */
28504 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28507 if (!nesting_depth
)
28512 if (cxx_dialect
== cxx98
)
28513 /* C++0x views the `>>' operator as two `>' tokens, but
28516 else if (!nesting_depth
&& level
-- == 0)
28518 /* We've hit a `>>' where the first `>' closes the
28519 template argument list, and the second `>' is
28520 spurious. Just consume the `>>' and stop; we've
28521 already produced at least one error. */
28522 cp_lexer_consume_token (parser
->lexer
);
28525 /* Fall through for C++0x, so we handle the second `>' in
28527 gcc_fallthrough ();
28530 if (!nesting_depth
&& level
-- == 0)
28532 /* We've reached the token we want, consume it and stop. */
28533 cp_lexer_consume_token (parser
->lexer
);
28538 case CPP_OPEN_PAREN
:
28539 case CPP_OPEN_SQUARE
:
28543 case CPP_CLOSE_PAREN
:
28544 case CPP_CLOSE_SQUARE
:
28545 if (nesting_depth
-- == 0)
28550 case CPP_PRAGMA_EOL
:
28551 case CPP_SEMICOLON
:
28552 case CPP_OPEN_BRACE
:
28553 case CPP_CLOSE_BRACE
:
28554 /* The '>' was probably forgotten, don't look further. */
28561 /* Consume this token. */
28562 cp_lexer_consume_token (parser
->lexer
);
28566 /* If the next token is the indicated keyword, consume it. Otherwise,
28567 issue an error message indicating that TOKEN_DESC was expected.
28569 Returns the token consumed, if the token had the appropriate type.
28570 Otherwise, returns NULL. */
28573 cp_parser_require_keyword (cp_parser
* parser
,
28575 required_token token_desc
)
28577 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28579 if (token
&& token
->keyword
!= keyword
)
28581 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28589 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28590 function-definition. */
28593 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28595 return (/* An ordinary function-body begins with an `{'. */
28596 token
->type
== CPP_OPEN_BRACE
28597 /* A ctor-initializer begins with a `:'. */
28598 || token
->type
== CPP_COLON
28599 /* A function-try-block begins with `try'. */
28600 || token
->keyword
== RID_TRY
28601 /* A function-transaction-block begins with `__transaction_atomic'
28602 or `__transaction_relaxed'. */
28603 || token
->keyword
== RID_TRANSACTION_ATOMIC
28604 || token
->keyword
== RID_TRANSACTION_RELAXED
28605 /* The named return value extension begins with `return'. */
28606 || token
->keyword
== RID_RETURN
);
28609 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28613 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28617 token
= cp_lexer_peek_token (parser
->lexer
);
28618 return (token
->type
== CPP_OPEN_BRACE
28619 || (token
->type
== CPP_COLON
28620 && !parser
->colon_doesnt_start_class_def_p
));
28623 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28624 C++0x) ending a template-argument. */
28627 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28631 token
= cp_lexer_peek_token (parser
->lexer
);
28632 return (token
->type
== CPP_COMMA
28633 || token
->type
== CPP_GREATER
28634 || token
->type
== CPP_ELLIPSIS
28635 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28638 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28639 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28642 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28647 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28648 if (token
->type
== CPP_LESS
)
28650 /* Check for the sequence `<::' in the original code. It would be lexed as
28651 `[:', where `[' is a digraph, and there is no whitespace before
28653 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28656 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28657 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28663 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28664 or none_type otherwise. */
28666 static enum tag_types
28667 cp_parser_token_is_class_key (cp_token
* token
)
28669 switch (token
->keyword
)
28674 return record_type
;
28683 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28684 or none_type otherwise or if the token is null. */
28686 static enum tag_types
28687 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28692 switch (token
->keyword
)
28697 return typename_type
;
28704 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28707 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28709 if (type
== error_mark_node
)
28711 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28713 if (permerror (input_location
, "%qs tag used in naming %q#T",
28714 class_key
== union_type
? "union"
28715 : class_key
== record_type
? "struct" : "class",
28717 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28718 "%q#T was previously declared here", type
);
28722 /* Issue an error message if DECL is redeclared with different
28723 access than its original declaration [class.access.spec/3].
28724 This applies to nested classes, nested class templates and
28725 enumerations [class.mem/1]. */
28728 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28731 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28732 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28735 if ((TREE_PRIVATE (decl
)
28736 != (current_access_specifier
== access_private_node
))
28737 || (TREE_PROTECTED (decl
)
28738 != (current_access_specifier
== access_protected_node
)))
28739 error_at (location
, "%qD redeclared with different access", decl
);
28742 /* Look for the `template' keyword, as a syntactic disambiguator.
28743 Return TRUE iff it is present, in which case it will be
28747 cp_parser_optional_template_keyword (cp_parser
*parser
)
28749 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28751 /* In C++98 the `template' keyword can only be used within templates;
28752 outside templates the parser can always figure out what is a
28753 template and what is not. In C++11, per the resolution of DR 468,
28754 `template' is allowed in cases where it is not strictly necessary. */
28755 if (!processing_template_decl
28756 && pedantic
&& cxx_dialect
== cxx98
)
28758 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28759 pedwarn (token
->location
, OPT_Wpedantic
,
28760 "in C++98 %<template%> (as a disambiguator) is only "
28761 "allowed within templates");
28762 /* If this part of the token stream is rescanned, the same
28763 error message would be generated. So, we purge the token
28764 from the stream. */
28765 cp_lexer_purge_token (parser
->lexer
);
28770 /* Consume the `template' keyword. */
28771 cp_lexer_consume_token (parser
->lexer
);
28778 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28779 set PARSER->SCOPE, and perform other related actions. */
28782 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28784 struct tree_check
*check_value
;
28786 /* Get the stored value. */
28787 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28788 /* Set the scope from the stored value. */
28789 parser
->scope
= saved_checks_value (check_value
);
28790 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28791 parser
->object_scope
= NULL_TREE
;
28794 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28795 encounter the end of a block before what we were looking for. */
28798 cp_parser_cache_group (cp_parser
*parser
,
28799 enum cpp_ttype end
,
28804 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28806 /* Abort a parenthesized expression if we encounter a semicolon. */
28807 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28808 && token
->type
== CPP_SEMICOLON
)
28810 /* If we've reached the end of the file, stop. */
28811 if (token
->type
== CPP_EOF
28812 || (end
!= CPP_PRAGMA_EOL
28813 && token
->type
== CPP_PRAGMA_EOL
))
28815 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28816 /* We've hit the end of an enclosing block, so there's been some
28817 kind of syntax error. */
28820 /* Consume the token. */
28821 cp_lexer_consume_token (parser
->lexer
);
28822 /* See if it starts a new group. */
28823 if (token
->type
== CPP_OPEN_BRACE
)
28825 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28826 /* In theory this should probably check end == '}', but
28827 cp_parser_save_member_function_body needs it to exit
28828 after either '}' or ')' when called with ')'. */
28832 else if (token
->type
== CPP_OPEN_PAREN
)
28834 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28835 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28838 else if (token
->type
== CPP_PRAGMA
)
28839 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28840 else if (token
->type
== end
)
28845 /* Like above, for caching a default argument or NSDMI. Both of these are
28846 terminated by a non-nested comma, but it can be unclear whether or not a
28847 comma is nested in a template argument list unless we do more parsing.
28848 In order to handle this ambiguity, when we encounter a ',' after a '<'
28849 we try to parse what follows as a parameter-declaration-list (in the
28850 case of a default argument) or a member-declarator (in the case of an
28851 NSDMI). If that succeeds, then we stop caching. */
28854 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28856 unsigned depth
= 0;
28857 int maybe_template_id
= 0;
28858 cp_token
*first_token
;
28860 tree default_argument
;
28862 /* Add tokens until we have processed the entire default
28863 argument. We add the range [first_token, token). */
28864 first_token
= cp_lexer_peek_token (parser
->lexer
);
28865 if (first_token
->type
== CPP_OPEN_BRACE
)
28867 /* For list-initialization, this is straightforward. */
28868 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28869 token
= cp_lexer_peek_token (parser
->lexer
);
28875 /* Peek at the next token. */
28876 token
= cp_lexer_peek_token (parser
->lexer
);
28877 /* What we do depends on what token we have. */
28878 switch (token
->type
)
28880 /* In valid code, a default argument must be
28881 immediately followed by a `,' `)', or `...'. */
28883 if (depth
== 0 && maybe_template_id
)
28885 /* If we've seen a '<', we might be in a
28886 template-argument-list. Until Core issue 325 is
28887 resolved, we don't know how this situation ought
28888 to be handled, so try to DTRT. We check whether
28889 what comes after the comma is a valid parameter
28890 declaration list. If it is, then the comma ends
28891 the default argument; otherwise the default
28892 argument continues. */
28893 bool error
= false;
28896 /* Set ITALP so cp_parser_parameter_declaration_list
28897 doesn't decide to commit to this parse. */
28898 bool saved_italp
= parser
->in_template_argument_list_p
;
28899 parser
->in_template_argument_list_p
= true;
28901 cp_parser_parse_tentatively (parser
);
28905 /* Parse declarators until we reach a non-comma or
28906 somthing that cannot be an initializer.
28907 Just checking whether we're looking at a single
28908 declarator is insufficient. Consider:
28909 int var = tuple<T,U>::x;
28910 The template parameter 'U' looks exactly like a
28914 int ctor_dtor_or_conv_p
;
28915 cp_lexer_consume_token (parser
->lexer
);
28916 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28917 &ctor_dtor_or_conv_p
,
28918 /*parenthesized_p=*/NULL
,
28920 /*friend_p=*/false);
28921 peek
= cp_lexer_peek_token (parser
->lexer
);
28922 if (cp_parser_error_occurred (parser
))
28925 while (peek
->type
== CPP_COMMA
);
28926 /* If we met an '=' or ';' then the original comma
28927 was the end of the NSDMI. Otherwise assume
28928 we're still in the NSDMI. */
28929 error
= (peek
->type
!= CPP_EQ
28930 && peek
->type
!= CPP_SEMICOLON
);
28934 cp_lexer_consume_token (parser
->lexer
);
28935 begin_scope (sk_function_parms
, NULL_TREE
);
28936 cp_parser_parameter_declaration_list (parser
, &error
);
28937 pop_bindings_and_leave_scope ();
28939 if (!cp_parser_error_occurred (parser
) && !error
)
28941 cp_parser_abort_tentative_parse (parser
);
28943 parser
->in_template_argument_list_p
= saved_italp
;
28947 case CPP_CLOSE_PAREN
:
28949 /* If we run into a non-nested `;', `}', or `]',
28950 then the code is invalid -- but the default
28951 argument is certainly over. */
28952 case CPP_SEMICOLON
:
28953 case CPP_CLOSE_BRACE
:
28954 case CPP_CLOSE_SQUARE
:
28956 /* Handle correctly int n = sizeof ... ( p ); */
28957 && token
->type
!= CPP_ELLIPSIS
)
28959 /* Update DEPTH, if necessary. */
28960 else if (token
->type
== CPP_CLOSE_PAREN
28961 || token
->type
== CPP_CLOSE_BRACE
28962 || token
->type
== CPP_CLOSE_SQUARE
)
28966 case CPP_OPEN_PAREN
:
28967 case CPP_OPEN_SQUARE
:
28968 case CPP_OPEN_BRACE
:
28974 /* This might be the comparison operator, or it might
28975 start a template argument list. */
28976 ++maybe_template_id
;
28980 if (cxx_dialect
== cxx98
)
28982 /* Fall through for C++0x, which treats the `>>'
28983 operator like two `>' tokens in certain
28985 gcc_fallthrough ();
28990 /* This might be an operator, or it might close a
28991 template argument list. But if a previous '<'
28992 started a template argument list, this will have
28993 closed it, so we can't be in one anymore. */
28994 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
28995 if (maybe_template_id
< 0)
28996 maybe_template_id
= 0;
29000 /* If we run out of tokens, issue an error message. */
29002 case CPP_PRAGMA_EOL
:
29003 error_at (token
->location
, "file ends in default argument");
29004 return error_mark_node
;
29008 /* In these cases, we should look for template-ids.
29009 For example, if the default argument is
29010 `X<int, double>()', we need to do name lookup to
29011 figure out whether or not `X' is a template; if
29012 so, the `,' does not end the default argument.
29014 That is not yet done. */
29021 /* If we've reached the end, stop. */
29025 /* Add the token to the token block. */
29026 token
= cp_lexer_consume_token (parser
->lexer
);
29029 /* Create a DEFAULT_ARG to represent the unparsed default
29031 default_argument
= make_node (DEFAULT_ARG
);
29032 DEFARG_TOKENS (default_argument
)
29033 = cp_token_cache_new (first_token
, token
);
29034 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29036 return default_argument
;
29039 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29042 defarg_location (tree default_argument
)
29044 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29045 location_t start
= tokens
->first
->location
;
29046 location_t end
= tokens
->last
->location
;
29047 return make_location (start
, start
, end
);
29050 /* Begin parsing tentatively. We always save tokens while parsing
29051 tentatively so that if the tentative parsing fails we can restore the
29055 cp_parser_parse_tentatively (cp_parser
* parser
)
29057 /* Enter a new parsing context. */
29058 parser
->context
= cp_parser_context_new (parser
->context
);
29059 /* Begin saving tokens. */
29060 cp_lexer_save_tokens (parser
->lexer
);
29061 /* In order to avoid repetitive access control error messages,
29062 access checks are queued up until we are no longer parsing
29064 push_deferring_access_checks (dk_deferred
);
29067 /* Commit to the currently active tentative parse. */
29070 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29072 cp_parser_context
*context
;
29075 /* Mark all of the levels as committed. */
29076 lexer
= parser
->lexer
;
29077 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29079 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29081 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29082 while (!cp_lexer_saving_tokens (lexer
))
29083 lexer
= lexer
->next
;
29084 cp_lexer_commit_tokens (lexer
);
29088 /* Commit to the topmost currently active tentative parse.
29090 Note that this function shouldn't be called when there are
29091 irreversible side-effects while in a tentative state. For
29092 example, we shouldn't create a permanent entry in the symbol
29093 table, or issue an error message that might not apply if the
29094 tentative parse is aborted. */
29097 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29099 cp_parser_context
*context
= parser
->context
;
29100 cp_lexer
*lexer
= parser
->lexer
;
29104 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29106 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29108 while (!cp_lexer_saving_tokens (lexer
))
29109 lexer
= lexer
->next
;
29110 cp_lexer_commit_tokens (lexer
);
29114 /* Abort the currently active tentative parse. All consumed tokens
29115 will be rolled back, and no diagnostics will be issued. */
29118 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29120 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29121 || errorcount
> 0);
29122 cp_parser_simulate_error (parser
);
29123 /* Now, pretend that we want to see if the construct was
29124 successfully parsed. */
29125 cp_parser_parse_definitely (parser
);
29128 /* Stop parsing tentatively. If a parse error has occurred, restore the
29129 token stream. Otherwise, commit to the tokens we have consumed.
29130 Returns true if no error occurred; false otherwise. */
29133 cp_parser_parse_definitely (cp_parser
* parser
)
29135 bool error_occurred
;
29136 cp_parser_context
*context
;
29138 /* Remember whether or not an error occurred, since we are about to
29139 destroy that information. */
29140 error_occurred
= cp_parser_error_occurred (parser
);
29141 /* Remove the topmost context from the stack. */
29142 context
= parser
->context
;
29143 parser
->context
= context
->next
;
29144 /* If no parse errors occurred, commit to the tentative parse. */
29145 if (!error_occurred
)
29147 /* Commit to the tokens read tentatively, unless that was
29149 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29150 cp_lexer_commit_tokens (parser
->lexer
);
29152 pop_to_parent_deferring_access_checks ();
29154 /* Otherwise, if errors occurred, roll back our state so that things
29155 are just as they were before we began the tentative parse. */
29158 cp_lexer_rollback_tokens (parser
->lexer
);
29159 pop_deferring_access_checks ();
29161 /* Add the context to the front of the free list. */
29162 context
->next
= cp_parser_context_free_list
;
29163 cp_parser_context_free_list
= context
;
29165 return !error_occurred
;
29168 /* Returns true if we are parsing tentatively and are not committed to
29169 this tentative parse. */
29172 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29174 return (cp_parser_parsing_tentatively (parser
)
29175 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29178 /* Returns nonzero iff an error has occurred during the most recent
29179 tentative parse. */
29182 cp_parser_error_occurred (cp_parser
* parser
)
29184 return (cp_parser_parsing_tentatively (parser
)
29185 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29188 /* Returns nonzero if GNU extensions are allowed. */
29191 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29193 return parser
->allow_gnu_extensions_p
;
29196 /* Objective-C++ Productions */
29199 /* Parse an Objective-C expression, which feeds into a primary-expression
29203 objc-message-expression
29204 objc-string-literal
29205 objc-encode-expression
29206 objc-protocol-expression
29207 objc-selector-expression
29209 Returns a tree representation of the expression. */
29212 cp_parser_objc_expression (cp_parser
* parser
)
29214 /* Try to figure out what kind of declaration is present. */
29215 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29219 case CPP_OPEN_SQUARE
:
29220 return cp_parser_objc_message_expression (parser
);
29222 case CPP_OBJC_STRING
:
29223 kwd
= cp_lexer_consume_token (parser
->lexer
);
29224 return objc_build_string_object (kwd
->u
.value
);
29227 switch (kwd
->keyword
)
29229 case RID_AT_ENCODE
:
29230 return cp_parser_objc_encode_expression (parser
);
29232 case RID_AT_PROTOCOL
:
29233 return cp_parser_objc_protocol_expression (parser
);
29235 case RID_AT_SELECTOR
:
29236 return cp_parser_objc_selector_expression (parser
);
29243 error_at (kwd
->location
,
29244 "misplaced %<@%D%> Objective-C++ construct",
29246 cp_parser_skip_to_end_of_block_or_statement (parser
);
29249 return error_mark_node
;
29252 /* Parse an Objective-C message expression.
29254 objc-message-expression:
29255 [ objc-message-receiver objc-message-args ]
29257 Returns a representation of an Objective-C message. */
29260 cp_parser_objc_message_expression (cp_parser
* parser
)
29262 tree receiver
, messageargs
;
29264 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29265 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29266 receiver
= cp_parser_objc_message_receiver (parser
);
29267 messageargs
= cp_parser_objc_message_args (parser
);
29268 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29269 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29271 tree result
= objc_build_message_expr (receiver
, messageargs
);
29273 /* Construct a location e.g.
29276 ranging from the '[' to the ']', with the caret at the start. */
29277 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29278 protected_set_expr_location (result
, combined_loc
);
29283 /* Parse an objc-message-receiver.
29285 objc-message-receiver:
29287 simple-type-specifier
29289 Returns a representation of the type or expression. */
29292 cp_parser_objc_message_receiver (cp_parser
* parser
)
29296 /* An Objective-C message receiver may be either (1) a type
29297 or (2) an expression. */
29298 cp_parser_parse_tentatively (parser
);
29299 rcv
= cp_parser_expression (parser
);
29301 /* If that worked out, fine. */
29302 if (cp_parser_parse_definitely (parser
))
29305 cp_parser_parse_tentatively (parser
);
29306 rcv
= cp_parser_simple_type_specifier (parser
,
29307 /*decl_specs=*/NULL
,
29308 CP_PARSER_FLAGS_NONE
);
29310 if (cp_parser_parse_definitely (parser
))
29311 return objc_get_class_reference (rcv
);
29313 cp_parser_error (parser
, "objective-c++ message receiver expected");
29314 return error_mark_node
;
29317 /* Parse the arguments and selectors comprising an Objective-C message.
29322 objc-selector-args , objc-comma-args
29324 objc-selector-args:
29325 objc-selector [opt] : assignment-expression
29326 objc-selector-args objc-selector [opt] : assignment-expression
29329 assignment-expression
29330 objc-comma-args , assignment-expression
29332 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29333 selector arguments and TREE_VALUE containing a list of comma
29337 cp_parser_objc_message_args (cp_parser
* parser
)
29339 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29340 bool maybe_unary_selector_p
= true;
29341 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29343 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29345 tree selector
= NULL_TREE
, arg
;
29347 if (token
->type
!= CPP_COLON
)
29348 selector
= cp_parser_objc_selector (parser
);
29350 /* Detect if we have a unary selector. */
29351 if (maybe_unary_selector_p
29352 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29353 return build_tree_list (selector
, NULL_TREE
);
29355 maybe_unary_selector_p
= false;
29356 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29357 arg
= cp_parser_assignment_expression (parser
);
29360 = chainon (sel_args
,
29361 build_tree_list (selector
, arg
));
29363 token
= cp_lexer_peek_token (parser
->lexer
);
29366 /* Handle non-selector arguments, if any. */
29367 while (token
->type
== CPP_COMMA
)
29371 cp_lexer_consume_token (parser
->lexer
);
29372 arg
= cp_parser_assignment_expression (parser
);
29375 = chainon (addl_args
,
29376 build_tree_list (NULL_TREE
, arg
));
29378 token
= cp_lexer_peek_token (parser
->lexer
);
29381 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29383 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29384 return build_tree_list (error_mark_node
, error_mark_node
);
29387 return build_tree_list (sel_args
, addl_args
);
29390 /* Parse an Objective-C encode expression.
29392 objc-encode-expression:
29393 @encode objc-typename
29395 Returns an encoded representation of the type argument. */
29398 cp_parser_objc_encode_expression (cp_parser
* parser
)
29402 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29404 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29405 matching_parens parens
;
29406 parens
.require_open (parser
);
29407 token
= cp_lexer_peek_token (parser
->lexer
);
29408 type
= complete_type (cp_parser_type_id (parser
));
29409 parens
.require_close (parser
);
29413 error_at (token
->location
,
29414 "%<@encode%> must specify a type as an argument");
29415 return error_mark_node
;
29418 /* This happens if we find @encode(T) (where T is a template
29419 typename or something dependent on a template typename) when
29420 parsing a template. In that case, we can't compile it
29421 immediately, but we rather create an AT_ENCODE_EXPR which will
29422 need to be instantiated when the template is used.
29424 if (dependent_type_p (type
))
29426 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29427 TREE_READONLY (value
) = 1;
29432 /* Build a location of the form:
29435 with caret==start at the @ token, finishing at the close paren. */
29436 location_t combined_loc
29437 = make_location (start_loc
, start_loc
,
29438 cp_lexer_previous_token (parser
->lexer
)->location
);
29440 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29443 /* Parse an Objective-C @defs expression. */
29446 cp_parser_objc_defs_expression (cp_parser
*parser
)
29450 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29451 matching_parens parens
;
29452 parens
.require_open (parser
);
29453 name
= cp_parser_identifier (parser
);
29454 parens
.require_close (parser
);
29456 return objc_get_class_ivars (name
);
29459 /* Parse an Objective-C protocol expression.
29461 objc-protocol-expression:
29462 @protocol ( identifier )
29464 Returns a representation of the protocol expression. */
29467 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29470 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29472 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29473 matching_parens parens
;
29474 parens
.require_open (parser
);
29475 proto
= cp_parser_identifier (parser
);
29476 parens
.require_close (parser
);
29478 /* Build a location of the form:
29481 with caret==start at the @ token, finishing at the close paren. */
29482 location_t combined_loc
29483 = make_location (start_loc
, start_loc
,
29484 cp_lexer_previous_token (parser
->lexer
)->location
);
29485 tree result
= objc_build_protocol_expr (proto
);
29486 protected_set_expr_location (result
, combined_loc
);
29490 /* Parse an Objective-C selector expression.
29492 objc-selector-expression:
29493 @selector ( objc-method-signature )
29495 objc-method-signature:
29501 objc-selector-seq objc-selector :
29503 Returns a representation of the method selector. */
29506 cp_parser_objc_selector_expression (cp_parser
* parser
)
29508 tree sel_seq
= NULL_TREE
;
29509 bool maybe_unary_selector_p
= true;
29511 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29513 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29514 matching_parens parens
;
29515 parens
.require_open (parser
);
29516 token
= cp_lexer_peek_token (parser
->lexer
);
29518 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29519 || token
->type
== CPP_SCOPE
)
29521 tree selector
= NULL_TREE
;
29523 if (token
->type
!= CPP_COLON
29524 || token
->type
== CPP_SCOPE
)
29525 selector
= cp_parser_objc_selector (parser
);
29527 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29528 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29530 /* Detect if we have a unary selector. */
29531 if (maybe_unary_selector_p
)
29533 sel_seq
= selector
;
29534 goto finish_selector
;
29538 cp_parser_error (parser
, "expected %<:%>");
29541 maybe_unary_selector_p
= false;
29542 token
= cp_lexer_consume_token (parser
->lexer
);
29544 if (token
->type
== CPP_SCOPE
)
29547 = chainon (sel_seq
,
29548 build_tree_list (selector
, NULL_TREE
));
29550 = chainon (sel_seq
,
29551 build_tree_list (NULL_TREE
, NULL_TREE
));
29555 = chainon (sel_seq
,
29556 build_tree_list (selector
, NULL_TREE
));
29558 token
= cp_lexer_peek_token (parser
->lexer
);
29562 parens
.require_close (parser
);
29565 /* Build a location of the form:
29568 with caret==start at the @ token, finishing at the close paren. */
29569 location_t combined_loc
29570 = make_location (loc
, loc
,
29571 cp_lexer_previous_token (parser
->lexer
)->location
);
29572 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29573 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29574 protected_set_expr_location (result
, combined_loc
);
29578 /* Parse a list of identifiers.
29580 objc-identifier-list:
29582 objc-identifier-list , identifier
29584 Returns a TREE_LIST of identifier nodes. */
29587 cp_parser_objc_identifier_list (cp_parser
* parser
)
29593 identifier
= cp_parser_identifier (parser
);
29594 if (identifier
== error_mark_node
)
29595 return error_mark_node
;
29597 list
= build_tree_list (NULL_TREE
, identifier
);
29598 sep
= cp_lexer_peek_token (parser
->lexer
);
29600 while (sep
->type
== CPP_COMMA
)
29602 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29603 identifier
= cp_parser_identifier (parser
);
29604 if (identifier
== error_mark_node
)
29607 list
= chainon (list
, build_tree_list (NULL_TREE
,
29609 sep
= cp_lexer_peek_token (parser
->lexer
);
29615 /* Parse an Objective-C alias declaration.
29617 objc-alias-declaration:
29618 @compatibility_alias identifier identifier ;
29620 This function registers the alias mapping with the Objective-C front end.
29621 It returns nothing. */
29624 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29628 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29629 alias
= cp_parser_identifier (parser
);
29630 orig
= cp_parser_identifier (parser
);
29631 objc_declare_alias (alias
, orig
);
29632 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29635 /* Parse an Objective-C class forward-declaration.
29637 objc-class-declaration:
29638 @class objc-identifier-list ;
29640 The function registers the forward declarations with the Objective-C
29641 front end. It returns nothing. */
29644 cp_parser_objc_class_declaration (cp_parser
* parser
)
29646 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29651 id
= cp_parser_identifier (parser
);
29652 if (id
== error_mark_node
)
29655 objc_declare_class (id
);
29657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29658 cp_lexer_consume_token (parser
->lexer
);
29662 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29665 /* Parse a list of Objective-C protocol references.
29667 objc-protocol-refs-opt:
29668 objc-protocol-refs [opt]
29670 objc-protocol-refs:
29671 < objc-identifier-list >
29673 Returns a TREE_LIST of identifiers, if any. */
29676 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29678 tree protorefs
= NULL_TREE
;
29680 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29682 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29683 protorefs
= cp_parser_objc_identifier_list (parser
);
29684 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29690 /* Parse a Objective-C visibility specification. */
29693 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29695 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29697 switch (vis
->keyword
)
29699 case RID_AT_PRIVATE
:
29700 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29702 case RID_AT_PROTECTED
:
29703 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29705 case RID_AT_PUBLIC
:
29706 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29708 case RID_AT_PACKAGE
:
29709 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29715 /* Eat '@private'/'@protected'/'@public'. */
29716 cp_lexer_consume_token (parser
->lexer
);
29719 /* Parse an Objective-C method type. Return 'true' if it is a class
29720 (+) method, and 'false' if it is an instance (-) method. */
29723 cp_parser_objc_method_type (cp_parser
* parser
)
29725 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29731 /* Parse an Objective-C protocol qualifier. */
29734 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29736 tree quals
= NULL_TREE
, node
;
29737 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29739 node
= token
->u
.value
;
29741 while (node
&& identifier_p (node
)
29742 && (node
== ridpointers
[(int) RID_IN
]
29743 || node
== ridpointers
[(int) RID_OUT
]
29744 || node
== ridpointers
[(int) RID_INOUT
]
29745 || node
== ridpointers
[(int) RID_BYCOPY
]
29746 || node
== ridpointers
[(int) RID_BYREF
]
29747 || node
== ridpointers
[(int) RID_ONEWAY
]))
29749 quals
= tree_cons (NULL_TREE
, node
, quals
);
29750 cp_lexer_consume_token (parser
->lexer
);
29751 token
= cp_lexer_peek_token (parser
->lexer
);
29752 node
= token
->u
.value
;
29758 /* Parse an Objective-C typename. */
29761 cp_parser_objc_typename (cp_parser
* parser
)
29763 tree type_name
= NULL_TREE
;
29765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29767 tree proto_quals
, cp_type
= NULL_TREE
;
29769 matching_parens parens
;
29770 parens
.consume_open (parser
); /* Eat '('. */
29771 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29773 /* An ObjC type name may consist of just protocol qualifiers, in which
29774 case the type shall default to 'id'. */
29775 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29777 cp_type
= cp_parser_type_id (parser
);
29779 /* If the type could not be parsed, an error has already
29780 been produced. For error recovery, behave as if it had
29781 not been specified, which will use the default type
29783 if (cp_type
== error_mark_node
)
29785 cp_type
= NULL_TREE
;
29786 /* We need to skip to the closing parenthesis as
29787 cp_parser_type_id() does not seem to do it for
29789 cp_parser_skip_to_closing_parenthesis (parser
,
29790 /*recovering=*/true,
29791 /*or_comma=*/false,
29792 /*consume_paren=*/false);
29796 parens
.require_close (parser
);
29797 type_name
= build_tree_list (proto_quals
, cp_type
);
29803 /* Check to see if TYPE refers to an Objective-C selector name. */
29806 cp_parser_objc_selector_p (enum cpp_ttype type
)
29808 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29809 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29810 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29811 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29812 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29815 /* Parse an Objective-C selector. */
29818 cp_parser_objc_selector (cp_parser
* parser
)
29820 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29822 if (!cp_parser_objc_selector_p (token
->type
))
29824 error_at (token
->location
, "invalid Objective-C++ selector name");
29825 return error_mark_node
;
29828 /* C++ operator names are allowed to appear in ObjC selectors. */
29829 switch (token
->type
)
29831 case CPP_AND_AND
: return get_identifier ("and");
29832 case CPP_AND_EQ
: return get_identifier ("and_eq");
29833 case CPP_AND
: return get_identifier ("bitand");
29834 case CPP_OR
: return get_identifier ("bitor");
29835 case CPP_COMPL
: return get_identifier ("compl");
29836 case CPP_NOT
: return get_identifier ("not");
29837 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29838 case CPP_OR_OR
: return get_identifier ("or");
29839 case CPP_OR_EQ
: return get_identifier ("or_eq");
29840 case CPP_XOR
: return get_identifier ("xor");
29841 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29842 default: return token
->u
.value
;
29846 /* Parse an Objective-C params list. */
29849 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29851 tree params
= NULL_TREE
;
29852 bool maybe_unary_selector_p
= true;
29853 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29855 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29857 tree selector
= NULL_TREE
, type_name
, identifier
;
29858 tree parm_attr
= NULL_TREE
;
29860 if (token
->keyword
== RID_ATTRIBUTE
)
29863 if (token
->type
!= CPP_COLON
)
29864 selector
= cp_parser_objc_selector (parser
);
29866 /* Detect if we have a unary selector. */
29867 if (maybe_unary_selector_p
29868 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29870 params
= selector
; /* Might be followed by attributes. */
29874 maybe_unary_selector_p
= false;
29875 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29877 /* Something went quite wrong. There should be a colon
29878 here, but there is not. Stop parsing parameters. */
29881 type_name
= cp_parser_objc_typename (parser
);
29882 /* New ObjC allows attributes on parameters too. */
29883 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29884 parm_attr
= cp_parser_attributes_opt (parser
);
29885 identifier
= cp_parser_identifier (parser
);
29889 objc_build_keyword_decl (selector
,
29894 token
= cp_lexer_peek_token (parser
->lexer
);
29897 if (params
== NULL_TREE
)
29899 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29900 return error_mark_node
;
29903 /* We allow tail attributes for the method. */
29904 if (token
->keyword
== RID_ATTRIBUTE
)
29906 *attributes
= cp_parser_attributes_opt (parser
);
29907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29908 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29910 cp_parser_error (parser
,
29911 "method attributes must be specified at the end");
29912 return error_mark_node
;
29915 if (params
== NULL_TREE
)
29917 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29918 return error_mark_node
;
29923 /* Parse the non-keyword Objective-C params. */
29926 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29929 tree params
= make_node (TREE_LIST
);
29930 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29931 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29933 while (token
->type
== CPP_COMMA
)
29935 cp_parameter_declarator
*parmdecl
;
29938 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29939 token
= cp_lexer_peek_token (parser
->lexer
);
29941 if (token
->type
== CPP_ELLIPSIS
)
29943 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29945 token
= cp_lexer_peek_token (parser
->lexer
);
29949 /* TODO: parse attributes for tail parameters. */
29950 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29951 parm
= grokdeclarator (parmdecl
->declarator
,
29952 &parmdecl
->decl_specifiers
,
29953 PARM
, /*initialized=*/0,
29954 /*attrlist=*/NULL
);
29956 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29957 token
= cp_lexer_peek_token (parser
->lexer
);
29960 /* We allow tail attributes for the method. */
29961 if (token
->keyword
== RID_ATTRIBUTE
)
29963 if (*attributes
== NULL_TREE
)
29965 *attributes
= cp_parser_attributes_opt (parser
);
29966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29967 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29971 /* We have an error, but parse the attributes, so that we can
29973 *attributes
= cp_parser_attributes_opt (parser
);
29975 cp_parser_error (parser
,
29976 "method attributes must be specified at the end");
29977 return error_mark_node
;
29983 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29986 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29988 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29990 /* If the next token is `extern' and the following token is a string
29991 literal, then we have a linkage specification. */
29992 if (token
->keyword
== RID_EXTERN
29993 && cp_parser_is_pure_string_literal
29994 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
29995 cp_parser_linkage_specification (parser
);
29996 /* Handle #pragma, if any. */
29997 else if (token
->type
== CPP_PRAGMA
)
29998 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
29999 /* Allow stray semicolons. */
30000 else if (token
->type
== CPP_SEMICOLON
)
30001 cp_lexer_consume_token (parser
->lexer
);
30002 /* Mark methods as optional or required, when building protocols. */
30003 else if (token
->keyword
== RID_AT_OPTIONAL
)
30005 cp_lexer_consume_token (parser
->lexer
);
30006 objc_set_method_opt (true);
30008 else if (token
->keyword
== RID_AT_REQUIRED
)
30010 cp_lexer_consume_token (parser
->lexer
);
30011 objc_set_method_opt (false);
30013 else if (token
->keyword
== RID_NAMESPACE
)
30014 cp_parser_namespace_definition (parser
);
30015 /* Other stray characters must generate errors. */
30016 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30018 cp_lexer_consume_token (parser
->lexer
);
30019 error ("stray %qs between Objective-C++ methods",
30020 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30022 /* Finally, try to parse a block-declaration, or a function-definition. */
30024 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30027 /* Parse a method signature. */
30030 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30032 tree rettype
, kwdparms
, optparms
;
30033 bool ellipsis
= false;
30034 bool is_class_method
;
30036 is_class_method
= cp_parser_objc_method_type (parser
);
30037 rettype
= cp_parser_objc_typename (parser
);
30038 *attributes
= NULL_TREE
;
30039 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30040 if (kwdparms
== error_mark_node
)
30041 return error_mark_node
;
30042 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30043 if (optparms
== error_mark_node
)
30044 return error_mark_node
;
30046 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30050 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30053 cp_lexer_save_tokens (parser
->lexer
);
30054 tattr
= cp_parser_attributes_opt (parser
);
30055 gcc_assert (tattr
) ;
30057 /* If the attributes are followed by a method introducer, this is not allowed.
30058 Dump the attributes and flag the situation. */
30059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30060 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30063 /* Otherwise, the attributes introduce some interstitial code, possibly so
30064 rewind to allow that check. */
30065 cp_lexer_rollback_tokens (parser
->lexer
);
30069 /* Parse an Objective-C method prototype list. */
30072 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30074 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30076 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30078 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30080 tree attributes
, sig
;
30081 bool is_class_method
;
30082 if (token
->type
== CPP_PLUS
)
30083 is_class_method
= true;
30085 is_class_method
= false;
30086 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30087 if (sig
== error_mark_node
)
30089 cp_parser_skip_to_end_of_block_or_statement (parser
);
30090 token
= cp_lexer_peek_token (parser
->lexer
);
30093 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30094 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30096 else if (token
->keyword
== RID_AT_PROPERTY
)
30097 cp_parser_objc_at_property_declaration (parser
);
30098 else if (token
->keyword
== RID_ATTRIBUTE
30099 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30100 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30102 "prefix attributes are ignored for methods");
30104 /* Allow for interspersed non-ObjC++ code. */
30105 cp_parser_objc_interstitial_code (parser
);
30107 token
= cp_lexer_peek_token (parser
->lexer
);
30110 if (token
->type
!= CPP_EOF
)
30111 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30113 cp_parser_error (parser
, "expected %<@end%>");
30115 objc_finish_interface ();
30118 /* Parse an Objective-C method definition list. */
30121 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30123 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30125 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30129 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30132 tree sig
, attribute
;
30133 bool is_class_method
;
30134 if (token
->type
== CPP_PLUS
)
30135 is_class_method
= true;
30137 is_class_method
= false;
30138 push_deferring_access_checks (dk_deferred
);
30139 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30140 if (sig
== error_mark_node
)
30142 cp_parser_skip_to_end_of_block_or_statement (parser
);
30143 token
= cp_lexer_peek_token (parser
->lexer
);
30146 objc_start_method_definition (is_class_method
, sig
, attribute
,
30149 /* For historical reasons, we accept an optional semicolon. */
30150 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30151 cp_lexer_consume_token (parser
->lexer
);
30153 ptk
= cp_lexer_peek_token (parser
->lexer
);
30154 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30155 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30157 perform_deferred_access_checks (tf_warning_or_error
);
30158 stop_deferring_access_checks ();
30159 meth
= cp_parser_function_definition_after_declarator (parser
,
30161 pop_deferring_access_checks ();
30162 objc_finish_method_definition (meth
);
30165 /* The following case will be removed once @synthesize is
30166 completely implemented. */
30167 else if (token
->keyword
== RID_AT_PROPERTY
)
30168 cp_parser_objc_at_property_declaration (parser
);
30169 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30170 cp_parser_objc_at_synthesize_declaration (parser
);
30171 else if (token
->keyword
== RID_AT_DYNAMIC
)
30172 cp_parser_objc_at_dynamic_declaration (parser
);
30173 else if (token
->keyword
== RID_ATTRIBUTE
30174 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30175 warning_at (token
->location
, OPT_Wattributes
,
30176 "prefix attributes are ignored for methods");
30178 /* Allow for interspersed non-ObjC++ code. */
30179 cp_parser_objc_interstitial_code (parser
);
30181 token
= cp_lexer_peek_token (parser
->lexer
);
30184 if (token
->type
!= CPP_EOF
)
30185 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30187 cp_parser_error (parser
, "expected %<@end%>");
30189 objc_finish_implementation ();
30192 /* Parse Objective-C ivars. */
30195 cp_parser_objc_class_ivars (cp_parser
* parser
)
30197 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30199 if (token
->type
!= CPP_OPEN_BRACE
)
30200 return; /* No ivars specified. */
30202 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30203 token
= cp_lexer_peek_token (parser
->lexer
);
30205 while (token
->type
!= CPP_CLOSE_BRACE
30206 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30208 cp_decl_specifier_seq declspecs
;
30209 int decl_class_or_enum_p
;
30210 tree prefix_attributes
;
30212 cp_parser_objc_visibility_spec (parser
);
30214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30217 cp_parser_decl_specifier_seq (parser
,
30218 CP_PARSER_FLAGS_OPTIONAL
,
30220 &decl_class_or_enum_p
);
30222 /* auto, register, static, extern, mutable. */
30223 if (declspecs
.storage_class
!= sc_none
)
30225 cp_parser_error (parser
, "invalid type for instance variable");
30226 declspecs
.storage_class
= sc_none
;
30229 /* thread_local. */
30230 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30232 cp_parser_error (parser
, "invalid type for instance variable");
30233 declspecs
.locations
[ds_thread
] = 0;
30237 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30239 cp_parser_error (parser
, "invalid type for instance variable");
30240 declspecs
.locations
[ds_typedef
] = 0;
30243 prefix_attributes
= declspecs
.attributes
;
30244 declspecs
.attributes
= NULL_TREE
;
30246 /* Keep going until we hit the `;' at the end of the
30248 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30250 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30251 cp_declarator
*declarator
= NULL
;
30252 int ctor_dtor_or_conv_p
;
30254 /* Check for a (possibly unnamed) bitfield declaration. */
30255 token
= cp_lexer_peek_token (parser
->lexer
);
30256 if (token
->type
== CPP_COLON
)
30259 if (token
->type
== CPP_NAME
30260 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30263 /* Get the name of the bitfield. */
30264 declarator
= make_id_declarator (NULL_TREE
,
30265 cp_parser_identifier (parser
),
30269 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30270 /* Get the width of the bitfield. */
30272 = cp_parser_constant_expression (parser
);
30276 /* Parse the declarator. */
30278 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30279 &ctor_dtor_or_conv_p
,
30280 /*parenthesized_p=*/NULL
,
30281 /*member_p=*/false,
30282 /*friend_p=*/false);
30285 /* Look for attributes that apply to the ivar. */
30286 attributes
= cp_parser_attributes_opt (parser
);
30287 /* Remember which attributes are prefix attributes and
30289 first_attribute
= attributes
;
30290 /* Combine the attributes. */
30291 attributes
= attr_chainon (prefix_attributes
, attributes
);
30294 /* Create the bitfield declaration. */
30295 decl
= grokbitfield (declarator
, &declspecs
,
30296 width
, NULL_TREE
, attributes
);
30298 decl
= grokfield (declarator
, &declspecs
,
30299 NULL_TREE
, /*init_const_expr_p=*/false,
30300 NULL_TREE
, attributes
);
30302 /* Add the instance variable. */
30303 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30304 objc_add_instance_variable (decl
);
30306 /* Reset PREFIX_ATTRIBUTES. */
30307 if (attributes
!= error_mark_node
)
30309 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30310 attributes
= TREE_CHAIN (attributes
);
30312 TREE_CHAIN (attributes
) = NULL_TREE
;
30315 token
= cp_lexer_peek_token (parser
->lexer
);
30317 if (token
->type
== CPP_COMMA
)
30319 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30325 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30326 token
= cp_lexer_peek_token (parser
->lexer
);
30329 if (token
->keyword
== RID_AT_END
)
30330 cp_parser_error (parser
, "expected %<}%>");
30332 /* Do not consume the RID_AT_END, so it will be read again as terminating
30333 the @interface of @implementation. */
30334 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30335 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30337 /* For historical reasons, we accept an optional semicolon. */
30338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30339 cp_lexer_consume_token (parser
->lexer
);
30342 /* Parse an Objective-C protocol declaration. */
30345 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30347 tree proto
, protorefs
;
30350 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30351 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30353 tok
= cp_lexer_peek_token (parser
->lexer
);
30354 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30355 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30359 /* See if we have a forward declaration or a definition. */
30360 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30362 /* Try a forward declaration first. */
30363 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30369 id
= cp_parser_identifier (parser
);
30370 if (id
== error_mark_node
)
30373 objc_declare_protocol (id
, attributes
);
30375 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30376 cp_lexer_consume_token (parser
->lexer
);
30380 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30383 /* Ok, we got a full-fledged definition (or at least should). */
30386 proto
= cp_parser_identifier (parser
);
30387 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30388 objc_start_protocol (proto
, protorefs
, attributes
);
30389 cp_parser_objc_method_prototype_list (parser
);
30393 /* Parse an Objective-C superclass or category. */
30396 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30399 tree
*categ
, bool *is_class_extension
)
30401 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30403 *super
= *categ
= NULL_TREE
;
30404 *is_class_extension
= false;
30405 if (next
->type
== CPP_COLON
)
30407 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30408 *super
= cp_parser_identifier (parser
);
30410 else if (next
->type
== CPP_OPEN_PAREN
)
30412 matching_parens parens
;
30413 parens
.consume_open (parser
); /* Eat '('. */
30415 /* If there is no category name, and this is an @interface, we
30416 have a class extension. */
30417 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30419 *categ
= NULL_TREE
;
30420 *is_class_extension
= true;
30423 *categ
= cp_parser_identifier (parser
);
30425 parens
.require_close (parser
);
30429 /* Parse an Objective-C class interface. */
30432 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30434 tree name
, super
, categ
, protos
;
30435 bool is_class_extension
;
30437 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30438 name
= cp_parser_identifier (parser
);
30439 if (name
== error_mark_node
)
30441 /* It's hard to recover because even if valid @interface stuff
30442 is to follow, we can't compile it (or validate it) if we
30443 don't even know which class it refers to. Let's assume this
30444 was a stray '@interface' token in the stream and skip it.
30448 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30449 &is_class_extension
);
30450 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30452 /* We have either a class or a category on our hands. */
30453 if (categ
|| is_class_extension
)
30454 objc_start_category_interface (name
, categ
, protos
, attributes
);
30457 objc_start_class_interface (name
, super
, protos
, attributes
);
30458 /* Handle instance variable declarations, if any. */
30459 cp_parser_objc_class_ivars (parser
);
30460 objc_continue_interface ();
30463 cp_parser_objc_method_prototype_list (parser
);
30466 /* Parse an Objective-C class implementation. */
30469 cp_parser_objc_class_implementation (cp_parser
* parser
)
30471 tree name
, super
, categ
;
30472 bool is_class_extension
;
30474 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30475 name
= cp_parser_identifier (parser
);
30476 if (name
== error_mark_node
)
30478 /* It's hard to recover because even if valid @implementation
30479 stuff is to follow, we can't compile it (or validate it) if
30480 we don't even know which class it refers to. Let's assume
30481 this was a stray '@implementation' token in the stream and
30486 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30487 &is_class_extension
);
30489 /* We have either a class or a category on our hands. */
30491 objc_start_category_implementation (name
, categ
);
30494 objc_start_class_implementation (name
, super
);
30495 /* Handle instance variable declarations, if any. */
30496 cp_parser_objc_class_ivars (parser
);
30497 objc_continue_implementation ();
30500 cp_parser_objc_method_definition_list (parser
);
30503 /* Consume the @end token and finish off the implementation. */
30506 cp_parser_objc_end_implementation (cp_parser
* parser
)
30508 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30509 objc_finish_implementation ();
30512 /* Parse an Objective-C declaration. */
30515 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30517 /* Try to figure out what kind of declaration is present. */
30518 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30521 switch (kwd
->keyword
)
30526 error_at (kwd
->location
, "attributes may not be specified before"
30527 " the %<@%D%> Objective-C++ keyword",
30531 case RID_AT_IMPLEMENTATION
:
30532 warning_at (kwd
->location
, OPT_Wattributes
,
30533 "prefix attributes are ignored before %<@%D%>",
30540 switch (kwd
->keyword
)
30543 cp_parser_objc_alias_declaration (parser
);
30546 cp_parser_objc_class_declaration (parser
);
30548 case RID_AT_PROTOCOL
:
30549 cp_parser_objc_protocol_declaration (parser
, attributes
);
30551 case RID_AT_INTERFACE
:
30552 cp_parser_objc_class_interface (parser
, attributes
);
30554 case RID_AT_IMPLEMENTATION
:
30555 cp_parser_objc_class_implementation (parser
);
30558 cp_parser_objc_end_implementation (parser
);
30561 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30563 cp_parser_skip_to_end_of_block_or_statement (parser
);
30567 /* Parse an Objective-C try-catch-finally statement.
30569 objc-try-catch-finally-stmt:
30570 @try compound-statement objc-catch-clause-seq [opt]
30571 objc-finally-clause [opt]
30573 objc-catch-clause-seq:
30574 objc-catch-clause objc-catch-clause-seq [opt]
30577 @catch ( objc-exception-declaration ) compound-statement
30579 objc-finally-clause:
30580 @finally compound-statement
30582 objc-exception-declaration:
30583 parameter-declaration
30586 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30590 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30591 for C. Keep them in sync. */
30594 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30596 location_t location
;
30599 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30600 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30601 objc_maybe_warn_exceptions (location
);
30602 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30603 node, lest it get absorbed into the surrounding block. */
30604 stmt
= push_stmt_list ();
30605 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30606 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30608 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30610 cp_parameter_declarator
*parm
;
30611 tree parameter_declaration
= error_mark_node
;
30612 bool seen_open_paren
= false;
30613 matching_parens parens
;
30615 cp_lexer_consume_token (parser
->lexer
);
30616 if (parens
.require_open (parser
))
30617 seen_open_paren
= true;
30618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30620 /* We have "@catch (...)" (where the '...' are literally
30621 what is in the code). Skip the '...'.
30622 parameter_declaration is set to NULL_TREE, and
30623 objc_being_catch_clauses() knows that that means
30625 cp_lexer_consume_token (parser
->lexer
);
30626 parameter_declaration
= NULL_TREE
;
30630 /* We have "@catch (NSException *exception)" or something
30631 like that. Parse the parameter declaration. */
30632 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30634 parameter_declaration
= error_mark_node
;
30636 parameter_declaration
= grokdeclarator (parm
->declarator
,
30637 &parm
->decl_specifiers
,
30638 PARM
, /*initialized=*/0,
30639 /*attrlist=*/NULL
);
30641 if (seen_open_paren
)
30642 parens
.require_close (parser
);
30645 /* If there was no open parenthesis, we are recovering from
30646 an error, and we are trying to figure out what mistake
30647 the user has made. */
30649 /* If there is an immediate closing parenthesis, the user
30650 probably forgot the opening one (ie, they typed "@catch
30651 NSException *e)". Parse the closing parenthesis and keep
30653 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30654 cp_lexer_consume_token (parser
->lexer
);
30656 /* If these is no immediate closing parenthesis, the user
30657 probably doesn't know that parenthesis are required at
30658 all (ie, they typed "@catch NSException *e"). So, just
30659 forget about the closing parenthesis and keep going. */
30661 objc_begin_catch_clause (parameter_declaration
);
30662 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30663 objc_finish_catch_clause ();
30665 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30667 cp_lexer_consume_token (parser
->lexer
);
30668 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30669 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30670 node, lest it get absorbed into the surrounding block. */
30671 stmt
= push_stmt_list ();
30672 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30673 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30676 return objc_finish_try_stmt ();
30679 /* Parse an Objective-C synchronized statement.
30681 objc-synchronized-stmt:
30682 @synchronized ( expression ) compound-statement
30684 Returns NULL_TREE. */
30687 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30689 location_t location
;
30692 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30694 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30695 objc_maybe_warn_exceptions (location
);
30696 matching_parens parens
;
30697 parens
.require_open (parser
);
30698 lock
= cp_parser_expression (parser
);
30699 parens
.require_close (parser
);
30701 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30702 node, lest it get absorbed into the surrounding block. */
30703 stmt
= push_stmt_list ();
30704 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30706 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30709 /* Parse an Objective-C throw statement.
30712 @throw assignment-expression [opt] ;
30714 Returns a constructed '@throw' statement. */
30717 cp_parser_objc_throw_statement (cp_parser
*parser
)
30719 tree expr
= NULL_TREE
;
30720 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30722 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30725 expr
= cp_parser_expression (parser
);
30727 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30729 return objc_build_throw_stmt (loc
, expr
);
30732 /* Parse an Objective-C statement. */
30735 cp_parser_objc_statement (cp_parser
* parser
)
30737 /* Try to figure out what kind of declaration is present. */
30738 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30740 switch (kwd
->keyword
)
30743 return cp_parser_objc_try_catch_finally_statement (parser
);
30744 case RID_AT_SYNCHRONIZED
:
30745 return cp_parser_objc_synchronized_statement (parser
);
30747 return cp_parser_objc_throw_statement (parser
);
30749 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30751 cp_parser_skip_to_end_of_block_or_statement (parser
);
30754 return error_mark_node
;
30757 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30758 look ahead to see if an objc keyword follows the attributes. This
30759 is to detect the use of prefix attributes on ObjC @interface and
30763 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30765 cp_lexer_save_tokens (parser
->lexer
);
30766 *attrib
= cp_parser_attributes_opt (parser
);
30767 gcc_assert (*attrib
);
30768 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30770 cp_lexer_commit_tokens (parser
->lexer
);
30773 cp_lexer_rollback_tokens (parser
->lexer
);
30777 /* This routine is a minimal replacement for
30778 c_parser_struct_declaration () used when parsing the list of
30779 types/names or ObjC++ properties. For example, when parsing the
30782 @property (readonly) int a, b, c;
30784 this function is responsible for parsing "int a, int b, int c" and
30785 returning the declarations as CHAIN of DECLs.
30787 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30788 similar parsing. */
30790 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30792 tree decls
= NULL_TREE
;
30793 cp_decl_specifier_seq declspecs
;
30794 int decl_class_or_enum_p
;
30795 tree prefix_attributes
;
30797 cp_parser_decl_specifier_seq (parser
,
30798 CP_PARSER_FLAGS_NONE
,
30800 &decl_class_or_enum_p
);
30802 if (declspecs
.type
== error_mark_node
)
30803 return error_mark_node
;
30805 /* auto, register, static, extern, mutable. */
30806 if (declspecs
.storage_class
!= sc_none
)
30808 cp_parser_error (parser
, "invalid type for property");
30809 declspecs
.storage_class
= sc_none
;
30812 /* thread_local. */
30813 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30815 cp_parser_error (parser
, "invalid type for property");
30816 declspecs
.locations
[ds_thread
] = 0;
30820 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30822 cp_parser_error (parser
, "invalid type for property");
30823 declspecs
.locations
[ds_typedef
] = 0;
30826 prefix_attributes
= declspecs
.attributes
;
30827 declspecs
.attributes
= NULL_TREE
;
30829 /* Keep going until we hit the `;' at the end of the declaration. */
30830 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30832 tree attributes
, first_attribute
, decl
;
30833 cp_declarator
*declarator
;
30836 /* Parse the declarator. */
30837 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30838 NULL
, NULL
, false, false);
30840 /* Look for attributes that apply to the ivar. */
30841 attributes
= cp_parser_attributes_opt (parser
);
30842 /* Remember which attributes are prefix attributes and
30844 first_attribute
= attributes
;
30845 /* Combine the attributes. */
30846 attributes
= attr_chainon (prefix_attributes
, attributes
);
30848 decl
= grokfield (declarator
, &declspecs
,
30849 NULL_TREE
, /*init_const_expr_p=*/false,
30850 NULL_TREE
, attributes
);
30852 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30853 return error_mark_node
;
30855 /* Reset PREFIX_ATTRIBUTES. */
30856 if (attributes
!= error_mark_node
)
30858 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30859 attributes
= TREE_CHAIN (attributes
);
30861 TREE_CHAIN (attributes
) = NULL_TREE
;
30864 DECL_CHAIN (decl
) = decls
;
30867 token
= cp_lexer_peek_token (parser
->lexer
);
30868 if (token
->type
== CPP_COMMA
)
30870 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30879 /* Parse an Objective-C @property declaration. The syntax is:
30881 objc-property-declaration:
30882 '@property' objc-property-attributes[opt] struct-declaration ;
30884 objc-property-attributes:
30885 '(' objc-property-attribute-list ')'
30887 objc-property-attribute-list:
30888 objc-property-attribute
30889 objc-property-attribute-list, objc-property-attribute
30891 objc-property-attribute
30892 'getter' = identifier
30893 'setter' = identifier
30902 @property NSString *name;
30903 @property (readonly) id object;
30904 @property (retain, nonatomic, getter=getTheName) id name;
30905 @property int a, b, c;
30907 PS: This function is identical to
30908 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30910 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30912 /* The following variables hold the attributes of the properties as
30913 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30914 seen. When we see an attribute, we set them to 'true' (if they
30915 are boolean properties) or to the identifier (if they have an
30916 argument, ie, for getter and setter). Note that here we only
30917 parse the list of attributes, check the syntax and accumulate the
30918 attributes that we find. objc_add_property_declaration() will
30919 then process the information. */
30920 bool property_assign
= false;
30921 bool property_copy
= false;
30922 tree property_getter_ident
= NULL_TREE
;
30923 bool property_nonatomic
= false;
30924 bool property_readonly
= false;
30925 bool property_readwrite
= false;
30926 bool property_retain
= false;
30927 tree property_setter_ident
= NULL_TREE
;
30929 /* 'properties' is the list of properties that we read. Usually a
30930 single one, but maybe more (eg, in "@property int a, b, c;" there
30935 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30937 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30939 /* Parse the optional attribute list... */
30940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30943 matching_parens parens
;
30944 parens
.consume_open (parser
);
30948 bool syntax_error
= false;
30949 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30952 if (token
->type
!= CPP_NAME
)
30954 cp_parser_error (parser
, "expected identifier");
30957 keyword
= C_RID_CODE (token
->u
.value
);
30958 cp_lexer_consume_token (parser
->lexer
);
30961 case RID_ASSIGN
: property_assign
= true; break;
30962 case RID_COPY
: property_copy
= true; break;
30963 case RID_NONATOMIC
: property_nonatomic
= true; break;
30964 case RID_READONLY
: property_readonly
= true; break;
30965 case RID_READWRITE
: property_readwrite
= true; break;
30966 case RID_RETAIN
: property_retain
= true; break;
30970 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30972 if (keyword
== RID_GETTER
)
30973 cp_parser_error (parser
,
30974 "missing %<=%> (after %<getter%> attribute)");
30976 cp_parser_error (parser
,
30977 "missing %<=%> (after %<setter%> attribute)");
30978 syntax_error
= true;
30981 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30982 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30984 cp_parser_error (parser
, "expected identifier");
30985 syntax_error
= true;
30988 if (keyword
== RID_SETTER
)
30990 if (property_setter_ident
!= NULL_TREE
)
30992 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
30993 cp_lexer_consume_token (parser
->lexer
);
30996 property_setter_ident
= cp_parser_objc_selector (parser
);
30997 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30998 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31000 cp_lexer_consume_token (parser
->lexer
);
31004 if (property_getter_ident
!= NULL_TREE
)
31006 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31007 cp_lexer_consume_token (parser
->lexer
);
31010 property_getter_ident
= cp_parser_objc_selector (parser
);
31014 cp_parser_error (parser
, "unknown property attribute");
31015 syntax_error
= true;
31022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31023 cp_lexer_consume_token (parser
->lexer
);
31028 /* FIXME: "@property (setter, assign);" will generate a spurious
31029 "error: expected ‘)’ before ‘,’ token". This is because
31030 cp_parser_require, unlike the C counterpart, will produce an
31031 error even if we are in error recovery. */
31032 if (!parens
.require_close (parser
))
31034 cp_parser_skip_to_closing_parenthesis (parser
,
31035 /*recovering=*/true,
31036 /*or_comma=*/false,
31037 /*consume_paren=*/true);
31041 /* ... and the property declaration(s). */
31042 properties
= cp_parser_objc_struct_declaration (parser
);
31044 if (properties
== error_mark_node
)
31046 cp_parser_skip_to_end_of_statement (parser
);
31047 /* If the next token is now a `;', consume it. */
31048 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31049 cp_lexer_consume_token (parser
->lexer
);
31053 if (properties
== NULL_TREE
)
31054 cp_parser_error (parser
, "expected identifier");
31057 /* Comma-separated properties are chained together in
31058 reverse order; add them one by one. */
31059 properties
= nreverse (properties
);
31061 for (; properties
; properties
= TREE_CHAIN (properties
))
31062 objc_add_property_declaration (loc
, copy_node (properties
),
31063 property_readonly
, property_readwrite
,
31064 property_assign
, property_retain
,
31065 property_copy
, property_nonatomic
,
31066 property_getter_ident
, property_setter_ident
);
31069 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31072 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31074 objc-synthesize-declaration:
31075 @synthesize objc-synthesize-identifier-list ;
31077 objc-synthesize-identifier-list:
31078 objc-synthesize-identifier
31079 objc-synthesize-identifier-list, objc-synthesize-identifier
31081 objc-synthesize-identifier
31083 identifier = identifier
31086 @synthesize MyProperty;
31087 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31089 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31090 for C. Keep them in sync.
31093 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31095 tree list
= NULL_TREE
;
31097 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31099 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31102 tree property
, ivar
;
31103 property
= cp_parser_identifier (parser
);
31104 if (property
== error_mark_node
)
31106 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31111 cp_lexer_consume_token (parser
->lexer
);
31112 ivar
= cp_parser_identifier (parser
);
31113 if (ivar
== error_mark_node
)
31115 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31121 list
= chainon (list
, build_tree_list (ivar
, property
));
31122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31123 cp_lexer_consume_token (parser
->lexer
);
31127 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31128 objc_add_synthesize_declaration (loc
, list
);
31131 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31133 objc-dynamic-declaration:
31134 @dynamic identifier-list ;
31137 @dynamic MyProperty;
31138 @dynamic MyProperty, AnotherProperty;
31140 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31141 for C. Keep them in sync.
31144 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31146 tree list
= NULL_TREE
;
31148 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31150 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31154 property
= cp_parser_identifier (parser
);
31155 if (property
== error_mark_node
)
31157 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31160 list
= chainon (list
, build_tree_list (NULL
, property
));
31161 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31162 cp_lexer_consume_token (parser
->lexer
);
31166 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31167 objc_add_dynamic_declaration (loc
, list
);
31171 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31173 /* Returns name of the next clause.
31174 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31175 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31176 returned and the token is consumed. */
31178 static pragma_omp_clause
31179 cp_parser_omp_clause_name (cp_parser
*parser
)
31181 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31183 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31184 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31185 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31186 result
= PRAGMA_OMP_CLAUSE_IF
;
31187 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31188 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31189 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31190 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31191 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31192 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31193 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31194 result
= PRAGMA_OMP_CLAUSE_FOR
;
31195 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31197 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31198 const char *p
= IDENTIFIER_POINTER (id
);
31203 if (!strcmp ("aligned", p
))
31204 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31205 else if (!strcmp ("async", p
))
31206 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31209 if (!strcmp ("collapse", p
))
31210 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31211 else if (!strcmp ("copy", p
))
31212 result
= PRAGMA_OACC_CLAUSE_COPY
;
31213 else if (!strcmp ("copyin", p
))
31214 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31215 else if (!strcmp ("copyout", p
))
31216 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31217 else if (!strcmp ("copyprivate", p
))
31218 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31219 else if (!strcmp ("create", p
))
31220 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31223 if (!strcmp ("defaultmap", p
))
31224 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31225 else if (!strcmp ("depend", p
))
31226 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31227 else if (!strcmp ("device", p
))
31228 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31229 else if (!strcmp ("deviceptr", p
))
31230 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31231 else if (!strcmp ("device_resident", p
))
31232 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31233 else if (!strcmp ("dist_schedule", p
))
31234 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31237 if (!strcmp ("final", p
))
31238 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31239 else if (!strcmp ("firstprivate", p
))
31240 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31241 else if (!strcmp ("from", p
))
31242 result
= PRAGMA_OMP_CLAUSE_FROM
;
31245 if (!strcmp ("gang", p
))
31246 result
= PRAGMA_OACC_CLAUSE_GANG
;
31247 else if (!strcmp ("grainsize", p
))
31248 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31251 if (!strcmp ("hint", p
))
31252 result
= PRAGMA_OMP_CLAUSE_HINT
;
31253 else if (!strcmp ("host", p
))
31254 result
= PRAGMA_OACC_CLAUSE_HOST
;
31257 if (!strcmp ("inbranch", p
))
31258 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31259 else if (!strcmp ("independent", p
))
31260 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31261 else if (!strcmp ("is_device_ptr", p
))
31262 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31265 if (!strcmp ("lastprivate", p
))
31266 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31267 else if (!strcmp ("linear", p
))
31268 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31269 else if (!strcmp ("link", p
))
31270 result
= PRAGMA_OMP_CLAUSE_LINK
;
31273 if (!strcmp ("map", p
))
31274 result
= PRAGMA_OMP_CLAUSE_MAP
;
31275 else if (!strcmp ("mergeable", p
))
31276 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31279 if (!strcmp ("nogroup", p
))
31280 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31281 else if (!strcmp ("notinbranch", p
))
31282 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31283 else if (!strcmp ("nowait", p
))
31284 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31285 else if (!strcmp ("num_gangs", p
))
31286 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31287 else if (!strcmp ("num_tasks", p
))
31288 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31289 else if (!strcmp ("num_teams", p
))
31290 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31291 else if (!strcmp ("num_threads", p
))
31292 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31293 else if (!strcmp ("num_workers", p
))
31294 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31297 if (!strcmp ("ordered", p
))
31298 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31301 if (!strcmp ("parallel", p
))
31302 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31303 else if (!strcmp ("present", p
))
31304 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31305 else if (!strcmp ("present_or_copy", p
)
31306 || !strcmp ("pcopy", p
))
31307 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31308 else if (!strcmp ("present_or_copyin", p
)
31309 || !strcmp ("pcopyin", p
))
31310 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31311 else if (!strcmp ("present_or_copyout", p
)
31312 || !strcmp ("pcopyout", p
))
31313 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31314 else if (!strcmp ("present_or_create", p
)
31315 || !strcmp ("pcreate", p
))
31316 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31317 else if (!strcmp ("priority", p
))
31318 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31319 else if (!strcmp ("proc_bind", p
))
31320 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31323 if (!strcmp ("reduction", p
))
31324 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31327 if (!strcmp ("safelen", p
))
31328 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31329 else if (!strcmp ("schedule", p
))
31330 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31331 else if (!strcmp ("sections", p
))
31332 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31333 else if (!strcmp ("self", p
))
31334 result
= PRAGMA_OACC_CLAUSE_SELF
;
31335 else if (!strcmp ("seq", p
))
31336 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31337 else if (!strcmp ("shared", p
))
31338 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31339 else if (!strcmp ("simd", p
))
31340 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31341 else if (!strcmp ("simdlen", p
))
31342 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31345 if (!strcmp ("taskgroup", p
))
31346 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31347 else if (!strcmp ("thread_limit", p
))
31348 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31349 else if (!strcmp ("threads", p
))
31350 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31351 else if (!strcmp ("tile", p
))
31352 result
= PRAGMA_OACC_CLAUSE_TILE
;
31353 else if (!strcmp ("to", p
))
31354 result
= PRAGMA_OMP_CLAUSE_TO
;
31357 if (!strcmp ("uniform", p
))
31358 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31359 else if (!strcmp ("untied", p
))
31360 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31361 else if (!strcmp ("use_device", p
))
31362 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31363 else if (!strcmp ("use_device_ptr", p
))
31364 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31367 if (!strcmp ("vector", p
))
31368 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31369 else if (!strcmp ("vector_length", p
))
31370 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31373 if (!strcmp ("wait", p
))
31374 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31375 else if (!strcmp ("worker", p
))
31376 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31381 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31382 cp_lexer_consume_token (parser
->lexer
);
31387 /* Validate that a clause of the given type does not already exist. */
31390 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31391 const char *name
, location_t location
)
31395 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31396 if (OMP_CLAUSE_CODE (c
) == code
)
31398 error_at (location
, "too many %qs clauses", name
);
31406 variable-list , identifier
31408 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31409 colon). An opening parenthesis will have been consumed by the caller.
31411 If KIND is nonzero, create the appropriate node and install the decl
31412 in OMP_CLAUSE_DECL and add the node to the head of the list.
31414 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31415 return the list created.
31417 COLON can be NULL if only closing parenthesis should end the list,
31418 or pointer to bool which will receive false if the list is terminated
31419 by closing parenthesis or true if the list is terminated by colon. */
31422 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31423 tree list
, bool *colon
)
31426 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31429 parser
->colon_corrects_to_scope_p
= false;
31436 token
= cp_lexer_peek_token (parser
->lexer
);
31438 && current_class_ptr
31439 && cp_parser_is_keyword (token
, RID_THIS
))
31441 decl
= finish_this_expr ();
31442 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31443 || CONVERT_EXPR_P (decl
))
31444 decl
= TREE_OPERAND (decl
, 0);
31445 cp_lexer_consume_token (parser
->lexer
);
31449 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31450 /*check_dependency_p=*/true,
31451 /*template_p=*/NULL
,
31452 /*declarator_p=*/false,
31453 /*optional_p=*/false);
31454 if (name
== error_mark_node
)
31457 if (identifier_p (name
))
31458 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31461 if (decl
== error_mark_node
)
31462 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31465 if (decl
== error_mark_node
)
31467 else if (kind
!= 0)
31471 case OMP_CLAUSE__CACHE_
:
31472 /* The OpenACC cache directive explicitly only allows "array
31473 elements or subarrays". */
31474 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31476 error_at (token
->location
, "expected %<[%>");
31477 decl
= error_mark_node
;
31481 case OMP_CLAUSE_MAP
:
31482 case OMP_CLAUSE_FROM
:
31483 case OMP_CLAUSE_TO
:
31484 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31487 = cp_lexer_peek_token (parser
->lexer
)->location
;
31488 cp_id_kind idk
= CP_ID_KIND_NONE
;
31489 cp_lexer_consume_token (parser
->lexer
);
31490 decl
= convert_from_reference (decl
);
31492 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31497 case OMP_CLAUSE_DEPEND
:
31498 case OMP_CLAUSE_REDUCTION
:
31499 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31501 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31503 parser
->colon_corrects_to_scope_p
= false;
31504 cp_lexer_consume_token (parser
->lexer
);
31505 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31506 low_bound
= cp_parser_expression (parser
);
31508 parser
->colon_corrects_to_scope_p
31509 = saved_colon_corrects_to_scope_p
;
31510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31511 length
= integer_one_node
;
31514 /* Look for `:'. */
31515 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31517 if (!cp_lexer_next_token_is (parser
->lexer
,
31519 length
= cp_parser_expression (parser
);
31521 /* Look for the closing `]'. */
31522 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31526 decl
= tree_cons (low_bound
, length
, decl
);
31533 tree u
= build_omp_clause (token
->location
, kind
);
31534 OMP_CLAUSE_DECL (u
) = decl
;
31535 OMP_CLAUSE_CHAIN (u
) = list
;
31539 list
= tree_cons (decl
, NULL_TREE
, list
);
31542 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31544 cp_lexer_consume_token (parser
->lexer
);
31548 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31550 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31553 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31557 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31561 /* Try to resync to an unnested comma. Copied from
31562 cp_parser_parenthesized_expression_list. */
31565 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31566 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31567 /*recovering=*/true,
31569 /*consume_paren=*/true);
31577 /* Similarly, but expect leading and trailing parenthesis. This is a very
31578 common case for omp clauses. */
31581 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31583 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31584 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31589 copy ( variable-list )
31590 copyin ( variable-list )
31591 copyout ( variable-list )
31592 create ( variable-list )
31593 delete ( variable-list )
31594 present ( variable-list )
31595 present_or_copy ( variable-list )
31596 pcopy ( variable-list )
31597 present_or_copyin ( variable-list )
31598 pcopyin ( variable-list )
31599 present_or_copyout ( variable-list )
31600 pcopyout ( variable-list )
31601 present_or_create ( variable-list )
31602 pcreate ( variable-list ) */
31605 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31608 enum gomp_map_kind kind
;
31611 case PRAGMA_OACC_CLAUSE_COPY
:
31612 kind
= GOMP_MAP_FORCE_TOFROM
;
31614 case PRAGMA_OACC_CLAUSE_COPYIN
:
31615 kind
= GOMP_MAP_FORCE_TO
;
31617 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31618 kind
= GOMP_MAP_FORCE_FROM
;
31620 case PRAGMA_OACC_CLAUSE_CREATE
:
31621 kind
= GOMP_MAP_FORCE_ALLOC
;
31623 case PRAGMA_OACC_CLAUSE_DELETE
:
31624 kind
= GOMP_MAP_DELETE
;
31626 case PRAGMA_OACC_CLAUSE_DEVICE
:
31627 kind
= GOMP_MAP_FORCE_TO
;
31629 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31630 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31632 case PRAGMA_OACC_CLAUSE_HOST
:
31633 case PRAGMA_OACC_CLAUSE_SELF
:
31634 kind
= GOMP_MAP_FORCE_FROM
;
31636 case PRAGMA_OACC_CLAUSE_LINK
:
31637 kind
= GOMP_MAP_LINK
;
31639 case PRAGMA_OACC_CLAUSE_PRESENT
:
31640 kind
= GOMP_MAP_FORCE_PRESENT
;
31642 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31643 kind
= GOMP_MAP_TOFROM
;
31645 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31646 kind
= GOMP_MAP_TO
;
31648 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31649 kind
= GOMP_MAP_FROM
;
31651 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31652 kind
= GOMP_MAP_ALLOC
;
31655 gcc_unreachable ();
31658 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31660 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31661 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31667 deviceptr ( variable-list ) */
31670 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31672 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31675 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31676 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31677 variable-list must only allow for pointer variables. */
31678 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31679 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31681 tree v
= TREE_PURPOSE (t
);
31682 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31683 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31684 OMP_CLAUSE_DECL (u
) = v
;
31685 OMP_CLAUSE_CHAIN (u
) = list
;
31699 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31700 enum omp_clause_code code
,
31701 tree list
, location_t location
)
31703 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31704 tree c
= build_omp_clause (location
, code
);
31705 OMP_CLAUSE_CHAIN (c
) = list
;
31710 num_gangs ( expression )
31711 num_workers ( expression )
31712 vector_length ( expression ) */
31715 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31716 const char *str
, tree list
)
31718 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31720 matching_parens parens
;
31721 if (!parens
.require_open (parser
))
31724 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31726 if (t
== error_mark_node
31727 || !parens
.require_close (parser
))
31729 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31730 /*or_comma=*/false,
31731 /*consume_paren=*/true);
31735 check_no_duplicate_clause (list
, code
, str
, loc
);
31737 tree c
= build_omp_clause (loc
, code
);
31738 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31739 OMP_CLAUSE_CHAIN (c
) = list
;
31745 gang [( gang-arg-list )]
31746 worker [( [num:] int-expr )]
31747 vector [( [length:] int-expr )]
31749 where gang-arg is one of:
31754 and size-expr may be:
31761 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31762 const char *str
, tree list
)
31764 const char *id
= "num";
31765 cp_lexer
*lexer
= parser
->lexer
;
31766 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31767 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31769 if (kind
== OMP_CLAUSE_VECTOR
)
31772 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31774 matching_parens parens
;
31775 parens
.consume_open (parser
);
31779 cp_token
*next
= cp_lexer_peek_token (lexer
);
31782 /* Gang static argument. */
31783 if (kind
== OMP_CLAUSE_GANG
31784 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31786 cp_lexer_consume_token (lexer
);
31788 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31789 goto cleanup_error
;
31792 if (ops
[idx
] != NULL
)
31794 cp_parser_error (parser
, "too many %<static%> arguments");
31795 goto cleanup_error
;
31798 /* Check for the '*' argument. */
31799 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31800 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31801 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31804 cp_lexer_consume_token (lexer
);
31805 ops
[idx
] = integer_minus_one_node
;
31807 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31809 cp_lexer_consume_token (lexer
);
31815 /* Worker num: argument and vector length: arguments. */
31816 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31817 && id_equal (next
->u
.value
, id
)
31818 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31820 cp_lexer_consume_token (lexer
); /* id */
31821 cp_lexer_consume_token (lexer
); /* ':' */
31824 /* Now collect the actual argument. */
31825 if (ops
[idx
] != NULL_TREE
)
31827 cp_parser_error (parser
, "unexpected argument");
31828 goto cleanup_error
;
31831 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31833 if (expr
== error_mark_node
)
31834 goto cleanup_error
;
31836 mark_exp_read (expr
);
31839 if (kind
== OMP_CLAUSE_GANG
31840 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31842 cp_lexer_consume_token (lexer
);
31849 if (!parens
.require_close (parser
))
31850 goto cleanup_error
;
31853 check_no_duplicate_clause (list
, kind
, str
, loc
);
31855 c
= build_omp_clause (loc
, kind
);
31858 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31860 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31861 OMP_CLAUSE_CHAIN (c
) = list
;
31866 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31871 tile ( size-expr-list ) */
31874 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31876 tree c
, expr
= error_mark_node
;
31877 tree tile
= NULL_TREE
;
31879 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31880 so, but the spec authors never considered such a case and have
31881 differing opinions on what it might mean, including 'not
31883 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31884 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31887 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31892 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31896 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31897 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31899 cp_lexer_consume_token (parser
->lexer
);
31900 expr
= integer_zero_node
;
31903 expr
= cp_parser_constant_expression (parser
);
31905 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31907 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31909 /* Consume the trailing ')'. */
31910 cp_lexer_consume_token (parser
->lexer
);
31912 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31913 tile
= nreverse (tile
);
31914 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31915 OMP_CLAUSE_CHAIN (c
) = list
;
31920 Parse wait clause or directive parameters. */
31923 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31925 vec
<tree
, va_gc
> *args
;
31928 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31930 /*allow_expansion_p=*/true,
31931 /*non_constant_p=*/NULL
);
31933 if (args
== NULL
|| args
->length () == 0)
31935 cp_parser_error (parser
, "expected integer expression before ')'");
31937 release_tree_vector (args
);
31941 args_tree
= build_tree_list_vec (args
);
31943 release_tree_vector (args
);
31945 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31947 tree targ
= TREE_VALUE (t
);
31949 if (targ
!= error_mark_node
)
31951 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31952 error ("%<wait%> expression must be integral");
31955 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31957 targ
= mark_rvalue_use (targ
);
31958 OMP_CLAUSE_DECL (c
) = targ
;
31959 OMP_CLAUSE_CHAIN (c
) = list
;
31969 wait ( int-expr-list ) */
31972 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31974 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31976 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31979 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31985 collapse ( constant-expression ) */
31988 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
31994 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31995 matching_parens parens
;
31996 if (!parens
.require_open (parser
))
31999 num
= cp_parser_constant_expression (parser
);
32001 if (!parens
.require_close (parser
))
32002 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32003 /*or_comma=*/false,
32004 /*consume_paren=*/true);
32006 if (num
== error_mark_node
)
32008 num
= fold_non_dependent_expr (num
);
32009 if (!tree_fits_shwi_p (num
)
32010 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32011 || (n
= tree_to_shwi (num
)) <= 0
32014 error_at (loc
, "collapse argument needs positive constant integer expression");
32018 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32019 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32020 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32021 OMP_CLAUSE_CHAIN (c
) = list
;
32022 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32028 default ( none | shared )
32031 default ( none | present ) */
32034 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32035 location_t location
, bool is_oacc
)
32037 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32040 matching_parens parens
;
32041 if (!parens
.require_open (parser
))
32043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32045 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32046 const char *p
= IDENTIFIER_POINTER (id
);
32051 if (strcmp ("none", p
) != 0)
32053 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32057 if (strcmp ("present", p
) != 0 || !is_oacc
)
32059 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32063 if (strcmp ("shared", p
) != 0 || is_oacc
)
32065 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32072 cp_lexer_consume_token (parser
->lexer
);
32078 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32080 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32083 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32084 || !parens
.require_close (parser
))
32085 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32086 /*or_comma=*/false,
32087 /*consume_paren=*/true);
32089 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32092 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32093 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32094 OMP_CLAUSE_CHAIN (c
) = list
;
32095 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32101 final ( expression ) */
32104 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32108 matching_parens parens
;
32109 if (!parens
.require_open (parser
))
32112 t
= cp_parser_condition (parser
);
32114 if (t
== error_mark_node
32115 || !parens
.require_close (parser
))
32116 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32117 /*or_comma=*/false,
32118 /*consume_paren=*/true);
32120 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32122 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32123 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32124 OMP_CLAUSE_CHAIN (c
) = list
;
32133 if ( directive-name-modifier : expression )
32135 directive-name-modifier:
32136 parallel | task | taskloop | target data | target | target update
32137 | target enter data | target exit data */
32140 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32144 enum tree_code if_modifier
= ERROR_MARK
;
32146 matching_parens parens
;
32147 if (!parens
.require_open (parser
))
32150 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32152 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32153 const char *p
= IDENTIFIER_POINTER (id
);
32156 if (strcmp ("parallel", p
) == 0)
32157 if_modifier
= OMP_PARALLEL
;
32158 else if (strcmp ("task", p
) == 0)
32159 if_modifier
= OMP_TASK
;
32160 else if (strcmp ("taskloop", p
) == 0)
32161 if_modifier
= OMP_TASKLOOP
;
32162 else if (strcmp ("target", p
) == 0)
32164 if_modifier
= OMP_TARGET
;
32165 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32167 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32168 p
= IDENTIFIER_POINTER (id
);
32169 if (strcmp ("data", p
) == 0)
32170 if_modifier
= OMP_TARGET_DATA
;
32171 else if (strcmp ("update", p
) == 0)
32172 if_modifier
= OMP_TARGET_UPDATE
;
32173 else if (strcmp ("enter", p
) == 0)
32174 if_modifier
= OMP_TARGET_ENTER_DATA
;
32175 else if (strcmp ("exit", p
) == 0)
32176 if_modifier
= OMP_TARGET_EXIT_DATA
;
32177 if (if_modifier
!= OMP_TARGET
)
32182 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32183 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32185 if_modifier
= ERROR_MARK
;
32187 if (if_modifier
== OMP_TARGET_ENTER_DATA
32188 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32190 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32192 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32193 p
= IDENTIFIER_POINTER (id
);
32194 if (strcmp ("data", p
) == 0)
32200 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32201 error_at (loc
, "expected %<data%>");
32202 if_modifier
= ERROR_MARK
;
32207 if (if_modifier
!= ERROR_MARK
)
32209 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32212 cp_lexer_consume_token (parser
->lexer
);
32219 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32220 error_at (loc
, "expected %<:%>");
32222 if_modifier
= ERROR_MARK
;
32227 t
= cp_parser_condition (parser
);
32229 if (t
== error_mark_node
32230 || !parens
.require_close (parser
))
32231 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32232 /*or_comma=*/false,
32233 /*consume_paren=*/true);
32235 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32236 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32238 if (if_modifier
!= ERROR_MARK
32239 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32241 const char *p
= NULL
;
32242 switch (if_modifier
)
32244 case OMP_PARALLEL
: p
= "parallel"; break;
32245 case OMP_TASK
: p
= "task"; break;
32246 case OMP_TASKLOOP
: p
= "taskloop"; break;
32247 case OMP_TARGET_DATA
: p
= "target data"; break;
32248 case OMP_TARGET
: p
= "target"; break;
32249 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32250 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32251 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32252 default: gcc_unreachable ();
32254 error_at (location
, "too many %<if%> clauses with %qs modifier",
32258 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32261 error_at (location
, "too many %<if%> clauses");
32263 error_at (location
, "too many %<if%> clauses without modifier");
32266 else if (if_modifier
== ERROR_MARK
32267 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32269 error_at (location
, "if any %<if%> clause has modifier, then all "
32270 "%<if%> clauses have to use modifier");
32275 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32276 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32277 OMP_CLAUSE_IF_EXPR (c
) = t
;
32278 OMP_CLAUSE_CHAIN (c
) = list
;
32287 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32288 tree list
, location_t location
)
32292 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32295 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32296 OMP_CLAUSE_CHAIN (c
) = list
;
32304 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32305 tree list
, location_t location
)
32309 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32311 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32312 OMP_CLAUSE_CHAIN (c
) = list
;
32317 num_threads ( expression ) */
32320 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32321 location_t location
)
32325 matching_parens parens
;
32326 if (!parens
.require_open (parser
))
32329 t
= cp_parser_expression (parser
);
32331 if (t
== error_mark_node
32332 || !parens
.require_close (parser
))
32333 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32334 /*or_comma=*/false,
32335 /*consume_paren=*/true);
32337 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32338 "num_threads", location
);
32340 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32341 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32342 OMP_CLAUSE_CHAIN (c
) = list
;
32348 num_tasks ( expression ) */
32351 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32352 location_t location
)
32356 matching_parens parens
;
32357 if (!parens
.require_open (parser
))
32360 t
= cp_parser_expression (parser
);
32362 if (t
== error_mark_node
32363 || !parens
.require_close (parser
))
32364 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32365 /*or_comma=*/false,
32366 /*consume_paren=*/true);
32368 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32369 "num_tasks", location
);
32371 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32372 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32373 OMP_CLAUSE_CHAIN (c
) = list
;
32379 grainsize ( expression ) */
32382 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32383 location_t location
)
32387 matching_parens parens
;
32388 if (!parens
.require_open (parser
))
32391 t
= cp_parser_expression (parser
);
32393 if (t
== error_mark_node
32394 || !parens
.require_close (parser
))
32395 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32396 /*or_comma=*/false,
32397 /*consume_paren=*/true);
32399 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32400 "grainsize", location
);
32402 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32403 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32404 OMP_CLAUSE_CHAIN (c
) = list
;
32410 priority ( expression ) */
32413 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32414 location_t location
)
32418 matching_parens parens
;
32419 if (!parens
.require_open (parser
))
32422 t
= cp_parser_expression (parser
);
32424 if (t
== error_mark_node
32425 || !parens
.require_close (parser
))
32426 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32427 /*or_comma=*/false,
32428 /*consume_paren=*/true);
32430 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32431 "priority", location
);
32433 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32434 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32435 OMP_CLAUSE_CHAIN (c
) = list
;
32441 hint ( expression ) */
32444 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32445 location_t location
)
32449 matching_parens parens
;
32450 if (!parens
.require_open (parser
))
32453 t
= cp_parser_expression (parser
);
32455 if (t
== error_mark_node
32456 || !parens
.require_close (parser
))
32457 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32458 /*or_comma=*/false,
32459 /*consume_paren=*/true);
32461 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32463 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32464 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32465 OMP_CLAUSE_CHAIN (c
) = list
;
32471 defaultmap ( tofrom : scalar ) */
32474 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32475 location_t location
)
32480 matching_parens parens
;
32481 if (!parens
.require_open (parser
))
32484 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32486 cp_parser_error (parser
, "expected %<tofrom%>");
32489 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32490 p
= IDENTIFIER_POINTER (id
);
32491 if (strcmp (p
, "tofrom") != 0)
32493 cp_parser_error (parser
, "expected %<tofrom%>");
32496 cp_lexer_consume_token (parser
->lexer
);
32497 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32500 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32502 cp_parser_error (parser
, "expected %<scalar%>");
32505 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32506 p
= IDENTIFIER_POINTER (id
);
32507 if (strcmp (p
, "scalar") != 0)
32509 cp_parser_error (parser
, "expected %<scalar%>");
32512 cp_lexer_consume_token (parser
->lexer
);
32513 if (!parens
.require_close (parser
))
32516 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32519 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32520 OMP_CLAUSE_CHAIN (c
) = list
;
32524 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32525 /*or_comma=*/false,
32526 /*consume_paren=*/true);
32534 ordered ( constant-expression ) */
32537 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32538 tree list
, location_t location
)
32540 tree c
, num
= NULL_TREE
;
32543 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32544 "ordered", location
);
32546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32548 matching_parens parens
;
32549 parens
.consume_open (parser
);
32551 num
= cp_parser_constant_expression (parser
);
32553 if (!parens
.require_close (parser
))
32554 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32555 /*or_comma=*/false,
32556 /*consume_paren=*/true);
32558 if (num
== error_mark_node
)
32560 num
= fold_non_dependent_expr (num
);
32561 if (!tree_fits_shwi_p (num
)
32562 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32563 || (n
= tree_to_shwi (num
)) <= 0
32566 error_at (location
,
32567 "ordered argument needs positive constant integer "
32573 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32574 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32575 OMP_CLAUSE_CHAIN (c
) = list
;
32580 reduction ( reduction-operator : variable-list )
32582 reduction-operator:
32583 One of: + * - & ^ | && ||
32587 reduction-operator:
32588 One of: + * - & ^ | && || min max
32592 reduction-operator:
32593 One of: + * - & ^ | && ||
32597 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32599 enum tree_code code
= ERROR_MARK
;
32600 tree nlist
, c
, id
= NULL_TREE
;
32602 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32605 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32607 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32608 case CPP_MULT
: code
= MULT_EXPR
; break;
32609 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32610 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32611 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32612 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32613 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32614 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32618 if (code
!= ERROR_MARK
)
32619 cp_lexer_consume_token (parser
->lexer
);
32622 bool saved_colon_corrects_to_scope_p
;
32623 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32624 parser
->colon_corrects_to_scope_p
= false;
32625 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32626 /*check_dependency_p=*/true,
32627 /*template_p=*/NULL
,
32628 /*declarator_p=*/false,
32629 /*optional_p=*/false);
32630 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32631 if (identifier_p (id
))
32633 const char *p
= IDENTIFIER_POINTER (id
);
32635 if (strcmp (p
, "min") == 0)
32637 else if (strcmp (p
, "max") == 0)
32639 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32641 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32643 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32645 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32646 code
= BIT_AND_EXPR
;
32647 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32648 code
= BIT_IOR_EXPR
;
32649 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32650 code
= BIT_XOR_EXPR
;
32651 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32652 code
= TRUTH_ANDIF_EXPR
;
32653 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32654 code
= TRUTH_ORIF_EXPR
;
32655 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32656 tree scope
= parser
->scope
;
32658 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32659 parser
->scope
= NULL_TREE
;
32660 parser
->qualifying_scope
= NULL_TREE
;
32661 parser
->object_scope
= NULL_TREE
;
32665 error ("invalid reduction-identifier");
32667 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32668 /*or_comma=*/false,
32669 /*consume_paren=*/true);
32674 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32677 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32679 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32681 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32682 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32689 schedule ( schedule-kind )
32690 schedule ( schedule-kind , expression )
32693 static | dynamic | guided | runtime | auto
32696 schedule ( schedule-modifier : schedule-kind )
32697 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32705 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32708 int modifiers
= 0, nmodifiers
= 0;
32710 matching_parens parens
;
32711 if (!parens
.require_open (parser
))
32714 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32716 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32718 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32719 const char *p
= IDENTIFIER_POINTER (id
);
32720 if (strcmp ("simd", p
) == 0)
32721 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32722 else if (strcmp ("monotonic", p
) == 0)
32723 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32724 else if (strcmp ("nonmonotonic", p
) == 0)
32725 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32728 cp_lexer_consume_token (parser
->lexer
);
32729 if (nmodifiers
++ == 0
32730 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32731 cp_lexer_consume_token (parser
->lexer
);
32734 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32741 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32742 const char *p
= IDENTIFIER_POINTER (id
);
32747 if (strcmp ("dynamic", p
) != 0)
32749 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32753 if (strcmp ("guided", p
) != 0)
32755 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32759 if (strcmp ("runtime", p
) != 0)
32761 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32768 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32769 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32770 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32771 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32774 cp_lexer_consume_token (parser
->lexer
);
32776 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32777 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32778 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32779 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32781 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32786 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32789 cp_lexer_consume_token (parser
->lexer
);
32791 token
= cp_lexer_peek_token (parser
->lexer
);
32792 t
= cp_parser_assignment_expression (parser
);
32794 if (t
== error_mark_node
)
32796 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32797 error_at (token
->location
, "schedule %<runtime%> does not take "
32798 "a %<chunk_size%> parameter");
32799 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32800 error_at (token
->location
, "schedule %<auto%> does not take "
32801 "a %<chunk_size%> parameter");
32803 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32805 if (!parens
.require_close (parser
))
32808 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32811 OMP_CLAUSE_SCHEDULE_KIND (c
)
32812 = (enum omp_clause_schedule_kind
)
32813 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32815 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32816 OMP_CLAUSE_CHAIN (c
) = list
;
32820 cp_parser_error (parser
, "invalid schedule kind");
32822 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32823 /*or_comma=*/false,
32824 /*consume_paren=*/true);
32832 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32833 tree list
, location_t location
)
32837 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32839 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32840 OMP_CLAUSE_CHAIN (c
) = list
;
32849 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32850 tree list
, location_t location
)
32852 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32853 tree c
= build_omp_clause (location
, code
);
32854 OMP_CLAUSE_CHAIN (c
) = list
;
32865 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32866 enum omp_clause_code code
,
32867 tree list
, location_t location
)
32869 tree c
= build_omp_clause (location
, code
);
32870 OMP_CLAUSE_CHAIN (c
) = list
;
32878 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32879 tree list
, location_t location
)
32881 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32882 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32883 OMP_CLAUSE_CHAIN (c
) = list
;
32892 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32893 enum omp_clause_code code
,
32894 tree list
, location_t location
)
32896 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32897 tree c
= build_omp_clause (location
, code
);
32898 OMP_CLAUSE_CHAIN (c
) = list
;
32903 num_teams ( expression ) */
32906 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32907 location_t location
)
32911 matching_parens parens
;
32912 if (!parens
.require_open (parser
))
32915 t
= cp_parser_expression (parser
);
32917 if (t
== error_mark_node
32918 || !parens
.require_close (parser
))
32919 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32920 /*or_comma=*/false,
32921 /*consume_paren=*/true);
32923 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32924 "num_teams", location
);
32926 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32927 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32928 OMP_CLAUSE_CHAIN (c
) = list
;
32934 thread_limit ( expression ) */
32937 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32938 location_t location
)
32942 matching_parens parens
;
32943 if (!parens
.require_open (parser
))
32946 t
= cp_parser_expression (parser
);
32948 if (t
== error_mark_node
32949 || !parens
.require_close (parser
))
32950 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32951 /*or_comma=*/false,
32952 /*consume_paren=*/true);
32954 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32955 "thread_limit", location
);
32957 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32958 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32959 OMP_CLAUSE_CHAIN (c
) = list
;
32965 aligned ( variable-list )
32966 aligned ( variable-list : constant-expression ) */
32969 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32971 tree nlist
, c
, alignment
= NULL_TREE
;
32974 matching_parens parens
;
32975 if (!parens
.require_open (parser
))
32978 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32983 alignment
= cp_parser_constant_expression (parser
);
32985 if (!parens
.require_close (parser
))
32986 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32987 /*or_comma=*/false,
32988 /*consume_paren=*/true);
32990 if (alignment
== error_mark_node
)
32991 alignment
= NULL_TREE
;
32994 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32995 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33001 linear ( variable-list )
33002 linear ( variable-list : expression )
33005 linear ( modifier ( variable-list ) )
33006 linear ( modifier ( variable-list ) : expression ) */
33009 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33012 tree nlist
, c
, step
= integer_one_node
;
33014 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33016 matching_parens parens
;
33017 if (!parens
.require_open (parser
))
33020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33022 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33023 const char *p
= IDENTIFIER_POINTER (id
);
33025 if (strcmp ("ref", p
) == 0)
33026 kind
= OMP_CLAUSE_LINEAR_REF
;
33027 else if (strcmp ("val", p
) == 0)
33028 kind
= OMP_CLAUSE_LINEAR_VAL
;
33029 else if (strcmp ("uval", p
) == 0)
33030 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33031 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33032 cp_lexer_consume_token (parser
->lexer
);
33034 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33037 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33038 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33042 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33043 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33045 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33046 else if (!parens
.require_close (parser
))
33047 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33048 /*or_comma=*/false,
33049 /*consume_paren=*/true);
33056 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33057 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33059 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33060 cp_parser_parse_tentatively (parser
);
33061 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33062 /*check_dependency_p=*/true,
33063 /*template_p=*/NULL
,
33064 /*declarator_p=*/false,
33065 /*optional_p=*/false);
33066 if (step
!= error_mark_node
)
33067 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33068 if (step
== error_mark_node
)
33071 cp_parser_abort_tentative_parse (parser
);
33073 else if (!cp_parser_parse_definitely (parser
))
33077 step
= cp_parser_expression (parser
);
33079 if (!parens
.require_close (parser
))
33080 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33081 /*or_comma=*/false,
33082 /*consume_paren=*/true);
33084 if (step
== error_mark_node
)
33088 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33090 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33091 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33098 safelen ( constant-expression ) */
33101 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33102 location_t location
)
33106 matching_parens parens
;
33107 if (!parens
.require_open (parser
))
33110 t
= cp_parser_constant_expression (parser
);
33112 if (t
== error_mark_node
33113 || !parens
.require_close (parser
))
33114 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33115 /*or_comma=*/false,
33116 /*consume_paren=*/true);
33118 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33120 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33121 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33122 OMP_CLAUSE_CHAIN (c
) = list
;
33128 simdlen ( constant-expression ) */
33131 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33132 location_t location
)
33136 matching_parens parens
;
33137 if (!parens
.require_open (parser
))
33140 t
= cp_parser_constant_expression (parser
);
33142 if (t
== error_mark_node
33143 || !parens
.require_close (parser
))
33144 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33145 /*or_comma=*/false,
33146 /*consume_paren=*/true);
33148 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33150 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33151 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33152 OMP_CLAUSE_CHAIN (c
) = list
;
33159 identifier [+/- integer]
33160 vec , identifier [+/- integer]
33164 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33169 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33171 cp_parser_error (parser
, "expected identifier");
33175 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33177 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33178 tree t
, identifier
= cp_parser_identifier (parser
);
33179 tree addend
= NULL
;
33181 if (identifier
== error_mark_node
)
33182 t
= error_mark_node
;
33185 t
= cp_parser_lookup_name_simple
33186 (parser
, identifier
,
33187 cp_lexer_peek_token (parser
->lexer
)->location
);
33188 if (t
== error_mark_node
)
33189 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33196 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33198 addend
= integer_zero_node
;
33199 goto add_to_vector
;
33201 cp_lexer_consume_token (parser
->lexer
);
33203 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33205 cp_parser_error (parser
, "expected integer");
33209 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33210 if (TREE_CODE (addend
) != INTEGER_CST
)
33212 cp_parser_error (parser
, "expected integer");
33215 cp_lexer_consume_token (parser
->lexer
);
33218 if (t
!= error_mark_node
)
33220 vec
= tree_cons (addend
, t
, vec
);
33222 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33225 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33228 cp_lexer_consume_token (parser
->lexer
);
33231 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33233 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33234 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33235 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33236 OMP_CLAUSE_CHAIN (u
) = list
;
33243 depend ( depend-kind : variable-list )
33251 depend ( sink : vec ) */
33254 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33257 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33259 matching_parens parens
;
33260 if (!parens
.require_open (parser
))
33263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33265 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33266 const char *p
= IDENTIFIER_POINTER (id
);
33268 if (strcmp ("in", p
) == 0)
33269 kind
= OMP_CLAUSE_DEPEND_IN
;
33270 else if (strcmp ("inout", p
) == 0)
33271 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33272 else if (strcmp ("out", p
) == 0)
33273 kind
= OMP_CLAUSE_DEPEND_OUT
;
33274 else if (strcmp ("source", p
) == 0)
33275 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33276 else if (strcmp ("sink", p
) == 0)
33277 kind
= OMP_CLAUSE_DEPEND_SINK
;
33284 cp_lexer_consume_token (parser
->lexer
);
33286 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33288 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33289 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33290 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33291 OMP_CLAUSE_CHAIN (c
) = list
;
33292 if (!parens
.require_close (parser
))
33293 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33294 /*or_comma=*/false,
33295 /*consume_paren=*/true);
33299 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33302 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33303 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33306 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33309 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33310 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33315 cp_parser_error (parser
, "invalid depend kind");
33317 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33318 /*or_comma=*/false,
33319 /*consume_paren=*/true);
33324 map ( map-kind : variable-list )
33325 map ( variable-list )
33328 alloc | to | from | tofrom
33332 alloc | to | from | tofrom | release | delete
33334 map ( always [,] map-kind: variable-list ) */
33337 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33340 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33341 bool always
= false;
33343 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33348 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33349 const char *p
= IDENTIFIER_POINTER (id
);
33351 if (strcmp ("always", p
) == 0)
33354 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33356 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33357 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33359 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33363 cp_lexer_consume_token (parser
->lexer
);
33365 cp_lexer_consume_token (parser
->lexer
);
33370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33371 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33373 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33374 const char *p
= IDENTIFIER_POINTER (id
);
33376 if (strcmp ("alloc", p
) == 0)
33377 kind
= GOMP_MAP_ALLOC
;
33378 else if (strcmp ("to", p
) == 0)
33379 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33380 else if (strcmp ("from", p
) == 0)
33381 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33382 else if (strcmp ("tofrom", p
) == 0)
33383 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33384 else if (strcmp ("release", p
) == 0)
33385 kind
= GOMP_MAP_RELEASE
;
33388 cp_parser_error (parser
, "invalid map kind");
33389 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33390 /*or_comma=*/false,
33391 /*consume_paren=*/true);
33394 cp_lexer_consume_token (parser
->lexer
);
33395 cp_lexer_consume_token (parser
->lexer
);
33397 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33398 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33400 kind
= GOMP_MAP_DELETE
;
33401 cp_lexer_consume_token (parser
->lexer
);
33402 cp_lexer_consume_token (parser
->lexer
);
33405 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33408 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33409 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33415 device ( expression ) */
33418 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33419 location_t location
)
33423 matching_parens parens
;
33424 if (!parens
.require_open (parser
))
33427 t
= cp_parser_expression (parser
);
33429 if (t
== error_mark_node
33430 || !parens
.require_close (parser
))
33431 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33432 /*or_comma=*/false,
33433 /*consume_paren=*/true);
33435 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33436 "device", location
);
33438 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33439 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33440 OMP_CLAUSE_CHAIN (c
) = list
;
33446 dist_schedule ( static )
33447 dist_schedule ( static , expression ) */
33450 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33451 location_t location
)
33455 matching_parens parens
;
33456 if (!parens
.require_open (parser
))
33459 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33461 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33463 cp_lexer_consume_token (parser
->lexer
);
33465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33467 cp_lexer_consume_token (parser
->lexer
);
33469 t
= cp_parser_assignment_expression (parser
);
33471 if (t
== error_mark_node
)
33473 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33475 if (!parens
.require_close (parser
))
33478 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33481 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33483 OMP_CLAUSE_CHAIN (c
) = list
;
33487 cp_parser_error (parser
, "invalid dist_schedule kind");
33489 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33490 /*or_comma=*/false,
33491 /*consume_paren=*/true);
33496 proc_bind ( proc-bind-kind )
33499 master | close | spread */
33502 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33503 location_t location
)
33506 enum omp_clause_proc_bind_kind kind
;
33508 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33513 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33514 const char *p
= IDENTIFIER_POINTER (id
);
33516 if (strcmp ("master", p
) == 0)
33517 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33518 else if (strcmp ("close", p
) == 0)
33519 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33520 else if (strcmp ("spread", p
) == 0)
33521 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33528 cp_lexer_consume_token (parser
->lexer
);
33529 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33532 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33533 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33535 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33536 OMP_CLAUSE_CHAIN (c
) = list
;
33540 cp_parser_error (parser
, "invalid depend kind");
33542 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33543 /*or_comma=*/false,
33544 /*consume_paren=*/true);
33549 async [( int-expr )] */
33552 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33555 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33557 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33559 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33561 matching_parens parens
;
33562 parens
.consume_open (parser
);
33564 t
= cp_parser_expression (parser
);
33565 if (t
== error_mark_node
33566 || !parens
.require_close (parser
))
33567 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33568 /*or_comma=*/false,
33569 /*consume_paren=*/true);
33572 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33574 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33575 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33576 OMP_CLAUSE_CHAIN (c
) = list
;
33582 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33583 is a bitmask in MASK. Return the list of clauses found. */
33586 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33587 const char *where
, cp_token
*pragma_tok
,
33588 bool finish_p
= true)
33590 tree clauses
= NULL
;
33593 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33596 pragma_omp_clause c_kind
;
33597 omp_clause_code code
;
33598 const char *c_name
;
33599 tree prev
= clauses
;
33601 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33602 cp_lexer_consume_token (parser
->lexer
);
33604 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33605 c_kind
= cp_parser_omp_clause_name (parser
);
33609 case PRAGMA_OACC_CLAUSE_ASYNC
:
33610 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33613 case PRAGMA_OACC_CLAUSE_AUTO
:
33614 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33618 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33619 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33620 c_name
= "collapse";
33622 case PRAGMA_OACC_CLAUSE_COPY
:
33623 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33626 case PRAGMA_OACC_CLAUSE_COPYIN
:
33627 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33630 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33631 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33632 c_name
= "copyout";
33634 case PRAGMA_OACC_CLAUSE_CREATE
:
33635 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33638 case PRAGMA_OACC_CLAUSE_DELETE
:
33639 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33642 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33643 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33644 c_name
= "default";
33646 case PRAGMA_OACC_CLAUSE_DEVICE
:
33647 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33650 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33651 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33652 c_name
= "deviceptr";
33654 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33655 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33656 c_name
= "device_resident";
33658 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33659 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33661 c_name
= "firstprivate";
33663 case PRAGMA_OACC_CLAUSE_GANG
:
33665 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33668 case PRAGMA_OACC_CLAUSE_HOST
:
33669 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33672 case PRAGMA_OACC_CLAUSE_IF
:
33673 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33676 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33677 clauses
= cp_parser_oacc_simple_clause (parser
,
33678 OMP_CLAUSE_INDEPENDENT
,
33680 c_name
= "independent";
33682 case PRAGMA_OACC_CLAUSE_LINK
:
33683 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33686 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33687 code
= OMP_CLAUSE_NUM_GANGS
;
33688 c_name
= "num_gangs";
33689 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33692 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33693 c_name
= "num_workers";
33694 code
= OMP_CLAUSE_NUM_WORKERS
;
33695 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33698 case PRAGMA_OACC_CLAUSE_PRESENT
:
33699 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33700 c_name
= "present";
33702 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33703 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33704 c_name
= "present_or_copy";
33706 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33707 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33708 c_name
= "present_or_copyin";
33710 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33711 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33712 c_name
= "present_or_copyout";
33714 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33715 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33716 c_name
= "present_or_create";
33718 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33719 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33721 c_name
= "private";
33723 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33724 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33725 c_name
= "reduction";
33727 case PRAGMA_OACC_CLAUSE_SELF
:
33728 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33731 case PRAGMA_OACC_CLAUSE_SEQ
:
33732 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33736 case PRAGMA_OACC_CLAUSE_TILE
:
33737 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33740 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33741 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33743 c_name
= "use_device";
33745 case PRAGMA_OACC_CLAUSE_VECTOR
:
33747 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33750 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33751 c_name
= "vector_length";
33752 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33753 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33756 case PRAGMA_OACC_CLAUSE_WAIT
:
33757 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33760 case PRAGMA_OACC_CLAUSE_WORKER
:
33762 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33766 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33772 if (((mask
>> c_kind
) & 1) == 0)
33774 /* Remove the invalid clause(s) from the list to avoid
33775 confusing the rest of the compiler. */
33777 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33782 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33785 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33790 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33791 is a bitmask in MASK. Return the list of clauses found; the result
33792 of clause default goes in *pdefault. */
33795 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33796 const char *where
, cp_token
*pragma_tok
,
33797 bool finish_p
= true)
33799 tree clauses
= NULL
;
33801 cp_token
*token
= NULL
;
33803 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33805 pragma_omp_clause c_kind
;
33806 const char *c_name
;
33807 tree prev
= clauses
;
33809 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33810 cp_lexer_consume_token (parser
->lexer
);
33812 token
= cp_lexer_peek_token (parser
->lexer
);
33813 c_kind
= cp_parser_omp_clause_name (parser
);
33817 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33818 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33820 c_name
= "collapse";
33822 case PRAGMA_OMP_CLAUSE_COPYIN
:
33823 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33826 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33827 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33829 c_name
= "copyprivate";
33831 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33832 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33833 token
->location
, false);
33834 c_name
= "default";
33836 case PRAGMA_OMP_CLAUSE_FINAL
:
33837 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33840 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33841 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33843 c_name
= "firstprivate";
33845 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33846 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33848 c_name
= "grainsize";
33850 case PRAGMA_OMP_CLAUSE_HINT
:
33851 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33855 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33856 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33858 c_name
= "defaultmap";
33860 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33861 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33863 c_name
= "use_device_ptr";
33865 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33866 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33868 c_name
= "is_device_ptr";
33870 case PRAGMA_OMP_CLAUSE_IF
:
33871 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33875 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33876 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33878 c_name
= "lastprivate";
33880 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33881 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33883 c_name
= "mergeable";
33885 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33886 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33889 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33890 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33892 c_name
= "num_tasks";
33894 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33895 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33897 c_name
= "num_threads";
33899 case PRAGMA_OMP_CLAUSE_ORDERED
:
33900 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33902 c_name
= "ordered";
33904 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33905 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33907 c_name
= "priority";
33909 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33910 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33912 c_name
= "private";
33914 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33915 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33916 c_name
= "reduction";
33918 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33919 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33921 c_name
= "schedule";
33923 case PRAGMA_OMP_CLAUSE_SHARED
:
33924 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33928 case PRAGMA_OMP_CLAUSE_UNTIED
:
33929 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33933 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33934 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33935 clauses
, token
->location
);
33936 c_name
= "inbranch";
33938 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33939 clauses
= cp_parser_omp_clause_branch (parser
,
33940 OMP_CLAUSE_NOTINBRANCH
,
33941 clauses
, token
->location
);
33942 c_name
= "notinbranch";
33944 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33945 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33946 clauses
, token
->location
);
33947 c_name
= "parallel";
33951 error_at (token
->location
, "%qs must be the first clause of %qs",
33956 case PRAGMA_OMP_CLAUSE_FOR
:
33957 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33958 clauses
, token
->location
);
33961 goto clause_not_first
;
33963 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33964 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33965 clauses
, token
->location
);
33966 c_name
= "sections";
33968 goto clause_not_first
;
33970 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33971 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33972 clauses
, token
->location
);
33973 c_name
= "taskgroup";
33975 goto clause_not_first
;
33977 case PRAGMA_OMP_CLAUSE_LINK
:
33978 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33981 case PRAGMA_OMP_CLAUSE_TO
:
33982 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33983 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33986 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33989 case PRAGMA_OMP_CLAUSE_FROM
:
33990 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
33993 case PRAGMA_OMP_CLAUSE_UNIFORM
:
33994 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
33996 c_name
= "uniform";
33998 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
33999 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34001 c_name
= "num_teams";
34003 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34004 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34006 c_name
= "thread_limit";
34008 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34009 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34010 c_name
= "aligned";
34012 case PRAGMA_OMP_CLAUSE_LINEAR
:
34014 bool declare_simd
= false;
34015 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34016 declare_simd
= true;
34017 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34021 case PRAGMA_OMP_CLAUSE_DEPEND
:
34022 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34026 case PRAGMA_OMP_CLAUSE_MAP
:
34027 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34030 case PRAGMA_OMP_CLAUSE_DEVICE
:
34031 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34035 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34036 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34038 c_name
= "dist_schedule";
34040 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34041 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34043 c_name
= "proc_bind";
34045 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34046 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34048 c_name
= "safelen";
34050 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34051 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34053 c_name
= "simdlen";
34055 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34056 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34058 c_name
= "nogroup";
34060 case PRAGMA_OMP_CLAUSE_THREADS
:
34062 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34063 clauses
, token
->location
);
34064 c_name
= "threads";
34066 case PRAGMA_OMP_CLAUSE_SIMD
:
34068 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34069 clauses
, token
->location
);
34073 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34079 if (((mask
>> c_kind
) & 1) == 0)
34081 /* Remove the invalid clause(s) from the list to avoid
34082 confusing the rest of the compiler. */
34084 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34088 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34091 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34092 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34094 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34103 In practice, we're also interested in adding the statement to an
34104 outer node. So it is convenient if we work around the fact that
34105 cp_parser_statement calls add_stmt. */
34108 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34110 unsigned save
= parser
->in_statement
;
34112 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34113 This preserves the "not within loop or switch" style error messages
34114 for nonsense cases like
34120 if (parser
->in_statement
)
34121 parser
->in_statement
= IN_OMP_BLOCK
;
34127 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34129 parser
->in_statement
= save
;
34133 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34135 tree stmt
= begin_omp_structured_block ();
34136 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34138 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34140 cp_parser_end_omp_structured_block (parser
, save
);
34141 return finish_omp_structured_block (stmt
);
34145 # pragma omp atomic new-line
34149 x binop= expr | x++ | ++x | x-- | --x
34151 +, *, -, /, &, ^, |, <<, >>
34153 where x is an lvalue expression with scalar type.
34156 # pragma omp atomic new-line
34159 # pragma omp atomic read new-line
34162 # pragma omp atomic write new-line
34165 # pragma omp atomic update new-line
34168 # pragma omp atomic capture new-line
34171 # pragma omp atomic capture new-line
34179 expression-stmt | x = x binop expr
34181 v = expression-stmt
34183 { v = x; update-stmt; } | { update-stmt; v = x; }
34187 expression-stmt | x = x binop expr | x = expr binop x
34191 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34193 where x and v are lvalue expressions with scalar type. */
34196 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34198 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34199 tree rhs1
= NULL_TREE
, orig_lhs
;
34200 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34201 bool structured_block
= false;
34202 bool seq_cst
= false;
34204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34206 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34207 const char *p
= IDENTIFIER_POINTER (id
);
34209 if (!strcmp (p
, "seq_cst"))
34212 cp_lexer_consume_token (parser
->lexer
);
34213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34214 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34215 cp_lexer_consume_token (parser
->lexer
);
34218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34220 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34221 const char *p
= IDENTIFIER_POINTER (id
);
34223 if (!strcmp (p
, "read"))
34224 code
= OMP_ATOMIC_READ
;
34225 else if (!strcmp (p
, "write"))
34227 else if (!strcmp (p
, "update"))
34229 else if (!strcmp (p
, "capture"))
34230 code
= OMP_ATOMIC_CAPTURE_NEW
;
34234 cp_lexer_consume_token (parser
->lexer
);
34238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34239 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34240 cp_lexer_consume_token (parser
->lexer
);
34242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34244 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34245 const char *p
= IDENTIFIER_POINTER (id
);
34247 if (!strcmp (p
, "seq_cst"))
34250 cp_lexer_consume_token (parser
->lexer
);
34254 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34258 case OMP_ATOMIC_READ
:
34259 case NOP_EXPR
: /* atomic write */
34260 v
= cp_parser_unary_expression (parser
);
34261 if (v
== error_mark_node
)
34263 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34265 if (code
== NOP_EXPR
)
34266 lhs
= cp_parser_expression (parser
);
34268 lhs
= cp_parser_unary_expression (parser
);
34269 if (lhs
== error_mark_node
)
34271 if (code
== NOP_EXPR
)
34273 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34281 case OMP_ATOMIC_CAPTURE_NEW
:
34282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34284 cp_lexer_consume_token (parser
->lexer
);
34285 structured_block
= true;
34289 v
= cp_parser_unary_expression (parser
);
34290 if (v
== error_mark_node
)
34292 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34300 lhs
= cp_parser_unary_expression (parser
);
34302 switch (TREE_CODE (lhs
))
34307 case POSTINCREMENT_EXPR
:
34308 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34309 code
= OMP_ATOMIC_CAPTURE_OLD
;
34311 case PREINCREMENT_EXPR
:
34312 lhs
= TREE_OPERAND (lhs
, 0);
34313 opcode
= PLUS_EXPR
;
34314 rhs
= integer_one_node
;
34317 case POSTDECREMENT_EXPR
:
34318 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34319 code
= OMP_ATOMIC_CAPTURE_OLD
;
34321 case PREDECREMENT_EXPR
:
34322 lhs
= TREE_OPERAND (lhs
, 0);
34323 opcode
= MINUS_EXPR
;
34324 rhs
= integer_one_node
;
34327 case COMPOUND_EXPR
:
34328 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34329 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34330 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34331 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34332 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34333 (TREE_OPERAND (lhs
, 1), 0), 0)))
34335 /* Undo effects of boolean_increment for post {in,de}crement. */
34336 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34339 if (TREE_CODE (lhs
) == MODIFY_EXPR
34340 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34342 /* Undo effects of boolean_increment. */
34343 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34345 /* This is pre or post increment. */
34346 rhs
= TREE_OPERAND (lhs
, 1);
34347 lhs
= TREE_OPERAND (lhs
, 0);
34349 if (code
== OMP_ATOMIC_CAPTURE_NEW
34350 && !structured_block
34351 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34352 code
= OMP_ATOMIC_CAPTURE_OLD
;
34358 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34361 opcode
= MULT_EXPR
;
34364 opcode
= TRUNC_DIV_EXPR
;
34367 opcode
= PLUS_EXPR
;
34370 opcode
= MINUS_EXPR
;
34372 case CPP_LSHIFT_EQ
:
34373 opcode
= LSHIFT_EXPR
;
34375 case CPP_RSHIFT_EQ
:
34376 opcode
= RSHIFT_EXPR
;
34379 opcode
= BIT_AND_EXPR
;
34382 opcode
= BIT_IOR_EXPR
;
34385 opcode
= BIT_XOR_EXPR
;
34388 enum cp_parser_prec oprec
;
34390 cp_lexer_consume_token (parser
->lexer
);
34391 cp_parser_parse_tentatively (parser
);
34392 rhs1
= cp_parser_simple_cast_expression (parser
);
34393 if (rhs1
== error_mark_node
)
34395 cp_parser_abort_tentative_parse (parser
);
34396 cp_parser_simple_cast_expression (parser
);
34399 token
= cp_lexer_peek_token (parser
->lexer
);
34400 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34402 cp_parser_abort_tentative_parse (parser
);
34403 cp_parser_parse_tentatively (parser
);
34404 rhs
= cp_parser_binary_expression (parser
, false, true,
34405 PREC_NOT_OPERATOR
, NULL
);
34406 if (rhs
== error_mark_node
)
34408 cp_parser_abort_tentative_parse (parser
);
34409 cp_parser_binary_expression (parser
, false, true,
34410 PREC_NOT_OPERATOR
, NULL
);
34413 switch (TREE_CODE (rhs
))
34416 case TRUNC_DIV_EXPR
:
34425 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34427 if (cp_parser_parse_definitely (parser
))
34429 opcode
= TREE_CODE (rhs
);
34430 rhs1
= TREE_OPERAND (rhs
, 0);
34431 rhs
= TREE_OPERAND (rhs
, 1);
34441 cp_parser_abort_tentative_parse (parser
);
34442 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34444 rhs
= cp_parser_expression (parser
);
34445 if (rhs
== error_mark_node
)
34451 cp_parser_error (parser
,
34452 "invalid form of %<#pragma omp atomic%>");
34455 if (!cp_parser_parse_definitely (parser
))
34457 switch (token
->type
)
34459 case CPP_SEMICOLON
:
34460 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34462 code
= OMP_ATOMIC_CAPTURE_OLD
;
34467 cp_lexer_consume_token (parser
->lexer
);
34470 else if (structured_block
)
34477 cp_parser_error (parser
,
34478 "invalid form of %<#pragma omp atomic%>");
34481 opcode
= MULT_EXPR
;
34484 opcode
= TRUNC_DIV_EXPR
;
34487 opcode
= PLUS_EXPR
;
34490 opcode
= MINUS_EXPR
;
34493 opcode
= LSHIFT_EXPR
;
34496 opcode
= RSHIFT_EXPR
;
34499 opcode
= BIT_AND_EXPR
;
34502 opcode
= BIT_IOR_EXPR
;
34505 opcode
= BIT_XOR_EXPR
;
34508 cp_parser_error (parser
,
34509 "invalid operator for %<#pragma omp atomic%>");
34512 oprec
= TOKEN_PRECEDENCE (token
);
34513 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34514 if (commutative_tree_code (opcode
))
34515 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34516 cp_lexer_consume_token (parser
->lexer
);
34517 rhs
= cp_parser_binary_expression (parser
, false, false,
34519 if (rhs
== error_mark_node
)
34524 cp_parser_error (parser
,
34525 "invalid operator for %<#pragma omp atomic%>");
34528 cp_lexer_consume_token (parser
->lexer
);
34530 rhs
= cp_parser_expression (parser
);
34531 if (rhs
== error_mark_node
)
34536 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34538 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34540 v
= cp_parser_unary_expression (parser
);
34541 if (v
== error_mark_node
)
34543 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34545 lhs1
= cp_parser_unary_expression (parser
);
34546 if (lhs1
== error_mark_node
)
34549 if (structured_block
)
34551 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34552 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34555 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34556 if (!structured_block
)
34557 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34561 cp_parser_skip_to_end_of_block_or_statement (parser
);
34562 if (structured_block
)
34564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34565 cp_lexer_consume_token (parser
->lexer
);
34566 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34568 cp_parser_skip_to_end_of_block_or_statement (parser
);
34569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34570 cp_lexer_consume_token (parser
->lexer
);
34577 # pragma omp barrier new-line */
34580 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34582 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34583 finish_omp_barrier ();
34587 # pragma omp critical [(name)] new-line
34591 # pragma omp critical [(name) [hint(expression)]] new-line
34592 structured-block */
34594 #define OMP_CRITICAL_CLAUSE_MASK \
34595 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34598 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34600 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34604 matching_parens parens
;
34605 parens
.consume_open (parser
);
34607 name
= cp_parser_identifier (parser
);
34609 if (name
== error_mark_node
34610 || !parens
.require_close (parser
))
34611 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34612 /*or_comma=*/false,
34613 /*consume_paren=*/true);
34614 if (name
== error_mark_node
)
34617 clauses
= cp_parser_omp_all_clauses (parser
,
34618 OMP_CRITICAL_CLAUSE_MASK
,
34619 "#pragma omp critical", pragma_tok
);
34622 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34624 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34625 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34629 # pragma omp flush flush-vars[opt] new-line
34632 ( variable-list ) */
34635 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34638 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34639 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34641 finish_omp_flush ();
34644 /* Helper function, to parse omp for increment expression. */
34647 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34649 tree cond
= cp_parser_binary_expression (parser
, false, true,
34650 PREC_NOT_OPERATOR
, NULL
);
34651 if (cond
== error_mark_node
34652 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34654 cp_parser_skip_to_end_of_statement (parser
);
34655 return error_mark_node
;
34658 switch (TREE_CODE (cond
))
34666 /* Fall through: OpenMP disallows NE_EXPR. */
34667 gcc_fallthrough ();
34669 return error_mark_node
;
34672 /* If decl is an iterator, preserve LHS and RHS of the relational
34673 expr until finish_omp_for. */
34675 && (type_dependent_expression_p (decl
)
34676 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34679 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34681 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34682 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34683 /*overload=*/NULL
, tf_warning_or_error
);
34686 /* Helper function, to parse omp for increment expression. */
34689 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34691 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34697 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34699 op
= (token
->type
== CPP_PLUS_PLUS
34700 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34701 cp_lexer_consume_token (parser
->lexer
);
34702 lhs
= cp_parser_simple_cast_expression (parser
);
34704 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34705 return error_mark_node
;
34706 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34709 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34711 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34712 return error_mark_node
;
34714 token
= cp_lexer_peek_token (parser
->lexer
);
34715 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34717 op
= (token
->type
== CPP_PLUS_PLUS
34718 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34719 cp_lexer_consume_token (parser
->lexer
);
34720 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34723 op
= cp_parser_assignment_operator_opt (parser
);
34724 if (op
== ERROR_MARK
)
34725 return error_mark_node
;
34727 if (op
!= NOP_EXPR
)
34729 rhs
= cp_parser_assignment_expression (parser
);
34730 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34731 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34734 lhs
= cp_parser_binary_expression (parser
, false, false,
34735 PREC_ADDITIVE_EXPRESSION
, NULL
);
34736 token
= cp_lexer_peek_token (parser
->lexer
);
34737 decl_first
= (lhs
== decl
34738 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34741 if (token
->type
!= CPP_PLUS
34742 && token
->type
!= CPP_MINUS
)
34743 return error_mark_node
;
34747 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34748 cp_lexer_consume_token (parser
->lexer
);
34749 rhs
= cp_parser_binary_expression (parser
, false, false,
34750 PREC_ADDITIVE_EXPRESSION
, NULL
);
34751 token
= cp_lexer_peek_token (parser
->lexer
);
34752 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34754 if (lhs
== NULL_TREE
)
34756 if (op
== PLUS_EXPR
)
34759 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34760 tf_warning_or_error
);
34763 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34764 ERROR_MARK
, NULL
, tf_warning_or_error
);
34767 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34772 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34773 || op
== MINUS_EXPR
)
34774 return error_mark_node
;
34775 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34778 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34780 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34783 /* Parse the initialization statement of an OpenMP for loop.
34785 Return true if the resulting construct should have an
34786 OMP_CLAUSE_PRIVATE added to it. */
34789 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34790 tree
&this_pre_body
,
34791 vec
<tree
, va_gc
> *for_block
,
34797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34800 tree add_private_clause
= NULL_TREE
;
34802 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34806 integer-type var = lb
34807 random-access-iterator-type var = lb
34808 pointer-type var = lb
34810 cp_decl_specifier_seq type_specifiers
;
34812 /* First, try to parse as an initialized declaration. See
34813 cp_parser_condition, from whence the bulk of this is copied. */
34815 cp_parser_parse_tentatively (parser
);
34816 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34817 /*is_trailing_return=*/false,
34819 if (cp_parser_parse_definitely (parser
))
34821 /* If parsing a type specifier seq succeeded, then this
34822 MUST be a initialized declaration. */
34823 tree asm_specification
, attributes
;
34824 cp_declarator
*declarator
;
34826 declarator
= cp_parser_declarator (parser
,
34827 CP_PARSER_DECLARATOR_NAMED
,
34828 /*ctor_dtor_or_conv_p=*/NULL
,
34829 /*parenthesized_p=*/NULL
,
34830 /*member_p=*/false,
34831 /*friend_p=*/false);
34832 attributes
= cp_parser_attributes_opt (parser
);
34833 asm_specification
= cp_parser_asm_specification_opt (parser
);
34835 if (declarator
== cp_error_declarator
)
34836 cp_parser_skip_to_end_of_statement (parser
);
34840 tree pushed_scope
, auto_node
;
34842 decl
= start_decl (declarator
, &type_specifiers
,
34843 SD_INITIALIZED
, attributes
,
34844 /*prefix_attributes=*/NULL_TREE
,
34847 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34848 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34850 if (cp_lexer_next_token_is (parser
->lexer
,
34852 error ("parenthesized initialization is not allowed in "
34853 "OpenMP %<for%> loop");
34855 /* Trigger an error. */
34856 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34858 init
= error_mark_node
;
34859 cp_parser_skip_to_end_of_statement (parser
);
34861 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34862 || type_dependent_expression_p (decl
)
34865 bool is_direct_init
, is_non_constant_init
;
34867 init
= cp_parser_initializer (parser
,
34869 &is_non_constant_init
);
34874 = do_auto_deduction (TREE_TYPE (decl
), init
,
34877 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34878 && !type_dependent_expression_p (decl
))
34882 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34884 LOOKUP_ONLYCONVERTING
);
34886 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34888 vec_safe_push (for_block
, this_pre_body
);
34893 init
= pop_stmt_list (this_pre_body
);
34894 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34896 tree_stmt_iterator i
= tsi_start (init
);
34897 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34898 while (!tsi_end_p (i
))
34900 tree t
= tsi_stmt (i
);
34901 if (TREE_CODE (t
) == DECL_EXPR
34902 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34905 vec_safe_push (for_block
, t
);
34910 if (tsi_one_before_end_p (i
))
34912 tree t
= tsi_stmt (i
);
34914 free_stmt_list (init
);
34919 this_pre_body
= NULL_TREE
;
34924 cp_lexer_consume_token (parser
->lexer
);
34925 init
= cp_parser_assignment_expression (parser
);
34928 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34929 init
= error_mark_node
;
34931 cp_finish_decl (decl
, NULL_TREE
,
34932 /*init_const_expr_p=*/false,
34934 LOOKUP_ONLYCONVERTING
);
34938 pop_scope (pushed_scope
);
34944 /* If parsing a type specifier sequence failed, then
34945 this MUST be a simple expression. */
34946 cp_parser_parse_tentatively (parser
);
34947 decl
= cp_parser_primary_expression (parser
, false, false,
34949 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34950 if (!cp_parser_error_occurred (parser
)
34952 && (TREE_CODE (decl
) == COMPONENT_REF
34953 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34955 cp_parser_abort_tentative_parse (parser
);
34956 cp_parser_parse_tentatively (parser
);
34957 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34958 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34959 /*check_dependency_p=*/true,
34960 /*template_p=*/NULL
,
34961 /*declarator_p=*/false,
34962 /*optional_p=*/false);
34963 if (name
!= error_mark_node
34964 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34966 decl
= cp_parser_lookup_name_simple (parser
, name
,
34968 if (TREE_CODE (decl
) == FIELD_DECL
)
34969 add_private_clause
= omp_privatize_field (decl
, false);
34971 cp_parser_abort_tentative_parse (parser
);
34972 cp_parser_parse_tentatively (parser
);
34973 decl
= cp_parser_primary_expression (parser
, false, false,
34976 if (!cp_parser_error_occurred (parser
)
34979 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34983 cp_parser_parse_definitely (parser
);
34984 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34985 rhs
= cp_parser_assignment_expression (parser
);
34987 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34990 tf_warning_or_error
));
34991 if (!add_private_clause
)
34992 add_private_clause
= decl
;
34997 cp_parser_abort_tentative_parse (parser
);
34998 init
= cp_parser_expression (parser
);
35001 if (TREE_CODE (init
) == MODIFY_EXPR
35002 || TREE_CODE (init
) == MODOP_EXPR
)
35003 real_decl
= TREE_OPERAND (init
, 0);
35007 return add_private_clause
;
35010 /* Parse the restricted form of the for statement allowed by OpenMP. */
35013 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35014 tree
*cclauses
, bool *if_p
)
35016 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35017 tree real_decl
, initv
, condv
, incrv
, declv
;
35018 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35019 location_t loc_first
;
35020 bool collapse_err
= false;
35021 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35022 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35023 auto_vec
<tree
, 4> orig_inits
;
35024 bool tiling
= false;
35026 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35027 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35028 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35029 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35032 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35034 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35035 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35038 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35041 if (ordered
&& ordered
< collapse
)
35043 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35044 "%<ordered%> clause parameter is less than %<collapse%>");
35045 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35046 = build_int_cst (NULL_TREE
, collapse
);
35047 ordered
= collapse
;
35051 for (tree
*pc
= &clauses
; *pc
; )
35052 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35054 error_at (OMP_CLAUSE_LOCATION (*pc
),
35055 "%<linear%> clause may not be specified together "
35056 "with %<ordered%> clause with a parameter");
35057 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35060 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35063 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35064 count
= ordered
? ordered
: collapse
;
35066 declv
= make_tree_vec (count
);
35067 initv
= make_tree_vec (count
);
35068 condv
= make_tree_vec (count
);
35069 incrv
= make_tree_vec (count
);
35071 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35073 for (i
= 0; i
< count
; i
++)
35075 int bracecount
= 0;
35076 tree add_private_clause
= NULL_TREE
;
35079 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35082 cp_parser_error (parser
, "for statement expected");
35085 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35087 matching_parens parens
;
35088 if (!parens
.require_open (parser
))
35091 init
= orig_init
= decl
= real_decl
= NULL
;
35092 this_pre_body
= push_stmt_list ();
35095 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35096 init
, orig_init
, decl
, real_decl
);
35098 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35101 this_pre_body
= pop_stmt_list (this_pre_body
);
35105 pre_body
= push_stmt_list ();
35107 add_stmt (this_pre_body
);
35108 pre_body
= pop_stmt_list (pre_body
);
35111 pre_body
= this_pre_body
;
35116 if (cclauses
!= NULL
35117 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35118 && real_decl
!= NULL_TREE
)
35121 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35122 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35123 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35125 error_at (loc
, "iteration variable %qD"
35126 " should not be firstprivate", real_decl
);
35127 *c
= OMP_CLAUSE_CHAIN (*c
);
35129 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35130 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35132 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35134 *c
= OMP_CLAUSE_CHAIN (*c
);
35135 if (code
== OMP_SIMD
)
35137 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35138 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35142 OMP_CLAUSE_CHAIN (l
) = clauses
;
35145 add_private_clause
= NULL_TREE
;
35149 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35150 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35151 add_private_clause
= NULL_TREE
;
35152 c
= &OMP_CLAUSE_CHAIN (*c
);
35156 if (add_private_clause
)
35159 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35161 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35162 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35163 && OMP_CLAUSE_DECL (c
) == decl
)
35165 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35166 && OMP_CLAUSE_DECL (c
) == decl
)
35167 error_at (loc
, "iteration variable %qD "
35168 "should not be firstprivate",
35170 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35171 && OMP_CLAUSE_DECL (c
) == decl
)
35172 error_at (loc
, "iteration variable %qD should not be reduction",
35177 if (code
!= OMP_SIMD
)
35178 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35179 else if (collapse
== 1)
35180 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35182 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35183 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35184 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35187 OMP_CLAUSE_CHAIN (c
) = clauses
;
35189 /* For linear, signal that we need to fill up
35190 the so far unknown linear step. */
35191 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35192 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35198 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35199 cond
= cp_parser_omp_for_cond (parser
, decl
);
35200 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35203 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35205 /* If decl is an iterator, preserve the operator on decl
35206 until finish_omp_for. */
35208 && ((processing_template_decl
35209 && (TREE_TYPE (real_decl
) == NULL_TREE
35210 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35211 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35212 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35214 incr
= cp_parser_expression (parser
);
35215 if (!EXPR_HAS_LOCATION (incr
))
35216 protected_set_expr_location (incr
, input_location
);
35219 if (!parens
.require_close (parser
))
35220 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35221 /*or_comma=*/false,
35222 /*consume_paren=*/true);
35224 TREE_VEC_ELT (declv
, i
) = decl
;
35225 TREE_VEC_ELT (initv
, i
) = init
;
35226 TREE_VEC_ELT (condv
, i
) = cond
;
35227 TREE_VEC_ELT (incrv
, i
) = incr
;
35230 orig_inits
.safe_grow_cleared (i
+ 1);
35231 orig_inits
[i
] = orig_init
;
35234 if (i
== count
- 1)
35237 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35238 in between the collapsed for loops to be still considered perfectly
35239 nested. Hopefully the final version clarifies this.
35240 For now handle (multiple) {'s and empty statements. */
35241 cp_parser_parse_tentatively (parser
);
35244 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35246 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35248 cp_lexer_consume_token (parser
->lexer
);
35251 else if (bracecount
35252 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35253 cp_lexer_consume_token (parser
->lexer
);
35256 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35257 error_at (loc
, "not enough for loops to collapse");
35258 collapse_err
= true;
35259 cp_parser_abort_tentative_parse (parser
);
35267 cp_parser_parse_definitely (parser
);
35268 nbraces
+= bracecount
;
35275 /* Note that we saved the original contents of this flag when we entered
35276 the structured block, and so we don't need to re-save it here. */
35277 parser
->in_statement
= IN_OMP_FOR
;
35279 /* Note that the grammar doesn't call for a structured block here,
35280 though the loop as a whole is a structured block. */
35281 body
= push_stmt_list ();
35282 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35283 body
= pop_stmt_list (body
);
35285 if (declv
== NULL_TREE
)
35288 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35289 body
, pre_body
, &orig_inits
, clauses
);
35293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35295 cp_lexer_consume_token (parser
->lexer
);
35298 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35299 cp_lexer_consume_token (parser
->lexer
);
35304 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35305 "collapsed loops not perfectly nested");
35307 collapse_err
= true;
35308 cp_parser_statement_seq_opt (parser
, NULL
);
35309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35314 while (!for_block
->is_empty ())
35316 tree t
= for_block
->pop ();
35317 if (TREE_CODE (t
) == STATEMENT_LIST
)
35318 add_stmt (pop_stmt_list (t
));
35322 release_tree_vector (for_block
);
35327 /* Helper function for OpenMP parsing, split clauses and call
35328 finish_omp_clauses on each of the set of clauses afterwards. */
35331 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35332 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35335 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35336 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35338 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35342 #pragma omp simd simd-clause[optseq] new-line
35345 #define OMP_SIMD_CLAUSE_MASK \
35346 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35356 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35357 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35360 tree clauses
, sb
, ret
;
35362 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35364 strcat (p_name
, " simd");
35365 mask
|= OMP_SIMD_CLAUSE_MASK
;
35367 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35371 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35372 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35373 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35374 OMP_CLAUSE_ORDERED
);
35375 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35377 error_at (OMP_CLAUSE_LOCATION (c
),
35378 "%<ordered%> clause with parameter may not be specified "
35379 "on %qs construct", p_name
);
35380 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35384 sb
= begin_omp_structured_block ();
35385 save
= cp_parser_begin_omp_structured_block (parser
);
35387 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35389 cp_parser_end_omp_structured_block (parser
, save
);
35390 add_stmt (finish_omp_structured_block (sb
));
35396 #pragma omp for for-clause[optseq] new-line
35400 #pragma omp for simd for-simd-clause[optseq] new-line
35403 #define OMP_FOR_CLAUSE_MASK \
35404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35415 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35416 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35419 tree clauses
, sb
, ret
;
35421 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35423 strcat (p_name
, " for");
35424 mask
|= OMP_FOR_CLAUSE_MASK
;
35425 /* parallel for{, simd} disallows nowait clause, but for
35426 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35427 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35428 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35429 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35430 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35431 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35435 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35436 const char *p
= IDENTIFIER_POINTER (id
);
35438 if (strcmp (p
, "simd") == 0)
35440 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35441 if (cclauses
== NULL
)
35442 cclauses
= cclauses_buf
;
35444 cp_lexer_consume_token (parser
->lexer
);
35445 if (!flag_openmp
) /* flag_openmp_simd */
35446 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35448 sb
= begin_omp_structured_block ();
35449 save
= cp_parser_begin_omp_structured_block (parser
);
35450 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35452 cp_parser_end_omp_structured_block (parser
, save
);
35453 tree body
= finish_omp_structured_block (sb
);
35456 ret
= make_node (OMP_FOR
);
35457 TREE_TYPE (ret
) = void_type_node
;
35458 OMP_FOR_BODY (ret
) = body
;
35459 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35460 SET_EXPR_LOCATION (ret
, loc
);
35465 if (!flag_openmp
) /* flag_openmp_simd */
35467 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35471 /* Composite distribute parallel for disallows linear clause. */
35472 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35473 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35475 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35479 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35480 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35483 sb
= begin_omp_structured_block ();
35484 save
= cp_parser_begin_omp_structured_block (parser
);
35486 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35488 cp_parser_end_omp_structured_block (parser
, save
);
35489 add_stmt (finish_omp_structured_block (sb
));
35495 # pragma omp master new-line
35496 structured-block */
35499 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35501 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35502 return c_finish_omp_master (input_location
,
35503 cp_parser_omp_structured_block (parser
, if_p
));
35507 # pragma omp ordered new-line
35511 # pragma omp ordered ordered-clauses new-line
35512 structured-block */
35514 #define OMP_ORDERED_CLAUSE_MASK \
35515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35518 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35519 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35522 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35523 enum pragma_context context
, bool *if_p
)
35525 location_t loc
= pragma_tok
->location
;
35527 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35529 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35530 const char *p
= IDENTIFIER_POINTER (id
);
35532 if (strcmp (p
, "depend") == 0)
35534 if (!flag_openmp
) /* flag_openmp_simd */
35536 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35539 if (context
== pragma_stmt
)
35541 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35542 "%<depend%> clause may only be used in compound "
35544 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35548 = cp_parser_omp_all_clauses (parser
,
35549 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35550 "#pragma omp ordered", pragma_tok
);
35551 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35557 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35558 "#pragma omp ordered", pragma_tok
);
35560 if (!flag_openmp
/* flag_openmp_simd */
35561 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35564 c_finish_omp_ordered (loc
, clauses
,
35565 cp_parser_omp_structured_block (parser
, if_p
));
35572 { section-sequence }
35575 section-directive[opt] structured-block
35576 section-sequence section-directive structured-block */
35579 cp_parser_omp_sections_scope (cp_parser
*parser
)
35581 tree stmt
, substmt
;
35582 bool error_suppress
= false;
35585 matching_braces braces
;
35586 if (!braces
.require_open (parser
))
35589 stmt
= push_stmt_list ();
35591 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35592 != PRAGMA_OMP_SECTION
)
35594 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35595 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35596 add_stmt (substmt
);
35601 tok
= cp_lexer_peek_token (parser
->lexer
);
35602 if (tok
->type
== CPP_CLOSE_BRACE
)
35604 if (tok
->type
== CPP_EOF
)
35607 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35609 cp_lexer_consume_token (parser
->lexer
);
35610 cp_parser_require_pragma_eol (parser
, tok
);
35611 error_suppress
= false;
35613 else if (!error_suppress
)
35615 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35616 error_suppress
= true;
35619 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35620 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35621 add_stmt (substmt
);
35623 braces
.require_close (parser
);
35625 substmt
= pop_stmt_list (stmt
);
35627 stmt
= make_node (OMP_SECTIONS
);
35628 TREE_TYPE (stmt
) = void_type_node
;
35629 OMP_SECTIONS_BODY (stmt
) = substmt
;
35636 # pragma omp sections sections-clause[optseq] newline
35639 #define OMP_SECTIONS_CLAUSE_MASK \
35640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35647 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35648 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35651 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35653 strcat (p_name
, " sections");
35654 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35656 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35658 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35662 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35663 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35666 ret
= cp_parser_omp_sections_scope (parser
);
35668 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35674 # pragma omp parallel parallel-clause[optseq] new-line
35676 # pragma omp parallel for parallel-for-clause[optseq] new-line
35678 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35682 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35683 structured-block */
35685 #define OMP_PARALLEL_CLAUSE_MASK \
35686 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35697 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35698 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35701 tree stmt
, clauses
, block
;
35703 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35705 strcat (p_name
, " parallel");
35706 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35707 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35708 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35709 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35710 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35712 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35714 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35715 if (cclauses
== NULL
)
35716 cclauses
= cclauses_buf
;
35718 cp_lexer_consume_token (parser
->lexer
);
35719 if (!flag_openmp
) /* flag_openmp_simd */
35720 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35722 block
= begin_omp_parallel ();
35723 save
= cp_parser_begin_omp_structured_block (parser
);
35724 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35726 cp_parser_end_omp_structured_block (parser
, save
);
35727 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35729 if (ret
== NULL_TREE
)
35731 OMP_PARALLEL_COMBINED (stmt
) = 1;
35734 /* When combined with distribute, parallel has to be followed by for.
35735 #pragma omp target parallel is allowed though. */
35737 && (mask
& (OMP_CLAUSE_MASK_1
35738 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35740 error_at (loc
, "expected %<for%> after %qs", p_name
);
35741 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35744 else if (!flag_openmp
) /* flag_openmp_simd */
35746 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35749 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35751 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35752 const char *p
= IDENTIFIER_POINTER (id
);
35753 if (strcmp (p
, "sections") == 0)
35755 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35756 cclauses
= cclauses_buf
;
35758 cp_lexer_consume_token (parser
->lexer
);
35759 block
= begin_omp_parallel ();
35760 save
= cp_parser_begin_omp_structured_block (parser
);
35761 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35762 cp_parser_end_omp_structured_block (parser
, save
);
35763 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35765 OMP_PARALLEL_COMBINED (stmt
) = 1;
35770 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35774 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35775 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35778 block
= begin_omp_parallel ();
35779 save
= cp_parser_begin_omp_structured_block (parser
);
35780 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35781 cp_parser_end_omp_structured_block (parser
, save
);
35782 stmt
= finish_omp_parallel (clauses
, block
);
35787 # pragma omp single single-clause[optseq] new-line
35788 structured-block */
35790 #define OMP_SINGLE_CLAUSE_MASK \
35791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35797 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35799 tree stmt
= make_node (OMP_SINGLE
);
35800 TREE_TYPE (stmt
) = void_type_node
;
35802 OMP_SINGLE_CLAUSES (stmt
)
35803 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35804 "#pragma omp single", pragma_tok
);
35805 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35807 return add_stmt (stmt
);
35811 # pragma omp task task-clause[optseq] new-line
35812 structured-block */
35814 #define OMP_TASK_CLAUSE_MASK \
35815 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35827 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35829 tree clauses
, block
;
35832 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35833 "#pragma omp task", pragma_tok
);
35834 block
= begin_omp_task ();
35835 save
= cp_parser_begin_omp_structured_block (parser
);
35836 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35837 cp_parser_end_omp_structured_block (parser
, save
);
35838 return finish_omp_task (clauses
, block
);
35842 # pragma omp taskwait new-line */
35845 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35847 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35848 finish_omp_taskwait ();
35852 # pragma omp taskyield new-line */
35855 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35857 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35858 finish_omp_taskyield ();
35862 # pragma omp taskgroup new-line
35863 structured-block */
35866 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35868 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35869 return c_finish_omp_taskgroup (input_location
,
35870 cp_parser_omp_structured_block (parser
,
35876 # pragma omp threadprivate (variable-list) */
35879 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35883 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35884 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35886 finish_omp_threadprivate (vars
);
35890 # pragma omp cancel cancel-clause[optseq] new-line */
35892 #define OMP_CANCEL_CLAUSE_MASK \
35893 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35900 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35902 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35903 "#pragma omp cancel", pragma_tok
);
35904 finish_omp_cancel (clauses
);
35908 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35910 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35911 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35917 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35918 enum pragma_context context
)
35921 bool point_seen
= false;
35923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35925 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35926 const char *p
= IDENTIFIER_POINTER (id
);
35928 if (strcmp (p
, "point") == 0)
35930 cp_lexer_consume_token (parser
->lexer
);
35936 cp_parser_error (parser
, "expected %<point%>");
35937 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35941 if (context
!= pragma_compound
)
35943 if (context
== pragma_stmt
)
35944 error_at (pragma_tok
->location
,
35945 "%<#pragma %s%> may only be used in compound statements",
35946 "omp cancellation point");
35948 cp_parser_error (parser
, "expected declaration specifiers");
35949 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35953 clauses
= cp_parser_omp_all_clauses (parser
,
35954 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35955 "#pragma omp cancellation point",
35957 finish_omp_cancellation_point (clauses
);
35961 #pragma omp distribute distribute-clause[optseq] new-line
35964 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35965 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35972 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35973 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35976 tree clauses
, sb
, ret
;
35978 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35980 strcat (p_name
, " distribute");
35981 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
35983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35985 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35986 const char *p
= IDENTIFIER_POINTER (id
);
35988 bool parallel
= false;
35990 if (strcmp (p
, "simd") == 0)
35993 parallel
= strcmp (p
, "parallel") == 0;
35994 if (parallel
|| simd
)
35996 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35997 if (cclauses
== NULL
)
35998 cclauses
= cclauses_buf
;
35999 cp_lexer_consume_token (parser
->lexer
);
36000 if (!flag_openmp
) /* flag_openmp_simd */
36003 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36006 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36009 sb
= begin_omp_structured_block ();
36010 save
= cp_parser_begin_omp_structured_block (parser
);
36012 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36015 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36017 cp_parser_end_omp_structured_block (parser
, save
);
36018 tree body
= finish_omp_structured_block (sb
);
36021 ret
= make_node (OMP_DISTRIBUTE
);
36022 TREE_TYPE (ret
) = void_type_node
;
36023 OMP_FOR_BODY (ret
) = body
;
36024 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36025 SET_EXPR_LOCATION (ret
, loc
);
36030 if (!flag_openmp
) /* flag_openmp_simd */
36032 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36036 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36040 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36041 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36044 sb
= begin_omp_structured_block ();
36045 save
= cp_parser_begin_omp_structured_block (parser
);
36047 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36049 cp_parser_end_omp_structured_block (parser
, save
);
36050 add_stmt (finish_omp_structured_block (sb
));
36056 # pragma omp teams teams-clause[optseq] new-line
36057 structured-block */
36059 #define OMP_TEAMS_CLAUSE_MASK \
36060 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36069 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36070 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36073 tree clauses
, sb
, ret
;
36075 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36077 strcat (p_name
, " teams");
36078 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36082 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36083 const char *p
= IDENTIFIER_POINTER (id
);
36084 if (strcmp (p
, "distribute") == 0)
36086 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36087 if (cclauses
== NULL
)
36088 cclauses
= cclauses_buf
;
36090 cp_lexer_consume_token (parser
->lexer
);
36091 if (!flag_openmp
) /* flag_openmp_simd */
36092 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36094 sb
= begin_omp_structured_block ();
36095 save
= cp_parser_begin_omp_structured_block (parser
);
36096 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36098 cp_parser_end_omp_structured_block (parser
, save
);
36099 tree body
= finish_omp_structured_block (sb
);
36102 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36103 ret
= make_node (OMP_TEAMS
);
36104 TREE_TYPE (ret
) = void_type_node
;
36105 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36106 OMP_TEAMS_BODY (ret
) = body
;
36107 OMP_TEAMS_COMBINED (ret
) = 1;
36108 SET_EXPR_LOCATION (ret
, loc
);
36109 return add_stmt (ret
);
36112 if (!flag_openmp
) /* flag_openmp_simd */
36114 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36118 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36122 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36123 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36126 tree stmt
= make_node (OMP_TEAMS
);
36127 TREE_TYPE (stmt
) = void_type_node
;
36128 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36129 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36130 SET_EXPR_LOCATION (stmt
, loc
);
36132 return add_stmt (stmt
);
36136 # pragma omp target data target-data-clause[optseq] new-line
36137 structured-block */
36139 #define OMP_TARGET_DATA_CLAUSE_MASK \
36140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36146 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36149 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36150 "#pragma omp target data", pragma_tok
);
36152 for (tree
*pc
= &clauses
; *pc
;)
36154 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36155 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36158 case GOMP_MAP_ALWAYS_TO
:
36159 case GOMP_MAP_FROM
:
36160 case GOMP_MAP_ALWAYS_FROM
:
36161 case GOMP_MAP_TOFROM
:
36162 case GOMP_MAP_ALWAYS_TOFROM
:
36163 case GOMP_MAP_ALLOC
:
36166 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36167 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36168 case GOMP_MAP_ALWAYS_POINTER
:
36172 error_at (OMP_CLAUSE_LOCATION (*pc
),
36173 "%<#pragma omp target data%> with map-type other "
36174 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36175 "on %<map%> clause");
36176 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36179 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36185 error_at (pragma_tok
->location
,
36186 "%<#pragma omp target data%> must contain at least "
36187 "one %<map%> clause");
36191 tree stmt
= make_node (OMP_TARGET_DATA
);
36192 TREE_TYPE (stmt
) = void_type_node
;
36193 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36195 keep_next_level (true);
36196 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36198 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36199 return add_stmt (stmt
);
36203 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36204 structured-block */
36206 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36214 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36215 enum pragma_context context
)
36217 bool data_seen
= false;
36218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36220 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36221 const char *p
= IDENTIFIER_POINTER (id
);
36223 if (strcmp (p
, "data") == 0)
36225 cp_lexer_consume_token (parser
->lexer
);
36231 cp_parser_error (parser
, "expected %<data%>");
36232 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36236 if (context
== pragma_stmt
)
36238 error_at (pragma_tok
->location
,
36239 "%<#pragma %s%> may only be used in compound statements",
36240 "omp target enter data");
36241 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36246 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36247 "#pragma omp target enter data", pragma_tok
);
36249 for (tree
*pc
= &clauses
; *pc
;)
36251 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36252 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36255 case GOMP_MAP_ALWAYS_TO
:
36256 case GOMP_MAP_ALLOC
:
36259 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36260 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36261 case GOMP_MAP_ALWAYS_POINTER
:
36265 error_at (OMP_CLAUSE_LOCATION (*pc
),
36266 "%<#pragma omp target enter data%> with map-type other "
36267 "than %<to%> or %<alloc%> on %<map%> clause");
36268 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36271 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36277 error_at (pragma_tok
->location
,
36278 "%<#pragma omp target enter data%> must contain at least "
36279 "one %<map%> clause");
36283 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36284 TREE_TYPE (stmt
) = void_type_node
;
36285 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36286 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36287 return add_stmt (stmt
);
36291 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36292 structured-block */
36294 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36295 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36302 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36303 enum pragma_context context
)
36305 bool data_seen
= false;
36306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36308 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36309 const char *p
= IDENTIFIER_POINTER (id
);
36311 if (strcmp (p
, "data") == 0)
36313 cp_lexer_consume_token (parser
->lexer
);
36319 cp_parser_error (parser
, "expected %<data%>");
36320 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36324 if (context
== pragma_stmt
)
36326 error_at (pragma_tok
->location
,
36327 "%<#pragma %s%> may only be used in compound statements",
36328 "omp target exit data");
36329 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36334 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36335 "#pragma omp target exit data", pragma_tok
);
36337 for (tree
*pc
= &clauses
; *pc
;)
36339 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36340 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36342 case GOMP_MAP_FROM
:
36343 case GOMP_MAP_ALWAYS_FROM
:
36344 case GOMP_MAP_RELEASE
:
36345 case GOMP_MAP_DELETE
:
36348 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36349 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36350 case GOMP_MAP_ALWAYS_POINTER
:
36354 error_at (OMP_CLAUSE_LOCATION (*pc
),
36355 "%<#pragma omp target exit data%> with map-type other "
36356 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36358 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36361 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36367 error_at (pragma_tok
->location
,
36368 "%<#pragma omp target exit data%> must contain at least "
36369 "one %<map%> clause");
36373 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36374 TREE_TYPE (stmt
) = void_type_node
;
36375 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36376 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36377 return add_stmt (stmt
);
36381 # pragma omp target update target-update-clause[optseq] new-line */
36383 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36384 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36392 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36393 enum pragma_context context
)
36395 if (context
== pragma_stmt
)
36397 error_at (pragma_tok
->location
,
36398 "%<#pragma %s%> may only be used in compound statements",
36399 "omp target update");
36400 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36405 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36406 "#pragma omp target update", pragma_tok
);
36407 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36408 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36410 error_at (pragma_tok
->location
,
36411 "%<#pragma omp target update%> must contain at least one "
36412 "%<from%> or %<to%> clauses");
36416 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36417 TREE_TYPE (stmt
) = void_type_node
;
36418 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36419 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36425 # pragma omp target target-clause[optseq] new-line
36426 structured-block */
36428 #define OMP_TARGET_CLAUSE_MASK \
36429 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36440 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36441 enum pragma_context context
, bool *if_p
)
36443 tree
*pc
= NULL
, stmt
;
36445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36447 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36448 const char *p
= IDENTIFIER_POINTER (id
);
36449 enum tree_code ccode
= ERROR_MARK
;
36451 if (strcmp (p
, "teams") == 0)
36453 else if (strcmp (p
, "parallel") == 0)
36454 ccode
= OMP_PARALLEL
;
36455 else if (strcmp (p
, "simd") == 0)
36457 if (ccode
!= ERROR_MARK
)
36459 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36460 char p_name
[sizeof ("#pragma omp target teams distribute "
36461 "parallel for simd")];
36463 cp_lexer_consume_token (parser
->lexer
);
36464 strcpy (p_name
, "#pragma omp target");
36465 if (!flag_openmp
) /* flag_openmp_simd */
36471 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36472 OMP_TARGET_CLAUSE_MASK
,
36476 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36477 OMP_TARGET_CLAUSE_MASK
,
36481 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36482 OMP_TARGET_CLAUSE_MASK
,
36486 gcc_unreachable ();
36488 return stmt
!= NULL_TREE
;
36490 keep_next_level (true);
36491 tree sb
= begin_omp_structured_block (), ret
;
36492 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36496 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36497 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36501 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36502 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36506 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36507 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36511 gcc_unreachable ();
36513 cp_parser_end_omp_structured_block (parser
, save
);
36514 tree body
= finish_omp_structured_block (sb
);
36515 if (ret
== NULL_TREE
)
36517 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36519 /* For combined target teams, ensure the num_teams and
36520 thread_limit clause expressions are evaluated on the host,
36521 before entering the target construct. */
36523 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36524 c
; c
= OMP_CLAUSE_CHAIN (c
))
36525 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36526 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36527 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36529 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36530 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36531 if (expr
== error_mark_node
)
36533 tree tmp
= TARGET_EXPR_SLOT (expr
);
36535 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36536 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36537 OMP_CLAUSE_FIRSTPRIVATE
);
36538 OMP_CLAUSE_DECL (tc
) = tmp
;
36539 OMP_CLAUSE_CHAIN (tc
)
36540 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36541 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36544 tree stmt
= make_node (OMP_TARGET
);
36545 TREE_TYPE (stmt
) = void_type_node
;
36546 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36547 OMP_TARGET_BODY (stmt
) = body
;
36548 OMP_TARGET_COMBINED (stmt
) = 1;
36549 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36551 pc
= &OMP_TARGET_CLAUSES (stmt
);
36552 goto check_clauses
;
36554 else if (!flag_openmp
) /* flag_openmp_simd */
36556 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36559 else if (strcmp (p
, "data") == 0)
36561 cp_lexer_consume_token (parser
->lexer
);
36562 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36565 else if (strcmp (p
, "enter") == 0)
36567 cp_lexer_consume_token (parser
->lexer
);
36568 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36571 else if (strcmp (p
, "exit") == 0)
36573 cp_lexer_consume_token (parser
->lexer
);
36574 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36577 else if (strcmp (p
, "update") == 0)
36579 cp_lexer_consume_token (parser
->lexer
);
36580 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36583 if (!flag_openmp
) /* flag_openmp_simd */
36585 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36589 stmt
= make_node (OMP_TARGET
);
36590 TREE_TYPE (stmt
) = void_type_node
;
36592 OMP_TARGET_CLAUSES (stmt
)
36593 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36594 "#pragma omp target", pragma_tok
);
36595 pc
= &OMP_TARGET_CLAUSES (stmt
);
36596 keep_next_level (true);
36597 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36599 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36605 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36606 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36609 case GOMP_MAP_ALWAYS_TO
:
36610 case GOMP_MAP_FROM
:
36611 case GOMP_MAP_ALWAYS_FROM
:
36612 case GOMP_MAP_TOFROM
:
36613 case GOMP_MAP_ALWAYS_TOFROM
:
36614 case GOMP_MAP_ALLOC
:
36615 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36616 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36617 case GOMP_MAP_ALWAYS_POINTER
:
36620 error_at (OMP_CLAUSE_LOCATION (*pc
),
36621 "%<#pragma omp target%> with map-type other "
36622 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36623 "on %<map%> clause");
36624 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36627 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36633 # pragma acc cache (variable-list) new-line
36637 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36639 tree stmt
, clauses
;
36641 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36642 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36644 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36646 stmt
= make_node (OACC_CACHE
);
36647 TREE_TYPE (stmt
) = void_type_node
;
36648 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36649 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36656 # pragma acc data oacc-data-clause[optseq] new-line
36657 structured-block */
36659 #define OACC_DATA_CLAUSE_MASK \
36660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36673 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36675 tree stmt
, clauses
, block
;
36678 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36679 "#pragma acc data", pragma_tok
);
36681 block
= begin_omp_parallel ();
36682 save
= cp_parser_begin_omp_structured_block (parser
);
36683 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36684 cp_parser_end_omp_structured_block (parser
, save
);
36685 stmt
= finish_oacc_data (clauses
, block
);
36690 # pragma acc host_data <clauses> new-line
36691 structured-block */
36693 #define OACC_HOST_DATA_CLAUSE_MASK \
36694 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36697 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36699 tree stmt
, clauses
, block
;
36702 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36703 "#pragma acc host_data", pragma_tok
);
36705 block
= begin_omp_parallel ();
36706 save
= cp_parser_begin_omp_structured_block (parser
);
36707 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36708 cp_parser_end_omp_structured_block (parser
, save
);
36709 stmt
= finish_oacc_host_data (clauses
, block
);
36714 # pragma acc declare oacc-data-clause[optseq] new-line
36717 #define OACC_DECLARE_CLAUSE_MASK \
36718 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36732 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36734 tree clauses
, stmt
;
36735 bool error
= false;
36737 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36738 "#pragma acc declare", pragma_tok
, true);
36741 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36743 error_at (pragma_tok
->location
,
36744 "no valid clauses specified in %<#pragma acc declare%>");
36748 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36750 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36751 tree decl
= OMP_CLAUSE_DECL (t
);
36752 if (!DECL_P (decl
))
36754 error_at (loc
, "array section in %<#pragma acc declare%>");
36758 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36759 switch (OMP_CLAUSE_MAP_KIND (t
))
36761 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36762 case GOMP_MAP_FORCE_ALLOC
:
36763 case GOMP_MAP_FORCE_TO
:
36764 case GOMP_MAP_FORCE_DEVICEPTR
:
36765 case GOMP_MAP_DEVICE_RESIDENT
:
36768 case GOMP_MAP_LINK
:
36769 if (!global_bindings_p ()
36770 && (TREE_STATIC (decl
)
36771 || !DECL_EXTERNAL (decl
)))
36774 "%qD must be a global variable in "
36775 "%<#pragma acc declare link%>",
36783 if (global_bindings_p ())
36785 error_at (loc
, "invalid OpenACC clause at file scope");
36789 if (DECL_EXTERNAL (decl
))
36792 "invalid use of %<extern%> variable %qD "
36793 "in %<#pragma acc declare%>", decl
);
36797 else if (TREE_PUBLIC (decl
))
36800 "invalid use of %<global%> variable %qD "
36801 "in %<#pragma acc declare%>", decl
);
36808 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36809 || lookup_attribute ("omp declare target link",
36810 DECL_ATTRIBUTES (decl
)))
36812 error_at (loc
, "variable %qD used more than once with "
36813 "%<#pragma acc declare%>", decl
);
36822 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36823 id
= get_identifier ("omp declare target link");
36825 id
= get_identifier ("omp declare target");
36827 DECL_ATTRIBUTES (decl
)
36828 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36829 if (global_bindings_p ())
36831 symtab_node
*node
= symtab_node::get (decl
);
36834 node
->offloadable
= 1;
36835 if (ENABLE_OFFLOADING
)
36837 g
->have_offload
= true;
36838 if (is_a
<varpool_node
*> (node
))
36839 vec_safe_push (offload_vars
, decl
);
36846 if (error
|| global_bindings_p ())
36849 stmt
= make_node (OACC_DECLARE
);
36850 TREE_TYPE (stmt
) = void_type_node
;
36851 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36852 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36860 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36864 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36866 LOC is the location of the #pragma token.
36869 #define OACC_ENTER_DATA_CLAUSE_MASK \
36870 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36878 #define OACC_EXIT_DATA_CLAUSE_MASK \
36879 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36886 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36889 location_t loc
= pragma_tok
->location
;
36890 tree stmt
, clauses
;
36891 const char *p
= "";
36893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36894 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36896 if (strcmp (p
, "data") != 0)
36898 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36899 enter
? "enter" : "exit");
36900 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36904 cp_lexer_consume_token (parser
->lexer
);
36907 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36908 "#pragma acc enter data", pragma_tok
);
36910 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36911 "#pragma acc exit data", pragma_tok
);
36913 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36915 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36916 enter
? "enter" : "exit");
36920 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36921 TREE_TYPE (stmt
) = void_type_node
;
36922 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36923 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36929 # pragma acc loop oacc-loop-clause[optseq] new-line
36930 structured-block */
36932 #define OACC_LOOP_CLAUSE_MASK \
36933 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36945 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36946 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36948 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36950 strcat (p_name
, " loop");
36951 mask
|= OACC_LOOP_CLAUSE_MASK
;
36953 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36957 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36959 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36961 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36964 tree block
= begin_omp_structured_block ();
36965 int save
= cp_parser_begin_omp_structured_block (parser
);
36966 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36967 cp_parser_end_omp_structured_block (parser
, save
);
36968 add_stmt (finish_omp_structured_block (block
));
36974 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36979 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36983 #define OACC_KERNELS_CLAUSE_MASK \
36984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37002 #define OACC_PARALLEL_CLAUSE_MASK \
37003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37025 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37026 char *p_name
, bool *if_p
)
37028 omp_clause_mask mask
;
37029 enum tree_code code
;
37030 switch (cp_parser_pragma_kind (pragma_tok
))
37032 case PRAGMA_OACC_KERNELS
:
37033 strcat (p_name
, " kernels");
37034 mask
= OACC_KERNELS_CLAUSE_MASK
;
37035 code
= OACC_KERNELS
;
37037 case PRAGMA_OACC_PARALLEL
:
37038 strcat (p_name
, " parallel");
37039 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37040 code
= OACC_PARALLEL
;
37043 gcc_unreachable ();
37046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37049 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37050 if (strcmp (p
, "loop") == 0)
37052 cp_lexer_consume_token (parser
->lexer
);
37053 tree block
= begin_omp_parallel ();
37055 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37057 return finish_omp_construct (code
, block
, clauses
);
37061 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37063 tree block
= begin_omp_parallel ();
37064 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37065 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37066 cp_parser_end_omp_structured_block (parser
, save
);
37067 return finish_omp_construct (code
, block
, clauses
);
37071 # pragma acc update oacc-update-clause[optseq] new-line
37074 #define OACC_UPDATE_CLAUSE_MASK \
37075 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37083 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37085 tree stmt
, clauses
;
37087 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37088 "#pragma acc update", pragma_tok
);
37090 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37092 error_at (pragma_tok
->location
,
37093 "%<#pragma acc update%> must contain at least one "
37094 "%<device%> or %<host%> or %<self%> clause");
37098 stmt
= make_node (OACC_UPDATE
);
37099 TREE_TYPE (stmt
) = void_type_node
;
37100 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37101 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37107 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37109 LOC is the location of the #pragma token.
37112 #define OACC_WAIT_CLAUSE_MASK \
37113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37116 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37118 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37119 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37121 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37122 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37124 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37125 "#pragma acc wait", pragma_tok
);
37127 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37128 stmt
= finish_expr_stmt (stmt
);
37134 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37136 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37145 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37146 enum pragma_context context
)
37148 bool first_p
= parser
->omp_declare_simd
== NULL
;
37149 cp_omp_declare_simd_data data
;
37152 data
.error_seen
= false;
37153 data
.fndecl_seen
= false;
37154 data
.tokens
= vNULL
;
37155 data
.clauses
= NULL_TREE
;
37156 /* It is safe to take the address of a local variable; it will only be
37157 used while this scope is live. */
37158 parser
->omp_declare_simd
= &data
;
37161 /* Store away all pragma tokens. */
37162 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37163 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37164 cp_lexer_consume_token (parser
->lexer
);
37165 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37166 parser
->omp_declare_simd
->error_seen
= true;
37167 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37168 struct cp_token_cache
*cp
37169 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37170 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37174 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37175 cp_parser_pragma (parser
, context
, NULL
);
37178 case pragma_external
:
37179 cp_parser_declaration (parser
);
37181 case pragma_member
:
37182 cp_parser_member_declaration (parser
);
37184 case pragma_objc_icode
:
37185 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37188 cp_parser_declaration_statement (parser
);
37191 if (parser
->omp_declare_simd
37192 && !parser
->omp_declare_simd
->error_seen
37193 && !parser
->omp_declare_simd
->fndecl_seen
)
37194 error_at (pragma_tok
->location
,
37195 "%<#pragma omp declare simd%> not immediately followed by "
37196 "function declaration or definition");
37197 data
.tokens
.release ();
37198 parser
->omp_declare_simd
= NULL
;
37202 /* Finalize #pragma omp declare simd clauses after direct declarator has
37203 been parsed, and put that into "omp declare simd" attribute. */
37206 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37208 struct cp_token_cache
*ce
;
37209 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37212 if (!data
->error_seen
&& data
->fndecl_seen
)
37214 error ("%<#pragma omp declare simd%> not immediately followed by "
37215 "a single function declaration or definition");
37216 data
->error_seen
= true;
37218 if (data
->error_seen
)
37221 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37225 cp_parser_push_lexer_for_tokens (parser
, ce
);
37226 parser
->lexer
->in_pragma
= true;
37227 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37228 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37229 cp_lexer_consume_token (parser
->lexer
);
37230 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37231 "#pragma omp declare simd", pragma_tok
);
37232 cp_parser_pop_lexer (parser
);
37234 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37235 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37236 TREE_CHAIN (c
) = attrs
;
37237 if (processing_template_decl
)
37238 ATTR_IS_DEPENDENT (c
) = 1;
37242 data
->fndecl_seen
= true;
37248 # pragma omp declare target new-line
37249 declarations and definitions
37250 # pragma omp end declare target new-line
37253 # pragma omp declare target ( extended-list ) new-line
37255 # pragma omp declare target declare-target-clauses[seq] new-line */
37257 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37258 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37262 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37264 tree clauses
= NULL_TREE
;
37265 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37267 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37268 "#pragma omp declare target", pragma_tok
);
37269 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37271 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37273 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37274 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37278 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37279 scope_chain
->omp_declare_target_attribute
++;
37282 if (scope_chain
->omp_declare_target_attribute
)
37283 error_at (pragma_tok
->location
,
37284 "%<#pragma omp declare target%> with clauses in between "
37285 "%<#pragma omp declare target%> without clauses and "
37286 "%<#pragma omp end declare target%>");
37287 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37289 tree t
= OMP_CLAUSE_DECL (c
), id
;
37290 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37291 tree at2
= lookup_attribute ("omp declare target link",
37292 DECL_ATTRIBUTES (t
));
37293 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37295 id
= get_identifier ("omp declare target link");
37296 std::swap (at1
, at2
);
37299 id
= get_identifier ("omp declare target");
37302 error_at (OMP_CLAUSE_LOCATION (c
),
37303 "%qD specified both in declare target %<link%> and %<to%>"
37309 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37310 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37313 symtab_node
*node
= symtab_node::get (t
);
37316 node
->offloadable
= 1;
37317 if (ENABLE_OFFLOADING
)
37319 g
->have_offload
= true;
37320 if (is_a
<varpool_node
*> (node
))
37321 vec_safe_push (offload_vars
, t
);
37329 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37331 const char *p
= "";
37332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37334 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37335 p
= IDENTIFIER_POINTER (id
);
37337 if (strcmp (p
, "declare") == 0)
37339 cp_lexer_consume_token (parser
->lexer
);
37341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37343 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37344 p
= IDENTIFIER_POINTER (id
);
37346 if (strcmp (p
, "target") == 0)
37347 cp_lexer_consume_token (parser
->lexer
);
37350 cp_parser_error (parser
, "expected %<target%>");
37351 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37357 cp_parser_error (parser
, "expected %<declare%>");
37358 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37361 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37362 if (!scope_chain
->omp_declare_target_attribute
)
37363 error_at (pragma_tok
->location
,
37364 "%<#pragma omp end declare target%> without corresponding "
37365 "%<#pragma omp declare target%>");
37367 scope_chain
->omp_declare_target_attribute
--;
37370 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37371 expression and optional initializer clause of
37372 #pragma omp declare reduction. We store the expression(s) as
37373 either 3, 6 or 7 special statements inside of the artificial function's
37374 body. The first two statements are DECL_EXPRs for the artificial
37375 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37376 expression that uses those variables.
37377 If there was any INITIALIZER clause, this is followed by further statements,
37378 the fourth and fifth statements are DECL_EXPRs for the artificial
37379 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37380 constructor variant (first token after open paren is not omp_priv),
37381 then the sixth statement is a statement with the function call expression
37382 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37383 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37384 to initialize the OMP_PRIV artificial variable and there is seventh
37385 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37388 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37390 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37391 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37392 type
= TREE_TYPE (type
);
37393 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37394 DECL_ARTIFICIAL (omp_out
) = 1;
37395 pushdecl (omp_out
);
37396 add_decl_expr (omp_out
);
37397 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37398 DECL_ARTIFICIAL (omp_in
) = 1;
37400 add_decl_expr (omp_in
);
37402 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37404 keep_next_level (true);
37405 tree block
= begin_omp_structured_block ();
37406 combiner
= cp_parser_expression (parser
);
37407 finish_expr_stmt (combiner
);
37408 block
= finish_omp_structured_block (block
);
37411 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37414 const char *p
= "";
37415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37417 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37418 p
= IDENTIFIER_POINTER (id
);
37421 if (strcmp (p
, "initializer") == 0)
37423 cp_lexer_consume_token (parser
->lexer
);
37424 matching_parens parens
;
37425 if (!parens
.require_open (parser
))
37429 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37431 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37432 p
= IDENTIFIER_POINTER (id
);
37435 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37436 DECL_ARTIFICIAL (omp_priv
) = 1;
37437 pushdecl (omp_priv
);
37438 add_decl_expr (omp_priv
);
37439 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37440 DECL_ARTIFICIAL (omp_orig
) = 1;
37441 pushdecl (omp_orig
);
37442 add_decl_expr (omp_orig
);
37444 keep_next_level (true);
37445 block
= begin_omp_structured_block ();
37448 if (strcmp (p
, "omp_priv") == 0)
37450 bool is_direct_init
, is_non_constant_init
;
37452 cp_lexer_consume_token (parser
->lexer
);
37453 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37455 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37456 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37458 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37459 == CPP_CLOSE_PAREN
))
37461 finish_omp_structured_block (block
);
37462 error ("invalid initializer clause");
37465 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37466 &is_non_constant_init
);
37467 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37468 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37472 cp_parser_parse_tentatively (parser
);
37473 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37474 /*check_dependency_p=*/true,
37475 /*template_p=*/NULL
,
37476 /*declarator_p=*/false,
37477 /*optional_p=*/false);
37478 vec
<tree
, va_gc
> *args
;
37479 if (fn_name
== error_mark_node
37480 || cp_parser_error_occurred (parser
)
37481 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37482 || ((args
= cp_parser_parenthesized_expression_list
37483 (parser
, non_attr
, /*cast_p=*/false,
37484 /*allow_expansion_p=*/true,
37485 /*non_constant_p=*/NULL
)),
37486 cp_parser_error_occurred (parser
)))
37488 finish_omp_structured_block (block
);
37489 cp_parser_abort_tentative_parse (parser
);
37490 cp_parser_error (parser
, "expected id-expression (arguments)");
37495 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37496 if (arg
== omp_priv
37497 || (TREE_CODE (arg
) == ADDR_EXPR
37498 && TREE_OPERAND (arg
, 0) == omp_priv
))
37500 cp_parser_abort_tentative_parse (parser
);
37501 if (arg
== NULL_TREE
)
37502 error ("one of the initializer call arguments should be %<omp_priv%>"
37503 " or %<&omp_priv%>");
37504 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37506 finish_expr_stmt (initializer
);
37509 block
= finish_omp_structured_block (block
);
37510 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37514 add_decl_expr (omp_orig
);
37516 if (!parens
.require_close (parser
))
37520 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37521 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37528 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37529 initializer-clause[opt] new-line
37531 initializer-clause:
37532 initializer (omp_priv initializer)
37533 initializer (function-name (argument-list)) */
37536 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37537 enum pragma_context
)
37539 auto_vec
<tree
> types
;
37540 enum tree_code reduc_code
= ERROR_MARK
;
37541 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37543 cp_token
*first_token
;
37544 cp_token_cache
*cp
;
37548 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37549 p
= obstack_alloc (&declarator_obstack
, 0);
37551 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37554 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37557 reduc_code
= PLUS_EXPR
;
37560 reduc_code
= MULT_EXPR
;
37563 reduc_code
= MINUS_EXPR
;
37566 reduc_code
= BIT_AND_EXPR
;
37569 reduc_code
= BIT_XOR_EXPR
;
37572 reduc_code
= BIT_IOR_EXPR
;
37575 reduc_code
= TRUTH_ANDIF_EXPR
;
37578 reduc_code
= TRUTH_ORIF_EXPR
;
37581 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37584 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37585 "%<|%>, %<&&%>, %<||%> or identifier");
37589 if (reduc_code
!= ERROR_MARK
)
37590 cp_lexer_consume_token (parser
->lexer
);
37592 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37593 if (reduc_id
== error_mark_node
)
37596 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37599 /* Types may not be defined in declare reduction type list. */
37600 const char *saved_message
;
37601 saved_message
= parser
->type_definition_forbidden_message
;
37602 parser
->type_definition_forbidden_message
37603 = G_("types may not be defined in declare reduction type list");
37604 bool saved_colon_corrects_to_scope_p
;
37605 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37606 parser
->colon_corrects_to_scope_p
= false;
37607 bool saved_colon_doesnt_start_class_def_p
;
37608 saved_colon_doesnt_start_class_def_p
37609 = parser
->colon_doesnt_start_class_def_p
;
37610 parser
->colon_doesnt_start_class_def_p
= true;
37614 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37615 type
= cp_parser_type_id (parser
);
37616 if (type
== error_mark_node
)
37618 else if (ARITHMETIC_TYPE_P (type
)
37619 && (orig_reduc_id
== NULL_TREE
37620 || (TREE_CODE (type
) != COMPLEX_TYPE
37621 && (id_equal (orig_reduc_id
, "min")
37622 || id_equal (orig_reduc_id
, "max")))))
37623 error_at (loc
, "predeclared arithmetic type %qT in "
37624 "%<#pragma omp declare reduction%>", type
);
37625 else if (TREE_CODE (type
) == FUNCTION_TYPE
37626 || TREE_CODE (type
) == METHOD_TYPE
37627 || TREE_CODE (type
) == ARRAY_TYPE
)
37628 error_at (loc
, "function or array type %qT in "
37629 "%<#pragma omp declare reduction%>", type
);
37630 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37631 error_at (loc
, "reference type %qT in "
37632 "%<#pragma omp declare reduction%>", type
);
37633 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37634 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37635 "%<#pragma omp declare reduction%>", type
);
37637 types
.safe_push (type
);
37639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37640 cp_lexer_consume_token (parser
->lexer
);
37645 /* Restore the saved message. */
37646 parser
->type_definition_forbidden_message
= saved_message
;
37647 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37648 parser
->colon_doesnt_start_class_def_p
37649 = saved_colon_doesnt_start_class_def_p
;
37651 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37652 || types
.is_empty ())
37655 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37659 first_token
= cp_lexer_peek_token (parser
->lexer
);
37662 FOR_EACH_VEC_ELT (types
, i
, type
)
37665 = build_function_type_list (void_type_node
,
37666 cp_build_reference_type (type
, false),
37668 tree this_reduc_id
= reduc_id
;
37669 if (!dependent_type_p (type
))
37670 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37671 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37672 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37673 DECL_ARTIFICIAL (fndecl
) = 1;
37674 DECL_EXTERNAL (fndecl
) = 1;
37675 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37676 DECL_IGNORED_P (fndecl
) = 1;
37677 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37678 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37679 DECL_ATTRIBUTES (fndecl
)
37680 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37681 DECL_ATTRIBUTES (fndecl
));
37682 if (processing_template_decl
)
37683 fndecl
= push_template_decl (fndecl
);
37684 bool block_scope
= false;
37685 tree block
= NULL_TREE
;
37686 if (current_function_decl
)
37688 block_scope
= true;
37689 DECL_CONTEXT (fndecl
) = global_namespace
;
37690 if (!processing_template_decl
)
37693 else if (current_class_type
)
37697 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37698 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37699 cp_lexer_consume_token (parser
->lexer
);
37700 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37702 cp
= cp_token_cache_new (first_token
,
37703 cp_lexer_peek_nth_token (parser
->lexer
,
37706 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37707 finish_member_declaration (fndecl
);
37708 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37709 DECL_PENDING_INLINE_P (fndecl
) = 1;
37710 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37715 DECL_CONTEXT (fndecl
) = current_namespace
;
37719 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37721 block
= begin_omp_structured_block ();
37724 cp_parser_push_lexer_for_tokens (parser
, cp
);
37725 parser
->lexer
->in_pragma
= true;
37727 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37730 finish_function (/*inline_p=*/false);
37732 DECL_CONTEXT (fndecl
) = current_function_decl
;
37734 cp_parser_pop_lexer (parser
);
37738 cp_parser_pop_lexer (parser
);
37740 finish_function (/*inline_p=*/false);
37743 DECL_CONTEXT (fndecl
) = current_function_decl
;
37744 block
= finish_omp_structured_block (block
);
37745 if (TREE_CODE (block
) == BIND_EXPR
)
37746 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37747 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37748 DECL_SAVED_TREE (fndecl
) = block
;
37749 if (processing_template_decl
)
37750 add_decl_expr (fndecl
);
37752 cp_check_omp_declare_reduction (fndecl
);
37753 if (cp
== NULL
&& types
.length () > 1)
37754 cp
= cp_token_cache_new (first_token
,
37755 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37756 if (errs
!= errorcount
)
37760 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37763 /* Free any declarators allocated. */
37764 obstack_free (&declarator_obstack
, p
);
37768 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37769 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37770 initializer-clause[opt] new-line
37771 #pragma omp declare target new-line */
37774 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37775 enum pragma_context context
)
37777 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37779 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37780 const char *p
= IDENTIFIER_POINTER (id
);
37782 if (strcmp (p
, "simd") == 0)
37784 cp_lexer_consume_token (parser
->lexer
);
37785 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37789 cp_ensure_no_omp_declare_simd (parser
);
37790 if (strcmp (p
, "reduction") == 0)
37792 cp_lexer_consume_token (parser
->lexer
);
37793 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37797 if (!flag_openmp
) /* flag_openmp_simd */
37799 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37802 if (strcmp (p
, "target") == 0)
37804 cp_lexer_consume_token (parser
->lexer
);
37805 cp_parser_omp_declare_target (parser
, pragma_tok
);
37809 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37811 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37816 #pragma omp taskloop taskloop-clause[optseq] new-line
37819 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37822 #define OMP_TASKLOOP_CLAUSE_MASK \
37823 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37839 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37840 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37843 tree clauses
, sb
, ret
;
37845 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37847 strcat (p_name
, " taskloop");
37848 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37850 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37852 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37853 const char *p
= IDENTIFIER_POINTER (id
);
37855 if (strcmp (p
, "simd") == 0)
37857 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37858 if (cclauses
== NULL
)
37859 cclauses
= cclauses_buf
;
37861 cp_lexer_consume_token (parser
->lexer
);
37862 if (!flag_openmp
) /* flag_openmp_simd */
37863 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37865 sb
= begin_omp_structured_block ();
37866 save
= cp_parser_begin_omp_structured_block (parser
);
37867 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37869 cp_parser_end_omp_structured_block (parser
, save
);
37870 tree body
= finish_omp_structured_block (sb
);
37873 ret
= make_node (OMP_TASKLOOP
);
37874 TREE_TYPE (ret
) = void_type_node
;
37875 OMP_FOR_BODY (ret
) = body
;
37876 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37877 SET_EXPR_LOCATION (ret
, loc
);
37882 if (!flag_openmp
) /* flag_openmp_simd */
37884 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37888 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37892 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37893 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37896 sb
= begin_omp_structured_block ();
37897 save
= cp_parser_begin_omp_structured_block (parser
);
37899 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37902 cp_parser_end_omp_structured_block (parser
, save
);
37903 add_stmt (finish_omp_structured_block (sb
));
37910 # pragma acc routine oacc-routine-clause[optseq] new-line
37911 function-definition
37913 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37916 #define OACC_ROUTINE_CLAUSE_MASK \
37917 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37923 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37924 component, which must resolve to a declared namespace-scope
37925 function. The clauses are either processed directly (for a named
37926 function), or defered until the immediatley following declaration
37930 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37931 enum pragma_context context
)
37933 gcc_checking_assert (context
== pragma_external
);
37934 /* The checking for "another pragma following this one" in the "no optional
37935 '( name )'" case makes sure that we dont re-enter. */
37936 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37938 cp_oacc_routine_data data
;
37939 data
.error_seen
= false;
37940 data
.fndecl_seen
= false;
37941 data
.tokens
= vNULL
;
37942 data
.clauses
= NULL_TREE
;
37943 data
.loc
= pragma_tok
->location
;
37944 /* It is safe to take the address of a local variable; it will only be
37945 used while this scope is live. */
37946 parser
->oacc_routine
= &data
;
37948 /* Look for optional '( name )'. */
37949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37951 matching_parens parens
;
37952 parens
.consume_open (parser
); /* '(' */
37954 /* We parse the name as an id-expression. If it resolves to
37955 anything other than a non-overloaded function at namespace
37956 scope, it's an error. */
37957 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37958 tree name
= cp_parser_id_expression (parser
,
37959 /*template_keyword_p=*/false,
37960 /*check_dependency_p=*/false,
37961 /*template_p=*/NULL
,
37962 /*declarator_p=*/false,
37963 /*optional_p=*/false);
37964 tree decl
= (identifier_p (name
)
37965 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
37967 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37968 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37970 if (decl
== error_mark_node
37971 || !parens
.require_close (parser
))
37973 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37974 parser
->oacc_routine
= NULL
;
37979 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37980 "#pragma acc routine",
37981 cp_lexer_peek_token (parser
->lexer
));
37983 if (decl
&& is_overloaded_fn (decl
)
37984 && (TREE_CODE (decl
) != FUNCTION_DECL
37985 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37987 error_at (name_loc
,
37988 "%<#pragma acc routine%> names a set of overloads");
37989 parser
->oacc_routine
= NULL
;
37993 /* Perhaps we should use the same rule as declarations in different
37995 if (!DECL_NAMESPACE_SCOPE_P (decl
))
37997 error_at (name_loc
,
37998 "%qD does not refer to a namespace scope function", decl
);
37999 parser
->oacc_routine
= NULL
;
38003 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38005 error_at (name_loc
, "%qD does not refer to a function", decl
);
38006 parser
->oacc_routine
= NULL
;
38010 cp_finalize_oacc_routine (parser
, decl
, false);
38011 parser
->oacc_routine
= NULL
;
38013 else /* No optional '( name )'. */
38015 /* Store away all pragma tokens. */
38016 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38017 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38018 cp_lexer_consume_token (parser
->lexer
);
38019 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38020 parser
->oacc_routine
->error_seen
= true;
38021 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38022 struct cp_token_cache
*cp
38023 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38024 parser
->oacc_routine
->tokens
.safe_push (cp
);
38026 /* Emit a helpful diagnostic if there's another pragma following this
38028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38030 cp_ensure_no_oacc_routine (parser
);
38031 data
.tokens
.release ();
38032 /* ..., and then just keep going. */
38036 /* We only have to consider the pragma_external case here. */
38037 cp_parser_declaration (parser
);
38038 if (parser
->oacc_routine
38039 && !parser
->oacc_routine
->fndecl_seen
)
38040 cp_ensure_no_oacc_routine (parser
);
38042 parser
->oacc_routine
= NULL
;
38043 data
.tokens
.release ();
38047 /* Finalize #pragma acc routine clauses after direct declarator has
38051 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38053 struct cp_token_cache
*ce
;
38054 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38056 if (!data
->error_seen
&& data
->fndecl_seen
)
38058 error_at (data
->loc
,
38059 "%<#pragma acc routine%> not immediately followed by "
38060 "a single function declaration or definition");
38061 data
->error_seen
= true;
38063 if (data
->error_seen
)
38066 gcc_checking_assert (data
->tokens
.length () == 1);
38067 ce
= data
->tokens
[0];
38069 cp_parser_push_lexer_for_tokens (parser
, ce
);
38070 parser
->lexer
->in_pragma
= true;
38071 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38073 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38074 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38075 parser
->oacc_routine
->clauses
38076 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38077 "#pragma acc routine", pragma_tok
);
38078 cp_parser_pop_lexer (parser
);
38079 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38085 /* Apply any saved OpenACC routine clauses to a just-parsed
38089 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38091 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38093 /* Keep going if we're in error reporting mode. */
38094 if (parser
->oacc_routine
->error_seen
38095 || fndecl
== error_mark_node
)
38098 if (parser
->oacc_routine
->fndecl_seen
)
38100 error_at (parser
->oacc_routine
->loc
,
38101 "%<#pragma acc routine%> not immediately followed by"
38102 " a single function declaration or definition");
38103 parser
->oacc_routine
= NULL
;
38106 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38108 cp_ensure_no_oacc_routine (parser
);
38112 if (oacc_get_fn_attrib (fndecl
))
38114 error_at (parser
->oacc_routine
->loc
,
38115 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38116 parser
->oacc_routine
= NULL
;
38120 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38122 error_at (parser
->oacc_routine
->loc
,
38124 ? G_("%<#pragma acc routine%> must be applied before use")
38125 : G_("%<#pragma acc routine%> must be applied before "
38127 parser
->oacc_routine
= NULL
;
38131 /* Process the routine's dimension clauses. */
38132 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38133 oacc_replace_fn_attrib (fndecl
, dims
);
38135 /* Add an "omp declare target" attribute. */
38136 DECL_ATTRIBUTES (fndecl
)
38137 = tree_cons (get_identifier ("omp declare target"),
38138 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38140 /* Don't unset parser->oacc_routine here: we may still need it to
38141 diagnose wrong usage. But, remember that we've used this "#pragma acc
38143 parser
->oacc_routine
->fndecl_seen
= true;
38147 /* Main entry point to OpenMP statement pragmas. */
38150 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38153 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38154 omp_clause_mask
mask (0);
38156 switch (cp_parser_pragma_kind (pragma_tok
))
38158 case PRAGMA_OACC_ATOMIC
:
38159 cp_parser_omp_atomic (parser
, pragma_tok
);
38161 case PRAGMA_OACC_CACHE
:
38162 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38164 case PRAGMA_OACC_DATA
:
38165 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38167 case PRAGMA_OACC_ENTER_DATA
:
38168 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38170 case PRAGMA_OACC_EXIT_DATA
:
38171 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38173 case PRAGMA_OACC_HOST_DATA
:
38174 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38176 case PRAGMA_OACC_KERNELS
:
38177 case PRAGMA_OACC_PARALLEL
:
38178 strcpy (p_name
, "#pragma acc");
38179 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38182 case PRAGMA_OACC_LOOP
:
38183 strcpy (p_name
, "#pragma acc");
38184 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38187 case PRAGMA_OACC_UPDATE
:
38188 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38190 case PRAGMA_OACC_WAIT
:
38191 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38193 case PRAGMA_OMP_ATOMIC
:
38194 cp_parser_omp_atomic (parser
, pragma_tok
);
38196 case PRAGMA_OMP_CRITICAL
:
38197 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38199 case PRAGMA_OMP_DISTRIBUTE
:
38200 strcpy (p_name
, "#pragma omp");
38201 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38204 case PRAGMA_OMP_FOR
:
38205 strcpy (p_name
, "#pragma omp");
38206 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38209 case PRAGMA_OMP_MASTER
:
38210 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38212 case PRAGMA_OMP_PARALLEL
:
38213 strcpy (p_name
, "#pragma omp");
38214 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38217 case PRAGMA_OMP_SECTIONS
:
38218 strcpy (p_name
, "#pragma omp");
38219 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38221 case PRAGMA_OMP_SIMD
:
38222 strcpy (p_name
, "#pragma omp");
38223 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38226 case PRAGMA_OMP_SINGLE
:
38227 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38229 case PRAGMA_OMP_TASK
:
38230 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38232 case PRAGMA_OMP_TASKGROUP
:
38233 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38235 case PRAGMA_OMP_TASKLOOP
:
38236 strcpy (p_name
, "#pragma omp");
38237 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38240 case PRAGMA_OMP_TEAMS
:
38241 strcpy (p_name
, "#pragma omp");
38242 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38246 gcc_unreachable ();
38249 protected_set_expr_location (stmt
, pragma_tok
->location
);
38252 /* Transactional Memory parsing routines. */
38254 /* Parse a transaction attribute.
38260 We use this instead of cp_parser_attributes_opt for transactions to avoid
38261 the pedwarn in C++98 mode. */
38264 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38267 tree attr_name
, attr
= NULL
;
38269 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38270 return cp_parser_attributes_opt (parser
);
38272 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38274 cp_lexer_consume_token (parser
->lexer
);
38275 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38278 token
= cp_lexer_peek_token (parser
->lexer
);
38279 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38281 token
= cp_lexer_consume_token (parser
->lexer
);
38283 attr_name
= (token
->type
== CPP_KEYWORD
38284 /* For keywords, use the canonical spelling,
38285 not the parsed identifier. */
38286 ? ridpointers
[(int) token
->keyword
]
38288 attr
= build_tree_list (attr_name
, NULL_TREE
);
38291 cp_parser_error (parser
, "expected identifier");
38293 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38295 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38299 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38301 transaction-statement:
38302 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38304 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38308 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38310 unsigned char old_in
= parser
->in_transaction
;
38311 unsigned char this_in
= 1, new_in
;
38312 enum rid keyword
= token
->keyword
;
38313 tree stmt
, attrs
, noex
;
38315 cp_lexer_consume_token (parser
->lexer
);
38317 if (keyword
== RID_TRANSACTION_RELAXED
38318 || keyword
== RID_SYNCHRONIZED
)
38319 this_in
|= TM_STMT_ATTR_RELAXED
;
38322 attrs
= cp_parser_txn_attribute_opt (parser
);
38324 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38327 /* Parse a noexcept specification. */
38328 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38329 noex
= boolean_true_node
;
38330 else if (keyword
== RID_ATOMIC_CANCEL
)
38332 /* cancel-and-throw is unimplemented. */
38333 sorry ("atomic_cancel");
38337 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38339 /* Keep track if we're in the lexical scope of an outer transaction. */
38340 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38342 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38344 parser
->in_transaction
= new_in
;
38345 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38346 parser
->in_transaction
= old_in
;
38348 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38353 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38355 transaction-expression:
38356 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38357 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38361 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38363 unsigned char old_in
= parser
->in_transaction
;
38364 unsigned char this_in
= 1;
38368 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38370 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38371 || keyword
== RID_TRANSACTION_RELAXED
);
38375 keyword
== RID_TRANSACTION_RELAXED
38376 ? G_("%<__transaction_relaxed%> without transactional memory "
38378 : G_("%<__transaction_atomic%> without transactional memory "
38379 "support enabled"));
38381 token
= cp_parser_require_keyword (parser
, keyword
,
38382 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38383 : RT_TRANSACTION_RELAXED
));
38384 gcc_assert (token
!= NULL
);
38386 if (keyword
== RID_TRANSACTION_RELAXED
)
38387 this_in
|= TM_STMT_ATTR_RELAXED
;
38389 /* Set this early. This might mean that we allow transaction_cancel in
38390 an expression that we find out later actually has to be a constexpr.
38391 However, we expect that cxx_constant_value will be able to deal with
38392 this; also, if the noexcept has no constexpr, then what we parse next
38393 really is a transaction's body. */
38394 parser
->in_transaction
= this_in
;
38396 /* Parse a noexcept specification. */
38397 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38400 if (!noex
|| !noex_expr
38401 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38403 matching_parens parens
;
38404 parens
.require_open (parser
);
38406 expr
= cp_parser_expression (parser
);
38407 expr
= finish_parenthesized_expr (expr
);
38409 parens
.require_close (parser
);
38413 /* The only expression that is available got parsed for the noexcept
38414 already. noexcept is true then. */
38416 noex
= boolean_true_node
;
38419 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38420 parser
->in_transaction
= old_in
;
38422 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38423 return error_mark_node
;
38425 return (flag_tm
? expr
: error_mark_node
);
38428 /* Parse a function-transaction-block.
38430 function-transaction-block:
38431 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38433 __transaction_atomic txn-attribute[opt] function-try-block
38434 __transaction_relaxed ctor-initializer[opt] function-body
38435 __transaction_relaxed function-try-block
38439 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38441 unsigned char old_in
= parser
->in_transaction
;
38442 unsigned char new_in
= 1;
38443 tree compound_stmt
, stmt
, attrs
;
38446 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38447 || keyword
== RID_TRANSACTION_RELAXED
);
38448 token
= cp_parser_require_keyword (parser
, keyword
,
38449 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38450 : RT_TRANSACTION_RELAXED
));
38451 gcc_assert (token
!= NULL
);
38453 if (keyword
== RID_TRANSACTION_RELAXED
)
38454 new_in
|= TM_STMT_ATTR_RELAXED
;
38457 attrs
= cp_parser_txn_attribute_opt (parser
);
38459 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38462 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38464 parser
->in_transaction
= new_in
;
38466 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38467 cp_parser_function_try_block (parser
);
38469 cp_parser_ctor_initializer_opt_and_function_body
38470 (parser
, /*in_function_try_block=*/false);
38472 parser
->in_transaction
= old_in
;
38474 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38477 /* Parse a __transaction_cancel statement.
38480 __transaction_cancel txn-attribute[opt] ;
38481 __transaction_cancel txn-attribute[opt] throw-expression ;
38483 ??? Cancel and throw is not yet implemented. */
38486 cp_parser_transaction_cancel (cp_parser
*parser
)
38489 bool is_outer
= false;
38492 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38493 RT_TRANSACTION_CANCEL
);
38494 gcc_assert (token
!= NULL
);
38496 attrs
= cp_parser_txn_attribute_opt (parser
);
38498 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38500 /* ??? Parse cancel-and-throw here. */
38502 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38506 error_at (token
->location
, "%<__transaction_cancel%> without "
38507 "transactional memory support enabled");
38508 return error_mark_node
;
38510 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38512 error_at (token
->location
, "%<__transaction_cancel%> within a "
38513 "%<__transaction_relaxed%>");
38514 return error_mark_node
;
38518 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38519 && !is_tm_may_cancel_outer (current_function_decl
))
38521 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38522 "within outer %<__transaction_atomic%>");
38523 error_at (token
->location
,
38524 " or a %<transaction_may_cancel_outer%> function");
38525 return error_mark_node
;
38528 else if (parser
->in_transaction
== 0)
38530 error_at (token
->location
, "%<__transaction_cancel%> not within "
38531 "%<__transaction_atomic%>");
38532 return error_mark_node
;
38535 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38543 static GTY (()) cp_parser
*the_parser
;
38546 /* Special handling for the first token or line in the file. The first
38547 thing in the file might be #pragma GCC pch_preprocess, which loads a
38548 PCH file, which is a GC collection point. So we need to handle this
38549 first pragma without benefit of an existing lexer structure.
38551 Always returns one token to the caller in *FIRST_TOKEN. This is
38552 either the true first token of the file, or the first token after
38553 the initial pragma. */
38556 cp_parser_initial_pragma (cp_token
*first_token
)
38560 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38561 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38564 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38565 if (first_token
->type
== CPP_STRING
)
38567 name
= first_token
->u
.value
;
38569 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38570 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38571 error_at (first_token
->location
,
38572 "junk at end of %<#pragma GCC pch_preprocess%>");
38575 error_at (first_token
->location
, "expected string literal");
38577 /* Skip to the end of the pragma. */
38578 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38579 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38581 /* Now actually load the PCH file. */
38583 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38585 /* Read one more token to return to our caller. We have to do this
38586 after reading the PCH file in, since its pointers have to be
38588 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38591 /* Parse a pragma GCC ivdep. */
38594 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38596 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38600 /* Parse a pragma GCC unroll. */
38602 static unsigned short
38603 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38605 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38606 tree expr
= cp_parser_constant_expression (parser
);
38607 unsigned short unroll
;
38608 expr
= maybe_constant_value (expr
);
38609 HOST_WIDE_INT lunroll
= 0;
38610 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38611 || TREE_CODE (expr
) != INTEGER_CST
38612 || (lunroll
= tree_to_shwi (expr
)) < 0
38613 || lunroll
>= USHRT_MAX
)
38615 error_at (location
, "%<#pragma GCC unroll%> requires an"
38616 " assignment-expression that evaluates to a non-negative"
38617 " integral constant less than %u", USHRT_MAX
);
38622 unroll
= (unsigned short)lunroll
;
38626 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38630 /* Normal parsing of a pragma token. Here we can (and must) use the
38634 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38636 cp_token
*pragma_tok
;
38641 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38642 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38643 parser
->lexer
->in_pragma
= true;
38645 id
= cp_parser_pragma_kind (pragma_tok
);
38646 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38647 cp_ensure_no_omp_declare_simd (parser
);
38650 case PRAGMA_GCC_PCH_PREPROCESS
:
38651 error_at (pragma_tok
->location
,
38652 "%<#pragma GCC pch_preprocess%> must be first");
38655 case PRAGMA_OMP_BARRIER
:
38658 case pragma_compound
:
38659 cp_parser_omp_barrier (parser
, pragma_tok
);
38662 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38663 "used in compound statements", "omp barrier");
38670 case PRAGMA_OMP_FLUSH
:
38673 case pragma_compound
:
38674 cp_parser_omp_flush (parser
, pragma_tok
);
38677 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38678 "used in compound statements", "omp flush");
38685 case PRAGMA_OMP_TASKWAIT
:
38688 case pragma_compound
:
38689 cp_parser_omp_taskwait (parser
, pragma_tok
);
38692 error_at (pragma_tok
->location
,
38693 "%<#pragma %s%> may only be used in compound statements",
38701 case PRAGMA_OMP_TASKYIELD
:
38704 case pragma_compound
:
38705 cp_parser_omp_taskyield (parser
, pragma_tok
);
38708 error_at (pragma_tok
->location
,
38709 "%<#pragma %s%> may only be used in compound statements",
38717 case PRAGMA_OMP_CANCEL
:
38720 case pragma_compound
:
38721 cp_parser_omp_cancel (parser
, pragma_tok
);
38724 error_at (pragma_tok
->location
,
38725 "%<#pragma %s%> may only be used in compound statements",
38733 case PRAGMA_OMP_CANCELLATION_POINT
:
38734 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38737 case PRAGMA_OMP_THREADPRIVATE
:
38738 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38741 case PRAGMA_OMP_DECLARE
:
38742 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38744 case PRAGMA_OACC_DECLARE
:
38745 cp_parser_oacc_declare (parser
, pragma_tok
);
38748 case PRAGMA_OACC_ENTER_DATA
:
38749 if (context
== pragma_stmt
)
38751 error_at (pragma_tok
->location
,
38752 "%<#pragma %s%> may only be used in compound statements",
38756 else if (context
!= pragma_compound
)
38758 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38761 case PRAGMA_OACC_EXIT_DATA
:
38762 if (context
== pragma_stmt
)
38764 error_at (pragma_tok
->location
,
38765 "%<#pragma %s%> may only be used in compound statements",
38769 else if (context
!= pragma_compound
)
38771 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38774 case PRAGMA_OACC_ROUTINE
:
38775 if (context
!= pragma_external
)
38777 error_at (pragma_tok
->location
,
38778 "%<#pragma acc routine%> must be at file scope");
38781 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38784 case PRAGMA_OACC_UPDATE
:
38785 if (context
== pragma_stmt
)
38787 error_at (pragma_tok
->location
,
38788 "%<#pragma %s%> may only be used in compound statements",
38792 else if (context
!= pragma_compound
)
38794 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38797 case PRAGMA_OACC_WAIT
:
38798 if (context
== pragma_stmt
)
38800 error_at (pragma_tok
->location
,
38801 "%<#pragma %s%> may only be used in compound statements",
38805 else if (context
!= pragma_compound
)
38807 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38810 case PRAGMA_OACC_ATOMIC
:
38811 case PRAGMA_OACC_CACHE
:
38812 case PRAGMA_OACC_DATA
:
38813 case PRAGMA_OACC_HOST_DATA
:
38814 case PRAGMA_OACC_KERNELS
:
38815 case PRAGMA_OACC_PARALLEL
:
38816 case PRAGMA_OACC_LOOP
:
38817 case PRAGMA_OMP_ATOMIC
:
38818 case PRAGMA_OMP_CRITICAL
:
38819 case PRAGMA_OMP_DISTRIBUTE
:
38820 case PRAGMA_OMP_FOR
:
38821 case PRAGMA_OMP_MASTER
:
38822 case PRAGMA_OMP_PARALLEL
:
38823 case PRAGMA_OMP_SECTIONS
:
38824 case PRAGMA_OMP_SIMD
:
38825 case PRAGMA_OMP_SINGLE
:
38826 case PRAGMA_OMP_TASK
:
38827 case PRAGMA_OMP_TASKGROUP
:
38828 case PRAGMA_OMP_TASKLOOP
:
38829 case PRAGMA_OMP_TEAMS
:
38830 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38832 stmt
= push_omp_privatization_clauses (false);
38833 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38834 pop_omp_privatization_clauses (stmt
);
38837 case PRAGMA_OMP_ORDERED
:
38838 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38840 stmt
= push_omp_privatization_clauses (false);
38841 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38842 pop_omp_privatization_clauses (stmt
);
38845 case PRAGMA_OMP_TARGET
:
38846 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38848 stmt
= push_omp_privatization_clauses (false);
38849 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38850 pop_omp_privatization_clauses (stmt
);
38853 case PRAGMA_OMP_END_DECLARE_TARGET
:
38854 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38857 case PRAGMA_OMP_SECTION
:
38858 error_at (pragma_tok
->location
,
38859 "%<#pragma omp section%> may only be used in "
38860 "%<#pragma omp sections%> construct");
38865 if (context
== pragma_external
)
38867 error_at (pragma_tok
->location
,
38868 "%<#pragma GCC ivdep%> must be inside a function");
38871 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38872 unsigned short unroll
;
38873 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38874 if (tok
->type
== CPP_PRAGMA
38875 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38877 tok
= cp_lexer_consume_token (parser
->lexer
);
38878 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38879 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38883 if (tok
->type
!= CPP_KEYWORD
38884 || (tok
->keyword
!= RID_FOR
38885 && tok
->keyword
!= RID_WHILE
38886 && tok
->keyword
!= RID_DO
))
38888 cp_parser_error (parser
, "for, while or do statement expected");
38891 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38895 case PRAGMA_UNROLL
:
38897 if (context
== pragma_external
)
38899 error_at (pragma_tok
->location
,
38900 "%<#pragma GCC unroll%> must be inside a function");
38903 const unsigned short unroll
38904 = cp_parser_pragma_unroll (parser
, pragma_tok
);
38906 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38907 if (tok
->type
== CPP_PRAGMA
38908 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
38910 tok
= cp_lexer_consume_token (parser
->lexer
);
38911 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
38912 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38916 if (tok
->type
!= CPP_KEYWORD
38917 || (tok
->keyword
!= RID_FOR
38918 && tok
->keyword
!= RID_WHILE
38919 && tok
->keyword
!= RID_DO
))
38921 cp_parser_error (parser
, "for, while or do statement expected");
38924 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38929 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38930 c_invoke_pragma_handler (id
);
38934 cp_parser_error (parser
, "expected declaration specifiers");
38938 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38942 /* The interface the pragma parsers have to the lexer. */
38945 pragma_lex (tree
*value
, location_t
*loc
)
38947 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38948 enum cpp_ttype ret
= tok
->type
;
38950 *value
= tok
->u
.value
;
38952 *loc
= tok
->location
;
38954 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38956 else if (ret
== CPP_STRING
)
38957 *value
= cp_parser_string_literal (the_parser
, false, false);
38960 if (ret
== CPP_KEYWORD
)
38962 cp_lexer_consume_token (the_parser
->lexer
);
38969 /* External interface. */
38971 /* Parse one entire translation unit. */
38974 c_parse_file (void)
38976 static bool already_called
= false;
38978 if (already_called
)
38979 fatal_error (input_location
,
38980 "inter-module optimizations not implemented for C++");
38981 already_called
= true;
38983 the_parser
= cp_parser_new ();
38984 push_deferring_access_checks (flag_access_control
38985 ? dk_no_deferred
: dk_no_check
);
38986 cp_parser_translation_unit (the_parser
);
38990 /* Create an identifier for a generic parameter type (a synthesized
38991 template parameter implied by `auto' or a concept identifier). */
38993 static GTY(()) int generic_parm_count
;
38995 make_generic_type_name ()
38998 sprintf (buf
, "auto:%d", ++generic_parm_count
);
38999 return get_identifier (buf
);
39002 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39003 (creating a new template parameter list if necessary). Returns the newly
39004 created template type parm. */
39007 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39009 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39011 /* Before committing to modifying any scope, if we're in an
39012 implicit template scope, and we're trying to synthesize a
39013 constrained parameter, try to find a previous parameter with
39014 the same name. This is the same-type rule for abbreviated
39015 function templates.
39017 NOTE: We can generate implicit parameters when tentatively
39018 parsing a nested name specifier, only to reject that parse
39019 later. However, matching the same template-id as part of a
39020 direct-declarator should generate an identical template
39021 parameter, so this rule will merge them. */
39022 if (parser
->implicit_template_scope
&& constr
)
39024 tree t
= parser
->implicit_template_parms
;
39027 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39029 tree d
= TREE_VALUE (t
);
39030 if (TREE_CODE (d
) == PARM_DECL
)
39031 /* Return the TEMPLATE_PARM_INDEX. */
39032 d
= DECL_INITIAL (d
);
39035 t
= TREE_CHAIN (t
);
39039 /* We are either continuing a function template that already contains implicit
39040 template parameters, creating a new fully-implicit function template, or
39041 extending an existing explicit function template with implicit template
39044 cp_binding_level
*const entry_scope
= current_binding_level
;
39046 bool become_template
= false;
39047 cp_binding_level
*parent_scope
= 0;
39049 if (parser
->implicit_template_scope
)
39051 gcc_assert (parser
->implicit_template_parms
);
39053 current_binding_level
= parser
->implicit_template_scope
;
39057 /* Roll back to the existing template parameter scope (in the case of
39058 extending an explicit function template) or introduce a new template
39059 parameter scope ahead of the function parameter scope (or class scope
39060 in the case of out-of-line member definitions). The function scope is
39061 added back after template parameter synthesis below. */
39063 cp_binding_level
*scope
= entry_scope
;
39065 while (scope
->kind
== sk_function_parms
)
39067 parent_scope
= scope
;
39068 scope
= scope
->level_chain
;
39070 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39072 /* If not defining a class, then any class scope is a scope level in
39073 an out-of-line member definition. In this case simply wind back
39074 beyond the first such scope to inject the template parameter list.
39075 Otherwise wind back to the class being defined. The latter can
39076 occur in class member friend declarations such as:
39082 friend void A::foo (auto);
39085 The template parameter list synthesized for the friend declaration
39086 must be injected in the scope of 'B'. This can also occur in
39087 erroneous cases such as:
39093 void B::foo (auto) {}
39096 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39097 but, nevertheless, the template parameter list synthesized for the
39098 declarator should be injected into the scope of 'A' as if the
39099 ill-formed template was specified explicitly. */
39101 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39103 parent_scope
= scope
;
39104 scope
= scope
->level_chain
;
39108 current_binding_level
= scope
;
39110 if (scope
->kind
!= sk_template_parms
39111 || !function_being_declared_is_template_p (parser
))
39113 /* Introduce a new template parameter list for implicit template
39116 become_template
= true;
39118 parser
->implicit_template_scope
39119 = begin_scope (sk_template_parms
, NULL
);
39121 ++processing_template_decl
;
39123 parser
->fully_implicit_function_template_p
= true;
39124 ++parser
->num_template_parameter_lists
;
39128 /* Synthesize implicit template parameters at the end of the explicit
39129 template parameter list. */
39131 gcc_assert (current_template_parms
);
39133 parser
->implicit_template_scope
= scope
;
39135 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39136 parser
->implicit_template_parms
39137 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39141 /* Synthesize a new template parameter and track the current template
39142 parameter chain with implicit_template_parms. */
39144 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39145 tree synth_id
= make_generic_type_name ();
39146 tree synth_tmpl_parm
;
39147 bool non_type
= false;
39149 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39151 = finish_template_type_parm (class_type_node
, synth_id
);
39152 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39154 = finish_constrained_template_template_parm (proto
, synth_id
);
39157 synth_tmpl_parm
= copy_decl (proto
);
39158 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39162 // Attach the constraint to the parm before processing.
39163 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39164 TREE_TYPE (node
) = constr
;
39166 = process_template_parm (parser
->implicit_template_parms
,
39169 /*non_type=*/non_type
,
39170 /*param_pack=*/false);
39172 // Chain the new parameter to the list of implicit parameters.
39173 if (parser
->implicit_template_parms
)
39174 parser
->implicit_template_parms
39175 = TREE_CHAIN (parser
->implicit_template_parms
);
39177 parser
->implicit_template_parms
= new_parm
;
39179 tree new_decl
= get_local_decls ();
39181 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39182 new_decl
= DECL_INITIAL (new_decl
);
39184 /* If creating a fully implicit function template, start the new implicit
39185 template parameter list with this synthesized type, otherwise grow the
39186 current template parameter list. */
39188 if (become_template
)
39190 parent_scope
->level_chain
= current_binding_level
;
39192 tree new_parms
= make_tree_vec (1);
39193 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39194 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39195 new_parms
, current_template_parms
);
39199 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39200 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39201 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39202 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39205 // If the new parameter was constrained, we need to add that to the
39206 // constraints in the template parameter list.
39207 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39209 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39210 reqs
= conjoin_constraints (reqs
, req
);
39211 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39214 current_binding_level
= entry_scope
;
39219 /* Finish the declaration of a fully implicit function template. Such a
39220 template has no explicit template parameter list so has not been through the
39221 normal template head and tail processing. synthesize_implicit_template_parm
39222 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39223 provided if the declaration is a class member such that its template
39224 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39225 form is returned. Otherwise NULL_TREE is returned. */
39228 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39230 gcc_assert (parser
->fully_implicit_function_template_p
);
39232 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39233 && DECL_VIRTUAL_P (member_decl_opt
))
39235 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39236 "implicit templates may not be %<virtual%>");
39237 DECL_VIRTUAL_P (member_decl_opt
) = false;
39240 if (member_decl_opt
)
39241 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39242 end_template_decl ();
39244 parser
->fully_implicit_function_template_p
= false;
39245 --parser
->num_template_parameter_lists
;
39247 return member_decl_opt
;
39250 /* Helper function for diagnostics that have complained about things
39251 being used with 'extern "C"' linkage.
39253 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39256 maybe_show_extern_c_location (void)
39258 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39259 inform (the_parser
->innermost_linkage_specification_location
,
39260 "%<extern \"C\"%> linkage started here");
39263 #include "gt-cp-parser.h"