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 *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2267 static void abort_fully_implicit_template
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2276 static tree cp_parser_class_head
2277 (cp_parser
*, bool *);
2278 static enum tag_types cp_parser_class_key
2280 static void cp_parser_type_parameter_key
2281 (cp_parser
* parser
);
2282 static void cp_parser_member_specification_opt
2284 static void cp_parser_member_declaration
2286 static tree cp_parser_pure_specifier
2288 static tree cp_parser_constant_initializer
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2295 static tree cp_parser_base_specifier
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2302 static tree cp_parser_conversion_type_id
2304 static cp_declarator
*cp_parser_conversion_declarator_opt
2306 static void cp_parser_ctor_initializer_opt
2308 static void cp_parser_mem_initializer_list
2310 static tree cp_parser_mem_initializer
2312 static tree cp_parser_mem_initializer_id
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2319 static cp_expr cp_parser_operator
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser
*, bool);
2326 static tree cp_parser_template_parameter_list
2328 static tree cp_parser_template_parameter
2329 (cp_parser
*, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser
*, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser
*, bool, bool, enum tag_types
, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2336 static tree cp_parser_template_argument_list
2338 static tree cp_parser_template_argument
2340 static void cp_parser_explicit_instantiation
2342 static void cp_parser_explicit_specialization
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2349 static void cp_parser_function_try_block
2351 static void cp_parser_handler_seq
2353 static void cp_parser_handler
2355 static tree cp_parser_exception_declaration
2357 static tree cp_parser_throw_expression
2359 static tree cp_parser_exception_specification_opt
2361 static tree cp_parser_type_id_list
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2368 static tree cp_parser_asm_operand_list
2370 static tree cp_parser_asm_clobber_list
2372 static tree cp_parser_asm_label_list
2374 static bool cp_next_tokens_can_be_attribute_p
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 static bool cp_next_tokens_can_be_std_attribute_p
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser
*, size_t);
2386 static tree cp_parser_attributes_opt
2388 static tree cp_parser_gnu_attributes_opt
2390 static tree cp_parser_gnu_attribute_list
2392 static tree cp_parser_std_attribute
2393 (cp_parser
*, tree
);
2394 static tree cp_parser_std_attribute_spec
2396 static tree cp_parser_std_attribute_spec_seq
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser
*, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser
*, int *);
2402 static void cp_parser_label_declaration
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2409 static tree cp_parser_requires_clause_opt
2411 static tree cp_parser_requires_expression
2413 static tree cp_parser_requirement_parameter_list
2415 static tree cp_parser_requirement_body
2417 static tree cp_parser_requirement_list
2419 static tree cp_parser_requirement
2421 static tree cp_parser_simple_requirement
2423 static tree cp_parser_compound_requirement
2425 static tree cp_parser_type_requirement
2427 static tree cp_parser_nested_requirement
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser
*, cp_token
*);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser
*, enum rid
);
2436 static void cp_parser_function_transaction
2437 (cp_parser
*, enum rid
);
2438 static tree cp_parser_transaction_cancel
2441 enum pragma_context
{
2448 static bool cp_parser_pragma
2449 (cp_parser
*, enum pragma_context
, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2455 static tree cp_parser_objc_message_args
2457 static tree cp_parser_objc_message_expression
2459 static cp_expr cp_parser_objc_encode_expression
2461 static tree cp_parser_objc_defs_expression
2463 static tree cp_parser_objc_protocol_expression
2465 static tree cp_parser_objc_selector_expression
2467 static cp_expr cp_parser_objc_expression
2469 static bool cp_parser_objc_selector_p
2471 static tree cp_parser_objc_selector
2473 static tree cp_parser_objc_protocol_refs_opt
2475 static void cp_parser_objc_declaration
2476 (cp_parser
*, tree
);
2477 static tree cp_parser_objc_statement
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser
*, tree
*);
2481 static void cp_parser_objc_at_property_declaration
2483 static void cp_parser_objc_at_synthesize_declaration
2485 static void cp_parser_objc_at_dynamic_declaration
2487 static tree cp_parser_objc_struct_declaration
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser
*, tree
, location_t
);
2496 static tree cp_parser_maybe_treat_template_as_class
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser
*, cp_declarator
*, location_t
);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2502 static cp_expr cp_parser_simple_cast_expression
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser
*, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser
*, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser
*, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser
*, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec
<deferred_access_check
, va_gc
> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser
*, tree
);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2522 static tree cp_parser_save_nsdmi
2524 static tree cp_parser_enclosed_template_argument_list
2526 static void cp_parser_save_default_args
2527 (cp_parser
*, tree
);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser
*, tree
);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser
*, tree
, tree
, tree
);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser
*, tree
);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser
*, tree
);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser
*, enum rid
);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser
*, enum rid
);
2540 static bool cp_parser_declares_only_class_p
2542 static void cp_parser_set_storage_class
2543 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq
*decl_specs
,
2548 cp_decl_spec ds
, cp_token
*);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq
*);
2551 static void cp_parser_required_error
2552 (cp_parser
*, required_token
, bool, location_t
);
2553 static cp_token
*cp_parser_require
2554 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2555 static cp_token
*cp_parser_require_keyword
2556 (cp_parser
*, enum rid
, required_token
);
2557 static bool cp_parser_token_starts_function_definition_p
2559 static bool cp_parser_next_token_starts_class_definition_p
2561 static bool cp_parser_next_token_ends_template_argument_p
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser
*, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2569 static void cp_parser_check_class_key
2570 (enum tag_types
, tree type
);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type
, location_t location
);
2573 static bool cp_parser_optional_template_keyword
2575 static void cp_parser_pre_parsed_nested_name_specifier
2577 static bool cp_parser_cache_group
2578 (cp_parser
*, enum cpp_ttype
, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser
*parser
, bool nsdmi
);
2581 static void cp_parser_parse_tentatively
2583 static void cp_parser_commit_to_tentative_parse
2585 static void cp_parser_commit_to_topmost_tentative_parse
2587 static void cp_parser_abort_tentative_parse
2589 static bool cp_parser_parse_definitely
2591 static inline bool cp_parser_parsing_tentatively
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2595 static void cp_parser_error
2596 (cp_parser
*, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2599 static bool cp_parser_simulate_error
2601 static bool cp_parser_check_type_definition
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator
*, tree
, location_t type_location
);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser
*, non_integral_constant
);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser
*, tree
, location_t
);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser
*, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2619 static void cp_parser_skip_to_end_of_block_or_statement
2621 static bool cp_parser_skip_to_closing_brace
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser
*, cp_token
*);
2627 static bool cp_parser_error_occurred
2629 static bool cp_parser_allow_gnu_extensions_p
2631 static bool cp_parser_is_pure_string_literal
2633 static bool cp_parser_is_string_literal
2635 static bool cp_parser_is_keyword
2636 (cp_token
*, enum rid
);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser
*, tree
, location_t location
);
2639 static cp_declarator
* cp_parser_make_indirect_declarator
2640 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2641 static bool cp_parser_compound_literal_p
2643 static bool cp_parser_array_designator_p
2645 static bool cp_parser_init_statement_p
2647 static bool cp_parser_skip_to_closing_square_bracket
2650 /* Concept-related syntactic transformations */
2652 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2653 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand
;
2662 ++c_inhibit_evaluation_warnings
;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings
;
2668 --cp_unevaluated_operand
;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2677 cp_parser_parsing_tentatively (cp_parser
* parser
)
2679 return parser
->context
->next
!= NULL
;
2682 /* Returns nonzero if TOKEN is a string literal. */
2685 cp_parser_is_pure_string_literal (cp_token
* token
)
2687 return (token
->type
== CPP_STRING
||
2688 token
->type
== CPP_STRING16
||
2689 token
->type
== CPP_STRING32
||
2690 token
->type
== CPP_WSTRING
||
2691 token
->type
== CPP_UTF8STRING
);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2698 cp_parser_is_string_literal (cp_token
* token
)
2700 return (cp_parser_is_pure_string_literal (token
) ||
2701 token
->type
== CPP_STRING_USERDEF
||
2702 token
->type
== CPP_STRING16_USERDEF
||
2703 token
->type
== CPP_STRING32_USERDEF
||
2704 token
->type
== CPP_WSTRING_USERDEF
||
2705 token
->type
== CPP_UTF8STRING_USERDEF
);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2713 return token
->keyword
== keyword
;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token
*token
)
2722 if (token
->type
!= CPP_PRAGMA
)
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2740 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2741 location_t
*out_loc
)
2743 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2744 if (token2
->type
!= tok1_kind
)
2746 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2747 if (token3
->type
!= tok1_kind
)
2749 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2750 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2753 /* It must be at the start of the line. */
2754 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2755 if (LOCATION_COLUMN (start_loc
) != 1)
2758 /* We have a conflict marker. Construct a location of the form:
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc
= get_finish (token4
->location
);
2763 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2772 get_matching_symbol (required_token token_desc
)
2779 case RT_CLOSE_BRACE
:
2781 case RT_CLOSE_PAREN
:
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc
)
2795 return CPP_SEMICOLON
;
2797 return CPP_OPEN_PAREN
;
2798 case RT_CLOSE_BRACE
:
2799 return CPP_CLOSE_BRACE
;
2801 return CPP_OPEN_BRACE
;
2802 case RT_CLOSE_SQUARE
:
2803 return CPP_CLOSE_SQUARE
;
2804 case RT_OPEN_SQUARE
:
2805 return CPP_OPEN_SQUARE
;
2810 case RT_CLOSE_PAREN
:
2811 return CPP_CLOSE_PAREN
;
2814 /* Use CPP_EOF as a "no completions possible" code. */
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2839 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2840 required_token missing_token_desc
,
2841 location_t matching_location
)
2843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token
);
2848 if (token
->type
== CPP_PRAGMA
)
2850 error_at (token
->location
,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser
, token
);
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token
->type
== CPP_LSHIFT
2858 || token
->type
== CPP_RSHIFT
2859 || token
->type
== CPP_EQ_EQ
)
2862 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2864 error_at (loc
, "version control conflict marker in file");
2869 gcc_rich_location
richloc (input_location
);
2871 bool added_matching_location
= false;
2873 if (missing_token_desc
!= RT_NONE
)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser
->lexer
)->location
;
2881 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location
!= UNKNOWN_LOCATION
)
2887 added_matching_location
2888 = richloc
.add_location_if_nearby (matching_location
);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid
,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2896 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2897 token
->u
.value
, token
->flags
, &richloc
);
2899 if (missing_token_desc
!= RT_NONE
)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location
!= UNKNOWN_LOCATION
2904 && !added_matching_location
)
2905 inform (matching_location
, "to match this %qs",
2906 get_matching_symbol (missing_token_desc
));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2917 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2919 if (!cp_parser_simulate_error (parser
))
2920 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2929 cp_parser_name_lookup_error (cp_parser
* parser
,
2932 name_lookup_error desired
,
2933 location_t location
)
2935 /* If name lookup completely failed, tell the user that NAME was not
2937 if (decl
== error_mark_node
)
2939 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2940 error_at (location
, "%<%E::%E%> has not been declared",
2941 parser
->scope
, name
);
2942 else if (parser
->scope
== global_namespace
)
2943 error_at (location
, "%<::%E%> has not been declared", name
);
2944 else if (parser
->object_scope
2945 && !CLASS_TYPE_P (parser
->object_scope
))
2946 error_at (location
, "request for member %qE in non-class type %qT",
2947 name
, parser
->object_scope
);
2948 else if (parser
->object_scope
)
2949 error_at (location
, "%<%T::%E%> has not been declared",
2950 parser
->object_scope
, name
);
2952 error_at (location
, "%qE has not been declared", name
);
2954 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2959 error_at (location
, "%<%E::%E%> is not a type",
2960 parser
->scope
, name
);
2963 error_at (location
, "%<%E::%E%> is not a class or namespace",
2964 parser
->scope
, name
);
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser
->scope
, name
);
2976 else if (parser
->scope
== global_namespace
)
2981 error_at (location
, "%<::%E%> is not a type", name
);
2984 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2988 "%<::%E%> is not a class, namespace, or enumeration",
3000 error_at (location
, "%qE is not a type", name
);
3003 error_at (location
, "%qE is not a class or namespace", name
);
3007 "%qE is not a class, namespace, or enumeration", name
);
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3020 cp_parser_simulate_error (cp_parser
* parser
)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3024 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3035 cp_parser_check_type_definition (cp_parser
* parser
)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser
->type_definition_forbidden_message
)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser
->type_definition_forbidden_message
);
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3055 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3056 tree type
, location_t type_location
)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3062 && (declarator
->kind
== cdk_pointer
3063 || declarator
->kind
== cdk_reference
3064 || declarator
->kind
== cdk_ptrmem
))
3065 declarator
= declarator
->declarator
;
3067 && declarator
->kind
== cdk_function
)
3069 error_at (type_location
,
3070 "new types may not be defined in a return type");
3071 inform (type_location
,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3084 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3086 enum tag_types tag_type
,
3087 location_t location
)
3089 cp_token_position start
= 0;
3091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3093 if (TREE_CODE (type
) == TYPE_DECL
)
3094 type
= TREE_TYPE (type
);
3095 if (TYPE_P (type
) && !template_placeholder_p (type
))
3096 error_at (location
, "%qT is not a template", type
);
3097 else if (identifier_p (type
))
3099 if (tag_type
!= none_type
)
3100 error_at (location
, "%qE is not a class template", type
);
3102 error_at (location
, "%qE is not a template", type
);
3105 error_at (location
, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3108 start
= cp_lexer_token_position (parser
->lexer
, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser
->lexer
);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser
);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3116 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3126 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3127 non_integral_constant thing
)
3129 parser
->non_integral_constant_expression_p
= true;
3130 if (parser
->integral_constant_expression_p
)
3132 if (!parser
->allow_non_integral_constant_expression_p
)
3134 const char *msg
= NULL
;
3138 pedwarn (input_location
, OPT_Wpedantic
,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3171 case NIC_ADDR_LABEL
:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3175 case NIC_OVERLOADED
:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3179 case NIC_ASSIGNMENT
:
3180 error ("an assignment cannot appear in a constant-expression");
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3186 case NIC_CONSTRUCTOR
:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3190 case NIC_TRANSACTION
:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3198 msg
= "__FUNCTION__";
3200 case NIC_PRETTY_FUNC
:
3201 msg
= "__PRETTY_FUNCTION__";
3221 case NIC_PREINCREMENT
:
3224 case NIC_PREDECREMENT
:
3237 error ("%qs cannot appear in a constant-expression", msg
);
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3250 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3251 location_t location
)
3253 tree decl
, ambiguous_decls
;
3254 cp_parser_commit_to_tentative_parse (parser
);
3255 /* Try to lookup the identifier. */
3256 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls
, location
);
3261 if (ambiguous_decls
)
3262 /* If the lookup was ambiguous, an error will already have
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl
))
3270 "invalid use of template-name %qE without an argument list",
3272 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3273 inform (location
, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3277 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3278 error_at (location
, "invalid use of destructor %qD as a type", id
);
3279 else if (TREE_CODE (decl
) == TYPE_DECL
)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location
, "invalid combination of multiple type-specifiers");
3282 else if (!parser
->scope
)
3284 /* Issue an error message. */
3286 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3287 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3290 gcc_rich_location
richloc (location
);
3291 richloc
.add_fixit_replace (hint
.suggestion ());
3293 "%qE does not name a type; did you mean %qs?",
3294 id
, hint
.suggestion ());
3297 error_at (location
, "%qE does not name a type", id
);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3306 inform (location
, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3309 inform (location
, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect
< cxx11
3312 && TREE_CODE (id
) == IDENTIFIER_NODE
3313 && id_equal (id
, "thread_local"))
3314 inform (location
, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3317 inform (location
, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl
&& current_class_type
3319 && TYPE_BINFO (current_class_type
))
3323 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3327 tree base_type
= BINFO_TYPE (b
);
3328 if (CLASS_TYPE_P (base_type
)
3329 && dependent_type_p (base_type
))
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3336 for (field
= TYPE_FIELDS (base_type
);
3338 field
= DECL_CHAIN (field
))
3339 if (TREE_CODE (field
) == TYPE_DECL
3340 && DECL_NAME (field
) == id
)
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b
), id
);
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser
->scope
!= error_mark_node
)
3357 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3360 error_at (location_of (id
),
3361 "%qE in namespace %qE does not name a template type",
3363 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3364 error_at (location_of (id
),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id
, 0), parser
->scope
);
3368 error_at (location_of (id
),
3369 "%qE in namespace %qE does not name a type",
3372 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3373 else if (decl
== error_mark_node
)
3374 suggest_alternative_in_explicit_scope (location
, id
,
3377 else if (CLASS_TYPE_P (parser
->scope
)
3378 && constructor_name_p (id
, parser
->scope
))
3381 error_at (location
, "%<%T::%E%> names the constructor, not"
3382 " the type", parser
->scope
, id
);
3383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3384 error_at (location
, "and %qT has no template constructors",
3387 else if (TYPE_P (parser
->scope
)
3388 && dependent_scope_p (parser
->scope
))
3390 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser
->scope
),
3395 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3397 TYPE_CONTEXT (parser
->scope
),
3398 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3400 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser
->scope
, id
, parser
->scope
);
3404 else if (TYPE_P (parser
->scope
))
3406 if (!COMPLETE_TYPE_P (parser
->scope
))
3407 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3409 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3410 error_at (location_of (id
),
3411 "%qE in %q#T does not name a template type",
3413 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id
, 0), parser
->scope
);
3418 error_at (location_of (id
),
3419 "%qE in %q#T does not name a type",
3422 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3448 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3449 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3453 cp_parser_parse_tentatively (parser
);
3454 id
= cp_parser_id_expression (parser
,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL
,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3463 || TREE_CODE (id
) == TYPE_DECL
)
3465 cp_parser_abort_tentative_parse (parser
);
3468 if (!cp_parser_parse_definitely (parser
))
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser
->in_declarator_p
)
3478 cp_parser_skip_to_end_of_block_or_statement (parser
);
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3493 unsigned paren_depth
= 0;
3494 unsigned brace_depth
= 0;
3495 unsigned square_depth
= 0;
3497 if (recovering
&& or_ttype
== CPP_EOF
3498 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3503 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3505 /* Have we found what we're looking for before the closing paren? */
3506 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3507 && !brace_depth
&& !paren_depth
&& !square_depth
)
3510 switch (token
->type
)
3513 case CPP_PRAGMA_EOL
:
3514 /* If we've run out of tokens, then there is no closing `)'. */
3517 /* This is good for lambda expression capture-lists. */
3518 case CPP_OPEN_SQUARE
:
3521 case CPP_CLOSE_SQUARE
:
3522 if (!square_depth
--)
3527 /* This matches the processing in skip_to_end_of_statement. */
3532 case CPP_OPEN_BRACE
:
3535 case CPP_CLOSE_BRACE
:
3540 case CPP_OPEN_PAREN
:
3545 case CPP_CLOSE_PAREN
:
3546 if (!brace_depth
&& !paren_depth
--)
3549 cp_lexer_consume_token (parser
->lexer
);
3558 /* Consume the token. */
3559 cp_lexer_consume_token (parser
->lexer
);
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3565 are doing error recovery. Returns -1 if OR_COMMA is true and we
3566 found an unnested token of that type. */
3569 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3574 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3575 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3576 ttype
, consume_paren
);
3579 /* Consume tokens until we reach the end of the current statement.
3580 Normally, that will be just before consuming a `;'. However, if a
3581 non-nested `}' comes first, then we stop before consuming that. */
3584 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3586 unsigned nesting_depth
= 0;
3588 /* Unwind generic function template scope if necessary. */
3589 if (parser
->fully_implicit_function_template_p
)
3590 abort_fully_implicit_template (parser
);
3594 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3596 switch (token
->type
)
3599 case CPP_PRAGMA_EOL
:
3600 /* If we've run out of tokens, stop. */
3604 /* If the next token is a `;', we have reached the end of the
3610 case CPP_CLOSE_BRACE
:
3611 /* If this is a non-nested '}', stop before consuming it.
3612 That way, when confronted with something like:
3616 we stop before consuming the closing '}', even though we
3617 have not yet reached a `;'. */
3618 if (nesting_depth
== 0)
3621 /* If it is the closing '}' for a block that we have
3622 scanned, stop -- but only after consuming the token.
3628 we will stop after the body of the erroneously declared
3629 function, but before consuming the following `typedef'
3631 if (--nesting_depth
== 0)
3633 cp_lexer_consume_token (parser
->lexer
);
3638 case CPP_OPEN_BRACE
:
3646 /* Consume the token. */
3647 cp_lexer_consume_token (parser
->lexer
);
3651 /* This function is called at the end of a statement or declaration.
3652 If the next token is a semicolon, it is consumed; otherwise, error
3653 recovery is attempted. */
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3658 /* Look for the trailing `;'. */
3659 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3661 /* If there is additional (erroneous) input, skip to the end of
3663 cp_parser_skip_to_end_of_statement (parser
);
3664 /* If the next token is now a `;', consume it. */
3665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3666 cp_lexer_consume_token (parser
->lexer
);
3670 /* Skip tokens until we have consumed an entire block, or until we
3671 have consumed a non-nested `;'. */
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3676 int nesting_depth
= 0;
3678 /* Unwind generic function template scope if necessary. */
3679 if (parser
->fully_implicit_function_template_p
)
3680 abort_fully_implicit_template (parser
);
3682 while (nesting_depth
>= 0)
3684 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3686 switch (token
->type
)
3689 case CPP_PRAGMA_EOL
:
3690 /* If we've run out of tokens, stop. */
3694 /* Stop if this is an unnested ';'. */
3699 case CPP_CLOSE_BRACE
:
3700 /* Stop if this is an unnested '}', or closes the outermost
3703 if (nesting_depth
< 0)
3709 case CPP_OPEN_BRACE
:
3718 /* Consume the token. */
3719 cp_lexer_consume_token (parser
->lexer
);
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724 token, or there are no more tokens. Return true in the first case,
3728 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3730 unsigned nesting_depth
= 0;
3734 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3736 switch (token
->type
)
3739 case CPP_PRAGMA_EOL
:
3740 /* If we've run out of tokens, stop. */
3743 case CPP_CLOSE_BRACE
:
3744 /* If the next token is a non-nested `}', then we have reached
3745 the end of the current block. */
3746 if (nesting_depth
-- == 0)
3750 case CPP_OPEN_BRACE
:
3751 /* If it the next token is a `{', then we are entering a new
3752 block. Consume the entire block. */
3760 /* Consume the token. */
3761 cp_lexer_consume_token (parser
->lexer
);
3765 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3766 parameter is the PRAGMA token, allowing us to purge the entire pragma
3770 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3774 parser
->lexer
->in_pragma
= false;
3777 token
= cp_lexer_consume_token (parser
->lexer
);
3778 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3780 /* Ensure that the pragma is not parsed again. */
3781 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3784 /* Require pragma end of line, resyncing with it as necessary. The
3785 arguments are as for cp_parser_skip_to_pragma_eol. */
3788 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3790 parser
->lexer
->in_pragma
= false;
3791 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3792 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796 an unresolved identifier node, we can provide a superior diagnostic
3797 using cp_parser_diagnose_invalid_type_name. */
3800 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3801 location_t id_location
)
3804 if (identifier_p (id
))
3806 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3807 /*complain=*/tf_none
);
3808 if (result
== error_mark_node
)
3809 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3812 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3815 /* This is a wrapper around the
3816 make_{pointer,ptrmem,reference}_declarator functions that decides
3817 which one to call based on the CODE and CLASS_TYPE arguments. The
3818 CODE argument should be one of the values returned by
3819 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3820 appertain to the pointer or reference. */
3822 static cp_declarator
*
3823 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3824 cp_cv_quals cv_qualifiers
,
3825 cp_declarator
*target
,
3828 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3829 return cp_error_declarator
;
3831 if (code
== INDIRECT_REF
)
3832 if (class_type
== NULL_TREE
)
3833 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3835 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3836 target
, attributes
);
3837 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3838 return make_reference_declarator (cv_qualifiers
, target
,
3840 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3841 return make_reference_declarator (cv_qualifiers
, target
,
3846 /* Create a new C++ parser. */
3849 cp_parser_new (void)
3855 /* cp_lexer_new_main is called before doing GC allocation because
3856 cp_lexer_new_main might load a PCH file. */
3857 lexer
= cp_lexer_new_main ();
3859 /* Initialize the binops_by_token so that we can get the tree
3860 directly from the token. */
3861 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3862 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3864 parser
= ggc_cleared_alloc
<cp_parser
> ();
3865 parser
->lexer
= lexer
;
3866 parser
->context
= cp_parser_context_new (NULL
);
3868 /* For now, we always accept GNU extensions. */
3869 parser
->allow_gnu_extensions_p
= 1;
3871 /* The `>' token is a greater-than operator, not the end of a
3873 parser
->greater_than_is_operator_p
= true;
3875 parser
->default_arg_ok_p
= true;
3877 /* We are not parsing a constant-expression. */
3878 parser
->integral_constant_expression_p
= false;
3879 parser
->allow_non_integral_constant_expression_p
= false;
3880 parser
->non_integral_constant_expression_p
= false;
3882 /* Local variable names are not forbidden. */
3883 parser
->local_variables_forbidden_p
= false;
3885 /* We are not processing an `extern "C"' declaration. */
3886 parser
->in_unbraced_linkage_specification_p
= false;
3888 /* We are not processing a declarator. */
3889 parser
->in_declarator_p
= false;
3891 /* We are not processing a template-argument-list. */
3892 parser
->in_template_argument_list_p
= false;
3894 /* We are not in an iteration statement. */
3895 parser
->in_statement
= 0;
3897 /* We are not in a switch statement. */
3898 parser
->in_switch_statement_p
= false;
3900 /* We are not parsing a type-id inside an expression. */
3901 parser
->in_type_id_in_expr_p
= false;
3903 /* Declarations aren't implicitly extern "C". */
3904 parser
->implicit_extern_c
= false;
3906 /* String literals should be translated to the execution character set. */
3907 parser
->translate_strings_p
= true;
3909 /* We are not parsing a function body. */
3910 parser
->in_function_body
= false;
3912 /* We can correct until told otherwise. */
3913 parser
->colon_corrects_to_scope_p
= true;
3915 /* The unparsed function queue is empty. */
3916 push_unparsed_function_queues (parser
);
3918 /* There are no classes being defined. */
3919 parser
->num_classes_being_defined
= 0;
3921 /* No template parameters apply. */
3922 parser
->num_template_parameter_lists
= 0;
3924 /* Special parsing data structures. */
3925 parser
->omp_declare_simd
= NULL
;
3926 parser
->oacc_routine
= NULL
;
3928 /* Not declaring an implicit function template. */
3929 parser
->auto_is_implicit_function_template_parm_p
= false;
3930 parser
->fully_implicit_function_template_p
= false;
3931 parser
->implicit_template_parms
= 0;
3932 parser
->implicit_template_scope
= 0;
3934 /* Allow constrained-type-specifiers. */
3935 parser
->prevent_constrained_type_specifiers
= 0;
3937 /* We haven't yet seen an 'extern "C"'. */
3938 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944 and push it onto the parser's lexer stack. This is used for delayed
3945 parsing of in-class method bodies and default arguments, and should
3946 not be confused with tentative parsing. */
3948 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3950 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3951 lexer
->next
= parser
->lexer
;
3952 parser
->lexer
= lexer
;
3954 /* Move the current source position to that of the first token in the
3956 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3959 /* Pop the top lexer off the parser stack. This is never used for the
3960 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3962 cp_parser_pop_lexer (cp_parser
*parser
)
3964 cp_lexer
*lexer
= parser
->lexer
;
3965 parser
->lexer
= lexer
->next
;
3966 cp_lexer_destroy (lexer
);
3968 /* Put the current source position back where it was before this
3969 lexer was pushed. */
3970 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3973 /* Lexical conventions [gram.lex] */
3975 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3979 cp_parser_identifier (cp_parser
* parser
)
3983 /* Look for the identifier. */
3984 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3985 /* Return the value. */
3987 return cp_expr (token
->u
.value
, token
->location
);
3989 return error_mark_node
;
3992 /* Parse a sequence of adjacent string constants. Returns a
3993 TREE_STRING representing the combined, nul-terminated string
3994 constant. If TRANSLATE is true, translate the string to the
3995 execution character set. If WIDE_OK is true, a wide string is
3998 C++98 [lex.string] says that if a narrow string literal token is
3999 adjacent to a wide string literal token, the behavior is undefined.
4000 However, C99 6.4.5p4 says that this results in a wide string literal.
4001 We follow C99 here, for consistency with the C front end.
4003 This code is largely lifted from lex_string() in c-lex.c.
4005 FUTURE: ObjC++ will need to handle @-strings here. */
4007 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4008 bool lookup_udlit
= true)
4012 struct obstack str_ob
;
4013 cpp_string str
, istr
, *strs
;
4015 enum cpp_ttype type
, curr_type
;
4016 int have_suffix_p
= 0;
4018 tree suffix_id
= NULL_TREE
;
4019 bool curr_tok_is_userdef_p
= false;
4021 tok
= cp_lexer_peek_token (parser
->lexer
);
4022 if (!cp_parser_is_string_literal (tok
))
4024 cp_parser_error (parser
, "expected string-literal");
4025 return error_mark_node
;
4028 location_t loc
= tok
->location
;
4030 if (cpp_userdef_string_p (tok
->type
))
4032 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4033 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4034 curr_tok_is_userdef_p
= true;
4038 string_tree
= tok
->u
.value
;
4039 curr_type
= tok
->type
;
4043 /* Try to avoid the overhead of creating and destroying an obstack
4044 for the common case of just one string. */
4045 if (!cp_parser_is_string_literal
4046 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4048 cp_lexer_consume_token (parser
->lexer
);
4050 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4051 str
.len
= TREE_STRING_LENGTH (string_tree
);
4054 if (curr_tok_is_userdef_p
)
4056 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4058 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4061 curr_type
= tok
->type
;
4067 location_t last_tok_loc
= tok
->location
;
4068 gcc_obstack_init (&str_ob
);
4073 cp_lexer_consume_token (parser
->lexer
);
4075 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4076 str
.len
= TREE_STRING_LENGTH (string_tree
);
4078 if (curr_tok_is_userdef_p
)
4080 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4081 if (have_suffix_p
== 0)
4083 suffix_id
= curr_suffix_id
;
4086 else if (have_suffix_p
== 1
4087 && curr_suffix_id
!= suffix_id
)
4089 error ("inconsistent user-defined literal suffixes"
4090 " %qD and %qD in string literal",
4091 suffix_id
, curr_suffix_id
);
4094 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4097 curr_type
= tok
->type
;
4099 if (type
!= curr_type
)
4101 if (type
== CPP_STRING
)
4103 else if (curr_type
!= CPP_STRING
)
4105 rich_location
rich_loc (line_table
, tok
->location
);
4106 rich_loc
.add_range (last_tok_loc
, false);
4107 error_at (&rich_loc
,
4108 "unsupported non-standard concatenation "
4109 "of string literals");
4113 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4115 last_tok_loc
= tok
->location
;
4117 tok
= cp_lexer_peek_token (parser
->lexer
);
4118 if (cpp_userdef_string_p (tok
->type
))
4120 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4121 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4122 curr_tok_is_userdef_p
= true;
4126 string_tree
= tok
->u
.value
;
4127 curr_type
= tok
->type
;
4128 curr_tok_is_userdef_p
= false;
4131 while (cp_parser_is_string_literal (tok
));
4133 /* A string literal built by concatenation has its caret=start at
4134 the start of the initial string, and its finish at the finish of
4135 the final string literal. */
4136 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4138 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4141 if (type
!= CPP_STRING
&& !wide_ok
)
4143 cp_parser_error (parser
, "a wide string is invalid in this context");
4147 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4148 (parse_in
, strs
, count
, &istr
, type
))
4150 value
= build_string (istr
.len
, (const char *)istr
.text
);
4151 free (CONST_CAST (unsigned char *, istr
.text
));
4157 case CPP_UTF8STRING
:
4158 TREE_TYPE (value
) = char_array_type_node
;
4161 TREE_TYPE (value
) = char16_array_type_node
;
4164 TREE_TYPE (value
) = char32_array_type_node
;
4167 TREE_TYPE (value
) = wchar_array_type_node
;
4171 value
= fix_string_type (value
);
4175 tree literal
= build_userdef_literal (suffix_id
, value
,
4176 OT_NONE
, NULL_TREE
);
4178 value
= cp_parser_userdef_string_literal (literal
);
4184 /* cpp_interpret_string has issued an error. */
4185 value
= error_mark_node
;
4188 obstack_free (&str_ob
, 0);
4190 return cp_expr (value
, loc
);
4193 /* Look up a literal operator with the name and the exact arguments. */
4196 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4199 decl
= lookup_name (name
);
4200 if (!decl
|| !is_overloaded_fn (decl
))
4201 return error_mark_node
;
4203 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4208 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4209 if (parmtypes
!= NULL_TREE
)
4211 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4212 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4214 tree tparm
= TREE_VALUE (parmtypes
);
4215 tree targ
= TREE_TYPE ((*args
)[ix
]);
4216 bool ptr
= TYPE_PTR_P (tparm
);
4217 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4218 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4220 || !same_type_p (TREE_TYPE (tparm
),
4225 && ix
== vec_safe_length (args
)
4226 /* May be this should be sufficient_parms_p instead,
4227 depending on how exactly should user-defined literals
4228 work in presence of default arguments on the literal
4229 operator parameters. */
4230 && parmtypes
== void_list_node
)
4235 return error_mark_node
;
4238 /* Parse a user-defined char constant. Returns a call to a user-defined
4239 literal operator taking the character as an argument. */
4242 cp_parser_userdef_char_literal (cp_parser
*parser
)
4244 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4245 tree literal
= token
->u
.value
;
4246 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4247 tree value
= USERDEF_LITERAL_VALUE (literal
);
4248 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4251 /* Build up a call to the user-defined operator */
4252 /* Lookup the name we got back from the id-expression. */
4253 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4254 vec_safe_push (args
, value
);
4255 decl
= lookup_literal_operator (name
, args
);
4256 if (!decl
|| decl
== error_mark_node
)
4258 error ("unable to find character literal operator %qD with %qT argument",
4259 name
, TREE_TYPE (value
));
4260 release_tree_vector (args
);
4261 return error_mark_node
;
4263 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4264 release_tree_vector (args
);
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269 create a char... template parameter pack from a string node. */
4272 make_char_string_pack (tree value
)
4275 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4276 const char *str
= TREE_STRING_POINTER (value
);
4277 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4278 tree argvec
= make_tree_vec (1);
4280 /* Fill in CHARVEC with all of the parameters. */
4281 charvec
= make_tree_vec (len
);
4282 for (i
= 0; i
< len
; ++i
)
4283 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4285 /* Build the argument packs. */
4286 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4288 TREE_VEC_ELT (argvec
, 0) = argpack
;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4297 make_string_pack (tree value
)
4300 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4301 const unsigned char *str
4302 = (const unsigned char *) TREE_STRING_POINTER (value
);
4303 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4304 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4305 tree argvec
= make_tree_vec (2);
4307 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4308 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4310 /* First template parm is character type. */
4311 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4313 /* Fill in CHARVEC with all of the parameters. */
4314 charvec
= make_tree_vec (len
);
4315 for (int i
= 0; i
< len
; ++i
)
4316 TREE_VEC_ELT (charvec
, i
)
4317 = double_int_to_tree (str_char_type_node
,
4318 double_int::from_buffer (str
+ i
* sz
, sz
));
4320 /* Build the argument packs. */
4321 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4323 TREE_VEC_ELT (argvec
, 1) = argpack
;
4328 /* Parse a user-defined numeric constant. returns a call to a user-defined
4329 literal operator. */
4332 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4334 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4335 tree literal
= token
->u
.value
;
4336 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4337 tree value
= USERDEF_LITERAL_VALUE (literal
);
4338 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4339 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4340 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4342 vec
<tree
, va_gc
> *args
;
4344 /* Look for a literal operator taking the exact type of numeric argument
4345 as the literal value. */
4346 args
= make_tree_vector ();
4347 vec_safe_push (args
, value
);
4348 decl
= lookup_literal_operator (name
, args
);
4349 if (decl
&& decl
!= error_mark_node
)
4351 result
= finish_call_expr (decl
, &args
, false, true,
4352 tf_warning_or_error
);
4354 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4356 warning_at (token
->location
, OPT_Woverflow
,
4357 "integer literal exceeds range of %qT type",
4358 long_long_unsigned_type_node
);
4363 warning_at (token
->location
, OPT_Woverflow
,
4364 "floating literal exceeds range of %qT type",
4365 long_double_type_node
);
4366 else if (overflow
< 0)
4367 warning_at (token
->location
, OPT_Woverflow
,
4368 "floating literal truncated to zero");
4371 release_tree_vector (args
);
4374 release_tree_vector (args
);
4376 /* If the numeric argument didn't work, look for a raw literal
4377 operator taking a const char* argument consisting of the number
4378 in string format. */
4379 args
= make_tree_vector ();
4380 vec_safe_push (args
, num_string
);
4381 decl
= lookup_literal_operator (name
, args
);
4382 if (decl
&& decl
!= error_mark_node
)
4384 result
= finish_call_expr (decl
, &args
, false, true,
4385 tf_warning_or_error
);
4386 release_tree_vector (args
);
4389 release_tree_vector (args
);
4391 /* If the raw literal didn't work, look for a non-type template
4392 function with parameter pack char.... Call the function with
4393 template parameter characters representing the number. */
4394 args
= make_tree_vector ();
4395 decl
= lookup_literal_operator (name
, args
);
4396 if (decl
&& decl
!= error_mark_node
)
4398 tree tmpl_args
= make_char_string_pack (num_string
);
4399 decl
= lookup_template_function (decl
, tmpl_args
);
4400 result
= finish_call_expr (decl
, &args
, false, true,
4401 tf_warning_or_error
);
4402 release_tree_vector (args
);
4406 release_tree_vector (args
);
4408 /* In C++14 the standard library defines complex number suffixes that
4409 conflict with GNU extensions. Prefer them if <complex> is #included. */
4410 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4411 bool i14
= (cxx_dialect
> cxx11
4412 && (id_equal (suffix_id
, "i")
4413 || id_equal (suffix_id
, "if")
4414 || id_equal (suffix_id
, "il")));
4415 diagnostic_t kind
= DK_ERROR
;
4420 tree cxlit
= lookup_qualified_name (std_node
,
4421 get_identifier ("complex_literals"),
4423 if (cxlit
== error_mark_node
)
4425 /* No <complex>, so pedwarn and use GNU semantics. */
4427 opt
= OPT_Wpedantic
;
4432 = emit_diagnostic (kind
, input_location
, opt
,
4433 "unable to find numeric literal operator %qD", name
);
4436 /* Don't inform either. */;
4439 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4440 "(from <complex>) to enable the C++14 user-defined literal "
4443 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4444 "GNU built-in suffix");
4447 inform (token
->location
, "use -fext-numeric-literals "
4448 "to enable more built-in suffixes");
4450 if (kind
== DK_ERROR
)
4451 value
= error_mark_node
;
4454 /* Use the built-in semantics. */
4456 if (id_equal (suffix_id
, "i"))
4458 if (TREE_CODE (value
) == INTEGER_CST
)
4459 type
= integer_type_node
;
4461 type
= double_type_node
;
4463 else if (id_equal (suffix_id
, "if"))
4464 type
= float_type_node
;
4465 else /* if (id_equal (suffix_id, "il")) */
4466 type
= long_double_type_node
;
4468 value
= build_complex (build_complex_type (type
),
4469 fold_convert (type
, integer_zero_node
),
4470 fold_convert (type
, value
));
4473 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4474 /* Avoid repeated diagnostics. */
4475 token
->u
.value
= value
;
4479 /* Parse a user-defined string constant. Returns a call to a user-defined
4480 literal operator taking a character pointer and the length of the string
4484 cp_parser_userdef_string_literal (tree literal
)
4486 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4487 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4488 tree value
= USERDEF_LITERAL_VALUE (literal
);
4489 int len
= TREE_STRING_LENGTH (value
)
4490 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4492 vec
<tree
, va_gc
> *args
;
4494 /* Build up a call to the user-defined operator. */
4495 /* Lookup the name we got back from the id-expression. */
4496 args
= make_tree_vector ();
4497 vec_safe_push (args
, value
);
4498 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4499 decl
= lookup_literal_operator (name
, args
);
4501 if (decl
&& decl
!= error_mark_node
)
4503 result
= finish_call_expr (decl
, &args
, false, true,
4504 tf_warning_or_error
);
4505 release_tree_vector (args
);
4508 release_tree_vector (args
);
4510 /* Look for a template function with typename parameter CharT
4511 and parameter pack CharT... Call the function with
4512 template parameter characters representing the string. */
4513 args
= make_tree_vector ();
4514 decl
= lookup_literal_operator (name
, args
);
4515 if (decl
&& decl
!= error_mark_node
)
4517 tree tmpl_args
= make_string_pack (value
);
4518 decl
= lookup_template_function (decl
, tmpl_args
);
4519 result
= finish_call_expr (decl
, &args
, false, true,
4520 tf_warning_or_error
);
4521 release_tree_vector (args
);
4524 release_tree_vector (args
);
4526 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527 name
, TREE_TYPE (value
), size_type_node
);
4528 return error_mark_node
;
4532 /* Basic concepts [gram.basic] */
4534 /* Parse a translation-unit.
4537 declaration-seq [opt]
4539 Returns TRUE if all went well. */
4542 cp_parser_translation_unit (cp_parser
* parser
)
4544 /* The address of the first non-permanent object on the declarator
4546 static void *declarator_obstack_base
;
4550 /* Create the declarator obstack, if necessary. */
4551 if (!cp_error_declarator
)
4553 gcc_obstack_init (&declarator_obstack
);
4554 /* Create the error declarator. */
4555 cp_error_declarator
= make_declarator (cdk_error
);
4556 /* Create the empty parameter list. */
4557 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4559 /* Remember where the base of the declarator obstack lies. */
4560 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4563 cp_parser_declaration_seq_opt (parser
);
4565 /* If there are no tokens left then all went well. */
4566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4568 /* Get rid of the token array; we don't need it any more. */
4569 cp_lexer_destroy (parser
->lexer
);
4570 parser
->lexer
= NULL
;
4572 /* This file might have been a context that's implicitly extern
4573 "C". If so, pop the lang context. (Only relevant for PCH.) */
4574 if (parser
->implicit_extern_c
)
4576 pop_lang_context ();
4577 parser
->implicit_extern_c
= false;
4581 finish_translation_unit ();
4587 cp_parser_error (parser
, "expected declaration");
4591 /* Make sure the declarator obstack was fully cleaned up. */
4592 gcc_assert (obstack_next_free (&declarator_obstack
)
4593 == declarator_obstack_base
);
4595 /* All went well. */
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600 decltype context. */
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p
)
4605 tsubst_flags_t complain
= tf_warning_or_error
;
4607 complain
|= tf_decltype
;
4611 /* We're about to parse a collection of statements. If we're currently
4612 parsing tentatively, set up a firewall so that any nested
4613 cp_parser_commit_to_tentative_parse won't affect the current context. */
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4618 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4621 cp_parser_parse_tentatively (parser
);
4622 cp_parser_commit_to_topmost_tentative_parse (parser
);
4623 return cp_lexer_token_position (parser
->lexer
, false);
4626 /* We've finished parsing the collection of statements. Wrap up the
4627 firewall and replace the relevant tokens with the parsed form. */
4630 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4636 /* Finish the firewall level. */
4637 cp_parser_parse_definitely (parser
);
4638 /* And remember the result of the parse for when we try again. */
4639 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4640 token
->type
= CPP_PREPARSED_EXPR
;
4641 token
->u
.value
= expr
;
4642 token
->keyword
= RID_MAX
;
4643 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4646 /* Like the above functions, but let the user modify the tokens. Used by
4647 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648 later parses, so it makes sense to localize the effects of
4649 cp_parser_commit_to_tentative_parse. */
4651 struct tentative_firewall
4656 tentative_firewall (cp_parser
*p
): parser(p
)
4658 /* If we're currently parsing tentatively, start a committed level as a
4659 firewall and then an inner tentative parse. */
4660 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4662 cp_parser_parse_tentatively (parser
);
4663 cp_parser_commit_to_topmost_tentative_parse (parser
);
4664 cp_parser_parse_tentatively (parser
);
4668 ~tentative_firewall()
4672 /* Finish the inner tentative parse and the firewall, propagating any
4673 uncommitted error state to the outer tentative parse. */
4674 bool err
= cp_parser_error_occurred (parser
);
4675 cp_parser_parse_definitely (parser
);
4676 cp_parser_parse_definitely (parser
);
4678 cp_parser_simulate_error (parser
);
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684 This class is for tracking such a matching pair of symbols.
4685 In particular, it tracks the location of the first token,
4686 so that if the second token is missing, we can highlight the
4687 location of the first token when notifying the user about the
4690 template <typename traits_t
>
4694 /* token_pair's ctor. */
4695 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4697 /* If the next token is the opening symbol for this pair, consume it and
4699 Otherwise, issue an error and return false.
4700 In either case, record the location of the opening token. */
4702 bool require_open (cp_parser
*parser
)
4704 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4705 return cp_parser_require (parser
, traits_t::open_token_type
,
4706 traits_t::required_token_open
);
4709 /* Consume the next token from PARSER, recording its location as
4710 that of the opening token within the pair. */
4712 cp_token
* consume_open (cp_parser
*parser
)
4714 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4715 gcc_assert (tok
->type
== traits_t::open_token_type
);
4716 m_open_loc
= tok
->location
;
4720 /* If the next token is the closing symbol for this pair, consume it
4722 Otherwise, issue an error, highlighting the location of the
4723 corresponding opening token, and return NULL. */
4725 cp_token
*require_close (cp_parser
*parser
) const
4727 return cp_parser_require (parser
, traits_t::close_token_type
,
4728 traits_t::required_token_close
,
4733 location_t m_open_loc
;
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4738 struct matching_paren_traits
4740 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4741 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4742 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4743 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747 pairs of parentheses. */
4749 typedef token_pair
<matching_paren_traits
> matching_parens
;
4751 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4753 struct matching_brace_traits
4755 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4756 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4757 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4758 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4764 typedef token_pair
<matching_brace_traits
> matching_braces
;
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768 enclosing parentheses. */
4771 cp_parser_statement_expr (cp_parser
*parser
)
4773 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4775 /* Consume the '('. */
4776 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4777 matching_parens parens
;
4778 parens
.consume_open (parser
);
4779 /* Start the statement-expression. */
4780 tree expr
= begin_stmt_expr ();
4781 /* Parse the compound-statement. */
4782 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4784 expr
= finish_stmt_expr (expr
, false);
4785 /* Consume the ')'. */
4786 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4787 if (!parens
.require_close (parser
))
4788 cp_parser_skip_to_end_of_statement (parser
);
4790 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4791 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4792 return cp_expr (expr
, combined_loc
);
4795 /* Expressions [gram.expr] */
4797 /* Parse a fold-operator.
4800 - * / % ^ & | = < > << >>
4801 = -= *= /= %= ^= &= |= <<= >>=
4802 == != <= >= && || , .* ->*
4804 This returns the tree code corresponding to the matched operator
4805 as an int. When the current token matches a compound assignment
4806 opertor, the resulting tree code is the negative value of the
4807 non-assignment operator. */
4810 cp_parser_fold_operator (cp_token
*token
)
4812 switch (token
->type
)
4814 case CPP_PLUS
: return PLUS_EXPR
;
4815 case CPP_MINUS
: return MINUS_EXPR
;
4816 case CPP_MULT
: return MULT_EXPR
;
4817 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4818 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4819 case CPP_XOR
: return BIT_XOR_EXPR
;
4820 case CPP_AND
: return BIT_AND_EXPR
;
4821 case CPP_OR
: return BIT_IOR_EXPR
;
4822 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4823 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4825 case CPP_EQ
: return -NOP_EXPR
;
4826 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4827 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4828 case CPP_MULT_EQ
: return -MULT_EXPR
;
4829 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4830 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4831 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4832 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4833 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4834 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4835 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4837 case CPP_EQ_EQ
: return EQ_EXPR
;
4838 case CPP_NOT_EQ
: return NE_EXPR
;
4839 case CPP_LESS
: return LT_EXPR
;
4840 case CPP_GREATER
: return GT_EXPR
;
4841 case CPP_LESS_EQ
: return LE_EXPR
;
4842 case CPP_GREATER_EQ
: return GE_EXPR
;
4844 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4845 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4847 case CPP_COMMA
: return COMPOUND_EXPR
;
4849 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4850 case CPP_DEREF_STAR
: return MEMBER_REF
;
4852 default: return ERROR_MARK
;
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857 the LHS of a fold-expression. More codes will need to be added to use this
4858 function in other contexts. */
4861 is_binary_op (tree_code code
)
4866 case POINTER_PLUS_EXPR
:
4869 case TRUNC_DIV_EXPR
:
4870 case TRUNC_MOD_EXPR
:
4886 case TRUTH_ANDIF_EXPR
:
4887 case TRUTH_ORIF_EXPR
:
4900 /* If the next token is a suitable fold operator, consume it and return as
4901 the function above. */
4904 cp_parser_fold_operator (cp_parser
*parser
)
4906 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4907 int code
= cp_parser_fold_operator (token
);
4908 if (code
!= ERROR_MARK
)
4909 cp_lexer_consume_token (parser
->lexer
);
4913 /* Parse a fold-expression.
4916 ( ... folding-operator cast-expression)
4917 ( cast-expression folding-operator ... )
4918 ( cast-expression folding operator ... folding-operator cast-expression)
4920 Note that the '(' and ')' are matched in primary expression. */
4923 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4930 cp_lexer_consume_token (parser
->lexer
);
4931 int op
= cp_parser_fold_operator (parser
);
4932 if (op
== ERROR_MARK
)
4934 cp_parser_error (parser
, "expected binary operator");
4935 return error_mark_node
;
4938 tree expr
= cp_parser_cast_expression (parser
, false, false,
4940 if (expr
== error_mark_node
)
4941 return error_mark_node
;
4942 return finish_left_unary_fold_expr (expr
, op
);
4945 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4946 int op
= cp_parser_fold_operator (parser
);
4947 if (op
== ERROR_MARK
)
4949 cp_parser_error (parser
, "expected binary operator");
4950 return error_mark_node
;
4953 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4955 cp_parser_error (parser
, "expected ...");
4956 return error_mark_node
;
4958 cp_lexer_consume_token (parser
->lexer
);
4960 /* The operands of a fold-expression are cast-expressions, so binary or
4961 conditional expressions are not allowed. We check this here to avoid
4962 tentative parsing. */
4963 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4964 /* OK, the expression was parenthesized. */;
4965 else if (is_binary_op (TREE_CODE (expr1
)))
4966 error_at (location_of (expr1
),
4967 "binary expression in operand of fold-expression");
4968 else if (TREE_CODE (expr1
) == COND_EXPR
4969 || (REFERENCE_REF_P (expr1
)
4970 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
4971 error_at (location_of (expr1
),
4972 "conditional expression in operand of fold-expression");
4975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4976 return finish_right_unary_fold_expr (expr1
, op
);
4978 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4980 cp_parser_error (parser
, "mismatched operator in fold-expression");
4981 return error_mark_node
;
4983 cp_lexer_consume_token (parser
->lexer
);
4985 // Binary left or right fold.
4986 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4987 if (expr2
== error_mark_node
)
4988 return error_mark_node
;
4989 return finish_binary_fold_expr (expr1
, expr2
, op
);
4992 /* Parse a primary-expression.
4999 lambda-expression (C++11)
5004 ( compound-statement )
5005 __builtin_va_arg ( assignment-expression , type-id )
5006 __builtin_offsetof ( type-id , offsetof-expression )
5009 __has_nothrow_assign ( type-id )
5010 __has_nothrow_constructor ( type-id )
5011 __has_nothrow_copy ( type-id )
5012 __has_trivial_assign ( type-id )
5013 __has_trivial_constructor ( type-id )
5014 __has_trivial_copy ( type-id )
5015 __has_trivial_destructor ( type-id )
5016 __has_virtual_destructor ( type-id )
5017 __is_abstract ( type-id )
5018 __is_base_of ( type-id , type-id )
5019 __is_class ( type-id )
5020 __is_empty ( type-id )
5021 __is_enum ( type-id )
5022 __is_final ( type-id )
5023 __is_literal_type ( type-id )
5024 __is_pod ( type-id )
5025 __is_polymorphic ( type-id )
5026 __is_std_layout ( type-id )
5027 __is_trivial ( type-id )
5028 __is_union ( type-id )
5030 Objective-C++ Extension:
5038 ADDRESS_P is true iff this expression was immediately preceded by
5039 "&" and therefore might denote a pointer-to-member. CAST_P is true
5040 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5041 true iff this expression is a template argument.
5043 Returns a representation of the expression. Upon return, *IDK
5044 indicates what kind of id-expression (if any) was present. */
5047 cp_parser_primary_expression (cp_parser
*parser
,
5050 bool template_arg_p
,
5054 cp_token
*token
= NULL
;
5056 /* Assume the primary expression is not an id-expression. */
5057 *idk
= CP_ID_KIND_NONE
;
5059 /* Peek at the next token. */
5060 token
= cp_lexer_peek_token (parser
->lexer
);
5061 switch ((int) token
->type
)
5070 user-defined-literal */
5077 case CPP_PREPARSED_EXPR
:
5078 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5079 return cp_parser_userdef_numeric_literal (parser
);
5080 token
= cp_lexer_consume_token (parser
->lexer
);
5081 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5083 error_at (token
->location
,
5084 "fixed-point types not supported in C++");
5085 return error_mark_node
;
5087 /* Floating-point literals are only allowed in an integral
5088 constant expression if they are cast to an integral or
5089 enumeration type. */
5090 if (TREE_CODE (token
->u
.value
) == REAL_CST
5091 && parser
->integral_constant_expression_p
5094 /* CAST_P will be set even in invalid code like "int(2.7 +
5095 ...)". Therefore, we have to check that the next token
5096 is sure to end the cast. */
5099 cp_token
*next_token
;
5101 next_token
= cp_lexer_peek_token (parser
->lexer
);
5102 if (/* The comma at the end of an
5103 enumerator-definition. */
5104 next_token
->type
!= CPP_COMMA
5105 /* The curly brace at the end of an enum-specifier. */
5106 && next_token
->type
!= CPP_CLOSE_BRACE
5107 /* The end of a statement. */
5108 && next_token
->type
!= CPP_SEMICOLON
5109 /* The end of the cast-expression. */
5110 && next_token
->type
!= CPP_CLOSE_PAREN
5111 /* The end of an array bound. */
5112 && next_token
->type
!= CPP_CLOSE_SQUARE
5113 /* The closing ">" in a template-argument-list. */
5114 && (next_token
->type
!= CPP_GREATER
5115 || parser
->greater_than_is_operator_p
)
5116 /* C++0x only: A ">>" treated like two ">" tokens,
5117 in a template-argument-list. */
5118 && (next_token
->type
!= CPP_RSHIFT
5119 || (cxx_dialect
== cxx98
)
5120 || parser
->greater_than_is_operator_p
))
5124 /* If we are within a cast, then the constraint that the
5125 cast is to an integral or enumeration type will be
5126 checked at that point. If we are not within a cast, then
5127 this code is invalid. */
5129 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5131 return cp_expr (token
->u
.value
, token
->location
);
5133 case CPP_CHAR_USERDEF
:
5134 case CPP_CHAR16_USERDEF
:
5135 case CPP_CHAR32_USERDEF
:
5136 case CPP_WCHAR_USERDEF
:
5137 case CPP_UTF8CHAR_USERDEF
:
5138 return cp_parser_userdef_char_literal (parser
);
5144 case CPP_UTF8STRING
:
5145 case CPP_STRING_USERDEF
:
5146 case CPP_STRING16_USERDEF
:
5147 case CPP_STRING32_USERDEF
:
5148 case CPP_WSTRING_USERDEF
:
5149 case CPP_UTF8STRING_USERDEF
:
5150 /* ??? Should wide strings be allowed when parser->translate_strings_p
5151 is false (i.e. in attributes)? If not, we can kill the third
5152 argument to cp_parser_string_literal. */
5153 return cp_parser_string_literal (parser
,
5154 parser
->translate_strings_p
,
5157 case CPP_OPEN_PAREN
:
5158 /* If we see `( { ' then we are looking at the beginning of
5159 a GNU statement-expression. */
5160 if (cp_parser_allow_gnu_extensions_p (parser
)
5161 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5163 /* Statement-expressions are not allowed by the standard. */
5164 pedwarn (token
->location
, OPT_Wpedantic
,
5165 "ISO C++ forbids braced-groups within expressions");
5167 /* And they're not allowed outside of a function-body; you
5168 cannot, for example, write:
5170 int i = ({ int j = 3; j + 1; });
5172 at class or namespace scope. */
5173 if (!parser
->in_function_body
5174 || parser
->in_template_argument_list_p
)
5176 error_at (token
->location
,
5177 "statement-expressions are not allowed outside "
5178 "functions nor in template-argument lists");
5179 cp_parser_skip_to_end_of_block_or_statement (parser
);
5180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5181 cp_lexer_consume_token (parser
->lexer
);
5182 return error_mark_node
;
5185 return cp_parser_statement_expr (parser
);
5187 /* Otherwise it's a normal parenthesized expression. */
5190 bool saved_greater_than_is_operator_p
;
5192 location_t open_paren_loc
= token
->location
;
5194 /* Consume the `('. */
5195 matching_parens parens
;
5196 parens
.consume_open (parser
);
5197 /* Within a parenthesized expression, a `>' token is always
5198 the greater-than operator. */
5199 saved_greater_than_is_operator_p
5200 = parser
->greater_than_is_operator_p
;
5201 parser
->greater_than_is_operator_p
= true;
5203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5204 /* Left fold expression. */
5207 /* Parse the parenthesized expression. */
5208 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5210 token
= cp_lexer_peek_token (parser
->lexer
);
5211 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5213 expr
= cp_parser_fold_expression (parser
, expr
);
5214 if (expr
!= error_mark_node
5215 && cxx_dialect
< cxx17
5216 && !in_system_header_at (input_location
))
5217 pedwarn (input_location
, 0, "fold-expressions only available "
5218 "with -std=c++17 or -std=gnu++17");
5221 /* Let the front end know that this expression was
5222 enclosed in parentheses. This matters in case, for
5223 example, the expression is of the form `A::B', since
5224 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5226 expr
= finish_parenthesized_expr (expr
);
5228 /* DR 705: Wrapping an unqualified name in parentheses
5229 suppresses arg-dependent lookup. We want to pass back
5230 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231 (c++/37862), but none of the others. */
5232 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5233 *idk
= CP_ID_KIND_NONE
;
5235 /* The `>' token might be the end of a template-id or
5236 template-parameter-list now. */
5237 parser
->greater_than_is_operator_p
5238 = saved_greater_than_is_operator_p
;
5240 /* Consume the `)'. */
5241 token
= cp_lexer_peek_token (parser
->lexer
);
5242 location_t close_paren_loc
= token
->location
;
5243 expr
.set_range (open_paren_loc
, close_paren_loc
);
5244 if (!parens
.require_close (parser
)
5245 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5246 cp_parser_skip_to_end_of_statement (parser
);
5251 case CPP_OPEN_SQUARE
:
5253 if (c_dialect_objc ())
5255 /* We might have an Objective-C++ message. */
5256 cp_parser_parse_tentatively (parser
);
5257 tree msg
= cp_parser_objc_message_expression (parser
);
5258 /* If that works out, we're done ... */
5259 if (cp_parser_parse_definitely (parser
))
5261 /* ... else, fall though to see if it's a lambda. */
5263 cp_expr lam
= cp_parser_lambda_expression (parser
);
5264 /* Don't warn about a failed tentative parse. */
5265 if (cp_parser_error_occurred (parser
))
5266 return error_mark_node
;
5267 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5271 case CPP_OBJC_STRING
:
5272 if (c_dialect_objc ())
5273 /* We have an Objective-C++ string literal. */
5274 return cp_parser_objc_expression (parser
);
5275 cp_parser_error (parser
, "expected primary-expression");
5276 return error_mark_node
;
5279 switch (token
->keyword
)
5281 /* These two are the boolean literals. */
5283 cp_lexer_consume_token (parser
->lexer
);
5284 return cp_expr (boolean_true_node
, token
->location
);
5286 cp_lexer_consume_token (parser
->lexer
);
5287 return cp_expr (boolean_false_node
, token
->location
);
5289 /* The `__null' literal. */
5291 cp_lexer_consume_token (parser
->lexer
);
5292 return cp_expr (null_node
, token
->location
);
5294 /* The `nullptr' literal. */
5296 cp_lexer_consume_token (parser
->lexer
);
5297 return cp_expr (nullptr_node
, token
->location
);
5299 /* Recognize the `this' keyword. */
5301 cp_lexer_consume_token (parser
->lexer
);
5302 if (parser
->local_variables_forbidden_p
)
5304 error_at (token
->location
,
5305 "%<this%> may not be used in this context");
5306 return error_mark_node
;
5308 /* Pointers cannot appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5310 return error_mark_node
;
5311 return cp_expr (finish_this_expr (), token
->location
);
5313 /* The `operator' keyword can be the beginning of an
5318 case RID_FUNCTION_NAME
:
5319 case RID_PRETTY_FUNCTION_NAME
:
5320 case RID_C99_FUNCTION_NAME
:
5322 non_integral_constant name
;
5324 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325 __func__ are the names of variables -- but they are
5326 treated specially. Therefore, they are handled here,
5327 rather than relying on the generic id-expression logic
5328 below. Grammatically, these names are id-expressions.
5330 Consume the token. */
5331 token
= cp_lexer_consume_token (parser
->lexer
);
5333 switch (token
->keyword
)
5335 case RID_FUNCTION_NAME
:
5336 name
= NIC_FUNC_NAME
;
5338 case RID_PRETTY_FUNCTION_NAME
:
5339 name
= NIC_PRETTY_FUNC
;
5341 case RID_C99_FUNCTION_NAME
:
5342 name
= NIC_C99_FUNC
;
5348 if (cp_parser_non_integral_constant_expression (parser
, name
))
5349 return error_mark_node
;
5351 /* Look up the name. */
5352 return finish_fname (token
->u
.value
);
5359 source_location type_location
;
5360 location_t start_loc
5361 = cp_lexer_peek_token (parser
->lexer
)->location
;
5362 /* The `__builtin_va_arg' construct is used to handle
5363 `va_arg'. Consume the `__builtin_va_arg' token. */
5364 cp_lexer_consume_token (parser
->lexer
);
5365 /* Look for the opening `('. */
5366 matching_parens parens
;
5367 parens
.require_open (parser
);
5368 /* Now, parse the assignment-expression. */
5369 expression
= cp_parser_assignment_expression (parser
);
5370 /* Look for the `,'. */
5371 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5372 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5373 /* Parse the type-id. */
5375 type_id_in_expr_sentinel
s (parser
);
5376 type
= cp_parser_type_id (parser
);
5378 /* Look for the closing `)'. */
5379 location_t finish_loc
5380 = cp_lexer_peek_token (parser
->lexer
)->location
;
5381 parens
.require_close (parser
);
5382 /* Using `va_arg' in a constant-expression is not
5384 if (cp_parser_non_integral_constant_expression (parser
,
5386 return error_mark_node
;
5387 /* Construct a location of the form:
5388 __builtin_va_arg (v, int)
5389 ~~~~~~~~~~~~~~~~~~~~~^~~~
5390 with the caret at the type, ranging from the start of the
5391 "__builtin_va_arg" token to the close paren. */
5392 location_t combined_loc
5393 = make_location (type_location
, start_loc
, finish_loc
);
5394 return build_x_va_arg (combined_loc
, expression
, type
);
5398 return cp_parser_builtin_offsetof (parser
);
5400 case RID_HAS_NOTHROW_ASSIGN
:
5401 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5402 case RID_HAS_NOTHROW_COPY
:
5403 case RID_HAS_TRIVIAL_ASSIGN
:
5404 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5405 case RID_HAS_TRIVIAL_COPY
:
5406 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5407 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5408 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5409 case RID_IS_ABSTRACT
:
5410 case RID_IS_AGGREGATE
:
5411 case RID_IS_BASE_OF
:
5416 case RID_IS_LITERAL_TYPE
:
5418 case RID_IS_POLYMORPHIC
:
5419 case RID_IS_SAME_AS
:
5420 case RID_IS_STD_LAYOUT
:
5421 case RID_IS_TRIVIAL
:
5422 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5423 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5424 case RID_IS_TRIVIALLY_COPYABLE
:
5426 case RID_IS_ASSIGNABLE
:
5427 case RID_IS_CONSTRUCTIBLE
:
5428 return cp_parser_trait_expr (parser
, token
->keyword
);
5432 return cp_parser_requires_expression (parser
);
5434 /* Objective-C++ expressions. */
5436 case RID_AT_PROTOCOL
:
5437 case RID_AT_SELECTOR
:
5438 return cp_parser_objc_expression (parser
);
5441 if (parser
->in_function_body
5442 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5445 error_at (token
->location
,
5446 "a template declaration cannot appear at block scope");
5447 cp_parser_skip_to_end_of_block_or_statement (parser
);
5448 return error_mark_node
;
5452 cp_parser_error (parser
, "expected primary-expression");
5453 return error_mark_node
;
5456 /* An id-expression can start with either an identifier, a
5457 `::' as the beginning of a qualified-id, or the "operator"
5461 case CPP_TEMPLATE_ID
:
5462 case CPP_NESTED_NAME_SPECIFIER
:
5465 cp_expr id_expression
;
5467 const char *error_msg
;
5470 cp_token
*id_expr_token
;
5472 /* Parse the id-expression. */
5474 = cp_parser_id_expression (parser
,
5475 /*template_keyword_p=*/false,
5476 /*check_dependency_p=*/true,
5478 /*declarator_p=*/false,
5479 /*optional_p=*/false);
5480 if (id_expression
== error_mark_node
)
5481 return error_mark_node
;
5482 id_expr_token
= token
;
5483 token
= cp_lexer_peek_token (parser
->lexer
);
5484 done
= (token
->type
!= CPP_OPEN_SQUARE
5485 && token
->type
!= CPP_OPEN_PAREN
5486 && token
->type
!= CPP_DOT
5487 && token
->type
!= CPP_DEREF
5488 && token
->type
!= CPP_PLUS_PLUS
5489 && token
->type
!= CPP_MINUS_MINUS
);
5490 /* If we have a template-id, then no further lookup is
5491 required. If the template-id was for a template-class, we
5492 will sometimes have a TYPE_DECL at this point. */
5493 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5494 || TREE_CODE (id_expression
) == TYPE_DECL
)
5495 decl
= id_expression
;
5496 /* Look up the name. */
5499 tree ambiguous_decls
;
5501 /* If we already know that this lookup is ambiguous, then
5502 we've already issued an error message; there's no reason
5504 if (id_expr_token
->type
== CPP_NAME
5505 && id_expr_token
->error_reported
)
5507 cp_parser_simulate_error (parser
);
5508 return error_mark_node
;
5511 decl
= cp_parser_lookup_name (parser
, id_expression
,
5514 /*is_namespace=*/false,
5515 /*check_dependency=*/true,
5517 id_expr_token
->location
);
5518 /* If the lookup was ambiguous, an error will already have
5520 if (ambiguous_decls
)
5521 return error_mark_node
;
5523 /* In Objective-C++, we may have an Objective-C 2.0
5524 dot-syntax for classes here. */
5525 if (c_dialect_objc ()
5526 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5527 && TREE_CODE (decl
) == TYPE_DECL
5528 && objc_is_class_name (decl
))
5531 cp_lexer_consume_token (parser
->lexer
);
5532 component
= cp_parser_identifier (parser
);
5533 if (component
== error_mark_node
)
5534 return error_mark_node
;
5536 tree result
= objc_build_class_component_ref (id_expression
,
5538 /* Build a location of the form:
5541 with caret at the start of the component name (at
5542 input_location), ranging from the start of the id_expression
5543 to the end of the component name. */
5544 location_t combined_loc
5545 = make_location (input_location
, id_expression
.get_start (),
5546 get_finish (input_location
));
5547 protected_set_expr_location (result
, combined_loc
);
5551 /* In Objective-C++, an instance variable (ivar) may be preferred
5552 to whatever cp_parser_lookup_name() found.
5553 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5554 rest of c-family, we have to do a little extra work to preserve
5555 any location information in cp_expr "decl". Given that
5556 objc_lookup_ivar is implemented in "c-family" and "objc", we
5557 have a trip through the pure "tree" type, rather than cp_expr.
5558 Naively copying it back to "decl" would implicitly give the
5559 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560 store an EXPR_LOCATION. Hence we only update "decl" (and
5561 hence its location_t) if we get back a different tree node. */
5562 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5564 if (decl_tree
!= decl
.get_value ())
5565 decl
= cp_expr (decl_tree
);
5567 /* If name lookup gives us a SCOPE_REF, then the
5568 qualifying scope was dependent. */
5569 if (TREE_CODE (decl
) == SCOPE_REF
)
5571 /* At this point, we do not know if DECL is a valid
5572 integral constant expression. We assume that it is
5573 in fact such an expression, so that code like:
5575 template <int N> struct A {
5579 is accepted. At template-instantiation time, we
5580 will check that B<N>::i is actually a constant. */
5583 /* Check to see if DECL is a local variable in a context
5584 where that is forbidden. */
5585 if (parser
->local_variables_forbidden_p
5586 && local_variable_p (decl
))
5588 error_at (id_expr_token
->location
,
5589 "local variable %qD may not appear in this context",
5591 return error_mark_node
;
5595 decl
= (finish_id_expression
5596 (id_expression
, decl
, parser
->scope
,
5598 parser
->integral_constant_expression_p
,
5599 parser
->allow_non_integral_constant_expression_p
,
5600 &parser
->non_integral_constant_expression_p
,
5601 template_p
, done
, address_p
,
5604 id_expression
.get_location ()));
5606 cp_parser_error (parser
, error_msg
);
5607 decl
.set_location (id_expr_token
->location
);
5611 /* Anything else is an error. */
5613 cp_parser_error (parser
, "expected primary-expression");
5614 return error_mark_node
;
5618 static inline cp_expr
5619 cp_parser_primary_expression (cp_parser
*parser
,
5622 bool template_arg_p
,
5625 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5626 /*decltype*/false, idk
);
5629 /* Parse an id-expression.
5636 :: [opt] nested-name-specifier template [opt] unqualified-id
5638 :: operator-function-id
5641 Return a representation of the unqualified portion of the
5642 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5643 a `::' or nested-name-specifier.
5645 Often, if the id-expression was a qualified-id, the caller will
5646 want to make a SCOPE_REF to represent the qualified-id. This
5647 function does not do this in order to avoid wastefully creating
5648 SCOPE_REFs when they are not required.
5650 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5653 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5654 uninstantiated templates.
5656 If *TEMPLATE_P is non-NULL, it is set to true iff the
5657 `template' keyword is used to explicitly indicate that the entity
5658 named is a template.
5660 If DECLARATOR_P is true, the id-expression is appearing as part of
5661 a declarator, rather than as part of an expression. */
5664 cp_parser_id_expression (cp_parser
*parser
,
5665 bool template_keyword_p
,
5666 bool check_dependency_p
,
5671 bool global_scope_p
;
5672 bool nested_name_specifier_p
;
5674 /* Assume the `template' keyword was not used. */
5676 *template_p
= template_keyword_p
;
5678 /* Look for the optional `::' operator. */
5680 = (!template_keyword_p
5681 && (cp_parser_global_scope_opt (parser
,
5682 /*current_scope_valid_p=*/false)
5685 /* Look for the optional nested-name-specifier. */
5686 nested_name_specifier_p
5687 = (cp_parser_nested_name_specifier_opt (parser
,
5688 /*typename_keyword_p=*/false,
5695 /* If there is a nested-name-specifier, then we are looking at
5696 the first qualified-id production. */
5697 if (nested_name_specifier_p
)
5700 tree saved_object_scope
;
5701 tree saved_qualifying_scope
;
5702 cp_expr unqualified_id
;
5705 /* See if the next token is the `template' keyword. */
5707 template_p
= &is_template
;
5708 *template_p
= cp_parser_optional_template_keyword (parser
);
5709 /* Name lookup we do during the processing of the
5710 unqualified-id might obliterate SCOPE. */
5711 saved_scope
= parser
->scope
;
5712 saved_object_scope
= parser
->object_scope
;
5713 saved_qualifying_scope
= parser
->qualifying_scope
;
5714 /* Process the final unqualified-id. */
5715 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5718 /*optional_p=*/false);
5719 /* Restore the SAVED_SCOPE for our caller. */
5720 parser
->scope
= saved_scope
;
5721 parser
->object_scope
= saved_object_scope
;
5722 parser
->qualifying_scope
= saved_qualifying_scope
;
5724 return unqualified_id
;
5726 /* Otherwise, if we are in global scope, then we are looking at one
5727 of the other qualified-id productions. */
5728 else if (global_scope_p
)
5733 /* Peek at the next token. */
5734 token
= cp_lexer_peek_token (parser
->lexer
);
5736 /* If it's an identifier, and the next token is not a "<", then
5737 we can avoid the template-id case. This is an optimization
5738 for this common case. */
5739 if (token
->type
== CPP_NAME
5740 && !cp_parser_nth_token_starts_template_argument_list_p
5742 return cp_parser_identifier (parser
);
5744 cp_parser_parse_tentatively (parser
);
5745 /* Try a template-id. */
5746 id
= cp_parser_template_id (parser
,
5747 /*template_keyword_p=*/false,
5748 /*check_dependency_p=*/true,
5751 /* If that worked, we're done. */
5752 if (cp_parser_parse_definitely (parser
))
5755 /* Peek at the next token. (Changes in the token buffer may
5756 have invalidated the pointer obtained above.) */
5757 token
= cp_lexer_peek_token (parser
->lexer
);
5759 switch (token
->type
)
5762 return cp_parser_identifier (parser
);
5765 if (token
->keyword
== RID_OPERATOR
)
5766 return cp_parser_operator_function_id (parser
);
5770 cp_parser_error (parser
, "expected id-expression");
5771 return error_mark_node
;
5775 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5776 /*check_dependency_p=*/true,
5781 /* Parse an unqualified-id.
5785 operator-function-id
5786 conversion-function-id
5790 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5791 keyword, in a construct like `A::template ...'.
5793 Returns a representation of unqualified-id. For the `identifier'
5794 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5795 production a BIT_NOT_EXPR is returned; the operand of the
5796 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5797 other productions, see the documentation accompanying the
5798 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5799 names are looked up in uninstantiated templates. If DECLARATOR_P
5800 is true, the unqualified-id is appearing as part of a declarator,
5801 rather than as part of an expression. */
5804 cp_parser_unqualified_id (cp_parser
* parser
,
5805 bool template_keyword_p
,
5806 bool check_dependency_p
,
5812 /* Peek at the next token. */
5813 token
= cp_lexer_peek_token (parser
->lexer
);
5815 switch ((int) token
->type
)
5821 /* We don't know yet whether or not this will be a
5823 cp_parser_parse_tentatively (parser
);
5824 /* Try a template-id. */
5825 id
= cp_parser_template_id (parser
, template_keyword_p
,
5829 /* If it worked, we're done. */
5830 if (cp_parser_parse_definitely (parser
))
5832 /* Otherwise, it's an ordinary identifier. */
5833 return cp_parser_identifier (parser
);
5836 case CPP_TEMPLATE_ID
:
5837 return cp_parser_template_id (parser
, template_keyword_p
,
5845 tree qualifying_scope
;
5850 /* Consume the `~' token. */
5851 cp_lexer_consume_token (parser
->lexer
);
5852 /* Parse the class-name. The standard, as written, seems to
5855 template <typename T> struct S { ~S (); };
5856 template <typename T> S<T>::~S() {}
5858 is invalid, since `~' must be followed by a class-name, but
5859 `S<T>' is dependent, and so not known to be a class.
5860 That's not right; we need to look in uninstantiated
5861 templates. A further complication arises from:
5863 template <typename T> void f(T t) {
5867 Here, it is not possible to look up `T' in the scope of `T'
5868 itself. We must look in both the current scope, and the
5869 scope of the containing complete expression.
5871 Yet another issue is:
5880 The standard does not seem to say that the `S' in `~S'
5881 should refer to the type `S' and not the data member
5884 /* DR 244 says that we look up the name after the "~" in the
5885 same scope as we looked up the qualifying name. That idea
5886 isn't fully worked out; it's more complicated than that. */
5887 scope
= parser
->scope
;
5888 object_scope
= parser
->object_scope
;
5889 qualifying_scope
= parser
->qualifying_scope
;
5891 /* Check for invalid scopes. */
5892 if (scope
== error_mark_node
)
5894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5895 cp_lexer_consume_token (parser
->lexer
);
5896 return error_mark_node
;
5898 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5900 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5901 error_at (token
->location
,
5902 "scope %qT before %<~%> is not a class-name",
5904 cp_parser_simulate_error (parser
);
5905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5906 cp_lexer_consume_token (parser
->lexer
);
5907 return error_mark_node
;
5909 gcc_assert (!scope
|| TYPE_P (scope
));
5911 /* If the name is of the form "X::~X" it's OK even if X is a
5913 token
= cp_lexer_peek_token (parser
->lexer
);
5915 && token
->type
== CPP_NAME
5916 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5918 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5919 || (CLASS_TYPE_P (scope
)
5920 && constructor_name_p (token
->u
.value
, scope
))))
5922 cp_lexer_consume_token (parser
->lexer
);
5923 return build_nt (BIT_NOT_EXPR
, scope
);
5926 /* ~auto means the destructor of whatever the object is. */
5927 if (cp_parser_is_keyword (token
, RID_AUTO
))
5929 if (cxx_dialect
< cxx14
)
5930 pedwarn (input_location
, 0,
5931 "%<~auto%> only available with "
5932 "-std=c++14 or -std=gnu++14");
5933 cp_lexer_consume_token (parser
->lexer
);
5934 return build_nt (BIT_NOT_EXPR
, make_auto ());
5937 /* If there was an explicit qualification (S::~T), first look
5938 in the scope given by the qualification (i.e., S).
5940 Note: in the calls to cp_parser_class_name below we pass
5941 typename_type so that lookup finds the injected-class-name
5942 rather than the constructor. */
5944 type_decl
= NULL_TREE
;
5947 cp_parser_parse_tentatively (parser
);
5948 type_decl
= cp_parser_class_name (parser
,
5949 /*typename_keyword_p=*/false,
5950 /*template_keyword_p=*/false,
5952 /*check_dependency=*/false,
5953 /*class_head_p=*/false,
5955 if (cp_parser_parse_definitely (parser
))
5958 /* In "N::S::~S", look in "N" as well. */
5959 if (!done
&& scope
&& qualifying_scope
)
5961 cp_parser_parse_tentatively (parser
);
5962 parser
->scope
= qualifying_scope
;
5963 parser
->object_scope
= NULL_TREE
;
5964 parser
->qualifying_scope
= NULL_TREE
;
5966 = cp_parser_class_name (parser
,
5967 /*typename_keyword_p=*/false,
5968 /*template_keyword_p=*/false,
5970 /*check_dependency=*/false,
5971 /*class_head_p=*/false,
5973 if (cp_parser_parse_definitely (parser
))
5976 /* In "p->S::~T", look in the scope given by "*p" as well. */
5977 else if (!done
&& object_scope
)
5979 cp_parser_parse_tentatively (parser
);
5980 parser
->scope
= object_scope
;
5981 parser
->object_scope
= NULL_TREE
;
5982 parser
->qualifying_scope
= NULL_TREE
;
5984 = cp_parser_class_name (parser
,
5985 /*typename_keyword_p=*/false,
5986 /*template_keyword_p=*/false,
5988 /*check_dependency=*/false,
5989 /*class_head_p=*/false,
5991 if (cp_parser_parse_definitely (parser
))
5994 /* Look in the surrounding context. */
5997 parser
->scope
= NULL_TREE
;
5998 parser
->object_scope
= NULL_TREE
;
5999 parser
->qualifying_scope
= NULL_TREE
;
6000 if (processing_template_decl
)
6001 cp_parser_parse_tentatively (parser
);
6003 = cp_parser_class_name (parser
,
6004 /*typename_keyword_p=*/false,
6005 /*template_keyword_p=*/false,
6007 /*check_dependency=*/false,
6008 /*class_head_p=*/false,
6010 if (processing_template_decl
6011 && ! cp_parser_parse_definitely (parser
))
6013 /* We couldn't find a type with this name. If we're parsing
6014 tentatively, fail and try something else. */
6015 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6017 cp_parser_simulate_error (parser
);
6018 return error_mark_node
;
6020 /* Otherwise, accept it and check for a match at instantiation
6022 type_decl
= cp_parser_identifier (parser
);
6023 if (type_decl
!= error_mark_node
)
6024 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6028 /* If an error occurred, assume that the name of the
6029 destructor is the same as the name of the qualifying
6030 class. That allows us to keep parsing after running
6031 into ill-formed destructor names. */
6032 if (type_decl
== error_mark_node
&& scope
)
6033 return build_nt (BIT_NOT_EXPR
, scope
);
6034 else if (type_decl
== error_mark_node
)
6035 return error_mark_node
;
6037 /* Check that destructor name and scope match. */
6038 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6040 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6041 error_at (token
->location
,
6042 "declaration of %<~%T%> as member of %qT",
6044 cp_parser_simulate_error (parser
);
6045 return error_mark_node
;
6050 A typedef-name that names a class shall not be used as the
6051 identifier in the declarator for a destructor declaration. */
6053 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6054 && !DECL_SELF_REFERENCE_P (type_decl
)
6055 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6056 error_at (token
->location
,
6057 "typedef-name %qD used as destructor declarator",
6060 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6064 if (token
->keyword
== RID_OPERATOR
)
6068 /* This could be a template-id, so we try that first. */
6069 cp_parser_parse_tentatively (parser
);
6070 /* Try a template-id. */
6071 id
= cp_parser_template_id (parser
, template_keyword_p
,
6072 /*check_dependency_p=*/true,
6075 /* If that worked, we're done. */
6076 if (cp_parser_parse_definitely (parser
))
6078 /* We still don't know whether we're looking at an
6079 operator-function-id or a conversion-function-id. */
6080 cp_parser_parse_tentatively (parser
);
6081 /* Try an operator-function-id. */
6082 id
= cp_parser_operator_function_id (parser
);
6083 /* If that didn't work, try a conversion-function-id. */
6084 if (!cp_parser_parse_definitely (parser
))
6085 id
= cp_parser_conversion_function_id (parser
);
6094 cp_parser_error (parser
, "expected unqualified-id");
6095 return error_mark_node
;
6099 /* Parse an (optional) nested-name-specifier.
6101 nested-name-specifier: [C++98]
6102 class-or-namespace-name :: nested-name-specifier [opt]
6103 class-or-namespace-name :: template nested-name-specifier [opt]
6105 nested-name-specifier: [C++0x]
6108 nested-name-specifier identifier ::
6109 nested-name-specifier template [opt] simple-template-id ::
6111 PARSER->SCOPE should be set appropriately before this function is
6112 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6113 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6116 Sets PARSER->SCOPE to the class (TYPE) or namespace
6117 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6118 it unchanged if there is no nested-name-specifier. Returns the new
6119 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6121 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6122 part of a declaration and/or decl-specifier. */
6125 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6126 bool typename_keyword_p
,
6127 bool check_dependency_p
,
6129 bool is_declaration
,
6130 bool template_keyword_p
/* = false */)
6132 bool success
= false;
6133 cp_token_position start
= 0;
6136 /* Remember where the nested-name-specifier starts. */
6137 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6139 start
= cp_lexer_token_position (parser
->lexer
, false);
6140 push_deferring_access_checks (dk_deferred
);
6147 tree saved_qualifying_scope
;
6149 /* Spot cases that cannot be the beginning of a
6150 nested-name-specifier. */
6151 token
= cp_lexer_peek_token (parser
->lexer
);
6153 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6154 the already parsed nested-name-specifier. */
6155 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6157 /* Grab the nested-name-specifier and continue the loop. */
6158 cp_parser_pre_parsed_nested_name_specifier (parser
);
6159 /* If we originally encountered this nested-name-specifier
6160 with IS_DECLARATION set to false, we will not have
6161 resolved TYPENAME_TYPEs, so we must do so here. */
6163 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6165 new_scope
= resolve_typename_type (parser
->scope
,
6166 /*only_current_p=*/false);
6167 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6168 parser
->scope
= new_scope
;
6174 /* Spot cases that cannot be the beginning of a
6175 nested-name-specifier. On the second and subsequent times
6176 through the loop, we look for the `template' keyword. */
6177 if (success
&& token
->keyword
== RID_TEMPLATE
)
6179 /* A template-id can start a nested-name-specifier. */
6180 else if (token
->type
== CPP_TEMPLATE_ID
)
6182 /* DR 743: decltype can be used in a nested-name-specifier. */
6183 else if (token_is_decltype (token
))
6187 /* If the next token is not an identifier, then it is
6188 definitely not a type-name or namespace-name. */
6189 if (token
->type
!= CPP_NAME
)
6191 /* If the following token is neither a `<' (to begin a
6192 template-id), nor a `::', then we are not looking at a
6193 nested-name-specifier. */
6194 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6196 if (token
->type
== CPP_COLON
6197 && parser
->colon_corrects_to_scope_p
6198 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6200 gcc_rich_location
richloc (token
->location
);
6201 richloc
.add_fixit_replace ("::");
6203 "found %<:%> in nested-name-specifier, "
6205 token
->type
= CPP_SCOPE
;
6208 if (token
->type
!= CPP_SCOPE
6209 && !cp_parser_nth_token_starts_template_argument_list_p
6214 /* The nested-name-specifier is optional, so we parse
6216 cp_parser_parse_tentatively (parser
);
6218 /* Look for the optional `template' keyword, if this isn't the
6219 first time through the loop. */
6221 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6223 /* Save the old scope since the name lookup we are about to do
6224 might destroy it. */
6225 old_scope
= parser
->scope
;
6226 saved_qualifying_scope
= parser
->qualifying_scope
;
6227 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6228 look up names in "X<T>::I" in order to determine that "Y" is
6229 a template. So, if we have a typename at this point, we make
6230 an effort to look through it. */
6232 && !typename_keyword_p
6234 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6235 parser
->scope
= resolve_typename_type (parser
->scope
,
6236 /*only_current_p=*/false);
6237 /* Parse the qualifying entity. */
6239 = cp_parser_qualifying_entity (parser
,
6245 /* Look for the `::' token. */
6246 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6248 /* If we found what we wanted, we keep going; otherwise, we're
6250 if (!cp_parser_parse_definitely (parser
))
6252 bool error_p
= false;
6254 /* Restore the OLD_SCOPE since it was valid before the
6255 failed attempt at finding the last
6256 class-or-namespace-name. */
6257 parser
->scope
= old_scope
;
6258 parser
->qualifying_scope
= saved_qualifying_scope
;
6260 /* If the next token is a decltype, and the one after that is a
6261 `::', then the decltype has failed to resolve to a class or
6262 enumeration type. Give this error even when parsing
6263 tentatively since it can't possibly be valid--and we're going
6264 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6265 won't get another chance.*/
6266 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6267 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6270 token
= cp_lexer_consume_token (parser
->lexer
);
6271 error_at (token
->location
, "decltype evaluates to %qT, "
6272 "which is not a class or enumeration type",
6273 token
->u
.tree_check_value
->value
);
6274 parser
->scope
= error_mark_node
;
6278 cp_lexer_consume_token (parser
->lexer
);
6281 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6282 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6284 /* If we have a non-type template-id followed by ::, it can't
6285 possibly be valid. */
6286 token
= cp_lexer_peek_token (parser
->lexer
);
6287 tree tid
= token
->u
.tree_check_value
->value
;
6288 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6289 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6291 tree tmpl
= NULL_TREE
;
6292 if (is_overloaded_fn (tid
))
6294 tree fns
= get_fns (tid
);
6295 if (OVL_SINGLE_P (fns
))
6296 tmpl
= OVL_FIRST (fns
);
6297 error_at (token
->location
, "function template-id %qD "
6298 "in nested-name-specifier", tid
);
6302 /* Variable template. */
6303 tmpl
= TREE_OPERAND (tid
, 0);
6304 gcc_assert (variable_template_p (tmpl
));
6305 error_at (token
->location
, "variable template-id %qD "
6306 "in nested-name-specifier", tid
);
6309 inform (DECL_SOURCE_LOCATION (tmpl
),
6310 "%qD declared here", tmpl
);
6312 parser
->scope
= error_mark_node
;
6316 cp_lexer_consume_token (parser
->lexer
);
6317 cp_lexer_consume_token (parser
->lexer
);
6321 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6323 /* If the next token is an identifier, and the one after
6324 that is a `::', then any valid interpretation would have
6325 found a class-or-namespace-name. */
6326 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6327 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6329 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6332 token
= cp_lexer_consume_token (parser
->lexer
);
6335 if (!token
->error_reported
)
6338 tree ambiguous_decls
;
6340 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6342 /*is_template=*/false,
6343 /*is_namespace=*/false,
6344 /*check_dependency=*/true,
6347 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6348 error_at (token
->location
,
6349 "%qD used without template parameters",
6351 else if (ambiguous_decls
)
6353 // cp_parser_lookup_name has the same diagnostic,
6354 // thus make sure to emit it at most once.
6355 if (cp_parser_uncommitted_to_tentative_parse_p
6358 error_at (token
->location
,
6359 "reference to %qD is ambiguous",
6361 print_candidates (ambiguous_decls
);
6363 decl
= error_mark_node
;
6367 if (cxx_dialect
!= cxx98
)
6368 cp_parser_name_lookup_error
6369 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6372 cp_parser_name_lookup_error
6373 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6377 parser
->scope
= error_mark_node
;
6379 /* Treat this as a successful nested-name-specifier
6384 If the name found is not a class-name (clause
6385 _class_) or namespace-name (_namespace.def_), the
6386 program is ill-formed. */
6389 cp_lexer_consume_token (parser
->lexer
);
6393 /* We've found one valid nested-name-specifier. */
6395 /* Name lookup always gives us a DECL. */
6396 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6397 new_scope
= TREE_TYPE (new_scope
);
6398 /* Uses of "template" must be followed by actual templates. */
6399 if (template_keyword_p
6400 && !(CLASS_TYPE_P (new_scope
)
6401 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6402 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6403 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6404 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6405 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6406 == TEMPLATE_ID_EXPR
)))
6407 permerror (input_location
, TYPE_P (new_scope
)
6408 ? G_("%qT is not a template")
6409 : G_("%qD is not a template"),
6411 /* If it is a class scope, try to complete it; we are about to
6412 be looking up names inside the class. */
6413 if (TYPE_P (new_scope
)
6414 /* Since checking types for dependency can be expensive,
6415 avoid doing it if the type is already complete. */
6416 && !COMPLETE_TYPE_P (new_scope
)
6417 /* Do not try to complete dependent types. */
6418 && !dependent_type_p (new_scope
))
6420 new_scope
= complete_type (new_scope
);
6421 /* If it is a typedef to current class, use the current
6422 class instead, as the typedef won't have any names inside
6424 if (!COMPLETE_TYPE_P (new_scope
)
6425 && currently_open_class (new_scope
))
6426 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6428 /* Make sure we look in the right scope the next time through
6430 parser
->scope
= new_scope
;
6433 /* If parsing tentatively, replace the sequence of tokens that makes
6434 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6435 token. That way, should we re-parse the token stream, we will
6436 not have to repeat the effort required to do the parse, nor will
6437 we issue duplicate error messages. */
6438 if (success
&& start
)
6442 token
= cp_lexer_token_at (parser
->lexer
, start
);
6443 /* Reset the contents of the START token. */
6444 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6445 /* Retrieve any deferred checks. Do not pop this access checks yet
6446 so the memory will not be reclaimed during token replacing below. */
6447 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6448 token
->u
.tree_check_value
->value
= parser
->scope
;
6449 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6450 token
->u
.tree_check_value
->qualifying_scope
=
6451 parser
->qualifying_scope
;
6452 token
->keyword
= RID_MAX
;
6454 /* Purge all subsequent tokens. */
6455 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6459 pop_to_parent_deferring_access_checks ();
6461 return success
? parser
->scope
: NULL_TREE
;
6464 /* Parse a nested-name-specifier. See
6465 cp_parser_nested_name_specifier_opt for details. This function
6466 behaves identically, except that it will an issue an error if no
6467 nested-name-specifier is present. */
6470 cp_parser_nested_name_specifier (cp_parser
*parser
,
6471 bool typename_keyword_p
,
6472 bool check_dependency_p
,
6474 bool is_declaration
)
6478 /* Look for the nested-name-specifier. */
6479 scope
= cp_parser_nested_name_specifier_opt (parser
,
6484 /* If it was not present, issue an error message. */
6487 cp_parser_error (parser
, "expected nested-name-specifier");
6488 parser
->scope
= NULL_TREE
;
6494 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6495 this is either a class-name or a namespace-name (which corresponds
6496 to the class-or-namespace-name production in the grammar). For
6497 C++0x, it can also be a type-name that refers to an enumeration
6498 type or a simple-template-id.
6500 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6501 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6502 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6503 TYPE_P is TRUE iff the next name should be taken as a class-name,
6504 even the same name is declared to be another entity in the same
6507 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6508 specified by the class-or-namespace-name. If neither is found the
6509 ERROR_MARK_NODE is returned. */
6512 cp_parser_qualifying_entity (cp_parser
*parser
,
6513 bool typename_keyword_p
,
6514 bool template_keyword_p
,
6515 bool check_dependency_p
,
6517 bool is_declaration
)
6520 tree saved_qualifying_scope
;
6521 tree saved_object_scope
;
6524 bool successful_parse_p
;
6526 /* DR 743: decltype can appear in a nested-name-specifier. */
6527 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6529 scope
= cp_parser_decltype (parser
);
6530 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6531 && !MAYBE_CLASS_TYPE_P (scope
))
6533 cp_parser_simulate_error (parser
);
6534 return error_mark_node
;
6536 if (TYPE_NAME (scope
))
6537 scope
= TYPE_NAME (scope
);
6541 /* Before we try to parse the class-name, we must save away the
6542 current PARSER->SCOPE since cp_parser_class_name will destroy
6544 saved_scope
= parser
->scope
;
6545 saved_qualifying_scope
= parser
->qualifying_scope
;
6546 saved_object_scope
= parser
->object_scope
;
6547 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6548 there is no need to look for a namespace-name. */
6549 only_class_p
= template_keyword_p
6550 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6552 cp_parser_parse_tentatively (parser
);
6553 scope
= cp_parser_class_name (parser
,
6556 type_p
? class_type
: none_type
,
6558 /*class_head_p=*/false,
6560 /*enum_ok=*/cxx_dialect
> cxx98
);
6561 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6562 /* If that didn't work, try for a namespace-name. */
6563 if (!only_class_p
&& !successful_parse_p
)
6565 /* Restore the saved scope. */
6566 parser
->scope
= saved_scope
;
6567 parser
->qualifying_scope
= saved_qualifying_scope
;
6568 parser
->object_scope
= saved_object_scope
;
6569 /* If we are not looking at an identifier followed by the scope
6570 resolution operator, then this is not part of a
6571 nested-name-specifier. (Note that this function is only used
6572 to parse the components of a nested-name-specifier.) */
6573 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6574 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6575 return error_mark_node
;
6576 scope
= cp_parser_namespace_name (parser
);
6582 /* Return true if we are looking at a compound-literal, false otherwise. */
6585 cp_parser_compound_literal_p (cp_parser
*parser
)
6587 cp_lexer_save_tokens (parser
->lexer
);
6589 /* Skip tokens until the next token is a closing parenthesis.
6590 If we find the closing `)', and the next token is a `{', then
6591 we are looking at a compound-literal. */
6592 bool compound_literal_p
6593 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6594 /*consume_paren=*/true)
6595 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6597 /* Roll back the tokens we skipped. */
6598 cp_lexer_rollback_tokens (parser
->lexer
);
6600 return compound_literal_p
;
6603 /* Return true if EXPR is the integer constant zero or a complex constant
6604 of zero, without any folding, but ignoring location wrappers. */
6607 literal_integer_zerop (const_tree expr
)
6609 STRIP_ANY_LOCATION_WRAPPER (expr
);
6610 return integer_zerop (expr
);
6613 /* Parse a postfix-expression.
6617 postfix-expression [ expression ]
6618 postfix-expression ( expression-list [opt] )
6619 simple-type-specifier ( expression-list [opt] )
6620 typename :: [opt] nested-name-specifier identifier
6621 ( expression-list [opt] )
6622 typename :: [opt] nested-name-specifier template [opt] template-id
6623 ( expression-list [opt] )
6624 postfix-expression . template [opt] id-expression
6625 postfix-expression -> template [opt] id-expression
6626 postfix-expression . pseudo-destructor-name
6627 postfix-expression -> pseudo-destructor-name
6628 postfix-expression ++
6629 postfix-expression --
6630 dynamic_cast < type-id > ( expression )
6631 static_cast < type-id > ( expression )
6632 reinterpret_cast < type-id > ( expression )
6633 const_cast < type-id > ( expression )
6634 typeid ( expression )
6640 ( type-id ) { initializer-list , [opt] }
6642 This extension is a GNU version of the C99 compound-literal
6643 construct. (The C99 grammar uses `type-name' instead of `type-id',
6644 but they are essentially the same concept.)
6646 If ADDRESS_P is true, the postfix expression is the operand of the
6647 `&' operator. CAST_P is true if this expression is the target of a
6650 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6651 class member access expressions [expr.ref].
6653 Returns a representation of the expression. */
6656 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6657 bool member_access_only_p
, bool decltype_p
,
6658 cp_id_kind
* pidk_return
)
6663 cp_id_kind idk
= CP_ID_KIND_NONE
;
6664 cp_expr postfix_expression
= NULL_TREE
;
6665 bool is_member_access
= false;
6667 /* Peek at the next token. */
6668 token
= cp_lexer_peek_token (parser
->lexer
);
6669 loc
= token
->location
;
6670 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6672 /* Some of the productions are determined by keywords. */
6673 keyword
= token
->keyword
;
6683 const char *saved_message
;
6684 bool saved_in_type_id_in_expr_p
;
6686 /* All of these can be handled in the same way from the point
6687 of view of parsing. Begin by consuming the token
6688 identifying the cast. */
6689 cp_lexer_consume_token (parser
->lexer
);
6691 /* New types cannot be defined in the cast. */
6692 saved_message
= parser
->type_definition_forbidden_message
;
6693 parser
->type_definition_forbidden_message
6694 = G_("types may not be defined in casts");
6696 /* Look for the opening `<'. */
6697 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6698 /* Parse the type to which we are casting. */
6699 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6700 parser
->in_type_id_in_expr_p
= true;
6701 type
= cp_parser_type_id (parser
);
6702 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6703 /* Look for the closing `>'. */
6704 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6705 /* Restore the old message. */
6706 parser
->type_definition_forbidden_message
= saved_message
;
6708 bool saved_greater_than_is_operator_p
6709 = parser
->greater_than_is_operator_p
;
6710 parser
->greater_than_is_operator_p
= true;
6712 /* And the expression which is being cast. */
6713 matching_parens parens
;
6714 parens
.require_open (parser
);
6715 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6716 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6718 location_t end_loc
= close_paren
?
6719 close_paren
->location
: UNKNOWN_LOCATION
;
6721 parser
->greater_than_is_operator_p
6722 = saved_greater_than_is_operator_p
;
6724 /* Only type conversions to integral or enumeration types
6725 can be used in constant-expressions. */
6726 if (!cast_valid_in_integral_constant_expression_p (type
)
6727 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6729 postfix_expression
= error_mark_node
;
6737 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6741 = build_static_cast (type
, expression
, tf_warning_or_error
);
6745 = build_reinterpret_cast (type
, expression
,
6746 tf_warning_or_error
);
6750 = build_const_cast (type
, expression
, tf_warning_or_error
);
6756 /* Construct a location e.g. :
6757 reinterpret_cast <int *> (expr)
6758 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6759 ranging from the start of the "*_cast" token to the final closing
6760 paren, with the caret at the start. */
6761 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6762 postfix_expression
.set_location (cp_cast_loc
);
6769 const char *saved_message
;
6770 bool saved_in_type_id_in_expr_p
;
6772 /* Consume the `typeid' token. */
6773 cp_lexer_consume_token (parser
->lexer
);
6774 /* Look for the `(' token. */
6775 matching_parens parens
;
6776 parens
.require_open (parser
);
6777 /* Types cannot be defined in a `typeid' expression. */
6778 saved_message
= parser
->type_definition_forbidden_message
;
6779 parser
->type_definition_forbidden_message
6780 = G_("types may not be defined in a %<typeid%> expression");
6781 /* We can't be sure yet whether we're looking at a type-id or an
6783 cp_parser_parse_tentatively (parser
);
6784 /* Try a type-id first. */
6785 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6786 parser
->in_type_id_in_expr_p
= true;
6787 type
= cp_parser_type_id (parser
);
6788 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6789 /* Look for the `)' token. Otherwise, we can't be sure that
6790 we're not looking at an expression: consider `typeid (int
6791 (3))', for example. */
6792 cp_token
*close_paren
= parens
.require_close (parser
);
6793 /* If all went well, simply lookup the type-id. */
6794 if (cp_parser_parse_definitely (parser
))
6795 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6796 /* Otherwise, fall back to the expression variant. */
6801 /* Look for an expression. */
6802 expression
= cp_parser_expression (parser
, & idk
);
6803 /* Compute its typeid. */
6804 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6805 /* Look for the `)' token. */
6806 close_paren
= parens
.require_close (parser
);
6808 /* Restore the saved message. */
6809 parser
->type_definition_forbidden_message
= saved_message
;
6810 /* `typeid' may not appear in an integral constant expression. */
6811 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6812 postfix_expression
= error_mark_node
;
6814 /* Construct a location e.g. :
6817 ranging from the start of the "typeid" token to the final closing
6818 paren, with the caret at the start. */
6821 location_t typeid_loc
6822 = make_location (start_loc
, start_loc
, close_paren
->location
);
6823 postfix_expression
.set_location (typeid_loc
);
6824 postfix_expression
.maybe_add_location_wrapper ();
6832 /* The syntax permitted here is the same permitted for an
6833 elaborated-type-specifier. */
6834 ++parser
->prevent_constrained_type_specifiers
;
6835 type
= cp_parser_elaborated_type_specifier (parser
,
6836 /*is_friend=*/false,
6837 /*is_declaration=*/false);
6838 --parser
->prevent_constrained_type_specifiers
;
6839 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6844 case RID_BUILTIN_SHUFFLE
:
6845 case RID_BUILTIN_LAUNDER
:
6847 vec
<tree
, va_gc
> *vec
;
6851 cp_lexer_consume_token (parser
->lexer
);
6852 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6853 /*cast_p=*/false, /*allow_expansion_p=*/true,
6854 /*non_constant_p=*/NULL
);
6857 postfix_expression
= error_mark_node
;
6861 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6867 if (vec
->length () == 1)
6869 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6872 error_at (loc
, "wrong number of arguments to "
6873 "%<__builtin_addressof%>");
6874 postfix_expression
= error_mark_node
;
6878 case RID_BUILTIN_LAUNDER
:
6879 if (vec
->length () == 1)
6880 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6881 tf_warning_or_error
);
6884 error_at (loc
, "wrong number of arguments to "
6885 "%<__builtin_launder%>");
6886 postfix_expression
= error_mark_node
;
6890 case RID_BUILTIN_SHUFFLE
:
6891 if (vec
->length () == 2)
6893 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6894 (*vec
)[1], tf_warning_or_error
);
6895 else if (vec
->length () == 3)
6897 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6898 (*vec
)[2], tf_warning_or_error
);
6901 error_at (loc
, "wrong number of arguments to "
6902 "%<__builtin_shuffle%>");
6903 postfix_expression
= error_mark_node
;
6917 /* If the next thing is a simple-type-specifier, we may be
6918 looking at a functional cast. We could also be looking at
6919 an id-expression. So, we try the functional cast, and if
6920 that doesn't work we fall back to the primary-expression. */
6921 cp_parser_parse_tentatively (parser
);
6922 /* Look for the simple-type-specifier. */
6923 ++parser
->prevent_constrained_type_specifiers
;
6924 type
= cp_parser_simple_type_specifier (parser
,
6925 /*decl_specs=*/NULL
,
6926 CP_PARSER_FLAGS_NONE
);
6927 --parser
->prevent_constrained_type_specifiers
;
6928 /* Parse the cast itself. */
6929 if (!cp_parser_error_occurred (parser
))
6931 = cp_parser_functional_cast (parser
, type
);
6932 /* If that worked, we're done. */
6933 if (cp_parser_parse_definitely (parser
))
6936 /* If the functional-cast didn't work out, try a
6937 compound-literal. */
6938 if (cp_parser_allow_gnu_extensions_p (parser
)
6939 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6941 cp_expr initializer
= NULL_TREE
;
6943 cp_parser_parse_tentatively (parser
);
6945 matching_parens parens
;
6946 parens
.consume_open (parser
);
6948 /* Avoid calling cp_parser_type_id pointlessly, see comment
6949 in cp_parser_cast_expression about c++/29234. */
6950 if (!cp_parser_compound_literal_p (parser
))
6951 cp_parser_simulate_error (parser
);
6954 /* Parse the type. */
6955 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6956 parser
->in_type_id_in_expr_p
= true;
6957 type
= cp_parser_type_id (parser
);
6958 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6959 parens
.require_close (parser
);
6962 /* If things aren't going well, there's no need to
6964 if (!cp_parser_error_occurred (parser
))
6966 bool non_constant_p
;
6967 /* Parse the brace-enclosed initializer list. */
6968 initializer
= cp_parser_braced_list (parser
,
6971 /* If that worked, we're definitely looking at a
6972 compound-literal expression. */
6973 if (cp_parser_parse_definitely (parser
))
6975 /* Warn the user that a compound literal is not
6976 allowed in standard C++. */
6977 pedwarn (input_location
, OPT_Wpedantic
,
6978 "ISO C++ forbids compound-literals");
6979 /* For simplicity, we disallow compound literals in
6980 constant-expressions. We could
6981 allow compound literals of integer type, whose
6982 initializer was a constant, in constant
6983 expressions. Permitting that usage, as a further
6984 extension, would not change the meaning of any
6985 currently accepted programs. (Of course, as
6986 compound literals are not part of ISO C++, the
6987 standard has nothing to say.) */
6988 if (cp_parser_non_integral_constant_expression (parser
,
6991 postfix_expression
= error_mark_node
;
6994 /* Form the representation of the compound-literal. */
6996 = finish_compound_literal (type
, initializer
,
6997 tf_warning_or_error
, fcl_c99
);
6998 postfix_expression
.set_location (initializer
.get_location ());
7003 /* It must be a primary-expression. */
7005 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7006 /*template_arg_p=*/false,
7013 /* Note that we don't need to worry about calling build_cplus_new on a
7014 class-valued CALL_EXPR in decltype when it isn't the end of the
7015 postfix-expression; unary_complex_lvalue will take care of that for
7018 /* Keep looping until the postfix-expression is complete. */
7021 if (idk
== CP_ID_KIND_UNQUALIFIED
7022 && identifier_p (postfix_expression
)
7023 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7024 /* It is not a Koenig lookup function call. */
7026 = unqualified_name_lookup_error (postfix_expression
);
7028 /* Peek at the next token. */
7029 token
= cp_lexer_peek_token (parser
->lexer
);
7031 switch (token
->type
)
7033 case CPP_OPEN_SQUARE
:
7034 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7036 cp_parser_error (parser
,
7037 "two consecutive %<[%> shall "
7038 "only introduce an attribute");
7039 return error_mark_node
;
7042 = cp_parser_postfix_open_square_expression (parser
,
7046 postfix_expression
.set_range (start_loc
,
7047 postfix_expression
.get_location ());
7049 idk
= CP_ID_KIND_NONE
;
7050 is_member_access
= false;
7053 case CPP_OPEN_PAREN
:
7054 /* postfix-expression ( expression-list [opt] ) */
7057 bool is_builtin_constant_p
;
7058 bool saved_integral_constant_expression_p
= false;
7059 bool saved_non_integral_constant_expression_p
= false;
7060 tsubst_flags_t complain
= complain_flags (decltype_p
);
7061 vec
<tree
, va_gc
> *args
;
7062 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7064 is_member_access
= false;
7066 is_builtin_constant_p
7067 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7068 if (is_builtin_constant_p
)
7070 /* The whole point of __builtin_constant_p is to allow
7071 non-constant expressions to appear as arguments. */
7072 saved_integral_constant_expression_p
7073 = parser
->integral_constant_expression_p
;
7074 saved_non_integral_constant_expression_p
7075 = parser
->non_integral_constant_expression_p
;
7076 parser
->integral_constant_expression_p
= false;
7078 args
= (cp_parser_parenthesized_expression_list
7080 /*cast_p=*/false, /*allow_expansion_p=*/true,
7081 /*non_constant_p=*/NULL
,
7082 /*close_paren_loc=*/&close_paren_loc
,
7083 /*wrap_locations_p=*/true));
7084 if (is_builtin_constant_p
)
7086 parser
->integral_constant_expression_p
7087 = saved_integral_constant_expression_p
;
7088 parser
->non_integral_constant_expression_p
7089 = saved_non_integral_constant_expression_p
;
7094 postfix_expression
= error_mark_node
;
7098 /* Function calls are not permitted in
7099 constant-expressions. */
7100 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7101 && cp_parser_non_integral_constant_expression (parser
,
7104 postfix_expression
= error_mark_node
;
7105 release_tree_vector (args
);
7110 if (idk
== CP_ID_KIND_UNQUALIFIED
7111 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7113 if (identifier_p (postfix_expression
))
7115 if (!args
->is_empty ())
7118 if (!any_type_dependent_arguments_p (args
))
7120 = perform_koenig_lookup (postfix_expression
, args
,
7125 = unqualified_fn_lookup_error (postfix_expression
);
7127 /* We do not perform argument-dependent lookup if
7128 normal lookup finds a non-function, in accordance
7129 with the expected resolution of DR 218. */
7130 else if (!args
->is_empty ()
7131 && is_overloaded_fn (postfix_expression
))
7133 tree fn
= get_first_fn (postfix_expression
);
7134 fn
= STRIP_TEMPLATE (fn
);
7136 /* Do not do argument dependent lookup if regular
7137 lookup finds a member function or a block-scope
7138 function declaration. [basic.lookup.argdep]/3 */
7139 if (!DECL_FUNCTION_MEMBER_P (fn
)
7140 && !DECL_LOCAL_FUNCTION_P (fn
))
7143 if (!any_type_dependent_arguments_p (args
))
7145 = perform_koenig_lookup (postfix_expression
, args
,
7151 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7152 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7153 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7154 && vec_safe_length (args
) == 3)
7156 tree arg0
= (*args
)[0];
7157 tree arg1
= (*args
)[1];
7158 tree arg2
= (*args
)[2];
7159 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7160 | (literal_integer_zerop (arg2
) << 2));
7161 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7164 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7166 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7167 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7169 if (processing_template_decl
7170 && (type_dependent_object_expression_p (instance
)
7171 || (!BASELINK_P (fn
)
7172 && TREE_CODE (fn
) != FIELD_DECL
)
7173 || type_dependent_expression_p (fn
)
7174 || any_type_dependent_arguments_p (args
)))
7176 maybe_generic_this_capture (instance
, fn
);
7178 = build_min_nt_call_vec (postfix_expression
, args
);
7179 release_tree_vector (args
);
7183 if (BASELINK_P (fn
))
7186 = (build_new_method_call
7187 (instance
, fn
, &args
, NULL_TREE
,
7188 (idk
== CP_ID_KIND_QUALIFIED
7189 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7196 = finish_call_expr (postfix_expression
, &args
,
7197 /*disallow_virtual=*/false,
7201 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7202 || TREE_CODE (postfix_expression
) == MEMBER_REF
7203 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7204 postfix_expression
= (build_offset_ref_call_from_tree
7205 (postfix_expression
, &args
,
7207 else if (idk
== CP_ID_KIND_QUALIFIED
)
7208 /* A call to a static class member, or a namespace-scope
7211 = finish_call_expr (postfix_expression
, &args
,
7212 /*disallow_virtual=*/true,
7216 /* All other function calls. */
7218 = finish_call_expr (postfix_expression
, &args
,
7219 /*disallow_virtual=*/false,
7223 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7225 location_t combined_loc
= make_location (token
->location
,
7228 postfix_expression
.set_location (combined_loc
);
7231 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7232 idk
= CP_ID_KIND_NONE
;
7234 release_tree_vector (args
);
7240 /* postfix-expression . template [opt] id-expression
7241 postfix-expression . pseudo-destructor-name
7242 postfix-expression -> template [opt] id-expression
7243 postfix-expression -> pseudo-destructor-name */
7245 /* Consume the `.' or `->' operator. */
7246 cp_lexer_consume_token (parser
->lexer
);
7249 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7253 is_member_access
= true;
7257 /* postfix-expression ++ */
7258 /* Consume the `++' token. */
7259 cp_lexer_consume_token (parser
->lexer
);
7260 /* Generate a representation for the complete expression. */
7262 = finish_increment_expr (postfix_expression
,
7263 POSTINCREMENT_EXPR
);
7264 /* Increments may not appear in constant-expressions. */
7265 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7266 postfix_expression
= error_mark_node
;
7267 idk
= CP_ID_KIND_NONE
;
7268 is_member_access
= false;
7271 case CPP_MINUS_MINUS
:
7272 /* postfix-expression -- */
7273 /* Consume the `--' token. */
7274 cp_lexer_consume_token (parser
->lexer
);
7275 /* Generate a representation for the complete expression. */
7277 = finish_increment_expr (postfix_expression
,
7278 POSTDECREMENT_EXPR
);
7279 /* Decrements may not appear in constant-expressions. */
7280 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7281 postfix_expression
= error_mark_node
;
7282 idk
= CP_ID_KIND_NONE
;
7283 is_member_access
= false;
7287 if (pidk_return
!= NULL
)
7288 * pidk_return
= idk
;
7289 if (member_access_only_p
)
7290 return is_member_access
7291 ? postfix_expression
7292 : cp_expr (error_mark_node
);
7294 return postfix_expression
;
7298 /* We should never get here. */
7300 return error_mark_node
;
7303 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7304 by cp_parser_builtin_offsetof. We're looking for
7306 postfix-expression [ expression ]
7307 postfix-expression [ braced-init-list ] (C++11)
7309 FOR_OFFSETOF is set if we're being called in that context, which
7310 changes how we deal with integer constant expressions. */
7313 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7314 tree postfix_expression
,
7318 tree index
= NULL_TREE
;
7319 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7320 bool saved_greater_than_is_operator_p
;
7322 /* Consume the `[' token. */
7323 cp_lexer_consume_token (parser
->lexer
);
7325 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7326 parser
->greater_than_is_operator_p
= true;
7328 /* Parse the index expression. */
7329 /* ??? For offsetof, there is a question of what to allow here. If
7330 offsetof is not being used in an integral constant expression context,
7331 then we *could* get the right answer by computing the value at runtime.
7332 If we are in an integral constant expression context, then we might
7333 could accept any constant expression; hard to say without analysis.
7334 Rather than open the barn door too wide right away, allow only integer
7335 constant expressions here. */
7337 index
= cp_parser_constant_expression (parser
);
7340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7342 bool expr_nonconst_p
;
7343 cp_lexer_set_source_position (parser
->lexer
);
7344 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7345 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7348 index
= cp_parser_expression (parser
);
7351 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7353 /* Look for the closing `]'. */
7354 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7356 /* Build the ARRAY_REF. */
7357 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7360 /* When not doing offsetof, array references are not permitted in
7361 constant-expressions. */
7363 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7364 postfix_expression
= error_mark_node
;
7366 return postfix_expression
;
7369 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7370 dereference of incomplete type, returns true if error_mark_node should
7371 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7372 and *DEPENDENT_P. */
7375 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7378 /* In a template, be permissive by treating an object expression
7379 of incomplete type as dependent (after a pedwarn). */
7380 diagnostic_t kind
= (processing_template_decl
7381 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7383 switch (TREE_CODE (*postfix_expression
))
7386 case REINTERPRET_CAST_EXPR
:
7387 case CONST_CAST_EXPR
:
7388 case STATIC_CAST_EXPR
:
7389 case DYNAMIC_CAST_EXPR
:
7390 case IMPLICIT_CONV_EXPR
:
7391 case VIEW_CONVERT_EXPR
:
7392 case NON_LVALUE_EXPR
:
7396 /* Don't emit any diagnostic for OVERLOADs. */
7400 /* Avoid clobbering e.g. DECLs. */
7401 if (!EXPR_P (*postfix_expression
))
7406 if (kind
== DK_IGNORED
)
7409 location_t exploc
= location_of (*postfix_expression
);
7410 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7411 if (!MAYBE_CLASS_TYPE_P (*scope
))
7413 if (kind
== DK_ERROR
)
7414 *scope
= *postfix_expression
= error_mark_node
;
7415 else if (processing_template_decl
)
7417 *dependent_p
= true;
7418 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7423 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7424 by cp_parser_builtin_offsetof. We're looking for
7426 postfix-expression . template [opt] id-expression
7427 postfix-expression . pseudo-destructor-name
7428 postfix-expression -> template [opt] id-expression
7429 postfix-expression -> pseudo-destructor-name
7431 FOR_OFFSETOF is set if we're being called in that context. That sorta
7432 limits what of the above we'll actually accept, but nevermind.
7433 TOKEN_TYPE is the "." or "->" token, which will already have been
7434 removed from the stream. */
7437 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7438 enum cpp_ttype token_type
,
7439 cp_expr postfix_expression
,
7440 bool for_offsetof
, cp_id_kind
*idk
,
7441 location_t location
)
7445 bool pseudo_destructor_p
;
7446 tree scope
= NULL_TREE
;
7447 location_t start_loc
= postfix_expression
.get_start ();
7449 /* If this is a `->' operator, dereference the pointer. */
7450 if (token_type
== CPP_DEREF
)
7451 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7452 tf_warning_or_error
);
7453 /* Check to see whether or not the expression is type-dependent and
7454 not the current instantiation. */
7455 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7456 /* The identifier following the `->' or `.' is not qualified. */
7457 parser
->scope
= NULL_TREE
;
7458 parser
->qualifying_scope
= NULL_TREE
;
7459 parser
->object_scope
= NULL_TREE
;
7460 *idk
= CP_ID_KIND_NONE
;
7462 /* Enter the scope corresponding to the type of the object
7463 given by the POSTFIX_EXPRESSION. */
7466 scope
= TREE_TYPE (postfix_expression
);
7467 /* According to the standard, no expression should ever have
7468 reference type. Unfortunately, we do not currently match
7469 the standard in this respect in that our internal representation
7470 of an expression may have reference type even when the standard
7471 says it does not. Therefore, we have to manually obtain the
7472 underlying type here. */
7473 scope
= non_reference (scope
);
7474 /* The type of the POSTFIX_EXPRESSION must be complete. */
7475 /* Unlike the object expression in other contexts, *this is not
7476 required to be of complete type for purposes of class member
7477 access (5.2.5) outside the member function body. */
7478 if (postfix_expression
!= current_class_ref
7479 && scope
!= error_mark_node
7480 && !(processing_template_decl
7481 && current_class_type
7482 && (same_type_ignoring_top_level_qualifiers_p
7483 (scope
, current_class_type
))))
7485 scope
= complete_type (scope
);
7486 if (!COMPLETE_TYPE_P (scope
)
7487 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7489 return error_mark_node
;
7494 /* Let the name lookup machinery know that we are processing a
7495 class member access expression. */
7496 parser
->context
->object_type
= scope
;
7497 /* If something went wrong, we want to be able to discern that case,
7498 as opposed to the case where there was no SCOPE due to the type
7499 of expression being dependent. */
7501 scope
= error_mark_node
;
7502 /* If the SCOPE was erroneous, make the various semantic analysis
7503 functions exit quickly -- and without issuing additional error
7505 if (scope
== error_mark_node
)
7506 postfix_expression
= error_mark_node
;
7511 /* Tell cp_parser_lookup_name that there was an object, even though it's
7513 parser
->context
->object_type
= unknown_type_node
;
7515 /* Assume this expression is not a pseudo-destructor access. */
7516 pseudo_destructor_p
= false;
7518 /* If the SCOPE is a scalar type, then, if this is a valid program,
7519 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7520 is type dependent, it can be pseudo-destructor-name or something else.
7521 Try to parse it as pseudo-destructor-name first. */
7522 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7527 cp_parser_parse_tentatively (parser
);
7528 /* Parse the pseudo-destructor-name. */
7530 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7533 && (cp_parser_error_occurred (parser
)
7534 || !SCALAR_TYPE_P (type
)))
7535 cp_parser_abort_tentative_parse (parser
);
7536 else if (cp_parser_parse_definitely (parser
))
7538 pseudo_destructor_p
= true;
7540 = finish_pseudo_destructor_expr (postfix_expression
,
7545 if (!pseudo_destructor_p
)
7547 /* If the SCOPE is not a scalar type, we are looking at an
7548 ordinary class member access expression, rather than a
7549 pseudo-destructor-name. */
7551 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7552 /* Parse the id-expression. */
7553 name
= (cp_parser_id_expression
7555 cp_parser_optional_template_keyword (parser
),
7556 /*check_dependency_p=*/true,
7558 /*declarator_p=*/false,
7559 /*optional_p=*/false));
7560 /* In general, build a SCOPE_REF if the member name is qualified.
7561 However, if the name was not dependent and has already been
7562 resolved; there is no need to build the SCOPE_REF. For example;
7564 struct X { void f(); };
7565 template <typename T> void f(T* t) { t->X::f(); }
7567 Even though "t" is dependent, "X::f" is not and has been resolved
7568 to a BASELINK; there is no need to include scope information. */
7570 /* But we do need to remember that there was an explicit scope for
7571 virtual function calls. */
7573 *idk
= CP_ID_KIND_QUALIFIED
;
7575 /* If the name is a template-id that names a type, we will get a
7576 TYPE_DECL here. That is invalid code. */
7577 if (TREE_CODE (name
) == TYPE_DECL
)
7579 error_at (token
->location
, "invalid use of %qD", name
);
7580 postfix_expression
= error_mark_node
;
7584 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7586 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7588 error_at (token
->location
, "%<%D::%D%> is not a class member",
7589 parser
->scope
, name
);
7590 postfix_expression
= error_mark_node
;
7593 name
= build_qualified_name (/*type=*/NULL_TREE
,
7597 parser
->scope
= NULL_TREE
;
7598 parser
->qualifying_scope
= NULL_TREE
;
7599 parser
->object_scope
= NULL_TREE
;
7601 if (parser
->scope
&& name
&& BASELINK_P (name
))
7602 adjust_result_of_qualified_name_lookup
7603 (name
, parser
->scope
, scope
);
7605 = finish_class_member_access_expr (postfix_expression
, name
,
7607 tf_warning_or_error
);
7608 /* Build a location e.g.:
7611 where the caret is at the deref token, ranging from
7612 the start of postfix_expression to the end of the access expr. */
7614 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7615 location_t combined_loc
7616 = make_location (input_location
, start_loc
, end_loc
);
7617 protected_set_expr_location (postfix_expression
, combined_loc
);
7621 /* We no longer need to look up names in the scope of the object on
7622 the left-hand side of the `.' or `->' operator. */
7623 parser
->context
->object_type
= NULL_TREE
;
7625 /* Outside of offsetof, these operators may not appear in
7626 constant-expressions. */
7628 && (cp_parser_non_integral_constant_expression
7629 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7630 postfix_expression
= error_mark_node
;
7632 return postfix_expression
;
7635 /* Parse a parenthesized expression-list.
7638 assignment-expression
7639 expression-list, assignment-expression
7644 identifier, expression-list
7646 CAST_P is true if this expression is the target of a cast.
7648 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7651 WRAP_LOCATIONS_P is true if expressions within this list for which
7652 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7653 their source locations.
7655 Returns a vector of trees. Each element is a representation of an
7656 assignment-expression. NULL is returned if the ( and or ) are
7657 missing. An empty, but allocated, vector is returned on no
7658 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7659 if we are parsing an attribute list for an attribute that wants a
7660 plain identifier argument, normal_attr for an attribute that wants
7661 an expression, or non_attr if we aren't parsing an attribute list. If
7662 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7663 not all of the expressions in the list were constant.
7664 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7665 will be written to with the location of the closing parenthesis. If
7666 an error occurs, it may or may not be written to. */
7668 static vec
<tree
, va_gc
> *
7669 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7670 int is_attribute_list
,
7672 bool allow_expansion_p
,
7673 bool *non_constant_p
,
7674 location_t
*close_paren_loc
,
7675 bool wrap_locations_p
)
7677 vec
<tree
, va_gc
> *expression_list
;
7678 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7679 tree identifier
= NULL_TREE
;
7680 bool saved_greater_than_is_operator_p
;
7682 /* Assume all the expressions will be constant. */
7684 *non_constant_p
= false;
7686 matching_parens parens
;
7687 if (!parens
.require_open (parser
))
7690 expression_list
= make_tree_vector ();
7692 /* Within a parenthesized expression, a `>' token is always
7693 the greater-than operator. */
7694 saved_greater_than_is_operator_p
7695 = parser
->greater_than_is_operator_p
;
7696 parser
->greater_than_is_operator_p
= true;
7698 cp_expr
expr (NULL_TREE
);
7700 /* Consume expressions until there are no more. */
7701 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7704 /* At the beginning of attribute lists, check to see if the
7705 next token is an identifier. */
7706 if (is_attribute_list
== id_attr
7707 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7711 /* Consume the identifier. */
7712 token
= cp_lexer_consume_token (parser
->lexer
);
7713 /* Save the identifier. */
7714 identifier
= token
->u
.value
;
7718 bool expr_non_constant_p
;
7720 /* Parse the next assignment-expression. */
7721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7723 /* A braced-init-list. */
7724 cp_lexer_set_source_position (parser
->lexer
);
7725 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7726 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7727 if (non_constant_p
&& expr_non_constant_p
)
7728 *non_constant_p
= true;
7730 else if (non_constant_p
)
7732 expr
= (cp_parser_constant_expression
7733 (parser
, /*allow_non_constant_p=*/true,
7734 &expr_non_constant_p
));
7735 if (expr_non_constant_p
)
7736 *non_constant_p
= true;
7739 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7743 expr
= instantiate_non_dependent_expr (expr
);
7745 /* If we have an ellipsis, then this is an expression
7747 if (allow_expansion_p
7748 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7750 /* Consume the `...'. */
7751 cp_lexer_consume_token (parser
->lexer
);
7753 /* Build the argument pack. */
7754 expr
= make_pack_expansion (expr
);
7757 if (wrap_locations_p
)
7758 expr
.maybe_add_location_wrapper ();
7760 /* Add it to the list. We add error_mark_node
7761 expressions to the list, so that we can still tell if
7762 the correct form for a parenthesized expression-list
7763 is found. That gives better errors. */
7764 vec_safe_push (expression_list
, expr
.get_value ());
7766 if (expr
== error_mark_node
)
7770 /* After the first item, attribute lists look the same as
7771 expression lists. */
7772 is_attribute_list
= non_attr
;
7775 /* If the next token isn't a `,', then we are done. */
7776 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7779 /* Otherwise, consume the `,' and keep going. */
7780 cp_lexer_consume_token (parser
->lexer
);
7783 if (close_paren_loc
)
7784 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7786 if (!parens
.require_close (parser
))
7791 /* We try and resync to an unnested comma, as that will give the
7792 user better diagnostics. */
7793 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7794 /*recovering=*/true,
7796 /*consume_paren=*/true);
7801 parser
->greater_than_is_operator_p
7802 = saved_greater_than_is_operator_p
;
7807 parser
->greater_than_is_operator_p
7808 = saved_greater_than_is_operator_p
;
7811 vec_safe_insert (expression_list
, 0, identifier
);
7813 return expression_list
;
7816 /* Parse a pseudo-destructor-name.
7818 pseudo-destructor-name:
7819 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7820 :: [opt] nested-name-specifier template template-id :: ~ type-name
7821 :: [opt] nested-name-specifier [opt] ~ type-name
7823 If either of the first two productions is used, sets *SCOPE to the
7824 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7825 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7826 or ERROR_MARK_NODE if the parse fails. */
7829 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7834 bool nested_name_specifier_p
;
7837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7838 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7839 && !type_dependent_expression_p (object
))
7841 if (cxx_dialect
< cxx14
)
7842 pedwarn (input_location
, 0,
7843 "%<~auto%> only available with "
7844 "-std=c++14 or -std=gnu++14");
7845 cp_lexer_consume_token (parser
->lexer
);
7846 cp_lexer_consume_token (parser
->lexer
);
7848 *type
= TREE_TYPE (object
);
7852 /* Assume that things will not work out. */
7853 *type
= error_mark_node
;
7855 /* Look for the optional `::' operator. */
7856 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7857 /* Look for the optional nested-name-specifier. */
7858 nested_name_specifier_p
7859 = (cp_parser_nested_name_specifier_opt (parser
,
7860 /*typename_keyword_p=*/false,
7861 /*check_dependency_p=*/true,
7863 /*is_declaration=*/false)
7865 /* Now, if we saw a nested-name-specifier, we might be doing the
7866 second production. */
7867 if (nested_name_specifier_p
7868 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7870 /* Consume the `template' keyword. */
7871 cp_lexer_consume_token (parser
->lexer
);
7872 /* Parse the template-id. */
7873 cp_parser_template_id (parser
,
7874 /*template_keyword_p=*/true,
7875 /*check_dependency_p=*/false,
7877 /*is_declaration=*/true);
7878 /* Look for the `::' token. */
7879 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7881 /* If the next token is not a `~', then there might be some
7882 additional qualification. */
7883 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7885 /* At this point, we're looking for "type-name :: ~". The type-name
7886 must not be a class-name, since this is a pseudo-destructor. So,
7887 it must be either an enum-name, or a typedef-name -- both of which
7888 are just identifiers. So, we peek ahead to check that the "::"
7889 and "~" tokens are present; if they are not, then we can avoid
7890 calling type_name. */
7891 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7892 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7893 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7895 cp_parser_error (parser
, "non-scalar type");
7899 /* Look for the type-name. */
7900 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7901 if (*scope
== error_mark_node
)
7904 /* Look for the `::' token. */
7905 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7910 /* Look for the `~'. */
7911 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7913 /* Once we see the ~, this has to be a pseudo-destructor. */
7914 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7915 cp_parser_commit_to_topmost_tentative_parse (parser
);
7917 /* Look for the type-name again. We are not responsible for
7918 checking that it matches the first type-name. */
7919 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7922 /* Parse a unary-expression.
7928 unary-operator cast-expression
7929 sizeof unary-expression
7931 alignof ( type-id ) [C++0x]
7938 __extension__ cast-expression
7939 __alignof__ unary-expression
7940 __alignof__ ( type-id )
7941 alignof unary-expression [C++0x]
7942 __real__ cast-expression
7943 __imag__ cast-expression
7945 sizeof ( type-id ) { initializer-list , [opt] }
7946 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7947 __alignof__ ( type-id ) { initializer-list , [opt] }
7949 ADDRESS_P is true iff the unary-expression is appearing as the
7950 operand of the `&' operator. CAST_P is true if this expression is
7951 the target of a cast.
7953 Returns a representation of the expression. */
7956 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7957 bool address_p
, bool cast_p
, bool decltype_p
)
7960 enum tree_code unary_operator
;
7962 /* Peek at the next token. */
7963 token
= cp_lexer_peek_token (parser
->lexer
);
7964 /* Some keywords give away the kind of expression. */
7965 if (token
->type
== CPP_KEYWORD
)
7967 enum rid keyword
= token
->keyword
;
7976 location_t start_loc
= token
->location
;
7978 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7979 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
7981 /* Consume the token. */
7982 cp_lexer_consume_token (parser
->lexer
);
7983 /* Parse the operand. */
7984 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7986 if (TYPE_P (operand
))
7987 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
7991 /* ISO C++ defines alignof only with types, not with
7992 expressions. So pedwarn if alignof is used with a non-
7993 type expression. However, __alignof__ is ok. */
7995 pedwarn (token
->location
, OPT_Wpedantic
,
7996 "ISO C++ does not allow %<alignof%> "
7999 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8001 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8002 SIZEOF_EXPR with the original operand. */
8003 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8005 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8007 if (!processing_template_decl
&& TYPE_P (operand
))
8009 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8010 build1 (NOP_EXPR
, operand
,
8012 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8015 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8016 TREE_SIDE_EFFECTS (ret
) = 0;
8017 TREE_READONLY (ret
) = 1;
8021 /* Construct a location e.g. :
8024 with start == caret at the start of the "alignof"/"sizeof"
8025 token, with the endpoint at the final closing paren. */
8026 location_t finish_loc
8027 = cp_lexer_previous_token (parser
->lexer
)->location
;
8028 location_t compound_loc
8029 = make_location (start_loc
, start_loc
, finish_loc
);
8031 cp_expr
ret_expr (ret
);
8032 ret_expr
.set_location (compound_loc
);
8033 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8038 return cp_parser_new_expression (parser
);
8041 return cp_parser_delete_expression (parser
);
8045 /* The saved value of the PEDANTIC flag. */
8049 /* Save away the PEDANTIC flag. */
8050 cp_parser_extension_opt (parser
, &saved_pedantic
);
8051 /* Parse the cast-expression. */
8052 expr
= cp_parser_simple_cast_expression (parser
);
8053 /* Restore the PEDANTIC flag. */
8054 pedantic
= saved_pedantic
;
8064 /* Consume the `__real__' or `__imag__' token. */
8065 cp_lexer_consume_token (parser
->lexer
);
8066 /* Parse the cast-expression. */
8067 expression
= cp_parser_simple_cast_expression (parser
);
8068 /* Create the complete representation. */
8069 return build_x_unary_op (token
->location
,
8070 (keyword
== RID_REALPART
8071 ? REALPART_EXPR
: IMAGPART_EXPR
),
8073 tf_warning_or_error
);
8077 case RID_TRANSACTION_ATOMIC
:
8078 case RID_TRANSACTION_RELAXED
:
8079 return cp_parser_transaction_expression (parser
, keyword
);
8084 const char *saved_message
;
8085 bool saved_integral_constant_expression_p
;
8086 bool saved_non_integral_constant_expression_p
;
8087 bool saved_greater_than_is_operator_p
;
8089 location_t start_loc
= token
->location
;
8091 cp_lexer_consume_token (parser
->lexer
);
8092 matching_parens parens
;
8093 parens
.require_open (parser
);
8095 saved_message
= parser
->type_definition_forbidden_message
;
8096 parser
->type_definition_forbidden_message
8097 = G_("types may not be defined in %<noexcept%> expressions");
8099 saved_integral_constant_expression_p
8100 = parser
->integral_constant_expression_p
;
8101 saved_non_integral_constant_expression_p
8102 = parser
->non_integral_constant_expression_p
;
8103 parser
->integral_constant_expression_p
= false;
8105 saved_greater_than_is_operator_p
8106 = parser
->greater_than_is_operator_p
;
8107 parser
->greater_than_is_operator_p
= true;
8109 ++cp_unevaluated_operand
;
8110 ++c_inhibit_evaluation_warnings
;
8111 ++cp_noexcept_operand
;
8112 expr
= cp_parser_expression (parser
);
8113 --cp_noexcept_operand
;
8114 --c_inhibit_evaluation_warnings
;
8115 --cp_unevaluated_operand
;
8117 parser
->greater_than_is_operator_p
8118 = saved_greater_than_is_operator_p
;
8120 parser
->integral_constant_expression_p
8121 = saved_integral_constant_expression_p
;
8122 parser
->non_integral_constant_expression_p
8123 = saved_non_integral_constant_expression_p
;
8125 parser
->type_definition_forbidden_message
= saved_message
;
8127 location_t finish_loc
8128 = cp_lexer_peek_token (parser
->lexer
)->location
;
8129 parens
.require_close (parser
);
8131 /* Construct a location of the form:
8134 with start == caret, finishing at the close-paren. */
8135 location_t noexcept_loc
8136 = make_location (start_loc
, start_loc
, finish_loc
);
8138 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8147 /* Look for the `:: new' and `:: delete', which also signal the
8148 beginning of a new-expression, or delete-expression,
8149 respectively. If the next token is `::', then it might be one of
8151 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8155 /* See if the token after the `::' is one of the keywords in
8156 which we're interested. */
8157 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8158 /* If it's `new', we have a new-expression. */
8159 if (keyword
== RID_NEW
)
8160 return cp_parser_new_expression (parser
);
8161 /* Similarly, for `delete'. */
8162 else if (keyword
== RID_DELETE
)
8163 return cp_parser_delete_expression (parser
);
8166 /* Look for a unary operator. */
8167 unary_operator
= cp_parser_unary_operator (token
);
8168 /* The `++' and `--' operators can be handled similarly, even though
8169 they are not technically unary-operators in the grammar. */
8170 if (unary_operator
== ERROR_MARK
)
8172 if (token
->type
== CPP_PLUS_PLUS
)
8173 unary_operator
= PREINCREMENT_EXPR
;
8174 else if (token
->type
== CPP_MINUS_MINUS
)
8175 unary_operator
= PREDECREMENT_EXPR
;
8176 /* Handle the GNU address-of-label extension. */
8177 else if (cp_parser_allow_gnu_extensions_p (parser
)
8178 && token
->type
== CPP_AND_AND
)
8182 location_t start_loc
= token
->location
;
8184 /* Consume the '&&' token. */
8185 cp_lexer_consume_token (parser
->lexer
);
8186 /* Look for the identifier. */
8187 location_t finish_loc
8188 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8189 identifier
= cp_parser_identifier (parser
);
8190 /* Construct a location of the form:
8193 with caret==start at the "&&", finish at the end of the label. */
8194 location_t combined_loc
8195 = make_location (start_loc
, start_loc
, finish_loc
);
8196 /* Create an expression representing the address. */
8197 expression
= finish_label_address_expr (identifier
, combined_loc
);
8198 if (cp_parser_non_integral_constant_expression (parser
,
8200 expression
= error_mark_node
;
8204 if (unary_operator
!= ERROR_MARK
)
8206 cp_expr cast_expression
;
8207 cp_expr expression
= error_mark_node
;
8208 non_integral_constant non_constant_p
= NIC_NONE
;
8209 location_t loc
= token
->location
;
8210 tsubst_flags_t complain
= complain_flags (decltype_p
);
8212 /* Consume the operator token. */
8213 token
= cp_lexer_consume_token (parser
->lexer
);
8214 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8216 /* Parse the cast-expression. */
8218 = cp_parser_cast_expression (parser
,
8219 unary_operator
== ADDR_EXPR
,
8225 OP_TOKEN CAST_EXPRESSION
8226 ^~~~~~~~~~~~~~~~~~~~~~~~~
8227 with start==caret at the operator token, and
8228 extending to the end of the cast_expression. */
8229 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8231 /* Now, build an appropriate representation. */
8232 switch (unary_operator
)
8235 non_constant_p
= NIC_STAR
;
8236 expression
= build_x_indirect_ref (loc
, cast_expression
,
8239 /* TODO: build_x_indirect_ref does not always honor the
8240 location, so ensure it is set. */
8241 expression
.set_location (loc
);
8245 non_constant_p
= NIC_ADDR
;
8248 expression
= build_x_unary_op (loc
, unary_operator
,
8251 /* TODO: build_x_unary_op does not always honor the location,
8252 so ensure it is set. */
8253 expression
.set_location (loc
);
8256 case PREINCREMENT_EXPR
:
8257 case PREDECREMENT_EXPR
:
8258 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8259 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8262 /* Immediately fold negation of a constant, unless the constant is 0
8263 (since -0 == 0) or it would overflow. */
8264 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8265 && CONSTANT_CLASS_P (cast_expression
)
8266 && !integer_zerop (cast_expression
)
8267 && !TREE_OVERFLOW (cast_expression
))
8269 tree folded
= fold_build1 (unary_operator
,
8270 TREE_TYPE (cast_expression
),
8272 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8274 expression
= cp_expr (folded
, loc
);
8279 case UNARY_PLUS_EXPR
:
8280 case TRUTH_NOT_EXPR
:
8281 expression
= finish_unary_op_expr (loc
, unary_operator
,
8282 cast_expression
, complain
);
8289 if (non_constant_p
!= NIC_NONE
8290 && cp_parser_non_integral_constant_expression (parser
,
8292 expression
= error_mark_node
;
8297 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8298 /*member_access_only_p=*/false,
8303 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8304 unary-operator, the corresponding tree code is returned. */
8306 static enum tree_code
8307 cp_parser_unary_operator (cp_token
* token
)
8309 switch (token
->type
)
8312 return INDIRECT_REF
;
8318 return UNARY_PLUS_EXPR
;
8324 return TRUTH_NOT_EXPR
;
8327 return BIT_NOT_EXPR
;
8334 /* Parse a new-expression.
8337 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8338 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8340 Returns a representation of the expression. */
8343 cp_parser_new_expression (cp_parser
* parser
)
8345 bool global_scope_p
;
8346 vec
<tree
, va_gc
> *placement
;
8348 vec
<tree
, va_gc
> *initializer
;
8349 tree nelts
= NULL_TREE
;
8352 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8354 /* Look for the optional `::' operator. */
8356 = (cp_parser_global_scope_opt (parser
,
8357 /*current_scope_valid_p=*/false)
8359 /* Look for the `new' operator. */
8360 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8361 /* There's no easy way to tell a new-placement from the
8362 `( type-id )' construct. */
8363 cp_parser_parse_tentatively (parser
);
8364 /* Look for a new-placement. */
8365 placement
= cp_parser_new_placement (parser
);
8366 /* If that didn't work out, there's no new-placement. */
8367 if (!cp_parser_parse_definitely (parser
))
8369 if (placement
!= NULL
)
8370 release_tree_vector (placement
);
8374 /* If the next token is a `(', then we have a parenthesized
8376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8379 const char *saved_message
= parser
->type_definition_forbidden_message
;
8381 /* Consume the `('. */
8382 matching_parens parens
;
8383 parens
.consume_open (parser
);
8385 /* Parse the type-id. */
8386 parser
->type_definition_forbidden_message
8387 = G_("types may not be defined in a new-expression");
8389 type_id_in_expr_sentinel
s (parser
);
8390 type
= cp_parser_type_id (parser
);
8392 parser
->type_definition_forbidden_message
= saved_message
;
8394 /* Look for the closing `)'. */
8395 parens
.require_close (parser
);
8396 token
= cp_lexer_peek_token (parser
->lexer
);
8397 /* There should not be a direct-new-declarator in this production,
8398 but GCC used to allowed this, so we check and emit a sensible error
8399 message for this case. */
8400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8402 error_at (token
->location
,
8403 "array bound forbidden after parenthesized type-id");
8404 inform (token
->location
,
8405 "try removing the parentheses around the type-id");
8406 cp_parser_direct_new_declarator (parser
);
8409 /* Otherwise, there must be a new-type-id. */
8411 type
= cp_parser_new_type_id (parser
, &nelts
);
8413 /* If the next token is a `(' or '{', then we have a new-initializer. */
8414 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8415 if (token
->type
== CPP_OPEN_PAREN
8416 || token
->type
== CPP_OPEN_BRACE
)
8417 initializer
= cp_parser_new_initializer (parser
);
8421 /* A new-expression may not appear in an integral constant
8423 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8424 ret
= error_mark_node
;
8425 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8426 of a new-type-id or type-id of a new-expression, the new-expression shall
8427 contain a new-initializer of the form ( assignment-expression )".
8428 Additionally, consistently with the spirit of DR 1467, we want to accept
8429 'new auto { 2 }' too. */
8430 else if ((ret
= type_uses_auto (type
))
8431 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8432 && (vec_safe_length (initializer
) != 1
8433 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8434 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8436 error_at (token
->location
,
8437 "initialization of new-expression for type %<auto%> "
8438 "requires exactly one element");
8439 ret
= error_mark_node
;
8443 /* Construct a location e.g.:
8446 with caret == start at the start of the "new" token, and the end
8447 at the end of the final token we consumed. */
8448 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8449 location_t end_loc
= get_finish (end_tok
->location
);
8450 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8452 /* Create a representation of the new-expression. */
8453 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8454 tf_warning_or_error
);
8455 protected_set_expr_location (ret
, combined_loc
);
8458 if (placement
!= NULL
)
8459 release_tree_vector (placement
);
8460 if (initializer
!= NULL
)
8461 release_tree_vector (initializer
);
8466 /* Parse a new-placement.
8471 Returns the same representation as for an expression-list. */
8473 static vec
<tree
, va_gc
> *
8474 cp_parser_new_placement (cp_parser
* parser
)
8476 vec
<tree
, va_gc
> *expression_list
;
8478 /* Parse the expression-list. */
8479 expression_list
= (cp_parser_parenthesized_expression_list
8480 (parser
, non_attr
, /*cast_p=*/false,
8481 /*allow_expansion_p=*/true,
8482 /*non_constant_p=*/NULL
));
8484 if (expression_list
&& expression_list
->is_empty ())
8485 error ("expected expression-list or type-id");
8487 return expression_list
;
8490 /* Parse a new-type-id.
8493 type-specifier-seq new-declarator [opt]
8495 Returns the TYPE allocated. If the new-type-id indicates an array
8496 type, *NELTS is set to the number of elements in the last array
8497 bound; the TYPE will not include the last array bound. */
8500 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8502 cp_decl_specifier_seq type_specifier_seq
;
8503 cp_declarator
*new_declarator
;
8504 cp_declarator
*declarator
;
8505 cp_declarator
*outer_declarator
;
8506 const char *saved_message
;
8508 /* The type-specifier sequence must not contain type definitions.
8509 (It cannot contain declarations of new types either, but if they
8510 are not definitions we will catch that because they are not
8512 saved_message
= parser
->type_definition_forbidden_message
;
8513 parser
->type_definition_forbidden_message
8514 = G_("types may not be defined in a new-type-id");
8515 /* Parse the type-specifier-seq. */
8516 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8517 /*is_trailing_return=*/false,
8518 &type_specifier_seq
);
8519 /* Restore the old message. */
8520 parser
->type_definition_forbidden_message
= saved_message
;
8522 if (type_specifier_seq
.type
== error_mark_node
)
8523 return error_mark_node
;
8525 /* Parse the new-declarator. */
8526 new_declarator
= cp_parser_new_declarator_opt (parser
);
8528 /* Determine the number of elements in the last array dimension, if
8531 /* Skip down to the last array dimension. */
8532 declarator
= new_declarator
;
8533 outer_declarator
= NULL
;
8534 while (declarator
&& (declarator
->kind
== cdk_pointer
8535 || declarator
->kind
== cdk_ptrmem
))
8537 outer_declarator
= declarator
;
8538 declarator
= declarator
->declarator
;
8541 && declarator
->kind
== cdk_array
8542 && declarator
->declarator
8543 && declarator
->declarator
->kind
== cdk_array
)
8545 outer_declarator
= declarator
;
8546 declarator
= declarator
->declarator
;
8549 if (declarator
&& declarator
->kind
== cdk_array
)
8551 *nelts
= declarator
->u
.array
.bounds
;
8552 if (*nelts
== error_mark_node
)
8553 *nelts
= integer_one_node
;
8555 if (outer_declarator
)
8556 outer_declarator
->declarator
= declarator
->declarator
;
8558 new_declarator
= NULL
;
8561 return groktypename (&type_specifier_seq
, new_declarator
, false);
8564 /* Parse an (optional) new-declarator.
8567 ptr-operator new-declarator [opt]
8568 direct-new-declarator
8570 Returns the declarator. */
8572 static cp_declarator
*
8573 cp_parser_new_declarator_opt (cp_parser
* parser
)
8575 enum tree_code code
;
8576 tree type
, std_attributes
= NULL_TREE
;
8577 cp_cv_quals cv_quals
;
8579 /* We don't know if there's a ptr-operator next, or not. */
8580 cp_parser_parse_tentatively (parser
);
8581 /* Look for a ptr-operator. */
8582 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8583 /* If that worked, look for more new-declarators. */
8584 if (cp_parser_parse_definitely (parser
))
8586 cp_declarator
*declarator
;
8588 /* Parse another optional declarator. */
8589 declarator
= cp_parser_new_declarator_opt (parser
);
8591 declarator
= cp_parser_make_indirect_declarator
8592 (code
, type
, cv_quals
, declarator
, std_attributes
);
8597 /* If the next token is a `[', there is a direct-new-declarator. */
8598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8599 return cp_parser_direct_new_declarator (parser
);
8604 /* Parse a direct-new-declarator.
8606 direct-new-declarator:
8608 direct-new-declarator [constant-expression]
8612 static cp_declarator
*
8613 cp_parser_direct_new_declarator (cp_parser
* parser
)
8615 cp_declarator
*declarator
= NULL
;
8622 /* Look for the opening `['. */
8623 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8625 token
= cp_lexer_peek_token (parser
->lexer
);
8626 expression
= cp_parser_expression (parser
);
8627 /* The standard requires that the expression have integral
8628 type. DR 74 adds enumeration types. We believe that the
8629 real intent is that these expressions be handled like the
8630 expression in a `switch' condition, which also allows
8631 classes with a single conversion to integral or
8632 enumeration type. */
8633 if (!processing_template_decl
)
8636 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8641 error_at (token
->location
,
8642 "expression in new-declarator must have integral "
8643 "or enumeration type");
8644 expression
= error_mark_node
;
8648 /* Look for the closing `]'. */
8649 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8651 /* Add this bound to the declarator. */
8652 declarator
= make_array_declarator (declarator
, expression
);
8654 /* If the next token is not a `[', then there are no more
8656 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8663 /* Parse a new-initializer.
8666 ( expression-list [opt] )
8669 Returns a representation of the expression-list. */
8671 static vec
<tree
, va_gc
> *
8672 cp_parser_new_initializer (cp_parser
* parser
)
8674 vec
<tree
, va_gc
> *expression_list
;
8676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8679 bool expr_non_constant_p
;
8680 cp_lexer_set_source_position (parser
->lexer
);
8681 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8682 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8683 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8684 expression_list
= make_tree_vector_single (t
);
8687 expression_list
= (cp_parser_parenthesized_expression_list
8688 (parser
, non_attr
, /*cast_p=*/false,
8689 /*allow_expansion_p=*/true,
8690 /*non_constant_p=*/NULL
));
8692 return expression_list
;
8695 /* Parse a delete-expression.
8698 :: [opt] delete cast-expression
8699 :: [opt] delete [ ] cast-expression
8701 Returns a representation of the expression. */
8704 cp_parser_delete_expression (cp_parser
* parser
)
8706 bool global_scope_p
;
8710 /* Look for the optional `::' operator. */
8712 = (cp_parser_global_scope_opt (parser
,
8713 /*current_scope_valid_p=*/false)
8715 /* Look for the `delete' keyword. */
8716 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8717 /* See if the array syntax is in use. */
8718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8720 /* Consume the `[' token. */
8721 cp_lexer_consume_token (parser
->lexer
);
8722 /* Look for the `]' token. */
8723 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8724 /* Remember that this is the `[]' construct. */
8730 /* Parse the cast-expression. */
8731 expression
= cp_parser_simple_cast_expression (parser
);
8733 /* A delete-expression may not appear in an integral constant
8735 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8736 return error_mark_node
;
8738 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8739 tf_warning_or_error
);
8742 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8743 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8747 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8749 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8750 switch (token
->type
)
8756 case CPP_CLOSE_SQUARE
:
8757 case CPP_CLOSE_PAREN
:
8758 case CPP_CLOSE_BRACE
:
8759 case CPP_OPEN_BRACE
:
8763 case CPP_DEREF_STAR
:
8771 case CPP_GREATER_EQ
:
8792 case CPP_OPEN_PAREN
:
8793 /* In ((type ()) () the last () isn't a valid cast-expression,
8794 so the whole must be parsed as postfix-expression. */
8795 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8798 case CPP_OPEN_SQUARE
:
8799 /* '[' may start a primary-expression in obj-c++ and in C++11,
8800 as a lambda-expression, eg, '(void)[]{}'. */
8801 if (cxx_dialect
>= cxx11
)
8803 return c_dialect_objc ();
8806 case CPP_MINUS_MINUS
:
8807 /* '++' and '--' may or may not start a cast-expression:
8809 struct T { void operator++(int); };
8810 void f() { (T())++; }
8823 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8824 in the order: const_cast, static_cast, reinterpret_cast.
8826 Don't suggest dynamic_cast.
8828 Return the first legal cast kind found, or NULL otherwise. */
8831 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8835 /* Reuse the parser logic by attempting to build the various kinds of
8836 cast, with "complain" disabled.
8837 Identify the first such cast that is valid. */
8839 /* Don't attempt to run such logic within template processing. */
8840 if (processing_template_decl
)
8843 /* First try const_cast. */
8844 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8845 if (trial
!= error_mark_node
)
8846 return "const_cast";
8848 /* If that fails, try static_cast. */
8849 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8850 if (trial
!= error_mark_node
)
8851 return "static_cast";
8853 /* Finally, try reinterpret_cast. */
8854 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8855 if (trial
!= error_mark_node
)
8856 return "reinterpret_cast";
8858 /* No such cast possible. */
8862 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8863 suggesting how to convert a C-style cast of the form:
8867 to a C++-style cast.
8869 The primary range of RICHLOC is asssumed to be that of the original
8870 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8871 of the parens in the C-style cast. */
8874 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8875 location_t close_paren_loc
, tree orig_expr
,
8878 /* This function is non-trivial, so bail out now if the warning isn't
8879 going to be emitted. */
8880 if (!warn_old_style_cast
)
8883 /* Try to find a legal C++ cast, trying them in order:
8884 const_cast, static_cast, reinterpret_cast. */
8885 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8886 if (!cast_suggestion
)
8889 /* Replace the open paren with "CAST_SUGGESTION<". */
8891 pp_printf (&pp
, "%s<", cast_suggestion
);
8892 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8894 /* Replace the close paren with "> (". */
8895 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8897 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8898 rich_loc
->add_fixit_insert_after (")");
8902 /* Parse a cast-expression.
8906 ( type-id ) cast-expression
8908 ADDRESS_P is true iff the unary-expression is appearing as the
8909 operand of the `&' operator. CAST_P is true if this expression is
8910 the target of a cast.
8912 Returns a representation of the expression. */
8915 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8916 bool decltype_p
, cp_id_kind
* pidk
)
8918 /* If it's a `(', then we might be looking at a cast. */
8919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8921 tree type
= NULL_TREE
;
8922 cp_expr
expr (NULL_TREE
);
8923 int cast_expression
= 0;
8924 const char *saved_message
;
8926 /* There's no way to know yet whether or not this is a cast.
8927 For example, `(int (3))' is a unary-expression, while `(int)
8928 3' is a cast. So, we resort to parsing tentatively. */
8929 cp_parser_parse_tentatively (parser
);
8930 /* Types may not be defined in a cast. */
8931 saved_message
= parser
->type_definition_forbidden_message
;
8932 parser
->type_definition_forbidden_message
8933 = G_("types may not be defined in casts");
8934 /* Consume the `('. */
8935 matching_parens parens
;
8936 cp_token
*open_paren
= parens
.consume_open (parser
);
8937 location_t open_paren_loc
= open_paren
->location
;
8938 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8940 /* A very tricky bit is that `(struct S) { 3 }' is a
8941 compound-literal (which we permit in C++ as an extension).
8942 But, that construct is not a cast-expression -- it is a
8943 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8944 is legal; if the compound-literal were a cast-expression,
8945 you'd need an extra set of parentheses.) But, if we parse
8946 the type-id, and it happens to be a class-specifier, then we
8947 will commit to the parse at that point, because we cannot
8948 undo the action that is done when creating a new class. So,
8949 then we cannot back up and do a postfix-expression.
8951 Another tricky case is the following (c++/29234):
8953 struct S { void operator () (); };
8960 As a type-id we parse the parenthesized S()() as a function
8961 returning a function, groktypename complains and we cannot
8962 back up in this case either.
8964 Therefore, we scan ahead to the closing `)', and check to see
8965 if the tokens after the `)' can start a cast-expression. Otherwise
8966 we are dealing with an unary-expression, a postfix-expression
8969 Yet another tricky case, in C++11, is the following (c++/54891):
8973 The issue is that usually, besides the case of lambda-expressions,
8974 the parenthesized type-id cannot be followed by '[', and, eg, we
8975 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8976 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8977 we don't commit, we try a cast-expression, then an unary-expression.
8979 Save tokens so that we can put them back. */
8980 cp_lexer_save_tokens (parser
->lexer
);
8982 /* We may be looking at a cast-expression. */
8983 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8984 /*consume_paren=*/true))
8986 = cp_parser_tokens_start_cast_expression (parser
);
8988 /* Roll back the tokens we skipped. */
8989 cp_lexer_rollback_tokens (parser
->lexer
);
8990 /* If we aren't looking at a cast-expression, simulate an error so
8991 that the call to cp_parser_error_occurred below returns true. */
8992 if (!cast_expression
)
8993 cp_parser_simulate_error (parser
);
8996 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8997 parser
->in_type_id_in_expr_p
= true;
8998 /* Look for the type-id. */
8999 type
= cp_parser_type_id (parser
);
9000 /* Look for the closing `)'. */
9001 cp_token
*close_paren
= parens
.require_close (parser
);
9003 close_paren_loc
= close_paren
->location
;
9004 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9007 /* Restore the saved message. */
9008 parser
->type_definition_forbidden_message
= saved_message
;
9010 /* At this point this can only be either a cast or a
9011 parenthesized ctor such as `(T ())' that looks like a cast to
9012 function returning T. */
9013 if (!cp_parser_error_occurred (parser
))
9015 /* Only commit if the cast-expression doesn't start with
9016 '++', '--', or '[' in C++11. */
9017 if (cast_expression
> 0)
9018 cp_parser_commit_to_topmost_tentative_parse (parser
);
9020 expr
= cp_parser_cast_expression (parser
,
9021 /*address_p=*/false,
9023 /*decltype_p=*/false,
9026 if (cp_parser_parse_definitely (parser
))
9028 /* Warn about old-style casts, if so requested. */
9029 if (warn_old_style_cast
9030 && !in_system_header_at (input_location
)
9031 && !VOID_TYPE_P (type
)
9032 && current_lang_name
!= lang_name_c
)
9034 gcc_rich_location
rich_loc (input_location
);
9035 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9037 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9038 "use of old-style cast to %q#T", type
);
9041 /* Only type conversions to integral or enumeration types
9042 can be used in constant-expressions. */
9043 if (!cast_valid_in_integral_constant_expression_p (type
)
9044 && cp_parser_non_integral_constant_expression (parser
,
9046 return error_mark_node
;
9048 /* Perform the cast. */
9052 with start==caret at the open paren, extending to the
9054 location_t cast_loc
= make_location (open_paren_loc
,
9056 expr
.get_finish ());
9057 expr
= build_c_cast (cast_loc
, type
, expr
);
9062 cp_parser_abort_tentative_parse (parser
);
9065 /* If we get here, then it's not a cast, so it must be a
9066 unary-expression. */
9067 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9068 cast_p
, decltype_p
);
9071 /* Parse a binary expression of the general form:
9075 pm-expression .* cast-expression
9076 pm-expression ->* cast-expression
9078 multiplicative-expression:
9080 multiplicative-expression * pm-expression
9081 multiplicative-expression / pm-expression
9082 multiplicative-expression % pm-expression
9084 additive-expression:
9085 multiplicative-expression
9086 additive-expression + multiplicative-expression
9087 additive-expression - multiplicative-expression
9091 shift-expression << additive-expression
9092 shift-expression >> additive-expression
9094 relational-expression:
9096 relational-expression < shift-expression
9097 relational-expression > shift-expression
9098 relational-expression <= shift-expression
9099 relational-expression >= shift-expression
9103 relational-expression:
9104 relational-expression <? shift-expression
9105 relational-expression >? shift-expression
9107 equality-expression:
9108 relational-expression
9109 equality-expression == relational-expression
9110 equality-expression != relational-expression
9114 and-expression & equality-expression
9116 exclusive-or-expression:
9118 exclusive-or-expression ^ and-expression
9120 inclusive-or-expression:
9121 exclusive-or-expression
9122 inclusive-or-expression | exclusive-or-expression
9124 logical-and-expression:
9125 inclusive-or-expression
9126 logical-and-expression && inclusive-or-expression
9128 logical-or-expression:
9129 logical-and-expression
9130 logical-or-expression || logical-and-expression
9132 All these are implemented with a single function like:
9135 simple-cast-expression
9136 binary-expression <token> binary-expression
9138 CAST_P is true if this expression is the target of a cast.
9140 The binops_by_token map is used to get the tree codes for each <token> type.
9141 binary-expressions are associated according to a precedence table. */
9143 #define TOKEN_PRECEDENCE(token) \
9144 (((token->type == CPP_GREATER \
9145 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9146 && !parser->greater_than_is_operator_p) \
9147 ? PREC_NOT_OPERATOR \
9148 : binops_by_token[token->type].prec)
9151 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9152 bool no_toplevel_fold_p
,
9154 enum cp_parser_prec prec
,
9157 cp_parser_expression_stack stack
;
9158 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9159 cp_parser_expression_stack_entry current
;
9162 enum tree_code rhs_type
;
9163 enum cp_parser_prec new_prec
, lookahead_prec
;
9166 /* Parse the first expression. */
9167 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9168 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9169 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9170 cast_p
, decltype_p
, pidk
);
9171 current
.prec
= prec
;
9173 if (cp_parser_error_occurred (parser
))
9174 return error_mark_node
;
9178 /* Get an operator token. */
9179 token
= cp_lexer_peek_token (parser
->lexer
);
9181 if (warn_cxx11_compat
9182 && token
->type
== CPP_RSHIFT
9183 && !parser
->greater_than_is_operator_p
)
9185 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9186 "%<>>%> operator is treated"
9187 " as two right angle brackets in C++11"))
9188 inform (token
->location
,
9189 "suggest parentheses around %<>>%> expression");
9192 new_prec
= TOKEN_PRECEDENCE (token
);
9193 if (new_prec
!= PREC_NOT_OPERATOR
9194 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9195 /* This is a fold-expression; handle it later. */
9196 new_prec
= PREC_NOT_OPERATOR
;
9198 /* Popping an entry off the stack means we completed a subexpression:
9199 - either we found a token which is not an operator (`>' where it is not
9200 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9201 will happen repeatedly;
9202 - or, we found an operator which has lower priority. This is the case
9203 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9205 if (new_prec
<= current
.prec
)
9214 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9215 current
.loc
= token
->location
;
9217 /* We used the operator token. */
9218 cp_lexer_consume_token (parser
->lexer
);
9220 /* For "false && x" or "true || x", x will never be executed;
9221 disable warnings while evaluating it. */
9222 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9223 c_inhibit_evaluation_warnings
+=
9224 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9225 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9226 c_inhibit_evaluation_warnings
+=
9227 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9229 /* Extract another operand. It may be the RHS of this expression
9230 or the LHS of a new, higher priority expression. */
9231 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9232 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9233 rhs
= cp_parser_simple_cast_expression (parser
);
9235 /* Get another operator token. Look up its precedence to avoid
9236 building a useless (immediately popped) stack entry for common
9237 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9238 token
= cp_lexer_peek_token (parser
->lexer
);
9239 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9240 if (lookahead_prec
!= PREC_NOT_OPERATOR
9241 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9242 lookahead_prec
= PREC_NOT_OPERATOR
;
9243 if (lookahead_prec
> new_prec
)
9245 /* ... and prepare to parse the RHS of the new, higher priority
9246 expression. Since precedence levels on the stack are
9247 monotonically increasing, we do not have to care about
9252 current
.lhs_type
= rhs_type
;
9253 current
.prec
= new_prec
;
9254 new_prec
= lookahead_prec
;
9258 lookahead_prec
= new_prec
;
9259 /* If the stack is not empty, we have parsed into LHS the right side
9260 (`4' in the example above) of an expression we had suspended.
9261 We can use the information on the stack to recover the LHS (`3')
9262 from the stack together with the tree code (`MULT_EXPR'), and
9263 the precedence of the higher level subexpression
9264 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9265 which will be used to actually build the additive expression. */
9267 rhs_type
= current
.lhs_type
;
9272 /* Undo the disabling of warnings done above. */
9273 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9274 c_inhibit_evaluation_warnings
-=
9275 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9276 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9277 c_inhibit_evaluation_warnings
-=
9278 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9280 if (warn_logical_not_paren
9281 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9282 && current
.lhs_type
== TRUTH_NOT_EXPR
9283 /* Avoid warning for !!x == y. */
9284 && (TREE_CODE (current
.lhs
) != NE_EXPR
9285 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9286 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9287 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9288 /* Avoid warning for !b == y where b is boolean. */
9289 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9290 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9292 /* Avoid warning for !!b == y where b is boolean. */
9293 && (!DECL_P (current
.lhs
)
9294 || TREE_TYPE (current
.lhs
) == NULL_TREE
9295 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9296 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9297 current
.lhs
, maybe_constant_value (rhs
));
9301 location_t combined_loc
= make_location (current
.loc
,
9302 current
.lhs
.get_start (),
9305 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9306 ERROR_MARK for everything that is not a binary expression.
9307 This makes warn_about_parentheses miss some warnings that
9308 involve unary operators. For unary expressions we should
9309 pass the correct tree_code unless the unary expression was
9310 surrounded by parentheses.
9312 if (no_toplevel_fold_p
9313 && lookahead_prec
<= current
.prec
9316 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9317 current
.lhs
= error_mark_node
;
9321 = build_min (current
.tree_type
,
9322 TREE_CODE_CLASS (current
.tree_type
)
9324 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9325 current
.lhs
.get_value (), rhs
.get_value ());
9326 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9331 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9332 current
.lhs
, current
.lhs_type
,
9333 rhs
, rhs_type
, &overload
,
9334 complain_flags (decltype_p
));
9335 /* TODO: build_x_binary_op doesn't always honor the location. */
9336 current
.lhs
.set_location (combined_loc
);
9338 current
.lhs_type
= current
.tree_type
;
9340 /* If the binary operator required the use of an overloaded operator,
9341 then this expression cannot be an integral constant-expression.
9342 An overloaded operator can be used even if both operands are
9343 otherwise permissible in an integral constant-expression if at
9344 least one of the operands is of enumeration type. */
9347 && cp_parser_non_integral_constant_expression (parser
,
9349 return error_mark_node
;
9356 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9357 bool no_toplevel_fold_p
,
9358 enum cp_parser_prec prec
,
9361 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9362 /*decltype*/false, prec
, pidk
);
9365 /* Parse the `? expression : assignment-expression' part of a
9366 conditional-expression. The LOGICAL_OR_EXPR is the
9367 logical-or-expression that started the conditional-expression.
9368 Returns a representation of the entire conditional-expression.
9370 This routine is used by cp_parser_assignment_expression.
9372 ? expression : assignment-expression
9376 ? : assignment-expression */
9379 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9381 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9382 cp_expr assignment_expr
;
9383 struct cp_token
*token
;
9384 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9386 /* Consume the `?' token. */
9387 cp_lexer_consume_token (parser
->lexer
);
9388 token
= cp_lexer_peek_token (parser
->lexer
);
9389 if (cp_parser_allow_gnu_extensions_p (parser
)
9390 && token
->type
== CPP_COLON
)
9392 pedwarn (token
->location
, OPT_Wpedantic
,
9393 "ISO C++ does not allow ?: with omitted middle operand");
9394 /* Implicit true clause. */
9396 c_inhibit_evaluation_warnings
+=
9397 folded_logical_or_expr
== truthvalue_true_node
;
9398 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9402 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9403 parser
->colon_corrects_to_scope_p
= false;
9404 /* Parse the expression. */
9405 c_inhibit_evaluation_warnings
+=
9406 folded_logical_or_expr
== truthvalue_false_node
;
9407 expr
= cp_parser_expression (parser
);
9408 c_inhibit_evaluation_warnings
+=
9409 ((folded_logical_or_expr
== truthvalue_true_node
)
9410 - (folded_logical_or_expr
== truthvalue_false_node
));
9411 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9414 /* The next token should be a `:'. */
9415 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9416 /* Parse the assignment-expression. */
9417 assignment_expr
= cp_parser_assignment_expression (parser
);
9418 c_inhibit_evaluation_warnings
-=
9419 folded_logical_or_expr
== truthvalue_true_node
;
9422 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9423 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9424 with the caret at the "?", ranging from the start of
9425 the logical_or_expr to the end of the assignment_expr. */
9426 loc
= make_location (loc
,
9427 logical_or_expr
.get_start (),
9428 assignment_expr
.get_finish ());
9430 /* Build the conditional-expression. */
9431 return build_x_conditional_expr (loc
, logical_or_expr
,
9434 tf_warning_or_error
);
9437 /* Parse an assignment-expression.
9439 assignment-expression:
9440 conditional-expression
9441 logical-or-expression assignment-operator assignment_expression
9444 CAST_P is true if this expression is the target of a cast.
9445 DECLTYPE_P is true if this expression is the operand of decltype.
9447 Returns a representation for the expression. */
9450 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9451 bool cast_p
, bool decltype_p
)
9455 /* If the next token is the `throw' keyword, then we're looking at
9456 a throw-expression. */
9457 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9458 expr
= cp_parser_throw_expression (parser
);
9459 /* Otherwise, it must be that we are looking at a
9460 logical-or-expression. */
9463 /* Parse the binary expressions (logical-or-expression). */
9464 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9466 PREC_NOT_OPERATOR
, pidk
);
9467 /* If the next token is a `?' then we're actually looking at a
9468 conditional-expression. */
9469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9470 return cp_parser_question_colon_clause (parser
, expr
);
9473 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9475 /* If it's an assignment-operator, we're using the second
9477 enum tree_code assignment_operator
9478 = cp_parser_assignment_operator_opt (parser
);
9479 if (assignment_operator
!= ERROR_MARK
)
9481 bool non_constant_p
;
9483 /* Parse the right-hand side of the assignment. */
9484 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9487 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9488 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9490 /* An assignment may not appear in a
9491 constant-expression. */
9492 if (cp_parser_non_integral_constant_expression (parser
,
9494 return error_mark_node
;
9495 /* Build the assignment expression. Its default
9499 is the location of the '=' token as the
9500 caret, ranging from the start of the lhs to the
9502 loc
= make_location (loc
,
9505 expr
= build_x_modify_expr (loc
, expr
,
9506 assignment_operator
,
9508 complain_flags (decltype_p
));
9509 /* TODO: build_x_modify_expr doesn't honor the location,
9510 so we must set it here. */
9511 expr
.set_location (loc
);
9519 /* Parse an (optional) assignment-operator.
9521 assignment-operator: one of
9522 = *= /= %= += -= >>= <<= &= ^= |=
9526 assignment-operator: one of
9529 If the next token is an assignment operator, the corresponding tree
9530 code is returned, and the token is consumed. For example, for
9531 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9532 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9533 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9534 operator, ERROR_MARK is returned. */
9536 static enum tree_code
9537 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9542 /* Peek at the next token. */
9543 token
= cp_lexer_peek_token (parser
->lexer
);
9545 switch (token
->type
)
9556 op
= TRUNC_DIV_EXPR
;
9560 op
= TRUNC_MOD_EXPR
;
9592 /* Nothing else is an assignment operator. */
9596 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9597 if (op
!= ERROR_MARK
9598 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9601 /* If it was an assignment operator, consume it. */
9602 if (op
!= ERROR_MARK
)
9603 cp_lexer_consume_token (parser
->lexer
);
9608 /* Parse an expression.
9611 assignment-expression
9612 expression , assignment-expression
9614 CAST_P is true if this expression is the target of a cast.
9615 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9616 except possibly parenthesized or on the RHS of a comma (N3276).
9618 Returns a representation of the expression. */
9621 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9622 bool cast_p
, bool decltype_p
)
9624 cp_expr expression
= NULL_TREE
;
9625 location_t loc
= UNKNOWN_LOCATION
;
9629 cp_expr assignment_expression
;
9631 /* Parse the next assignment-expression. */
9632 assignment_expression
9633 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9635 /* We don't create a temporary for a call that is the immediate operand
9636 of decltype or on the RHS of a comma. But when we see a comma, we
9637 need to create a temporary for a call on the LHS. */
9638 if (decltype_p
&& !processing_template_decl
9639 && TREE_CODE (assignment_expression
) == CALL_EXPR
9640 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9641 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9642 assignment_expression
9643 = build_cplus_new (TREE_TYPE (assignment_expression
),
9644 assignment_expression
, tf_warning_or_error
);
9646 /* If this is the first assignment-expression, we can just
9649 expression
= assignment_expression
;
9652 /* Create a location with caret at the comma, ranging
9653 from the start of the LHS to the end of the RHS. */
9654 loc
= make_location (loc
,
9655 expression
.get_start (),
9656 assignment_expression
.get_finish ());
9657 expression
= build_x_compound_expr (loc
, expression
,
9658 assignment_expression
,
9659 complain_flags (decltype_p
));
9660 expression
.set_location (loc
);
9662 /* If the next token is not a comma, or we're in a fold-expression, then
9663 we are done with the expression. */
9664 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9665 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9667 /* Consume the `,'. */
9668 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9669 cp_lexer_consume_token (parser
->lexer
);
9670 /* A comma operator cannot appear in a constant-expression. */
9671 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9672 expression
= error_mark_node
;
9678 /* Parse a constant-expression.
9680 constant-expression:
9681 conditional-expression
9683 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9684 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9685 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9686 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9687 only parse a conditional-expression, otherwise parse an
9688 assignment-expression. See below for rationale. */
9691 cp_parser_constant_expression (cp_parser
* parser
,
9692 bool allow_non_constant_p
,
9693 bool *non_constant_p
,
9696 bool saved_integral_constant_expression_p
;
9697 bool saved_allow_non_integral_constant_expression_p
;
9698 bool saved_non_integral_constant_expression_p
;
9701 /* It might seem that we could simply parse the
9702 conditional-expression, and then check to see if it were
9703 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9704 one that the compiler can figure out is constant, possibly after
9705 doing some simplifications or optimizations. The standard has a
9706 precise definition of constant-expression, and we must honor
9707 that, even though it is somewhat more restrictive.
9713 is not a legal declaration, because `(2, 3)' is not a
9714 constant-expression. The `,' operator is forbidden in a
9715 constant-expression. However, GCC's constant-folding machinery
9716 will fold this operation to an INTEGER_CST for `3'. */
9718 /* Save the old settings. */
9719 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9720 saved_allow_non_integral_constant_expression_p
9721 = parser
->allow_non_integral_constant_expression_p
;
9722 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9723 /* We are now parsing a constant-expression. */
9724 parser
->integral_constant_expression_p
= true;
9725 parser
->allow_non_integral_constant_expression_p
9726 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9727 parser
->non_integral_constant_expression_p
= false;
9728 /* Although the grammar says "conditional-expression", when not STRICT_P,
9729 we parse an "assignment-expression", which also permits
9730 "throw-expression" and the use of assignment operators. In the case
9731 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9732 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9733 actually essential that we look for an assignment-expression.
9734 For example, cp_parser_initializer_clauses uses this function to
9735 determine whether a particular assignment-expression is in fact
9739 /* Parse the binary expressions (logical-or-expression). */
9740 expression
= cp_parser_binary_expression (parser
, false, false, false,
9741 PREC_NOT_OPERATOR
, NULL
);
9742 /* If the next token is a `?' then we're actually looking at
9743 a conditional-expression; otherwise we're done. */
9744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9745 expression
= cp_parser_question_colon_clause (parser
, expression
);
9748 expression
= cp_parser_assignment_expression (parser
);
9749 /* Restore the old settings. */
9750 parser
->integral_constant_expression_p
9751 = saved_integral_constant_expression_p
;
9752 parser
->allow_non_integral_constant_expression_p
9753 = saved_allow_non_integral_constant_expression_p
;
9754 if (cxx_dialect
>= cxx11
)
9756 /* Require an rvalue constant expression here; that's what our
9757 callers expect. Reference constant expressions are handled
9758 separately in e.g. cp_parser_template_argument. */
9759 tree decay
= expression
;
9760 if (TREE_TYPE (expression
)
9761 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9762 decay
= build_address (expression
);
9763 bool is_const
= potential_rvalue_constant_expression (decay
);
9764 parser
->non_integral_constant_expression_p
= !is_const
;
9765 if (!is_const
&& !allow_non_constant_p
)
9766 require_potential_rvalue_constant_expression (decay
);
9768 if (allow_non_constant_p
)
9769 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9770 parser
->non_integral_constant_expression_p
9771 = saved_non_integral_constant_expression_p
;
9776 /* Parse __builtin_offsetof.
9778 offsetof-expression:
9779 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9781 offsetof-member-designator:
9783 | offsetof-member-designator "." id-expression
9784 | offsetof-member-designator "[" expression "]"
9785 | offsetof-member-designator "->" id-expression */
9788 cp_parser_builtin_offsetof (cp_parser
*parser
)
9790 int save_ice_p
, save_non_ice_p
;
9795 location_t finish_loc
;
9797 /* We're about to accept non-integral-constant things, but will
9798 definitely yield an integral constant expression. Save and
9799 restore these values around our local parsing. */
9800 save_ice_p
= parser
->integral_constant_expression_p
;
9801 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9803 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9805 /* Consume the "__builtin_offsetof" token. */
9806 cp_lexer_consume_token (parser
->lexer
);
9807 /* Consume the opening `('. */
9808 matching_parens parens
;
9809 parens
.require_open (parser
);
9810 /* Parse the type-id. */
9811 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9813 const char *saved_message
= parser
->type_definition_forbidden_message
;
9814 parser
->type_definition_forbidden_message
9815 = G_("types may not be defined within __builtin_offsetof");
9816 type
= cp_parser_type_id (parser
);
9817 parser
->type_definition_forbidden_message
= saved_message
;
9819 /* Look for the `,'. */
9820 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9821 token
= cp_lexer_peek_token (parser
->lexer
);
9823 /* Build the (type *)null that begins the traditional offsetof macro. */
9825 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9826 tf_warning_or_error
);
9828 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9829 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9830 true, &dummy
, token
->location
);
9833 token
= cp_lexer_peek_token (parser
->lexer
);
9834 switch (token
->type
)
9836 case CPP_OPEN_SQUARE
:
9837 /* offsetof-member-designator "[" expression "]" */
9838 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9843 /* offsetof-member-designator "->" identifier */
9844 expr
= grok_array_decl (token
->location
, expr
,
9845 integer_zero_node
, false);
9849 /* offsetof-member-designator "." identifier */
9850 cp_lexer_consume_token (parser
->lexer
);
9851 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9856 case CPP_CLOSE_PAREN
:
9857 /* Consume the ")" token. */
9858 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9859 cp_lexer_consume_token (parser
->lexer
);
9863 /* Error. We know the following require will fail, but
9864 that gives the proper error message. */
9865 parens
.require_close (parser
);
9866 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9867 expr
= error_mark_node
;
9873 /* Make a location of the form:
9874 __builtin_offsetof (struct s, f)
9875 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9876 with caret at the type-id, ranging from the start of the
9877 "_builtin_offsetof" token to the close paren. */
9878 loc
= make_location (loc
, start_loc
, finish_loc
);
9879 /* The result will be an INTEGER_CST, so we need to explicitly
9880 preserve the location. */
9881 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9884 parser
->integral_constant_expression_p
= save_ice_p
;
9885 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9887 expr
= expr
.maybe_add_location_wrapper ();
9891 /* Parse a trait expression.
9893 Returns a representation of the expression, the underlying type
9894 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9897 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9900 tree type1
, type2
= NULL_TREE
;
9901 bool binary
= false;
9902 bool variadic
= false;
9906 case RID_HAS_NOTHROW_ASSIGN
:
9907 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9909 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9910 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9912 case RID_HAS_NOTHROW_COPY
:
9913 kind
= CPTK_HAS_NOTHROW_COPY
;
9915 case RID_HAS_TRIVIAL_ASSIGN
:
9916 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9918 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9919 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9921 case RID_HAS_TRIVIAL_COPY
:
9922 kind
= CPTK_HAS_TRIVIAL_COPY
;
9924 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9925 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9927 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9928 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9930 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9931 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9933 case RID_IS_ABSTRACT
:
9934 kind
= CPTK_IS_ABSTRACT
;
9936 case RID_IS_AGGREGATE
:
9937 kind
= CPTK_IS_AGGREGATE
;
9939 case RID_IS_BASE_OF
:
9940 kind
= CPTK_IS_BASE_OF
;
9944 kind
= CPTK_IS_CLASS
;
9947 kind
= CPTK_IS_EMPTY
;
9950 kind
= CPTK_IS_ENUM
;
9953 kind
= CPTK_IS_FINAL
;
9955 case RID_IS_LITERAL_TYPE
:
9956 kind
= CPTK_IS_LITERAL_TYPE
;
9961 case RID_IS_POLYMORPHIC
:
9962 kind
= CPTK_IS_POLYMORPHIC
;
9964 case RID_IS_SAME_AS
:
9965 kind
= CPTK_IS_SAME_AS
;
9968 case RID_IS_STD_LAYOUT
:
9969 kind
= CPTK_IS_STD_LAYOUT
;
9971 case RID_IS_TRIVIAL
:
9972 kind
= CPTK_IS_TRIVIAL
;
9974 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9975 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9978 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9979 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9982 case RID_IS_TRIVIALLY_COPYABLE
:
9983 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9986 kind
= CPTK_IS_UNION
;
9988 case RID_UNDERLYING_TYPE
:
9989 kind
= CPTK_UNDERLYING_TYPE
;
9994 case RID_DIRECT_BASES
:
9995 kind
= CPTK_DIRECT_BASES
;
9997 case RID_IS_ASSIGNABLE
:
9998 kind
= CPTK_IS_ASSIGNABLE
;
10001 case RID_IS_CONSTRUCTIBLE
:
10002 kind
= CPTK_IS_CONSTRUCTIBLE
;
10006 gcc_unreachable ();
10009 /* Get location of initial token. */
10010 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10012 /* Consume the token. */
10013 cp_lexer_consume_token (parser
->lexer
);
10015 matching_parens parens
;
10016 parens
.require_open (parser
);
10019 type_id_in_expr_sentinel
s (parser
);
10020 type1
= cp_parser_type_id (parser
);
10023 if (type1
== error_mark_node
)
10024 return error_mark_node
;
10028 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10031 type_id_in_expr_sentinel
s (parser
);
10032 type2
= cp_parser_type_id (parser
);
10035 if (type2
== error_mark_node
)
10036 return error_mark_node
;
10040 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10042 cp_lexer_consume_token (parser
->lexer
);
10043 tree elt
= cp_parser_type_id (parser
);
10044 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10046 cp_lexer_consume_token (parser
->lexer
);
10047 elt
= make_pack_expansion (elt
);
10049 if (elt
== error_mark_node
)
10050 return error_mark_node
;
10051 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10055 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10056 parens
.require_close (parser
);
10058 /* Construct a location of the form:
10059 __is_trivially_copyable(_Tp)
10060 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10061 with start == caret, finishing at the close-paren. */
10062 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10064 /* Complete the trait expression, which may mean either processing
10065 the trait expr now or saving it for template instantiation. */
10068 case CPTK_UNDERLYING_TYPE
:
10069 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10071 return cp_expr (finish_bases (type1
, false), trait_loc
);
10072 case CPTK_DIRECT_BASES
:
10073 return cp_expr (finish_bases (type1
, true), trait_loc
);
10075 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10079 /* Parse a lambda expression.
10082 lambda-introducer lambda-declarator [opt] compound-statement
10084 Returns a representation of the expression. */
10087 cp_parser_lambda_expression (cp_parser
* parser
)
10089 tree lambda_expr
= build_lambda_expr ();
10092 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10093 cp_token_position start
= 0;
10095 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10097 if (cp_unevaluated_operand
)
10099 if (!token
->error_reported
)
10101 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10102 "lambda-expression in unevaluated context");
10103 token
->error_reported
= true;
10107 else if (parser
->in_template_argument_list_p
)
10109 if (!token
->error_reported
)
10111 error_at (token
->location
, "lambda-expression in template-argument");
10112 token
->error_reported
= true;
10117 /* We may be in the middle of deferred access check. Disable
10119 push_deferring_access_checks (dk_no_deferred
);
10121 cp_parser_lambda_introducer (parser
, lambda_expr
);
10123 type
= begin_lambda_type (lambda_expr
);
10124 if (type
== error_mark_node
)
10125 return error_mark_node
;
10127 record_lambda_scope (lambda_expr
);
10129 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10130 determine_visibility (TYPE_NAME (type
));
10132 /* Now that we've started the type, add the capture fields for any
10133 explicit captures. */
10134 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10137 /* Inside the class, surrounding template-parameter-lists do not apply. */
10138 unsigned int saved_num_template_parameter_lists
10139 = parser
->num_template_parameter_lists
;
10140 unsigned char in_statement
= parser
->in_statement
;
10141 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10142 bool fully_implicit_function_template_p
10143 = parser
->fully_implicit_function_template_p
;
10144 tree implicit_template_parms
= parser
->implicit_template_parms
;
10145 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10146 bool auto_is_implicit_function_template_parm_p
10147 = parser
->auto_is_implicit_function_template_parm_p
;
10149 parser
->num_template_parameter_lists
= 0;
10150 parser
->in_statement
= 0;
10151 parser
->in_switch_statement_p
= false;
10152 parser
->fully_implicit_function_template_p
= false;
10153 parser
->implicit_template_parms
= 0;
10154 parser
->implicit_template_scope
= 0;
10155 parser
->auto_is_implicit_function_template_parm_p
= false;
10157 /* By virtue of defining a local class, a lambda expression has access to
10158 the private variables of enclosing classes. */
10160 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10162 if (ok
&& cp_parser_error_occurred (parser
))
10167 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10168 && cp_parser_start_tentative_firewall (parser
))
10170 cp_parser_lambda_body (parser
, lambda_expr
);
10172 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10174 if (cp_parser_skip_to_closing_brace (parser
))
10175 cp_lexer_consume_token (parser
->lexer
);
10178 /* The capture list was built up in reverse order; fix that now. */
10179 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10180 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10183 maybe_add_lambda_conv_op (type
);
10185 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10187 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10188 parser
->in_statement
= in_statement
;
10189 parser
->in_switch_statement_p
= in_switch_statement_p
;
10190 parser
->fully_implicit_function_template_p
10191 = fully_implicit_function_template_p
;
10192 parser
->implicit_template_parms
= implicit_template_parms
;
10193 parser
->implicit_template_scope
= implicit_template_scope
;
10194 parser
->auto_is_implicit_function_template_parm_p
10195 = auto_is_implicit_function_template_parm_p
;
10198 /* This field is only used during parsing of the lambda. */
10199 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10201 /* This lambda shouldn't have any proxies left at this point. */
10202 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10203 /* And now that we're done, push proxies for an enclosing lambda. */
10204 insert_pending_capture_proxies ();
10207 lambda_expr
= build_lambda_object (lambda_expr
);
10209 lambda_expr
= error_mark_node
;
10211 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10213 pop_deferring_access_checks ();
10215 return lambda_expr
;
10218 /* Parse the beginning of a lambda expression.
10221 [ lambda-capture [opt] ]
10223 LAMBDA_EXPR is the current representation of the lambda expression. */
10226 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10228 /* Need commas after the first capture. */
10231 /* Eat the leading `['. */
10232 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10234 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10236 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10237 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10238 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10239 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10241 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10243 cp_lexer_consume_token (parser
->lexer
);
10247 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10249 cp_token
* capture_token
;
10251 tree capture_init_expr
;
10252 cp_id_kind idk
= CP_ID_KIND_NONE
;
10253 bool explicit_init_p
= false;
10255 enum capture_kind_type
10260 enum capture_kind_type capture_kind
= BY_COPY
;
10262 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10264 error ("expected end of capture-list");
10271 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10273 /* Possibly capture `this'. */
10274 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10276 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10277 if (cxx_dialect
< cxx2a
10278 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10279 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10280 "with by-copy capture default");
10281 cp_lexer_consume_token (parser
->lexer
);
10282 add_capture (lambda_expr
,
10283 /*id=*/this_identifier
,
10284 /*initializer=*/finish_this_expr (),
10285 /*by_reference_p=*/true,
10290 /* Possibly capture `*this'. */
10291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10292 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10294 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10295 if (cxx_dialect
< cxx17
)
10296 pedwarn (loc
, 0, "%<*this%> capture only available with "
10297 "-std=c++17 or -std=gnu++17");
10298 cp_lexer_consume_token (parser
->lexer
);
10299 cp_lexer_consume_token (parser
->lexer
);
10300 add_capture (lambda_expr
,
10301 /*id=*/this_identifier
,
10302 /*initializer=*/finish_this_expr (),
10303 /*by_reference_p=*/false,
10308 /* Remember whether we want to capture as a reference or not. */
10309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10311 capture_kind
= BY_REFERENCE
;
10312 cp_lexer_consume_token (parser
->lexer
);
10315 /* Get the identifier. */
10316 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10317 capture_id
= cp_parser_identifier (parser
);
10319 if (capture_id
== error_mark_node
)
10320 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10321 delimiters, but I modified this to stop on unnested ']' as well. It
10322 was already changed to stop on unnested '}', so the
10323 "closing_parenthesis" name is no more misleading with my change. */
10325 cp_parser_skip_to_closing_parenthesis (parser
,
10326 /*recovering=*/true,
10328 /*consume_paren=*/true);
10332 /* Find the initializer for this capture. */
10333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10334 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10335 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10337 bool direct
, non_constant
;
10338 /* An explicit initializer exists. */
10339 if (cxx_dialect
< cxx14
)
10340 pedwarn (input_location
, 0,
10341 "lambda capture initializers "
10342 "only available with -std=c++14 or -std=gnu++14");
10343 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10344 &non_constant
, true);
10345 explicit_init_p
= true;
10346 if (capture_init_expr
== NULL_TREE
)
10348 error ("empty initializer for lambda init-capture");
10349 capture_init_expr
= error_mark_node
;
10354 const char* error_msg
;
10356 /* Turn the identifier into an id-expression. */
10358 = cp_parser_lookup_name_simple (parser
, capture_id
,
10359 capture_token
->location
);
10361 if (capture_init_expr
== error_mark_node
)
10363 unqualified_name_lookup_error (capture_id
);
10366 else if (!VAR_P (capture_init_expr
)
10367 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10369 error_at (capture_token
->location
,
10370 "capture of non-variable %qE",
10371 capture_init_expr
);
10372 if (DECL_P (capture_init_expr
))
10373 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10374 "%q#D declared here", capture_init_expr
);
10377 if (VAR_P (capture_init_expr
)
10378 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10380 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10381 "%qD with non-automatic storage duration",
10382 capture_init_expr
))
10383 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10384 "%q#D declared here", capture_init_expr
);
10389 = finish_id_expression
10394 /*integral_constant_expression_p=*/false,
10395 /*allow_non_integral_constant_expression_p=*/false,
10396 /*non_integral_constant_expression_p=*/NULL
,
10397 /*template_p=*/false,
10399 /*address_p=*/false,
10400 /*template_arg_p=*/false,
10402 capture_token
->location
);
10404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10406 cp_lexer_consume_token (parser
->lexer
);
10407 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10411 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10412 && !explicit_init_p
)
10414 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10415 && capture_kind
== BY_COPY
)
10416 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10417 "of %qD redundant with by-copy capture default",
10419 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10420 && capture_kind
== BY_REFERENCE
)
10421 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10422 "capture of %qD redundant with by-reference capture "
10423 "default", capture_id
);
10426 add_capture (lambda_expr
,
10429 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10432 /* If there is any qualification still in effect, clear it
10433 now; we will be starting fresh with the next capture. */
10434 parser
->scope
= NULL_TREE
;
10435 parser
->qualifying_scope
= NULL_TREE
;
10436 parser
->object_scope
= NULL_TREE
;
10439 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10442 /* Parse the (optional) middle of a lambda expression.
10445 < template-parameter-list [opt] >
10446 ( parameter-declaration-clause [opt] )
10447 attribute-specifier [opt]
10448 decl-specifier-seq [opt]
10449 exception-specification [opt]
10450 lambda-return-type-clause [opt]
10452 LAMBDA_EXPR is the current representation of the lambda expression. */
10455 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10457 /* 5.1.1.4 of the standard says:
10458 If a lambda-expression does not include a lambda-declarator, it is as if
10459 the lambda-declarator were ().
10460 This means an empty parameter list, no attributes, and no exception
10462 tree param_list
= void_list_node
;
10463 tree attributes
= NULL_TREE
;
10464 tree exception_spec
= NULL_TREE
;
10465 tree template_param_list
= NULL_TREE
;
10466 tree tx_qual
= NULL_TREE
;
10467 tree return_type
= NULL_TREE
;
10468 cp_decl_specifier_seq lambda_specs
;
10469 clear_decl_specs (&lambda_specs
);
10471 /* The template-parameter-list is optional, but must begin with
10472 an opening angle if present. */
10473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10475 if (cxx_dialect
< cxx14
)
10476 pedwarn (parser
->lexer
->next_token
->location
, 0,
10477 "lambda templates are only available with "
10478 "-std=c++14 or -std=gnu++14");
10479 else if (cxx_dialect
< cxx2a
)
10480 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10481 "lambda templates are only available with "
10482 "-std=c++2a or -std=gnu++2a");
10484 cp_lexer_consume_token (parser
->lexer
);
10486 template_param_list
= cp_parser_template_parameter_list (parser
);
10488 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10490 /* We just processed one more parameter list. */
10491 ++parser
->num_template_parameter_lists
;
10494 /* The parameter-declaration-clause is optional (unless
10495 template-parameter-list was given), but must begin with an
10496 opening parenthesis if present. */
10497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10499 matching_parens parens
;
10500 parens
.consume_open (parser
);
10502 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10504 /* Parse parameters. */
10505 param_list
= cp_parser_parameter_declaration_clause (parser
);
10507 /* Default arguments shall not be specified in the
10508 parameter-declaration-clause of a lambda-declarator. */
10509 if (cxx_dialect
< cxx14
)
10510 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10511 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10512 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10513 "default argument specified for lambda parameter");
10515 parens
.require_close (parser
);
10517 attributes
= cp_parser_attributes_opt (parser
);
10519 /* In the decl-specifier-seq of the lambda-declarator, each
10520 decl-specifier shall either be mutable or constexpr. */
10521 int declares_class_or_enum
;
10522 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10523 cp_parser_decl_specifier_seq (parser
,
10524 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10525 &lambda_specs
, &declares_class_or_enum
);
10526 if (lambda_specs
.storage_class
== sc_mutable
)
10528 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10529 if (lambda_specs
.conflicting_specifiers_p
)
10530 error_at (lambda_specs
.locations
[ds_storage_class
],
10531 "duplicate %<mutable%>");
10534 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10536 /* Parse optional exception specification. */
10537 exception_spec
= cp_parser_exception_specification_opt (parser
);
10539 /* Parse optional trailing return type. */
10540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10542 cp_lexer_consume_token (parser
->lexer
);
10543 return_type
= cp_parser_trailing_type_id (parser
);
10546 /* The function parameters must be in scope all the way until after the
10547 trailing-return-type in case of decltype. */
10548 pop_bindings_and_leave_scope ();
10550 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10551 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10553 /* Create the function call operator.
10555 Messing with declarators like this is no uglier than building up the
10556 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10559 cp_decl_specifier_seq return_type_specs
;
10560 cp_declarator
* declarator
;
10565 clear_decl_specs (&return_type_specs
);
10567 return_type_specs
.type
= return_type
;
10569 /* Maybe we will deduce the return type later. */
10570 return_type_specs
.type
= make_auto ();
10572 if (lambda_specs
.locations
[ds_constexpr
])
10574 if (cxx_dialect
>= cxx17
)
10575 return_type_specs
.locations
[ds_constexpr
]
10576 = lambda_specs
.locations
[ds_constexpr
];
10578 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10579 "lambda only available with -std=c++17 or -std=gnu++17");
10582 p
= obstack_alloc (&declarator_obstack
, 0);
10584 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10586 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10587 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10588 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10589 VIRT_SPEC_UNSPECIFIED
,
10593 /*late_return_type=*/NULL_TREE
,
10594 /*requires_clause*/NULL_TREE
);
10595 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10597 fco
= grokmethod (&return_type_specs
,
10600 if (fco
!= error_mark_node
)
10602 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10603 DECL_ARTIFICIAL (fco
) = 1;
10604 /* Give the object parameter a different name. */
10605 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10607 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10609 if (template_param_list
)
10611 fco
= finish_member_template_decl (fco
);
10612 finish_template_decl (template_param_list
);
10613 --parser
->num_template_parameter_lists
;
10615 else if (parser
->fully_implicit_function_template_p
)
10616 fco
= finish_fully_implicit_template (parser
, fco
);
10618 finish_member_declaration (fco
);
10620 obstack_free (&declarator_obstack
, p
);
10622 return (fco
!= error_mark_node
);
10626 /* Parse the body of a lambda expression, which is simply
10630 but which requires special handling.
10631 LAMBDA_EXPR is the current representation of the lambda expression. */
10634 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10636 bool nested
= (current_function_decl
!= NULL_TREE
);
10637 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10638 bool in_function_body
= parser
->in_function_body
;
10641 push_function_context ();
10643 /* Still increment function_depth so that we don't GC in the
10644 middle of an expression. */
10647 vec
<tree
> omp_privatization_save
;
10648 save_omp_privatization_clauses (omp_privatization_save
);
10649 /* Clear this in case we're in the middle of a default argument. */
10650 parser
->local_variables_forbidden_p
= false;
10651 parser
->in_function_body
= true;
10654 local_specialization_stack
s (lss_copy
);
10655 tree fco
= lambda_function (lambda_expr
);
10656 tree body
= start_lambda_function (fco
, lambda_expr
);
10657 matching_braces braces
;
10659 if (braces
.require_open (parser
))
10661 tree compound_stmt
= begin_compound_stmt (0);
10663 /* Originally C++11 required us to peek for 'return expr'; and
10664 process it specially here to deduce the return type. N3638
10665 removed the need for that. */
10667 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10668 cp_parser_label_declaration (parser
);
10669 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10670 braces
.require_close (parser
);
10672 finish_compound_stmt (compound_stmt
);
10675 finish_lambda_function (body
);
10678 restore_omp_privatization_clauses (omp_privatization_save
);
10679 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10680 parser
->in_function_body
= in_function_body
;
10682 pop_function_context();
10687 /* Statements [gram.stmt.stmt] */
10689 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10692 add_debug_begin_stmt (location_t loc
)
10694 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10696 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10697 /* A concept is never expanded normally. */
10700 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10701 SET_EXPR_LOCATION (stmt
, loc
);
10705 /* Parse a statement.
10709 expression-statement
10711 selection-statement
10712 iteration-statement
10714 declaration-statement
10721 attribute-specifier-seq (opt) expression-statement
10722 attribute-specifier-seq (opt) compound-statement
10723 attribute-specifier-seq (opt) selection-statement
10724 attribute-specifier-seq (opt) iteration-statement
10725 attribute-specifier-seq (opt) jump-statement
10726 declaration-statement
10727 attribute-specifier-seq (opt) try-block
10730 expression-statement
10738 IN_COMPOUND is true when the statement is nested inside a
10739 cp_parser_compound_statement; this matters for certain pragmas.
10741 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10742 is a (possibly labeled) if statement which is not enclosed in braces
10743 and has an else clause. This is used to implement -Wparentheses.
10745 CHAIN is a vector of if-else-if conditions. */
10748 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10749 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10750 location_t
*loc_after_labels
)
10752 tree statement
, std_attrs
= NULL_TREE
;
10754 location_t statement_location
, attrs_location
;
10759 /* There is no statement yet. */
10760 statement
= NULL_TREE
;
10762 saved_token_sentinel
saved_tokens (parser
->lexer
);
10763 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10764 if (c_dialect_objc ())
10765 /* In obj-c++, seeing '[[' might be the either the beginning of
10766 c++11 attributes, or a nested objc-message-expression. So
10767 let's parse the c++11 attributes tentatively. */
10768 cp_parser_parse_tentatively (parser
);
10769 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10770 if (c_dialect_objc ())
10772 if (!cp_parser_parse_definitely (parser
))
10773 std_attrs
= NULL_TREE
;
10776 /* Peek at the next token. */
10777 token
= cp_lexer_peek_token (parser
->lexer
);
10778 /* Remember the location of the first token in the statement. */
10779 statement_location
= token
->location
;
10780 add_debug_begin_stmt (statement_location
);
10781 /* If this is a keyword, then that will often determine what kind of
10782 statement we have. */
10783 if (token
->type
== CPP_KEYWORD
)
10785 enum rid keyword
= token
->keyword
;
10791 /* Looks like a labeled-statement with a case label.
10792 Parse the label, and then use tail recursion to parse
10794 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10795 in_compound
= false;
10800 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10806 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10813 statement
= cp_parser_jump_statement (parser
);
10816 /* Objective-C++ exception-handling constructs. */
10819 case RID_AT_FINALLY
:
10820 case RID_AT_SYNCHRONIZED
:
10822 statement
= cp_parser_objc_statement (parser
);
10826 statement
= cp_parser_try_block (parser
);
10829 case RID_NAMESPACE
:
10830 /* This must be a namespace alias definition. */
10831 cp_parser_declaration_statement (parser
);
10834 case RID_TRANSACTION_ATOMIC
:
10835 case RID_TRANSACTION_RELAXED
:
10836 case RID_SYNCHRONIZED
:
10837 case RID_ATOMIC_NOEXCEPT
:
10838 case RID_ATOMIC_CANCEL
:
10839 statement
= cp_parser_transaction (parser
, token
);
10841 case RID_TRANSACTION_CANCEL
:
10842 statement
= cp_parser_transaction_cancel (parser
);
10846 /* It might be a keyword like `int' that can start a
10847 declaration-statement. */
10851 else if (token
->type
== CPP_NAME
)
10853 /* If the next token is a `:', then we are looking at a
10854 labeled-statement. */
10855 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10856 if (token
->type
== CPP_COLON
)
10858 /* Looks like a labeled-statement with an ordinary label.
10859 Parse the label, and then use tail recursion to parse
10862 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10863 in_compound
= false;
10867 /* Anything that starts with a `{' must be a compound-statement. */
10868 else if (token
->type
== CPP_OPEN_BRACE
)
10869 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10870 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10871 a statement all its own. */
10872 else if (token
->type
== CPP_PRAGMA
)
10874 /* Only certain OpenMP pragmas are attached to statements, and thus
10875 are considered statements themselves. All others are not. In
10876 the context of a compound, accept the pragma as a "statement" and
10877 return so that we can check for a close brace. Otherwise we
10878 require a real statement and must go back and read one. */
10880 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10881 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10885 else if (token
->type
== CPP_EOF
)
10887 cp_parser_error (parser
, "expected statement");
10891 /* Everything else must be a declaration-statement or an
10892 expression-statement. Try for the declaration-statement
10893 first, unless we are looking at a `;', in which case we know that
10894 we have an expression-statement. */
10897 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10899 if (std_attrs
!= NULL_TREE
)
10901 /* Attributes should be parsed as part of the the
10902 declaration, so let's un-parse them. */
10903 saved_tokens
.rollback();
10904 std_attrs
= NULL_TREE
;
10907 cp_parser_parse_tentatively (parser
);
10908 /* Try to parse the declaration-statement. */
10909 cp_parser_declaration_statement (parser
);
10910 /* If that worked, we're done. */
10911 if (cp_parser_parse_definitely (parser
))
10914 /* All preceding labels have been parsed at this point. */
10915 if (loc_after_labels
!= NULL
)
10916 *loc_after_labels
= statement_location
;
10918 /* Look for an expression-statement instead. */
10919 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10921 /* Handle [[fallthrough]];. */
10922 if (attribute_fallthrough_p (std_attrs
))
10924 /* The next token after the fallthrough attribute is ';'. */
10925 if (statement
== NULL_TREE
)
10927 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10928 statement
= build_call_expr_internal_loc (statement_location
,
10930 void_type_node
, 0);
10931 finish_expr_stmt (statement
);
10934 warning_at (statement_location
, OPT_Wattributes
,
10935 "%<fallthrough%> attribute not followed by %<;%>");
10936 std_attrs
= NULL_TREE
;
10940 /* Set the line number for the statement. */
10941 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10942 SET_EXPR_LOCATION (statement
, statement_location
);
10944 /* Allow "[[fallthrough]];", but warn otherwise. */
10945 if (std_attrs
!= NULL_TREE
)
10946 warning_at (attrs_location
,
10948 "attributes at the beginning of statement are ignored");
10951 /* Append ATTR to attribute list ATTRS. */
10954 attr_chainon (tree attrs
, tree attr
)
10956 if (attrs
== error_mark_node
)
10957 return error_mark_node
;
10958 if (attr
== error_mark_node
)
10959 return error_mark_node
;
10960 return chainon (attrs
, attr
);
10963 /* Parse the label for a labeled-statement, i.e.
10966 case constant-expression :
10970 case constant-expression ... constant-expression : statement
10972 When a label is parsed without errors, the label is added to the
10973 parse tree by the finish_* functions, so this function doesn't
10974 have to return the label. */
10977 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10980 tree label
= NULL_TREE
;
10981 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10983 /* The next token should be an identifier. */
10984 token
= cp_lexer_peek_token (parser
->lexer
);
10985 if (token
->type
!= CPP_NAME
10986 && token
->type
!= CPP_KEYWORD
)
10988 cp_parser_error (parser
, "expected labeled-statement");
10992 /* Remember whether this case or a user-defined label is allowed to fall
10994 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10996 parser
->colon_corrects_to_scope_p
= false;
10997 switch (token
->keyword
)
11001 tree expr
, expr_hi
;
11002 cp_token
*ellipsis
;
11004 /* Consume the `case' token. */
11005 cp_lexer_consume_token (parser
->lexer
);
11006 /* Parse the constant-expression. */
11007 expr
= cp_parser_constant_expression (parser
);
11008 if (check_for_bare_parameter_packs (expr
))
11009 expr
= error_mark_node
;
11011 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11012 if (ellipsis
->type
== CPP_ELLIPSIS
)
11014 /* Consume the `...' token. */
11015 cp_lexer_consume_token (parser
->lexer
);
11016 expr_hi
= cp_parser_constant_expression (parser
);
11017 if (check_for_bare_parameter_packs (expr_hi
))
11018 expr_hi
= error_mark_node
;
11020 /* We don't need to emit warnings here, as the common code
11021 will do this for us. */
11024 expr_hi
= NULL_TREE
;
11026 if (parser
->in_switch_statement_p
)
11028 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11029 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11030 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11033 error_at (token
->location
,
11034 "case label %qE not within a switch statement",
11040 /* Consume the `default' token. */
11041 cp_lexer_consume_token (parser
->lexer
);
11043 if (parser
->in_switch_statement_p
)
11045 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11046 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11047 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11050 error_at (token
->location
, "case label not within a switch statement");
11054 /* Anything else must be an ordinary label. */
11055 label
= finish_label_stmt (cp_parser_identifier (parser
));
11056 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11057 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11061 /* Require the `:' token. */
11062 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11064 /* An ordinary label may optionally be followed by attributes.
11065 However, this is only permitted if the attributes are then
11066 followed by a semicolon. This is because, for backward
11067 compatibility, when parsing
11068 lab: __attribute__ ((unused)) int i;
11069 we want the attribute to attach to "i", not "lab". */
11070 if (label
!= NULL_TREE
11071 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11074 cp_parser_parse_tentatively (parser
);
11075 attrs
= cp_parser_gnu_attributes_opt (parser
);
11076 if (attrs
== NULL_TREE
11077 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11078 cp_parser_abort_tentative_parse (parser
);
11079 else if (!cp_parser_parse_definitely (parser
))
11082 attributes
= attr_chainon (attributes
, attrs
);
11085 if (attributes
!= NULL_TREE
)
11086 cplus_decl_attributes (&label
, attributes
, 0);
11088 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11091 /* Parse an expression-statement.
11093 expression-statement:
11096 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11097 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11098 indicates whether this expression-statement is part of an
11099 expression statement. */
11102 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11104 tree statement
= NULL_TREE
;
11105 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11106 location_t loc
= token
->location
;
11108 /* There might be attribute fallthrough. */
11109 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11111 /* If the next token is a ';', then there is no expression
11113 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11115 statement
= cp_parser_expression (parser
);
11116 if (statement
== error_mark_node
11117 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11119 cp_parser_skip_to_end_of_block_or_statement (parser
);
11120 return error_mark_node
;
11124 /* Handle [[fallthrough]];. */
11125 if (attribute_fallthrough_p (attr
))
11127 /* The next token after the fallthrough attribute is ';'. */
11128 if (statement
== NULL_TREE
)
11129 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11130 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11131 void_type_node
, 0);
11133 warning_at (loc
, OPT_Wattributes
,
11134 "%<fallthrough%> attribute not followed by %<;%>");
11138 /* Allow "[[fallthrough]];", but warn otherwise. */
11139 if (attr
!= NULL_TREE
)
11140 warning_at (loc
, OPT_Wattributes
,
11141 "attributes at the beginning of statement are ignored");
11143 /* Give a helpful message for "A<T>::type t;" and the like. */
11144 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11145 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11147 if (TREE_CODE (statement
) == SCOPE_REF
)
11148 error_at (token
->location
, "need %<typename%> before %qE because "
11149 "%qT is a dependent scope",
11150 statement
, TREE_OPERAND (statement
, 0));
11151 else if (is_overloaded_fn (statement
)
11152 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11155 tree fn
= get_first_fn (statement
);
11156 error_at (token
->location
,
11157 "%<%T::%D%> names the constructor, not the type",
11158 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11162 /* Consume the final `;'. */
11163 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11165 if (in_statement_expr
11166 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11167 /* This is the final expression statement of a statement
11169 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11170 else if (statement
)
11171 statement
= finish_expr_stmt (statement
);
11176 /* Parse a compound-statement.
11178 compound-statement:
11179 { statement-seq [opt] }
11183 compound-statement:
11184 { label-declaration-seq [opt] statement-seq [opt] }
11186 label-declaration-seq:
11188 label-declaration-seq label-declaration
11190 Returns a tree representing the statement. */
11193 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11194 int bcs_flags
, bool function_body
)
11196 tree compound_stmt
;
11197 matching_braces braces
;
11199 /* Consume the `{'. */
11200 if (!braces
.require_open (parser
))
11201 return error_mark_node
;
11202 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11203 && !function_body
&& cxx_dialect
< cxx14
)
11204 pedwarn (input_location
, OPT_Wpedantic
,
11205 "compound-statement in %<constexpr%> function");
11206 /* Begin the compound-statement. */
11207 compound_stmt
= begin_compound_stmt (bcs_flags
);
11208 /* If the next keyword is `__label__' we have a label declaration. */
11209 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11210 cp_parser_label_declaration (parser
);
11211 /* Parse an (optional) statement-seq. */
11212 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11213 /* Finish the compound-statement. */
11214 finish_compound_stmt (compound_stmt
);
11215 /* Consume the `}'. */
11216 braces
.require_close (parser
);
11218 return compound_stmt
;
11221 /* Parse an (optional) statement-seq.
11225 statement-seq [opt] statement */
11228 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11230 /* Scan statements until there aren't any more. */
11233 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11235 /* If we are looking at a `}', then we have run out of
11236 statements; the same is true if we have reached the end
11237 of file, or have stumbled upon a stray '@end'. */
11238 if (token
->type
== CPP_CLOSE_BRACE
11239 || token
->type
== CPP_EOF
11240 || token
->type
== CPP_PRAGMA_EOL
11241 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11244 /* If we are in a compound statement and find 'else' then
11245 something went wrong. */
11246 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11248 if (parser
->in_statement
& IN_IF_STMT
)
11252 token
= cp_lexer_consume_token (parser
->lexer
);
11253 error_at (token
->location
, "%<else%> without a previous %<if%>");
11257 /* Parse the statement. */
11258 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11262 /* Return true if we're looking at (init; cond), false otherwise. */
11265 cp_parser_init_statement_p (cp_parser
*parser
)
11267 /* Save tokens so that we can put them back. */
11268 cp_lexer_save_tokens (parser
->lexer
);
11270 /* Look for ';' that is not nested in () or {}. */
11271 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11272 /*recovering=*/false,
11274 /*consume_paren=*/false);
11276 /* Roll back the tokens we skipped. */
11277 cp_lexer_rollback_tokens (parser
->lexer
);
11282 /* Parse a selection-statement.
11284 selection-statement:
11285 if ( init-statement [opt] condition ) statement
11286 if ( init-statement [opt] condition ) statement else statement
11287 switch ( init-statement [opt] condition ) statement
11289 Returns the new IF_STMT or SWITCH_STMT.
11291 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11292 is a (possibly labeled) if statement which is not enclosed in
11293 braces and has an else clause. This is used to implement
11296 CHAIN is a vector of if-else-if conditions. This is used to implement
11297 -Wduplicated-cond. */
11300 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11305 token_indent_info guard_tinfo
;
11310 /* Peek at the next token. */
11311 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11312 guard_tinfo
= get_token_indent_info (token
);
11314 /* See what kind of keyword it is. */
11315 keyword
= token
->keyword
;
11325 if (keyword
== RID_IF
11326 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11330 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11331 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11332 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11333 "with -std=c++17 or -std=gnu++17");
11336 /* Look for the `('. */
11337 matching_parens parens
;
11338 if (!parens
.require_open (parser
))
11340 cp_parser_skip_to_end_of_statement (parser
);
11341 return error_mark_node
;
11344 /* Begin the selection-statement. */
11345 if (keyword
== RID_IF
)
11347 statement
= begin_if_stmt ();
11348 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11351 statement
= begin_switch_stmt ();
11353 /* Parse the optional init-statement. */
11354 if (cp_parser_init_statement_p (parser
))
11357 if (cxx_dialect
< cxx17
)
11358 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11359 "init-statement in selection statements only available "
11360 "with -std=c++17 or -std=gnu++17");
11361 cp_parser_init_statement (parser
, &decl
);
11364 /* Parse the condition. */
11365 condition
= cp_parser_condition (parser
);
11366 /* Look for the `)'. */
11367 if (!parens
.require_close (parser
))
11368 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11369 /*consume_paren=*/true);
11371 if (keyword
== RID_IF
)
11374 unsigned char in_statement
;
11376 /* Add the condition. */
11377 condition
= finish_if_stmt_cond (condition
, statement
);
11379 if (warn_duplicated_cond
)
11380 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11383 /* Parse the then-clause. */
11384 in_statement
= parser
->in_statement
;
11385 parser
->in_statement
|= IN_IF_STMT
;
11387 /* Outside a template, the non-selected branch of a constexpr
11388 if is a 'discarded statement', i.e. unevaluated. */
11389 bool was_discarded
= in_discarded_stmt
;
11390 bool discard_then
= (cx
&& !processing_template_decl
11391 && integer_zerop (condition
));
11394 in_discarded_stmt
= true;
11395 ++c_inhibit_evaluation_warnings
;
11398 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11401 parser
->in_statement
= in_statement
;
11403 finish_then_clause (statement
);
11407 THEN_CLAUSE (statement
) = NULL_TREE
;
11408 in_discarded_stmt
= was_discarded
;
11409 --c_inhibit_evaluation_warnings
;
11412 /* If the next token is `else', parse the else-clause. */
11413 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11416 bool discard_else
= (cx
&& !processing_template_decl
11417 && integer_nonzerop (condition
));
11420 in_discarded_stmt
= true;
11421 ++c_inhibit_evaluation_warnings
;
11425 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11426 /* Consume the `else' keyword. */
11427 cp_lexer_consume_token (parser
->lexer
);
11428 if (warn_duplicated_cond
)
11430 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11434 /* We've got "if (COND) else if (COND2)". Start
11435 the condition chain and add COND as the first
11437 chain
= new vec
<tree
> ();
11438 if (!CONSTANT_CLASS_P (condition
)
11439 && !TREE_SIDE_EFFECTS (condition
))
11441 /* Wrap it in a NOP_EXPR so that we can set the
11442 location of the condition. */
11443 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11445 SET_EXPR_LOCATION (e
, token
->location
);
11446 chain
->safe_push (e
);
11449 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11452 /* This is if-else without subsequent if. Zap the
11453 condition chain; we would have already warned at
11459 begin_else_clause (statement
);
11460 /* Parse the else-clause. */
11461 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11462 guard_tinfo
, chain
);
11464 finish_else_clause (statement
);
11466 /* If we are currently parsing a then-clause, then
11467 IF_P will not be NULL. We set it to true to
11468 indicate that this if statement has an else clause.
11469 This may trigger the Wparentheses warning below
11470 when we get back up to the parent if statement. */
11476 ELSE_CLAUSE (statement
) = NULL_TREE
;
11477 in_discarded_stmt
= was_discarded
;
11478 --c_inhibit_evaluation_warnings
;
11483 /* This if statement does not have an else clause. If
11484 NESTED_IF is true, then the then-clause has an if
11485 statement which does have an else clause. We warn
11486 about the potential ambiguity. */
11488 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11489 "suggest explicit braces to avoid ambiguous"
11491 if (warn_duplicated_cond
)
11493 /* We don't need the condition chain anymore. */
11499 /* Now we're all done with the if-statement. */
11500 finish_if_stmt (statement
);
11504 bool in_switch_statement_p
;
11505 unsigned char in_statement
;
11507 /* Add the condition. */
11508 finish_switch_cond (condition
, statement
);
11510 /* Parse the body of the switch-statement. */
11511 in_switch_statement_p
= parser
->in_switch_statement_p
;
11512 in_statement
= parser
->in_statement
;
11513 parser
->in_switch_statement_p
= true;
11514 parser
->in_statement
|= IN_SWITCH_STMT
;
11515 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11517 parser
->in_switch_statement_p
= in_switch_statement_p
;
11518 parser
->in_statement
= in_statement
;
11520 /* Now we're all done with the switch-statement. */
11521 finish_switch_stmt (statement
);
11529 cp_parser_error (parser
, "expected selection-statement");
11530 return error_mark_node
;
11534 /* Parse a condition.
11538 type-specifier-seq declarator = initializer-clause
11539 type-specifier-seq declarator braced-init-list
11544 type-specifier-seq declarator asm-specification [opt]
11545 attributes [opt] = assignment-expression
11547 Returns the expression that should be tested. */
11550 cp_parser_condition (cp_parser
* parser
)
11552 cp_decl_specifier_seq type_specifiers
;
11553 const char *saved_message
;
11554 int declares_class_or_enum
;
11556 /* Try the declaration first. */
11557 cp_parser_parse_tentatively (parser
);
11558 /* New types are not allowed in the type-specifier-seq for a
11560 saved_message
= parser
->type_definition_forbidden_message
;
11561 parser
->type_definition_forbidden_message
11562 = G_("types may not be defined in conditions");
11563 /* Parse the type-specifier-seq. */
11564 cp_parser_decl_specifier_seq (parser
,
11565 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11567 &declares_class_or_enum
);
11568 /* Restore the saved message. */
11569 parser
->type_definition_forbidden_message
= saved_message
;
11570 /* If all is well, we might be looking at a declaration. */
11571 if (!cp_parser_error_occurred (parser
))
11574 tree asm_specification
;
11576 cp_declarator
*declarator
;
11577 tree initializer
= NULL_TREE
;
11579 /* Parse the declarator. */
11580 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11581 /*ctor_dtor_or_conv_p=*/NULL
,
11582 /*parenthesized_p=*/NULL
,
11583 /*member_p=*/false,
11584 /*friend_p=*/false);
11585 /* Parse the attributes. */
11586 attributes
= cp_parser_attributes_opt (parser
);
11587 /* Parse the asm-specification. */
11588 asm_specification
= cp_parser_asm_specification_opt (parser
);
11589 /* If the next token is not an `=' or '{', then we might still be
11590 looking at an expression. For example:
11594 looks like a decl-specifier-seq and a declarator -- but then
11595 there is no `=', so this is an expression. */
11596 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11597 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11598 cp_parser_simulate_error (parser
);
11600 /* If we did see an `=' or '{', then we are looking at a declaration
11602 if (cp_parser_parse_definitely (parser
))
11605 bool non_constant_p
;
11606 int flags
= LOOKUP_ONLYCONVERTING
;
11608 /* Create the declaration. */
11609 decl
= start_decl (declarator
, &type_specifiers
,
11610 /*initialized_p=*/true,
11611 attributes
, /*prefix_attributes=*/NULL_TREE
,
11614 /* Parse the initializer. */
11615 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11617 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11618 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11623 /* Consume the `='. */
11624 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11625 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11627 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11628 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11630 /* Process the initializer. */
11631 cp_finish_decl (decl
,
11632 initializer
, !non_constant_p
,
11637 pop_scope (pushed_scope
);
11639 return convert_from_reference (decl
);
11642 /* If we didn't even get past the declarator successfully, we are
11643 definitely not looking at a declaration. */
11645 cp_parser_abort_tentative_parse (parser
);
11647 /* Otherwise, we are looking at an expression. */
11648 return cp_parser_expression (parser
);
11651 /* Parses a for-statement or range-for-statement until the closing ')',
11655 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11657 tree init
, scope
, decl
;
11660 /* Begin the for-statement. */
11661 scope
= begin_for_scope (&init
);
11663 /* Parse the initialization. */
11664 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11667 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11669 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11673 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11674 unsigned short unroll
)
11676 /* Normal for loop */
11677 tree condition
= NULL_TREE
;
11678 tree expression
= NULL_TREE
;
11681 stmt
= begin_for_stmt (scope
, init
);
11682 /* The init-statement has already been parsed in
11683 cp_parser_init_statement, so no work is needed here. */
11684 finish_init_stmt (stmt
);
11686 /* If there's a condition, process it. */
11687 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11688 condition
= cp_parser_condition (parser
);
11691 cp_parser_error (parser
, "missing loop condition in loop with "
11692 "%<GCC ivdep%> pragma");
11693 condition
= error_mark_node
;
11697 cp_parser_error (parser
, "missing loop condition in loop with "
11698 "%<GCC unroll%> pragma");
11699 condition
= error_mark_node
;
11701 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11702 /* Look for the `;'. */
11703 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11705 /* If there's an expression, process it. */
11706 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11707 expression
= cp_parser_expression (parser
);
11708 finish_for_expr (expression
, stmt
);
11713 /* Tries to parse a range-based for-statement:
11716 decl-specifier-seq declarator : expression
11718 The decl-specifier-seq declarator and the `:' are already parsed by
11719 cp_parser_init_statement. If processing_template_decl it returns a
11720 newly created RANGE_FOR_STMT; if not, it is converted to a
11721 regular FOR_STMT. */
11724 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11725 bool ivdep
, unsigned short unroll
)
11727 tree stmt
, range_expr
;
11728 auto_vec
<cxx_binding
*, 16> bindings
;
11729 auto_vec
<tree
, 16> names
;
11730 tree decomp_first_name
= NULL_TREE
;
11731 unsigned int decomp_cnt
= 0;
11733 /* Get the range declaration momentarily out of the way so that
11734 the range expression doesn't clash with it. */
11735 if (range_decl
!= error_mark_node
)
11737 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11739 tree v
= DECL_VALUE_EXPR (range_decl
);
11740 /* For decomposition declaration get all of the corresponding
11741 declarations out of the way. */
11742 if (TREE_CODE (v
) == ARRAY_REF
11743 && VAR_P (TREE_OPERAND (v
, 0))
11744 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11746 tree d
= range_decl
;
11747 range_decl
= TREE_OPERAND (v
, 0);
11748 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11749 decomp_first_name
= d
;
11750 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11752 tree name
= DECL_NAME (d
);
11753 names
.safe_push (name
);
11754 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11755 IDENTIFIER_BINDING (name
)
11756 = IDENTIFIER_BINDING (name
)->previous
;
11760 if (names
.is_empty ())
11762 tree name
= DECL_NAME (range_decl
);
11763 names
.safe_push (name
);
11764 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11765 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11769 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11771 bool expr_non_constant_p
;
11772 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11775 range_expr
= cp_parser_expression (parser
);
11777 /* Put the range declaration(s) back into scope. */
11778 for (unsigned int i
= 0; i
< names
.length (); i
++)
11780 cxx_binding
*binding
= bindings
[i
];
11781 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11782 IDENTIFIER_BINDING (names
[i
]) = binding
;
11785 /* If in template, STMT is converted to a normal for-statement
11786 at instantiation. If not, it is done just ahead. */
11787 if (processing_template_decl
)
11789 if (check_for_bare_parameter_packs (range_expr
))
11790 range_expr
= error_mark_node
;
11791 stmt
= begin_range_for_stmt (scope
, init
);
11793 RANGE_FOR_IVDEP (stmt
) = 1;
11795 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11796 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11797 if (!type_dependent_expression_p (range_expr
)
11798 /* do_auto_deduction doesn't mess with template init-lists. */
11799 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11800 do_range_for_auto_deduction (range_decl
, range_expr
);
11804 stmt
= begin_for_stmt (scope
, init
);
11805 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11806 decomp_first_name
, decomp_cnt
, ivdep
,
11812 /* Subroutine of cp_convert_range_for: given the initializer expression,
11813 builds up the range temporary. */
11816 build_range_temp (tree range_expr
)
11818 tree range_type
, range_temp
;
11820 /* Find out the type deduced by the declaration
11821 `auto &&__range = range_expr'. */
11822 range_type
= cp_build_reference_type (make_auto (), true);
11823 range_type
= do_auto_deduction (range_type
, range_expr
,
11824 type_uses_auto (range_type
));
11826 /* Create the __range variable. */
11827 range_temp
= build_decl (input_location
, VAR_DECL
,
11828 get_identifier ("__for_range"), range_type
);
11829 TREE_USED (range_temp
) = 1;
11830 DECL_ARTIFICIAL (range_temp
) = 1;
11835 /* Used by cp_parser_range_for in template context: we aren't going to
11836 do a full conversion yet, but we still need to resolve auto in the
11837 type of the for-range-declaration if present. This is basically
11838 a shortcut version of cp_convert_range_for. */
11841 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11843 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11846 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11847 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11848 iter_type
= (cp_parser_perform_range_for_lookup
11849 (range_temp
, &begin_dummy
, &end_dummy
));
11852 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11854 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11856 tf_warning_or_error
);
11857 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11858 iter_decl
, auto_node
);
11863 /* Converts a range-based for-statement into a normal
11864 for-statement, as per the definition.
11866 for (RANGE_DECL : RANGE_EXPR)
11869 should be equivalent to:
11872 auto &&__range = RANGE_EXPR;
11873 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11877 RANGE_DECL = *__begin;
11882 If RANGE_EXPR is an array:
11883 BEGIN_EXPR = __range
11884 END_EXPR = __range + ARRAY_SIZE(__range)
11885 Else if RANGE_EXPR has a member 'begin' or 'end':
11886 BEGIN_EXPR = __range.begin()
11887 END_EXPR = __range.end()
11889 BEGIN_EXPR = begin(__range)
11890 END_EXPR = end(__range);
11892 If __range has a member 'begin' but not 'end', or vice versa, we must
11893 still use the second alternative (it will surely fail, however).
11894 When calling begin()/end() in the third alternative we must use
11895 argument dependent lookup, but always considering 'std' as an associated
11899 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11900 tree decomp_first_name
, unsigned int decomp_cnt
,
11901 bool ivdep
, unsigned short unroll
)
11904 tree iter_type
, begin_expr
, end_expr
;
11905 tree condition
, expression
;
11907 range_expr
= mark_lvalue_use (range_expr
);
11909 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11910 /* If an error happened previously do nothing or else a lot of
11911 unhelpful errors would be issued. */
11912 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11917 if (VAR_P (range_expr
)
11918 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11919 /* Can't bind a reference to an array of runtime bound. */
11920 range_temp
= range_expr
;
11923 range_temp
= build_range_temp (range_expr
);
11924 pushdecl (range_temp
);
11925 cp_finish_decl (range_temp
, range_expr
,
11926 /*is_constant_init*/false, NULL_TREE
,
11927 LOOKUP_ONLYCONVERTING
);
11928 range_temp
= convert_from_reference (range_temp
);
11930 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11931 &begin_expr
, &end_expr
);
11934 /* The new for initialization statement. */
11935 begin
= build_decl (input_location
, VAR_DECL
,
11936 get_identifier ("__for_begin"), iter_type
);
11937 TREE_USED (begin
) = 1;
11938 DECL_ARTIFICIAL (begin
) = 1;
11940 cp_finish_decl (begin
, begin_expr
,
11941 /*is_constant_init*/false, NULL_TREE
,
11942 LOOKUP_ONLYCONVERTING
);
11944 if (cxx_dialect
>= cxx17
)
11945 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11946 end
= build_decl (input_location
, VAR_DECL
,
11947 get_identifier ("__for_end"), iter_type
);
11948 TREE_USED (end
) = 1;
11949 DECL_ARTIFICIAL (end
) = 1;
11951 cp_finish_decl (end
, end_expr
,
11952 /*is_constant_init*/false, NULL_TREE
,
11953 LOOKUP_ONLYCONVERTING
);
11955 finish_init_stmt (statement
);
11957 /* The new for condition. */
11958 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11961 NULL
, tf_warning_or_error
);
11962 finish_for_cond (condition
, statement
, ivdep
, unroll
);
11964 /* The new increment expression. */
11965 expression
= finish_unary_op_expr (input_location
,
11966 PREINCREMENT_EXPR
, begin
,
11967 tf_warning_or_error
);
11968 finish_for_expr (expression
, statement
);
11970 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11971 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11973 /* The declaration is initialized with *__begin inside the loop body. */
11974 cp_finish_decl (range_decl
,
11975 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11976 tf_warning_or_error
),
11977 /*is_constant_init*/false, NULL_TREE
,
11978 LOOKUP_ONLYCONVERTING
);
11979 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11980 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11985 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11986 We need to solve both at the same time because the method used
11987 depends on the existence of members begin or end.
11988 Returns the type deduced for the iterator expression. */
11991 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11993 if (error_operand_p (range
))
11995 *begin
= *end
= error_mark_node
;
11996 return error_mark_node
;
11999 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12001 error ("range-based %<for%> expression of type %qT "
12002 "has incomplete type", TREE_TYPE (range
));
12003 *begin
= *end
= error_mark_node
;
12004 return error_mark_node
;
12006 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12008 /* If RANGE is an array, we will use pointer arithmetic. */
12009 *begin
= decay_conversion (range
, tf_warning_or_error
);
12010 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12012 array_type_nelts_top (TREE_TYPE (range
)),
12014 return TREE_TYPE (*begin
);
12018 /* If it is not an array, we must do a bit of magic. */
12019 tree id_begin
, id_end
;
12020 tree member_begin
, member_end
;
12022 *begin
= *end
= error_mark_node
;
12024 id_begin
= get_identifier ("begin");
12025 id_end
= get_identifier ("end");
12026 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12027 /*protect=*/2, /*want_type=*/false,
12028 tf_warning_or_error
);
12029 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12030 /*protect=*/2, /*want_type=*/false,
12031 tf_warning_or_error
);
12033 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12035 /* Use the member functions. */
12036 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12037 *end
= cp_parser_range_for_member_function (range
, id_end
);
12041 /* Use global functions with ADL. */
12042 vec
<tree
, va_gc
> *vec
;
12043 vec
= make_tree_vector ();
12045 vec_safe_push (vec
, range
);
12047 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12048 tf_warning_or_error
);
12049 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12050 tf_warning_or_error
);
12051 member_end
= perform_koenig_lookup (id_end
, vec
,
12052 tf_warning_or_error
);
12053 *end
= finish_call_expr (member_end
, &vec
, false, true,
12054 tf_warning_or_error
);
12056 release_tree_vector (vec
);
12059 /* Last common checks. */
12060 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12062 /* If one of the expressions is an error do no more checks. */
12063 *begin
= *end
= error_mark_node
;
12064 return error_mark_node
;
12066 else if (type_dependent_expression_p (*begin
)
12067 || type_dependent_expression_p (*end
))
12068 /* Can happen, when, eg, in a template context, Koenig lookup
12069 can't resolve begin/end (c++/58503). */
12073 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12074 /* The unqualified type of the __begin and __end temporaries should
12075 be the same, as required by the multiple auto declaration. */
12076 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12078 if (cxx_dialect
>= cxx17
12079 && (build_x_binary_op (input_location
, NE_EXPR
,
12080 *begin
, ERROR_MARK
,
12083 != error_mark_node
))
12084 /* P0184R0 allows __begin and __end to have different types,
12085 but make sure they are comparable so we can give a better
12088 error ("inconsistent begin/end types in range-based %<for%> "
12089 "statement: %qT and %qT",
12090 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12097 /* Helper function for cp_parser_perform_range_for_lookup.
12098 Builds a tree for RANGE.IDENTIFIER(). */
12101 cp_parser_range_for_member_function (tree range
, tree identifier
)
12104 vec
<tree
, va_gc
> *vec
;
12106 member
= finish_class_member_access_expr (range
, identifier
,
12107 false, tf_warning_or_error
);
12108 if (member
== error_mark_node
)
12109 return error_mark_node
;
12111 vec
= make_tree_vector ();
12112 res
= finish_call_expr (member
, &vec
,
12113 /*disallow_virtual=*/false,
12114 /*koenig_p=*/false,
12115 tf_warning_or_error
);
12116 release_tree_vector (vec
);
12120 /* Parse an iteration-statement.
12122 iteration-statement:
12123 while ( condition ) statement
12124 do statement while ( expression ) ;
12125 for ( init-statement condition [opt] ; expression [opt] )
12128 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12131 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12132 unsigned short unroll
)
12137 unsigned char in_statement
;
12138 token_indent_info guard_tinfo
;
12140 /* Peek at the next token. */
12141 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12143 return error_mark_node
;
12145 guard_tinfo
= get_token_indent_info (token
);
12147 /* Remember whether or not we are already within an iteration
12149 in_statement
= parser
->in_statement
;
12151 /* See what kind of keyword it is. */
12152 keyword
= token
->keyword
;
12159 /* Begin the while-statement. */
12160 statement
= begin_while_stmt ();
12161 /* Look for the `('. */
12162 matching_parens parens
;
12163 parens
.require_open (parser
);
12164 /* Parse the condition. */
12165 condition
= cp_parser_condition (parser
);
12166 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12167 /* Look for the `)'. */
12168 parens
.require_close (parser
);
12169 /* Parse the dependent statement. */
12170 parser
->in_statement
= IN_ITERATION_STMT
;
12171 bool prev
= note_iteration_stmt_body_start ();
12172 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12173 note_iteration_stmt_body_end (prev
);
12174 parser
->in_statement
= in_statement
;
12175 /* We're done with the while-statement. */
12176 finish_while_stmt (statement
);
12184 /* Begin the do-statement. */
12185 statement
= begin_do_stmt ();
12186 /* Parse the body of the do-statement. */
12187 parser
->in_statement
= IN_ITERATION_STMT
;
12188 bool prev
= note_iteration_stmt_body_start ();
12189 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12190 note_iteration_stmt_body_end (prev
);
12191 parser
->in_statement
= in_statement
;
12192 finish_do_body (statement
);
12193 /* Look for the `while' keyword. */
12194 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12195 /* Look for the `('. */
12196 matching_parens parens
;
12197 parens
.require_open (parser
);
12198 /* Parse the expression. */
12199 expression
= cp_parser_expression (parser
);
12200 /* We're done with the do-statement. */
12201 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12202 /* Look for the `)'. */
12203 parens
.require_close (parser
);
12204 /* Look for the `;'. */
12205 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12211 /* Look for the `('. */
12212 matching_parens parens
;
12213 parens
.require_open (parser
);
12215 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12217 /* Look for the `)'. */
12218 parens
.require_close (parser
);
12220 /* Parse the body of the for-statement. */
12221 parser
->in_statement
= IN_ITERATION_STMT
;
12222 bool prev
= note_iteration_stmt_body_start ();
12223 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12224 note_iteration_stmt_body_end (prev
);
12225 parser
->in_statement
= in_statement
;
12227 /* We're done with the for-statement. */
12228 finish_for_stmt (statement
);
12233 cp_parser_error (parser
, "expected iteration-statement");
12234 statement
= error_mark_node
;
12241 /* Parse a init-statement or the declarator of a range-based-for.
12242 Returns true if a range-based-for declaration is seen.
12245 expression-statement
12246 simple-declaration */
12249 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12251 /* If the next token is a `;', then we have an empty
12252 expression-statement. Grammatically, this is also a
12253 simple-declaration, but an invalid one, because it does not
12254 declare anything. Therefore, if we did not handle this case
12255 specially, we would issue an error message about an invalid
12257 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12259 bool is_range_for
= false;
12260 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12262 /* A colon is used in range-based for. */
12263 parser
->colon_corrects_to_scope_p
= false;
12265 /* We're going to speculatively look for a declaration, falling back
12266 to an expression, if necessary. */
12267 cp_parser_parse_tentatively (parser
);
12268 /* Parse the declaration. */
12269 cp_parser_simple_declaration (parser
,
12270 /*function_definition_allowed_p=*/false,
12272 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12275 /* It is a range-for, consume the ':' */
12276 cp_lexer_consume_token (parser
->lexer
);
12277 is_range_for
= true;
12278 if (cxx_dialect
< cxx11
)
12280 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12281 "range-based %<for%> loops only available with "
12282 "-std=c++11 or -std=gnu++11");
12283 *decl
= error_mark_node
;
12287 /* The ';' is not consumed yet because we told
12288 cp_parser_simple_declaration not to. */
12289 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12291 if (cp_parser_parse_definitely (parser
))
12292 return is_range_for
;
12293 /* If the tentative parse failed, then we shall need to look for an
12294 expression-statement. */
12296 /* If we are here, it is an expression-statement. */
12297 cp_parser_expression_statement (parser
, NULL_TREE
);
12301 /* Parse a jump-statement.
12306 return expression [opt] ;
12307 return braced-init-list ;
12313 goto * expression ;
12315 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12318 cp_parser_jump_statement (cp_parser
* parser
)
12320 tree statement
= error_mark_node
;
12323 unsigned char in_statement
;
12325 /* Peek at the next token. */
12326 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12328 return error_mark_node
;
12330 /* See what kind of keyword it is. */
12331 keyword
= token
->keyword
;
12335 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12336 switch (in_statement
)
12339 error_at (token
->location
, "break statement not within loop or switch");
12342 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12343 || in_statement
== IN_ITERATION_STMT
);
12344 statement
= finish_break_stmt ();
12345 if (in_statement
== IN_ITERATION_STMT
)
12346 break_maybe_infinite_loop ();
12349 error_at (token
->location
, "invalid exit from OpenMP structured block");
12352 error_at (token
->location
, "break statement used with OpenMP for loop");
12355 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12359 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12362 error_at (token
->location
, "continue statement not within a loop");
12364 /* Fall through. */
12365 case IN_ITERATION_STMT
:
12367 statement
= finish_continue_stmt ();
12370 error_at (token
->location
, "invalid exit from OpenMP structured block");
12373 gcc_unreachable ();
12375 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12381 bool expr_non_constant_p
;
12383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12385 cp_lexer_set_source_position (parser
->lexer
);
12386 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12387 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12389 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12390 expr
= cp_parser_expression (parser
);
12392 /* If the next token is a `;', then there is no
12395 /* Build the return-statement. */
12396 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12397 /* Don't deduce from a discarded return statement. */;
12399 statement
= finish_return_stmt (expr
);
12400 /* Look for the final `;'. */
12401 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12406 if (parser
->in_function_body
12407 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12409 error ("%<goto%> in %<constexpr%> function");
12410 cp_function_chain
->invalid_constexpr
= true;
12413 /* Create the goto-statement. */
12414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12416 /* Issue a warning about this use of a GNU extension. */
12417 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12418 /* Consume the '*' token. */
12419 cp_lexer_consume_token (parser
->lexer
);
12420 /* Parse the dependent expression. */
12421 finish_goto_stmt (cp_parser_expression (parser
));
12424 finish_goto_stmt (cp_parser_identifier (parser
));
12425 /* Look for the final `;'. */
12426 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12430 cp_parser_error (parser
, "expected jump-statement");
12437 /* Parse a declaration-statement.
12439 declaration-statement:
12440 block-declaration */
12443 cp_parser_declaration_statement (cp_parser
* parser
)
12447 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12448 p
= obstack_alloc (&declarator_obstack
, 0);
12450 /* Parse the block-declaration. */
12451 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12453 /* Free any declarators allocated. */
12454 obstack_free (&declarator_obstack
, p
);
12457 /* Some dependent statements (like `if (cond) statement'), are
12458 implicitly in their own scope. In other words, if the statement is
12459 a single statement (as opposed to a compound-statement), it is
12460 none-the-less treated as if it were enclosed in braces. Any
12461 declarations appearing in the dependent statement are out of scope
12462 after control passes that point. This function parses a statement,
12463 but ensures that is in its own scope, even if it is not a
12464 compound-statement.
12466 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12467 is a (possibly labeled) if statement which is not enclosed in
12468 braces and has an else clause. This is used to implement
12471 CHAIN is a vector of if-else-if conditions. This is used to implement
12474 Returns the new statement. */
12477 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12478 const token_indent_info
&guard_tinfo
,
12482 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12483 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12484 token_indent_info body_tinfo
12485 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12490 /* Mark if () ; with a special NOP_EXPR. */
12491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12493 cp_lexer_consume_token (parser
->lexer
);
12494 statement
= add_stmt (build_empty_stmt (body_loc
));
12496 if (guard_tinfo
.keyword
== RID_IF
12497 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12498 warning_at (body_loc
, OPT_Wempty_body
,
12499 "suggest braces around empty body in an %<if%> statement");
12500 else if (guard_tinfo
.keyword
== RID_ELSE
)
12501 warning_at (body_loc
, OPT_Wempty_body
,
12502 "suggest braces around empty body in an %<else%> statement");
12504 /* if a compound is opened, we simply parse the statement directly. */
12505 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12506 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12507 /* If the token is not a `{', then we must take special action. */
12510 /* Create a compound-statement. */
12511 statement
= begin_compound_stmt (0);
12512 /* Parse the dependent-statement. */
12513 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12514 &body_loc_after_labels
);
12515 /* Finish the dummy compound-statement. */
12516 finish_compound_stmt (statement
);
12519 token_indent_info next_tinfo
12520 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12521 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12523 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12524 && next_tinfo
.type
!= CPP_SEMICOLON
)
12525 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12526 guard_tinfo
.location
, guard_tinfo
.keyword
);
12528 /* Return the statement. */
12532 /* For some dependent statements (like `while (cond) statement'), we
12533 have already created a scope. Therefore, even if the dependent
12534 statement is a compound-statement, we do not want to create another
12538 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12539 const token_indent_info
&guard_tinfo
)
12541 /* If the token is a `{', then we must take special action. */
12542 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12544 token_indent_info body_tinfo
12545 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12546 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12548 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12549 &loc_after_labels
);
12550 token_indent_info next_tinfo
12551 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12552 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12554 if (loc_after_labels
!= UNKNOWN_LOCATION
12555 && next_tinfo
.type
!= CPP_SEMICOLON
)
12556 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12557 guard_tinfo
.location
,
12558 guard_tinfo
.keyword
);
12562 /* Avoid calling cp_parser_compound_statement, so that we
12563 don't create a new scope. Do everything else by hand. */
12564 matching_braces braces
;
12565 braces
.require_open (parser
);
12566 /* If the next keyword is `__label__' we have a label declaration. */
12567 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12568 cp_parser_label_declaration (parser
);
12569 /* Parse an (optional) statement-seq. */
12570 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12571 braces
.require_close (parser
);
12575 /* Declarations [gram.dcl.dcl] */
12577 /* Parse an optional declaration-sequence.
12581 declaration-seq declaration */
12584 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12590 token
= cp_lexer_peek_token (parser
->lexer
);
12592 if (token
->type
== CPP_CLOSE_BRACE
12593 || token
->type
== CPP_EOF
12594 || token
->type
== CPP_PRAGMA_EOL
)
12597 if (token
->type
== CPP_SEMICOLON
)
12599 /* A declaration consisting of a single semicolon is
12600 invalid. Allow it unless we're being pedantic. */
12601 cp_lexer_consume_token (parser
->lexer
);
12602 if (!in_system_header_at (input_location
))
12603 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12607 /* If we're entering or exiting a region that's implicitly
12608 extern "C", modify the lang context appropriately. */
12609 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12611 push_lang_context (lang_name_c
);
12612 parser
->implicit_extern_c
= true;
12614 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12616 pop_lang_context ();
12617 parser
->implicit_extern_c
= false;
12620 if (token
->type
== CPP_PRAGMA
)
12622 /* A top-level declaration can consist solely of a #pragma.
12623 A nested declaration cannot, so this is done here and not
12624 in cp_parser_declaration. (A #pragma at block scope is
12625 handled in cp_parser_statement.) */
12626 cp_parser_pragma (parser
, pragma_external
, NULL
);
12630 /* Parse the declaration itself. */
12631 cp_parser_declaration (parser
);
12635 /* Parse a declaration.
12639 function-definition
12640 template-declaration
12641 explicit-instantiation
12642 explicit-specialization
12643 linkage-specification
12644 namespace-definition
12652 __extension__ declaration */
12655 cp_parser_declaration (cp_parser
* parser
)
12659 int saved_pedantic
;
12661 tree attributes
= NULL_TREE
;
12663 /* Check for the `__extension__' keyword. */
12664 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12666 /* Parse the qualified declaration. */
12667 cp_parser_declaration (parser
);
12668 /* Restore the PEDANTIC flag. */
12669 pedantic
= saved_pedantic
;
12674 /* Try to figure out what kind of declaration is present. */
12675 token1
= *cp_lexer_peek_token (parser
->lexer
);
12677 if (token1
.type
!= CPP_EOF
)
12678 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12681 token2
.type
= CPP_EOF
;
12682 token2
.keyword
= RID_MAX
;
12685 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12686 p
= obstack_alloc (&declarator_obstack
, 0);
12688 /* If the next token is `extern' and the following token is a string
12689 literal, then we have a linkage specification. */
12690 if (token1
.keyword
== RID_EXTERN
12691 && cp_parser_is_pure_string_literal (&token2
))
12692 cp_parser_linkage_specification (parser
);
12693 /* If the next token is `template', then we have either a template
12694 declaration, an explicit instantiation, or an explicit
12696 else if (token1
.keyword
== RID_TEMPLATE
)
12698 /* `template <>' indicates a template specialization. */
12699 if (token2
.type
== CPP_LESS
12700 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12701 cp_parser_explicit_specialization (parser
);
12702 /* `template <' indicates a template declaration. */
12703 else if (token2
.type
== CPP_LESS
)
12704 cp_parser_template_declaration (parser
, /*member_p=*/false);
12705 /* Anything else must be an explicit instantiation. */
12707 cp_parser_explicit_instantiation (parser
);
12709 /* If the next token is `export', then we have a template
12711 else if (token1
.keyword
== RID_EXPORT
)
12712 cp_parser_template_declaration (parser
, /*member_p=*/false);
12713 /* If the next token is `extern', 'static' or 'inline' and the one
12714 after that is `template', we have a GNU extended explicit
12715 instantiation directive. */
12716 else if (cp_parser_allow_gnu_extensions_p (parser
)
12717 && (token1
.keyword
== RID_EXTERN
12718 || token1
.keyword
== RID_STATIC
12719 || token1
.keyword
== RID_INLINE
)
12720 && token2
.keyword
== RID_TEMPLATE
)
12721 cp_parser_explicit_instantiation (parser
);
12722 /* If the next token is `namespace', check for a named or unnamed
12723 namespace definition. */
12724 else if (token1
.keyword
== RID_NAMESPACE
12725 && (/* A named namespace definition. */
12726 (token2
.type
== CPP_NAME
12727 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12729 || (token2
.type
== CPP_OPEN_SQUARE
12730 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12731 == CPP_OPEN_SQUARE
)
12732 /* An unnamed namespace definition. */
12733 || token2
.type
== CPP_OPEN_BRACE
12734 || token2
.keyword
== RID_ATTRIBUTE
))
12735 cp_parser_namespace_definition (parser
);
12736 /* An inline (associated) namespace definition. */
12737 else if (token1
.keyword
== RID_INLINE
12738 && token2
.keyword
== RID_NAMESPACE
)
12739 cp_parser_namespace_definition (parser
);
12740 /* Objective-C++ declaration/definition. */
12741 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12742 cp_parser_objc_declaration (parser
, NULL_TREE
);
12743 else if (c_dialect_objc ()
12744 && token1
.keyword
== RID_ATTRIBUTE
12745 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12746 cp_parser_objc_declaration (parser
, attributes
);
12747 /* At this point we may have a template declared by a concept
12749 else if (flag_concepts
12750 && cp_parser_template_declaration_after_export (parser
,
12751 /*member_p=*/false))
12754 /* Try to parse a block-declaration, or a function-definition. */
12755 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12757 /* Free any declarators allocated. */
12758 obstack_free (&declarator_obstack
, p
);
12761 /* Parse a block-declaration.
12766 namespace-alias-definition
12773 __extension__ block-declaration
12778 static_assert-declaration
12780 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12781 part of a declaration-statement. */
12784 cp_parser_block_declaration (cp_parser
*parser
,
12788 int saved_pedantic
;
12790 /* Check for the `__extension__' keyword. */
12791 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12793 /* Parse the qualified declaration. */
12794 cp_parser_block_declaration (parser
, statement_p
);
12795 /* Restore the PEDANTIC flag. */
12796 pedantic
= saved_pedantic
;
12801 /* Peek at the next token to figure out which kind of declaration is
12803 token1
= cp_lexer_peek_token (parser
->lexer
);
12805 /* If the next keyword is `asm', we have an asm-definition. */
12806 if (token1
->keyword
== RID_ASM
)
12809 cp_parser_commit_to_tentative_parse (parser
);
12810 cp_parser_asm_definition (parser
);
12812 /* If the next keyword is `namespace', we have a
12813 namespace-alias-definition. */
12814 else if (token1
->keyword
== RID_NAMESPACE
)
12815 cp_parser_namespace_alias_definition (parser
);
12816 /* If the next keyword is `using', we have a
12817 using-declaration, a using-directive, or an alias-declaration. */
12818 else if (token1
->keyword
== RID_USING
)
12823 cp_parser_commit_to_tentative_parse (parser
);
12824 /* If the token after `using' is `namespace', then we have a
12825 using-directive. */
12826 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12827 if (token2
->keyword
== RID_NAMESPACE
)
12828 cp_parser_using_directive (parser
);
12829 /* If the second token after 'using' is '=', then we have an
12830 alias-declaration. */
12831 else if (cxx_dialect
>= cxx11
12832 && token2
->type
== CPP_NAME
12833 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12834 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12835 cp_parser_alias_declaration (parser
);
12836 /* Otherwise, it's a using-declaration. */
12838 cp_parser_using_declaration (parser
,
12839 /*access_declaration_p=*/false);
12841 /* If the next keyword is `__label__' we have a misplaced label
12843 else if (token1
->keyword
== RID_LABEL
)
12845 cp_lexer_consume_token (parser
->lexer
);
12846 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12847 cp_parser_skip_to_end_of_statement (parser
);
12848 /* If the next token is now a `;', consume it. */
12849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12850 cp_lexer_consume_token (parser
->lexer
);
12852 /* If the next token is `static_assert' we have a static assertion. */
12853 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12854 cp_parser_static_assert (parser
, /*member_p=*/false);
12855 /* Anything else must be a simple-declaration. */
12857 cp_parser_simple_declaration (parser
, !statement_p
,
12858 /*maybe_range_for_decl*/NULL
);
12861 /* Parse a simple-declaration.
12863 simple-declaration:
12864 decl-specifier-seq [opt] init-declarator-list [opt] ;
12865 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12866 brace-or-equal-initializer ;
12868 init-declarator-list:
12870 init-declarator-list , init-declarator
12872 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12873 function-definition as a simple-declaration.
12875 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12876 parsed declaration if it is an uninitialized single declarator not followed
12877 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12878 if present, will not be consumed. */
12881 cp_parser_simple_declaration (cp_parser
* parser
,
12882 bool function_definition_allowed_p
,
12883 tree
*maybe_range_for_decl
)
12885 cp_decl_specifier_seq decl_specifiers
;
12886 int declares_class_or_enum
;
12887 bool saw_declarator
;
12888 location_t comma_loc
= UNKNOWN_LOCATION
;
12889 location_t init_loc
= UNKNOWN_LOCATION
;
12891 if (maybe_range_for_decl
)
12892 *maybe_range_for_decl
= NULL_TREE
;
12894 /* Defer access checks until we know what is being declared; the
12895 checks for names appearing in the decl-specifier-seq should be
12896 done as if we were in the scope of the thing being declared. */
12897 push_deferring_access_checks (dk_deferred
);
12899 /* Parse the decl-specifier-seq. We have to keep track of whether
12900 or not the decl-specifier-seq declares a named class or
12901 enumeration type, since that is the only case in which the
12902 init-declarator-list is allowed to be empty.
12906 In a simple-declaration, the optional init-declarator-list can be
12907 omitted only when declaring a class or enumeration, that is when
12908 the decl-specifier-seq contains either a class-specifier, an
12909 elaborated-type-specifier, or an enum-specifier. */
12910 cp_parser_decl_specifier_seq (parser
,
12911 CP_PARSER_FLAGS_OPTIONAL
,
12913 &declares_class_or_enum
);
12914 /* We no longer need to defer access checks. */
12915 stop_deferring_access_checks ();
12917 /* In a block scope, a valid declaration must always have a
12918 decl-specifier-seq. By not trying to parse declarators, we can
12919 resolve the declaration/expression ambiguity more quickly. */
12920 if (!function_definition_allowed_p
12921 && !decl_specifiers
.any_specifiers_p
)
12923 cp_parser_error (parser
, "expected declaration");
12927 /* If the next two tokens are both identifiers, the code is
12928 erroneous. The usual cause of this situation is code like:
12932 where "T" should name a type -- but does not. */
12933 if (!decl_specifiers
.any_type_specifiers_p
12934 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12936 /* If parsing tentatively, we should commit; we really are
12937 looking at a declaration. */
12938 cp_parser_commit_to_tentative_parse (parser
);
12943 /* If we have seen at least one decl-specifier, and the next token
12944 is not a parenthesis, then we must be looking at a declaration.
12945 (After "int (" we might be looking at a functional cast.) */
12946 if (decl_specifiers
.any_specifiers_p
12947 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12948 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12949 && !cp_parser_error_occurred (parser
))
12950 cp_parser_commit_to_tentative_parse (parser
);
12952 /* Look for C++17 decomposition declaration. */
12953 for (size_t n
= 1; ; n
++)
12954 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12955 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12957 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12958 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12959 && decl_specifiers
.any_specifiers_p
)
12962 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12963 maybe_range_for_decl
,
12966 /* The next token should be either a `,' or a `;'. */
12967 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12968 /* If it's a `;', we are done. */
12969 if (token
->type
== CPP_SEMICOLON
)
12971 else if (maybe_range_for_decl
)
12973 if (*maybe_range_for_decl
== NULL_TREE
)
12974 *maybe_range_for_decl
= error_mark_node
;
12977 /* Anything else is an error. */
12980 /* If we have already issued an error message we don't need
12981 to issue another one. */
12982 if ((decl
!= error_mark_node
12983 && DECL_INITIAL (decl
) != error_mark_node
)
12984 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12985 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12986 /* Skip tokens until we reach the end of the statement. */
12987 cp_parser_skip_to_end_of_statement (parser
);
12988 /* If the next token is now a `;', consume it. */
12989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12990 cp_lexer_consume_token (parser
->lexer
);
12998 bool auto_specifier_p
;
12999 /* NULL_TREE if both variable and function declaration are allowed,
13000 error_mark_node if function declaration are not allowed and
13001 a FUNCTION_DECL that should be diagnosed if it is followed by
13002 variable declarations. */
13003 tree auto_function_declaration
;
13005 last_type
= NULL_TREE
;
13007 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13008 auto_function_declaration
= NULL_TREE
;
13010 /* Keep going until we hit the `;' at the end of the simple
13012 saw_declarator
= false;
13013 while (cp_lexer_next_token_is_not (parser
->lexer
,
13017 bool function_definition_p
;
13019 tree auto_result
= NULL_TREE
;
13021 if (saw_declarator
)
13023 /* If we are processing next declarator, comma is expected */
13024 token
= cp_lexer_peek_token (parser
->lexer
);
13025 gcc_assert (token
->type
== CPP_COMMA
);
13026 cp_lexer_consume_token (parser
->lexer
);
13027 if (maybe_range_for_decl
)
13029 *maybe_range_for_decl
= error_mark_node
;
13030 if (comma_loc
== UNKNOWN_LOCATION
)
13031 comma_loc
= token
->location
;
13035 saw_declarator
= true;
13037 /* Parse the init-declarator. */
13038 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13040 function_definition_allowed_p
,
13041 /*member_p=*/false,
13042 declares_class_or_enum
,
13043 &function_definition_p
,
13044 maybe_range_for_decl
,
13047 /* If an error occurred while parsing tentatively, exit quickly.
13048 (That usually happens when in the body of a function; each
13049 statement is treated as a declaration-statement until proven
13051 if (cp_parser_error_occurred (parser
))
13054 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13056 /* If the init-declarator-list contains more than one
13057 init-declarator, they shall all form declarations of
13059 if (auto_function_declaration
== NULL_TREE
)
13060 auto_function_declaration
13061 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13062 else if (TREE_CODE (decl
) == FUNCTION_DECL
13063 || auto_function_declaration
!= error_mark_node
)
13065 error_at (decl_specifiers
.locations
[ds_type_spec
],
13066 "non-variable %qD in declaration with more than one "
13067 "declarator with placeholder type",
13068 TREE_CODE (decl
) == FUNCTION_DECL
13069 ? decl
: auto_function_declaration
);
13070 auto_function_declaration
= error_mark_node
;
13075 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13078 && last_type
!= error_mark_node
13079 && !same_type_p (auto_result
, last_type
))
13081 /* If the list of declarators contains more than one declarator,
13082 the type of each declared variable is determined as described
13083 above. If the type deduced for the template parameter U is not
13084 the same in each deduction, the program is ill-formed. */
13085 error_at (decl_specifiers
.locations
[ds_type_spec
],
13086 "inconsistent deduction for %qT: %qT and then %qT",
13087 decl_specifiers
.type
, last_type
, auto_result
);
13088 last_type
= error_mark_node
;
13091 last_type
= auto_result
;
13094 /* Handle function definitions specially. */
13095 if (function_definition_p
)
13097 /* If the next token is a `,', then we are probably
13098 processing something like:
13102 which is erroneous. */
13103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13105 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13106 error_at (token
->location
,
13108 " declarations and function-definitions is forbidden");
13110 /* Otherwise, we're done with the list of declarators. */
13113 pop_deferring_access_checks ();
13117 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13118 *maybe_range_for_decl
= decl
;
13119 /* The next token should be either a `,' or a `;'. */
13120 token
= cp_lexer_peek_token (parser
->lexer
);
13121 /* If it's a `,', there are more declarators to come. */
13122 if (token
->type
== CPP_COMMA
)
13123 /* will be consumed next time around */;
13124 /* If it's a `;', we are done. */
13125 else if (token
->type
== CPP_SEMICOLON
)
13127 else if (maybe_range_for_decl
)
13129 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13130 permerror (decl_specifiers
.locations
[ds_type_spec
],
13131 "types may not be defined in a for-range-declaration");
13134 /* Anything else is an error. */
13137 /* If we have already issued an error message we don't need
13138 to issue another one. */
13139 if ((decl
!= error_mark_node
13140 && DECL_INITIAL (decl
) != error_mark_node
)
13141 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13142 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13143 /* Skip tokens until we reach the end of the statement. */
13144 cp_parser_skip_to_end_of_statement (parser
);
13145 /* If the next token is now a `;', consume it. */
13146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13147 cp_lexer_consume_token (parser
->lexer
);
13150 /* After the first time around, a function-definition is not
13151 allowed -- even if it was OK at first. For example:
13156 function_definition_allowed_p
= false;
13159 /* Issue an error message if no declarators are present, and the
13160 decl-specifier-seq does not itself declare a class or
13161 enumeration: [dcl.dcl]/3. */
13162 if (!saw_declarator
)
13164 if (cp_parser_declares_only_class_p (parser
))
13166 if (!declares_class_or_enum
13167 && decl_specifiers
.type
13168 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13169 /* Ensure an error is issued anyway when finish_decltype_type,
13170 called via cp_parser_decl_specifier_seq, returns a class or
13171 an enumeration (c++/51786). */
13172 decl_specifiers
.type
= NULL_TREE
;
13173 shadow_tag (&decl_specifiers
);
13175 /* Perform any deferred access checks. */
13176 perform_deferred_access_checks (tf_warning_or_error
);
13179 /* Consume the `;'. */
13181 if (!maybe_range_for_decl
)
13182 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13183 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13185 if (init_loc
!= UNKNOWN_LOCATION
)
13186 error_at (init_loc
, "initializer in range-based %<for%> loop");
13187 if (comma_loc
!= UNKNOWN_LOCATION
)
13188 error_at (comma_loc
,
13189 "multiple declarations in range-based %<for%> loop");
13193 pop_deferring_access_checks ();
13196 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13197 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13201 cp_parser_decomposition_declaration (cp_parser
*parser
,
13202 cp_decl_specifier_seq
*decl_specifiers
,
13203 tree
*maybe_range_for_decl
,
13204 location_t
*init_loc
)
13206 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13207 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13208 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13210 /* Parse the identifier-list. */
13211 auto_vec
<cp_expr
, 10> v
;
13212 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13215 cp_expr e
= cp_parser_identifier (parser
);
13216 if (e
.get_value () == error_mark_node
)
13219 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13221 cp_lexer_consume_token (parser
->lexer
);
13224 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13225 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13227 end_loc
= UNKNOWN_LOCATION
;
13228 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13231 cp_lexer_consume_token (parser
->lexer
);
13234 cp_parser_skip_to_end_of_statement (parser
);
13235 return error_mark_node
;
13239 if (cxx_dialect
< cxx17
)
13240 pedwarn (loc
, 0, "structured bindings only available with "
13241 "-std=c++17 or -std=gnu++17");
13244 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13245 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13246 declarator
->id_loc
= loc
;
13247 if (ref_qual
!= REF_QUAL_NONE
)
13248 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13249 ref_qual
== REF_QUAL_RVALUE
,
13251 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13252 NULL_TREE
, decl_specifiers
->attributes
,
13254 tree orig_decl
= decl
;
13258 cp_decl_specifier_seq decl_specs
;
13259 clear_decl_specs (&decl_specs
);
13260 decl_specs
.type
= make_auto ();
13262 FOR_EACH_VEC_ELT (v
, i
, e
)
13265 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13267 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13268 declarator
->id_loc
= e
.get_location ();
13269 tree elt_pushed_scope
;
13270 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13271 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13272 if (decl2
== error_mark_node
)
13273 decl
= error_mark_node
;
13274 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13276 /* Ensure we've diagnosed redeclaration if we aren't creating
13278 gcc_assert (errorcount
);
13279 decl
= error_mark_node
;
13283 if (elt_pushed_scope
)
13284 pop_scope (elt_pushed_scope
);
13289 error_at (loc
, "empty structured binding declaration");
13290 decl
= error_mark_node
;
13293 if (maybe_range_for_decl
== NULL
13294 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13296 bool non_constant_p
= false, is_direct_init
= false;
13297 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13298 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13300 if (initializer
== NULL_TREE
13301 || (TREE_CODE (initializer
) == TREE_LIST
13302 && TREE_CHAIN (initializer
))
13304 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13305 && CONSTRUCTOR_NELTS (initializer
) != 1))
13307 error_at (loc
, "invalid initializer for structured binding "
13309 initializer
= error_mark_node
;
13312 if (decl
!= error_mark_node
)
13314 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13315 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13316 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13317 cp_finish_decomp (decl
, prev
, v
.length ());
13320 else if (decl
!= error_mark_node
)
13322 *maybe_range_for_decl
= prev
;
13323 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13324 the underlying DECL. */
13325 cp_finish_decomp (decl
, prev
, v
.length ());
13329 pop_scope (pushed_scope
);
13331 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13333 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13334 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13340 /* Parse a decl-specifier-seq.
13342 decl-specifier-seq:
13343 decl-specifier-seq [opt] decl-specifier
13344 decl-specifier attribute-specifier-seq [opt] (C++11)
13347 storage-class-specifier
13358 Concepts Extension:
13363 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13365 The parser flags FLAGS is used to control type-specifier parsing.
13367 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13370 1: one of the decl-specifiers is an elaborated-type-specifier
13371 (i.e., a type declaration)
13372 2: one of the decl-specifiers is an enum-specifier or a
13373 class-specifier (i.e., a type definition)
13378 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13379 cp_parser_flags flags
,
13380 cp_decl_specifier_seq
*decl_specs
,
13381 int* declares_class_or_enum
)
13383 bool constructor_possible_p
= !parser
->in_declarator_p
;
13384 bool found_decl_spec
= false;
13385 cp_token
*start_token
= NULL
;
13388 /* Clear DECL_SPECS. */
13389 clear_decl_specs (decl_specs
);
13391 /* Assume no class or enumeration type is declared. */
13392 *declares_class_or_enum
= 0;
13394 /* Keep reading specifiers until there are no more to read. */
13397 bool constructor_p
;
13401 /* Peek at the next token. */
13402 token
= cp_lexer_peek_token (parser
->lexer
);
13404 /* Save the first token of the decl spec list for error
13407 start_token
= token
;
13408 /* Handle attributes. */
13409 if (cp_next_tokens_can_be_attribute_p (parser
))
13411 /* Parse the attributes. */
13412 tree attrs
= cp_parser_attributes_opt (parser
);
13414 /* In a sequence of declaration specifiers, c++11 attributes
13415 appertain to the type that precede them. In that case
13418 The attribute-specifier-seq affects the type only for
13419 the declaration it appears in, not other declarations
13420 involving the same type.
13422 But for now let's force the user to position the
13423 attribute either at the beginning of the declaration or
13424 after the declarator-id, which would clearly mean that it
13425 applies to the declarator. */
13426 if (cxx11_attribute_p (attrs
))
13428 if (!found_decl_spec
)
13429 /* The c++11 attribute is at the beginning of the
13430 declaration. It appertains to the entity being
13434 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13436 /* This is an attribute following a
13437 class-specifier. */
13438 if (decl_specs
->type_definition_p
)
13439 warn_misplaced_attr_for_class_type (token
->location
,
13445 decl_specs
->std_attributes
13446 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13447 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13448 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13454 decl_specs
->attributes
13455 = attr_chainon (decl_specs
->attributes
, attrs
);
13456 if (decl_specs
->locations
[ds_attribute
] == 0)
13457 decl_specs
->locations
[ds_attribute
] = token
->location
;
13460 /* Assume we will find a decl-specifier keyword. */
13461 found_decl_spec
= true;
13462 /* If the next token is an appropriate keyword, we can simply
13463 add it to the list. */
13464 switch (token
->keyword
)
13470 if (!at_class_scope_p ())
13472 gcc_rich_location
richloc (token
->location
);
13473 richloc
.add_fixit_remove ();
13474 error_at (&richloc
, "%<friend%> used outside of class");
13475 cp_lexer_purge_token (parser
->lexer
);
13480 /* Consume the token. */
13481 cp_lexer_consume_token (parser
->lexer
);
13485 case RID_CONSTEXPR
:
13487 cp_lexer_consume_token (parser
->lexer
);
13492 cp_lexer_consume_token (parser
->lexer
);
13495 /* function-specifier:
13502 cp_parser_function_specifier_opt (parser
, decl_specs
);
13509 /* Consume the token. */
13510 cp_lexer_consume_token (parser
->lexer
);
13511 /* A constructor declarator cannot appear in a typedef. */
13512 constructor_possible_p
= false;
13513 /* The "typedef" keyword can only occur in a declaration; we
13514 may as well commit at this point. */
13515 cp_parser_commit_to_tentative_parse (parser
);
13517 if (decl_specs
->storage_class
!= sc_none
)
13518 decl_specs
->conflicting_specifiers_p
= true;
13521 /* storage-class-specifier:
13531 if (cxx_dialect
== cxx98
)
13533 /* Consume the token. */
13534 cp_lexer_consume_token (parser
->lexer
);
13536 /* Complain about `auto' as a storage specifier, if
13537 we're complaining about C++0x compatibility. */
13538 gcc_rich_location
richloc (token
->location
);
13539 richloc
.add_fixit_remove ();
13540 warning_at (&richloc
, OPT_Wc__11_compat
,
13541 "%<auto%> changes meaning in C++11; "
13542 "please remove it");
13544 /* Set the storage class anyway. */
13545 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13549 /* C++0x auto type-specifier. */
13550 found_decl_spec
= false;
13557 /* Consume the token. */
13558 cp_lexer_consume_token (parser
->lexer
);
13559 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13563 /* Consume the token. */
13565 cp_lexer_consume_token (parser
->lexer
);
13569 /* We did not yet find a decl-specifier yet. */
13570 found_decl_spec
= false;
13574 if (found_decl_spec
13575 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13576 && token
->keyword
!= RID_CONSTEXPR
)
13577 error ("decl-specifier invalid in condition");
13579 if (found_decl_spec
13580 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13581 && token
->keyword
!= RID_MUTABLE
13582 && token
->keyword
!= RID_CONSTEXPR
)
13583 error_at (token
->location
, "%qD invalid in lambda",
13584 ridpointers
[token
->keyword
]);
13587 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13589 /* Constructors are a special case. The `S' in `S()' is not a
13590 decl-specifier; it is the beginning of the declarator. */
13592 = (!found_decl_spec
13593 && constructor_possible_p
13594 && (cp_parser_constructor_declarator_p
13595 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13597 /* If we don't have a DECL_SPEC yet, then we must be looking at
13598 a type-specifier. */
13599 if (!found_decl_spec
&& !constructor_p
)
13601 int decl_spec_declares_class_or_enum
;
13602 bool is_cv_qualifier
;
13606 = cp_parser_type_specifier (parser
, flags
,
13608 /*is_declaration=*/true,
13609 &decl_spec_declares_class_or_enum
,
13611 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13613 /* If this type-specifier referenced a user-defined type
13614 (a typedef, class-name, etc.), then we can't allow any
13615 more such type-specifiers henceforth.
13619 The longest sequence of decl-specifiers that could
13620 possibly be a type name is taken as the
13621 decl-specifier-seq of a declaration. The sequence shall
13622 be self-consistent as described below.
13626 As a general rule, at most one type-specifier is allowed
13627 in the complete decl-specifier-seq of a declaration. The
13628 only exceptions are the following:
13630 -- const or volatile can be combined with any other
13633 -- signed or unsigned can be combined with char, long,
13641 void g (const int Pc);
13643 Here, Pc is *not* part of the decl-specifier seq; it's
13644 the declarator. Therefore, once we see a type-specifier
13645 (other than a cv-qualifier), we forbid any additional
13646 user-defined types. We *do* still allow things like `int
13647 int' to be considered a decl-specifier-seq, and issue the
13648 error message later. */
13649 if (type_spec
&& !is_cv_qualifier
)
13650 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13651 /* A constructor declarator cannot follow a type-specifier. */
13654 constructor_possible_p
= false;
13655 found_decl_spec
= true;
13656 if (!is_cv_qualifier
)
13657 decl_specs
->any_type_specifiers_p
= true;
13661 /* If we still do not have a DECL_SPEC, then there are no more
13662 decl-specifiers. */
13663 if (!found_decl_spec
)
13666 decl_specs
->any_specifiers_p
= true;
13667 /* After we see one decl-specifier, further decl-specifiers are
13668 always optional. */
13669 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13672 /* Don't allow a friend specifier with a class definition. */
13673 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13674 && (*declares_class_or_enum
& 2))
13675 error_at (decl_specs
->locations
[ds_friend
],
13676 "class definition may not be declared a friend");
13679 /* Parse an (optional) storage-class-specifier.
13681 storage-class-specifier:
13690 storage-class-specifier:
13693 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13696 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13698 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13701 if (cxx_dialect
!= cxx98
)
13703 /* Fall through for C++98. */
13704 gcc_fallthrough ();
13711 /* Consume the token. */
13712 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13719 /* Parse an (optional) function-specifier.
13721 function-specifier:
13726 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13727 Updates DECL_SPECS, if it is non-NULL. */
13730 cp_parser_function_specifier_opt (cp_parser
* parser
,
13731 cp_decl_specifier_seq
*decl_specs
)
13733 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13734 switch (token
->keyword
)
13737 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13741 /* 14.5.2.3 [temp.mem]
13743 A member function template shall not be virtual. */
13744 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13745 && current_class_type
)
13746 error_at (token
->location
, "templates may not be %<virtual%>");
13748 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13752 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13759 /* Consume the token. */
13760 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13763 /* Parse a linkage-specification.
13765 linkage-specification:
13766 extern string-literal { declaration-seq [opt] }
13767 extern string-literal declaration */
13770 cp_parser_linkage_specification (cp_parser
* parser
)
13774 /* Look for the `extern' keyword. */
13775 cp_token
*extern_token
13776 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13778 /* Look for the string-literal. */
13779 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13780 linkage
= cp_parser_string_literal (parser
, false, false);
13782 /* Transform the literal into an identifier. If the literal is a
13783 wide-character string, or contains embedded NULs, then we can't
13784 handle it as the user wants. */
13785 if (strlen (TREE_STRING_POINTER (linkage
))
13786 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13788 cp_parser_error (parser
, "invalid linkage-specification");
13789 /* Assume C++ linkage. */
13790 linkage
= lang_name_cplusplus
;
13793 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13795 /* We're now using the new linkage. */
13796 push_lang_context (linkage
);
13798 /* Preserve the location of the the innermost linkage specification,
13799 tracking the locations of nested specifications via a local. */
13800 location_t saved_location
13801 = parser
->innermost_linkage_specification_location
;
13802 /* Construct a location ranging from the start of the "extern" to
13803 the end of the string-literal, with the caret at the start, e.g.:
13807 parser
->innermost_linkage_specification_location
13808 = make_location (extern_token
->location
,
13809 extern_token
->location
,
13810 get_finish (string_token
->location
));
13812 /* If the next token is a `{', then we're using the first
13814 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13816 cp_ensure_no_omp_declare_simd (parser
);
13817 cp_ensure_no_oacc_routine (parser
);
13819 /* Consume the `{' token. */
13820 matching_braces braces
;
13821 braces
.consume_open (parser
)->location
;
13822 /* Parse the declarations. */
13823 cp_parser_declaration_seq_opt (parser
);
13824 /* Look for the closing `}'. */
13825 braces
.require_close (parser
);
13827 /* Otherwise, there's just one declaration. */
13830 bool saved_in_unbraced_linkage_specification_p
;
13832 saved_in_unbraced_linkage_specification_p
13833 = parser
->in_unbraced_linkage_specification_p
;
13834 parser
->in_unbraced_linkage_specification_p
= true;
13835 cp_parser_declaration (parser
);
13836 parser
->in_unbraced_linkage_specification_p
13837 = saved_in_unbraced_linkage_specification_p
;
13840 /* We're done with the linkage-specification. */
13841 pop_lang_context ();
13843 /* Restore location of parent linkage specification, if any. */
13844 parser
->innermost_linkage_specification_location
= saved_location
;
13847 /* Parse a static_assert-declaration.
13849 static_assert-declaration:
13850 static_assert ( constant-expression , string-literal ) ;
13851 static_assert ( constant-expression ) ; (C++17)
13853 If MEMBER_P, this static_assert is a class member. */
13856 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13859 location_t token_loc
;
13863 /* Peek at the `static_assert' token so we can keep track of exactly
13864 where the static assertion started. */
13865 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13867 /* Look for the `static_assert' keyword. */
13868 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13872 /* We know we are in a static assertion; commit to any tentative
13874 if (cp_parser_parsing_tentatively (parser
))
13875 cp_parser_commit_to_tentative_parse (parser
);
13877 /* Parse the `(' starting the static assertion condition. */
13878 matching_parens parens
;
13879 parens
.require_open (parser
);
13881 /* Parse the constant-expression. Allow a non-constant expression
13882 here in order to give better diagnostics in finish_static_assert. */
13884 cp_parser_constant_expression (parser
,
13885 /*allow_non_constant_p=*/true,
13886 /*non_constant_p=*/&dummy
);
13888 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13890 if (cxx_dialect
< cxx17
)
13891 pedwarn (input_location
, OPT_Wpedantic
,
13892 "static_assert without a message "
13893 "only available with -std=c++17 or -std=gnu++17");
13895 cp_lexer_consume_token (parser
->lexer
);
13896 message
= build_string (1, "");
13897 TREE_TYPE (message
) = char_array_type_node
;
13898 fix_string_type (message
);
13902 /* Parse the separating `,'. */
13903 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13905 /* Parse the string-literal message. */
13906 message
= cp_parser_string_literal (parser
,
13907 /*translate=*/false,
13910 /* A `)' completes the static assertion. */
13911 if (!parens
.require_close (parser
))
13912 cp_parser_skip_to_closing_parenthesis (parser
,
13913 /*recovering=*/true,
13914 /*or_comma=*/false,
13915 /*consume_paren=*/true);
13918 /* A semicolon terminates the declaration. */
13919 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13921 /* Get the location for the static assertion. Use that of the
13922 condition if available, otherwise, use that of the "static_assert"
13924 location_t assert_loc
= condition
.get_location ();
13925 if (assert_loc
== UNKNOWN_LOCATION
)
13926 assert_loc
= token_loc
;
13928 /* Complete the static assertion, which may mean either processing
13929 the static assert now or saving it for template instantiation. */
13930 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13933 /* Parse the expression in decltype ( expression ). */
13936 cp_parser_decltype_expr (cp_parser
*parser
,
13937 bool &id_expression_or_member_access_p
)
13939 cp_token
*id_expr_start_token
;
13942 /* Since we're going to preserve any side-effects from this parse, set up a
13943 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13944 in the expression. */
13945 tentative_firewall
firewall (parser
);
13947 /* First, try parsing an id-expression. */
13948 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13949 cp_parser_parse_tentatively (parser
);
13950 expr
= cp_parser_id_expression (parser
,
13951 /*template_keyword_p=*/false,
13952 /*check_dependency_p=*/true,
13953 /*template_p=*/NULL
,
13954 /*declarator_p=*/false,
13955 /*optional_p=*/false);
13957 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13959 bool non_integral_constant_expression_p
= false;
13960 tree id_expression
= expr
;
13962 const char *error_msg
;
13964 if (identifier_p (expr
))
13965 /* Lookup the name we got back from the id-expression. */
13966 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13967 id_expr_start_token
->location
);
13969 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
13970 /* A template without args is not a complete id-expression. */
13971 expr
= error_mark_node
;
13974 && expr
!= error_mark_node
13975 && TREE_CODE (expr
) != TYPE_DECL
13976 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13977 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13978 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13980 /* Complete lookup of the id-expression. */
13981 expr
= (finish_id_expression
13982 (id_expression
, expr
, parser
->scope
, &idk
,
13983 /*integral_constant_expression_p=*/false,
13984 /*allow_non_integral_constant_expression_p=*/true,
13985 &non_integral_constant_expression_p
,
13986 /*template_p=*/false,
13988 /*address_p=*/false,
13989 /*template_arg_p=*/false,
13991 id_expr_start_token
->location
));
13993 if (expr
== error_mark_node
)
13994 /* We found an id-expression, but it was something that we
13995 should not have found. This is an error, not something
13996 we can recover from, so note that we found an
13997 id-expression and we'll recover as gracefully as
13999 id_expression_or_member_access_p
= true;
14003 && expr
!= error_mark_node
14004 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14005 /* We have an id-expression. */
14006 id_expression_or_member_access_p
= true;
14009 if (!id_expression_or_member_access_p
)
14011 /* Abort the id-expression parse. */
14012 cp_parser_abort_tentative_parse (parser
);
14014 /* Parsing tentatively, again. */
14015 cp_parser_parse_tentatively (parser
);
14017 /* Parse a class member access. */
14018 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14019 /*cast_p=*/false, /*decltype*/true,
14020 /*member_access_only_p=*/true, NULL
);
14023 && expr
!= error_mark_node
14024 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14025 /* We have an id-expression. */
14026 id_expression_or_member_access_p
= true;
14029 if (id_expression_or_member_access_p
)
14030 /* We have parsed the complete id-expression or member access. */
14031 cp_parser_parse_definitely (parser
);
14034 /* Abort our attempt to parse an id-expression or member access
14036 cp_parser_abort_tentative_parse (parser
);
14038 /* Commit to the tentative_firewall so we get syntax errors. */
14039 cp_parser_commit_to_tentative_parse (parser
);
14041 /* Parse a full expression. */
14042 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14043 /*decltype_p=*/true);
14049 /* Parse a `decltype' type. Returns the type.
14051 simple-type-specifier:
14052 decltype ( expression )
14054 decltype ( auto ) */
14057 cp_parser_decltype (cp_parser
*parser
)
14059 bool id_expression_or_member_access_p
= false;
14060 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14062 if (start_token
->type
== CPP_DECLTYPE
)
14064 /* Already parsed. */
14065 cp_lexer_consume_token (parser
->lexer
);
14066 return saved_checks_value (start_token
->u
.tree_check_value
);
14069 /* Look for the `decltype' token. */
14070 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14071 return error_mark_node
;
14073 /* Parse the opening `('. */
14074 matching_parens parens
;
14075 if (!parens
.require_open (parser
))
14076 return error_mark_node
;
14078 push_deferring_access_checks (dk_deferred
);
14080 tree expr
= NULL_TREE
;
14082 if (cxx_dialect
>= cxx14
14083 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14084 /* decltype (auto) */
14085 cp_lexer_consume_token (parser
->lexer
);
14088 /* decltype (expression) */
14090 /* Types cannot be defined in a `decltype' expression. Save away the
14091 old message and set the new one. */
14092 const char *saved_message
= parser
->type_definition_forbidden_message
;
14093 parser
->type_definition_forbidden_message
14094 = G_("types may not be defined in %<decltype%> expressions");
14096 /* The restrictions on constant-expressions do not apply inside
14097 decltype expressions. */
14098 bool saved_integral_constant_expression_p
14099 = parser
->integral_constant_expression_p
;
14100 bool saved_non_integral_constant_expression_p
14101 = parser
->non_integral_constant_expression_p
;
14102 parser
->integral_constant_expression_p
= false;
14104 /* Within a parenthesized expression, a `>' token is always
14105 the greater-than operator. */
14106 bool saved_greater_than_is_operator_p
14107 = parser
->greater_than_is_operator_p
;
14108 parser
->greater_than_is_operator_p
= true;
14110 /* Do not actually evaluate the expression. */
14111 ++cp_unevaluated_operand
;
14113 /* Do not warn about problems with the expression. */
14114 ++c_inhibit_evaluation_warnings
;
14116 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14118 /* Go back to evaluating expressions. */
14119 --cp_unevaluated_operand
;
14120 --c_inhibit_evaluation_warnings
;
14122 /* The `>' token might be the end of a template-id or
14123 template-parameter-list now. */
14124 parser
->greater_than_is_operator_p
14125 = saved_greater_than_is_operator_p
;
14127 /* Restore the old message and the integral constant expression
14129 parser
->type_definition_forbidden_message
= saved_message
;
14130 parser
->integral_constant_expression_p
14131 = saved_integral_constant_expression_p
;
14132 parser
->non_integral_constant_expression_p
14133 = saved_non_integral_constant_expression_p
;
14136 /* Parse to the closing `)'. */
14137 if (!parens
.require_close (parser
))
14139 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14140 /*consume_paren=*/true);
14141 pop_deferring_access_checks ();
14142 return error_mark_node
;
14148 expr
= make_decltype_auto ();
14149 AUTO_IS_DECLTYPE (expr
) = true;
14152 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14153 tf_warning_or_error
);
14155 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14157 start_token
->type
= CPP_DECLTYPE
;
14158 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14159 start_token
->u
.tree_check_value
->value
= expr
;
14160 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14161 start_token
->keyword
= RID_MAX
;
14162 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14164 pop_to_parent_deferring_access_checks ();
14169 /* Special member functions [gram.special] */
14171 /* Parse a conversion-function-id.
14173 conversion-function-id:
14174 operator conversion-type-id
14176 Returns an IDENTIFIER_NODE representing the operator. */
14179 cp_parser_conversion_function_id (cp_parser
* parser
)
14183 tree saved_qualifying_scope
;
14184 tree saved_object_scope
;
14185 tree pushed_scope
= NULL_TREE
;
14187 /* Look for the `operator' token. */
14188 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14189 return error_mark_node
;
14190 /* When we parse the conversion-type-id, the current scope will be
14191 reset. However, we need that information in able to look up the
14192 conversion function later, so we save it here. */
14193 saved_scope
= parser
->scope
;
14194 saved_qualifying_scope
= parser
->qualifying_scope
;
14195 saved_object_scope
= parser
->object_scope
;
14196 /* We must enter the scope of the class so that the names of
14197 entities declared within the class are available in the
14198 conversion-type-id. For example, consider:
14205 S::operator I() { ... }
14207 In order to see that `I' is a type-name in the definition, we
14208 must be in the scope of `S'. */
14210 pushed_scope
= push_scope (saved_scope
);
14211 /* Parse the conversion-type-id. */
14212 type
= cp_parser_conversion_type_id (parser
);
14213 /* Leave the scope of the class, if any. */
14215 pop_scope (pushed_scope
);
14216 /* Restore the saved scope. */
14217 parser
->scope
= saved_scope
;
14218 parser
->qualifying_scope
= saved_qualifying_scope
;
14219 parser
->object_scope
= saved_object_scope
;
14220 /* If the TYPE is invalid, indicate failure. */
14221 if (type
== error_mark_node
)
14222 return error_mark_node
;
14223 return make_conv_op_name (type
);
14226 /* Parse a conversion-type-id:
14228 conversion-type-id:
14229 type-specifier-seq conversion-declarator [opt]
14231 Returns the TYPE specified. */
14234 cp_parser_conversion_type_id (cp_parser
* parser
)
14237 cp_decl_specifier_seq type_specifiers
;
14238 cp_declarator
*declarator
;
14239 tree type_specified
;
14240 const char *saved_message
;
14242 /* Parse the attributes. */
14243 attributes
= cp_parser_attributes_opt (parser
);
14245 saved_message
= parser
->type_definition_forbidden_message
;
14246 parser
->type_definition_forbidden_message
14247 = G_("types may not be defined in a conversion-type-id");
14249 /* Parse the type-specifiers. */
14250 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14251 /*is_trailing_return=*/false,
14254 parser
->type_definition_forbidden_message
= saved_message
;
14256 /* If that didn't work, stop. */
14257 if (type_specifiers
.type
== error_mark_node
)
14258 return error_mark_node
;
14259 /* Parse the conversion-declarator. */
14260 declarator
= cp_parser_conversion_declarator_opt (parser
);
14262 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14263 /*initialized=*/0, &attributes
);
14265 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14267 /* Don't give this error when parsing tentatively. This happens to
14268 work because we always parse this definitively once. */
14269 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14270 && type_uses_auto (type_specified
))
14272 if (cxx_dialect
< cxx14
)
14274 error ("invalid use of %<auto%> in conversion operator");
14275 return error_mark_node
;
14277 else if (template_parm_scope_p ())
14278 warning (0, "use of %<auto%> in member template "
14279 "conversion operator can never be deduced");
14282 return type_specified
;
14285 /* Parse an (optional) conversion-declarator.
14287 conversion-declarator:
14288 ptr-operator conversion-declarator [opt]
14292 static cp_declarator
*
14293 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14295 enum tree_code code
;
14296 tree class_type
, std_attributes
= NULL_TREE
;
14297 cp_cv_quals cv_quals
;
14299 /* We don't know if there's a ptr-operator next, or not. */
14300 cp_parser_parse_tentatively (parser
);
14301 /* Try the ptr-operator. */
14302 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14304 /* If it worked, look for more conversion-declarators. */
14305 if (cp_parser_parse_definitely (parser
))
14307 cp_declarator
*declarator
;
14309 /* Parse another optional declarator. */
14310 declarator
= cp_parser_conversion_declarator_opt (parser
);
14312 declarator
= cp_parser_make_indirect_declarator
14313 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14321 /* Parse an (optional) ctor-initializer.
14324 : mem-initializer-list */
14327 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14329 /* If the next token is not a `:', then there is no
14330 ctor-initializer. */
14331 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14333 /* Do default initialization of any bases and members. */
14334 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14335 finish_mem_initializers (NULL_TREE
);
14339 /* Consume the `:' token. */
14340 cp_lexer_consume_token (parser
->lexer
);
14341 /* And the mem-initializer-list. */
14342 cp_parser_mem_initializer_list (parser
);
14345 /* Parse a mem-initializer-list.
14347 mem-initializer-list:
14348 mem-initializer ... [opt]
14349 mem-initializer ... [opt] , mem-initializer-list */
14352 cp_parser_mem_initializer_list (cp_parser
* parser
)
14354 tree mem_initializer_list
= NULL_TREE
;
14355 tree target_ctor
= error_mark_node
;
14356 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14358 /* Let the semantic analysis code know that we are starting the
14359 mem-initializer-list. */
14360 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14361 error_at (token
->location
,
14362 "only constructors take member initializers");
14364 /* Loop through the list. */
14367 tree mem_initializer
;
14369 token
= cp_lexer_peek_token (parser
->lexer
);
14370 /* Parse the mem-initializer. */
14371 mem_initializer
= cp_parser_mem_initializer (parser
);
14372 /* If the next token is a `...', we're expanding member initializers. */
14373 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14375 || (mem_initializer
!= error_mark_node
14376 && check_for_bare_parameter_packs (TREE_PURPOSE
14377 (mem_initializer
))))
14379 /* Consume the `...'. */
14381 cp_lexer_consume_token (parser
->lexer
);
14383 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14384 can be expanded but members cannot. */
14385 if (mem_initializer
!= error_mark_node
14386 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14388 error_at (token
->location
,
14389 "cannot expand initializer for member %qD",
14390 TREE_PURPOSE (mem_initializer
));
14391 mem_initializer
= error_mark_node
;
14394 /* Construct the pack expansion type. */
14395 if (mem_initializer
!= error_mark_node
)
14396 mem_initializer
= make_pack_expansion (mem_initializer
);
14398 if (target_ctor
!= error_mark_node
14399 && mem_initializer
!= error_mark_node
)
14401 error ("mem-initializer for %qD follows constructor delegation",
14402 TREE_PURPOSE (mem_initializer
));
14403 mem_initializer
= error_mark_node
;
14405 /* Look for a target constructor. */
14406 if (mem_initializer
!= error_mark_node
14407 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14408 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14410 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14411 if (mem_initializer_list
)
14413 error ("constructor delegation follows mem-initializer for %qD",
14414 TREE_PURPOSE (mem_initializer_list
));
14415 mem_initializer
= error_mark_node
;
14417 target_ctor
= mem_initializer
;
14419 /* Add it to the list, unless it was erroneous. */
14420 if (mem_initializer
!= error_mark_node
)
14422 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14423 mem_initializer_list
= mem_initializer
;
14425 /* If the next token is not a `,', we're done. */
14426 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14428 /* Consume the `,' token. */
14429 cp_lexer_consume_token (parser
->lexer
);
14432 /* Perform semantic analysis. */
14433 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14434 finish_mem_initializers (mem_initializer_list
);
14437 /* Parse a mem-initializer.
14440 mem-initializer-id ( expression-list [opt] )
14441 mem-initializer-id braced-init-list
14446 ( expression-list [opt] )
14448 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14449 class) or FIELD_DECL (for a non-static data member) to initialize;
14450 the TREE_VALUE is the expression-list. An empty initialization
14451 list is represented by void_list_node. */
14454 cp_parser_mem_initializer (cp_parser
* parser
)
14456 tree mem_initializer_id
;
14457 tree expression_list
;
14459 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14461 /* Find out what is being initialized. */
14462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14464 permerror (token
->location
,
14465 "anachronistic old-style base class initializer");
14466 mem_initializer_id
= NULL_TREE
;
14470 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14471 if (mem_initializer_id
== error_mark_node
)
14472 return mem_initializer_id
;
14474 member
= expand_member_init (mem_initializer_id
);
14475 if (member
&& !DECL_P (member
))
14476 in_base_initializer
= 1;
14478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14480 bool expr_non_constant_p
;
14481 cp_lexer_set_source_position (parser
->lexer
);
14482 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14483 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14484 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14485 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14489 vec
<tree
, va_gc
> *vec
;
14490 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14492 /*allow_expansion_p=*/true,
14493 /*non_constant_p=*/NULL
);
14495 return error_mark_node
;
14496 expression_list
= build_tree_list_vec (vec
);
14497 release_tree_vector (vec
);
14500 if (expression_list
== error_mark_node
)
14501 return error_mark_node
;
14502 if (!expression_list
)
14503 expression_list
= void_type_node
;
14505 in_base_initializer
= 0;
14507 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14510 /* Parse a mem-initializer-id.
14512 mem-initializer-id:
14513 :: [opt] nested-name-specifier [opt] class-name
14514 decltype-specifier (C++11)
14517 Returns a TYPE indicating the class to be initialized for the first
14518 production (and the second in C++11). Returns an IDENTIFIER_NODE
14519 indicating the data member to be initialized for the last production. */
14522 cp_parser_mem_initializer_id (cp_parser
* parser
)
14524 bool global_scope_p
;
14525 bool nested_name_specifier_p
;
14526 bool template_p
= false;
14529 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14531 /* `typename' is not allowed in this context ([temp.res]). */
14532 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14534 error_at (token
->location
,
14535 "keyword %<typename%> not allowed in this context (a qualified "
14536 "member initializer is implicitly a type)");
14537 cp_lexer_consume_token (parser
->lexer
);
14539 /* Look for the optional `::' operator. */
14541 = (cp_parser_global_scope_opt (parser
,
14542 /*current_scope_valid_p=*/false)
14544 /* Look for the optional nested-name-specifier. The simplest way to
14549 The keyword `typename' is not permitted in a base-specifier or
14550 mem-initializer; in these contexts a qualified name that
14551 depends on a template-parameter is implicitly assumed to be a
14554 is to assume that we have seen the `typename' keyword at this
14556 nested_name_specifier_p
14557 = (cp_parser_nested_name_specifier_opt (parser
,
14558 /*typename_keyword_p=*/true,
14559 /*check_dependency_p=*/true,
14561 /*is_declaration=*/true)
14563 if (nested_name_specifier_p
)
14564 template_p
= cp_parser_optional_template_keyword (parser
);
14565 /* If there is a `::' operator or a nested-name-specifier, then we
14566 are definitely looking for a class-name. */
14567 if (global_scope_p
|| nested_name_specifier_p
)
14568 return cp_parser_class_name (parser
,
14569 /*typename_keyword_p=*/true,
14570 /*template_keyword_p=*/template_p
,
14572 /*check_dependency_p=*/true,
14573 /*class_head_p=*/false,
14574 /*is_declaration=*/true);
14575 /* Otherwise, we could also be looking for an ordinary identifier. */
14576 cp_parser_parse_tentatively (parser
);
14577 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14578 /* Try a decltype-specifier. */
14579 id
= cp_parser_decltype (parser
);
14581 /* Otherwise, try a class-name. */
14582 id
= cp_parser_class_name (parser
,
14583 /*typename_keyword_p=*/true,
14584 /*template_keyword_p=*/false,
14586 /*check_dependency_p=*/true,
14587 /*class_head_p=*/false,
14588 /*is_declaration=*/true);
14589 /* If we found one, we're done. */
14590 if (cp_parser_parse_definitely (parser
))
14592 /* Otherwise, look for an ordinary identifier. */
14593 return cp_parser_identifier (parser
);
14596 /* Overloading [gram.over] */
14598 /* Parse an operator-function-id.
14600 operator-function-id:
14603 Returns an IDENTIFIER_NODE for the operator which is a
14604 human-readable spelling of the identifier, e.g., `operator +'. */
14607 cp_parser_operator_function_id (cp_parser
* parser
)
14609 /* Look for the `operator' keyword. */
14610 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14611 return error_mark_node
;
14612 /* And then the name of the operator itself. */
14613 return cp_parser_operator (parser
);
14616 /* Return an identifier node for a user-defined literal operator.
14617 The suffix identifier is chained to the operator name identifier. */
14620 cp_literal_operator_id (const char* name
)
14623 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14624 + strlen (name
) + 10);
14625 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14626 identifier
= get_identifier (buffer
);
14631 /* Parse an operator.
14634 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14635 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14636 || ++ -- , ->* -> () []
14643 Returns an IDENTIFIER_NODE for the operator which is a
14644 human-readable spelling of the identifier, e.g., `operator +'. */
14647 cp_parser_operator (cp_parser
* parser
)
14649 tree id
= NULL_TREE
;
14653 /* Peek at the next token. */
14654 token
= cp_lexer_peek_token (parser
->lexer
);
14656 location_t start_loc
= token
->location
;
14658 /* Figure out which operator we have. */
14659 enum tree_code op
= ERROR_MARK
;
14660 bool assop
= false;
14661 bool consumed
= false;
14662 switch (token
->type
)
14666 /* The keyword should be either `new' or `delete'. */
14667 if (token
->keyword
== RID_NEW
)
14669 else if (token
->keyword
== RID_DELETE
)
14674 /* Consume the `new' or `delete' token. */
14675 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14677 /* Peek at the next token. */
14678 token
= cp_lexer_peek_token (parser
->lexer
);
14679 /* If it's a `[' token then this is the array variant of the
14681 if (token
->type
== CPP_OPEN_SQUARE
)
14683 /* Consume the `[' token. */
14684 cp_lexer_consume_token (parser
->lexer
);
14685 /* Look for the `]' token. */
14686 if (cp_token
*close_token
14687 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14688 end_loc
= close_token
->location
;
14689 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14691 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14709 op
= TRUNC_DIV_EXPR
;
14713 op
= TRUNC_MOD_EXPR
;
14733 op
= TRUTH_NOT_EXPR
;
14766 op
= TRUNC_DIV_EXPR
;
14771 op
= TRUNC_MOD_EXPR
;
14797 case CPP_LSHIFT_EQ
:
14802 case CPP_RSHIFT_EQ
:
14819 case CPP_GREATER_EQ
:
14824 op
= TRUTH_ANDIF_EXPR
;
14828 op
= TRUTH_ORIF_EXPR
;
14831 case CPP_PLUS_PLUS
:
14832 op
= POSTINCREMENT_EXPR
;
14835 case CPP_MINUS_MINUS
:
14836 op
= PREDECREMENT_EXPR
;
14840 op
= COMPOUND_EXPR
;
14843 case CPP_DEREF_STAR
:
14848 op
= COMPONENT_REF
;
14851 case CPP_OPEN_PAREN
:
14853 /* Consume the `('. */
14854 matching_parens parens
;
14855 parens
.consume_open (parser
);
14856 /* Look for the matching `)'. */
14857 parens
.require_close (parser
);
14863 case CPP_OPEN_SQUARE
:
14864 /* Consume the `['. */
14865 cp_lexer_consume_token (parser
->lexer
);
14866 /* Look for the matching `]'. */
14867 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14872 case CPP_UTF8STRING
:
14873 case CPP_UTF8STRING_USERDEF
:
14880 case CPP_STRING_USERDEF
:
14881 case CPP_WSTRING_USERDEF
:
14882 case CPP_STRING16_USERDEF
:
14883 case CPP_STRING32_USERDEF
:
14885 tree str
, string_tree
;
14888 if (cxx_dialect
== cxx98
)
14889 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14891 /* Consume the string. */
14892 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14893 /*wide_ok=*/true, /*lookup_udlit=*/false);
14894 if (str
== error_mark_node
)
14895 return error_mark_node
;
14896 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14898 string_tree
= USERDEF_LITERAL_VALUE (str
);
14899 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14904 /* Look for the suffix identifier. */
14905 token
= cp_lexer_peek_token (parser
->lexer
);
14906 if (token
->type
== CPP_NAME
)
14907 id
= cp_parser_identifier (parser
);
14908 else if (token
->type
== CPP_KEYWORD
)
14910 error ("unexpected keyword;"
14911 " remove space between quotes and suffix identifier");
14912 return error_mark_node
;
14916 error ("expected suffix identifier");
14917 return error_mark_node
;
14920 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14921 (TREE_TYPE (TREE_TYPE (string_tree
))));
14922 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14925 error ("expected empty string after %<operator%> keyword");
14926 return error_mark_node
;
14928 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14931 error ("invalid encoding prefix in literal operator");
14932 return error_mark_node
;
14934 if (id
!= error_mark_node
)
14936 const char *name
= IDENTIFIER_POINTER (id
);
14937 id
= cp_literal_operator_id (name
);
14943 /* Anything else is an error. */
14947 /* If we have selected an identifier, we need to consume the
14949 if (op
!= ERROR_MARK
)
14951 id
= ovl_op_identifier (assop
, op
);
14953 cp_lexer_consume_token (parser
->lexer
);
14955 /* Otherwise, no valid operator name was present. */
14958 cp_parser_error (parser
, "expected operator");
14959 id
= error_mark_node
;
14962 return cp_expr (id
, start_loc
);
14965 /* Parse a template-declaration.
14967 template-declaration:
14968 export [opt] template < template-parameter-list > declaration
14970 If MEMBER_P is TRUE, this template-declaration occurs within a
14973 The grammar rule given by the standard isn't correct. What
14974 is really meant is:
14976 template-declaration:
14977 export [opt] template-parameter-list-seq
14978 decl-specifier-seq [opt] init-declarator [opt] ;
14979 export [opt] template-parameter-list-seq
14980 function-definition
14982 template-parameter-list-seq:
14983 template-parameter-list-seq [opt]
14984 template < template-parameter-list >
14986 Concept Extensions:
14988 template-parameter-list-seq:
14989 template < template-parameter-list > requires-clause [opt]
14992 requires logical-or-expression */
14995 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14997 /* Check for `export'. */
14998 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15000 /* Consume the `export' token. */
15001 cp_lexer_consume_token (parser
->lexer
);
15002 /* Warn that we do not support `export'. */
15003 warning (0, "keyword %<export%> not implemented, and will be ignored");
15006 cp_parser_template_declaration_after_export (parser
, member_p
);
15009 /* Parse a template-parameter-list.
15011 template-parameter-list:
15013 template-parameter-list , template-parameter
15015 Returns a TREE_LIST. Each node represents a template parameter.
15016 The nodes are connected via their TREE_CHAINs. */
15019 cp_parser_template_parameter_list (cp_parser
* parser
)
15021 tree parameter_list
= NULL_TREE
;
15023 begin_template_parm_list ();
15025 /* The loop below parses the template parms. We first need to know
15026 the total number of template parms to be able to compute proper
15027 canonical types of each dependent type. So after the loop, when
15028 we know the total number of template parms,
15029 end_template_parm_list computes the proper canonical types and
15030 fixes up the dependent types accordingly. */
15035 bool is_parameter_pack
;
15036 location_t parm_loc
;
15038 /* Parse the template-parameter. */
15039 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15040 parameter
= cp_parser_template_parameter (parser
,
15042 &is_parameter_pack
);
15043 /* Add it to the list. */
15044 if (parameter
!= error_mark_node
)
15045 parameter_list
= process_template_parm (parameter_list
,
15049 is_parameter_pack
);
15052 tree err_parm
= build_tree_list (parameter
, parameter
);
15053 parameter_list
= chainon (parameter_list
, err_parm
);
15056 /* If the next token is not a `,', we're done. */
15057 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15059 /* Otherwise, consume the `,' token. */
15060 cp_lexer_consume_token (parser
->lexer
);
15063 return end_template_parm_list (parameter_list
);
15066 /* Parse a introduction-list.
15069 introduced-parameter
15070 introduction-list , introduced-parameter
15072 introduced-parameter:
15073 ...[opt] identifier
15075 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15076 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15077 WILDCARD_DECL will also have DECL_NAME set and token location in
15078 DECL_SOURCE_LOCATION. */
15081 cp_parser_introduction_list (cp_parser
*parser
)
15083 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15087 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15089 cp_lexer_consume_token (parser
->lexer
);
15091 /* Build placeholder. */
15092 tree parm
= build_nt (WILDCARD_DECL
);
15093 DECL_SOURCE_LOCATION (parm
)
15094 = cp_lexer_peek_token (parser
->lexer
)->location
;
15095 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15096 WILDCARD_PACK_P (parm
) = is_pack
;
15097 vec_safe_push (introduction_vec
, parm
);
15099 /* If the next token is not a `,', we're done. */
15100 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15102 /* Otherwise, consume the `,' token. */
15103 cp_lexer_consume_token (parser
->lexer
);
15106 /* Convert the vec into a TREE_VEC. */
15107 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15110 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15111 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15113 release_tree_vector (introduction_vec
);
15114 return introduction_list
;
15117 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15118 is an abstract declarator. */
15120 static inline cp_declarator
*
15121 get_id_declarator (cp_declarator
*declarator
)
15123 cp_declarator
*d
= declarator
;
15124 while (d
&& d
->kind
!= cdk_id
)
15129 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15130 is an abstract declarator. */
15133 get_unqualified_id (cp_declarator
*declarator
)
15135 declarator
= get_id_declarator (declarator
);
15137 return declarator
->u
.id
.unqualified_name
;
15142 /* Returns true if DECL represents a constrained-parameter. */
15145 is_constrained_parameter (tree decl
)
15148 && TREE_CODE (decl
) == TYPE_DECL
15149 && CONSTRAINED_PARM_CONCEPT (decl
)
15150 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15153 /* Returns true if PARM declares a constrained-parameter. */
15156 is_constrained_parameter (cp_parameter_declarator
*parm
)
15158 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15161 /* Check that the type parameter is only a declarator-id, and that its
15162 type is not cv-qualified. */
15165 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15166 cp_parameter_declarator
*parm
)
15168 if (!parm
->declarator
)
15171 if (parm
->declarator
->kind
!= cdk_id
)
15173 cp_parser_error (parser
, "invalid constrained type parameter");
15177 /* Don't allow cv-qualified type parameters. */
15178 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15179 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15181 cp_parser_error (parser
, "cv-qualified type parameter");
15188 /* Finish parsing/processing a template type parameter and checking
15189 various restrictions. */
15192 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15194 cp_parameter_declarator
* parmdecl
)
15196 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15197 return finish_template_type_parm (class_type_node
, id
);
15199 return error_mark_node
;
15203 finish_constrained_template_template_parm (tree proto
, tree id
)
15205 /* FIXME: This should probably be copied, and we may need to adjust
15206 the template parameter depths. */
15207 tree saved_parms
= current_template_parms
;
15208 begin_template_parm_list ();
15209 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15210 end_template_parm_list ();
15212 tree parm
= finish_template_template_parm (class_type_node
, id
);
15213 current_template_parms
= saved_parms
;
15218 /* Finish parsing/processing a template template parameter by borrowing
15219 the template parameter list from the prototype parameter. */
15222 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15225 cp_parameter_declarator
*parmdecl
)
15227 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15228 return error_mark_node
;
15229 return finish_constrained_template_template_parm (proto
, id
);
15232 /* Create a new non-type template parameter from the given PARM
15236 constrained_non_type_template_parm (bool *is_non_type
,
15237 cp_parameter_declarator
*parm
)
15239 *is_non_type
= true;
15240 cp_declarator
*decl
= parm
->declarator
;
15241 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15242 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15243 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15246 /* Build a constrained template parameter based on the PARMDECL
15247 declarator. The type of PARMDECL is the constrained type, which
15248 refers to the prototype template parameter that ultimately
15249 specifies the type of the declared parameter. */
15252 finish_constrained_parameter (cp_parser
*parser
,
15253 cp_parameter_declarator
*parmdecl
,
15255 bool *is_parameter_pack
)
15257 tree decl
= parmdecl
->decl_specifiers
.type
;
15258 tree id
= get_unqualified_id (parmdecl
->declarator
);
15259 tree def
= parmdecl
->default_argument
;
15260 tree proto
= DECL_INITIAL (decl
);
15262 /* A template parameter constrained by a variadic concept shall also
15263 be declared as a template parameter pack. */
15264 bool is_variadic
= template_parameter_pack_p (proto
);
15265 if (is_variadic
&& !*is_parameter_pack
)
15266 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15268 /* Build the parameter. Return an error if the declarator was invalid. */
15270 if (TREE_CODE (proto
) == TYPE_DECL
)
15271 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15272 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15273 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15276 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15277 if (parm
== error_mark_node
)
15278 return error_mark_node
;
15280 /* Finish the parameter decl and create a node attaching the
15281 default argument and constraint. */
15282 parm
= build_tree_list (def
, parm
);
15283 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15288 /* Returns true if the parsed type actually represents the declaration
15289 of a type template-parameter. */
15292 declares_constrained_type_template_parameter (tree type
)
15294 return (is_constrained_parameter (type
)
15295 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15299 /* Returns true if the parsed type actually represents the declaration of
15300 a template template-parameter. */
15303 declares_constrained_template_template_parameter (tree type
)
15305 return (is_constrained_parameter (type
)
15306 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15309 /* Parse a default argument for a type template-parameter.
15310 Note that diagnostics are handled in cp_parser_template_parameter. */
15313 cp_parser_default_type_template_argument (cp_parser
*parser
)
15315 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15317 /* Consume the `=' token. */
15318 cp_lexer_consume_token (parser
->lexer
);
15320 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15322 /* Parse the default-argument. */
15323 push_deferring_access_checks (dk_no_deferred
);
15324 tree default_argument
= cp_parser_type_id (parser
);
15325 pop_deferring_access_checks ();
15327 if (flag_concepts
&& type_uses_auto (default_argument
))
15329 error_at (token
->location
,
15330 "invalid use of %<auto%> in default template argument");
15331 return error_mark_node
;
15334 return default_argument
;
15337 /* Parse a default argument for a template template-parameter. */
15340 cp_parser_default_template_template_argument (cp_parser
*parser
)
15342 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15346 /* Consume the `='. */
15347 cp_lexer_consume_token (parser
->lexer
);
15348 /* Parse the id-expression. */
15349 push_deferring_access_checks (dk_no_deferred
);
15350 /* save token before parsing the id-expression, for error
15352 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15353 tree default_argument
15354 = cp_parser_id_expression (parser
,
15355 /*template_keyword_p=*/false,
15356 /*check_dependency_p=*/true,
15357 /*template_p=*/&is_template
,
15358 /*declarator_p=*/false,
15359 /*optional_p=*/false);
15360 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15361 /* If the id-expression was a template-id that refers to
15362 a template-class, we already have the declaration here,
15363 so no further lookup is needed. */
15366 /* Look up the name. */
15368 = cp_parser_lookup_name (parser
, default_argument
,
15370 /*is_template=*/is_template
,
15371 /*is_namespace=*/false,
15372 /*check_dependency=*/true,
15373 /*ambiguous_decls=*/NULL
,
15375 /* See if the default argument is valid. */
15376 default_argument
= check_template_template_default_arg (default_argument
);
15377 pop_deferring_access_checks ();
15378 return default_argument
;
15381 /* Parse a template-parameter.
15383 template-parameter:
15385 parameter-declaration
15387 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15388 the parameter. The TREE_PURPOSE is the default value, if any.
15389 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15390 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15391 set to true iff this parameter is a parameter pack. */
15394 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15395 bool *is_parameter_pack
)
15398 cp_parameter_declarator
*parameter_declarator
;
15401 /* Assume it is a type parameter or a template parameter. */
15402 *is_non_type
= false;
15403 /* Assume it not a parameter pack. */
15404 *is_parameter_pack
= false;
15405 /* Peek at the next token. */
15406 token
= cp_lexer_peek_token (parser
->lexer
);
15407 /* If it is `template', we have a type-parameter. */
15408 if (token
->keyword
== RID_TEMPLATE
)
15409 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15410 /* If it is `class' or `typename' we do not know yet whether it is a
15411 type parameter or a non-type parameter. Consider:
15413 template <typename T, typename T::X X> ...
15417 template <class C, class D*> ...
15419 Here, the first parameter is a type parameter, and the second is
15420 a non-type parameter. We can tell by looking at the token after
15421 the identifier -- if it is a `,', `=', or `>' then we have a type
15423 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15425 /* Peek at the token after `class' or `typename'. */
15426 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15427 /* If it's an ellipsis, we have a template type parameter
15429 if (token
->type
== CPP_ELLIPSIS
)
15430 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15431 /* If it's an identifier, skip it. */
15432 if (token
->type
== CPP_NAME
)
15433 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15434 /* Now, see if the token looks like the end of a template
15436 if (token
->type
== CPP_COMMA
15437 || token
->type
== CPP_EQ
15438 || token
->type
== CPP_GREATER
)
15439 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15442 /* Otherwise, it is a non-type parameter or a constrained parameter.
15446 When parsing a default template-argument for a non-type
15447 template-parameter, the first non-nested `>' is taken as the end
15448 of the template parameter-list rather than a greater-than
15450 parameter_declarator
15451 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15452 /*parenthesized_p=*/NULL
);
15454 if (!parameter_declarator
)
15455 return error_mark_node
;
15457 /* If the parameter declaration is marked as a parameter pack, set
15458 *IS_PARAMETER_PACK to notify the caller. */
15459 if (parameter_declarator
->template_parameter_pack_p
)
15460 *is_parameter_pack
= true;
15462 if (parameter_declarator
->default_argument
)
15464 /* Can happen in some cases of erroneous input (c++/34892). */
15465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15466 /* Consume the `...' for better error recovery. */
15467 cp_lexer_consume_token (parser
->lexer
);
15470 // The parameter may have been constrained.
15471 if (is_constrained_parameter (parameter_declarator
))
15472 return finish_constrained_parameter (parser
,
15473 parameter_declarator
,
15475 is_parameter_pack
);
15477 // Now we're sure that the parameter is a non-type parameter.
15478 *is_non_type
= true;
15480 parm
= grokdeclarator (parameter_declarator
->declarator
,
15481 ¶meter_declarator
->decl_specifiers
,
15482 TPARM
, /*initialized=*/0,
15483 /*attrlist=*/NULL
);
15484 if (parm
== error_mark_node
)
15485 return error_mark_node
;
15487 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15490 /* Parse a type-parameter.
15493 class identifier [opt]
15494 class identifier [opt] = type-id
15495 typename identifier [opt]
15496 typename identifier [opt] = type-id
15497 template < template-parameter-list > class identifier [opt]
15498 template < template-parameter-list > class identifier [opt]
15501 GNU Extension (variadic templates):
15504 class ... identifier [opt]
15505 typename ... identifier [opt]
15507 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15508 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15509 the declaration of the parameter.
15511 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15514 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15519 /* Look for a keyword to tell us what kind of parameter this is. */
15520 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15522 return error_mark_node
;
15524 switch (token
->keyword
)
15530 tree default_argument
;
15532 /* If the next token is an ellipsis, we have a template
15534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15536 /* Consume the `...' token. */
15537 cp_lexer_consume_token (parser
->lexer
);
15538 maybe_warn_variadic_templates ();
15540 *is_parameter_pack
= true;
15543 /* If the next token is an identifier, then it names the
15545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15546 identifier
= cp_parser_identifier (parser
);
15548 identifier
= NULL_TREE
;
15550 /* Create the parameter. */
15551 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15553 /* If the next token is an `=', we have a default argument. */
15554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15557 = cp_parser_default_type_template_argument (parser
);
15559 /* Template parameter packs cannot have default
15561 if (*is_parameter_pack
)
15564 error_at (token
->location
,
15565 "template parameter pack %qD cannot have a "
15566 "default argument", identifier
);
15568 error_at (token
->location
,
15569 "template parameter packs cannot have "
15570 "default arguments");
15571 default_argument
= NULL_TREE
;
15573 else if (check_for_bare_parameter_packs (default_argument
))
15574 default_argument
= error_mark_node
;
15577 default_argument
= NULL_TREE
;
15579 /* Create the combined representation of the parameter and the
15580 default argument. */
15581 parameter
= build_tree_list (default_argument
, parameter
);
15588 tree default_argument
;
15590 /* Look for the `<'. */
15591 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15592 /* Parse the template-parameter-list. */
15593 cp_parser_template_parameter_list (parser
);
15594 /* Look for the `>'. */
15595 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15597 // If template requirements are present, parse them.
15600 tree reqs
= get_shorthand_constraints (current_template_parms
);
15601 if (tree r
= cp_parser_requires_clause_opt (parser
))
15602 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15603 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15606 /* Look for the `class' or 'typename' keywords. */
15607 cp_parser_type_parameter_key (parser
);
15608 /* If the next token is an ellipsis, we have a template
15610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15612 /* Consume the `...' token. */
15613 cp_lexer_consume_token (parser
->lexer
);
15614 maybe_warn_variadic_templates ();
15616 *is_parameter_pack
= true;
15618 /* If the next token is an `=', then there is a
15619 default-argument. If the next token is a `>', we are at
15620 the end of the parameter-list. If the next token is a `,',
15621 then we are at the end of this parameter. */
15622 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15623 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15624 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15626 identifier
= cp_parser_identifier (parser
);
15627 /* Treat invalid names as if the parameter were nameless. */
15628 if (identifier
== error_mark_node
)
15629 identifier
= NULL_TREE
;
15632 identifier
= NULL_TREE
;
15634 /* Create the template parameter. */
15635 parameter
= finish_template_template_parm (class_type_node
,
15638 /* If the next token is an `=', then there is a
15639 default-argument. */
15640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15643 = cp_parser_default_template_template_argument (parser
);
15645 /* Template parameter packs cannot have default
15647 if (*is_parameter_pack
)
15650 error_at (token
->location
,
15651 "template parameter pack %qD cannot "
15652 "have a default argument",
15655 error_at (token
->location
, "template parameter packs cannot "
15656 "have default arguments");
15657 default_argument
= NULL_TREE
;
15661 default_argument
= NULL_TREE
;
15663 /* Create the combined representation of the parameter and the
15664 default argument. */
15665 parameter
= build_tree_list (default_argument
, parameter
);
15670 gcc_unreachable ();
15677 /* Parse a template-id.
15680 template-name < template-argument-list [opt] >
15682 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15683 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15684 returned. Otherwise, if the template-name names a function, or set
15685 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15686 names a class, returns a TYPE_DECL for the specialization.
15688 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15689 uninstantiated templates. */
15692 cp_parser_template_id (cp_parser
*parser
,
15693 bool template_keyword_p
,
15694 bool check_dependency_p
,
15695 enum tag_types tag_type
,
15696 bool is_declaration
)
15701 cp_token_position start_of_id
= 0;
15702 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15703 bool is_identifier
;
15705 /* If the next token corresponds to a template-id, there is no need
15707 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15708 if (token
->type
== CPP_TEMPLATE_ID
)
15710 cp_lexer_consume_token (parser
->lexer
);
15711 return saved_checks_value (token
->u
.tree_check_value
);
15714 /* Avoid performing name lookup if there is no possibility of
15715 finding a template-id. */
15716 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15717 || (token
->type
== CPP_NAME
15718 && !cp_parser_nth_token_starts_template_argument_list_p
15721 cp_parser_error (parser
, "expected template-id");
15722 return error_mark_node
;
15725 /* Remember where the template-id starts. */
15726 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15727 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15729 push_deferring_access_checks (dk_deferred
);
15731 /* Parse the template-name. */
15732 is_identifier
= false;
15733 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15734 check_dependency_p
,
15738 if (templ
== error_mark_node
|| is_identifier
)
15740 pop_deferring_access_checks ();
15744 /* Since we're going to preserve any side-effects from this parse, set up a
15745 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15746 in the template arguments. */
15747 tentative_firewall
firewall (parser
);
15749 /* If we find the sequence `[:' after a template-name, it's probably
15750 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15751 parse correctly the argument list. */
15752 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15753 == CPP_OPEN_SQUARE
)
15754 && next_token
->flags
& DIGRAPH
15755 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15757 && !(next_token_2
->flags
& PREV_WHITE
))
15759 cp_parser_parse_tentatively (parser
);
15760 /* Change `:' into `::'. */
15761 next_token_2
->type
= CPP_SCOPE
;
15762 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15764 cp_lexer_consume_token (parser
->lexer
);
15766 /* Parse the arguments. */
15767 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15768 if (!cp_parser_parse_definitely (parser
))
15770 /* If we couldn't parse an argument list, then we revert our changes
15771 and return simply an error. Maybe this is not a template-id
15773 next_token_2
->type
= CPP_COLON
;
15774 cp_parser_error (parser
, "expected %<<%>");
15775 pop_deferring_access_checks ();
15776 return error_mark_node
;
15778 /* Otherwise, emit an error about the invalid digraph, but continue
15779 parsing because we got our argument list. */
15780 if (permerror (next_token
->location
,
15781 "%<<::%> cannot begin a template-argument list"))
15783 static bool hint
= false;
15784 inform (next_token
->location
,
15785 "%<<:%> is an alternate spelling for %<[%>."
15786 " Insert whitespace between %<<%> and %<::%>");
15787 if (!hint
&& !flag_permissive
)
15789 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15790 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15791 "accept your code)");
15798 /* Look for the `<' that starts the template-argument-list. */
15799 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15801 pop_deferring_access_checks ();
15802 return error_mark_node
;
15804 /* Parse the arguments. */
15805 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15808 /* Set the location to be of the form:
15809 template-name < template-argument-list [opt] >
15810 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15811 with caret == start at the start of the template-name,
15812 ranging until the closing '>'. */
15813 location_t finish_loc
15814 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15815 location_t combined_loc
15816 = make_location (token
->location
, token
->location
, finish_loc
);
15818 /* Check for concepts autos where they don't belong. We could
15819 identify types in some cases of idnetifier TEMPL, looking ahead
15820 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15821 types. We reject them in functions, but if what we have is an
15822 identifier, even with none_type we can't conclude it's NOT a
15823 type, we have to wait for template substitution. */
15824 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15825 template_id
= error_mark_node
;
15826 /* Build a representation of the specialization. */
15827 else if (identifier_p (templ
))
15828 template_id
= build_min_nt_loc (combined_loc
,
15831 else if (DECL_TYPE_TEMPLATE_P (templ
)
15832 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15834 bool entering_scope
;
15835 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15836 template (rather than some instantiation thereof) only if
15837 is not nested within some other construct. For example, in
15838 "template <typename T> void f(T) { A<T>::", A<T> is just an
15839 instantiation of A. */
15840 entering_scope
= (template_parm_scope_p ()
15841 && cp_lexer_next_token_is (parser
->lexer
,
15844 = finish_template_type (templ
, arguments
, entering_scope
);
15846 /* A template-like identifier may be a partial concept id. */
15847 else if (flag_concepts
15848 && (template_id
= (cp_parser_maybe_partial_concept_id
15849 (parser
, templ
, arguments
))))
15850 return template_id
;
15851 else if (variable_template_p (templ
))
15853 template_id
= lookup_template_variable (templ
, arguments
);
15854 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15855 SET_EXPR_LOCATION (template_id
, combined_loc
);
15859 /* If it's not a class-template or a template-template, it should be
15860 a function-template. */
15861 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15862 || TREE_CODE (templ
) == OVERLOAD
15863 || BASELINK_P (templ
)));
15865 template_id
= lookup_template_function (templ
, arguments
);
15866 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15867 SET_EXPR_LOCATION (template_id
, combined_loc
);
15870 /* If parsing tentatively, replace the sequence of tokens that makes
15871 up the template-id with a CPP_TEMPLATE_ID token. That way,
15872 should we re-parse the token stream, we will not have to repeat
15873 the effort required to do the parse, nor will we issue duplicate
15874 error messages about problems during instantiation of the
15877 /* Don't do this if we had a parse error in a declarator; re-parsing
15878 might succeed if a name changes meaning (60361). */
15879 && !(cp_parser_error_occurred (parser
)
15880 && cp_parser_parsing_tentatively (parser
)
15881 && parser
->in_declarator_p
))
15883 /* Reset the contents of the START_OF_ID token. */
15884 token
->type
= CPP_TEMPLATE_ID
;
15885 token
->location
= combined_loc
;
15887 /* We must mark the lookup as kept, so we don't throw it away on
15888 the first parse. */
15889 if (is_overloaded_fn (template_id
))
15890 lookup_keep (get_fns (template_id
), true);
15892 /* Retrieve any deferred checks. Do not pop this access checks yet
15893 so the memory will not be reclaimed during token replacing below. */
15894 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15895 token
->u
.tree_check_value
->value
= template_id
;
15896 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15897 token
->keyword
= RID_MAX
;
15899 /* Purge all subsequent tokens. */
15900 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15902 /* ??? Can we actually assume that, if template_id ==
15903 error_mark_node, we will have issued a diagnostic to the
15904 user, as opposed to simply marking the tentative parse as
15906 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15907 error_at (token
->location
, "parse error in template argument list");
15910 pop_to_parent_deferring_access_checks ();
15911 return template_id
;
15914 /* Parse a template-name.
15919 The standard should actually say:
15923 operator-function-id
15925 A defect report has been filed about this issue.
15927 A conversion-function-id cannot be a template name because they cannot
15928 be part of a template-id. In fact, looking at this code:
15930 a.operator K<int>()
15932 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15933 It is impossible to call a templated conversion-function-id with an
15934 explicit argument list, since the only allowed template parameter is
15935 the type to which it is converting.
15937 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15938 `template' keyword, in a construction like:
15942 In that case `f' is taken to be a template-name, even though there
15943 is no way of knowing for sure.
15945 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15946 name refers to a set of overloaded functions, at least one of which
15947 is a template, or an IDENTIFIER_NODE with the name of the template,
15948 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15949 names are looked up inside uninstantiated templates. */
15952 cp_parser_template_name (cp_parser
* parser
,
15953 bool template_keyword_p
,
15954 bool check_dependency_p
,
15955 bool is_declaration
,
15956 enum tag_types tag_type
,
15957 bool *is_identifier
)
15961 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15963 /* If the next token is `operator', then we have either an
15964 operator-function-id or a conversion-function-id. */
15965 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15967 /* We don't know whether we're looking at an
15968 operator-function-id or a conversion-function-id. */
15969 cp_parser_parse_tentatively (parser
);
15970 /* Try an operator-function-id. */
15971 identifier
= cp_parser_operator_function_id (parser
);
15972 /* If that didn't work, try a conversion-function-id. */
15973 if (!cp_parser_parse_definitely (parser
))
15975 cp_parser_error (parser
, "expected template-name");
15976 return error_mark_node
;
15979 /* Look for the identifier. */
15981 identifier
= cp_parser_identifier (parser
);
15983 /* If we didn't find an identifier, we don't have a template-id. */
15984 if (identifier
== error_mark_node
)
15985 return error_mark_node
;
15987 /* If the name immediately followed the `template' keyword, then it
15988 is a template-name. However, if the next token is not `<', then
15989 we do not treat it as a template-name, since it is not being used
15990 as part of a template-id. This enables us to handle constructs
15993 template <typename T> struct S { S(); };
15994 template <typename T> S<T>::S();
15996 correctly. We would treat `S' as a template -- if it were `S<T>'
15997 -- but we do not if there is no `<'. */
15999 if (processing_template_decl
16000 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16002 /* In a declaration, in a dependent context, we pretend that the
16003 "template" keyword was present in order to improve error
16004 recovery. For example, given:
16006 template <typename T> void f(T::X<int>);
16008 we want to treat "X<int>" as a template-id. */
16010 && !template_keyword_p
16011 && parser
->scope
&& TYPE_P (parser
->scope
)
16012 && check_dependency_p
16013 && dependent_scope_p (parser
->scope
)
16014 /* Do not do this for dtors (or ctors), since they never
16015 need the template keyword before their name. */
16016 && !constructor_name_p (identifier
, parser
->scope
))
16018 cp_token_position start
= 0;
16020 /* Explain what went wrong. */
16021 error_at (token
->location
, "non-template %qD used as template",
16023 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16024 parser
->scope
, identifier
);
16025 /* If parsing tentatively, find the location of the "<" token. */
16026 if (cp_parser_simulate_error (parser
))
16027 start
= cp_lexer_token_position (parser
->lexer
, true);
16028 /* Parse the template arguments so that we can issue error
16029 messages about them. */
16030 cp_lexer_consume_token (parser
->lexer
);
16031 cp_parser_enclosed_template_argument_list (parser
);
16032 /* Skip tokens until we find a good place from which to
16033 continue parsing. */
16034 cp_parser_skip_to_closing_parenthesis (parser
,
16035 /*recovering=*/true,
16037 /*consume_paren=*/false);
16038 /* If parsing tentatively, permanently remove the
16039 template argument list. That will prevent duplicate
16040 error messages from being issued about the missing
16041 "template" keyword. */
16043 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16045 *is_identifier
= true;
16046 parser
->context
->object_type
= NULL_TREE
;
16050 /* If the "template" keyword is present, then there is generally
16051 no point in doing name-lookup, so we just return IDENTIFIER.
16052 But, if the qualifying scope is non-dependent then we can
16053 (and must) do name-lookup normally. */
16054 if (template_keyword_p
)
16056 tree scope
= (parser
->scope
? parser
->scope
16057 : parser
->context
->object_type
);
16058 if (scope
&& TYPE_P (scope
)
16059 && (!CLASS_TYPE_P (scope
)
16060 || (check_dependency_p
&& dependent_type_p (scope
))))
16062 /* We're optimizing away the call to cp_parser_lookup_name, but
16063 we still need to do this. */
16064 parser
->context
->object_type
= NULL_TREE
;
16070 /* Look up the name. */
16071 decl
= cp_parser_lookup_name (parser
, identifier
,
16073 /*is_template=*/true,
16074 /*is_namespace=*/false,
16075 check_dependency_p
,
16076 /*ambiguous_decls=*/NULL
,
16079 decl
= strip_using_decl (decl
);
16081 /* If DECL is a template, then the name was a template-name. */
16082 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16084 if (TREE_DEPRECATED (decl
)
16085 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16086 warn_deprecated_use (decl
, NULL_TREE
);
16090 /* The standard does not explicitly indicate whether a name that
16091 names a set of overloaded declarations, some of which are
16092 templates, is a template-name. However, such a name should
16093 be a template-name; otherwise, there is no way to form a
16094 template-id for the overloaded templates. */
16095 bool found
= false;
16097 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16098 !found
&& iter
; ++iter
)
16099 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16104 /* The name does not name a template. */
16105 cp_parser_error (parser
, "expected template-name");
16106 return error_mark_node
;
16110 /* If DECL is dependent, and refers to a function, then just return
16111 its name; we will look it up again during template instantiation. */
16112 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16114 tree scope
= ovl_scope (decl
);
16115 if (TYPE_P (scope
) && dependent_type_p (scope
))
16122 /* Parse a template-argument-list.
16124 template-argument-list:
16125 template-argument ... [opt]
16126 template-argument-list , template-argument ... [opt]
16128 Returns a TREE_VEC containing the arguments. */
16131 cp_parser_template_argument_list (cp_parser
* parser
)
16133 tree fixed_args
[10];
16134 unsigned n_args
= 0;
16135 unsigned alloced
= 10;
16136 tree
*arg_ary
= fixed_args
;
16138 bool saved_in_template_argument_list_p
;
16140 bool saved_non_ice_p
;
16142 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16143 parser
->in_template_argument_list_p
= true;
16144 /* Even if the template-id appears in an integral
16145 constant-expression, the contents of the argument list do
16147 saved_ice_p
= parser
->integral_constant_expression_p
;
16148 parser
->integral_constant_expression_p
= false;
16149 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16150 parser
->non_integral_constant_expression_p
= false;
16152 /* Parse the arguments. */
16158 /* Consume the comma. */
16159 cp_lexer_consume_token (parser
->lexer
);
16161 /* Parse the template-argument. */
16162 argument
= cp_parser_template_argument (parser
);
16164 /* If the next token is an ellipsis, we're expanding a template
16166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16168 if (argument
== error_mark_node
)
16170 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16171 error_at (token
->location
,
16172 "expected parameter pack before %<...%>");
16174 /* Consume the `...' token. */
16175 cp_lexer_consume_token (parser
->lexer
);
16177 /* Make the argument into a TYPE_PACK_EXPANSION or
16178 EXPR_PACK_EXPANSION. */
16179 argument
= make_pack_expansion (argument
);
16182 if (n_args
== alloced
)
16186 if (arg_ary
== fixed_args
)
16188 arg_ary
= XNEWVEC (tree
, alloced
);
16189 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16192 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16194 arg_ary
[n_args
++] = argument
;
16196 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16198 vec
= make_tree_vec (n_args
);
16201 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16203 if (arg_ary
!= fixed_args
)
16205 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16206 parser
->integral_constant_expression_p
= saved_ice_p
;
16207 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16209 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16213 /* Parse a template-argument.
16216 assignment-expression
16220 The representation is that of an assignment-expression, type-id, or
16221 id-expression -- except that the qualified id-expression is
16222 evaluated, so that the value returned is either a DECL or an
16225 Although the standard says "assignment-expression", it forbids
16226 throw-expressions or assignments in the template argument.
16227 Therefore, we use "conditional-expression" instead. */
16230 cp_parser_template_argument (cp_parser
* parser
)
16235 bool maybe_type_id
= false;
16236 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16237 location_t loc
= 0;
16240 /* There's really no way to know what we're looking at, so we just
16241 try each alternative in order.
16245 In a template-argument, an ambiguity between a type-id and an
16246 expression is resolved to a type-id, regardless of the form of
16247 the corresponding template-parameter.
16249 Therefore, we try a type-id first. */
16250 cp_parser_parse_tentatively (parser
);
16251 argument
= cp_parser_template_type_arg (parser
);
16252 /* If there was no error parsing the type-id but the next token is a
16253 '>>', our behavior depends on which dialect of C++ we're
16254 parsing. In C++98, we probably found a typo for '> >'. But there
16255 are type-id which are also valid expressions. For instance:
16257 struct X { int operator >> (int); };
16258 template <int V> struct Foo {};
16261 Here 'X()' is a valid type-id of a function type, but the user just
16262 wanted to write the expression "X() >> 5". Thus, we remember that we
16263 found a valid type-id, but we still try to parse the argument as an
16264 expression to see what happens.
16266 In C++0x, the '>>' will be considered two separate '>'
16268 if (!cp_parser_error_occurred (parser
)
16269 && cxx_dialect
== cxx98
16270 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16272 maybe_type_id
= true;
16273 cp_parser_abort_tentative_parse (parser
);
16277 /* If the next token isn't a `,' or a `>', then this argument wasn't
16278 really finished. This means that the argument is not a valid
16280 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16281 cp_parser_error (parser
, "expected template-argument");
16282 /* If that worked, we're done. */
16283 if (cp_parser_parse_definitely (parser
))
16286 /* We're still not sure what the argument will be. */
16287 cp_parser_parse_tentatively (parser
);
16288 /* Try a template. */
16289 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16290 argument
= cp_parser_id_expression (parser
,
16291 /*template_keyword_p=*/false,
16292 /*check_dependency_p=*/true,
16294 /*declarator_p=*/false,
16295 /*optional_p=*/false);
16296 /* If the next token isn't a `,' or a `>', then this argument wasn't
16297 really finished. */
16298 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16299 cp_parser_error (parser
, "expected template-argument");
16300 if (!cp_parser_error_occurred (parser
))
16302 /* Figure out what is being referred to. If the id-expression
16303 was for a class template specialization, then we will have a
16304 TYPE_DECL at this point. There is no need to do name lookup
16305 at this point in that case. */
16306 if (TREE_CODE (argument
) != TYPE_DECL
)
16307 argument
= cp_parser_lookup_name (parser
, argument
,
16309 /*is_template=*/template_p
,
16310 /*is_namespace=*/false,
16311 /*check_dependency=*/true,
16312 /*ambiguous_decls=*/NULL
,
16313 argument_start_token
->location
);
16314 /* Handle a constrained-type-specifier for a non-type template
16316 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16318 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16319 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16320 cp_parser_error (parser
, "expected template-name");
16322 if (cp_parser_parse_definitely (parser
))
16324 if (TREE_DEPRECATED (argument
))
16325 warn_deprecated_use (argument
, NULL_TREE
);
16328 /* It must be a non-type argument. In C++17 any constant-expression is
16330 if (cxx_dialect
> cxx14
)
16333 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16335 -- an integral constant-expression of integral or enumeration
16338 -- the name of a non-type template-parameter; or
16340 -- the name of an object or function with external linkage...
16342 -- the address of an object or function with external linkage...
16344 -- a pointer to member... */
16345 /* Look for a non-type template parameter. */
16346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16348 cp_parser_parse_tentatively (parser
);
16349 argument
= cp_parser_primary_expression (parser
,
16350 /*address_p=*/false,
16352 /*template_arg_p=*/true,
16354 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16355 || !cp_parser_next_token_ends_template_argument_p (parser
))
16356 cp_parser_simulate_error (parser
);
16357 if (cp_parser_parse_definitely (parser
))
16361 /* If the next token is "&", the argument must be the address of an
16362 object or function with external linkage. */
16363 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16366 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16367 cp_lexer_consume_token (parser
->lexer
);
16369 /* See if we might have an id-expression. */
16370 token
= cp_lexer_peek_token (parser
->lexer
);
16371 if (token
->type
== CPP_NAME
16372 || token
->keyword
== RID_OPERATOR
16373 || token
->type
== CPP_SCOPE
16374 || token
->type
== CPP_TEMPLATE_ID
16375 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16377 cp_parser_parse_tentatively (parser
);
16378 argument
= cp_parser_primary_expression (parser
,
16381 /*template_arg_p=*/true,
16383 if (cp_parser_error_occurred (parser
)
16384 || !cp_parser_next_token_ends_template_argument_p (parser
))
16385 cp_parser_abort_tentative_parse (parser
);
16390 if (INDIRECT_REF_P (argument
))
16392 /* Strip the dereference temporarily. */
16393 gcc_assert (REFERENCE_REF_P (argument
));
16394 argument
= TREE_OPERAND (argument
, 0);
16397 /* If we're in a template, we represent a qualified-id referring
16398 to a static data member as a SCOPE_REF even if the scope isn't
16399 dependent so that we can check access control later. */
16401 if (TREE_CODE (probe
) == SCOPE_REF
)
16402 probe
= TREE_OPERAND (probe
, 1);
16405 /* A variable without external linkage might still be a
16406 valid constant-expression, so no error is issued here
16407 if the external-linkage check fails. */
16408 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16409 cp_parser_simulate_error (parser
);
16411 else if (is_overloaded_fn (argument
))
16412 /* All overloaded functions are allowed; if the external
16413 linkage test does not pass, an error will be issued
16417 && (TREE_CODE (argument
) == OFFSET_REF
16418 || TREE_CODE (argument
) == SCOPE_REF
))
16419 /* A pointer-to-member. */
16421 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16424 cp_parser_simulate_error (parser
);
16426 if (cp_parser_parse_definitely (parser
))
16429 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16430 tf_warning_or_error
);
16432 argument
= convert_from_reference (argument
);
16437 /* If the argument started with "&", there are no other valid
16438 alternatives at this point. */
16441 cp_parser_error (parser
, "invalid non-type template argument");
16442 return error_mark_node
;
16446 /* If the argument wasn't successfully parsed as a type-id followed
16447 by '>>', the argument can only be a constant expression now.
16448 Otherwise, we try parsing the constant-expression tentatively,
16449 because the argument could really be a type-id. */
16451 cp_parser_parse_tentatively (parser
);
16453 if (cxx_dialect
<= cxx14
)
16454 argument
= cp_parser_constant_expression (parser
);
16457 /* With C++17 generalized non-type template arguments we need to handle
16458 lvalue constant expressions, too. */
16459 argument
= cp_parser_assignment_expression (parser
);
16460 require_potential_constant_expression (argument
);
16463 if (!maybe_type_id
)
16465 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16466 cp_parser_error (parser
, "expected template-argument");
16467 if (cp_parser_parse_definitely (parser
))
16469 /* We did our best to parse the argument as a non type-id, but that
16470 was the only alternative that matched (albeit with a '>' after
16471 it). We can assume it's just a typo from the user, and a
16472 diagnostic will then be issued. */
16473 return cp_parser_template_type_arg (parser
);
16476 /* Parse an explicit-instantiation.
16478 explicit-instantiation:
16479 template declaration
16481 Although the standard says `declaration', what it really means is:
16483 explicit-instantiation:
16484 template decl-specifier-seq [opt] declarator [opt] ;
16486 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16487 supposed to be allowed. A defect report has been filed about this
16492 explicit-instantiation:
16493 storage-class-specifier template
16494 decl-specifier-seq [opt] declarator [opt] ;
16495 function-specifier template
16496 decl-specifier-seq [opt] declarator [opt] ; */
16499 cp_parser_explicit_instantiation (cp_parser
* parser
)
16501 int declares_class_or_enum
;
16502 cp_decl_specifier_seq decl_specifiers
;
16503 tree extension_specifier
= NULL_TREE
;
16505 timevar_push (TV_TEMPLATE_INST
);
16507 /* Look for an (optional) storage-class-specifier or
16508 function-specifier. */
16509 if (cp_parser_allow_gnu_extensions_p (parser
))
16511 extension_specifier
16512 = cp_parser_storage_class_specifier_opt (parser
);
16513 if (!extension_specifier
)
16514 extension_specifier
16515 = cp_parser_function_specifier_opt (parser
,
16516 /*decl_specs=*/NULL
);
16519 /* Look for the `template' keyword. */
16520 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16521 /* Let the front end know that we are processing an explicit
16523 begin_explicit_instantiation ();
16524 /* [temp.explicit] says that we are supposed to ignore access
16525 control while processing explicit instantiation directives. */
16526 push_deferring_access_checks (dk_no_check
);
16527 /* Parse a decl-specifier-seq. */
16528 cp_parser_decl_specifier_seq (parser
,
16529 CP_PARSER_FLAGS_OPTIONAL
,
16531 &declares_class_or_enum
);
16532 /* If there was exactly one decl-specifier, and it declared a class,
16533 and there's no declarator, then we have an explicit type
16535 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16539 type
= check_tag_decl (&decl_specifiers
,
16540 /*explicit_type_instantiation_p=*/true);
16541 /* Turn access control back on for names used during
16542 template instantiation. */
16543 pop_deferring_access_checks ();
16545 do_type_instantiation (type
, extension_specifier
,
16546 /*complain=*/tf_error
);
16550 cp_declarator
*declarator
;
16553 /* Parse the declarator. */
16555 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16556 /*ctor_dtor_or_conv_p=*/NULL
,
16557 /*parenthesized_p=*/NULL
,
16558 /*member_p=*/false,
16559 /*friend_p=*/false);
16560 if (declares_class_or_enum
& 2)
16561 cp_parser_check_for_definition_in_return_type (declarator
,
16562 decl_specifiers
.type
,
16563 decl_specifiers
.locations
[ds_type_spec
]);
16564 if (declarator
!= cp_error_declarator
)
16566 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16567 permerror (decl_specifiers
.locations
[ds_inline
],
16568 "explicit instantiation shall not use"
16569 " %<inline%> specifier");
16570 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16571 permerror (decl_specifiers
.locations
[ds_constexpr
],
16572 "explicit instantiation shall not use"
16573 " %<constexpr%> specifier");
16575 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16576 NORMAL
, 0, &decl_specifiers
.attributes
);
16577 /* Turn access control back on for names used during
16578 template instantiation. */
16579 pop_deferring_access_checks ();
16580 /* Do the explicit instantiation. */
16581 do_decl_instantiation (decl
, extension_specifier
);
16585 pop_deferring_access_checks ();
16586 /* Skip the body of the explicit instantiation. */
16587 cp_parser_skip_to_end_of_statement (parser
);
16590 /* We're done with the instantiation. */
16591 end_explicit_instantiation ();
16593 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16595 timevar_pop (TV_TEMPLATE_INST
);
16598 /* Parse an explicit-specialization.
16600 explicit-specialization:
16601 template < > declaration
16603 Although the standard says `declaration', what it really means is:
16605 explicit-specialization:
16606 template <> decl-specifier [opt] init-declarator [opt] ;
16607 template <> function-definition
16608 template <> explicit-specialization
16609 template <> template-declaration */
16612 cp_parser_explicit_specialization (cp_parser
* parser
)
16614 bool need_lang_pop
;
16615 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16617 /* Look for the `template' keyword. */
16618 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16619 /* Look for the `<'. */
16620 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16621 /* Look for the `>'. */
16622 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16623 /* We have processed another parameter list. */
16624 ++parser
->num_template_parameter_lists
;
16627 A template ... explicit specialization ... shall not have C
16629 if (current_lang_name
== lang_name_c
)
16631 error_at (token
->location
, "template specialization with C linkage");
16632 maybe_show_extern_c_location ();
16633 /* Give it C++ linkage to avoid confusing other parts of the
16635 push_lang_context (lang_name_cplusplus
);
16636 need_lang_pop
= true;
16639 need_lang_pop
= false;
16640 /* Let the front end know that we are beginning a specialization. */
16641 if (!begin_specialization ())
16643 end_specialization ();
16647 /* If the next keyword is `template', we need to figure out whether
16648 or not we're looking a template-declaration. */
16649 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16651 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16652 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16653 cp_parser_template_declaration_after_export (parser
,
16654 /*member_p=*/false);
16656 cp_parser_explicit_specialization (parser
);
16659 /* Parse the dependent declaration. */
16660 cp_parser_single_declaration (parser
,
16662 /*member_p=*/false,
16663 /*explicit_specialization_p=*/true,
16664 /*friend_p=*/NULL
);
16665 /* We're done with the specialization. */
16666 end_specialization ();
16667 /* For the erroneous case of a template with C linkage, we pushed an
16668 implicit C++ linkage scope; exit that scope now. */
16670 pop_lang_context ();
16671 /* We're done with this parameter list. */
16672 --parser
->num_template_parameter_lists
;
16675 /* Parse a type-specifier.
16678 simple-type-specifier
16681 elaborated-type-specifier
16689 Returns a representation of the type-specifier. For a
16690 class-specifier, enum-specifier, or elaborated-type-specifier, a
16691 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16693 The parser flags FLAGS is used to control type-specifier parsing.
16695 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16696 in a decl-specifier-seq.
16698 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16699 class-specifier, enum-specifier, or elaborated-type-specifier, then
16700 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16701 if a type is declared; 2 if it is defined. Otherwise, it is set to
16704 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16705 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16706 is set to FALSE. */
16709 cp_parser_type_specifier (cp_parser
* parser
,
16710 cp_parser_flags flags
,
16711 cp_decl_specifier_seq
*decl_specs
,
16712 bool is_declaration
,
16713 int* declares_class_or_enum
,
16714 bool* is_cv_qualifier
)
16716 tree type_spec
= NULL_TREE
;
16719 cp_decl_spec ds
= ds_last
;
16721 /* Assume this type-specifier does not declare a new type. */
16722 if (declares_class_or_enum
)
16723 *declares_class_or_enum
= 0;
16724 /* And that it does not specify a cv-qualifier. */
16725 if (is_cv_qualifier
)
16726 *is_cv_qualifier
= false;
16727 /* Peek at the next token. */
16728 token
= cp_lexer_peek_token (parser
->lexer
);
16730 /* If we're looking at a keyword, we can use that to guide the
16731 production we choose. */
16732 keyword
= token
->keyword
;
16736 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16737 goto elaborated_type_specifier
;
16739 /* Look for the enum-specifier. */
16740 type_spec
= cp_parser_enum_specifier (parser
);
16741 /* If that worked, we're done. */
16744 if (declares_class_or_enum
)
16745 *declares_class_or_enum
= 2;
16747 cp_parser_set_decl_spec_type (decl_specs
,
16750 /*type_definition_p=*/true);
16754 goto elaborated_type_specifier
;
16756 /* Any of these indicate either a class-specifier, or an
16757 elaborated-type-specifier. */
16761 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16762 goto elaborated_type_specifier
;
16764 /* Parse tentatively so that we can back up if we don't find a
16765 class-specifier. */
16766 cp_parser_parse_tentatively (parser
);
16767 /* Look for the class-specifier. */
16768 type_spec
= cp_parser_class_specifier (parser
);
16769 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16770 /* If that worked, we're done. */
16771 if (cp_parser_parse_definitely (parser
))
16773 if (declares_class_or_enum
)
16774 *declares_class_or_enum
= 2;
16776 cp_parser_set_decl_spec_type (decl_specs
,
16779 /*type_definition_p=*/true);
16783 /* Fall through. */
16784 elaborated_type_specifier
:
16785 /* We're declaring (not defining) a class or enum. */
16786 if (declares_class_or_enum
)
16787 *declares_class_or_enum
= 1;
16789 /* Fall through. */
16791 /* Look for an elaborated-type-specifier. */
16793 = (cp_parser_elaborated_type_specifier
16795 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16798 cp_parser_set_decl_spec_type (decl_specs
,
16801 /*type_definition_p=*/false);
16806 if (is_cv_qualifier
)
16807 *is_cv_qualifier
= true;
16812 if (is_cv_qualifier
)
16813 *is_cv_qualifier
= true;
16818 if (is_cv_qualifier
)
16819 *is_cv_qualifier
= true;
16823 /* The `__complex__' keyword is a GNU extension. */
16831 /* Handle simple keywords. */
16836 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16837 decl_specs
->any_specifiers_p
= true;
16839 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16842 /* If we do not already have a type-specifier, assume we are looking
16843 at a simple-type-specifier. */
16844 type_spec
= cp_parser_simple_type_specifier (parser
,
16848 /* If we didn't find a type-specifier, and a type-specifier was not
16849 optional in this context, issue an error message. */
16850 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16852 cp_parser_error (parser
, "expected type specifier");
16853 return error_mark_node
;
16859 /* Parse a simple-type-specifier.
16861 simple-type-specifier:
16862 :: [opt] nested-name-specifier [opt] type-name
16863 :: [opt] nested-name-specifier template template-id
16878 simple-type-specifier:
16880 decltype ( expression )
16883 __underlying_type ( type-id )
16887 nested-name-specifier(opt) template-name
16891 simple-type-specifier:
16893 __typeof__ unary-expression
16894 __typeof__ ( type-id )
16895 __typeof__ ( type-id ) { initializer-list , [opt] }
16897 Concepts Extension:
16899 simple-type-specifier:
16900 constrained-type-specifier
16902 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16903 appropriately updated. */
16906 cp_parser_simple_type_specifier (cp_parser
* parser
,
16907 cp_decl_specifier_seq
*decl_specs
,
16908 cp_parser_flags flags
)
16910 tree type
= NULL_TREE
;
16914 /* Peek at the next token. */
16915 token
= cp_lexer_peek_token (parser
->lexer
);
16917 /* If we're looking at a keyword, things are easy. */
16918 switch (token
->keyword
)
16922 decl_specs
->explicit_char_p
= true;
16923 type
= char_type_node
;
16926 type
= char16_type_node
;
16929 type
= char32_type_node
;
16932 type
= wchar_type_node
;
16935 type
= boolean_type_node
;
16938 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16939 type
= short_integer_type_node
;
16943 decl_specs
->explicit_int_p
= true;
16944 type
= integer_type_node
;
16950 idx
= token
->keyword
- RID_INT_N_0
;
16951 if (! int_n_enabled_p
[idx
])
16955 decl_specs
->explicit_intN_p
= true;
16956 decl_specs
->int_n_idx
= idx
;
16958 type
= int_n_trees
[idx
].signed_type
;
16962 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16963 type
= long_integer_type_node
;
16966 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16967 type
= integer_type_node
;
16970 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16971 type
= unsigned_type_node
;
16974 type
= float_type_node
;
16977 type
= double_type_node
;
16980 type
= void_type_node
;
16984 maybe_warn_cpp0x (CPP0X_AUTO
);
16985 if (parser
->auto_is_implicit_function_template_parm_p
)
16987 /* The 'auto' might be the placeholder return type for a function decl
16988 with trailing return type. */
16989 bool have_trailing_return_fn_decl
= false;
16991 cp_parser_parse_tentatively (parser
);
16992 cp_lexer_consume_token (parser
->lexer
);
16993 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16994 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16995 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16996 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16998 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17000 cp_lexer_consume_token (parser
->lexer
);
17001 cp_parser_skip_to_closing_parenthesis (parser
,
17002 /*recovering*/false,
17004 /*consume_paren*/true);
17008 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17010 have_trailing_return_fn_decl
= true;
17014 cp_lexer_consume_token (parser
->lexer
);
17016 cp_parser_abort_tentative_parse (parser
);
17018 if (have_trailing_return_fn_decl
)
17020 type
= make_auto ();
17024 if (cxx_dialect
>= cxx14
)
17026 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17027 type
= TREE_TYPE (type
);
17030 type
= error_mark_node
;
17032 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17034 if (cxx_dialect
< cxx14
)
17035 error_at (token
->location
,
17036 "use of %<auto%> in lambda parameter declaration "
17037 "only available with "
17038 "-std=c++14 or -std=gnu++14");
17040 else if (cxx_dialect
< cxx14
)
17041 error_at (token
->location
,
17042 "use of %<auto%> in parameter declaration "
17043 "only available with "
17044 "-std=c++14 or -std=gnu++14");
17045 else if (!flag_concepts
)
17046 pedwarn (token
->location
, 0,
17047 "use of %<auto%> in parameter declaration "
17048 "only available with -fconcepts");
17051 type
= make_auto ();
17055 /* Since DR 743, decltype can either be a simple-type-specifier by
17056 itself or begin a nested-name-specifier. Parsing it will replace
17057 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17058 handling below decide what to do. */
17059 cp_parser_decltype (parser
);
17060 cp_lexer_set_token_position (parser
->lexer
, token
);
17064 /* Consume the `typeof' token. */
17065 cp_lexer_consume_token (parser
->lexer
);
17066 /* Parse the operand to `typeof'. */
17067 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17068 /* If it is not already a TYPE, take its type. */
17069 if (!TYPE_P (type
))
17070 type
= finish_typeof (type
);
17073 cp_parser_set_decl_spec_type (decl_specs
, type
,
17075 /*type_definition_p=*/false);
17079 case RID_UNDERLYING_TYPE
:
17080 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17082 cp_parser_set_decl_spec_type (decl_specs
, type
,
17084 /*type_definition_p=*/false);
17089 case RID_DIRECT_BASES
:
17090 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17092 cp_parser_set_decl_spec_type (decl_specs
, type
,
17094 /*type_definition_p=*/false);
17100 /* If token is an already-parsed decltype not followed by ::,
17101 it's a simple-type-specifier. */
17102 if (token
->type
== CPP_DECLTYPE
17103 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17105 type
= saved_checks_value (token
->u
.tree_check_value
);
17108 cp_parser_set_decl_spec_type (decl_specs
, type
,
17110 /*type_definition_p=*/false);
17111 /* Remember that we are handling a decltype in order to
17112 implement the resolution of DR 1510 when the argument
17113 isn't instantiation dependent. */
17114 decl_specs
->decltype_p
= true;
17116 cp_lexer_consume_token (parser
->lexer
);
17120 /* If the type-specifier was for a built-in type, we're done. */
17123 /* Record the type. */
17125 && (token
->keyword
!= RID_SIGNED
17126 && token
->keyword
!= RID_UNSIGNED
17127 && token
->keyword
!= RID_SHORT
17128 && token
->keyword
!= RID_LONG
))
17129 cp_parser_set_decl_spec_type (decl_specs
,
17132 /*type_definition_p=*/false);
17134 decl_specs
->any_specifiers_p
= true;
17136 /* Consume the token. */
17137 cp_lexer_consume_token (parser
->lexer
);
17139 if (type
== error_mark_node
)
17140 return error_mark_node
;
17142 /* There is no valid C++ program where a non-template type is
17143 followed by a "<". That usually indicates that the user thought
17144 that the type was a template. */
17145 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17148 return TYPE_NAME (type
);
17151 /* The type-specifier must be a user-defined type. */
17152 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17157 /* Don't gobble tokens or issue error messages if this is an
17158 optional type-specifier. */
17159 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17160 cp_parser_parse_tentatively (parser
);
17162 token
= cp_lexer_peek_token (parser
->lexer
);
17164 /* Look for the optional `::' operator. */
17166 = (cp_parser_global_scope_opt (parser
,
17167 /*current_scope_valid_p=*/false)
17169 /* Look for the nested-name specifier. */
17171 = (cp_parser_nested_name_specifier_opt (parser
,
17172 /*typename_keyword_p=*/false,
17173 /*check_dependency_p=*/true,
17175 /*is_declaration=*/false)
17177 /* If we have seen a nested-name-specifier, and the next token
17178 is `template', then we are using the template-id production. */
17180 && cp_parser_optional_template_keyword (parser
))
17182 /* Look for the template-id. */
17183 type
= cp_parser_template_id (parser
,
17184 /*template_keyword_p=*/true,
17185 /*check_dependency_p=*/true,
17187 /*is_declaration=*/false);
17188 /* If the template-id did not name a type, we are out of
17190 if (TREE_CODE (type
) != TYPE_DECL
)
17192 cp_parser_error (parser
, "expected template-id for type");
17196 /* Otherwise, look for a type-name. */
17198 type
= cp_parser_type_name (parser
);
17199 /* Keep track of all name-lookups performed in class scopes. */
17203 && TREE_CODE (type
) == TYPE_DECL
17204 && identifier_p (DECL_NAME (type
)))
17205 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17206 /* If it didn't work out, we don't have a TYPE. */
17207 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17208 && !cp_parser_parse_definitely (parser
))
17210 if (!type
&& cxx_dialect
>= cxx17
)
17212 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17213 cp_parser_parse_tentatively (parser
);
17215 cp_parser_global_scope_opt (parser
,
17216 /*current_scope_valid_p=*/false);
17217 cp_parser_nested_name_specifier_opt (parser
,
17218 /*typename_keyword_p=*/false,
17219 /*check_dependency_p=*/true,
17221 /*is_declaration=*/false);
17222 tree name
= cp_parser_identifier (parser
);
17223 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17224 && parser
->scope
!= error_mark_node
)
17226 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17228 /*is_template=*/false,
17229 /*is_namespace=*/false,
17230 /*check_dependency=*/true,
17231 /*ambiguous_decls=*/NULL
,
17233 if (tmpl
&& tmpl
!= error_mark_node
17234 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17235 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17236 type
= make_template_placeholder (tmpl
);
17239 type
= error_mark_node
;
17240 if (!cp_parser_simulate_error (parser
))
17241 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17242 NLE_TYPE
, token
->location
);
17246 type
= error_mark_node
;
17248 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17249 && !cp_parser_parse_definitely (parser
))
17252 if (type
&& decl_specs
)
17253 cp_parser_set_decl_spec_type (decl_specs
, type
,
17255 /*type_definition_p=*/false);
17258 /* If we didn't get a type-name, issue an error message. */
17259 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17261 cp_parser_error (parser
, "expected type-name");
17262 return error_mark_node
;
17265 if (type
&& type
!= error_mark_node
)
17267 /* See if TYPE is an Objective-C type, and if so, parse and
17268 accept any protocol references following it. Do this before
17269 the cp_parser_check_for_invalid_template_id() call, because
17270 Objective-C types can be followed by '<...>' which would
17271 enclose protocol names rather than template arguments, and so
17272 everything is fine. */
17273 if (c_dialect_objc () && !parser
->scope
17274 && (objc_is_id (type
) || objc_is_class_name (type
)))
17276 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17277 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17279 /* Clobber the "unqualified" type previously entered into
17280 DECL_SPECS with the new, improved protocol-qualified version. */
17282 decl_specs
->type
= qual_type
;
17287 /* There is no valid C++ program where a non-template type is
17288 followed by a "<". That usually indicates that the user
17289 thought that the type was a template. */
17290 cp_parser_check_for_invalid_template_id (parser
, type
,
17298 /* Parse a type-name.
17304 simple-template-id [in c++0x]
17321 Returns a TYPE_DECL for the type. */
17324 cp_parser_type_name (cp_parser
* parser
)
17326 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17331 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17335 /* We can't know yet whether it is a class-name or not. */
17336 cp_parser_parse_tentatively (parser
);
17337 /* Try a class-name. */
17338 type_decl
= cp_parser_class_name (parser
,
17339 typename_keyword_p
,
17340 /*template_keyword_p=*/false,
17342 /*check_dependency_p=*/true,
17343 /*class_head_p=*/false,
17344 /*is_declaration=*/false);
17345 /* If it's not a class-name, keep looking. */
17346 if (!cp_parser_parse_definitely (parser
))
17348 if (cxx_dialect
< cxx11
)
17349 /* It must be a typedef-name or an enum-name. */
17350 return cp_parser_nonclass_name (parser
);
17352 cp_parser_parse_tentatively (parser
);
17353 /* It is either a simple-template-id representing an
17354 instantiation of an alias template... */
17355 type_decl
= cp_parser_template_id (parser
,
17356 /*template_keyword_p=*/false,
17357 /*check_dependency_p=*/true,
17359 /*is_declaration=*/false);
17360 /* Note that this must be an instantiation of an alias template
17361 because [temp.names]/6 says:
17363 A template-id that names an alias template specialization
17366 Whereas [temp.names]/7 says:
17368 A simple-template-id that names a class template
17369 specialization is a class-name.
17371 With concepts, this could also be a partial-concept-id that
17372 declares a non-type template parameter. */
17373 if (type_decl
!= NULL_TREE
17374 && TREE_CODE (type_decl
) == TYPE_DECL
17375 && TYPE_DECL_ALIAS_P (type_decl
))
17376 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17377 else if (is_constrained_parameter (type_decl
))
17378 /* Don't do anything. */ ;
17380 cp_parser_simulate_error (parser
);
17382 if (!cp_parser_parse_definitely (parser
))
17383 /* ... Or a typedef-name or an enum-name. */
17384 return cp_parser_nonclass_name (parser
);
17390 /* Check if DECL and ARGS can form a constrained-type-specifier.
17391 If ARGS is non-null, we try to form a concept check of the
17392 form DECL<?, ARGS> where ? is a wildcard that matches any
17393 kind of template argument. If ARGS is NULL, then we try to
17394 form a concept check of the form DECL<?>. */
17397 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17398 tree decl
, tree args
)
17400 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17402 /* If we a constrained-type-specifier cannot be deduced. */
17403 if (parser
->prevent_constrained_type_specifiers
)
17406 /* A constrained type specifier can only be found in an
17407 overload set or as a reference to a template declaration.
17409 FIXME: This might be masking a bug. It's possible that
17410 that the deduction below is causing template specializations
17411 to be formed with the wildcard as an argument. */
17412 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17415 /* Try to build a call expression that evaluates the
17416 concept. This can fail if the overload set refers
17417 only to non-templates. */
17418 tree placeholder
= build_nt (WILDCARD_DECL
);
17419 tree check
= build_concept_check (decl
, placeholder
, args
);
17420 if (check
== error_mark_node
)
17423 /* Deduce the checked constraint and the prototype parameter.
17425 FIXME: In certain cases, failure to deduce should be a
17426 diagnosable error. */
17429 if (!deduce_constrained_parameter (check
, conc
, proto
))
17432 /* In template parameter scope, this results in a constrained
17433 parameter. Return a descriptor of that parm. */
17434 if (processing_template_parmlist
)
17435 return build_constrained_parameter (conc
, proto
, args
);
17437 /* In a parameter-declaration-clause, constrained-type
17438 specifiers result in invented template parameters. */
17439 if (parser
->auto_is_implicit_function_template_parm_p
)
17441 tree x
= build_constrained_parameter (conc
, proto
, args
);
17442 return synthesize_implicit_template_parm (parser
, x
);
17446 /* Otherwise, we're in a context where the constrained
17447 type name is deduced and the constraint applies
17448 after deduction. */
17449 return make_constrained_auto (conc
, args
);
17455 /* If DECL refers to a concept, return a TYPE_DECL representing
17456 the result of using the constrained type specifier in the
17457 current context. DECL refers to a concept if
17459 - it is an overload set containing a function concept taking a single
17462 - it is a variable concept taking a single type argument. */
17465 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17468 && (TREE_CODE (decl
) == OVERLOAD
17469 || BASELINK_P (decl
)
17470 || variable_concept_p (decl
)))
17471 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17476 /* Check if DECL and ARGS form a partial-concept-id. If so,
17477 assign ID to the resulting constrained placeholder.
17479 Returns true if the partial-concept-id designates a placeholder
17480 and false otherwise. Note that *id is set to NULL_TREE in
17484 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17486 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17489 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17501 Returns a TYPE_DECL for the type. */
17504 cp_parser_nonclass_name (cp_parser
* parser
)
17509 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17510 identifier
= cp_parser_identifier (parser
);
17511 if (identifier
== error_mark_node
)
17512 return error_mark_node
;
17514 /* Look up the type-name. */
17515 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17517 type_decl
= strip_using_decl (type_decl
);
17519 /* If we found an overload set, then it may refer to a concept-name. */
17520 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17523 if (TREE_CODE (type_decl
) != TYPE_DECL
17524 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17526 /* See if this is an Objective-C type. */
17527 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17528 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17530 type_decl
= TYPE_NAME (type
);
17533 /* Issue an error if we did not find a type-name. */
17534 if (TREE_CODE (type_decl
) != TYPE_DECL
17535 /* In Objective-C, we have the complication that class names are
17536 normally type names and start declarations (eg, the
17537 "NSObject" in "NSObject *object;"), but can be used in an
17538 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17539 is an expression. So, a classname followed by a dot is not a
17540 valid type-name. */
17541 || (objc_is_class_name (TREE_TYPE (type_decl
))
17542 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17544 if (!cp_parser_simulate_error (parser
))
17545 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17546 NLE_TYPE
, token
->location
);
17547 return error_mark_node
;
17549 /* Remember that the name was used in the definition of the
17550 current class so that we can check later to see if the
17551 meaning would have been different after the class was
17552 entirely defined. */
17553 else if (type_decl
!= error_mark_node
17555 maybe_note_name_used_in_class (identifier
, type_decl
);
17560 /* Parse an elaborated-type-specifier. Note that the grammar given
17561 here incorporates the resolution to DR68.
17563 elaborated-type-specifier:
17564 class-key :: [opt] nested-name-specifier [opt] identifier
17565 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17566 enum-key :: [opt] nested-name-specifier [opt] identifier
17567 typename :: [opt] nested-name-specifier identifier
17568 typename :: [opt] nested-name-specifier template [opt]
17573 elaborated-type-specifier:
17574 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17575 class-key attributes :: [opt] nested-name-specifier [opt]
17576 template [opt] template-id
17577 enum attributes :: [opt] nested-name-specifier [opt] identifier
17579 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17580 declared `friend'. If IS_DECLARATION is TRUE, then this
17581 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17582 something is being declared.
17584 Returns the TYPE specified. */
17587 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17589 bool is_declaration
)
17591 enum tag_types tag_type
;
17593 tree type
= NULL_TREE
;
17594 tree attributes
= NULL_TREE
;
17596 cp_token
*token
= NULL
;
17598 /* See if we're looking at the `enum' keyword. */
17599 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17601 /* Consume the `enum' token. */
17602 cp_lexer_consume_token (parser
->lexer
);
17603 /* Remember that it's an enumeration type. */
17604 tag_type
= enum_type
;
17605 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17606 enums) is used here. */
17607 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17608 if (cp_parser_is_keyword (token
, RID_CLASS
)
17609 || cp_parser_is_keyword (token
, RID_STRUCT
))
17611 gcc_rich_location
richloc (token
->location
);
17612 richloc
.add_range (input_location
, false);
17613 richloc
.add_fixit_remove ();
17614 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17615 "a scoped enum must not use the %qD keyword",
17617 /* Consume the `struct' or `class' and parse it anyway. */
17618 cp_lexer_consume_token (parser
->lexer
);
17620 /* Parse the attributes. */
17621 attributes
= cp_parser_attributes_opt (parser
);
17623 /* Or, it might be `typename'. */
17624 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17627 /* Consume the `typename' token. */
17628 cp_lexer_consume_token (parser
->lexer
);
17629 /* Remember that it's a `typename' type. */
17630 tag_type
= typename_type
;
17632 /* Otherwise it must be a class-key. */
17635 tag_type
= cp_parser_class_key (parser
);
17636 if (tag_type
== none_type
)
17637 return error_mark_node
;
17638 /* Parse the attributes. */
17639 attributes
= cp_parser_attributes_opt (parser
);
17642 /* Look for the `::' operator. */
17643 globalscope
= cp_parser_global_scope_opt (parser
,
17644 /*current_scope_valid_p=*/false);
17645 /* Look for the nested-name-specifier. */
17646 tree nested_name_specifier
;
17647 if (tag_type
== typename_type
&& !globalscope
)
17649 nested_name_specifier
17650 = cp_parser_nested_name_specifier (parser
,
17651 /*typename_keyword_p=*/true,
17652 /*check_dependency_p=*/true,
17655 if (!nested_name_specifier
)
17656 return error_mark_node
;
17659 /* Even though `typename' is not present, the proposed resolution
17660 to Core Issue 180 says that in `class A<T>::B', `B' should be
17661 considered a type-name, even if `A<T>' is dependent. */
17662 nested_name_specifier
17663 = cp_parser_nested_name_specifier_opt (parser
,
17664 /*typename_keyword_p=*/true,
17665 /*check_dependency_p=*/true,
17668 /* For everything but enumeration types, consider a template-id.
17669 For an enumeration type, consider only a plain identifier. */
17670 if (tag_type
!= enum_type
)
17672 bool template_p
= false;
17675 /* Allow the `template' keyword. */
17676 template_p
= cp_parser_optional_template_keyword (parser
);
17677 /* If we didn't see `template', we don't know if there's a
17678 template-id or not. */
17680 cp_parser_parse_tentatively (parser
);
17681 /* Parse the template-id. */
17682 token
= cp_lexer_peek_token (parser
->lexer
);
17683 decl
= cp_parser_template_id (parser
, template_p
,
17684 /*check_dependency_p=*/true,
17687 /* If we didn't find a template-id, look for an ordinary
17689 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17691 /* We can get here when cp_parser_template_id, called by
17692 cp_parser_class_name with tag_type == none_type, succeeds
17693 and caches a BASELINK. Then, when called again here,
17694 instead of failing and returning an error_mark_node
17695 returns it (see template/typename17.C in C++11).
17696 ??? Could we diagnose this earlier? */
17697 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17699 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17700 type
= error_mark_node
;
17702 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17703 in effect, then we must assume that, upon instantiation, the
17704 template will correspond to a class. */
17705 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17706 && tag_type
== typename_type
)
17707 type
= make_typename_type (parser
->scope
, decl
,
17709 /*complain=*/tf_error
);
17710 /* If the `typename' keyword is in effect and DECL is not a type
17711 decl, then type is non existent. */
17712 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17714 else if (TREE_CODE (decl
) == TYPE_DECL
)
17716 type
= check_elaborated_type_specifier (tag_type
, decl
,
17717 /*allow_template_p=*/true);
17719 /* If the next token is a semicolon, this must be a specialization,
17720 instantiation, or friend declaration. Check the scope while we
17721 still know whether or not we had a nested-name-specifier. */
17722 if (type
!= error_mark_node
17723 && !nested_name_specifier
&& !is_friend
17724 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17725 check_unqualified_spec_or_inst (type
, token
->location
);
17727 else if (decl
== error_mark_node
)
17728 type
= error_mark_node
;
17733 token
= cp_lexer_peek_token (parser
->lexer
);
17734 identifier
= cp_parser_identifier (parser
);
17736 if (identifier
== error_mark_node
)
17738 parser
->scope
= NULL_TREE
;
17739 return error_mark_node
;
17742 /* For a `typename', we needn't call xref_tag. */
17743 if (tag_type
== typename_type
17744 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17745 return cp_parser_make_typename_type (parser
, identifier
,
17748 /* Template parameter lists apply only if we are not within a
17749 function parameter list. */
17750 bool template_parm_lists_apply
17751 = parser
->num_template_parameter_lists
;
17752 if (template_parm_lists_apply
)
17753 for (cp_binding_level
*s
= current_binding_level
;
17754 s
&& s
->kind
!= sk_template_parms
;
17755 s
= s
->level_chain
)
17756 if (s
->kind
== sk_function_parms
)
17757 template_parm_lists_apply
= false;
17759 /* Look up a qualified name in the usual way. */
17763 tree ambiguous_decls
;
17765 decl
= cp_parser_lookup_name (parser
, identifier
,
17767 /*is_template=*/false,
17768 /*is_namespace=*/false,
17769 /*check_dependency=*/true,
17773 /* If the lookup was ambiguous, an error will already have been
17775 if (ambiguous_decls
)
17776 return error_mark_node
;
17778 /* If we are parsing friend declaration, DECL may be a
17779 TEMPLATE_DECL tree node here. However, we need to check
17780 whether this TEMPLATE_DECL results in valid code. Consider
17781 the following example:
17784 template <class T> class C {};
17787 template <class T> friend class N::C; // #1, valid code
17789 template <class T> class Y {
17790 friend class N::C; // #2, invalid code
17793 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17794 name lookup of `N::C'. We see that friend declaration must
17795 be template for the code to be valid. Note that
17796 processing_template_decl does not work here since it is
17797 always 1 for the above two cases. */
17799 decl
= (cp_parser_maybe_treat_template_as_class
17800 (decl
, /*tag_name_p=*/is_friend
17801 && template_parm_lists_apply
));
17803 if (TREE_CODE (decl
) != TYPE_DECL
)
17805 cp_parser_diagnose_invalid_type_name (parser
,
17808 return error_mark_node
;
17811 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17813 bool allow_template
= (template_parm_lists_apply
17814 || DECL_SELF_REFERENCE_P (decl
));
17815 type
= check_elaborated_type_specifier (tag_type
, decl
,
17818 if (type
== error_mark_node
)
17819 return error_mark_node
;
17822 /* Forward declarations of nested types, such as
17827 are invalid unless all components preceding the final '::'
17828 are complete. If all enclosing types are complete, these
17829 declarations become merely pointless.
17831 Invalid forward declarations of nested types are errors
17832 caught elsewhere in parsing. Those that are pointless arrive
17835 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17836 && !is_friend
&& !processing_explicit_instantiation
)
17837 warning (0, "declaration %qD does not declare anything", decl
);
17839 type
= TREE_TYPE (decl
);
17843 /* An elaborated-type-specifier sometimes introduces a new type and
17844 sometimes names an existing type. Normally, the rule is that it
17845 introduces a new type only if there is not an existing type of
17846 the same name already in scope. For example, given:
17849 void f() { struct S s; }
17851 the `struct S' in the body of `f' is the same `struct S' as in
17852 the global scope; the existing definition is used. However, if
17853 there were no global declaration, this would introduce a new
17854 local class named `S'.
17856 An exception to this rule applies to the following code:
17858 namespace N { struct S; }
17860 Here, the elaborated-type-specifier names a new type
17861 unconditionally; even if there is already an `S' in the
17862 containing scope this declaration names a new type.
17863 This exception only applies if the elaborated-type-specifier
17864 forms the complete declaration:
17868 A declaration consisting solely of `class-key identifier ;' is
17869 either a redeclaration of the name in the current scope or a
17870 forward declaration of the identifier as a class name. It
17871 introduces the name into the current scope.
17873 We are in this situation precisely when the next token is a `;'.
17875 An exception to the exception is that a `friend' declaration does
17876 *not* name a new type; i.e., given:
17878 struct S { friend struct T; };
17880 `T' is not a new type in the scope of `S'.
17882 Also, `new struct S' or `sizeof (struct S)' never results in the
17883 definition of a new type; a new type can only be declared in a
17884 declaration context. */
17890 /* Friends have special name lookup rules. */
17891 ts
= ts_within_enclosing_non_class
;
17892 else if (is_declaration
17893 && cp_lexer_next_token_is (parser
->lexer
,
17895 /* This is a `class-key identifier ;' */
17901 (template_parm_lists_apply
17902 && (cp_parser_next_token_starts_class_definition_p (parser
)
17903 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17904 /* An unqualified name was used to reference this type, so
17905 there were no qualifying templates. */
17906 if (template_parm_lists_apply
17907 && !cp_parser_check_template_parameters (parser
,
17908 /*num_templates=*/0,
17909 /*template_id*/false,
17911 /*declarator=*/NULL
))
17912 return error_mark_node
;
17913 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17917 if (type
== error_mark_node
)
17918 return error_mark_node
;
17920 /* Allow attributes on forward declarations of classes. */
17923 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17924 warning (OPT_Wattributes
,
17925 "attributes ignored on uninstantiated type");
17926 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17927 && ! processing_explicit_instantiation
)
17928 warning (OPT_Wattributes
,
17929 "attributes ignored on template instantiation");
17930 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17931 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17933 warning (OPT_Wattributes
,
17934 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17937 if (tag_type
!= enum_type
)
17939 /* Indicate whether this class was declared as a `class' or as a
17941 if (CLASS_TYPE_P (type
))
17942 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17943 cp_parser_check_class_key (tag_type
, type
);
17946 /* A "<" cannot follow an elaborated type specifier. If that
17947 happens, the user was probably trying to form a template-id. */
17948 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17954 /* Parse an enum-specifier.
17957 enum-head { enumerator-list [opt] }
17958 enum-head { enumerator-list , } [C++0x]
17961 enum-key identifier [opt] enum-base [opt]
17962 enum-key nested-name-specifier identifier enum-base [opt]
17967 enum struct [C++0x]
17970 : type-specifier-seq
17972 opaque-enum-specifier:
17973 enum-key identifier enum-base [opt] ;
17976 enum-key attributes[opt] identifier [opt] enum-base [opt]
17977 { enumerator-list [opt] }attributes[opt]
17978 enum-key attributes[opt] identifier [opt] enum-base [opt]
17979 { enumerator-list, }attributes[opt] [C++0x]
17981 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17982 if the token stream isn't an enum-specifier after all. */
17985 cp_parser_enum_specifier (cp_parser
* parser
)
17988 tree type
= NULL_TREE
;
17990 tree nested_name_specifier
= NULL_TREE
;
17992 bool scoped_enum_p
= false;
17993 bool has_underlying_type
= false;
17994 bool nested_being_defined
= false;
17995 bool new_value_list
= false;
17996 bool is_new_type
= false;
17997 bool is_unnamed
= false;
17998 tree underlying_type
= NULL_TREE
;
17999 cp_token
*type_start_token
= NULL
;
18000 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18002 parser
->colon_corrects_to_scope_p
= false;
18004 /* Parse tentatively so that we can back up if we don't find a
18006 cp_parser_parse_tentatively (parser
);
18008 /* Caller guarantees that the current token is 'enum', an identifier
18009 possibly follows, and the token after that is an opening brace.
18010 If we don't have an identifier, fabricate an anonymous name for
18011 the enumeration being defined. */
18012 cp_lexer_consume_token (parser
->lexer
);
18014 /* Parse the "class" or "struct", which indicates a scoped
18015 enumeration type in C++0x. */
18016 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18017 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18019 if (cxx_dialect
< cxx11
)
18020 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18022 /* Consume the `struct' or `class' token. */
18023 cp_lexer_consume_token (parser
->lexer
);
18025 scoped_enum_p
= true;
18028 attributes
= cp_parser_attributes_opt (parser
);
18030 /* Clear the qualification. */
18031 parser
->scope
= NULL_TREE
;
18032 parser
->qualifying_scope
= NULL_TREE
;
18033 parser
->object_scope
= NULL_TREE
;
18035 /* Figure out in what scope the declaration is being placed. */
18036 prev_scope
= current_scope ();
18038 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18040 push_deferring_access_checks (dk_no_check
);
18041 nested_name_specifier
18042 = cp_parser_nested_name_specifier_opt (parser
,
18043 /*typename_keyword_p=*/true,
18044 /*check_dependency_p=*/false,
18046 /*is_declaration=*/false);
18048 if (nested_name_specifier
)
18052 identifier
= cp_parser_identifier (parser
);
18053 name
= cp_parser_lookup_name (parser
, identifier
,
18055 /*is_template=*/false,
18056 /*is_namespace=*/false,
18057 /*check_dependency=*/true,
18058 /*ambiguous_decls=*/NULL
,
18060 if (name
&& name
!= error_mark_node
)
18062 type
= TREE_TYPE (name
);
18063 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18065 /* Are template enums allowed in ISO? */
18066 if (template_parm_scope_p ())
18067 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18068 "%qD is an enumeration template", name
);
18069 /* ignore a typename reference, for it will be solved by name
18074 else if (nested_name_specifier
== error_mark_node
)
18075 /* We already issued an error. */;
18078 error_at (type_start_token
->location
,
18079 "%qD does not name an enumeration in %qT",
18080 identifier
, nested_name_specifier
);
18081 nested_name_specifier
= error_mark_node
;
18086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18087 identifier
= cp_parser_identifier (parser
);
18090 identifier
= make_anon_name ();
18093 error_at (type_start_token
->location
,
18094 "unnamed scoped enum is not allowed");
18097 pop_deferring_access_checks ();
18099 /* Check for the `:' that denotes a specified underlying type in C++0x.
18100 Note that a ':' could also indicate a bitfield width, however. */
18101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18103 cp_decl_specifier_seq type_specifiers
;
18105 /* Consume the `:'. */
18106 cp_lexer_consume_token (parser
->lexer
);
18108 /* Parse the type-specifier-seq. */
18109 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18110 /*is_trailing_return=*/false,
18113 /* At this point this is surely not elaborated type specifier. */
18114 if (!cp_parser_parse_definitely (parser
))
18117 if (cxx_dialect
< cxx11
)
18118 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18120 has_underlying_type
= true;
18122 /* If that didn't work, stop. */
18123 if (type_specifiers
.type
!= error_mark_node
)
18125 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18126 /*initialized=*/0, NULL
);
18127 if (underlying_type
== error_mark_node
18128 || check_for_bare_parameter_packs (underlying_type
))
18129 underlying_type
= NULL_TREE
;
18133 /* Look for the `{' but don't consume it yet. */
18134 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18136 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18138 cp_parser_error (parser
, "expected %<{%>");
18139 if (has_underlying_type
)
18145 /* An opaque-enum-specifier must have a ';' here. */
18146 if ((scoped_enum_p
|| underlying_type
)
18147 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18149 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18150 if (has_underlying_type
)
18158 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18161 if (nested_name_specifier
)
18163 if (CLASS_TYPE_P (nested_name_specifier
))
18165 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18166 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18167 push_scope (nested_name_specifier
);
18169 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18171 push_nested_namespace (nested_name_specifier
);
18175 /* Issue an error message if type-definitions are forbidden here. */
18176 if (!cp_parser_check_type_definition (parser
))
18177 type
= error_mark_node
;
18179 /* Create the new type. We do this before consuming the opening
18180 brace so the enum will be recorded as being on the line of its
18181 tag (or the 'enum' keyword, if there is no tag). */
18182 type
= start_enum (identifier
, type
, underlying_type
,
18183 attributes
, scoped_enum_p
, &is_new_type
);
18185 /* If the next token is not '{' it is an opaque-enum-specifier or an
18186 elaborated-type-specifier. */
18187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18189 timevar_push (TV_PARSE_ENUM
);
18190 if (nested_name_specifier
18191 && nested_name_specifier
!= error_mark_node
)
18193 /* The following catches invalid code such as:
18194 enum class S<int>::E { A, B, C }; */
18195 if (!processing_specialization
18196 && CLASS_TYPE_P (nested_name_specifier
)
18197 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18198 error_at (type_start_token
->location
, "cannot add an enumerator "
18199 "list to a template instantiation");
18201 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18203 error_at (type_start_token
->location
,
18204 "%<%T::%E%> has not been declared",
18205 TYPE_CONTEXT (nested_name_specifier
),
18206 nested_name_specifier
);
18207 type
= error_mark_node
;
18209 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18210 && !CLASS_TYPE_P (nested_name_specifier
))
18212 error_at (type_start_token
->location
, "nested name specifier "
18213 "%qT for enum declaration does not name a class "
18214 "or namespace", nested_name_specifier
);
18215 type
= error_mark_node
;
18217 /* If that scope does not contain the scope in which the
18218 class was originally declared, the program is invalid. */
18219 else if (prev_scope
&& !is_ancestor (prev_scope
,
18220 nested_name_specifier
))
18222 if (at_namespace_scope_p ())
18223 error_at (type_start_token
->location
,
18224 "declaration of %qD in namespace %qD which does not "
18226 type
, prev_scope
, nested_name_specifier
);
18228 error_at (type_start_token
->location
,
18229 "declaration of %qD in %qD which does not "
18231 type
, prev_scope
, nested_name_specifier
);
18232 type
= error_mark_node
;
18234 /* If that scope is the scope where the declaration is being placed
18235 the program is invalid. */
18236 else if (CLASS_TYPE_P (nested_name_specifier
)
18237 && CLASS_TYPE_P (prev_scope
)
18238 && same_type_p (nested_name_specifier
, prev_scope
))
18240 permerror (type_start_token
->location
,
18241 "extra qualification not allowed");
18242 nested_name_specifier
= NULL_TREE
;
18247 begin_scope (sk_scoped_enum
, type
);
18249 /* Consume the opening brace. */
18250 matching_braces braces
;
18251 braces
.consume_open (parser
);
18253 if (type
== error_mark_node
)
18254 ; /* Nothing to add */
18255 else if (OPAQUE_ENUM_P (type
)
18256 || (cxx_dialect
> cxx98
&& processing_specialization
))
18258 new_value_list
= true;
18259 SET_OPAQUE_ENUM_P (type
, false);
18260 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18264 error_at (type_start_token
->location
,
18265 "multiple definition of %q#T", type
);
18266 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18267 "previous definition here");
18268 type
= error_mark_node
;
18271 if (type
== error_mark_node
)
18272 cp_parser_skip_to_end_of_block_or_statement (parser
);
18273 /* If the next token is not '}', then there are some enumerators. */
18274 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18276 if (is_unnamed
&& !scoped_enum_p
)
18277 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18278 "ISO C++ forbids empty unnamed enum");
18281 cp_parser_enumerator_list (parser
, type
);
18283 /* Consume the final '}'. */
18284 braces
.require_close (parser
);
18288 timevar_pop (TV_PARSE_ENUM
);
18292 /* If a ';' follows, then it is an opaque-enum-specifier
18293 and additional restrictions apply. */
18294 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18297 error_at (type_start_token
->location
,
18298 "opaque-enum-specifier without name");
18299 else if (nested_name_specifier
)
18300 error_at (type_start_token
->location
,
18301 "opaque-enum-specifier must use a simple identifier");
18305 /* Look for trailing attributes to apply to this enumeration, and
18306 apply them if appropriate. */
18307 if (cp_parser_allow_gnu_extensions_p (parser
))
18309 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18310 cplus_decl_attributes (&type
,
18312 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18315 /* Finish up the enumeration. */
18316 if (type
!= error_mark_node
)
18318 if (new_value_list
)
18319 finish_enum_value_list (type
);
18321 finish_enum (type
);
18324 if (nested_name_specifier
)
18326 if (CLASS_TYPE_P (nested_name_specifier
))
18328 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18329 pop_scope (nested_name_specifier
);
18331 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18333 pop_nested_namespace (nested_name_specifier
);
18337 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18341 /* Parse an enumerator-list. The enumerators all have the indicated
18345 enumerator-definition
18346 enumerator-list , enumerator-definition */
18349 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18353 /* Parse an enumerator-definition. */
18354 cp_parser_enumerator_definition (parser
, type
);
18356 /* If the next token is not a ',', we've reached the end of
18358 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18360 /* Otherwise, consume the `,' and keep going. */
18361 cp_lexer_consume_token (parser
->lexer
);
18362 /* If the next token is a `}', there is a trailing comma. */
18363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18365 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18366 pedwarn (input_location
, OPT_Wpedantic
,
18367 "comma at end of enumerator list");
18373 /* Parse an enumerator-definition. The enumerator has the indicated
18376 enumerator-definition:
18378 enumerator = constant-expression
18385 enumerator-definition:
18386 enumerator attributes [opt]
18387 enumerator attributes [opt] = constant-expression */
18390 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18396 /* Save the input location because we are interested in the location
18397 of the identifier and not the location of the explicit value. */
18398 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18400 /* Look for the identifier. */
18401 identifier
= cp_parser_identifier (parser
);
18402 if (identifier
== error_mark_node
)
18405 /* Parse any specified attributes. */
18406 tree attrs
= cp_parser_attributes_opt (parser
);
18408 /* If the next token is an '=', then there is an explicit value. */
18409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18411 /* Consume the `=' token. */
18412 cp_lexer_consume_token (parser
->lexer
);
18413 /* Parse the value. */
18414 value
= cp_parser_constant_expression (parser
);
18419 /* If we are processing a template, make sure the initializer of the
18420 enumerator doesn't contain any bare template parameter pack. */
18421 if (check_for_bare_parameter_packs (value
))
18422 value
= error_mark_node
;
18424 /* Create the enumerator. */
18425 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18428 /* Parse a namespace-name.
18431 original-namespace-name
18434 Returns the NAMESPACE_DECL for the namespace. */
18437 cp_parser_namespace_name (cp_parser
* parser
)
18440 tree namespace_decl
;
18442 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18444 /* Get the name of the namespace. */
18445 identifier
= cp_parser_identifier (parser
);
18446 if (identifier
== error_mark_node
)
18447 return error_mark_node
;
18449 /* Look up the identifier in the currently active scope. Look only
18450 for namespaces, due to:
18452 [basic.lookup.udir]
18454 When looking up a namespace-name in a using-directive or alias
18455 definition, only namespace names are considered.
18459 [basic.lookup.qual]
18461 During the lookup of a name preceding the :: scope resolution
18462 operator, object, function, and enumerator names are ignored.
18464 (Note that cp_parser_qualifying_entity only calls this
18465 function if the token after the name is the scope resolution
18467 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18469 /*is_template=*/false,
18470 /*is_namespace=*/true,
18471 /*check_dependency=*/true,
18472 /*ambiguous_decls=*/NULL
,
18474 /* If it's not a namespace, issue an error. */
18475 if (namespace_decl
== error_mark_node
18476 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18478 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18480 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18481 if (namespace_decl
== error_mark_node
18482 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18483 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18486 cp_parser_error (parser
, "expected namespace-name");
18487 namespace_decl
= error_mark_node
;
18490 return namespace_decl
;
18493 /* Parse a namespace-definition.
18495 namespace-definition:
18496 named-namespace-definition
18497 unnamed-namespace-definition
18499 named-namespace-definition:
18500 original-namespace-definition
18501 extension-namespace-definition
18503 original-namespace-definition:
18504 namespace identifier { namespace-body }
18506 extension-namespace-definition:
18507 namespace original-namespace-name { namespace-body }
18509 unnamed-namespace-definition:
18510 namespace { namespace-body } */
18513 cp_parser_namespace_definition (cp_parser
* parser
)
18516 int nested_definition_count
= 0;
18518 cp_ensure_no_omp_declare_simd (parser
);
18519 cp_ensure_no_oacc_routine (parser
);
18521 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18525 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18526 cp_lexer_consume_token (parser
->lexer
);
18529 /* Look for the `namespace' keyword. */
18531 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18533 /* Parse any specified attributes before the identifier. */
18534 tree attribs
= cp_parser_attributes_opt (parser
);
18538 identifier
= NULL_TREE
;
18540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18542 identifier
= cp_parser_identifier (parser
);
18544 /* Parse any attributes specified after the identifier. */
18545 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18548 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18551 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18552 pedwarn (input_location
, OPT_Wpedantic
,
18553 "nested namespace definitions only available with "
18554 "-std=c++17 or -std=gnu++17");
18556 /* Nested namespace names can create new namespaces (unlike
18557 other qualified-ids). */
18558 if (int count
= identifier
? push_namespace (identifier
) : 0)
18559 nested_definition_count
+= count
;
18561 cp_parser_error (parser
, "nested namespace name required");
18562 cp_lexer_consume_token (parser
->lexer
);
18565 if (nested_definition_count
&& !identifier
)
18566 cp_parser_error (parser
, "namespace name required");
18568 if (nested_definition_count
&& attribs
)
18569 error_at (token
->location
,
18570 "a nested namespace definition cannot have attributes");
18571 if (nested_definition_count
&& is_inline
)
18572 error_at (token
->location
,
18573 "a nested namespace definition cannot be inline");
18575 /* Start the namespace. */
18576 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18578 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18580 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18582 /* Look for the `{' to validate starting the namespace. */
18583 matching_braces braces
;
18584 if (braces
.require_open (parser
))
18586 /* Parse the body of the namespace. */
18587 cp_parser_namespace_body (parser
);
18589 /* Look for the final `}'. */
18590 braces
.require_close (parser
);
18593 if (has_visibility
)
18594 pop_visibility (1);
18596 /* Pop the nested namespace definitions. */
18597 while (nested_definition_count
--)
18601 /* Parse a namespace-body.
18604 declaration-seq [opt] */
18607 cp_parser_namespace_body (cp_parser
* parser
)
18609 cp_parser_declaration_seq_opt (parser
);
18612 /* Parse a namespace-alias-definition.
18614 namespace-alias-definition:
18615 namespace identifier = qualified-namespace-specifier ; */
18618 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18621 tree namespace_specifier
;
18623 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18625 /* Look for the `namespace' keyword. */
18626 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18627 /* Look for the identifier. */
18628 identifier
= cp_parser_identifier (parser
);
18629 if (identifier
== error_mark_node
)
18631 /* Look for the `=' token. */
18632 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18633 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18635 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18636 /* Skip the definition. */
18637 cp_lexer_consume_token (parser
->lexer
);
18638 if (cp_parser_skip_to_closing_brace (parser
))
18639 cp_lexer_consume_token (parser
->lexer
);
18642 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18643 /* Look for the qualified-namespace-specifier. */
18644 namespace_specifier
18645 = cp_parser_qualified_namespace_specifier (parser
);
18646 /* Look for the `;' token. */
18647 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18649 /* Register the alias in the symbol table. */
18650 do_namespace_alias (identifier
, namespace_specifier
);
18653 /* Parse a qualified-namespace-specifier.
18655 qualified-namespace-specifier:
18656 :: [opt] nested-name-specifier [opt] namespace-name
18658 Returns a NAMESPACE_DECL corresponding to the specified
18662 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18664 /* Look for the optional `::'. */
18665 cp_parser_global_scope_opt (parser
,
18666 /*current_scope_valid_p=*/false);
18668 /* Look for the optional nested-name-specifier. */
18669 cp_parser_nested_name_specifier_opt (parser
,
18670 /*typename_keyword_p=*/false,
18671 /*check_dependency_p=*/true,
18673 /*is_declaration=*/true);
18675 return cp_parser_namespace_name (parser
);
18678 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18679 access declaration.
18682 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18683 using :: unqualified-id ;
18685 access-declaration:
18691 cp_parser_using_declaration (cp_parser
* parser
,
18692 bool access_declaration_p
)
18695 bool typename_p
= false;
18696 bool global_scope_p
;
18700 int oldcount
= errorcount
;
18701 cp_token
*diag_token
= NULL
;
18703 if (access_declaration_p
)
18705 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18706 cp_parser_parse_tentatively (parser
);
18710 /* Look for the `using' keyword. */
18711 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18714 /* Peek at the next token. */
18715 token
= cp_lexer_peek_token (parser
->lexer
);
18716 /* See if it's `typename'. */
18717 if (token
->keyword
== RID_TYPENAME
)
18719 /* Remember that we've seen it. */
18721 /* Consume the `typename' token. */
18722 cp_lexer_consume_token (parser
->lexer
);
18726 /* Look for the optional global scope qualification. */
18728 = (cp_parser_global_scope_opt (parser
,
18729 /*current_scope_valid_p=*/false)
18732 /* If we saw `typename', or didn't see `::', then there must be a
18733 nested-name-specifier present. */
18734 if (typename_p
|| !global_scope_p
)
18736 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18737 /*check_dependency_p=*/true,
18739 /*is_declaration=*/true);
18740 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18742 cp_parser_skip_to_end_of_block_or_statement (parser
);
18746 /* Otherwise, we could be in either of the two productions. In that
18747 case, treat the nested-name-specifier as optional. */
18749 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18750 /*typename_keyword_p=*/false,
18751 /*check_dependency_p=*/true,
18753 /*is_declaration=*/true);
18755 qscope
= global_namespace
;
18756 else if (UNSCOPED_ENUM_P (qscope
))
18757 qscope
= CP_TYPE_CONTEXT (qscope
);
18759 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18760 /* Something has already gone wrong; there's no need to parse
18761 further. Since an error has occurred, the return value of
18762 cp_parser_parse_definitely will be false, as required. */
18763 return cp_parser_parse_definitely (parser
);
18765 token
= cp_lexer_peek_token (parser
->lexer
);
18766 /* Parse the unqualified-id. */
18767 identifier
= cp_parser_unqualified_id (parser
,
18768 /*template_keyword_p=*/false,
18769 /*check_dependency_p=*/true,
18770 /*declarator_p=*/true,
18771 /*optional_p=*/false);
18773 if (access_declaration_p
)
18775 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18776 cp_parser_simulate_error (parser
);
18777 if (!cp_parser_parse_definitely (parser
))
18780 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18782 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18783 if (cxx_dialect
< cxx17
18784 && !in_system_header_at (ell
->location
))
18785 pedwarn (ell
->location
, 0,
18786 "pack expansion in using-declaration only available "
18787 "with -std=c++17 or -std=gnu++17");
18788 qscope
= make_pack_expansion (qscope
);
18791 /* The function we call to handle a using-declaration is different
18792 depending on what scope we are in. */
18793 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18795 else if (!identifier_p (identifier
)
18796 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18797 /* [namespace.udecl]
18799 A using declaration shall not name a template-id. */
18800 error_at (token
->location
,
18801 "a template-id may not appear in a using-declaration");
18804 if (at_class_scope_p ())
18806 /* Create the USING_DECL. */
18807 decl
= do_class_using_decl (qscope
, identifier
);
18809 if (decl
&& typename_p
)
18810 USING_DECL_TYPENAME_P (decl
) = 1;
18812 if (check_for_bare_parameter_packs (decl
))
18814 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18818 /* Add it to the list of members in this class. */
18819 finish_member_declaration (decl
);
18823 decl
= cp_parser_lookup_name_simple (parser
,
18826 if (decl
== error_mark_node
)
18827 cp_parser_name_lookup_error (parser
, identifier
,
18830 else if (check_for_bare_parameter_packs (decl
))
18832 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18835 else if (!at_namespace_scope_p ())
18836 finish_local_using_decl (decl
, qscope
, identifier
);
18838 finish_namespace_using_decl (decl
, qscope
, identifier
);
18842 if (!access_declaration_p
18843 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18845 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18846 if (cxx_dialect
< cxx17
)
18847 pedwarn (comma
->location
, 0,
18848 "comma-separated list in using-declaration only available "
18849 "with -std=c++17 or -std=gnu++17");
18853 /* Look for the final `;'. */
18854 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18856 if (access_declaration_p
&& errorcount
== oldcount
)
18857 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18858 "access declarations are deprecated "
18859 "in favour of using-declarations; "
18860 "suggestion: add the %<using%> keyword");
18865 /* Parse an alias-declaration.
18868 using identifier attribute-specifier-seq [opt] = type-id */
18871 cp_parser_alias_declaration (cp_parser
* parser
)
18873 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18874 location_t id_location
;
18875 cp_declarator
*declarator
;
18876 cp_decl_specifier_seq decl_specs
;
18878 const char *saved_message
= NULL
;
18880 /* Look for the `using' keyword. */
18881 cp_token
*using_token
18882 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18883 if (using_token
== NULL
)
18884 return error_mark_node
;
18886 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18887 id
= cp_parser_identifier (parser
);
18888 if (id
== error_mark_node
)
18889 return error_mark_node
;
18891 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18892 attributes
= cp_parser_attributes_opt (parser
);
18893 if (attributes
== error_mark_node
)
18894 return error_mark_node
;
18896 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18898 if (cp_parser_error_occurred (parser
))
18899 return error_mark_node
;
18901 cp_parser_commit_to_tentative_parse (parser
);
18903 /* Now we are going to parse the type-id of the declaration. */
18908 "A type-specifier-seq shall not define a class or enumeration
18909 unless it appears in the type-id of an alias-declaration (7.1.3) that
18910 is not the declaration of a template-declaration."
18912 In other words, if we currently are in an alias template, the
18913 type-id should not define a type.
18915 So let's set parser->type_definition_forbidden_message in that
18916 case; cp_parser_check_type_definition (called by
18917 cp_parser_class_specifier) will then emit an error if a type is
18918 defined in the type-id. */
18919 if (parser
->num_template_parameter_lists
)
18921 saved_message
= parser
->type_definition_forbidden_message
;
18922 parser
->type_definition_forbidden_message
=
18923 G_("types may not be defined in alias template declarations");
18926 type
= cp_parser_type_id (parser
);
18928 /* Restore the error message if need be. */
18929 if (parser
->num_template_parameter_lists
)
18930 parser
->type_definition_forbidden_message
= saved_message
;
18932 if (type
== error_mark_node
18933 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18935 cp_parser_skip_to_end_of_block_or_statement (parser
);
18936 return error_mark_node
;
18939 /* A typedef-name can also be introduced by an alias-declaration. The
18940 identifier following the using keyword becomes a typedef-name. It has
18941 the same semantics as if it were introduced by the typedef
18942 specifier. In particular, it does not define a new type and it shall
18943 not appear in the type-id. */
18945 clear_decl_specs (&decl_specs
);
18946 decl_specs
.type
= type
;
18947 if (attributes
!= NULL_TREE
)
18949 decl_specs
.attributes
= attributes
;
18950 set_and_check_decl_spec_loc (&decl_specs
,
18954 set_and_check_decl_spec_loc (&decl_specs
,
18957 set_and_check_decl_spec_loc (&decl_specs
,
18961 if (parser
->num_template_parameter_lists
18962 && !cp_parser_check_template_parameters (parser
,
18963 /*num_templates=*/0,
18964 /*template_id*/false,
18966 /*declarator=*/NULL
))
18967 return error_mark_node
;
18969 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18970 declarator
->id_loc
= id_location
;
18972 member_p
= at_class_scope_p ();
18974 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18975 NULL_TREE
, attributes
);
18977 decl
= start_decl (declarator
, &decl_specs
, 0,
18978 attributes
, NULL_TREE
, &pushed_scope
);
18979 if (decl
== error_mark_node
)
18982 // Attach constraints to the alias declaration.
18983 if (flag_concepts
&& current_template_parms
)
18985 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18986 tree constr
= build_constraints (reqs
, NULL_TREE
);
18987 set_constraints (decl
, constr
);
18990 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18993 pop_scope (pushed_scope
);
18995 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18996 added into the symbol table; otherwise, return the TYPE_DECL. */
18997 if (DECL_LANG_SPECIFIC (decl
)
18998 && DECL_TEMPLATE_INFO (decl
)
18999 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19001 decl
= DECL_TI_TEMPLATE (decl
);
19003 check_member_template (decl
);
19009 /* Parse a using-directive.
19012 using namespace :: [opt] nested-name-specifier [opt]
19013 namespace-name ; */
19016 cp_parser_using_directive (cp_parser
* parser
)
19018 tree namespace_decl
;
19021 /* Look for the `using' keyword. */
19022 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19023 /* And the `namespace' keyword. */
19024 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19025 /* Look for the optional `::' operator. */
19026 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19027 /* And the optional nested-name-specifier. */
19028 cp_parser_nested_name_specifier_opt (parser
,
19029 /*typename_keyword_p=*/false,
19030 /*check_dependency_p=*/true,
19032 /*is_declaration=*/true);
19033 /* Get the namespace being used. */
19034 namespace_decl
= cp_parser_namespace_name (parser
);
19035 /* And any specified attributes. */
19036 attribs
= cp_parser_attributes_opt (parser
);
19038 /* Update the symbol table. */
19039 if (namespace_bindings_p ())
19040 finish_namespace_using_directive (namespace_decl
, attribs
);
19042 finish_local_using_directive (namespace_decl
, attribs
);
19044 /* Look for the final `;'. */
19045 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19048 /* Parse an asm-definition.
19051 asm ( string-literal ) ;
19056 asm volatile [opt] ( string-literal ) ;
19057 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19058 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19059 : asm-operand-list [opt] ) ;
19060 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19061 : asm-operand-list [opt]
19062 : asm-clobber-list [opt] ) ;
19063 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19064 : asm-clobber-list [opt]
19065 : asm-goto-list ) ; */
19068 cp_parser_asm_definition (cp_parser
* parser
)
19071 tree outputs
= NULL_TREE
;
19072 tree inputs
= NULL_TREE
;
19073 tree clobbers
= NULL_TREE
;
19074 tree labels
= NULL_TREE
;
19076 bool volatile_p
= false;
19077 bool extended_p
= false;
19078 bool invalid_inputs_p
= false;
19079 bool invalid_outputs_p
= false;
19080 bool goto_p
= false;
19081 required_token missing
= RT_NONE
;
19083 /* Look for the `asm' keyword. */
19084 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19086 if (parser
->in_function_body
19087 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19089 error ("%<asm%> in %<constexpr%> function");
19090 cp_function_chain
->invalid_constexpr
= true;
19093 /* See if the next token is `volatile'. */
19094 if (cp_parser_allow_gnu_extensions_p (parser
)
19095 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19097 /* Remember that we saw the `volatile' keyword. */
19099 /* Consume the token. */
19100 cp_lexer_consume_token (parser
->lexer
);
19102 if (cp_parser_allow_gnu_extensions_p (parser
)
19103 && parser
->in_function_body
19104 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19106 /* Remember that we saw the `goto' keyword. */
19108 /* Consume the token. */
19109 cp_lexer_consume_token (parser
->lexer
);
19111 /* Look for the opening `('. */
19112 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19114 /* Look for the string. */
19115 string
= cp_parser_string_literal (parser
, false, false);
19116 if (string
== error_mark_node
)
19118 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19119 /*consume_paren=*/true);
19123 /* If we're allowing GNU extensions, check for the extended assembly
19124 syntax. Unfortunately, the `:' tokens need not be separated by
19125 a space in C, and so, for compatibility, we tolerate that here
19126 too. Doing that means that we have to treat the `::' operator as
19128 if (cp_parser_allow_gnu_extensions_p (parser
)
19129 && parser
->in_function_body
19130 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19131 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19133 bool inputs_p
= false;
19134 bool clobbers_p
= false;
19135 bool labels_p
= false;
19137 /* The extended syntax was used. */
19140 /* Look for outputs. */
19141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19143 /* Consume the `:'. */
19144 cp_lexer_consume_token (parser
->lexer
);
19145 /* Parse the output-operands. */
19146 if (cp_lexer_next_token_is_not (parser
->lexer
,
19148 && cp_lexer_next_token_is_not (parser
->lexer
,
19150 && cp_lexer_next_token_is_not (parser
->lexer
,
19154 outputs
= cp_parser_asm_operand_list (parser
);
19155 if (outputs
== error_mark_node
)
19156 invalid_outputs_p
= true;
19159 /* If the next token is `::', there are no outputs, and the
19160 next token is the beginning of the inputs. */
19161 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19162 /* The inputs are coming next. */
19165 /* Look for inputs. */
19167 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19169 /* Consume the `:' or `::'. */
19170 cp_lexer_consume_token (parser
->lexer
);
19171 /* Parse the output-operands. */
19172 if (cp_lexer_next_token_is_not (parser
->lexer
,
19174 && cp_lexer_next_token_is_not (parser
->lexer
,
19176 && cp_lexer_next_token_is_not (parser
->lexer
,
19179 inputs
= cp_parser_asm_operand_list (parser
);
19180 if (inputs
== error_mark_node
)
19181 invalid_inputs_p
= true;
19184 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19185 /* The clobbers are coming next. */
19188 /* Look for clobbers. */
19190 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19193 /* Consume the `:' or `::'. */
19194 cp_lexer_consume_token (parser
->lexer
);
19195 /* Parse the clobbers. */
19196 if (cp_lexer_next_token_is_not (parser
->lexer
,
19198 && cp_lexer_next_token_is_not (parser
->lexer
,
19200 clobbers
= cp_parser_asm_clobber_list (parser
);
19203 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19204 /* The labels are coming next. */
19207 /* Look for labels. */
19209 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19212 /* Consume the `:' or `::'. */
19213 cp_lexer_consume_token (parser
->lexer
);
19214 /* Parse the labels. */
19215 labels
= cp_parser_asm_label_list (parser
);
19218 if (goto_p
&& !labels_p
)
19219 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19222 missing
= RT_COLON_SCOPE
;
19224 /* Look for the closing `)'. */
19225 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19226 missing
? missing
: RT_CLOSE_PAREN
))
19227 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19228 /*consume_paren=*/true);
19229 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19231 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19233 /* Create the ASM_EXPR. */
19234 if (parser
->in_function_body
)
19236 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19237 inputs
, clobbers
, labels
);
19238 /* If the extended syntax was not used, mark the ASM_EXPR. */
19241 tree temp
= asm_stmt
;
19242 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19243 temp
= TREE_OPERAND (temp
, 0);
19245 ASM_INPUT_P (temp
) = 1;
19249 symtab
->finalize_toplevel_asm (string
);
19253 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19254 type that comes from the decl-specifier-seq. */
19257 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19259 for (cp_declarator
*d
= declarator
; d
;)
19269 if (TYPE_PTRMEMFUNC_P (type
))
19270 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19271 type
= TREE_TYPE (type
);
19279 /* Declarators [gram.dcl.decl] */
19281 /* Parse an init-declarator.
19284 declarator initializer [opt]
19289 declarator asm-specification [opt] attributes [opt] initializer [opt]
19291 function-definition:
19292 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19294 decl-specifier-seq [opt] declarator function-try-block
19298 function-definition:
19299 __extension__ function-definition
19303 function-definition:
19304 decl-specifier-seq [opt] declarator function-transaction-block
19306 The DECL_SPECIFIERS apply to this declarator. Returns a
19307 representation of the entity declared. If MEMBER_P is TRUE, then
19308 this declarator appears in a class scope. The new DECL created by
19309 this declarator is returned.
19311 The CHECKS are access checks that should be performed once we know
19312 what entity is being declared (and, therefore, what classes have
19315 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19316 for a function-definition here as well. If the declarator is a
19317 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19318 be TRUE upon return. By that point, the function-definition will
19319 have been completely parsed.
19321 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19324 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19325 parsed declaration if it is an uninitialized single declarator not followed
19326 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19327 if present, will not be consumed. If returned, this declarator will be
19328 created with SD_INITIALIZED but will not call cp_finish_decl.
19330 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19331 and there is an initializer, the pointed location_t is set to the
19332 location of the '=' or `(', or '{' in C++11 token introducing the
19336 cp_parser_init_declarator (cp_parser
* parser
,
19337 cp_decl_specifier_seq
*decl_specifiers
,
19338 vec
<deferred_access_check
, va_gc
> *checks
,
19339 bool function_definition_allowed_p
,
19341 int declares_class_or_enum
,
19342 bool* function_definition_p
,
19343 tree
* maybe_range_for_decl
,
19344 location_t
* init_loc
,
19347 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19348 *attributes_start_token
= NULL
;
19349 cp_declarator
*declarator
;
19350 tree prefix_attributes
;
19351 tree attributes
= NULL
;
19352 tree asm_specification
;
19354 tree decl
= NULL_TREE
;
19356 int is_initialized
;
19357 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19358 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19360 enum cpp_ttype initialization_kind
;
19361 bool is_direct_init
= false;
19362 bool is_non_constant_init
;
19363 int ctor_dtor_or_conv_p
;
19364 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19365 tree pushed_scope
= NULL_TREE
;
19366 bool range_for_decl_p
= false;
19367 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19368 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19370 /* Gather the attributes that were provided with the
19371 decl-specifiers. */
19372 prefix_attributes
= decl_specifiers
->attributes
;
19374 /* Assume that this is not the declarator for a function
19376 if (function_definition_p
)
19377 *function_definition_p
= false;
19379 /* Default arguments are only permitted for function parameters. */
19380 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19381 parser
->default_arg_ok_p
= false;
19383 /* Defer access checks while parsing the declarator; we cannot know
19384 what names are accessible until we know what is being
19386 resume_deferring_access_checks ();
19388 token
= cp_lexer_peek_token (parser
->lexer
);
19390 /* Parse the declarator. */
19392 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19393 &ctor_dtor_or_conv_p
,
19394 /*parenthesized_p=*/NULL
,
19395 member_p
, friend_p
);
19396 /* Gather up the deferred checks. */
19397 stop_deferring_access_checks ();
19399 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19401 /* If the DECLARATOR was erroneous, there's no need to go
19403 if (declarator
== cp_error_declarator
)
19404 return error_mark_node
;
19406 /* Check that the number of template-parameter-lists is OK. */
19407 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19409 return error_mark_node
;
19411 if (declares_class_or_enum
& 2)
19412 cp_parser_check_for_definition_in_return_type (declarator
,
19413 decl_specifiers
->type
,
19414 decl_specifiers
->locations
[ds_type_spec
]);
19416 /* Figure out what scope the entity declared by the DECLARATOR is
19417 located in. `grokdeclarator' sometimes changes the scope, so
19418 we compute it now. */
19419 scope
= get_scope_of_declarator (declarator
);
19421 /* Perform any lookups in the declared type which were thought to be
19422 dependent, but are not in the scope of the declarator. */
19423 decl_specifiers
->type
19424 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19426 /* If we're allowing GNU extensions, look for an
19427 asm-specification. */
19428 if (cp_parser_allow_gnu_extensions_p (parser
))
19430 /* Look for an asm-specification. */
19431 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19432 asm_specification
= cp_parser_asm_specification_opt (parser
);
19435 asm_specification
= NULL_TREE
;
19437 /* Look for attributes. */
19438 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19439 attributes
= cp_parser_attributes_opt (parser
);
19441 /* Peek at the next token. */
19442 token
= cp_lexer_peek_token (parser
->lexer
);
19444 bool bogus_implicit_tmpl
= false;
19446 if (function_declarator_p (declarator
))
19448 /* Handle C++17 deduction guides. */
19449 if (!decl_specifiers
->type
19450 && ctor_dtor_or_conv_p
<= 0
19451 && cxx_dialect
>= cxx17
)
19453 cp_declarator
*id
= get_id_declarator (declarator
);
19454 tree name
= id
->u
.id
.unqualified_name
;
19455 parser
->scope
= id
->u
.id
.qualifying_scope
;
19456 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19458 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19459 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19461 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19462 id
->u
.id
.sfk
= sfk_deduction_guide
;
19463 ctor_dtor_or_conv_p
= 1;
19467 /* Check to see if the token indicates the start of a
19468 function-definition. */
19469 if (cp_parser_token_starts_function_definition_p (token
))
19471 if (!function_definition_allowed_p
)
19473 /* If a function-definition should not appear here, issue an
19475 cp_parser_error (parser
,
19476 "a function-definition is not allowed here");
19477 return error_mark_node
;
19480 location_t func_brace_location
19481 = cp_lexer_peek_token (parser
->lexer
)->location
;
19483 /* Neither attributes nor an asm-specification are allowed
19484 on a function-definition. */
19485 if (asm_specification
)
19486 error_at (asm_spec_start_token
->location
,
19487 "an asm-specification is not allowed "
19488 "on a function-definition");
19490 error_at (attributes_start_token
->location
,
19491 "attributes are not allowed "
19492 "on a function-definition");
19493 /* This is a function-definition. */
19494 *function_definition_p
= true;
19496 /* Parse the function definition. */
19498 decl
= cp_parser_save_member_function_body (parser
,
19501 prefix_attributes
);
19504 (cp_parser_function_definition_from_specifiers_and_declarator
19505 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19507 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19509 /* This is where the prologue starts... */
19510 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19511 = func_brace_location
;
19517 else if (parser
->fully_implicit_function_template_p
)
19519 /* A non-template declaration involving a function parameter list
19520 containing an implicit template parameter will be made into a
19521 template. If the resulting declaration is not going to be an
19522 actual function then finish the template scope here to prevent it.
19523 An error message will be issued once we have a decl to talk about.
19525 FIXME probably we should do type deduction rather than create an
19526 implicit template, but the standard currently doesn't allow it. */
19527 bogus_implicit_tmpl
= true;
19528 finish_fully_implicit_template (parser
, NULL_TREE
);
19533 Only in function declarations for constructors, destructors, type
19534 conversions, and deduction guides can the decl-specifier-seq be omitted.
19536 We explicitly postpone this check past the point where we handle
19537 function-definitions because we tolerate function-definitions
19538 that are missing their return types in some modes. */
19539 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19541 cp_parser_error (parser
,
19542 "expected constructor, destructor, or type conversion");
19543 return error_mark_node
;
19546 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19547 if (token
->type
== CPP_EQ
19548 || token
->type
== CPP_OPEN_PAREN
19549 || token
->type
== CPP_OPEN_BRACE
)
19551 is_initialized
= SD_INITIALIZED
;
19552 initialization_kind
= token
->type
;
19553 if (maybe_range_for_decl
)
19554 *maybe_range_for_decl
= error_mark_node
;
19555 tmp_init_loc
= token
->location
;
19556 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19557 *init_loc
= tmp_init_loc
;
19559 if (token
->type
== CPP_EQ
19560 && function_declarator_p (declarator
))
19562 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19563 if (t2
->keyword
== RID_DEFAULT
)
19564 is_initialized
= SD_DEFAULTED
;
19565 else if (t2
->keyword
== RID_DELETE
)
19566 is_initialized
= SD_DELETED
;
19571 /* If the init-declarator isn't initialized and isn't followed by a
19572 `,' or `;', it's not a valid init-declarator. */
19573 if (token
->type
!= CPP_COMMA
19574 && token
->type
!= CPP_SEMICOLON
)
19576 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19577 range_for_decl_p
= true;
19580 if (!maybe_range_for_decl
)
19581 cp_parser_error (parser
, "expected initializer");
19582 return error_mark_node
;
19585 is_initialized
= SD_UNINITIALIZED
;
19586 initialization_kind
= CPP_EOF
;
19589 /* Because start_decl has side-effects, we should only call it if we
19590 know we're going ahead. By this point, we know that we cannot
19591 possibly be looking at any other construct. */
19592 cp_parser_commit_to_tentative_parse (parser
);
19594 /* Enter the newly declared entry in the symbol table. If we're
19595 processing a declaration in a class-specifier, we wait until
19596 after processing the initializer. */
19599 if (parser
->in_unbraced_linkage_specification_p
)
19600 decl_specifiers
->storage_class
= sc_extern
;
19601 decl
= start_decl (declarator
, decl_specifiers
,
19602 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19603 attributes
, prefix_attributes
, &pushed_scope
);
19604 cp_finalize_omp_declare_simd (parser
, decl
);
19605 cp_finalize_oacc_routine (parser
, decl
, false);
19606 /* Adjust location of decl if declarator->id_loc is more appropriate:
19607 set, and decl wasn't merged with another decl, in which case its
19608 location would be different from input_location, and more accurate. */
19610 && declarator
->id_loc
!= UNKNOWN_LOCATION
19611 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19612 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19615 /* Enter the SCOPE. That way unqualified names appearing in the
19616 initializer will be looked up in SCOPE. */
19617 pushed_scope
= push_scope (scope
);
19619 /* Perform deferred access control checks, now that we know in which
19620 SCOPE the declared entity resides. */
19621 if (!member_p
&& decl
)
19623 tree saved_current_function_decl
= NULL_TREE
;
19625 /* If the entity being declared is a function, pretend that we
19626 are in its scope. If it is a `friend', it may have access to
19627 things that would not otherwise be accessible. */
19628 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19630 saved_current_function_decl
= current_function_decl
;
19631 current_function_decl
= decl
;
19634 /* Perform access checks for template parameters. */
19635 cp_parser_perform_template_parameter_access_checks (checks
);
19637 /* Perform the access control checks for the declarator and the
19638 decl-specifiers. */
19639 perform_deferred_access_checks (tf_warning_or_error
);
19641 /* Restore the saved value. */
19642 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19643 current_function_decl
= saved_current_function_decl
;
19646 /* Parse the initializer. */
19647 initializer
= NULL_TREE
;
19648 is_direct_init
= false;
19649 is_non_constant_init
= true;
19650 if (is_initialized
)
19652 if (function_declarator_p (declarator
))
19654 if (initialization_kind
== CPP_EQ
)
19655 initializer
= cp_parser_pure_specifier (parser
);
19658 /* If the declaration was erroneous, we don't really
19659 know what the user intended, so just silently
19660 consume the initializer. */
19661 if (decl
!= error_mark_node
)
19662 error_at (tmp_init_loc
, "initializer provided for function");
19663 cp_parser_skip_to_closing_parenthesis (parser
,
19664 /*recovering=*/true,
19665 /*or_comma=*/false,
19666 /*consume_paren=*/true);
19671 /* We want to record the extra mangling scope for in-class
19672 initializers of class members and initializers of static data
19673 member templates. The former involves deferring
19674 parsing of the initializer until end of class as with default
19675 arguments. So right here we only handle the latter. */
19676 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19677 start_lambda_scope (decl
);
19678 initializer
= cp_parser_initializer (parser
,
19680 &is_non_constant_init
);
19681 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19682 finish_lambda_scope ();
19683 if (initializer
== error_mark_node
)
19684 cp_parser_skip_to_end_of_statement (parser
);
19688 /* The old parser allows attributes to appear after a parenthesized
19689 initializer. Mark Mitchell proposed removing this functionality
19690 on the GCC mailing lists on 2002-08-13. This parser accepts the
19691 attributes -- but ignores them. Made a permerror in GCC 8. */
19692 if (cp_parser_allow_gnu_extensions_p (parser
)
19693 && initialization_kind
== CPP_OPEN_PAREN
19694 && cp_parser_attributes_opt (parser
)
19695 && permerror (input_location
,
19696 "attributes after parenthesized initializer ignored"))
19699 if (flag_permissive
&& !hint
)
19702 inform (input_location
,
19703 "this flexibility is deprecated and will be removed");
19707 /* And now complain about a non-function implicit template. */
19708 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19709 error_at (DECL_SOURCE_LOCATION (decl
),
19710 "non-function %qD declared as implicit template", decl
);
19712 /* For an in-class declaration, use `grokfield' to create the
19718 pop_scope (pushed_scope
);
19719 pushed_scope
= NULL_TREE
;
19721 decl
= grokfield (declarator
, decl_specifiers
,
19722 initializer
, !is_non_constant_init
,
19723 /*asmspec=*/NULL_TREE
,
19724 attr_chainon (attributes
, prefix_attributes
));
19725 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19726 cp_parser_save_default_args (parser
, decl
);
19727 cp_finalize_omp_declare_simd (parser
, decl
);
19728 cp_finalize_oacc_routine (parser
, decl
, false);
19731 /* Finish processing the declaration. But, skip member
19733 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19735 cp_finish_decl (decl
,
19736 initializer
, !is_non_constant_init
,
19738 /* If the initializer is in parentheses, then this is
19739 a direct-initialization, which means that an
19740 `explicit' constructor is OK. Otherwise, an
19741 `explicit' constructor cannot be used. */
19742 ((is_direct_init
|| !is_initialized
)
19743 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19745 else if ((cxx_dialect
!= cxx98
) && friend_p
19746 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19747 /* Core issue #226 (C++0x only): A default template-argument
19748 shall not be specified in a friend class template
19750 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19751 /*is_partial=*/false, /*is_friend_decl=*/1);
19753 if (!friend_p
&& pushed_scope
)
19754 pop_scope (pushed_scope
);
19756 if (function_declarator_p (declarator
)
19757 && parser
->fully_implicit_function_template_p
)
19760 decl
= finish_fully_implicit_template (parser
, decl
);
19762 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19765 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19766 && type_uses_auto (decl_specifiers
->type
))
19767 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19772 /* Parse a declarator.
19776 ptr-operator declarator
19778 abstract-declarator:
19779 ptr-operator abstract-declarator [opt]
19780 direct-abstract-declarator
19785 attributes [opt] direct-declarator
19786 attributes [opt] ptr-operator declarator
19788 abstract-declarator:
19789 attributes [opt] ptr-operator abstract-declarator [opt]
19790 attributes [opt] direct-abstract-declarator
19792 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19793 detect constructors, destructors, deduction guides, or conversion operators.
19794 It is set to -1 if the declarator is a name, and +1 if it is a
19795 function. Otherwise it is set to zero. Usually you just want to
19796 test for >0, but internally the negative value is used.
19798 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19799 a decl-specifier-seq unless it declares a constructor, destructor,
19800 or conversion. It might seem that we could check this condition in
19801 semantic analysis, rather than parsing, but that makes it difficult
19802 to handle something like `f()'. We want to notice that there are
19803 no decl-specifiers, and therefore realize that this is an
19804 expression, not a declaration.)
19806 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19807 the declarator is a direct-declarator of the form "(...)".
19809 MEMBER_P is true iff this declarator is a member-declarator.
19811 FRIEND_P is true iff this declarator is a friend. */
19813 static cp_declarator
*
19814 cp_parser_declarator (cp_parser
* parser
,
19815 cp_parser_declarator_kind dcl_kind
,
19816 int* ctor_dtor_or_conv_p
,
19817 bool* parenthesized_p
,
19818 bool member_p
, bool friend_p
)
19820 cp_declarator
*declarator
;
19821 enum tree_code code
;
19822 cp_cv_quals cv_quals
;
19824 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19826 /* Assume this is not a constructor, destructor, or type-conversion
19828 if (ctor_dtor_or_conv_p
)
19829 *ctor_dtor_or_conv_p
= 0;
19831 if (cp_parser_allow_gnu_extensions_p (parser
))
19832 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19834 /* Check for the ptr-operator production. */
19835 cp_parser_parse_tentatively (parser
);
19836 /* Parse the ptr-operator. */
19837 code
= cp_parser_ptr_operator (parser
,
19842 /* If that worked, then we have a ptr-operator. */
19843 if (cp_parser_parse_definitely (parser
))
19845 /* If a ptr-operator was found, then this declarator was not
19847 if (parenthesized_p
)
19848 *parenthesized_p
= true;
19849 /* The dependent declarator is optional if we are parsing an
19850 abstract-declarator. */
19851 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19852 cp_parser_parse_tentatively (parser
);
19854 /* Parse the dependent declarator. */
19855 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19856 /*ctor_dtor_or_conv_p=*/NULL
,
19857 /*parenthesized_p=*/NULL
,
19858 /*member_p=*/false,
19861 /* If we are parsing an abstract-declarator, we must handle the
19862 case where the dependent declarator is absent. */
19863 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19864 && !cp_parser_parse_definitely (parser
))
19867 declarator
= cp_parser_make_indirect_declarator
19868 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19870 /* Everything else is a direct-declarator. */
19873 if (parenthesized_p
)
19874 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19876 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19877 ctor_dtor_or_conv_p
,
19878 member_p
, friend_p
);
19881 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19882 declarator
->attributes
= gnu_attributes
;
19886 /* Parse a direct-declarator or direct-abstract-declarator.
19890 direct-declarator ( parameter-declaration-clause )
19891 cv-qualifier-seq [opt]
19892 ref-qualifier [opt]
19893 exception-specification [opt]
19894 direct-declarator [ constant-expression [opt] ]
19897 direct-abstract-declarator:
19898 direct-abstract-declarator [opt]
19899 ( parameter-declaration-clause )
19900 cv-qualifier-seq [opt]
19901 ref-qualifier [opt]
19902 exception-specification [opt]
19903 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19904 ( abstract-declarator )
19906 Returns a representation of the declarator. DCL_KIND is
19907 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19908 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19909 we are parsing a direct-declarator. It is
19910 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19911 of ambiguity we prefer an abstract declarator, as per
19912 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19913 as for cp_parser_declarator. */
19915 static cp_declarator
*
19916 cp_parser_direct_declarator (cp_parser
* parser
,
19917 cp_parser_declarator_kind dcl_kind
,
19918 int* ctor_dtor_or_conv_p
,
19919 bool member_p
, bool friend_p
)
19922 cp_declarator
*declarator
= NULL
;
19923 tree scope
= NULL_TREE
;
19924 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19925 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19927 tree pushed_scope
= NULL_TREE
;
19928 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19932 /* Peek at the next token. */
19933 token
= cp_lexer_peek_token (parser
->lexer
);
19934 if (token
->type
== CPP_OPEN_PAREN
)
19936 /* This is either a parameter-declaration-clause, or a
19937 parenthesized declarator. When we know we are parsing a
19938 named declarator, it must be a parenthesized declarator
19939 if FIRST is true. For instance, `(int)' is a
19940 parameter-declaration-clause, with an omitted
19941 direct-abstract-declarator. But `((*))', is a
19942 parenthesized abstract declarator. Finally, when T is a
19943 template parameter `(T)' is a
19944 parameter-declaration-clause, and not a parenthesized
19947 We first try and parse a parameter-declaration-clause,
19948 and then try a nested declarator (if FIRST is true).
19950 It is not an error for it not to be a
19951 parameter-declaration-clause, even when FIRST is
19957 The first is the declaration of a function while the
19958 second is the definition of a variable, including its
19961 Having seen only the parenthesis, we cannot know which of
19962 these two alternatives should be selected. Even more
19963 complex are examples like:
19968 The former is a function-declaration; the latter is a
19969 variable initialization.
19971 Thus again, we try a parameter-declaration-clause, and if
19972 that fails, we back out and return. */
19974 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19977 bool is_declarator
= false;
19981 /* In a member-declarator, the only valid interpretation
19982 of a parenthesis is the start of a
19983 parameter-declaration-clause. (It is invalid to
19984 initialize a static data member with a parenthesized
19985 initializer; only the "=" form of initialization is
19988 cp_parser_parse_tentatively (parser
);
19990 /* Consume the `('. */
19991 matching_parens parens
;
19992 parens
.consume_open (parser
);
19995 /* If this is going to be an abstract declarator, we're
19996 in a declarator and we can't have default args. */
19997 parser
->default_arg_ok_p
= false;
19998 parser
->in_declarator_p
= true;
20001 begin_scope (sk_function_parms
, NULL_TREE
);
20003 /* Parse the parameter-declaration-clause. */
20004 params
= cp_parser_parameter_declaration_clause (parser
);
20006 /* Consume the `)'. */
20007 parens
.require_close (parser
);
20009 /* If all went well, parse the cv-qualifier-seq,
20010 ref-qualifier and the exception-specification. */
20011 if (member_p
|| cp_parser_parse_definitely (parser
))
20013 cp_cv_quals cv_quals
;
20014 cp_virt_specifiers virt_specifiers
;
20015 cp_ref_qualifier ref_qual
;
20016 tree exception_specification
;
20019 bool memfn
= (member_p
|| (pushed_scope
20020 && CLASS_TYPE_P (pushed_scope
)));
20022 is_declarator
= true;
20024 if (ctor_dtor_or_conv_p
)
20025 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20028 /* Parse the cv-qualifier-seq. */
20029 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20030 /* Parse the ref-qualifier. */
20031 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20032 /* Parse the tx-qualifier. */
20033 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20034 /* And the exception-specification. */
20035 exception_specification
20036 = cp_parser_exception_specification_opt (parser
);
20038 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20040 /* In here, we handle cases where attribute is used after
20041 the function declaration. For example:
20042 void func (int x) __attribute__((vector(..))); */
20043 tree gnu_attrs
= NULL_TREE
;
20044 tree requires_clause
= NULL_TREE
;
20045 late_return
= (cp_parser_late_return_type_opt
20046 (parser
, declarator
, requires_clause
,
20047 memfn
? cv_quals
: -1));
20049 /* Parse the virt-specifier-seq. */
20050 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20052 /* Create the function-declarator. */
20053 declarator
= make_call_declarator (declarator
,
20059 exception_specification
,
20062 declarator
->std_attributes
= attrs
;
20063 declarator
->attributes
= gnu_attrs
;
20064 /* Any subsequent parameter lists are to do with
20065 return type, so are not those of the declared
20067 parser
->default_arg_ok_p
= false;
20070 /* Remove the function parms from scope. */
20071 pop_bindings_and_leave_scope ();
20074 /* Repeat the main loop. */
20078 /* If this is the first, we can try a parenthesized
20082 bool saved_in_type_id_in_expr_p
;
20084 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20085 parser
->in_declarator_p
= saved_in_declarator_p
;
20087 open_paren
= token
;
20088 /* Consume the `('. */
20089 matching_parens parens
;
20090 parens
.consume_open (parser
);
20091 /* Parse the nested declarator. */
20092 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20093 parser
->in_type_id_in_expr_p
= true;
20095 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20096 /*parenthesized_p=*/NULL
,
20097 member_p
, friend_p
);
20098 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20100 /* Expect a `)'. */
20101 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20102 if (!parens
.require_close (parser
))
20103 declarator
= cp_error_declarator
;
20104 if (declarator
== cp_error_declarator
)
20107 goto handle_declarator
;
20109 /* Otherwise, we must be done. */
20113 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20114 && token
->type
== CPP_OPEN_SQUARE
20115 && !cp_next_tokens_can_be_attribute_p (parser
))
20117 /* Parse an array-declarator. */
20118 tree bounds
, attrs
;
20120 if (ctor_dtor_or_conv_p
)
20121 *ctor_dtor_or_conv_p
= 0;
20125 parser
->default_arg_ok_p
= false;
20126 parser
->in_declarator_p
= true;
20127 /* Consume the `['. */
20128 cp_lexer_consume_token (parser
->lexer
);
20129 /* Peek at the next token. */
20130 token
= cp_lexer_peek_token (parser
->lexer
);
20131 /* If the next token is `]', then there is no
20132 constant-expression. */
20133 if (token
->type
!= CPP_CLOSE_SQUARE
)
20135 bool non_constant_p
;
20137 = cp_parser_constant_expression (parser
,
20138 /*allow_non_constant=*/true,
20140 if (!non_constant_p
)
20142 else if (error_operand_p (bounds
))
20143 /* Already gave an error. */;
20144 else if (!parser
->in_function_body
20145 || current_binding_level
->kind
== sk_function_parms
)
20147 /* Normally, the array bound must be an integral constant
20148 expression. However, as an extension, we allow VLAs
20149 in function scopes as long as they aren't part of a
20150 parameter declaration. */
20151 cp_parser_error (parser
,
20152 "array bound is not an integer constant");
20153 bounds
= error_mark_node
;
20155 else if (processing_template_decl
20156 && !type_dependent_expression_p (bounds
))
20158 /* Remember this wasn't a constant-expression. */
20159 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20160 TREE_SIDE_EFFECTS (bounds
) = 1;
20164 bounds
= NULL_TREE
;
20165 /* Look for the closing `]'. */
20166 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20168 declarator
= cp_error_declarator
;
20172 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20173 declarator
= make_array_declarator (declarator
, bounds
);
20174 declarator
->std_attributes
= attrs
;
20176 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20179 tree qualifying_scope
;
20180 tree unqualified_name
;
20182 special_function_kind sfk
;
20184 bool pack_expansion_p
= false;
20185 cp_token
*declarator_id_start_token
;
20187 /* Parse a declarator-id */
20188 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20191 cp_parser_parse_tentatively (parser
);
20193 /* If we see an ellipsis, we should be looking at a
20195 if (token
->type
== CPP_ELLIPSIS
)
20197 /* Consume the `...' */
20198 cp_lexer_consume_token (parser
->lexer
);
20200 pack_expansion_p
= true;
20204 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20206 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20207 qualifying_scope
= parser
->scope
;
20212 if (!unqualified_name
&& pack_expansion_p
)
20214 /* Check whether an error occurred. */
20215 okay
= !cp_parser_error_occurred (parser
);
20217 /* We already consumed the ellipsis to mark a
20218 parameter pack, but we have no way to report it,
20219 so abort the tentative parse. We will be exiting
20220 immediately anyway. */
20221 cp_parser_abort_tentative_parse (parser
);
20224 okay
= cp_parser_parse_definitely (parser
);
20227 unqualified_name
= error_mark_node
;
20228 else if (unqualified_name
20229 && (qualifying_scope
20230 || (!identifier_p (unqualified_name
))))
20232 cp_parser_error (parser
, "expected unqualified-id");
20233 unqualified_name
= error_mark_node
;
20237 if (!unqualified_name
)
20239 if (unqualified_name
== error_mark_node
)
20241 declarator
= cp_error_declarator
;
20242 pack_expansion_p
= false;
20243 declarator
->parameter_pack_p
= false;
20247 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20249 if (qualifying_scope
&& at_namespace_scope_p ()
20250 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20252 /* In the declaration of a member of a template class
20253 outside of the class itself, the SCOPE will sometimes
20254 be a TYPENAME_TYPE. For example, given:
20256 template <typename T>
20257 int S<T>::R::i = 3;
20259 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20260 this context, we must resolve S<T>::R to an ordinary
20261 type, rather than a typename type.
20263 The reason we normally avoid resolving TYPENAME_TYPEs
20264 is that a specialization of `S' might render
20265 `S<T>::R' not a type. However, if `S' is
20266 specialized, then this `i' will not be used, so there
20267 is no harm in resolving the types here. */
20270 /* Resolve the TYPENAME_TYPE. */
20271 type
= resolve_typename_type (qualifying_scope
,
20272 /*only_current_p=*/false);
20273 /* If that failed, the declarator is invalid. */
20274 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20276 if (typedef_variant_p (type
))
20277 error_at (declarator_id_start_token
->location
,
20278 "cannot define member of dependent typedef "
20281 error_at (declarator_id_start_token
->location
,
20282 "%<%T::%E%> is not a type",
20283 TYPE_CONTEXT (qualifying_scope
),
20284 TYPE_IDENTIFIER (qualifying_scope
));
20286 qualifying_scope
= type
;
20291 if (unqualified_name
)
20295 if (qualifying_scope
20296 && CLASS_TYPE_P (qualifying_scope
))
20297 class_type
= qualifying_scope
;
20299 class_type
= current_class_type
;
20301 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20303 tree name_type
= TREE_TYPE (unqualified_name
);
20305 if (!class_type
|| !same_type_p (name_type
, class_type
))
20307 /* We do not attempt to print the declarator
20308 here because we do not have enough
20309 information about its original syntactic
20311 cp_parser_error (parser
, "invalid declarator");
20312 declarator
= cp_error_declarator
;
20315 else if (qualifying_scope
20316 && CLASSTYPE_USE_TEMPLATE (name_type
))
20318 error_at (declarator_id_start_token
->location
,
20319 "invalid use of constructor as a template");
20320 inform (declarator_id_start_token
->location
,
20321 "use %<%T::%D%> instead of %<%T::%D%> to "
20322 "name the constructor in a qualified name",
20324 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20325 class_type
, name_type
);
20326 declarator
= cp_error_declarator
;
20329 unqualified_name
= constructor_name (class_type
);
20334 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20335 sfk
= sfk_destructor
;
20336 else if (identifier_p (unqualified_name
)
20337 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20338 sfk
= sfk_conversion
;
20339 else if (/* There's no way to declare a constructor
20340 for an unnamed type, even if the type
20341 got a name for linkage purposes. */
20342 !TYPE_WAS_UNNAMED (class_type
)
20343 /* Handle correctly (c++/19200):
20357 friend void N::S();
20359 && (!friend_p
|| class_type
== qualifying_scope
)
20360 && constructor_name_p (unqualified_name
,
20362 sfk
= sfk_constructor
;
20363 else if (is_overloaded_fn (unqualified_name
)
20364 && DECL_CONSTRUCTOR_P (get_first_fn
20365 (unqualified_name
)))
20366 sfk
= sfk_constructor
;
20368 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20369 *ctor_dtor_or_conv_p
= -1;
20372 declarator
= make_id_declarator (qualifying_scope
,
20375 declarator
->std_attributes
= attrs
;
20376 declarator
->id_loc
= token
->location
;
20377 declarator
->parameter_pack_p
= pack_expansion_p
;
20379 if (pack_expansion_p
)
20380 maybe_warn_variadic_templates ();
20383 handle_declarator
:;
20384 scope
= get_scope_of_declarator (declarator
);
20387 /* Any names that appear after the declarator-id for a
20388 member are looked up in the containing scope. */
20389 if (at_function_scope_p ())
20391 /* But declarations with qualified-ids can't appear in a
20393 cp_parser_error (parser
, "qualified-id in declaration");
20394 declarator
= cp_error_declarator
;
20397 pushed_scope
= push_scope (scope
);
20399 parser
->in_declarator_p
= true;
20400 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20401 || (declarator
&& declarator
->kind
== cdk_id
))
20402 /* Default args are only allowed on function
20404 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20406 parser
->default_arg_ok_p
= false;
20415 /* For an abstract declarator, we might wind up with nothing at this
20416 point. That's an error; the declarator is not optional. */
20418 cp_parser_error (parser
, "expected declarator");
20419 else if (open_paren
)
20421 /* Record overly parenthesized declarator so we can give a
20422 diagnostic about confusing decl/expr disambiguation. */
20423 if (declarator
->kind
== cdk_array
)
20425 /* If the open and close parens are on different lines, this
20426 is probably a formatting thing, so ignore. */
20427 expanded_location open
= expand_location (open_paren
->location
);
20428 expanded_location close
= expand_location (close_paren
->location
);
20429 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20433 declarator
->parenthesized
= open_paren
->location
;
20436 /* If we entered a scope, we must exit it now. */
20438 pop_scope (pushed_scope
);
20440 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20441 parser
->in_declarator_p
= saved_in_declarator_p
;
20446 /* Parse a ptr-operator.
20449 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20450 * cv-qualifier-seq [opt]
20452 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20453 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20458 & cv-qualifier-seq [opt]
20460 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20461 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20462 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20463 filled in with the TYPE containing the member. *CV_QUALS is
20464 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20465 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20466 Note that the tree codes returned by this function have nothing
20467 to do with the types of trees that will be eventually be created
20468 to represent the pointer or reference type being parsed. They are
20469 just constants with suggestive names. */
20470 static enum tree_code
20471 cp_parser_ptr_operator (cp_parser
* parser
,
20473 cp_cv_quals
*cv_quals
,
20476 enum tree_code code
= ERROR_MARK
;
20478 tree attrs
= NULL_TREE
;
20480 /* Assume that it's not a pointer-to-member. */
20482 /* And that there are no cv-qualifiers. */
20483 *cv_quals
= TYPE_UNQUALIFIED
;
20485 /* Peek at the next token. */
20486 token
= cp_lexer_peek_token (parser
->lexer
);
20488 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20489 if (token
->type
== CPP_MULT
)
20490 code
= INDIRECT_REF
;
20491 else if (token
->type
== CPP_AND
)
20493 else if ((cxx_dialect
!= cxx98
) &&
20494 token
->type
== CPP_AND_AND
) /* C++0x only */
20495 code
= NON_LVALUE_EXPR
;
20497 if (code
!= ERROR_MARK
)
20499 /* Consume the `*', `&' or `&&'. */
20500 cp_lexer_consume_token (parser
->lexer
);
20502 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20503 `&', if we are allowing GNU extensions. (The only qualifier
20504 that can legally appear after `&' is `restrict', but that is
20505 enforced during semantic analysis. */
20506 if (code
== INDIRECT_REF
20507 || cp_parser_allow_gnu_extensions_p (parser
))
20508 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20510 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20511 if (attributes
!= NULL
)
20512 *attributes
= attrs
;
20516 /* Try the pointer-to-member case. */
20517 cp_parser_parse_tentatively (parser
);
20518 /* Look for the optional `::' operator. */
20519 cp_parser_global_scope_opt (parser
,
20520 /*current_scope_valid_p=*/false);
20521 /* Look for the nested-name specifier. */
20522 token
= cp_lexer_peek_token (parser
->lexer
);
20523 cp_parser_nested_name_specifier (parser
,
20524 /*typename_keyword_p=*/false,
20525 /*check_dependency_p=*/true,
20527 /*is_declaration=*/false);
20528 /* If we found it, and the next token is a `*', then we are
20529 indeed looking at a pointer-to-member operator. */
20530 if (!cp_parser_error_occurred (parser
)
20531 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20533 /* Indicate that the `*' operator was used. */
20534 code
= INDIRECT_REF
;
20536 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20537 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20538 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20539 error_at (token
->location
, "cannot form pointer to member of "
20540 "non-class %q#T", parser
->scope
);
20543 /* The type of which the member is a member is given by the
20545 *type
= parser
->scope
;
20546 /* The next name will not be qualified. */
20547 parser
->scope
= NULL_TREE
;
20548 parser
->qualifying_scope
= NULL_TREE
;
20549 parser
->object_scope
= NULL_TREE
;
20550 /* Look for optional c++11 attributes. */
20551 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20552 if (attributes
!= NULL
)
20553 *attributes
= attrs
;
20554 /* Look for the optional cv-qualifier-seq. */
20555 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20558 /* If that didn't work we don't have a ptr-operator. */
20559 if (!cp_parser_parse_definitely (parser
))
20560 cp_parser_error (parser
, "expected ptr-operator");
20566 /* Parse an (optional) cv-qualifier-seq.
20569 cv-qualifier cv-qualifier-seq [opt]
20580 Returns a bitmask representing the cv-qualifiers. */
20583 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20585 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20590 cp_cv_quals cv_qualifier
;
20592 /* Peek at the next token. */
20593 token
= cp_lexer_peek_token (parser
->lexer
);
20594 /* See if it's a cv-qualifier. */
20595 switch (token
->keyword
)
20598 cv_qualifier
= TYPE_QUAL_CONST
;
20602 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20606 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20610 cv_qualifier
= TYPE_UNQUALIFIED
;
20617 if (cv_quals
& cv_qualifier
)
20619 gcc_rich_location
richloc (token
->location
);
20620 richloc
.add_fixit_remove ();
20621 error_at (&richloc
, "duplicate cv-qualifier");
20622 cp_lexer_purge_token (parser
->lexer
);
20626 cp_lexer_consume_token (parser
->lexer
);
20627 cv_quals
|= cv_qualifier
;
20634 /* Parse an (optional) ref-qualifier
20640 Returns cp_ref_qualifier representing ref-qualifier. */
20642 static cp_ref_qualifier
20643 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20645 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20647 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20648 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20653 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20654 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20656 switch (token
->type
)
20659 curr_ref_qual
= REF_QUAL_LVALUE
;
20663 curr_ref_qual
= REF_QUAL_RVALUE
;
20667 curr_ref_qual
= REF_QUAL_NONE
;
20671 if (!curr_ref_qual
)
20675 error_at (token
->location
, "multiple ref-qualifiers");
20676 cp_lexer_purge_token (parser
->lexer
);
20680 ref_qual
= curr_ref_qual
;
20681 cp_lexer_consume_token (parser
->lexer
);
20688 /* Parse an optional tx-qualifier.
20692 transaction_safe_dynamic */
20695 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20697 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20698 if (token
->type
== CPP_NAME
)
20700 tree name
= token
->u
.value
;
20701 const char *p
= IDENTIFIER_POINTER (name
);
20702 const int len
= strlen ("transaction_safe");
20703 if (!strncmp (p
, "transaction_safe", len
))
20707 || !strcmp (p
, "_dynamic"))
20709 cp_lexer_consume_token (parser
->lexer
);
20712 error ("%qE requires %<-fgnu-tm%>", name
);
20723 /* Parse an (optional) virt-specifier-seq.
20725 virt-specifier-seq:
20726 virt-specifier virt-specifier-seq [opt]
20732 Returns a bitmask representing the virt-specifiers. */
20734 static cp_virt_specifiers
20735 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20737 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20742 cp_virt_specifiers virt_specifier
;
20744 /* Peek at the next token. */
20745 token
= cp_lexer_peek_token (parser
->lexer
);
20746 /* See if it's a virt-specifier-qualifier. */
20747 if (token
->type
!= CPP_NAME
)
20749 if (id_equal (token
->u
.value
, "override"))
20751 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20752 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20754 else if (id_equal (token
->u
.value
, "final"))
20756 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20757 virt_specifier
= VIRT_SPEC_FINAL
;
20759 else if (id_equal (token
->u
.value
, "__final"))
20761 virt_specifier
= VIRT_SPEC_FINAL
;
20766 if (virt_specifiers
& virt_specifier
)
20768 gcc_rich_location
richloc (token
->location
);
20769 richloc
.add_fixit_remove ();
20770 error_at (&richloc
, "duplicate virt-specifier");
20771 cp_lexer_purge_token (parser
->lexer
);
20775 cp_lexer_consume_token (parser
->lexer
);
20776 virt_specifiers
|= virt_specifier
;
20779 return virt_specifiers
;
20782 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20783 is in scope even though it isn't real. */
20786 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20790 if (current_class_ptr
)
20792 /* We don't clear this between NSDMIs. Is it already what we want? */
20793 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20794 if (DECL_P (current_class_ptr
)
20795 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20796 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20797 && cp_type_quals (type
) == quals
)
20801 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20802 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20803 current_class_ptr
= NULL_TREE
;
20805 = cp_build_fold_indirect_ref (this_parm
);
20806 current_class_ptr
= this_parm
;
20809 /* Return true iff our current scope is a non-static data member
20813 parsing_nsdmi (void)
20815 /* We recognize NSDMI context by the context-less 'this' pointer set up
20816 by the function above. */
20817 if (current_class_ptr
20818 && TREE_CODE (current_class_ptr
) == PARM_DECL
20819 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20824 /* Parse a late-specified return type, if any. This is not a separate
20825 non-terminal, but part of a function declarator, which looks like
20827 -> trailing-type-specifier-seq abstract-declarator(opt)
20829 Returns the type indicated by the type-id.
20831 In addition to this, parse any queued up #pragma omp declare simd
20832 clauses, and #pragma acc routine clauses.
20834 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20838 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20839 tree
& requires_clause
, cp_cv_quals quals
)
20842 tree type
= NULL_TREE
;
20843 bool declare_simd_p
= (parser
->omp_declare_simd
20845 && declarator
->kind
== cdk_id
);
20847 bool oacc_routine_p
= (parser
->oacc_routine
20849 && declarator
->kind
== cdk_id
);
20851 /* Peek at the next token. */
20852 token
= cp_lexer_peek_token (parser
->lexer
);
20853 /* A late-specified return type is indicated by an initial '->'. */
20854 if (token
->type
!= CPP_DEREF
20855 && token
->keyword
!= RID_REQUIRES
20856 && !(token
->type
== CPP_NAME
20857 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20858 && !(declare_simd_p
|| oacc_routine_p
))
20861 tree save_ccp
= current_class_ptr
;
20862 tree save_ccr
= current_class_ref
;
20865 /* DR 1207: 'this' is in scope in the trailing return type. */
20866 inject_this_parameter (current_class_type
, quals
);
20869 if (token
->type
== CPP_DEREF
)
20871 /* Consume the ->. */
20872 cp_lexer_consume_token (parser
->lexer
);
20874 type
= cp_parser_trailing_type_id (parser
);
20877 /* Function declarations may be followed by a trailing
20878 requires-clause. */
20879 requires_clause
= cp_parser_requires_clause_opt (parser
);
20881 if (declare_simd_p
)
20882 declarator
->attributes
20883 = cp_parser_late_parsing_omp_declare_simd (parser
,
20884 declarator
->attributes
);
20885 if (oacc_routine_p
)
20886 declarator
->attributes
20887 = cp_parser_late_parsing_oacc_routine (parser
,
20888 declarator
->attributes
);
20892 current_class_ptr
= save_ccp
;
20893 current_class_ref
= save_ccr
;
20899 /* Parse a declarator-id.
20903 :: [opt] nested-name-specifier [opt] type-name
20905 In the `id-expression' case, the value returned is as for
20906 cp_parser_id_expression if the id-expression was an unqualified-id.
20907 If the id-expression was a qualified-id, then a SCOPE_REF is
20908 returned. The first operand is the scope (either a NAMESPACE_DECL
20909 or TREE_TYPE), but the second is still just a representation of an
20913 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20916 /* The expression must be an id-expression. Assume that qualified
20917 names are the names of types so that:
20920 int S<T>::R::i = 3;
20922 will work; we must treat `S<T>::R' as the name of a type.
20923 Similarly, assume that qualified names are templates, where
20927 int S<T>::R<T>::i = 3;
20930 id
= cp_parser_id_expression (parser
,
20931 /*template_keyword_p=*/false,
20932 /*check_dependency_p=*/false,
20933 /*template_p=*/NULL
,
20934 /*declarator_p=*/true,
20936 if (id
&& BASELINK_P (id
))
20937 id
= BASELINK_FUNCTIONS (id
);
20941 /* Parse a type-id.
20944 type-specifier-seq abstract-declarator [opt]
20946 Returns the TYPE specified. */
20949 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20950 bool is_trailing_return
)
20952 cp_decl_specifier_seq type_specifier_seq
;
20953 cp_declarator
*abstract_declarator
;
20955 /* Parse the type-specifier-seq. */
20956 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20957 is_trailing_return
,
20958 &type_specifier_seq
);
20959 if (is_template_arg
&& type_specifier_seq
.type
20960 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20961 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20962 /* A bare template name as a template argument is a template template
20963 argument, not a placeholder, so fail parsing it as a type argument. */
20965 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20966 cp_parser_simulate_error (parser
);
20967 return error_mark_node
;
20969 if (type_specifier_seq
.type
== error_mark_node
)
20970 return error_mark_node
;
20972 /* There might or might not be an abstract declarator. */
20973 cp_parser_parse_tentatively (parser
);
20974 /* Look for the declarator. */
20975 abstract_declarator
20976 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20977 /*parenthesized_p=*/NULL
,
20978 /*member_p=*/false,
20979 /*friend_p=*/false);
20980 /* Check to see if there really was a declarator. */
20981 if (!cp_parser_parse_definitely (parser
))
20982 abstract_declarator
= NULL
;
20984 if (type_specifier_seq
.type
20985 /* The concepts TS allows 'auto' as a type-id. */
20986 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20987 /* None of the valid uses of 'auto' in C++14 involve the type-id
20988 nonterminal, but it is valid in a trailing-return-type. */
20989 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20990 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20992 /* A type-id with type 'auto' is only ok if the abstract declarator
20993 is a function declarator with a late-specified return type.
20995 A type-id with 'auto' is also valid in a trailing-return-type
20996 in a compound-requirement. */
20997 if (abstract_declarator
20998 && abstract_declarator
->kind
== cdk_function
20999 && abstract_declarator
->u
.function
.late_return_type
)
21001 else if (parser
->in_result_type_constraint_p
)
21005 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21006 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21008 error_at (loc
, "missing template arguments after %qT",
21010 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21014 error_at (loc
, "invalid use of %qT", auto_node
);
21015 return error_mark_node
;
21019 return groktypename (&type_specifier_seq
, abstract_declarator
,
21024 cp_parser_type_id (cp_parser
*parser
)
21026 return cp_parser_type_id_1 (parser
, false, false);
21030 cp_parser_template_type_arg (cp_parser
*parser
)
21033 const char *saved_message
= parser
->type_definition_forbidden_message
;
21034 parser
->type_definition_forbidden_message
21035 = G_("types may not be defined in template arguments");
21036 r
= cp_parser_type_id_1 (parser
, true, false);
21037 parser
->type_definition_forbidden_message
= saved_message
;
21038 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21040 error ("invalid use of %<auto%> in template argument");
21041 r
= error_mark_node
;
21047 cp_parser_trailing_type_id (cp_parser
*parser
)
21049 return cp_parser_type_id_1 (parser
, false, true);
21052 /* Parse a type-specifier-seq.
21054 type-specifier-seq:
21055 type-specifier type-specifier-seq [opt]
21059 type-specifier-seq:
21060 attributes type-specifier-seq [opt]
21062 If IS_DECLARATION is true, we are at the start of a "condition" or
21063 exception-declaration, so we might be followed by a declarator-id.
21065 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21066 i.e. we've just seen "->".
21068 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21071 cp_parser_type_specifier_seq (cp_parser
* parser
,
21072 bool is_declaration
,
21073 bool is_trailing_return
,
21074 cp_decl_specifier_seq
*type_specifier_seq
)
21076 bool seen_type_specifier
= false;
21077 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21078 cp_token
*start_token
= NULL
;
21080 /* Clear the TYPE_SPECIFIER_SEQ. */
21081 clear_decl_specs (type_specifier_seq
);
21083 /* In the context of a trailing return type, enum E { } is an
21084 elaborated-type-specifier followed by a function-body, not an
21086 if (is_trailing_return
)
21087 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21089 /* Parse the type-specifiers and attributes. */
21092 tree type_specifier
;
21093 bool is_cv_qualifier
;
21095 /* Check for attributes first. */
21096 if (cp_next_tokens_can_be_attribute_p (parser
))
21098 type_specifier_seq
->attributes
21099 = attr_chainon (type_specifier_seq
->attributes
,
21100 cp_parser_attributes_opt (parser
));
21104 /* record the token of the beginning of the type specifier seq,
21105 for error reporting purposes*/
21107 start_token
= cp_lexer_peek_token (parser
->lexer
);
21109 /* Look for the type-specifier. */
21110 type_specifier
= cp_parser_type_specifier (parser
,
21112 type_specifier_seq
,
21113 /*is_declaration=*/false,
21116 if (!type_specifier
)
21118 /* If the first type-specifier could not be found, this is not a
21119 type-specifier-seq at all. */
21120 if (!seen_type_specifier
)
21122 /* Set in_declarator_p to avoid skipping to the semicolon. */
21123 int in_decl
= parser
->in_declarator_p
;
21124 parser
->in_declarator_p
= true;
21126 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21127 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21128 cp_parser_error (parser
, "expected type-specifier");
21130 parser
->in_declarator_p
= in_decl
;
21132 type_specifier_seq
->type
= error_mark_node
;
21135 /* If subsequent type-specifiers could not be found, the
21136 type-specifier-seq is complete. */
21140 seen_type_specifier
= true;
21141 /* The standard says that a condition can be:
21143 type-specifier-seq declarator = assignment-expression
21150 we should treat the "S" as a declarator, not as a
21151 type-specifier. The standard doesn't say that explicitly for
21152 type-specifier-seq, but it does say that for
21153 decl-specifier-seq in an ordinary declaration. Perhaps it
21154 would be clearer just to allow a decl-specifier-seq here, and
21155 then add a semantic restriction that if any decl-specifiers
21156 that are not type-specifiers appear, the program is invalid. */
21157 if (is_declaration
&& !is_cv_qualifier
)
21158 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21162 /* Return whether the function currently being declared has an associated
21163 template parameter list. */
21166 function_being_declared_is_template_p (cp_parser
* parser
)
21168 if (!current_template_parms
|| processing_template_parmlist
)
21171 if (parser
->implicit_template_scope
)
21174 if (at_class_scope_p ()
21175 && TYPE_BEING_DEFINED (current_class_type
))
21176 return parser
->num_template_parameter_lists
!= 0;
21178 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21179 (current_class_type
));
21182 /* Parse a parameter-declaration-clause.
21184 parameter-declaration-clause:
21185 parameter-declaration-list [opt] ... [opt]
21186 parameter-declaration-list , ...
21188 Returns a representation for the parameter declarations. A return
21189 value of NULL indicates a parameter-declaration-clause consisting
21190 only of an ellipsis. */
21193 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21200 temp_override
<bool> cleanup
21201 (parser
->auto_is_implicit_function_template_parm_p
);
21203 if (!processing_specialization
21204 && !processing_template_parmlist
21205 && !processing_explicit_instantiation
21206 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21207 actual function or a random abstract declarator. */
21208 && parser
->default_arg_ok_p
)
21209 if (!current_function_decl
21210 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21211 parser
->auto_is_implicit_function_template_parm_p
= true;
21213 /* Peek at the next token. */
21214 token
= cp_lexer_peek_token (parser
->lexer
);
21215 /* Check for trivial parameter-declaration-clauses. */
21216 if (token
->type
== CPP_ELLIPSIS
)
21218 /* Consume the `...' token. */
21219 cp_lexer_consume_token (parser
->lexer
);
21222 else if (token
->type
== CPP_CLOSE_PAREN
)
21223 /* There are no parameters. */
21225 #ifndef NO_IMPLICIT_EXTERN_C
21226 if (in_system_header_at (input_location
)
21227 && current_class_type
== NULL
21228 && current_lang_name
== lang_name_c
)
21232 return void_list_node
;
21234 /* Check for `(void)', too, which is a special case. */
21235 else if (token
->keyword
== RID_VOID
21236 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21237 == CPP_CLOSE_PAREN
))
21239 /* Consume the `void' token. */
21240 cp_lexer_consume_token (parser
->lexer
);
21241 /* There are no parameters. */
21242 return void_list_node
;
21245 /* Parse the parameter-declaration-list. */
21246 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21247 /* If a parse error occurred while parsing the
21248 parameter-declaration-list, then the entire
21249 parameter-declaration-clause is erroneous. */
21253 /* Peek at the next token. */
21254 token
= cp_lexer_peek_token (parser
->lexer
);
21255 /* If it's a `,', the clause should terminate with an ellipsis. */
21256 if (token
->type
== CPP_COMMA
)
21258 /* Consume the `,'. */
21259 cp_lexer_consume_token (parser
->lexer
);
21260 /* Expect an ellipsis. */
21262 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21264 /* It might also be `...' if the optional trailing `,' was
21266 else if (token
->type
== CPP_ELLIPSIS
)
21268 /* Consume the `...' token. */
21269 cp_lexer_consume_token (parser
->lexer
);
21270 /* And remember that we saw it. */
21274 ellipsis_p
= false;
21276 /* Finish the parameter list. */
21278 parameters
= chainon (parameters
, void_list_node
);
21283 /* Parse a parameter-declaration-list.
21285 parameter-declaration-list:
21286 parameter-declaration
21287 parameter-declaration-list , parameter-declaration
21289 Returns a representation of the parameter-declaration-list, as for
21290 cp_parser_parameter_declaration_clause. However, the
21291 `void_list_node' is never appended to the list. Upon return,
21292 *IS_ERROR will be true iff an error occurred. */
21295 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21297 tree parameters
= NULL_TREE
;
21298 tree
*tail
= ¶meters
;
21299 bool saved_in_unbraced_linkage_specification_p
;
21302 /* Assume all will go well. */
21304 /* The special considerations that apply to a function within an
21305 unbraced linkage specifications do not apply to the parameters
21306 to the function. */
21307 saved_in_unbraced_linkage_specification_p
21308 = parser
->in_unbraced_linkage_specification_p
;
21309 parser
->in_unbraced_linkage_specification_p
= false;
21311 /* Look for more parameters. */
21314 cp_parameter_declarator
*parameter
;
21315 tree decl
= error_mark_node
;
21316 bool parenthesized_p
= false;
21318 /* Parse the parameter. */
21320 = cp_parser_parameter_declaration (parser
,
21321 /*template_parm_p=*/false,
21324 /* We don't know yet if the enclosing context is deprecated, so wait
21325 and warn in grokparms if appropriate. */
21326 deprecated_state
= DEPRECATED_SUPPRESS
;
21330 decl
= grokdeclarator (parameter
->declarator
,
21331 ¶meter
->decl_specifiers
,
21333 parameter
->default_argument
!= NULL_TREE
,
21334 ¶meter
->decl_specifiers
.attributes
);
21335 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21336 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21339 deprecated_state
= DEPRECATED_NORMAL
;
21341 /* If a parse error occurred parsing the parameter declaration,
21342 then the entire parameter-declaration-list is erroneous. */
21343 if (decl
== error_mark_node
)
21346 parameters
= error_mark_node
;
21347 if (parser
->fully_implicit_function_template_p
)
21348 abort_fully_implicit_template (parser
);
21352 if (parameter
->decl_specifiers
.attributes
)
21353 cplus_decl_attributes (&decl
,
21354 parameter
->decl_specifiers
.attributes
,
21356 if (DECL_NAME (decl
))
21357 decl
= pushdecl (decl
);
21359 if (decl
!= error_mark_node
)
21361 retrofit_lang_decl (decl
);
21362 DECL_PARM_INDEX (decl
) = ++index
;
21363 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21366 /* Add the new parameter to the list. */
21367 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21368 tail
= &TREE_CHAIN (*tail
);
21370 /* Peek at the next token. */
21371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21372 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21373 /* These are for Objective-C++ */
21374 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21375 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21376 /* The parameter-declaration-list is complete. */
21378 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21382 /* Peek at the next token. */
21383 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21384 /* If it's an ellipsis, then the list is complete. */
21385 if (token
->type
== CPP_ELLIPSIS
)
21387 /* Otherwise, there must be more parameters. Consume the
21389 cp_lexer_consume_token (parser
->lexer
);
21390 /* When parsing something like:
21392 int i(float f, double d)
21394 we can tell after seeing the declaration for "f" that we
21395 are not looking at an initialization of a variable "i",
21396 but rather at the declaration of a function "i".
21398 Due to the fact that the parsing of template arguments
21399 (as specified to a template-id) requires backtracking we
21400 cannot use this technique when inside a template argument
21402 if (!parser
->in_template_argument_list_p
21403 && !parser
->in_type_id_in_expr_p
21404 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21405 /* However, a parameter-declaration of the form
21406 "float(f)" (which is a valid declaration of a
21407 parameter "f") can also be interpreted as an
21408 expression (the conversion of "f" to "float"). */
21409 && !parenthesized_p
)
21410 cp_parser_commit_to_tentative_parse (parser
);
21414 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21415 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21416 cp_parser_skip_to_closing_parenthesis (parser
,
21417 /*recovering=*/true,
21418 /*or_comma=*/false,
21419 /*consume_paren=*/false);
21424 parser
->in_unbraced_linkage_specification_p
21425 = saved_in_unbraced_linkage_specification_p
;
21427 /* Reset implicit_template_scope if we are about to leave the function
21428 parameter list that introduced it. Note that for out-of-line member
21429 definitions, there will be one or more class scopes before we get to
21430 the template parameter scope. */
21432 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21433 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21435 while (maybe_its
->kind
== sk_class
)
21436 maybe_its
= maybe_its
->level_chain
;
21437 if (maybe_its
== its
)
21439 parser
->implicit_template_parms
= 0;
21440 parser
->implicit_template_scope
= 0;
21447 /* Parse a parameter declaration.
21449 parameter-declaration:
21450 decl-specifier-seq ... [opt] declarator
21451 decl-specifier-seq declarator = assignment-expression
21452 decl-specifier-seq ... [opt] abstract-declarator [opt]
21453 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21455 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21456 declares a template parameter. (In that case, a non-nested `>'
21457 token encountered during the parsing of the assignment-expression
21458 is not interpreted as a greater-than operator.)
21460 Returns a representation of the parameter, or NULL if an error
21461 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21462 true iff the declarator is of the form "(p)". */
21464 static cp_parameter_declarator
*
21465 cp_parser_parameter_declaration (cp_parser
*parser
,
21466 bool template_parm_p
,
21467 bool *parenthesized_p
)
21469 int declares_class_or_enum
;
21470 cp_decl_specifier_seq decl_specifiers
;
21471 cp_declarator
*declarator
;
21472 tree default_argument
;
21473 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21474 const char *saved_message
;
21475 bool template_parameter_pack_p
= false;
21477 /* In a template parameter, `>' is not an operator.
21481 When parsing a default template-argument for a non-type
21482 template-parameter, the first non-nested `>' is taken as the end
21483 of the template parameter-list rather than a greater-than
21486 /* Type definitions may not appear in parameter types. */
21487 saved_message
= parser
->type_definition_forbidden_message
;
21488 parser
->type_definition_forbidden_message
21489 = G_("types may not be defined in parameter types");
21491 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21492 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21493 (current_template_parms
)) : 0);
21495 /* Parse the declaration-specifiers. */
21496 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21497 cp_parser_decl_specifier_seq (parser
,
21498 CP_PARSER_FLAGS_NONE
,
21500 &declares_class_or_enum
);
21502 /* Complain about missing 'typename' or other invalid type names. */
21503 if (!decl_specifiers
.any_type_specifiers_p
21504 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21505 decl_specifiers
.type
= error_mark_node
;
21507 /* If an error occurred, there's no reason to attempt to parse the
21508 rest of the declaration. */
21509 if (cp_parser_error_occurred (parser
))
21511 parser
->type_definition_forbidden_message
= saved_message
;
21515 /* Peek at the next token. */
21516 token
= cp_lexer_peek_token (parser
->lexer
);
21518 /* If the next token is a `)', `,', `=', `>', or `...', then there
21519 is no declarator. However, when variadic templates are enabled,
21520 there may be a declarator following `...'. */
21521 if (token
->type
== CPP_CLOSE_PAREN
21522 || token
->type
== CPP_COMMA
21523 || token
->type
== CPP_EQ
21524 || token
->type
== CPP_GREATER
)
21527 if (parenthesized_p
)
21528 *parenthesized_p
= false;
21530 /* Otherwise, there should be a declarator. */
21533 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21534 parser
->default_arg_ok_p
= false;
21536 /* After seeing a decl-specifier-seq, if the next token is not a
21537 "(", there is no possibility that the code is a valid
21538 expression. Therefore, if parsing tentatively, we commit at
21540 if (!parser
->in_template_argument_list_p
21541 /* In an expression context, having seen:
21545 we cannot be sure whether we are looking at a
21546 function-type (taking a "char" as a parameter) or a cast
21547 of some object of type "char" to "int". */
21548 && !parser
->in_type_id_in_expr_p
21549 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21550 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21551 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21552 cp_parser_commit_to_tentative_parse (parser
);
21553 /* Parse the declarator. */
21554 declarator_token_start
= token
;
21555 declarator
= cp_parser_declarator (parser
,
21556 CP_PARSER_DECLARATOR_EITHER
,
21557 /*ctor_dtor_or_conv_p=*/NULL
,
21559 /*member_p=*/false,
21560 /*friend_p=*/false);
21561 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21562 /* After the declarator, allow more attributes. */
21563 decl_specifiers
.attributes
21564 = attr_chainon (decl_specifiers
.attributes
,
21565 cp_parser_attributes_opt (parser
));
21567 /* If the declarator is a template parameter pack, remember that and
21568 clear the flag in the declarator itself so we don't get errors
21569 from grokdeclarator. */
21570 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21572 declarator
->parameter_pack_p
= false;
21573 template_parameter_pack_p
= true;
21577 /* If the next token is an ellipsis, and we have not seen a declarator
21578 name, and if either the type of the declarator contains parameter
21579 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21580 for, eg, abbreviated integral type names), then we actually have a
21581 parameter pack expansion expression. Otherwise, leave the ellipsis
21582 for a C-style variadic function. */
21583 token
= cp_lexer_peek_token (parser
->lexer
);
21585 /* If a function parameter pack was specified and an implicit template
21586 parameter was introduced during cp_parser_parameter_declaration,
21587 change any implicit parameters introduced into packs. */
21588 if (parser
->implicit_template_parms
21589 && (token
->type
== CPP_ELLIPSIS
21590 || (declarator
&& declarator
->parameter_pack_p
)))
21592 int latest_template_parm_idx
= TREE_VEC_LENGTH
21593 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21595 if (latest_template_parm_idx
!= template_parm_idx
)
21596 decl_specifiers
.type
= convert_generic_types_to_packs
21597 (decl_specifiers
.type
,
21598 template_parm_idx
, latest_template_parm_idx
);
21601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21603 tree type
= decl_specifiers
.type
;
21605 if (type
&& DECL_P (type
))
21606 type
= TREE_TYPE (type
);
21609 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21610 && (template_parm_p
|| uses_parameter_packs (type
)))
21611 || (!type
&& template_parm_p
))
21612 && declarator_can_be_parameter_pack (declarator
))
21614 /* Consume the `...'. */
21615 cp_lexer_consume_token (parser
->lexer
);
21616 maybe_warn_variadic_templates ();
21618 /* Build a pack expansion type */
21619 if (template_parm_p
)
21620 template_parameter_pack_p
= true;
21621 else if (declarator
)
21622 declarator
->parameter_pack_p
= true;
21624 decl_specifiers
.type
= make_pack_expansion (type
);
21628 /* The restriction on defining new types applies only to the type
21629 of the parameter, not to the default argument. */
21630 parser
->type_definition_forbidden_message
= saved_message
;
21632 /* If the next token is `=', then process a default argument. */
21633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21635 tree type
= decl_specifiers
.type
;
21636 token
= cp_lexer_peek_token (parser
->lexer
);
21637 /* If we are defining a class, then the tokens that make up the
21638 default argument must be saved and processed later. */
21639 if (!template_parm_p
&& at_class_scope_p ()
21640 && TYPE_BEING_DEFINED (current_class_type
)
21641 && !LAMBDA_TYPE_P (current_class_type
))
21642 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21644 // A constrained-type-specifier may declare a type template-parameter.
21645 else if (declares_constrained_type_template_parameter (type
))
21647 = cp_parser_default_type_template_argument (parser
);
21649 // A constrained-type-specifier may declare a template-template-parameter.
21650 else if (declares_constrained_template_template_parameter (type
))
21652 = cp_parser_default_template_template_argument (parser
);
21654 /* Outside of a class definition, we can just parse the
21655 assignment-expression. */
21658 = cp_parser_default_argument (parser
, template_parm_p
);
21660 if (!parser
->default_arg_ok_p
)
21662 permerror (token
->location
,
21663 "default arguments are only "
21664 "permitted for function parameters");
21666 else if ((declarator
&& declarator
->parameter_pack_p
)
21667 || template_parameter_pack_p
21668 || (decl_specifiers
.type
21669 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21671 /* Find the name of the parameter pack. */
21672 cp_declarator
*id_declarator
= declarator
;
21673 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21674 id_declarator
= id_declarator
->declarator
;
21676 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21677 error_at (declarator_token_start
->location
,
21679 ? G_("template parameter pack %qD "
21680 "cannot have a default argument")
21681 : G_("parameter pack %qD cannot have "
21682 "a default argument"),
21683 id_declarator
->u
.id
.unqualified_name
);
21685 error_at (declarator_token_start
->location
,
21687 ? G_("template parameter pack cannot have "
21688 "a default argument")
21689 : G_("parameter pack cannot have a "
21690 "default argument"));
21692 default_argument
= NULL_TREE
;
21696 default_argument
= NULL_TREE
;
21698 /* Generate a location for the parameter, ranging from the start of the
21699 initial token to the end of the final token (using input_location for
21700 the latter, set up by cp_lexer_set_source_position_from_token when
21703 If we have a identifier, then use it for the caret location, e.g.
21705 extern int callee (int one, int (*two)(int, int), float three);
21706 ~~~~~~^~~~~~~~~~~~~~
21708 otherwise, reuse the start location for the caret location e.g.:
21710 extern int callee (int one, int (*)(int, int), float three);
21714 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21715 ? declarator
->id_loc
21716 : decl_spec_token_start
->location
);
21717 location_t param_loc
= make_location (caret_loc
,
21718 decl_spec_token_start
->location
,
21721 return make_parameter_declarator (&decl_specifiers
,
21725 template_parameter_pack_p
);
21728 /* Parse a default argument and return it.
21730 TEMPLATE_PARM_P is true if this is a default argument for a
21731 non-type template parameter. */
21733 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21735 tree default_argument
= NULL_TREE
;
21736 bool saved_greater_than_is_operator_p
;
21737 bool saved_local_variables_forbidden_p
;
21738 bool non_constant_p
, is_direct_init
;
21740 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21742 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21743 parser
->greater_than_is_operator_p
= !template_parm_p
;
21744 /* Local variable names (and the `this' keyword) may not
21745 appear in a default argument. */
21746 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21747 parser
->local_variables_forbidden_p
= true;
21748 /* Parse the assignment-expression. */
21749 if (template_parm_p
)
21750 push_deferring_access_checks (dk_no_deferred
);
21751 tree saved_class_ptr
= NULL_TREE
;
21752 tree saved_class_ref
= NULL_TREE
;
21753 /* The "this" pointer is not valid in a default argument. */
21756 saved_class_ptr
= current_class_ptr
;
21757 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21758 saved_class_ref
= current_class_ref
;
21759 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21762 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21763 /* Restore the "this" pointer. */
21766 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21767 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21769 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21770 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21771 if (template_parm_p
)
21772 pop_deferring_access_checks ();
21773 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21774 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21776 return default_argument
;
21779 /* Parse a function-body.
21782 compound_statement */
21785 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21787 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21788 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21792 /* Parse a ctor-initializer-opt followed by a function-body. Return
21793 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21794 is true we are parsing a function-try-block. */
21797 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21798 bool in_function_try_block
)
21801 const bool check_body_p
=
21802 DECL_CONSTRUCTOR_P (current_function_decl
)
21803 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21806 /* Begin the function body. */
21807 body
= begin_function_body ();
21808 /* Parse the optional ctor-initializer. */
21809 cp_parser_ctor_initializer_opt (parser
);
21811 /* If we're parsing a constexpr constructor definition, we need
21812 to check that the constructor body is indeed empty. However,
21813 before we get to cp_parser_function_body lot of junk has been
21814 generated, so we can't just check that we have an empty block.
21815 Rather we take a snapshot of the outermost block, and check whether
21816 cp_parser_function_body changed its state. */
21819 list
= cur_stmt_list
;
21820 if (STATEMENT_LIST_TAIL (list
))
21821 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21823 /* Parse the function-body. */
21824 cp_parser_function_body (parser
, in_function_try_block
);
21826 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21827 /* Finish the function body. */
21828 finish_function_body (body
);
21831 /* Parse an initializer.
21834 = initializer-clause
21835 ( expression-list )
21837 Returns an expression representing the initializer. If no
21838 initializer is present, NULL_TREE is returned.
21840 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21841 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21842 set to TRUE if there is no initializer present. If there is an
21843 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21844 is set to true; otherwise it is set to false. */
21847 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21848 bool* non_constant_p
, bool subexpression_p
)
21853 /* Peek at the next token. */
21854 token
= cp_lexer_peek_token (parser
->lexer
);
21856 /* Let our caller know whether or not this initializer was
21858 *is_direct_init
= (token
->type
!= CPP_EQ
);
21859 /* Assume that the initializer is constant. */
21860 *non_constant_p
= false;
21862 if (token
->type
== CPP_EQ
)
21864 /* Consume the `='. */
21865 cp_lexer_consume_token (parser
->lexer
);
21866 /* Parse the initializer-clause. */
21867 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21869 else if (token
->type
== CPP_OPEN_PAREN
)
21871 vec
<tree
, va_gc
> *vec
;
21872 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21874 /*allow_expansion_p=*/true,
21877 return error_mark_node
;
21878 init
= build_tree_list_vec (vec
);
21879 release_tree_vector (vec
);
21881 else if (token
->type
== CPP_OPEN_BRACE
)
21883 cp_lexer_set_source_position (parser
->lexer
);
21884 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21885 init
= cp_parser_braced_list (parser
, non_constant_p
);
21886 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21890 /* Anything else is an error. */
21891 cp_parser_error (parser
, "expected initializer");
21892 init
= error_mark_node
;
21895 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
21896 init
= error_mark_node
;
21901 /* Parse an initializer-clause.
21903 initializer-clause:
21904 assignment-expression
21907 Returns an expression representing the initializer.
21909 If the `assignment-expression' production is used the value
21910 returned is simply a representation for the expression.
21912 Otherwise, calls cp_parser_braced_list. */
21915 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21917 cp_expr initializer
;
21919 /* Assume the expression is constant. */
21920 *non_constant_p
= false;
21922 /* If it is not a `{', then we are looking at an
21923 assignment-expression. */
21924 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21927 = cp_parser_constant_expression (parser
,
21928 /*allow_non_constant_p=*/true,
21932 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21934 return initializer
;
21937 /* Parse a brace-enclosed initializer list.
21940 { initializer-list , [opt] }
21941 { designated-initializer-list , [opt] }
21944 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21945 the elements of the initializer-list (or NULL, if the last
21946 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21947 NULL_TREE. There is no way to detect whether or not the optional
21948 trailing `,' was provided. NON_CONSTANT_P is as for
21949 cp_parser_initializer. */
21952 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21955 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21957 /* Consume the `{' token. */
21958 matching_braces braces
;
21959 braces
.require_open (parser
);
21960 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21961 initializer
= make_node (CONSTRUCTOR
);
21962 /* If it's not a `}', then there is a non-trivial initializer. */
21963 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21965 /* Parse the initializer list. */
21966 CONSTRUCTOR_ELTS (initializer
)
21967 = cp_parser_initializer_list (parser
, non_constant_p
);
21968 /* A trailing `,' token is allowed. */
21969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21970 cp_lexer_consume_token (parser
->lexer
);
21973 *non_constant_p
= false;
21974 /* Now, there should be a trailing `}'. */
21975 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21976 braces
.require_close (parser
);
21977 TREE_TYPE (initializer
) = init_list_type_node
;
21979 cp_expr
result (initializer
);
21980 /* Build a location of the form:
21983 with caret==start at the open brace, finish at the close brace. */
21984 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21985 result
.set_location (combined_loc
);
21989 /* Consume tokens up to, and including, the next non-nested closing `]'.
21990 Returns true iff we found a closing `]'. */
21993 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21995 unsigned square_depth
= 0;
21999 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22001 switch (token
->type
)
22004 case CPP_PRAGMA_EOL
:
22005 /* If we've run out of tokens, then there is no closing `]'. */
22008 case CPP_OPEN_SQUARE
:
22012 case CPP_CLOSE_SQUARE
:
22013 if (!square_depth
--)
22015 cp_lexer_consume_token (parser
->lexer
);
22024 /* Consume the token. */
22025 cp_lexer_consume_token (parser
->lexer
);
22029 /* Return true if we are looking at an array-designator, false otherwise. */
22032 cp_parser_array_designator_p (cp_parser
*parser
)
22034 /* Consume the `['. */
22035 cp_lexer_consume_token (parser
->lexer
);
22037 cp_lexer_save_tokens (parser
->lexer
);
22039 /* Skip tokens until the next token is a closing square bracket.
22040 If we find the closing `]', and the next token is a `=', then
22041 we are looking at an array designator. */
22042 bool array_designator_p
22043 = (cp_parser_skip_to_closing_square_bracket (parser
)
22044 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22046 /* Roll back the tokens we skipped. */
22047 cp_lexer_rollback_tokens (parser
->lexer
);
22049 return array_designator_p
;
22052 /* Parse an initializer-list.
22055 initializer-clause ... [opt]
22056 initializer-list , initializer-clause ... [opt]
22060 designated-initializer-list:
22061 designated-initializer-clause
22062 designated-initializer-list , designated-initializer-clause
22064 designated-initializer-clause:
22065 designator brace-or-equal-initializer
22073 designation initializer-clause ...[opt]
22074 initializer-list , designation initializer-clause ...[opt]
22079 [ constant-expression ] =
22081 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22082 for the initializer. If the INDEX of the elt is non-NULL, it is the
22083 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22084 as for cp_parser_initializer. */
22086 static vec
<constructor_elt
, va_gc
> *
22087 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22089 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22090 bool first_p
= true;
22091 tree first_designator
= NULL_TREE
;
22093 /* Assume all of the expressions are constant. */
22094 *non_constant_p
= false;
22096 /* Parse the rest of the list. */
22102 bool clause_non_constant_p
;
22103 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22105 /* Handle the C++2A syntax, '. id ='. */
22106 if ((cxx_dialect
>= cxx2a
22107 || cp_parser_allow_gnu_extensions_p (parser
))
22108 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22109 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22110 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22111 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22112 == CPP_OPEN_BRACE
)))
22114 if (cxx_dialect
< cxx2a
)
22115 pedwarn (loc
, OPT_Wpedantic
,
22116 "C++ designated initializers only available with "
22117 "-std=c++2a or -std=gnu++2a");
22118 /* Consume the `.'. */
22119 cp_lexer_consume_token (parser
->lexer
);
22120 /* Consume the identifier. */
22121 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22123 /* Consume the `='. */
22124 cp_lexer_consume_token (parser
->lexer
);
22126 /* Also, if the next token is an identifier and the following one is a
22127 colon, we are looking at the GNU designated-initializer
22129 else if (cp_parser_allow_gnu_extensions_p (parser
)
22130 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22131 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22134 /* Warn the user that they are using an extension. */
22135 pedwarn (loc
, OPT_Wpedantic
,
22136 "ISO C++ does not allow GNU designated initializers");
22137 /* Consume the identifier. */
22138 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22139 /* Consume the `:'. */
22140 cp_lexer_consume_token (parser
->lexer
);
22142 /* Also handle C99 array designators, '[ const ] ='. */
22143 else if (cp_parser_allow_gnu_extensions_p (parser
)
22144 && !c_dialect_objc ()
22145 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22147 /* In C++11, [ could start a lambda-introducer. */
22148 bool non_const
= false;
22150 cp_parser_parse_tentatively (parser
);
22152 if (!cp_parser_array_designator_p (parser
))
22154 cp_parser_simulate_error (parser
);
22155 designator
= NULL_TREE
;
22159 designator
= cp_parser_constant_expression (parser
, true,
22161 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22162 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22165 if (!cp_parser_parse_definitely (parser
))
22166 designator
= NULL_TREE
;
22168 && (!require_potential_rvalue_constant_expression
22170 designator
= NULL_TREE
;
22172 /* Warn the user that they are using an extension. */
22173 pedwarn (loc
, OPT_Wpedantic
,
22174 "ISO C++ does not allow C99 designated initializers");
22177 designator
= NULL_TREE
;
22181 first_designator
= designator
;
22184 else if (cxx_dialect
>= cxx2a
22185 && first_designator
!= error_mark_node
22186 && (!first_designator
!= !designator
))
22188 error_at (loc
, "either all initializer clauses should be designated "
22189 "or none of them should be");
22190 first_designator
= error_mark_node
;
22192 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22193 first_designator
= designator
;
22195 /* Parse the initializer. */
22196 initializer
= cp_parser_initializer_clause (parser
,
22197 &clause_non_constant_p
);
22198 /* If any clause is non-constant, so is the entire initializer. */
22199 if (clause_non_constant_p
)
22200 *non_constant_p
= true;
22202 /* If we have an ellipsis, this is an initializer pack
22204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22206 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22208 /* Consume the `...'. */
22209 cp_lexer_consume_token (parser
->lexer
);
22211 if (designator
&& cxx_dialect
>= cxx2a
)
22213 "%<...%> not allowed in designated initializer list");
22215 /* Turn the initializer into an initializer expansion. */
22216 initializer
= make_pack_expansion (initializer
);
22219 /* Add it to the vector. */
22220 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22222 /* If the next token is not a comma, we have reached the end of
22224 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22227 /* Peek at the next token. */
22228 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22229 /* If the next token is a `}', then we're still done. An
22230 initializer-clause can have a trailing `,' after the
22231 initializer-list and before the closing `}'. */
22232 if (token
->type
== CPP_CLOSE_BRACE
)
22235 /* Consume the `,' token. */
22236 cp_lexer_consume_token (parser
->lexer
);
22239 /* The same identifier shall not appear in multiple designators
22240 of a designated-initializer-list. */
22241 if (first_designator
)
22244 tree designator
, val
;
22245 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22246 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22248 if (IDENTIFIER_MARKED (designator
))
22250 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22251 "%<.%s%> designator used multiple times in "
22252 "the same initializer list",
22253 IDENTIFIER_POINTER (designator
));
22254 (*v
)[i
].index
= NULL_TREE
;
22257 IDENTIFIER_MARKED (designator
) = 1;
22259 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22260 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22261 IDENTIFIER_MARKED (designator
) = 0;
22267 /* Classes [gram.class] */
22269 /* Parse a class-name.
22275 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22276 to indicate that names looked up in dependent types should be
22277 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22278 keyword has been used to indicate that the name that appears next
22279 is a template. TAG_TYPE indicates the explicit tag given before
22280 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22281 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22282 is the class being defined in a class-head. If ENUM_OK is TRUE,
22283 enum-names are also accepted.
22285 Returns the TYPE_DECL representing the class. */
22288 cp_parser_class_name (cp_parser
*parser
,
22289 bool typename_keyword_p
,
22290 bool template_keyword_p
,
22291 enum tag_types tag_type
,
22292 bool check_dependency_p
,
22294 bool is_declaration
,
22301 tree identifier
= NULL_TREE
;
22303 /* All class-names start with an identifier. */
22304 token
= cp_lexer_peek_token (parser
->lexer
);
22305 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22307 cp_parser_error (parser
, "expected class-name");
22308 return error_mark_node
;
22311 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22312 to a template-id, so we save it here. */
22313 scope
= parser
->scope
;
22314 if (scope
== error_mark_node
)
22315 return error_mark_node
;
22317 /* Any name names a type if we're following the `typename' keyword
22318 in a qualified name where the enclosing scope is type-dependent. */
22319 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22320 && dependent_type_p (scope
));
22321 /* Handle the common case (an identifier, but not a template-id)
22323 if (token
->type
== CPP_NAME
22324 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22326 cp_token
*identifier_token
;
22329 /* Look for the identifier. */
22330 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22331 ambiguous_p
= identifier_token
->error_reported
;
22332 identifier
= cp_parser_identifier (parser
);
22333 /* If the next token isn't an identifier, we are certainly not
22334 looking at a class-name. */
22335 if (identifier
== error_mark_node
)
22336 decl
= error_mark_node
;
22337 /* If we know this is a type-name, there's no need to look it
22339 else if (typename_p
)
22343 tree ambiguous_decls
;
22344 /* If we already know that this lookup is ambiguous, then
22345 we've already issued an error message; there's no reason
22349 cp_parser_simulate_error (parser
);
22350 return error_mark_node
;
22352 /* If the next token is a `::', then the name must be a type
22355 [basic.lookup.qual]
22357 During the lookup for a name preceding the :: scope
22358 resolution operator, object, function, and enumerator
22359 names are ignored. */
22360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22361 tag_type
= scope_type
;
22362 /* Look up the name. */
22363 decl
= cp_parser_lookup_name (parser
, identifier
,
22365 /*is_template=*/false,
22366 /*is_namespace=*/false,
22367 check_dependency_p
,
22369 identifier_token
->location
);
22370 if (ambiguous_decls
)
22372 if (cp_parser_parsing_tentatively (parser
))
22373 cp_parser_simulate_error (parser
);
22374 return error_mark_node
;
22380 /* Try a template-id. */
22381 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22382 check_dependency_p
,
22385 if (decl
== error_mark_node
)
22386 return error_mark_node
;
22389 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22391 /* If this is a typename, create a TYPENAME_TYPE. */
22392 if (typename_p
&& decl
!= error_mark_node
)
22394 decl
= make_typename_type (scope
, decl
, typename_type
,
22395 /*complain=*/tf_error
);
22396 if (decl
!= error_mark_node
)
22397 decl
= TYPE_NAME (decl
);
22400 decl
= strip_using_decl (decl
);
22402 /* Check to see that it is really the name of a class. */
22403 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22404 && identifier_p (TREE_OPERAND (decl
, 0))
22405 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22406 /* Situations like this:
22408 template <typename T> struct A {
22409 typename T::template X<int>::I i;
22412 are problematic. Is `T::template X<int>' a class-name? The
22413 standard does not seem to be definitive, but there is no other
22414 valid interpretation of the following `::'. Therefore, those
22415 names are considered class-names. */
22417 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22418 if (decl
!= error_mark_node
)
22419 decl
= TYPE_NAME (decl
);
22421 else if (TREE_CODE (decl
) != TYPE_DECL
22422 || TREE_TYPE (decl
) == error_mark_node
22423 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22424 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22425 /* In Objective-C 2.0, a classname followed by '.' starts a
22426 dot-syntax expression, and it's not a type-name. */
22427 || (c_dialect_objc ()
22428 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22429 && objc_is_class_name (decl
)))
22430 decl
= error_mark_node
;
22432 if (decl
== error_mark_node
)
22433 cp_parser_error (parser
, "expected class-name");
22434 else if (identifier
&& !parser
->scope
)
22435 maybe_note_name_used_in_class (identifier
, decl
);
22440 /* Parse a class-specifier.
22443 class-head { member-specification [opt] }
22445 Returns the TREE_TYPE representing the class. */
22448 cp_parser_class_specifier_1 (cp_parser
* parser
)
22451 tree attributes
= NULL_TREE
;
22452 bool nested_name_specifier_p
;
22453 unsigned saved_num_template_parameter_lists
;
22454 bool saved_in_function_body
;
22455 unsigned char in_statement
;
22456 bool in_switch_statement_p
;
22457 bool saved_in_unbraced_linkage_specification_p
;
22458 tree old_scope
= NULL_TREE
;
22459 tree scope
= NULL_TREE
;
22460 cp_token
*closing_brace
;
22462 push_deferring_access_checks (dk_no_deferred
);
22464 /* Parse the class-head. */
22465 type
= cp_parser_class_head (parser
,
22466 &nested_name_specifier_p
);
22467 /* If the class-head was a semantic disaster, skip the entire body
22471 cp_parser_skip_to_end_of_block_or_statement (parser
);
22472 pop_deferring_access_checks ();
22473 return error_mark_node
;
22476 /* Look for the `{'. */
22477 matching_braces braces
;
22478 if (!braces
.require_open (parser
))
22480 pop_deferring_access_checks ();
22481 return error_mark_node
;
22484 cp_ensure_no_omp_declare_simd (parser
);
22485 cp_ensure_no_oacc_routine (parser
);
22487 /* Issue an error message if type-definitions are forbidden here. */
22488 cp_parser_check_type_definition (parser
);
22489 /* Remember that we are defining one more class. */
22490 ++parser
->num_classes_being_defined
;
22491 /* Inside the class, surrounding template-parameter-lists do not
22493 saved_num_template_parameter_lists
22494 = parser
->num_template_parameter_lists
;
22495 parser
->num_template_parameter_lists
= 0;
22496 /* We are not in a function body. */
22497 saved_in_function_body
= parser
->in_function_body
;
22498 parser
->in_function_body
= false;
22499 /* Or in a loop. */
22500 in_statement
= parser
->in_statement
;
22501 parser
->in_statement
= 0;
22502 /* Or in a switch. */
22503 in_switch_statement_p
= parser
->in_switch_statement_p
;
22504 parser
->in_switch_statement_p
= false;
22505 /* We are not immediately inside an extern "lang" block. */
22506 saved_in_unbraced_linkage_specification_p
22507 = parser
->in_unbraced_linkage_specification_p
;
22508 parser
->in_unbraced_linkage_specification_p
= false;
22510 // Associate constraints with the type.
22512 type
= associate_classtype_constraints (type
);
22514 /* Start the class. */
22515 if (nested_name_specifier_p
)
22517 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22518 old_scope
= push_inner_scope (scope
);
22520 type
= begin_class_definition (type
);
22522 if (type
== error_mark_node
)
22523 /* If the type is erroneous, skip the entire body of the class. */
22524 cp_parser_skip_to_closing_brace (parser
);
22526 /* Parse the member-specification. */
22527 cp_parser_member_specification_opt (parser
);
22529 /* Look for the trailing `}'. */
22530 closing_brace
= braces
.require_close (parser
);
22531 /* Look for trailing attributes to apply to this class. */
22532 if (cp_parser_allow_gnu_extensions_p (parser
))
22533 attributes
= cp_parser_gnu_attributes_opt (parser
);
22534 if (type
!= error_mark_node
)
22535 type
= finish_struct (type
, attributes
);
22536 if (nested_name_specifier_p
)
22537 pop_inner_scope (old_scope
, scope
);
22539 /* We've finished a type definition. Check for the common syntax
22540 error of forgetting a semicolon after the definition. We need to
22541 be careful, as we can't just check for not-a-semicolon and be done
22542 with it; the user might have typed:
22544 class X { } c = ...;
22545 class X { } *p = ...;
22547 and so forth. Instead, enumerate all the possible tokens that
22548 might follow this production; if we don't see one of them, then
22549 complain and silently insert the semicolon. */
22551 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22552 bool want_semicolon
= true;
22554 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22555 /* Don't try to parse c++11 attributes here. As per the
22556 grammar, that should be a task for
22557 cp_parser_decl_specifier_seq. */
22558 want_semicolon
= false;
22560 switch (token
->type
)
22563 case CPP_SEMICOLON
:
22566 case CPP_OPEN_PAREN
:
22567 case CPP_CLOSE_PAREN
:
22569 want_semicolon
= false;
22572 /* While it's legal for type qualifiers and storage class
22573 specifiers to follow type definitions in the grammar, only
22574 compiler testsuites contain code like that. Assume that if
22575 we see such code, then what we're really seeing is a case
22579 const <type> var = ...;
22584 static <type> func (...) ...
22586 i.e. the qualifier or specifier applies to the next
22587 declaration. To do so, however, we need to look ahead one
22588 more token to see if *that* token is a type specifier.
22590 This code could be improved to handle:
22593 static const <type> var = ...; */
22595 if (keyword_is_decl_specifier (token
->keyword
))
22597 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22599 /* Handling user-defined types here would be nice, but very
22602 = (lookahead
->type
== CPP_KEYWORD
22603 && keyword_begins_type_specifier (lookahead
->keyword
));
22610 /* If we don't have a type, then something is very wrong and we
22611 shouldn't try to do anything clever. Likewise for not seeing the
22613 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22615 /* Locate the closing brace. */
22616 cp_token_position prev
22617 = cp_lexer_previous_token_position (parser
->lexer
);
22618 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22619 location_t loc
= prev_token
->location
;
22621 /* We want to suggest insertion of a ';' immediately *after* the
22622 closing brace, so, if we can, offset the location by 1 column. */
22623 location_t next_loc
= loc
;
22624 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22625 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22627 rich_location
richloc (line_table
, next_loc
);
22629 /* If we successfully offset the location, suggest the fix-it. */
22630 if (next_loc
!= loc
)
22631 richloc
.add_fixit_insert_before (next_loc
, ";");
22633 if (CLASSTYPE_DECLARED_CLASS (type
))
22634 error_at (&richloc
,
22635 "expected %<;%> after class definition");
22636 else if (TREE_CODE (type
) == RECORD_TYPE
)
22637 error_at (&richloc
,
22638 "expected %<;%> after struct definition");
22639 else if (TREE_CODE (type
) == UNION_TYPE
)
22640 error_at (&richloc
,
22641 "expected %<;%> after union definition");
22643 gcc_unreachable ();
22645 /* Unget one token and smash it to look as though we encountered
22646 a semicolon in the input stream. */
22647 cp_lexer_set_token_position (parser
->lexer
, prev
);
22648 token
= cp_lexer_peek_token (parser
->lexer
);
22649 token
->type
= CPP_SEMICOLON
;
22650 token
->keyword
= RID_MAX
;
22654 /* If this class is not itself within the scope of another class,
22655 then we need to parse the bodies of all of the queued function
22656 definitions. Note that the queued functions defined in a class
22657 are not always processed immediately following the
22658 class-specifier for that class. Consider:
22661 struct B { void f() { sizeof (A); } };
22664 If `f' were processed before the processing of `A' were
22665 completed, there would be no way to compute the size of `A'.
22666 Note that the nesting we are interested in here is lexical --
22667 not the semantic nesting given by TYPE_CONTEXT. In particular,
22670 struct A { struct B; };
22671 struct A::B { void f() { } };
22673 there is no need to delay the parsing of `A::B::f'. */
22674 if (--parser
->num_classes_being_defined
== 0)
22677 tree class_type
= NULL_TREE
;
22678 tree pushed_scope
= NULL_TREE
;
22680 cp_default_arg_entry
*e
;
22681 tree save_ccp
, save_ccr
;
22683 if (any_erroneous_template_args_p (type
))
22685 /* Skip default arguments, NSDMIs, etc, in order to improve
22686 error recovery (c++/71169, c++/71832). */
22687 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22688 vec_safe_truncate (unparsed_nsdmis
, 0);
22689 vec_safe_truncate (unparsed_classes
, 0);
22690 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22693 /* In a first pass, parse default arguments to the functions.
22694 Then, in a second pass, parse the bodies of the functions.
22695 This two-phased approach handles cases like:
22703 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22706 /* If there are default arguments that have not yet been processed,
22707 take care of them now. */
22708 if (class_type
!= e
->class_type
)
22711 pop_scope (pushed_scope
);
22712 class_type
= e
->class_type
;
22713 pushed_scope
= push_scope (class_type
);
22715 /* Make sure that any template parameters are in scope. */
22716 maybe_begin_member_template_processing (decl
);
22717 /* Parse the default argument expressions. */
22718 cp_parser_late_parsing_default_args (parser
, decl
);
22719 /* Remove any template parameters from the symbol table. */
22720 maybe_end_member_template_processing ();
22722 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22723 /* Now parse any NSDMIs. */
22724 save_ccp
= current_class_ptr
;
22725 save_ccr
= current_class_ref
;
22726 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22728 if (class_type
!= DECL_CONTEXT (decl
))
22731 pop_scope (pushed_scope
);
22732 class_type
= DECL_CONTEXT (decl
);
22733 pushed_scope
= push_scope (class_type
);
22735 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22736 cp_parser_late_parsing_nsdmi (parser
, decl
);
22738 vec_safe_truncate (unparsed_nsdmis
, 0);
22739 current_class_ptr
= save_ccp
;
22740 current_class_ref
= save_ccr
;
22742 pop_scope (pushed_scope
);
22744 /* Now do some post-NSDMI bookkeeping. */
22745 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22746 after_nsdmi_defaulted_late_checks (class_type
);
22747 vec_safe_truncate (unparsed_classes
, 0);
22748 after_nsdmi_defaulted_late_checks (type
);
22750 /* Now parse the body of the functions. */
22753 /* OpenMP UDRs need to be parsed before all other functions. */
22754 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22755 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22756 cp_parser_late_parsing_for_member (parser
, decl
);
22757 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22758 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22759 cp_parser_late_parsing_for_member (parser
, decl
);
22762 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22763 cp_parser_late_parsing_for_member (parser
, decl
);
22764 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22767 vec_safe_push (unparsed_classes
, type
);
22769 /* Put back any saved access checks. */
22770 pop_deferring_access_checks ();
22772 /* Restore saved state. */
22773 parser
->in_switch_statement_p
= in_switch_statement_p
;
22774 parser
->in_statement
= in_statement
;
22775 parser
->in_function_body
= saved_in_function_body
;
22776 parser
->num_template_parameter_lists
22777 = saved_num_template_parameter_lists
;
22778 parser
->in_unbraced_linkage_specification_p
22779 = saved_in_unbraced_linkage_specification_p
;
22785 cp_parser_class_specifier (cp_parser
* parser
)
22788 timevar_push (TV_PARSE_STRUCT
);
22789 ret
= cp_parser_class_specifier_1 (parser
);
22790 timevar_pop (TV_PARSE_STRUCT
);
22794 /* Parse a class-head.
22797 class-key identifier [opt] base-clause [opt]
22798 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22799 class-key nested-name-specifier [opt] template-id
22802 class-virt-specifier:
22806 class-key attributes identifier [opt] base-clause [opt]
22807 class-key attributes nested-name-specifier identifier base-clause [opt]
22808 class-key attributes nested-name-specifier [opt] template-id
22811 Upon return BASES is initialized to the list of base classes (or
22812 NULL, if there are none) in the same form returned by
22813 cp_parser_base_clause.
22815 Returns the TYPE of the indicated class. Sets
22816 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22817 involving a nested-name-specifier was used, and FALSE otherwise.
22819 Returns error_mark_node if this is not a class-head.
22821 Returns NULL_TREE if the class-head is syntactically valid, but
22822 semantically invalid in a way that means we should skip the entire
22823 body of the class. */
22826 cp_parser_class_head (cp_parser
* parser
,
22827 bool* nested_name_specifier_p
)
22829 tree nested_name_specifier
;
22830 enum tag_types class_key
;
22831 tree id
= NULL_TREE
;
22832 tree type
= NULL_TREE
;
22835 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22836 bool template_id_p
= false;
22837 bool qualified_p
= false;
22838 bool invalid_nested_name_p
= false;
22839 bool invalid_explicit_specialization_p
= false;
22840 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22841 tree pushed_scope
= NULL_TREE
;
22842 unsigned num_templates
;
22843 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22844 /* Assume no nested-name-specifier will be present. */
22845 *nested_name_specifier_p
= false;
22846 /* Assume no template parameter lists will be used in defining the
22849 parser
->colon_corrects_to_scope_p
= false;
22851 /* Look for the class-key. */
22852 class_key
= cp_parser_class_key (parser
);
22853 if (class_key
== none_type
)
22854 return error_mark_node
;
22856 location_t class_head_start_location
= input_location
;
22858 /* Parse the attributes. */
22859 attributes
= cp_parser_attributes_opt (parser
);
22861 /* If the next token is `::', that is invalid -- but sometimes
22862 people do try to write:
22866 Handle this gracefully by accepting the extra qualifier, and then
22867 issuing an error about it later if this really is a
22868 class-head. If it turns out just to be an elaborated type
22869 specifier, remain silent. */
22870 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22871 qualified_p
= true;
22873 push_deferring_access_checks (dk_no_check
);
22875 /* Determine the name of the class. Begin by looking for an
22876 optional nested-name-specifier. */
22877 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22878 nested_name_specifier
22879 = cp_parser_nested_name_specifier_opt (parser
,
22880 /*typename_keyword_p=*/false,
22881 /*check_dependency_p=*/false,
22883 /*is_declaration=*/false);
22884 /* If there was a nested-name-specifier, then there *must* be an
22887 cp_token
*bad_template_keyword
= NULL
;
22889 if (nested_name_specifier
)
22891 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22892 /* Although the grammar says `identifier', it really means
22893 `class-name' or `template-name'. You are only allowed to
22894 define a class that has already been declared with this
22897 The proposed resolution for Core Issue 180 says that wherever
22898 you see `class T::X' you should treat `X' as a type-name.
22900 It is OK to define an inaccessible class; for example:
22902 class A { class B; };
22905 We do not know if we will see a class-name, or a
22906 template-name. We look for a class-name first, in case the
22907 class-name is a template-id; if we looked for the
22908 template-name first we would stop after the template-name. */
22909 cp_parser_parse_tentatively (parser
);
22910 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22911 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22912 type
= cp_parser_class_name (parser
,
22913 /*typename_keyword_p=*/false,
22914 /*template_keyword_p=*/false,
22916 /*check_dependency_p=*/false,
22917 /*class_head_p=*/true,
22918 /*is_declaration=*/false);
22919 /* If that didn't work, ignore the nested-name-specifier. */
22920 if (!cp_parser_parse_definitely (parser
))
22922 invalid_nested_name_p
= true;
22923 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22924 id
= cp_parser_identifier (parser
);
22925 if (id
== error_mark_node
)
22928 /* If we could not find a corresponding TYPE, treat this
22929 declaration like an unqualified declaration. */
22930 if (type
== error_mark_node
)
22931 nested_name_specifier
= NULL_TREE
;
22932 /* Otherwise, count the number of templates used in TYPE and its
22933 containing scopes. */
22938 for (scope
= TREE_TYPE (type
);
22939 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22940 scope
= get_containing_scope (scope
))
22942 && CLASS_TYPE_P (scope
)
22943 && CLASSTYPE_TEMPLATE_INFO (scope
)
22944 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22945 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22946 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22950 /* Otherwise, the identifier is optional. */
22953 /* We don't know whether what comes next is a template-id,
22954 an identifier, or nothing at all. */
22955 cp_parser_parse_tentatively (parser
);
22956 /* Check for a template-id. */
22957 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22958 id
= cp_parser_template_id (parser
,
22959 /*template_keyword_p=*/false,
22960 /*check_dependency_p=*/true,
22962 /*is_declaration=*/true);
22963 /* If that didn't work, it could still be an identifier. */
22964 if (!cp_parser_parse_definitely (parser
))
22966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22968 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22969 id
= cp_parser_identifier (parser
);
22976 template_id_p
= true;
22981 pop_deferring_access_checks ();
22985 cp_parser_check_for_invalid_template_id (parser
, id
,
22987 type_start_token
->location
);
22989 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22991 /* If it's not a `:' or a `{' then we can't really be looking at a
22992 class-head, since a class-head only appears as part of a
22993 class-specifier. We have to detect this situation before calling
22994 xref_tag, since that has irreversible side-effects. */
22995 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22997 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22998 type
= error_mark_node
;
23002 /* At this point, we're going ahead with the class-specifier, even
23003 if some other problem occurs. */
23004 cp_parser_commit_to_tentative_parse (parser
);
23005 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23007 cp_parser_error (parser
,
23008 "cannot specify %<override%> for a class");
23009 type
= error_mark_node
;
23012 /* Issue the error about the overly-qualified name now. */
23015 cp_parser_error (parser
,
23016 "global qualification of class name is invalid");
23017 type
= error_mark_node
;
23020 else if (invalid_nested_name_p
)
23022 cp_parser_error (parser
,
23023 "qualified name does not name a class");
23024 type
= error_mark_node
;
23027 else if (nested_name_specifier
)
23031 if (bad_template_keyword
)
23032 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23033 keyword template shall not appear at the top level. */
23034 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23035 "keyword %<template%> not allowed in class-head-name");
23037 /* Reject typedef-names in class heads. */
23038 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23040 error_at (type_start_token
->location
,
23041 "invalid class name in declaration of %qD",
23047 /* Figure out in what scope the declaration is being placed. */
23048 scope
= current_scope ();
23049 /* If that scope does not contain the scope in which the
23050 class was originally declared, the program is invalid. */
23051 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23053 if (at_namespace_scope_p ())
23054 error_at (type_start_token
->location
,
23055 "declaration of %qD in namespace %qD which does not "
23057 type
, scope
, nested_name_specifier
);
23059 error_at (type_start_token
->location
,
23060 "declaration of %qD in %qD which does not enclose %qD",
23061 type
, scope
, nested_name_specifier
);
23067 A declarator-id shall not be qualified except for the
23068 definition of a ... nested class outside of its class
23069 ... [or] the definition or explicit instantiation of a
23070 class member of a namespace outside of its namespace. */
23071 if (scope
== nested_name_specifier
)
23073 permerror (nested_name_specifier_token_start
->location
,
23074 "extra qualification not allowed");
23075 nested_name_specifier
= NULL_TREE
;
23079 /* An explicit-specialization must be preceded by "template <>". If
23080 it is not, try to recover gracefully. */
23081 if (at_namespace_scope_p ()
23082 && parser
->num_template_parameter_lists
== 0
23083 && !processing_template_parmlist
23086 /* Build a location of this form:
23087 struct typename <ARGS>
23088 ^~~~~~~~~~~~~~~~~~~~~~
23089 with caret==start at the start token, and
23090 finishing at the end of the type. */
23091 location_t reported_loc
23092 = make_location (class_head_start_location
,
23093 class_head_start_location
,
23094 get_finish (type_start_token
->location
));
23095 rich_location
richloc (line_table
, reported_loc
);
23096 richloc
.add_fixit_insert_before (class_head_start_location
,
23098 error_at (&richloc
,
23099 "an explicit specialization must be preceded by"
23100 " %<template <>%>");
23101 invalid_explicit_specialization_p
= true;
23102 /* Take the same action that would have been taken by
23103 cp_parser_explicit_specialization. */
23104 ++parser
->num_template_parameter_lists
;
23105 begin_specialization ();
23107 /* There must be no "return" statements between this point and the
23108 end of this function; set "type "to the correct return value and
23109 use "goto done;" to return. */
23110 /* Make sure that the right number of template parameters were
23112 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23114 type_start_token
->location
,
23115 /*declarator=*/NULL
))
23117 /* If something went wrong, there is no point in even trying to
23118 process the class-definition. */
23123 /* Look up the type. */
23126 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23127 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23128 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23130 error_at (type_start_token
->location
,
23131 "function template %qD redeclared as a class template", id
);
23132 type
= error_mark_node
;
23136 type
= TREE_TYPE (id
);
23137 type
= maybe_process_partial_specialization (type
);
23139 /* Check the scope while we still know whether or not we had a
23140 nested-name-specifier. */
23141 if (type
!= error_mark_node
)
23142 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23144 if (nested_name_specifier
)
23145 pushed_scope
= push_scope (nested_name_specifier
);
23147 else if (nested_name_specifier
)
23153 template <typename T> struct S { struct T };
23154 template <typename T> struct S<T>::T { };
23156 we will get a TYPENAME_TYPE when processing the definition of
23157 `S::T'. We need to resolve it to the actual type before we
23158 try to define it. */
23159 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23161 class_type
= resolve_typename_type (TREE_TYPE (type
),
23162 /*only_current_p=*/false);
23163 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23164 type
= TYPE_NAME (class_type
);
23167 cp_parser_error (parser
, "could not resolve typename type");
23168 type
= error_mark_node
;
23172 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23173 == error_mark_node
)
23179 class_type
= current_class_type
;
23180 /* Enter the scope indicated by the nested-name-specifier. */
23181 pushed_scope
= push_scope (nested_name_specifier
);
23182 /* Get the canonical version of this type. */
23183 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23184 /* Call push_template_decl if it seems like we should be defining a
23185 template either from the template headers or the type we're
23186 defining, so that we diagnose both extra and missing headers. */
23187 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23188 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23189 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23191 type
= push_template_decl (type
);
23192 if (type
== error_mark_node
)
23199 type
= TREE_TYPE (type
);
23200 *nested_name_specifier_p
= true;
23202 else /* The name is not a nested name. */
23204 /* If the class was unnamed, create a dummy name. */
23206 id
= make_anon_name ();
23207 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23208 ? ts_within_enclosing_non_class
23210 type
= xref_tag (class_key
, id
, tag_scope
,
23211 parser
->num_template_parameter_lists
);
23214 /* Indicate whether this class was declared as a `class' or as a
23216 if (TREE_CODE (type
) == RECORD_TYPE
)
23217 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23218 cp_parser_check_class_key (class_key
, type
);
23220 /* If this type was already complete, and we see another definition,
23221 that's an error. */
23222 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23224 error_at (type_start_token
->location
, "redefinition of %q#T",
23226 inform (location_of (type
), "previous definition of %q#T",
23231 else if (type
== error_mark_node
)
23236 /* Apply attributes now, before any use of the class as a template
23237 argument in its base list. */
23238 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23239 fixup_attribute_variants (type
);
23242 /* We will have entered the scope containing the class; the names of
23243 base classes should be looked up in that context. For example:
23245 struct A { struct B {}; struct C; };
23246 struct A::C : B {};
23250 /* Get the list of base-classes, if there is one. */
23251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23253 /* PR59482: enter the class scope so that base-specifiers are looked
23257 bases
= cp_parser_base_clause (parser
);
23258 /* PR59482: get out of the previously pushed class scope so that the
23259 subsequent pops pop the right thing. */
23266 /* If we're really defining a class, process the base classes.
23267 If they're invalid, fail. */
23268 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23269 xref_basetypes (type
, bases
);
23272 /* Leave the scope given by the nested-name-specifier. We will
23273 enter the class scope itself while processing the members. */
23275 pop_scope (pushed_scope
);
23277 if (invalid_explicit_specialization_p
)
23279 end_specialization ();
23280 --parser
->num_template_parameter_lists
;
23284 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23285 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23286 CLASSTYPE_FINAL (type
) = 1;
23288 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23292 /* Parse a class-key.
23299 Returns the kind of class-key specified, or none_type to indicate
23302 static enum tag_types
23303 cp_parser_class_key (cp_parser
* parser
)
23306 enum tag_types tag_type
;
23308 /* Look for the class-key. */
23309 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23313 /* Check to see if the TOKEN is a class-key. */
23314 tag_type
= cp_parser_token_is_class_key (token
);
23316 cp_parser_error (parser
, "expected class-key");
23320 /* Parse a type-parameter-key.
23322 type-parameter-key:
23328 cp_parser_type_parameter_key (cp_parser
* parser
)
23330 /* Look for the type-parameter-key. */
23331 enum tag_types tag_type
= none_type
;
23332 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23333 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23335 cp_lexer_consume_token (parser
->lexer
);
23336 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23337 /* typename is not allowed in a template template parameter
23338 by the standard until C++17. */
23339 pedwarn (token
->location
, OPT_Wpedantic
,
23340 "ISO C++ forbids typename key in template template parameter;"
23341 " use -std=c++17 or -std=gnu++17");
23344 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23349 /* Parse an (optional) member-specification.
23351 member-specification:
23352 member-declaration member-specification [opt]
23353 access-specifier : member-specification [opt] */
23356 cp_parser_member_specification_opt (cp_parser
* parser
)
23363 /* Peek at the next token. */
23364 token
= cp_lexer_peek_token (parser
->lexer
);
23365 /* If it's a `}', or EOF then we've seen all the members. */
23366 if (token
->type
== CPP_CLOSE_BRACE
23367 || token
->type
== CPP_EOF
23368 || token
->type
== CPP_PRAGMA_EOL
)
23371 /* See if this token is a keyword. */
23372 keyword
= token
->keyword
;
23376 case RID_PROTECTED
:
23378 /* Consume the access-specifier. */
23379 cp_lexer_consume_token (parser
->lexer
);
23380 /* Remember which access-specifier is active. */
23381 current_access_specifier
= token
->u
.value
;
23382 /* Look for the `:'. */
23383 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23387 /* Accept #pragmas at class scope. */
23388 if (token
->type
== CPP_PRAGMA
)
23390 cp_parser_pragma (parser
, pragma_member
, NULL
);
23394 /* Otherwise, the next construction must be a
23395 member-declaration. */
23396 cp_parser_member_declaration (parser
);
23401 /* Parse a member-declaration.
23403 member-declaration:
23404 decl-specifier-seq [opt] member-declarator-list [opt] ;
23405 function-definition ; [opt]
23406 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23408 template-declaration
23411 member-declarator-list:
23413 member-declarator-list , member-declarator
23416 declarator pure-specifier [opt]
23417 declarator constant-initializer [opt]
23418 identifier [opt] : constant-expression
23422 member-declaration:
23423 __extension__ member-declaration
23426 declarator attributes [opt] pure-specifier [opt]
23427 declarator attributes [opt] constant-initializer [opt]
23428 identifier [opt] attributes [opt] : constant-expression
23432 member-declaration:
23433 static_assert-declaration */
23436 cp_parser_member_declaration (cp_parser
* parser
)
23438 cp_decl_specifier_seq decl_specifiers
;
23439 tree prefix_attributes
;
23441 int declares_class_or_enum
;
23443 cp_token
*token
= NULL
;
23444 cp_token
*decl_spec_token_start
= NULL
;
23445 cp_token
*initializer_token_start
= NULL
;
23446 int saved_pedantic
;
23447 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23449 /* Check for the `__extension__' keyword. */
23450 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23453 cp_parser_member_declaration (parser
);
23454 /* Restore the old value of the PEDANTIC flag. */
23455 pedantic
= saved_pedantic
;
23460 /* Check for a template-declaration. */
23461 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23463 /* An explicit specialization here is an error condition, and we
23464 expect the specialization handler to detect and report this. */
23465 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23466 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23467 cp_parser_explicit_specialization (parser
);
23469 cp_parser_template_declaration (parser
, /*member_p=*/true);
23473 /* Check for a template introduction. */
23474 else if (cp_parser_template_declaration_after_export (parser
, true))
23477 /* Check for a using-declaration. */
23478 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23480 if (cxx_dialect
< cxx11
)
23482 /* Parse the using-declaration. */
23483 cp_parser_using_declaration (parser
,
23484 /*access_declaration_p=*/false);
23490 bool alias_decl_expected
;
23491 cp_parser_parse_tentatively (parser
);
23492 decl
= cp_parser_alias_declaration (parser
);
23493 /* Note that if we actually see the '=' token after the
23494 identifier, cp_parser_alias_declaration commits the
23495 tentative parse. In that case, we really expect an
23496 alias-declaration. Otherwise, we expect a using
23498 alias_decl_expected
=
23499 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23500 cp_parser_parse_definitely (parser
);
23502 if (alias_decl_expected
)
23503 finish_member_declaration (decl
);
23505 cp_parser_using_declaration (parser
,
23506 /*access_declaration_p=*/false);
23511 /* Check for @defs. */
23512 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23515 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23516 ivar
= ivar_chains
;
23520 ivar
= TREE_CHAIN (member
);
23521 TREE_CHAIN (member
) = NULL_TREE
;
23522 finish_member_declaration (member
);
23527 /* If the next token is `static_assert' we have a static assertion. */
23528 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23530 cp_parser_static_assert (parser
, /*member_p=*/true);
23534 parser
->colon_corrects_to_scope_p
= false;
23536 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23539 /* Parse the decl-specifier-seq. */
23540 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23541 cp_parser_decl_specifier_seq (parser
,
23542 CP_PARSER_FLAGS_OPTIONAL
,
23544 &declares_class_or_enum
);
23545 /* Check for an invalid type-name. */
23546 if (!decl_specifiers
.any_type_specifiers_p
23547 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23549 /* If there is no declarator, then the decl-specifier-seq should
23551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23553 /* If there was no decl-specifier-seq, and the next token is a
23554 `;', then we have something like:
23560 Each member-declaration shall declare at least one member
23561 name of the class. */
23562 if (!decl_specifiers
.any_specifiers_p
)
23564 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23565 if (!in_system_header_at (token
->location
))
23567 gcc_rich_location
richloc (token
->location
);
23568 richloc
.add_fixit_remove ();
23569 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23576 /* See if this declaration is a friend. */
23577 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23578 /* If there were decl-specifiers, check to see if there was
23579 a class-declaration. */
23580 type
= check_tag_decl (&decl_specifiers
,
23581 /*explicit_type_instantiation_p=*/false);
23582 /* Nested classes have already been added to the class, but
23583 a `friend' needs to be explicitly registered. */
23586 /* If the `friend' keyword was present, the friend must
23587 be introduced with a class-key. */
23588 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23589 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23590 "in C++03 a class-key must be used "
23591 "when declaring a friend");
23594 template <typename T> struct A {
23595 friend struct A<T>::B;
23598 A<T>::B will be represented by a TYPENAME_TYPE, and
23599 therefore not recognized by check_tag_decl. */
23602 type
= decl_specifiers
.type
;
23603 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23604 type
= TREE_TYPE (type
);
23606 if (!type
|| !TYPE_P (type
))
23607 error_at (decl_spec_token_start
->location
,
23608 "friend declaration does not name a class or "
23611 make_friend_class (current_class_type
, type
,
23612 /*complain=*/true);
23614 /* If there is no TYPE, an error message will already have
23616 else if (!type
|| type
== error_mark_node
)
23618 /* An anonymous aggregate has to be handled specially; such
23619 a declaration really declares a data member (with a
23620 particular type), as opposed to a nested class. */
23621 else if (ANON_AGGR_TYPE_P (type
))
23624 if (decl_specifiers
.storage_class
!= sc_none
)
23625 error_at (decl_spec_token_start
->location
,
23626 "a storage class on an anonymous aggregate "
23627 "in class scope is not allowed");
23629 /* Remove constructors and such from TYPE, now that we
23630 know it is an anonymous aggregate. */
23631 fixup_anonymous_aggr (type
);
23632 /* And make the corresponding data member. */
23633 decl
= build_decl (decl_spec_token_start
->location
,
23634 FIELD_DECL
, NULL_TREE
, type
);
23635 /* Add it to the class. */
23636 finish_member_declaration (decl
);
23639 cp_parser_check_access_in_redeclaration
23641 decl_spec_token_start
->location
);
23646 bool assume_semicolon
= false;
23648 /* Clear attributes from the decl_specifiers but keep them
23649 around as prefix attributes that apply them to the entity
23651 prefix_attributes
= decl_specifiers
.attributes
;
23652 decl_specifiers
.attributes
= NULL_TREE
;
23654 /* See if these declarations will be friends. */
23655 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23657 /* Keep going until we hit the `;' at the end of the
23659 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23661 tree attributes
= NULL_TREE
;
23662 tree first_attribute
;
23664 bool named_bitfld
= false;
23666 /* Peek at the next token. */
23667 token
= cp_lexer_peek_token (parser
->lexer
);
23669 /* The following code wants to know early if it is a bit-field
23670 or some other declaration. Attributes can appear before
23671 the `:' token. Skip over them without consuming any tokens
23672 to peek if they are followed by `:'. */
23673 if (cp_next_tokens_can_be_attribute_p (parser
)
23674 || (token
->type
== CPP_NAME
23675 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23676 && (named_bitfld
= true)))
23679 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23680 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23683 /* Check for a bitfield declaration. */
23684 if (token
->type
== CPP_COLON
23685 || (token
->type
== CPP_NAME
23686 && token
== cp_lexer_peek_token (parser
->lexer
)
23687 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23688 && (named_bitfld
= true)))
23692 tree late_attributes
= NULL_TREE
;
23695 identifier
= cp_parser_identifier (parser
);
23697 identifier
= NULL_TREE
;
23699 /* Look for attributes that apply to the bitfield. */
23700 attributes
= cp_parser_attributes_opt (parser
);
23702 /* Consume the `:' token. */
23703 cp_lexer_consume_token (parser
->lexer
);
23705 /* Get the width of the bitfield. */
23706 width
= cp_parser_constant_expression (parser
, false, NULL
,
23707 cxx_dialect
>= cxx11
);
23709 /* In C++2A and as extension for C++11 and above we allow
23710 default member initializers for bit-fields. */
23711 initializer
= NULL_TREE
;
23712 if (cxx_dialect
>= cxx11
23713 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23714 || cp_lexer_next_token_is (parser
->lexer
,
23718 = cp_lexer_peek_token (parser
->lexer
)->location
;
23719 if (cxx_dialect
< cxx2a
23720 && !in_system_header_at (loc
)
23721 && identifier
!= NULL_TREE
)
23723 "default member initializers for bit-fields "
23724 "only available with -std=c++2a or "
23727 initializer
= cp_parser_save_nsdmi (parser
);
23728 if (identifier
== NULL_TREE
)
23730 error_at (loc
, "default member initializer for "
23731 "unnamed bit-field");
23732 initializer
= NULL_TREE
;
23737 /* Look for attributes that apply to the bitfield after
23738 the `:' token and width. This is where GCC used to
23739 parse attributes in the past, pedwarn if there is
23740 a std attribute. */
23741 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23742 pedwarn (input_location
, OPT_Wpedantic
,
23743 "ISO C++ allows bit-field attributes only "
23744 "before the %<:%> token");
23746 late_attributes
= cp_parser_attributes_opt (parser
);
23749 attributes
= attr_chainon (attributes
, late_attributes
);
23751 /* Remember which attributes are prefix attributes and
23753 first_attribute
= attributes
;
23754 /* Combine the attributes. */
23755 attributes
= attr_chainon (prefix_attributes
, attributes
);
23757 /* Create the bitfield declaration. */
23758 decl
= grokbitfield (identifier
23759 ? make_id_declarator (NULL_TREE
,
23764 width
, initializer
,
23769 cp_declarator
*declarator
;
23770 tree asm_specification
;
23771 int ctor_dtor_or_conv_p
;
23773 /* Parse the declarator. */
23775 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23776 &ctor_dtor_or_conv_p
,
23777 /*parenthesized_p=*/NULL
,
23781 /* If something went wrong parsing the declarator, make sure
23782 that we at least consume some tokens. */
23783 if (declarator
== cp_error_declarator
)
23785 /* Skip to the end of the statement. */
23786 cp_parser_skip_to_end_of_statement (parser
);
23787 /* If the next token is not a semicolon, that is
23788 probably because we just skipped over the body of
23789 a function. So, we consume a semicolon if
23790 present, but do not issue an error message if it
23792 if (cp_lexer_next_token_is (parser
->lexer
,
23794 cp_lexer_consume_token (parser
->lexer
);
23798 if (declares_class_or_enum
& 2)
23799 cp_parser_check_for_definition_in_return_type
23800 (declarator
, decl_specifiers
.type
,
23801 decl_specifiers
.locations
[ds_type_spec
]);
23803 /* Look for an asm-specification. */
23804 asm_specification
= cp_parser_asm_specification_opt (parser
);
23805 /* Look for attributes that apply to the declaration. */
23806 attributes
= cp_parser_attributes_opt (parser
);
23807 /* Remember which attributes are prefix attributes and
23809 first_attribute
= attributes
;
23810 /* Combine the attributes. */
23811 attributes
= attr_chainon (prefix_attributes
, attributes
);
23813 /* If it's an `=', then we have a constant-initializer or a
23814 pure-specifier. It is not correct to parse the
23815 initializer before registering the member declaration
23816 since the member declaration should be in scope while
23817 its initializer is processed. However, the rest of the
23818 front end does not yet provide an interface that allows
23819 us to handle this correctly. */
23820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23824 A pure-specifier shall be used only in the declaration of
23825 a virtual function.
23827 A member-declarator can contain a constant-initializer
23828 only if it declares a static member of integral or
23831 Therefore, if the DECLARATOR is for a function, we look
23832 for a pure-specifier; otherwise, we look for a
23833 constant-initializer. When we call `grokfield', it will
23834 perform more stringent semantics checks. */
23835 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23836 if (function_declarator_p (declarator
)
23837 || (decl_specifiers
.type
23838 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23839 && declarator
->kind
== cdk_id
23840 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23841 == FUNCTION_TYPE
)))
23842 initializer
= cp_parser_pure_specifier (parser
);
23843 else if (decl_specifiers
.storage_class
!= sc_static
)
23844 initializer
= cp_parser_save_nsdmi (parser
);
23845 else if (cxx_dialect
>= cxx11
)
23848 /* Don't require a constant rvalue in C++11, since we
23849 might want a reference constant. We'll enforce
23850 constancy later. */
23851 cp_lexer_consume_token (parser
->lexer
);
23852 /* Parse the initializer. */
23853 initializer
= cp_parser_initializer_clause (parser
,
23857 /* Parse the initializer. */
23858 initializer
= cp_parser_constant_initializer (parser
);
23860 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23861 && !function_declarator_p (declarator
))
23864 if (decl_specifiers
.storage_class
!= sc_static
)
23865 initializer
= cp_parser_save_nsdmi (parser
);
23867 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23869 /* Otherwise, there is no initializer. */
23871 initializer
= NULL_TREE
;
23873 /* See if we are probably looking at a function
23874 definition. We are certainly not looking at a
23875 member-declarator. Calling `grokfield' has
23876 side-effects, so we must not do it unless we are sure
23877 that we are looking at a member-declarator. */
23878 if (cp_parser_token_starts_function_definition_p
23879 (cp_lexer_peek_token (parser
->lexer
)))
23881 /* The grammar does not allow a pure-specifier to be
23882 used when a member function is defined. (It is
23883 possible that this fact is an oversight in the
23884 standard, since a pure function may be defined
23885 outside of the class-specifier. */
23886 if (initializer
&& initializer_token_start
)
23887 error_at (initializer_token_start
->location
,
23888 "pure-specifier on function-definition");
23889 decl
= cp_parser_save_member_function_body (parser
,
23893 if (parser
->fully_implicit_function_template_p
)
23894 decl
= finish_fully_implicit_template (parser
, decl
);
23895 /* If the member was not a friend, declare it here. */
23897 finish_member_declaration (decl
);
23898 /* Peek at the next token. */
23899 token
= cp_lexer_peek_token (parser
->lexer
);
23900 /* If the next token is a semicolon, consume it. */
23901 if (token
->type
== CPP_SEMICOLON
)
23903 location_t semicolon_loc
23904 = cp_lexer_consume_token (parser
->lexer
)->location
;
23905 gcc_rich_location
richloc (semicolon_loc
);
23906 richloc
.add_fixit_remove ();
23907 warning_at (&richloc
, OPT_Wextra_semi
,
23908 "extra %<;%> after in-class "
23909 "function definition");
23914 if (declarator
->kind
== cdk_function
)
23915 declarator
->id_loc
= token
->location
;
23916 /* Create the declaration. */
23917 decl
= grokfield (declarator
, &decl_specifiers
,
23918 initializer
, /*init_const_expr_p=*/true,
23919 asm_specification
, attributes
);
23920 if (parser
->fully_implicit_function_template_p
)
23923 finish_fully_implicit_template (parser
, 0);
23925 decl
= finish_fully_implicit_template (parser
, decl
);
23929 cp_finalize_omp_declare_simd (parser
, decl
);
23930 cp_finalize_oacc_routine (parser
, decl
, false);
23932 /* Reset PREFIX_ATTRIBUTES. */
23933 if (attributes
!= error_mark_node
)
23935 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23936 attributes
= TREE_CHAIN (attributes
);
23938 TREE_CHAIN (attributes
) = NULL_TREE
;
23941 /* If there is any qualification still in effect, clear it
23942 now; we will be starting fresh with the next declarator. */
23943 parser
->scope
= NULL_TREE
;
23944 parser
->qualifying_scope
= NULL_TREE
;
23945 parser
->object_scope
= NULL_TREE
;
23946 /* If it's a `,', then there are more declarators. */
23947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23949 cp_lexer_consume_token (parser
->lexer
);
23950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23952 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23953 gcc_rich_location
richloc (token
->location
);
23954 richloc
.add_fixit_remove ();
23955 error_at (&richloc
, "stray %<,%> at end of "
23956 "member declaration");
23959 /* If the next token isn't a `;', then we have a parse error. */
23960 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23963 /* The next token might be a ways away from where the
23964 actual semicolon is missing. Find the previous token
23965 and use that for our error position. */
23966 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23967 gcc_rich_location
richloc (token
->location
);
23968 richloc
.add_fixit_insert_after (";");
23969 error_at (&richloc
, "expected %<;%> at end of "
23970 "member declaration");
23972 /* Assume that the user meant to provide a semicolon. If
23973 we were to cp_parser_skip_to_end_of_statement, we might
23974 skip to a semicolon inside a member function definition
23975 and issue nonsensical error messages. */
23976 assume_semicolon
= true;
23981 /* Add DECL to the list of members. */
23983 /* Explicitly include, eg, NSDMIs, for better error
23984 recovery (c++/58650). */
23985 || !DECL_DECLARES_FUNCTION_P (decl
))
23986 finish_member_declaration (decl
);
23988 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23989 cp_parser_save_default_args (parser
, decl
);
23990 else if (TREE_CODE (decl
) == FIELD_DECL
23991 && DECL_INITIAL (decl
))
23992 /* Add DECL to the queue of NSDMI to be parsed later. */
23993 vec_safe_push (unparsed_nsdmis
, decl
);
23996 if (assume_semicolon
)
24001 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24003 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24006 /* Parse a pure-specifier.
24011 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24012 Otherwise, ERROR_MARK_NODE is returned. */
24015 cp_parser_pure_specifier (cp_parser
* parser
)
24019 /* Look for the `=' token. */
24020 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24021 return error_mark_node
;
24022 /* Look for the `0' token. */
24023 token
= cp_lexer_peek_token (parser
->lexer
);
24025 if (token
->type
== CPP_EOF
24026 || token
->type
== CPP_PRAGMA_EOL
)
24027 return error_mark_node
;
24029 cp_lexer_consume_token (parser
->lexer
);
24031 /* Accept = default or = delete in c++0x mode. */
24032 if (token
->keyword
== RID_DEFAULT
24033 || token
->keyword
== RID_DELETE
)
24035 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24036 return token
->u
.value
;
24039 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24040 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24042 cp_parser_error (parser
,
24043 "invalid pure specifier (only %<= 0%> is allowed)");
24044 cp_parser_skip_to_end_of_statement (parser
);
24045 return error_mark_node
;
24047 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24049 error_at (token
->location
, "templates may not be %<virtual%>");
24050 return error_mark_node
;
24053 return integer_zero_node
;
24056 /* Parse a constant-initializer.
24058 constant-initializer:
24059 = constant-expression
24061 Returns a representation of the constant-expression. */
24064 cp_parser_constant_initializer (cp_parser
* parser
)
24066 /* Look for the `=' token. */
24067 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24068 return error_mark_node
;
24070 /* It is invalid to write:
24072 struct S { static const int i = { 7 }; };
24075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24077 cp_parser_error (parser
,
24078 "a brace-enclosed initializer is not allowed here");
24079 /* Consume the opening brace. */
24080 matching_braces braces
;
24081 braces
.consume_open (parser
);
24082 /* Skip the initializer. */
24083 cp_parser_skip_to_closing_brace (parser
);
24084 /* Look for the trailing `}'. */
24085 braces
.require_close (parser
);
24087 return error_mark_node
;
24090 return cp_parser_constant_expression (parser
);
24093 /* Derived classes [gram.class.derived] */
24095 /* Parse a base-clause.
24098 : base-specifier-list
24100 base-specifier-list:
24101 base-specifier ... [opt]
24102 base-specifier-list , base-specifier ... [opt]
24104 Returns a TREE_LIST representing the base-classes, in the order in
24105 which they were declared. The representation of each node is as
24106 described by cp_parser_base_specifier.
24108 In the case that no bases are specified, this function will return
24109 NULL_TREE, not ERROR_MARK_NODE. */
24112 cp_parser_base_clause (cp_parser
* parser
)
24114 tree bases
= NULL_TREE
;
24116 /* Look for the `:' that begins the list. */
24117 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24119 /* Scan the base-specifier-list. */
24124 bool pack_expansion_p
= false;
24126 /* Look for the base-specifier. */
24127 base
= cp_parser_base_specifier (parser
);
24128 /* Look for the (optional) ellipsis. */
24129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24131 /* Consume the `...'. */
24132 cp_lexer_consume_token (parser
->lexer
);
24134 pack_expansion_p
= true;
24137 /* Add BASE to the front of the list. */
24138 if (base
&& base
!= error_mark_node
)
24140 if (pack_expansion_p
)
24141 /* Make this a pack expansion type. */
24142 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24144 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24146 TREE_CHAIN (base
) = bases
;
24150 /* Peek at the next token. */
24151 token
= cp_lexer_peek_token (parser
->lexer
);
24152 /* If it's not a comma, then the list is complete. */
24153 if (token
->type
!= CPP_COMMA
)
24155 /* Consume the `,'. */
24156 cp_lexer_consume_token (parser
->lexer
);
24159 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24160 base class had a qualified name. However, the next name that
24161 appears is certainly not qualified. */
24162 parser
->scope
= NULL_TREE
;
24163 parser
->qualifying_scope
= NULL_TREE
;
24164 parser
->object_scope
= NULL_TREE
;
24166 return nreverse (bases
);
24169 /* Parse a base-specifier.
24172 :: [opt] nested-name-specifier [opt] class-name
24173 virtual access-specifier [opt] :: [opt] nested-name-specifier
24175 access-specifier virtual [opt] :: [opt] nested-name-specifier
24178 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24179 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24180 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24181 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24184 cp_parser_base_specifier (cp_parser
* parser
)
24188 bool virtual_p
= false;
24189 bool duplicate_virtual_error_issued_p
= false;
24190 bool duplicate_access_error_issued_p
= false;
24191 bool class_scope_p
, template_p
;
24192 tree access
= access_default_node
;
24195 /* Process the optional `virtual' and `access-specifier'. */
24198 /* Peek at the next token. */
24199 token
= cp_lexer_peek_token (parser
->lexer
);
24200 /* Process `virtual'. */
24201 switch (token
->keyword
)
24204 /* If `virtual' appears more than once, issue an error. */
24205 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24207 cp_parser_error (parser
,
24208 "%<virtual%> specified more than once in base-specifier");
24209 duplicate_virtual_error_issued_p
= true;
24214 /* Consume the `virtual' token. */
24215 cp_lexer_consume_token (parser
->lexer
);
24220 case RID_PROTECTED
:
24222 /* If more than one access specifier appears, issue an
24224 if (access
!= access_default_node
24225 && !duplicate_access_error_issued_p
)
24227 cp_parser_error (parser
,
24228 "more than one access specifier in base-specifier");
24229 duplicate_access_error_issued_p
= true;
24232 access
= ridpointers
[(int) token
->keyword
];
24234 /* Consume the access-specifier. */
24235 cp_lexer_consume_token (parser
->lexer
);
24244 /* It is not uncommon to see programs mechanically, erroneously, use
24245 the 'typename' keyword to denote (dependent) qualified types
24246 as base classes. */
24247 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24249 token
= cp_lexer_peek_token (parser
->lexer
);
24250 if (!processing_template_decl
)
24251 error_at (token
->location
,
24252 "keyword %<typename%> not allowed outside of templates");
24254 error_at (token
->location
,
24255 "keyword %<typename%> not allowed in this context "
24256 "(the base class is implicitly a type)");
24257 cp_lexer_consume_token (parser
->lexer
);
24260 /* Look for the optional `::' operator. */
24261 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24262 /* Look for the nested-name-specifier. The simplest way to
24267 The keyword `typename' is not permitted in a base-specifier or
24268 mem-initializer; in these contexts a qualified name that
24269 depends on a template-parameter is implicitly assumed to be a
24272 is to pretend that we have seen the `typename' keyword at this
24274 cp_parser_nested_name_specifier_opt (parser
,
24275 /*typename_keyword_p=*/true,
24276 /*check_dependency_p=*/true,
24278 /*is_declaration=*/true);
24279 /* If the base class is given by a qualified name, assume that names
24280 we see are type names or templates, as appropriate. */
24281 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24282 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24285 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24286 /* DR 950 allows decltype as a base-specifier. */
24287 type
= cp_parser_decltype (parser
);
24290 /* Otherwise, look for the class-name. */
24291 type
= cp_parser_class_name (parser
,
24295 /*check_dependency_p=*/true,
24296 /*class_head_p=*/false,
24297 /*is_declaration=*/true);
24298 type
= TREE_TYPE (type
);
24301 if (type
== error_mark_node
)
24302 return error_mark_node
;
24304 return finish_base_specifier (type
, access
, virtual_p
);
24307 /* Exception handling [gram.exception] */
24309 /* Parse an (optional) noexcept-specification.
24311 noexcept-specification:
24312 noexcept ( constant-expression ) [opt]
24314 If no noexcept-specification is present, returns NULL_TREE.
24315 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24316 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24317 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24318 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24319 in which case a boolean condition is returned instead. */
24322 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24323 bool require_constexpr
,
24324 bool* consumed_expr
,
24328 const char *saved_message
;
24330 /* Peek at the next token. */
24331 token
= cp_lexer_peek_token (parser
->lexer
);
24333 /* Is it a noexcept-specification? */
24334 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24337 cp_lexer_consume_token (parser
->lexer
);
24339 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24341 matching_parens parens
;
24342 parens
.consume_open (parser
);
24344 if (require_constexpr
)
24346 /* Types may not be defined in an exception-specification. */
24347 saved_message
= parser
->type_definition_forbidden_message
;
24348 parser
->type_definition_forbidden_message
24349 = G_("types may not be defined in an exception-specification");
24351 expr
= cp_parser_constant_expression (parser
);
24353 /* Restore the saved message. */
24354 parser
->type_definition_forbidden_message
= saved_message
;
24358 expr
= cp_parser_expression (parser
);
24359 *consumed_expr
= true;
24362 parens
.require_close (parser
);
24366 expr
= boolean_true_node
;
24367 if (!require_constexpr
)
24368 *consumed_expr
= false;
24371 /* We cannot build a noexcept-spec right away because this will check
24372 that expr is a constexpr. */
24374 return build_noexcept_spec (expr
, tf_warning_or_error
);
24382 /* Parse an (optional) exception-specification.
24384 exception-specification:
24385 throw ( type-id-list [opt] )
24387 Returns a TREE_LIST representing the exception-specification. The
24388 TREE_VALUE of each node is a type. */
24391 cp_parser_exception_specification_opt (cp_parser
* parser
)
24395 const char *saved_message
;
24397 /* Peek at the next token. */
24398 token
= cp_lexer_peek_token (parser
->lexer
);
24400 /* Is it a noexcept-specification? */
24401 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24403 if (type_id_list
!= NULL_TREE
)
24404 return type_id_list
;
24406 /* If it's not `throw', then there's no exception-specification. */
24407 if (!cp_parser_is_keyword (token
, RID_THROW
))
24410 location_t loc
= token
->location
;
24412 /* Consume the `throw'. */
24413 cp_lexer_consume_token (parser
->lexer
);
24415 /* Look for the `('. */
24416 matching_parens parens
;
24417 parens
.require_open (parser
);
24419 /* Peek at the next token. */
24420 token
= cp_lexer_peek_token (parser
->lexer
);
24421 /* If it's not a `)', then there is a type-id-list. */
24422 if (token
->type
!= CPP_CLOSE_PAREN
)
24424 /* Types may not be defined in an exception-specification. */
24425 saved_message
= parser
->type_definition_forbidden_message
;
24426 parser
->type_definition_forbidden_message
24427 = G_("types may not be defined in an exception-specification");
24428 /* Parse the type-id-list. */
24429 type_id_list
= cp_parser_type_id_list (parser
);
24430 /* Restore the saved message. */
24431 parser
->type_definition_forbidden_message
= saved_message
;
24433 if (cxx_dialect
>= cxx17
)
24435 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24437 type_id_list
= NULL_TREE
;
24439 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24440 warning_at (loc
, OPT_Wdeprecated
,
24441 "dynamic exception specifications are deprecated in "
24444 /* In C++17, throw() is equivalent to noexcept (true). throw()
24445 is deprecated in C++11 and above as well, but is still widely used,
24446 so don't warn about it yet. */
24447 else if (cxx_dialect
>= cxx17
)
24448 type_id_list
= noexcept_true_spec
;
24450 type_id_list
= empty_except_spec
;
24452 /* Look for the `)'. */
24453 parens
.require_close (parser
);
24455 return type_id_list
;
24458 /* Parse an (optional) type-id-list.
24462 type-id-list , type-id ... [opt]
24464 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24465 in the order that the types were presented. */
24468 cp_parser_type_id_list (cp_parser
* parser
)
24470 tree types
= NULL_TREE
;
24477 token
= cp_lexer_peek_token (parser
->lexer
);
24479 /* Get the next type-id. */
24480 type
= cp_parser_type_id (parser
);
24481 /* Check for invalid 'auto'. */
24482 if (flag_concepts
&& type_uses_auto (type
))
24484 error_at (token
->location
,
24485 "invalid use of %<auto%> in exception-specification");
24486 type
= error_mark_node
;
24488 /* Parse the optional ellipsis. */
24489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24491 /* Consume the `...'. */
24492 cp_lexer_consume_token (parser
->lexer
);
24494 /* Turn the type into a pack expansion expression. */
24495 type
= make_pack_expansion (type
);
24497 /* Add it to the list. */
24498 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24499 /* Peek at the next token. */
24500 token
= cp_lexer_peek_token (parser
->lexer
);
24501 /* If it is not a `,', we are done. */
24502 if (token
->type
!= CPP_COMMA
)
24504 /* Consume the `,'. */
24505 cp_lexer_consume_token (parser
->lexer
);
24508 return nreverse (types
);
24511 /* Parse a try-block.
24514 try compound-statement handler-seq */
24517 cp_parser_try_block (cp_parser
* parser
)
24521 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24522 if (parser
->in_function_body
24523 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24524 error ("%<try%> in %<constexpr%> function");
24526 try_block
= begin_try_block ();
24527 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24528 finish_try_block (try_block
);
24529 cp_parser_handler_seq (parser
);
24530 finish_handler_sequence (try_block
);
24535 /* Parse a function-try-block.
24537 function-try-block:
24538 try ctor-initializer [opt] function-body handler-seq */
24541 cp_parser_function_try_block (cp_parser
* parser
)
24543 tree compound_stmt
;
24546 /* Look for the `try' keyword. */
24547 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24549 /* Let the rest of the front end know where we are. */
24550 try_block
= begin_function_try_block (&compound_stmt
);
24551 /* Parse the function-body. */
24552 cp_parser_ctor_initializer_opt_and_function_body
24553 (parser
, /*in_function_try_block=*/true);
24554 /* We're done with the `try' part. */
24555 finish_function_try_block (try_block
);
24556 /* Parse the handlers. */
24557 cp_parser_handler_seq (parser
);
24558 /* We're done with the handlers. */
24559 finish_function_handler_sequence (try_block
, compound_stmt
);
24562 /* Parse a handler-seq.
24565 handler handler-seq [opt] */
24568 cp_parser_handler_seq (cp_parser
* parser
)
24574 /* Parse the handler. */
24575 cp_parser_handler (parser
);
24576 /* Peek at the next token. */
24577 token
= cp_lexer_peek_token (parser
->lexer
);
24578 /* If it's not `catch' then there are no more handlers. */
24579 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24584 /* Parse a handler.
24587 catch ( exception-declaration ) compound-statement */
24590 cp_parser_handler (cp_parser
* parser
)
24595 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24596 handler
= begin_handler ();
24597 matching_parens parens
;
24598 parens
.require_open (parser
);
24599 declaration
= cp_parser_exception_declaration (parser
);
24600 finish_handler_parms (declaration
, handler
);
24601 parens
.require_close (parser
);
24602 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24603 finish_handler (handler
);
24606 /* Parse an exception-declaration.
24608 exception-declaration:
24609 type-specifier-seq declarator
24610 type-specifier-seq abstract-declarator
24614 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24615 ellipsis variant is used. */
24618 cp_parser_exception_declaration (cp_parser
* parser
)
24620 cp_decl_specifier_seq type_specifiers
;
24621 cp_declarator
*declarator
;
24622 const char *saved_message
;
24624 /* If it's an ellipsis, it's easy to handle. */
24625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24627 /* Consume the `...' token. */
24628 cp_lexer_consume_token (parser
->lexer
);
24632 /* Types may not be defined in exception-declarations. */
24633 saved_message
= parser
->type_definition_forbidden_message
;
24634 parser
->type_definition_forbidden_message
24635 = G_("types may not be defined in exception-declarations");
24637 /* Parse the type-specifier-seq. */
24638 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24639 /*is_trailing_return=*/false,
24641 /* If it's a `)', then there is no declarator. */
24642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24645 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24646 /*ctor_dtor_or_conv_p=*/NULL
,
24647 /*parenthesized_p=*/NULL
,
24648 /*member_p=*/false,
24649 /*friend_p=*/false);
24651 /* Restore the saved message. */
24652 parser
->type_definition_forbidden_message
= saved_message
;
24654 if (!type_specifiers
.any_specifiers_p
)
24655 return error_mark_node
;
24657 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24660 /* Parse a throw-expression.
24663 throw assignment-expression [opt]
24665 Returns a THROW_EXPR representing the throw-expression. */
24668 cp_parser_throw_expression (cp_parser
* parser
)
24673 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24674 token
= cp_lexer_peek_token (parser
->lexer
);
24675 /* Figure out whether or not there is an assignment-expression
24676 following the "throw" keyword. */
24677 if (token
->type
== CPP_COMMA
24678 || token
->type
== CPP_SEMICOLON
24679 || token
->type
== CPP_CLOSE_PAREN
24680 || token
->type
== CPP_CLOSE_SQUARE
24681 || token
->type
== CPP_CLOSE_BRACE
24682 || token
->type
== CPP_COLON
)
24683 expression
= NULL_TREE
;
24685 expression
= cp_parser_assignment_expression (parser
);
24687 return build_throw (expression
);
24690 /* GNU Extensions */
24692 /* Parse an (optional) asm-specification.
24695 asm ( string-literal )
24697 If the asm-specification is present, returns a STRING_CST
24698 corresponding to the string-literal. Otherwise, returns
24702 cp_parser_asm_specification_opt (cp_parser
* parser
)
24705 tree asm_specification
;
24707 /* Peek at the next token. */
24708 token
= cp_lexer_peek_token (parser
->lexer
);
24709 /* If the next token isn't the `asm' keyword, then there's no
24710 asm-specification. */
24711 if (!cp_parser_is_keyword (token
, RID_ASM
))
24714 /* Consume the `asm' token. */
24715 cp_lexer_consume_token (parser
->lexer
);
24716 /* Look for the `('. */
24717 matching_parens parens
;
24718 parens
.require_open (parser
);
24720 /* Look for the string-literal. */
24721 asm_specification
= cp_parser_string_literal (parser
, false, false);
24723 /* Look for the `)'. */
24724 parens
.require_close (parser
);
24726 return asm_specification
;
24729 /* Parse an asm-operand-list.
24733 asm-operand-list , asm-operand
24736 string-literal ( expression )
24737 [ string-literal ] string-literal ( expression )
24739 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24740 each node is the expression. The TREE_PURPOSE is itself a
24741 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24742 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24743 is a STRING_CST for the string literal before the parenthesis. Returns
24744 ERROR_MARK_NODE if any of the operands are invalid. */
24747 cp_parser_asm_operand_list (cp_parser
* parser
)
24749 tree asm_operands
= NULL_TREE
;
24750 bool invalid_operands
= false;
24754 tree string_literal
;
24758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24760 /* Consume the `[' token. */
24761 cp_lexer_consume_token (parser
->lexer
);
24762 /* Read the operand name. */
24763 name
= cp_parser_identifier (parser
);
24764 if (name
!= error_mark_node
)
24765 name
= build_string (IDENTIFIER_LENGTH (name
),
24766 IDENTIFIER_POINTER (name
));
24767 /* Look for the closing `]'. */
24768 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24772 /* Look for the string-literal. */
24773 string_literal
= cp_parser_string_literal (parser
, false, false);
24775 /* Look for the `('. */
24776 matching_parens parens
;
24777 parens
.require_open (parser
);
24778 /* Parse the expression. */
24779 expression
= cp_parser_expression (parser
);
24780 /* Look for the `)'. */
24781 parens
.require_close (parser
);
24783 if (name
== error_mark_node
24784 || string_literal
== error_mark_node
24785 || expression
== error_mark_node
)
24786 invalid_operands
= true;
24788 /* Add this operand to the list. */
24789 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24792 /* If the next token is not a `,', there are no more
24794 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24796 /* Consume the `,'. */
24797 cp_lexer_consume_token (parser
->lexer
);
24800 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24803 /* Parse an asm-clobber-list.
24807 asm-clobber-list , string-literal
24809 Returns a TREE_LIST, indicating the clobbers in the order that they
24810 appeared. The TREE_VALUE of each node is a STRING_CST. */
24813 cp_parser_asm_clobber_list (cp_parser
* parser
)
24815 tree clobbers
= NULL_TREE
;
24819 tree string_literal
;
24821 /* Look for the string literal. */
24822 string_literal
= cp_parser_string_literal (parser
, false, false);
24823 /* Add it to the list. */
24824 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24825 /* If the next token is not a `,', then the list is
24827 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24829 /* Consume the `,' token. */
24830 cp_lexer_consume_token (parser
->lexer
);
24836 /* Parse an asm-label-list.
24840 asm-label-list , identifier
24842 Returns a TREE_LIST, indicating the labels in the order that they
24843 appeared. The TREE_VALUE of each node is a label. */
24846 cp_parser_asm_label_list (cp_parser
* parser
)
24848 tree labels
= NULL_TREE
;
24852 tree identifier
, label
, name
;
24854 /* Look for the identifier. */
24855 identifier
= cp_parser_identifier (parser
);
24856 if (!error_operand_p (identifier
))
24858 label
= lookup_label (identifier
);
24859 if (TREE_CODE (label
) == LABEL_DECL
)
24861 TREE_USED (label
) = 1;
24862 check_goto (label
);
24863 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24864 IDENTIFIER_POINTER (identifier
));
24865 labels
= tree_cons (name
, label
, labels
);
24868 /* If the next token is not a `,', then the list is
24870 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24872 /* Consume the `,' token. */
24873 cp_lexer_consume_token (parser
->lexer
);
24876 return nreverse (labels
);
24879 /* Return TRUE iff the next tokens in the stream are possibly the
24880 beginning of a GNU extension attribute. */
24883 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24885 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24888 /* Return TRUE iff the next tokens in the stream are possibly the
24889 beginning of a standard C++-11 attribute specifier. */
24892 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24894 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24897 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24898 beginning of a standard C++-11 attribute specifier. */
24901 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24903 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24905 return (cxx_dialect
>= cxx11
24906 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24907 || (token
->type
== CPP_OPEN_SQUARE
24908 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24909 && token
->type
== CPP_OPEN_SQUARE
)));
24912 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24913 beginning of a GNU extension attribute. */
24916 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24918 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24920 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24923 /* Return true iff the next tokens can be the beginning of either a
24924 GNU attribute list, or a standard C++11 attribute sequence. */
24927 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24929 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24930 || cp_next_tokens_can_be_std_attribute_p (parser
));
24933 /* Return true iff the next Nth tokens can be the beginning of either
24934 a GNU attribute list, or a standard C++11 attribute sequence. */
24937 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24939 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24940 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24943 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24944 of GNU attributes, or return NULL. */
24947 cp_parser_attributes_opt (cp_parser
*parser
)
24949 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24950 return cp_parser_gnu_attributes_opt (parser
);
24951 return cp_parser_std_attribute_spec_seq (parser
);
24954 /* Parse an (optional) series of attributes.
24957 attributes attribute
24960 __attribute__ (( attribute-list [opt] ))
24962 The return value is as for cp_parser_gnu_attribute_list. */
24965 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24967 tree attributes
= NULL_TREE
;
24969 temp_override
<bool> cleanup
24970 (parser
->auto_is_implicit_function_template_parm_p
, false);
24975 tree attribute_list
;
24978 /* Peek at the next token. */
24979 token
= cp_lexer_peek_token (parser
->lexer
);
24980 /* If it's not `__attribute__', then we're done. */
24981 if (token
->keyword
!= RID_ATTRIBUTE
)
24984 /* Consume the `__attribute__' keyword. */
24985 cp_lexer_consume_token (parser
->lexer
);
24986 /* Look for the two `(' tokens. */
24987 matching_parens outer_parens
;
24988 outer_parens
.require_open (parser
);
24989 matching_parens inner_parens
;
24990 inner_parens
.require_open (parser
);
24992 /* Peek at the next token. */
24993 token
= cp_lexer_peek_token (parser
->lexer
);
24994 if (token
->type
!= CPP_CLOSE_PAREN
)
24995 /* Parse the attribute-list. */
24996 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24998 /* If the next token is a `)', then there is no attribute
25000 attribute_list
= NULL
;
25002 /* Look for the two `)' tokens. */
25003 if (!inner_parens
.require_close (parser
))
25005 if (!outer_parens
.require_close (parser
))
25008 cp_parser_skip_to_end_of_statement (parser
);
25010 /* Add these new attributes to the list. */
25011 attributes
= attr_chainon (attributes
, attribute_list
);
25017 /* Parse a GNU attribute-list.
25021 attribute-list , attribute
25025 identifier ( identifier )
25026 identifier ( identifier , expression-list )
25027 identifier ( expression-list )
25029 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25030 to an attribute. The TREE_PURPOSE of each node is the identifier
25031 indicating which attribute is in use. The TREE_VALUE represents
25032 the arguments, if any. */
25035 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25037 tree attribute_list
= NULL_TREE
;
25038 bool save_translate_strings_p
= parser
->translate_strings_p
;
25040 parser
->translate_strings_p
= false;
25047 /* Look for the identifier. We also allow keywords here; for
25048 example `__attribute__ ((const))' is legal. */
25049 token
= cp_lexer_peek_token (parser
->lexer
);
25050 if (token
->type
== CPP_NAME
25051 || token
->type
== CPP_KEYWORD
)
25053 tree arguments
= NULL_TREE
;
25055 /* Consume the token, but save it since we need it for the
25056 SIMD enabled function parsing. */
25057 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25059 /* Save away the identifier that indicates which attribute
25061 identifier
= (token
->type
== CPP_KEYWORD
)
25062 /* For keywords, use the canonical spelling, not the
25063 parsed identifier. */
25064 ? ridpointers
[(int) token
->keyword
]
25065 : id_token
->u
.value
;
25067 identifier
= canonicalize_attr_name (identifier
);
25068 attribute
= build_tree_list (identifier
, NULL_TREE
);
25070 /* Peek at the next token. */
25071 token
= cp_lexer_peek_token (parser
->lexer
);
25072 /* If it's an `(', then parse the attribute arguments. */
25073 if (token
->type
== CPP_OPEN_PAREN
)
25075 vec
<tree
, va_gc
> *vec
;
25076 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25077 ? id_attr
: normal_attr
);
25078 vec
= cp_parser_parenthesized_expression_list
25079 (parser
, attr_flag
, /*cast_p=*/false,
25080 /*allow_expansion_p=*/false,
25081 /*non_constant_p=*/NULL
);
25083 arguments
= error_mark_node
;
25086 arguments
= build_tree_list_vec (vec
);
25087 release_tree_vector (vec
);
25089 /* Save the arguments away. */
25090 TREE_VALUE (attribute
) = arguments
;
25093 if (arguments
!= error_mark_node
)
25095 /* Add this attribute to the list. */
25096 TREE_CHAIN (attribute
) = attribute_list
;
25097 attribute_list
= attribute
;
25100 token
= cp_lexer_peek_token (parser
->lexer
);
25102 /* Now, look for more attributes. If the next token isn't a
25103 `,', we're done. */
25104 if (token
->type
!= CPP_COMMA
)
25107 /* Consume the comma and keep going. */
25108 cp_lexer_consume_token (parser
->lexer
);
25110 parser
->translate_strings_p
= save_translate_strings_p
;
25112 /* We built up the list in reverse order. */
25113 return nreverse (attribute_list
);
25116 /* Parse a standard C++11 attribute.
25118 The returned representation is a TREE_LIST which TREE_PURPOSE is
25119 the scoped name of the attribute, and the TREE_VALUE is its
25122 Note that the scoped name of the attribute is itself a TREE_LIST
25123 which TREE_PURPOSE is the namespace of the attribute, and
25124 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25125 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25126 and which TREE_PURPOSE is directly the attribute name.
25128 Clients of the attribute code should use get_attribute_namespace
25129 and get_attribute_name to get the actual namespace and name of
25130 attributes, regardless of their being GNU or C++11 attributes.
25133 attribute-token attribute-argument-clause [opt]
25137 attribute-scoped-token
25139 attribute-scoped-token:
25140 attribute-namespace :: identifier
25142 attribute-namespace:
25145 attribute-argument-clause:
25146 ( balanced-token-seq )
25148 balanced-token-seq:
25149 balanced-token [opt]
25150 balanced-token-seq balanced-token
25153 ( balanced-token-seq )
25154 [ balanced-token-seq ]
25155 { balanced-token-seq }. */
25158 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25160 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25163 temp_override
<bool> cleanup
25164 (parser
->auto_is_implicit_function_template_parm_p
, false);
25166 /* First, parse name of the attribute, a.k.a attribute-token. */
25168 token
= cp_lexer_peek_token (parser
->lexer
);
25169 if (token
->type
== CPP_NAME
)
25170 attr_id
= token
->u
.value
;
25171 else if (token
->type
== CPP_KEYWORD
)
25172 attr_id
= ridpointers
[(int) token
->keyword
];
25173 else if (token
->flags
& NAMED_OP
)
25174 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25176 if (attr_id
== NULL_TREE
)
25179 cp_lexer_consume_token (parser
->lexer
);
25181 token
= cp_lexer_peek_token (parser
->lexer
);
25182 if (token
->type
== CPP_SCOPE
)
25184 /* We are seeing a scoped attribute token. */
25186 cp_lexer_consume_token (parser
->lexer
);
25188 error_at (token
->location
, "attribute using prefix used together "
25189 "with scoped attribute token");
25192 token
= cp_lexer_consume_token (parser
->lexer
);
25193 if (token
->type
== CPP_NAME
)
25194 attr_id
= token
->u
.value
;
25195 else if (token
->type
== CPP_KEYWORD
)
25196 attr_id
= ridpointers
[(int) token
->keyword
];
25197 else if (token
->flags
& NAMED_OP
)
25198 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25201 error_at (token
->location
,
25202 "expected an identifier for the attribute name");
25203 return error_mark_node
;
25206 attr_id
= canonicalize_attr_name (attr_id
);
25207 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25209 token
= cp_lexer_peek_token (parser
->lexer
);
25212 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25216 attr_id
= canonicalize_attr_name (attr_id
);
25217 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25219 /* C++11 noreturn attribute is equivalent to GNU's. */
25220 if (is_attribute_p ("noreturn", attr_id
))
25221 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25222 /* C++14 deprecated attribute is equivalent to GNU's. */
25223 else if (is_attribute_p ("deprecated", attr_id
))
25224 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25225 /* C++17 fallthrough attribute is equivalent to GNU's. */
25226 else if (is_attribute_p ("fallthrough", attr_id
))
25227 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25228 /* Transactional Memory TS optimize_for_synchronized attribute is
25229 equivalent to GNU transaction_callable. */
25230 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25231 TREE_PURPOSE (attribute
)
25232 = get_identifier ("transaction_callable");
25233 /* Transactional Memory attributes are GNU attributes. */
25234 else if (tm_attr_to_mask (attr_id
))
25235 TREE_PURPOSE (attribute
) = attr_id
;
25238 /* Now parse the optional argument clause of the attribute. */
25240 if (token
->type
!= CPP_OPEN_PAREN
)
25244 vec
<tree
, va_gc
> *vec
;
25245 int attr_flag
= normal_attr
;
25247 if (attr_ns
== get_identifier ("gnu")
25248 && attribute_takes_identifier_p (attr_id
))
25249 /* A GNU attribute that takes an identifier in parameter. */
25250 attr_flag
= id_attr
;
25252 vec
= cp_parser_parenthesized_expression_list
25253 (parser
, attr_flag
, /*cast_p=*/false,
25254 /*allow_expansion_p=*/true,
25255 /*non_constant_p=*/NULL
);
25257 arguments
= error_mark_node
;
25260 arguments
= build_tree_list_vec (vec
);
25261 release_tree_vector (vec
);
25264 if (arguments
== error_mark_node
)
25265 attribute
= error_mark_node
;
25267 TREE_VALUE (attribute
) = arguments
;
25273 /* Check that the attribute ATTRIBUTE appears at most once in the
25274 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25275 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25276 isn't implemented yet in GCC. */
25279 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25283 tree name
= get_attribute_name (attribute
);
25284 if (is_attribute_p ("noreturn", name
)
25285 && lookup_attribute ("noreturn", attributes
))
25286 error ("attribute %<noreturn%> can appear at most once "
25287 "in an attribute-list");
25288 else if (is_attribute_p ("deprecated", name
)
25289 && lookup_attribute ("deprecated", attributes
))
25290 error ("attribute %<deprecated%> can appear at most once "
25291 "in an attribute-list");
25295 /* Parse a list of standard C++-11 attributes.
25299 attribute-list , attribute[opt]
25301 attribute-list , attribute ...
25305 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25307 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25308 cp_token
*token
= NULL
;
25312 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25313 if (attribute
== error_mark_node
)
25315 if (attribute
!= NULL_TREE
)
25317 cp_parser_check_std_attribute (attributes
, attribute
);
25318 TREE_CHAIN (attribute
) = attributes
;
25319 attributes
= attribute
;
25321 token
= cp_lexer_peek_token (parser
->lexer
);
25322 if (token
->type
== CPP_ELLIPSIS
)
25324 cp_lexer_consume_token (parser
->lexer
);
25325 if (attribute
== NULL_TREE
)
25326 error_at (token
->location
,
25327 "expected attribute before %<...%>");
25330 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25331 if (pack
== error_mark_node
)
25332 return error_mark_node
;
25333 TREE_VALUE (attribute
) = pack
;
25335 token
= cp_lexer_peek_token (parser
->lexer
);
25337 if (token
->type
!= CPP_COMMA
)
25339 cp_lexer_consume_token (parser
->lexer
);
25341 attributes
= nreverse (attributes
);
25345 /* Parse a standard C++-11 attribute specifier.
25347 attribute-specifier:
25348 [ [ attribute-using-prefix [opt] attribute-list ] ]
25349 alignment-specifier
25351 attribute-using-prefix:
25352 using attribute-namespace :
25354 alignment-specifier:
25355 alignas ( type-id ... [opt] )
25356 alignas ( alignment-expression ... [opt] ). */
25359 cp_parser_std_attribute_spec (cp_parser
*parser
)
25361 tree attributes
= NULL_TREE
;
25362 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25364 if (token
->type
== CPP_OPEN_SQUARE
25365 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25367 tree attr_ns
= NULL_TREE
;
25369 cp_lexer_consume_token (parser
->lexer
);
25370 cp_lexer_consume_token (parser
->lexer
);
25372 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25374 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25375 if (token
->type
== CPP_NAME
)
25376 attr_ns
= token
->u
.value
;
25377 else if (token
->type
== CPP_KEYWORD
)
25378 attr_ns
= ridpointers
[(int) token
->keyword
];
25379 else if (token
->flags
& NAMED_OP
)
25380 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25383 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25385 if (cxx_dialect
< cxx17
25386 && !in_system_header_at (input_location
))
25387 pedwarn (input_location
, 0,
25388 "attribute using prefix only available "
25389 "with -std=c++17 or -std=gnu++17");
25391 cp_lexer_consume_token (parser
->lexer
);
25392 cp_lexer_consume_token (parser
->lexer
);
25393 cp_lexer_consume_token (parser
->lexer
);
25396 attr_ns
= NULL_TREE
;
25399 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25401 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25402 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25403 cp_parser_skip_to_end_of_statement (parser
);
25405 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25406 when we are sure that we have actually parsed them. */
25407 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25413 /* Look for an alignment-specifier. */
25415 token
= cp_lexer_peek_token (parser
->lexer
);
25417 if (token
->type
!= CPP_KEYWORD
25418 || token
->keyword
!= RID_ALIGNAS
)
25421 cp_lexer_consume_token (parser
->lexer
);
25422 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25424 matching_parens parens
;
25425 if (!parens
.require_open (parser
))
25426 return error_mark_node
;
25428 cp_parser_parse_tentatively (parser
);
25429 alignas_expr
= cp_parser_type_id (parser
);
25431 if (!cp_parser_parse_definitely (parser
))
25433 alignas_expr
= cp_parser_assignment_expression (parser
);
25434 if (alignas_expr
== error_mark_node
)
25435 cp_parser_skip_to_end_of_statement (parser
);
25436 if (alignas_expr
== NULL_TREE
25437 || alignas_expr
== error_mark_node
)
25438 return alignas_expr
;
25441 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25442 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25444 /* Handle alignas (pack...). */
25445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25447 cp_lexer_consume_token (parser
->lexer
);
25448 alignas_expr
= make_pack_expansion (alignas_expr
);
25451 /* Something went wrong, so don't build the attribute. */
25452 if (alignas_expr
== error_mark_node
)
25453 return error_mark_node
;
25455 if (!parens
.require_close (parser
))
25456 return error_mark_node
;
25458 /* Build the C++-11 representation of an 'aligned'
25461 build_tree_list (build_tree_list (get_identifier ("gnu"),
25462 get_identifier ("aligned")),
25469 /* Parse a standard C++-11 attribute-specifier-seq.
25471 attribute-specifier-seq:
25472 attribute-specifier-seq [opt] attribute-specifier
25476 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25478 tree attr_specs
= NULL_TREE
;
25479 tree attr_last
= NULL_TREE
;
25483 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25484 if (attr_spec
== NULL_TREE
)
25486 if (attr_spec
== error_mark_node
)
25487 return error_mark_node
;
25490 TREE_CHAIN (attr_last
) = attr_spec
;
25492 attr_specs
= attr_last
= attr_spec
;
25493 attr_last
= tree_last (attr_last
);
25499 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25500 return index of the first token after balanced-token, or N on failure. */
25503 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25506 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25508 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25511 case CPP_PRAGMA_EOL
:
25512 /* Ran out of tokens. */
25514 case CPP_OPEN_PAREN
:
25517 case CPP_OPEN_BRACE
:
25520 case CPP_OPEN_SQUARE
:
25523 case CPP_CLOSE_PAREN
:
25526 case CPP_CLOSE_BRACE
:
25529 case CPP_CLOSE_SQUARE
:
25535 while (nparens
|| nbraces
|| nsquares
);
25539 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25540 return index of the first token after the GNU attribute tokens, or N on
25544 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25548 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25549 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25550 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25553 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25556 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25563 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25564 next token), return index of the first token after the standard C++11
25565 attribute tokens, or N on failure. */
25568 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25572 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25573 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25575 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25578 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25582 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25583 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25585 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25596 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25597 as the next token), return index of the first token after the attribute
25598 tokens, or N on failure. */
25601 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25603 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25604 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25605 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25608 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25609 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25610 current value of the PEDANTIC flag, regardless of whether or not
25611 the `__extension__' keyword is present. The caller is responsible
25612 for restoring the value of the PEDANTIC flag. */
25615 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25617 /* Save the old value of the PEDANTIC flag. */
25618 *saved_pedantic
= pedantic
;
25620 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25622 /* Consume the `__extension__' token. */
25623 cp_lexer_consume_token (parser
->lexer
);
25624 /* We're not being pedantic while the `__extension__' keyword is
25634 /* Parse a label declaration.
25637 __label__ label-declarator-seq ;
25639 label-declarator-seq:
25640 identifier , label-declarator-seq
25644 cp_parser_label_declaration (cp_parser
* parser
)
25646 /* Look for the `__label__' keyword. */
25647 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25653 /* Look for an identifier. */
25654 identifier
= cp_parser_identifier (parser
);
25655 /* If we failed, stop. */
25656 if (identifier
== error_mark_node
)
25658 /* Declare it as a label. */
25659 finish_label_decl (identifier
);
25660 /* If the next token is a `;', stop. */
25661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25663 /* Look for the `,' separating the label declarations. */
25664 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25667 /* Look for the final `;'. */
25668 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25671 // -------------------------------------------------------------------------- //
25674 // Parse a requires clause.
25676 // requires-clause:
25677 // 'requires' logical-or-expression
25679 // The required logical-or-expression must be a constant expression. Note
25680 // that we don't check that the expression is constepxr here. We defer until
25681 // we analyze constraints and then, we only check atomic constraints.
25683 cp_parser_requires_clause (cp_parser
*parser
)
25685 // Parse the requires clause so that it is not automatically folded.
25686 ++processing_template_decl
;
25687 tree expr
= cp_parser_binary_expression (parser
, false, false,
25688 PREC_NOT_OPERATOR
, NULL
);
25689 if (check_for_bare_parameter_packs (expr
))
25690 expr
= error_mark_node
;
25691 --processing_template_decl
;
25695 // Optionally parse a requires clause:
25697 cp_parser_requires_clause_opt (cp_parser
*parser
)
25699 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25700 if (tok
->keyword
!= RID_REQUIRES
)
25702 if (!flag_concepts
&& tok
->type
== CPP_NAME
25703 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25705 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25706 "%<requires%> only available with -fconcepts");
25707 /* Parse and discard the requires-clause. */
25708 cp_lexer_consume_token (parser
->lexer
);
25709 cp_parser_requires_clause (parser
);
25713 cp_lexer_consume_token (parser
->lexer
);
25714 return cp_parser_requires_clause (parser
);
25718 /*---------------------------------------------------------------------------
25719 Requires expressions
25720 ---------------------------------------------------------------------------*/
25722 /* Parse a requires expression
25724 requirement-expression:
25725 'requires' requirement-parameter-list [opt] requirement-body */
25727 cp_parser_requires_expression (cp_parser
*parser
)
25729 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25730 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25732 /* A requires-expression shall appear only within a concept
25733 definition or a requires-clause.
25735 TODO: Implement this diagnostic correctly. */
25736 if (!processing_template_decl
)
25738 error_at (loc
, "a requires expression cannot appear outside a template");
25739 cp_parser_skip_to_end_of_statement (parser
);
25740 return error_mark_node
;
25745 /* Local parameters are delared as variables within the scope
25746 of the expression. They are not visible past the end of
25747 the expression. Expressions within the requires-expression
25748 are unevaluated. */
25749 struct scope_sentinel
25753 ++cp_unevaluated_operand
;
25754 begin_scope (sk_block
, NULL_TREE
);
25759 pop_bindings_and_leave_scope ();
25760 --cp_unevaluated_operand
;
25764 /* Parse the optional parameter list. */
25765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25767 parms
= cp_parser_requirement_parameter_list (parser
);
25768 if (parms
== error_mark_node
)
25769 return error_mark_node
;
25774 /* Parse the requirement body. */
25775 reqs
= cp_parser_requirement_body (parser
);
25776 if (reqs
== error_mark_node
)
25777 return error_mark_node
;
25780 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25782 grokparms (parms
, &parms
);
25783 return finish_requires_expr (parms
, reqs
);
25786 /* Parse a parameterized requirement.
25788 requirement-parameter-list:
25789 '(' parameter-declaration-clause ')' */
25791 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25793 matching_parens parens
;
25794 if (!parens
.require_open (parser
))
25795 return error_mark_node
;
25797 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25799 if (!parens
.require_close (parser
))
25800 return error_mark_node
;
25805 /* Parse the body of a requirement.
25808 '{' requirement-list '}' */
25810 cp_parser_requirement_body (cp_parser
*parser
)
25812 matching_braces braces
;
25813 if (!braces
.require_open (parser
))
25814 return error_mark_node
;
25816 tree reqs
= cp_parser_requirement_list (parser
);
25818 if (!braces
.require_close (parser
))
25819 return error_mark_node
;
25824 /* Parse a list of requirements.
25828 requirement-list ';' requirement[opt] */
25830 cp_parser_requirement_list (cp_parser
*parser
)
25832 tree result
= NULL_TREE
;
25835 tree req
= cp_parser_requirement (parser
);
25836 if (req
== error_mark_node
)
25837 return error_mark_node
;
25839 result
= tree_cons (NULL_TREE
, req
, result
);
25841 /* If we see a semi-colon, consume it. */
25842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25843 cp_lexer_consume_token (parser
->lexer
);
25845 /* Stop processing at the end of the list. */
25846 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25850 /* Reverse the order of requirements so they are analyzed in
25851 declaration order. */
25852 return nreverse (result
);
25855 /* Parse a syntactic requirement or type requirement.
25859 compound-requirement
25861 nested-requirement */
25863 cp_parser_requirement (cp_parser
*parser
)
25865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25866 return cp_parser_compound_requirement (parser
);
25867 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25868 return cp_parser_type_requirement (parser
);
25869 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25870 return cp_parser_nested_requirement (parser
);
25872 return cp_parser_simple_requirement (parser
);
25875 /* Parse a simple requirement.
25877 simple-requirement:
25880 cp_parser_simple_requirement (cp_parser
*parser
)
25882 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25883 if (!expr
|| expr
== error_mark_node
)
25884 return error_mark_node
;
25886 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25887 return error_mark_node
;
25889 return finish_simple_requirement (expr
);
25892 /* Parse a type requirement
25895 nested-name-specifier [opt] required-type-name ';'
25897 required-type-name:
25899 'template' [opt] simple-template-id */
25901 cp_parser_type_requirement (cp_parser
*parser
)
25903 cp_lexer_consume_token (parser
->lexer
);
25905 // Save the scope before parsing name specifiers.
25906 tree saved_scope
= parser
->scope
;
25907 tree saved_object_scope
= parser
->object_scope
;
25908 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25909 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25910 cp_parser_nested_name_specifier_opt (parser
,
25911 /*typename_keyword_p=*/true,
25912 /*check_dependency_p=*/false,
25914 /*is_declaration=*/false);
25917 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25919 cp_lexer_consume_token (parser
->lexer
);
25920 type
= cp_parser_template_id (parser
,
25921 /*template_keyword_p=*/true,
25922 /*check_dependency=*/false,
25923 /*tag_type=*/none_type
,
25924 /*is_declaration=*/false);
25925 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25926 /*complain=*/tf_error
);
25929 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25931 if (TREE_CODE (type
) == TYPE_DECL
)
25932 type
= TREE_TYPE (type
);
25934 parser
->scope
= saved_scope
;
25935 parser
->object_scope
= saved_object_scope
;
25936 parser
->qualifying_scope
= saved_qualifying_scope
;
25938 if (type
== error_mark_node
)
25939 cp_parser_skip_to_end_of_statement (parser
);
25941 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25942 return error_mark_node
;
25943 if (type
== error_mark_node
)
25944 return error_mark_node
;
25946 return finish_type_requirement (type
);
25949 /* Parse a compound requirement
25951 compound-requirement:
25952 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25954 cp_parser_compound_requirement (cp_parser
*parser
)
25956 /* Parse an expression enclosed in '{ }'s. */
25957 matching_braces braces
;
25958 if (!braces
.require_open (parser
))
25959 return error_mark_node
;
25961 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25962 if (!expr
|| expr
== error_mark_node
)
25963 return error_mark_node
;
25965 if (!braces
.require_close (parser
))
25966 return error_mark_node
;
25968 /* Parse the optional noexcept. */
25969 bool noexcept_p
= false;
25970 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25972 cp_lexer_consume_token (parser
->lexer
);
25976 /* Parse the optional trailing return type. */
25977 tree type
= NULL_TREE
;
25978 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25980 cp_lexer_consume_token (parser
->lexer
);
25981 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25982 parser
->in_result_type_constraint_p
= true;
25983 type
= cp_parser_trailing_type_id (parser
);
25984 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25985 if (type
== error_mark_node
)
25986 return error_mark_node
;
25989 return finish_compound_requirement (expr
, type
, noexcept_p
);
25992 /* Parse a nested requirement. This is the same as a requires clause.
25994 nested-requirement:
25997 cp_parser_nested_requirement (cp_parser
*parser
)
25999 cp_lexer_consume_token (parser
->lexer
);
26000 tree req
= cp_parser_requires_clause (parser
);
26001 if (req
== error_mark_node
)
26002 return error_mark_node
;
26003 return finish_nested_requirement (req
);
26006 /* Support Functions */
26008 /* Return the appropriate prefer_type argument for lookup_name_real based on
26009 tag_type and template_mem_access. */
26012 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26014 /* DR 141: When looking in the current enclosing context for a template-name
26015 after -> or ., only consider class templates. */
26016 if (template_mem_access
)
26020 case none_type
: return 0; // No preference.
26021 case scope_type
: return 1; // Type or namespace.
26022 default: return 2; // Type only.
26026 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26027 NAME should have one of the representations used for an
26028 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26029 is returned. If PARSER->SCOPE is a dependent type, then a
26030 SCOPE_REF is returned.
26032 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26033 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26034 was formed. Abstractly, such entities should not be passed to this
26035 function, because they do not need to be looked up, but it is
26036 simpler to check for this special case here, rather than at the
26039 In cases not explicitly covered above, this function returns a
26040 DECL, OVERLOAD, or baselink representing the result of the lookup.
26041 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26044 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26045 (e.g., "struct") that was used. In that case bindings that do not
26046 refer to types are ignored.
26048 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26051 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26054 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26057 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26058 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26059 NULL_TREE otherwise. */
26062 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26063 enum tag_types tag_type
,
26066 bool check_dependency
,
26067 tree
*ambiguous_decls
,
26068 location_t name_location
)
26071 tree object_type
= parser
->context
->object_type
;
26073 /* Assume that the lookup will be unambiguous. */
26074 if (ambiguous_decls
)
26075 *ambiguous_decls
= NULL_TREE
;
26077 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26078 no longer valid. Note that if we are parsing tentatively, and
26079 the parse fails, OBJECT_TYPE will be automatically restored. */
26080 parser
->context
->object_type
= NULL_TREE
;
26082 if (name
== error_mark_node
)
26083 return error_mark_node
;
26085 /* A template-id has already been resolved; there is no lookup to
26087 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26089 if (BASELINK_P (name
))
26091 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26092 == TEMPLATE_ID_EXPR
);
26096 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26097 it should already have been checked to make sure that the name
26098 used matches the type being destroyed. */
26099 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26103 /* Figure out to which type this destructor applies. */
26105 type
= parser
->scope
;
26106 else if (object_type
)
26107 type
= object_type
;
26109 type
= current_class_type
;
26110 /* If that's not a class type, there is no destructor. */
26111 if (!type
|| !CLASS_TYPE_P (type
))
26112 return error_mark_node
;
26114 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26115 lazily_declare_fn (sfk_destructor
, type
);
26117 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26120 return error_mark_node
;
26123 /* By this point, the NAME should be an ordinary identifier. If
26124 the id-expression was a qualified name, the qualifying scope is
26125 stored in PARSER->SCOPE at this point. */
26126 gcc_assert (identifier_p (name
));
26128 /* Perform the lookup. */
26133 if (parser
->scope
== error_mark_node
)
26134 return error_mark_node
;
26136 /* If the SCOPE is dependent, the lookup must be deferred until
26137 the template is instantiated -- unless we are explicitly
26138 looking up names in uninstantiated templates. Even then, we
26139 cannot look up the name if the scope is not a class type; it
26140 might, for example, be a template type parameter. */
26141 dependent_p
= (TYPE_P (parser
->scope
)
26142 && dependent_scope_p (parser
->scope
));
26143 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26145 /* Defer lookup. */
26146 decl
= error_mark_node
;
26149 tree pushed_scope
= NULL_TREE
;
26151 /* If PARSER->SCOPE is a dependent type, then it must be a
26152 class type, and we must not be checking dependencies;
26153 otherwise, we would have processed this lookup above. So
26154 that PARSER->SCOPE is not considered a dependent base by
26155 lookup_member, we must enter the scope here. */
26157 pushed_scope
= push_scope (parser
->scope
);
26159 /* If the PARSER->SCOPE is a template specialization, it
26160 may be instantiated during name lookup. In that case,
26161 errors may be issued. Even if we rollback the current
26162 tentative parse, those errors are valid. */
26163 decl
= lookup_qualified_name (parser
->scope
, name
,
26164 prefer_type_arg (tag_type
),
26165 /*complain=*/true);
26167 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26168 lookup result and the nested-name-specifier nominates a class C:
26169 * if the name specified after the nested-name-specifier, when
26170 looked up in C, is the injected-class-name of C (Clause 9), or
26171 * if the name specified after the nested-name-specifier is the
26172 same as the identifier or the simple-template-id's template-
26173 name in the last component of the nested-name-specifier,
26174 the name is instead considered to name the constructor of
26175 class C. [ Note: for example, the constructor is not an
26176 acceptable lookup result in an elaborated-type-specifier so
26177 the constructor would not be used in place of the
26178 injected-class-name. --end note ] Such a constructor name
26179 shall be used only in the declarator-id of a declaration that
26180 names a constructor or in a using-declaration. */
26181 if (tag_type
== none_type
26182 && DECL_SELF_REFERENCE_P (decl
)
26183 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26184 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26185 prefer_type_arg (tag_type
),
26186 /*complain=*/true);
26188 /* If we have a single function from a using decl, pull it out. */
26189 if (TREE_CODE (decl
) == OVERLOAD
26190 && !really_overloaded_fn (decl
))
26191 decl
= OVL_FUNCTION (decl
);
26194 pop_scope (pushed_scope
);
26197 /* If the scope is a dependent type and either we deferred lookup or
26198 we did lookup but didn't find the name, rememeber the name. */
26199 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26200 && dependent_type_p (parser
->scope
))
26206 /* The resolution to Core Issue 180 says that `struct
26207 A::B' should be considered a type-name, even if `A'
26209 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26210 /*complain=*/tf_error
);
26211 if (type
!= error_mark_node
)
26212 decl
= TYPE_NAME (type
);
26214 else if (is_template
26215 && (cp_parser_next_token_ends_template_argument_p (parser
)
26216 || cp_lexer_next_token_is (parser
->lexer
,
26218 decl
= make_unbound_class_template (parser
->scope
,
26220 /*complain=*/tf_error
);
26222 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26223 parser
->scope
, name
,
26226 parser
->qualifying_scope
= parser
->scope
;
26227 parser
->object_scope
= NULL_TREE
;
26229 else if (object_type
)
26231 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26232 OBJECT_TYPE is not a class. */
26233 if (CLASS_TYPE_P (object_type
))
26234 /* If the OBJECT_TYPE is a template specialization, it may
26235 be instantiated during name lookup. In that case, errors
26236 may be issued. Even if we rollback the current tentative
26237 parse, those errors are valid. */
26238 decl
= lookup_member (object_type
,
26241 prefer_type_arg (tag_type
),
26242 tf_warning_or_error
);
26247 /* Look it up in the enclosing context. DR 141: When looking for a
26248 template-name after -> or ., only consider class templates. */
26249 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26251 /*block_p=*/true, is_namespace
, 0);
26252 if (object_type
== unknown_type_node
)
26253 /* The object is type-dependent, so we can't look anything up; we used
26254 this to get the DR 141 behavior. */
26255 object_type
= NULL_TREE
;
26256 parser
->object_scope
= object_type
;
26257 parser
->qualifying_scope
= NULL_TREE
;
26261 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26263 /*block_p=*/true, is_namespace
, 0);
26264 parser
->qualifying_scope
= NULL_TREE
;
26265 parser
->object_scope
= NULL_TREE
;
26268 /* If the lookup failed, let our caller know. */
26269 if (!decl
|| decl
== error_mark_node
)
26270 return error_mark_node
;
26272 /* Pull out the template from an injected-class-name (or multiple). */
26274 decl
= maybe_get_template_decl_from_type_decl (decl
);
26276 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26277 if (TREE_CODE (decl
) == TREE_LIST
)
26279 if (ambiguous_decls
)
26280 *ambiguous_decls
= decl
;
26281 /* The error message we have to print is too complicated for
26282 cp_parser_error, so we incorporate its actions directly. */
26283 if (!cp_parser_simulate_error (parser
))
26285 error_at (name_location
, "reference to %qD is ambiguous",
26287 print_candidates (decl
);
26289 return error_mark_node
;
26292 gcc_assert (DECL_P (decl
)
26293 || TREE_CODE (decl
) == OVERLOAD
26294 || TREE_CODE (decl
) == SCOPE_REF
26295 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26296 || BASELINK_P (decl
));
26298 /* If we have resolved the name of a member declaration, check to
26299 see if the declaration is accessible. When the name resolves to
26300 set of overloaded functions, accessibility is checked when
26301 overload resolution is done.
26303 During an explicit instantiation, access is not checked at all,
26304 as per [temp.explicit]. */
26306 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26308 maybe_record_typedef_use (decl
);
26310 return cp_expr (decl
, name_location
);
26313 /* Like cp_parser_lookup_name, but for use in the typical case where
26314 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26315 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26318 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26320 return cp_parser_lookup_name (parser
, name
,
26322 /*is_template=*/false,
26323 /*is_namespace=*/false,
26324 /*check_dependency=*/true,
26325 /*ambiguous_decls=*/NULL
,
26329 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26330 the current context, return the TYPE_DECL. If TAG_NAME_P is
26331 true, the DECL indicates the class being defined in a class-head,
26332 or declared in an elaborated-type-specifier.
26334 Otherwise, return DECL. */
26337 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26339 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26340 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26343 template <typename T> struct B;
26346 template <typename T> struct A::B {};
26348 Similarly, in an elaborated-type-specifier:
26350 namespace N { struct X{}; }
26353 template <typename T> friend struct N::X;
26356 However, if the DECL refers to a class type, and we are in
26357 the scope of the class, then the name lookup automatically
26358 finds the TYPE_DECL created by build_self_reference rather
26359 than a TEMPLATE_DECL. For example, in:
26361 template <class T> struct S {
26365 there is no need to handle such case. */
26367 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26368 return DECL_TEMPLATE_RESULT (decl
);
26373 /* If too many, or too few, template-parameter lists apply to the
26374 declarator, issue an error message. Returns TRUE if all went well,
26375 and FALSE otherwise. */
26378 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26379 cp_declarator
*declarator
,
26380 location_t declarator_location
)
26382 switch (declarator
->kind
)
26386 unsigned num_templates
= 0;
26387 tree scope
= declarator
->u
.id
.qualifying_scope
;
26388 bool template_id_p
= false;
26391 num_templates
= num_template_headers_for_class (scope
);
26392 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26393 == TEMPLATE_ID_EXPR
)
26395 /* If the DECLARATOR has the form `X<y>' then it uses one
26396 additional level of template parameters. */
26398 template_id_p
= true;
26401 return cp_parser_check_template_parameters
26402 (parser
, num_templates
, template_id_p
, declarator_location
,
26409 case cdk_reference
:
26411 return (cp_parser_check_declarator_template_parameters
26412 (parser
, declarator
->declarator
, declarator_location
));
26419 gcc_unreachable ();
26424 /* NUM_TEMPLATES were used in the current declaration. If that is
26425 invalid, return FALSE and issue an error messages. Otherwise,
26426 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26427 declarator and we can print more accurate diagnostics. */
26430 cp_parser_check_template_parameters (cp_parser
* parser
,
26431 unsigned num_templates
,
26432 bool template_id_p
,
26433 location_t location
,
26434 cp_declarator
*declarator
)
26436 /* If there are the same number of template classes and parameter
26437 lists, that's OK. */
26438 if (parser
->num_template_parameter_lists
== num_templates
)
26440 /* If there are more, but only one more, and the name ends in an identifier,
26441 then we are declaring a primary template. That's OK too. */
26443 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26445 /* If there are more template classes than parameter lists, we have
26448 template <class T> void S<T>::R<T>::f (); */
26449 if (parser
->num_template_parameter_lists
< num_templates
)
26451 if (declarator
&& !current_function_decl
)
26452 error_at (location
, "specializing member %<%T::%E%> "
26453 "requires %<template<>%> syntax",
26454 declarator
->u
.id
.qualifying_scope
,
26455 declarator
->u
.id
.unqualified_name
);
26456 else if (declarator
)
26457 error_at (location
, "invalid declaration of %<%T::%E%>",
26458 declarator
->u
.id
.qualifying_scope
,
26459 declarator
->u
.id
.unqualified_name
);
26461 error_at (location
, "too few template-parameter-lists");
26464 /* Otherwise, there are too many template parameter lists. We have
26467 template <class T> template <class U> void S::f(); */
26468 error_at (location
, "too many template-parameter-lists");
26472 /* Parse an optional `::' token indicating that the following name is
26473 from the global namespace. If so, PARSER->SCOPE is set to the
26474 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26475 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26476 Returns the new value of PARSER->SCOPE, if the `::' token is
26477 present, and NULL_TREE otherwise. */
26480 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26484 /* Peek at the next token. */
26485 token
= cp_lexer_peek_token (parser
->lexer
);
26486 /* If we're looking at a `::' token then we're starting from the
26487 global namespace, not our current location. */
26488 if (token
->type
== CPP_SCOPE
)
26490 /* Consume the `::' token. */
26491 cp_lexer_consume_token (parser
->lexer
);
26492 /* Set the SCOPE so that we know where to start the lookup. */
26493 parser
->scope
= global_namespace
;
26494 parser
->qualifying_scope
= global_namespace
;
26495 parser
->object_scope
= NULL_TREE
;
26497 return parser
->scope
;
26499 else if (!current_scope_valid_p
)
26501 parser
->scope
= NULL_TREE
;
26502 parser
->qualifying_scope
= NULL_TREE
;
26503 parser
->object_scope
= NULL_TREE
;
26509 /* Returns TRUE if the upcoming token sequence is the start of a
26510 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26511 declarator is preceded by the `friend' specifier. */
26514 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26516 bool constructor_p
;
26517 bool outside_class_specifier_p
;
26518 tree nested_name_specifier
;
26519 cp_token
*next_token
;
26521 /* The common case is that this is not a constructor declarator, so
26522 try to avoid doing lots of work if at all possible. It's not
26523 valid declare a constructor at function scope. */
26524 if (parser
->in_function_body
)
26526 /* And only certain tokens can begin a constructor declarator. */
26527 next_token
= cp_lexer_peek_token (parser
->lexer
);
26528 if (next_token
->type
!= CPP_NAME
26529 && next_token
->type
!= CPP_SCOPE
26530 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26531 && next_token
->type
!= CPP_TEMPLATE_ID
)
26534 /* Parse tentatively; we are going to roll back all of the tokens
26536 cp_parser_parse_tentatively (parser
);
26537 /* Assume that we are looking at a constructor declarator. */
26538 constructor_p
= true;
26540 /* Look for the optional `::' operator. */
26541 cp_parser_global_scope_opt (parser
,
26542 /*current_scope_valid_p=*/false);
26543 /* Look for the nested-name-specifier. */
26544 nested_name_specifier
26545 = (cp_parser_nested_name_specifier_opt (parser
,
26546 /*typename_keyword_p=*/false,
26547 /*check_dependency_p=*/false,
26549 /*is_declaration=*/false));
26551 outside_class_specifier_p
= (!at_class_scope_p ()
26552 || !TYPE_BEING_DEFINED (current_class_type
)
26555 /* Outside of a class-specifier, there must be a
26556 nested-name-specifier. Except in C++17 mode, where we
26557 might be declaring a guiding declaration. */
26558 if (!nested_name_specifier
&& outside_class_specifier_p
26559 && cxx_dialect
< cxx17
)
26560 constructor_p
= false;
26561 else if (nested_name_specifier
== error_mark_node
)
26562 constructor_p
= false;
26564 /* If we have a class scope, this is easy; DR 147 says that S::S always
26565 names the constructor, and no other qualified name could. */
26566 if (constructor_p
&& nested_name_specifier
26567 && CLASS_TYPE_P (nested_name_specifier
))
26569 tree id
= cp_parser_unqualified_id (parser
,
26570 /*template_keyword_p=*/false,
26571 /*check_dependency_p=*/false,
26572 /*declarator_p=*/true,
26573 /*optional_p=*/false);
26574 if (is_overloaded_fn (id
))
26575 id
= DECL_NAME (get_first_fn (id
));
26576 if (!constructor_name_p (id
, nested_name_specifier
))
26577 constructor_p
= false;
26579 /* If we still think that this might be a constructor-declarator,
26580 look for a class-name. */
26581 else if (constructor_p
)
26585 template <typename T> struct S {
26589 we must recognize that the nested `S' names a class. */
26590 if (cxx_dialect
>= cxx17
)
26591 cp_parser_parse_tentatively (parser
);
26594 type_decl
= cp_parser_class_name (parser
,
26595 /*typename_keyword_p=*/false,
26596 /*template_keyword_p=*/false,
26598 /*check_dependency_p=*/false,
26599 /*class_head_p=*/false,
26600 /*is_declaration=*/false);
26602 if (cxx_dialect
>= cxx17
26603 && !cp_parser_parse_definitely (parser
))
26605 type_decl
= NULL_TREE
;
26606 tree tmpl
= cp_parser_template_name (parser
,
26607 /*template_keyword*/false,
26608 /*check_dependency_p*/false,
26609 /*is_declaration*/false,
26611 /*is_identifier*/NULL
);
26612 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26613 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26614 /* It's a deduction guide, return true. */;
26616 cp_parser_simulate_error (parser
);
26619 /* If there was no class-name, then this is not a constructor.
26620 Otherwise, if we are in a class-specifier and we aren't
26621 handling a friend declaration, check that its type matches
26622 current_class_type (c++/38313). Note: error_mark_node
26623 is left alone for error recovery purposes. */
26624 constructor_p
= (!cp_parser_error_occurred (parser
)
26625 && (outside_class_specifier_p
26626 || type_decl
== NULL_TREE
26627 || type_decl
== error_mark_node
26628 || same_type_p (current_class_type
,
26629 TREE_TYPE (type_decl
))));
26631 /* If we're still considering a constructor, we have to see a `(',
26632 to begin the parameter-declaration-clause, followed by either a
26633 `)', an `...', or a decl-specifier. We need to check for a
26634 type-specifier to avoid being fooled into thinking that:
26638 is a constructor. (It is actually a function named `f' that
26639 takes one parameter (of type `int') and returns a value of type
26642 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26643 constructor_p
= false;
26646 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26647 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26648 /* A parameter declaration begins with a decl-specifier,
26649 which is either the "attribute" keyword, a storage class
26650 specifier, or (usually) a type-specifier. */
26651 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26654 tree pushed_scope
= NULL_TREE
;
26655 unsigned saved_num_template_parameter_lists
;
26657 /* Names appearing in the type-specifier should be looked up
26658 in the scope of the class. */
26659 if (current_class_type
)
26661 else if (type_decl
)
26663 type
= TREE_TYPE (type_decl
);
26664 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26666 type
= resolve_typename_type (type
,
26667 /*only_current_p=*/false);
26668 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26670 cp_parser_abort_tentative_parse (parser
);
26674 pushed_scope
= push_scope (type
);
26677 /* Inside the constructor parameter list, surrounding
26678 template-parameter-lists do not apply. */
26679 saved_num_template_parameter_lists
26680 = parser
->num_template_parameter_lists
;
26681 parser
->num_template_parameter_lists
= 0;
26683 /* Look for the type-specifier. */
26684 cp_parser_type_specifier (parser
,
26685 CP_PARSER_FLAGS_NONE
,
26686 /*decl_specs=*/NULL
,
26687 /*is_declarator=*/true,
26688 /*declares_class_or_enum=*/NULL
,
26689 /*is_cv_qualifier=*/NULL
);
26691 parser
->num_template_parameter_lists
26692 = saved_num_template_parameter_lists
;
26694 /* Leave the scope of the class. */
26696 pop_scope (pushed_scope
);
26698 constructor_p
= !cp_parser_error_occurred (parser
);
26702 /* We did not really want to consume any tokens. */
26703 cp_parser_abort_tentative_parse (parser
);
26705 return constructor_p
;
26708 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26709 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26710 they must be performed once we are in the scope of the function.
26712 Returns the function defined. */
26715 cp_parser_function_definition_from_specifiers_and_declarator
26716 (cp_parser
* parser
,
26717 cp_decl_specifier_seq
*decl_specifiers
,
26719 const cp_declarator
*declarator
)
26724 /* Begin the function-definition. */
26725 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26727 /* The things we're about to see are not directly qualified by any
26728 template headers we've seen thus far. */
26729 reset_specialization ();
26731 /* If there were names looked up in the decl-specifier-seq that we
26732 did not check, check them now. We must wait until we are in the
26733 scope of the function to perform the checks, since the function
26734 might be a friend. */
26735 perform_deferred_access_checks (tf_warning_or_error
);
26739 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26740 parser
->omp_declare_simd
= NULL
;
26741 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26742 parser
->oacc_routine
= NULL
;
26747 /* Skip the entire function. */
26748 cp_parser_skip_to_end_of_block_or_statement (parser
);
26749 fn
= error_mark_node
;
26751 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26753 /* Seen already, skip it. An error message has already been output. */
26754 cp_parser_skip_to_end_of_block_or_statement (parser
);
26755 fn
= current_function_decl
;
26756 current_function_decl
= NULL_TREE
;
26757 /* If this is a function from a class, pop the nested class. */
26758 if (current_class_name
)
26759 pop_nested_class ();
26764 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26765 tv
= TV_PARSE_INLINE
;
26767 tv
= TV_PARSE_FUNC
;
26769 fn
= cp_parser_function_definition_after_declarator (parser
,
26770 /*inline_p=*/false);
26777 /* Parse the part of a function-definition that follows the
26778 declarator. INLINE_P is TRUE iff this function is an inline
26779 function defined within a class-specifier.
26781 Returns the function defined. */
26784 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26788 bool saved_in_unbraced_linkage_specification_p
;
26789 bool saved_in_function_body
;
26790 unsigned saved_num_template_parameter_lists
;
26792 bool fully_implicit_function_template_p
26793 = parser
->fully_implicit_function_template_p
;
26794 parser
->fully_implicit_function_template_p
= false;
26795 tree implicit_template_parms
26796 = parser
->implicit_template_parms
;
26797 parser
->implicit_template_parms
= 0;
26798 cp_binding_level
* implicit_template_scope
26799 = parser
->implicit_template_scope
;
26800 parser
->implicit_template_scope
= 0;
26802 saved_in_function_body
= parser
->in_function_body
;
26803 parser
->in_function_body
= true;
26804 /* If the next token is `return', then the code may be trying to
26805 make use of the "named return value" extension that G++ used to
26807 token
= cp_lexer_peek_token (parser
->lexer
);
26808 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26810 /* Consume the `return' keyword. */
26811 cp_lexer_consume_token (parser
->lexer
);
26812 /* Look for the identifier that indicates what value is to be
26814 cp_parser_identifier (parser
);
26815 /* Issue an error message. */
26816 error_at (token
->location
,
26817 "named return values are no longer supported");
26818 /* Skip tokens until we reach the start of the function body. */
26821 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26822 if (token
->type
== CPP_OPEN_BRACE
26823 || token
->type
== CPP_EOF
26824 || token
->type
== CPP_PRAGMA_EOL
)
26826 cp_lexer_consume_token (parser
->lexer
);
26829 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26830 anything declared inside `f'. */
26831 saved_in_unbraced_linkage_specification_p
26832 = parser
->in_unbraced_linkage_specification_p
;
26833 parser
->in_unbraced_linkage_specification_p
= false;
26834 /* Inside the function, surrounding template-parameter-lists do not
26836 saved_num_template_parameter_lists
26837 = parser
->num_template_parameter_lists
;
26838 parser
->num_template_parameter_lists
= 0;
26840 /* If the next token is `try', `__transaction_atomic', or
26841 `__transaction_relaxed`, then we are looking at either function-try-block
26842 or function-transaction-block. Note that all of these include the
26844 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26845 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26846 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26847 RID_TRANSACTION_RELAXED
))
26848 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26849 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26850 cp_parser_function_try_block (parser
);
26852 cp_parser_ctor_initializer_opt_and_function_body
26853 (parser
, /*in_function_try_block=*/false);
26855 /* Finish the function. */
26856 fn
= finish_function (inline_p
);
26857 /* Generate code for it, if necessary. */
26858 expand_or_defer_fn (fn
);
26859 /* Restore the saved values. */
26860 parser
->in_unbraced_linkage_specification_p
26861 = saved_in_unbraced_linkage_specification_p
;
26862 parser
->num_template_parameter_lists
26863 = saved_num_template_parameter_lists
;
26864 parser
->in_function_body
= saved_in_function_body
;
26866 parser
->fully_implicit_function_template_p
26867 = fully_implicit_function_template_p
;
26868 parser
->implicit_template_parms
26869 = implicit_template_parms
;
26870 parser
->implicit_template_scope
26871 = implicit_template_scope
;
26873 if (parser
->fully_implicit_function_template_p
)
26874 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26879 /* Parse a template-declaration body (following argument list). */
26882 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26883 tree parameter_list
,
26886 tree decl
= NULL_TREE
;
26887 bool friend_p
= false;
26889 /* We just processed one more parameter list. */
26890 ++parser
->num_template_parameter_lists
;
26892 /* Get the deferred access checks from the parameter list. These
26893 will be checked once we know what is being declared, as for a
26894 member template the checks must be performed in the scope of the
26895 class containing the member. */
26896 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26898 /* Tentatively parse for a new template parameter list, which can either be
26899 the template keyword or a template introduction. */
26900 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26902 else if (cxx_dialect
>= cxx11
26903 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26904 decl
= cp_parser_alias_declaration (parser
);
26907 /* There are no access checks when parsing a template, as we do not
26908 know if a specialization will be a friend. */
26909 push_deferring_access_checks (dk_no_check
);
26910 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26911 decl
= cp_parser_single_declaration (parser
,
26914 /*explicit_specialization_p=*/false,
26916 pop_deferring_access_checks ();
26918 /* If this is a member template declaration, let the front
26920 if (member_p
&& !friend_p
&& decl
)
26922 if (TREE_CODE (decl
) == TYPE_DECL
)
26923 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26925 decl
= finish_member_template_decl (decl
);
26927 else if (friend_p
&& decl
26928 && DECL_DECLARES_TYPE_P (decl
))
26929 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26930 /*complain=*/true);
26932 /* We are done with the current parameter list. */
26933 --parser
->num_template_parameter_lists
;
26935 pop_deferring_access_checks ();
26938 finish_template_decl (parameter_list
);
26940 /* Check the template arguments for a literal operator template. */
26942 && DECL_DECLARES_FUNCTION_P (decl
)
26943 && UDLIT_OPER_P (DECL_NAME (decl
)))
26946 if (parameter_list
== NULL_TREE
)
26950 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26951 if (num_parms
== 1)
26953 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26954 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26955 if (TREE_TYPE (parm
) != char_type_node
26956 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26959 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26961 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26962 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26963 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26964 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26965 if (parm
== error_mark_node
26966 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26967 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26975 if (cxx_dialect
>= cxx14
)
26976 error ("literal operator template %qD has invalid parameter list."
26977 " Expected non-type template argument pack <char...>"
26978 " or <typename CharT, CharT...>",
26981 error ("literal operator template %qD has invalid parameter list."
26982 " Expected non-type template argument pack <char...>",
26987 /* Register member declarations. */
26988 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26989 finish_member_declaration (decl
);
26990 /* If DECL is a function template, we must return to parse it later.
26991 (Even though there is no definition, there might be default
26992 arguments that need handling.) */
26993 if (member_p
&& decl
26994 && DECL_DECLARES_FUNCTION_P (decl
))
26995 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26998 /* Parse a template introduction header for a template-declaration. Returns
26999 false if tentative parse fails. */
27002 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27004 cp_parser_parse_tentatively (parser
);
27006 tree saved_scope
= parser
->scope
;
27007 tree saved_object_scope
= parser
->object_scope
;
27008 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27010 /* Look for the optional `::' operator. */
27011 cp_parser_global_scope_opt (parser
,
27012 /*current_scope_valid_p=*/false);
27013 /* Look for the nested-name-specifier. */
27014 cp_parser_nested_name_specifier_opt (parser
,
27015 /*typename_keyword_p=*/false,
27016 /*check_dependency_p=*/true,
27018 /*is_declaration=*/false);
27020 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27021 tree concept_name
= cp_parser_identifier (parser
);
27023 /* Look up the concept for which we will be matching
27024 template parameters. */
27025 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27027 parser
->scope
= saved_scope
;
27028 parser
->object_scope
= saved_object_scope
;
27029 parser
->qualifying_scope
= saved_qualifying_scope
;
27031 if (concept_name
== error_mark_node
)
27032 cp_parser_simulate_error (parser
);
27034 /* Look for opening brace for introduction. */
27035 matching_braces braces
;
27036 braces
.require_open (parser
);
27038 if (!cp_parser_parse_definitely (parser
))
27041 push_deferring_access_checks (dk_deferred
);
27043 /* Build vector of placeholder parameters and grab
27044 matching identifiers. */
27045 tree introduction_list
= cp_parser_introduction_list (parser
);
27047 /* The introduction-list shall not be empty. */
27048 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27051 error ("empty introduction-list");
27055 /* Look for closing brace for introduction. */
27056 if (!braces
.require_close (parser
))
27059 if (tmpl_decl
== error_mark_node
)
27061 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27066 /* Build and associate the constraint. */
27067 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27068 if (parms
&& parms
!= error_mark_node
)
27070 cp_parser_template_declaration_after_parameters (parser
, parms
,
27075 error_at (token
->location
, "no matching concept for template-introduction");
27079 /* Parse a normal template-declaration following the template keyword. */
27082 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27084 tree parameter_list
;
27085 bool need_lang_pop
;
27086 location_t location
= input_location
;
27088 /* Look for the `<' token. */
27089 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27091 if (at_class_scope_p () && current_function_decl
)
27093 /* 14.5.2.2 [temp.mem]
27095 A local class shall not have member templates. */
27096 error_at (location
,
27097 "invalid declaration of member template in local class");
27098 cp_parser_skip_to_end_of_block_or_statement (parser
);
27103 A template ... shall not have C linkage. */
27104 if (current_lang_name
== lang_name_c
)
27106 error_at (location
, "template with C linkage");
27107 maybe_show_extern_c_location ();
27108 /* Give it C++ linkage to avoid confusing other parts of the
27110 push_lang_context (lang_name_cplusplus
);
27111 need_lang_pop
= true;
27114 need_lang_pop
= false;
27116 /* We cannot perform access checks on the template parameter
27117 declarations until we know what is being declared, just as we
27118 cannot check the decl-specifier list. */
27119 push_deferring_access_checks (dk_deferred
);
27121 /* If the next token is `>', then we have an invalid
27122 specialization. Rather than complain about an invalid template
27123 parameter, issue an error message here. */
27124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27126 cp_parser_error (parser
, "invalid explicit specialization");
27127 begin_specialization ();
27128 parameter_list
= NULL_TREE
;
27132 /* Parse the template parameters. */
27133 parameter_list
= cp_parser_template_parameter_list (parser
);
27136 /* Look for the `>'. */
27137 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27139 /* Manage template requirements */
27142 tree reqs
= get_shorthand_constraints (current_template_parms
);
27143 if (tree r
= cp_parser_requires_clause_opt (parser
))
27144 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27145 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27148 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27151 /* For the erroneous case of a template with C linkage, we pushed an
27152 implicit C++ linkage scope; exit that scope now. */
27154 pop_lang_context ();
27157 /* Parse a template-declaration, assuming that the `export' (and
27158 `extern') keywords, if present, has already been scanned. MEMBER_P
27159 is as for cp_parser_template_declaration. */
27162 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27164 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27166 cp_lexer_consume_token (parser
->lexer
);
27167 cp_parser_explicit_template_declaration (parser
, member_p
);
27170 else if (flag_concepts
)
27171 return cp_parser_template_introduction (parser
, member_p
);
27176 /* Perform the deferred access checks from a template-parameter-list.
27177 CHECKS is a TREE_LIST of access checks, as returned by
27178 get_deferred_access_checks. */
27181 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27183 ++processing_template_parmlist
;
27184 perform_access_checks (checks
, tf_warning_or_error
);
27185 --processing_template_parmlist
;
27188 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27189 `function-definition' sequence that follows a template header.
27190 If MEMBER_P is true, this declaration appears in a class scope.
27192 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27193 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27196 cp_parser_single_declaration (cp_parser
* parser
,
27197 vec
<deferred_access_check
, va_gc
> *checks
,
27199 bool explicit_specialization_p
,
27202 int declares_class_or_enum
;
27203 tree decl
= NULL_TREE
;
27204 cp_decl_specifier_seq decl_specifiers
;
27205 bool function_definition_p
= false;
27206 cp_token
*decl_spec_token_start
;
27208 /* This function is only used when processing a template
27210 gcc_assert (innermost_scope_kind () == sk_template_parms
27211 || innermost_scope_kind () == sk_template_spec
);
27213 /* Defer access checks until we know what is being declared. */
27214 push_deferring_access_checks (dk_deferred
);
27216 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27218 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27219 cp_parser_decl_specifier_seq (parser
,
27220 CP_PARSER_FLAGS_OPTIONAL
,
27222 &declares_class_or_enum
);
27224 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27226 /* There are no template typedefs. */
27227 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27229 error_at (decl_spec_token_start
->location
,
27230 "template declaration of %<typedef%>");
27231 decl
= error_mark_node
;
27234 /* Gather up the access checks that occurred the
27235 decl-specifier-seq. */
27236 stop_deferring_access_checks ();
27238 /* Check for the declaration of a template class. */
27239 if (declares_class_or_enum
)
27241 if (cp_parser_declares_only_class_p (parser
)
27242 || (declares_class_or_enum
& 2))
27244 // If this is a declaration, but not a definition, associate
27245 // any constraints with the type declaration. Constraints
27246 // are associated with definitions in cp_parser_class_specifier.
27247 if (declares_class_or_enum
== 1)
27248 associate_classtype_constraints (decl_specifiers
.type
);
27250 decl
= shadow_tag (&decl_specifiers
);
27255 friend template <typename T> struct A<T>::B;
27258 A<T>::B will be represented by a TYPENAME_TYPE, and
27259 therefore not recognized by shadow_tag. */
27260 if (friend_p
&& *friend_p
27262 && decl_specifiers
.type
27263 && TYPE_P (decl_specifiers
.type
))
27264 decl
= decl_specifiers
.type
;
27266 if (decl
&& decl
!= error_mark_node
)
27267 decl
= TYPE_NAME (decl
);
27269 decl
= error_mark_node
;
27271 /* Perform access checks for template parameters. */
27272 cp_parser_perform_template_parameter_access_checks (checks
);
27274 /* Give a helpful diagnostic for
27275 template <class T> struct A { } a;
27276 if we aren't already recovering from an error. */
27277 if (!cp_parser_declares_only_class_p (parser
)
27280 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27281 "a class template declaration must not declare "
27283 cp_parser_skip_to_end_of_block_or_statement (parser
);
27289 /* Complain about missing 'typename' or other invalid type names. */
27290 if (!decl_specifiers
.any_type_specifiers_p
27291 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27293 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27294 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27295 the rest of this declaration. */
27296 decl
= error_mark_node
;
27300 /* If it's not a template class, try for a template function. If
27301 the next token is a `;', then this declaration does not declare
27302 anything. But, if there were errors in the decl-specifiers, then
27303 the error might well have come from an attempted class-specifier.
27304 In that case, there's no need to warn about a missing declarator. */
27306 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27307 || decl_specifiers
.type
!= error_mark_node
))
27309 decl
= cp_parser_init_declarator (parser
,
27312 /*function_definition_allowed_p=*/true,
27314 declares_class_or_enum
,
27315 &function_definition_p
,
27318 /* 7.1.1-1 [dcl.stc]
27320 A storage-class-specifier shall not be specified in an explicit
27321 specialization... */
27323 && explicit_specialization_p
27324 && decl_specifiers
.storage_class
!= sc_none
)
27326 error_at (decl_spec_token_start
->location
,
27327 "explicit template specialization cannot have a storage class");
27328 decl
= error_mark_node
;
27331 if (decl
&& VAR_P (decl
))
27332 check_template_variable (decl
);
27335 /* Look for a trailing `;' after the declaration. */
27336 if (!function_definition_p
27337 && (decl
== error_mark_node
27338 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27339 cp_parser_skip_to_end_of_block_or_statement (parser
);
27342 pop_deferring_access_checks ();
27344 /* Clear any current qualification; whatever comes next is the start
27345 of something new. */
27346 parser
->scope
= NULL_TREE
;
27347 parser
->qualifying_scope
= NULL_TREE
;
27348 parser
->object_scope
= NULL_TREE
;
27353 /* Parse a cast-expression that is not the operand of a unary "&". */
27356 cp_parser_simple_cast_expression (cp_parser
*parser
)
27358 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27359 /*cast_p=*/false, /*decltype*/false, NULL
);
27362 /* Parse a functional cast to TYPE. Returns an expression
27363 representing the cast. */
27366 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27368 vec
<tree
, va_gc
> *vec
;
27369 tree expression_list
;
27373 location_t start_loc
= input_location
;
27376 type
= error_mark_node
;
27378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27380 cp_lexer_set_source_position (parser
->lexer
);
27381 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27382 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27383 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27384 if (TREE_CODE (type
) == TYPE_DECL
)
27385 type
= TREE_TYPE (type
);
27387 cast
= finish_compound_literal (type
, expression_list
,
27388 tf_warning_or_error
, fcl_functional
);
27389 /* Create a location of the form:
27392 with caret == start at the start of the type name,
27393 finishing at the closing brace. */
27394 location_t finish_loc
27395 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27396 location_t combined_loc
= make_location (start_loc
, start_loc
,
27398 cast
.set_location (combined_loc
);
27403 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27405 /*allow_expansion_p=*/true,
27406 /*non_constant_p=*/NULL
);
27408 expression_list
= error_mark_node
;
27411 expression_list
= build_tree_list_vec (vec
);
27412 release_tree_vector (vec
);
27415 cast
= build_functional_cast (type
, expression_list
,
27416 tf_warning_or_error
);
27417 /* [expr.const]/1: In an integral constant expression "only type
27418 conversions to integral or enumeration type can be used". */
27419 if (TREE_CODE (type
) == TYPE_DECL
)
27420 type
= TREE_TYPE (type
);
27421 if (cast
!= error_mark_node
27422 && !cast_valid_in_integral_constant_expression_p (type
)
27423 && cp_parser_non_integral_constant_expression (parser
,
27425 return error_mark_node
;
27427 /* Create a location of the form:
27430 with caret == start at the start of the type name,
27431 finishing at the closing paren. */
27432 location_t finish_loc
27433 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27434 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27435 cast
.set_location (combined_loc
);
27439 /* Save the tokens that make up the body of a member function defined
27440 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27441 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27442 specifiers applied to the declaration. Returns the FUNCTION_DECL
27443 for the member function. */
27446 cp_parser_save_member_function_body (cp_parser
* parser
,
27447 cp_decl_specifier_seq
*decl_specifiers
,
27448 cp_declarator
*declarator
,
27454 bool function_try_block
= false;
27456 /* Create the FUNCTION_DECL. */
27457 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27458 cp_finalize_omp_declare_simd (parser
, fn
);
27459 cp_finalize_oacc_routine (parser
, fn
, true);
27460 /* If something went badly wrong, bail out now. */
27461 if (fn
== error_mark_node
)
27463 /* If there's a function-body, skip it. */
27464 if (cp_parser_token_starts_function_definition_p
27465 (cp_lexer_peek_token (parser
->lexer
)))
27466 cp_parser_skip_to_end_of_block_or_statement (parser
);
27467 return error_mark_node
;
27470 /* Remember it, if there default args to post process. */
27471 cp_parser_save_default_args (parser
, fn
);
27473 /* Save away the tokens that make up the body of the
27475 first
= parser
->lexer
->next_token
;
27477 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27478 cp_lexer_consume_token (parser
->lexer
);
27479 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27480 RID_TRANSACTION_ATOMIC
))
27482 cp_lexer_consume_token (parser
->lexer
);
27483 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27485 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27486 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27487 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27488 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27489 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27491 cp_lexer_consume_token (parser
->lexer
);
27492 cp_lexer_consume_token (parser
->lexer
);
27493 cp_lexer_consume_token (parser
->lexer
);
27494 cp_lexer_consume_token (parser
->lexer
);
27495 cp_lexer_consume_token (parser
->lexer
);
27498 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27499 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27501 cp_lexer_consume_token (parser
->lexer
);
27502 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27507 /* Handle function try blocks. */
27508 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27510 cp_lexer_consume_token (parser
->lexer
);
27511 function_try_block
= true;
27513 /* We can have braced-init-list mem-initializers before the fn body. */
27514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27516 cp_lexer_consume_token (parser
->lexer
);
27517 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27519 /* cache_group will stop after an un-nested { } pair, too. */
27520 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27523 /* variadic mem-inits have ... after the ')'. */
27524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27525 cp_lexer_consume_token (parser
->lexer
);
27528 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27529 /* Handle function try blocks. */
27530 if (function_try_block
)
27531 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27532 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27533 last
= parser
->lexer
->next_token
;
27535 /* Save away the inline definition; we will process it when the
27536 class is complete. */
27537 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27538 DECL_PENDING_INLINE_P (fn
) = 1;
27540 /* We need to know that this was defined in the class, so that
27541 friend templates are handled correctly. */
27542 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27544 /* Add FN to the queue of functions to be parsed later. */
27545 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27550 /* Save the tokens that make up the in-class initializer for a non-static
27551 data member. Returns a DEFAULT_ARG. */
27554 cp_parser_save_nsdmi (cp_parser
* parser
)
27556 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27559 /* Parse a template-argument-list, as well as the trailing ">" (but
27560 not the opening "<"). See cp_parser_template_argument_list for the
27564 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27568 tree saved_qualifying_scope
;
27569 tree saved_object_scope
;
27570 bool saved_greater_than_is_operator_p
;
27571 int saved_unevaluated_operand
;
27572 int saved_inhibit_evaluation_warnings
;
27576 When parsing a template-id, the first non-nested `>' is taken as
27577 the end of the template-argument-list rather than a greater-than
27579 saved_greater_than_is_operator_p
27580 = parser
->greater_than_is_operator_p
;
27581 parser
->greater_than_is_operator_p
= false;
27582 /* Parsing the argument list may modify SCOPE, so we save it
27584 saved_scope
= parser
->scope
;
27585 saved_qualifying_scope
= parser
->qualifying_scope
;
27586 saved_object_scope
= parser
->object_scope
;
27587 /* We need to evaluate the template arguments, even though this
27588 template-id may be nested within a "sizeof". */
27589 saved_unevaluated_operand
= cp_unevaluated_operand
;
27590 cp_unevaluated_operand
= 0;
27591 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27592 c_inhibit_evaluation_warnings
= 0;
27593 /* Parse the template-argument-list itself. */
27594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27595 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27596 arguments
= NULL_TREE
;
27598 arguments
= cp_parser_template_argument_list (parser
);
27599 /* Look for the `>' that ends the template-argument-list. If we find
27600 a '>>' instead, it's probably just a typo. */
27601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27603 if (cxx_dialect
!= cxx98
)
27605 /* In C++0x, a `>>' in a template argument list or cast
27606 expression is considered to be two separate `>'
27607 tokens. So, change the current token to a `>', but don't
27608 consume it: it will be consumed later when the outer
27609 template argument list (or cast expression) is parsed.
27610 Note that this replacement of `>' for `>>' is necessary
27611 even if we are parsing tentatively: in the tentative
27612 case, after calling
27613 cp_parser_enclosed_template_argument_list we will always
27614 throw away all of the template arguments and the first
27615 closing `>', either because the template argument list
27616 was erroneous or because we are replacing those tokens
27617 with a CPP_TEMPLATE_ID token. The second `>' (which will
27618 not have been thrown away) is needed either to close an
27619 outer template argument list or to complete a new-style
27621 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27622 token
->type
= CPP_GREATER
;
27624 else if (!saved_greater_than_is_operator_p
)
27626 /* If we're in a nested template argument list, the '>>' has
27627 to be a typo for '> >'. We emit the error message, but we
27628 continue parsing and we push a '>' as next token, so that
27629 the argument list will be parsed correctly. Note that the
27630 global source location is still on the token before the
27631 '>>', so we need to say explicitly where we want it. */
27632 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27633 gcc_rich_location
richloc (token
->location
);
27634 richloc
.add_fixit_replace ("> >");
27635 error_at (&richloc
, "%<>>%> should be %<> >%> "
27636 "within a nested template argument list");
27638 token
->type
= CPP_GREATER
;
27642 /* If this is not a nested template argument list, the '>>'
27643 is a typo for '>'. Emit an error message and continue.
27644 Same deal about the token location, but here we can get it
27645 right by consuming the '>>' before issuing the diagnostic. */
27646 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27647 error_at (token
->location
,
27648 "spurious %<>>%>, use %<>%> to terminate "
27649 "a template argument list");
27653 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27654 /* The `>' token might be a greater-than operator again now. */
27655 parser
->greater_than_is_operator_p
27656 = saved_greater_than_is_operator_p
;
27657 /* Restore the SAVED_SCOPE. */
27658 parser
->scope
= saved_scope
;
27659 parser
->qualifying_scope
= saved_qualifying_scope
;
27660 parser
->object_scope
= saved_object_scope
;
27661 cp_unevaluated_operand
= saved_unevaluated_operand
;
27662 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27667 /* MEMBER_FUNCTION is a member function, or a friend. If default
27668 arguments, or the body of the function have not yet been parsed,
27672 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27674 timevar_push (TV_PARSE_INMETH
);
27675 /* If this member is a template, get the underlying
27677 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27678 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27680 /* There should not be any class definitions in progress at this
27681 point; the bodies of members are only parsed outside of all class
27683 gcc_assert (parser
->num_classes_being_defined
== 0);
27684 /* While we're parsing the member functions we might encounter more
27685 classes. We want to handle them right away, but we don't want
27686 them getting mixed up with functions that are currently in the
27688 push_unparsed_function_queues (parser
);
27690 /* Make sure that any template parameters are in scope. */
27691 maybe_begin_member_template_processing (member_function
);
27693 /* If the body of the function has not yet been parsed, parse it
27695 if (DECL_PENDING_INLINE_P (member_function
))
27697 tree function_scope
;
27698 cp_token_cache
*tokens
;
27700 /* The function is no longer pending; we are processing it. */
27701 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27702 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27703 DECL_PENDING_INLINE_P (member_function
) = 0;
27705 /* If this is a local class, enter the scope of the containing
27707 function_scope
= current_function_decl
;
27708 if (function_scope
)
27709 push_function_context ();
27711 /* Push the body of the function onto the lexer stack. */
27712 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27714 /* Let the front end know that we going to be defining this
27716 start_preparsed_function (member_function
, NULL_TREE
,
27717 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27719 /* Don't do access checking if it is a templated function. */
27720 if (processing_template_decl
)
27721 push_deferring_access_checks (dk_no_check
);
27723 /* #pragma omp declare reduction needs special parsing. */
27724 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27726 parser
->lexer
->in_pragma
= true;
27727 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27728 finish_function (/*inline_p=*/true);
27729 cp_check_omp_declare_reduction (member_function
);
27732 /* Now, parse the body of the function. */
27733 cp_parser_function_definition_after_declarator (parser
,
27734 /*inline_p=*/true);
27736 if (processing_template_decl
)
27737 pop_deferring_access_checks ();
27739 /* Leave the scope of the containing function. */
27740 if (function_scope
)
27741 pop_function_context ();
27742 cp_parser_pop_lexer (parser
);
27745 /* Remove any template parameters from the symbol table. */
27746 maybe_end_member_template_processing ();
27748 /* Restore the queue. */
27749 pop_unparsed_function_queues (parser
);
27750 timevar_pop (TV_PARSE_INMETH
);
27753 /* If DECL contains any default args, remember it on the unparsed
27754 functions queue. */
27757 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27761 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27763 probe
= TREE_CHAIN (probe
))
27764 if (TREE_PURPOSE (probe
))
27766 cp_default_arg_entry entry
= {current_class_type
, decl
};
27767 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27772 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27773 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27774 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27775 from the parameter-type-list. */
27778 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27779 tree default_arg
, tree parmtype
)
27781 cp_token_cache
*tokens
;
27785 if (default_arg
== error_mark_node
)
27786 return error_mark_node
;
27788 /* Push the saved tokens for the default argument onto the parser's
27790 tokens
= DEFARG_TOKENS (default_arg
);
27791 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27793 start_lambda_scope (decl
);
27795 /* Parse the default argument. */
27796 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27797 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27798 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27800 finish_lambda_scope ();
27802 if (parsed_arg
== error_mark_node
)
27803 cp_parser_skip_to_end_of_statement (parser
);
27805 if (!processing_template_decl
)
27807 /* In a non-template class, check conversions now. In a template,
27808 we'll wait and instantiate these as needed. */
27809 if (TREE_CODE (decl
) == PARM_DECL
)
27810 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27811 tf_warning_or_error
);
27812 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27813 parsed_arg
= error_mark_node
;
27815 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27818 /* If the token stream has not been completely used up, then
27819 there was extra junk after the end of the default
27821 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27823 if (TREE_CODE (decl
) == PARM_DECL
)
27824 cp_parser_error (parser
, "expected %<,%>");
27826 cp_parser_error (parser
, "expected %<;%>");
27829 /* Revert to the main lexer. */
27830 cp_parser_pop_lexer (parser
);
27835 /* FIELD is a non-static data member with an initializer which we saved for
27836 later; parse it now. */
27839 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27843 maybe_begin_member_template_processing (field
);
27845 push_unparsed_function_queues (parser
);
27846 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27847 DECL_INITIAL (field
),
27849 pop_unparsed_function_queues (parser
);
27851 maybe_end_member_template_processing ();
27853 DECL_INITIAL (field
) = def
;
27856 /* FN is a FUNCTION_DECL which may contains a parameter with an
27857 unparsed DEFAULT_ARG. Parse the default args now. This function
27858 assumes that the current scope is the scope in which the default
27859 argument should be processed. */
27862 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27864 bool saved_local_variables_forbidden_p
;
27865 tree parm
, parmdecl
;
27867 /* While we're parsing the default args, we might (due to the
27868 statement expression extension) encounter more classes. We want
27869 to handle them right away, but we don't want them getting mixed
27870 up with default args that are currently in the queue. */
27871 push_unparsed_function_queues (parser
);
27873 /* Local variable names (and the `this' keyword) may not appear
27874 in a default argument. */
27875 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27876 parser
->local_variables_forbidden_p
= true;
27878 push_defarg_context (fn
);
27880 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27881 parmdecl
= DECL_ARGUMENTS (fn
);
27882 parm
&& parm
!= void_list_node
;
27883 parm
= TREE_CHAIN (parm
),
27884 parmdecl
= DECL_CHAIN (parmdecl
))
27886 tree default_arg
= TREE_PURPOSE (parm
);
27888 vec
<tree
, va_gc
> *insts
;
27895 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27896 /* This can happen for a friend declaration for a function
27897 already declared with default arguments. */
27901 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27903 TREE_VALUE (parm
));
27904 TREE_PURPOSE (parm
) = parsed_arg
;
27906 /* Update any instantiations we've already created. */
27907 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27908 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27909 TREE_PURPOSE (copy
) = parsed_arg
;
27912 pop_defarg_context ();
27914 /* Make sure no default arg is missing. */
27915 check_default_args (fn
);
27917 /* Restore the state of local_variables_forbidden_p. */
27918 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27920 /* Restore the queue. */
27921 pop_unparsed_function_queues (parser
);
27924 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27926 sizeof ... ( identifier )
27928 where the 'sizeof' token has already been consumed. */
27931 cp_parser_sizeof_pack (cp_parser
*parser
)
27933 /* Consume the `...'. */
27934 cp_lexer_consume_token (parser
->lexer
);
27935 maybe_warn_variadic_templates ();
27937 matching_parens parens
;
27938 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27940 parens
.consume_open (parser
);
27942 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27943 "%<sizeof...%> argument must be surrounded by parentheses");
27945 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27946 tree name
= cp_parser_identifier (parser
);
27947 if (name
== error_mark_node
)
27948 return error_mark_node
;
27949 /* The name is not qualified. */
27950 parser
->scope
= NULL_TREE
;
27951 parser
->qualifying_scope
= NULL_TREE
;
27952 parser
->object_scope
= NULL_TREE
;
27953 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27954 if (expr
== error_mark_node
)
27955 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27957 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27958 expr
= TREE_TYPE (expr
);
27959 else if (TREE_CODE (expr
) == CONST_DECL
)
27960 expr
= DECL_INITIAL (expr
);
27961 expr
= make_pack_expansion (expr
);
27962 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27965 parens
.require_close (parser
);
27970 /* Parse the operand of `sizeof' (or a similar operator). Returns
27971 either a TYPE or an expression, depending on the form of the
27972 input. The KEYWORD indicates which kind of expression we have
27976 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27978 tree expr
= NULL_TREE
;
27979 const char *saved_message
;
27981 bool saved_integral_constant_expression_p
;
27982 bool saved_non_integral_constant_expression_p
;
27984 /* If it's a `...', then we are computing the length of a parameter
27986 if (keyword
== RID_SIZEOF
27987 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27988 return cp_parser_sizeof_pack (parser
);
27990 /* Types cannot be defined in a `sizeof' expression. Save away the
27992 saved_message
= parser
->type_definition_forbidden_message
;
27993 /* And create the new one. */
27994 tmp
= concat ("types may not be defined in %<",
27995 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27996 "%> expressions", NULL
);
27997 parser
->type_definition_forbidden_message
= tmp
;
27999 /* The restrictions on constant-expressions do not apply inside
28000 sizeof expressions. */
28001 saved_integral_constant_expression_p
28002 = parser
->integral_constant_expression_p
;
28003 saved_non_integral_constant_expression_p
28004 = parser
->non_integral_constant_expression_p
;
28005 parser
->integral_constant_expression_p
= false;
28007 /* Do not actually evaluate the expression. */
28008 ++cp_unevaluated_operand
;
28009 ++c_inhibit_evaluation_warnings
;
28010 /* If it's a `(', then we might be looking at the type-id
28012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28014 tree type
= NULL_TREE
;
28016 /* We can't be sure yet whether we're looking at a type-id or an
28018 cp_parser_parse_tentatively (parser
);
28020 matching_parens parens
;
28021 parens
.consume_open (parser
);
28023 /* Note: as a GNU Extension, compound literals are considered
28024 postfix-expressions as they are in C99, so they are valid
28025 arguments to sizeof. See comment in cp_parser_cast_expression
28027 if (cp_parser_compound_literal_p (parser
))
28028 cp_parser_simulate_error (parser
);
28031 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28032 parser
->in_type_id_in_expr_p
= true;
28033 /* Look for the type-id. */
28034 type
= cp_parser_type_id (parser
);
28035 /* Look for the closing `)'. */
28036 parens
.require_close (parser
);
28037 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28040 /* If all went well, then we're done. */
28041 if (cp_parser_parse_definitely (parser
))
28043 cp_decl_specifier_seq decl_specs
;
28045 /* Build a trivial decl-specifier-seq. */
28046 clear_decl_specs (&decl_specs
);
28047 decl_specs
.type
= type
;
28049 /* Call grokdeclarator to figure out what type this is. */
28050 expr
= grokdeclarator (NULL
,
28054 /*attrlist=*/NULL
);
28058 /* If the type-id production did not work out, then we must be
28059 looking at the unary-expression production. */
28061 expr
= cp_parser_unary_expression (parser
);
28063 /* Go back to evaluating expressions. */
28064 --cp_unevaluated_operand
;
28065 --c_inhibit_evaluation_warnings
;
28067 /* Free the message we created. */
28069 /* And restore the old one. */
28070 parser
->type_definition_forbidden_message
= saved_message
;
28071 parser
->integral_constant_expression_p
28072 = saved_integral_constant_expression_p
;
28073 parser
->non_integral_constant_expression_p
28074 = saved_non_integral_constant_expression_p
;
28079 /* If the current declaration has no declarator, return true. */
28082 cp_parser_declares_only_class_p (cp_parser
*parser
)
28084 /* If the next token is a `;' or a `,' then there is no
28086 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28087 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28090 /* Update the DECL_SPECS to reflect the storage class indicated by
28094 cp_parser_set_storage_class (cp_parser
*parser
,
28095 cp_decl_specifier_seq
*decl_specs
,
28099 cp_storage_class storage_class
;
28101 if (parser
->in_unbraced_linkage_specification_p
)
28103 error_at (token
->location
, "invalid use of %qD in linkage specification",
28104 ridpointers
[keyword
]);
28107 else if (decl_specs
->storage_class
!= sc_none
)
28109 decl_specs
->conflicting_specifiers_p
= true;
28113 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28114 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28115 && decl_specs
->gnu_thread_keyword_p
)
28117 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28118 "%<__thread%> before %qD", ridpointers
[keyword
]);
28124 storage_class
= sc_auto
;
28127 storage_class
= sc_register
;
28130 storage_class
= sc_static
;
28133 storage_class
= sc_extern
;
28136 storage_class
= sc_mutable
;
28139 gcc_unreachable ();
28141 decl_specs
->storage_class
= storage_class
;
28142 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28144 /* A storage class specifier cannot be applied alongside a typedef
28145 specifier. If there is a typedef specifier present then set
28146 conflicting_specifiers_p which will trigger an error later
28147 on in grokdeclarator. */
28148 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28149 decl_specs
->conflicting_specifiers_p
= true;
28152 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28153 is true, the type is a class or enum definition. */
28156 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28159 bool type_definition_p
)
28161 decl_specs
->any_specifiers_p
= true;
28163 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28164 (with, for example, in "typedef int wchar_t;") we remember that
28165 this is what happened. In system headers, we ignore these
28166 declarations so that G++ can work with system headers that are not
28168 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28169 && !type_definition_p
28170 && (type_spec
== boolean_type_node
28171 || type_spec
== char16_type_node
28172 || type_spec
== char32_type_node
28173 || type_spec
== wchar_type_node
)
28174 && (decl_specs
->type
28175 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28176 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28177 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28178 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28180 decl_specs
->redefined_builtin_type
= type_spec
;
28181 set_and_check_decl_spec_loc (decl_specs
,
28182 ds_redefined_builtin_type_spec
,
28184 if (!decl_specs
->type
)
28186 decl_specs
->type
= type_spec
;
28187 decl_specs
->type_definition_p
= false;
28188 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28191 else if (decl_specs
->type
)
28192 decl_specs
->multiple_types_p
= true;
28195 decl_specs
->type
= type_spec
;
28196 decl_specs
->type_definition_p
= type_definition_p
;
28197 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28198 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28202 /* True iff TOKEN is the GNU keyword __thread. */
28205 token_is__thread (cp_token
*token
)
28207 gcc_assert (token
->keyword
== RID_THREAD
);
28208 return id_equal (token
->u
.value
, "__thread");
28211 /* Set the location for a declarator specifier and check if it is
28214 DECL_SPECS is the sequence of declarator specifiers onto which to
28217 DS is the single declarator specifier to set which location is to
28218 be set onto the existing sequence of declarators.
28220 LOCATION is the location for the declarator specifier to
28224 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28225 cp_decl_spec ds
, cp_token
*token
)
28227 gcc_assert (ds
< ds_last
);
28229 if (decl_specs
== NULL
)
28232 source_location location
= token
->location
;
28234 if (decl_specs
->locations
[ds
] == 0)
28236 decl_specs
->locations
[ds
] = location
;
28237 if (ds
== ds_thread
)
28238 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28244 if (decl_specs
->locations
[ds_long_long
] != 0)
28245 error_at (location
,
28246 "%<long long long%> is too long for GCC");
28249 decl_specs
->locations
[ds_long_long
] = location
;
28250 pedwarn_cxx98 (location
,
28252 "ISO C++ 1998 does not support %<long long%>");
28255 else if (ds
== ds_thread
)
28257 bool gnu
= token_is__thread (token
);
28258 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28259 error_at (location
,
28260 "both %<__thread%> and %<thread_local%> specified");
28263 gcc_rich_location
richloc (location
);
28264 richloc
.add_fixit_remove ();
28265 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28270 static const char *const decl_spec_names
[] = {
28287 gcc_rich_location
richloc (location
);
28288 richloc
.add_fixit_remove ();
28289 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28294 /* Return true iff the declarator specifier DS is present in the
28295 sequence of declarator specifiers DECL_SPECS. */
28298 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28301 gcc_assert (ds
< ds_last
);
28303 if (decl_specs
== NULL
)
28306 return decl_specs
->locations
[ds
] != 0;
28309 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28310 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28313 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28315 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28318 /* Issue an error message indicating that TOKEN_DESC was expected.
28319 If KEYWORD is true, it indicated this function is called by
28320 cp_parser_require_keword and the required token can only be
28321 a indicated keyword.
28323 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28324 within any error as the location of an "opening" token matching
28325 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28326 RT_CLOSE_PAREN). */
28329 cp_parser_required_error (cp_parser
*parser
,
28330 required_token token_desc
,
28332 location_t matching_location
)
28334 if (cp_parser_simulate_error (parser
))
28337 const char *gmsgid
= NULL
;
28338 switch (token_desc
)
28341 gmsgid
= G_("expected %<new%>");
28344 gmsgid
= G_("expected %<delete%>");
28347 gmsgid
= G_("expected %<return%>");
28350 gmsgid
= G_("expected %<while%>");
28353 gmsgid
= G_("expected %<extern%>");
28355 case RT_STATIC_ASSERT
:
28356 gmsgid
= G_("expected %<static_assert%>");
28359 gmsgid
= G_("expected %<decltype%>");
28362 gmsgid
= G_("expected %<operator%>");
28365 gmsgid
= G_("expected %<class%>");
28368 gmsgid
= G_("expected %<template%>");
28371 gmsgid
= G_("expected %<namespace%>");
28374 gmsgid
= G_("expected %<using%>");
28377 gmsgid
= G_("expected %<asm%>");
28380 gmsgid
= G_("expected %<try%>");
28383 gmsgid
= G_("expected %<catch%>");
28386 gmsgid
= G_("expected %<throw%>");
28389 gmsgid
= G_("expected %<__label__%>");
28392 gmsgid
= G_("expected %<@try%>");
28394 case RT_AT_SYNCHRONIZED
:
28395 gmsgid
= G_("expected %<@synchronized%>");
28398 gmsgid
= G_("expected %<@throw%>");
28400 case RT_TRANSACTION_ATOMIC
:
28401 gmsgid
= G_("expected %<__transaction_atomic%>");
28403 case RT_TRANSACTION_RELAXED
:
28404 gmsgid
= G_("expected %<__transaction_relaxed%>");
28410 if (!gmsgid
&& !keyword
)
28412 switch (token_desc
)
28415 gmsgid
= G_("expected %<;%>");
28417 case RT_OPEN_PAREN
:
28418 gmsgid
= G_("expected %<(%>");
28420 case RT_CLOSE_BRACE
:
28421 gmsgid
= G_("expected %<}%>");
28423 case RT_OPEN_BRACE
:
28424 gmsgid
= G_("expected %<{%>");
28426 case RT_CLOSE_SQUARE
:
28427 gmsgid
= G_("expected %<]%>");
28429 case RT_OPEN_SQUARE
:
28430 gmsgid
= G_("expected %<[%>");
28433 gmsgid
= G_("expected %<,%>");
28436 gmsgid
= G_("expected %<::%>");
28439 gmsgid
= G_("expected %<<%>");
28442 gmsgid
= G_("expected %<>%>");
28445 gmsgid
= G_("expected %<=%>");
28448 gmsgid
= G_("expected %<...%>");
28451 gmsgid
= G_("expected %<*%>");
28454 gmsgid
= G_("expected %<~%>");
28457 gmsgid
= G_("expected %<:%>");
28459 case RT_COLON_SCOPE
:
28460 gmsgid
= G_("expected %<:%> or %<::%>");
28462 case RT_CLOSE_PAREN
:
28463 gmsgid
= G_("expected %<)%>");
28465 case RT_COMMA_CLOSE_PAREN
:
28466 gmsgid
= G_("expected %<,%> or %<)%>");
28468 case RT_PRAGMA_EOL
:
28469 gmsgid
= G_("expected end of line");
28472 gmsgid
= G_("expected identifier");
28475 gmsgid
= G_("expected selection-statement");
28478 gmsgid
= G_("expected iteration-statement");
28481 gmsgid
= G_("expected jump-statement");
28484 gmsgid
= G_("expected class-key");
28486 case RT_CLASS_TYPENAME_TEMPLATE
:
28487 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28490 gcc_unreachable ();
28495 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28499 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28500 issue an error message indicating that TOKEN_DESC was expected.
28502 Returns the token consumed, if the token had the appropriate type.
28503 Otherwise, returns NULL.
28505 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28506 within any error as the location of an "opening" token matching
28507 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28508 RT_CLOSE_PAREN). */
28511 cp_parser_require (cp_parser
* parser
,
28512 enum cpp_ttype type
,
28513 required_token token_desc
,
28514 location_t matching_location
)
28516 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28517 return cp_lexer_consume_token (parser
->lexer
);
28520 /* Output the MESSAGE -- unless we're parsing tentatively. */
28521 if (!cp_parser_simulate_error (parser
))
28522 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28523 matching_location
);
28528 /* An error message is produced if the next token is not '>'.
28529 All further tokens are skipped until the desired token is
28530 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28533 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28535 /* Current level of '< ... >'. */
28536 unsigned level
= 0;
28537 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28538 unsigned nesting_depth
= 0;
28540 /* Are we ready, yet? If not, issue error message. */
28541 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28544 /* Skip tokens until the desired token is found. */
28547 /* Peek at the next token. */
28548 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28551 if (!nesting_depth
)
28556 if (cxx_dialect
== cxx98
)
28557 /* C++0x views the `>>' operator as two `>' tokens, but
28560 else if (!nesting_depth
&& level
-- == 0)
28562 /* We've hit a `>>' where the first `>' closes the
28563 template argument list, and the second `>' is
28564 spurious. Just consume the `>>' and stop; we've
28565 already produced at least one error. */
28566 cp_lexer_consume_token (parser
->lexer
);
28569 /* Fall through for C++0x, so we handle the second `>' in
28571 gcc_fallthrough ();
28574 if (!nesting_depth
&& level
-- == 0)
28576 /* We've reached the token we want, consume it and stop. */
28577 cp_lexer_consume_token (parser
->lexer
);
28582 case CPP_OPEN_PAREN
:
28583 case CPP_OPEN_SQUARE
:
28587 case CPP_CLOSE_PAREN
:
28588 case CPP_CLOSE_SQUARE
:
28589 if (nesting_depth
-- == 0)
28594 case CPP_PRAGMA_EOL
:
28595 case CPP_SEMICOLON
:
28596 case CPP_OPEN_BRACE
:
28597 case CPP_CLOSE_BRACE
:
28598 /* The '>' was probably forgotten, don't look further. */
28605 /* Consume this token. */
28606 cp_lexer_consume_token (parser
->lexer
);
28610 /* If the next token is the indicated keyword, consume it. Otherwise,
28611 issue an error message indicating that TOKEN_DESC was expected.
28613 Returns the token consumed, if the token had the appropriate type.
28614 Otherwise, returns NULL. */
28617 cp_parser_require_keyword (cp_parser
* parser
,
28619 required_token token_desc
)
28621 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28623 if (token
&& token
->keyword
!= keyword
)
28625 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28633 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28634 function-definition. */
28637 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28639 return (/* An ordinary function-body begins with an `{'. */
28640 token
->type
== CPP_OPEN_BRACE
28641 /* A ctor-initializer begins with a `:'. */
28642 || token
->type
== CPP_COLON
28643 /* A function-try-block begins with `try'. */
28644 || token
->keyword
== RID_TRY
28645 /* A function-transaction-block begins with `__transaction_atomic'
28646 or `__transaction_relaxed'. */
28647 || token
->keyword
== RID_TRANSACTION_ATOMIC
28648 || token
->keyword
== RID_TRANSACTION_RELAXED
28649 /* The named return value extension begins with `return'. */
28650 || token
->keyword
== RID_RETURN
);
28653 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28657 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28661 token
= cp_lexer_peek_token (parser
->lexer
);
28662 return (token
->type
== CPP_OPEN_BRACE
28663 || (token
->type
== CPP_COLON
28664 && !parser
->colon_doesnt_start_class_def_p
));
28667 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28668 C++0x) ending a template-argument. */
28671 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28675 token
= cp_lexer_peek_token (parser
->lexer
);
28676 return (token
->type
== CPP_COMMA
28677 || token
->type
== CPP_GREATER
28678 || token
->type
== CPP_ELLIPSIS
28679 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28682 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28683 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28686 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28691 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28692 if (token
->type
== CPP_LESS
)
28694 /* Check for the sequence `<::' in the original code. It would be lexed as
28695 `[:', where `[' is a digraph, and there is no whitespace before
28697 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28700 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28701 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28707 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28708 or none_type otherwise. */
28710 static enum tag_types
28711 cp_parser_token_is_class_key (cp_token
* token
)
28713 switch (token
->keyword
)
28718 return record_type
;
28727 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28728 or none_type otherwise or if the token is null. */
28730 static enum tag_types
28731 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28736 switch (token
->keyword
)
28741 return typename_type
;
28748 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28751 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28753 if (type
== error_mark_node
)
28755 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28757 if (permerror (input_location
, "%qs tag used in naming %q#T",
28758 class_key
== union_type
? "union"
28759 : class_key
== record_type
? "struct" : "class",
28761 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28762 "%q#T was previously declared here", type
);
28766 /* Issue an error message if DECL is redeclared with different
28767 access than its original declaration [class.access.spec/3].
28768 This applies to nested classes, nested class templates and
28769 enumerations [class.mem/1]. */
28772 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28775 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28776 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28779 if ((TREE_PRIVATE (decl
)
28780 != (current_access_specifier
== access_private_node
))
28781 || (TREE_PROTECTED (decl
)
28782 != (current_access_specifier
== access_protected_node
)))
28783 error_at (location
, "%qD redeclared with different access", decl
);
28786 /* Look for the `template' keyword, as a syntactic disambiguator.
28787 Return TRUE iff it is present, in which case it will be
28791 cp_parser_optional_template_keyword (cp_parser
*parser
)
28793 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28795 /* In C++98 the `template' keyword can only be used within templates;
28796 outside templates the parser can always figure out what is a
28797 template and what is not. In C++11, per the resolution of DR 468,
28798 `template' is allowed in cases where it is not strictly necessary. */
28799 if (!processing_template_decl
28800 && pedantic
&& cxx_dialect
== cxx98
)
28802 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28803 pedwarn (token
->location
, OPT_Wpedantic
,
28804 "in C++98 %<template%> (as a disambiguator) is only "
28805 "allowed within templates");
28806 /* If this part of the token stream is rescanned, the same
28807 error message would be generated. So, we purge the token
28808 from the stream. */
28809 cp_lexer_purge_token (parser
->lexer
);
28814 /* Consume the `template' keyword. */
28815 cp_lexer_consume_token (parser
->lexer
);
28822 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28823 set PARSER->SCOPE, and perform other related actions. */
28826 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28828 struct tree_check
*check_value
;
28830 /* Get the stored value. */
28831 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28832 /* Set the scope from the stored value. */
28833 parser
->scope
= saved_checks_value (check_value
);
28834 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28835 parser
->object_scope
= NULL_TREE
;
28838 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28839 encounter the end of a block before what we were looking for. */
28842 cp_parser_cache_group (cp_parser
*parser
,
28843 enum cpp_ttype end
,
28848 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28850 /* Abort a parenthesized expression if we encounter a semicolon. */
28851 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28852 && token
->type
== CPP_SEMICOLON
)
28854 /* If we've reached the end of the file, stop. */
28855 if (token
->type
== CPP_EOF
28856 || (end
!= CPP_PRAGMA_EOL
28857 && token
->type
== CPP_PRAGMA_EOL
))
28859 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28860 /* We've hit the end of an enclosing block, so there's been some
28861 kind of syntax error. */
28864 /* Consume the token. */
28865 cp_lexer_consume_token (parser
->lexer
);
28866 /* See if it starts a new group. */
28867 if (token
->type
== CPP_OPEN_BRACE
)
28869 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28870 /* In theory this should probably check end == '}', but
28871 cp_parser_save_member_function_body needs it to exit
28872 after either '}' or ')' when called with ')'. */
28876 else if (token
->type
== CPP_OPEN_PAREN
)
28878 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28879 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28882 else if (token
->type
== CPP_PRAGMA
)
28883 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28884 else if (token
->type
== end
)
28889 /* Like above, for caching a default argument or NSDMI. Both of these are
28890 terminated by a non-nested comma, but it can be unclear whether or not a
28891 comma is nested in a template argument list unless we do more parsing.
28892 In order to handle this ambiguity, when we encounter a ',' after a '<'
28893 we try to parse what follows as a parameter-declaration-list (in the
28894 case of a default argument) or a member-declarator (in the case of an
28895 NSDMI). If that succeeds, then we stop caching. */
28898 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28900 unsigned depth
= 0;
28901 int maybe_template_id
= 0;
28902 cp_token
*first_token
;
28904 tree default_argument
;
28906 /* Add tokens until we have processed the entire default
28907 argument. We add the range [first_token, token). */
28908 first_token
= cp_lexer_peek_token (parser
->lexer
);
28909 if (first_token
->type
== CPP_OPEN_BRACE
)
28911 /* For list-initialization, this is straightforward. */
28912 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28913 token
= cp_lexer_peek_token (parser
->lexer
);
28919 /* Peek at the next token. */
28920 token
= cp_lexer_peek_token (parser
->lexer
);
28921 /* What we do depends on what token we have. */
28922 switch (token
->type
)
28924 /* In valid code, a default argument must be
28925 immediately followed by a `,' `)', or `...'. */
28927 if (depth
== 0 && maybe_template_id
)
28929 /* If we've seen a '<', we might be in a
28930 template-argument-list. Until Core issue 325 is
28931 resolved, we don't know how this situation ought
28932 to be handled, so try to DTRT. We check whether
28933 what comes after the comma is a valid parameter
28934 declaration list. If it is, then the comma ends
28935 the default argument; otherwise the default
28936 argument continues. */
28937 bool error
= false;
28940 /* Set ITALP so cp_parser_parameter_declaration_list
28941 doesn't decide to commit to this parse. */
28942 bool saved_italp
= parser
->in_template_argument_list_p
;
28943 parser
->in_template_argument_list_p
= true;
28945 cp_parser_parse_tentatively (parser
);
28949 /* Parse declarators until we reach a non-comma or
28950 somthing that cannot be an initializer.
28951 Just checking whether we're looking at a single
28952 declarator is insufficient. Consider:
28953 int var = tuple<T,U>::x;
28954 The template parameter 'U' looks exactly like a
28958 int ctor_dtor_or_conv_p
;
28959 cp_lexer_consume_token (parser
->lexer
);
28960 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28961 &ctor_dtor_or_conv_p
,
28962 /*parenthesized_p=*/NULL
,
28964 /*friend_p=*/false);
28965 peek
= cp_lexer_peek_token (parser
->lexer
);
28966 if (cp_parser_error_occurred (parser
))
28969 while (peek
->type
== CPP_COMMA
);
28970 /* If we met an '=' or ';' then the original comma
28971 was the end of the NSDMI. Otherwise assume
28972 we're still in the NSDMI. */
28973 error
= (peek
->type
!= CPP_EQ
28974 && peek
->type
!= CPP_SEMICOLON
);
28978 cp_lexer_consume_token (parser
->lexer
);
28979 begin_scope (sk_function_parms
, NULL_TREE
);
28980 cp_parser_parameter_declaration_list (parser
, &error
);
28981 pop_bindings_and_leave_scope ();
28983 if (!cp_parser_error_occurred (parser
) && !error
)
28985 cp_parser_abort_tentative_parse (parser
);
28987 parser
->in_template_argument_list_p
= saved_italp
;
28991 case CPP_CLOSE_PAREN
:
28993 /* If we run into a non-nested `;', `}', or `]',
28994 then the code is invalid -- but the default
28995 argument is certainly over. */
28996 case CPP_SEMICOLON
:
28997 case CPP_CLOSE_BRACE
:
28998 case CPP_CLOSE_SQUARE
:
29000 /* Handle correctly int n = sizeof ... ( p ); */
29001 && token
->type
!= CPP_ELLIPSIS
)
29003 /* Update DEPTH, if necessary. */
29004 else if (token
->type
== CPP_CLOSE_PAREN
29005 || token
->type
== CPP_CLOSE_BRACE
29006 || token
->type
== CPP_CLOSE_SQUARE
)
29010 case CPP_OPEN_PAREN
:
29011 case CPP_OPEN_SQUARE
:
29012 case CPP_OPEN_BRACE
:
29018 /* This might be the comparison operator, or it might
29019 start a template argument list. */
29020 ++maybe_template_id
;
29024 if (cxx_dialect
== cxx98
)
29026 /* Fall through for C++0x, which treats the `>>'
29027 operator like two `>' tokens in certain
29029 gcc_fallthrough ();
29034 /* This might be an operator, or it might close a
29035 template argument list. But if a previous '<'
29036 started a template argument list, this will have
29037 closed it, so we can't be in one anymore. */
29038 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29039 if (maybe_template_id
< 0)
29040 maybe_template_id
= 0;
29044 /* If we run out of tokens, issue an error message. */
29046 case CPP_PRAGMA_EOL
:
29047 error_at (token
->location
, "file ends in default argument");
29048 return error_mark_node
;
29052 /* In these cases, we should look for template-ids.
29053 For example, if the default argument is
29054 `X<int, double>()', we need to do name lookup to
29055 figure out whether or not `X' is a template; if
29056 so, the `,' does not end the default argument.
29058 That is not yet done. */
29065 /* If we've reached the end, stop. */
29069 /* Add the token to the token block. */
29070 token
= cp_lexer_consume_token (parser
->lexer
);
29073 /* Create a DEFAULT_ARG to represent the unparsed default
29075 default_argument
= make_node (DEFAULT_ARG
);
29076 DEFARG_TOKENS (default_argument
)
29077 = cp_token_cache_new (first_token
, token
);
29078 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29080 return default_argument
;
29083 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29086 defarg_location (tree default_argument
)
29088 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29089 location_t start
= tokens
->first
->location
;
29090 location_t end
= tokens
->last
->location
;
29091 return make_location (start
, start
, end
);
29094 /* Begin parsing tentatively. We always save tokens while parsing
29095 tentatively so that if the tentative parsing fails we can restore the
29099 cp_parser_parse_tentatively (cp_parser
* parser
)
29101 /* Enter a new parsing context. */
29102 parser
->context
= cp_parser_context_new (parser
->context
);
29103 /* Begin saving tokens. */
29104 cp_lexer_save_tokens (parser
->lexer
);
29105 /* In order to avoid repetitive access control error messages,
29106 access checks are queued up until we are no longer parsing
29108 push_deferring_access_checks (dk_deferred
);
29111 /* Commit to the currently active tentative parse. */
29114 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29116 cp_parser_context
*context
;
29119 /* Mark all of the levels as committed. */
29120 lexer
= parser
->lexer
;
29121 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29123 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29125 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29126 while (!cp_lexer_saving_tokens (lexer
))
29127 lexer
= lexer
->next
;
29128 cp_lexer_commit_tokens (lexer
);
29132 /* Commit to the topmost currently active tentative parse.
29134 Note that this function shouldn't be called when there are
29135 irreversible side-effects while in a tentative state. For
29136 example, we shouldn't create a permanent entry in the symbol
29137 table, or issue an error message that might not apply if the
29138 tentative parse is aborted. */
29141 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29143 cp_parser_context
*context
= parser
->context
;
29144 cp_lexer
*lexer
= parser
->lexer
;
29148 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29150 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29152 while (!cp_lexer_saving_tokens (lexer
))
29153 lexer
= lexer
->next
;
29154 cp_lexer_commit_tokens (lexer
);
29158 /* Abort the currently active tentative parse. All consumed tokens
29159 will be rolled back, and no diagnostics will be issued. */
29162 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29164 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29165 || errorcount
> 0);
29166 cp_parser_simulate_error (parser
);
29167 /* Now, pretend that we want to see if the construct was
29168 successfully parsed. */
29169 cp_parser_parse_definitely (parser
);
29172 /* Stop parsing tentatively. If a parse error has occurred, restore the
29173 token stream. Otherwise, commit to the tokens we have consumed.
29174 Returns true if no error occurred; false otherwise. */
29177 cp_parser_parse_definitely (cp_parser
* parser
)
29179 bool error_occurred
;
29180 cp_parser_context
*context
;
29182 /* Remember whether or not an error occurred, since we are about to
29183 destroy that information. */
29184 error_occurred
= cp_parser_error_occurred (parser
);
29185 /* Remove the topmost context from the stack. */
29186 context
= parser
->context
;
29187 parser
->context
= context
->next
;
29188 /* If no parse errors occurred, commit to the tentative parse. */
29189 if (!error_occurred
)
29191 /* Commit to the tokens read tentatively, unless that was
29193 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29194 cp_lexer_commit_tokens (parser
->lexer
);
29196 pop_to_parent_deferring_access_checks ();
29198 /* Otherwise, if errors occurred, roll back our state so that things
29199 are just as they were before we began the tentative parse. */
29202 cp_lexer_rollback_tokens (parser
->lexer
);
29203 pop_deferring_access_checks ();
29205 /* Add the context to the front of the free list. */
29206 context
->next
= cp_parser_context_free_list
;
29207 cp_parser_context_free_list
= context
;
29209 return !error_occurred
;
29212 /* Returns true if we are parsing tentatively and are not committed to
29213 this tentative parse. */
29216 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29218 return (cp_parser_parsing_tentatively (parser
)
29219 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29222 /* Returns nonzero iff an error has occurred during the most recent
29223 tentative parse. */
29226 cp_parser_error_occurred (cp_parser
* parser
)
29228 return (cp_parser_parsing_tentatively (parser
)
29229 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29232 /* Returns nonzero if GNU extensions are allowed. */
29235 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29237 return parser
->allow_gnu_extensions_p
;
29240 /* Objective-C++ Productions */
29243 /* Parse an Objective-C expression, which feeds into a primary-expression
29247 objc-message-expression
29248 objc-string-literal
29249 objc-encode-expression
29250 objc-protocol-expression
29251 objc-selector-expression
29253 Returns a tree representation of the expression. */
29256 cp_parser_objc_expression (cp_parser
* parser
)
29258 /* Try to figure out what kind of declaration is present. */
29259 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29263 case CPP_OPEN_SQUARE
:
29264 return cp_parser_objc_message_expression (parser
);
29266 case CPP_OBJC_STRING
:
29267 kwd
= cp_lexer_consume_token (parser
->lexer
);
29268 return objc_build_string_object (kwd
->u
.value
);
29271 switch (kwd
->keyword
)
29273 case RID_AT_ENCODE
:
29274 return cp_parser_objc_encode_expression (parser
);
29276 case RID_AT_PROTOCOL
:
29277 return cp_parser_objc_protocol_expression (parser
);
29279 case RID_AT_SELECTOR
:
29280 return cp_parser_objc_selector_expression (parser
);
29287 error_at (kwd
->location
,
29288 "misplaced %<@%D%> Objective-C++ construct",
29290 cp_parser_skip_to_end_of_block_or_statement (parser
);
29293 return error_mark_node
;
29296 /* Parse an Objective-C message expression.
29298 objc-message-expression:
29299 [ objc-message-receiver objc-message-args ]
29301 Returns a representation of an Objective-C message. */
29304 cp_parser_objc_message_expression (cp_parser
* parser
)
29306 tree receiver
, messageargs
;
29308 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29309 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29310 receiver
= cp_parser_objc_message_receiver (parser
);
29311 messageargs
= cp_parser_objc_message_args (parser
);
29312 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29313 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29315 tree result
= objc_build_message_expr (receiver
, messageargs
);
29317 /* Construct a location e.g.
29320 ranging from the '[' to the ']', with the caret at the start. */
29321 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29322 protected_set_expr_location (result
, combined_loc
);
29327 /* Parse an objc-message-receiver.
29329 objc-message-receiver:
29331 simple-type-specifier
29333 Returns a representation of the type or expression. */
29336 cp_parser_objc_message_receiver (cp_parser
* parser
)
29340 /* An Objective-C message receiver may be either (1) a type
29341 or (2) an expression. */
29342 cp_parser_parse_tentatively (parser
);
29343 rcv
= cp_parser_expression (parser
);
29345 /* If that worked out, fine. */
29346 if (cp_parser_parse_definitely (parser
))
29349 cp_parser_parse_tentatively (parser
);
29350 rcv
= cp_parser_simple_type_specifier (parser
,
29351 /*decl_specs=*/NULL
,
29352 CP_PARSER_FLAGS_NONE
);
29354 if (cp_parser_parse_definitely (parser
))
29355 return objc_get_class_reference (rcv
);
29357 cp_parser_error (parser
, "objective-c++ message receiver expected");
29358 return error_mark_node
;
29361 /* Parse the arguments and selectors comprising an Objective-C message.
29366 objc-selector-args , objc-comma-args
29368 objc-selector-args:
29369 objc-selector [opt] : assignment-expression
29370 objc-selector-args objc-selector [opt] : assignment-expression
29373 assignment-expression
29374 objc-comma-args , assignment-expression
29376 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29377 selector arguments and TREE_VALUE containing a list of comma
29381 cp_parser_objc_message_args (cp_parser
* parser
)
29383 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29384 bool maybe_unary_selector_p
= true;
29385 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29387 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29389 tree selector
= NULL_TREE
, arg
;
29391 if (token
->type
!= CPP_COLON
)
29392 selector
= cp_parser_objc_selector (parser
);
29394 /* Detect if we have a unary selector. */
29395 if (maybe_unary_selector_p
29396 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29397 return build_tree_list (selector
, NULL_TREE
);
29399 maybe_unary_selector_p
= false;
29400 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29401 arg
= cp_parser_assignment_expression (parser
);
29404 = chainon (sel_args
,
29405 build_tree_list (selector
, arg
));
29407 token
= cp_lexer_peek_token (parser
->lexer
);
29410 /* Handle non-selector arguments, if any. */
29411 while (token
->type
== CPP_COMMA
)
29415 cp_lexer_consume_token (parser
->lexer
);
29416 arg
= cp_parser_assignment_expression (parser
);
29419 = chainon (addl_args
,
29420 build_tree_list (NULL_TREE
, arg
));
29422 token
= cp_lexer_peek_token (parser
->lexer
);
29425 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29427 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29428 return build_tree_list (error_mark_node
, error_mark_node
);
29431 return build_tree_list (sel_args
, addl_args
);
29434 /* Parse an Objective-C encode expression.
29436 objc-encode-expression:
29437 @encode objc-typename
29439 Returns an encoded representation of the type argument. */
29442 cp_parser_objc_encode_expression (cp_parser
* parser
)
29446 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29448 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29449 matching_parens parens
;
29450 parens
.require_open (parser
);
29451 token
= cp_lexer_peek_token (parser
->lexer
);
29452 type
= complete_type (cp_parser_type_id (parser
));
29453 parens
.require_close (parser
);
29457 error_at (token
->location
,
29458 "%<@encode%> must specify a type as an argument");
29459 return error_mark_node
;
29462 /* This happens if we find @encode(T) (where T is a template
29463 typename or something dependent on a template typename) when
29464 parsing a template. In that case, we can't compile it
29465 immediately, but we rather create an AT_ENCODE_EXPR which will
29466 need to be instantiated when the template is used.
29468 if (dependent_type_p (type
))
29470 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29471 TREE_READONLY (value
) = 1;
29476 /* Build a location of the form:
29479 with caret==start at the @ token, finishing at the close paren. */
29480 location_t combined_loc
29481 = make_location (start_loc
, start_loc
,
29482 cp_lexer_previous_token (parser
->lexer
)->location
);
29484 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29487 /* Parse an Objective-C @defs expression. */
29490 cp_parser_objc_defs_expression (cp_parser
*parser
)
29494 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29495 matching_parens parens
;
29496 parens
.require_open (parser
);
29497 name
= cp_parser_identifier (parser
);
29498 parens
.require_close (parser
);
29500 return objc_get_class_ivars (name
);
29503 /* Parse an Objective-C protocol expression.
29505 objc-protocol-expression:
29506 @protocol ( identifier )
29508 Returns a representation of the protocol expression. */
29511 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29514 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29516 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29517 matching_parens parens
;
29518 parens
.require_open (parser
);
29519 proto
= cp_parser_identifier (parser
);
29520 parens
.require_close (parser
);
29522 /* Build a location of the form:
29525 with caret==start at the @ token, finishing at the close paren. */
29526 location_t combined_loc
29527 = make_location (start_loc
, start_loc
,
29528 cp_lexer_previous_token (parser
->lexer
)->location
);
29529 tree result
= objc_build_protocol_expr (proto
);
29530 protected_set_expr_location (result
, combined_loc
);
29534 /* Parse an Objective-C selector expression.
29536 objc-selector-expression:
29537 @selector ( objc-method-signature )
29539 objc-method-signature:
29545 objc-selector-seq objc-selector :
29547 Returns a representation of the method selector. */
29550 cp_parser_objc_selector_expression (cp_parser
* parser
)
29552 tree sel_seq
= NULL_TREE
;
29553 bool maybe_unary_selector_p
= true;
29555 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29557 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29558 matching_parens parens
;
29559 parens
.require_open (parser
);
29560 token
= cp_lexer_peek_token (parser
->lexer
);
29562 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29563 || token
->type
== CPP_SCOPE
)
29565 tree selector
= NULL_TREE
;
29567 if (token
->type
!= CPP_COLON
29568 || token
->type
== CPP_SCOPE
)
29569 selector
= cp_parser_objc_selector (parser
);
29571 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29572 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29574 /* Detect if we have a unary selector. */
29575 if (maybe_unary_selector_p
)
29577 sel_seq
= selector
;
29578 goto finish_selector
;
29582 cp_parser_error (parser
, "expected %<:%>");
29585 maybe_unary_selector_p
= false;
29586 token
= cp_lexer_consume_token (parser
->lexer
);
29588 if (token
->type
== CPP_SCOPE
)
29591 = chainon (sel_seq
,
29592 build_tree_list (selector
, NULL_TREE
));
29594 = chainon (sel_seq
,
29595 build_tree_list (NULL_TREE
, NULL_TREE
));
29599 = chainon (sel_seq
,
29600 build_tree_list (selector
, NULL_TREE
));
29602 token
= cp_lexer_peek_token (parser
->lexer
);
29606 parens
.require_close (parser
);
29609 /* Build a location of the form:
29612 with caret==start at the @ token, finishing at the close paren. */
29613 location_t combined_loc
29614 = make_location (loc
, loc
,
29615 cp_lexer_previous_token (parser
->lexer
)->location
);
29616 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29617 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29618 protected_set_expr_location (result
, combined_loc
);
29622 /* Parse a list of identifiers.
29624 objc-identifier-list:
29626 objc-identifier-list , identifier
29628 Returns a TREE_LIST of identifier nodes. */
29631 cp_parser_objc_identifier_list (cp_parser
* parser
)
29637 identifier
= cp_parser_identifier (parser
);
29638 if (identifier
== error_mark_node
)
29639 return error_mark_node
;
29641 list
= build_tree_list (NULL_TREE
, identifier
);
29642 sep
= cp_lexer_peek_token (parser
->lexer
);
29644 while (sep
->type
== CPP_COMMA
)
29646 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29647 identifier
= cp_parser_identifier (parser
);
29648 if (identifier
== error_mark_node
)
29651 list
= chainon (list
, build_tree_list (NULL_TREE
,
29653 sep
= cp_lexer_peek_token (parser
->lexer
);
29659 /* Parse an Objective-C alias declaration.
29661 objc-alias-declaration:
29662 @compatibility_alias identifier identifier ;
29664 This function registers the alias mapping with the Objective-C front end.
29665 It returns nothing. */
29668 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29672 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29673 alias
= cp_parser_identifier (parser
);
29674 orig
= cp_parser_identifier (parser
);
29675 objc_declare_alias (alias
, orig
);
29676 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29679 /* Parse an Objective-C class forward-declaration.
29681 objc-class-declaration:
29682 @class objc-identifier-list ;
29684 The function registers the forward declarations with the Objective-C
29685 front end. It returns nothing. */
29688 cp_parser_objc_class_declaration (cp_parser
* parser
)
29690 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29695 id
= cp_parser_identifier (parser
);
29696 if (id
== error_mark_node
)
29699 objc_declare_class (id
);
29701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29702 cp_lexer_consume_token (parser
->lexer
);
29706 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29709 /* Parse a list of Objective-C protocol references.
29711 objc-protocol-refs-opt:
29712 objc-protocol-refs [opt]
29714 objc-protocol-refs:
29715 < objc-identifier-list >
29717 Returns a TREE_LIST of identifiers, if any. */
29720 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29722 tree protorefs
= NULL_TREE
;
29724 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29726 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29727 protorefs
= cp_parser_objc_identifier_list (parser
);
29728 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29734 /* Parse a Objective-C visibility specification. */
29737 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29739 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29741 switch (vis
->keyword
)
29743 case RID_AT_PRIVATE
:
29744 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29746 case RID_AT_PROTECTED
:
29747 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29749 case RID_AT_PUBLIC
:
29750 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29752 case RID_AT_PACKAGE
:
29753 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29759 /* Eat '@private'/'@protected'/'@public'. */
29760 cp_lexer_consume_token (parser
->lexer
);
29763 /* Parse an Objective-C method type. Return 'true' if it is a class
29764 (+) method, and 'false' if it is an instance (-) method. */
29767 cp_parser_objc_method_type (cp_parser
* parser
)
29769 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29775 /* Parse an Objective-C protocol qualifier. */
29778 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29780 tree quals
= NULL_TREE
, node
;
29781 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29783 node
= token
->u
.value
;
29785 while (node
&& identifier_p (node
)
29786 && (node
== ridpointers
[(int) RID_IN
]
29787 || node
== ridpointers
[(int) RID_OUT
]
29788 || node
== ridpointers
[(int) RID_INOUT
]
29789 || node
== ridpointers
[(int) RID_BYCOPY
]
29790 || node
== ridpointers
[(int) RID_BYREF
]
29791 || node
== ridpointers
[(int) RID_ONEWAY
]))
29793 quals
= tree_cons (NULL_TREE
, node
, quals
);
29794 cp_lexer_consume_token (parser
->lexer
);
29795 token
= cp_lexer_peek_token (parser
->lexer
);
29796 node
= token
->u
.value
;
29802 /* Parse an Objective-C typename. */
29805 cp_parser_objc_typename (cp_parser
* parser
)
29807 tree type_name
= NULL_TREE
;
29809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29811 tree proto_quals
, cp_type
= NULL_TREE
;
29813 matching_parens parens
;
29814 parens
.consume_open (parser
); /* Eat '('. */
29815 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29817 /* An ObjC type name may consist of just protocol qualifiers, in which
29818 case the type shall default to 'id'. */
29819 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29821 cp_type
= cp_parser_type_id (parser
);
29823 /* If the type could not be parsed, an error has already
29824 been produced. For error recovery, behave as if it had
29825 not been specified, which will use the default type
29827 if (cp_type
== error_mark_node
)
29829 cp_type
= NULL_TREE
;
29830 /* We need to skip to the closing parenthesis as
29831 cp_parser_type_id() does not seem to do it for
29833 cp_parser_skip_to_closing_parenthesis (parser
,
29834 /*recovering=*/true,
29835 /*or_comma=*/false,
29836 /*consume_paren=*/false);
29840 parens
.require_close (parser
);
29841 type_name
= build_tree_list (proto_quals
, cp_type
);
29847 /* Check to see if TYPE refers to an Objective-C selector name. */
29850 cp_parser_objc_selector_p (enum cpp_ttype type
)
29852 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29853 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29854 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29855 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29856 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29859 /* Parse an Objective-C selector. */
29862 cp_parser_objc_selector (cp_parser
* parser
)
29864 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29866 if (!cp_parser_objc_selector_p (token
->type
))
29868 error_at (token
->location
, "invalid Objective-C++ selector name");
29869 return error_mark_node
;
29872 /* C++ operator names are allowed to appear in ObjC selectors. */
29873 switch (token
->type
)
29875 case CPP_AND_AND
: return get_identifier ("and");
29876 case CPP_AND_EQ
: return get_identifier ("and_eq");
29877 case CPP_AND
: return get_identifier ("bitand");
29878 case CPP_OR
: return get_identifier ("bitor");
29879 case CPP_COMPL
: return get_identifier ("compl");
29880 case CPP_NOT
: return get_identifier ("not");
29881 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29882 case CPP_OR_OR
: return get_identifier ("or");
29883 case CPP_OR_EQ
: return get_identifier ("or_eq");
29884 case CPP_XOR
: return get_identifier ("xor");
29885 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29886 default: return token
->u
.value
;
29890 /* Parse an Objective-C params list. */
29893 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29895 tree params
= NULL_TREE
;
29896 bool maybe_unary_selector_p
= true;
29897 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29899 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29901 tree selector
= NULL_TREE
, type_name
, identifier
;
29902 tree parm_attr
= NULL_TREE
;
29904 if (token
->keyword
== RID_ATTRIBUTE
)
29907 if (token
->type
!= CPP_COLON
)
29908 selector
= cp_parser_objc_selector (parser
);
29910 /* Detect if we have a unary selector. */
29911 if (maybe_unary_selector_p
29912 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29914 params
= selector
; /* Might be followed by attributes. */
29918 maybe_unary_selector_p
= false;
29919 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29921 /* Something went quite wrong. There should be a colon
29922 here, but there is not. Stop parsing parameters. */
29925 type_name
= cp_parser_objc_typename (parser
);
29926 /* New ObjC allows attributes on parameters too. */
29927 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29928 parm_attr
= cp_parser_attributes_opt (parser
);
29929 identifier
= cp_parser_identifier (parser
);
29933 objc_build_keyword_decl (selector
,
29938 token
= cp_lexer_peek_token (parser
->lexer
);
29941 if (params
== NULL_TREE
)
29943 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29944 return error_mark_node
;
29947 /* We allow tail attributes for the method. */
29948 if (token
->keyword
== RID_ATTRIBUTE
)
29950 *attributes
= cp_parser_attributes_opt (parser
);
29951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29952 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29954 cp_parser_error (parser
,
29955 "method attributes must be specified at the end");
29956 return error_mark_node
;
29959 if (params
== NULL_TREE
)
29961 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29962 return error_mark_node
;
29967 /* Parse the non-keyword Objective-C params. */
29970 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29973 tree params
= make_node (TREE_LIST
);
29974 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29975 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29977 while (token
->type
== CPP_COMMA
)
29979 cp_parameter_declarator
*parmdecl
;
29982 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29983 token
= cp_lexer_peek_token (parser
->lexer
);
29985 if (token
->type
== CPP_ELLIPSIS
)
29987 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29989 token
= cp_lexer_peek_token (parser
->lexer
);
29993 /* TODO: parse attributes for tail parameters. */
29994 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29995 parm
= grokdeclarator (parmdecl
->declarator
,
29996 &parmdecl
->decl_specifiers
,
29997 PARM
, /*initialized=*/0,
29998 /*attrlist=*/NULL
);
30000 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30001 token
= cp_lexer_peek_token (parser
->lexer
);
30004 /* We allow tail attributes for the method. */
30005 if (token
->keyword
== RID_ATTRIBUTE
)
30007 if (*attributes
== NULL_TREE
)
30009 *attributes
= cp_parser_attributes_opt (parser
);
30010 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30011 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30015 /* We have an error, but parse the attributes, so that we can
30017 *attributes
= cp_parser_attributes_opt (parser
);
30019 cp_parser_error (parser
,
30020 "method attributes must be specified at the end");
30021 return error_mark_node
;
30027 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30030 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30032 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30034 /* If the next token is `extern' and the following token is a string
30035 literal, then we have a linkage specification. */
30036 if (token
->keyword
== RID_EXTERN
30037 && cp_parser_is_pure_string_literal
30038 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30039 cp_parser_linkage_specification (parser
);
30040 /* Handle #pragma, if any. */
30041 else if (token
->type
== CPP_PRAGMA
)
30042 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30043 /* Allow stray semicolons. */
30044 else if (token
->type
== CPP_SEMICOLON
)
30045 cp_lexer_consume_token (parser
->lexer
);
30046 /* Mark methods as optional or required, when building protocols. */
30047 else if (token
->keyword
== RID_AT_OPTIONAL
)
30049 cp_lexer_consume_token (parser
->lexer
);
30050 objc_set_method_opt (true);
30052 else if (token
->keyword
== RID_AT_REQUIRED
)
30054 cp_lexer_consume_token (parser
->lexer
);
30055 objc_set_method_opt (false);
30057 else if (token
->keyword
== RID_NAMESPACE
)
30058 cp_parser_namespace_definition (parser
);
30059 /* Other stray characters must generate errors. */
30060 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30062 cp_lexer_consume_token (parser
->lexer
);
30063 error ("stray %qs between Objective-C++ methods",
30064 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30066 /* Finally, try to parse a block-declaration, or a function-definition. */
30068 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30071 /* Parse a method signature. */
30074 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30076 tree rettype
, kwdparms
, optparms
;
30077 bool ellipsis
= false;
30078 bool is_class_method
;
30080 is_class_method
= cp_parser_objc_method_type (parser
);
30081 rettype
= cp_parser_objc_typename (parser
);
30082 *attributes
= NULL_TREE
;
30083 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30084 if (kwdparms
== error_mark_node
)
30085 return error_mark_node
;
30086 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30087 if (optparms
== error_mark_node
)
30088 return error_mark_node
;
30090 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30094 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30097 cp_lexer_save_tokens (parser
->lexer
);
30098 tattr
= cp_parser_attributes_opt (parser
);
30099 gcc_assert (tattr
) ;
30101 /* If the attributes are followed by a method introducer, this is not allowed.
30102 Dump the attributes and flag the situation. */
30103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30104 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30107 /* Otherwise, the attributes introduce some interstitial code, possibly so
30108 rewind to allow that check. */
30109 cp_lexer_rollback_tokens (parser
->lexer
);
30113 /* Parse an Objective-C method prototype list. */
30116 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30118 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30120 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30122 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30124 tree attributes
, sig
;
30125 bool is_class_method
;
30126 if (token
->type
== CPP_PLUS
)
30127 is_class_method
= true;
30129 is_class_method
= false;
30130 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30131 if (sig
== error_mark_node
)
30133 cp_parser_skip_to_end_of_block_or_statement (parser
);
30134 token
= cp_lexer_peek_token (parser
->lexer
);
30137 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30138 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30140 else if (token
->keyword
== RID_AT_PROPERTY
)
30141 cp_parser_objc_at_property_declaration (parser
);
30142 else if (token
->keyword
== RID_ATTRIBUTE
30143 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30144 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30146 "prefix attributes are ignored for methods");
30148 /* Allow for interspersed non-ObjC++ code. */
30149 cp_parser_objc_interstitial_code (parser
);
30151 token
= cp_lexer_peek_token (parser
->lexer
);
30154 if (token
->type
!= CPP_EOF
)
30155 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30157 cp_parser_error (parser
, "expected %<@end%>");
30159 objc_finish_interface ();
30162 /* Parse an Objective-C method definition list. */
30165 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30167 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30169 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30173 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30176 tree sig
, attribute
;
30177 bool is_class_method
;
30178 if (token
->type
== CPP_PLUS
)
30179 is_class_method
= true;
30181 is_class_method
= false;
30182 push_deferring_access_checks (dk_deferred
);
30183 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30184 if (sig
== error_mark_node
)
30186 cp_parser_skip_to_end_of_block_or_statement (parser
);
30187 token
= cp_lexer_peek_token (parser
->lexer
);
30190 objc_start_method_definition (is_class_method
, sig
, attribute
,
30193 /* For historical reasons, we accept an optional semicolon. */
30194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30195 cp_lexer_consume_token (parser
->lexer
);
30197 ptk
= cp_lexer_peek_token (parser
->lexer
);
30198 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30199 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30201 perform_deferred_access_checks (tf_warning_or_error
);
30202 stop_deferring_access_checks ();
30203 meth
= cp_parser_function_definition_after_declarator (parser
,
30205 pop_deferring_access_checks ();
30206 objc_finish_method_definition (meth
);
30209 /* The following case will be removed once @synthesize is
30210 completely implemented. */
30211 else if (token
->keyword
== RID_AT_PROPERTY
)
30212 cp_parser_objc_at_property_declaration (parser
);
30213 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30214 cp_parser_objc_at_synthesize_declaration (parser
);
30215 else if (token
->keyword
== RID_AT_DYNAMIC
)
30216 cp_parser_objc_at_dynamic_declaration (parser
);
30217 else if (token
->keyword
== RID_ATTRIBUTE
30218 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30219 warning_at (token
->location
, OPT_Wattributes
,
30220 "prefix attributes are ignored for methods");
30222 /* Allow for interspersed non-ObjC++ code. */
30223 cp_parser_objc_interstitial_code (parser
);
30225 token
= cp_lexer_peek_token (parser
->lexer
);
30228 if (token
->type
!= CPP_EOF
)
30229 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30231 cp_parser_error (parser
, "expected %<@end%>");
30233 objc_finish_implementation ();
30236 /* Parse Objective-C ivars. */
30239 cp_parser_objc_class_ivars (cp_parser
* parser
)
30241 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30243 if (token
->type
!= CPP_OPEN_BRACE
)
30244 return; /* No ivars specified. */
30246 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30247 token
= cp_lexer_peek_token (parser
->lexer
);
30249 while (token
->type
!= CPP_CLOSE_BRACE
30250 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30252 cp_decl_specifier_seq declspecs
;
30253 int decl_class_or_enum_p
;
30254 tree prefix_attributes
;
30256 cp_parser_objc_visibility_spec (parser
);
30258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30261 cp_parser_decl_specifier_seq (parser
,
30262 CP_PARSER_FLAGS_OPTIONAL
,
30264 &decl_class_or_enum_p
);
30266 /* auto, register, static, extern, mutable. */
30267 if (declspecs
.storage_class
!= sc_none
)
30269 cp_parser_error (parser
, "invalid type for instance variable");
30270 declspecs
.storage_class
= sc_none
;
30273 /* thread_local. */
30274 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30276 cp_parser_error (parser
, "invalid type for instance variable");
30277 declspecs
.locations
[ds_thread
] = 0;
30281 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30283 cp_parser_error (parser
, "invalid type for instance variable");
30284 declspecs
.locations
[ds_typedef
] = 0;
30287 prefix_attributes
= declspecs
.attributes
;
30288 declspecs
.attributes
= NULL_TREE
;
30290 /* Keep going until we hit the `;' at the end of the
30292 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30294 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30295 cp_declarator
*declarator
= NULL
;
30296 int ctor_dtor_or_conv_p
;
30298 /* Check for a (possibly unnamed) bitfield declaration. */
30299 token
= cp_lexer_peek_token (parser
->lexer
);
30300 if (token
->type
== CPP_COLON
)
30303 if (token
->type
== CPP_NAME
30304 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30307 /* Get the name of the bitfield. */
30308 declarator
= make_id_declarator (NULL_TREE
,
30309 cp_parser_identifier (parser
),
30313 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30314 /* Get the width of the bitfield. */
30316 = cp_parser_constant_expression (parser
);
30320 /* Parse the declarator. */
30322 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30323 &ctor_dtor_or_conv_p
,
30324 /*parenthesized_p=*/NULL
,
30325 /*member_p=*/false,
30326 /*friend_p=*/false);
30329 /* Look for attributes that apply to the ivar. */
30330 attributes
= cp_parser_attributes_opt (parser
);
30331 /* Remember which attributes are prefix attributes and
30333 first_attribute
= attributes
;
30334 /* Combine the attributes. */
30335 attributes
= attr_chainon (prefix_attributes
, attributes
);
30338 /* Create the bitfield declaration. */
30339 decl
= grokbitfield (declarator
, &declspecs
,
30340 width
, NULL_TREE
, attributes
);
30342 decl
= grokfield (declarator
, &declspecs
,
30343 NULL_TREE
, /*init_const_expr_p=*/false,
30344 NULL_TREE
, attributes
);
30346 /* Add the instance variable. */
30347 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30348 objc_add_instance_variable (decl
);
30350 /* Reset PREFIX_ATTRIBUTES. */
30351 if (attributes
!= error_mark_node
)
30353 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30354 attributes
= TREE_CHAIN (attributes
);
30356 TREE_CHAIN (attributes
) = NULL_TREE
;
30359 token
= cp_lexer_peek_token (parser
->lexer
);
30361 if (token
->type
== CPP_COMMA
)
30363 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30369 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30370 token
= cp_lexer_peek_token (parser
->lexer
);
30373 if (token
->keyword
== RID_AT_END
)
30374 cp_parser_error (parser
, "expected %<}%>");
30376 /* Do not consume the RID_AT_END, so it will be read again as terminating
30377 the @interface of @implementation. */
30378 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30379 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30381 /* For historical reasons, we accept an optional semicolon. */
30382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30383 cp_lexer_consume_token (parser
->lexer
);
30386 /* Parse an Objective-C protocol declaration. */
30389 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30391 tree proto
, protorefs
;
30394 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30395 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30397 tok
= cp_lexer_peek_token (parser
->lexer
);
30398 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30399 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30403 /* See if we have a forward declaration or a definition. */
30404 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30406 /* Try a forward declaration first. */
30407 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30413 id
= cp_parser_identifier (parser
);
30414 if (id
== error_mark_node
)
30417 objc_declare_protocol (id
, attributes
);
30419 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30420 cp_lexer_consume_token (parser
->lexer
);
30424 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30427 /* Ok, we got a full-fledged definition (or at least should). */
30430 proto
= cp_parser_identifier (parser
);
30431 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30432 objc_start_protocol (proto
, protorefs
, attributes
);
30433 cp_parser_objc_method_prototype_list (parser
);
30437 /* Parse an Objective-C superclass or category. */
30440 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30443 tree
*categ
, bool *is_class_extension
)
30445 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30447 *super
= *categ
= NULL_TREE
;
30448 *is_class_extension
= false;
30449 if (next
->type
== CPP_COLON
)
30451 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30452 *super
= cp_parser_identifier (parser
);
30454 else if (next
->type
== CPP_OPEN_PAREN
)
30456 matching_parens parens
;
30457 parens
.consume_open (parser
); /* Eat '('. */
30459 /* If there is no category name, and this is an @interface, we
30460 have a class extension. */
30461 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30463 *categ
= NULL_TREE
;
30464 *is_class_extension
= true;
30467 *categ
= cp_parser_identifier (parser
);
30469 parens
.require_close (parser
);
30473 /* Parse an Objective-C class interface. */
30476 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30478 tree name
, super
, categ
, protos
;
30479 bool is_class_extension
;
30481 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30482 name
= cp_parser_identifier (parser
);
30483 if (name
== error_mark_node
)
30485 /* It's hard to recover because even if valid @interface stuff
30486 is to follow, we can't compile it (or validate it) if we
30487 don't even know which class it refers to. Let's assume this
30488 was a stray '@interface' token in the stream and skip it.
30492 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30493 &is_class_extension
);
30494 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30496 /* We have either a class or a category on our hands. */
30497 if (categ
|| is_class_extension
)
30498 objc_start_category_interface (name
, categ
, protos
, attributes
);
30501 objc_start_class_interface (name
, super
, protos
, attributes
);
30502 /* Handle instance variable declarations, if any. */
30503 cp_parser_objc_class_ivars (parser
);
30504 objc_continue_interface ();
30507 cp_parser_objc_method_prototype_list (parser
);
30510 /* Parse an Objective-C class implementation. */
30513 cp_parser_objc_class_implementation (cp_parser
* parser
)
30515 tree name
, super
, categ
;
30516 bool is_class_extension
;
30518 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30519 name
= cp_parser_identifier (parser
);
30520 if (name
== error_mark_node
)
30522 /* It's hard to recover because even if valid @implementation
30523 stuff is to follow, we can't compile it (or validate it) if
30524 we don't even know which class it refers to. Let's assume
30525 this was a stray '@implementation' token in the stream and
30530 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30531 &is_class_extension
);
30533 /* We have either a class or a category on our hands. */
30535 objc_start_category_implementation (name
, categ
);
30538 objc_start_class_implementation (name
, super
);
30539 /* Handle instance variable declarations, if any. */
30540 cp_parser_objc_class_ivars (parser
);
30541 objc_continue_implementation ();
30544 cp_parser_objc_method_definition_list (parser
);
30547 /* Consume the @end token and finish off the implementation. */
30550 cp_parser_objc_end_implementation (cp_parser
* parser
)
30552 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30553 objc_finish_implementation ();
30556 /* Parse an Objective-C declaration. */
30559 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30561 /* Try to figure out what kind of declaration is present. */
30562 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30565 switch (kwd
->keyword
)
30570 error_at (kwd
->location
, "attributes may not be specified before"
30571 " the %<@%D%> Objective-C++ keyword",
30575 case RID_AT_IMPLEMENTATION
:
30576 warning_at (kwd
->location
, OPT_Wattributes
,
30577 "prefix attributes are ignored before %<@%D%>",
30584 switch (kwd
->keyword
)
30587 cp_parser_objc_alias_declaration (parser
);
30590 cp_parser_objc_class_declaration (parser
);
30592 case RID_AT_PROTOCOL
:
30593 cp_parser_objc_protocol_declaration (parser
, attributes
);
30595 case RID_AT_INTERFACE
:
30596 cp_parser_objc_class_interface (parser
, attributes
);
30598 case RID_AT_IMPLEMENTATION
:
30599 cp_parser_objc_class_implementation (parser
);
30602 cp_parser_objc_end_implementation (parser
);
30605 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30607 cp_parser_skip_to_end_of_block_or_statement (parser
);
30611 /* Parse an Objective-C try-catch-finally statement.
30613 objc-try-catch-finally-stmt:
30614 @try compound-statement objc-catch-clause-seq [opt]
30615 objc-finally-clause [opt]
30617 objc-catch-clause-seq:
30618 objc-catch-clause objc-catch-clause-seq [opt]
30621 @catch ( objc-exception-declaration ) compound-statement
30623 objc-finally-clause:
30624 @finally compound-statement
30626 objc-exception-declaration:
30627 parameter-declaration
30630 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30634 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30635 for C. Keep them in sync. */
30638 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30640 location_t location
;
30643 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30644 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30645 objc_maybe_warn_exceptions (location
);
30646 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30647 node, lest it get absorbed into the surrounding block. */
30648 stmt
= push_stmt_list ();
30649 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30650 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30652 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30654 cp_parameter_declarator
*parm
;
30655 tree parameter_declaration
= error_mark_node
;
30656 bool seen_open_paren
= false;
30657 matching_parens parens
;
30659 cp_lexer_consume_token (parser
->lexer
);
30660 if (parens
.require_open (parser
))
30661 seen_open_paren
= true;
30662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30664 /* We have "@catch (...)" (where the '...' are literally
30665 what is in the code). Skip the '...'.
30666 parameter_declaration is set to NULL_TREE, and
30667 objc_being_catch_clauses() knows that that means
30669 cp_lexer_consume_token (parser
->lexer
);
30670 parameter_declaration
= NULL_TREE
;
30674 /* We have "@catch (NSException *exception)" or something
30675 like that. Parse the parameter declaration. */
30676 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30678 parameter_declaration
= error_mark_node
;
30680 parameter_declaration
= grokdeclarator (parm
->declarator
,
30681 &parm
->decl_specifiers
,
30682 PARM
, /*initialized=*/0,
30683 /*attrlist=*/NULL
);
30685 if (seen_open_paren
)
30686 parens
.require_close (parser
);
30689 /* If there was no open parenthesis, we are recovering from
30690 an error, and we are trying to figure out what mistake
30691 the user has made. */
30693 /* If there is an immediate closing parenthesis, the user
30694 probably forgot the opening one (ie, they typed "@catch
30695 NSException *e)". Parse the closing parenthesis and keep
30697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30698 cp_lexer_consume_token (parser
->lexer
);
30700 /* If these is no immediate closing parenthesis, the user
30701 probably doesn't know that parenthesis are required at
30702 all (ie, they typed "@catch NSException *e"). So, just
30703 forget about the closing parenthesis and keep going. */
30705 objc_begin_catch_clause (parameter_declaration
);
30706 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30707 objc_finish_catch_clause ();
30709 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30711 cp_lexer_consume_token (parser
->lexer
);
30712 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30713 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30714 node, lest it get absorbed into the surrounding block. */
30715 stmt
= push_stmt_list ();
30716 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30717 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30720 return objc_finish_try_stmt ();
30723 /* Parse an Objective-C synchronized statement.
30725 objc-synchronized-stmt:
30726 @synchronized ( expression ) compound-statement
30728 Returns NULL_TREE. */
30731 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30733 location_t location
;
30736 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30738 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30739 objc_maybe_warn_exceptions (location
);
30740 matching_parens parens
;
30741 parens
.require_open (parser
);
30742 lock
= cp_parser_expression (parser
);
30743 parens
.require_close (parser
);
30745 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30746 node, lest it get absorbed into the surrounding block. */
30747 stmt
= push_stmt_list ();
30748 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30750 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30753 /* Parse an Objective-C throw statement.
30756 @throw assignment-expression [opt] ;
30758 Returns a constructed '@throw' statement. */
30761 cp_parser_objc_throw_statement (cp_parser
*parser
)
30763 tree expr
= NULL_TREE
;
30764 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30766 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30768 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30769 expr
= cp_parser_expression (parser
);
30771 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30773 return objc_build_throw_stmt (loc
, expr
);
30776 /* Parse an Objective-C statement. */
30779 cp_parser_objc_statement (cp_parser
* parser
)
30781 /* Try to figure out what kind of declaration is present. */
30782 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30784 switch (kwd
->keyword
)
30787 return cp_parser_objc_try_catch_finally_statement (parser
);
30788 case RID_AT_SYNCHRONIZED
:
30789 return cp_parser_objc_synchronized_statement (parser
);
30791 return cp_parser_objc_throw_statement (parser
);
30793 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30795 cp_parser_skip_to_end_of_block_or_statement (parser
);
30798 return error_mark_node
;
30801 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30802 look ahead to see if an objc keyword follows the attributes. This
30803 is to detect the use of prefix attributes on ObjC @interface and
30807 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30809 cp_lexer_save_tokens (parser
->lexer
);
30810 *attrib
= cp_parser_attributes_opt (parser
);
30811 gcc_assert (*attrib
);
30812 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30814 cp_lexer_commit_tokens (parser
->lexer
);
30817 cp_lexer_rollback_tokens (parser
->lexer
);
30821 /* This routine is a minimal replacement for
30822 c_parser_struct_declaration () used when parsing the list of
30823 types/names or ObjC++ properties. For example, when parsing the
30826 @property (readonly) int a, b, c;
30828 this function is responsible for parsing "int a, int b, int c" and
30829 returning the declarations as CHAIN of DECLs.
30831 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30832 similar parsing. */
30834 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30836 tree decls
= NULL_TREE
;
30837 cp_decl_specifier_seq declspecs
;
30838 int decl_class_or_enum_p
;
30839 tree prefix_attributes
;
30841 cp_parser_decl_specifier_seq (parser
,
30842 CP_PARSER_FLAGS_NONE
,
30844 &decl_class_or_enum_p
);
30846 if (declspecs
.type
== error_mark_node
)
30847 return error_mark_node
;
30849 /* auto, register, static, extern, mutable. */
30850 if (declspecs
.storage_class
!= sc_none
)
30852 cp_parser_error (parser
, "invalid type for property");
30853 declspecs
.storage_class
= sc_none
;
30856 /* thread_local. */
30857 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30859 cp_parser_error (parser
, "invalid type for property");
30860 declspecs
.locations
[ds_thread
] = 0;
30864 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30866 cp_parser_error (parser
, "invalid type for property");
30867 declspecs
.locations
[ds_typedef
] = 0;
30870 prefix_attributes
= declspecs
.attributes
;
30871 declspecs
.attributes
= NULL_TREE
;
30873 /* Keep going until we hit the `;' at the end of the declaration. */
30874 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30876 tree attributes
, first_attribute
, decl
;
30877 cp_declarator
*declarator
;
30880 /* Parse the declarator. */
30881 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30882 NULL
, NULL
, false, false);
30884 /* Look for attributes that apply to the ivar. */
30885 attributes
= cp_parser_attributes_opt (parser
);
30886 /* Remember which attributes are prefix attributes and
30888 first_attribute
= attributes
;
30889 /* Combine the attributes. */
30890 attributes
= attr_chainon (prefix_attributes
, attributes
);
30892 decl
= grokfield (declarator
, &declspecs
,
30893 NULL_TREE
, /*init_const_expr_p=*/false,
30894 NULL_TREE
, attributes
);
30896 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30897 return error_mark_node
;
30899 /* Reset PREFIX_ATTRIBUTES. */
30900 if (attributes
!= error_mark_node
)
30902 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30903 attributes
= TREE_CHAIN (attributes
);
30905 TREE_CHAIN (attributes
) = NULL_TREE
;
30908 DECL_CHAIN (decl
) = decls
;
30911 token
= cp_lexer_peek_token (parser
->lexer
);
30912 if (token
->type
== CPP_COMMA
)
30914 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30923 /* Parse an Objective-C @property declaration. The syntax is:
30925 objc-property-declaration:
30926 '@property' objc-property-attributes[opt] struct-declaration ;
30928 objc-property-attributes:
30929 '(' objc-property-attribute-list ')'
30931 objc-property-attribute-list:
30932 objc-property-attribute
30933 objc-property-attribute-list, objc-property-attribute
30935 objc-property-attribute
30936 'getter' = identifier
30937 'setter' = identifier
30946 @property NSString *name;
30947 @property (readonly) id object;
30948 @property (retain, nonatomic, getter=getTheName) id name;
30949 @property int a, b, c;
30951 PS: This function is identical to
30952 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30954 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30956 /* The following variables hold the attributes of the properties as
30957 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30958 seen. When we see an attribute, we set them to 'true' (if they
30959 are boolean properties) or to the identifier (if they have an
30960 argument, ie, for getter and setter). Note that here we only
30961 parse the list of attributes, check the syntax and accumulate the
30962 attributes that we find. objc_add_property_declaration() will
30963 then process the information. */
30964 bool property_assign
= false;
30965 bool property_copy
= false;
30966 tree property_getter_ident
= NULL_TREE
;
30967 bool property_nonatomic
= false;
30968 bool property_readonly
= false;
30969 bool property_readwrite
= false;
30970 bool property_retain
= false;
30971 tree property_setter_ident
= NULL_TREE
;
30973 /* 'properties' is the list of properties that we read. Usually a
30974 single one, but maybe more (eg, in "@property int a, b, c;" there
30979 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30981 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30983 /* Parse the optional attribute list... */
30984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30987 matching_parens parens
;
30988 parens
.consume_open (parser
);
30992 bool syntax_error
= false;
30993 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30996 if (token
->type
!= CPP_NAME
)
30998 cp_parser_error (parser
, "expected identifier");
31001 keyword
= C_RID_CODE (token
->u
.value
);
31002 cp_lexer_consume_token (parser
->lexer
);
31005 case RID_ASSIGN
: property_assign
= true; break;
31006 case RID_COPY
: property_copy
= true; break;
31007 case RID_NONATOMIC
: property_nonatomic
= true; break;
31008 case RID_READONLY
: property_readonly
= true; break;
31009 case RID_READWRITE
: property_readwrite
= true; break;
31010 case RID_RETAIN
: property_retain
= true; break;
31014 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31016 if (keyword
== RID_GETTER
)
31017 cp_parser_error (parser
,
31018 "missing %<=%> (after %<getter%> attribute)");
31020 cp_parser_error (parser
,
31021 "missing %<=%> (after %<setter%> attribute)");
31022 syntax_error
= true;
31025 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31026 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31028 cp_parser_error (parser
, "expected identifier");
31029 syntax_error
= true;
31032 if (keyword
== RID_SETTER
)
31034 if (property_setter_ident
!= NULL_TREE
)
31036 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31037 cp_lexer_consume_token (parser
->lexer
);
31040 property_setter_ident
= cp_parser_objc_selector (parser
);
31041 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31042 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31044 cp_lexer_consume_token (parser
->lexer
);
31048 if (property_getter_ident
!= NULL_TREE
)
31050 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31051 cp_lexer_consume_token (parser
->lexer
);
31054 property_getter_ident
= cp_parser_objc_selector (parser
);
31058 cp_parser_error (parser
, "unknown property attribute");
31059 syntax_error
= true;
31066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31067 cp_lexer_consume_token (parser
->lexer
);
31072 /* FIXME: "@property (setter, assign);" will generate a spurious
31073 "error: expected ‘)’ before ‘,’ token". This is because
31074 cp_parser_require, unlike the C counterpart, will produce an
31075 error even if we are in error recovery. */
31076 if (!parens
.require_close (parser
))
31078 cp_parser_skip_to_closing_parenthesis (parser
,
31079 /*recovering=*/true,
31080 /*or_comma=*/false,
31081 /*consume_paren=*/true);
31085 /* ... and the property declaration(s). */
31086 properties
= cp_parser_objc_struct_declaration (parser
);
31088 if (properties
== error_mark_node
)
31090 cp_parser_skip_to_end_of_statement (parser
);
31091 /* If the next token is now a `;', consume it. */
31092 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31093 cp_lexer_consume_token (parser
->lexer
);
31097 if (properties
== NULL_TREE
)
31098 cp_parser_error (parser
, "expected identifier");
31101 /* Comma-separated properties are chained together in
31102 reverse order; add them one by one. */
31103 properties
= nreverse (properties
);
31105 for (; properties
; properties
= TREE_CHAIN (properties
))
31106 objc_add_property_declaration (loc
, copy_node (properties
),
31107 property_readonly
, property_readwrite
,
31108 property_assign
, property_retain
,
31109 property_copy
, property_nonatomic
,
31110 property_getter_ident
, property_setter_ident
);
31113 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31116 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31118 objc-synthesize-declaration:
31119 @synthesize objc-synthesize-identifier-list ;
31121 objc-synthesize-identifier-list:
31122 objc-synthesize-identifier
31123 objc-synthesize-identifier-list, objc-synthesize-identifier
31125 objc-synthesize-identifier
31127 identifier = identifier
31130 @synthesize MyProperty;
31131 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31133 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31134 for C. Keep them in sync.
31137 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31139 tree list
= NULL_TREE
;
31141 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31143 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31146 tree property
, ivar
;
31147 property
= cp_parser_identifier (parser
);
31148 if (property
== error_mark_node
)
31150 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31155 cp_lexer_consume_token (parser
->lexer
);
31156 ivar
= cp_parser_identifier (parser
);
31157 if (ivar
== error_mark_node
)
31159 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31165 list
= chainon (list
, build_tree_list (ivar
, property
));
31166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31167 cp_lexer_consume_token (parser
->lexer
);
31171 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31172 objc_add_synthesize_declaration (loc
, list
);
31175 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31177 objc-dynamic-declaration:
31178 @dynamic identifier-list ;
31181 @dynamic MyProperty;
31182 @dynamic MyProperty, AnotherProperty;
31184 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31185 for C. Keep them in sync.
31188 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31190 tree list
= NULL_TREE
;
31192 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31194 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31198 property
= cp_parser_identifier (parser
);
31199 if (property
== error_mark_node
)
31201 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31204 list
= chainon (list
, build_tree_list (NULL
, property
));
31205 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31206 cp_lexer_consume_token (parser
->lexer
);
31210 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31211 objc_add_dynamic_declaration (loc
, list
);
31215 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31217 /* Returns name of the next clause.
31218 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31219 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31220 returned and the token is consumed. */
31222 static pragma_omp_clause
31223 cp_parser_omp_clause_name (cp_parser
*parser
)
31225 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31227 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31228 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31229 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31230 result
= PRAGMA_OMP_CLAUSE_IF
;
31231 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31232 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31233 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31234 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31235 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31236 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31237 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31238 result
= PRAGMA_OMP_CLAUSE_FOR
;
31239 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31241 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31242 const char *p
= IDENTIFIER_POINTER (id
);
31247 if (!strcmp ("aligned", p
))
31248 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31249 else if (!strcmp ("async", p
))
31250 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31253 if (!strcmp ("collapse", p
))
31254 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31255 else if (!strcmp ("copy", p
))
31256 result
= PRAGMA_OACC_CLAUSE_COPY
;
31257 else if (!strcmp ("copyin", p
))
31258 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31259 else if (!strcmp ("copyout", p
))
31260 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31261 else if (!strcmp ("copyprivate", p
))
31262 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31263 else if (!strcmp ("create", p
))
31264 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31267 if (!strcmp ("defaultmap", p
))
31268 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31269 else if (!strcmp ("depend", p
))
31270 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31271 else if (!strcmp ("device", p
))
31272 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31273 else if (!strcmp ("deviceptr", p
))
31274 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31275 else if (!strcmp ("device_resident", p
))
31276 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31277 else if (!strcmp ("dist_schedule", p
))
31278 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31281 if (!strcmp ("final", p
))
31282 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31283 else if (!strcmp ("firstprivate", p
))
31284 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31285 else if (!strcmp ("from", p
))
31286 result
= PRAGMA_OMP_CLAUSE_FROM
;
31289 if (!strcmp ("gang", p
))
31290 result
= PRAGMA_OACC_CLAUSE_GANG
;
31291 else if (!strcmp ("grainsize", p
))
31292 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31295 if (!strcmp ("hint", p
))
31296 result
= PRAGMA_OMP_CLAUSE_HINT
;
31297 else if (!strcmp ("host", p
))
31298 result
= PRAGMA_OACC_CLAUSE_HOST
;
31301 if (!strcmp ("inbranch", p
))
31302 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31303 else if (!strcmp ("independent", p
))
31304 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31305 else if (!strcmp ("is_device_ptr", p
))
31306 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31309 if (!strcmp ("lastprivate", p
))
31310 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31311 else if (!strcmp ("linear", p
))
31312 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31313 else if (!strcmp ("link", p
))
31314 result
= PRAGMA_OMP_CLAUSE_LINK
;
31317 if (!strcmp ("map", p
))
31318 result
= PRAGMA_OMP_CLAUSE_MAP
;
31319 else if (!strcmp ("mergeable", p
))
31320 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31323 if (!strcmp ("nogroup", p
))
31324 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31325 else if (!strcmp ("notinbranch", p
))
31326 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31327 else if (!strcmp ("nowait", p
))
31328 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31329 else if (!strcmp ("num_gangs", p
))
31330 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31331 else if (!strcmp ("num_tasks", p
))
31332 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31333 else if (!strcmp ("num_teams", p
))
31334 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31335 else if (!strcmp ("num_threads", p
))
31336 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31337 else if (!strcmp ("num_workers", p
))
31338 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31341 if (!strcmp ("ordered", p
))
31342 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31345 if (!strcmp ("parallel", p
))
31346 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31347 else if (!strcmp ("present", p
))
31348 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31349 else if (!strcmp ("present_or_copy", p
)
31350 || !strcmp ("pcopy", p
))
31351 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31352 else if (!strcmp ("present_or_copyin", p
)
31353 || !strcmp ("pcopyin", p
))
31354 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31355 else if (!strcmp ("present_or_copyout", p
)
31356 || !strcmp ("pcopyout", p
))
31357 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31358 else if (!strcmp ("present_or_create", p
)
31359 || !strcmp ("pcreate", p
))
31360 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31361 else if (!strcmp ("priority", p
))
31362 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31363 else if (!strcmp ("proc_bind", p
))
31364 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31367 if (!strcmp ("reduction", p
))
31368 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31371 if (!strcmp ("safelen", p
))
31372 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31373 else if (!strcmp ("schedule", p
))
31374 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31375 else if (!strcmp ("sections", p
))
31376 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31377 else if (!strcmp ("self", p
))
31378 result
= PRAGMA_OACC_CLAUSE_SELF
;
31379 else if (!strcmp ("seq", p
))
31380 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31381 else if (!strcmp ("shared", p
))
31382 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31383 else if (!strcmp ("simd", p
))
31384 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31385 else if (!strcmp ("simdlen", p
))
31386 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31389 if (!strcmp ("taskgroup", p
))
31390 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31391 else if (!strcmp ("thread_limit", p
))
31392 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31393 else if (!strcmp ("threads", p
))
31394 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31395 else if (!strcmp ("tile", p
))
31396 result
= PRAGMA_OACC_CLAUSE_TILE
;
31397 else if (!strcmp ("to", p
))
31398 result
= PRAGMA_OMP_CLAUSE_TO
;
31401 if (!strcmp ("uniform", p
))
31402 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31403 else if (!strcmp ("untied", p
))
31404 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31405 else if (!strcmp ("use_device", p
))
31406 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31407 else if (!strcmp ("use_device_ptr", p
))
31408 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31411 if (!strcmp ("vector", p
))
31412 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31413 else if (!strcmp ("vector_length", p
))
31414 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31417 if (!strcmp ("wait", p
))
31418 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31419 else if (!strcmp ("worker", p
))
31420 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31425 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31426 cp_lexer_consume_token (parser
->lexer
);
31431 /* Validate that a clause of the given type does not already exist. */
31434 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31435 const char *name
, location_t location
)
31439 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31440 if (OMP_CLAUSE_CODE (c
) == code
)
31442 error_at (location
, "too many %qs clauses", name
);
31450 variable-list , identifier
31452 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31453 colon). An opening parenthesis will have been consumed by the caller.
31455 If KIND is nonzero, create the appropriate node and install the decl
31456 in OMP_CLAUSE_DECL and add the node to the head of the list.
31458 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31459 return the list created.
31461 COLON can be NULL if only closing parenthesis should end the list,
31462 or pointer to bool which will receive false if the list is terminated
31463 by closing parenthesis or true if the list is terminated by colon. */
31466 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31467 tree list
, bool *colon
)
31470 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31473 parser
->colon_corrects_to_scope_p
= false;
31480 token
= cp_lexer_peek_token (parser
->lexer
);
31482 && current_class_ptr
31483 && cp_parser_is_keyword (token
, RID_THIS
))
31485 decl
= finish_this_expr ();
31486 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31487 || CONVERT_EXPR_P (decl
))
31488 decl
= TREE_OPERAND (decl
, 0);
31489 cp_lexer_consume_token (parser
->lexer
);
31493 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31494 /*check_dependency_p=*/true,
31495 /*template_p=*/NULL
,
31496 /*declarator_p=*/false,
31497 /*optional_p=*/false);
31498 if (name
== error_mark_node
)
31501 if (identifier_p (name
))
31502 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31505 if (decl
== error_mark_node
)
31506 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31509 if (decl
== error_mark_node
)
31511 else if (kind
!= 0)
31515 case OMP_CLAUSE__CACHE_
:
31516 /* The OpenACC cache directive explicitly only allows "array
31517 elements or subarrays". */
31518 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31520 error_at (token
->location
, "expected %<[%>");
31521 decl
= error_mark_node
;
31525 case OMP_CLAUSE_MAP
:
31526 case OMP_CLAUSE_FROM
:
31527 case OMP_CLAUSE_TO
:
31528 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31531 = cp_lexer_peek_token (parser
->lexer
)->location
;
31532 cp_id_kind idk
= CP_ID_KIND_NONE
;
31533 cp_lexer_consume_token (parser
->lexer
);
31534 decl
= convert_from_reference (decl
);
31536 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31541 case OMP_CLAUSE_DEPEND
:
31542 case OMP_CLAUSE_REDUCTION
:
31543 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31545 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31547 parser
->colon_corrects_to_scope_p
= false;
31548 cp_lexer_consume_token (parser
->lexer
);
31549 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31550 low_bound
= cp_parser_expression (parser
);
31552 parser
->colon_corrects_to_scope_p
31553 = saved_colon_corrects_to_scope_p
;
31554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31555 length
= integer_one_node
;
31558 /* Look for `:'. */
31559 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31561 if (!cp_lexer_next_token_is (parser
->lexer
,
31563 length
= cp_parser_expression (parser
);
31565 /* Look for the closing `]'. */
31566 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31570 decl
= tree_cons (low_bound
, length
, decl
);
31577 tree u
= build_omp_clause (token
->location
, kind
);
31578 OMP_CLAUSE_DECL (u
) = decl
;
31579 OMP_CLAUSE_CHAIN (u
) = list
;
31583 list
= tree_cons (decl
, NULL_TREE
, list
);
31586 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31588 cp_lexer_consume_token (parser
->lexer
);
31592 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31594 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31597 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31601 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31605 /* Try to resync to an unnested comma. Copied from
31606 cp_parser_parenthesized_expression_list. */
31609 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31610 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31611 /*recovering=*/true,
31613 /*consume_paren=*/true);
31621 /* Similarly, but expect leading and trailing parenthesis. This is a very
31622 common case for omp clauses. */
31625 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31627 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31628 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31633 copy ( variable-list )
31634 copyin ( variable-list )
31635 copyout ( variable-list )
31636 create ( variable-list )
31637 delete ( variable-list )
31638 present ( variable-list )
31639 present_or_copy ( variable-list )
31640 pcopy ( variable-list )
31641 present_or_copyin ( variable-list )
31642 pcopyin ( variable-list )
31643 present_or_copyout ( variable-list )
31644 pcopyout ( variable-list )
31645 present_or_create ( variable-list )
31646 pcreate ( variable-list ) */
31649 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31652 enum gomp_map_kind kind
;
31655 case PRAGMA_OACC_CLAUSE_COPY
:
31656 kind
= GOMP_MAP_FORCE_TOFROM
;
31658 case PRAGMA_OACC_CLAUSE_COPYIN
:
31659 kind
= GOMP_MAP_FORCE_TO
;
31661 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31662 kind
= GOMP_MAP_FORCE_FROM
;
31664 case PRAGMA_OACC_CLAUSE_CREATE
:
31665 kind
= GOMP_MAP_FORCE_ALLOC
;
31667 case PRAGMA_OACC_CLAUSE_DELETE
:
31668 kind
= GOMP_MAP_DELETE
;
31670 case PRAGMA_OACC_CLAUSE_DEVICE
:
31671 kind
= GOMP_MAP_FORCE_TO
;
31673 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31674 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31676 case PRAGMA_OACC_CLAUSE_HOST
:
31677 case PRAGMA_OACC_CLAUSE_SELF
:
31678 kind
= GOMP_MAP_FORCE_FROM
;
31680 case PRAGMA_OACC_CLAUSE_LINK
:
31681 kind
= GOMP_MAP_LINK
;
31683 case PRAGMA_OACC_CLAUSE_PRESENT
:
31684 kind
= GOMP_MAP_FORCE_PRESENT
;
31686 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31687 kind
= GOMP_MAP_TOFROM
;
31689 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31690 kind
= GOMP_MAP_TO
;
31692 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31693 kind
= GOMP_MAP_FROM
;
31695 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31696 kind
= GOMP_MAP_ALLOC
;
31699 gcc_unreachable ();
31702 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31704 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31705 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31711 deviceptr ( variable-list ) */
31714 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31716 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31719 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31720 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31721 variable-list must only allow for pointer variables. */
31722 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31723 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31725 tree v
= TREE_PURPOSE (t
);
31726 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31727 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31728 OMP_CLAUSE_DECL (u
) = v
;
31729 OMP_CLAUSE_CHAIN (u
) = list
;
31743 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31744 enum omp_clause_code code
,
31745 tree list
, location_t location
)
31747 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31748 tree c
= build_omp_clause (location
, code
);
31749 OMP_CLAUSE_CHAIN (c
) = list
;
31754 num_gangs ( expression )
31755 num_workers ( expression )
31756 vector_length ( expression ) */
31759 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31760 const char *str
, tree list
)
31762 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31764 matching_parens parens
;
31765 if (!parens
.require_open (parser
))
31768 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31770 if (t
== error_mark_node
31771 || !parens
.require_close (parser
))
31773 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31774 /*or_comma=*/false,
31775 /*consume_paren=*/true);
31779 check_no_duplicate_clause (list
, code
, str
, loc
);
31781 tree c
= build_omp_clause (loc
, code
);
31782 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31783 OMP_CLAUSE_CHAIN (c
) = list
;
31789 gang [( gang-arg-list )]
31790 worker [( [num:] int-expr )]
31791 vector [( [length:] int-expr )]
31793 where gang-arg is one of:
31798 and size-expr may be:
31805 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31806 const char *str
, tree list
)
31808 const char *id
= "num";
31809 cp_lexer
*lexer
= parser
->lexer
;
31810 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31811 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31813 if (kind
== OMP_CLAUSE_VECTOR
)
31816 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31818 matching_parens parens
;
31819 parens
.consume_open (parser
);
31823 cp_token
*next
= cp_lexer_peek_token (lexer
);
31826 /* Gang static argument. */
31827 if (kind
== OMP_CLAUSE_GANG
31828 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31830 cp_lexer_consume_token (lexer
);
31832 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31833 goto cleanup_error
;
31836 if (ops
[idx
] != NULL
)
31838 cp_parser_error (parser
, "too many %<static%> arguments");
31839 goto cleanup_error
;
31842 /* Check for the '*' argument. */
31843 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31844 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31845 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31848 cp_lexer_consume_token (lexer
);
31849 ops
[idx
] = integer_minus_one_node
;
31851 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31853 cp_lexer_consume_token (lexer
);
31859 /* Worker num: argument and vector length: arguments. */
31860 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31861 && id_equal (next
->u
.value
, id
)
31862 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31864 cp_lexer_consume_token (lexer
); /* id */
31865 cp_lexer_consume_token (lexer
); /* ':' */
31868 /* Now collect the actual argument. */
31869 if (ops
[idx
] != NULL_TREE
)
31871 cp_parser_error (parser
, "unexpected argument");
31872 goto cleanup_error
;
31875 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31877 if (expr
== error_mark_node
)
31878 goto cleanup_error
;
31880 mark_exp_read (expr
);
31883 if (kind
== OMP_CLAUSE_GANG
31884 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31886 cp_lexer_consume_token (lexer
);
31893 if (!parens
.require_close (parser
))
31894 goto cleanup_error
;
31897 check_no_duplicate_clause (list
, kind
, str
, loc
);
31899 c
= build_omp_clause (loc
, kind
);
31902 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31904 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31905 OMP_CLAUSE_CHAIN (c
) = list
;
31910 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31915 tile ( size-expr-list ) */
31918 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31920 tree c
, expr
= error_mark_node
;
31921 tree tile
= NULL_TREE
;
31923 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31924 so, but the spec authors never considered such a case and have
31925 differing opinions on what it might mean, including 'not
31927 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31928 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31931 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31936 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31940 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31941 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31943 cp_lexer_consume_token (parser
->lexer
);
31944 expr
= integer_zero_node
;
31947 expr
= cp_parser_constant_expression (parser
);
31949 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31951 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31953 /* Consume the trailing ')'. */
31954 cp_lexer_consume_token (parser
->lexer
);
31956 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31957 tile
= nreverse (tile
);
31958 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31959 OMP_CLAUSE_CHAIN (c
) = list
;
31964 Parse wait clause or directive parameters. */
31967 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31969 vec
<tree
, va_gc
> *args
;
31972 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31974 /*allow_expansion_p=*/true,
31975 /*non_constant_p=*/NULL
);
31977 if (args
== NULL
|| args
->length () == 0)
31979 cp_parser_error (parser
, "expected integer expression before ')'");
31981 release_tree_vector (args
);
31985 args_tree
= build_tree_list_vec (args
);
31987 release_tree_vector (args
);
31989 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31991 tree targ
= TREE_VALUE (t
);
31993 if (targ
!= error_mark_node
)
31995 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31996 error ("%<wait%> expression must be integral");
31999 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32001 targ
= mark_rvalue_use (targ
);
32002 OMP_CLAUSE_DECL (c
) = targ
;
32003 OMP_CLAUSE_CHAIN (c
) = list
;
32013 wait ( int-expr-list ) */
32016 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32018 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32020 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32023 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32029 collapse ( constant-expression ) */
32032 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32038 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32039 matching_parens parens
;
32040 if (!parens
.require_open (parser
))
32043 num
= cp_parser_constant_expression (parser
);
32045 if (!parens
.require_close (parser
))
32046 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32047 /*or_comma=*/false,
32048 /*consume_paren=*/true);
32050 if (num
== error_mark_node
)
32052 num
= fold_non_dependent_expr (num
);
32053 if (!tree_fits_shwi_p (num
)
32054 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32055 || (n
= tree_to_shwi (num
)) <= 0
32058 error_at (loc
, "collapse argument needs positive constant integer expression");
32062 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32063 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32064 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32065 OMP_CLAUSE_CHAIN (c
) = list
;
32066 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32072 default ( none | shared )
32075 default ( none | present ) */
32078 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32079 location_t location
, bool is_oacc
)
32081 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32084 matching_parens parens
;
32085 if (!parens
.require_open (parser
))
32087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32089 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32090 const char *p
= IDENTIFIER_POINTER (id
);
32095 if (strcmp ("none", p
) != 0)
32097 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32101 if (strcmp ("present", p
) != 0 || !is_oacc
)
32103 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32107 if (strcmp ("shared", p
) != 0 || is_oacc
)
32109 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32116 cp_lexer_consume_token (parser
->lexer
);
32122 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32124 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32127 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32128 || !parens
.require_close (parser
))
32129 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32130 /*or_comma=*/false,
32131 /*consume_paren=*/true);
32133 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32136 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32137 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32138 OMP_CLAUSE_CHAIN (c
) = list
;
32139 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32145 final ( expression ) */
32148 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32152 matching_parens parens
;
32153 if (!parens
.require_open (parser
))
32156 t
= cp_parser_condition (parser
);
32158 if (t
== error_mark_node
32159 || !parens
.require_close (parser
))
32160 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32161 /*or_comma=*/false,
32162 /*consume_paren=*/true);
32164 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32166 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32167 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32168 OMP_CLAUSE_CHAIN (c
) = list
;
32177 if ( directive-name-modifier : expression )
32179 directive-name-modifier:
32180 parallel | task | taskloop | target data | target | target update
32181 | target enter data | target exit data */
32184 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32188 enum tree_code if_modifier
= ERROR_MARK
;
32190 matching_parens parens
;
32191 if (!parens
.require_open (parser
))
32194 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32196 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32197 const char *p
= IDENTIFIER_POINTER (id
);
32200 if (strcmp ("parallel", p
) == 0)
32201 if_modifier
= OMP_PARALLEL
;
32202 else if (strcmp ("task", p
) == 0)
32203 if_modifier
= OMP_TASK
;
32204 else if (strcmp ("taskloop", p
) == 0)
32205 if_modifier
= OMP_TASKLOOP
;
32206 else if (strcmp ("target", p
) == 0)
32208 if_modifier
= OMP_TARGET
;
32209 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32211 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32212 p
= IDENTIFIER_POINTER (id
);
32213 if (strcmp ("data", p
) == 0)
32214 if_modifier
= OMP_TARGET_DATA
;
32215 else if (strcmp ("update", p
) == 0)
32216 if_modifier
= OMP_TARGET_UPDATE
;
32217 else if (strcmp ("enter", p
) == 0)
32218 if_modifier
= OMP_TARGET_ENTER_DATA
;
32219 else if (strcmp ("exit", p
) == 0)
32220 if_modifier
= OMP_TARGET_EXIT_DATA
;
32221 if (if_modifier
!= OMP_TARGET
)
32226 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32227 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32229 if_modifier
= ERROR_MARK
;
32231 if (if_modifier
== OMP_TARGET_ENTER_DATA
32232 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32234 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32236 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32237 p
= IDENTIFIER_POINTER (id
);
32238 if (strcmp ("data", p
) == 0)
32244 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32245 error_at (loc
, "expected %<data%>");
32246 if_modifier
= ERROR_MARK
;
32251 if (if_modifier
!= ERROR_MARK
)
32253 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32256 cp_lexer_consume_token (parser
->lexer
);
32263 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32264 error_at (loc
, "expected %<:%>");
32266 if_modifier
= ERROR_MARK
;
32271 t
= cp_parser_condition (parser
);
32273 if (t
== error_mark_node
32274 || !parens
.require_close (parser
))
32275 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32276 /*or_comma=*/false,
32277 /*consume_paren=*/true);
32279 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32280 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32282 if (if_modifier
!= ERROR_MARK
32283 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32285 const char *p
= NULL
;
32286 switch (if_modifier
)
32288 case OMP_PARALLEL
: p
= "parallel"; break;
32289 case OMP_TASK
: p
= "task"; break;
32290 case OMP_TASKLOOP
: p
= "taskloop"; break;
32291 case OMP_TARGET_DATA
: p
= "target data"; break;
32292 case OMP_TARGET
: p
= "target"; break;
32293 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32294 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32295 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32296 default: gcc_unreachable ();
32298 error_at (location
, "too many %<if%> clauses with %qs modifier",
32302 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32305 error_at (location
, "too many %<if%> clauses");
32307 error_at (location
, "too many %<if%> clauses without modifier");
32310 else if (if_modifier
== ERROR_MARK
32311 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32313 error_at (location
, "if any %<if%> clause has modifier, then all "
32314 "%<if%> clauses have to use modifier");
32319 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32320 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32321 OMP_CLAUSE_IF_EXPR (c
) = t
;
32322 OMP_CLAUSE_CHAIN (c
) = list
;
32331 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32332 tree list
, location_t location
)
32336 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32339 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32340 OMP_CLAUSE_CHAIN (c
) = list
;
32348 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32349 tree list
, location_t location
)
32353 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32355 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32356 OMP_CLAUSE_CHAIN (c
) = list
;
32361 num_threads ( expression ) */
32364 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32365 location_t location
)
32369 matching_parens parens
;
32370 if (!parens
.require_open (parser
))
32373 t
= cp_parser_expression (parser
);
32375 if (t
== error_mark_node
32376 || !parens
.require_close (parser
))
32377 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32378 /*or_comma=*/false,
32379 /*consume_paren=*/true);
32381 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32382 "num_threads", location
);
32384 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32385 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32386 OMP_CLAUSE_CHAIN (c
) = list
;
32392 num_tasks ( expression ) */
32395 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32396 location_t location
)
32400 matching_parens parens
;
32401 if (!parens
.require_open (parser
))
32404 t
= cp_parser_expression (parser
);
32406 if (t
== error_mark_node
32407 || !parens
.require_close (parser
))
32408 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32409 /*or_comma=*/false,
32410 /*consume_paren=*/true);
32412 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32413 "num_tasks", location
);
32415 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32416 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32417 OMP_CLAUSE_CHAIN (c
) = list
;
32423 grainsize ( expression ) */
32426 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32427 location_t location
)
32431 matching_parens parens
;
32432 if (!parens
.require_open (parser
))
32435 t
= cp_parser_expression (parser
);
32437 if (t
== error_mark_node
32438 || !parens
.require_close (parser
))
32439 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32440 /*or_comma=*/false,
32441 /*consume_paren=*/true);
32443 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32444 "grainsize", location
);
32446 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32447 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32448 OMP_CLAUSE_CHAIN (c
) = list
;
32454 priority ( expression ) */
32457 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32458 location_t location
)
32462 matching_parens parens
;
32463 if (!parens
.require_open (parser
))
32466 t
= cp_parser_expression (parser
);
32468 if (t
== error_mark_node
32469 || !parens
.require_close (parser
))
32470 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32471 /*or_comma=*/false,
32472 /*consume_paren=*/true);
32474 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32475 "priority", location
);
32477 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32478 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32479 OMP_CLAUSE_CHAIN (c
) = list
;
32485 hint ( expression ) */
32488 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32489 location_t location
)
32493 matching_parens parens
;
32494 if (!parens
.require_open (parser
))
32497 t
= cp_parser_expression (parser
);
32499 if (t
== error_mark_node
32500 || !parens
.require_close (parser
))
32501 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32502 /*or_comma=*/false,
32503 /*consume_paren=*/true);
32505 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32507 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32508 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32509 OMP_CLAUSE_CHAIN (c
) = list
;
32515 defaultmap ( tofrom : scalar ) */
32518 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32519 location_t location
)
32524 matching_parens parens
;
32525 if (!parens
.require_open (parser
))
32528 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32530 cp_parser_error (parser
, "expected %<tofrom%>");
32533 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32534 p
= IDENTIFIER_POINTER (id
);
32535 if (strcmp (p
, "tofrom") != 0)
32537 cp_parser_error (parser
, "expected %<tofrom%>");
32540 cp_lexer_consume_token (parser
->lexer
);
32541 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32544 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32546 cp_parser_error (parser
, "expected %<scalar%>");
32549 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32550 p
= IDENTIFIER_POINTER (id
);
32551 if (strcmp (p
, "scalar") != 0)
32553 cp_parser_error (parser
, "expected %<scalar%>");
32556 cp_lexer_consume_token (parser
->lexer
);
32557 if (!parens
.require_close (parser
))
32560 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32563 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32564 OMP_CLAUSE_CHAIN (c
) = list
;
32568 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32569 /*or_comma=*/false,
32570 /*consume_paren=*/true);
32578 ordered ( constant-expression ) */
32581 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32582 tree list
, location_t location
)
32584 tree c
, num
= NULL_TREE
;
32587 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32588 "ordered", location
);
32590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32592 matching_parens parens
;
32593 parens
.consume_open (parser
);
32595 num
= cp_parser_constant_expression (parser
);
32597 if (!parens
.require_close (parser
))
32598 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32599 /*or_comma=*/false,
32600 /*consume_paren=*/true);
32602 if (num
== error_mark_node
)
32604 num
= fold_non_dependent_expr (num
);
32605 if (!tree_fits_shwi_p (num
)
32606 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32607 || (n
= tree_to_shwi (num
)) <= 0
32610 error_at (location
,
32611 "ordered argument needs positive constant integer "
32617 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32618 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32619 OMP_CLAUSE_CHAIN (c
) = list
;
32624 reduction ( reduction-operator : variable-list )
32626 reduction-operator:
32627 One of: + * - & ^ | && ||
32631 reduction-operator:
32632 One of: + * - & ^ | && || min max
32636 reduction-operator:
32637 One of: + * - & ^ | && ||
32641 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32643 enum tree_code code
= ERROR_MARK
;
32644 tree nlist
, c
, id
= NULL_TREE
;
32646 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32649 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32651 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32652 case CPP_MULT
: code
= MULT_EXPR
; break;
32653 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32654 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32655 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32656 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32657 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32658 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32662 if (code
!= ERROR_MARK
)
32663 cp_lexer_consume_token (parser
->lexer
);
32666 bool saved_colon_corrects_to_scope_p
;
32667 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32668 parser
->colon_corrects_to_scope_p
= false;
32669 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32670 /*check_dependency_p=*/true,
32671 /*template_p=*/NULL
,
32672 /*declarator_p=*/false,
32673 /*optional_p=*/false);
32674 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32675 if (identifier_p (id
))
32677 const char *p
= IDENTIFIER_POINTER (id
);
32679 if (strcmp (p
, "min") == 0)
32681 else if (strcmp (p
, "max") == 0)
32683 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32685 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32687 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32689 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32690 code
= BIT_AND_EXPR
;
32691 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32692 code
= BIT_IOR_EXPR
;
32693 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32694 code
= BIT_XOR_EXPR
;
32695 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32696 code
= TRUTH_ANDIF_EXPR
;
32697 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32698 code
= TRUTH_ORIF_EXPR
;
32699 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32700 tree scope
= parser
->scope
;
32702 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32703 parser
->scope
= NULL_TREE
;
32704 parser
->qualifying_scope
= NULL_TREE
;
32705 parser
->object_scope
= NULL_TREE
;
32709 error ("invalid reduction-identifier");
32711 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32712 /*or_comma=*/false,
32713 /*consume_paren=*/true);
32718 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32721 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32723 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32725 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32726 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32733 schedule ( schedule-kind )
32734 schedule ( schedule-kind , expression )
32737 static | dynamic | guided | runtime | auto
32740 schedule ( schedule-modifier : schedule-kind )
32741 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32749 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32752 int modifiers
= 0, nmodifiers
= 0;
32754 matching_parens parens
;
32755 if (!parens
.require_open (parser
))
32758 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32760 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32762 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32763 const char *p
= IDENTIFIER_POINTER (id
);
32764 if (strcmp ("simd", p
) == 0)
32765 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32766 else if (strcmp ("monotonic", p
) == 0)
32767 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32768 else if (strcmp ("nonmonotonic", p
) == 0)
32769 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32772 cp_lexer_consume_token (parser
->lexer
);
32773 if (nmodifiers
++ == 0
32774 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32775 cp_lexer_consume_token (parser
->lexer
);
32778 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32785 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32786 const char *p
= IDENTIFIER_POINTER (id
);
32791 if (strcmp ("dynamic", p
) != 0)
32793 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32797 if (strcmp ("guided", p
) != 0)
32799 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32803 if (strcmp ("runtime", p
) != 0)
32805 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32812 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32813 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32814 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32815 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32818 cp_lexer_consume_token (parser
->lexer
);
32820 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32821 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32822 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32823 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32825 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32833 cp_lexer_consume_token (parser
->lexer
);
32835 token
= cp_lexer_peek_token (parser
->lexer
);
32836 t
= cp_parser_assignment_expression (parser
);
32838 if (t
== error_mark_node
)
32840 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32841 error_at (token
->location
, "schedule %<runtime%> does not take "
32842 "a %<chunk_size%> parameter");
32843 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32844 error_at (token
->location
, "schedule %<auto%> does not take "
32845 "a %<chunk_size%> parameter");
32847 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32849 if (!parens
.require_close (parser
))
32852 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32855 OMP_CLAUSE_SCHEDULE_KIND (c
)
32856 = (enum omp_clause_schedule_kind
)
32857 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32859 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32860 OMP_CLAUSE_CHAIN (c
) = list
;
32864 cp_parser_error (parser
, "invalid schedule kind");
32866 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32867 /*or_comma=*/false,
32868 /*consume_paren=*/true);
32876 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32877 tree list
, location_t location
)
32881 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32883 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32884 OMP_CLAUSE_CHAIN (c
) = list
;
32893 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, 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
;
32909 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32910 enum omp_clause_code code
,
32911 tree list
, location_t location
)
32913 tree c
= build_omp_clause (location
, code
);
32914 OMP_CLAUSE_CHAIN (c
) = list
;
32922 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32923 tree list
, location_t location
)
32925 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32926 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32927 OMP_CLAUSE_CHAIN (c
) = list
;
32936 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32937 enum omp_clause_code code
,
32938 tree list
, location_t location
)
32940 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32941 tree c
= build_omp_clause (location
, code
);
32942 OMP_CLAUSE_CHAIN (c
) = list
;
32947 num_teams ( expression ) */
32950 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32951 location_t location
)
32955 matching_parens parens
;
32956 if (!parens
.require_open (parser
))
32959 t
= cp_parser_expression (parser
);
32961 if (t
== error_mark_node
32962 || !parens
.require_close (parser
))
32963 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32964 /*or_comma=*/false,
32965 /*consume_paren=*/true);
32967 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32968 "num_teams", location
);
32970 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32971 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32972 OMP_CLAUSE_CHAIN (c
) = list
;
32978 thread_limit ( expression ) */
32981 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32982 location_t location
)
32986 matching_parens parens
;
32987 if (!parens
.require_open (parser
))
32990 t
= cp_parser_expression (parser
);
32992 if (t
== error_mark_node
32993 || !parens
.require_close (parser
))
32994 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32995 /*or_comma=*/false,
32996 /*consume_paren=*/true);
32998 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32999 "thread_limit", location
);
33001 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33002 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33003 OMP_CLAUSE_CHAIN (c
) = list
;
33009 aligned ( variable-list )
33010 aligned ( variable-list : constant-expression ) */
33013 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33015 tree nlist
, c
, alignment
= NULL_TREE
;
33018 matching_parens parens
;
33019 if (!parens
.require_open (parser
))
33022 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33027 alignment
= cp_parser_constant_expression (parser
);
33029 if (!parens
.require_close (parser
))
33030 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33031 /*or_comma=*/false,
33032 /*consume_paren=*/true);
33034 if (alignment
== error_mark_node
)
33035 alignment
= NULL_TREE
;
33038 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33039 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33045 linear ( variable-list )
33046 linear ( variable-list : expression )
33049 linear ( modifier ( variable-list ) )
33050 linear ( modifier ( variable-list ) : expression ) */
33053 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33056 tree nlist
, c
, step
= integer_one_node
;
33058 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33060 matching_parens parens
;
33061 if (!parens
.require_open (parser
))
33064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33066 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33067 const char *p
= IDENTIFIER_POINTER (id
);
33069 if (strcmp ("ref", p
) == 0)
33070 kind
= OMP_CLAUSE_LINEAR_REF
;
33071 else if (strcmp ("val", p
) == 0)
33072 kind
= OMP_CLAUSE_LINEAR_VAL
;
33073 else if (strcmp ("uval", p
) == 0)
33074 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33075 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33076 cp_lexer_consume_token (parser
->lexer
);
33078 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33081 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33082 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33086 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33087 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33089 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33090 else if (!parens
.require_close (parser
))
33091 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33092 /*or_comma=*/false,
33093 /*consume_paren=*/true);
33100 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33101 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33103 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33104 cp_parser_parse_tentatively (parser
);
33105 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33106 /*check_dependency_p=*/true,
33107 /*template_p=*/NULL
,
33108 /*declarator_p=*/false,
33109 /*optional_p=*/false);
33110 if (step
!= error_mark_node
)
33111 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33112 if (step
== error_mark_node
)
33115 cp_parser_abort_tentative_parse (parser
);
33117 else if (!cp_parser_parse_definitely (parser
))
33121 step
= cp_parser_expression (parser
);
33123 if (!parens
.require_close (parser
))
33124 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33125 /*or_comma=*/false,
33126 /*consume_paren=*/true);
33128 if (step
== error_mark_node
)
33132 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33134 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33135 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33142 safelen ( constant-expression ) */
33145 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33146 location_t location
)
33150 matching_parens parens
;
33151 if (!parens
.require_open (parser
))
33154 t
= cp_parser_constant_expression (parser
);
33156 if (t
== error_mark_node
33157 || !parens
.require_close (parser
))
33158 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33159 /*or_comma=*/false,
33160 /*consume_paren=*/true);
33162 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33164 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33165 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33166 OMP_CLAUSE_CHAIN (c
) = list
;
33172 simdlen ( constant-expression ) */
33175 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33176 location_t location
)
33180 matching_parens parens
;
33181 if (!parens
.require_open (parser
))
33184 t
= cp_parser_constant_expression (parser
);
33186 if (t
== error_mark_node
33187 || !parens
.require_close (parser
))
33188 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33189 /*or_comma=*/false,
33190 /*consume_paren=*/true);
33192 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33194 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33195 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33196 OMP_CLAUSE_CHAIN (c
) = list
;
33203 identifier [+/- integer]
33204 vec , identifier [+/- integer]
33208 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33213 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33215 cp_parser_error (parser
, "expected identifier");
33219 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33221 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33222 tree t
, identifier
= cp_parser_identifier (parser
);
33223 tree addend
= NULL
;
33225 if (identifier
== error_mark_node
)
33226 t
= error_mark_node
;
33229 t
= cp_parser_lookup_name_simple
33230 (parser
, identifier
,
33231 cp_lexer_peek_token (parser
->lexer
)->location
);
33232 if (t
== error_mark_node
)
33233 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33240 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33242 addend
= integer_zero_node
;
33243 goto add_to_vector
;
33245 cp_lexer_consume_token (parser
->lexer
);
33247 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33249 cp_parser_error (parser
, "expected integer");
33253 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33254 if (TREE_CODE (addend
) != INTEGER_CST
)
33256 cp_parser_error (parser
, "expected integer");
33259 cp_lexer_consume_token (parser
->lexer
);
33262 if (t
!= error_mark_node
)
33264 vec
= tree_cons (addend
, t
, vec
);
33266 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33269 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33272 cp_lexer_consume_token (parser
->lexer
);
33275 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33277 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33278 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33279 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33280 OMP_CLAUSE_CHAIN (u
) = list
;
33287 depend ( depend-kind : variable-list )
33295 depend ( sink : vec ) */
33298 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33301 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33303 matching_parens parens
;
33304 if (!parens
.require_open (parser
))
33307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33309 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33310 const char *p
= IDENTIFIER_POINTER (id
);
33312 if (strcmp ("in", p
) == 0)
33313 kind
= OMP_CLAUSE_DEPEND_IN
;
33314 else if (strcmp ("inout", p
) == 0)
33315 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33316 else if (strcmp ("out", p
) == 0)
33317 kind
= OMP_CLAUSE_DEPEND_OUT
;
33318 else if (strcmp ("source", p
) == 0)
33319 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33320 else if (strcmp ("sink", p
) == 0)
33321 kind
= OMP_CLAUSE_DEPEND_SINK
;
33328 cp_lexer_consume_token (parser
->lexer
);
33330 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33332 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33333 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33334 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33335 OMP_CLAUSE_CHAIN (c
) = list
;
33336 if (!parens
.require_close (parser
))
33337 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33338 /*or_comma=*/false,
33339 /*consume_paren=*/true);
33343 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33346 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33347 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33350 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33353 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33354 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33359 cp_parser_error (parser
, "invalid depend kind");
33361 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33362 /*or_comma=*/false,
33363 /*consume_paren=*/true);
33368 map ( map-kind : variable-list )
33369 map ( variable-list )
33372 alloc | to | from | tofrom
33376 alloc | to | from | tofrom | release | delete
33378 map ( always [,] map-kind: variable-list ) */
33381 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33384 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33385 bool always
= false;
33387 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33390 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33392 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33393 const char *p
= IDENTIFIER_POINTER (id
);
33395 if (strcmp ("always", p
) == 0)
33398 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33400 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33401 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33403 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33407 cp_lexer_consume_token (parser
->lexer
);
33409 cp_lexer_consume_token (parser
->lexer
);
33414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33415 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33417 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33418 const char *p
= IDENTIFIER_POINTER (id
);
33420 if (strcmp ("alloc", p
) == 0)
33421 kind
= GOMP_MAP_ALLOC
;
33422 else if (strcmp ("to", p
) == 0)
33423 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33424 else if (strcmp ("from", p
) == 0)
33425 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33426 else if (strcmp ("tofrom", p
) == 0)
33427 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33428 else if (strcmp ("release", p
) == 0)
33429 kind
= GOMP_MAP_RELEASE
;
33432 cp_parser_error (parser
, "invalid map kind");
33433 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33434 /*or_comma=*/false,
33435 /*consume_paren=*/true);
33438 cp_lexer_consume_token (parser
->lexer
);
33439 cp_lexer_consume_token (parser
->lexer
);
33441 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33442 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33444 kind
= GOMP_MAP_DELETE
;
33445 cp_lexer_consume_token (parser
->lexer
);
33446 cp_lexer_consume_token (parser
->lexer
);
33449 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33452 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33453 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33459 device ( expression ) */
33462 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33463 location_t location
)
33467 matching_parens parens
;
33468 if (!parens
.require_open (parser
))
33471 t
= cp_parser_expression (parser
);
33473 if (t
== error_mark_node
33474 || !parens
.require_close (parser
))
33475 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33476 /*or_comma=*/false,
33477 /*consume_paren=*/true);
33479 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33480 "device", location
);
33482 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33483 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33484 OMP_CLAUSE_CHAIN (c
) = list
;
33490 dist_schedule ( static )
33491 dist_schedule ( static , expression ) */
33494 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33495 location_t location
)
33499 matching_parens parens
;
33500 if (!parens
.require_open (parser
))
33503 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33505 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33507 cp_lexer_consume_token (parser
->lexer
);
33509 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33511 cp_lexer_consume_token (parser
->lexer
);
33513 t
= cp_parser_assignment_expression (parser
);
33515 if (t
== error_mark_node
)
33517 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33519 if (!parens
.require_close (parser
))
33522 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33525 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33527 OMP_CLAUSE_CHAIN (c
) = list
;
33531 cp_parser_error (parser
, "invalid dist_schedule kind");
33533 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33534 /*or_comma=*/false,
33535 /*consume_paren=*/true);
33540 proc_bind ( proc-bind-kind )
33543 master | close | spread */
33546 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33547 location_t location
)
33550 enum omp_clause_proc_bind_kind kind
;
33552 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33557 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33558 const char *p
= IDENTIFIER_POINTER (id
);
33560 if (strcmp ("master", p
) == 0)
33561 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33562 else if (strcmp ("close", p
) == 0)
33563 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33564 else if (strcmp ("spread", p
) == 0)
33565 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33572 cp_lexer_consume_token (parser
->lexer
);
33573 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33576 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33577 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33579 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33580 OMP_CLAUSE_CHAIN (c
) = list
;
33584 cp_parser_error (parser
, "invalid depend kind");
33586 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33587 /*or_comma=*/false,
33588 /*consume_paren=*/true);
33593 async [( int-expr )] */
33596 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33599 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33601 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33603 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33605 matching_parens parens
;
33606 parens
.consume_open (parser
);
33608 t
= cp_parser_expression (parser
);
33609 if (t
== error_mark_node
33610 || !parens
.require_close (parser
))
33611 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33612 /*or_comma=*/false,
33613 /*consume_paren=*/true);
33616 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33618 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33619 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33620 OMP_CLAUSE_CHAIN (c
) = list
;
33626 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33627 is a bitmask in MASK. Return the list of clauses found. */
33630 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33631 const char *where
, cp_token
*pragma_tok
,
33632 bool finish_p
= true)
33634 tree clauses
= NULL
;
33637 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33640 pragma_omp_clause c_kind
;
33641 omp_clause_code code
;
33642 const char *c_name
;
33643 tree prev
= clauses
;
33645 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33646 cp_lexer_consume_token (parser
->lexer
);
33648 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33649 c_kind
= cp_parser_omp_clause_name (parser
);
33653 case PRAGMA_OACC_CLAUSE_ASYNC
:
33654 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33657 case PRAGMA_OACC_CLAUSE_AUTO
:
33658 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33662 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33663 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33664 c_name
= "collapse";
33666 case PRAGMA_OACC_CLAUSE_COPY
:
33667 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33670 case PRAGMA_OACC_CLAUSE_COPYIN
:
33671 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33674 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33675 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33676 c_name
= "copyout";
33678 case PRAGMA_OACC_CLAUSE_CREATE
:
33679 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33682 case PRAGMA_OACC_CLAUSE_DELETE
:
33683 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33686 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33687 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33688 c_name
= "default";
33690 case PRAGMA_OACC_CLAUSE_DEVICE
:
33691 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33694 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33695 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33696 c_name
= "deviceptr";
33698 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33699 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33700 c_name
= "device_resident";
33702 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33703 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33705 c_name
= "firstprivate";
33707 case PRAGMA_OACC_CLAUSE_GANG
:
33709 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33712 case PRAGMA_OACC_CLAUSE_HOST
:
33713 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33716 case PRAGMA_OACC_CLAUSE_IF
:
33717 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33720 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33721 clauses
= cp_parser_oacc_simple_clause (parser
,
33722 OMP_CLAUSE_INDEPENDENT
,
33724 c_name
= "independent";
33726 case PRAGMA_OACC_CLAUSE_LINK
:
33727 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33730 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33731 code
= OMP_CLAUSE_NUM_GANGS
;
33732 c_name
= "num_gangs";
33733 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33736 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33737 c_name
= "num_workers";
33738 code
= OMP_CLAUSE_NUM_WORKERS
;
33739 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33742 case PRAGMA_OACC_CLAUSE_PRESENT
:
33743 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33744 c_name
= "present";
33746 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33747 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33748 c_name
= "present_or_copy";
33750 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33751 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33752 c_name
= "present_or_copyin";
33754 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33755 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33756 c_name
= "present_or_copyout";
33758 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33759 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33760 c_name
= "present_or_create";
33762 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33763 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33765 c_name
= "private";
33767 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33768 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33769 c_name
= "reduction";
33771 case PRAGMA_OACC_CLAUSE_SELF
:
33772 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33775 case PRAGMA_OACC_CLAUSE_SEQ
:
33776 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33780 case PRAGMA_OACC_CLAUSE_TILE
:
33781 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33784 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33785 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33787 c_name
= "use_device";
33789 case PRAGMA_OACC_CLAUSE_VECTOR
:
33791 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33794 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33795 c_name
= "vector_length";
33796 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33797 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33800 case PRAGMA_OACC_CLAUSE_WAIT
:
33801 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33804 case PRAGMA_OACC_CLAUSE_WORKER
:
33806 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33810 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33816 if (((mask
>> c_kind
) & 1) == 0)
33818 /* Remove the invalid clause(s) from the list to avoid
33819 confusing the rest of the compiler. */
33821 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33826 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33829 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33834 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33835 is a bitmask in MASK. Return the list of clauses found; the result
33836 of clause default goes in *pdefault. */
33839 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33840 const char *where
, cp_token
*pragma_tok
,
33841 bool finish_p
= true)
33843 tree clauses
= NULL
;
33845 cp_token
*token
= NULL
;
33847 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33849 pragma_omp_clause c_kind
;
33850 const char *c_name
;
33851 tree prev
= clauses
;
33853 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33854 cp_lexer_consume_token (parser
->lexer
);
33856 token
= cp_lexer_peek_token (parser
->lexer
);
33857 c_kind
= cp_parser_omp_clause_name (parser
);
33861 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33862 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33864 c_name
= "collapse";
33866 case PRAGMA_OMP_CLAUSE_COPYIN
:
33867 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33870 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33871 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33873 c_name
= "copyprivate";
33875 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33876 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33877 token
->location
, false);
33878 c_name
= "default";
33880 case PRAGMA_OMP_CLAUSE_FINAL
:
33881 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33884 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33885 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33887 c_name
= "firstprivate";
33889 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33890 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33892 c_name
= "grainsize";
33894 case PRAGMA_OMP_CLAUSE_HINT
:
33895 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33899 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33900 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33902 c_name
= "defaultmap";
33904 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33905 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33907 c_name
= "use_device_ptr";
33909 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33910 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33912 c_name
= "is_device_ptr";
33914 case PRAGMA_OMP_CLAUSE_IF
:
33915 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33919 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33920 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33922 c_name
= "lastprivate";
33924 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33925 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33927 c_name
= "mergeable";
33929 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33930 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33933 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33934 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33936 c_name
= "num_tasks";
33938 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33939 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33941 c_name
= "num_threads";
33943 case PRAGMA_OMP_CLAUSE_ORDERED
:
33944 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33946 c_name
= "ordered";
33948 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33949 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33951 c_name
= "priority";
33953 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33954 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33956 c_name
= "private";
33958 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33959 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33960 c_name
= "reduction";
33962 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33963 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33965 c_name
= "schedule";
33967 case PRAGMA_OMP_CLAUSE_SHARED
:
33968 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33972 case PRAGMA_OMP_CLAUSE_UNTIED
:
33973 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33977 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33978 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33979 clauses
, token
->location
);
33980 c_name
= "inbranch";
33982 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33983 clauses
= cp_parser_omp_clause_branch (parser
,
33984 OMP_CLAUSE_NOTINBRANCH
,
33985 clauses
, token
->location
);
33986 c_name
= "notinbranch";
33988 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33989 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33990 clauses
, token
->location
);
33991 c_name
= "parallel";
33995 error_at (token
->location
, "%qs must be the first clause of %qs",
34000 case PRAGMA_OMP_CLAUSE_FOR
:
34001 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34002 clauses
, token
->location
);
34005 goto clause_not_first
;
34007 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34008 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34009 clauses
, token
->location
);
34010 c_name
= "sections";
34012 goto clause_not_first
;
34014 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34015 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34016 clauses
, token
->location
);
34017 c_name
= "taskgroup";
34019 goto clause_not_first
;
34021 case PRAGMA_OMP_CLAUSE_LINK
:
34022 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34025 case PRAGMA_OMP_CLAUSE_TO
:
34026 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34027 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34030 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34033 case PRAGMA_OMP_CLAUSE_FROM
:
34034 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34037 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34038 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34040 c_name
= "uniform";
34042 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34043 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34045 c_name
= "num_teams";
34047 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34048 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34050 c_name
= "thread_limit";
34052 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34053 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34054 c_name
= "aligned";
34056 case PRAGMA_OMP_CLAUSE_LINEAR
:
34058 bool declare_simd
= false;
34059 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34060 declare_simd
= true;
34061 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34065 case PRAGMA_OMP_CLAUSE_DEPEND
:
34066 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34070 case PRAGMA_OMP_CLAUSE_MAP
:
34071 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34074 case PRAGMA_OMP_CLAUSE_DEVICE
:
34075 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34079 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34080 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34082 c_name
= "dist_schedule";
34084 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34085 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34087 c_name
= "proc_bind";
34089 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34090 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34092 c_name
= "safelen";
34094 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34095 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34097 c_name
= "simdlen";
34099 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34100 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34102 c_name
= "nogroup";
34104 case PRAGMA_OMP_CLAUSE_THREADS
:
34106 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34107 clauses
, token
->location
);
34108 c_name
= "threads";
34110 case PRAGMA_OMP_CLAUSE_SIMD
:
34112 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34113 clauses
, token
->location
);
34117 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34123 if (((mask
>> c_kind
) & 1) == 0)
34125 /* Remove the invalid clause(s) from the list to avoid
34126 confusing the rest of the compiler. */
34128 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34132 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34135 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34136 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34138 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34147 In practice, we're also interested in adding the statement to an
34148 outer node. So it is convenient if we work around the fact that
34149 cp_parser_statement calls add_stmt. */
34152 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34154 unsigned save
= parser
->in_statement
;
34156 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34157 This preserves the "not within loop or switch" style error messages
34158 for nonsense cases like
34164 if (parser
->in_statement
)
34165 parser
->in_statement
= IN_OMP_BLOCK
;
34171 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34173 parser
->in_statement
= save
;
34177 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34179 tree stmt
= begin_omp_structured_block ();
34180 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34182 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34184 cp_parser_end_omp_structured_block (parser
, save
);
34185 return finish_omp_structured_block (stmt
);
34189 # pragma omp atomic new-line
34193 x binop= expr | x++ | ++x | x-- | --x
34195 +, *, -, /, &, ^, |, <<, >>
34197 where x is an lvalue expression with scalar type.
34200 # pragma omp atomic new-line
34203 # pragma omp atomic read new-line
34206 # pragma omp atomic write new-line
34209 # pragma omp atomic update new-line
34212 # pragma omp atomic capture new-line
34215 # pragma omp atomic capture new-line
34223 expression-stmt | x = x binop expr
34225 v = expression-stmt
34227 { v = x; update-stmt; } | { update-stmt; v = x; }
34231 expression-stmt | x = x binop expr | x = expr binop x
34235 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34237 where x and v are lvalue expressions with scalar type. */
34240 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34242 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34243 tree rhs1
= NULL_TREE
, orig_lhs
;
34244 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34245 bool structured_block
= false;
34246 bool seq_cst
= false;
34248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34250 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34251 const char *p
= IDENTIFIER_POINTER (id
);
34253 if (!strcmp (p
, "seq_cst"))
34256 cp_lexer_consume_token (parser
->lexer
);
34257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34258 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34259 cp_lexer_consume_token (parser
->lexer
);
34262 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34264 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34265 const char *p
= IDENTIFIER_POINTER (id
);
34267 if (!strcmp (p
, "read"))
34268 code
= OMP_ATOMIC_READ
;
34269 else if (!strcmp (p
, "write"))
34271 else if (!strcmp (p
, "update"))
34273 else if (!strcmp (p
, "capture"))
34274 code
= OMP_ATOMIC_CAPTURE_NEW
;
34278 cp_lexer_consume_token (parser
->lexer
);
34282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34283 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34284 cp_lexer_consume_token (parser
->lexer
);
34286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34288 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34289 const char *p
= IDENTIFIER_POINTER (id
);
34291 if (!strcmp (p
, "seq_cst"))
34294 cp_lexer_consume_token (parser
->lexer
);
34298 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34302 case OMP_ATOMIC_READ
:
34303 case NOP_EXPR
: /* atomic write */
34304 v
= cp_parser_unary_expression (parser
);
34305 if (v
== error_mark_node
)
34307 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34309 if (code
== NOP_EXPR
)
34310 lhs
= cp_parser_expression (parser
);
34312 lhs
= cp_parser_unary_expression (parser
);
34313 if (lhs
== error_mark_node
)
34315 if (code
== NOP_EXPR
)
34317 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34325 case OMP_ATOMIC_CAPTURE_NEW
:
34326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34328 cp_lexer_consume_token (parser
->lexer
);
34329 structured_block
= true;
34333 v
= cp_parser_unary_expression (parser
);
34334 if (v
== error_mark_node
)
34336 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34344 lhs
= cp_parser_unary_expression (parser
);
34346 switch (TREE_CODE (lhs
))
34351 case POSTINCREMENT_EXPR
:
34352 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34353 code
= OMP_ATOMIC_CAPTURE_OLD
;
34355 case PREINCREMENT_EXPR
:
34356 lhs
= TREE_OPERAND (lhs
, 0);
34357 opcode
= PLUS_EXPR
;
34358 rhs
= integer_one_node
;
34361 case POSTDECREMENT_EXPR
:
34362 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34363 code
= OMP_ATOMIC_CAPTURE_OLD
;
34365 case PREDECREMENT_EXPR
:
34366 lhs
= TREE_OPERAND (lhs
, 0);
34367 opcode
= MINUS_EXPR
;
34368 rhs
= integer_one_node
;
34371 case COMPOUND_EXPR
:
34372 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34373 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34374 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34375 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34376 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34377 (TREE_OPERAND (lhs
, 1), 0), 0)))
34379 /* Undo effects of boolean_increment for post {in,de}crement. */
34380 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34383 if (TREE_CODE (lhs
) == MODIFY_EXPR
34384 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34386 /* Undo effects of boolean_increment. */
34387 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34389 /* This is pre or post increment. */
34390 rhs
= TREE_OPERAND (lhs
, 1);
34391 lhs
= TREE_OPERAND (lhs
, 0);
34393 if (code
== OMP_ATOMIC_CAPTURE_NEW
34394 && !structured_block
34395 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34396 code
= OMP_ATOMIC_CAPTURE_OLD
;
34402 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34405 opcode
= MULT_EXPR
;
34408 opcode
= TRUNC_DIV_EXPR
;
34411 opcode
= PLUS_EXPR
;
34414 opcode
= MINUS_EXPR
;
34416 case CPP_LSHIFT_EQ
:
34417 opcode
= LSHIFT_EXPR
;
34419 case CPP_RSHIFT_EQ
:
34420 opcode
= RSHIFT_EXPR
;
34423 opcode
= BIT_AND_EXPR
;
34426 opcode
= BIT_IOR_EXPR
;
34429 opcode
= BIT_XOR_EXPR
;
34432 enum cp_parser_prec oprec
;
34434 cp_lexer_consume_token (parser
->lexer
);
34435 cp_parser_parse_tentatively (parser
);
34436 rhs1
= cp_parser_simple_cast_expression (parser
);
34437 if (rhs1
== error_mark_node
)
34439 cp_parser_abort_tentative_parse (parser
);
34440 cp_parser_simple_cast_expression (parser
);
34443 token
= cp_lexer_peek_token (parser
->lexer
);
34444 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34446 cp_parser_abort_tentative_parse (parser
);
34447 cp_parser_parse_tentatively (parser
);
34448 rhs
= cp_parser_binary_expression (parser
, false, true,
34449 PREC_NOT_OPERATOR
, NULL
);
34450 if (rhs
== error_mark_node
)
34452 cp_parser_abort_tentative_parse (parser
);
34453 cp_parser_binary_expression (parser
, false, true,
34454 PREC_NOT_OPERATOR
, NULL
);
34457 switch (TREE_CODE (rhs
))
34460 case TRUNC_DIV_EXPR
:
34469 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34471 if (cp_parser_parse_definitely (parser
))
34473 opcode
= TREE_CODE (rhs
);
34474 rhs1
= TREE_OPERAND (rhs
, 0);
34475 rhs
= TREE_OPERAND (rhs
, 1);
34485 cp_parser_abort_tentative_parse (parser
);
34486 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34488 rhs
= cp_parser_expression (parser
);
34489 if (rhs
== error_mark_node
)
34495 cp_parser_error (parser
,
34496 "invalid form of %<#pragma omp atomic%>");
34499 if (!cp_parser_parse_definitely (parser
))
34501 switch (token
->type
)
34503 case CPP_SEMICOLON
:
34504 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34506 code
= OMP_ATOMIC_CAPTURE_OLD
;
34511 cp_lexer_consume_token (parser
->lexer
);
34514 else if (structured_block
)
34521 cp_parser_error (parser
,
34522 "invalid form of %<#pragma omp atomic%>");
34525 opcode
= MULT_EXPR
;
34528 opcode
= TRUNC_DIV_EXPR
;
34531 opcode
= PLUS_EXPR
;
34534 opcode
= MINUS_EXPR
;
34537 opcode
= LSHIFT_EXPR
;
34540 opcode
= RSHIFT_EXPR
;
34543 opcode
= BIT_AND_EXPR
;
34546 opcode
= BIT_IOR_EXPR
;
34549 opcode
= BIT_XOR_EXPR
;
34552 cp_parser_error (parser
,
34553 "invalid operator for %<#pragma omp atomic%>");
34556 oprec
= TOKEN_PRECEDENCE (token
);
34557 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34558 if (commutative_tree_code (opcode
))
34559 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34560 cp_lexer_consume_token (parser
->lexer
);
34561 rhs
= cp_parser_binary_expression (parser
, false, false,
34563 if (rhs
== error_mark_node
)
34568 cp_parser_error (parser
,
34569 "invalid operator for %<#pragma omp atomic%>");
34572 cp_lexer_consume_token (parser
->lexer
);
34574 rhs
= cp_parser_expression (parser
);
34575 if (rhs
== error_mark_node
)
34580 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34582 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34584 v
= cp_parser_unary_expression (parser
);
34585 if (v
== error_mark_node
)
34587 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34589 lhs1
= cp_parser_unary_expression (parser
);
34590 if (lhs1
== error_mark_node
)
34593 if (structured_block
)
34595 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34596 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34599 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34600 if (!structured_block
)
34601 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34605 cp_parser_skip_to_end_of_block_or_statement (parser
);
34606 if (structured_block
)
34608 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34609 cp_lexer_consume_token (parser
->lexer
);
34610 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34612 cp_parser_skip_to_end_of_block_or_statement (parser
);
34613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34614 cp_lexer_consume_token (parser
->lexer
);
34621 # pragma omp barrier new-line */
34624 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34626 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34627 finish_omp_barrier ();
34631 # pragma omp critical [(name)] new-line
34635 # pragma omp critical [(name) [hint(expression)]] new-line
34636 structured-block */
34638 #define OMP_CRITICAL_CLAUSE_MASK \
34639 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34642 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34644 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34648 matching_parens parens
;
34649 parens
.consume_open (parser
);
34651 name
= cp_parser_identifier (parser
);
34653 if (name
== error_mark_node
34654 || !parens
.require_close (parser
))
34655 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34656 /*or_comma=*/false,
34657 /*consume_paren=*/true);
34658 if (name
== error_mark_node
)
34661 clauses
= cp_parser_omp_all_clauses (parser
,
34662 OMP_CRITICAL_CLAUSE_MASK
,
34663 "#pragma omp critical", pragma_tok
);
34666 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34668 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34669 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34673 # pragma omp flush flush-vars[opt] new-line
34676 ( variable-list ) */
34679 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34681 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34682 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34683 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34685 finish_omp_flush ();
34688 /* Helper function, to parse omp for increment expression. */
34691 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34693 tree cond
= cp_parser_binary_expression (parser
, false, true,
34694 PREC_NOT_OPERATOR
, NULL
);
34695 if (cond
== error_mark_node
34696 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34698 cp_parser_skip_to_end_of_statement (parser
);
34699 return error_mark_node
;
34702 switch (TREE_CODE (cond
))
34710 /* Fall through: OpenMP disallows NE_EXPR. */
34711 gcc_fallthrough ();
34713 return error_mark_node
;
34716 /* If decl is an iterator, preserve LHS and RHS of the relational
34717 expr until finish_omp_for. */
34719 && (type_dependent_expression_p (decl
)
34720 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34723 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34725 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34726 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34727 /*overload=*/NULL
, tf_warning_or_error
);
34730 /* Helper function, to parse omp for increment expression. */
34733 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34735 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34741 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34743 op
= (token
->type
== CPP_PLUS_PLUS
34744 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34745 cp_lexer_consume_token (parser
->lexer
);
34746 lhs
= cp_parser_simple_cast_expression (parser
);
34748 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34749 return error_mark_node
;
34750 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34753 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34755 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34756 return error_mark_node
;
34758 token
= cp_lexer_peek_token (parser
->lexer
);
34759 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34761 op
= (token
->type
== CPP_PLUS_PLUS
34762 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34763 cp_lexer_consume_token (parser
->lexer
);
34764 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34767 op
= cp_parser_assignment_operator_opt (parser
);
34768 if (op
== ERROR_MARK
)
34769 return error_mark_node
;
34771 if (op
!= NOP_EXPR
)
34773 rhs
= cp_parser_assignment_expression (parser
);
34774 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34775 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34778 lhs
= cp_parser_binary_expression (parser
, false, false,
34779 PREC_ADDITIVE_EXPRESSION
, NULL
);
34780 token
= cp_lexer_peek_token (parser
->lexer
);
34781 decl_first
= (lhs
== decl
34782 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34785 if (token
->type
!= CPP_PLUS
34786 && token
->type
!= CPP_MINUS
)
34787 return error_mark_node
;
34791 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34792 cp_lexer_consume_token (parser
->lexer
);
34793 rhs
= cp_parser_binary_expression (parser
, false, false,
34794 PREC_ADDITIVE_EXPRESSION
, NULL
);
34795 token
= cp_lexer_peek_token (parser
->lexer
);
34796 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34798 if (lhs
== NULL_TREE
)
34800 if (op
== PLUS_EXPR
)
34803 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34804 tf_warning_or_error
);
34807 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34808 ERROR_MARK
, NULL
, tf_warning_or_error
);
34811 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34816 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34817 || op
== MINUS_EXPR
)
34818 return error_mark_node
;
34819 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34822 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34824 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34827 /* Parse the initialization statement of an OpenMP for loop.
34829 Return true if the resulting construct should have an
34830 OMP_CLAUSE_PRIVATE added to it. */
34833 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34834 tree
&this_pre_body
,
34835 vec
<tree
, va_gc
> *for_block
,
34841 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34844 tree add_private_clause
= NULL_TREE
;
34846 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34850 integer-type var = lb
34851 random-access-iterator-type var = lb
34852 pointer-type var = lb
34854 cp_decl_specifier_seq type_specifiers
;
34856 /* First, try to parse as an initialized declaration. See
34857 cp_parser_condition, from whence the bulk of this is copied. */
34859 cp_parser_parse_tentatively (parser
);
34860 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34861 /*is_trailing_return=*/false,
34863 if (cp_parser_parse_definitely (parser
))
34865 /* If parsing a type specifier seq succeeded, then this
34866 MUST be a initialized declaration. */
34867 tree asm_specification
, attributes
;
34868 cp_declarator
*declarator
;
34870 declarator
= cp_parser_declarator (parser
,
34871 CP_PARSER_DECLARATOR_NAMED
,
34872 /*ctor_dtor_or_conv_p=*/NULL
,
34873 /*parenthesized_p=*/NULL
,
34874 /*member_p=*/false,
34875 /*friend_p=*/false);
34876 attributes
= cp_parser_attributes_opt (parser
);
34877 asm_specification
= cp_parser_asm_specification_opt (parser
);
34879 if (declarator
== cp_error_declarator
)
34880 cp_parser_skip_to_end_of_statement (parser
);
34884 tree pushed_scope
, auto_node
;
34886 decl
= start_decl (declarator
, &type_specifiers
,
34887 SD_INITIALIZED
, attributes
,
34888 /*prefix_attributes=*/NULL_TREE
,
34891 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34892 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34894 if (cp_lexer_next_token_is (parser
->lexer
,
34896 error ("parenthesized initialization is not allowed in "
34897 "OpenMP %<for%> loop");
34899 /* Trigger an error. */
34900 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34902 init
= error_mark_node
;
34903 cp_parser_skip_to_end_of_statement (parser
);
34905 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34906 || type_dependent_expression_p (decl
)
34909 bool is_direct_init
, is_non_constant_init
;
34911 init
= cp_parser_initializer (parser
,
34913 &is_non_constant_init
);
34918 = do_auto_deduction (TREE_TYPE (decl
), init
,
34921 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34922 && !type_dependent_expression_p (decl
))
34926 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34928 LOOKUP_ONLYCONVERTING
);
34930 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34932 vec_safe_push (for_block
, this_pre_body
);
34937 init
= pop_stmt_list (this_pre_body
);
34938 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34940 tree_stmt_iterator i
= tsi_start (init
);
34941 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34942 while (!tsi_end_p (i
))
34944 tree t
= tsi_stmt (i
);
34945 if (TREE_CODE (t
) == DECL_EXPR
34946 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34949 vec_safe_push (for_block
, t
);
34954 if (tsi_one_before_end_p (i
))
34956 tree t
= tsi_stmt (i
);
34958 free_stmt_list (init
);
34963 this_pre_body
= NULL_TREE
;
34968 cp_lexer_consume_token (parser
->lexer
);
34969 init
= cp_parser_assignment_expression (parser
);
34972 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34973 init
= error_mark_node
;
34975 cp_finish_decl (decl
, NULL_TREE
,
34976 /*init_const_expr_p=*/false,
34978 LOOKUP_ONLYCONVERTING
);
34982 pop_scope (pushed_scope
);
34988 /* If parsing a type specifier sequence failed, then
34989 this MUST be a simple expression. */
34990 cp_parser_parse_tentatively (parser
);
34991 decl
= cp_parser_primary_expression (parser
, false, false,
34993 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34994 if (!cp_parser_error_occurred (parser
)
34996 && (TREE_CODE (decl
) == COMPONENT_REF
34997 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34999 cp_parser_abort_tentative_parse (parser
);
35000 cp_parser_parse_tentatively (parser
);
35001 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35002 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35003 /*check_dependency_p=*/true,
35004 /*template_p=*/NULL
,
35005 /*declarator_p=*/false,
35006 /*optional_p=*/false);
35007 if (name
!= error_mark_node
35008 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35010 decl
= cp_parser_lookup_name_simple (parser
, name
,
35012 if (TREE_CODE (decl
) == FIELD_DECL
)
35013 add_private_clause
= omp_privatize_field (decl
, false);
35015 cp_parser_abort_tentative_parse (parser
);
35016 cp_parser_parse_tentatively (parser
);
35017 decl
= cp_parser_primary_expression (parser
, false, false,
35020 if (!cp_parser_error_occurred (parser
)
35023 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35027 cp_parser_parse_definitely (parser
);
35028 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35029 rhs
= cp_parser_assignment_expression (parser
);
35031 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35034 tf_warning_or_error
));
35035 if (!add_private_clause
)
35036 add_private_clause
= decl
;
35041 cp_parser_abort_tentative_parse (parser
);
35042 init
= cp_parser_expression (parser
);
35045 if (TREE_CODE (init
) == MODIFY_EXPR
35046 || TREE_CODE (init
) == MODOP_EXPR
)
35047 real_decl
= TREE_OPERAND (init
, 0);
35051 return add_private_clause
;
35054 /* Parse the restricted form of the for statement allowed by OpenMP. */
35057 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35058 tree
*cclauses
, bool *if_p
)
35060 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35061 tree real_decl
, initv
, condv
, incrv
, declv
;
35062 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35063 location_t loc_first
;
35064 bool collapse_err
= false;
35065 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35066 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35067 auto_vec
<tree
, 4> orig_inits
;
35068 bool tiling
= false;
35070 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35071 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35072 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35073 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35076 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35078 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35079 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35082 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35085 if (ordered
&& ordered
< collapse
)
35087 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35088 "%<ordered%> clause parameter is less than %<collapse%>");
35089 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35090 = build_int_cst (NULL_TREE
, collapse
);
35091 ordered
= collapse
;
35095 for (tree
*pc
= &clauses
; *pc
; )
35096 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35098 error_at (OMP_CLAUSE_LOCATION (*pc
),
35099 "%<linear%> clause may not be specified together "
35100 "with %<ordered%> clause with a parameter");
35101 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35104 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35107 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35108 count
= ordered
? ordered
: collapse
;
35110 declv
= make_tree_vec (count
);
35111 initv
= make_tree_vec (count
);
35112 condv
= make_tree_vec (count
);
35113 incrv
= make_tree_vec (count
);
35115 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35117 for (i
= 0; i
< count
; i
++)
35119 int bracecount
= 0;
35120 tree add_private_clause
= NULL_TREE
;
35123 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35126 cp_parser_error (parser
, "for statement expected");
35129 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35131 matching_parens parens
;
35132 if (!parens
.require_open (parser
))
35135 init
= orig_init
= decl
= real_decl
= NULL
;
35136 this_pre_body
= push_stmt_list ();
35139 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35140 init
, orig_init
, decl
, real_decl
);
35142 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35145 this_pre_body
= pop_stmt_list (this_pre_body
);
35149 pre_body
= push_stmt_list ();
35151 add_stmt (this_pre_body
);
35152 pre_body
= pop_stmt_list (pre_body
);
35155 pre_body
= this_pre_body
;
35160 if (cclauses
!= NULL
35161 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35162 && real_decl
!= NULL_TREE
)
35165 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35166 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35167 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35169 error_at (loc
, "iteration variable %qD"
35170 " should not be firstprivate", real_decl
);
35171 *c
= OMP_CLAUSE_CHAIN (*c
);
35173 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35174 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35176 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35178 *c
= OMP_CLAUSE_CHAIN (*c
);
35179 if (code
== OMP_SIMD
)
35181 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35182 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35186 OMP_CLAUSE_CHAIN (l
) = clauses
;
35189 add_private_clause
= NULL_TREE
;
35193 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35194 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35195 add_private_clause
= NULL_TREE
;
35196 c
= &OMP_CLAUSE_CHAIN (*c
);
35200 if (add_private_clause
)
35203 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35205 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35206 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35207 && OMP_CLAUSE_DECL (c
) == decl
)
35209 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35210 && OMP_CLAUSE_DECL (c
) == decl
)
35211 error_at (loc
, "iteration variable %qD "
35212 "should not be firstprivate",
35214 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35215 && OMP_CLAUSE_DECL (c
) == decl
)
35216 error_at (loc
, "iteration variable %qD should not be reduction",
35221 if (code
!= OMP_SIMD
)
35222 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35223 else if (collapse
== 1)
35224 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35226 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35227 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35228 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35231 OMP_CLAUSE_CHAIN (c
) = clauses
;
35233 /* For linear, signal that we need to fill up
35234 the so far unknown linear step. */
35235 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35236 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35243 cond
= cp_parser_omp_for_cond (parser
, decl
);
35244 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35247 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35249 /* If decl is an iterator, preserve the operator on decl
35250 until finish_omp_for. */
35252 && ((processing_template_decl
35253 && (TREE_TYPE (real_decl
) == NULL_TREE
35254 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35255 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35256 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35258 incr
= cp_parser_expression (parser
);
35259 if (!EXPR_HAS_LOCATION (incr
))
35260 protected_set_expr_location (incr
, input_location
);
35263 if (!parens
.require_close (parser
))
35264 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35265 /*or_comma=*/false,
35266 /*consume_paren=*/true);
35268 TREE_VEC_ELT (declv
, i
) = decl
;
35269 TREE_VEC_ELT (initv
, i
) = init
;
35270 TREE_VEC_ELT (condv
, i
) = cond
;
35271 TREE_VEC_ELT (incrv
, i
) = incr
;
35274 orig_inits
.safe_grow_cleared (i
+ 1);
35275 orig_inits
[i
] = orig_init
;
35278 if (i
== count
- 1)
35281 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35282 in between the collapsed for loops to be still considered perfectly
35283 nested. Hopefully the final version clarifies this.
35284 For now handle (multiple) {'s and empty statements. */
35285 cp_parser_parse_tentatively (parser
);
35288 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35290 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35292 cp_lexer_consume_token (parser
->lexer
);
35295 else if (bracecount
35296 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35297 cp_lexer_consume_token (parser
->lexer
);
35300 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35301 error_at (loc
, "not enough for loops to collapse");
35302 collapse_err
= true;
35303 cp_parser_abort_tentative_parse (parser
);
35311 cp_parser_parse_definitely (parser
);
35312 nbraces
+= bracecount
;
35319 /* Note that we saved the original contents of this flag when we entered
35320 the structured block, and so we don't need to re-save it here. */
35321 parser
->in_statement
= IN_OMP_FOR
;
35323 /* Note that the grammar doesn't call for a structured block here,
35324 though the loop as a whole is a structured block. */
35325 body
= push_stmt_list ();
35326 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35327 body
= pop_stmt_list (body
);
35329 if (declv
== NULL_TREE
)
35332 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35333 body
, pre_body
, &orig_inits
, clauses
);
35337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35339 cp_lexer_consume_token (parser
->lexer
);
35342 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35343 cp_lexer_consume_token (parser
->lexer
);
35348 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35349 "collapsed loops not perfectly nested");
35351 collapse_err
= true;
35352 cp_parser_statement_seq_opt (parser
, NULL
);
35353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35358 while (!for_block
->is_empty ())
35360 tree t
= for_block
->pop ();
35361 if (TREE_CODE (t
) == STATEMENT_LIST
)
35362 add_stmt (pop_stmt_list (t
));
35366 release_tree_vector (for_block
);
35371 /* Helper function for OpenMP parsing, split clauses and call
35372 finish_omp_clauses on each of the set of clauses afterwards. */
35375 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35376 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35379 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35380 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35382 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35386 #pragma omp simd simd-clause[optseq] new-line
35389 #define OMP_SIMD_CLAUSE_MASK \
35390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35400 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35401 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35404 tree clauses
, sb
, ret
;
35406 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35408 strcat (p_name
, " simd");
35409 mask
|= OMP_SIMD_CLAUSE_MASK
;
35411 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35415 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35416 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35417 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35418 OMP_CLAUSE_ORDERED
);
35419 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35421 error_at (OMP_CLAUSE_LOCATION (c
),
35422 "%<ordered%> clause with parameter may not be specified "
35423 "on %qs construct", p_name
);
35424 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35428 sb
= begin_omp_structured_block ();
35429 save
= cp_parser_begin_omp_structured_block (parser
);
35431 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35433 cp_parser_end_omp_structured_block (parser
, save
);
35434 add_stmt (finish_omp_structured_block (sb
));
35440 #pragma omp for for-clause[optseq] new-line
35444 #pragma omp for simd for-simd-clause[optseq] new-line
35447 #define OMP_FOR_CLAUSE_MASK \
35448 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35459 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35460 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35463 tree clauses
, sb
, ret
;
35465 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35467 strcat (p_name
, " for");
35468 mask
|= OMP_FOR_CLAUSE_MASK
;
35469 /* parallel for{, simd} disallows nowait clause, but for
35470 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35471 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35472 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35473 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35474 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35475 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35479 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35480 const char *p
= IDENTIFIER_POINTER (id
);
35482 if (strcmp (p
, "simd") == 0)
35484 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35485 if (cclauses
== NULL
)
35486 cclauses
= cclauses_buf
;
35488 cp_lexer_consume_token (parser
->lexer
);
35489 if (!flag_openmp
) /* flag_openmp_simd */
35490 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35492 sb
= begin_omp_structured_block ();
35493 save
= cp_parser_begin_omp_structured_block (parser
);
35494 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35496 cp_parser_end_omp_structured_block (parser
, save
);
35497 tree body
= finish_omp_structured_block (sb
);
35500 ret
= make_node (OMP_FOR
);
35501 TREE_TYPE (ret
) = void_type_node
;
35502 OMP_FOR_BODY (ret
) = body
;
35503 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35504 SET_EXPR_LOCATION (ret
, loc
);
35509 if (!flag_openmp
) /* flag_openmp_simd */
35511 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35515 /* Composite distribute parallel for disallows linear clause. */
35516 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35517 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35519 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35523 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35524 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35527 sb
= begin_omp_structured_block ();
35528 save
= cp_parser_begin_omp_structured_block (parser
);
35530 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35532 cp_parser_end_omp_structured_block (parser
, save
);
35533 add_stmt (finish_omp_structured_block (sb
));
35539 # pragma omp master new-line
35540 structured-block */
35543 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35545 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35546 return c_finish_omp_master (input_location
,
35547 cp_parser_omp_structured_block (parser
, if_p
));
35551 # pragma omp ordered new-line
35555 # pragma omp ordered ordered-clauses new-line
35556 structured-block */
35558 #define OMP_ORDERED_CLAUSE_MASK \
35559 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35562 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35563 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35566 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35567 enum pragma_context context
, bool *if_p
)
35569 location_t loc
= pragma_tok
->location
;
35571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35573 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35574 const char *p
= IDENTIFIER_POINTER (id
);
35576 if (strcmp (p
, "depend") == 0)
35578 if (!flag_openmp
) /* flag_openmp_simd */
35580 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35583 if (context
== pragma_stmt
)
35585 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35586 "%<depend%> clause may only be used in compound "
35588 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35592 = cp_parser_omp_all_clauses (parser
,
35593 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35594 "#pragma omp ordered", pragma_tok
);
35595 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35601 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35602 "#pragma omp ordered", pragma_tok
);
35604 if (!flag_openmp
/* flag_openmp_simd */
35605 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35608 c_finish_omp_ordered (loc
, clauses
,
35609 cp_parser_omp_structured_block (parser
, if_p
));
35616 { section-sequence }
35619 section-directive[opt] structured-block
35620 section-sequence section-directive structured-block */
35623 cp_parser_omp_sections_scope (cp_parser
*parser
)
35625 tree stmt
, substmt
;
35626 bool error_suppress
= false;
35629 matching_braces braces
;
35630 if (!braces
.require_open (parser
))
35633 stmt
= push_stmt_list ();
35635 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35636 != PRAGMA_OMP_SECTION
)
35638 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35639 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35640 add_stmt (substmt
);
35645 tok
= cp_lexer_peek_token (parser
->lexer
);
35646 if (tok
->type
== CPP_CLOSE_BRACE
)
35648 if (tok
->type
== CPP_EOF
)
35651 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35653 cp_lexer_consume_token (parser
->lexer
);
35654 cp_parser_require_pragma_eol (parser
, tok
);
35655 error_suppress
= false;
35657 else if (!error_suppress
)
35659 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35660 error_suppress
= true;
35663 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35664 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35665 add_stmt (substmt
);
35667 braces
.require_close (parser
);
35669 substmt
= pop_stmt_list (stmt
);
35671 stmt
= make_node (OMP_SECTIONS
);
35672 TREE_TYPE (stmt
) = void_type_node
;
35673 OMP_SECTIONS_BODY (stmt
) = substmt
;
35680 # pragma omp sections sections-clause[optseq] newline
35683 #define OMP_SECTIONS_CLAUSE_MASK \
35684 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35691 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35692 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35695 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35697 strcat (p_name
, " sections");
35698 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35700 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35702 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35706 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35707 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35710 ret
= cp_parser_omp_sections_scope (parser
);
35712 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35718 # pragma omp parallel parallel-clause[optseq] new-line
35720 # pragma omp parallel for parallel-for-clause[optseq] new-line
35722 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35726 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35727 structured-block */
35729 #define OMP_PARALLEL_CLAUSE_MASK \
35730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35741 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35742 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35745 tree stmt
, clauses
, block
;
35747 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35749 strcat (p_name
, " parallel");
35750 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35751 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35752 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35753 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35754 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35756 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35758 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35759 if (cclauses
== NULL
)
35760 cclauses
= cclauses_buf
;
35762 cp_lexer_consume_token (parser
->lexer
);
35763 if (!flag_openmp
) /* flag_openmp_simd */
35764 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35766 block
= begin_omp_parallel ();
35767 save
= cp_parser_begin_omp_structured_block (parser
);
35768 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35770 cp_parser_end_omp_structured_block (parser
, save
);
35771 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35773 if (ret
== NULL_TREE
)
35775 OMP_PARALLEL_COMBINED (stmt
) = 1;
35778 /* When combined with distribute, parallel has to be followed by for.
35779 #pragma omp target parallel is allowed though. */
35781 && (mask
& (OMP_CLAUSE_MASK_1
35782 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35784 error_at (loc
, "expected %<for%> after %qs", p_name
);
35785 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35788 else if (!flag_openmp
) /* flag_openmp_simd */
35790 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35793 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35795 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35796 const char *p
= IDENTIFIER_POINTER (id
);
35797 if (strcmp (p
, "sections") == 0)
35799 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35800 cclauses
= cclauses_buf
;
35802 cp_lexer_consume_token (parser
->lexer
);
35803 block
= begin_omp_parallel ();
35804 save
= cp_parser_begin_omp_structured_block (parser
);
35805 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35806 cp_parser_end_omp_structured_block (parser
, save
);
35807 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35809 OMP_PARALLEL_COMBINED (stmt
) = 1;
35814 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35818 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35819 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35822 block
= begin_omp_parallel ();
35823 save
= cp_parser_begin_omp_structured_block (parser
);
35824 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35825 cp_parser_end_omp_structured_block (parser
, save
);
35826 stmt
= finish_omp_parallel (clauses
, block
);
35831 # pragma omp single single-clause[optseq] new-line
35832 structured-block */
35834 #define OMP_SINGLE_CLAUSE_MASK \
35835 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35841 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35843 tree stmt
= make_node (OMP_SINGLE
);
35844 TREE_TYPE (stmt
) = void_type_node
;
35846 OMP_SINGLE_CLAUSES (stmt
)
35847 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35848 "#pragma omp single", pragma_tok
);
35849 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35851 return add_stmt (stmt
);
35855 # pragma omp task task-clause[optseq] new-line
35856 structured-block */
35858 #define OMP_TASK_CLAUSE_MASK \
35859 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35871 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35873 tree clauses
, block
;
35876 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35877 "#pragma omp task", pragma_tok
);
35878 block
= begin_omp_task ();
35879 save
= cp_parser_begin_omp_structured_block (parser
);
35880 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35881 cp_parser_end_omp_structured_block (parser
, save
);
35882 return finish_omp_task (clauses
, block
);
35886 # pragma omp taskwait new-line */
35889 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35891 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35892 finish_omp_taskwait ();
35896 # pragma omp taskyield new-line */
35899 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35901 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35902 finish_omp_taskyield ();
35906 # pragma omp taskgroup new-line
35907 structured-block */
35910 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35912 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35913 return c_finish_omp_taskgroup (input_location
,
35914 cp_parser_omp_structured_block (parser
,
35920 # pragma omp threadprivate (variable-list) */
35923 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35927 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35928 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35930 finish_omp_threadprivate (vars
);
35934 # pragma omp cancel cancel-clause[optseq] new-line */
35936 #define OMP_CANCEL_CLAUSE_MASK \
35937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35944 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35946 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35947 "#pragma omp cancel", pragma_tok
);
35948 finish_omp_cancel (clauses
);
35952 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35954 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35961 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35962 enum pragma_context context
)
35965 bool point_seen
= false;
35967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35969 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35970 const char *p
= IDENTIFIER_POINTER (id
);
35972 if (strcmp (p
, "point") == 0)
35974 cp_lexer_consume_token (parser
->lexer
);
35980 cp_parser_error (parser
, "expected %<point%>");
35981 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35985 if (context
!= pragma_compound
)
35987 if (context
== pragma_stmt
)
35988 error_at (pragma_tok
->location
,
35989 "%<#pragma %s%> may only be used in compound statements",
35990 "omp cancellation point");
35992 cp_parser_error (parser
, "expected declaration specifiers");
35993 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35997 clauses
= cp_parser_omp_all_clauses (parser
,
35998 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35999 "#pragma omp cancellation point",
36001 finish_omp_cancellation_point (clauses
);
36005 #pragma omp distribute distribute-clause[optseq] new-line
36008 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36016 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36017 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36020 tree clauses
, sb
, ret
;
36022 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36024 strcat (p_name
, " distribute");
36025 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36029 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36030 const char *p
= IDENTIFIER_POINTER (id
);
36032 bool parallel
= false;
36034 if (strcmp (p
, "simd") == 0)
36037 parallel
= strcmp (p
, "parallel") == 0;
36038 if (parallel
|| simd
)
36040 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36041 if (cclauses
== NULL
)
36042 cclauses
= cclauses_buf
;
36043 cp_lexer_consume_token (parser
->lexer
);
36044 if (!flag_openmp
) /* flag_openmp_simd */
36047 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36050 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36053 sb
= begin_omp_structured_block ();
36054 save
= cp_parser_begin_omp_structured_block (parser
);
36056 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36059 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36061 cp_parser_end_omp_structured_block (parser
, save
);
36062 tree body
= finish_omp_structured_block (sb
);
36065 ret
= make_node (OMP_DISTRIBUTE
);
36066 TREE_TYPE (ret
) = void_type_node
;
36067 OMP_FOR_BODY (ret
) = body
;
36068 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36069 SET_EXPR_LOCATION (ret
, loc
);
36074 if (!flag_openmp
) /* flag_openmp_simd */
36076 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36080 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36084 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36085 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36088 sb
= begin_omp_structured_block ();
36089 save
= cp_parser_begin_omp_structured_block (parser
);
36091 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36093 cp_parser_end_omp_structured_block (parser
, save
);
36094 add_stmt (finish_omp_structured_block (sb
));
36100 # pragma omp teams teams-clause[optseq] new-line
36101 structured-block */
36103 #define OMP_TEAMS_CLAUSE_MASK \
36104 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36113 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36114 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36117 tree clauses
, sb
, ret
;
36119 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36121 strcat (p_name
, " teams");
36122 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36126 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36127 const char *p
= IDENTIFIER_POINTER (id
);
36128 if (strcmp (p
, "distribute") == 0)
36130 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36131 if (cclauses
== NULL
)
36132 cclauses
= cclauses_buf
;
36134 cp_lexer_consume_token (parser
->lexer
);
36135 if (!flag_openmp
) /* flag_openmp_simd */
36136 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36138 sb
= begin_omp_structured_block ();
36139 save
= cp_parser_begin_omp_structured_block (parser
);
36140 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36142 cp_parser_end_omp_structured_block (parser
, save
);
36143 tree body
= finish_omp_structured_block (sb
);
36146 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36147 ret
= make_node (OMP_TEAMS
);
36148 TREE_TYPE (ret
) = void_type_node
;
36149 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36150 OMP_TEAMS_BODY (ret
) = body
;
36151 OMP_TEAMS_COMBINED (ret
) = 1;
36152 SET_EXPR_LOCATION (ret
, loc
);
36153 return add_stmt (ret
);
36156 if (!flag_openmp
) /* flag_openmp_simd */
36158 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36162 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36166 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36167 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36170 tree stmt
= make_node (OMP_TEAMS
);
36171 TREE_TYPE (stmt
) = void_type_node
;
36172 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36173 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36174 SET_EXPR_LOCATION (stmt
, loc
);
36176 return add_stmt (stmt
);
36180 # pragma omp target data target-data-clause[optseq] new-line
36181 structured-block */
36183 #define OMP_TARGET_DATA_CLAUSE_MASK \
36184 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36190 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36193 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36194 "#pragma omp target data", pragma_tok
);
36196 for (tree
*pc
= &clauses
; *pc
;)
36198 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36199 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36202 case GOMP_MAP_ALWAYS_TO
:
36203 case GOMP_MAP_FROM
:
36204 case GOMP_MAP_ALWAYS_FROM
:
36205 case GOMP_MAP_TOFROM
:
36206 case GOMP_MAP_ALWAYS_TOFROM
:
36207 case GOMP_MAP_ALLOC
:
36210 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36211 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36212 case GOMP_MAP_ALWAYS_POINTER
:
36216 error_at (OMP_CLAUSE_LOCATION (*pc
),
36217 "%<#pragma omp target data%> with map-type other "
36218 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36219 "on %<map%> clause");
36220 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36223 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36229 error_at (pragma_tok
->location
,
36230 "%<#pragma omp target data%> must contain at least "
36231 "one %<map%> clause");
36235 tree stmt
= make_node (OMP_TARGET_DATA
);
36236 TREE_TYPE (stmt
) = void_type_node
;
36237 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36239 keep_next_level (true);
36240 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36242 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36243 return add_stmt (stmt
);
36247 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36248 structured-block */
36250 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36251 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36258 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36259 enum pragma_context context
)
36261 bool data_seen
= false;
36262 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36264 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36265 const char *p
= IDENTIFIER_POINTER (id
);
36267 if (strcmp (p
, "data") == 0)
36269 cp_lexer_consume_token (parser
->lexer
);
36275 cp_parser_error (parser
, "expected %<data%>");
36276 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36280 if (context
== pragma_stmt
)
36282 error_at (pragma_tok
->location
,
36283 "%<#pragma %s%> may only be used in compound statements",
36284 "omp target enter data");
36285 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36290 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36291 "#pragma omp target enter data", pragma_tok
);
36293 for (tree
*pc
= &clauses
; *pc
;)
36295 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36296 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36299 case GOMP_MAP_ALWAYS_TO
:
36300 case GOMP_MAP_ALLOC
:
36303 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36304 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36305 case GOMP_MAP_ALWAYS_POINTER
:
36309 error_at (OMP_CLAUSE_LOCATION (*pc
),
36310 "%<#pragma omp target enter data%> with map-type other "
36311 "than %<to%> or %<alloc%> on %<map%> clause");
36312 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36315 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36321 error_at (pragma_tok
->location
,
36322 "%<#pragma omp target enter data%> must contain at least "
36323 "one %<map%> clause");
36327 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36328 TREE_TYPE (stmt
) = void_type_node
;
36329 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36330 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36331 return add_stmt (stmt
);
36335 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36336 structured-block */
36338 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36346 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36347 enum pragma_context context
)
36349 bool data_seen
= false;
36350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36352 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36353 const char *p
= IDENTIFIER_POINTER (id
);
36355 if (strcmp (p
, "data") == 0)
36357 cp_lexer_consume_token (parser
->lexer
);
36363 cp_parser_error (parser
, "expected %<data%>");
36364 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36368 if (context
== pragma_stmt
)
36370 error_at (pragma_tok
->location
,
36371 "%<#pragma %s%> may only be used in compound statements",
36372 "omp target exit data");
36373 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36378 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36379 "#pragma omp target exit data", pragma_tok
);
36381 for (tree
*pc
= &clauses
; *pc
;)
36383 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36384 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36386 case GOMP_MAP_FROM
:
36387 case GOMP_MAP_ALWAYS_FROM
:
36388 case GOMP_MAP_RELEASE
:
36389 case GOMP_MAP_DELETE
:
36392 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36393 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36394 case GOMP_MAP_ALWAYS_POINTER
:
36398 error_at (OMP_CLAUSE_LOCATION (*pc
),
36399 "%<#pragma omp target exit data%> with map-type other "
36400 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36402 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36405 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36411 error_at (pragma_tok
->location
,
36412 "%<#pragma omp target exit data%> must contain at least "
36413 "one %<map%> clause");
36417 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36418 TREE_TYPE (stmt
) = void_type_node
;
36419 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36420 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36421 return add_stmt (stmt
);
36425 # pragma omp target update target-update-clause[optseq] new-line */
36427 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
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))
36436 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36437 enum pragma_context context
)
36439 if (context
== pragma_stmt
)
36441 error_at (pragma_tok
->location
,
36442 "%<#pragma %s%> may only be used in compound statements",
36443 "omp target update");
36444 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36449 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36450 "#pragma omp target update", pragma_tok
);
36451 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36452 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36454 error_at (pragma_tok
->location
,
36455 "%<#pragma omp target update%> must contain at least one "
36456 "%<from%> or %<to%> clauses");
36460 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36461 TREE_TYPE (stmt
) = void_type_node
;
36462 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36463 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36469 # pragma omp target target-clause[optseq] new-line
36470 structured-block */
36472 #define OMP_TARGET_CLAUSE_MASK \
36473 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36484 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36485 enum pragma_context context
, bool *if_p
)
36487 tree
*pc
= NULL
, stmt
;
36489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36491 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36492 const char *p
= IDENTIFIER_POINTER (id
);
36493 enum tree_code ccode
= ERROR_MARK
;
36495 if (strcmp (p
, "teams") == 0)
36497 else if (strcmp (p
, "parallel") == 0)
36498 ccode
= OMP_PARALLEL
;
36499 else if (strcmp (p
, "simd") == 0)
36501 if (ccode
!= ERROR_MARK
)
36503 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36504 char p_name
[sizeof ("#pragma omp target teams distribute "
36505 "parallel for simd")];
36507 cp_lexer_consume_token (parser
->lexer
);
36508 strcpy (p_name
, "#pragma omp target");
36509 if (!flag_openmp
) /* flag_openmp_simd */
36515 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36516 OMP_TARGET_CLAUSE_MASK
,
36520 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36521 OMP_TARGET_CLAUSE_MASK
,
36525 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36526 OMP_TARGET_CLAUSE_MASK
,
36530 gcc_unreachable ();
36532 return stmt
!= NULL_TREE
;
36534 keep_next_level (true);
36535 tree sb
= begin_omp_structured_block (), ret
;
36536 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36540 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36541 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36545 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36546 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36550 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36551 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36555 gcc_unreachable ();
36557 cp_parser_end_omp_structured_block (parser
, save
);
36558 tree body
= finish_omp_structured_block (sb
);
36559 if (ret
== NULL_TREE
)
36561 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36563 /* For combined target teams, ensure the num_teams and
36564 thread_limit clause expressions are evaluated on the host,
36565 before entering the target construct. */
36567 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36568 c
; c
= OMP_CLAUSE_CHAIN (c
))
36569 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36570 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36571 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36573 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36574 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36575 if (expr
== error_mark_node
)
36577 tree tmp
= TARGET_EXPR_SLOT (expr
);
36579 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36580 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36581 OMP_CLAUSE_FIRSTPRIVATE
);
36582 OMP_CLAUSE_DECL (tc
) = tmp
;
36583 OMP_CLAUSE_CHAIN (tc
)
36584 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36585 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36588 tree stmt
= make_node (OMP_TARGET
);
36589 TREE_TYPE (stmt
) = void_type_node
;
36590 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36591 OMP_TARGET_BODY (stmt
) = body
;
36592 OMP_TARGET_COMBINED (stmt
) = 1;
36593 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36595 pc
= &OMP_TARGET_CLAUSES (stmt
);
36596 goto check_clauses
;
36598 else if (!flag_openmp
) /* flag_openmp_simd */
36600 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36603 else if (strcmp (p
, "data") == 0)
36605 cp_lexer_consume_token (parser
->lexer
);
36606 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36609 else if (strcmp (p
, "enter") == 0)
36611 cp_lexer_consume_token (parser
->lexer
);
36612 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36615 else if (strcmp (p
, "exit") == 0)
36617 cp_lexer_consume_token (parser
->lexer
);
36618 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36621 else if (strcmp (p
, "update") == 0)
36623 cp_lexer_consume_token (parser
->lexer
);
36624 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36627 if (!flag_openmp
) /* flag_openmp_simd */
36629 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36633 stmt
= make_node (OMP_TARGET
);
36634 TREE_TYPE (stmt
) = void_type_node
;
36636 OMP_TARGET_CLAUSES (stmt
)
36637 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36638 "#pragma omp target", pragma_tok
);
36639 pc
= &OMP_TARGET_CLAUSES (stmt
);
36640 keep_next_level (true);
36641 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36643 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36649 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36650 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36653 case GOMP_MAP_ALWAYS_TO
:
36654 case GOMP_MAP_FROM
:
36655 case GOMP_MAP_ALWAYS_FROM
:
36656 case GOMP_MAP_TOFROM
:
36657 case GOMP_MAP_ALWAYS_TOFROM
:
36658 case GOMP_MAP_ALLOC
:
36659 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36660 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36661 case GOMP_MAP_ALWAYS_POINTER
:
36664 error_at (OMP_CLAUSE_LOCATION (*pc
),
36665 "%<#pragma omp target%> with map-type other "
36666 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36667 "on %<map%> clause");
36668 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36671 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36677 # pragma acc cache (variable-list) new-line
36681 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36683 tree stmt
, clauses
;
36685 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36686 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36688 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36690 stmt
= make_node (OACC_CACHE
);
36691 TREE_TYPE (stmt
) = void_type_node
;
36692 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36693 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36700 # pragma acc data oacc-data-clause[optseq] new-line
36701 structured-block */
36703 #define OACC_DATA_CLAUSE_MASK \
36704 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36717 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36719 tree stmt
, clauses
, block
;
36722 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36723 "#pragma acc data", pragma_tok
);
36725 block
= begin_omp_parallel ();
36726 save
= cp_parser_begin_omp_structured_block (parser
);
36727 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36728 cp_parser_end_omp_structured_block (parser
, save
);
36729 stmt
= finish_oacc_data (clauses
, block
);
36734 # pragma acc host_data <clauses> new-line
36735 structured-block */
36737 #define OACC_HOST_DATA_CLAUSE_MASK \
36738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36741 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36743 tree stmt
, clauses
, block
;
36746 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36747 "#pragma acc host_data", pragma_tok
);
36749 block
= begin_omp_parallel ();
36750 save
= cp_parser_begin_omp_structured_block (parser
);
36751 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36752 cp_parser_end_omp_structured_block (parser
, save
);
36753 stmt
= finish_oacc_host_data (clauses
, block
);
36758 # pragma acc declare oacc-data-clause[optseq] new-line
36761 #define OACC_DECLARE_CLAUSE_MASK \
36762 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36776 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36778 tree clauses
, stmt
;
36779 bool error
= false;
36781 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36782 "#pragma acc declare", pragma_tok
, true);
36785 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36787 error_at (pragma_tok
->location
,
36788 "no valid clauses specified in %<#pragma acc declare%>");
36792 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36794 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36795 tree decl
= OMP_CLAUSE_DECL (t
);
36796 if (!DECL_P (decl
))
36798 error_at (loc
, "array section in %<#pragma acc declare%>");
36802 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36803 switch (OMP_CLAUSE_MAP_KIND (t
))
36805 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36806 case GOMP_MAP_FORCE_ALLOC
:
36807 case GOMP_MAP_FORCE_TO
:
36808 case GOMP_MAP_FORCE_DEVICEPTR
:
36809 case GOMP_MAP_DEVICE_RESIDENT
:
36812 case GOMP_MAP_LINK
:
36813 if (!global_bindings_p ()
36814 && (TREE_STATIC (decl
)
36815 || !DECL_EXTERNAL (decl
)))
36818 "%qD must be a global variable in "
36819 "%<#pragma acc declare link%>",
36827 if (global_bindings_p ())
36829 error_at (loc
, "invalid OpenACC clause at file scope");
36833 if (DECL_EXTERNAL (decl
))
36836 "invalid use of %<extern%> variable %qD "
36837 "in %<#pragma acc declare%>", decl
);
36841 else if (TREE_PUBLIC (decl
))
36844 "invalid use of %<global%> variable %qD "
36845 "in %<#pragma acc declare%>", decl
);
36852 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36853 || lookup_attribute ("omp declare target link",
36854 DECL_ATTRIBUTES (decl
)))
36856 error_at (loc
, "variable %qD used more than once with "
36857 "%<#pragma acc declare%>", decl
);
36866 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36867 id
= get_identifier ("omp declare target link");
36869 id
= get_identifier ("omp declare target");
36871 DECL_ATTRIBUTES (decl
)
36872 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36873 if (global_bindings_p ())
36875 symtab_node
*node
= symtab_node::get (decl
);
36878 node
->offloadable
= 1;
36879 if (ENABLE_OFFLOADING
)
36881 g
->have_offload
= true;
36882 if (is_a
<varpool_node
*> (node
))
36883 vec_safe_push (offload_vars
, decl
);
36890 if (error
|| global_bindings_p ())
36893 stmt
= make_node (OACC_DECLARE
);
36894 TREE_TYPE (stmt
) = void_type_node
;
36895 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36896 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36904 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36908 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36910 LOC is the location of the #pragma token.
36913 #define OACC_ENTER_DATA_CLAUSE_MASK \
36914 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36922 #define OACC_EXIT_DATA_CLAUSE_MASK \
36923 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36930 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36933 location_t loc
= pragma_tok
->location
;
36934 tree stmt
, clauses
;
36935 const char *p
= "";
36937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36938 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36940 if (strcmp (p
, "data") != 0)
36942 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36943 enter
? "enter" : "exit");
36944 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36948 cp_lexer_consume_token (parser
->lexer
);
36951 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36952 "#pragma acc enter data", pragma_tok
);
36954 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36955 "#pragma acc exit data", pragma_tok
);
36957 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36959 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36960 enter
? "enter" : "exit");
36964 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36965 TREE_TYPE (stmt
) = void_type_node
;
36966 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36967 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36973 # pragma acc loop oacc-loop-clause[optseq] new-line
36974 structured-block */
36976 #define OACC_LOOP_CLAUSE_MASK \
36977 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36989 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36990 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36992 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36994 strcat (p_name
, " loop");
36995 mask
|= OACC_LOOP_CLAUSE_MASK
;
36997 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37001 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37003 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37005 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37008 tree block
= begin_omp_structured_block ();
37009 int save
= cp_parser_begin_omp_structured_block (parser
);
37010 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37011 cp_parser_end_omp_structured_block (parser
, save
);
37012 add_stmt (finish_omp_structured_block (block
));
37018 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37023 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37027 #define OACC_KERNELS_CLAUSE_MASK \
37028 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37046 #define OACC_PARALLEL_CLAUSE_MASK \
37047 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37069 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37070 char *p_name
, bool *if_p
)
37072 omp_clause_mask mask
;
37073 enum tree_code code
;
37074 switch (cp_parser_pragma_kind (pragma_tok
))
37076 case PRAGMA_OACC_KERNELS
:
37077 strcat (p_name
, " kernels");
37078 mask
= OACC_KERNELS_CLAUSE_MASK
;
37079 code
= OACC_KERNELS
;
37081 case PRAGMA_OACC_PARALLEL
:
37082 strcat (p_name
, " parallel");
37083 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37084 code
= OACC_PARALLEL
;
37087 gcc_unreachable ();
37090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37093 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37094 if (strcmp (p
, "loop") == 0)
37096 cp_lexer_consume_token (parser
->lexer
);
37097 tree block
= begin_omp_parallel ();
37099 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37101 return finish_omp_construct (code
, block
, clauses
);
37105 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37107 tree block
= begin_omp_parallel ();
37108 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37109 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37110 cp_parser_end_omp_structured_block (parser
, save
);
37111 return finish_omp_construct (code
, block
, clauses
);
37115 # pragma acc update oacc-update-clause[optseq] new-line
37118 #define OACC_UPDATE_CLAUSE_MASK \
37119 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37127 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37129 tree stmt
, clauses
;
37131 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37132 "#pragma acc update", pragma_tok
);
37134 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37136 error_at (pragma_tok
->location
,
37137 "%<#pragma acc update%> must contain at least one "
37138 "%<device%> or %<host%> or %<self%> clause");
37142 stmt
= make_node (OACC_UPDATE
);
37143 TREE_TYPE (stmt
) = void_type_node
;
37144 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37145 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37151 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37153 LOC is the location of the #pragma token.
37156 #define OACC_WAIT_CLAUSE_MASK \
37157 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37160 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37162 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37163 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37165 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37166 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37168 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37169 "#pragma acc wait", pragma_tok
);
37171 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37172 stmt
= finish_expr_stmt (stmt
);
37178 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37180 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37181 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37189 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37190 enum pragma_context context
)
37192 bool first_p
= parser
->omp_declare_simd
== NULL
;
37193 cp_omp_declare_simd_data data
;
37196 data
.error_seen
= false;
37197 data
.fndecl_seen
= false;
37198 data
.tokens
= vNULL
;
37199 data
.clauses
= NULL_TREE
;
37200 /* It is safe to take the address of a local variable; it will only be
37201 used while this scope is live. */
37202 parser
->omp_declare_simd
= &data
;
37205 /* Store away all pragma tokens. */
37206 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37207 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37208 cp_lexer_consume_token (parser
->lexer
);
37209 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37210 parser
->omp_declare_simd
->error_seen
= true;
37211 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37212 struct cp_token_cache
*cp
37213 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37214 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37218 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37219 cp_parser_pragma (parser
, context
, NULL
);
37222 case pragma_external
:
37223 cp_parser_declaration (parser
);
37225 case pragma_member
:
37226 cp_parser_member_declaration (parser
);
37228 case pragma_objc_icode
:
37229 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37232 cp_parser_declaration_statement (parser
);
37235 if (parser
->omp_declare_simd
37236 && !parser
->omp_declare_simd
->error_seen
37237 && !parser
->omp_declare_simd
->fndecl_seen
)
37238 error_at (pragma_tok
->location
,
37239 "%<#pragma omp declare simd%> not immediately followed by "
37240 "function declaration or definition");
37241 data
.tokens
.release ();
37242 parser
->omp_declare_simd
= NULL
;
37246 /* Finalize #pragma omp declare simd clauses after direct declarator has
37247 been parsed, and put that into "omp declare simd" attribute. */
37250 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37252 struct cp_token_cache
*ce
;
37253 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37256 if (!data
->error_seen
&& data
->fndecl_seen
)
37258 error ("%<#pragma omp declare simd%> not immediately followed by "
37259 "a single function declaration or definition");
37260 data
->error_seen
= true;
37262 if (data
->error_seen
)
37265 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37269 cp_parser_push_lexer_for_tokens (parser
, ce
);
37270 parser
->lexer
->in_pragma
= true;
37271 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37272 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37273 cp_lexer_consume_token (parser
->lexer
);
37274 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37275 "#pragma omp declare simd", pragma_tok
);
37276 cp_parser_pop_lexer (parser
);
37278 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37279 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37280 TREE_CHAIN (c
) = attrs
;
37281 if (processing_template_decl
)
37282 ATTR_IS_DEPENDENT (c
) = 1;
37286 data
->fndecl_seen
= true;
37292 # pragma omp declare target new-line
37293 declarations and definitions
37294 # pragma omp end declare target new-line
37297 # pragma omp declare target ( extended-list ) new-line
37299 # pragma omp declare target declare-target-clauses[seq] new-line */
37301 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37302 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37306 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37308 tree clauses
= NULL_TREE
;
37309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37311 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37312 "#pragma omp declare target", pragma_tok
);
37313 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37315 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37317 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37318 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37322 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37323 scope_chain
->omp_declare_target_attribute
++;
37326 if (scope_chain
->omp_declare_target_attribute
)
37327 error_at (pragma_tok
->location
,
37328 "%<#pragma omp declare target%> with clauses in between "
37329 "%<#pragma omp declare target%> without clauses and "
37330 "%<#pragma omp end declare target%>");
37331 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37333 tree t
= OMP_CLAUSE_DECL (c
), id
;
37334 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37335 tree at2
= lookup_attribute ("omp declare target link",
37336 DECL_ATTRIBUTES (t
));
37337 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37339 id
= get_identifier ("omp declare target link");
37340 std::swap (at1
, at2
);
37343 id
= get_identifier ("omp declare target");
37346 error_at (OMP_CLAUSE_LOCATION (c
),
37347 "%qD specified both in declare target %<link%> and %<to%>"
37353 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37354 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37357 symtab_node
*node
= symtab_node::get (t
);
37360 node
->offloadable
= 1;
37361 if (ENABLE_OFFLOADING
)
37363 g
->have_offload
= true;
37364 if (is_a
<varpool_node
*> (node
))
37365 vec_safe_push (offload_vars
, t
);
37373 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37375 const char *p
= "";
37376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37378 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37379 p
= IDENTIFIER_POINTER (id
);
37381 if (strcmp (p
, "declare") == 0)
37383 cp_lexer_consume_token (parser
->lexer
);
37385 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37387 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37388 p
= IDENTIFIER_POINTER (id
);
37390 if (strcmp (p
, "target") == 0)
37391 cp_lexer_consume_token (parser
->lexer
);
37394 cp_parser_error (parser
, "expected %<target%>");
37395 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37401 cp_parser_error (parser
, "expected %<declare%>");
37402 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37405 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37406 if (!scope_chain
->omp_declare_target_attribute
)
37407 error_at (pragma_tok
->location
,
37408 "%<#pragma omp end declare target%> without corresponding "
37409 "%<#pragma omp declare target%>");
37411 scope_chain
->omp_declare_target_attribute
--;
37414 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37415 expression and optional initializer clause of
37416 #pragma omp declare reduction. We store the expression(s) as
37417 either 3, 6 or 7 special statements inside of the artificial function's
37418 body. The first two statements are DECL_EXPRs for the artificial
37419 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37420 expression that uses those variables.
37421 If there was any INITIALIZER clause, this is followed by further statements,
37422 the fourth and fifth statements are DECL_EXPRs for the artificial
37423 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37424 constructor variant (first token after open paren is not omp_priv),
37425 then the sixth statement is a statement with the function call expression
37426 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37427 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37428 to initialize the OMP_PRIV artificial variable and there is seventh
37429 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37432 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37434 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37435 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37436 type
= TREE_TYPE (type
);
37437 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37438 DECL_ARTIFICIAL (omp_out
) = 1;
37439 pushdecl (omp_out
);
37440 add_decl_expr (omp_out
);
37441 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37442 DECL_ARTIFICIAL (omp_in
) = 1;
37444 add_decl_expr (omp_in
);
37446 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37448 keep_next_level (true);
37449 tree block
= begin_omp_structured_block ();
37450 combiner
= cp_parser_expression (parser
);
37451 finish_expr_stmt (combiner
);
37452 block
= finish_omp_structured_block (block
);
37455 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37458 const char *p
= "";
37459 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37461 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37462 p
= IDENTIFIER_POINTER (id
);
37465 if (strcmp (p
, "initializer") == 0)
37467 cp_lexer_consume_token (parser
->lexer
);
37468 matching_parens parens
;
37469 if (!parens
.require_open (parser
))
37473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37475 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37476 p
= IDENTIFIER_POINTER (id
);
37479 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37480 DECL_ARTIFICIAL (omp_priv
) = 1;
37481 pushdecl (omp_priv
);
37482 add_decl_expr (omp_priv
);
37483 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37484 DECL_ARTIFICIAL (omp_orig
) = 1;
37485 pushdecl (omp_orig
);
37486 add_decl_expr (omp_orig
);
37488 keep_next_level (true);
37489 block
= begin_omp_structured_block ();
37492 if (strcmp (p
, "omp_priv") == 0)
37494 bool is_direct_init
, is_non_constant_init
;
37496 cp_lexer_consume_token (parser
->lexer
);
37497 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37499 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37500 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37502 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37503 == CPP_CLOSE_PAREN
))
37505 finish_omp_structured_block (block
);
37506 error ("invalid initializer clause");
37509 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37510 &is_non_constant_init
);
37511 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37512 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37516 cp_parser_parse_tentatively (parser
);
37517 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37518 /*check_dependency_p=*/true,
37519 /*template_p=*/NULL
,
37520 /*declarator_p=*/false,
37521 /*optional_p=*/false);
37522 vec
<tree
, va_gc
> *args
;
37523 if (fn_name
== error_mark_node
37524 || cp_parser_error_occurred (parser
)
37525 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37526 || ((args
= cp_parser_parenthesized_expression_list
37527 (parser
, non_attr
, /*cast_p=*/false,
37528 /*allow_expansion_p=*/true,
37529 /*non_constant_p=*/NULL
)),
37530 cp_parser_error_occurred (parser
)))
37532 finish_omp_structured_block (block
);
37533 cp_parser_abort_tentative_parse (parser
);
37534 cp_parser_error (parser
, "expected id-expression (arguments)");
37539 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37540 if (arg
== omp_priv
37541 || (TREE_CODE (arg
) == ADDR_EXPR
37542 && TREE_OPERAND (arg
, 0) == omp_priv
))
37544 cp_parser_abort_tentative_parse (parser
);
37545 if (arg
== NULL_TREE
)
37546 error ("one of the initializer call arguments should be %<omp_priv%>"
37547 " or %<&omp_priv%>");
37548 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37550 finish_expr_stmt (initializer
);
37553 block
= finish_omp_structured_block (block
);
37554 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37558 add_decl_expr (omp_orig
);
37560 if (!parens
.require_close (parser
))
37564 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37565 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37572 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37573 initializer-clause[opt] new-line
37575 initializer-clause:
37576 initializer (omp_priv initializer)
37577 initializer (function-name (argument-list)) */
37580 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37581 enum pragma_context
)
37583 auto_vec
<tree
> types
;
37584 enum tree_code reduc_code
= ERROR_MARK
;
37585 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37587 cp_token
*first_token
;
37588 cp_token_cache
*cp
;
37592 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37593 p
= obstack_alloc (&declarator_obstack
, 0);
37595 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37598 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37601 reduc_code
= PLUS_EXPR
;
37604 reduc_code
= MULT_EXPR
;
37607 reduc_code
= MINUS_EXPR
;
37610 reduc_code
= BIT_AND_EXPR
;
37613 reduc_code
= BIT_XOR_EXPR
;
37616 reduc_code
= BIT_IOR_EXPR
;
37619 reduc_code
= TRUTH_ANDIF_EXPR
;
37622 reduc_code
= TRUTH_ORIF_EXPR
;
37625 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37628 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37629 "%<|%>, %<&&%>, %<||%> or identifier");
37633 if (reduc_code
!= ERROR_MARK
)
37634 cp_lexer_consume_token (parser
->lexer
);
37636 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37637 if (reduc_id
== error_mark_node
)
37640 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37643 /* Types may not be defined in declare reduction type list. */
37644 const char *saved_message
;
37645 saved_message
= parser
->type_definition_forbidden_message
;
37646 parser
->type_definition_forbidden_message
37647 = G_("types may not be defined in declare reduction type list");
37648 bool saved_colon_corrects_to_scope_p
;
37649 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37650 parser
->colon_corrects_to_scope_p
= false;
37651 bool saved_colon_doesnt_start_class_def_p
;
37652 saved_colon_doesnt_start_class_def_p
37653 = parser
->colon_doesnt_start_class_def_p
;
37654 parser
->colon_doesnt_start_class_def_p
= true;
37658 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37659 type
= cp_parser_type_id (parser
);
37660 if (type
== error_mark_node
)
37662 else if (ARITHMETIC_TYPE_P (type
)
37663 && (orig_reduc_id
== NULL_TREE
37664 || (TREE_CODE (type
) != COMPLEX_TYPE
37665 && (id_equal (orig_reduc_id
, "min")
37666 || id_equal (orig_reduc_id
, "max")))))
37667 error_at (loc
, "predeclared arithmetic type %qT in "
37668 "%<#pragma omp declare reduction%>", type
);
37669 else if (TREE_CODE (type
) == FUNCTION_TYPE
37670 || TREE_CODE (type
) == METHOD_TYPE
37671 || TREE_CODE (type
) == ARRAY_TYPE
)
37672 error_at (loc
, "function or array type %qT in "
37673 "%<#pragma omp declare reduction%>", type
);
37674 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37675 error_at (loc
, "reference type %qT in "
37676 "%<#pragma omp declare reduction%>", type
);
37677 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37678 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37679 "%<#pragma omp declare reduction%>", type
);
37681 types
.safe_push (type
);
37683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37684 cp_lexer_consume_token (parser
->lexer
);
37689 /* Restore the saved message. */
37690 parser
->type_definition_forbidden_message
= saved_message
;
37691 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37692 parser
->colon_doesnt_start_class_def_p
37693 = saved_colon_doesnt_start_class_def_p
;
37695 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37696 || types
.is_empty ())
37699 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37703 first_token
= cp_lexer_peek_token (parser
->lexer
);
37706 FOR_EACH_VEC_ELT (types
, i
, type
)
37709 = build_function_type_list (void_type_node
,
37710 cp_build_reference_type (type
, false),
37712 tree this_reduc_id
= reduc_id
;
37713 if (!dependent_type_p (type
))
37714 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37715 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37716 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37717 DECL_ARTIFICIAL (fndecl
) = 1;
37718 DECL_EXTERNAL (fndecl
) = 1;
37719 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37720 DECL_IGNORED_P (fndecl
) = 1;
37721 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37722 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37723 DECL_ATTRIBUTES (fndecl
)
37724 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37725 DECL_ATTRIBUTES (fndecl
));
37726 if (processing_template_decl
)
37727 fndecl
= push_template_decl (fndecl
);
37728 bool block_scope
= false;
37729 tree block
= NULL_TREE
;
37730 if (current_function_decl
)
37732 block_scope
= true;
37733 DECL_CONTEXT (fndecl
) = global_namespace
;
37734 if (!processing_template_decl
)
37737 else if (current_class_type
)
37741 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37742 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37743 cp_lexer_consume_token (parser
->lexer
);
37744 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37746 cp
= cp_token_cache_new (first_token
,
37747 cp_lexer_peek_nth_token (parser
->lexer
,
37750 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37751 finish_member_declaration (fndecl
);
37752 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37753 DECL_PENDING_INLINE_P (fndecl
) = 1;
37754 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37759 DECL_CONTEXT (fndecl
) = current_namespace
;
37763 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37765 block
= begin_omp_structured_block ();
37768 cp_parser_push_lexer_for_tokens (parser
, cp
);
37769 parser
->lexer
->in_pragma
= true;
37771 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37774 finish_function (/*inline_p=*/false);
37776 DECL_CONTEXT (fndecl
) = current_function_decl
;
37778 cp_parser_pop_lexer (parser
);
37782 cp_parser_pop_lexer (parser
);
37784 finish_function (/*inline_p=*/false);
37787 DECL_CONTEXT (fndecl
) = current_function_decl
;
37788 block
= finish_omp_structured_block (block
);
37789 if (TREE_CODE (block
) == BIND_EXPR
)
37790 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37791 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37792 DECL_SAVED_TREE (fndecl
) = block
;
37793 if (processing_template_decl
)
37794 add_decl_expr (fndecl
);
37796 cp_check_omp_declare_reduction (fndecl
);
37797 if (cp
== NULL
&& types
.length () > 1)
37798 cp
= cp_token_cache_new (first_token
,
37799 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37800 if (errs
!= errorcount
)
37804 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37807 /* Free any declarators allocated. */
37808 obstack_free (&declarator_obstack
, p
);
37812 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37813 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37814 initializer-clause[opt] new-line
37815 #pragma omp declare target new-line */
37818 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37819 enum pragma_context context
)
37821 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37823 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37824 const char *p
= IDENTIFIER_POINTER (id
);
37826 if (strcmp (p
, "simd") == 0)
37828 cp_lexer_consume_token (parser
->lexer
);
37829 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37833 cp_ensure_no_omp_declare_simd (parser
);
37834 if (strcmp (p
, "reduction") == 0)
37836 cp_lexer_consume_token (parser
->lexer
);
37837 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37841 if (!flag_openmp
) /* flag_openmp_simd */
37843 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37846 if (strcmp (p
, "target") == 0)
37848 cp_lexer_consume_token (parser
->lexer
);
37849 cp_parser_omp_declare_target (parser
, pragma_tok
);
37853 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37855 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37860 #pragma omp taskloop taskloop-clause[optseq] new-line
37863 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37866 #define OMP_TASKLOOP_CLAUSE_MASK \
37867 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37883 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37884 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37887 tree clauses
, sb
, ret
;
37889 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37891 strcat (p_name
, " taskloop");
37892 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37896 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37897 const char *p
= IDENTIFIER_POINTER (id
);
37899 if (strcmp (p
, "simd") == 0)
37901 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37902 if (cclauses
== NULL
)
37903 cclauses
= cclauses_buf
;
37905 cp_lexer_consume_token (parser
->lexer
);
37906 if (!flag_openmp
) /* flag_openmp_simd */
37907 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37909 sb
= begin_omp_structured_block ();
37910 save
= cp_parser_begin_omp_structured_block (parser
);
37911 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37913 cp_parser_end_omp_structured_block (parser
, save
);
37914 tree body
= finish_omp_structured_block (sb
);
37917 ret
= make_node (OMP_TASKLOOP
);
37918 TREE_TYPE (ret
) = void_type_node
;
37919 OMP_FOR_BODY (ret
) = body
;
37920 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37921 SET_EXPR_LOCATION (ret
, loc
);
37926 if (!flag_openmp
) /* flag_openmp_simd */
37928 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37932 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37936 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37937 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37940 sb
= begin_omp_structured_block ();
37941 save
= cp_parser_begin_omp_structured_block (parser
);
37943 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37946 cp_parser_end_omp_structured_block (parser
, save
);
37947 add_stmt (finish_omp_structured_block (sb
));
37954 # pragma acc routine oacc-routine-clause[optseq] new-line
37955 function-definition
37957 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37960 #define OACC_ROUTINE_CLAUSE_MASK \
37961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37967 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37968 component, which must resolve to a declared namespace-scope
37969 function. The clauses are either processed directly (for a named
37970 function), or defered until the immediatley following declaration
37974 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37975 enum pragma_context context
)
37977 gcc_checking_assert (context
== pragma_external
);
37978 /* The checking for "another pragma following this one" in the "no optional
37979 '( name )'" case makes sure that we dont re-enter. */
37980 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37982 cp_oacc_routine_data data
;
37983 data
.error_seen
= false;
37984 data
.fndecl_seen
= false;
37985 data
.tokens
= vNULL
;
37986 data
.clauses
= NULL_TREE
;
37987 data
.loc
= pragma_tok
->location
;
37988 /* It is safe to take the address of a local variable; it will only be
37989 used while this scope is live. */
37990 parser
->oacc_routine
= &data
;
37992 /* Look for optional '( name )'. */
37993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37995 matching_parens parens
;
37996 parens
.consume_open (parser
); /* '(' */
37998 /* We parse the name as an id-expression. If it resolves to
37999 anything other than a non-overloaded function at namespace
38000 scope, it's an error. */
38001 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38002 tree name
= cp_parser_id_expression (parser
,
38003 /*template_keyword_p=*/false,
38004 /*check_dependency_p=*/false,
38005 /*template_p=*/NULL
,
38006 /*declarator_p=*/false,
38007 /*optional_p=*/false);
38008 tree decl
= (identifier_p (name
)
38009 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38011 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38012 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38014 if (decl
== error_mark_node
38015 || !parens
.require_close (parser
))
38017 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38018 parser
->oacc_routine
= NULL
;
38023 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38024 "#pragma acc routine",
38025 cp_lexer_peek_token (parser
->lexer
));
38027 if (decl
&& is_overloaded_fn (decl
)
38028 && (TREE_CODE (decl
) != FUNCTION_DECL
38029 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38031 error_at (name_loc
,
38032 "%<#pragma acc routine%> names a set of overloads");
38033 parser
->oacc_routine
= NULL
;
38037 /* Perhaps we should use the same rule as declarations in different
38039 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38041 error_at (name_loc
,
38042 "%qD does not refer to a namespace scope function", decl
);
38043 parser
->oacc_routine
= NULL
;
38047 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38049 error_at (name_loc
, "%qD does not refer to a function", decl
);
38050 parser
->oacc_routine
= NULL
;
38054 cp_finalize_oacc_routine (parser
, decl
, false);
38055 parser
->oacc_routine
= NULL
;
38057 else /* No optional '( name )'. */
38059 /* Store away all pragma tokens. */
38060 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38061 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38062 cp_lexer_consume_token (parser
->lexer
);
38063 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38064 parser
->oacc_routine
->error_seen
= true;
38065 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38066 struct cp_token_cache
*cp
38067 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38068 parser
->oacc_routine
->tokens
.safe_push (cp
);
38070 /* Emit a helpful diagnostic if there's another pragma following this
38072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38074 cp_ensure_no_oacc_routine (parser
);
38075 data
.tokens
.release ();
38076 /* ..., and then just keep going. */
38080 /* We only have to consider the pragma_external case here. */
38081 cp_parser_declaration (parser
);
38082 if (parser
->oacc_routine
38083 && !parser
->oacc_routine
->fndecl_seen
)
38084 cp_ensure_no_oacc_routine (parser
);
38086 parser
->oacc_routine
= NULL
;
38087 data
.tokens
.release ();
38091 /* Finalize #pragma acc routine clauses after direct declarator has
38095 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38097 struct cp_token_cache
*ce
;
38098 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38100 if (!data
->error_seen
&& data
->fndecl_seen
)
38102 error_at (data
->loc
,
38103 "%<#pragma acc routine%> not immediately followed by "
38104 "a single function declaration or definition");
38105 data
->error_seen
= true;
38107 if (data
->error_seen
)
38110 gcc_checking_assert (data
->tokens
.length () == 1);
38111 ce
= data
->tokens
[0];
38113 cp_parser_push_lexer_for_tokens (parser
, ce
);
38114 parser
->lexer
->in_pragma
= true;
38115 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38117 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38118 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38119 parser
->oacc_routine
->clauses
38120 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38121 "#pragma acc routine", pragma_tok
);
38122 cp_parser_pop_lexer (parser
);
38123 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38129 /* Apply any saved OpenACC routine clauses to a just-parsed
38133 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38135 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38137 /* Keep going if we're in error reporting mode. */
38138 if (parser
->oacc_routine
->error_seen
38139 || fndecl
== error_mark_node
)
38142 if (parser
->oacc_routine
->fndecl_seen
)
38144 error_at (parser
->oacc_routine
->loc
,
38145 "%<#pragma acc routine%> not immediately followed by"
38146 " a single function declaration or definition");
38147 parser
->oacc_routine
= NULL
;
38150 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38152 cp_ensure_no_oacc_routine (parser
);
38156 if (oacc_get_fn_attrib (fndecl
))
38158 error_at (parser
->oacc_routine
->loc
,
38159 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38160 parser
->oacc_routine
= NULL
;
38164 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38166 error_at (parser
->oacc_routine
->loc
,
38168 ? G_("%<#pragma acc routine%> must be applied before use")
38169 : G_("%<#pragma acc routine%> must be applied before "
38171 parser
->oacc_routine
= NULL
;
38175 /* Process the routine's dimension clauses. */
38176 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38177 oacc_replace_fn_attrib (fndecl
, dims
);
38179 /* Add an "omp declare target" attribute. */
38180 DECL_ATTRIBUTES (fndecl
)
38181 = tree_cons (get_identifier ("omp declare target"),
38182 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38184 /* Don't unset parser->oacc_routine here: we may still need it to
38185 diagnose wrong usage. But, remember that we've used this "#pragma acc
38187 parser
->oacc_routine
->fndecl_seen
= true;
38191 /* Main entry point to OpenMP statement pragmas. */
38194 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38197 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38198 omp_clause_mask
mask (0);
38200 switch (cp_parser_pragma_kind (pragma_tok
))
38202 case PRAGMA_OACC_ATOMIC
:
38203 cp_parser_omp_atomic (parser
, pragma_tok
);
38205 case PRAGMA_OACC_CACHE
:
38206 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38208 case PRAGMA_OACC_DATA
:
38209 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38211 case PRAGMA_OACC_ENTER_DATA
:
38212 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38214 case PRAGMA_OACC_EXIT_DATA
:
38215 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38217 case PRAGMA_OACC_HOST_DATA
:
38218 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38220 case PRAGMA_OACC_KERNELS
:
38221 case PRAGMA_OACC_PARALLEL
:
38222 strcpy (p_name
, "#pragma acc");
38223 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38226 case PRAGMA_OACC_LOOP
:
38227 strcpy (p_name
, "#pragma acc");
38228 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38231 case PRAGMA_OACC_UPDATE
:
38232 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38234 case PRAGMA_OACC_WAIT
:
38235 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38237 case PRAGMA_OMP_ATOMIC
:
38238 cp_parser_omp_atomic (parser
, pragma_tok
);
38240 case PRAGMA_OMP_CRITICAL
:
38241 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38243 case PRAGMA_OMP_DISTRIBUTE
:
38244 strcpy (p_name
, "#pragma omp");
38245 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38248 case PRAGMA_OMP_FOR
:
38249 strcpy (p_name
, "#pragma omp");
38250 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38253 case PRAGMA_OMP_MASTER
:
38254 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38256 case PRAGMA_OMP_PARALLEL
:
38257 strcpy (p_name
, "#pragma omp");
38258 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38261 case PRAGMA_OMP_SECTIONS
:
38262 strcpy (p_name
, "#pragma omp");
38263 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38265 case PRAGMA_OMP_SIMD
:
38266 strcpy (p_name
, "#pragma omp");
38267 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38270 case PRAGMA_OMP_SINGLE
:
38271 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38273 case PRAGMA_OMP_TASK
:
38274 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38276 case PRAGMA_OMP_TASKGROUP
:
38277 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38279 case PRAGMA_OMP_TASKLOOP
:
38280 strcpy (p_name
, "#pragma omp");
38281 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38284 case PRAGMA_OMP_TEAMS
:
38285 strcpy (p_name
, "#pragma omp");
38286 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38290 gcc_unreachable ();
38293 protected_set_expr_location (stmt
, pragma_tok
->location
);
38296 /* Transactional Memory parsing routines. */
38298 /* Parse a transaction attribute.
38304 We use this instead of cp_parser_attributes_opt for transactions to avoid
38305 the pedwarn in C++98 mode. */
38308 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38311 tree attr_name
, attr
= NULL
;
38313 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38314 return cp_parser_attributes_opt (parser
);
38316 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38318 cp_lexer_consume_token (parser
->lexer
);
38319 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38322 token
= cp_lexer_peek_token (parser
->lexer
);
38323 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38325 token
= cp_lexer_consume_token (parser
->lexer
);
38327 attr_name
= (token
->type
== CPP_KEYWORD
38328 /* For keywords, use the canonical spelling,
38329 not the parsed identifier. */
38330 ? ridpointers
[(int) token
->keyword
]
38332 attr
= build_tree_list (attr_name
, NULL_TREE
);
38335 cp_parser_error (parser
, "expected identifier");
38337 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38339 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38343 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38345 transaction-statement:
38346 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38348 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38352 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38354 unsigned char old_in
= parser
->in_transaction
;
38355 unsigned char this_in
= 1, new_in
;
38356 enum rid keyword
= token
->keyword
;
38357 tree stmt
, attrs
, noex
;
38359 cp_lexer_consume_token (parser
->lexer
);
38361 if (keyword
== RID_TRANSACTION_RELAXED
38362 || keyword
== RID_SYNCHRONIZED
)
38363 this_in
|= TM_STMT_ATTR_RELAXED
;
38366 attrs
= cp_parser_txn_attribute_opt (parser
);
38368 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38371 /* Parse a noexcept specification. */
38372 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38373 noex
= boolean_true_node
;
38374 else if (keyword
== RID_ATOMIC_CANCEL
)
38376 /* cancel-and-throw is unimplemented. */
38377 sorry ("atomic_cancel");
38381 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38383 /* Keep track if we're in the lexical scope of an outer transaction. */
38384 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38386 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38388 parser
->in_transaction
= new_in
;
38389 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38390 parser
->in_transaction
= old_in
;
38392 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38397 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38399 transaction-expression:
38400 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38401 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38405 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38407 unsigned char old_in
= parser
->in_transaction
;
38408 unsigned char this_in
= 1;
38412 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38414 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38415 || keyword
== RID_TRANSACTION_RELAXED
);
38419 keyword
== RID_TRANSACTION_RELAXED
38420 ? G_("%<__transaction_relaxed%> without transactional memory "
38422 : G_("%<__transaction_atomic%> without transactional memory "
38423 "support enabled"));
38425 token
= cp_parser_require_keyword (parser
, keyword
,
38426 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38427 : RT_TRANSACTION_RELAXED
));
38428 gcc_assert (token
!= NULL
);
38430 if (keyword
== RID_TRANSACTION_RELAXED
)
38431 this_in
|= TM_STMT_ATTR_RELAXED
;
38433 /* Set this early. This might mean that we allow transaction_cancel in
38434 an expression that we find out later actually has to be a constexpr.
38435 However, we expect that cxx_constant_value will be able to deal with
38436 this; also, if the noexcept has no constexpr, then what we parse next
38437 really is a transaction's body. */
38438 parser
->in_transaction
= this_in
;
38440 /* Parse a noexcept specification. */
38441 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38444 if (!noex
|| !noex_expr
38445 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38447 matching_parens parens
;
38448 parens
.require_open (parser
);
38450 expr
= cp_parser_expression (parser
);
38451 expr
= finish_parenthesized_expr (expr
);
38453 parens
.require_close (parser
);
38457 /* The only expression that is available got parsed for the noexcept
38458 already. noexcept is true then. */
38460 noex
= boolean_true_node
;
38463 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38464 parser
->in_transaction
= old_in
;
38466 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38467 return error_mark_node
;
38469 return (flag_tm
? expr
: error_mark_node
);
38472 /* Parse a function-transaction-block.
38474 function-transaction-block:
38475 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38477 __transaction_atomic txn-attribute[opt] function-try-block
38478 __transaction_relaxed ctor-initializer[opt] function-body
38479 __transaction_relaxed function-try-block
38483 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38485 unsigned char old_in
= parser
->in_transaction
;
38486 unsigned char new_in
= 1;
38487 tree compound_stmt
, stmt
, attrs
;
38490 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38491 || keyword
== RID_TRANSACTION_RELAXED
);
38492 token
= cp_parser_require_keyword (parser
, keyword
,
38493 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38494 : RT_TRANSACTION_RELAXED
));
38495 gcc_assert (token
!= NULL
);
38497 if (keyword
== RID_TRANSACTION_RELAXED
)
38498 new_in
|= TM_STMT_ATTR_RELAXED
;
38501 attrs
= cp_parser_txn_attribute_opt (parser
);
38503 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38506 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38508 parser
->in_transaction
= new_in
;
38510 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38511 cp_parser_function_try_block (parser
);
38513 cp_parser_ctor_initializer_opt_and_function_body
38514 (parser
, /*in_function_try_block=*/false);
38516 parser
->in_transaction
= old_in
;
38518 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38521 /* Parse a __transaction_cancel statement.
38524 __transaction_cancel txn-attribute[opt] ;
38525 __transaction_cancel txn-attribute[opt] throw-expression ;
38527 ??? Cancel and throw is not yet implemented. */
38530 cp_parser_transaction_cancel (cp_parser
*parser
)
38533 bool is_outer
= false;
38536 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38537 RT_TRANSACTION_CANCEL
);
38538 gcc_assert (token
!= NULL
);
38540 attrs
= cp_parser_txn_attribute_opt (parser
);
38542 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38544 /* ??? Parse cancel-and-throw here. */
38546 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38550 error_at (token
->location
, "%<__transaction_cancel%> without "
38551 "transactional memory support enabled");
38552 return error_mark_node
;
38554 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38556 error_at (token
->location
, "%<__transaction_cancel%> within a "
38557 "%<__transaction_relaxed%>");
38558 return error_mark_node
;
38562 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38563 && !is_tm_may_cancel_outer (current_function_decl
))
38565 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38566 "within outer %<__transaction_atomic%>");
38567 error_at (token
->location
,
38568 " or a %<transaction_may_cancel_outer%> function");
38569 return error_mark_node
;
38572 else if (parser
->in_transaction
== 0)
38574 error_at (token
->location
, "%<__transaction_cancel%> not within "
38575 "%<__transaction_atomic%>");
38576 return error_mark_node
;
38579 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38587 static GTY (()) cp_parser
*the_parser
;
38590 /* Special handling for the first token or line in the file. The first
38591 thing in the file might be #pragma GCC pch_preprocess, which loads a
38592 PCH file, which is a GC collection point. So we need to handle this
38593 first pragma without benefit of an existing lexer structure.
38595 Always returns one token to the caller in *FIRST_TOKEN. This is
38596 either the true first token of the file, or the first token after
38597 the initial pragma. */
38600 cp_parser_initial_pragma (cp_token
*first_token
)
38604 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38605 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38608 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38609 if (first_token
->type
== CPP_STRING
)
38611 name
= first_token
->u
.value
;
38613 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38614 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38615 error_at (first_token
->location
,
38616 "junk at end of %<#pragma GCC pch_preprocess%>");
38619 error_at (first_token
->location
, "expected string literal");
38621 /* Skip to the end of the pragma. */
38622 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38623 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38625 /* Now actually load the PCH file. */
38627 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38629 /* Read one more token to return to our caller. We have to do this
38630 after reading the PCH file in, since its pointers have to be
38632 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38635 /* Parse a pragma GCC ivdep. */
38638 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38640 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38644 /* Parse a pragma GCC unroll. */
38646 static unsigned short
38647 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38649 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38650 tree expr
= cp_parser_constant_expression (parser
);
38651 unsigned short unroll
;
38652 expr
= maybe_constant_value (expr
);
38653 HOST_WIDE_INT lunroll
= 0;
38654 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38655 || TREE_CODE (expr
) != INTEGER_CST
38656 || (lunroll
= tree_to_shwi (expr
)) < 0
38657 || lunroll
>= USHRT_MAX
)
38659 error_at (location
, "%<#pragma GCC unroll%> requires an"
38660 " assignment-expression that evaluates to a non-negative"
38661 " integral constant less than %u", USHRT_MAX
);
38666 unroll
= (unsigned short)lunroll
;
38670 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38674 /* Normal parsing of a pragma token. Here we can (and must) use the
38678 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38680 cp_token
*pragma_tok
;
38685 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38686 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38687 parser
->lexer
->in_pragma
= true;
38689 id
= cp_parser_pragma_kind (pragma_tok
);
38690 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38691 cp_ensure_no_omp_declare_simd (parser
);
38694 case PRAGMA_GCC_PCH_PREPROCESS
:
38695 error_at (pragma_tok
->location
,
38696 "%<#pragma GCC pch_preprocess%> must be first");
38699 case PRAGMA_OMP_BARRIER
:
38702 case pragma_compound
:
38703 cp_parser_omp_barrier (parser
, pragma_tok
);
38706 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38707 "used in compound statements", "omp barrier");
38714 case PRAGMA_OMP_FLUSH
:
38717 case pragma_compound
:
38718 cp_parser_omp_flush (parser
, pragma_tok
);
38721 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38722 "used in compound statements", "omp flush");
38729 case PRAGMA_OMP_TASKWAIT
:
38732 case pragma_compound
:
38733 cp_parser_omp_taskwait (parser
, pragma_tok
);
38736 error_at (pragma_tok
->location
,
38737 "%<#pragma %s%> may only be used in compound statements",
38745 case PRAGMA_OMP_TASKYIELD
:
38748 case pragma_compound
:
38749 cp_parser_omp_taskyield (parser
, pragma_tok
);
38752 error_at (pragma_tok
->location
,
38753 "%<#pragma %s%> may only be used in compound statements",
38761 case PRAGMA_OMP_CANCEL
:
38764 case pragma_compound
:
38765 cp_parser_omp_cancel (parser
, pragma_tok
);
38768 error_at (pragma_tok
->location
,
38769 "%<#pragma %s%> may only be used in compound statements",
38777 case PRAGMA_OMP_CANCELLATION_POINT
:
38778 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38781 case PRAGMA_OMP_THREADPRIVATE
:
38782 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38785 case PRAGMA_OMP_DECLARE
:
38786 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38788 case PRAGMA_OACC_DECLARE
:
38789 cp_parser_oacc_declare (parser
, pragma_tok
);
38792 case PRAGMA_OACC_ENTER_DATA
:
38793 if (context
== pragma_stmt
)
38795 error_at (pragma_tok
->location
,
38796 "%<#pragma %s%> may only be used in compound statements",
38800 else if (context
!= pragma_compound
)
38802 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38805 case PRAGMA_OACC_EXIT_DATA
:
38806 if (context
== pragma_stmt
)
38808 error_at (pragma_tok
->location
,
38809 "%<#pragma %s%> may only be used in compound statements",
38813 else if (context
!= pragma_compound
)
38815 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38818 case PRAGMA_OACC_ROUTINE
:
38819 if (context
!= pragma_external
)
38821 error_at (pragma_tok
->location
,
38822 "%<#pragma acc routine%> must be at file scope");
38825 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38828 case PRAGMA_OACC_UPDATE
:
38829 if (context
== pragma_stmt
)
38831 error_at (pragma_tok
->location
,
38832 "%<#pragma %s%> may only be used in compound statements",
38836 else if (context
!= pragma_compound
)
38838 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38841 case PRAGMA_OACC_WAIT
:
38842 if (context
== pragma_stmt
)
38844 error_at (pragma_tok
->location
,
38845 "%<#pragma %s%> may only be used in compound statements",
38849 else if (context
!= pragma_compound
)
38851 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38854 case PRAGMA_OACC_ATOMIC
:
38855 case PRAGMA_OACC_CACHE
:
38856 case PRAGMA_OACC_DATA
:
38857 case PRAGMA_OACC_HOST_DATA
:
38858 case PRAGMA_OACC_KERNELS
:
38859 case PRAGMA_OACC_PARALLEL
:
38860 case PRAGMA_OACC_LOOP
:
38861 case PRAGMA_OMP_ATOMIC
:
38862 case PRAGMA_OMP_CRITICAL
:
38863 case PRAGMA_OMP_DISTRIBUTE
:
38864 case PRAGMA_OMP_FOR
:
38865 case PRAGMA_OMP_MASTER
:
38866 case PRAGMA_OMP_PARALLEL
:
38867 case PRAGMA_OMP_SECTIONS
:
38868 case PRAGMA_OMP_SIMD
:
38869 case PRAGMA_OMP_SINGLE
:
38870 case PRAGMA_OMP_TASK
:
38871 case PRAGMA_OMP_TASKGROUP
:
38872 case PRAGMA_OMP_TASKLOOP
:
38873 case PRAGMA_OMP_TEAMS
:
38874 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38876 stmt
= push_omp_privatization_clauses (false);
38877 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38878 pop_omp_privatization_clauses (stmt
);
38881 case PRAGMA_OMP_ORDERED
:
38882 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38884 stmt
= push_omp_privatization_clauses (false);
38885 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38886 pop_omp_privatization_clauses (stmt
);
38889 case PRAGMA_OMP_TARGET
:
38890 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38892 stmt
= push_omp_privatization_clauses (false);
38893 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38894 pop_omp_privatization_clauses (stmt
);
38897 case PRAGMA_OMP_END_DECLARE_TARGET
:
38898 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38901 case PRAGMA_OMP_SECTION
:
38902 error_at (pragma_tok
->location
,
38903 "%<#pragma omp section%> may only be used in "
38904 "%<#pragma omp sections%> construct");
38909 if (context
== pragma_external
)
38911 error_at (pragma_tok
->location
,
38912 "%<#pragma GCC ivdep%> must be inside a function");
38915 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38916 unsigned short unroll
;
38917 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38918 if (tok
->type
== CPP_PRAGMA
38919 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38921 tok
= cp_lexer_consume_token (parser
->lexer
);
38922 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38923 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38927 if (tok
->type
!= CPP_KEYWORD
38928 || (tok
->keyword
!= RID_FOR
38929 && tok
->keyword
!= RID_WHILE
38930 && tok
->keyword
!= RID_DO
))
38932 cp_parser_error (parser
, "for, while or do statement expected");
38935 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38939 case PRAGMA_UNROLL
:
38941 if (context
== pragma_external
)
38943 error_at (pragma_tok
->location
,
38944 "%<#pragma GCC unroll%> must be inside a function");
38947 const unsigned short unroll
38948 = cp_parser_pragma_unroll (parser
, pragma_tok
);
38950 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38951 if (tok
->type
== CPP_PRAGMA
38952 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
38954 tok
= cp_lexer_consume_token (parser
->lexer
);
38955 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
38956 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38960 if (tok
->type
!= CPP_KEYWORD
38961 || (tok
->keyword
!= RID_FOR
38962 && tok
->keyword
!= RID_WHILE
38963 && tok
->keyword
!= RID_DO
))
38965 cp_parser_error (parser
, "for, while or do statement expected");
38968 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38973 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38974 c_invoke_pragma_handler (id
);
38978 cp_parser_error (parser
, "expected declaration specifiers");
38982 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38986 /* The interface the pragma parsers have to the lexer. */
38989 pragma_lex (tree
*value
, location_t
*loc
)
38991 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38992 enum cpp_ttype ret
= tok
->type
;
38994 *value
= tok
->u
.value
;
38996 *loc
= tok
->location
;
38998 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39000 else if (ret
== CPP_STRING
)
39001 *value
= cp_parser_string_literal (the_parser
, false, false);
39004 if (ret
== CPP_KEYWORD
)
39006 cp_lexer_consume_token (the_parser
->lexer
);
39013 /* External interface. */
39015 /* Parse one entire translation unit. */
39018 c_parse_file (void)
39020 static bool already_called
= false;
39022 if (already_called
)
39023 fatal_error (input_location
,
39024 "inter-module optimizations not implemented for C++");
39025 already_called
= true;
39027 the_parser
= cp_parser_new ();
39028 push_deferring_access_checks (flag_access_control
39029 ? dk_no_deferred
: dk_no_check
);
39030 cp_parser_translation_unit (the_parser
);
39034 /* Create an identifier for a generic parameter type (a synthesized
39035 template parameter implied by `auto' or a concept identifier). */
39037 static GTY(()) int generic_parm_count
;
39039 make_generic_type_name ()
39042 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39043 return get_identifier (buf
);
39046 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39047 (creating a new template parameter list if necessary). Returns the newly
39048 created template type parm. */
39051 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39053 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39055 /* Before committing to modifying any scope, if we're in an
39056 implicit template scope, and we're trying to synthesize a
39057 constrained parameter, try to find a previous parameter with
39058 the same name. This is the same-type rule for abbreviated
39059 function templates.
39061 NOTE: We can generate implicit parameters when tentatively
39062 parsing a nested name specifier, only to reject that parse
39063 later. However, matching the same template-id as part of a
39064 direct-declarator should generate an identical template
39065 parameter, so this rule will merge them. */
39066 if (parser
->implicit_template_scope
&& constr
)
39068 tree t
= parser
->implicit_template_parms
;
39071 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39073 tree d
= TREE_VALUE (t
);
39074 if (TREE_CODE (d
) == PARM_DECL
)
39075 /* Return the TEMPLATE_PARM_INDEX. */
39076 d
= DECL_INITIAL (d
);
39079 t
= TREE_CHAIN (t
);
39083 /* We are either continuing a function template that already contains implicit
39084 template parameters, creating a new fully-implicit function template, or
39085 extending an existing explicit function template with implicit template
39088 cp_binding_level
*const entry_scope
= current_binding_level
;
39090 bool become_template
= false;
39091 cp_binding_level
*parent_scope
= 0;
39093 if (parser
->implicit_template_scope
)
39095 gcc_assert (parser
->implicit_template_parms
);
39097 current_binding_level
= parser
->implicit_template_scope
;
39101 /* Roll back to the existing template parameter scope (in the case of
39102 extending an explicit function template) or introduce a new template
39103 parameter scope ahead of the function parameter scope (or class scope
39104 in the case of out-of-line member definitions). The function scope is
39105 added back after template parameter synthesis below. */
39107 cp_binding_level
*scope
= entry_scope
;
39109 while (scope
->kind
== sk_function_parms
)
39111 parent_scope
= scope
;
39112 scope
= scope
->level_chain
;
39114 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39116 /* If not defining a class, then any class scope is a scope level in
39117 an out-of-line member definition. In this case simply wind back
39118 beyond the first such scope to inject the template parameter list.
39119 Otherwise wind back to the class being defined. The latter can
39120 occur in class member friend declarations such as:
39126 friend void A::foo (auto);
39129 The template parameter list synthesized for the friend declaration
39130 must be injected in the scope of 'B'. This can also occur in
39131 erroneous cases such as:
39137 void B::foo (auto) {}
39140 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39141 but, nevertheless, the template parameter list synthesized for the
39142 declarator should be injected into the scope of 'A' as if the
39143 ill-formed template was specified explicitly. */
39145 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39147 parent_scope
= scope
;
39148 scope
= scope
->level_chain
;
39152 current_binding_level
= scope
;
39154 if (scope
->kind
!= sk_template_parms
39155 || !function_being_declared_is_template_p (parser
))
39157 /* Introduce a new template parameter list for implicit template
39160 become_template
= true;
39162 parser
->implicit_template_scope
39163 = begin_scope (sk_template_parms
, NULL
);
39165 ++processing_template_decl
;
39167 parser
->fully_implicit_function_template_p
= true;
39168 ++parser
->num_template_parameter_lists
;
39172 /* Synthesize implicit template parameters at the end of the explicit
39173 template parameter list. */
39175 gcc_assert (current_template_parms
);
39177 parser
->implicit_template_scope
= scope
;
39179 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39180 parser
->implicit_template_parms
39181 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39185 /* Synthesize a new template parameter and track the current template
39186 parameter chain with implicit_template_parms. */
39188 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39189 tree synth_id
= make_generic_type_name ();
39190 tree synth_tmpl_parm
;
39191 bool non_type
= false;
39193 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39195 = finish_template_type_parm (class_type_node
, synth_id
);
39196 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39198 = finish_constrained_template_template_parm (proto
, synth_id
);
39201 synth_tmpl_parm
= copy_decl (proto
);
39202 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39206 // Attach the constraint to the parm before processing.
39207 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39208 TREE_TYPE (node
) = constr
;
39210 = process_template_parm (parser
->implicit_template_parms
,
39213 /*non_type=*/non_type
,
39214 /*param_pack=*/false);
39216 // Chain the new parameter to the list of implicit parameters.
39217 if (parser
->implicit_template_parms
)
39218 parser
->implicit_template_parms
39219 = TREE_CHAIN (parser
->implicit_template_parms
);
39221 parser
->implicit_template_parms
= new_parm
;
39223 tree new_decl
= get_local_decls ();
39225 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39226 new_decl
= DECL_INITIAL (new_decl
);
39228 /* If creating a fully implicit function template, start the new implicit
39229 template parameter list with this synthesized type, otherwise grow the
39230 current template parameter list. */
39232 if (become_template
)
39234 parent_scope
->level_chain
= current_binding_level
;
39236 tree new_parms
= make_tree_vec (1);
39237 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39238 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39239 new_parms
, current_template_parms
);
39243 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39244 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39245 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39246 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39249 // If the new parameter was constrained, we need to add that to the
39250 // constraints in the template parameter list.
39251 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39253 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39254 reqs
= conjoin_constraints (reqs
, req
);
39255 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39258 current_binding_level
= entry_scope
;
39263 /* Finish the declaration of a fully implicit function template. Such a
39264 template has no explicit template parameter list so has not been through the
39265 normal template head and tail processing. synthesize_implicit_template_parm
39266 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39267 provided if the declaration is a class member such that its template
39268 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39269 form is returned. Otherwise NULL_TREE is returned. */
39272 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39274 gcc_assert (parser
->fully_implicit_function_template_p
);
39276 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39277 && DECL_VIRTUAL_P (member_decl_opt
))
39279 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39280 "implicit templates may not be %<virtual%>");
39281 DECL_VIRTUAL_P (member_decl_opt
) = false;
39284 if (member_decl_opt
)
39285 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39286 end_template_decl ();
39288 parser
->fully_implicit_function_template_p
= false;
39289 parser
->implicit_template_parms
= 0;
39290 parser
->implicit_template_scope
= 0;
39291 --parser
->num_template_parameter_lists
;
39293 return member_decl_opt
;
39296 /* Like finish_fully_implicit_template, but to be used in error
39297 recovery, rearranging scopes so that we restore the state we had
39298 before synthesize_implicit_template_parm inserted the implement
39299 template parms scope. */
39302 abort_fully_implicit_template (cp_parser
*parser
)
39304 cp_binding_level
*return_to_scope
= current_binding_level
;
39306 if (parser
->implicit_template_scope
39307 && return_to_scope
!= parser
->implicit_template_scope
)
39309 cp_binding_level
*child
= return_to_scope
;
39310 for (cp_binding_level
*scope
= child
->level_chain
;
39311 scope
!= parser
->implicit_template_scope
;
39312 scope
= child
->level_chain
)
39314 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39315 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39316 current_binding_level
= parser
->implicit_template_scope
;
39319 return_to_scope
= return_to_scope
->level_chain
;
39321 finish_fully_implicit_template (parser
, NULL
);
39323 gcc_assert (current_binding_level
== return_to_scope
);
39326 /* Helper function for diagnostics that have complained about things
39327 being used with 'extern "C"' linkage.
39329 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39332 maybe_show_extern_c_location (void)
39334 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39335 inform (the_parser
->innermost_linkage_specification_location
,
39336 "%<extern \"C\"%> linkage started here");
39339 #include "gt-cp-parser.h"