2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
560 parser
->implicit_extern_c
);
561 cp_debug_print_flag (file
, "String expressions should be translated "
562 "to execution character set",
563 parser
->translate_strings_p
);
564 cp_debug_print_flag (file
, "Parsing function body outside of a "
565 "local class", parser
->in_function_body
);
566 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
567 parser
->colon_corrects_to_scope_p
);
568 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
569 parser
->colon_doesnt_start_class_def_p
);
570 if (parser
->type_definition_forbidden_message
)
571 fprintf (file
, "Error message for forbidden type definitions: %s\n",
572 parser
->type_definition_forbidden_message
);
573 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
574 fprintf (file
, "Number of class definitions in progress: %u\n",
575 parser
->num_classes_being_defined
);
576 fprintf (file
, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser
->num_template_parameter_lists
);
578 cp_debug_parser_tokens (file
, parser
, window_size
);
579 token
= parser
->lexer
->next_token
;
580 fprintf (file
, "Next token to parse:\n");
581 fprintf (file
, "\tToken: ");
582 cp_lexer_print_token (file
, token
);
583 eloc
= expand_location (token
->location
);
584 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
585 fprintf (file
, "\tLine: %d\n", eloc
.line
);
586 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
590 debug (cp_parser
&ref
)
592 cp_debug_parser (stderr
, &ref
);
596 debug (cp_parser
*ptr
)
601 fprintf (stderr
, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
616 /* Initially we are not debugging. */
617 lexer
->debugging_p
= false;
619 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
621 /* Create the buffer. */
622 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token
);
642 lexer
= cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer
->buffer
->quick_push (token
);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token
.type
!= CPP_EOF
)
650 cp_lexer_get_preprocessor_token (lexer
, &token
);
651 vec_safe_push (lexer
->buffer
, token
);
654 lexer
->last_token
= lexer
->buffer
->address ()
655 + lexer
->buffer
->length ()
657 lexer
->next_token
= lexer
->buffer
->length ()
658 ? lexer
->buffer
->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
681 lexer
->next_token
= first
== last
? &eof_token
: first
;
682 lexer
->last_token
= last
;
684 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
686 /* Initially we are not debugging. */
687 lexer
->debugging_p
= false;
689 gcc_assert (!lexer
->next_token
->purged_p
);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer
*lexer
)
698 vec_free (lexer
->buffer
);
699 lexer
->saved_tokens
.release ();
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
713 cp_lexer_debugging_p (cp_lexer
*lexer
)
715 if (!LEXER_DEBUGGING_ENABLED_P
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 /* Skip past purged tokens. */
759 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
763 return cp_lexer_token_at (lexer
, tp
);
766 /* nonzero if we are presently saving tokens. */
769 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
771 return lexer
->saved_tokens
.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
780 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
782 static int is_extern_c
= 0;
784 /* Get a new token from the preprocessor. */
786 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
787 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
788 token
->keyword
= RID_MAX
;
789 token
->purged_p
= false;
790 token
->error_reported
= false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c
+= pending_lang_change
;
796 pending_lang_change
= 0;
797 token
->implicit_extern_c
= is_extern_c
> 0;
799 /* Check to see if this token is a keyword. */
800 if (token
->type
== CPP_NAME
)
802 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
804 /* Mark this token as a keyword. */
805 token
->type
= CPP_KEYWORD
;
806 /* Record which keyword. */
807 token
->keyword
= C_RID_CODE (token
->u
.value
);
811 if (warn_cxx11_compat
812 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
813 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
815 /* Warn about the C++0x keyword (but still treat it as
817 warning (OPT_Wc__11_compat
,
818 "identifier %qE is a keyword in C++11",
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
826 token
->keyword
= RID_MAX
;
829 else if (token
->type
== CPP_AT_NAME
)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token
->type
= CPP_KEYWORD
;
833 switch (C_RID_CODE (token
->u
.value
))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
843 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
844 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
845 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
846 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
847 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
848 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
849 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
850 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
851 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if KEYWORD can start a decl-specifier. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE
:
979 if (keyword
>= RID_FIRST_INT_N
980 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
981 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
994 token
= cp_lexer_peek_token (lexer
);
995 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
998 /* Returns TRUE iff the token T begins a decltype type. */
1001 token_is_decltype (cp_token
*t
)
1003 return (t
->keyword
== RID_DECLTYPE
1004 || t
->type
== CPP_DECLTYPE
);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1010 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1012 cp_token
*t
= cp_lexer_peek_token (lexer
);
1013 return token_is_decltype (t
);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1020 saved_checks_value (struct tree_check
*check_value
)
1022 /* Perform any access checks that were deferred. */
1023 vec
<deferred_access_check
, va_gc
> *checks
;
1024 deferred_access_check
*chk
;
1025 checks
= check_value
->checks
;
1029 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1030 perform_or_defer_access_check (chk
->binfo
,
1032 chk
->diag_decl
, tf_warning_or_error
);
1034 /* Return the stored value. */
1035 return check_value
->value
;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1045 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer
))
1053 fprintf (cp_lexer_debug_stream
,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1057 token
= lexer
->next_token
;
1058 gcc_assert (!n
|| token
!= &eof_token
);
1062 if (token
== lexer
->last_token
)
1068 if (!token
->purged_p
)
1072 if (cp_lexer_debugging_p (lexer
))
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1085 cp_lexer_consume_token (cp_lexer
* lexer
)
1087 cp_token
*token
= lexer
->next_token
;
1089 gcc_assert (token
!= &eof_token
);
1090 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1094 lexer
->next_token
++;
1095 if (lexer
->next_token
== lexer
->last_token
)
1097 lexer
->next_token
= &eof_token
;
1102 while (lexer
->next_token
->purged_p
);
1104 cp_lexer_set_source_position_from_token (token
);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer
))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1110 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1111 putc ('\n', cp_lexer_debug_stream
);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1122 cp_lexer_purge_token (cp_lexer
*lexer
)
1124 cp_token
*tok
= lexer
->next_token
;
1126 gcc_assert (tok
!= &eof_token
);
1127 tok
->purged_p
= true;
1128 tok
->location
= UNKNOWN_LOCATION
;
1129 tok
->u
.value
= NULL_TREE
;
1130 tok
->keyword
= RID_MAX
;
1135 if (tok
== lexer
->last_token
)
1141 while (tok
->purged_p
);
1142 lexer
->next_token
= tok
;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1150 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1152 cp_token
*peek
= lexer
->next_token
;
1154 if (peek
== &eof_token
)
1155 peek
= lexer
->last_token
;
1157 gcc_assert (tok
< peek
);
1159 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1161 tok
->purged_p
= true;
1162 tok
->location
= UNKNOWN_LOCATION
;
1163 tok
->u
.value
= NULL_TREE
;
1164 tok
->keyword
= RID_MAX
;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1172 cp_lexer_save_tokens (cp_lexer
* lexer
)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer
))
1176 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1178 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1181 /* Commit to the portion of the token stream most recently saved. */
1184 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer
))
1188 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1190 lexer
->saved_tokens
.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1197 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer
))
1201 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1203 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1218 len
= lexer
->saved_tokens
.length ();
1219 cp_lexer_save_tokens (lexer
);
1223 cp_lexer_rollback_tokens (lexer
);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer
);
1230 gcc_assert (lexer
->saved_tokens
.length () == len
);
1234 /* Print a representation of the TOKEN on the STREAM. */
1237 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names
[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token
->type
)
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token
->u
.value
))
1264 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1271 case CPP_UTF8STRING
:
1272 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1276 print_generic_expr (stream
, token
->u
.value
);
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token
->type
< ARRAY_SIZE(token_names
))
1283 fputs (token_names
[token
->type
], stream
);
1285 fprintf (stream
, "[%d]", token
->type
);
1291 debug (cp_token
&ref
)
1293 cp_lexer_print_token (stderr
, &ref
);
1294 fprintf (stderr
, "\n");
1298 debug (cp_token
*ptr
)
1303 fprintf (stderr
, "<nil>\n");
1307 /* Start emitting debugging information. */
1310 cp_lexer_start_debugging (cp_lexer
* lexer
)
1312 if (!LEXER_DEBUGGING_ENABLED_P
)
1313 fatal_error (input_location
,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer
->debugging_p
= true;
1317 cp_lexer_debug_stream
= stderr
;
1320 /* Stop emitting debugging information. */
1323 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1325 if (!LEXER_DEBUGGING_ENABLED_P
)
1326 fatal_error (input_location
,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer
->debugging_p
= false;
1330 cp_lexer_debug_stream
= NULL
;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache
*
1336 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1338 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1339 cache
->first
= first
;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1348 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1350 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser
->omp_declare_simd
= NULL
;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1362 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1364 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1366 if (fndecl
== error_mark_node
)
1368 parser
->omp_declare_simd
= NULL
;
1371 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1373 cp_ensure_no_omp_declare_simd (parser
);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1383 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1385 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1387 error_at (parser
->oacc_routine
->loc
,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser
->oacc_routine
= NULL
;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1399 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1401 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator
*make_call_declarator
1412 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1413 static cp_declarator
*make_array_declarator
1414 (cp_declarator
*, tree
);
1415 static cp_declarator
*make_pointer_declarator
1416 (cp_cv_quals
, cp_declarator
*, tree
);
1417 static cp_declarator
*make_reference_declarator
1418 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1419 static cp_declarator
*make_ptrmem_declarator
1420 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1422 /* An erroneous declarator. */
1423 static cp_declarator
*cp_error_declarator
;
1425 /* The obstack on which declarators and related data structures are
1427 static struct obstack declarator_obstack
;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes
)
1434 return obstack_alloc (&declarator_obstack
, bytes
);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator
*
1441 make_declarator (cp_declarator_kind kind
)
1443 cp_declarator
*declarator
;
1445 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1446 declarator
->kind
= kind
;
1447 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1448 declarator
->attributes
= NULL_TREE
;
1449 declarator
->std_attributes
= NULL_TREE
;
1450 declarator
->declarator
= NULL
;
1451 declarator
->parameter_pack_p
= false;
1452 declarator
->id_loc
= UNKNOWN_LOCATION
;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1463 static cp_declarator
*
1464 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1465 special_function_kind sfk
)
1467 cp_declarator
*declarator
;
1469 /* It is valid to write:
1471 class C { void f(); };
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1479 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1480 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1482 gcc_assert (identifier_p (unqualified_name
)
1483 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1486 declarator
= make_declarator (cdk_id
);
1487 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1488 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1489 declarator
->u
.id
.sfk
= sfk
;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_pointer
);
1506 declarator
->declarator
= target
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1511 declarator
->id_loc
= target
->id_loc
;
1512 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1513 target
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1528 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1529 bool rvalue_ref
, tree attributes
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_reference
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1536 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1539 declarator
->id_loc
= target
->id_loc
;
1540 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1541 target
->parameter_pack_p
= false;
1544 declarator
->parameter_pack_p
= false;
1546 declarator
->std_attributes
= attributes
;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1557 cp_declarator
*pointee
,
1560 cp_declarator
*declarator
;
1562 declarator
= make_declarator (cdk_ptrmem
);
1563 declarator
->declarator
= pointee
;
1564 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1565 declarator
->u
.pointer
.class_type
= class_type
;
1569 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1570 pointee
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1575 declarator
->std_attributes
= attributes
;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1586 make_call_declarator (cp_declarator
*target
,
1588 cp_cv_quals cv_qualifiers
,
1589 cp_virt_specifiers virt_specifiers
,
1590 cp_ref_qualifier ref_qualifier
,
1592 tree exception_specification
,
1593 tree late_return_type
,
1594 tree requires_clause
)
1596 cp_declarator
*declarator
;
1598 declarator
= make_declarator (cdk_function
);
1599 declarator
->declarator
= target
;
1600 declarator
->u
.function
.parameters
= parms
;
1601 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1603 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1604 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1605 declarator
->u
.function
.exception_specification
= exception_specification
;
1606 declarator
->u
.function
.late_return_type
= late_return_type
;
1607 declarator
->u
.function
.requires_clause
= requires_clause
;
1610 declarator
->id_loc
= target
->id_loc
;
1611 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1612 target
->parameter_pack_p
= false;
1615 declarator
->parameter_pack_p
= false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1624 make_array_declarator (cp_declarator
*element
, tree bounds
)
1626 cp_declarator
*declarator
;
1628 declarator
= make_declarator (cdk_array
);
1629 declarator
->declarator
= element
;
1630 declarator
->u
.array
.bounds
= bounds
;
1633 declarator
->id_loc
= element
->id_loc
;
1634 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1635 element
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1648 if (declarator
&& declarator
->parameter_pack_p
)
1649 /* We already saw an ellipsis. */
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1657 while (declarator
&& !found
)
1659 switch ((int)declarator
->kind
)
1671 declarator
= declarator
->declarator
;
1679 cp_parameter_declarator
*no_parameters
;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator
*
1685 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1686 cp_declarator
*declarator
,
1687 tree default_argument
,
1689 bool template_parameter_pack_p
= false)
1691 cp_parameter_declarator
*parameter
;
1693 parameter
= ((cp_parameter_declarator
*)
1694 alloc_declarator (sizeof (cp_parameter_declarator
)));
1695 parameter
->next
= NULL
;
1696 if (decl_specifiers
)
1697 parameter
->decl_specifiers
= *decl_specifiers
;
1699 clear_decl_specs (¶meter
->decl_specifiers
);
1700 parameter
->declarator
= declarator
;
1701 parameter
->default_argument
= default_argument
;
1702 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1703 parameter
->loc
= loc
;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator
*declarator
)
1715 if (declarator
->kind
== cdk_function
1716 && declarator
->declarator
->kind
== cdk_id
)
1718 if (declarator
->kind
== cdk_id
1719 || declarator
->kind
== cdk_decomp
1720 || declarator
->kind
== cdk_error
)
1722 declarator
= declarator
->declarator
;
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1788 CP_PARSER_FLAGS_NONE
= 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser
*, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser
*, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser
*, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser
*, tree
, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2049 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2050 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser
*, tree
, tree
*, tree
*);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2060 static tree cp_parser_new_expression
2062 static vec
<tree
, va_gc
> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser
*, tree
*);
2066 static cp_declarator
*cp_parser_new_declarator_opt
2068 static cp_declarator
*cp_parser_direct_new_declarator
2070 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser
*, cp_expr
);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2084 static cp_expr cp_parser_expression
2085 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2090 static cp_expr cp_parser_lambda_expression
2092 static void cp_parser_lambda_introducer
2093 (cp_parser
*, tree
);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser
*, tree
);
2096 static void cp_parser_lambda_body
2097 (cp_parser
*, tree
);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_expression_statement
2106 (cp_parser
*, tree
);
2107 static tree cp_parser_compound_statement
2108 (cp_parser
*, tree
, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_selection_statement
2112 (cp_parser
*, bool *, vec
<tree
> *);
2113 static tree cp_parser_condition
2115 static tree cp_parser_iteration_statement
2116 (cp_parser
*, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser
*, tree
*decl
);
2119 static tree cp_parser_for
2120 (cp_parser
*, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser
*, tree
, tree
, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree
, tree
*, tree
*);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser
*, bool *, const token_indent_info
&);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2145 static void cp_parser_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_type_name
2166 static tree cp_parser_nonclass_name
2167 (cp_parser
* parser
);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser
*, bool, bool);
2170 static tree cp_parser_enum_specifier
2172 static void cp_parser_enumerator_list
2173 (cp_parser
*, tree
);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser
*, tree
);
2176 static tree cp_parser_namespace_name
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser
*, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser
*, bool);
2198 static tree cp_parser_decltype
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2207 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2224 static tree cp_parser_declarator_id
2225 (cp_parser
*, bool);
2226 static tree cp_parser_type_id
2228 static tree cp_parser_template_type_arg
2230 static tree
cp_parser_trailing_type_id (cp_parser
*);
2231 static tree cp_parser_type_id_1
2232 (cp_parser
*, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2235 static tree cp_parser_parameter_declaration_clause
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser
*, bool *);
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
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 /* It might be that we only found DECL because we are
5589 trying to be generous with pre-ISO scoping rules.
5590 For example, consider:
5594 for (int i = 0; i < 10; ++i) {}
5595 extern void f(int j = i);
5598 Here, name look up will originally find the out
5599 of scope `i'. We need to issue a warning message,
5600 but then use the global `i'. */
5601 decl
= check_for_out_of_scope_variable (decl
);
5602 if (local_variable_p (decl
))
5604 error_at (id_expr_token
->location
,
5605 "local variable %qD may not appear in this context",
5607 return error_mark_node
;
5612 decl
= (finish_id_expression
5613 (id_expression
, decl
, parser
->scope
,
5615 parser
->integral_constant_expression_p
,
5616 parser
->allow_non_integral_constant_expression_p
,
5617 &parser
->non_integral_constant_expression_p
,
5618 template_p
, done
, address_p
,
5621 id_expression
.get_location ()));
5623 cp_parser_error (parser
, error_msg
);
5624 decl
.set_location (id_expr_token
->location
);
5628 /* Anything else is an error. */
5630 cp_parser_error (parser
, "expected primary-expression");
5631 return error_mark_node
;
5635 static inline cp_expr
5636 cp_parser_primary_expression (cp_parser
*parser
,
5639 bool template_arg_p
,
5642 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5643 /*decltype*/false, idk
);
5646 /* Parse an id-expression.
5653 :: [opt] nested-name-specifier template [opt] unqualified-id
5655 :: operator-function-id
5658 Return a representation of the unqualified portion of the
5659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5660 a `::' or nested-name-specifier.
5662 Often, if the id-expression was a qualified-id, the caller will
5663 want to make a SCOPE_REF to represent the qualified-id. This
5664 function does not do this in order to avoid wastefully creating
5665 SCOPE_REFs when they are not required.
5667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5671 uninstantiated templates.
5673 If *TEMPLATE_P is non-NULL, it is set to true iff the
5674 `template' keyword is used to explicitly indicate that the entity
5675 named is a template.
5677 If DECLARATOR_P is true, the id-expression is appearing as part of
5678 a declarator, rather than as part of an expression. */
5681 cp_parser_id_expression (cp_parser
*parser
,
5682 bool template_keyword_p
,
5683 bool check_dependency_p
,
5688 bool global_scope_p
;
5689 bool nested_name_specifier_p
;
5691 /* Assume the `template' keyword was not used. */
5693 *template_p
= template_keyword_p
;
5695 /* Look for the optional `::' operator. */
5697 = (!template_keyword_p
5698 && (cp_parser_global_scope_opt (parser
,
5699 /*current_scope_valid_p=*/false)
5702 /* Look for the optional nested-name-specifier. */
5703 nested_name_specifier_p
5704 = (cp_parser_nested_name_specifier_opt (parser
,
5705 /*typename_keyword_p=*/false,
5712 /* If there is a nested-name-specifier, then we are looking at
5713 the first qualified-id production. */
5714 if (nested_name_specifier_p
)
5717 tree saved_object_scope
;
5718 tree saved_qualifying_scope
;
5719 cp_expr unqualified_id
;
5722 /* See if the next token is the `template' keyword. */
5724 template_p
= &is_template
;
5725 *template_p
= cp_parser_optional_template_keyword (parser
);
5726 /* Name lookup we do during the processing of the
5727 unqualified-id might obliterate SCOPE. */
5728 saved_scope
= parser
->scope
;
5729 saved_object_scope
= parser
->object_scope
;
5730 saved_qualifying_scope
= parser
->qualifying_scope
;
5731 /* Process the final unqualified-id. */
5732 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5735 /*optional_p=*/false);
5736 /* Restore the SAVED_SCOPE for our caller. */
5737 parser
->scope
= saved_scope
;
5738 parser
->object_scope
= saved_object_scope
;
5739 parser
->qualifying_scope
= saved_qualifying_scope
;
5741 return unqualified_id
;
5743 /* Otherwise, if we are in global scope, then we are looking at one
5744 of the other qualified-id productions. */
5745 else if (global_scope_p
)
5750 /* Peek at the next token. */
5751 token
= cp_lexer_peek_token (parser
->lexer
);
5753 /* If it's an identifier, and the next token is not a "<", then
5754 we can avoid the template-id case. This is an optimization
5755 for this common case. */
5756 if (token
->type
== CPP_NAME
5757 && !cp_parser_nth_token_starts_template_argument_list_p
5759 return cp_parser_identifier (parser
);
5761 cp_parser_parse_tentatively (parser
);
5762 /* Try a template-id. */
5763 id
= cp_parser_template_id (parser
,
5764 /*template_keyword_p=*/false,
5765 /*check_dependency_p=*/true,
5768 /* If that worked, we're done. */
5769 if (cp_parser_parse_definitely (parser
))
5772 /* Peek at the next token. (Changes in the token buffer may
5773 have invalidated the pointer obtained above.) */
5774 token
= cp_lexer_peek_token (parser
->lexer
);
5776 switch (token
->type
)
5779 return cp_parser_identifier (parser
);
5782 if (token
->keyword
== RID_OPERATOR
)
5783 return cp_parser_operator_function_id (parser
);
5787 cp_parser_error (parser
, "expected id-expression");
5788 return error_mark_node
;
5792 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5793 /*check_dependency_p=*/true,
5798 /* Parse an unqualified-id.
5802 operator-function-id
5803 conversion-function-id
5807 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5808 keyword, in a construct like `A::template ...'.
5810 Returns a representation of unqualified-id. For the `identifier'
5811 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5812 production a BIT_NOT_EXPR is returned; the operand of the
5813 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5814 other productions, see the documentation accompanying the
5815 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5816 names are looked up in uninstantiated templates. If DECLARATOR_P
5817 is true, the unqualified-id is appearing as part of a declarator,
5818 rather than as part of an expression. */
5821 cp_parser_unqualified_id (cp_parser
* parser
,
5822 bool template_keyword_p
,
5823 bool check_dependency_p
,
5829 /* Peek at the next token. */
5830 token
= cp_lexer_peek_token (parser
->lexer
);
5832 switch ((int) token
->type
)
5838 /* We don't know yet whether or not this will be a
5840 cp_parser_parse_tentatively (parser
);
5841 /* Try a template-id. */
5842 id
= cp_parser_template_id (parser
, template_keyword_p
,
5846 /* If it worked, we're done. */
5847 if (cp_parser_parse_definitely (parser
))
5849 /* Otherwise, it's an ordinary identifier. */
5850 return cp_parser_identifier (parser
);
5853 case CPP_TEMPLATE_ID
:
5854 return cp_parser_template_id (parser
, template_keyword_p
,
5862 tree qualifying_scope
;
5867 /* Consume the `~' token. */
5868 cp_lexer_consume_token (parser
->lexer
);
5869 /* Parse the class-name. The standard, as written, seems to
5872 template <typename T> struct S { ~S (); };
5873 template <typename T> S<T>::~S() {}
5875 is invalid, since `~' must be followed by a class-name, but
5876 `S<T>' is dependent, and so not known to be a class.
5877 That's not right; we need to look in uninstantiated
5878 templates. A further complication arises from:
5880 template <typename T> void f(T t) {
5884 Here, it is not possible to look up `T' in the scope of `T'
5885 itself. We must look in both the current scope, and the
5886 scope of the containing complete expression.
5888 Yet another issue is:
5897 The standard does not seem to say that the `S' in `~S'
5898 should refer to the type `S' and not the data member
5901 /* DR 244 says that we look up the name after the "~" in the
5902 same scope as we looked up the qualifying name. That idea
5903 isn't fully worked out; it's more complicated than that. */
5904 scope
= parser
->scope
;
5905 object_scope
= parser
->object_scope
;
5906 qualifying_scope
= parser
->qualifying_scope
;
5908 /* Check for invalid scopes. */
5909 if (scope
== error_mark_node
)
5911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5912 cp_lexer_consume_token (parser
->lexer
);
5913 return error_mark_node
;
5915 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5917 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5918 error_at (token
->location
,
5919 "scope %qT before %<~%> is not a class-name",
5921 cp_parser_simulate_error (parser
);
5922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5923 cp_lexer_consume_token (parser
->lexer
);
5924 return error_mark_node
;
5926 gcc_assert (!scope
|| TYPE_P (scope
));
5928 /* If the name is of the form "X::~X" it's OK even if X is a
5930 token
= cp_lexer_peek_token (parser
->lexer
);
5932 && token
->type
== CPP_NAME
5933 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5935 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5936 || (CLASS_TYPE_P (scope
)
5937 && constructor_name_p (token
->u
.value
, scope
))))
5939 cp_lexer_consume_token (parser
->lexer
);
5940 return build_nt (BIT_NOT_EXPR
, scope
);
5943 /* ~auto means the destructor of whatever the object is. */
5944 if (cp_parser_is_keyword (token
, RID_AUTO
))
5946 if (cxx_dialect
< cxx14
)
5947 pedwarn (input_location
, 0,
5948 "%<~auto%> only available with "
5949 "-std=c++14 or -std=gnu++14");
5950 cp_lexer_consume_token (parser
->lexer
);
5951 return build_nt (BIT_NOT_EXPR
, make_auto ());
5954 /* If there was an explicit qualification (S::~T), first look
5955 in the scope given by the qualification (i.e., S).
5957 Note: in the calls to cp_parser_class_name below we pass
5958 typename_type so that lookup finds the injected-class-name
5959 rather than the constructor. */
5961 type_decl
= NULL_TREE
;
5964 cp_parser_parse_tentatively (parser
);
5965 type_decl
= cp_parser_class_name (parser
,
5966 /*typename_keyword_p=*/false,
5967 /*template_keyword_p=*/false,
5969 /*check_dependency=*/false,
5970 /*class_head_p=*/false,
5972 if (cp_parser_parse_definitely (parser
))
5975 /* In "N::S::~S", look in "N" as well. */
5976 if (!done
&& scope
&& qualifying_scope
)
5978 cp_parser_parse_tentatively (parser
);
5979 parser
->scope
= qualifying_scope
;
5980 parser
->object_scope
= NULL_TREE
;
5981 parser
->qualifying_scope
= NULL_TREE
;
5983 = cp_parser_class_name (parser
,
5984 /*typename_keyword_p=*/false,
5985 /*template_keyword_p=*/false,
5987 /*check_dependency=*/false,
5988 /*class_head_p=*/false,
5990 if (cp_parser_parse_definitely (parser
))
5993 /* In "p->S::~T", look in the scope given by "*p" as well. */
5994 else if (!done
&& object_scope
)
5996 cp_parser_parse_tentatively (parser
);
5997 parser
->scope
= object_scope
;
5998 parser
->object_scope
= NULL_TREE
;
5999 parser
->qualifying_scope
= NULL_TREE
;
6001 = cp_parser_class_name (parser
,
6002 /*typename_keyword_p=*/false,
6003 /*template_keyword_p=*/false,
6005 /*check_dependency=*/false,
6006 /*class_head_p=*/false,
6008 if (cp_parser_parse_definitely (parser
))
6011 /* Look in the surrounding context. */
6014 parser
->scope
= NULL_TREE
;
6015 parser
->object_scope
= NULL_TREE
;
6016 parser
->qualifying_scope
= NULL_TREE
;
6017 if (processing_template_decl
)
6018 cp_parser_parse_tentatively (parser
);
6020 = cp_parser_class_name (parser
,
6021 /*typename_keyword_p=*/false,
6022 /*template_keyword_p=*/false,
6024 /*check_dependency=*/false,
6025 /*class_head_p=*/false,
6027 if (processing_template_decl
6028 && ! cp_parser_parse_definitely (parser
))
6030 /* We couldn't find a type with this name. If we're parsing
6031 tentatively, fail and try something else. */
6032 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6034 cp_parser_simulate_error (parser
);
6035 return error_mark_node
;
6037 /* Otherwise, accept it and check for a match at instantiation
6039 type_decl
= cp_parser_identifier (parser
);
6040 if (type_decl
!= error_mark_node
)
6041 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6045 /* If an error occurred, assume that the name of the
6046 destructor is the same as the name of the qualifying
6047 class. That allows us to keep parsing after running
6048 into ill-formed destructor names. */
6049 if (type_decl
== error_mark_node
&& scope
)
6050 return build_nt (BIT_NOT_EXPR
, scope
);
6051 else if (type_decl
== error_mark_node
)
6052 return error_mark_node
;
6054 /* Check that destructor name and scope match. */
6055 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6057 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6058 error_at (token
->location
,
6059 "declaration of %<~%T%> as member of %qT",
6061 cp_parser_simulate_error (parser
);
6062 return error_mark_node
;
6067 A typedef-name that names a class shall not be used as the
6068 identifier in the declarator for a destructor declaration. */
6070 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6071 && !DECL_SELF_REFERENCE_P (type_decl
)
6072 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6073 error_at (token
->location
,
6074 "typedef-name %qD used as destructor declarator",
6077 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6081 if (token
->keyword
== RID_OPERATOR
)
6085 /* This could be a template-id, so we try that first. */
6086 cp_parser_parse_tentatively (parser
);
6087 /* Try a template-id. */
6088 id
= cp_parser_template_id (parser
, template_keyword_p
,
6089 /*check_dependency_p=*/true,
6092 /* If that worked, we're done. */
6093 if (cp_parser_parse_definitely (parser
))
6095 /* We still don't know whether we're looking at an
6096 operator-function-id or a conversion-function-id. */
6097 cp_parser_parse_tentatively (parser
);
6098 /* Try an operator-function-id. */
6099 id
= cp_parser_operator_function_id (parser
);
6100 /* If that didn't work, try a conversion-function-id. */
6101 if (!cp_parser_parse_definitely (parser
))
6102 id
= cp_parser_conversion_function_id (parser
);
6111 cp_parser_error (parser
, "expected unqualified-id");
6112 return error_mark_node
;
6116 /* Parse an (optional) nested-name-specifier.
6118 nested-name-specifier: [C++98]
6119 class-or-namespace-name :: nested-name-specifier [opt]
6120 class-or-namespace-name :: template nested-name-specifier [opt]
6122 nested-name-specifier: [C++0x]
6125 nested-name-specifier identifier ::
6126 nested-name-specifier template [opt] simple-template-id ::
6128 PARSER->SCOPE should be set appropriately before this function is
6129 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6130 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6133 Sets PARSER->SCOPE to the class (TYPE) or namespace
6134 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6135 it unchanged if there is no nested-name-specifier. Returns the new
6136 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6138 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6139 part of a declaration and/or decl-specifier. */
6142 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6143 bool typename_keyword_p
,
6144 bool check_dependency_p
,
6146 bool is_declaration
,
6147 bool template_keyword_p
/* = false */)
6149 bool success
= false;
6150 cp_token_position start
= 0;
6153 /* Remember where the nested-name-specifier starts. */
6154 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6156 start
= cp_lexer_token_position (parser
->lexer
, false);
6157 push_deferring_access_checks (dk_deferred
);
6164 tree saved_qualifying_scope
;
6166 /* Spot cases that cannot be the beginning of a
6167 nested-name-specifier. */
6168 token
= cp_lexer_peek_token (parser
->lexer
);
6170 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6171 the already parsed nested-name-specifier. */
6172 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6174 /* Grab the nested-name-specifier and continue the loop. */
6175 cp_parser_pre_parsed_nested_name_specifier (parser
);
6176 /* If we originally encountered this nested-name-specifier
6177 with IS_DECLARATION set to false, we will not have
6178 resolved TYPENAME_TYPEs, so we must do so here. */
6180 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6182 new_scope
= resolve_typename_type (parser
->scope
,
6183 /*only_current_p=*/false);
6184 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6185 parser
->scope
= new_scope
;
6191 /* Spot cases that cannot be the beginning of a
6192 nested-name-specifier. On the second and subsequent times
6193 through the loop, we look for the `template' keyword. */
6194 if (success
&& token
->keyword
== RID_TEMPLATE
)
6196 /* A template-id can start a nested-name-specifier. */
6197 else if (token
->type
== CPP_TEMPLATE_ID
)
6199 /* DR 743: decltype can be used in a nested-name-specifier. */
6200 else if (token_is_decltype (token
))
6204 /* If the next token is not an identifier, then it is
6205 definitely not a type-name or namespace-name. */
6206 if (token
->type
!= CPP_NAME
)
6208 /* If the following token is neither a `<' (to begin a
6209 template-id), nor a `::', then we are not looking at a
6210 nested-name-specifier. */
6211 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6213 if (token
->type
== CPP_COLON
6214 && parser
->colon_corrects_to_scope_p
6215 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6217 gcc_rich_location
richloc (token
->location
);
6218 richloc
.add_fixit_replace ("::");
6220 "found %<:%> in nested-name-specifier, "
6222 token
->type
= CPP_SCOPE
;
6225 if (token
->type
!= CPP_SCOPE
6226 && !cp_parser_nth_token_starts_template_argument_list_p
6231 /* The nested-name-specifier is optional, so we parse
6233 cp_parser_parse_tentatively (parser
);
6235 /* Look for the optional `template' keyword, if this isn't the
6236 first time through the loop. */
6238 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6240 /* Save the old scope since the name lookup we are about to do
6241 might destroy it. */
6242 old_scope
= parser
->scope
;
6243 saved_qualifying_scope
= parser
->qualifying_scope
;
6244 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6245 look up names in "X<T>::I" in order to determine that "Y" is
6246 a template. So, if we have a typename at this point, we make
6247 an effort to look through it. */
6249 && !typename_keyword_p
6251 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6252 parser
->scope
= resolve_typename_type (parser
->scope
,
6253 /*only_current_p=*/false);
6254 /* Parse the qualifying entity. */
6256 = cp_parser_qualifying_entity (parser
,
6262 /* Look for the `::' token. */
6263 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6265 /* If we found what we wanted, we keep going; otherwise, we're
6267 if (!cp_parser_parse_definitely (parser
))
6269 bool error_p
= false;
6271 /* Restore the OLD_SCOPE since it was valid before the
6272 failed attempt at finding the last
6273 class-or-namespace-name. */
6274 parser
->scope
= old_scope
;
6275 parser
->qualifying_scope
= saved_qualifying_scope
;
6277 /* If the next token is a decltype, and the one after that is a
6278 `::', then the decltype has failed to resolve to a class or
6279 enumeration type. Give this error even when parsing
6280 tentatively since it can't possibly be valid--and we're going
6281 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6282 won't get another chance.*/
6283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6284 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6287 token
= cp_lexer_consume_token (parser
->lexer
);
6288 error_at (token
->location
, "decltype evaluates to %qT, "
6289 "which is not a class or enumeration type",
6290 token
->u
.tree_check_value
->value
);
6291 parser
->scope
= error_mark_node
;
6295 cp_lexer_consume_token (parser
->lexer
);
6298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6299 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6301 /* If we have a non-type template-id followed by ::, it can't
6302 possibly be valid. */
6303 token
= cp_lexer_peek_token (parser
->lexer
);
6304 tree tid
= token
->u
.tree_check_value
->value
;
6305 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6306 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6308 tree tmpl
= NULL_TREE
;
6309 if (is_overloaded_fn (tid
))
6311 tree fns
= get_fns (tid
);
6312 if (OVL_SINGLE_P (fns
))
6313 tmpl
= OVL_FIRST (fns
);
6314 error_at (token
->location
, "function template-id %qD "
6315 "in nested-name-specifier", tid
);
6319 /* Variable template. */
6320 tmpl
= TREE_OPERAND (tid
, 0);
6321 gcc_assert (variable_template_p (tmpl
));
6322 error_at (token
->location
, "variable template-id %qD "
6323 "in nested-name-specifier", tid
);
6326 inform (DECL_SOURCE_LOCATION (tmpl
),
6327 "%qD declared here", tmpl
);
6329 parser
->scope
= error_mark_node
;
6333 cp_lexer_consume_token (parser
->lexer
);
6334 cp_lexer_consume_token (parser
->lexer
);
6338 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6340 /* If the next token is an identifier, and the one after
6341 that is a `::', then any valid interpretation would have
6342 found a class-or-namespace-name. */
6343 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6344 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6346 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6349 token
= cp_lexer_consume_token (parser
->lexer
);
6352 if (!token
->error_reported
)
6355 tree ambiguous_decls
;
6357 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6359 /*is_template=*/false,
6360 /*is_namespace=*/false,
6361 /*check_dependency=*/true,
6364 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6365 error_at (token
->location
,
6366 "%qD used without template parameters",
6368 else if (ambiguous_decls
)
6370 // cp_parser_lookup_name has the same diagnostic,
6371 // thus make sure to emit it at most once.
6372 if (cp_parser_uncommitted_to_tentative_parse_p
6375 error_at (token
->location
,
6376 "reference to %qD is ambiguous",
6378 print_candidates (ambiguous_decls
);
6380 decl
= error_mark_node
;
6384 if (cxx_dialect
!= cxx98
)
6385 cp_parser_name_lookup_error
6386 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6389 cp_parser_name_lookup_error
6390 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6394 parser
->scope
= error_mark_node
;
6396 /* Treat this as a successful nested-name-specifier
6401 If the name found is not a class-name (clause
6402 _class_) or namespace-name (_namespace.def_), the
6403 program is ill-formed. */
6406 cp_lexer_consume_token (parser
->lexer
);
6410 /* We've found one valid nested-name-specifier. */
6412 /* Name lookup always gives us a DECL. */
6413 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6414 new_scope
= TREE_TYPE (new_scope
);
6415 /* Uses of "template" must be followed by actual templates. */
6416 if (template_keyword_p
6417 && !(CLASS_TYPE_P (new_scope
)
6418 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6419 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6420 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6421 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6422 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6423 == TEMPLATE_ID_EXPR
)))
6424 permerror (input_location
, TYPE_P (new_scope
)
6425 ? G_("%qT is not a template")
6426 : G_("%qD is not a template"),
6428 /* If it is a class scope, try to complete it; we are about to
6429 be looking up names inside the class. */
6430 if (TYPE_P (new_scope
)
6431 /* Since checking types for dependency can be expensive,
6432 avoid doing it if the type is already complete. */
6433 && !COMPLETE_TYPE_P (new_scope
)
6434 /* Do not try to complete dependent types. */
6435 && !dependent_type_p (new_scope
))
6437 new_scope
= complete_type (new_scope
);
6438 /* If it is a typedef to current class, use the current
6439 class instead, as the typedef won't have any names inside
6441 if (!COMPLETE_TYPE_P (new_scope
)
6442 && currently_open_class (new_scope
))
6443 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6445 /* Make sure we look in the right scope the next time through
6447 parser
->scope
= new_scope
;
6450 /* If parsing tentatively, replace the sequence of tokens that makes
6451 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6452 token. That way, should we re-parse the token stream, we will
6453 not have to repeat the effort required to do the parse, nor will
6454 we issue duplicate error messages. */
6455 if (success
&& start
)
6459 token
= cp_lexer_token_at (parser
->lexer
, start
);
6460 /* Reset the contents of the START token. */
6461 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6462 /* Retrieve any deferred checks. Do not pop this access checks yet
6463 so the memory will not be reclaimed during token replacing below. */
6464 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6465 token
->u
.tree_check_value
->value
= parser
->scope
;
6466 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6467 token
->u
.tree_check_value
->qualifying_scope
=
6468 parser
->qualifying_scope
;
6469 token
->keyword
= RID_MAX
;
6471 /* Purge all subsequent tokens. */
6472 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6476 pop_to_parent_deferring_access_checks ();
6478 return success
? parser
->scope
: NULL_TREE
;
6481 /* Parse a nested-name-specifier. See
6482 cp_parser_nested_name_specifier_opt for details. This function
6483 behaves identically, except that it will an issue an error if no
6484 nested-name-specifier is present. */
6487 cp_parser_nested_name_specifier (cp_parser
*parser
,
6488 bool typename_keyword_p
,
6489 bool check_dependency_p
,
6491 bool is_declaration
)
6495 /* Look for the nested-name-specifier. */
6496 scope
= cp_parser_nested_name_specifier_opt (parser
,
6501 /* If it was not present, issue an error message. */
6504 cp_parser_error (parser
, "expected nested-name-specifier");
6505 parser
->scope
= NULL_TREE
;
6511 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6512 this is either a class-name or a namespace-name (which corresponds
6513 to the class-or-namespace-name production in the grammar). For
6514 C++0x, it can also be a type-name that refers to an enumeration
6515 type or a simple-template-id.
6517 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6518 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6519 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6520 TYPE_P is TRUE iff the next name should be taken as a class-name,
6521 even the same name is declared to be another entity in the same
6524 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6525 specified by the class-or-namespace-name. If neither is found the
6526 ERROR_MARK_NODE is returned. */
6529 cp_parser_qualifying_entity (cp_parser
*parser
,
6530 bool typename_keyword_p
,
6531 bool template_keyword_p
,
6532 bool check_dependency_p
,
6534 bool is_declaration
)
6537 tree saved_qualifying_scope
;
6538 tree saved_object_scope
;
6541 bool successful_parse_p
;
6543 /* DR 743: decltype can appear in a nested-name-specifier. */
6544 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6546 scope
= cp_parser_decltype (parser
);
6547 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6548 && !MAYBE_CLASS_TYPE_P (scope
))
6550 cp_parser_simulate_error (parser
);
6551 return error_mark_node
;
6553 if (TYPE_NAME (scope
))
6554 scope
= TYPE_NAME (scope
);
6558 /* Before we try to parse the class-name, we must save away the
6559 current PARSER->SCOPE since cp_parser_class_name will destroy
6561 saved_scope
= parser
->scope
;
6562 saved_qualifying_scope
= parser
->qualifying_scope
;
6563 saved_object_scope
= parser
->object_scope
;
6564 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6565 there is no need to look for a namespace-name. */
6566 only_class_p
= template_keyword_p
6567 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6569 cp_parser_parse_tentatively (parser
);
6570 scope
= cp_parser_class_name (parser
,
6573 type_p
? class_type
: none_type
,
6575 /*class_head_p=*/false,
6577 /*enum_ok=*/cxx_dialect
> cxx98
);
6578 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6579 /* If that didn't work, try for a namespace-name. */
6580 if (!only_class_p
&& !successful_parse_p
)
6582 /* Restore the saved scope. */
6583 parser
->scope
= saved_scope
;
6584 parser
->qualifying_scope
= saved_qualifying_scope
;
6585 parser
->object_scope
= saved_object_scope
;
6586 /* If we are not looking at an identifier followed by the scope
6587 resolution operator, then this is not part of a
6588 nested-name-specifier. (Note that this function is only used
6589 to parse the components of a nested-name-specifier.) */
6590 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6591 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6592 return error_mark_node
;
6593 scope
= cp_parser_namespace_name (parser
);
6599 /* Return true if we are looking at a compound-literal, false otherwise. */
6602 cp_parser_compound_literal_p (cp_parser
*parser
)
6604 cp_lexer_save_tokens (parser
->lexer
);
6606 /* Skip tokens until the next token is a closing parenthesis.
6607 If we find the closing `)', and the next token is a `{', then
6608 we are looking at a compound-literal. */
6609 bool compound_literal_p
6610 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6611 /*consume_paren=*/true)
6612 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6614 /* Roll back the tokens we skipped. */
6615 cp_lexer_rollback_tokens (parser
->lexer
);
6617 return compound_literal_p
;
6620 /* Return true if EXPR is the integer constant zero or a complex constant
6621 of zero, without any folding, but ignoring location wrappers. */
6624 literal_integer_zerop (const_tree expr
)
6626 STRIP_ANY_LOCATION_WRAPPER (expr
);
6627 return integer_zerop (expr
);
6630 /* Parse a postfix-expression.
6634 postfix-expression [ expression ]
6635 postfix-expression ( expression-list [opt] )
6636 simple-type-specifier ( expression-list [opt] )
6637 typename :: [opt] nested-name-specifier identifier
6638 ( expression-list [opt] )
6639 typename :: [opt] nested-name-specifier template [opt] template-id
6640 ( expression-list [opt] )
6641 postfix-expression . template [opt] id-expression
6642 postfix-expression -> template [opt] id-expression
6643 postfix-expression . pseudo-destructor-name
6644 postfix-expression -> pseudo-destructor-name
6645 postfix-expression ++
6646 postfix-expression --
6647 dynamic_cast < type-id > ( expression )
6648 static_cast < type-id > ( expression )
6649 reinterpret_cast < type-id > ( expression )
6650 const_cast < type-id > ( expression )
6651 typeid ( expression )
6657 ( type-id ) { initializer-list , [opt] }
6659 This extension is a GNU version of the C99 compound-literal
6660 construct. (The C99 grammar uses `type-name' instead of `type-id',
6661 but they are essentially the same concept.)
6663 If ADDRESS_P is true, the postfix expression is the operand of the
6664 `&' operator. CAST_P is true if this expression is the target of a
6667 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6668 class member access expressions [expr.ref].
6670 Returns a representation of the expression. */
6673 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6674 bool member_access_only_p
, bool decltype_p
,
6675 cp_id_kind
* pidk_return
)
6680 cp_id_kind idk
= CP_ID_KIND_NONE
;
6681 cp_expr postfix_expression
= NULL_TREE
;
6682 bool is_member_access
= false;
6684 /* Peek at the next token. */
6685 token
= cp_lexer_peek_token (parser
->lexer
);
6686 loc
= token
->location
;
6687 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6689 /* Some of the productions are determined by keywords. */
6690 keyword
= token
->keyword
;
6700 const char *saved_message
;
6701 bool saved_in_type_id_in_expr_p
;
6703 /* All of these can be handled in the same way from the point
6704 of view of parsing. Begin by consuming the token
6705 identifying the cast. */
6706 cp_lexer_consume_token (parser
->lexer
);
6708 /* New types cannot be defined in the cast. */
6709 saved_message
= parser
->type_definition_forbidden_message
;
6710 parser
->type_definition_forbidden_message
6711 = G_("types may not be defined in casts");
6713 /* Look for the opening `<'. */
6714 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6715 /* Parse the type to which we are casting. */
6716 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6717 parser
->in_type_id_in_expr_p
= true;
6718 type
= cp_parser_type_id (parser
);
6719 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6720 /* Look for the closing `>'. */
6721 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6722 /* Restore the old message. */
6723 parser
->type_definition_forbidden_message
= saved_message
;
6725 bool saved_greater_than_is_operator_p
6726 = parser
->greater_than_is_operator_p
;
6727 parser
->greater_than_is_operator_p
= true;
6729 /* And the expression which is being cast. */
6730 matching_parens parens
;
6731 parens
.require_open (parser
);
6732 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6733 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6735 location_t end_loc
= close_paren
?
6736 close_paren
->location
: UNKNOWN_LOCATION
;
6738 parser
->greater_than_is_operator_p
6739 = saved_greater_than_is_operator_p
;
6741 /* Only type conversions to integral or enumeration types
6742 can be used in constant-expressions. */
6743 if (!cast_valid_in_integral_constant_expression_p (type
)
6744 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6746 postfix_expression
= error_mark_node
;
6754 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6758 = build_static_cast (type
, expression
, tf_warning_or_error
);
6762 = build_reinterpret_cast (type
, expression
,
6763 tf_warning_or_error
);
6767 = build_const_cast (type
, expression
, tf_warning_or_error
);
6773 /* Construct a location e.g. :
6774 reinterpret_cast <int *> (expr)
6775 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6776 ranging from the start of the "*_cast" token to the final closing
6777 paren, with the caret at the start. */
6778 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6779 postfix_expression
.set_location (cp_cast_loc
);
6786 const char *saved_message
;
6787 bool saved_in_type_id_in_expr_p
;
6789 /* Consume the `typeid' token. */
6790 cp_lexer_consume_token (parser
->lexer
);
6791 /* Look for the `(' token. */
6792 matching_parens parens
;
6793 parens
.require_open (parser
);
6794 /* Types cannot be defined in a `typeid' expression. */
6795 saved_message
= parser
->type_definition_forbidden_message
;
6796 parser
->type_definition_forbidden_message
6797 = G_("types may not be defined in a %<typeid%> expression");
6798 /* We can't be sure yet whether we're looking at a type-id or an
6800 cp_parser_parse_tentatively (parser
);
6801 /* Try a type-id first. */
6802 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6803 parser
->in_type_id_in_expr_p
= true;
6804 type
= cp_parser_type_id (parser
);
6805 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6806 /* Look for the `)' token. Otherwise, we can't be sure that
6807 we're not looking at an expression: consider `typeid (int
6808 (3))', for example. */
6809 cp_token
*close_paren
= parens
.require_close (parser
);
6810 /* If all went well, simply lookup the type-id. */
6811 if (cp_parser_parse_definitely (parser
))
6812 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6813 /* Otherwise, fall back to the expression variant. */
6818 /* Look for an expression. */
6819 expression
= cp_parser_expression (parser
, & idk
);
6820 /* Compute its typeid. */
6821 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6822 /* Look for the `)' token. */
6823 close_paren
= parens
.require_close (parser
);
6825 /* Restore the saved message. */
6826 parser
->type_definition_forbidden_message
= saved_message
;
6827 /* `typeid' may not appear in an integral constant expression. */
6828 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6829 postfix_expression
= error_mark_node
;
6831 /* Construct a location e.g. :
6834 ranging from the start of the "typeid" token to the final closing
6835 paren, with the caret at the start. */
6838 location_t typeid_loc
6839 = make_location (start_loc
, start_loc
, close_paren
->location
);
6840 postfix_expression
.set_location (typeid_loc
);
6841 postfix_expression
.maybe_add_location_wrapper ();
6849 /* The syntax permitted here is the same permitted for an
6850 elaborated-type-specifier. */
6851 ++parser
->prevent_constrained_type_specifiers
;
6852 type
= cp_parser_elaborated_type_specifier (parser
,
6853 /*is_friend=*/false,
6854 /*is_declaration=*/false);
6855 --parser
->prevent_constrained_type_specifiers
;
6856 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6861 case RID_BUILTIN_SHUFFLE
:
6862 case RID_BUILTIN_LAUNDER
:
6864 vec
<tree
, va_gc
> *vec
;
6868 cp_lexer_consume_token (parser
->lexer
);
6869 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6870 /*cast_p=*/false, /*allow_expansion_p=*/true,
6871 /*non_constant_p=*/NULL
);
6874 postfix_expression
= error_mark_node
;
6878 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6884 if (vec
->length () == 1)
6886 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6889 error_at (loc
, "wrong number of arguments to "
6890 "%<__builtin_addressof%>");
6891 postfix_expression
= error_mark_node
;
6895 case RID_BUILTIN_LAUNDER
:
6896 if (vec
->length () == 1)
6897 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6898 tf_warning_or_error
);
6901 error_at (loc
, "wrong number of arguments to "
6902 "%<__builtin_launder%>");
6903 postfix_expression
= error_mark_node
;
6907 case RID_BUILTIN_SHUFFLE
:
6908 if (vec
->length () == 2)
6910 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6911 (*vec
)[1], tf_warning_or_error
);
6912 else if (vec
->length () == 3)
6914 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6915 (*vec
)[2], tf_warning_or_error
);
6918 error_at (loc
, "wrong number of arguments to "
6919 "%<__builtin_shuffle%>");
6920 postfix_expression
= error_mark_node
;
6934 /* If the next thing is a simple-type-specifier, we may be
6935 looking at a functional cast. We could also be looking at
6936 an id-expression. So, we try the functional cast, and if
6937 that doesn't work we fall back to the primary-expression. */
6938 cp_parser_parse_tentatively (parser
);
6939 /* Look for the simple-type-specifier. */
6940 ++parser
->prevent_constrained_type_specifiers
;
6941 type
= cp_parser_simple_type_specifier (parser
,
6942 /*decl_specs=*/NULL
,
6943 CP_PARSER_FLAGS_NONE
);
6944 --parser
->prevent_constrained_type_specifiers
;
6945 /* Parse the cast itself. */
6946 if (!cp_parser_error_occurred (parser
))
6948 = cp_parser_functional_cast (parser
, type
);
6949 /* If that worked, we're done. */
6950 if (cp_parser_parse_definitely (parser
))
6953 /* If the functional-cast didn't work out, try a
6954 compound-literal. */
6955 if (cp_parser_allow_gnu_extensions_p (parser
)
6956 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6958 cp_expr initializer
= NULL_TREE
;
6960 cp_parser_parse_tentatively (parser
);
6962 matching_parens parens
;
6963 parens
.consume_open (parser
);
6965 /* Avoid calling cp_parser_type_id pointlessly, see comment
6966 in cp_parser_cast_expression about c++/29234. */
6967 if (!cp_parser_compound_literal_p (parser
))
6968 cp_parser_simulate_error (parser
);
6971 /* Parse the type. */
6972 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6973 parser
->in_type_id_in_expr_p
= true;
6974 type
= cp_parser_type_id (parser
);
6975 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6976 parens
.require_close (parser
);
6979 /* If things aren't going well, there's no need to
6981 if (!cp_parser_error_occurred (parser
))
6983 bool non_constant_p
;
6984 /* Parse the brace-enclosed initializer list. */
6985 initializer
= cp_parser_braced_list (parser
,
6988 /* If that worked, we're definitely looking at a
6989 compound-literal expression. */
6990 if (cp_parser_parse_definitely (parser
))
6992 /* Warn the user that a compound literal is not
6993 allowed in standard C++. */
6994 pedwarn (input_location
, OPT_Wpedantic
,
6995 "ISO C++ forbids compound-literals");
6996 /* For simplicity, we disallow compound literals in
6997 constant-expressions. We could
6998 allow compound literals of integer type, whose
6999 initializer was a constant, in constant
7000 expressions. Permitting that usage, as a further
7001 extension, would not change the meaning of any
7002 currently accepted programs. (Of course, as
7003 compound literals are not part of ISO C++, the
7004 standard has nothing to say.) */
7005 if (cp_parser_non_integral_constant_expression (parser
,
7008 postfix_expression
= error_mark_node
;
7011 /* Form the representation of the compound-literal. */
7013 = finish_compound_literal (type
, initializer
,
7014 tf_warning_or_error
, fcl_c99
);
7015 postfix_expression
.set_location (initializer
.get_location ());
7020 /* It must be a primary-expression. */
7022 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7023 /*template_arg_p=*/false,
7030 /* Note that we don't need to worry about calling build_cplus_new on a
7031 class-valued CALL_EXPR in decltype when it isn't the end of the
7032 postfix-expression; unary_complex_lvalue will take care of that for
7035 /* Keep looping until the postfix-expression is complete. */
7038 if (idk
== CP_ID_KIND_UNQUALIFIED
7039 && identifier_p (postfix_expression
)
7040 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7041 /* It is not a Koenig lookup function call. */
7043 = unqualified_name_lookup_error (postfix_expression
);
7045 /* Peek at the next token. */
7046 token
= cp_lexer_peek_token (parser
->lexer
);
7048 switch (token
->type
)
7050 case CPP_OPEN_SQUARE
:
7051 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7053 cp_parser_error (parser
,
7054 "two consecutive %<[%> shall "
7055 "only introduce an attribute");
7056 return error_mark_node
;
7059 = cp_parser_postfix_open_square_expression (parser
,
7063 postfix_expression
.set_range (start_loc
,
7064 postfix_expression
.get_location ());
7066 idk
= CP_ID_KIND_NONE
;
7067 is_member_access
= false;
7070 case CPP_OPEN_PAREN
:
7071 /* postfix-expression ( expression-list [opt] ) */
7074 bool is_builtin_constant_p
;
7075 bool saved_integral_constant_expression_p
= false;
7076 bool saved_non_integral_constant_expression_p
= false;
7077 tsubst_flags_t complain
= complain_flags (decltype_p
);
7078 vec
<tree
, va_gc
> *args
;
7079 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7081 is_member_access
= false;
7083 is_builtin_constant_p
7084 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7085 if (is_builtin_constant_p
)
7087 /* The whole point of __builtin_constant_p is to allow
7088 non-constant expressions to appear as arguments. */
7089 saved_integral_constant_expression_p
7090 = parser
->integral_constant_expression_p
;
7091 saved_non_integral_constant_expression_p
7092 = parser
->non_integral_constant_expression_p
;
7093 parser
->integral_constant_expression_p
= false;
7095 args
= (cp_parser_parenthesized_expression_list
7097 /*cast_p=*/false, /*allow_expansion_p=*/true,
7098 /*non_constant_p=*/NULL
,
7099 /*close_paren_loc=*/&close_paren_loc
,
7100 /*wrap_locations_p=*/true));
7101 if (is_builtin_constant_p
)
7103 parser
->integral_constant_expression_p
7104 = saved_integral_constant_expression_p
;
7105 parser
->non_integral_constant_expression_p
7106 = saved_non_integral_constant_expression_p
;
7111 postfix_expression
= error_mark_node
;
7115 /* Function calls are not permitted in
7116 constant-expressions. */
7117 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7118 && cp_parser_non_integral_constant_expression (parser
,
7121 postfix_expression
= error_mark_node
;
7122 release_tree_vector (args
);
7127 if (idk
== CP_ID_KIND_UNQUALIFIED
7128 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7130 if (identifier_p (postfix_expression
))
7132 if (!args
->is_empty ())
7135 if (!any_type_dependent_arguments_p (args
))
7137 = perform_koenig_lookup (postfix_expression
, args
,
7142 = unqualified_fn_lookup_error (postfix_expression
);
7144 /* We do not perform argument-dependent lookup if
7145 normal lookup finds a non-function, in accordance
7146 with the expected resolution of DR 218. */
7147 else if (!args
->is_empty ()
7148 && is_overloaded_fn (postfix_expression
))
7150 tree fn
= get_first_fn (postfix_expression
);
7151 fn
= STRIP_TEMPLATE (fn
);
7153 /* Do not do argument dependent lookup if regular
7154 lookup finds a member function or a block-scope
7155 function declaration. [basic.lookup.argdep]/3 */
7156 if (!DECL_FUNCTION_MEMBER_P (fn
)
7157 && !DECL_LOCAL_FUNCTION_P (fn
))
7160 if (!any_type_dependent_arguments_p (args
))
7162 = perform_koenig_lookup (postfix_expression
, args
,
7168 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7169 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7170 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7171 && vec_safe_length (args
) == 3)
7173 tree arg0
= (*args
)[0];
7174 tree arg1
= (*args
)[1];
7175 tree arg2
= (*args
)[2];
7176 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7177 | (literal_integer_zerop (arg2
) << 2));
7178 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7181 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7183 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7184 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7186 if (processing_template_decl
7187 && (type_dependent_object_expression_p (instance
)
7188 || (!BASELINK_P (fn
)
7189 && TREE_CODE (fn
) != FIELD_DECL
)
7190 || type_dependent_expression_p (fn
)
7191 || any_type_dependent_arguments_p (args
)))
7193 maybe_generic_this_capture (instance
, fn
);
7195 = build_min_nt_call_vec (postfix_expression
, args
);
7196 release_tree_vector (args
);
7200 if (BASELINK_P (fn
))
7203 = (build_new_method_call
7204 (instance
, fn
, &args
, NULL_TREE
,
7205 (idk
== CP_ID_KIND_QUALIFIED
7206 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7213 = finish_call_expr (postfix_expression
, &args
,
7214 /*disallow_virtual=*/false,
7218 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7219 || TREE_CODE (postfix_expression
) == MEMBER_REF
7220 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7221 postfix_expression
= (build_offset_ref_call_from_tree
7222 (postfix_expression
, &args
,
7224 else if (idk
== CP_ID_KIND_QUALIFIED
)
7225 /* A call to a static class member, or a namespace-scope
7228 = finish_call_expr (postfix_expression
, &args
,
7229 /*disallow_virtual=*/true,
7233 /* All other function calls. */
7235 = finish_call_expr (postfix_expression
, &args
,
7236 /*disallow_virtual=*/false,
7240 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7242 location_t combined_loc
= make_location (token
->location
,
7245 postfix_expression
.set_location (combined_loc
);
7248 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7249 idk
= CP_ID_KIND_NONE
;
7251 release_tree_vector (args
);
7257 /* postfix-expression . template [opt] id-expression
7258 postfix-expression . pseudo-destructor-name
7259 postfix-expression -> template [opt] id-expression
7260 postfix-expression -> pseudo-destructor-name */
7262 /* Consume the `.' or `->' operator. */
7263 cp_lexer_consume_token (parser
->lexer
);
7266 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7270 is_member_access
= true;
7274 /* postfix-expression ++ */
7275 /* Consume the `++' token. */
7276 cp_lexer_consume_token (parser
->lexer
);
7277 /* Generate a representation for the complete expression. */
7279 = finish_increment_expr (postfix_expression
,
7280 POSTINCREMENT_EXPR
);
7281 /* Increments may not appear in constant-expressions. */
7282 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7283 postfix_expression
= error_mark_node
;
7284 idk
= CP_ID_KIND_NONE
;
7285 is_member_access
= false;
7288 case CPP_MINUS_MINUS
:
7289 /* postfix-expression -- */
7290 /* Consume the `--' token. */
7291 cp_lexer_consume_token (parser
->lexer
);
7292 /* Generate a representation for the complete expression. */
7294 = finish_increment_expr (postfix_expression
,
7295 POSTDECREMENT_EXPR
);
7296 /* Decrements may not appear in constant-expressions. */
7297 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7298 postfix_expression
= error_mark_node
;
7299 idk
= CP_ID_KIND_NONE
;
7300 is_member_access
= false;
7304 if (pidk_return
!= NULL
)
7305 * pidk_return
= idk
;
7306 if (member_access_only_p
)
7307 return is_member_access
7308 ? postfix_expression
7309 : cp_expr (error_mark_node
);
7311 return postfix_expression
;
7315 /* We should never get here. */
7317 return error_mark_node
;
7320 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7321 by cp_parser_builtin_offsetof. We're looking for
7323 postfix-expression [ expression ]
7324 postfix-expression [ braced-init-list ] (C++11)
7326 FOR_OFFSETOF is set if we're being called in that context, which
7327 changes how we deal with integer constant expressions. */
7330 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7331 tree postfix_expression
,
7335 tree index
= NULL_TREE
;
7336 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7337 bool saved_greater_than_is_operator_p
;
7339 /* Consume the `[' token. */
7340 cp_lexer_consume_token (parser
->lexer
);
7342 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7343 parser
->greater_than_is_operator_p
= true;
7345 /* Parse the index expression. */
7346 /* ??? For offsetof, there is a question of what to allow here. If
7347 offsetof is not being used in an integral constant expression context,
7348 then we *could* get the right answer by computing the value at runtime.
7349 If we are in an integral constant expression context, then we might
7350 could accept any constant expression; hard to say without analysis.
7351 Rather than open the barn door too wide right away, allow only integer
7352 constant expressions here. */
7354 index
= cp_parser_constant_expression (parser
);
7357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7359 bool expr_nonconst_p
;
7360 cp_lexer_set_source_position (parser
->lexer
);
7361 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7362 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7365 index
= cp_parser_expression (parser
);
7368 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7370 /* Look for the closing `]'. */
7371 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7373 /* Build the ARRAY_REF. */
7374 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7377 /* When not doing offsetof, array references are not permitted in
7378 constant-expressions. */
7380 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7381 postfix_expression
= error_mark_node
;
7383 return postfix_expression
;
7386 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7387 dereference of incomplete type, returns true if error_mark_node should
7388 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7389 and *DEPENDENT_P. */
7392 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7395 /* In a template, be permissive by treating an object expression
7396 of incomplete type as dependent (after a pedwarn). */
7397 diagnostic_t kind
= (processing_template_decl
7398 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7400 switch (TREE_CODE (*postfix_expression
))
7403 case REINTERPRET_CAST_EXPR
:
7404 case CONST_CAST_EXPR
:
7405 case STATIC_CAST_EXPR
:
7406 case DYNAMIC_CAST_EXPR
:
7407 case IMPLICIT_CONV_EXPR
:
7408 case VIEW_CONVERT_EXPR
:
7409 case NON_LVALUE_EXPR
:
7413 /* Don't emit any diagnostic for OVERLOADs. */
7417 /* Avoid clobbering e.g. DECLs. */
7418 if (!EXPR_P (*postfix_expression
))
7423 if (kind
== DK_IGNORED
)
7426 location_t exploc
= location_of (*postfix_expression
);
7427 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7428 if (!MAYBE_CLASS_TYPE_P (*scope
))
7430 if (kind
== DK_ERROR
)
7431 *scope
= *postfix_expression
= error_mark_node
;
7432 else if (processing_template_decl
)
7434 *dependent_p
= true;
7435 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7440 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7441 by cp_parser_builtin_offsetof. We're looking for
7443 postfix-expression . template [opt] id-expression
7444 postfix-expression . pseudo-destructor-name
7445 postfix-expression -> template [opt] id-expression
7446 postfix-expression -> pseudo-destructor-name
7448 FOR_OFFSETOF is set if we're being called in that context. That sorta
7449 limits what of the above we'll actually accept, but nevermind.
7450 TOKEN_TYPE is the "." or "->" token, which will already have been
7451 removed from the stream. */
7454 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7455 enum cpp_ttype token_type
,
7456 cp_expr postfix_expression
,
7457 bool for_offsetof
, cp_id_kind
*idk
,
7458 location_t location
)
7462 bool pseudo_destructor_p
;
7463 tree scope
= NULL_TREE
;
7464 location_t start_loc
= postfix_expression
.get_start ();
7466 /* If this is a `->' operator, dereference the pointer. */
7467 if (token_type
== CPP_DEREF
)
7468 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7469 tf_warning_or_error
);
7470 /* Check to see whether or not the expression is type-dependent and
7471 not the current instantiation. */
7472 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7473 /* The identifier following the `->' or `.' is not qualified. */
7474 parser
->scope
= NULL_TREE
;
7475 parser
->qualifying_scope
= NULL_TREE
;
7476 parser
->object_scope
= NULL_TREE
;
7477 *idk
= CP_ID_KIND_NONE
;
7479 /* Enter the scope corresponding to the type of the object
7480 given by the POSTFIX_EXPRESSION. */
7483 scope
= TREE_TYPE (postfix_expression
);
7484 /* According to the standard, no expression should ever have
7485 reference type. Unfortunately, we do not currently match
7486 the standard in this respect in that our internal representation
7487 of an expression may have reference type even when the standard
7488 says it does not. Therefore, we have to manually obtain the
7489 underlying type here. */
7490 scope
= non_reference (scope
);
7491 /* The type of the POSTFIX_EXPRESSION must be complete. */
7492 /* Unlike the object expression in other contexts, *this is not
7493 required to be of complete type for purposes of class member
7494 access (5.2.5) outside the member function body. */
7495 if (postfix_expression
!= current_class_ref
7496 && scope
!= error_mark_node
7497 && !(processing_template_decl
7498 && current_class_type
7499 && (same_type_ignoring_top_level_qualifiers_p
7500 (scope
, current_class_type
))))
7502 scope
= complete_type (scope
);
7503 if (!COMPLETE_TYPE_P (scope
)
7504 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7506 return error_mark_node
;
7511 /* Let the name lookup machinery know that we are processing a
7512 class member access expression. */
7513 parser
->context
->object_type
= scope
;
7514 /* If something went wrong, we want to be able to discern that case,
7515 as opposed to the case where there was no SCOPE due to the type
7516 of expression being dependent. */
7518 scope
= error_mark_node
;
7519 /* If the SCOPE was erroneous, make the various semantic analysis
7520 functions exit quickly -- and without issuing additional error
7522 if (scope
== error_mark_node
)
7523 postfix_expression
= error_mark_node
;
7528 /* Tell cp_parser_lookup_name that there was an object, even though it's
7530 parser
->context
->object_type
= unknown_type_node
;
7532 /* Assume this expression is not a pseudo-destructor access. */
7533 pseudo_destructor_p
= false;
7535 /* If the SCOPE is a scalar type, then, if this is a valid program,
7536 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7537 is type dependent, it can be pseudo-destructor-name or something else.
7538 Try to parse it as pseudo-destructor-name first. */
7539 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7544 cp_parser_parse_tentatively (parser
);
7545 /* Parse the pseudo-destructor-name. */
7547 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7550 && (cp_parser_error_occurred (parser
)
7551 || !SCALAR_TYPE_P (type
)))
7552 cp_parser_abort_tentative_parse (parser
);
7553 else if (cp_parser_parse_definitely (parser
))
7555 pseudo_destructor_p
= true;
7557 = finish_pseudo_destructor_expr (postfix_expression
,
7562 if (!pseudo_destructor_p
)
7564 /* If the SCOPE is not a scalar type, we are looking at an
7565 ordinary class member access expression, rather than a
7566 pseudo-destructor-name. */
7568 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7569 /* Parse the id-expression. */
7570 name
= (cp_parser_id_expression
7572 cp_parser_optional_template_keyword (parser
),
7573 /*check_dependency_p=*/true,
7575 /*declarator_p=*/false,
7576 /*optional_p=*/false));
7577 /* In general, build a SCOPE_REF if the member name is qualified.
7578 However, if the name was not dependent and has already been
7579 resolved; there is no need to build the SCOPE_REF. For example;
7581 struct X { void f(); };
7582 template <typename T> void f(T* t) { t->X::f(); }
7584 Even though "t" is dependent, "X::f" is not and has been resolved
7585 to a BASELINK; there is no need to include scope information. */
7587 /* But we do need to remember that there was an explicit scope for
7588 virtual function calls. */
7590 *idk
= CP_ID_KIND_QUALIFIED
;
7592 /* If the name is a template-id that names a type, we will get a
7593 TYPE_DECL here. That is invalid code. */
7594 if (TREE_CODE (name
) == TYPE_DECL
)
7596 error_at (token
->location
, "invalid use of %qD", name
);
7597 postfix_expression
= error_mark_node
;
7601 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7603 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7605 error_at (token
->location
, "%<%D::%D%> is not a class member",
7606 parser
->scope
, name
);
7607 postfix_expression
= error_mark_node
;
7610 name
= build_qualified_name (/*type=*/NULL_TREE
,
7614 parser
->scope
= NULL_TREE
;
7615 parser
->qualifying_scope
= NULL_TREE
;
7616 parser
->object_scope
= NULL_TREE
;
7618 if (parser
->scope
&& name
&& BASELINK_P (name
))
7619 adjust_result_of_qualified_name_lookup
7620 (name
, parser
->scope
, scope
);
7622 = finish_class_member_access_expr (postfix_expression
, name
,
7624 tf_warning_or_error
);
7625 /* Build a location e.g.:
7628 where the caret is at the deref token, ranging from
7629 the start of postfix_expression to the end of the access expr. */
7631 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7632 location_t combined_loc
7633 = make_location (input_location
, start_loc
, end_loc
);
7634 protected_set_expr_location (postfix_expression
, combined_loc
);
7638 /* We no longer need to look up names in the scope of the object on
7639 the left-hand side of the `.' or `->' operator. */
7640 parser
->context
->object_type
= NULL_TREE
;
7642 /* Outside of offsetof, these operators may not appear in
7643 constant-expressions. */
7645 && (cp_parser_non_integral_constant_expression
7646 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7647 postfix_expression
= error_mark_node
;
7649 return postfix_expression
;
7652 /* Parse a parenthesized expression-list.
7655 assignment-expression
7656 expression-list, assignment-expression
7661 identifier, expression-list
7663 CAST_P is true if this expression is the target of a cast.
7665 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7668 WRAP_LOCATIONS_P is true if expressions within this list for which
7669 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7670 their source locations.
7672 Returns a vector of trees. Each element is a representation of an
7673 assignment-expression. NULL is returned if the ( and or ) are
7674 missing. An empty, but allocated, vector is returned on no
7675 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7676 if we are parsing an attribute list for an attribute that wants a
7677 plain identifier argument, normal_attr for an attribute that wants
7678 an expression, or non_attr if we aren't parsing an attribute list. If
7679 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7680 not all of the expressions in the list were constant.
7681 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7682 will be written to with the location of the closing parenthesis. If
7683 an error occurs, it may or may not be written to. */
7685 static vec
<tree
, va_gc
> *
7686 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7687 int is_attribute_list
,
7689 bool allow_expansion_p
,
7690 bool *non_constant_p
,
7691 location_t
*close_paren_loc
,
7692 bool wrap_locations_p
)
7694 vec
<tree
, va_gc
> *expression_list
;
7695 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7696 tree identifier
= NULL_TREE
;
7697 bool saved_greater_than_is_operator_p
;
7699 /* Assume all the expressions will be constant. */
7701 *non_constant_p
= false;
7703 matching_parens parens
;
7704 if (!parens
.require_open (parser
))
7707 expression_list
= make_tree_vector ();
7709 /* Within a parenthesized expression, a `>' token is always
7710 the greater-than operator. */
7711 saved_greater_than_is_operator_p
7712 = parser
->greater_than_is_operator_p
;
7713 parser
->greater_than_is_operator_p
= true;
7715 cp_expr
expr (NULL_TREE
);
7717 /* Consume expressions until there are no more. */
7718 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7721 /* At the beginning of attribute lists, check to see if the
7722 next token is an identifier. */
7723 if (is_attribute_list
== id_attr
7724 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7728 /* Consume the identifier. */
7729 token
= cp_lexer_consume_token (parser
->lexer
);
7730 /* Save the identifier. */
7731 identifier
= token
->u
.value
;
7735 bool expr_non_constant_p
;
7737 /* Parse the next assignment-expression. */
7738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7740 /* A braced-init-list. */
7741 cp_lexer_set_source_position (parser
->lexer
);
7742 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7743 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7744 if (non_constant_p
&& expr_non_constant_p
)
7745 *non_constant_p
= true;
7747 else if (non_constant_p
)
7749 expr
= (cp_parser_constant_expression
7750 (parser
, /*allow_non_constant_p=*/true,
7751 &expr_non_constant_p
));
7752 if (expr_non_constant_p
)
7753 *non_constant_p
= true;
7756 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7760 expr
= instantiate_non_dependent_expr (expr
);
7762 /* If we have an ellipsis, then this is an expression
7764 if (allow_expansion_p
7765 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7767 /* Consume the `...'. */
7768 cp_lexer_consume_token (parser
->lexer
);
7770 /* Build the argument pack. */
7771 expr
= make_pack_expansion (expr
);
7774 if (wrap_locations_p
)
7775 expr
.maybe_add_location_wrapper ();
7777 /* Add it to the list. We add error_mark_node
7778 expressions to the list, so that we can still tell if
7779 the correct form for a parenthesized expression-list
7780 is found. That gives better errors. */
7781 vec_safe_push (expression_list
, expr
.get_value ());
7783 if (expr
== error_mark_node
)
7787 /* After the first item, attribute lists look the same as
7788 expression lists. */
7789 is_attribute_list
= non_attr
;
7792 /* If the next token isn't a `,', then we are done. */
7793 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7796 /* Otherwise, consume the `,' and keep going. */
7797 cp_lexer_consume_token (parser
->lexer
);
7800 if (close_paren_loc
)
7801 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7803 if (!parens
.require_close (parser
))
7808 /* We try and resync to an unnested comma, as that will give the
7809 user better diagnostics. */
7810 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7811 /*recovering=*/true,
7813 /*consume_paren=*/true);
7818 parser
->greater_than_is_operator_p
7819 = saved_greater_than_is_operator_p
;
7824 parser
->greater_than_is_operator_p
7825 = saved_greater_than_is_operator_p
;
7828 vec_safe_insert (expression_list
, 0, identifier
);
7830 return expression_list
;
7833 /* Parse a pseudo-destructor-name.
7835 pseudo-destructor-name:
7836 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7837 :: [opt] nested-name-specifier template template-id :: ~ type-name
7838 :: [opt] nested-name-specifier [opt] ~ type-name
7840 If either of the first two productions is used, sets *SCOPE to the
7841 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7842 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7843 or ERROR_MARK_NODE if the parse fails. */
7846 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7851 bool nested_name_specifier_p
;
7854 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7855 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7856 && !type_dependent_expression_p (object
))
7858 if (cxx_dialect
< cxx14
)
7859 pedwarn (input_location
, 0,
7860 "%<~auto%> only available with "
7861 "-std=c++14 or -std=gnu++14");
7862 cp_lexer_consume_token (parser
->lexer
);
7863 cp_lexer_consume_token (parser
->lexer
);
7865 *type
= TREE_TYPE (object
);
7869 /* Assume that things will not work out. */
7870 *type
= error_mark_node
;
7872 /* Look for the optional `::' operator. */
7873 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7874 /* Look for the optional nested-name-specifier. */
7875 nested_name_specifier_p
7876 = (cp_parser_nested_name_specifier_opt (parser
,
7877 /*typename_keyword_p=*/false,
7878 /*check_dependency_p=*/true,
7880 /*is_declaration=*/false)
7882 /* Now, if we saw a nested-name-specifier, we might be doing the
7883 second production. */
7884 if (nested_name_specifier_p
7885 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7887 /* Consume the `template' keyword. */
7888 cp_lexer_consume_token (parser
->lexer
);
7889 /* Parse the template-id. */
7890 cp_parser_template_id (parser
,
7891 /*template_keyword_p=*/true,
7892 /*check_dependency_p=*/false,
7894 /*is_declaration=*/true);
7895 /* Look for the `::' token. */
7896 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7898 /* If the next token is not a `~', then there might be some
7899 additional qualification. */
7900 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7902 /* At this point, we're looking for "type-name :: ~". The type-name
7903 must not be a class-name, since this is a pseudo-destructor. So,
7904 it must be either an enum-name, or a typedef-name -- both of which
7905 are just identifiers. So, we peek ahead to check that the "::"
7906 and "~" tokens are present; if they are not, then we can avoid
7907 calling type_name. */
7908 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7909 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7910 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7912 cp_parser_error (parser
, "non-scalar type");
7916 /* Look for the type-name. */
7917 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7918 if (*scope
== error_mark_node
)
7921 /* Look for the `::' token. */
7922 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7927 /* Look for the `~'. */
7928 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7930 /* Once we see the ~, this has to be a pseudo-destructor. */
7931 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7932 cp_parser_commit_to_topmost_tentative_parse (parser
);
7934 /* Look for the type-name again. We are not responsible for
7935 checking that it matches the first type-name. */
7936 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7939 /* Parse a unary-expression.
7945 unary-operator cast-expression
7946 sizeof unary-expression
7948 alignof ( type-id ) [C++0x]
7955 __extension__ cast-expression
7956 __alignof__ unary-expression
7957 __alignof__ ( type-id )
7958 alignof unary-expression [C++0x]
7959 __real__ cast-expression
7960 __imag__ cast-expression
7962 sizeof ( type-id ) { initializer-list , [opt] }
7963 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7964 __alignof__ ( type-id ) { initializer-list , [opt] }
7966 ADDRESS_P is true iff the unary-expression is appearing as the
7967 operand of the `&' operator. CAST_P is true if this expression is
7968 the target of a cast.
7970 Returns a representation of the expression. */
7973 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7974 bool address_p
, bool cast_p
, bool decltype_p
)
7977 enum tree_code unary_operator
;
7979 /* Peek at the next token. */
7980 token
= cp_lexer_peek_token (parser
->lexer
);
7981 /* Some keywords give away the kind of expression. */
7982 if (token
->type
== CPP_KEYWORD
)
7984 enum rid keyword
= token
->keyword
;
7993 location_t start_loc
= token
->location
;
7995 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7996 /* Consume the token. */
7997 cp_lexer_consume_token (parser
->lexer
);
7998 /* Parse the operand. */
7999 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8001 if (TYPE_P (operand
))
8002 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
8005 /* ISO C++ defines alignof only with types, not with
8006 expressions. So pedwarn if alignof is used with a non-
8007 type expression. However, __alignof__ is ok. */
8008 if (id_equal (token
->u
.value
, "alignof"))
8009 pedwarn (token
->location
, OPT_Wpedantic
,
8010 "ISO C++ does not allow %<alignof%> "
8013 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8015 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8016 SIZEOF_EXPR with the original operand. */
8017 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8019 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8021 if (!processing_template_decl
&& TYPE_P (operand
))
8023 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8024 build1 (NOP_EXPR
, operand
,
8026 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8029 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8030 TREE_SIDE_EFFECTS (ret
) = 0;
8031 TREE_READONLY (ret
) = 1;
8035 /* Construct a location e.g. :
8038 with start == caret at the start of the "alignof"/"sizeof"
8039 token, with the endpoint at the final closing paren. */
8040 location_t finish_loc
8041 = cp_lexer_previous_token (parser
->lexer
)->location
;
8042 location_t compound_loc
8043 = make_location (start_loc
, start_loc
, finish_loc
);
8045 cp_expr
ret_expr (ret
);
8046 ret_expr
.set_location (compound_loc
);
8047 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8052 return cp_parser_new_expression (parser
);
8055 return cp_parser_delete_expression (parser
);
8059 /* The saved value of the PEDANTIC flag. */
8063 /* Save away the PEDANTIC flag. */
8064 cp_parser_extension_opt (parser
, &saved_pedantic
);
8065 /* Parse the cast-expression. */
8066 expr
= cp_parser_simple_cast_expression (parser
);
8067 /* Restore the PEDANTIC flag. */
8068 pedantic
= saved_pedantic
;
8078 /* Consume the `__real__' or `__imag__' token. */
8079 cp_lexer_consume_token (parser
->lexer
);
8080 /* Parse the cast-expression. */
8081 expression
= cp_parser_simple_cast_expression (parser
);
8082 /* Create the complete representation. */
8083 return build_x_unary_op (token
->location
,
8084 (keyword
== RID_REALPART
8085 ? REALPART_EXPR
: IMAGPART_EXPR
),
8087 tf_warning_or_error
);
8091 case RID_TRANSACTION_ATOMIC
:
8092 case RID_TRANSACTION_RELAXED
:
8093 return cp_parser_transaction_expression (parser
, keyword
);
8098 const char *saved_message
;
8099 bool saved_integral_constant_expression_p
;
8100 bool saved_non_integral_constant_expression_p
;
8101 bool saved_greater_than_is_operator_p
;
8103 location_t start_loc
= token
->location
;
8105 cp_lexer_consume_token (parser
->lexer
);
8106 matching_parens parens
;
8107 parens
.require_open (parser
);
8109 saved_message
= parser
->type_definition_forbidden_message
;
8110 parser
->type_definition_forbidden_message
8111 = G_("types may not be defined in %<noexcept%> expressions");
8113 saved_integral_constant_expression_p
8114 = parser
->integral_constant_expression_p
;
8115 saved_non_integral_constant_expression_p
8116 = parser
->non_integral_constant_expression_p
;
8117 parser
->integral_constant_expression_p
= false;
8119 saved_greater_than_is_operator_p
8120 = parser
->greater_than_is_operator_p
;
8121 parser
->greater_than_is_operator_p
= true;
8123 ++cp_unevaluated_operand
;
8124 ++c_inhibit_evaluation_warnings
;
8125 ++cp_noexcept_operand
;
8126 expr
= cp_parser_expression (parser
);
8127 --cp_noexcept_operand
;
8128 --c_inhibit_evaluation_warnings
;
8129 --cp_unevaluated_operand
;
8131 parser
->greater_than_is_operator_p
8132 = saved_greater_than_is_operator_p
;
8134 parser
->integral_constant_expression_p
8135 = saved_integral_constant_expression_p
;
8136 parser
->non_integral_constant_expression_p
8137 = saved_non_integral_constant_expression_p
;
8139 parser
->type_definition_forbidden_message
= saved_message
;
8141 location_t finish_loc
8142 = cp_lexer_peek_token (parser
->lexer
)->location
;
8143 parens
.require_close (parser
);
8145 /* Construct a location of the form:
8148 with start == caret, finishing at the close-paren. */
8149 location_t noexcept_loc
8150 = make_location (start_loc
, start_loc
, finish_loc
);
8152 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8161 /* Look for the `:: new' and `:: delete', which also signal the
8162 beginning of a new-expression, or delete-expression,
8163 respectively. If the next token is `::', then it might be one of
8165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8169 /* See if the token after the `::' is one of the keywords in
8170 which we're interested. */
8171 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8172 /* If it's `new', we have a new-expression. */
8173 if (keyword
== RID_NEW
)
8174 return cp_parser_new_expression (parser
);
8175 /* Similarly, for `delete'. */
8176 else if (keyword
== RID_DELETE
)
8177 return cp_parser_delete_expression (parser
);
8180 /* Look for a unary operator. */
8181 unary_operator
= cp_parser_unary_operator (token
);
8182 /* The `++' and `--' operators can be handled similarly, even though
8183 they are not technically unary-operators in the grammar. */
8184 if (unary_operator
== ERROR_MARK
)
8186 if (token
->type
== CPP_PLUS_PLUS
)
8187 unary_operator
= PREINCREMENT_EXPR
;
8188 else if (token
->type
== CPP_MINUS_MINUS
)
8189 unary_operator
= PREDECREMENT_EXPR
;
8190 /* Handle the GNU address-of-label extension. */
8191 else if (cp_parser_allow_gnu_extensions_p (parser
)
8192 && token
->type
== CPP_AND_AND
)
8196 location_t start_loc
= token
->location
;
8198 /* Consume the '&&' token. */
8199 cp_lexer_consume_token (parser
->lexer
);
8200 /* Look for the identifier. */
8201 location_t finish_loc
8202 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8203 identifier
= cp_parser_identifier (parser
);
8204 /* Construct a location of the form:
8207 with caret==start at the "&&", finish at the end of the label. */
8208 location_t combined_loc
8209 = make_location (start_loc
, start_loc
, finish_loc
);
8210 /* Create an expression representing the address. */
8211 expression
= finish_label_address_expr (identifier
, combined_loc
);
8212 if (cp_parser_non_integral_constant_expression (parser
,
8214 expression
= error_mark_node
;
8218 if (unary_operator
!= ERROR_MARK
)
8220 cp_expr cast_expression
;
8221 cp_expr expression
= error_mark_node
;
8222 non_integral_constant non_constant_p
= NIC_NONE
;
8223 location_t loc
= token
->location
;
8224 tsubst_flags_t complain
= complain_flags (decltype_p
);
8226 /* Consume the operator token. */
8227 token
= cp_lexer_consume_token (parser
->lexer
);
8228 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8230 /* Parse the cast-expression. */
8232 = cp_parser_cast_expression (parser
,
8233 unary_operator
== ADDR_EXPR
,
8239 OP_TOKEN CAST_EXPRESSION
8240 ^~~~~~~~~~~~~~~~~~~~~~~~~
8241 with start==caret at the operator token, and
8242 extending to the end of the cast_expression. */
8243 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8245 /* Now, build an appropriate representation. */
8246 switch (unary_operator
)
8249 non_constant_p
= NIC_STAR
;
8250 expression
= build_x_indirect_ref (loc
, cast_expression
,
8253 /* TODO: build_x_indirect_ref does not always honor the
8254 location, so ensure it is set. */
8255 expression
.set_location (loc
);
8259 non_constant_p
= NIC_ADDR
;
8262 expression
= build_x_unary_op (loc
, unary_operator
,
8265 /* TODO: build_x_unary_op does not always honor the location,
8266 so ensure it is set. */
8267 expression
.set_location (loc
);
8270 case PREINCREMENT_EXPR
:
8271 case PREDECREMENT_EXPR
:
8272 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8273 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8276 /* Immediately fold negation of a constant, unless the constant is 0
8277 (since -0 == 0) or it would overflow. */
8278 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8279 && CONSTANT_CLASS_P (cast_expression
)
8280 && !integer_zerop (cast_expression
)
8281 && !TREE_OVERFLOW (cast_expression
))
8283 tree folded
= fold_build1 (unary_operator
,
8284 TREE_TYPE (cast_expression
),
8286 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8288 expression
= cp_expr (folded
, loc
);
8293 case UNARY_PLUS_EXPR
:
8294 case TRUTH_NOT_EXPR
:
8295 expression
= finish_unary_op_expr (loc
, unary_operator
,
8296 cast_expression
, complain
);
8303 if (non_constant_p
!= NIC_NONE
8304 && cp_parser_non_integral_constant_expression (parser
,
8306 expression
= error_mark_node
;
8311 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8312 /*member_access_only_p=*/false,
8317 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8318 unary-operator, the corresponding tree code is returned. */
8320 static enum tree_code
8321 cp_parser_unary_operator (cp_token
* token
)
8323 switch (token
->type
)
8326 return INDIRECT_REF
;
8332 return UNARY_PLUS_EXPR
;
8338 return TRUTH_NOT_EXPR
;
8341 return BIT_NOT_EXPR
;
8348 /* Parse a new-expression.
8351 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8352 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8354 Returns a representation of the expression. */
8357 cp_parser_new_expression (cp_parser
* parser
)
8359 bool global_scope_p
;
8360 vec
<tree
, va_gc
> *placement
;
8362 vec
<tree
, va_gc
> *initializer
;
8363 tree nelts
= NULL_TREE
;
8366 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8368 /* Look for the optional `::' operator. */
8370 = (cp_parser_global_scope_opt (parser
,
8371 /*current_scope_valid_p=*/false)
8373 /* Look for the `new' operator. */
8374 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8375 /* There's no easy way to tell a new-placement from the
8376 `( type-id )' construct. */
8377 cp_parser_parse_tentatively (parser
);
8378 /* Look for a new-placement. */
8379 placement
= cp_parser_new_placement (parser
);
8380 /* If that didn't work out, there's no new-placement. */
8381 if (!cp_parser_parse_definitely (parser
))
8383 if (placement
!= NULL
)
8384 release_tree_vector (placement
);
8388 /* If the next token is a `(', then we have a parenthesized
8390 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8393 const char *saved_message
= parser
->type_definition_forbidden_message
;
8395 /* Consume the `('. */
8396 matching_parens parens
;
8397 parens
.consume_open (parser
);
8399 /* Parse the type-id. */
8400 parser
->type_definition_forbidden_message
8401 = G_("types may not be defined in a new-expression");
8403 type_id_in_expr_sentinel
s (parser
);
8404 type
= cp_parser_type_id (parser
);
8406 parser
->type_definition_forbidden_message
= saved_message
;
8408 /* Look for the closing `)'. */
8409 parens
.require_close (parser
);
8410 token
= cp_lexer_peek_token (parser
->lexer
);
8411 /* There should not be a direct-new-declarator in this production,
8412 but GCC used to allowed this, so we check and emit a sensible error
8413 message for this case. */
8414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8416 error_at (token
->location
,
8417 "array bound forbidden after parenthesized type-id");
8418 inform (token
->location
,
8419 "try removing the parentheses around the type-id");
8420 cp_parser_direct_new_declarator (parser
);
8423 /* Otherwise, there must be a new-type-id. */
8425 type
= cp_parser_new_type_id (parser
, &nelts
);
8427 /* If the next token is a `(' or '{', then we have a new-initializer. */
8428 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8429 if (token
->type
== CPP_OPEN_PAREN
8430 || token
->type
== CPP_OPEN_BRACE
)
8431 initializer
= cp_parser_new_initializer (parser
);
8435 /* A new-expression may not appear in an integral constant
8437 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8438 ret
= error_mark_node
;
8439 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8440 of a new-type-id or type-id of a new-expression, the new-expression shall
8441 contain a new-initializer of the form ( assignment-expression )".
8442 Additionally, consistently with the spirit of DR 1467, we want to accept
8443 'new auto { 2 }' too. */
8444 else if ((ret
= type_uses_auto (type
))
8445 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8446 && (vec_safe_length (initializer
) != 1
8447 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8448 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8450 error_at (token
->location
,
8451 "initialization of new-expression for type %<auto%> "
8452 "requires exactly one element");
8453 ret
= error_mark_node
;
8457 /* Construct a location e.g.:
8460 with caret == start at the start of the "new" token, and the end
8461 at the end of the final token we consumed. */
8462 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8463 location_t end_loc
= get_finish (end_tok
->location
);
8464 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8466 /* Create a representation of the new-expression. */
8467 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8468 tf_warning_or_error
);
8469 protected_set_expr_location (ret
, combined_loc
);
8472 if (placement
!= NULL
)
8473 release_tree_vector (placement
);
8474 if (initializer
!= NULL
)
8475 release_tree_vector (initializer
);
8480 /* Parse a new-placement.
8485 Returns the same representation as for an expression-list. */
8487 static vec
<tree
, va_gc
> *
8488 cp_parser_new_placement (cp_parser
* parser
)
8490 vec
<tree
, va_gc
> *expression_list
;
8492 /* Parse the expression-list. */
8493 expression_list
= (cp_parser_parenthesized_expression_list
8494 (parser
, non_attr
, /*cast_p=*/false,
8495 /*allow_expansion_p=*/true,
8496 /*non_constant_p=*/NULL
));
8498 if (expression_list
&& expression_list
->is_empty ())
8499 error ("expected expression-list or type-id");
8501 return expression_list
;
8504 /* Parse a new-type-id.
8507 type-specifier-seq new-declarator [opt]
8509 Returns the TYPE allocated. If the new-type-id indicates an array
8510 type, *NELTS is set to the number of elements in the last array
8511 bound; the TYPE will not include the last array bound. */
8514 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8516 cp_decl_specifier_seq type_specifier_seq
;
8517 cp_declarator
*new_declarator
;
8518 cp_declarator
*declarator
;
8519 cp_declarator
*outer_declarator
;
8520 const char *saved_message
;
8522 /* The type-specifier sequence must not contain type definitions.
8523 (It cannot contain declarations of new types either, but if they
8524 are not definitions we will catch that because they are not
8526 saved_message
= parser
->type_definition_forbidden_message
;
8527 parser
->type_definition_forbidden_message
8528 = G_("types may not be defined in a new-type-id");
8529 /* Parse the type-specifier-seq. */
8530 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8531 /*is_trailing_return=*/false,
8532 &type_specifier_seq
);
8533 /* Restore the old message. */
8534 parser
->type_definition_forbidden_message
= saved_message
;
8536 if (type_specifier_seq
.type
== error_mark_node
)
8537 return error_mark_node
;
8539 /* Parse the new-declarator. */
8540 new_declarator
= cp_parser_new_declarator_opt (parser
);
8542 /* Determine the number of elements in the last array dimension, if
8545 /* Skip down to the last array dimension. */
8546 declarator
= new_declarator
;
8547 outer_declarator
= NULL
;
8548 while (declarator
&& (declarator
->kind
== cdk_pointer
8549 || declarator
->kind
== cdk_ptrmem
))
8551 outer_declarator
= declarator
;
8552 declarator
= declarator
->declarator
;
8555 && declarator
->kind
== cdk_array
8556 && declarator
->declarator
8557 && declarator
->declarator
->kind
== cdk_array
)
8559 outer_declarator
= declarator
;
8560 declarator
= declarator
->declarator
;
8563 if (declarator
&& declarator
->kind
== cdk_array
)
8565 *nelts
= declarator
->u
.array
.bounds
;
8566 if (*nelts
== error_mark_node
)
8567 *nelts
= integer_one_node
;
8569 if (outer_declarator
)
8570 outer_declarator
->declarator
= declarator
->declarator
;
8572 new_declarator
= NULL
;
8575 return groktypename (&type_specifier_seq
, new_declarator
, false);
8578 /* Parse an (optional) new-declarator.
8581 ptr-operator new-declarator [opt]
8582 direct-new-declarator
8584 Returns the declarator. */
8586 static cp_declarator
*
8587 cp_parser_new_declarator_opt (cp_parser
* parser
)
8589 enum tree_code code
;
8590 tree type
, std_attributes
= NULL_TREE
;
8591 cp_cv_quals cv_quals
;
8593 /* We don't know if there's a ptr-operator next, or not. */
8594 cp_parser_parse_tentatively (parser
);
8595 /* Look for a ptr-operator. */
8596 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8597 /* If that worked, look for more new-declarators. */
8598 if (cp_parser_parse_definitely (parser
))
8600 cp_declarator
*declarator
;
8602 /* Parse another optional declarator. */
8603 declarator
= cp_parser_new_declarator_opt (parser
);
8605 declarator
= cp_parser_make_indirect_declarator
8606 (code
, type
, cv_quals
, declarator
, std_attributes
);
8611 /* If the next token is a `[', there is a direct-new-declarator. */
8612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8613 return cp_parser_direct_new_declarator (parser
);
8618 /* Parse a direct-new-declarator.
8620 direct-new-declarator:
8622 direct-new-declarator [constant-expression]
8626 static cp_declarator
*
8627 cp_parser_direct_new_declarator (cp_parser
* parser
)
8629 cp_declarator
*declarator
= NULL
;
8636 /* Look for the opening `['. */
8637 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8639 token
= cp_lexer_peek_token (parser
->lexer
);
8640 expression
= cp_parser_expression (parser
);
8641 /* The standard requires that the expression have integral
8642 type. DR 74 adds enumeration types. We believe that the
8643 real intent is that these expressions be handled like the
8644 expression in a `switch' condition, which also allows
8645 classes with a single conversion to integral or
8646 enumeration type. */
8647 if (!processing_template_decl
)
8650 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8655 error_at (token
->location
,
8656 "expression in new-declarator must have integral "
8657 "or enumeration type");
8658 expression
= error_mark_node
;
8662 /* Look for the closing `]'. */
8663 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8665 /* Add this bound to the declarator. */
8666 declarator
= make_array_declarator (declarator
, expression
);
8668 /* If the next token is not a `[', then there are no more
8670 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8677 /* Parse a new-initializer.
8680 ( expression-list [opt] )
8683 Returns a representation of the expression-list. */
8685 static vec
<tree
, va_gc
> *
8686 cp_parser_new_initializer (cp_parser
* parser
)
8688 vec
<tree
, va_gc
> *expression_list
;
8690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8693 bool expr_non_constant_p
;
8694 cp_lexer_set_source_position (parser
->lexer
);
8695 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8696 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8697 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8698 expression_list
= make_tree_vector_single (t
);
8701 expression_list
= (cp_parser_parenthesized_expression_list
8702 (parser
, non_attr
, /*cast_p=*/false,
8703 /*allow_expansion_p=*/true,
8704 /*non_constant_p=*/NULL
));
8706 return expression_list
;
8709 /* Parse a delete-expression.
8712 :: [opt] delete cast-expression
8713 :: [opt] delete [ ] cast-expression
8715 Returns a representation of the expression. */
8718 cp_parser_delete_expression (cp_parser
* parser
)
8720 bool global_scope_p
;
8724 /* Look for the optional `::' operator. */
8726 = (cp_parser_global_scope_opt (parser
,
8727 /*current_scope_valid_p=*/false)
8729 /* Look for the `delete' keyword. */
8730 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8731 /* See if the array syntax is in use. */
8732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8734 /* Consume the `[' token. */
8735 cp_lexer_consume_token (parser
->lexer
);
8736 /* Look for the `]' token. */
8737 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8738 /* Remember that this is the `[]' construct. */
8744 /* Parse the cast-expression. */
8745 expression
= cp_parser_simple_cast_expression (parser
);
8747 /* A delete-expression may not appear in an integral constant
8749 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8750 return error_mark_node
;
8752 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8753 tf_warning_or_error
);
8756 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8757 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8761 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8763 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8764 switch (token
->type
)
8770 case CPP_CLOSE_SQUARE
:
8771 case CPP_CLOSE_PAREN
:
8772 case CPP_CLOSE_BRACE
:
8773 case CPP_OPEN_BRACE
:
8777 case CPP_DEREF_STAR
:
8785 case CPP_GREATER_EQ
:
8806 case CPP_OPEN_PAREN
:
8807 /* In ((type ()) () the last () isn't a valid cast-expression,
8808 so the whole must be parsed as postfix-expression. */
8809 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8812 case CPP_OPEN_SQUARE
:
8813 /* '[' may start a primary-expression in obj-c++ and in C++11,
8814 as a lambda-expression, eg, '(void)[]{}'. */
8815 if (cxx_dialect
>= cxx11
)
8817 return c_dialect_objc ();
8820 case CPP_MINUS_MINUS
:
8821 /* '++' and '--' may or may not start a cast-expression:
8823 struct T { void operator++(int); };
8824 void f() { (T())++; }
8837 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8838 in the order: const_cast, static_cast, reinterpret_cast.
8840 Don't suggest dynamic_cast.
8842 Return the first legal cast kind found, or NULL otherwise. */
8845 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8849 /* Reuse the parser logic by attempting to build the various kinds of
8850 cast, with "complain" disabled.
8851 Identify the first such cast that is valid. */
8853 /* Don't attempt to run such logic within template processing. */
8854 if (processing_template_decl
)
8857 /* First try const_cast. */
8858 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8859 if (trial
!= error_mark_node
)
8860 return "const_cast";
8862 /* If that fails, try static_cast. */
8863 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8864 if (trial
!= error_mark_node
)
8865 return "static_cast";
8867 /* Finally, try reinterpret_cast. */
8868 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8869 if (trial
!= error_mark_node
)
8870 return "reinterpret_cast";
8872 /* No such cast possible. */
8876 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8877 suggesting how to convert a C-style cast of the form:
8881 to a C++-style cast.
8883 The primary range of RICHLOC is asssumed to be that of the original
8884 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8885 of the parens in the C-style cast. */
8888 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8889 location_t close_paren_loc
, tree orig_expr
,
8892 /* This function is non-trivial, so bail out now if the warning isn't
8893 going to be emitted. */
8894 if (!warn_old_style_cast
)
8897 /* Try to find a legal C++ cast, trying them in order:
8898 const_cast, static_cast, reinterpret_cast. */
8899 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8900 if (!cast_suggestion
)
8903 /* Replace the open paren with "CAST_SUGGESTION<". */
8905 pp_printf (&pp
, "%s<", cast_suggestion
);
8906 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8908 /* Replace the close paren with "> (". */
8909 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8911 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8912 rich_loc
->add_fixit_insert_after (")");
8916 /* Parse a cast-expression.
8920 ( type-id ) cast-expression
8922 ADDRESS_P is true iff the unary-expression is appearing as the
8923 operand of the `&' operator. CAST_P is true if this expression is
8924 the target of a cast.
8926 Returns a representation of the expression. */
8929 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8930 bool decltype_p
, cp_id_kind
* pidk
)
8932 /* If it's a `(', then we might be looking at a cast. */
8933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8935 tree type
= NULL_TREE
;
8936 cp_expr
expr (NULL_TREE
);
8937 int cast_expression
= 0;
8938 const char *saved_message
;
8940 /* There's no way to know yet whether or not this is a cast.
8941 For example, `(int (3))' is a unary-expression, while `(int)
8942 3' is a cast. So, we resort to parsing tentatively. */
8943 cp_parser_parse_tentatively (parser
);
8944 /* Types may not be defined in a cast. */
8945 saved_message
= parser
->type_definition_forbidden_message
;
8946 parser
->type_definition_forbidden_message
8947 = G_("types may not be defined in casts");
8948 /* Consume the `('. */
8949 matching_parens parens
;
8950 cp_token
*open_paren
= parens
.consume_open (parser
);
8951 location_t open_paren_loc
= open_paren
->location
;
8952 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8954 /* A very tricky bit is that `(struct S) { 3 }' is a
8955 compound-literal (which we permit in C++ as an extension).
8956 But, that construct is not a cast-expression -- it is a
8957 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8958 is legal; if the compound-literal were a cast-expression,
8959 you'd need an extra set of parentheses.) But, if we parse
8960 the type-id, and it happens to be a class-specifier, then we
8961 will commit to the parse at that point, because we cannot
8962 undo the action that is done when creating a new class. So,
8963 then we cannot back up and do a postfix-expression.
8965 Another tricky case is the following (c++/29234):
8967 struct S { void operator () (); };
8974 As a type-id we parse the parenthesized S()() as a function
8975 returning a function, groktypename complains and we cannot
8976 back up in this case either.
8978 Therefore, we scan ahead to the closing `)', and check to see
8979 if the tokens after the `)' can start a cast-expression. Otherwise
8980 we are dealing with an unary-expression, a postfix-expression
8983 Yet another tricky case, in C++11, is the following (c++/54891):
8987 The issue is that usually, besides the case of lambda-expressions,
8988 the parenthesized type-id cannot be followed by '[', and, eg, we
8989 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8990 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8991 we don't commit, we try a cast-expression, then an unary-expression.
8993 Save tokens so that we can put them back. */
8994 cp_lexer_save_tokens (parser
->lexer
);
8996 /* We may be looking at a cast-expression. */
8997 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8998 /*consume_paren=*/true))
9000 = cp_parser_tokens_start_cast_expression (parser
);
9002 /* Roll back the tokens we skipped. */
9003 cp_lexer_rollback_tokens (parser
->lexer
);
9004 /* If we aren't looking at a cast-expression, simulate an error so
9005 that the call to cp_parser_error_occurred below returns true. */
9006 if (!cast_expression
)
9007 cp_parser_simulate_error (parser
);
9010 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9011 parser
->in_type_id_in_expr_p
= true;
9012 /* Look for the type-id. */
9013 type
= cp_parser_type_id (parser
);
9014 /* Look for the closing `)'. */
9015 cp_token
*close_paren
= parens
.require_close (parser
);
9017 close_paren_loc
= close_paren
->location
;
9018 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9021 /* Restore the saved message. */
9022 parser
->type_definition_forbidden_message
= saved_message
;
9024 /* At this point this can only be either a cast or a
9025 parenthesized ctor such as `(T ())' that looks like a cast to
9026 function returning T. */
9027 if (!cp_parser_error_occurred (parser
))
9029 /* Only commit if the cast-expression doesn't start with
9030 '++', '--', or '[' in C++11. */
9031 if (cast_expression
> 0)
9032 cp_parser_commit_to_topmost_tentative_parse (parser
);
9034 expr
= cp_parser_cast_expression (parser
,
9035 /*address_p=*/false,
9037 /*decltype_p=*/false,
9040 if (cp_parser_parse_definitely (parser
))
9042 /* Warn about old-style casts, if so requested. */
9043 if (warn_old_style_cast
9044 && !in_system_header_at (input_location
)
9045 && !VOID_TYPE_P (type
)
9046 && current_lang_name
!= lang_name_c
)
9048 gcc_rich_location
rich_loc (input_location
);
9049 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9051 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9052 "use of old-style cast to %q#T", type
);
9055 /* Only type conversions to integral or enumeration types
9056 can be used in constant-expressions. */
9057 if (!cast_valid_in_integral_constant_expression_p (type
)
9058 && cp_parser_non_integral_constant_expression (parser
,
9060 return error_mark_node
;
9062 /* Perform the cast. */
9066 with start==caret at the open paren, extending to the
9068 location_t cast_loc
= make_location (open_paren_loc
,
9070 expr
.get_finish ());
9071 expr
= build_c_cast (cast_loc
, type
, expr
);
9076 cp_parser_abort_tentative_parse (parser
);
9079 /* If we get here, then it's not a cast, so it must be a
9080 unary-expression. */
9081 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9082 cast_p
, decltype_p
);
9085 /* Parse a binary expression of the general form:
9089 pm-expression .* cast-expression
9090 pm-expression ->* cast-expression
9092 multiplicative-expression:
9094 multiplicative-expression * pm-expression
9095 multiplicative-expression / pm-expression
9096 multiplicative-expression % pm-expression
9098 additive-expression:
9099 multiplicative-expression
9100 additive-expression + multiplicative-expression
9101 additive-expression - multiplicative-expression
9105 shift-expression << additive-expression
9106 shift-expression >> additive-expression
9108 relational-expression:
9110 relational-expression < shift-expression
9111 relational-expression > shift-expression
9112 relational-expression <= shift-expression
9113 relational-expression >= shift-expression
9117 relational-expression:
9118 relational-expression <? shift-expression
9119 relational-expression >? shift-expression
9121 equality-expression:
9122 relational-expression
9123 equality-expression == relational-expression
9124 equality-expression != relational-expression
9128 and-expression & equality-expression
9130 exclusive-or-expression:
9132 exclusive-or-expression ^ and-expression
9134 inclusive-or-expression:
9135 exclusive-or-expression
9136 inclusive-or-expression | exclusive-or-expression
9138 logical-and-expression:
9139 inclusive-or-expression
9140 logical-and-expression && inclusive-or-expression
9142 logical-or-expression:
9143 logical-and-expression
9144 logical-or-expression || logical-and-expression
9146 All these are implemented with a single function like:
9149 simple-cast-expression
9150 binary-expression <token> binary-expression
9152 CAST_P is true if this expression is the target of a cast.
9154 The binops_by_token map is used to get the tree codes for each <token> type.
9155 binary-expressions are associated according to a precedence table. */
9157 #define TOKEN_PRECEDENCE(token) \
9158 (((token->type == CPP_GREATER \
9159 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9160 && !parser->greater_than_is_operator_p) \
9161 ? PREC_NOT_OPERATOR \
9162 : binops_by_token[token->type].prec)
9165 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9166 bool no_toplevel_fold_p
,
9168 enum cp_parser_prec prec
,
9171 cp_parser_expression_stack stack
;
9172 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9173 cp_parser_expression_stack_entry current
;
9176 enum tree_code rhs_type
;
9177 enum cp_parser_prec new_prec
, lookahead_prec
;
9180 /* Parse the first expression. */
9181 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9182 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9183 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9184 cast_p
, decltype_p
, pidk
);
9185 current
.prec
= prec
;
9187 if (cp_parser_error_occurred (parser
))
9188 return error_mark_node
;
9192 /* Get an operator token. */
9193 token
= cp_lexer_peek_token (parser
->lexer
);
9195 if (warn_cxx11_compat
9196 && token
->type
== CPP_RSHIFT
9197 && !parser
->greater_than_is_operator_p
)
9199 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9200 "%<>>%> operator is treated"
9201 " as two right angle brackets in C++11"))
9202 inform (token
->location
,
9203 "suggest parentheses around %<>>%> expression");
9206 new_prec
= TOKEN_PRECEDENCE (token
);
9207 if (new_prec
!= PREC_NOT_OPERATOR
9208 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9209 /* This is a fold-expression; handle it later. */
9210 new_prec
= PREC_NOT_OPERATOR
;
9212 /* Popping an entry off the stack means we completed a subexpression:
9213 - either we found a token which is not an operator (`>' where it is not
9214 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9215 will happen repeatedly;
9216 - or, we found an operator which has lower priority. This is the case
9217 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9219 if (new_prec
<= current
.prec
)
9228 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9229 current
.loc
= token
->location
;
9231 /* We used the operator token. */
9232 cp_lexer_consume_token (parser
->lexer
);
9234 /* For "false && x" or "true || x", x will never be executed;
9235 disable warnings while evaluating it. */
9236 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9237 c_inhibit_evaluation_warnings
+=
9238 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9239 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9240 c_inhibit_evaluation_warnings
+=
9241 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9243 /* Extract another operand. It may be the RHS of this expression
9244 or the LHS of a new, higher priority expression. */
9245 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9246 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9247 rhs
= cp_parser_simple_cast_expression (parser
);
9249 /* Get another operator token. Look up its precedence to avoid
9250 building a useless (immediately popped) stack entry for common
9251 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9252 token
= cp_lexer_peek_token (parser
->lexer
);
9253 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9254 if (lookahead_prec
!= PREC_NOT_OPERATOR
9255 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9256 lookahead_prec
= PREC_NOT_OPERATOR
;
9257 if (lookahead_prec
> new_prec
)
9259 /* ... and prepare to parse the RHS of the new, higher priority
9260 expression. Since precedence levels on the stack are
9261 monotonically increasing, we do not have to care about
9266 current
.lhs_type
= rhs_type
;
9267 current
.prec
= new_prec
;
9268 new_prec
= lookahead_prec
;
9272 lookahead_prec
= new_prec
;
9273 /* If the stack is not empty, we have parsed into LHS the right side
9274 (`4' in the example above) of an expression we had suspended.
9275 We can use the information on the stack to recover the LHS (`3')
9276 from the stack together with the tree code (`MULT_EXPR'), and
9277 the precedence of the higher level subexpression
9278 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9279 which will be used to actually build the additive expression. */
9281 rhs_type
= current
.lhs_type
;
9286 /* Undo the disabling of warnings done above. */
9287 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9288 c_inhibit_evaluation_warnings
-=
9289 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9290 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9291 c_inhibit_evaluation_warnings
-=
9292 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9294 if (warn_logical_not_paren
9295 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9296 && current
.lhs_type
== TRUTH_NOT_EXPR
9297 /* Avoid warning for !!x == y. */
9298 && (TREE_CODE (current
.lhs
) != NE_EXPR
9299 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9300 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9301 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9302 /* Avoid warning for !b == y where b is boolean. */
9303 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9304 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9306 /* Avoid warning for !!b == y where b is boolean. */
9307 && (!DECL_P (current
.lhs
)
9308 || TREE_TYPE (current
.lhs
) == NULL_TREE
9309 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9310 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9311 current
.lhs
, maybe_constant_value (rhs
));
9315 location_t combined_loc
= make_location (current
.loc
,
9316 current
.lhs
.get_start (),
9319 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9320 ERROR_MARK for everything that is not a binary expression.
9321 This makes warn_about_parentheses miss some warnings that
9322 involve unary operators. For unary expressions we should
9323 pass the correct tree_code unless the unary expression was
9324 surrounded by parentheses.
9326 if (no_toplevel_fold_p
9327 && lookahead_prec
<= current
.prec
9330 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9331 current
.lhs
= error_mark_node
;
9335 = build_min (current
.tree_type
,
9336 TREE_CODE_CLASS (current
.tree_type
)
9338 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9339 current
.lhs
.get_value (), rhs
.get_value ());
9340 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9345 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9346 current
.lhs
, current
.lhs_type
,
9347 rhs
, rhs_type
, &overload
,
9348 complain_flags (decltype_p
));
9349 /* TODO: build_x_binary_op doesn't always honor the location. */
9350 current
.lhs
.set_location (combined_loc
);
9352 current
.lhs_type
= current
.tree_type
;
9354 /* If the binary operator required the use of an overloaded operator,
9355 then this expression cannot be an integral constant-expression.
9356 An overloaded operator can be used even if both operands are
9357 otherwise permissible in an integral constant-expression if at
9358 least one of the operands is of enumeration type. */
9361 && cp_parser_non_integral_constant_expression (parser
,
9363 return error_mark_node
;
9370 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9371 bool no_toplevel_fold_p
,
9372 enum cp_parser_prec prec
,
9375 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9376 /*decltype*/false, prec
, pidk
);
9379 /* Parse the `? expression : assignment-expression' part of a
9380 conditional-expression. The LOGICAL_OR_EXPR is the
9381 logical-or-expression that started the conditional-expression.
9382 Returns a representation of the entire conditional-expression.
9384 This routine is used by cp_parser_assignment_expression.
9386 ? expression : assignment-expression
9390 ? : assignment-expression */
9393 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9395 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9396 cp_expr assignment_expr
;
9397 struct cp_token
*token
;
9398 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9400 /* Consume the `?' token. */
9401 cp_lexer_consume_token (parser
->lexer
);
9402 token
= cp_lexer_peek_token (parser
->lexer
);
9403 if (cp_parser_allow_gnu_extensions_p (parser
)
9404 && token
->type
== CPP_COLON
)
9406 pedwarn (token
->location
, OPT_Wpedantic
,
9407 "ISO C++ does not allow ?: with omitted middle operand");
9408 /* Implicit true clause. */
9410 c_inhibit_evaluation_warnings
+=
9411 folded_logical_or_expr
== truthvalue_true_node
;
9412 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9416 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9417 parser
->colon_corrects_to_scope_p
= false;
9418 /* Parse the expression. */
9419 c_inhibit_evaluation_warnings
+=
9420 folded_logical_or_expr
== truthvalue_false_node
;
9421 expr
= cp_parser_expression (parser
);
9422 c_inhibit_evaluation_warnings
+=
9423 ((folded_logical_or_expr
== truthvalue_true_node
)
9424 - (folded_logical_or_expr
== truthvalue_false_node
));
9425 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9428 /* The next token should be a `:'. */
9429 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9430 /* Parse the assignment-expression. */
9431 assignment_expr
= cp_parser_assignment_expression (parser
);
9432 c_inhibit_evaluation_warnings
-=
9433 folded_logical_or_expr
== truthvalue_true_node
;
9436 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9437 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9438 with the caret at the "?", ranging from the start of
9439 the logical_or_expr to the end of the assignment_expr. */
9440 loc
= make_location (loc
,
9441 logical_or_expr
.get_start (),
9442 assignment_expr
.get_finish ());
9444 /* Build the conditional-expression. */
9445 return build_x_conditional_expr (loc
, logical_or_expr
,
9448 tf_warning_or_error
);
9451 /* Parse an assignment-expression.
9453 assignment-expression:
9454 conditional-expression
9455 logical-or-expression assignment-operator assignment_expression
9458 CAST_P is true if this expression is the target of a cast.
9459 DECLTYPE_P is true if this expression is the operand of decltype.
9461 Returns a representation for the expression. */
9464 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9465 bool cast_p
, bool decltype_p
)
9469 /* If the next token is the `throw' keyword, then we're looking at
9470 a throw-expression. */
9471 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9472 expr
= cp_parser_throw_expression (parser
);
9473 /* Otherwise, it must be that we are looking at a
9474 logical-or-expression. */
9477 /* Parse the binary expressions (logical-or-expression). */
9478 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9480 PREC_NOT_OPERATOR
, pidk
);
9481 /* If the next token is a `?' then we're actually looking at a
9482 conditional-expression. */
9483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9484 return cp_parser_question_colon_clause (parser
, expr
);
9487 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9489 /* If it's an assignment-operator, we're using the second
9491 enum tree_code assignment_operator
9492 = cp_parser_assignment_operator_opt (parser
);
9493 if (assignment_operator
!= ERROR_MARK
)
9495 bool non_constant_p
;
9497 /* Parse the right-hand side of the assignment. */
9498 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9501 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9502 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9504 /* An assignment may not appear in a
9505 constant-expression. */
9506 if (cp_parser_non_integral_constant_expression (parser
,
9508 return error_mark_node
;
9509 /* Build the assignment expression. Its default
9513 is the location of the '=' token as the
9514 caret, ranging from the start of the lhs to the
9516 loc
= make_location (loc
,
9519 expr
= build_x_modify_expr (loc
, expr
,
9520 assignment_operator
,
9522 complain_flags (decltype_p
));
9523 /* TODO: build_x_modify_expr doesn't honor the location,
9524 so we must set it here. */
9525 expr
.set_location (loc
);
9533 /* Parse an (optional) assignment-operator.
9535 assignment-operator: one of
9536 = *= /= %= += -= >>= <<= &= ^= |=
9540 assignment-operator: one of
9543 If the next token is an assignment operator, the corresponding tree
9544 code is returned, and the token is consumed. For example, for
9545 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9546 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9547 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9548 operator, ERROR_MARK is returned. */
9550 static enum tree_code
9551 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9556 /* Peek at the next token. */
9557 token
= cp_lexer_peek_token (parser
->lexer
);
9559 switch (token
->type
)
9570 op
= TRUNC_DIV_EXPR
;
9574 op
= TRUNC_MOD_EXPR
;
9606 /* Nothing else is an assignment operator. */
9610 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9611 if (op
!= ERROR_MARK
9612 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9615 /* If it was an assignment operator, consume it. */
9616 if (op
!= ERROR_MARK
)
9617 cp_lexer_consume_token (parser
->lexer
);
9622 /* Parse an expression.
9625 assignment-expression
9626 expression , assignment-expression
9628 CAST_P is true if this expression is the target of a cast.
9629 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9630 except possibly parenthesized or on the RHS of a comma (N3276).
9632 Returns a representation of the expression. */
9635 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9636 bool cast_p
, bool decltype_p
)
9638 cp_expr expression
= NULL_TREE
;
9639 location_t loc
= UNKNOWN_LOCATION
;
9643 cp_expr assignment_expression
;
9645 /* Parse the next assignment-expression. */
9646 assignment_expression
9647 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9649 /* We don't create a temporary for a call that is the immediate operand
9650 of decltype or on the RHS of a comma. But when we see a comma, we
9651 need to create a temporary for a call on the LHS. */
9652 if (decltype_p
&& !processing_template_decl
9653 && TREE_CODE (assignment_expression
) == CALL_EXPR
9654 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9655 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9656 assignment_expression
9657 = build_cplus_new (TREE_TYPE (assignment_expression
),
9658 assignment_expression
, tf_warning_or_error
);
9660 /* If this is the first assignment-expression, we can just
9663 expression
= assignment_expression
;
9666 /* Create a location with caret at the comma, ranging
9667 from the start of the LHS to the end of the RHS. */
9668 loc
= make_location (loc
,
9669 expression
.get_start (),
9670 assignment_expression
.get_finish ());
9671 expression
= build_x_compound_expr (loc
, expression
,
9672 assignment_expression
,
9673 complain_flags (decltype_p
));
9674 expression
.set_location (loc
);
9676 /* If the next token is not a comma, or we're in a fold-expression, then
9677 we are done with the expression. */
9678 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9679 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9681 /* Consume the `,'. */
9682 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9683 cp_lexer_consume_token (parser
->lexer
);
9684 /* A comma operator cannot appear in a constant-expression. */
9685 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9686 expression
= error_mark_node
;
9692 /* Parse a constant-expression.
9694 constant-expression:
9695 conditional-expression
9697 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9698 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9699 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9700 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9701 only parse a conditional-expression, otherwise parse an
9702 assignment-expression. See below for rationale. */
9705 cp_parser_constant_expression (cp_parser
* parser
,
9706 bool allow_non_constant_p
,
9707 bool *non_constant_p
,
9710 bool saved_integral_constant_expression_p
;
9711 bool saved_allow_non_integral_constant_expression_p
;
9712 bool saved_non_integral_constant_expression_p
;
9715 /* It might seem that we could simply parse the
9716 conditional-expression, and then check to see if it were
9717 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9718 one that the compiler can figure out is constant, possibly after
9719 doing some simplifications or optimizations. The standard has a
9720 precise definition of constant-expression, and we must honor
9721 that, even though it is somewhat more restrictive.
9727 is not a legal declaration, because `(2, 3)' is not a
9728 constant-expression. The `,' operator is forbidden in a
9729 constant-expression. However, GCC's constant-folding machinery
9730 will fold this operation to an INTEGER_CST for `3'. */
9732 /* Save the old settings. */
9733 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9734 saved_allow_non_integral_constant_expression_p
9735 = parser
->allow_non_integral_constant_expression_p
;
9736 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9737 /* We are now parsing a constant-expression. */
9738 parser
->integral_constant_expression_p
= true;
9739 parser
->allow_non_integral_constant_expression_p
9740 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9741 parser
->non_integral_constant_expression_p
= false;
9742 /* Although the grammar says "conditional-expression", when not STRICT_P,
9743 we parse an "assignment-expression", which also permits
9744 "throw-expression" and the use of assignment operators. In the case
9745 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9746 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9747 actually essential that we look for an assignment-expression.
9748 For example, cp_parser_initializer_clauses uses this function to
9749 determine whether a particular assignment-expression is in fact
9753 /* Parse the binary expressions (logical-or-expression). */
9754 expression
= cp_parser_binary_expression (parser
, false, false, false,
9755 PREC_NOT_OPERATOR
, NULL
);
9756 /* If the next token is a `?' then we're actually looking at
9757 a conditional-expression; otherwise we're done. */
9758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9759 expression
= cp_parser_question_colon_clause (parser
, expression
);
9762 expression
= cp_parser_assignment_expression (parser
);
9763 /* Restore the old settings. */
9764 parser
->integral_constant_expression_p
9765 = saved_integral_constant_expression_p
;
9766 parser
->allow_non_integral_constant_expression_p
9767 = saved_allow_non_integral_constant_expression_p
;
9768 if (cxx_dialect
>= cxx11
)
9770 /* Require an rvalue constant expression here; that's what our
9771 callers expect. Reference constant expressions are handled
9772 separately in e.g. cp_parser_template_argument. */
9773 tree decay
= expression
;
9774 if (TREE_TYPE (expression
)
9775 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9776 decay
= build_address (expression
);
9777 bool is_const
= potential_rvalue_constant_expression (decay
);
9778 parser
->non_integral_constant_expression_p
= !is_const
;
9779 if (!is_const
&& !allow_non_constant_p
)
9780 require_potential_rvalue_constant_expression (decay
);
9782 if (allow_non_constant_p
)
9783 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9784 parser
->non_integral_constant_expression_p
9785 = saved_non_integral_constant_expression_p
;
9790 /* Parse __builtin_offsetof.
9792 offsetof-expression:
9793 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9795 offsetof-member-designator:
9797 | offsetof-member-designator "." id-expression
9798 | offsetof-member-designator "[" expression "]"
9799 | offsetof-member-designator "->" id-expression */
9802 cp_parser_builtin_offsetof (cp_parser
*parser
)
9804 int save_ice_p
, save_non_ice_p
;
9809 location_t finish_loc
;
9811 /* We're about to accept non-integral-constant things, but will
9812 definitely yield an integral constant expression. Save and
9813 restore these values around our local parsing. */
9814 save_ice_p
= parser
->integral_constant_expression_p
;
9815 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9817 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9819 /* Consume the "__builtin_offsetof" token. */
9820 cp_lexer_consume_token (parser
->lexer
);
9821 /* Consume the opening `('. */
9822 matching_parens parens
;
9823 parens
.require_open (parser
);
9824 /* Parse the type-id. */
9825 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9826 type
= cp_parser_type_id (parser
);
9827 /* Look for the `,'. */
9828 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9829 token
= cp_lexer_peek_token (parser
->lexer
);
9831 /* Build the (type *)null that begins the traditional offsetof macro. */
9833 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9834 tf_warning_or_error
);
9836 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9837 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9838 true, &dummy
, token
->location
);
9841 token
= cp_lexer_peek_token (parser
->lexer
);
9842 switch (token
->type
)
9844 case CPP_OPEN_SQUARE
:
9845 /* offsetof-member-designator "[" expression "]" */
9846 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9851 /* offsetof-member-designator "->" identifier */
9852 expr
= grok_array_decl (token
->location
, expr
,
9853 integer_zero_node
, false);
9857 /* offsetof-member-designator "." identifier */
9858 cp_lexer_consume_token (parser
->lexer
);
9859 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9864 case CPP_CLOSE_PAREN
:
9865 /* Consume the ")" token. */
9866 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9867 cp_lexer_consume_token (parser
->lexer
);
9871 /* Error. We know the following require will fail, but
9872 that gives the proper error message. */
9873 parens
.require_close (parser
);
9874 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9875 expr
= error_mark_node
;
9881 /* Make a location of the form:
9882 __builtin_offsetof (struct s, f)
9883 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9884 with caret at the type-id, ranging from the start of the
9885 "_builtin_offsetof" token to the close paren. */
9886 loc
= make_location (loc
, start_loc
, finish_loc
);
9887 /* The result will be an INTEGER_CST, so we need to explicitly
9888 preserve the location. */
9889 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9892 parser
->integral_constant_expression_p
= save_ice_p
;
9893 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9895 expr
= expr
.maybe_add_location_wrapper ();
9899 /* Parse a trait expression.
9901 Returns a representation of the expression, the underlying type
9902 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9905 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9908 tree type1
, type2
= NULL_TREE
;
9909 bool binary
= false;
9910 bool variadic
= false;
9914 case RID_HAS_NOTHROW_ASSIGN
:
9915 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9917 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9918 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9920 case RID_HAS_NOTHROW_COPY
:
9921 kind
= CPTK_HAS_NOTHROW_COPY
;
9923 case RID_HAS_TRIVIAL_ASSIGN
:
9924 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9926 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9927 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9929 case RID_HAS_TRIVIAL_COPY
:
9930 kind
= CPTK_HAS_TRIVIAL_COPY
;
9932 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9933 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9935 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9936 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9938 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9939 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9941 case RID_IS_ABSTRACT
:
9942 kind
= CPTK_IS_ABSTRACT
;
9944 case RID_IS_AGGREGATE
:
9945 kind
= CPTK_IS_AGGREGATE
;
9947 case RID_IS_BASE_OF
:
9948 kind
= CPTK_IS_BASE_OF
;
9952 kind
= CPTK_IS_CLASS
;
9955 kind
= CPTK_IS_EMPTY
;
9958 kind
= CPTK_IS_ENUM
;
9961 kind
= CPTK_IS_FINAL
;
9963 case RID_IS_LITERAL_TYPE
:
9964 kind
= CPTK_IS_LITERAL_TYPE
;
9969 case RID_IS_POLYMORPHIC
:
9970 kind
= CPTK_IS_POLYMORPHIC
;
9972 case RID_IS_SAME_AS
:
9973 kind
= CPTK_IS_SAME_AS
;
9976 case RID_IS_STD_LAYOUT
:
9977 kind
= CPTK_IS_STD_LAYOUT
;
9979 case RID_IS_TRIVIAL
:
9980 kind
= CPTK_IS_TRIVIAL
;
9982 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9983 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9986 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9987 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9990 case RID_IS_TRIVIALLY_COPYABLE
:
9991 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9994 kind
= CPTK_IS_UNION
;
9996 case RID_UNDERLYING_TYPE
:
9997 kind
= CPTK_UNDERLYING_TYPE
;
10002 case RID_DIRECT_BASES
:
10003 kind
= CPTK_DIRECT_BASES
;
10005 case RID_IS_ASSIGNABLE
:
10006 kind
= CPTK_IS_ASSIGNABLE
;
10009 case RID_IS_CONSTRUCTIBLE
:
10010 kind
= CPTK_IS_CONSTRUCTIBLE
;
10014 gcc_unreachable ();
10017 /* Get location of initial token. */
10018 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10020 /* Consume the token. */
10021 cp_lexer_consume_token (parser
->lexer
);
10023 matching_parens parens
;
10024 parens
.require_open (parser
);
10027 type_id_in_expr_sentinel
s (parser
);
10028 type1
= cp_parser_type_id (parser
);
10031 if (type1
== error_mark_node
)
10032 return error_mark_node
;
10036 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10039 type_id_in_expr_sentinel
s (parser
);
10040 type2
= cp_parser_type_id (parser
);
10043 if (type2
== error_mark_node
)
10044 return error_mark_node
;
10048 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10050 cp_lexer_consume_token (parser
->lexer
);
10051 tree elt
= cp_parser_type_id (parser
);
10052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10054 cp_lexer_consume_token (parser
->lexer
);
10055 elt
= make_pack_expansion (elt
);
10057 if (elt
== error_mark_node
)
10058 return error_mark_node
;
10059 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10063 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10064 parens
.require_close (parser
);
10066 /* Construct a location of the form:
10067 __is_trivially_copyable(_Tp)
10068 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10069 with start == caret, finishing at the close-paren. */
10070 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10072 /* Complete the trait expression, which may mean either processing
10073 the trait expr now or saving it for template instantiation. */
10076 case CPTK_UNDERLYING_TYPE
:
10077 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10079 return cp_expr (finish_bases (type1
, false), trait_loc
);
10080 case CPTK_DIRECT_BASES
:
10081 return cp_expr (finish_bases (type1
, true), trait_loc
);
10083 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10087 /* Parse a lambda expression.
10090 lambda-introducer lambda-declarator [opt] compound-statement
10092 Returns a representation of the expression. */
10095 cp_parser_lambda_expression (cp_parser
* parser
)
10097 tree lambda_expr
= build_lambda_expr ();
10100 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10101 cp_token_position start
= 0;
10103 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10105 if (cp_unevaluated_operand
)
10107 if (!token
->error_reported
)
10109 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10110 "lambda-expression in unevaluated context");
10111 token
->error_reported
= true;
10115 else if (parser
->in_template_argument_list_p
)
10117 if (!token
->error_reported
)
10119 error_at (token
->location
, "lambda-expression in template-argument");
10120 token
->error_reported
= true;
10125 /* We may be in the middle of deferred access check. Disable
10127 push_deferring_access_checks (dk_no_deferred
);
10129 cp_parser_lambda_introducer (parser
, lambda_expr
);
10131 type
= begin_lambda_type (lambda_expr
);
10132 if (type
== error_mark_node
)
10133 return error_mark_node
;
10135 record_lambda_scope (lambda_expr
);
10137 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10138 determine_visibility (TYPE_NAME (type
));
10140 /* Now that we've started the type, add the capture fields for any
10141 explicit captures. */
10142 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10145 /* Inside the class, surrounding template-parameter-lists do not apply. */
10146 unsigned int saved_num_template_parameter_lists
10147 = parser
->num_template_parameter_lists
;
10148 unsigned char in_statement
= parser
->in_statement
;
10149 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10150 bool fully_implicit_function_template_p
10151 = parser
->fully_implicit_function_template_p
;
10152 tree implicit_template_parms
= parser
->implicit_template_parms
;
10153 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10154 bool auto_is_implicit_function_template_parm_p
10155 = parser
->auto_is_implicit_function_template_parm_p
;
10157 parser
->num_template_parameter_lists
= 0;
10158 parser
->in_statement
= 0;
10159 parser
->in_switch_statement_p
= false;
10160 parser
->fully_implicit_function_template_p
= false;
10161 parser
->implicit_template_parms
= 0;
10162 parser
->implicit_template_scope
= 0;
10163 parser
->auto_is_implicit_function_template_parm_p
= false;
10165 /* By virtue of defining a local class, a lambda expression has access to
10166 the private variables of enclosing classes. */
10168 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10170 if (ok
&& cp_parser_error_occurred (parser
))
10175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10176 && cp_parser_start_tentative_firewall (parser
))
10178 cp_parser_lambda_body (parser
, lambda_expr
);
10180 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10182 if (cp_parser_skip_to_closing_brace (parser
))
10183 cp_lexer_consume_token (parser
->lexer
);
10186 /* The capture list was built up in reverse order; fix that now. */
10187 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10188 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10191 maybe_add_lambda_conv_op (type
);
10193 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10195 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10196 parser
->in_statement
= in_statement
;
10197 parser
->in_switch_statement_p
= in_switch_statement_p
;
10198 parser
->fully_implicit_function_template_p
10199 = fully_implicit_function_template_p
;
10200 parser
->implicit_template_parms
= implicit_template_parms
;
10201 parser
->implicit_template_scope
= implicit_template_scope
;
10202 parser
->auto_is_implicit_function_template_parm_p
10203 = auto_is_implicit_function_template_parm_p
;
10206 /* This field is only used during parsing of the lambda. */
10207 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10209 /* This lambda shouldn't have any proxies left at this point. */
10210 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10211 /* And now that we're done, push proxies for an enclosing lambda. */
10212 insert_pending_capture_proxies ();
10215 lambda_expr
= build_lambda_object (lambda_expr
);
10217 lambda_expr
= error_mark_node
;
10219 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10221 pop_deferring_access_checks ();
10223 return lambda_expr
;
10226 /* Parse the beginning of a lambda expression.
10229 [ lambda-capture [opt] ]
10231 LAMBDA_EXPR is the current representation of the lambda expression. */
10234 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10236 /* Need commas after the first capture. */
10239 /* Eat the leading `['. */
10240 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10242 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10244 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10245 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10246 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10247 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10249 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10251 cp_lexer_consume_token (parser
->lexer
);
10255 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10257 cp_token
* capture_token
;
10259 tree capture_init_expr
;
10260 cp_id_kind idk
= CP_ID_KIND_NONE
;
10261 bool explicit_init_p
= false;
10263 enum capture_kind_type
10268 enum capture_kind_type capture_kind
= BY_COPY
;
10270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10272 error ("expected end of capture-list");
10279 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10281 /* Possibly capture `this'. */
10282 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10284 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10285 if (cxx_dialect
< cxx2a
10286 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10287 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10288 "with by-copy capture default");
10289 cp_lexer_consume_token (parser
->lexer
);
10290 add_capture (lambda_expr
,
10291 /*id=*/this_identifier
,
10292 /*initializer=*/finish_this_expr (),
10293 /*by_reference_p=*/true,
10298 /* Possibly capture `*this'. */
10299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10300 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10302 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10303 if (cxx_dialect
< cxx17
)
10304 pedwarn (loc
, 0, "%<*this%> capture only available with "
10305 "-std=c++17 or -std=gnu++17");
10306 cp_lexer_consume_token (parser
->lexer
);
10307 cp_lexer_consume_token (parser
->lexer
);
10308 add_capture (lambda_expr
,
10309 /*id=*/this_identifier
,
10310 /*initializer=*/finish_this_expr (),
10311 /*by_reference_p=*/false,
10316 /* Remember whether we want to capture as a reference or not. */
10317 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10319 capture_kind
= BY_REFERENCE
;
10320 cp_lexer_consume_token (parser
->lexer
);
10323 /* Get the identifier. */
10324 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10325 capture_id
= cp_parser_identifier (parser
);
10327 if (capture_id
== error_mark_node
)
10328 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10329 delimiters, but I modified this to stop on unnested ']' as well. It
10330 was already changed to stop on unnested '}', so the
10331 "closing_parenthesis" name is no more misleading with my change. */
10333 cp_parser_skip_to_closing_parenthesis (parser
,
10334 /*recovering=*/true,
10336 /*consume_paren=*/true);
10340 /* Find the initializer for this capture. */
10341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10342 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10343 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10345 bool direct
, non_constant
;
10346 /* An explicit initializer exists. */
10347 if (cxx_dialect
< cxx14
)
10348 pedwarn (input_location
, 0,
10349 "lambda capture initializers "
10350 "only available with -std=c++14 or -std=gnu++14");
10351 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10353 explicit_init_p
= true;
10354 if (capture_init_expr
== NULL_TREE
)
10356 error ("empty initializer for lambda init-capture");
10357 capture_init_expr
= error_mark_node
;
10362 const char* error_msg
;
10364 /* Turn the identifier into an id-expression. */
10366 = cp_parser_lookup_name_simple (parser
, capture_id
,
10367 capture_token
->location
);
10369 if (capture_init_expr
== error_mark_node
)
10371 unqualified_name_lookup_error (capture_id
);
10374 else if (!VAR_P (capture_init_expr
)
10375 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10377 error_at (capture_token
->location
,
10378 "capture of non-variable %qE",
10379 capture_init_expr
);
10380 if (DECL_P (capture_init_expr
))
10381 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10382 "%q#D declared here", capture_init_expr
);
10385 if (VAR_P (capture_init_expr
)
10386 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10388 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10389 "%qD with non-automatic storage duration",
10390 capture_init_expr
))
10391 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10392 "%q#D declared here", capture_init_expr
);
10397 = finish_id_expression
10402 /*integral_constant_expression_p=*/false,
10403 /*allow_non_integral_constant_expression_p=*/false,
10404 /*non_integral_constant_expression_p=*/NULL
,
10405 /*template_p=*/false,
10407 /*address_p=*/false,
10408 /*template_arg_p=*/false,
10410 capture_token
->location
);
10412 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10414 cp_lexer_consume_token (parser
->lexer
);
10415 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10419 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10420 && !explicit_init_p
)
10422 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10423 && capture_kind
== BY_COPY
)
10424 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10425 "of %qD redundant with by-copy capture default",
10427 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10428 && capture_kind
== BY_REFERENCE
)
10429 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10430 "capture of %qD redundant with by-reference capture "
10431 "default", capture_id
);
10434 add_capture (lambda_expr
,
10437 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10440 /* If there is any qualification still in effect, clear it
10441 now; we will be starting fresh with the next capture. */
10442 parser
->scope
= NULL_TREE
;
10443 parser
->qualifying_scope
= NULL_TREE
;
10444 parser
->object_scope
= NULL_TREE
;
10447 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10450 /* Parse the (optional) middle of a lambda expression.
10453 < template-parameter-list [opt] >
10454 ( parameter-declaration-clause [opt] )
10455 attribute-specifier [opt]
10456 decl-specifier-seq [opt]
10457 exception-specification [opt]
10458 lambda-return-type-clause [opt]
10460 LAMBDA_EXPR is the current representation of the lambda expression. */
10463 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10465 /* 5.1.1.4 of the standard says:
10466 If a lambda-expression does not include a lambda-declarator, it is as if
10467 the lambda-declarator were ().
10468 This means an empty parameter list, no attributes, and no exception
10470 tree param_list
= void_list_node
;
10471 tree attributes
= NULL_TREE
;
10472 tree exception_spec
= NULL_TREE
;
10473 tree template_param_list
= NULL_TREE
;
10474 tree tx_qual
= NULL_TREE
;
10475 tree return_type
= NULL_TREE
;
10476 cp_decl_specifier_seq lambda_specs
;
10477 clear_decl_specs (&lambda_specs
);
10479 /* The template-parameter-list is optional, but must begin with
10480 an opening angle if present. */
10481 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10483 if (cxx_dialect
< cxx14
)
10484 pedwarn (parser
->lexer
->next_token
->location
, 0,
10485 "lambda templates are only available with "
10486 "-std=c++14 or -std=gnu++14");
10487 else if (cxx_dialect
< cxx2a
)
10488 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10489 "lambda templates are only available with "
10490 "-std=c++2a or -std=gnu++2a");
10492 cp_lexer_consume_token (parser
->lexer
);
10494 template_param_list
= cp_parser_template_parameter_list (parser
);
10496 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10498 /* We just processed one more parameter list. */
10499 ++parser
->num_template_parameter_lists
;
10502 /* The parameter-declaration-clause is optional (unless
10503 template-parameter-list was given), but must begin with an
10504 opening parenthesis if present. */
10505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10507 matching_parens parens
;
10508 parens
.consume_open (parser
);
10510 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10512 /* Parse parameters. */
10513 param_list
= cp_parser_parameter_declaration_clause (parser
);
10515 /* Default arguments shall not be specified in the
10516 parameter-declaration-clause of a lambda-declarator. */
10517 if (cxx_dialect
< cxx14
)
10518 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10519 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10520 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10521 "default argument specified for lambda parameter");
10523 parens
.require_close (parser
);
10525 attributes
= cp_parser_attributes_opt (parser
);
10527 /* In the decl-specifier-seq of the lambda-declarator, each
10528 decl-specifier shall either be mutable or constexpr. */
10529 int declares_class_or_enum
;
10530 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10531 cp_parser_decl_specifier_seq (parser
,
10532 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10533 &lambda_specs
, &declares_class_or_enum
);
10534 if (lambda_specs
.storage_class
== sc_mutable
)
10536 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10537 if (lambda_specs
.conflicting_specifiers_p
)
10538 error_at (lambda_specs
.locations
[ds_storage_class
],
10539 "duplicate %<mutable%>");
10542 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10544 /* Parse optional exception specification. */
10545 exception_spec
= cp_parser_exception_specification_opt (parser
);
10547 /* Parse optional trailing return type. */
10548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10550 cp_lexer_consume_token (parser
->lexer
);
10551 return_type
= cp_parser_trailing_type_id (parser
);
10554 /* The function parameters must be in scope all the way until after the
10555 trailing-return-type in case of decltype. */
10556 pop_bindings_and_leave_scope ();
10558 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10559 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10561 /* Create the function call operator.
10563 Messing with declarators like this is no uglier than building up the
10564 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10567 cp_decl_specifier_seq return_type_specs
;
10568 cp_declarator
* declarator
;
10573 clear_decl_specs (&return_type_specs
);
10575 return_type_specs
.type
= return_type
;
10577 /* Maybe we will deduce the return type later. */
10578 return_type_specs
.type
= make_auto ();
10580 if (lambda_specs
.locations
[ds_constexpr
])
10582 if (cxx_dialect
>= cxx17
)
10583 return_type_specs
.locations
[ds_constexpr
]
10584 = lambda_specs
.locations
[ds_constexpr
];
10586 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10587 "lambda only available with -std=c++17 or -std=gnu++17");
10590 p
= obstack_alloc (&declarator_obstack
, 0);
10592 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10594 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10595 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10596 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10597 VIRT_SPEC_UNSPECIFIED
,
10601 /*late_return_type=*/NULL_TREE
,
10602 /*requires_clause*/NULL_TREE
);
10603 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10605 fco
= grokmethod (&return_type_specs
,
10608 if (fco
!= error_mark_node
)
10610 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10611 DECL_ARTIFICIAL (fco
) = 1;
10612 /* Give the object parameter a different name. */
10613 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10615 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10617 if (template_param_list
)
10619 fco
= finish_member_template_decl (fco
);
10620 finish_template_decl (template_param_list
);
10621 --parser
->num_template_parameter_lists
;
10623 else if (parser
->fully_implicit_function_template_p
)
10624 fco
= finish_fully_implicit_template (parser
, fco
);
10626 finish_member_declaration (fco
);
10628 obstack_free (&declarator_obstack
, p
);
10630 return (fco
!= error_mark_node
);
10634 /* Parse the body of a lambda expression, which is simply
10638 but which requires special handling.
10639 LAMBDA_EXPR is the current representation of the lambda expression. */
10642 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10644 bool nested
= (current_function_decl
!= NULL_TREE
);
10645 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10646 bool in_function_body
= parser
->in_function_body
;
10649 push_function_context ();
10651 /* Still increment function_depth so that we don't GC in the
10652 middle of an expression. */
10655 vec
<tree
> omp_privatization_save
;
10656 save_omp_privatization_clauses (omp_privatization_save
);
10657 /* Clear this in case we're in the middle of a default argument. */
10658 parser
->local_variables_forbidden_p
= false;
10659 parser
->in_function_body
= true;
10662 local_specialization_stack
s (lss_copy
);
10663 tree fco
= lambda_function (lambda_expr
);
10664 tree body
= start_lambda_function (fco
, lambda_expr
);
10665 matching_braces braces
;
10667 if (braces
.require_open (parser
))
10669 tree compound_stmt
= begin_compound_stmt (0);
10671 /* Originally C++11 required us to peek for 'return expr'; and
10672 process it specially here to deduce the return type. N3638
10673 removed the need for that. */
10675 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10676 cp_parser_label_declaration (parser
);
10677 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10678 braces
.require_close (parser
);
10680 finish_compound_stmt (compound_stmt
);
10683 finish_lambda_function (body
);
10686 restore_omp_privatization_clauses (omp_privatization_save
);
10687 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10688 parser
->in_function_body
= in_function_body
;
10690 pop_function_context();
10695 /* Statements [gram.stmt.stmt] */
10697 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10700 add_debug_begin_stmt (location_t loc
)
10702 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10704 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10705 /* A concept is never expanded normally. */
10708 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10709 SET_EXPR_LOCATION (stmt
, loc
);
10713 /* Parse a statement.
10717 expression-statement
10719 selection-statement
10720 iteration-statement
10722 declaration-statement
10729 attribute-specifier-seq (opt) expression-statement
10730 attribute-specifier-seq (opt) compound-statement
10731 attribute-specifier-seq (opt) selection-statement
10732 attribute-specifier-seq (opt) iteration-statement
10733 attribute-specifier-seq (opt) jump-statement
10734 declaration-statement
10735 attribute-specifier-seq (opt) try-block
10738 expression-statement
10746 IN_COMPOUND is true when the statement is nested inside a
10747 cp_parser_compound_statement; this matters for certain pragmas.
10749 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10750 is a (possibly labeled) if statement which is not enclosed in braces
10751 and has an else clause. This is used to implement -Wparentheses.
10753 CHAIN is a vector of if-else-if conditions. */
10756 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10757 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10758 location_t
*loc_after_labels
)
10760 tree statement
, std_attrs
= NULL_TREE
;
10762 location_t statement_location
, attrs_location
;
10767 /* There is no statement yet. */
10768 statement
= NULL_TREE
;
10770 saved_token_sentinel
saved_tokens (parser
->lexer
);
10771 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10772 if (c_dialect_objc ())
10773 /* In obj-c++, seeing '[[' might be the either the beginning of
10774 c++11 attributes, or a nested objc-message-expression. So
10775 let's parse the c++11 attributes tentatively. */
10776 cp_parser_parse_tentatively (parser
);
10777 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10778 if (c_dialect_objc ())
10780 if (!cp_parser_parse_definitely (parser
))
10781 std_attrs
= NULL_TREE
;
10784 /* Peek at the next token. */
10785 token
= cp_lexer_peek_token (parser
->lexer
);
10786 /* Remember the location of the first token in the statement. */
10787 statement_location
= token
->location
;
10788 add_debug_begin_stmt (statement_location
);
10789 /* If this is a keyword, then that will often determine what kind of
10790 statement we have. */
10791 if (token
->type
== CPP_KEYWORD
)
10793 enum rid keyword
= token
->keyword
;
10799 /* Looks like a labeled-statement with a case label.
10800 Parse the label, and then use tail recursion to parse
10802 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10803 in_compound
= false;
10808 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10814 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10821 statement
= cp_parser_jump_statement (parser
);
10824 /* Objective-C++ exception-handling constructs. */
10827 case RID_AT_FINALLY
:
10828 case RID_AT_SYNCHRONIZED
:
10830 statement
= cp_parser_objc_statement (parser
);
10834 statement
= cp_parser_try_block (parser
);
10837 case RID_NAMESPACE
:
10838 /* This must be a namespace alias definition. */
10839 cp_parser_declaration_statement (parser
);
10842 case RID_TRANSACTION_ATOMIC
:
10843 case RID_TRANSACTION_RELAXED
:
10844 case RID_SYNCHRONIZED
:
10845 case RID_ATOMIC_NOEXCEPT
:
10846 case RID_ATOMIC_CANCEL
:
10847 statement
= cp_parser_transaction (parser
, token
);
10849 case RID_TRANSACTION_CANCEL
:
10850 statement
= cp_parser_transaction_cancel (parser
);
10854 /* It might be a keyword like `int' that can start a
10855 declaration-statement. */
10859 else if (token
->type
== CPP_NAME
)
10861 /* If the next token is a `:', then we are looking at a
10862 labeled-statement. */
10863 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10864 if (token
->type
== CPP_COLON
)
10866 /* Looks like a labeled-statement with an ordinary label.
10867 Parse the label, and then use tail recursion to parse
10870 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10871 in_compound
= false;
10875 /* Anything that starts with a `{' must be a compound-statement. */
10876 else if (token
->type
== CPP_OPEN_BRACE
)
10877 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10878 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10879 a statement all its own. */
10880 else if (token
->type
== CPP_PRAGMA
)
10882 /* Only certain OpenMP pragmas are attached to statements, and thus
10883 are considered statements themselves. All others are not. In
10884 the context of a compound, accept the pragma as a "statement" and
10885 return so that we can check for a close brace. Otherwise we
10886 require a real statement and must go back and read one. */
10888 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10889 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10893 else if (token
->type
== CPP_EOF
)
10895 cp_parser_error (parser
, "expected statement");
10899 /* Everything else must be a declaration-statement or an
10900 expression-statement. Try for the declaration-statement
10901 first, unless we are looking at a `;', in which case we know that
10902 we have an expression-statement. */
10905 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10907 if (std_attrs
!= NULL_TREE
)
10909 /* Attributes should be parsed as part of the the
10910 declaration, so let's un-parse them. */
10911 saved_tokens
.rollback();
10912 std_attrs
= NULL_TREE
;
10915 cp_parser_parse_tentatively (parser
);
10916 /* Try to parse the declaration-statement. */
10917 cp_parser_declaration_statement (parser
);
10918 /* If that worked, we're done. */
10919 if (cp_parser_parse_definitely (parser
))
10922 /* All preceding labels have been parsed at this point. */
10923 if (loc_after_labels
!= NULL
)
10924 *loc_after_labels
= statement_location
;
10926 /* Look for an expression-statement instead. */
10927 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10929 /* Handle [[fallthrough]];. */
10930 if (attribute_fallthrough_p (std_attrs
))
10932 /* The next token after the fallthrough attribute is ';'. */
10933 if (statement
== NULL_TREE
)
10935 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10936 statement
= build_call_expr_internal_loc (statement_location
,
10938 void_type_node
, 0);
10939 finish_expr_stmt (statement
);
10942 warning_at (statement_location
, OPT_Wattributes
,
10943 "%<fallthrough%> attribute not followed by %<;%>");
10944 std_attrs
= NULL_TREE
;
10948 /* Set the line number for the statement. */
10949 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10950 SET_EXPR_LOCATION (statement
, statement_location
);
10952 /* Allow "[[fallthrough]];", but warn otherwise. */
10953 if (std_attrs
!= NULL_TREE
)
10954 warning_at (attrs_location
,
10956 "attributes at the beginning of statement are ignored");
10959 /* Append ATTR to attribute list ATTRS. */
10962 attr_chainon (tree attrs
, tree attr
)
10964 if (attrs
== error_mark_node
)
10965 return error_mark_node
;
10966 if (attr
== error_mark_node
)
10967 return error_mark_node
;
10968 return chainon (attrs
, attr
);
10971 /* Parse the label for a labeled-statement, i.e.
10974 case constant-expression :
10978 case constant-expression ... constant-expression : statement
10980 When a label is parsed without errors, the label is added to the
10981 parse tree by the finish_* functions, so this function doesn't
10982 have to return the label. */
10985 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10988 tree label
= NULL_TREE
;
10989 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10991 /* The next token should be an identifier. */
10992 token
= cp_lexer_peek_token (parser
->lexer
);
10993 if (token
->type
!= CPP_NAME
10994 && token
->type
!= CPP_KEYWORD
)
10996 cp_parser_error (parser
, "expected labeled-statement");
11000 /* Remember whether this case or a user-defined label is allowed to fall
11002 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11004 parser
->colon_corrects_to_scope_p
= false;
11005 switch (token
->keyword
)
11009 tree expr
, expr_hi
;
11010 cp_token
*ellipsis
;
11012 /* Consume the `case' token. */
11013 cp_lexer_consume_token (parser
->lexer
);
11014 /* Parse the constant-expression. */
11015 expr
= cp_parser_constant_expression (parser
);
11016 if (check_for_bare_parameter_packs (expr
))
11017 expr
= error_mark_node
;
11019 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11020 if (ellipsis
->type
== CPP_ELLIPSIS
)
11022 /* Consume the `...' token. */
11023 cp_lexer_consume_token (parser
->lexer
);
11024 expr_hi
= cp_parser_constant_expression (parser
);
11025 if (check_for_bare_parameter_packs (expr_hi
))
11026 expr_hi
= error_mark_node
;
11028 /* We don't need to emit warnings here, as the common code
11029 will do this for us. */
11032 expr_hi
= NULL_TREE
;
11034 if (parser
->in_switch_statement_p
)
11036 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11037 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11038 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11041 error_at (token
->location
,
11042 "case label %qE not within a switch statement",
11048 /* Consume the `default' token. */
11049 cp_lexer_consume_token (parser
->lexer
);
11051 if (parser
->in_switch_statement_p
)
11053 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11054 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11055 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11058 error_at (token
->location
, "case label not within a switch statement");
11062 /* Anything else must be an ordinary label. */
11063 label
= finish_label_stmt (cp_parser_identifier (parser
));
11064 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11065 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11069 /* Require the `:' token. */
11070 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11072 /* An ordinary label may optionally be followed by attributes.
11073 However, this is only permitted if the attributes are then
11074 followed by a semicolon. This is because, for backward
11075 compatibility, when parsing
11076 lab: __attribute__ ((unused)) int i;
11077 we want the attribute to attach to "i", not "lab". */
11078 if (label
!= NULL_TREE
11079 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11082 cp_parser_parse_tentatively (parser
);
11083 attrs
= cp_parser_gnu_attributes_opt (parser
);
11084 if (attrs
== NULL_TREE
11085 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11086 cp_parser_abort_tentative_parse (parser
);
11087 else if (!cp_parser_parse_definitely (parser
))
11090 attributes
= attr_chainon (attributes
, attrs
);
11093 if (attributes
!= NULL_TREE
)
11094 cplus_decl_attributes (&label
, attributes
, 0);
11096 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11099 /* Parse an expression-statement.
11101 expression-statement:
11104 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11105 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11106 indicates whether this expression-statement is part of an
11107 expression statement. */
11110 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11112 tree statement
= NULL_TREE
;
11113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11114 location_t loc
= token
->location
;
11116 /* There might be attribute fallthrough. */
11117 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11119 /* If the next token is a ';', then there is no expression
11121 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11123 statement
= cp_parser_expression (parser
);
11124 if (statement
== error_mark_node
11125 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11127 cp_parser_skip_to_end_of_block_or_statement (parser
);
11128 return error_mark_node
;
11132 /* Handle [[fallthrough]];. */
11133 if (attribute_fallthrough_p (attr
))
11135 /* The next token after the fallthrough attribute is ';'. */
11136 if (statement
== NULL_TREE
)
11137 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11138 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11139 void_type_node
, 0);
11141 warning_at (loc
, OPT_Wattributes
,
11142 "%<fallthrough%> attribute not followed by %<;%>");
11146 /* Allow "[[fallthrough]];", but warn otherwise. */
11147 if (attr
!= NULL_TREE
)
11148 warning_at (loc
, OPT_Wattributes
,
11149 "attributes at the beginning of statement are ignored");
11151 /* Give a helpful message for "A<T>::type t;" and the like. */
11152 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11153 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11155 if (TREE_CODE (statement
) == SCOPE_REF
)
11156 error_at (token
->location
, "need %<typename%> before %qE because "
11157 "%qT is a dependent scope",
11158 statement
, TREE_OPERAND (statement
, 0));
11159 else if (is_overloaded_fn (statement
)
11160 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11163 tree fn
= get_first_fn (statement
);
11164 error_at (token
->location
,
11165 "%<%T::%D%> names the constructor, not the type",
11166 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11170 /* Consume the final `;'. */
11171 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11173 if (in_statement_expr
11174 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11175 /* This is the final expression statement of a statement
11177 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11178 else if (statement
)
11179 statement
= finish_expr_stmt (statement
);
11184 /* Parse a compound-statement.
11186 compound-statement:
11187 { statement-seq [opt] }
11191 compound-statement:
11192 { label-declaration-seq [opt] statement-seq [opt] }
11194 label-declaration-seq:
11196 label-declaration-seq label-declaration
11198 Returns a tree representing the statement. */
11201 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11202 int bcs_flags
, bool function_body
)
11204 tree compound_stmt
;
11205 matching_braces braces
;
11207 /* Consume the `{'. */
11208 if (!braces
.require_open (parser
))
11209 return error_mark_node
;
11210 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11211 && !function_body
&& cxx_dialect
< cxx14
)
11212 pedwarn (input_location
, OPT_Wpedantic
,
11213 "compound-statement in %<constexpr%> function");
11214 /* Begin the compound-statement. */
11215 compound_stmt
= begin_compound_stmt (bcs_flags
);
11216 /* If the next keyword is `__label__' we have a label declaration. */
11217 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11218 cp_parser_label_declaration (parser
);
11219 /* Parse an (optional) statement-seq. */
11220 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11221 /* Finish the compound-statement. */
11222 finish_compound_stmt (compound_stmt
);
11223 /* Consume the `}'. */
11224 braces
.require_close (parser
);
11226 return compound_stmt
;
11229 /* Parse an (optional) statement-seq.
11233 statement-seq [opt] statement */
11236 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11238 /* Scan statements until there aren't any more. */
11241 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11243 /* If we are looking at a `}', then we have run out of
11244 statements; the same is true if we have reached the end
11245 of file, or have stumbled upon a stray '@end'. */
11246 if (token
->type
== CPP_CLOSE_BRACE
11247 || token
->type
== CPP_EOF
11248 || token
->type
== CPP_PRAGMA_EOL
11249 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11252 /* If we are in a compound statement and find 'else' then
11253 something went wrong. */
11254 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11256 if (parser
->in_statement
& IN_IF_STMT
)
11260 token
= cp_lexer_consume_token (parser
->lexer
);
11261 error_at (token
->location
, "%<else%> without a previous %<if%>");
11265 /* Parse the statement. */
11266 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11270 /* Return true if we're looking at (init; cond), false otherwise. */
11273 cp_parser_init_statement_p (cp_parser
*parser
)
11275 /* Save tokens so that we can put them back. */
11276 cp_lexer_save_tokens (parser
->lexer
);
11278 /* Look for ';' that is not nested in () or {}. */
11279 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11280 /*recovering=*/false,
11282 /*consume_paren=*/false);
11284 /* Roll back the tokens we skipped. */
11285 cp_lexer_rollback_tokens (parser
->lexer
);
11290 /* Parse a selection-statement.
11292 selection-statement:
11293 if ( init-statement [opt] condition ) statement
11294 if ( init-statement [opt] condition ) statement else statement
11295 switch ( init-statement [opt] condition ) statement
11297 Returns the new IF_STMT or SWITCH_STMT.
11299 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11300 is a (possibly labeled) if statement which is not enclosed in
11301 braces and has an else clause. This is used to implement
11304 CHAIN is a vector of if-else-if conditions. This is used to implement
11305 -Wduplicated-cond. */
11308 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11313 token_indent_info guard_tinfo
;
11318 /* Peek at the next token. */
11319 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11320 guard_tinfo
= get_token_indent_info (token
);
11322 /* See what kind of keyword it is. */
11323 keyword
= token
->keyword
;
11333 if (keyword
== RID_IF
11334 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11338 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11339 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11340 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11341 "with -std=c++17 or -std=gnu++17");
11344 /* Look for the `('. */
11345 matching_parens parens
;
11346 if (!parens
.require_open (parser
))
11348 cp_parser_skip_to_end_of_statement (parser
);
11349 return error_mark_node
;
11352 /* Begin the selection-statement. */
11353 if (keyword
== RID_IF
)
11355 statement
= begin_if_stmt ();
11356 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11359 statement
= begin_switch_stmt ();
11361 /* Parse the optional init-statement. */
11362 if (cp_parser_init_statement_p (parser
))
11365 if (cxx_dialect
< cxx17
)
11366 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11367 "init-statement in selection statements only available "
11368 "with -std=c++17 or -std=gnu++17");
11369 cp_parser_init_statement (parser
, &decl
);
11372 /* Parse the condition. */
11373 condition
= cp_parser_condition (parser
);
11374 /* Look for the `)'. */
11375 if (!parens
.require_close (parser
))
11376 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11377 /*consume_paren=*/true);
11379 if (keyword
== RID_IF
)
11382 unsigned char in_statement
;
11384 /* Add the condition. */
11385 condition
= finish_if_stmt_cond (condition
, statement
);
11387 if (warn_duplicated_cond
)
11388 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11391 /* Parse the then-clause. */
11392 in_statement
= parser
->in_statement
;
11393 parser
->in_statement
|= IN_IF_STMT
;
11395 /* Outside a template, the non-selected branch of a constexpr
11396 if is a 'discarded statement', i.e. unevaluated. */
11397 bool was_discarded
= in_discarded_stmt
;
11398 bool discard_then
= (cx
&& !processing_template_decl
11399 && integer_zerop (condition
));
11402 in_discarded_stmt
= true;
11403 ++c_inhibit_evaluation_warnings
;
11406 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11409 parser
->in_statement
= in_statement
;
11411 finish_then_clause (statement
);
11415 THEN_CLAUSE (statement
) = NULL_TREE
;
11416 in_discarded_stmt
= was_discarded
;
11417 --c_inhibit_evaluation_warnings
;
11420 /* If the next token is `else', parse the else-clause. */
11421 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11424 bool discard_else
= (cx
&& !processing_template_decl
11425 && integer_nonzerop (condition
));
11428 in_discarded_stmt
= true;
11429 ++c_inhibit_evaluation_warnings
;
11433 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11434 /* Consume the `else' keyword. */
11435 cp_lexer_consume_token (parser
->lexer
);
11436 if (warn_duplicated_cond
)
11438 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11442 /* We've got "if (COND) else if (COND2)". Start
11443 the condition chain and add COND as the first
11445 chain
= new vec
<tree
> ();
11446 if (!CONSTANT_CLASS_P (condition
)
11447 && !TREE_SIDE_EFFECTS (condition
))
11449 /* Wrap it in a NOP_EXPR so that we can set the
11450 location of the condition. */
11451 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11453 SET_EXPR_LOCATION (e
, token
->location
);
11454 chain
->safe_push (e
);
11457 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11460 /* This is if-else without subsequent if. Zap the
11461 condition chain; we would have already warned at
11467 begin_else_clause (statement
);
11468 /* Parse the else-clause. */
11469 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11470 guard_tinfo
, chain
);
11472 finish_else_clause (statement
);
11474 /* If we are currently parsing a then-clause, then
11475 IF_P will not be NULL. We set it to true to
11476 indicate that this if statement has an else clause.
11477 This may trigger the Wparentheses warning below
11478 when we get back up to the parent if statement. */
11484 ELSE_CLAUSE (statement
) = NULL_TREE
;
11485 in_discarded_stmt
= was_discarded
;
11486 --c_inhibit_evaluation_warnings
;
11491 /* This if statement does not have an else clause. If
11492 NESTED_IF is true, then the then-clause has an if
11493 statement which does have an else clause. We warn
11494 about the potential ambiguity. */
11496 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11497 "suggest explicit braces to avoid ambiguous"
11499 if (warn_duplicated_cond
)
11501 /* We don't need the condition chain anymore. */
11507 /* Now we're all done with the if-statement. */
11508 finish_if_stmt (statement
);
11512 bool in_switch_statement_p
;
11513 unsigned char in_statement
;
11515 /* Add the condition. */
11516 finish_switch_cond (condition
, statement
);
11518 /* Parse the body of the switch-statement. */
11519 in_switch_statement_p
= parser
->in_switch_statement_p
;
11520 in_statement
= parser
->in_statement
;
11521 parser
->in_switch_statement_p
= true;
11522 parser
->in_statement
|= IN_SWITCH_STMT
;
11523 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11525 parser
->in_switch_statement_p
= in_switch_statement_p
;
11526 parser
->in_statement
= in_statement
;
11528 /* Now we're all done with the switch-statement. */
11529 finish_switch_stmt (statement
);
11537 cp_parser_error (parser
, "expected selection-statement");
11538 return error_mark_node
;
11542 /* Parse a condition.
11546 type-specifier-seq declarator = initializer-clause
11547 type-specifier-seq declarator braced-init-list
11552 type-specifier-seq declarator asm-specification [opt]
11553 attributes [opt] = assignment-expression
11555 Returns the expression that should be tested. */
11558 cp_parser_condition (cp_parser
* parser
)
11560 cp_decl_specifier_seq type_specifiers
;
11561 const char *saved_message
;
11562 int declares_class_or_enum
;
11564 /* Try the declaration first. */
11565 cp_parser_parse_tentatively (parser
);
11566 /* New types are not allowed in the type-specifier-seq for a
11568 saved_message
= parser
->type_definition_forbidden_message
;
11569 parser
->type_definition_forbidden_message
11570 = G_("types may not be defined in conditions");
11571 /* Parse the type-specifier-seq. */
11572 cp_parser_decl_specifier_seq (parser
,
11573 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11575 &declares_class_or_enum
);
11576 /* Restore the saved message. */
11577 parser
->type_definition_forbidden_message
= saved_message
;
11578 /* If all is well, we might be looking at a declaration. */
11579 if (!cp_parser_error_occurred (parser
))
11582 tree asm_specification
;
11584 cp_declarator
*declarator
;
11585 tree initializer
= NULL_TREE
;
11587 /* Parse the declarator. */
11588 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11589 /*ctor_dtor_or_conv_p=*/NULL
,
11590 /*parenthesized_p=*/NULL
,
11591 /*member_p=*/false,
11592 /*friend_p=*/false);
11593 /* Parse the attributes. */
11594 attributes
= cp_parser_attributes_opt (parser
);
11595 /* Parse the asm-specification. */
11596 asm_specification
= cp_parser_asm_specification_opt (parser
);
11597 /* If the next token is not an `=' or '{', then we might still be
11598 looking at an expression. For example:
11602 looks like a decl-specifier-seq and a declarator -- but then
11603 there is no `=', so this is an expression. */
11604 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11605 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11606 cp_parser_simulate_error (parser
);
11608 /* If we did see an `=' or '{', then we are looking at a declaration
11610 if (cp_parser_parse_definitely (parser
))
11613 bool non_constant_p
;
11614 int flags
= LOOKUP_ONLYCONVERTING
;
11616 /* Create the declaration. */
11617 decl
= start_decl (declarator
, &type_specifiers
,
11618 /*initialized_p=*/true,
11619 attributes
, /*prefix_attributes=*/NULL_TREE
,
11622 /* Parse the initializer. */
11623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11625 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11626 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11631 /* Consume the `='. */
11632 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11633 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11635 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11636 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11638 /* Process the initializer. */
11639 cp_finish_decl (decl
,
11640 initializer
, !non_constant_p
,
11645 pop_scope (pushed_scope
);
11647 return convert_from_reference (decl
);
11650 /* If we didn't even get past the declarator successfully, we are
11651 definitely not looking at a declaration. */
11653 cp_parser_abort_tentative_parse (parser
);
11655 /* Otherwise, we are looking at an expression. */
11656 return cp_parser_expression (parser
);
11659 /* Parses a for-statement or range-for-statement until the closing ')',
11663 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11665 tree init
, scope
, decl
;
11668 /* Begin the for-statement. */
11669 scope
= begin_for_scope (&init
);
11671 /* Parse the initialization. */
11672 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11675 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11677 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11681 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11682 unsigned short unroll
)
11684 /* Normal for loop */
11685 tree condition
= NULL_TREE
;
11686 tree expression
= NULL_TREE
;
11689 stmt
= begin_for_stmt (scope
, init
);
11690 /* The init-statement has already been parsed in
11691 cp_parser_init_statement, so no work is needed here. */
11692 finish_init_stmt (stmt
);
11694 /* If there's a condition, process it. */
11695 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11696 condition
= cp_parser_condition (parser
);
11699 cp_parser_error (parser
, "missing loop condition in loop with "
11700 "%<GCC ivdep%> pragma");
11701 condition
= error_mark_node
;
11705 cp_parser_error (parser
, "missing loop condition in loop with "
11706 "%<GCC unroll%> pragma");
11707 condition
= error_mark_node
;
11709 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11710 /* Look for the `;'. */
11711 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11713 /* If there's an expression, process it. */
11714 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11715 expression
= cp_parser_expression (parser
);
11716 finish_for_expr (expression
, stmt
);
11721 /* Tries to parse a range-based for-statement:
11724 decl-specifier-seq declarator : expression
11726 The decl-specifier-seq declarator and the `:' are already parsed by
11727 cp_parser_init_statement. If processing_template_decl it returns a
11728 newly created RANGE_FOR_STMT; if not, it is converted to a
11729 regular FOR_STMT. */
11732 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11733 bool ivdep
, unsigned short unroll
)
11735 tree stmt
, range_expr
;
11736 auto_vec
<cxx_binding
*, 16> bindings
;
11737 auto_vec
<tree
, 16> names
;
11738 tree decomp_first_name
= NULL_TREE
;
11739 unsigned int decomp_cnt
= 0;
11741 /* Get the range declaration momentarily out of the way so that
11742 the range expression doesn't clash with it. */
11743 if (range_decl
!= error_mark_node
)
11745 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11747 tree v
= DECL_VALUE_EXPR (range_decl
);
11748 /* For decomposition declaration get all of the corresponding
11749 declarations out of the way. */
11750 if (TREE_CODE (v
) == ARRAY_REF
11751 && VAR_P (TREE_OPERAND (v
, 0))
11752 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11754 tree d
= range_decl
;
11755 range_decl
= TREE_OPERAND (v
, 0);
11756 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11757 decomp_first_name
= d
;
11758 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11760 tree name
= DECL_NAME (d
);
11761 names
.safe_push (name
);
11762 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11763 IDENTIFIER_BINDING (name
)
11764 = IDENTIFIER_BINDING (name
)->previous
;
11768 if (names
.is_empty ())
11770 tree name
= DECL_NAME (range_decl
);
11771 names
.safe_push (name
);
11772 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11773 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11777 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11779 bool expr_non_constant_p
;
11780 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11783 range_expr
= cp_parser_expression (parser
);
11785 /* Put the range declaration(s) back into scope. */
11786 for (unsigned int i
= 0; i
< names
.length (); i
++)
11788 cxx_binding
*binding
= bindings
[i
];
11789 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11790 IDENTIFIER_BINDING (names
[i
]) = binding
;
11793 /* If in template, STMT is converted to a normal for-statement
11794 at instantiation. If not, it is done just ahead. */
11795 if (processing_template_decl
)
11797 if (check_for_bare_parameter_packs (range_expr
))
11798 range_expr
= error_mark_node
;
11799 stmt
= begin_range_for_stmt (scope
, init
);
11801 RANGE_FOR_IVDEP (stmt
) = 1;
11803 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11804 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11805 if (!type_dependent_expression_p (range_expr
)
11806 /* do_auto_deduction doesn't mess with template init-lists. */
11807 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11808 do_range_for_auto_deduction (range_decl
, range_expr
);
11812 stmt
= begin_for_stmt (scope
, init
);
11813 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11814 decomp_first_name
, decomp_cnt
, ivdep
,
11820 /* Subroutine of cp_convert_range_for: given the initializer expression,
11821 builds up the range temporary. */
11824 build_range_temp (tree range_expr
)
11826 tree range_type
, range_temp
;
11828 /* Find out the type deduced by the declaration
11829 `auto &&__range = range_expr'. */
11830 range_type
= cp_build_reference_type (make_auto (), true);
11831 range_type
= do_auto_deduction (range_type
, range_expr
,
11832 type_uses_auto (range_type
));
11834 /* Create the __range variable. */
11835 range_temp
= build_decl (input_location
, VAR_DECL
,
11836 get_identifier ("__for_range"), range_type
);
11837 TREE_USED (range_temp
) = 1;
11838 DECL_ARTIFICIAL (range_temp
) = 1;
11843 /* Used by cp_parser_range_for in template context: we aren't going to
11844 do a full conversion yet, but we still need to resolve auto in the
11845 type of the for-range-declaration if present. This is basically
11846 a shortcut version of cp_convert_range_for. */
11849 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11851 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11854 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11855 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11856 iter_type
= (cp_parser_perform_range_for_lookup
11857 (range_temp
, &begin_dummy
, &end_dummy
));
11860 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11862 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11864 tf_warning_or_error
);
11865 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11866 iter_decl
, auto_node
);
11871 /* Converts a range-based for-statement into a normal
11872 for-statement, as per the definition.
11874 for (RANGE_DECL : RANGE_EXPR)
11877 should be equivalent to:
11880 auto &&__range = RANGE_EXPR;
11881 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11885 RANGE_DECL = *__begin;
11890 If RANGE_EXPR is an array:
11891 BEGIN_EXPR = __range
11892 END_EXPR = __range + ARRAY_SIZE(__range)
11893 Else if RANGE_EXPR has a member 'begin' or 'end':
11894 BEGIN_EXPR = __range.begin()
11895 END_EXPR = __range.end()
11897 BEGIN_EXPR = begin(__range)
11898 END_EXPR = end(__range);
11900 If __range has a member 'begin' but not 'end', or vice versa, we must
11901 still use the second alternative (it will surely fail, however).
11902 When calling begin()/end() in the third alternative we must use
11903 argument dependent lookup, but always considering 'std' as an associated
11907 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11908 tree decomp_first_name
, unsigned int decomp_cnt
,
11909 bool ivdep
, unsigned short unroll
)
11912 tree iter_type
, begin_expr
, end_expr
;
11913 tree condition
, expression
;
11915 range_expr
= mark_lvalue_use (range_expr
);
11917 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11918 /* If an error happened previously do nothing or else a lot of
11919 unhelpful errors would be issued. */
11920 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11925 if (VAR_P (range_expr
)
11926 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11927 /* Can't bind a reference to an array of runtime bound. */
11928 range_temp
= range_expr
;
11931 range_temp
= build_range_temp (range_expr
);
11932 pushdecl (range_temp
);
11933 cp_finish_decl (range_temp
, range_expr
,
11934 /*is_constant_init*/false, NULL_TREE
,
11935 LOOKUP_ONLYCONVERTING
);
11936 range_temp
= convert_from_reference (range_temp
);
11938 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11939 &begin_expr
, &end_expr
);
11942 /* The new for initialization statement. */
11943 begin
= build_decl (input_location
, VAR_DECL
,
11944 get_identifier ("__for_begin"), iter_type
);
11945 TREE_USED (begin
) = 1;
11946 DECL_ARTIFICIAL (begin
) = 1;
11948 cp_finish_decl (begin
, begin_expr
,
11949 /*is_constant_init*/false, NULL_TREE
,
11950 LOOKUP_ONLYCONVERTING
);
11952 if (cxx_dialect
>= cxx17
)
11953 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11954 end
= build_decl (input_location
, VAR_DECL
,
11955 get_identifier ("__for_end"), iter_type
);
11956 TREE_USED (end
) = 1;
11957 DECL_ARTIFICIAL (end
) = 1;
11959 cp_finish_decl (end
, end_expr
,
11960 /*is_constant_init*/false, NULL_TREE
,
11961 LOOKUP_ONLYCONVERTING
);
11963 finish_init_stmt (statement
);
11965 /* The new for condition. */
11966 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11969 NULL
, tf_warning_or_error
);
11970 finish_for_cond (condition
, statement
, ivdep
, unroll
);
11972 /* The new increment expression. */
11973 expression
= finish_unary_op_expr (input_location
,
11974 PREINCREMENT_EXPR
, begin
,
11975 tf_warning_or_error
);
11976 finish_for_expr (expression
, statement
);
11978 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11979 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11981 /* The declaration is initialized with *__begin inside the loop body. */
11982 cp_finish_decl (range_decl
,
11983 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11984 tf_warning_or_error
),
11985 /*is_constant_init*/false, NULL_TREE
,
11986 LOOKUP_ONLYCONVERTING
);
11987 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11988 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11993 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11994 We need to solve both at the same time because the method used
11995 depends on the existence of members begin or end.
11996 Returns the type deduced for the iterator expression. */
11999 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12001 if (error_operand_p (range
))
12003 *begin
= *end
= error_mark_node
;
12004 return error_mark_node
;
12007 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12009 error ("range-based %<for%> expression of type %qT "
12010 "has incomplete type", TREE_TYPE (range
));
12011 *begin
= *end
= error_mark_node
;
12012 return error_mark_node
;
12014 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12016 /* If RANGE is an array, we will use pointer arithmetic. */
12017 *begin
= decay_conversion (range
, tf_warning_or_error
);
12018 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12020 array_type_nelts_top (TREE_TYPE (range
)),
12022 return TREE_TYPE (*begin
);
12026 /* If it is not an array, we must do a bit of magic. */
12027 tree id_begin
, id_end
;
12028 tree member_begin
, member_end
;
12030 *begin
= *end
= error_mark_node
;
12032 id_begin
= get_identifier ("begin");
12033 id_end
= get_identifier ("end");
12034 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12035 /*protect=*/2, /*want_type=*/false,
12036 tf_warning_or_error
);
12037 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12038 /*protect=*/2, /*want_type=*/false,
12039 tf_warning_or_error
);
12041 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12043 /* Use the member functions. */
12044 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12045 *end
= cp_parser_range_for_member_function (range
, id_end
);
12049 /* Use global functions with ADL. */
12050 vec
<tree
, va_gc
> *vec
;
12051 vec
= make_tree_vector ();
12053 vec_safe_push (vec
, range
);
12055 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12056 tf_warning_or_error
);
12057 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12058 tf_warning_or_error
);
12059 member_end
= perform_koenig_lookup (id_end
, vec
,
12060 tf_warning_or_error
);
12061 *end
= finish_call_expr (member_end
, &vec
, false, true,
12062 tf_warning_or_error
);
12064 release_tree_vector (vec
);
12067 /* Last common checks. */
12068 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12070 /* If one of the expressions is an error do no more checks. */
12071 *begin
= *end
= error_mark_node
;
12072 return error_mark_node
;
12074 else if (type_dependent_expression_p (*begin
)
12075 || type_dependent_expression_p (*end
))
12076 /* Can happen, when, eg, in a template context, Koenig lookup
12077 can't resolve begin/end (c++/58503). */
12081 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12082 /* The unqualified type of the __begin and __end temporaries should
12083 be the same, as required by the multiple auto declaration. */
12084 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12086 if (cxx_dialect
>= cxx17
12087 && (build_x_binary_op (input_location
, NE_EXPR
,
12088 *begin
, ERROR_MARK
,
12091 != error_mark_node
))
12092 /* P0184R0 allows __begin and __end to have different types,
12093 but make sure they are comparable so we can give a better
12096 error ("inconsistent begin/end types in range-based %<for%> "
12097 "statement: %qT and %qT",
12098 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12105 /* Helper function for cp_parser_perform_range_for_lookup.
12106 Builds a tree for RANGE.IDENTIFIER(). */
12109 cp_parser_range_for_member_function (tree range
, tree identifier
)
12112 vec
<tree
, va_gc
> *vec
;
12114 member
= finish_class_member_access_expr (range
, identifier
,
12115 false, tf_warning_or_error
);
12116 if (member
== error_mark_node
)
12117 return error_mark_node
;
12119 vec
= make_tree_vector ();
12120 res
= finish_call_expr (member
, &vec
,
12121 /*disallow_virtual=*/false,
12122 /*koenig_p=*/false,
12123 tf_warning_or_error
);
12124 release_tree_vector (vec
);
12128 /* Parse an iteration-statement.
12130 iteration-statement:
12131 while ( condition ) statement
12132 do statement while ( expression ) ;
12133 for ( init-statement condition [opt] ; expression [opt] )
12136 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12139 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12140 unsigned short unroll
)
12145 unsigned char in_statement
;
12146 token_indent_info guard_tinfo
;
12148 /* Peek at the next token. */
12149 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12151 return error_mark_node
;
12153 guard_tinfo
= get_token_indent_info (token
);
12155 /* Remember whether or not we are already within an iteration
12157 in_statement
= parser
->in_statement
;
12159 /* See what kind of keyword it is. */
12160 keyword
= token
->keyword
;
12167 /* Begin the while-statement. */
12168 statement
= begin_while_stmt ();
12169 /* Look for the `('. */
12170 matching_parens parens
;
12171 parens
.require_open (parser
);
12172 /* Parse the condition. */
12173 condition
= cp_parser_condition (parser
);
12174 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12175 /* Look for the `)'. */
12176 parens
.require_close (parser
);
12177 /* Parse the dependent statement. */
12178 parser
->in_statement
= IN_ITERATION_STMT
;
12179 bool prev
= note_iteration_stmt_body_start ();
12180 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12181 note_iteration_stmt_body_end (prev
);
12182 parser
->in_statement
= in_statement
;
12183 /* We're done with the while-statement. */
12184 finish_while_stmt (statement
);
12192 /* Begin the do-statement. */
12193 statement
= begin_do_stmt ();
12194 /* Parse the body of the do-statement. */
12195 parser
->in_statement
= IN_ITERATION_STMT
;
12196 bool prev
= note_iteration_stmt_body_start ();
12197 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12198 note_iteration_stmt_body_end (prev
);
12199 parser
->in_statement
= in_statement
;
12200 finish_do_body (statement
);
12201 /* Look for the `while' keyword. */
12202 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12203 /* Look for the `('. */
12204 matching_parens parens
;
12205 parens
.require_open (parser
);
12206 /* Parse the expression. */
12207 expression
= cp_parser_expression (parser
);
12208 /* We're done with the do-statement. */
12209 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12210 /* Look for the `)'. */
12211 parens
.require_close (parser
);
12212 /* Look for the `;'. */
12213 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12219 /* Look for the `('. */
12220 matching_parens parens
;
12221 parens
.require_open (parser
);
12223 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12225 /* Look for the `)'. */
12226 parens
.require_close (parser
);
12228 /* Parse the body of the for-statement. */
12229 parser
->in_statement
= IN_ITERATION_STMT
;
12230 bool prev
= note_iteration_stmt_body_start ();
12231 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12232 note_iteration_stmt_body_end (prev
);
12233 parser
->in_statement
= in_statement
;
12235 /* We're done with the for-statement. */
12236 finish_for_stmt (statement
);
12241 cp_parser_error (parser
, "expected iteration-statement");
12242 statement
= error_mark_node
;
12249 /* Parse a init-statement or the declarator of a range-based-for.
12250 Returns true if a range-based-for declaration is seen.
12253 expression-statement
12254 simple-declaration */
12257 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12259 /* If the next token is a `;', then we have an empty
12260 expression-statement. Grammatically, this is also a
12261 simple-declaration, but an invalid one, because it does not
12262 declare anything. Therefore, if we did not handle this case
12263 specially, we would issue an error message about an invalid
12265 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12267 bool is_range_for
= false;
12268 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12270 /* A colon is used in range-based for. */
12271 parser
->colon_corrects_to_scope_p
= false;
12273 /* We're going to speculatively look for a declaration, falling back
12274 to an expression, if necessary. */
12275 cp_parser_parse_tentatively (parser
);
12276 /* Parse the declaration. */
12277 cp_parser_simple_declaration (parser
,
12278 /*function_definition_allowed_p=*/false,
12280 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12281 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12283 /* It is a range-for, consume the ':' */
12284 cp_lexer_consume_token (parser
->lexer
);
12285 is_range_for
= true;
12286 if (cxx_dialect
< cxx11
)
12288 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12289 "range-based %<for%> loops only available with "
12290 "-std=c++11 or -std=gnu++11");
12291 *decl
= error_mark_node
;
12295 /* The ';' is not consumed yet because we told
12296 cp_parser_simple_declaration not to. */
12297 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12299 if (cp_parser_parse_definitely (parser
))
12300 return is_range_for
;
12301 /* If the tentative parse failed, then we shall need to look for an
12302 expression-statement. */
12304 /* If we are here, it is an expression-statement. */
12305 cp_parser_expression_statement (parser
, NULL_TREE
);
12309 /* Parse a jump-statement.
12314 return expression [opt] ;
12315 return braced-init-list ;
12321 goto * expression ;
12323 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12326 cp_parser_jump_statement (cp_parser
* parser
)
12328 tree statement
= error_mark_node
;
12331 unsigned char in_statement
;
12333 /* Peek at the next token. */
12334 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12336 return error_mark_node
;
12338 /* See what kind of keyword it is. */
12339 keyword
= token
->keyword
;
12343 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12344 switch (in_statement
)
12347 error_at (token
->location
, "break statement not within loop or switch");
12350 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12351 || in_statement
== IN_ITERATION_STMT
);
12352 statement
= finish_break_stmt ();
12353 if (in_statement
== IN_ITERATION_STMT
)
12354 break_maybe_infinite_loop ();
12357 error_at (token
->location
, "invalid exit from OpenMP structured block");
12360 error_at (token
->location
, "break statement used with OpenMP for loop");
12363 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12367 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12370 error_at (token
->location
, "continue statement not within a loop");
12372 /* Fall through. */
12373 case IN_ITERATION_STMT
:
12375 statement
= finish_continue_stmt ();
12378 error_at (token
->location
, "invalid exit from OpenMP structured block");
12381 gcc_unreachable ();
12383 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12389 bool expr_non_constant_p
;
12391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12393 cp_lexer_set_source_position (parser
->lexer
);
12394 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12395 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12397 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12398 expr
= cp_parser_expression (parser
);
12400 /* If the next token is a `;', then there is no
12403 /* Build the return-statement. */
12404 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12405 /* Don't deduce from a discarded return statement. */;
12407 statement
= finish_return_stmt (expr
);
12408 /* Look for the final `;'. */
12409 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12414 if (parser
->in_function_body
12415 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12417 error ("%<goto%> in %<constexpr%> function");
12418 cp_function_chain
->invalid_constexpr
= true;
12421 /* Create the goto-statement. */
12422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12424 /* Issue a warning about this use of a GNU extension. */
12425 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12426 /* Consume the '*' token. */
12427 cp_lexer_consume_token (parser
->lexer
);
12428 /* Parse the dependent expression. */
12429 finish_goto_stmt (cp_parser_expression (parser
));
12432 finish_goto_stmt (cp_parser_identifier (parser
));
12433 /* Look for the final `;'. */
12434 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12438 cp_parser_error (parser
, "expected jump-statement");
12445 /* Parse a declaration-statement.
12447 declaration-statement:
12448 block-declaration */
12451 cp_parser_declaration_statement (cp_parser
* parser
)
12455 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12456 p
= obstack_alloc (&declarator_obstack
, 0);
12458 /* Parse the block-declaration. */
12459 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12461 /* Free any declarators allocated. */
12462 obstack_free (&declarator_obstack
, p
);
12465 /* Some dependent statements (like `if (cond) statement'), are
12466 implicitly in their own scope. In other words, if the statement is
12467 a single statement (as opposed to a compound-statement), it is
12468 none-the-less treated as if it were enclosed in braces. Any
12469 declarations appearing in the dependent statement are out of scope
12470 after control passes that point. This function parses a statement,
12471 but ensures that is in its own scope, even if it is not a
12472 compound-statement.
12474 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12475 is a (possibly labeled) if statement which is not enclosed in
12476 braces and has an else clause. This is used to implement
12479 CHAIN is a vector of if-else-if conditions. This is used to implement
12482 Returns the new statement. */
12485 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12486 const token_indent_info
&guard_tinfo
,
12490 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12491 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12492 token_indent_info body_tinfo
12493 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12498 /* Mark if () ; with a special NOP_EXPR. */
12499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12501 cp_lexer_consume_token (parser
->lexer
);
12502 statement
= add_stmt (build_empty_stmt (body_loc
));
12504 if (guard_tinfo
.keyword
== RID_IF
12505 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12506 warning_at (body_loc
, OPT_Wempty_body
,
12507 "suggest braces around empty body in an %<if%> statement");
12508 else if (guard_tinfo
.keyword
== RID_ELSE
)
12509 warning_at (body_loc
, OPT_Wempty_body
,
12510 "suggest braces around empty body in an %<else%> statement");
12512 /* if a compound is opened, we simply parse the statement directly. */
12513 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12514 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12515 /* If the token is not a `{', then we must take special action. */
12518 /* Create a compound-statement. */
12519 statement
= begin_compound_stmt (0);
12520 /* Parse the dependent-statement. */
12521 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12522 &body_loc_after_labels
);
12523 /* Finish the dummy compound-statement. */
12524 finish_compound_stmt (statement
);
12527 token_indent_info next_tinfo
12528 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12529 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12531 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12532 && next_tinfo
.type
!= CPP_SEMICOLON
)
12533 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12534 guard_tinfo
.location
, guard_tinfo
.keyword
);
12536 /* Return the statement. */
12540 /* For some dependent statements (like `while (cond) statement'), we
12541 have already created a scope. Therefore, even if the dependent
12542 statement is a compound-statement, we do not want to create another
12546 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12547 const token_indent_info
&guard_tinfo
)
12549 /* If the token is a `{', then we must take special action. */
12550 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12552 token_indent_info body_tinfo
12553 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12554 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12556 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12557 &loc_after_labels
);
12558 token_indent_info next_tinfo
12559 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12560 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12562 if (loc_after_labels
!= UNKNOWN_LOCATION
12563 && next_tinfo
.type
!= CPP_SEMICOLON
)
12564 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12565 guard_tinfo
.location
,
12566 guard_tinfo
.keyword
);
12570 /* Avoid calling cp_parser_compound_statement, so that we
12571 don't create a new scope. Do everything else by hand. */
12572 matching_braces braces
;
12573 braces
.require_open (parser
);
12574 /* If the next keyword is `__label__' we have a label declaration. */
12575 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12576 cp_parser_label_declaration (parser
);
12577 /* Parse an (optional) statement-seq. */
12578 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12579 braces
.require_close (parser
);
12583 /* Declarations [gram.dcl.dcl] */
12585 /* Parse an optional declaration-sequence.
12589 declaration-seq declaration */
12592 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12598 token
= cp_lexer_peek_token (parser
->lexer
);
12600 if (token
->type
== CPP_CLOSE_BRACE
12601 || token
->type
== CPP_EOF
12602 || token
->type
== CPP_PRAGMA_EOL
)
12605 if (token
->type
== CPP_SEMICOLON
)
12607 /* A declaration consisting of a single semicolon is
12608 invalid. Allow it unless we're being pedantic. */
12609 cp_lexer_consume_token (parser
->lexer
);
12610 if (!in_system_header_at (input_location
))
12611 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12615 /* If we're entering or exiting a region that's implicitly
12616 extern "C", modify the lang context appropriately. */
12617 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12619 push_lang_context (lang_name_c
);
12620 parser
->implicit_extern_c
= true;
12622 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12624 pop_lang_context ();
12625 parser
->implicit_extern_c
= false;
12628 if (token
->type
== CPP_PRAGMA
)
12630 /* A top-level declaration can consist solely of a #pragma.
12631 A nested declaration cannot, so this is done here and not
12632 in cp_parser_declaration. (A #pragma at block scope is
12633 handled in cp_parser_statement.) */
12634 cp_parser_pragma (parser
, pragma_external
, NULL
);
12638 /* Parse the declaration itself. */
12639 cp_parser_declaration (parser
);
12643 /* Parse a declaration.
12647 function-definition
12648 template-declaration
12649 explicit-instantiation
12650 explicit-specialization
12651 linkage-specification
12652 namespace-definition
12660 __extension__ declaration */
12663 cp_parser_declaration (cp_parser
* parser
)
12667 int saved_pedantic
;
12669 tree attributes
= NULL_TREE
;
12671 /* Check for the `__extension__' keyword. */
12672 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12674 /* Parse the qualified declaration. */
12675 cp_parser_declaration (parser
);
12676 /* Restore the PEDANTIC flag. */
12677 pedantic
= saved_pedantic
;
12682 /* Try to figure out what kind of declaration is present. */
12683 token1
= *cp_lexer_peek_token (parser
->lexer
);
12685 if (token1
.type
!= CPP_EOF
)
12686 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12689 token2
.type
= CPP_EOF
;
12690 token2
.keyword
= RID_MAX
;
12693 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12694 p
= obstack_alloc (&declarator_obstack
, 0);
12696 /* If the next token is `extern' and the following token is a string
12697 literal, then we have a linkage specification. */
12698 if (token1
.keyword
== RID_EXTERN
12699 && cp_parser_is_pure_string_literal (&token2
))
12700 cp_parser_linkage_specification (parser
);
12701 /* If the next token is `template', then we have either a template
12702 declaration, an explicit instantiation, or an explicit
12704 else if (token1
.keyword
== RID_TEMPLATE
)
12706 /* `template <>' indicates a template specialization. */
12707 if (token2
.type
== CPP_LESS
12708 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12709 cp_parser_explicit_specialization (parser
);
12710 /* `template <' indicates a template declaration. */
12711 else if (token2
.type
== CPP_LESS
)
12712 cp_parser_template_declaration (parser
, /*member_p=*/false);
12713 /* Anything else must be an explicit instantiation. */
12715 cp_parser_explicit_instantiation (parser
);
12717 /* If the next token is `export', then we have a template
12719 else if (token1
.keyword
== RID_EXPORT
)
12720 cp_parser_template_declaration (parser
, /*member_p=*/false);
12721 /* If the next token is `extern', 'static' or 'inline' and the one
12722 after that is `template', we have a GNU extended explicit
12723 instantiation directive. */
12724 else if (cp_parser_allow_gnu_extensions_p (parser
)
12725 && (token1
.keyword
== RID_EXTERN
12726 || token1
.keyword
== RID_STATIC
12727 || token1
.keyword
== RID_INLINE
)
12728 && token2
.keyword
== RID_TEMPLATE
)
12729 cp_parser_explicit_instantiation (parser
);
12730 /* If the next token is `namespace', check for a named or unnamed
12731 namespace definition. */
12732 else if (token1
.keyword
== RID_NAMESPACE
12733 && (/* A named namespace definition. */
12734 (token2
.type
== CPP_NAME
12735 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12737 || (token2
.type
== CPP_OPEN_SQUARE
12738 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12739 == CPP_OPEN_SQUARE
)
12740 /* An unnamed namespace definition. */
12741 || token2
.type
== CPP_OPEN_BRACE
12742 || token2
.keyword
== RID_ATTRIBUTE
))
12743 cp_parser_namespace_definition (parser
);
12744 /* An inline (associated) namespace definition. */
12745 else if (token1
.keyword
== RID_INLINE
12746 && token2
.keyword
== RID_NAMESPACE
)
12747 cp_parser_namespace_definition (parser
);
12748 /* Objective-C++ declaration/definition. */
12749 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12750 cp_parser_objc_declaration (parser
, NULL_TREE
);
12751 else if (c_dialect_objc ()
12752 && token1
.keyword
== RID_ATTRIBUTE
12753 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12754 cp_parser_objc_declaration (parser
, attributes
);
12755 /* At this point we may have a template declared by a concept
12757 else if (flag_concepts
12758 && cp_parser_template_declaration_after_export (parser
,
12759 /*member_p=*/false))
12762 /* Try to parse a block-declaration, or a function-definition. */
12763 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12765 /* Free any declarators allocated. */
12766 obstack_free (&declarator_obstack
, p
);
12769 /* Parse a block-declaration.
12774 namespace-alias-definition
12781 __extension__ block-declaration
12786 static_assert-declaration
12788 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12789 part of a declaration-statement. */
12792 cp_parser_block_declaration (cp_parser
*parser
,
12796 int saved_pedantic
;
12798 /* Check for the `__extension__' keyword. */
12799 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12801 /* Parse the qualified declaration. */
12802 cp_parser_block_declaration (parser
, statement_p
);
12803 /* Restore the PEDANTIC flag. */
12804 pedantic
= saved_pedantic
;
12809 /* Peek at the next token to figure out which kind of declaration is
12811 token1
= cp_lexer_peek_token (parser
->lexer
);
12813 /* If the next keyword is `asm', we have an asm-definition. */
12814 if (token1
->keyword
== RID_ASM
)
12817 cp_parser_commit_to_tentative_parse (parser
);
12818 cp_parser_asm_definition (parser
);
12820 /* If the next keyword is `namespace', we have a
12821 namespace-alias-definition. */
12822 else if (token1
->keyword
== RID_NAMESPACE
)
12823 cp_parser_namespace_alias_definition (parser
);
12824 /* If the next keyword is `using', we have a
12825 using-declaration, a using-directive, or an alias-declaration. */
12826 else if (token1
->keyword
== RID_USING
)
12831 cp_parser_commit_to_tentative_parse (parser
);
12832 /* If the token after `using' is `namespace', then we have a
12833 using-directive. */
12834 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12835 if (token2
->keyword
== RID_NAMESPACE
)
12836 cp_parser_using_directive (parser
);
12837 /* If the second token after 'using' is '=', then we have an
12838 alias-declaration. */
12839 else if (cxx_dialect
>= cxx11
12840 && token2
->type
== CPP_NAME
12841 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12842 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12843 cp_parser_alias_declaration (parser
);
12844 /* Otherwise, it's a using-declaration. */
12846 cp_parser_using_declaration (parser
,
12847 /*access_declaration_p=*/false);
12849 /* If the next keyword is `__label__' we have a misplaced label
12851 else if (token1
->keyword
== RID_LABEL
)
12853 cp_lexer_consume_token (parser
->lexer
);
12854 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12855 cp_parser_skip_to_end_of_statement (parser
);
12856 /* If the next token is now a `;', consume it. */
12857 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12858 cp_lexer_consume_token (parser
->lexer
);
12860 /* If the next token is `static_assert' we have a static assertion. */
12861 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12862 cp_parser_static_assert (parser
, /*member_p=*/false);
12863 /* Anything else must be a simple-declaration. */
12865 cp_parser_simple_declaration (parser
, !statement_p
,
12866 /*maybe_range_for_decl*/NULL
);
12869 /* Parse a simple-declaration.
12871 simple-declaration:
12872 decl-specifier-seq [opt] init-declarator-list [opt] ;
12873 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12874 brace-or-equal-initializer ;
12876 init-declarator-list:
12878 init-declarator-list , init-declarator
12880 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12881 function-definition as a simple-declaration.
12883 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12884 parsed declaration if it is an uninitialized single declarator not followed
12885 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12886 if present, will not be consumed. */
12889 cp_parser_simple_declaration (cp_parser
* parser
,
12890 bool function_definition_allowed_p
,
12891 tree
*maybe_range_for_decl
)
12893 cp_decl_specifier_seq decl_specifiers
;
12894 int declares_class_or_enum
;
12895 bool saw_declarator
;
12896 location_t comma_loc
= UNKNOWN_LOCATION
;
12897 location_t init_loc
= UNKNOWN_LOCATION
;
12899 if (maybe_range_for_decl
)
12900 *maybe_range_for_decl
= NULL_TREE
;
12902 /* Defer access checks until we know what is being declared; the
12903 checks for names appearing in the decl-specifier-seq should be
12904 done as if we were in the scope of the thing being declared. */
12905 push_deferring_access_checks (dk_deferred
);
12907 /* Parse the decl-specifier-seq. We have to keep track of whether
12908 or not the decl-specifier-seq declares a named class or
12909 enumeration type, since that is the only case in which the
12910 init-declarator-list is allowed to be empty.
12914 In a simple-declaration, the optional init-declarator-list can be
12915 omitted only when declaring a class or enumeration, that is when
12916 the decl-specifier-seq contains either a class-specifier, an
12917 elaborated-type-specifier, or an enum-specifier. */
12918 cp_parser_decl_specifier_seq (parser
,
12919 CP_PARSER_FLAGS_OPTIONAL
,
12921 &declares_class_or_enum
);
12922 /* We no longer need to defer access checks. */
12923 stop_deferring_access_checks ();
12925 /* In a block scope, a valid declaration must always have a
12926 decl-specifier-seq. By not trying to parse declarators, we can
12927 resolve the declaration/expression ambiguity more quickly. */
12928 if (!function_definition_allowed_p
12929 && !decl_specifiers
.any_specifiers_p
)
12931 cp_parser_error (parser
, "expected declaration");
12935 /* If the next two tokens are both identifiers, the code is
12936 erroneous. The usual cause of this situation is code like:
12940 where "T" should name a type -- but does not. */
12941 if (!decl_specifiers
.any_type_specifiers_p
12942 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12944 /* If parsing tentatively, we should commit; we really are
12945 looking at a declaration. */
12946 cp_parser_commit_to_tentative_parse (parser
);
12951 /* If we have seen at least one decl-specifier, and the next token
12952 is not a parenthesis, then we must be looking at a declaration.
12953 (After "int (" we might be looking at a functional cast.) */
12954 if (decl_specifiers
.any_specifiers_p
12955 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12956 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12957 && !cp_parser_error_occurred (parser
))
12958 cp_parser_commit_to_tentative_parse (parser
);
12960 /* Look for C++17 decomposition declaration. */
12961 for (size_t n
= 1; ; n
++)
12962 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12963 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12965 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12966 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12967 && decl_specifiers
.any_specifiers_p
)
12970 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12971 maybe_range_for_decl
,
12974 /* The next token should be either a `,' or a `;'. */
12975 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12976 /* If it's a `;', we are done. */
12977 if (token
->type
== CPP_SEMICOLON
)
12979 else if (maybe_range_for_decl
)
12981 if (*maybe_range_for_decl
== NULL_TREE
)
12982 *maybe_range_for_decl
= error_mark_node
;
12985 /* Anything else is an error. */
12988 /* If we have already issued an error message we don't need
12989 to issue another one. */
12990 if ((decl
!= error_mark_node
12991 && DECL_INITIAL (decl
) != error_mark_node
)
12992 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12993 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12994 /* Skip tokens until we reach the end of the statement. */
12995 cp_parser_skip_to_end_of_statement (parser
);
12996 /* If the next token is now a `;', consume it. */
12997 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12998 cp_lexer_consume_token (parser
->lexer
);
13006 bool auto_specifier_p
;
13007 /* NULL_TREE if both variable and function declaration are allowed,
13008 error_mark_node if function declaration are not allowed and
13009 a FUNCTION_DECL that should be diagnosed if it is followed by
13010 variable declarations. */
13011 tree auto_function_declaration
;
13013 last_type
= NULL_TREE
;
13015 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13016 auto_function_declaration
= NULL_TREE
;
13018 /* Keep going until we hit the `;' at the end of the simple
13020 saw_declarator
= false;
13021 while (cp_lexer_next_token_is_not (parser
->lexer
,
13025 bool function_definition_p
;
13027 tree auto_result
= NULL_TREE
;
13029 if (saw_declarator
)
13031 /* If we are processing next declarator, comma is expected */
13032 token
= cp_lexer_peek_token (parser
->lexer
);
13033 gcc_assert (token
->type
== CPP_COMMA
);
13034 cp_lexer_consume_token (parser
->lexer
);
13035 if (maybe_range_for_decl
)
13037 *maybe_range_for_decl
= error_mark_node
;
13038 if (comma_loc
== UNKNOWN_LOCATION
)
13039 comma_loc
= token
->location
;
13043 saw_declarator
= true;
13045 /* Parse the init-declarator. */
13046 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13048 function_definition_allowed_p
,
13049 /*member_p=*/false,
13050 declares_class_or_enum
,
13051 &function_definition_p
,
13052 maybe_range_for_decl
,
13055 /* If an error occurred while parsing tentatively, exit quickly.
13056 (That usually happens when in the body of a function; each
13057 statement is treated as a declaration-statement until proven
13059 if (cp_parser_error_occurred (parser
))
13062 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13064 /* If the init-declarator-list contains more than one
13065 init-declarator, they shall all form declarations of
13067 if (auto_function_declaration
== NULL_TREE
)
13068 auto_function_declaration
13069 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13070 else if (TREE_CODE (decl
) == FUNCTION_DECL
13071 || auto_function_declaration
!= error_mark_node
)
13073 error_at (decl_specifiers
.locations
[ds_type_spec
],
13074 "non-variable %qD in declaration with more than one "
13075 "declarator with placeholder type",
13076 TREE_CODE (decl
) == FUNCTION_DECL
13077 ? decl
: auto_function_declaration
);
13078 auto_function_declaration
= error_mark_node
;
13083 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13086 && last_type
!= error_mark_node
13087 && !same_type_p (auto_result
, last_type
))
13089 /* If the list of declarators contains more than one declarator,
13090 the type of each declared variable is determined as described
13091 above. If the type deduced for the template parameter U is not
13092 the same in each deduction, the program is ill-formed. */
13093 error_at (decl_specifiers
.locations
[ds_type_spec
],
13094 "inconsistent deduction for %qT: %qT and then %qT",
13095 decl_specifiers
.type
, last_type
, auto_result
);
13096 last_type
= error_mark_node
;
13099 last_type
= auto_result
;
13102 /* Handle function definitions specially. */
13103 if (function_definition_p
)
13105 /* If the next token is a `,', then we are probably
13106 processing something like:
13110 which is erroneous. */
13111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13114 error_at (token
->location
,
13116 " declarations and function-definitions is forbidden");
13118 /* Otherwise, we're done with the list of declarators. */
13121 pop_deferring_access_checks ();
13125 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13126 *maybe_range_for_decl
= decl
;
13127 /* The next token should be either a `,' or a `;'. */
13128 token
= cp_lexer_peek_token (parser
->lexer
);
13129 /* If it's a `,', there are more declarators to come. */
13130 if (token
->type
== CPP_COMMA
)
13131 /* will be consumed next time around */;
13132 /* If it's a `;', we are done. */
13133 else if (token
->type
== CPP_SEMICOLON
)
13135 else if (maybe_range_for_decl
)
13137 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13138 permerror (decl_specifiers
.locations
[ds_type_spec
],
13139 "types may not be defined in a for-range-declaration");
13142 /* Anything else is an error. */
13145 /* If we have already issued an error message we don't need
13146 to issue another one. */
13147 if ((decl
!= error_mark_node
13148 && DECL_INITIAL (decl
) != error_mark_node
)
13149 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13150 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13151 /* Skip tokens until we reach the end of the statement. */
13152 cp_parser_skip_to_end_of_statement (parser
);
13153 /* If the next token is now a `;', consume it. */
13154 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13155 cp_lexer_consume_token (parser
->lexer
);
13158 /* After the first time around, a function-definition is not
13159 allowed -- even if it was OK at first. For example:
13164 function_definition_allowed_p
= false;
13167 /* Issue an error message if no declarators are present, and the
13168 decl-specifier-seq does not itself declare a class or
13169 enumeration: [dcl.dcl]/3. */
13170 if (!saw_declarator
)
13172 if (cp_parser_declares_only_class_p (parser
))
13174 if (!declares_class_or_enum
13175 && decl_specifiers
.type
13176 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13177 /* Ensure an error is issued anyway when finish_decltype_type,
13178 called via cp_parser_decl_specifier_seq, returns a class or
13179 an enumeration (c++/51786). */
13180 decl_specifiers
.type
= NULL_TREE
;
13181 shadow_tag (&decl_specifiers
);
13183 /* Perform any deferred access checks. */
13184 perform_deferred_access_checks (tf_warning_or_error
);
13187 /* Consume the `;'. */
13189 if (!maybe_range_for_decl
)
13190 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13191 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13193 if (init_loc
!= UNKNOWN_LOCATION
)
13194 error_at (init_loc
, "initializer in range-based %<for%> loop");
13195 if (comma_loc
!= UNKNOWN_LOCATION
)
13196 error_at (comma_loc
,
13197 "multiple declarations in range-based %<for%> loop");
13201 pop_deferring_access_checks ();
13204 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13205 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13209 cp_parser_decomposition_declaration (cp_parser
*parser
,
13210 cp_decl_specifier_seq
*decl_specifiers
,
13211 tree
*maybe_range_for_decl
,
13212 location_t
*init_loc
)
13214 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13215 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13216 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13218 /* Parse the identifier-list. */
13219 auto_vec
<cp_expr
, 10> v
;
13220 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13223 cp_expr e
= cp_parser_identifier (parser
);
13224 if (e
.get_value () == error_mark_node
)
13227 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13229 cp_lexer_consume_token (parser
->lexer
);
13232 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13233 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13235 end_loc
= UNKNOWN_LOCATION
;
13236 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13239 cp_lexer_consume_token (parser
->lexer
);
13242 cp_parser_skip_to_end_of_statement (parser
);
13243 return error_mark_node
;
13247 if (cxx_dialect
< cxx17
)
13248 pedwarn (loc
, 0, "structured bindings only available with "
13249 "-std=c++17 or -std=gnu++17");
13252 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13253 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13254 declarator
->id_loc
= loc
;
13255 if (ref_qual
!= REF_QUAL_NONE
)
13256 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13257 ref_qual
== REF_QUAL_RVALUE
,
13259 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13260 NULL_TREE
, decl_specifiers
->attributes
,
13262 tree orig_decl
= decl
;
13266 cp_decl_specifier_seq decl_specs
;
13267 clear_decl_specs (&decl_specs
);
13268 decl_specs
.type
= make_auto ();
13270 FOR_EACH_VEC_ELT (v
, i
, e
)
13273 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13275 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13276 declarator
->id_loc
= e
.get_location ();
13277 tree elt_pushed_scope
;
13278 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13279 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13280 if (decl2
== error_mark_node
)
13281 decl
= error_mark_node
;
13282 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13284 /* Ensure we've diagnosed redeclaration if we aren't creating
13286 gcc_assert (errorcount
);
13287 decl
= error_mark_node
;
13291 if (elt_pushed_scope
)
13292 pop_scope (elt_pushed_scope
);
13297 error_at (loc
, "empty structured binding declaration");
13298 decl
= error_mark_node
;
13301 if (maybe_range_for_decl
== NULL
13302 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13304 bool non_constant_p
= false, is_direct_init
= false;
13305 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13306 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13308 if (initializer
== NULL_TREE
13309 || (TREE_CODE (initializer
) == TREE_LIST
13310 && TREE_CHAIN (initializer
))
13312 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13313 && CONSTRUCTOR_NELTS (initializer
) != 1))
13315 error_at (loc
, "invalid initializer for structured binding "
13317 initializer
= error_mark_node
;
13320 if (decl
!= error_mark_node
)
13322 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13323 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13324 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13325 cp_finish_decomp (decl
, prev
, v
.length ());
13328 else if (decl
!= error_mark_node
)
13330 *maybe_range_for_decl
= prev
;
13331 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13332 the underlying DECL. */
13333 cp_finish_decomp (decl
, prev
, v
.length ());
13337 pop_scope (pushed_scope
);
13339 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13341 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13342 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13348 /* Parse a decl-specifier-seq.
13350 decl-specifier-seq:
13351 decl-specifier-seq [opt] decl-specifier
13352 decl-specifier attribute-specifier-seq [opt] (C++11)
13355 storage-class-specifier
13366 Concepts Extension:
13371 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13373 The parser flags FLAGS is used to control type-specifier parsing.
13375 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13378 1: one of the decl-specifiers is an elaborated-type-specifier
13379 (i.e., a type declaration)
13380 2: one of the decl-specifiers is an enum-specifier or a
13381 class-specifier (i.e., a type definition)
13386 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13387 cp_parser_flags flags
,
13388 cp_decl_specifier_seq
*decl_specs
,
13389 int* declares_class_or_enum
)
13391 bool constructor_possible_p
= !parser
->in_declarator_p
;
13392 bool found_decl_spec
= false;
13393 cp_token
*start_token
= NULL
;
13396 /* Clear DECL_SPECS. */
13397 clear_decl_specs (decl_specs
);
13399 /* Assume no class or enumeration type is declared. */
13400 *declares_class_or_enum
= 0;
13402 /* Keep reading specifiers until there are no more to read. */
13405 bool constructor_p
;
13409 /* Peek at the next token. */
13410 token
= cp_lexer_peek_token (parser
->lexer
);
13412 /* Save the first token of the decl spec list for error
13415 start_token
= token
;
13416 /* Handle attributes. */
13417 if (cp_next_tokens_can_be_attribute_p (parser
))
13419 /* Parse the attributes. */
13420 tree attrs
= cp_parser_attributes_opt (parser
);
13422 /* In a sequence of declaration specifiers, c++11 attributes
13423 appertain to the type that precede them. In that case
13426 The attribute-specifier-seq affects the type only for
13427 the declaration it appears in, not other declarations
13428 involving the same type.
13430 But for now let's force the user to position the
13431 attribute either at the beginning of the declaration or
13432 after the declarator-id, which would clearly mean that it
13433 applies to the declarator. */
13434 if (cxx11_attribute_p (attrs
))
13436 if (!found_decl_spec
)
13437 /* The c++11 attribute is at the beginning of the
13438 declaration. It appertains to the entity being
13442 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13444 /* This is an attribute following a
13445 class-specifier. */
13446 if (decl_specs
->type_definition_p
)
13447 warn_misplaced_attr_for_class_type (token
->location
,
13453 decl_specs
->std_attributes
13454 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13455 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13456 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13462 decl_specs
->attributes
13463 = attr_chainon (decl_specs
->attributes
, attrs
);
13464 if (decl_specs
->locations
[ds_attribute
] == 0)
13465 decl_specs
->locations
[ds_attribute
] = token
->location
;
13468 /* Assume we will find a decl-specifier keyword. */
13469 found_decl_spec
= true;
13470 /* If the next token is an appropriate keyword, we can simply
13471 add it to the list. */
13472 switch (token
->keyword
)
13478 if (!at_class_scope_p ())
13480 gcc_rich_location
richloc (token
->location
);
13481 richloc
.add_fixit_remove ();
13482 error_at (&richloc
, "%<friend%> used outside of class");
13483 cp_lexer_purge_token (parser
->lexer
);
13488 /* Consume the token. */
13489 cp_lexer_consume_token (parser
->lexer
);
13493 case RID_CONSTEXPR
:
13495 cp_lexer_consume_token (parser
->lexer
);
13500 cp_lexer_consume_token (parser
->lexer
);
13503 /* function-specifier:
13510 cp_parser_function_specifier_opt (parser
, decl_specs
);
13517 /* Consume the token. */
13518 cp_lexer_consume_token (parser
->lexer
);
13519 /* A constructor declarator cannot appear in a typedef. */
13520 constructor_possible_p
= false;
13521 /* The "typedef" keyword can only occur in a declaration; we
13522 may as well commit at this point. */
13523 cp_parser_commit_to_tentative_parse (parser
);
13525 if (decl_specs
->storage_class
!= sc_none
)
13526 decl_specs
->conflicting_specifiers_p
= true;
13529 /* storage-class-specifier:
13539 if (cxx_dialect
== cxx98
)
13541 /* Consume the token. */
13542 cp_lexer_consume_token (parser
->lexer
);
13544 /* Complain about `auto' as a storage specifier, if
13545 we're complaining about C++0x compatibility. */
13546 gcc_rich_location
richloc (token
->location
);
13547 richloc
.add_fixit_remove ();
13548 warning_at (&richloc
, OPT_Wc__11_compat
,
13549 "%<auto%> changes meaning in C++11; "
13550 "please remove it");
13552 /* Set the storage class anyway. */
13553 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13557 /* C++0x auto type-specifier. */
13558 found_decl_spec
= false;
13565 /* Consume the token. */
13566 cp_lexer_consume_token (parser
->lexer
);
13567 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13571 /* Consume the token. */
13573 cp_lexer_consume_token (parser
->lexer
);
13577 /* We did not yet find a decl-specifier yet. */
13578 found_decl_spec
= false;
13582 if (found_decl_spec
13583 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13584 && token
->keyword
!= RID_CONSTEXPR
)
13585 error ("decl-specifier invalid in condition");
13587 if (found_decl_spec
13588 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13589 && token
->keyword
!= RID_MUTABLE
13590 && token
->keyword
!= RID_CONSTEXPR
)
13591 error_at (token
->location
, "%qD invalid in lambda",
13592 ridpointers
[token
->keyword
]);
13595 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13597 /* Constructors are a special case. The `S' in `S()' is not a
13598 decl-specifier; it is the beginning of the declarator. */
13600 = (!found_decl_spec
13601 && constructor_possible_p
13602 && (cp_parser_constructor_declarator_p
13603 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13605 /* If we don't have a DECL_SPEC yet, then we must be looking at
13606 a type-specifier. */
13607 if (!found_decl_spec
&& !constructor_p
)
13609 int decl_spec_declares_class_or_enum
;
13610 bool is_cv_qualifier
;
13614 = cp_parser_type_specifier (parser
, flags
,
13616 /*is_declaration=*/true,
13617 &decl_spec_declares_class_or_enum
,
13619 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13621 /* If this type-specifier referenced a user-defined type
13622 (a typedef, class-name, etc.), then we can't allow any
13623 more such type-specifiers henceforth.
13627 The longest sequence of decl-specifiers that could
13628 possibly be a type name is taken as the
13629 decl-specifier-seq of a declaration. The sequence shall
13630 be self-consistent as described below.
13634 As a general rule, at most one type-specifier is allowed
13635 in the complete decl-specifier-seq of a declaration. The
13636 only exceptions are the following:
13638 -- const or volatile can be combined with any other
13641 -- signed or unsigned can be combined with char, long,
13649 void g (const int Pc);
13651 Here, Pc is *not* part of the decl-specifier seq; it's
13652 the declarator. Therefore, once we see a type-specifier
13653 (other than a cv-qualifier), we forbid any additional
13654 user-defined types. We *do* still allow things like `int
13655 int' to be considered a decl-specifier-seq, and issue the
13656 error message later. */
13657 if (type_spec
&& !is_cv_qualifier
)
13658 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13659 /* A constructor declarator cannot follow a type-specifier. */
13662 constructor_possible_p
= false;
13663 found_decl_spec
= true;
13664 if (!is_cv_qualifier
)
13665 decl_specs
->any_type_specifiers_p
= true;
13669 /* If we still do not have a DECL_SPEC, then there are no more
13670 decl-specifiers. */
13671 if (!found_decl_spec
)
13674 decl_specs
->any_specifiers_p
= true;
13675 /* After we see one decl-specifier, further decl-specifiers are
13676 always optional. */
13677 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13680 /* Don't allow a friend specifier with a class definition. */
13681 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13682 && (*declares_class_or_enum
& 2))
13683 error_at (decl_specs
->locations
[ds_friend
],
13684 "class definition may not be declared a friend");
13687 /* Parse an (optional) storage-class-specifier.
13689 storage-class-specifier:
13698 storage-class-specifier:
13701 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13704 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13706 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13709 if (cxx_dialect
!= cxx98
)
13711 /* Fall through for C++98. */
13712 gcc_fallthrough ();
13719 /* Consume the token. */
13720 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13727 /* Parse an (optional) function-specifier.
13729 function-specifier:
13734 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13735 Updates DECL_SPECS, if it is non-NULL. */
13738 cp_parser_function_specifier_opt (cp_parser
* parser
,
13739 cp_decl_specifier_seq
*decl_specs
)
13741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13742 switch (token
->keyword
)
13745 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13749 /* 14.5.2.3 [temp.mem]
13751 A member function template shall not be virtual. */
13752 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13753 && current_class_type
)
13754 error_at (token
->location
, "templates may not be %<virtual%>");
13756 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13760 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13767 /* Consume the token. */
13768 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13771 /* Parse a linkage-specification.
13773 linkage-specification:
13774 extern string-literal { declaration-seq [opt] }
13775 extern string-literal declaration */
13778 cp_parser_linkage_specification (cp_parser
* parser
)
13782 /* Look for the `extern' keyword. */
13783 cp_token
*extern_token
13784 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13786 /* Look for the string-literal. */
13787 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13788 linkage
= cp_parser_string_literal (parser
, false, false);
13790 /* Transform the literal into an identifier. If the literal is a
13791 wide-character string, or contains embedded NULs, then we can't
13792 handle it as the user wants. */
13793 if (strlen (TREE_STRING_POINTER (linkage
))
13794 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13796 cp_parser_error (parser
, "invalid linkage-specification");
13797 /* Assume C++ linkage. */
13798 linkage
= lang_name_cplusplus
;
13801 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13803 /* We're now using the new linkage. */
13804 push_lang_context (linkage
);
13806 /* Preserve the location of the the innermost linkage specification,
13807 tracking the locations of nested specifications via a local. */
13808 location_t saved_location
13809 = parser
->innermost_linkage_specification_location
;
13810 /* Construct a location ranging from the start of the "extern" to
13811 the end of the string-literal, with the caret at the start, e.g.:
13815 parser
->innermost_linkage_specification_location
13816 = make_location (extern_token
->location
,
13817 extern_token
->location
,
13818 get_finish (string_token
->location
));
13820 /* If the next token is a `{', then we're using the first
13822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13824 cp_ensure_no_omp_declare_simd (parser
);
13825 cp_ensure_no_oacc_routine (parser
);
13827 /* Consume the `{' token. */
13828 matching_braces braces
;
13829 braces
.consume_open (parser
)->location
;
13830 /* Parse the declarations. */
13831 cp_parser_declaration_seq_opt (parser
);
13832 /* Look for the closing `}'. */
13833 braces
.require_close (parser
);
13835 /* Otherwise, there's just one declaration. */
13838 bool saved_in_unbraced_linkage_specification_p
;
13840 saved_in_unbraced_linkage_specification_p
13841 = parser
->in_unbraced_linkage_specification_p
;
13842 parser
->in_unbraced_linkage_specification_p
= true;
13843 cp_parser_declaration (parser
);
13844 parser
->in_unbraced_linkage_specification_p
13845 = saved_in_unbraced_linkage_specification_p
;
13848 /* We're done with the linkage-specification. */
13849 pop_lang_context ();
13851 /* Restore location of parent linkage specification, if any. */
13852 parser
->innermost_linkage_specification_location
= saved_location
;
13855 /* Parse a static_assert-declaration.
13857 static_assert-declaration:
13858 static_assert ( constant-expression , string-literal ) ;
13859 static_assert ( constant-expression ) ; (C++17)
13861 If MEMBER_P, this static_assert is a class member. */
13864 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13867 location_t token_loc
;
13871 /* Peek at the `static_assert' token so we can keep track of exactly
13872 where the static assertion started. */
13873 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13875 /* Look for the `static_assert' keyword. */
13876 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13880 /* We know we are in a static assertion; commit to any tentative
13882 if (cp_parser_parsing_tentatively (parser
))
13883 cp_parser_commit_to_tentative_parse (parser
);
13885 /* Parse the `(' starting the static assertion condition. */
13886 matching_parens parens
;
13887 parens
.require_open (parser
);
13889 /* Parse the constant-expression. Allow a non-constant expression
13890 here in order to give better diagnostics in finish_static_assert. */
13892 cp_parser_constant_expression (parser
,
13893 /*allow_non_constant_p=*/true,
13894 /*non_constant_p=*/&dummy
);
13896 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13898 if (cxx_dialect
< cxx17
)
13899 pedwarn (input_location
, OPT_Wpedantic
,
13900 "static_assert without a message "
13901 "only available with -std=c++17 or -std=gnu++17");
13903 cp_lexer_consume_token (parser
->lexer
);
13904 message
= build_string (1, "");
13905 TREE_TYPE (message
) = char_array_type_node
;
13906 fix_string_type (message
);
13910 /* Parse the separating `,'. */
13911 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13913 /* Parse the string-literal message. */
13914 message
= cp_parser_string_literal (parser
,
13915 /*translate=*/false,
13918 /* A `)' completes the static assertion. */
13919 if (!parens
.require_close (parser
))
13920 cp_parser_skip_to_closing_parenthesis (parser
,
13921 /*recovering=*/true,
13922 /*or_comma=*/false,
13923 /*consume_paren=*/true);
13926 /* A semicolon terminates the declaration. */
13927 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13929 /* Get the location for the static assertion. Use that of the
13930 condition if available, otherwise, use that of the "static_assert"
13932 location_t assert_loc
= condition
.get_location ();
13933 if (assert_loc
== UNKNOWN_LOCATION
)
13934 assert_loc
= token_loc
;
13936 /* Complete the static assertion, which may mean either processing
13937 the static assert now or saving it for template instantiation. */
13938 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13941 /* Parse the expression in decltype ( expression ). */
13944 cp_parser_decltype_expr (cp_parser
*parser
,
13945 bool &id_expression_or_member_access_p
)
13947 cp_token
*id_expr_start_token
;
13950 /* Since we're going to preserve any side-effects from this parse, set up a
13951 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13952 in the expression. */
13953 tentative_firewall
firewall (parser
);
13955 /* First, try parsing an id-expression. */
13956 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13957 cp_parser_parse_tentatively (parser
);
13958 expr
= cp_parser_id_expression (parser
,
13959 /*template_keyword_p=*/false,
13960 /*check_dependency_p=*/true,
13961 /*template_p=*/NULL
,
13962 /*declarator_p=*/false,
13963 /*optional_p=*/false);
13965 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13967 bool non_integral_constant_expression_p
= false;
13968 tree id_expression
= expr
;
13970 const char *error_msg
;
13972 if (identifier_p (expr
))
13973 /* Lookup the name we got back from the id-expression. */
13974 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13975 id_expr_start_token
->location
);
13977 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
13978 /* A template without args is not a complete id-expression. */
13979 expr
= error_mark_node
;
13982 && expr
!= error_mark_node
13983 && TREE_CODE (expr
) != TYPE_DECL
13984 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13985 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13986 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13988 /* Complete lookup of the id-expression. */
13989 expr
= (finish_id_expression
13990 (id_expression
, expr
, parser
->scope
, &idk
,
13991 /*integral_constant_expression_p=*/false,
13992 /*allow_non_integral_constant_expression_p=*/true,
13993 &non_integral_constant_expression_p
,
13994 /*template_p=*/false,
13996 /*address_p=*/false,
13997 /*template_arg_p=*/false,
13999 id_expr_start_token
->location
));
14001 if (expr
== error_mark_node
)
14002 /* We found an id-expression, but it was something that we
14003 should not have found. This is an error, not something
14004 we can recover from, so note that we found an
14005 id-expression and we'll recover as gracefully as
14007 id_expression_or_member_access_p
= true;
14011 && expr
!= error_mark_node
14012 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14013 /* We have an id-expression. */
14014 id_expression_or_member_access_p
= true;
14017 if (!id_expression_or_member_access_p
)
14019 /* Abort the id-expression parse. */
14020 cp_parser_abort_tentative_parse (parser
);
14022 /* Parsing tentatively, again. */
14023 cp_parser_parse_tentatively (parser
);
14025 /* Parse a class member access. */
14026 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14027 /*cast_p=*/false, /*decltype*/true,
14028 /*member_access_only_p=*/true, NULL
);
14031 && expr
!= error_mark_node
14032 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14033 /* We have an id-expression. */
14034 id_expression_or_member_access_p
= true;
14037 if (id_expression_or_member_access_p
)
14038 /* We have parsed the complete id-expression or member access. */
14039 cp_parser_parse_definitely (parser
);
14042 /* Abort our attempt to parse an id-expression or member access
14044 cp_parser_abort_tentative_parse (parser
);
14046 /* Commit to the tentative_firewall so we get syntax errors. */
14047 cp_parser_commit_to_tentative_parse (parser
);
14049 /* Parse a full expression. */
14050 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14051 /*decltype_p=*/true);
14057 /* Parse a `decltype' type. Returns the type.
14059 simple-type-specifier:
14060 decltype ( expression )
14062 decltype ( auto ) */
14065 cp_parser_decltype (cp_parser
*parser
)
14067 bool id_expression_or_member_access_p
= false;
14068 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14070 if (start_token
->type
== CPP_DECLTYPE
)
14072 /* Already parsed. */
14073 cp_lexer_consume_token (parser
->lexer
);
14074 return saved_checks_value (start_token
->u
.tree_check_value
);
14077 /* Look for the `decltype' token. */
14078 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14079 return error_mark_node
;
14081 /* Parse the opening `('. */
14082 matching_parens parens
;
14083 if (!parens
.require_open (parser
))
14084 return error_mark_node
;
14086 push_deferring_access_checks (dk_deferred
);
14088 tree expr
= NULL_TREE
;
14090 if (cxx_dialect
>= cxx14
14091 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14092 /* decltype (auto) */
14093 cp_lexer_consume_token (parser
->lexer
);
14096 /* decltype (expression) */
14098 /* Types cannot be defined in a `decltype' expression. Save away the
14099 old message and set the new one. */
14100 const char *saved_message
= parser
->type_definition_forbidden_message
;
14101 parser
->type_definition_forbidden_message
14102 = G_("types may not be defined in %<decltype%> expressions");
14104 /* The restrictions on constant-expressions do not apply inside
14105 decltype expressions. */
14106 bool saved_integral_constant_expression_p
14107 = parser
->integral_constant_expression_p
;
14108 bool saved_non_integral_constant_expression_p
14109 = parser
->non_integral_constant_expression_p
;
14110 parser
->integral_constant_expression_p
= false;
14112 /* Within a parenthesized expression, a `>' token is always
14113 the greater-than operator. */
14114 bool saved_greater_than_is_operator_p
14115 = parser
->greater_than_is_operator_p
;
14116 parser
->greater_than_is_operator_p
= true;
14118 /* Do not actually evaluate the expression. */
14119 ++cp_unevaluated_operand
;
14121 /* Do not warn about problems with the expression. */
14122 ++c_inhibit_evaluation_warnings
;
14124 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14126 /* Go back to evaluating expressions. */
14127 --cp_unevaluated_operand
;
14128 --c_inhibit_evaluation_warnings
;
14130 /* The `>' token might be the end of a template-id or
14131 template-parameter-list now. */
14132 parser
->greater_than_is_operator_p
14133 = saved_greater_than_is_operator_p
;
14135 /* Restore the old message and the integral constant expression
14137 parser
->type_definition_forbidden_message
= saved_message
;
14138 parser
->integral_constant_expression_p
14139 = saved_integral_constant_expression_p
;
14140 parser
->non_integral_constant_expression_p
14141 = saved_non_integral_constant_expression_p
;
14144 /* Parse to the closing `)'. */
14145 if (!parens
.require_close (parser
))
14147 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14148 /*consume_paren=*/true);
14149 pop_deferring_access_checks ();
14150 return error_mark_node
;
14156 expr
= make_decltype_auto ();
14157 AUTO_IS_DECLTYPE (expr
) = true;
14160 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14161 tf_warning_or_error
);
14163 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14165 start_token
->type
= CPP_DECLTYPE
;
14166 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14167 start_token
->u
.tree_check_value
->value
= expr
;
14168 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14169 start_token
->keyword
= RID_MAX
;
14170 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14172 pop_to_parent_deferring_access_checks ();
14177 /* Special member functions [gram.special] */
14179 /* Parse a conversion-function-id.
14181 conversion-function-id:
14182 operator conversion-type-id
14184 Returns an IDENTIFIER_NODE representing the operator. */
14187 cp_parser_conversion_function_id (cp_parser
* parser
)
14191 tree saved_qualifying_scope
;
14192 tree saved_object_scope
;
14193 tree pushed_scope
= NULL_TREE
;
14195 /* Look for the `operator' token. */
14196 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14197 return error_mark_node
;
14198 /* When we parse the conversion-type-id, the current scope will be
14199 reset. However, we need that information in able to look up the
14200 conversion function later, so we save it here. */
14201 saved_scope
= parser
->scope
;
14202 saved_qualifying_scope
= parser
->qualifying_scope
;
14203 saved_object_scope
= parser
->object_scope
;
14204 /* We must enter the scope of the class so that the names of
14205 entities declared within the class are available in the
14206 conversion-type-id. For example, consider:
14213 S::operator I() { ... }
14215 In order to see that `I' is a type-name in the definition, we
14216 must be in the scope of `S'. */
14218 pushed_scope
= push_scope (saved_scope
);
14219 /* Parse the conversion-type-id. */
14220 type
= cp_parser_conversion_type_id (parser
);
14221 /* Leave the scope of the class, if any. */
14223 pop_scope (pushed_scope
);
14224 /* Restore the saved scope. */
14225 parser
->scope
= saved_scope
;
14226 parser
->qualifying_scope
= saved_qualifying_scope
;
14227 parser
->object_scope
= saved_object_scope
;
14228 /* If the TYPE is invalid, indicate failure. */
14229 if (type
== error_mark_node
)
14230 return error_mark_node
;
14231 return make_conv_op_name (type
);
14234 /* Parse a conversion-type-id:
14236 conversion-type-id:
14237 type-specifier-seq conversion-declarator [opt]
14239 Returns the TYPE specified. */
14242 cp_parser_conversion_type_id (cp_parser
* parser
)
14245 cp_decl_specifier_seq type_specifiers
;
14246 cp_declarator
*declarator
;
14247 tree type_specified
;
14248 const char *saved_message
;
14250 /* Parse the attributes. */
14251 attributes
= cp_parser_attributes_opt (parser
);
14253 saved_message
= parser
->type_definition_forbidden_message
;
14254 parser
->type_definition_forbidden_message
14255 = G_("types may not be defined in a conversion-type-id");
14257 /* Parse the type-specifiers. */
14258 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14259 /*is_trailing_return=*/false,
14262 parser
->type_definition_forbidden_message
= saved_message
;
14264 /* If that didn't work, stop. */
14265 if (type_specifiers
.type
== error_mark_node
)
14266 return error_mark_node
;
14267 /* Parse the conversion-declarator. */
14268 declarator
= cp_parser_conversion_declarator_opt (parser
);
14270 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14271 /*initialized=*/0, &attributes
);
14273 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14275 /* Don't give this error when parsing tentatively. This happens to
14276 work because we always parse this definitively once. */
14277 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14278 && type_uses_auto (type_specified
))
14280 if (cxx_dialect
< cxx14
)
14282 error ("invalid use of %<auto%> in conversion operator");
14283 return error_mark_node
;
14285 else if (template_parm_scope_p ())
14286 warning (0, "use of %<auto%> in member template "
14287 "conversion operator can never be deduced");
14290 return type_specified
;
14293 /* Parse an (optional) conversion-declarator.
14295 conversion-declarator:
14296 ptr-operator conversion-declarator [opt]
14300 static cp_declarator
*
14301 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14303 enum tree_code code
;
14304 tree class_type
, std_attributes
= NULL_TREE
;
14305 cp_cv_quals cv_quals
;
14307 /* We don't know if there's a ptr-operator next, or not. */
14308 cp_parser_parse_tentatively (parser
);
14309 /* Try the ptr-operator. */
14310 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14312 /* If it worked, look for more conversion-declarators. */
14313 if (cp_parser_parse_definitely (parser
))
14315 cp_declarator
*declarator
;
14317 /* Parse another optional declarator. */
14318 declarator
= cp_parser_conversion_declarator_opt (parser
);
14320 declarator
= cp_parser_make_indirect_declarator
14321 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14329 /* Parse an (optional) ctor-initializer.
14332 : mem-initializer-list */
14335 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14337 /* If the next token is not a `:', then there is no
14338 ctor-initializer. */
14339 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14341 /* Do default initialization of any bases and members. */
14342 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14343 finish_mem_initializers (NULL_TREE
);
14347 /* Consume the `:' token. */
14348 cp_lexer_consume_token (parser
->lexer
);
14349 /* And the mem-initializer-list. */
14350 cp_parser_mem_initializer_list (parser
);
14353 /* Parse a mem-initializer-list.
14355 mem-initializer-list:
14356 mem-initializer ... [opt]
14357 mem-initializer ... [opt] , mem-initializer-list */
14360 cp_parser_mem_initializer_list (cp_parser
* parser
)
14362 tree mem_initializer_list
= NULL_TREE
;
14363 tree target_ctor
= error_mark_node
;
14364 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14366 /* Let the semantic analysis code know that we are starting the
14367 mem-initializer-list. */
14368 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14369 error_at (token
->location
,
14370 "only constructors take member initializers");
14372 /* Loop through the list. */
14375 tree mem_initializer
;
14377 token
= cp_lexer_peek_token (parser
->lexer
);
14378 /* Parse the mem-initializer. */
14379 mem_initializer
= cp_parser_mem_initializer (parser
);
14380 /* If the next token is a `...', we're expanding member initializers. */
14381 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14383 || (mem_initializer
!= error_mark_node
14384 && check_for_bare_parameter_packs (TREE_PURPOSE
14385 (mem_initializer
))))
14387 /* Consume the `...'. */
14389 cp_lexer_consume_token (parser
->lexer
);
14391 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14392 can be expanded but members cannot. */
14393 if (mem_initializer
!= error_mark_node
14394 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14396 error_at (token
->location
,
14397 "cannot expand initializer for member %qD",
14398 TREE_PURPOSE (mem_initializer
));
14399 mem_initializer
= error_mark_node
;
14402 /* Construct the pack expansion type. */
14403 if (mem_initializer
!= error_mark_node
)
14404 mem_initializer
= make_pack_expansion (mem_initializer
);
14406 if (target_ctor
!= error_mark_node
14407 && mem_initializer
!= error_mark_node
)
14409 error ("mem-initializer for %qD follows constructor delegation",
14410 TREE_PURPOSE (mem_initializer
));
14411 mem_initializer
= error_mark_node
;
14413 /* Look for a target constructor. */
14414 if (mem_initializer
!= error_mark_node
14415 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14416 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14418 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14419 if (mem_initializer_list
)
14421 error ("constructor delegation follows mem-initializer for %qD",
14422 TREE_PURPOSE (mem_initializer_list
));
14423 mem_initializer
= error_mark_node
;
14425 target_ctor
= mem_initializer
;
14427 /* Add it to the list, unless it was erroneous. */
14428 if (mem_initializer
!= error_mark_node
)
14430 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14431 mem_initializer_list
= mem_initializer
;
14433 /* If the next token is not a `,', we're done. */
14434 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14436 /* Consume the `,' token. */
14437 cp_lexer_consume_token (parser
->lexer
);
14440 /* Perform semantic analysis. */
14441 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14442 finish_mem_initializers (mem_initializer_list
);
14445 /* Parse a mem-initializer.
14448 mem-initializer-id ( expression-list [opt] )
14449 mem-initializer-id braced-init-list
14454 ( expression-list [opt] )
14456 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14457 class) or FIELD_DECL (for a non-static data member) to initialize;
14458 the TREE_VALUE is the expression-list. An empty initialization
14459 list is represented by void_list_node. */
14462 cp_parser_mem_initializer (cp_parser
* parser
)
14464 tree mem_initializer_id
;
14465 tree expression_list
;
14467 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14469 /* Find out what is being initialized. */
14470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14472 permerror (token
->location
,
14473 "anachronistic old-style base class initializer");
14474 mem_initializer_id
= NULL_TREE
;
14478 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14479 if (mem_initializer_id
== error_mark_node
)
14480 return mem_initializer_id
;
14482 member
= expand_member_init (mem_initializer_id
);
14483 if (member
&& !DECL_P (member
))
14484 in_base_initializer
= 1;
14486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14488 bool expr_non_constant_p
;
14489 cp_lexer_set_source_position (parser
->lexer
);
14490 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14491 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14492 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14493 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14497 vec
<tree
, va_gc
> *vec
;
14498 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14500 /*allow_expansion_p=*/true,
14501 /*non_constant_p=*/NULL
);
14503 return error_mark_node
;
14504 expression_list
= build_tree_list_vec (vec
);
14505 release_tree_vector (vec
);
14508 if (expression_list
== error_mark_node
)
14509 return error_mark_node
;
14510 if (!expression_list
)
14511 expression_list
= void_type_node
;
14513 in_base_initializer
= 0;
14515 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14518 /* Parse a mem-initializer-id.
14520 mem-initializer-id:
14521 :: [opt] nested-name-specifier [opt] class-name
14522 decltype-specifier (C++11)
14525 Returns a TYPE indicating the class to be initialized for the first
14526 production (and the second in C++11). Returns an IDENTIFIER_NODE
14527 indicating the data member to be initialized for the last production. */
14530 cp_parser_mem_initializer_id (cp_parser
* parser
)
14532 bool global_scope_p
;
14533 bool nested_name_specifier_p
;
14534 bool template_p
= false;
14537 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14539 /* `typename' is not allowed in this context ([temp.res]). */
14540 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14542 error_at (token
->location
,
14543 "keyword %<typename%> not allowed in this context (a qualified "
14544 "member initializer is implicitly a type)");
14545 cp_lexer_consume_token (parser
->lexer
);
14547 /* Look for the optional `::' operator. */
14549 = (cp_parser_global_scope_opt (parser
,
14550 /*current_scope_valid_p=*/false)
14552 /* Look for the optional nested-name-specifier. The simplest way to
14557 The keyword `typename' is not permitted in a base-specifier or
14558 mem-initializer; in these contexts a qualified name that
14559 depends on a template-parameter is implicitly assumed to be a
14562 is to assume that we have seen the `typename' keyword at this
14564 nested_name_specifier_p
14565 = (cp_parser_nested_name_specifier_opt (parser
,
14566 /*typename_keyword_p=*/true,
14567 /*check_dependency_p=*/true,
14569 /*is_declaration=*/true)
14571 if (nested_name_specifier_p
)
14572 template_p
= cp_parser_optional_template_keyword (parser
);
14573 /* If there is a `::' operator or a nested-name-specifier, then we
14574 are definitely looking for a class-name. */
14575 if (global_scope_p
|| nested_name_specifier_p
)
14576 return cp_parser_class_name (parser
,
14577 /*typename_keyword_p=*/true,
14578 /*template_keyword_p=*/template_p
,
14580 /*check_dependency_p=*/true,
14581 /*class_head_p=*/false,
14582 /*is_declaration=*/true);
14583 /* Otherwise, we could also be looking for an ordinary identifier. */
14584 cp_parser_parse_tentatively (parser
);
14585 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14586 /* Try a decltype-specifier. */
14587 id
= cp_parser_decltype (parser
);
14589 /* Otherwise, try a class-name. */
14590 id
= cp_parser_class_name (parser
,
14591 /*typename_keyword_p=*/true,
14592 /*template_keyword_p=*/false,
14594 /*check_dependency_p=*/true,
14595 /*class_head_p=*/false,
14596 /*is_declaration=*/true);
14597 /* If we found one, we're done. */
14598 if (cp_parser_parse_definitely (parser
))
14600 /* Otherwise, look for an ordinary identifier. */
14601 return cp_parser_identifier (parser
);
14604 /* Overloading [gram.over] */
14606 /* Parse an operator-function-id.
14608 operator-function-id:
14611 Returns an IDENTIFIER_NODE for the operator which is a
14612 human-readable spelling of the identifier, e.g., `operator +'. */
14615 cp_parser_operator_function_id (cp_parser
* parser
)
14617 /* Look for the `operator' keyword. */
14618 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14619 return error_mark_node
;
14620 /* And then the name of the operator itself. */
14621 return cp_parser_operator (parser
);
14624 /* Return an identifier node for a user-defined literal operator.
14625 The suffix identifier is chained to the operator name identifier. */
14628 cp_literal_operator_id (const char* name
)
14631 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14632 + strlen (name
) + 10);
14633 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14634 identifier
= get_identifier (buffer
);
14639 /* Parse an operator.
14642 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14643 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14644 || ++ -- , ->* -> () []
14651 Returns an IDENTIFIER_NODE for the operator which is a
14652 human-readable spelling of the identifier, e.g., `operator +'. */
14655 cp_parser_operator (cp_parser
* parser
)
14657 tree id
= NULL_TREE
;
14661 /* Peek at the next token. */
14662 token
= cp_lexer_peek_token (parser
->lexer
);
14664 location_t start_loc
= token
->location
;
14666 /* Figure out which operator we have. */
14667 enum tree_code op
= ERROR_MARK
;
14668 bool assop
= false;
14669 bool consumed
= false;
14670 switch (token
->type
)
14674 /* The keyword should be either `new' or `delete'. */
14675 if (token
->keyword
== RID_NEW
)
14677 else if (token
->keyword
== RID_DELETE
)
14682 /* Consume the `new' or `delete' token. */
14683 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14685 /* Peek at the next token. */
14686 token
= cp_lexer_peek_token (parser
->lexer
);
14687 /* If it's a `[' token then this is the array variant of the
14689 if (token
->type
== CPP_OPEN_SQUARE
)
14691 /* Consume the `[' token. */
14692 cp_lexer_consume_token (parser
->lexer
);
14693 /* Look for the `]' token. */
14694 if (cp_token
*close_token
14695 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14696 end_loc
= close_token
->location
;
14697 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14699 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14717 op
= TRUNC_DIV_EXPR
;
14721 op
= TRUNC_MOD_EXPR
;
14741 op
= TRUTH_NOT_EXPR
;
14774 op
= TRUNC_DIV_EXPR
;
14779 op
= TRUNC_MOD_EXPR
;
14805 case CPP_LSHIFT_EQ
:
14810 case CPP_RSHIFT_EQ
:
14827 case CPP_GREATER_EQ
:
14832 op
= TRUTH_ANDIF_EXPR
;
14836 op
= TRUTH_ORIF_EXPR
;
14839 case CPP_PLUS_PLUS
:
14840 op
= POSTINCREMENT_EXPR
;
14843 case CPP_MINUS_MINUS
:
14844 op
= PREDECREMENT_EXPR
;
14848 op
= COMPOUND_EXPR
;
14851 case CPP_DEREF_STAR
:
14856 op
= COMPONENT_REF
;
14859 case CPP_OPEN_PAREN
:
14861 /* Consume the `('. */
14862 matching_parens parens
;
14863 parens
.consume_open (parser
);
14864 /* Look for the matching `)'. */
14865 parens
.require_close (parser
);
14871 case CPP_OPEN_SQUARE
:
14872 /* Consume the `['. */
14873 cp_lexer_consume_token (parser
->lexer
);
14874 /* Look for the matching `]'. */
14875 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14880 case CPP_UTF8STRING
:
14881 case CPP_UTF8STRING_USERDEF
:
14888 case CPP_STRING_USERDEF
:
14889 case CPP_WSTRING_USERDEF
:
14890 case CPP_STRING16_USERDEF
:
14891 case CPP_STRING32_USERDEF
:
14893 tree str
, string_tree
;
14896 if (cxx_dialect
== cxx98
)
14897 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14899 /* Consume the string. */
14900 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14901 /*wide_ok=*/true, /*lookup_udlit=*/false);
14902 if (str
== error_mark_node
)
14903 return error_mark_node
;
14904 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14906 string_tree
= USERDEF_LITERAL_VALUE (str
);
14907 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14912 /* Look for the suffix identifier. */
14913 token
= cp_lexer_peek_token (parser
->lexer
);
14914 if (token
->type
== CPP_NAME
)
14915 id
= cp_parser_identifier (parser
);
14916 else if (token
->type
== CPP_KEYWORD
)
14918 error ("unexpected keyword;"
14919 " remove space between quotes and suffix identifier");
14920 return error_mark_node
;
14924 error ("expected suffix identifier");
14925 return error_mark_node
;
14928 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14929 (TREE_TYPE (TREE_TYPE (string_tree
))));
14930 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14933 error ("expected empty string after %<operator%> keyword");
14934 return error_mark_node
;
14936 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14939 error ("invalid encoding prefix in literal operator");
14940 return error_mark_node
;
14942 if (id
!= error_mark_node
)
14944 const char *name
= IDENTIFIER_POINTER (id
);
14945 id
= cp_literal_operator_id (name
);
14951 /* Anything else is an error. */
14955 /* If we have selected an identifier, we need to consume the
14957 if (op
!= ERROR_MARK
)
14959 id
= ovl_op_identifier (assop
, op
);
14961 cp_lexer_consume_token (parser
->lexer
);
14963 /* Otherwise, no valid operator name was present. */
14966 cp_parser_error (parser
, "expected operator");
14967 id
= error_mark_node
;
14970 return cp_expr (id
, start_loc
);
14973 /* Parse a template-declaration.
14975 template-declaration:
14976 export [opt] template < template-parameter-list > declaration
14978 If MEMBER_P is TRUE, this template-declaration occurs within a
14981 The grammar rule given by the standard isn't correct. What
14982 is really meant is:
14984 template-declaration:
14985 export [opt] template-parameter-list-seq
14986 decl-specifier-seq [opt] init-declarator [opt] ;
14987 export [opt] template-parameter-list-seq
14988 function-definition
14990 template-parameter-list-seq:
14991 template-parameter-list-seq [opt]
14992 template < template-parameter-list >
14994 Concept Extensions:
14996 template-parameter-list-seq:
14997 template < template-parameter-list > requires-clause [opt]
15000 requires logical-or-expression */
15003 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15005 /* Check for `export'. */
15006 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15008 /* Consume the `export' token. */
15009 cp_lexer_consume_token (parser
->lexer
);
15010 /* Warn that we do not support `export'. */
15011 warning (0, "keyword %<export%> not implemented, and will be ignored");
15014 cp_parser_template_declaration_after_export (parser
, member_p
);
15017 /* Parse a template-parameter-list.
15019 template-parameter-list:
15021 template-parameter-list , template-parameter
15023 Returns a TREE_LIST. Each node represents a template parameter.
15024 The nodes are connected via their TREE_CHAINs. */
15027 cp_parser_template_parameter_list (cp_parser
* parser
)
15029 tree parameter_list
= NULL_TREE
;
15031 begin_template_parm_list ();
15033 /* The loop below parses the template parms. We first need to know
15034 the total number of template parms to be able to compute proper
15035 canonical types of each dependent type. So after the loop, when
15036 we know the total number of template parms,
15037 end_template_parm_list computes the proper canonical types and
15038 fixes up the dependent types accordingly. */
15043 bool is_parameter_pack
;
15044 location_t parm_loc
;
15046 /* Parse the template-parameter. */
15047 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15048 parameter
= cp_parser_template_parameter (parser
,
15050 &is_parameter_pack
);
15051 /* Add it to the list. */
15052 if (parameter
!= error_mark_node
)
15053 parameter_list
= process_template_parm (parameter_list
,
15057 is_parameter_pack
);
15060 tree err_parm
= build_tree_list (parameter
, parameter
);
15061 parameter_list
= chainon (parameter_list
, err_parm
);
15064 /* If the next token is not a `,', we're done. */
15065 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15067 /* Otherwise, consume the `,' token. */
15068 cp_lexer_consume_token (parser
->lexer
);
15071 return end_template_parm_list (parameter_list
);
15074 /* Parse a introduction-list.
15077 introduced-parameter
15078 introduction-list , introduced-parameter
15080 introduced-parameter:
15081 ...[opt] identifier
15083 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15084 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15085 WILDCARD_DECL will also have DECL_NAME set and token location in
15086 DECL_SOURCE_LOCATION. */
15089 cp_parser_introduction_list (cp_parser
*parser
)
15091 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15095 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15097 cp_lexer_consume_token (parser
->lexer
);
15099 /* Build placeholder. */
15100 tree parm
= build_nt (WILDCARD_DECL
);
15101 DECL_SOURCE_LOCATION (parm
)
15102 = cp_lexer_peek_token (parser
->lexer
)->location
;
15103 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15104 WILDCARD_PACK_P (parm
) = is_pack
;
15105 vec_safe_push (introduction_vec
, parm
);
15107 /* If the next token is not a `,', we're done. */
15108 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15110 /* Otherwise, consume the `,' token. */
15111 cp_lexer_consume_token (parser
->lexer
);
15114 /* Convert the vec into a TREE_VEC. */
15115 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15118 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15119 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15121 release_tree_vector (introduction_vec
);
15122 return introduction_list
;
15125 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15126 is an abstract declarator. */
15128 static inline cp_declarator
*
15129 get_id_declarator (cp_declarator
*declarator
)
15131 cp_declarator
*d
= declarator
;
15132 while (d
&& d
->kind
!= cdk_id
)
15137 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15138 is an abstract declarator. */
15141 get_unqualified_id (cp_declarator
*declarator
)
15143 declarator
= get_id_declarator (declarator
);
15145 return declarator
->u
.id
.unqualified_name
;
15150 /* Returns true if DECL represents a constrained-parameter. */
15153 is_constrained_parameter (tree decl
)
15156 && TREE_CODE (decl
) == TYPE_DECL
15157 && CONSTRAINED_PARM_CONCEPT (decl
)
15158 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15161 /* Returns true if PARM declares a constrained-parameter. */
15164 is_constrained_parameter (cp_parameter_declarator
*parm
)
15166 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15169 /* Check that the type parameter is only a declarator-id, and that its
15170 type is not cv-qualified. */
15173 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15174 cp_parameter_declarator
*parm
)
15176 if (!parm
->declarator
)
15179 if (parm
->declarator
->kind
!= cdk_id
)
15181 cp_parser_error (parser
, "invalid constrained type parameter");
15185 /* Don't allow cv-qualified type parameters. */
15186 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15187 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15189 cp_parser_error (parser
, "cv-qualified type parameter");
15196 /* Finish parsing/processing a template type parameter and checking
15197 various restrictions. */
15200 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15202 cp_parameter_declarator
* parmdecl
)
15204 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15205 return finish_template_type_parm (class_type_node
, id
);
15207 return error_mark_node
;
15211 finish_constrained_template_template_parm (tree proto
, tree id
)
15213 /* FIXME: This should probably be copied, and we may need to adjust
15214 the template parameter depths. */
15215 tree saved_parms
= current_template_parms
;
15216 begin_template_parm_list ();
15217 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15218 end_template_parm_list ();
15220 tree parm
= finish_template_template_parm (class_type_node
, id
);
15221 current_template_parms
= saved_parms
;
15226 /* Finish parsing/processing a template template parameter by borrowing
15227 the template parameter list from the prototype parameter. */
15230 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15233 cp_parameter_declarator
*parmdecl
)
15235 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15236 return error_mark_node
;
15237 return finish_constrained_template_template_parm (proto
, id
);
15240 /* Create a new non-type template parameter from the given PARM
15244 constrained_non_type_template_parm (bool *is_non_type
,
15245 cp_parameter_declarator
*parm
)
15247 *is_non_type
= true;
15248 cp_declarator
*decl
= parm
->declarator
;
15249 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15250 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15251 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15254 /* Build a constrained template parameter based on the PARMDECL
15255 declarator. The type of PARMDECL is the constrained type, which
15256 refers to the prototype template parameter that ultimately
15257 specifies the type of the declared parameter. */
15260 finish_constrained_parameter (cp_parser
*parser
,
15261 cp_parameter_declarator
*parmdecl
,
15263 bool *is_parameter_pack
)
15265 tree decl
= parmdecl
->decl_specifiers
.type
;
15266 tree id
= get_unqualified_id (parmdecl
->declarator
);
15267 tree def
= parmdecl
->default_argument
;
15268 tree proto
= DECL_INITIAL (decl
);
15270 /* A template parameter constrained by a variadic concept shall also
15271 be declared as a template parameter pack. */
15272 bool is_variadic
= template_parameter_pack_p (proto
);
15273 if (is_variadic
&& !*is_parameter_pack
)
15274 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15276 /* Build the parameter. Return an error if the declarator was invalid. */
15278 if (TREE_CODE (proto
) == TYPE_DECL
)
15279 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15280 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15281 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15284 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15285 if (parm
== error_mark_node
)
15286 return error_mark_node
;
15288 /* Finish the parameter decl and create a node attaching the
15289 default argument and constraint. */
15290 parm
= build_tree_list (def
, parm
);
15291 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15296 /* Returns true if the parsed type actually represents the declaration
15297 of a type template-parameter. */
15300 declares_constrained_type_template_parameter (tree type
)
15302 return (is_constrained_parameter (type
)
15303 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15307 /* Returns true if the parsed type actually represents the declaration of
15308 a template template-parameter. */
15311 declares_constrained_template_template_parameter (tree type
)
15313 return (is_constrained_parameter (type
)
15314 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15317 /* Parse a default argument for a type template-parameter.
15318 Note that diagnostics are handled in cp_parser_template_parameter. */
15321 cp_parser_default_type_template_argument (cp_parser
*parser
)
15323 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15325 /* Consume the `=' token. */
15326 cp_lexer_consume_token (parser
->lexer
);
15328 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15330 /* Parse the default-argument. */
15331 push_deferring_access_checks (dk_no_deferred
);
15332 tree default_argument
= cp_parser_type_id (parser
);
15333 pop_deferring_access_checks ();
15335 if (flag_concepts
&& type_uses_auto (default_argument
))
15337 error_at (token
->location
,
15338 "invalid use of %<auto%> in default template argument");
15339 return error_mark_node
;
15342 return default_argument
;
15345 /* Parse a default argument for a template template-parameter. */
15348 cp_parser_default_template_template_argument (cp_parser
*parser
)
15350 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15354 /* Consume the `='. */
15355 cp_lexer_consume_token (parser
->lexer
);
15356 /* Parse the id-expression. */
15357 push_deferring_access_checks (dk_no_deferred
);
15358 /* save token before parsing the id-expression, for error
15360 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15361 tree default_argument
15362 = cp_parser_id_expression (parser
,
15363 /*template_keyword_p=*/false,
15364 /*check_dependency_p=*/true,
15365 /*template_p=*/&is_template
,
15366 /*declarator_p=*/false,
15367 /*optional_p=*/false);
15368 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15369 /* If the id-expression was a template-id that refers to
15370 a template-class, we already have the declaration here,
15371 so no further lookup is needed. */
15374 /* Look up the name. */
15376 = cp_parser_lookup_name (parser
, default_argument
,
15378 /*is_template=*/is_template
,
15379 /*is_namespace=*/false,
15380 /*check_dependency=*/true,
15381 /*ambiguous_decls=*/NULL
,
15383 /* See if the default argument is valid. */
15384 default_argument
= check_template_template_default_arg (default_argument
);
15385 pop_deferring_access_checks ();
15386 return default_argument
;
15389 /* Parse a template-parameter.
15391 template-parameter:
15393 parameter-declaration
15395 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15396 the parameter. The TREE_PURPOSE is the default value, if any.
15397 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15398 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15399 set to true iff this parameter is a parameter pack. */
15402 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15403 bool *is_parameter_pack
)
15406 cp_parameter_declarator
*parameter_declarator
;
15409 /* Assume it is a type parameter or a template parameter. */
15410 *is_non_type
= false;
15411 /* Assume it not a parameter pack. */
15412 *is_parameter_pack
= false;
15413 /* Peek at the next token. */
15414 token
= cp_lexer_peek_token (parser
->lexer
);
15415 /* If it is `template', we have a type-parameter. */
15416 if (token
->keyword
== RID_TEMPLATE
)
15417 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15418 /* If it is `class' or `typename' we do not know yet whether it is a
15419 type parameter or a non-type parameter. Consider:
15421 template <typename T, typename T::X X> ...
15425 template <class C, class D*> ...
15427 Here, the first parameter is a type parameter, and the second is
15428 a non-type parameter. We can tell by looking at the token after
15429 the identifier -- if it is a `,', `=', or `>' then we have a type
15431 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15433 /* Peek at the token after `class' or `typename'. */
15434 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15435 /* If it's an ellipsis, we have a template type parameter
15437 if (token
->type
== CPP_ELLIPSIS
)
15438 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15439 /* If it's an identifier, skip it. */
15440 if (token
->type
== CPP_NAME
)
15441 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15442 /* Now, see if the token looks like the end of a template
15444 if (token
->type
== CPP_COMMA
15445 || token
->type
== CPP_EQ
15446 || token
->type
== CPP_GREATER
)
15447 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15450 /* Otherwise, it is a non-type parameter or a constrained parameter.
15454 When parsing a default template-argument for a non-type
15455 template-parameter, the first non-nested `>' is taken as the end
15456 of the template parameter-list rather than a greater-than
15458 parameter_declarator
15459 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15460 /*parenthesized_p=*/NULL
);
15462 if (!parameter_declarator
)
15463 return error_mark_node
;
15465 /* If the parameter declaration is marked as a parameter pack, set
15466 *IS_PARAMETER_PACK to notify the caller. */
15467 if (parameter_declarator
->template_parameter_pack_p
)
15468 *is_parameter_pack
= true;
15470 if (parameter_declarator
->default_argument
)
15472 /* Can happen in some cases of erroneous input (c++/34892). */
15473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15474 /* Consume the `...' for better error recovery. */
15475 cp_lexer_consume_token (parser
->lexer
);
15478 // The parameter may have been constrained.
15479 if (is_constrained_parameter (parameter_declarator
))
15480 return finish_constrained_parameter (parser
,
15481 parameter_declarator
,
15483 is_parameter_pack
);
15485 // Now we're sure that the parameter is a non-type parameter.
15486 *is_non_type
= true;
15488 parm
= grokdeclarator (parameter_declarator
->declarator
,
15489 ¶meter_declarator
->decl_specifiers
,
15490 TPARM
, /*initialized=*/0,
15491 /*attrlist=*/NULL
);
15492 if (parm
== error_mark_node
)
15493 return error_mark_node
;
15495 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15498 /* Parse a type-parameter.
15501 class identifier [opt]
15502 class identifier [opt] = type-id
15503 typename identifier [opt]
15504 typename identifier [opt] = type-id
15505 template < template-parameter-list > class identifier [opt]
15506 template < template-parameter-list > class identifier [opt]
15509 GNU Extension (variadic templates):
15512 class ... identifier [opt]
15513 typename ... identifier [opt]
15515 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15516 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15517 the declaration of the parameter.
15519 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15522 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15527 /* Look for a keyword to tell us what kind of parameter this is. */
15528 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15530 return error_mark_node
;
15532 switch (token
->keyword
)
15538 tree default_argument
;
15540 /* If the next token is an ellipsis, we have a template
15542 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15544 /* Consume the `...' token. */
15545 cp_lexer_consume_token (parser
->lexer
);
15546 maybe_warn_variadic_templates ();
15548 *is_parameter_pack
= true;
15551 /* If the next token is an identifier, then it names the
15553 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15554 identifier
= cp_parser_identifier (parser
);
15556 identifier
= NULL_TREE
;
15558 /* Create the parameter. */
15559 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15561 /* If the next token is an `=', we have a default argument. */
15562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15565 = cp_parser_default_type_template_argument (parser
);
15567 /* Template parameter packs cannot have default
15569 if (*is_parameter_pack
)
15572 error_at (token
->location
,
15573 "template parameter pack %qD cannot have a "
15574 "default argument", identifier
);
15576 error_at (token
->location
,
15577 "template parameter packs cannot have "
15578 "default arguments");
15579 default_argument
= NULL_TREE
;
15581 else if (check_for_bare_parameter_packs (default_argument
))
15582 default_argument
= error_mark_node
;
15585 default_argument
= NULL_TREE
;
15587 /* Create the combined representation of the parameter and the
15588 default argument. */
15589 parameter
= build_tree_list (default_argument
, parameter
);
15596 tree default_argument
;
15598 /* Look for the `<'. */
15599 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15600 /* Parse the template-parameter-list. */
15601 cp_parser_template_parameter_list (parser
);
15602 /* Look for the `>'. */
15603 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15605 // If template requirements are present, parse them.
15608 tree reqs
= get_shorthand_constraints (current_template_parms
);
15609 if (tree r
= cp_parser_requires_clause_opt (parser
))
15610 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15611 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15614 /* Look for the `class' or 'typename' keywords. */
15615 cp_parser_type_parameter_key (parser
);
15616 /* If the next token is an ellipsis, we have a template
15618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15620 /* Consume the `...' token. */
15621 cp_lexer_consume_token (parser
->lexer
);
15622 maybe_warn_variadic_templates ();
15624 *is_parameter_pack
= true;
15626 /* If the next token is an `=', then there is a
15627 default-argument. If the next token is a `>', we are at
15628 the end of the parameter-list. If the next token is a `,',
15629 then we are at the end of this parameter. */
15630 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15631 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15632 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15634 identifier
= cp_parser_identifier (parser
);
15635 /* Treat invalid names as if the parameter were nameless. */
15636 if (identifier
== error_mark_node
)
15637 identifier
= NULL_TREE
;
15640 identifier
= NULL_TREE
;
15642 /* Create the template parameter. */
15643 parameter
= finish_template_template_parm (class_type_node
,
15646 /* If the next token is an `=', then there is a
15647 default-argument. */
15648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15651 = cp_parser_default_template_template_argument (parser
);
15653 /* Template parameter packs cannot have default
15655 if (*is_parameter_pack
)
15658 error_at (token
->location
,
15659 "template parameter pack %qD cannot "
15660 "have a default argument",
15663 error_at (token
->location
, "template parameter packs cannot "
15664 "have default arguments");
15665 default_argument
= NULL_TREE
;
15669 default_argument
= NULL_TREE
;
15671 /* Create the combined representation of the parameter and the
15672 default argument. */
15673 parameter
= build_tree_list (default_argument
, parameter
);
15678 gcc_unreachable ();
15685 /* Parse a template-id.
15688 template-name < template-argument-list [opt] >
15690 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15691 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15692 returned. Otherwise, if the template-name names a function, or set
15693 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15694 names a class, returns a TYPE_DECL for the specialization.
15696 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15697 uninstantiated templates. */
15700 cp_parser_template_id (cp_parser
*parser
,
15701 bool template_keyword_p
,
15702 bool check_dependency_p
,
15703 enum tag_types tag_type
,
15704 bool is_declaration
)
15709 cp_token_position start_of_id
= 0;
15710 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15711 bool is_identifier
;
15713 /* If the next token corresponds to a template-id, there is no need
15715 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15716 if (token
->type
== CPP_TEMPLATE_ID
)
15718 cp_lexer_consume_token (parser
->lexer
);
15719 return saved_checks_value (token
->u
.tree_check_value
);
15722 /* Avoid performing name lookup if there is no possibility of
15723 finding a template-id. */
15724 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15725 || (token
->type
== CPP_NAME
15726 && !cp_parser_nth_token_starts_template_argument_list_p
15729 cp_parser_error (parser
, "expected template-id");
15730 return error_mark_node
;
15733 /* Remember where the template-id starts. */
15734 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15735 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15737 push_deferring_access_checks (dk_deferred
);
15739 /* Parse the template-name. */
15740 is_identifier
= false;
15741 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15742 check_dependency_p
,
15746 if (templ
== error_mark_node
|| is_identifier
)
15748 pop_deferring_access_checks ();
15752 /* Since we're going to preserve any side-effects from this parse, set up a
15753 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15754 in the template arguments. */
15755 tentative_firewall
firewall (parser
);
15757 /* If we find the sequence `[:' after a template-name, it's probably
15758 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15759 parse correctly the argument list. */
15760 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15761 == CPP_OPEN_SQUARE
)
15762 && next_token
->flags
& DIGRAPH
15763 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15765 && !(next_token_2
->flags
& PREV_WHITE
))
15767 cp_parser_parse_tentatively (parser
);
15768 /* Change `:' into `::'. */
15769 next_token_2
->type
= CPP_SCOPE
;
15770 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15772 cp_lexer_consume_token (parser
->lexer
);
15774 /* Parse the arguments. */
15775 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15776 if (!cp_parser_parse_definitely (parser
))
15778 /* If we couldn't parse an argument list, then we revert our changes
15779 and return simply an error. Maybe this is not a template-id
15781 next_token_2
->type
= CPP_COLON
;
15782 cp_parser_error (parser
, "expected %<<%>");
15783 pop_deferring_access_checks ();
15784 return error_mark_node
;
15786 /* Otherwise, emit an error about the invalid digraph, but continue
15787 parsing because we got our argument list. */
15788 if (permerror (next_token
->location
,
15789 "%<<::%> cannot begin a template-argument list"))
15791 static bool hint
= false;
15792 inform (next_token
->location
,
15793 "%<<:%> is an alternate spelling for %<[%>."
15794 " Insert whitespace between %<<%> and %<::%>");
15795 if (!hint
&& !flag_permissive
)
15797 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15798 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15799 "accept your code)");
15806 /* Look for the `<' that starts the template-argument-list. */
15807 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15809 pop_deferring_access_checks ();
15810 return error_mark_node
;
15812 /* Parse the arguments. */
15813 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15816 /* Set the location to be of the form:
15817 template-name < template-argument-list [opt] >
15818 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15819 with caret == start at the start of the template-name,
15820 ranging until the closing '>'. */
15821 location_t finish_loc
15822 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15823 location_t combined_loc
15824 = make_location (token
->location
, token
->location
, finish_loc
);
15826 /* Check for concepts autos where they don't belong. We could
15827 identify types in some cases of idnetifier TEMPL, looking ahead
15828 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15829 types. We reject them in functions, but if what we have is an
15830 identifier, even with none_type we can't conclude it's NOT a
15831 type, we have to wait for template substitution. */
15832 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15833 template_id
= error_mark_node
;
15834 /* Build a representation of the specialization. */
15835 else if (identifier_p (templ
))
15836 template_id
= build_min_nt_loc (combined_loc
,
15839 else if (DECL_TYPE_TEMPLATE_P (templ
)
15840 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15842 bool entering_scope
;
15843 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15844 template (rather than some instantiation thereof) only if
15845 is not nested within some other construct. For example, in
15846 "template <typename T> void f(T) { A<T>::", A<T> is just an
15847 instantiation of A. */
15848 entering_scope
= (template_parm_scope_p ()
15849 && cp_lexer_next_token_is (parser
->lexer
,
15852 = finish_template_type (templ
, arguments
, entering_scope
);
15854 /* A template-like identifier may be a partial concept id. */
15855 else if (flag_concepts
15856 && (template_id
= (cp_parser_maybe_partial_concept_id
15857 (parser
, templ
, arguments
))))
15858 return template_id
;
15859 else if (variable_template_p (templ
))
15861 template_id
= lookup_template_variable (templ
, arguments
);
15862 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15863 SET_EXPR_LOCATION (template_id
, combined_loc
);
15867 /* If it's not a class-template or a template-template, it should be
15868 a function-template. */
15869 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15870 || TREE_CODE (templ
) == OVERLOAD
15871 || BASELINK_P (templ
)));
15873 template_id
= lookup_template_function (templ
, arguments
);
15874 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15875 SET_EXPR_LOCATION (template_id
, combined_loc
);
15878 /* If parsing tentatively, replace the sequence of tokens that makes
15879 up the template-id with a CPP_TEMPLATE_ID token. That way,
15880 should we re-parse the token stream, we will not have to repeat
15881 the effort required to do the parse, nor will we issue duplicate
15882 error messages about problems during instantiation of the
15885 /* Don't do this if we had a parse error in a declarator; re-parsing
15886 might succeed if a name changes meaning (60361). */
15887 && !(cp_parser_error_occurred (parser
)
15888 && cp_parser_parsing_tentatively (parser
)
15889 && parser
->in_declarator_p
))
15891 /* Reset the contents of the START_OF_ID token. */
15892 token
->type
= CPP_TEMPLATE_ID
;
15893 token
->location
= combined_loc
;
15895 /* We must mark the lookup as kept, so we don't throw it away on
15896 the first parse. */
15897 if (is_overloaded_fn (template_id
))
15898 lookup_keep (get_fns (template_id
), true);
15900 /* Retrieve any deferred checks. Do not pop this access checks yet
15901 so the memory will not be reclaimed during token replacing below. */
15902 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15903 token
->u
.tree_check_value
->value
= template_id
;
15904 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15905 token
->keyword
= RID_MAX
;
15907 /* Purge all subsequent tokens. */
15908 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15910 /* ??? Can we actually assume that, if template_id ==
15911 error_mark_node, we will have issued a diagnostic to the
15912 user, as opposed to simply marking the tentative parse as
15914 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15915 error_at (token
->location
, "parse error in template argument list");
15918 pop_to_parent_deferring_access_checks ();
15919 return template_id
;
15922 /* Parse a template-name.
15927 The standard should actually say:
15931 operator-function-id
15933 A defect report has been filed about this issue.
15935 A conversion-function-id cannot be a template name because they cannot
15936 be part of a template-id. In fact, looking at this code:
15938 a.operator K<int>()
15940 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15941 It is impossible to call a templated conversion-function-id with an
15942 explicit argument list, since the only allowed template parameter is
15943 the type to which it is converting.
15945 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15946 `template' keyword, in a construction like:
15950 In that case `f' is taken to be a template-name, even though there
15951 is no way of knowing for sure.
15953 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15954 name refers to a set of overloaded functions, at least one of which
15955 is a template, or an IDENTIFIER_NODE with the name of the template,
15956 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15957 names are looked up inside uninstantiated templates. */
15960 cp_parser_template_name (cp_parser
* parser
,
15961 bool template_keyword_p
,
15962 bool check_dependency_p
,
15963 bool is_declaration
,
15964 enum tag_types tag_type
,
15965 bool *is_identifier
)
15969 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15971 /* If the next token is `operator', then we have either an
15972 operator-function-id or a conversion-function-id. */
15973 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15975 /* We don't know whether we're looking at an
15976 operator-function-id or a conversion-function-id. */
15977 cp_parser_parse_tentatively (parser
);
15978 /* Try an operator-function-id. */
15979 identifier
= cp_parser_operator_function_id (parser
);
15980 /* If that didn't work, try a conversion-function-id. */
15981 if (!cp_parser_parse_definitely (parser
))
15983 cp_parser_error (parser
, "expected template-name");
15984 return error_mark_node
;
15987 /* Look for the identifier. */
15989 identifier
= cp_parser_identifier (parser
);
15991 /* If we didn't find an identifier, we don't have a template-id. */
15992 if (identifier
== error_mark_node
)
15993 return error_mark_node
;
15995 /* If the name immediately followed the `template' keyword, then it
15996 is a template-name. However, if the next token is not `<', then
15997 we do not treat it as a template-name, since it is not being used
15998 as part of a template-id. This enables us to handle constructs
16001 template <typename T> struct S { S(); };
16002 template <typename T> S<T>::S();
16004 correctly. We would treat `S' as a template -- if it were `S<T>'
16005 -- but we do not if there is no `<'. */
16007 if (processing_template_decl
16008 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16010 /* In a declaration, in a dependent context, we pretend that the
16011 "template" keyword was present in order to improve error
16012 recovery. For example, given:
16014 template <typename T> void f(T::X<int>);
16016 we want to treat "X<int>" as a template-id. */
16018 && !template_keyword_p
16019 && parser
->scope
&& TYPE_P (parser
->scope
)
16020 && check_dependency_p
16021 && dependent_scope_p (parser
->scope
)
16022 /* Do not do this for dtors (or ctors), since they never
16023 need the template keyword before their name. */
16024 && !constructor_name_p (identifier
, parser
->scope
))
16026 cp_token_position start
= 0;
16028 /* Explain what went wrong. */
16029 error_at (token
->location
, "non-template %qD used as template",
16031 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16032 parser
->scope
, identifier
);
16033 /* If parsing tentatively, find the location of the "<" token. */
16034 if (cp_parser_simulate_error (parser
))
16035 start
= cp_lexer_token_position (parser
->lexer
, true);
16036 /* Parse the template arguments so that we can issue error
16037 messages about them. */
16038 cp_lexer_consume_token (parser
->lexer
);
16039 cp_parser_enclosed_template_argument_list (parser
);
16040 /* Skip tokens until we find a good place from which to
16041 continue parsing. */
16042 cp_parser_skip_to_closing_parenthesis (parser
,
16043 /*recovering=*/true,
16045 /*consume_paren=*/false);
16046 /* If parsing tentatively, permanently remove the
16047 template argument list. That will prevent duplicate
16048 error messages from being issued about the missing
16049 "template" keyword. */
16051 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16053 *is_identifier
= true;
16054 parser
->context
->object_type
= NULL_TREE
;
16058 /* If the "template" keyword is present, then there is generally
16059 no point in doing name-lookup, so we just return IDENTIFIER.
16060 But, if the qualifying scope is non-dependent then we can
16061 (and must) do name-lookup normally. */
16062 if (template_keyword_p
)
16064 tree scope
= (parser
->scope
? parser
->scope
16065 : parser
->context
->object_type
);
16066 if (scope
&& TYPE_P (scope
)
16067 && (!CLASS_TYPE_P (scope
)
16068 || (check_dependency_p
&& dependent_type_p (scope
))))
16070 /* We're optimizing away the call to cp_parser_lookup_name, but
16071 we still need to do this. */
16072 parser
->context
->object_type
= NULL_TREE
;
16078 /* Look up the name. */
16079 decl
= cp_parser_lookup_name (parser
, identifier
,
16081 /*is_template=*/true,
16082 /*is_namespace=*/false,
16083 check_dependency_p
,
16084 /*ambiguous_decls=*/NULL
,
16087 decl
= strip_using_decl (decl
);
16089 /* If DECL is a template, then the name was a template-name. */
16090 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16092 if (TREE_DEPRECATED (decl
)
16093 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16094 warn_deprecated_use (decl
, NULL_TREE
);
16098 /* The standard does not explicitly indicate whether a name that
16099 names a set of overloaded declarations, some of which are
16100 templates, is a template-name. However, such a name should
16101 be a template-name; otherwise, there is no way to form a
16102 template-id for the overloaded templates. */
16103 bool found
= false;
16105 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16106 !found
&& iter
; ++iter
)
16107 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16112 /* The name does not name a template. */
16113 cp_parser_error (parser
, "expected template-name");
16114 return error_mark_node
;
16118 /* If DECL is dependent, and refers to a function, then just return
16119 its name; we will look it up again during template instantiation. */
16120 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16122 tree scope
= ovl_scope (decl
);
16123 if (TYPE_P (scope
) && dependent_type_p (scope
))
16130 /* Parse a template-argument-list.
16132 template-argument-list:
16133 template-argument ... [opt]
16134 template-argument-list , template-argument ... [opt]
16136 Returns a TREE_VEC containing the arguments. */
16139 cp_parser_template_argument_list (cp_parser
* parser
)
16141 tree fixed_args
[10];
16142 unsigned n_args
= 0;
16143 unsigned alloced
= 10;
16144 tree
*arg_ary
= fixed_args
;
16146 bool saved_in_template_argument_list_p
;
16148 bool saved_non_ice_p
;
16150 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16151 parser
->in_template_argument_list_p
= true;
16152 /* Even if the template-id appears in an integral
16153 constant-expression, the contents of the argument list do
16155 saved_ice_p
= parser
->integral_constant_expression_p
;
16156 parser
->integral_constant_expression_p
= false;
16157 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16158 parser
->non_integral_constant_expression_p
= false;
16160 /* Parse the arguments. */
16166 /* Consume the comma. */
16167 cp_lexer_consume_token (parser
->lexer
);
16169 /* Parse the template-argument. */
16170 argument
= cp_parser_template_argument (parser
);
16172 /* If the next token is an ellipsis, we're expanding a template
16174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16176 if (argument
== error_mark_node
)
16178 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16179 error_at (token
->location
,
16180 "expected parameter pack before %<...%>");
16182 /* Consume the `...' token. */
16183 cp_lexer_consume_token (parser
->lexer
);
16185 /* Make the argument into a TYPE_PACK_EXPANSION or
16186 EXPR_PACK_EXPANSION. */
16187 argument
= make_pack_expansion (argument
);
16190 if (n_args
== alloced
)
16194 if (arg_ary
== fixed_args
)
16196 arg_ary
= XNEWVEC (tree
, alloced
);
16197 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16200 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16202 arg_ary
[n_args
++] = argument
;
16204 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16206 vec
= make_tree_vec (n_args
);
16209 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16211 if (arg_ary
!= fixed_args
)
16213 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16214 parser
->integral_constant_expression_p
= saved_ice_p
;
16215 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16217 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16221 /* Parse a template-argument.
16224 assignment-expression
16228 The representation is that of an assignment-expression, type-id, or
16229 id-expression -- except that the qualified id-expression is
16230 evaluated, so that the value returned is either a DECL or an
16233 Although the standard says "assignment-expression", it forbids
16234 throw-expressions or assignments in the template argument.
16235 Therefore, we use "conditional-expression" instead. */
16238 cp_parser_template_argument (cp_parser
* parser
)
16243 bool maybe_type_id
= false;
16244 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16245 location_t loc
= 0;
16248 /* There's really no way to know what we're looking at, so we just
16249 try each alternative in order.
16253 In a template-argument, an ambiguity between a type-id and an
16254 expression is resolved to a type-id, regardless of the form of
16255 the corresponding template-parameter.
16257 Therefore, we try a type-id first. */
16258 cp_parser_parse_tentatively (parser
);
16259 argument
= cp_parser_template_type_arg (parser
);
16260 /* If there was no error parsing the type-id but the next token is a
16261 '>>', our behavior depends on which dialect of C++ we're
16262 parsing. In C++98, we probably found a typo for '> >'. But there
16263 are type-id which are also valid expressions. For instance:
16265 struct X { int operator >> (int); };
16266 template <int V> struct Foo {};
16269 Here 'X()' is a valid type-id of a function type, but the user just
16270 wanted to write the expression "X() >> 5". Thus, we remember that we
16271 found a valid type-id, but we still try to parse the argument as an
16272 expression to see what happens.
16274 In C++0x, the '>>' will be considered two separate '>'
16276 if (!cp_parser_error_occurred (parser
)
16277 && cxx_dialect
== cxx98
16278 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16280 maybe_type_id
= true;
16281 cp_parser_abort_tentative_parse (parser
);
16285 /* If the next token isn't a `,' or a `>', then this argument wasn't
16286 really finished. This means that the argument is not a valid
16288 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16289 cp_parser_error (parser
, "expected template-argument");
16290 /* If that worked, we're done. */
16291 if (cp_parser_parse_definitely (parser
))
16294 /* We're still not sure what the argument will be. */
16295 cp_parser_parse_tentatively (parser
);
16296 /* Try a template. */
16297 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16298 argument
= cp_parser_id_expression (parser
,
16299 /*template_keyword_p=*/false,
16300 /*check_dependency_p=*/true,
16302 /*declarator_p=*/false,
16303 /*optional_p=*/false);
16304 /* If the next token isn't a `,' or a `>', then this argument wasn't
16305 really finished. */
16306 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16307 cp_parser_error (parser
, "expected template-argument");
16308 if (!cp_parser_error_occurred (parser
))
16310 /* Figure out what is being referred to. If the id-expression
16311 was for a class template specialization, then we will have a
16312 TYPE_DECL at this point. There is no need to do name lookup
16313 at this point in that case. */
16314 if (TREE_CODE (argument
) != TYPE_DECL
)
16315 argument
= cp_parser_lookup_name (parser
, argument
,
16317 /*is_template=*/template_p
,
16318 /*is_namespace=*/false,
16319 /*check_dependency=*/true,
16320 /*ambiguous_decls=*/NULL
,
16321 argument_start_token
->location
);
16322 /* Handle a constrained-type-specifier for a non-type template
16324 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16326 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16327 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16328 cp_parser_error (parser
, "expected template-name");
16330 if (cp_parser_parse_definitely (parser
))
16332 if (TREE_DEPRECATED (argument
))
16333 warn_deprecated_use (argument
, NULL_TREE
);
16336 /* It must be a non-type argument. In C++17 any constant-expression is
16338 if (cxx_dialect
> cxx14
)
16341 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16343 -- an integral constant-expression of integral or enumeration
16346 -- the name of a non-type template-parameter; or
16348 -- the name of an object or function with external linkage...
16350 -- the address of an object or function with external linkage...
16352 -- a pointer to member... */
16353 /* Look for a non-type template parameter. */
16354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16356 cp_parser_parse_tentatively (parser
);
16357 argument
= cp_parser_primary_expression (parser
,
16358 /*address_p=*/false,
16360 /*template_arg_p=*/true,
16362 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16363 || !cp_parser_next_token_ends_template_argument_p (parser
))
16364 cp_parser_simulate_error (parser
);
16365 if (cp_parser_parse_definitely (parser
))
16369 /* If the next token is "&", the argument must be the address of an
16370 object or function with external linkage. */
16371 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16374 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16375 cp_lexer_consume_token (parser
->lexer
);
16377 /* See if we might have an id-expression. */
16378 token
= cp_lexer_peek_token (parser
->lexer
);
16379 if (token
->type
== CPP_NAME
16380 || token
->keyword
== RID_OPERATOR
16381 || token
->type
== CPP_SCOPE
16382 || token
->type
== CPP_TEMPLATE_ID
16383 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16385 cp_parser_parse_tentatively (parser
);
16386 argument
= cp_parser_primary_expression (parser
,
16389 /*template_arg_p=*/true,
16391 if (cp_parser_error_occurred (parser
)
16392 || !cp_parser_next_token_ends_template_argument_p (parser
))
16393 cp_parser_abort_tentative_parse (parser
);
16398 if (INDIRECT_REF_P (argument
))
16400 /* Strip the dereference temporarily. */
16401 gcc_assert (REFERENCE_REF_P (argument
));
16402 argument
= TREE_OPERAND (argument
, 0);
16405 /* If we're in a template, we represent a qualified-id referring
16406 to a static data member as a SCOPE_REF even if the scope isn't
16407 dependent so that we can check access control later. */
16409 if (TREE_CODE (probe
) == SCOPE_REF
)
16410 probe
= TREE_OPERAND (probe
, 1);
16413 /* A variable without external linkage might still be a
16414 valid constant-expression, so no error is issued here
16415 if the external-linkage check fails. */
16416 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16417 cp_parser_simulate_error (parser
);
16419 else if (is_overloaded_fn (argument
))
16420 /* All overloaded functions are allowed; if the external
16421 linkage test does not pass, an error will be issued
16425 && (TREE_CODE (argument
) == OFFSET_REF
16426 || TREE_CODE (argument
) == SCOPE_REF
))
16427 /* A pointer-to-member. */
16429 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16432 cp_parser_simulate_error (parser
);
16434 if (cp_parser_parse_definitely (parser
))
16437 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16438 tf_warning_or_error
);
16440 argument
= convert_from_reference (argument
);
16445 /* If the argument started with "&", there are no other valid
16446 alternatives at this point. */
16449 cp_parser_error (parser
, "invalid non-type template argument");
16450 return error_mark_node
;
16454 /* If the argument wasn't successfully parsed as a type-id followed
16455 by '>>', the argument can only be a constant expression now.
16456 Otherwise, we try parsing the constant-expression tentatively,
16457 because the argument could really be a type-id. */
16459 cp_parser_parse_tentatively (parser
);
16461 if (cxx_dialect
<= cxx14
)
16462 argument
= cp_parser_constant_expression (parser
);
16465 /* With C++17 generalized non-type template arguments we need to handle
16466 lvalue constant expressions, too. */
16467 argument
= cp_parser_assignment_expression (parser
);
16468 require_potential_constant_expression (argument
);
16471 if (!maybe_type_id
)
16473 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16474 cp_parser_error (parser
, "expected template-argument");
16475 if (cp_parser_parse_definitely (parser
))
16477 /* We did our best to parse the argument as a non type-id, but that
16478 was the only alternative that matched (albeit with a '>' after
16479 it). We can assume it's just a typo from the user, and a
16480 diagnostic will then be issued. */
16481 return cp_parser_template_type_arg (parser
);
16484 /* Parse an explicit-instantiation.
16486 explicit-instantiation:
16487 template declaration
16489 Although the standard says `declaration', what it really means is:
16491 explicit-instantiation:
16492 template decl-specifier-seq [opt] declarator [opt] ;
16494 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16495 supposed to be allowed. A defect report has been filed about this
16500 explicit-instantiation:
16501 storage-class-specifier template
16502 decl-specifier-seq [opt] declarator [opt] ;
16503 function-specifier template
16504 decl-specifier-seq [opt] declarator [opt] ; */
16507 cp_parser_explicit_instantiation (cp_parser
* parser
)
16509 int declares_class_or_enum
;
16510 cp_decl_specifier_seq decl_specifiers
;
16511 tree extension_specifier
= NULL_TREE
;
16513 timevar_push (TV_TEMPLATE_INST
);
16515 /* Look for an (optional) storage-class-specifier or
16516 function-specifier. */
16517 if (cp_parser_allow_gnu_extensions_p (parser
))
16519 extension_specifier
16520 = cp_parser_storage_class_specifier_opt (parser
);
16521 if (!extension_specifier
)
16522 extension_specifier
16523 = cp_parser_function_specifier_opt (parser
,
16524 /*decl_specs=*/NULL
);
16527 /* Look for the `template' keyword. */
16528 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16529 /* Let the front end know that we are processing an explicit
16531 begin_explicit_instantiation ();
16532 /* [temp.explicit] says that we are supposed to ignore access
16533 control while processing explicit instantiation directives. */
16534 push_deferring_access_checks (dk_no_check
);
16535 /* Parse a decl-specifier-seq. */
16536 cp_parser_decl_specifier_seq (parser
,
16537 CP_PARSER_FLAGS_OPTIONAL
,
16539 &declares_class_or_enum
);
16540 /* If there was exactly one decl-specifier, and it declared a class,
16541 and there's no declarator, then we have an explicit type
16543 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16547 type
= check_tag_decl (&decl_specifiers
,
16548 /*explicit_type_instantiation_p=*/true);
16549 /* Turn access control back on for names used during
16550 template instantiation. */
16551 pop_deferring_access_checks ();
16553 do_type_instantiation (type
, extension_specifier
,
16554 /*complain=*/tf_error
);
16558 cp_declarator
*declarator
;
16561 /* Parse the declarator. */
16563 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16564 /*ctor_dtor_or_conv_p=*/NULL
,
16565 /*parenthesized_p=*/NULL
,
16566 /*member_p=*/false,
16567 /*friend_p=*/false);
16568 if (declares_class_or_enum
& 2)
16569 cp_parser_check_for_definition_in_return_type (declarator
,
16570 decl_specifiers
.type
,
16571 decl_specifiers
.locations
[ds_type_spec
]);
16572 if (declarator
!= cp_error_declarator
)
16574 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16575 permerror (decl_specifiers
.locations
[ds_inline
],
16576 "explicit instantiation shall not use"
16577 " %<inline%> specifier");
16578 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16579 permerror (decl_specifiers
.locations
[ds_constexpr
],
16580 "explicit instantiation shall not use"
16581 " %<constexpr%> specifier");
16583 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16584 NORMAL
, 0, &decl_specifiers
.attributes
);
16585 /* Turn access control back on for names used during
16586 template instantiation. */
16587 pop_deferring_access_checks ();
16588 /* Do the explicit instantiation. */
16589 do_decl_instantiation (decl
, extension_specifier
);
16593 pop_deferring_access_checks ();
16594 /* Skip the body of the explicit instantiation. */
16595 cp_parser_skip_to_end_of_statement (parser
);
16598 /* We're done with the instantiation. */
16599 end_explicit_instantiation ();
16601 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16603 timevar_pop (TV_TEMPLATE_INST
);
16606 /* Parse an explicit-specialization.
16608 explicit-specialization:
16609 template < > declaration
16611 Although the standard says `declaration', what it really means is:
16613 explicit-specialization:
16614 template <> decl-specifier [opt] init-declarator [opt] ;
16615 template <> function-definition
16616 template <> explicit-specialization
16617 template <> template-declaration */
16620 cp_parser_explicit_specialization (cp_parser
* parser
)
16622 bool need_lang_pop
;
16623 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16625 /* Look for the `template' keyword. */
16626 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16627 /* Look for the `<'. */
16628 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16629 /* Look for the `>'. */
16630 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16631 /* We have processed another parameter list. */
16632 ++parser
->num_template_parameter_lists
;
16635 A template ... explicit specialization ... shall not have C
16637 if (current_lang_name
== lang_name_c
)
16639 error_at (token
->location
, "template specialization with C linkage");
16640 maybe_show_extern_c_location ();
16641 /* Give it C++ linkage to avoid confusing other parts of the
16643 push_lang_context (lang_name_cplusplus
);
16644 need_lang_pop
= true;
16647 need_lang_pop
= false;
16648 /* Let the front end know that we are beginning a specialization. */
16649 if (!begin_specialization ())
16651 end_specialization ();
16655 /* If the next keyword is `template', we need to figure out whether
16656 or not we're looking a template-declaration. */
16657 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16659 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16660 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16661 cp_parser_template_declaration_after_export (parser
,
16662 /*member_p=*/false);
16664 cp_parser_explicit_specialization (parser
);
16667 /* Parse the dependent declaration. */
16668 cp_parser_single_declaration (parser
,
16670 /*member_p=*/false,
16671 /*explicit_specialization_p=*/true,
16672 /*friend_p=*/NULL
);
16673 /* We're done with the specialization. */
16674 end_specialization ();
16675 /* For the erroneous case of a template with C linkage, we pushed an
16676 implicit C++ linkage scope; exit that scope now. */
16678 pop_lang_context ();
16679 /* We're done with this parameter list. */
16680 --parser
->num_template_parameter_lists
;
16683 /* Parse a type-specifier.
16686 simple-type-specifier
16689 elaborated-type-specifier
16697 Returns a representation of the type-specifier. For a
16698 class-specifier, enum-specifier, or elaborated-type-specifier, a
16699 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16701 The parser flags FLAGS is used to control type-specifier parsing.
16703 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16704 in a decl-specifier-seq.
16706 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16707 class-specifier, enum-specifier, or elaborated-type-specifier, then
16708 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16709 if a type is declared; 2 if it is defined. Otherwise, it is set to
16712 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16713 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16714 is set to FALSE. */
16717 cp_parser_type_specifier (cp_parser
* parser
,
16718 cp_parser_flags flags
,
16719 cp_decl_specifier_seq
*decl_specs
,
16720 bool is_declaration
,
16721 int* declares_class_or_enum
,
16722 bool* is_cv_qualifier
)
16724 tree type_spec
= NULL_TREE
;
16727 cp_decl_spec ds
= ds_last
;
16729 /* Assume this type-specifier does not declare a new type. */
16730 if (declares_class_or_enum
)
16731 *declares_class_or_enum
= 0;
16732 /* And that it does not specify a cv-qualifier. */
16733 if (is_cv_qualifier
)
16734 *is_cv_qualifier
= false;
16735 /* Peek at the next token. */
16736 token
= cp_lexer_peek_token (parser
->lexer
);
16738 /* If we're looking at a keyword, we can use that to guide the
16739 production we choose. */
16740 keyword
= token
->keyword
;
16744 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16745 goto elaborated_type_specifier
;
16747 /* Look for the enum-specifier. */
16748 type_spec
= cp_parser_enum_specifier (parser
);
16749 /* If that worked, we're done. */
16752 if (declares_class_or_enum
)
16753 *declares_class_or_enum
= 2;
16755 cp_parser_set_decl_spec_type (decl_specs
,
16758 /*type_definition_p=*/true);
16762 goto elaborated_type_specifier
;
16764 /* Any of these indicate either a class-specifier, or an
16765 elaborated-type-specifier. */
16769 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16770 goto elaborated_type_specifier
;
16772 /* Parse tentatively so that we can back up if we don't find a
16773 class-specifier. */
16774 cp_parser_parse_tentatively (parser
);
16775 /* Look for the class-specifier. */
16776 type_spec
= cp_parser_class_specifier (parser
);
16777 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16778 /* If that worked, we're done. */
16779 if (cp_parser_parse_definitely (parser
))
16781 if (declares_class_or_enum
)
16782 *declares_class_or_enum
= 2;
16784 cp_parser_set_decl_spec_type (decl_specs
,
16787 /*type_definition_p=*/true);
16791 /* Fall through. */
16792 elaborated_type_specifier
:
16793 /* We're declaring (not defining) a class or enum. */
16794 if (declares_class_or_enum
)
16795 *declares_class_or_enum
= 1;
16797 /* Fall through. */
16799 /* Look for an elaborated-type-specifier. */
16801 = (cp_parser_elaborated_type_specifier
16803 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16806 cp_parser_set_decl_spec_type (decl_specs
,
16809 /*type_definition_p=*/false);
16814 if (is_cv_qualifier
)
16815 *is_cv_qualifier
= true;
16820 if (is_cv_qualifier
)
16821 *is_cv_qualifier
= true;
16826 if (is_cv_qualifier
)
16827 *is_cv_qualifier
= true;
16831 /* The `__complex__' keyword is a GNU extension. */
16839 /* Handle simple keywords. */
16844 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16845 decl_specs
->any_specifiers_p
= true;
16847 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16850 /* If we do not already have a type-specifier, assume we are looking
16851 at a simple-type-specifier. */
16852 type_spec
= cp_parser_simple_type_specifier (parser
,
16856 /* If we didn't find a type-specifier, and a type-specifier was not
16857 optional in this context, issue an error message. */
16858 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16860 cp_parser_error (parser
, "expected type specifier");
16861 return error_mark_node
;
16867 /* Parse a simple-type-specifier.
16869 simple-type-specifier:
16870 :: [opt] nested-name-specifier [opt] type-name
16871 :: [opt] nested-name-specifier template template-id
16886 simple-type-specifier:
16888 decltype ( expression )
16891 __underlying_type ( type-id )
16895 nested-name-specifier(opt) template-name
16899 simple-type-specifier:
16901 __typeof__ unary-expression
16902 __typeof__ ( type-id )
16903 __typeof__ ( type-id ) { initializer-list , [opt] }
16905 Concepts Extension:
16907 simple-type-specifier:
16908 constrained-type-specifier
16910 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16911 appropriately updated. */
16914 cp_parser_simple_type_specifier (cp_parser
* parser
,
16915 cp_decl_specifier_seq
*decl_specs
,
16916 cp_parser_flags flags
)
16918 tree type
= NULL_TREE
;
16922 /* Peek at the next token. */
16923 token
= cp_lexer_peek_token (parser
->lexer
);
16925 /* If we're looking at a keyword, things are easy. */
16926 switch (token
->keyword
)
16930 decl_specs
->explicit_char_p
= true;
16931 type
= char_type_node
;
16934 type
= char16_type_node
;
16937 type
= char32_type_node
;
16940 type
= wchar_type_node
;
16943 type
= boolean_type_node
;
16946 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16947 type
= short_integer_type_node
;
16951 decl_specs
->explicit_int_p
= true;
16952 type
= integer_type_node
;
16958 idx
= token
->keyword
- RID_INT_N_0
;
16959 if (! int_n_enabled_p
[idx
])
16963 decl_specs
->explicit_intN_p
= true;
16964 decl_specs
->int_n_idx
= idx
;
16966 type
= int_n_trees
[idx
].signed_type
;
16970 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16971 type
= long_integer_type_node
;
16974 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16975 type
= integer_type_node
;
16978 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16979 type
= unsigned_type_node
;
16982 type
= float_type_node
;
16985 type
= double_type_node
;
16988 type
= void_type_node
;
16992 maybe_warn_cpp0x (CPP0X_AUTO
);
16993 if (parser
->auto_is_implicit_function_template_parm_p
)
16995 /* The 'auto' might be the placeholder return type for a function decl
16996 with trailing return type. */
16997 bool have_trailing_return_fn_decl
= false;
16999 cp_parser_parse_tentatively (parser
);
17000 cp_lexer_consume_token (parser
->lexer
);
17001 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17002 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17003 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17004 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17008 cp_lexer_consume_token (parser
->lexer
);
17009 cp_parser_skip_to_closing_parenthesis (parser
,
17010 /*recovering*/false,
17012 /*consume_paren*/true);
17016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17018 have_trailing_return_fn_decl
= true;
17022 cp_lexer_consume_token (parser
->lexer
);
17024 cp_parser_abort_tentative_parse (parser
);
17026 if (have_trailing_return_fn_decl
)
17028 type
= make_auto ();
17032 if (cxx_dialect
>= cxx14
)
17034 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17035 type
= TREE_TYPE (type
);
17038 type
= error_mark_node
;
17040 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17042 if (cxx_dialect
< cxx14
)
17043 error_at (token
->location
,
17044 "use of %<auto%> in lambda parameter declaration "
17045 "only available with "
17046 "-std=c++14 or -std=gnu++14");
17048 else if (cxx_dialect
< cxx14
)
17049 error_at (token
->location
,
17050 "use of %<auto%> in parameter declaration "
17051 "only available with "
17052 "-std=c++14 or -std=gnu++14");
17053 else if (!flag_concepts
)
17054 pedwarn (token
->location
, 0,
17055 "use of %<auto%> in parameter declaration "
17056 "only available with -fconcepts");
17059 type
= make_auto ();
17063 /* Since DR 743, decltype can either be a simple-type-specifier by
17064 itself or begin a nested-name-specifier. Parsing it will replace
17065 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17066 handling below decide what to do. */
17067 cp_parser_decltype (parser
);
17068 cp_lexer_set_token_position (parser
->lexer
, token
);
17072 /* Consume the `typeof' token. */
17073 cp_lexer_consume_token (parser
->lexer
);
17074 /* Parse the operand to `typeof'. */
17075 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17076 /* If it is not already a TYPE, take its type. */
17077 if (!TYPE_P (type
))
17078 type
= finish_typeof (type
);
17081 cp_parser_set_decl_spec_type (decl_specs
, type
,
17083 /*type_definition_p=*/false);
17087 case RID_UNDERLYING_TYPE
:
17088 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17090 cp_parser_set_decl_spec_type (decl_specs
, type
,
17092 /*type_definition_p=*/false);
17097 case RID_DIRECT_BASES
:
17098 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17100 cp_parser_set_decl_spec_type (decl_specs
, type
,
17102 /*type_definition_p=*/false);
17108 /* If token is an already-parsed decltype not followed by ::,
17109 it's a simple-type-specifier. */
17110 if (token
->type
== CPP_DECLTYPE
17111 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17113 type
= saved_checks_value (token
->u
.tree_check_value
);
17116 cp_parser_set_decl_spec_type (decl_specs
, type
,
17118 /*type_definition_p=*/false);
17119 /* Remember that we are handling a decltype in order to
17120 implement the resolution of DR 1510 when the argument
17121 isn't instantiation dependent. */
17122 decl_specs
->decltype_p
= true;
17124 cp_lexer_consume_token (parser
->lexer
);
17128 /* If the type-specifier was for a built-in type, we're done. */
17131 /* Record the type. */
17133 && (token
->keyword
!= RID_SIGNED
17134 && token
->keyword
!= RID_UNSIGNED
17135 && token
->keyword
!= RID_SHORT
17136 && token
->keyword
!= RID_LONG
))
17137 cp_parser_set_decl_spec_type (decl_specs
,
17140 /*type_definition_p=*/false);
17142 decl_specs
->any_specifiers_p
= true;
17144 /* Consume the token. */
17145 cp_lexer_consume_token (parser
->lexer
);
17147 if (type
== error_mark_node
)
17148 return error_mark_node
;
17150 /* There is no valid C++ program where a non-template type is
17151 followed by a "<". That usually indicates that the user thought
17152 that the type was a template. */
17153 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17156 return TYPE_NAME (type
);
17159 /* The type-specifier must be a user-defined type. */
17160 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17165 /* Don't gobble tokens or issue error messages if this is an
17166 optional type-specifier. */
17167 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17168 cp_parser_parse_tentatively (parser
);
17170 token
= cp_lexer_peek_token (parser
->lexer
);
17172 /* Look for the optional `::' operator. */
17174 = (cp_parser_global_scope_opt (parser
,
17175 /*current_scope_valid_p=*/false)
17177 /* Look for the nested-name specifier. */
17179 = (cp_parser_nested_name_specifier_opt (parser
,
17180 /*typename_keyword_p=*/false,
17181 /*check_dependency_p=*/true,
17183 /*is_declaration=*/false)
17185 /* If we have seen a nested-name-specifier, and the next token
17186 is `template', then we are using the template-id production. */
17188 && cp_parser_optional_template_keyword (parser
))
17190 /* Look for the template-id. */
17191 type
= cp_parser_template_id (parser
,
17192 /*template_keyword_p=*/true,
17193 /*check_dependency_p=*/true,
17195 /*is_declaration=*/false);
17196 /* If the template-id did not name a type, we are out of
17198 if (TREE_CODE (type
) != TYPE_DECL
)
17200 cp_parser_error (parser
, "expected template-id for type");
17204 /* Otherwise, look for a type-name. */
17206 type
= cp_parser_type_name (parser
);
17207 /* Keep track of all name-lookups performed in class scopes. */
17211 && TREE_CODE (type
) == TYPE_DECL
17212 && identifier_p (DECL_NAME (type
)))
17213 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17214 /* If it didn't work out, we don't have a TYPE. */
17215 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17216 && !cp_parser_parse_definitely (parser
))
17218 if (!type
&& cxx_dialect
>= cxx17
)
17220 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17221 cp_parser_parse_tentatively (parser
);
17223 cp_parser_global_scope_opt (parser
,
17224 /*current_scope_valid_p=*/false);
17225 cp_parser_nested_name_specifier_opt (parser
,
17226 /*typename_keyword_p=*/false,
17227 /*check_dependency_p=*/true,
17229 /*is_declaration=*/false);
17230 tree name
= cp_parser_identifier (parser
);
17231 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17232 && parser
->scope
!= error_mark_node
)
17234 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17236 /*is_template=*/false,
17237 /*is_namespace=*/false,
17238 /*check_dependency=*/true,
17239 /*ambiguous_decls=*/NULL
,
17241 if (tmpl
&& tmpl
!= error_mark_node
17242 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17243 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17244 type
= make_template_placeholder (tmpl
);
17247 type
= error_mark_node
;
17248 if (!cp_parser_simulate_error (parser
))
17249 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17250 NLE_TYPE
, token
->location
);
17254 type
= error_mark_node
;
17256 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17257 && !cp_parser_parse_definitely (parser
))
17260 if (type
&& decl_specs
)
17261 cp_parser_set_decl_spec_type (decl_specs
, type
,
17263 /*type_definition_p=*/false);
17266 /* If we didn't get a type-name, issue an error message. */
17267 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17269 cp_parser_error (parser
, "expected type-name");
17270 return error_mark_node
;
17273 if (type
&& type
!= error_mark_node
)
17275 /* See if TYPE is an Objective-C type, and if so, parse and
17276 accept any protocol references following it. Do this before
17277 the cp_parser_check_for_invalid_template_id() call, because
17278 Objective-C types can be followed by '<...>' which would
17279 enclose protocol names rather than template arguments, and so
17280 everything is fine. */
17281 if (c_dialect_objc () && !parser
->scope
17282 && (objc_is_id (type
) || objc_is_class_name (type
)))
17284 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17285 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17287 /* Clobber the "unqualified" type previously entered into
17288 DECL_SPECS with the new, improved protocol-qualified version. */
17290 decl_specs
->type
= qual_type
;
17295 /* There is no valid C++ program where a non-template type is
17296 followed by a "<". That usually indicates that the user
17297 thought that the type was a template. */
17298 cp_parser_check_for_invalid_template_id (parser
, type
,
17306 /* Parse a type-name.
17312 simple-template-id [in c++0x]
17329 Returns a TYPE_DECL for the type. */
17332 cp_parser_type_name (cp_parser
* parser
)
17334 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17339 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17343 /* We can't know yet whether it is a class-name or not. */
17344 cp_parser_parse_tentatively (parser
);
17345 /* Try a class-name. */
17346 type_decl
= cp_parser_class_name (parser
,
17347 typename_keyword_p
,
17348 /*template_keyword_p=*/false,
17350 /*check_dependency_p=*/true,
17351 /*class_head_p=*/false,
17352 /*is_declaration=*/false);
17353 /* If it's not a class-name, keep looking. */
17354 if (!cp_parser_parse_definitely (parser
))
17356 if (cxx_dialect
< cxx11
)
17357 /* It must be a typedef-name or an enum-name. */
17358 return cp_parser_nonclass_name (parser
);
17360 cp_parser_parse_tentatively (parser
);
17361 /* It is either a simple-template-id representing an
17362 instantiation of an alias template... */
17363 type_decl
= cp_parser_template_id (parser
,
17364 /*template_keyword_p=*/false,
17365 /*check_dependency_p=*/true,
17367 /*is_declaration=*/false);
17368 /* Note that this must be an instantiation of an alias template
17369 because [temp.names]/6 says:
17371 A template-id that names an alias template specialization
17374 Whereas [temp.names]/7 says:
17376 A simple-template-id that names a class template
17377 specialization is a class-name.
17379 With concepts, this could also be a partial-concept-id that
17380 declares a non-type template parameter. */
17381 if (type_decl
!= NULL_TREE
17382 && TREE_CODE (type_decl
) == TYPE_DECL
17383 && TYPE_DECL_ALIAS_P (type_decl
))
17384 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17385 else if (is_constrained_parameter (type_decl
))
17386 /* Don't do anything. */ ;
17388 cp_parser_simulate_error (parser
);
17390 if (!cp_parser_parse_definitely (parser
))
17391 /* ... Or a typedef-name or an enum-name. */
17392 return cp_parser_nonclass_name (parser
);
17398 /* Check if DECL and ARGS can form a constrained-type-specifier.
17399 If ARGS is non-null, we try to form a concept check of the
17400 form DECL<?, ARGS> where ? is a wildcard that matches any
17401 kind of template argument. If ARGS is NULL, then we try to
17402 form a concept check of the form DECL<?>. */
17405 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17406 tree decl
, tree args
)
17408 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17410 /* If we a constrained-type-specifier cannot be deduced. */
17411 if (parser
->prevent_constrained_type_specifiers
)
17414 /* A constrained type specifier can only be found in an
17415 overload set or as a reference to a template declaration.
17417 FIXME: This might be masking a bug. It's possible that
17418 that the deduction below is causing template specializations
17419 to be formed with the wildcard as an argument. */
17420 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17423 /* Try to build a call expression that evaluates the
17424 concept. This can fail if the overload set refers
17425 only to non-templates. */
17426 tree placeholder
= build_nt (WILDCARD_DECL
);
17427 tree check
= build_concept_check (decl
, placeholder
, args
);
17428 if (check
== error_mark_node
)
17431 /* Deduce the checked constraint and the prototype parameter.
17433 FIXME: In certain cases, failure to deduce should be a
17434 diagnosable error. */
17437 if (!deduce_constrained_parameter (check
, conc
, proto
))
17440 /* In template parameter scope, this results in a constrained
17441 parameter. Return a descriptor of that parm. */
17442 if (processing_template_parmlist
)
17443 return build_constrained_parameter (conc
, proto
, args
);
17445 /* In a parameter-declaration-clause, constrained-type
17446 specifiers result in invented template parameters. */
17447 if (parser
->auto_is_implicit_function_template_parm_p
)
17449 tree x
= build_constrained_parameter (conc
, proto
, args
);
17450 return synthesize_implicit_template_parm (parser
, x
);
17454 /* Otherwise, we're in a context where the constrained
17455 type name is deduced and the constraint applies
17456 after deduction. */
17457 return make_constrained_auto (conc
, args
);
17463 /* If DECL refers to a concept, return a TYPE_DECL representing
17464 the result of using the constrained type specifier in the
17465 current context. DECL refers to a concept if
17467 - it is an overload set containing a function concept taking a single
17470 - it is a variable concept taking a single type argument. */
17473 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17476 && (TREE_CODE (decl
) == OVERLOAD
17477 || BASELINK_P (decl
)
17478 || variable_concept_p (decl
)))
17479 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17484 /* Check if DECL and ARGS form a partial-concept-id. If so,
17485 assign ID to the resulting constrained placeholder.
17487 Returns true if the partial-concept-id designates a placeholder
17488 and false otherwise. Note that *id is set to NULL_TREE in
17492 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17494 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17497 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17509 Returns a TYPE_DECL for the type. */
17512 cp_parser_nonclass_name (cp_parser
* parser
)
17517 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17518 identifier
= cp_parser_identifier (parser
);
17519 if (identifier
== error_mark_node
)
17520 return error_mark_node
;
17522 /* Look up the type-name. */
17523 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17525 type_decl
= strip_using_decl (type_decl
);
17527 /* If we found an overload set, then it may refer to a concept-name. */
17528 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17531 if (TREE_CODE (type_decl
) != TYPE_DECL
17532 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17534 /* See if this is an Objective-C type. */
17535 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17536 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17538 type_decl
= TYPE_NAME (type
);
17541 /* Issue an error if we did not find a type-name. */
17542 if (TREE_CODE (type_decl
) != TYPE_DECL
17543 /* In Objective-C, we have the complication that class names are
17544 normally type names and start declarations (eg, the
17545 "NSObject" in "NSObject *object;"), but can be used in an
17546 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17547 is an expression. So, a classname followed by a dot is not a
17548 valid type-name. */
17549 || (objc_is_class_name (TREE_TYPE (type_decl
))
17550 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17552 if (!cp_parser_simulate_error (parser
))
17553 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17554 NLE_TYPE
, token
->location
);
17555 return error_mark_node
;
17557 /* Remember that the name was used in the definition of the
17558 current class so that we can check later to see if the
17559 meaning would have been different after the class was
17560 entirely defined. */
17561 else if (type_decl
!= error_mark_node
17563 maybe_note_name_used_in_class (identifier
, type_decl
);
17568 /* Parse an elaborated-type-specifier. Note that the grammar given
17569 here incorporates the resolution to DR68.
17571 elaborated-type-specifier:
17572 class-key :: [opt] nested-name-specifier [opt] identifier
17573 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17574 enum-key :: [opt] nested-name-specifier [opt] identifier
17575 typename :: [opt] nested-name-specifier identifier
17576 typename :: [opt] nested-name-specifier template [opt]
17581 elaborated-type-specifier:
17582 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17583 class-key attributes :: [opt] nested-name-specifier [opt]
17584 template [opt] template-id
17585 enum attributes :: [opt] nested-name-specifier [opt] identifier
17587 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17588 declared `friend'. If IS_DECLARATION is TRUE, then this
17589 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17590 something is being declared.
17592 Returns the TYPE specified. */
17595 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17597 bool is_declaration
)
17599 enum tag_types tag_type
;
17601 tree type
= NULL_TREE
;
17602 tree attributes
= NULL_TREE
;
17604 cp_token
*token
= NULL
;
17606 /* See if we're looking at the `enum' keyword. */
17607 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17609 /* Consume the `enum' token. */
17610 cp_lexer_consume_token (parser
->lexer
);
17611 /* Remember that it's an enumeration type. */
17612 tag_type
= enum_type
;
17613 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17614 enums) is used here. */
17615 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17616 if (cp_parser_is_keyword (token
, RID_CLASS
)
17617 || cp_parser_is_keyword (token
, RID_STRUCT
))
17619 gcc_rich_location
richloc (token
->location
);
17620 richloc
.add_range (input_location
, false);
17621 richloc
.add_fixit_remove ();
17622 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17623 "a scoped enum must not use the %qD keyword",
17625 /* Consume the `struct' or `class' and parse it anyway. */
17626 cp_lexer_consume_token (parser
->lexer
);
17628 /* Parse the attributes. */
17629 attributes
= cp_parser_attributes_opt (parser
);
17631 /* Or, it might be `typename'. */
17632 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17635 /* Consume the `typename' token. */
17636 cp_lexer_consume_token (parser
->lexer
);
17637 /* Remember that it's a `typename' type. */
17638 tag_type
= typename_type
;
17640 /* Otherwise it must be a class-key. */
17643 tag_type
= cp_parser_class_key (parser
);
17644 if (tag_type
== none_type
)
17645 return error_mark_node
;
17646 /* Parse the attributes. */
17647 attributes
= cp_parser_attributes_opt (parser
);
17650 /* Look for the `::' operator. */
17651 globalscope
= cp_parser_global_scope_opt (parser
,
17652 /*current_scope_valid_p=*/false);
17653 /* Look for the nested-name-specifier. */
17654 tree nested_name_specifier
;
17655 if (tag_type
== typename_type
&& !globalscope
)
17657 nested_name_specifier
17658 = cp_parser_nested_name_specifier (parser
,
17659 /*typename_keyword_p=*/true,
17660 /*check_dependency_p=*/true,
17663 if (!nested_name_specifier
)
17664 return error_mark_node
;
17667 /* Even though `typename' is not present, the proposed resolution
17668 to Core Issue 180 says that in `class A<T>::B', `B' should be
17669 considered a type-name, even if `A<T>' is dependent. */
17670 nested_name_specifier
17671 = cp_parser_nested_name_specifier_opt (parser
,
17672 /*typename_keyword_p=*/true,
17673 /*check_dependency_p=*/true,
17676 /* For everything but enumeration types, consider a template-id.
17677 For an enumeration type, consider only a plain identifier. */
17678 if (tag_type
!= enum_type
)
17680 bool template_p
= false;
17683 /* Allow the `template' keyword. */
17684 template_p
= cp_parser_optional_template_keyword (parser
);
17685 /* If we didn't see `template', we don't know if there's a
17686 template-id or not. */
17688 cp_parser_parse_tentatively (parser
);
17689 /* Parse the template-id. */
17690 token
= cp_lexer_peek_token (parser
->lexer
);
17691 decl
= cp_parser_template_id (parser
, template_p
,
17692 /*check_dependency_p=*/true,
17695 /* If we didn't find a template-id, look for an ordinary
17697 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17699 /* We can get here when cp_parser_template_id, called by
17700 cp_parser_class_name with tag_type == none_type, succeeds
17701 and caches a BASELINK. Then, when called again here,
17702 instead of failing and returning an error_mark_node
17703 returns it (see template/typename17.C in C++11).
17704 ??? Could we diagnose this earlier? */
17705 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17707 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17708 type
= error_mark_node
;
17710 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17711 in effect, then we must assume that, upon instantiation, the
17712 template will correspond to a class. */
17713 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17714 && tag_type
== typename_type
)
17715 type
= make_typename_type (parser
->scope
, decl
,
17717 /*complain=*/tf_error
);
17718 /* If the `typename' keyword is in effect and DECL is not a type
17719 decl, then type is non existent. */
17720 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17722 else if (TREE_CODE (decl
) == TYPE_DECL
)
17724 type
= check_elaborated_type_specifier (tag_type
, decl
,
17725 /*allow_template_p=*/true);
17727 /* If the next token is a semicolon, this must be a specialization,
17728 instantiation, or friend declaration. Check the scope while we
17729 still know whether or not we had a nested-name-specifier. */
17730 if (type
!= error_mark_node
17731 && !nested_name_specifier
&& !is_friend
17732 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17733 check_unqualified_spec_or_inst (type
, token
->location
);
17735 else if (decl
== error_mark_node
)
17736 type
= error_mark_node
;
17741 token
= cp_lexer_peek_token (parser
->lexer
);
17742 identifier
= cp_parser_identifier (parser
);
17744 if (identifier
== error_mark_node
)
17746 parser
->scope
= NULL_TREE
;
17747 return error_mark_node
;
17750 /* For a `typename', we needn't call xref_tag. */
17751 if (tag_type
== typename_type
17752 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17753 return cp_parser_make_typename_type (parser
, identifier
,
17756 /* Template parameter lists apply only if we are not within a
17757 function parameter list. */
17758 bool template_parm_lists_apply
17759 = parser
->num_template_parameter_lists
;
17760 if (template_parm_lists_apply
)
17761 for (cp_binding_level
*s
= current_binding_level
;
17762 s
&& s
->kind
!= sk_template_parms
;
17763 s
= s
->level_chain
)
17764 if (s
->kind
== sk_function_parms
)
17765 template_parm_lists_apply
= false;
17767 /* Look up a qualified name in the usual way. */
17771 tree ambiguous_decls
;
17773 decl
= cp_parser_lookup_name (parser
, identifier
,
17775 /*is_template=*/false,
17776 /*is_namespace=*/false,
17777 /*check_dependency=*/true,
17781 /* If the lookup was ambiguous, an error will already have been
17783 if (ambiguous_decls
)
17784 return error_mark_node
;
17786 /* If we are parsing friend declaration, DECL may be a
17787 TEMPLATE_DECL tree node here. However, we need to check
17788 whether this TEMPLATE_DECL results in valid code. Consider
17789 the following example:
17792 template <class T> class C {};
17795 template <class T> friend class N::C; // #1, valid code
17797 template <class T> class Y {
17798 friend class N::C; // #2, invalid code
17801 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17802 name lookup of `N::C'. We see that friend declaration must
17803 be template for the code to be valid. Note that
17804 processing_template_decl does not work here since it is
17805 always 1 for the above two cases. */
17807 decl
= (cp_parser_maybe_treat_template_as_class
17808 (decl
, /*tag_name_p=*/is_friend
17809 && template_parm_lists_apply
));
17811 if (TREE_CODE (decl
) != TYPE_DECL
)
17813 cp_parser_diagnose_invalid_type_name (parser
,
17816 return error_mark_node
;
17819 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17821 bool allow_template
= (template_parm_lists_apply
17822 || DECL_SELF_REFERENCE_P (decl
));
17823 type
= check_elaborated_type_specifier (tag_type
, decl
,
17826 if (type
== error_mark_node
)
17827 return error_mark_node
;
17830 /* Forward declarations of nested types, such as
17835 are invalid unless all components preceding the final '::'
17836 are complete. If all enclosing types are complete, these
17837 declarations become merely pointless.
17839 Invalid forward declarations of nested types are errors
17840 caught elsewhere in parsing. Those that are pointless arrive
17843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17844 && !is_friend
&& !processing_explicit_instantiation
)
17845 warning (0, "declaration %qD does not declare anything", decl
);
17847 type
= TREE_TYPE (decl
);
17851 /* An elaborated-type-specifier sometimes introduces a new type and
17852 sometimes names an existing type. Normally, the rule is that it
17853 introduces a new type only if there is not an existing type of
17854 the same name already in scope. For example, given:
17857 void f() { struct S s; }
17859 the `struct S' in the body of `f' is the same `struct S' as in
17860 the global scope; the existing definition is used. However, if
17861 there were no global declaration, this would introduce a new
17862 local class named `S'.
17864 An exception to this rule applies to the following code:
17866 namespace N { struct S; }
17868 Here, the elaborated-type-specifier names a new type
17869 unconditionally; even if there is already an `S' in the
17870 containing scope this declaration names a new type.
17871 This exception only applies if the elaborated-type-specifier
17872 forms the complete declaration:
17876 A declaration consisting solely of `class-key identifier ;' is
17877 either a redeclaration of the name in the current scope or a
17878 forward declaration of the identifier as a class name. It
17879 introduces the name into the current scope.
17881 We are in this situation precisely when the next token is a `;'.
17883 An exception to the exception is that a `friend' declaration does
17884 *not* name a new type; i.e., given:
17886 struct S { friend struct T; };
17888 `T' is not a new type in the scope of `S'.
17890 Also, `new struct S' or `sizeof (struct S)' never results in the
17891 definition of a new type; a new type can only be declared in a
17892 declaration context. */
17898 /* Friends have special name lookup rules. */
17899 ts
= ts_within_enclosing_non_class
;
17900 else if (is_declaration
17901 && cp_lexer_next_token_is (parser
->lexer
,
17903 /* This is a `class-key identifier ;' */
17909 (template_parm_lists_apply
17910 && (cp_parser_next_token_starts_class_definition_p (parser
)
17911 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17912 /* An unqualified name was used to reference this type, so
17913 there were no qualifying templates. */
17914 if (template_parm_lists_apply
17915 && !cp_parser_check_template_parameters (parser
,
17916 /*num_templates=*/0,
17917 /*template_id*/false,
17919 /*declarator=*/NULL
))
17920 return error_mark_node
;
17921 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17925 if (type
== error_mark_node
)
17926 return error_mark_node
;
17928 /* Allow attributes on forward declarations of classes. */
17931 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17932 warning (OPT_Wattributes
,
17933 "attributes ignored on uninstantiated type");
17934 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17935 && ! processing_explicit_instantiation
)
17936 warning (OPT_Wattributes
,
17937 "attributes ignored on template instantiation");
17938 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17939 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17941 warning (OPT_Wattributes
,
17942 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17945 if (tag_type
!= enum_type
)
17947 /* Indicate whether this class was declared as a `class' or as a
17949 if (CLASS_TYPE_P (type
))
17950 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17951 cp_parser_check_class_key (tag_type
, type
);
17954 /* A "<" cannot follow an elaborated type specifier. If that
17955 happens, the user was probably trying to form a template-id. */
17956 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17962 /* Parse an enum-specifier.
17965 enum-head { enumerator-list [opt] }
17966 enum-head { enumerator-list , } [C++0x]
17969 enum-key identifier [opt] enum-base [opt]
17970 enum-key nested-name-specifier identifier enum-base [opt]
17975 enum struct [C++0x]
17978 : type-specifier-seq
17980 opaque-enum-specifier:
17981 enum-key identifier enum-base [opt] ;
17984 enum-key attributes[opt] identifier [opt] enum-base [opt]
17985 { enumerator-list [opt] }attributes[opt]
17986 enum-key attributes[opt] identifier [opt] enum-base [opt]
17987 { enumerator-list, }attributes[opt] [C++0x]
17989 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17990 if the token stream isn't an enum-specifier after all. */
17993 cp_parser_enum_specifier (cp_parser
* parser
)
17996 tree type
= NULL_TREE
;
17998 tree nested_name_specifier
= NULL_TREE
;
18000 bool scoped_enum_p
= false;
18001 bool has_underlying_type
= false;
18002 bool nested_being_defined
= false;
18003 bool new_value_list
= false;
18004 bool is_new_type
= false;
18005 bool is_unnamed
= false;
18006 tree underlying_type
= NULL_TREE
;
18007 cp_token
*type_start_token
= NULL
;
18008 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18010 parser
->colon_corrects_to_scope_p
= false;
18012 /* Parse tentatively so that we can back up if we don't find a
18014 cp_parser_parse_tentatively (parser
);
18016 /* Caller guarantees that the current token is 'enum', an identifier
18017 possibly follows, and the token after that is an opening brace.
18018 If we don't have an identifier, fabricate an anonymous name for
18019 the enumeration being defined. */
18020 cp_lexer_consume_token (parser
->lexer
);
18022 /* Parse the "class" or "struct", which indicates a scoped
18023 enumeration type in C++0x. */
18024 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18025 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18027 if (cxx_dialect
< cxx11
)
18028 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18030 /* Consume the `struct' or `class' token. */
18031 cp_lexer_consume_token (parser
->lexer
);
18033 scoped_enum_p
= true;
18036 attributes
= cp_parser_attributes_opt (parser
);
18038 /* Clear the qualification. */
18039 parser
->scope
= NULL_TREE
;
18040 parser
->qualifying_scope
= NULL_TREE
;
18041 parser
->object_scope
= NULL_TREE
;
18043 /* Figure out in what scope the declaration is being placed. */
18044 prev_scope
= current_scope ();
18046 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18048 push_deferring_access_checks (dk_no_check
);
18049 nested_name_specifier
18050 = cp_parser_nested_name_specifier_opt (parser
,
18051 /*typename_keyword_p=*/true,
18052 /*check_dependency_p=*/false,
18054 /*is_declaration=*/false);
18056 if (nested_name_specifier
)
18060 identifier
= cp_parser_identifier (parser
);
18061 name
= cp_parser_lookup_name (parser
, identifier
,
18063 /*is_template=*/false,
18064 /*is_namespace=*/false,
18065 /*check_dependency=*/true,
18066 /*ambiguous_decls=*/NULL
,
18068 if (name
&& name
!= error_mark_node
)
18070 type
= TREE_TYPE (name
);
18071 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18073 /* Are template enums allowed in ISO? */
18074 if (template_parm_scope_p ())
18075 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18076 "%qD is an enumeration template", name
);
18077 /* ignore a typename reference, for it will be solved by name
18082 else if (nested_name_specifier
== error_mark_node
)
18083 /* We already issued an error. */;
18086 error_at (type_start_token
->location
,
18087 "%qD does not name an enumeration in %qT",
18088 identifier
, nested_name_specifier
);
18089 nested_name_specifier
= error_mark_node
;
18094 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18095 identifier
= cp_parser_identifier (parser
);
18098 identifier
= make_anon_name ();
18101 error_at (type_start_token
->location
,
18102 "unnamed scoped enum is not allowed");
18105 pop_deferring_access_checks ();
18107 /* Check for the `:' that denotes a specified underlying type in C++0x.
18108 Note that a ':' could also indicate a bitfield width, however. */
18109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18111 cp_decl_specifier_seq type_specifiers
;
18113 /* Consume the `:'. */
18114 cp_lexer_consume_token (parser
->lexer
);
18116 /* Parse the type-specifier-seq. */
18117 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18118 /*is_trailing_return=*/false,
18121 /* At this point this is surely not elaborated type specifier. */
18122 if (!cp_parser_parse_definitely (parser
))
18125 if (cxx_dialect
< cxx11
)
18126 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18128 has_underlying_type
= true;
18130 /* If that didn't work, stop. */
18131 if (type_specifiers
.type
!= error_mark_node
)
18133 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18134 /*initialized=*/0, NULL
);
18135 if (underlying_type
== error_mark_node
18136 || check_for_bare_parameter_packs (underlying_type
))
18137 underlying_type
= NULL_TREE
;
18141 /* Look for the `{' but don't consume it yet. */
18142 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18144 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18146 cp_parser_error (parser
, "expected %<{%>");
18147 if (has_underlying_type
)
18153 /* An opaque-enum-specifier must have a ';' here. */
18154 if ((scoped_enum_p
|| underlying_type
)
18155 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18157 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18158 if (has_underlying_type
)
18166 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18169 if (nested_name_specifier
)
18171 if (CLASS_TYPE_P (nested_name_specifier
))
18173 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18174 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18175 push_scope (nested_name_specifier
);
18177 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18179 push_nested_namespace (nested_name_specifier
);
18183 /* Issue an error message if type-definitions are forbidden here. */
18184 if (!cp_parser_check_type_definition (parser
))
18185 type
= error_mark_node
;
18187 /* Create the new type. We do this before consuming the opening
18188 brace so the enum will be recorded as being on the line of its
18189 tag (or the 'enum' keyword, if there is no tag). */
18190 type
= start_enum (identifier
, type
, underlying_type
,
18191 attributes
, scoped_enum_p
, &is_new_type
);
18193 /* If the next token is not '{' it is an opaque-enum-specifier or an
18194 elaborated-type-specifier. */
18195 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18197 timevar_push (TV_PARSE_ENUM
);
18198 if (nested_name_specifier
18199 && nested_name_specifier
!= error_mark_node
)
18201 /* The following catches invalid code such as:
18202 enum class S<int>::E { A, B, C }; */
18203 if (!processing_specialization
18204 && CLASS_TYPE_P (nested_name_specifier
)
18205 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18206 error_at (type_start_token
->location
, "cannot add an enumerator "
18207 "list to a template instantiation");
18209 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18211 error_at (type_start_token
->location
,
18212 "%<%T::%E%> has not been declared",
18213 TYPE_CONTEXT (nested_name_specifier
),
18214 nested_name_specifier
);
18215 type
= error_mark_node
;
18217 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18218 && !CLASS_TYPE_P (nested_name_specifier
))
18220 error_at (type_start_token
->location
, "nested name specifier "
18221 "%qT for enum declaration does not name a class "
18222 "or namespace", nested_name_specifier
);
18223 type
= error_mark_node
;
18225 /* If that scope does not contain the scope in which the
18226 class was originally declared, the program is invalid. */
18227 else if (prev_scope
&& !is_ancestor (prev_scope
,
18228 nested_name_specifier
))
18230 if (at_namespace_scope_p ())
18231 error_at (type_start_token
->location
,
18232 "declaration of %qD in namespace %qD which does not "
18234 type
, prev_scope
, nested_name_specifier
);
18236 error_at (type_start_token
->location
,
18237 "declaration of %qD in %qD which does not "
18239 type
, prev_scope
, nested_name_specifier
);
18240 type
= error_mark_node
;
18242 /* If that scope is the scope where the declaration is being placed
18243 the program is invalid. */
18244 else if (CLASS_TYPE_P (nested_name_specifier
)
18245 && CLASS_TYPE_P (prev_scope
)
18246 && same_type_p (nested_name_specifier
, prev_scope
))
18248 permerror (type_start_token
->location
,
18249 "extra qualification not allowed");
18250 nested_name_specifier
= NULL_TREE
;
18255 begin_scope (sk_scoped_enum
, type
);
18257 /* Consume the opening brace. */
18258 matching_braces braces
;
18259 braces
.consume_open (parser
);
18261 if (type
== error_mark_node
)
18262 ; /* Nothing to add */
18263 else if (OPAQUE_ENUM_P (type
)
18264 || (cxx_dialect
> cxx98
&& processing_specialization
))
18266 new_value_list
= true;
18267 SET_OPAQUE_ENUM_P (type
, false);
18268 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18272 error_at (type_start_token
->location
,
18273 "multiple definition of %q#T", type
);
18274 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18275 "previous definition here");
18276 type
= error_mark_node
;
18279 if (type
== error_mark_node
)
18280 cp_parser_skip_to_end_of_block_or_statement (parser
);
18281 /* If the next token is not '}', then there are some enumerators. */
18282 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18284 if (is_unnamed
&& !scoped_enum_p
)
18285 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18286 "ISO C++ forbids empty unnamed enum");
18289 cp_parser_enumerator_list (parser
, type
);
18291 /* Consume the final '}'. */
18292 braces
.require_close (parser
);
18296 timevar_pop (TV_PARSE_ENUM
);
18300 /* If a ';' follows, then it is an opaque-enum-specifier
18301 and additional restrictions apply. */
18302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18305 error_at (type_start_token
->location
,
18306 "opaque-enum-specifier without name");
18307 else if (nested_name_specifier
)
18308 error_at (type_start_token
->location
,
18309 "opaque-enum-specifier must use a simple identifier");
18313 /* Look for trailing attributes to apply to this enumeration, and
18314 apply them if appropriate. */
18315 if (cp_parser_allow_gnu_extensions_p (parser
))
18317 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18318 cplus_decl_attributes (&type
,
18320 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18323 /* Finish up the enumeration. */
18324 if (type
!= error_mark_node
)
18326 if (new_value_list
)
18327 finish_enum_value_list (type
);
18329 finish_enum (type
);
18332 if (nested_name_specifier
)
18334 if (CLASS_TYPE_P (nested_name_specifier
))
18336 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18337 pop_scope (nested_name_specifier
);
18339 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18341 pop_nested_namespace (nested_name_specifier
);
18345 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18349 /* Parse an enumerator-list. The enumerators all have the indicated
18353 enumerator-definition
18354 enumerator-list , enumerator-definition */
18357 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18361 /* Parse an enumerator-definition. */
18362 cp_parser_enumerator_definition (parser
, type
);
18364 /* If the next token is not a ',', we've reached the end of
18366 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18368 /* Otherwise, consume the `,' and keep going. */
18369 cp_lexer_consume_token (parser
->lexer
);
18370 /* If the next token is a `}', there is a trailing comma. */
18371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18373 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18374 pedwarn (input_location
, OPT_Wpedantic
,
18375 "comma at end of enumerator list");
18381 /* Parse an enumerator-definition. The enumerator has the indicated
18384 enumerator-definition:
18386 enumerator = constant-expression
18393 enumerator-definition:
18394 enumerator attributes [opt]
18395 enumerator attributes [opt] = constant-expression */
18398 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18404 /* Save the input location because we are interested in the location
18405 of the identifier and not the location of the explicit value. */
18406 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18408 /* Look for the identifier. */
18409 identifier
= cp_parser_identifier (parser
);
18410 if (identifier
== error_mark_node
)
18413 /* Parse any specified attributes. */
18414 tree attrs
= cp_parser_attributes_opt (parser
);
18416 /* If the next token is an '=', then there is an explicit value. */
18417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18419 /* Consume the `=' token. */
18420 cp_lexer_consume_token (parser
->lexer
);
18421 /* Parse the value. */
18422 value
= cp_parser_constant_expression (parser
);
18427 /* If we are processing a template, make sure the initializer of the
18428 enumerator doesn't contain any bare template parameter pack. */
18429 if (check_for_bare_parameter_packs (value
))
18430 value
= error_mark_node
;
18432 /* Create the enumerator. */
18433 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18436 /* Parse a namespace-name.
18439 original-namespace-name
18442 Returns the NAMESPACE_DECL for the namespace. */
18445 cp_parser_namespace_name (cp_parser
* parser
)
18448 tree namespace_decl
;
18450 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18452 /* Get the name of the namespace. */
18453 identifier
= cp_parser_identifier (parser
);
18454 if (identifier
== error_mark_node
)
18455 return error_mark_node
;
18457 /* Look up the identifier in the currently active scope. Look only
18458 for namespaces, due to:
18460 [basic.lookup.udir]
18462 When looking up a namespace-name in a using-directive or alias
18463 definition, only namespace names are considered.
18467 [basic.lookup.qual]
18469 During the lookup of a name preceding the :: scope resolution
18470 operator, object, function, and enumerator names are ignored.
18472 (Note that cp_parser_qualifying_entity only calls this
18473 function if the token after the name is the scope resolution
18475 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18477 /*is_template=*/false,
18478 /*is_namespace=*/true,
18479 /*check_dependency=*/true,
18480 /*ambiguous_decls=*/NULL
,
18482 /* If it's not a namespace, issue an error. */
18483 if (namespace_decl
== error_mark_node
18484 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18486 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18488 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18489 if (namespace_decl
== error_mark_node
18490 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18491 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18494 cp_parser_error (parser
, "expected namespace-name");
18495 namespace_decl
= error_mark_node
;
18498 return namespace_decl
;
18501 /* Parse a namespace-definition.
18503 namespace-definition:
18504 named-namespace-definition
18505 unnamed-namespace-definition
18507 named-namespace-definition:
18508 original-namespace-definition
18509 extension-namespace-definition
18511 original-namespace-definition:
18512 namespace identifier { namespace-body }
18514 extension-namespace-definition:
18515 namespace original-namespace-name { namespace-body }
18517 unnamed-namespace-definition:
18518 namespace { namespace-body } */
18521 cp_parser_namespace_definition (cp_parser
* parser
)
18524 int nested_definition_count
= 0;
18526 cp_ensure_no_omp_declare_simd (parser
);
18527 cp_ensure_no_oacc_routine (parser
);
18529 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18533 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18534 cp_lexer_consume_token (parser
->lexer
);
18537 /* Look for the `namespace' keyword. */
18539 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18541 /* Parse any specified attributes before the identifier. */
18542 tree attribs
= cp_parser_attributes_opt (parser
);
18546 identifier
= NULL_TREE
;
18548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18550 identifier
= cp_parser_identifier (parser
);
18552 /* Parse any attributes specified after the identifier. */
18553 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18559 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18560 pedwarn (input_location
, OPT_Wpedantic
,
18561 "nested namespace definitions only available with "
18562 "-std=c++17 or -std=gnu++17");
18564 /* Nested namespace names can create new namespaces (unlike
18565 other qualified-ids). */
18566 if (int count
= identifier
? push_namespace (identifier
) : 0)
18567 nested_definition_count
+= count
;
18569 cp_parser_error (parser
, "nested namespace name required");
18570 cp_lexer_consume_token (parser
->lexer
);
18573 if (nested_definition_count
&& !identifier
)
18574 cp_parser_error (parser
, "namespace name required");
18576 if (nested_definition_count
&& attribs
)
18577 error_at (token
->location
,
18578 "a nested namespace definition cannot have attributes");
18579 if (nested_definition_count
&& is_inline
)
18580 error_at (token
->location
,
18581 "a nested namespace definition cannot be inline");
18583 /* Start the namespace. */
18584 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18586 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18588 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18590 /* Look for the `{' to validate starting the namespace. */
18591 matching_braces braces
;
18592 if (braces
.require_open (parser
))
18594 /* Parse the body of the namespace. */
18595 cp_parser_namespace_body (parser
);
18597 /* Look for the final `}'. */
18598 braces
.require_close (parser
);
18601 if (has_visibility
)
18602 pop_visibility (1);
18604 /* Pop the nested namespace definitions. */
18605 while (nested_definition_count
--)
18609 /* Parse a namespace-body.
18612 declaration-seq [opt] */
18615 cp_parser_namespace_body (cp_parser
* parser
)
18617 cp_parser_declaration_seq_opt (parser
);
18620 /* Parse a namespace-alias-definition.
18622 namespace-alias-definition:
18623 namespace identifier = qualified-namespace-specifier ; */
18626 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18629 tree namespace_specifier
;
18631 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18633 /* Look for the `namespace' keyword. */
18634 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18635 /* Look for the identifier. */
18636 identifier
= cp_parser_identifier (parser
);
18637 if (identifier
== error_mark_node
)
18639 /* Look for the `=' token. */
18640 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18641 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18643 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18644 /* Skip the definition. */
18645 cp_lexer_consume_token (parser
->lexer
);
18646 if (cp_parser_skip_to_closing_brace (parser
))
18647 cp_lexer_consume_token (parser
->lexer
);
18650 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18651 /* Look for the qualified-namespace-specifier. */
18652 namespace_specifier
18653 = cp_parser_qualified_namespace_specifier (parser
);
18654 /* Look for the `;' token. */
18655 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18657 /* Register the alias in the symbol table. */
18658 do_namespace_alias (identifier
, namespace_specifier
);
18661 /* Parse a qualified-namespace-specifier.
18663 qualified-namespace-specifier:
18664 :: [opt] nested-name-specifier [opt] namespace-name
18666 Returns a NAMESPACE_DECL corresponding to the specified
18670 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18672 /* Look for the optional `::'. */
18673 cp_parser_global_scope_opt (parser
,
18674 /*current_scope_valid_p=*/false);
18676 /* Look for the optional nested-name-specifier. */
18677 cp_parser_nested_name_specifier_opt (parser
,
18678 /*typename_keyword_p=*/false,
18679 /*check_dependency_p=*/true,
18681 /*is_declaration=*/true);
18683 return cp_parser_namespace_name (parser
);
18686 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18687 access declaration.
18690 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18691 using :: unqualified-id ;
18693 access-declaration:
18699 cp_parser_using_declaration (cp_parser
* parser
,
18700 bool access_declaration_p
)
18703 bool typename_p
= false;
18704 bool global_scope_p
;
18708 int oldcount
= errorcount
;
18709 cp_token
*diag_token
= NULL
;
18711 if (access_declaration_p
)
18713 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18714 cp_parser_parse_tentatively (parser
);
18718 /* Look for the `using' keyword. */
18719 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18722 /* Peek at the next token. */
18723 token
= cp_lexer_peek_token (parser
->lexer
);
18724 /* See if it's `typename'. */
18725 if (token
->keyword
== RID_TYPENAME
)
18727 /* Remember that we've seen it. */
18729 /* Consume the `typename' token. */
18730 cp_lexer_consume_token (parser
->lexer
);
18734 /* Look for the optional global scope qualification. */
18736 = (cp_parser_global_scope_opt (parser
,
18737 /*current_scope_valid_p=*/false)
18740 /* If we saw `typename', or didn't see `::', then there must be a
18741 nested-name-specifier present. */
18742 if (typename_p
|| !global_scope_p
)
18744 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18745 /*check_dependency_p=*/true,
18747 /*is_declaration=*/true);
18748 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18750 cp_parser_skip_to_end_of_block_or_statement (parser
);
18754 /* Otherwise, we could be in either of the two productions. In that
18755 case, treat the nested-name-specifier as optional. */
18757 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18758 /*typename_keyword_p=*/false,
18759 /*check_dependency_p=*/true,
18761 /*is_declaration=*/true);
18763 qscope
= global_namespace
;
18764 else if (UNSCOPED_ENUM_P (qscope
))
18765 qscope
= CP_TYPE_CONTEXT (qscope
);
18767 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18768 /* Something has already gone wrong; there's no need to parse
18769 further. Since an error has occurred, the return value of
18770 cp_parser_parse_definitely will be false, as required. */
18771 return cp_parser_parse_definitely (parser
);
18773 token
= cp_lexer_peek_token (parser
->lexer
);
18774 /* Parse the unqualified-id. */
18775 identifier
= cp_parser_unqualified_id (parser
,
18776 /*template_keyword_p=*/false,
18777 /*check_dependency_p=*/true,
18778 /*declarator_p=*/true,
18779 /*optional_p=*/false);
18781 if (access_declaration_p
)
18783 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18784 cp_parser_simulate_error (parser
);
18785 if (!cp_parser_parse_definitely (parser
))
18788 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18790 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18791 if (cxx_dialect
< cxx17
18792 && !in_system_header_at (ell
->location
))
18793 pedwarn (ell
->location
, 0,
18794 "pack expansion in using-declaration only available "
18795 "with -std=c++17 or -std=gnu++17");
18796 qscope
= make_pack_expansion (qscope
);
18799 /* The function we call to handle a using-declaration is different
18800 depending on what scope we are in. */
18801 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18803 else if (!identifier_p (identifier
)
18804 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18805 /* [namespace.udecl]
18807 A using declaration shall not name a template-id. */
18808 error_at (token
->location
,
18809 "a template-id may not appear in a using-declaration");
18812 if (at_class_scope_p ())
18814 /* Create the USING_DECL. */
18815 decl
= do_class_using_decl (qscope
, identifier
);
18817 if (decl
&& typename_p
)
18818 USING_DECL_TYPENAME_P (decl
) = 1;
18820 if (check_for_bare_parameter_packs (decl
))
18822 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18826 /* Add it to the list of members in this class. */
18827 finish_member_declaration (decl
);
18831 decl
= cp_parser_lookup_name_simple (parser
,
18834 if (decl
== error_mark_node
)
18835 cp_parser_name_lookup_error (parser
, identifier
,
18838 else if (check_for_bare_parameter_packs (decl
))
18840 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18843 else if (!at_namespace_scope_p ())
18844 finish_local_using_decl (decl
, qscope
, identifier
);
18846 finish_namespace_using_decl (decl
, qscope
, identifier
);
18850 if (!access_declaration_p
18851 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18853 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18854 if (cxx_dialect
< cxx17
)
18855 pedwarn (comma
->location
, 0,
18856 "comma-separated list in using-declaration only available "
18857 "with -std=c++17 or -std=gnu++17");
18861 /* Look for the final `;'. */
18862 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18864 if (access_declaration_p
&& errorcount
== oldcount
)
18865 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18866 "access declarations are deprecated "
18867 "in favour of using-declarations; "
18868 "suggestion: add the %<using%> keyword");
18873 /* Parse an alias-declaration.
18876 using identifier attribute-specifier-seq [opt] = type-id */
18879 cp_parser_alias_declaration (cp_parser
* parser
)
18881 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18882 location_t id_location
;
18883 cp_declarator
*declarator
;
18884 cp_decl_specifier_seq decl_specs
;
18886 const char *saved_message
= NULL
;
18888 /* Look for the `using' keyword. */
18889 cp_token
*using_token
18890 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18891 if (using_token
== NULL
)
18892 return error_mark_node
;
18894 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18895 id
= cp_parser_identifier (parser
);
18896 if (id
== error_mark_node
)
18897 return error_mark_node
;
18899 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18900 attributes
= cp_parser_attributes_opt (parser
);
18901 if (attributes
== error_mark_node
)
18902 return error_mark_node
;
18904 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18906 if (cp_parser_error_occurred (parser
))
18907 return error_mark_node
;
18909 cp_parser_commit_to_tentative_parse (parser
);
18911 /* Now we are going to parse the type-id of the declaration. */
18916 "A type-specifier-seq shall not define a class or enumeration
18917 unless it appears in the type-id of an alias-declaration (7.1.3) that
18918 is not the declaration of a template-declaration."
18920 In other words, if we currently are in an alias template, the
18921 type-id should not define a type.
18923 So let's set parser->type_definition_forbidden_message in that
18924 case; cp_parser_check_type_definition (called by
18925 cp_parser_class_specifier) will then emit an error if a type is
18926 defined in the type-id. */
18927 if (parser
->num_template_parameter_lists
)
18929 saved_message
= parser
->type_definition_forbidden_message
;
18930 parser
->type_definition_forbidden_message
=
18931 G_("types may not be defined in alias template declarations");
18934 type
= cp_parser_type_id (parser
);
18936 /* Restore the error message if need be. */
18937 if (parser
->num_template_parameter_lists
)
18938 parser
->type_definition_forbidden_message
= saved_message
;
18940 if (type
== error_mark_node
18941 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18943 cp_parser_skip_to_end_of_block_or_statement (parser
);
18944 return error_mark_node
;
18947 /* A typedef-name can also be introduced by an alias-declaration. The
18948 identifier following the using keyword becomes a typedef-name. It has
18949 the same semantics as if it were introduced by the typedef
18950 specifier. In particular, it does not define a new type and it shall
18951 not appear in the type-id. */
18953 clear_decl_specs (&decl_specs
);
18954 decl_specs
.type
= type
;
18955 if (attributes
!= NULL_TREE
)
18957 decl_specs
.attributes
= attributes
;
18958 set_and_check_decl_spec_loc (&decl_specs
,
18962 set_and_check_decl_spec_loc (&decl_specs
,
18965 set_and_check_decl_spec_loc (&decl_specs
,
18969 if (parser
->num_template_parameter_lists
18970 && !cp_parser_check_template_parameters (parser
,
18971 /*num_templates=*/0,
18972 /*template_id*/false,
18974 /*declarator=*/NULL
))
18975 return error_mark_node
;
18977 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18978 declarator
->id_loc
= id_location
;
18980 member_p
= at_class_scope_p ();
18982 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18983 NULL_TREE
, attributes
);
18985 decl
= start_decl (declarator
, &decl_specs
, 0,
18986 attributes
, NULL_TREE
, &pushed_scope
);
18987 if (decl
== error_mark_node
)
18990 // Attach constraints to the alias declaration.
18991 if (flag_concepts
&& current_template_parms
)
18993 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18994 tree constr
= build_constraints (reqs
, NULL_TREE
);
18995 set_constraints (decl
, constr
);
18998 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19001 pop_scope (pushed_scope
);
19003 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19004 added into the symbol table; otherwise, return the TYPE_DECL. */
19005 if (DECL_LANG_SPECIFIC (decl
)
19006 && DECL_TEMPLATE_INFO (decl
)
19007 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19009 decl
= DECL_TI_TEMPLATE (decl
);
19011 check_member_template (decl
);
19017 /* Parse a using-directive.
19020 using namespace :: [opt] nested-name-specifier [opt]
19021 namespace-name ; */
19024 cp_parser_using_directive (cp_parser
* parser
)
19026 tree namespace_decl
;
19029 /* Look for the `using' keyword. */
19030 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19031 /* And the `namespace' keyword. */
19032 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19033 /* Look for the optional `::' operator. */
19034 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19035 /* And the optional nested-name-specifier. */
19036 cp_parser_nested_name_specifier_opt (parser
,
19037 /*typename_keyword_p=*/false,
19038 /*check_dependency_p=*/true,
19040 /*is_declaration=*/true);
19041 /* Get the namespace being used. */
19042 namespace_decl
= cp_parser_namespace_name (parser
);
19043 /* And any specified attributes. */
19044 attribs
= cp_parser_attributes_opt (parser
);
19046 /* Update the symbol table. */
19047 if (namespace_bindings_p ())
19048 finish_namespace_using_directive (namespace_decl
, attribs
);
19050 finish_local_using_directive (namespace_decl
, attribs
);
19052 /* Look for the final `;'. */
19053 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19056 /* Parse an asm-definition.
19059 asm ( string-literal ) ;
19064 asm volatile [opt] ( string-literal ) ;
19065 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19066 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19067 : asm-operand-list [opt] ) ;
19068 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19069 : asm-operand-list [opt]
19070 : asm-clobber-list [opt] ) ;
19071 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19072 : asm-clobber-list [opt]
19073 : asm-goto-list ) ; */
19076 cp_parser_asm_definition (cp_parser
* parser
)
19079 tree outputs
= NULL_TREE
;
19080 tree inputs
= NULL_TREE
;
19081 tree clobbers
= NULL_TREE
;
19082 tree labels
= NULL_TREE
;
19084 bool volatile_p
= false;
19085 bool extended_p
= false;
19086 bool invalid_inputs_p
= false;
19087 bool invalid_outputs_p
= false;
19088 bool goto_p
= false;
19089 required_token missing
= RT_NONE
;
19091 /* Look for the `asm' keyword. */
19092 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19094 if (parser
->in_function_body
19095 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19097 error ("%<asm%> in %<constexpr%> function");
19098 cp_function_chain
->invalid_constexpr
= true;
19101 /* See if the next token is `volatile'. */
19102 if (cp_parser_allow_gnu_extensions_p (parser
)
19103 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19105 /* Remember that we saw the `volatile' keyword. */
19107 /* Consume the token. */
19108 cp_lexer_consume_token (parser
->lexer
);
19110 if (cp_parser_allow_gnu_extensions_p (parser
)
19111 && parser
->in_function_body
19112 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19114 /* Remember that we saw the `goto' keyword. */
19116 /* Consume the token. */
19117 cp_lexer_consume_token (parser
->lexer
);
19119 /* Look for the opening `('. */
19120 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19122 /* Look for the string. */
19123 string
= cp_parser_string_literal (parser
, false, false);
19124 if (string
== error_mark_node
)
19126 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19127 /*consume_paren=*/true);
19131 /* If we're allowing GNU extensions, check for the extended assembly
19132 syntax. Unfortunately, the `:' tokens need not be separated by
19133 a space in C, and so, for compatibility, we tolerate that here
19134 too. Doing that means that we have to treat the `::' operator as
19136 if (cp_parser_allow_gnu_extensions_p (parser
)
19137 && parser
->in_function_body
19138 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19139 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19141 bool inputs_p
= false;
19142 bool clobbers_p
= false;
19143 bool labels_p
= false;
19145 /* The extended syntax was used. */
19148 /* Look for outputs. */
19149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19151 /* Consume the `:'. */
19152 cp_lexer_consume_token (parser
->lexer
);
19153 /* Parse the output-operands. */
19154 if (cp_lexer_next_token_is_not (parser
->lexer
,
19156 && cp_lexer_next_token_is_not (parser
->lexer
,
19158 && cp_lexer_next_token_is_not (parser
->lexer
,
19162 outputs
= cp_parser_asm_operand_list (parser
);
19163 if (outputs
== error_mark_node
)
19164 invalid_outputs_p
= true;
19167 /* If the next token is `::', there are no outputs, and the
19168 next token is the beginning of the inputs. */
19169 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19170 /* The inputs are coming next. */
19173 /* Look for inputs. */
19175 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19177 /* Consume the `:' or `::'. */
19178 cp_lexer_consume_token (parser
->lexer
);
19179 /* Parse the output-operands. */
19180 if (cp_lexer_next_token_is_not (parser
->lexer
,
19182 && cp_lexer_next_token_is_not (parser
->lexer
,
19184 && cp_lexer_next_token_is_not (parser
->lexer
,
19187 inputs
= cp_parser_asm_operand_list (parser
);
19188 if (inputs
== error_mark_node
)
19189 invalid_inputs_p
= true;
19192 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19193 /* The clobbers are coming next. */
19196 /* Look for clobbers. */
19198 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19201 /* Consume the `:' or `::'. */
19202 cp_lexer_consume_token (parser
->lexer
);
19203 /* Parse the clobbers. */
19204 if (cp_lexer_next_token_is_not (parser
->lexer
,
19206 && cp_lexer_next_token_is_not (parser
->lexer
,
19208 clobbers
= cp_parser_asm_clobber_list (parser
);
19211 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19212 /* The labels are coming next. */
19215 /* Look for labels. */
19217 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19220 /* Consume the `:' or `::'. */
19221 cp_lexer_consume_token (parser
->lexer
);
19222 /* Parse the labels. */
19223 labels
= cp_parser_asm_label_list (parser
);
19226 if (goto_p
&& !labels_p
)
19227 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19230 missing
= RT_COLON_SCOPE
;
19232 /* Look for the closing `)'. */
19233 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19234 missing
? missing
: RT_CLOSE_PAREN
))
19235 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19236 /*consume_paren=*/true);
19237 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19239 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19241 /* Create the ASM_EXPR. */
19242 if (parser
->in_function_body
)
19244 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19245 inputs
, clobbers
, labels
);
19246 /* If the extended syntax was not used, mark the ASM_EXPR. */
19249 tree temp
= asm_stmt
;
19250 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19251 temp
= TREE_OPERAND (temp
, 0);
19253 ASM_INPUT_P (temp
) = 1;
19257 symtab
->finalize_toplevel_asm (string
);
19261 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19262 type that comes from the decl-specifier-seq. */
19265 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19267 for (cp_declarator
*d
= declarator
; d
;)
19277 if (TYPE_PTRMEMFUNC_P (type
))
19278 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19279 type
= TREE_TYPE (type
);
19287 /* Declarators [gram.dcl.decl] */
19289 /* Parse an init-declarator.
19292 declarator initializer [opt]
19297 declarator asm-specification [opt] attributes [opt] initializer [opt]
19299 function-definition:
19300 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19302 decl-specifier-seq [opt] declarator function-try-block
19306 function-definition:
19307 __extension__ function-definition
19311 function-definition:
19312 decl-specifier-seq [opt] declarator function-transaction-block
19314 The DECL_SPECIFIERS apply to this declarator. Returns a
19315 representation of the entity declared. If MEMBER_P is TRUE, then
19316 this declarator appears in a class scope. The new DECL created by
19317 this declarator is returned.
19319 The CHECKS are access checks that should be performed once we know
19320 what entity is being declared (and, therefore, what classes have
19323 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19324 for a function-definition here as well. If the declarator is a
19325 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19326 be TRUE upon return. By that point, the function-definition will
19327 have been completely parsed.
19329 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19332 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19333 parsed declaration if it is an uninitialized single declarator not followed
19334 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19335 if present, will not be consumed. If returned, this declarator will be
19336 created with SD_INITIALIZED but will not call cp_finish_decl.
19338 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19339 and there is an initializer, the pointed location_t is set to the
19340 location of the '=' or `(', or '{' in C++11 token introducing the
19344 cp_parser_init_declarator (cp_parser
* parser
,
19345 cp_decl_specifier_seq
*decl_specifiers
,
19346 vec
<deferred_access_check
, va_gc
> *checks
,
19347 bool function_definition_allowed_p
,
19349 int declares_class_or_enum
,
19350 bool* function_definition_p
,
19351 tree
* maybe_range_for_decl
,
19352 location_t
* init_loc
,
19355 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19356 *attributes_start_token
= NULL
;
19357 cp_declarator
*declarator
;
19358 tree prefix_attributes
;
19359 tree attributes
= NULL
;
19360 tree asm_specification
;
19362 tree decl
= NULL_TREE
;
19364 int is_initialized
;
19365 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19366 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19368 enum cpp_ttype initialization_kind
;
19369 bool is_direct_init
= false;
19370 bool is_non_constant_init
;
19371 int ctor_dtor_or_conv_p
;
19372 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19373 tree pushed_scope
= NULL_TREE
;
19374 bool range_for_decl_p
= false;
19375 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19376 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19378 /* Gather the attributes that were provided with the
19379 decl-specifiers. */
19380 prefix_attributes
= decl_specifiers
->attributes
;
19382 /* Assume that this is not the declarator for a function
19384 if (function_definition_p
)
19385 *function_definition_p
= false;
19387 /* Default arguments are only permitted for function parameters. */
19388 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19389 parser
->default_arg_ok_p
= false;
19391 /* Defer access checks while parsing the declarator; we cannot know
19392 what names are accessible until we know what is being
19394 resume_deferring_access_checks ();
19396 token
= cp_lexer_peek_token (parser
->lexer
);
19398 /* Parse the declarator. */
19400 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19401 &ctor_dtor_or_conv_p
,
19402 /*parenthesized_p=*/NULL
,
19403 member_p
, friend_p
);
19404 /* Gather up the deferred checks. */
19405 stop_deferring_access_checks ();
19407 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19409 /* If the DECLARATOR was erroneous, there's no need to go
19411 if (declarator
== cp_error_declarator
)
19412 return error_mark_node
;
19414 /* Check that the number of template-parameter-lists is OK. */
19415 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19417 return error_mark_node
;
19419 if (declares_class_or_enum
& 2)
19420 cp_parser_check_for_definition_in_return_type (declarator
,
19421 decl_specifiers
->type
,
19422 decl_specifiers
->locations
[ds_type_spec
]);
19424 /* Figure out what scope the entity declared by the DECLARATOR is
19425 located in. `grokdeclarator' sometimes changes the scope, so
19426 we compute it now. */
19427 scope
= get_scope_of_declarator (declarator
);
19429 /* Perform any lookups in the declared type which were thought to be
19430 dependent, but are not in the scope of the declarator. */
19431 decl_specifiers
->type
19432 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19434 /* If we're allowing GNU extensions, look for an
19435 asm-specification. */
19436 if (cp_parser_allow_gnu_extensions_p (parser
))
19438 /* Look for an asm-specification. */
19439 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19440 asm_specification
= cp_parser_asm_specification_opt (parser
);
19443 asm_specification
= NULL_TREE
;
19445 /* Look for attributes. */
19446 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19447 attributes
= cp_parser_attributes_opt (parser
);
19449 /* Peek at the next token. */
19450 token
= cp_lexer_peek_token (parser
->lexer
);
19452 bool bogus_implicit_tmpl
= false;
19454 if (function_declarator_p (declarator
))
19456 /* Handle C++17 deduction guides. */
19457 if (!decl_specifiers
->type
19458 && ctor_dtor_or_conv_p
<= 0
19459 && cxx_dialect
>= cxx17
)
19461 cp_declarator
*id
= get_id_declarator (declarator
);
19462 tree name
= id
->u
.id
.unqualified_name
;
19463 parser
->scope
= id
->u
.id
.qualifying_scope
;
19464 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19466 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19467 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19469 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19470 id
->u
.id
.sfk
= sfk_deduction_guide
;
19471 ctor_dtor_or_conv_p
= 1;
19475 /* Check to see if the token indicates the start of a
19476 function-definition. */
19477 if (cp_parser_token_starts_function_definition_p (token
))
19479 if (!function_definition_allowed_p
)
19481 /* If a function-definition should not appear here, issue an
19483 cp_parser_error (parser
,
19484 "a function-definition is not allowed here");
19485 return error_mark_node
;
19488 location_t func_brace_location
19489 = cp_lexer_peek_token (parser
->lexer
)->location
;
19491 /* Neither attributes nor an asm-specification are allowed
19492 on a function-definition. */
19493 if (asm_specification
)
19494 error_at (asm_spec_start_token
->location
,
19495 "an asm-specification is not allowed "
19496 "on a function-definition");
19498 error_at (attributes_start_token
->location
,
19499 "attributes are not allowed "
19500 "on a function-definition");
19501 /* This is a function-definition. */
19502 *function_definition_p
= true;
19504 /* Parse the function definition. */
19506 decl
= cp_parser_save_member_function_body (parser
,
19509 prefix_attributes
);
19512 (cp_parser_function_definition_from_specifiers_and_declarator
19513 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19515 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19517 /* This is where the prologue starts... */
19518 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19519 = func_brace_location
;
19525 else if (parser
->fully_implicit_function_template_p
)
19527 /* A non-template declaration involving a function parameter list
19528 containing an implicit template parameter will be made into a
19529 template. If the resulting declaration is not going to be an
19530 actual function then finish the template scope here to prevent it.
19531 An error message will be issued once we have a decl to talk about.
19533 FIXME probably we should do type deduction rather than create an
19534 implicit template, but the standard currently doesn't allow it. */
19535 bogus_implicit_tmpl
= true;
19536 finish_fully_implicit_template (parser
, NULL_TREE
);
19541 Only in function declarations for constructors, destructors, type
19542 conversions, and deduction guides can the decl-specifier-seq be omitted.
19544 We explicitly postpone this check past the point where we handle
19545 function-definitions because we tolerate function-definitions
19546 that are missing their return types in some modes. */
19547 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19549 cp_parser_error (parser
,
19550 "expected constructor, destructor, or type conversion");
19551 return error_mark_node
;
19554 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19555 if (token
->type
== CPP_EQ
19556 || token
->type
== CPP_OPEN_PAREN
19557 || token
->type
== CPP_OPEN_BRACE
)
19559 is_initialized
= SD_INITIALIZED
;
19560 initialization_kind
= token
->type
;
19561 if (maybe_range_for_decl
)
19562 *maybe_range_for_decl
= error_mark_node
;
19563 tmp_init_loc
= token
->location
;
19564 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19565 *init_loc
= tmp_init_loc
;
19567 if (token
->type
== CPP_EQ
19568 && function_declarator_p (declarator
))
19570 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19571 if (t2
->keyword
== RID_DEFAULT
)
19572 is_initialized
= SD_DEFAULTED
;
19573 else if (t2
->keyword
== RID_DELETE
)
19574 is_initialized
= SD_DELETED
;
19579 /* If the init-declarator isn't initialized and isn't followed by a
19580 `,' or `;', it's not a valid init-declarator. */
19581 if (token
->type
!= CPP_COMMA
19582 && token
->type
!= CPP_SEMICOLON
)
19584 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19585 range_for_decl_p
= true;
19588 if (!maybe_range_for_decl
)
19589 cp_parser_error (parser
, "expected initializer");
19590 return error_mark_node
;
19593 is_initialized
= SD_UNINITIALIZED
;
19594 initialization_kind
= CPP_EOF
;
19597 /* Because start_decl has side-effects, we should only call it if we
19598 know we're going ahead. By this point, we know that we cannot
19599 possibly be looking at any other construct. */
19600 cp_parser_commit_to_tentative_parse (parser
);
19602 /* Enter the newly declared entry in the symbol table. If we're
19603 processing a declaration in a class-specifier, we wait until
19604 after processing the initializer. */
19607 if (parser
->in_unbraced_linkage_specification_p
)
19608 decl_specifiers
->storage_class
= sc_extern
;
19609 decl
= start_decl (declarator
, decl_specifiers
,
19610 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19611 attributes
, prefix_attributes
, &pushed_scope
);
19612 cp_finalize_omp_declare_simd (parser
, decl
);
19613 cp_finalize_oacc_routine (parser
, decl
, false);
19614 /* Adjust location of decl if declarator->id_loc is more appropriate:
19615 set, and decl wasn't merged with another decl, in which case its
19616 location would be different from input_location, and more accurate. */
19618 && declarator
->id_loc
!= UNKNOWN_LOCATION
19619 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19620 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19623 /* Enter the SCOPE. That way unqualified names appearing in the
19624 initializer will be looked up in SCOPE. */
19625 pushed_scope
= push_scope (scope
);
19627 /* Perform deferred access control checks, now that we know in which
19628 SCOPE the declared entity resides. */
19629 if (!member_p
&& decl
)
19631 tree saved_current_function_decl
= NULL_TREE
;
19633 /* If the entity being declared is a function, pretend that we
19634 are in its scope. If it is a `friend', it may have access to
19635 things that would not otherwise be accessible. */
19636 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19638 saved_current_function_decl
= current_function_decl
;
19639 current_function_decl
= decl
;
19642 /* Perform access checks for template parameters. */
19643 cp_parser_perform_template_parameter_access_checks (checks
);
19645 /* Perform the access control checks for the declarator and the
19646 decl-specifiers. */
19647 perform_deferred_access_checks (tf_warning_or_error
);
19649 /* Restore the saved value. */
19650 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19651 current_function_decl
= saved_current_function_decl
;
19654 /* Parse the initializer. */
19655 initializer
= NULL_TREE
;
19656 is_direct_init
= false;
19657 is_non_constant_init
= true;
19658 if (is_initialized
)
19660 if (function_declarator_p (declarator
))
19662 if (initialization_kind
== CPP_EQ
)
19663 initializer
= cp_parser_pure_specifier (parser
);
19666 /* If the declaration was erroneous, we don't really
19667 know what the user intended, so just silently
19668 consume the initializer. */
19669 if (decl
!= error_mark_node
)
19670 error_at (tmp_init_loc
, "initializer provided for function");
19671 cp_parser_skip_to_closing_parenthesis (parser
,
19672 /*recovering=*/true,
19673 /*or_comma=*/false,
19674 /*consume_paren=*/true);
19679 /* We want to record the extra mangling scope for in-class
19680 initializers of class members and initializers of static data
19681 member templates. The former involves deferring
19682 parsing of the initializer until end of class as with default
19683 arguments. So right here we only handle the latter. */
19684 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19685 start_lambda_scope (decl
);
19686 initializer
= cp_parser_initializer (parser
,
19688 &is_non_constant_init
);
19689 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19690 finish_lambda_scope ();
19691 if (initializer
== error_mark_node
)
19692 cp_parser_skip_to_end_of_statement (parser
);
19696 /* The old parser allows attributes to appear after a parenthesized
19697 initializer. Mark Mitchell proposed removing this functionality
19698 on the GCC mailing lists on 2002-08-13. This parser accepts the
19699 attributes -- but ignores them. Made a permerror in GCC 8. */
19700 if (cp_parser_allow_gnu_extensions_p (parser
)
19701 && initialization_kind
== CPP_OPEN_PAREN
19702 && cp_parser_attributes_opt (parser
)
19703 && permerror (input_location
,
19704 "attributes after parenthesized initializer ignored"))
19707 if (flag_permissive
&& !hint
)
19710 inform (input_location
,
19711 "this flexibility is deprecated and will be removed");
19715 /* And now complain about a non-function implicit template. */
19716 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19717 error_at (DECL_SOURCE_LOCATION (decl
),
19718 "non-function %qD declared as implicit template", decl
);
19720 /* For an in-class declaration, use `grokfield' to create the
19726 pop_scope (pushed_scope
);
19727 pushed_scope
= NULL_TREE
;
19729 decl
= grokfield (declarator
, decl_specifiers
,
19730 initializer
, !is_non_constant_init
,
19731 /*asmspec=*/NULL_TREE
,
19732 attr_chainon (attributes
, prefix_attributes
));
19733 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19734 cp_parser_save_default_args (parser
, decl
);
19735 cp_finalize_omp_declare_simd (parser
, decl
);
19736 cp_finalize_oacc_routine (parser
, decl
, false);
19739 /* Finish processing the declaration. But, skip member
19741 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19743 cp_finish_decl (decl
,
19744 initializer
, !is_non_constant_init
,
19746 /* If the initializer is in parentheses, then this is
19747 a direct-initialization, which means that an
19748 `explicit' constructor is OK. Otherwise, an
19749 `explicit' constructor cannot be used. */
19750 ((is_direct_init
|| !is_initialized
)
19751 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19753 else if ((cxx_dialect
!= cxx98
) && friend_p
19754 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19755 /* Core issue #226 (C++0x only): A default template-argument
19756 shall not be specified in a friend class template
19758 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19759 /*is_partial=*/false, /*is_friend_decl=*/1);
19761 if (!friend_p
&& pushed_scope
)
19762 pop_scope (pushed_scope
);
19764 if (function_declarator_p (declarator
)
19765 && parser
->fully_implicit_function_template_p
)
19768 decl
= finish_fully_implicit_template (parser
, decl
);
19770 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19773 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19774 && type_uses_auto (decl_specifiers
->type
))
19775 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19780 /* Parse a declarator.
19784 ptr-operator declarator
19786 abstract-declarator:
19787 ptr-operator abstract-declarator [opt]
19788 direct-abstract-declarator
19793 attributes [opt] direct-declarator
19794 attributes [opt] ptr-operator declarator
19796 abstract-declarator:
19797 attributes [opt] ptr-operator abstract-declarator [opt]
19798 attributes [opt] direct-abstract-declarator
19800 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19801 detect constructors, destructors, deduction guides, or conversion operators.
19802 It is set to -1 if the declarator is a name, and +1 if it is a
19803 function. Otherwise it is set to zero. Usually you just want to
19804 test for >0, but internally the negative value is used.
19806 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19807 a decl-specifier-seq unless it declares a constructor, destructor,
19808 or conversion. It might seem that we could check this condition in
19809 semantic analysis, rather than parsing, but that makes it difficult
19810 to handle something like `f()'. We want to notice that there are
19811 no decl-specifiers, and therefore realize that this is an
19812 expression, not a declaration.)
19814 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19815 the declarator is a direct-declarator of the form "(...)".
19817 MEMBER_P is true iff this declarator is a member-declarator.
19819 FRIEND_P is true iff this declarator is a friend. */
19821 static cp_declarator
*
19822 cp_parser_declarator (cp_parser
* parser
,
19823 cp_parser_declarator_kind dcl_kind
,
19824 int* ctor_dtor_or_conv_p
,
19825 bool* parenthesized_p
,
19826 bool member_p
, bool friend_p
)
19828 cp_declarator
*declarator
;
19829 enum tree_code code
;
19830 cp_cv_quals cv_quals
;
19832 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19834 /* Assume this is not a constructor, destructor, or type-conversion
19836 if (ctor_dtor_or_conv_p
)
19837 *ctor_dtor_or_conv_p
= 0;
19839 if (cp_parser_allow_gnu_extensions_p (parser
))
19840 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19842 /* Check for the ptr-operator production. */
19843 cp_parser_parse_tentatively (parser
);
19844 /* Parse the ptr-operator. */
19845 code
= cp_parser_ptr_operator (parser
,
19850 /* If that worked, then we have a ptr-operator. */
19851 if (cp_parser_parse_definitely (parser
))
19853 /* If a ptr-operator was found, then this declarator was not
19855 if (parenthesized_p
)
19856 *parenthesized_p
= true;
19857 /* The dependent declarator is optional if we are parsing an
19858 abstract-declarator. */
19859 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19860 cp_parser_parse_tentatively (parser
);
19862 /* Parse the dependent declarator. */
19863 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19864 /*ctor_dtor_or_conv_p=*/NULL
,
19865 /*parenthesized_p=*/NULL
,
19866 /*member_p=*/false,
19869 /* If we are parsing an abstract-declarator, we must handle the
19870 case where the dependent declarator is absent. */
19871 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19872 && !cp_parser_parse_definitely (parser
))
19875 declarator
= cp_parser_make_indirect_declarator
19876 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19878 /* Everything else is a direct-declarator. */
19881 if (parenthesized_p
)
19882 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19884 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19885 ctor_dtor_or_conv_p
,
19886 member_p
, friend_p
);
19889 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19890 declarator
->attributes
= gnu_attributes
;
19894 /* Parse a direct-declarator or direct-abstract-declarator.
19898 direct-declarator ( parameter-declaration-clause )
19899 cv-qualifier-seq [opt]
19900 ref-qualifier [opt]
19901 exception-specification [opt]
19902 direct-declarator [ constant-expression [opt] ]
19905 direct-abstract-declarator:
19906 direct-abstract-declarator [opt]
19907 ( parameter-declaration-clause )
19908 cv-qualifier-seq [opt]
19909 ref-qualifier [opt]
19910 exception-specification [opt]
19911 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19912 ( abstract-declarator )
19914 Returns a representation of the declarator. DCL_KIND is
19915 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19916 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19917 we are parsing a direct-declarator. It is
19918 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19919 of ambiguity we prefer an abstract declarator, as per
19920 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19921 as for cp_parser_declarator. */
19923 static cp_declarator
*
19924 cp_parser_direct_declarator (cp_parser
* parser
,
19925 cp_parser_declarator_kind dcl_kind
,
19926 int* ctor_dtor_or_conv_p
,
19927 bool member_p
, bool friend_p
)
19930 cp_declarator
*declarator
= NULL
;
19931 tree scope
= NULL_TREE
;
19932 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19933 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19935 tree pushed_scope
= NULL_TREE
;
19936 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19940 /* Peek at the next token. */
19941 token
= cp_lexer_peek_token (parser
->lexer
);
19942 if (token
->type
== CPP_OPEN_PAREN
)
19944 /* This is either a parameter-declaration-clause, or a
19945 parenthesized declarator. When we know we are parsing a
19946 named declarator, it must be a parenthesized declarator
19947 if FIRST is true. For instance, `(int)' is a
19948 parameter-declaration-clause, with an omitted
19949 direct-abstract-declarator. But `((*))', is a
19950 parenthesized abstract declarator. Finally, when T is a
19951 template parameter `(T)' is a
19952 parameter-declaration-clause, and not a parenthesized
19955 We first try and parse a parameter-declaration-clause,
19956 and then try a nested declarator (if FIRST is true).
19958 It is not an error for it not to be a
19959 parameter-declaration-clause, even when FIRST is
19965 The first is the declaration of a function while the
19966 second is the definition of a variable, including its
19969 Having seen only the parenthesis, we cannot know which of
19970 these two alternatives should be selected. Even more
19971 complex are examples like:
19976 The former is a function-declaration; the latter is a
19977 variable initialization.
19979 Thus again, we try a parameter-declaration-clause, and if
19980 that fails, we back out and return. */
19982 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19985 bool is_declarator
= false;
19989 /* In a member-declarator, the only valid interpretation
19990 of a parenthesis is the start of a
19991 parameter-declaration-clause. (It is invalid to
19992 initialize a static data member with a parenthesized
19993 initializer; only the "=" form of initialization is
19996 cp_parser_parse_tentatively (parser
);
19998 /* Consume the `('. */
19999 matching_parens parens
;
20000 parens
.consume_open (parser
);
20003 /* If this is going to be an abstract declarator, we're
20004 in a declarator and we can't have default args. */
20005 parser
->default_arg_ok_p
= false;
20006 parser
->in_declarator_p
= true;
20009 begin_scope (sk_function_parms
, NULL_TREE
);
20011 /* Parse the parameter-declaration-clause. */
20012 params
= cp_parser_parameter_declaration_clause (parser
);
20014 /* Consume the `)'. */
20015 parens
.require_close (parser
);
20017 /* If all went well, parse the cv-qualifier-seq,
20018 ref-qualifier and the exception-specification. */
20019 if (member_p
|| cp_parser_parse_definitely (parser
))
20021 cp_cv_quals cv_quals
;
20022 cp_virt_specifiers virt_specifiers
;
20023 cp_ref_qualifier ref_qual
;
20024 tree exception_specification
;
20027 bool memfn
= (member_p
|| (pushed_scope
20028 && CLASS_TYPE_P (pushed_scope
)));
20030 is_declarator
= true;
20032 if (ctor_dtor_or_conv_p
)
20033 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20036 /* Parse the cv-qualifier-seq. */
20037 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20038 /* Parse the ref-qualifier. */
20039 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20040 /* Parse the tx-qualifier. */
20041 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20042 /* And the exception-specification. */
20043 exception_specification
20044 = cp_parser_exception_specification_opt (parser
);
20046 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20048 /* In here, we handle cases where attribute is used after
20049 the function declaration. For example:
20050 void func (int x) __attribute__((vector(..))); */
20051 tree gnu_attrs
= NULL_TREE
;
20052 tree requires_clause
= NULL_TREE
;
20053 late_return
= (cp_parser_late_return_type_opt
20054 (parser
, declarator
, requires_clause
,
20055 memfn
? cv_quals
: -1));
20057 /* Parse the virt-specifier-seq. */
20058 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20060 /* Create the function-declarator. */
20061 declarator
= make_call_declarator (declarator
,
20067 exception_specification
,
20070 declarator
->std_attributes
= attrs
;
20071 declarator
->attributes
= gnu_attrs
;
20072 /* Any subsequent parameter lists are to do with
20073 return type, so are not those of the declared
20075 parser
->default_arg_ok_p
= false;
20078 /* Remove the function parms from scope. */
20079 pop_bindings_and_leave_scope ();
20082 /* Repeat the main loop. */
20086 /* If this is the first, we can try a parenthesized
20090 bool saved_in_type_id_in_expr_p
;
20092 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20093 parser
->in_declarator_p
= saved_in_declarator_p
;
20095 open_paren
= token
;
20096 /* Consume the `('. */
20097 matching_parens parens
;
20098 parens
.consume_open (parser
);
20099 /* Parse the nested declarator. */
20100 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20101 parser
->in_type_id_in_expr_p
= true;
20103 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20104 /*parenthesized_p=*/NULL
,
20105 member_p
, friend_p
);
20106 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20108 /* Expect a `)'. */
20109 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20110 if (!parens
.require_close (parser
))
20111 declarator
= cp_error_declarator
;
20112 if (declarator
== cp_error_declarator
)
20115 goto handle_declarator
;
20117 /* Otherwise, we must be done. */
20121 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20122 && token
->type
== CPP_OPEN_SQUARE
20123 && !cp_next_tokens_can_be_attribute_p (parser
))
20125 /* Parse an array-declarator. */
20126 tree bounds
, attrs
;
20128 if (ctor_dtor_or_conv_p
)
20129 *ctor_dtor_or_conv_p
= 0;
20133 parser
->default_arg_ok_p
= false;
20134 parser
->in_declarator_p
= true;
20135 /* Consume the `['. */
20136 cp_lexer_consume_token (parser
->lexer
);
20137 /* Peek at the next token. */
20138 token
= cp_lexer_peek_token (parser
->lexer
);
20139 /* If the next token is `]', then there is no
20140 constant-expression. */
20141 if (token
->type
!= CPP_CLOSE_SQUARE
)
20143 bool non_constant_p
;
20145 = cp_parser_constant_expression (parser
,
20146 /*allow_non_constant=*/true,
20148 if (!non_constant_p
)
20150 else if (error_operand_p (bounds
))
20151 /* Already gave an error. */;
20152 else if (!parser
->in_function_body
20153 || current_binding_level
->kind
== sk_function_parms
)
20155 /* Normally, the array bound must be an integral constant
20156 expression. However, as an extension, we allow VLAs
20157 in function scopes as long as they aren't part of a
20158 parameter declaration. */
20159 cp_parser_error (parser
,
20160 "array bound is not an integer constant");
20161 bounds
= error_mark_node
;
20163 else if (processing_template_decl
20164 && !type_dependent_expression_p (bounds
))
20166 /* Remember this wasn't a constant-expression. */
20167 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20168 TREE_SIDE_EFFECTS (bounds
) = 1;
20172 bounds
= NULL_TREE
;
20173 /* Look for the closing `]'. */
20174 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20176 declarator
= cp_error_declarator
;
20180 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20181 declarator
= make_array_declarator (declarator
, bounds
);
20182 declarator
->std_attributes
= attrs
;
20184 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20187 tree qualifying_scope
;
20188 tree unqualified_name
;
20190 special_function_kind sfk
;
20192 bool pack_expansion_p
= false;
20193 cp_token
*declarator_id_start_token
;
20195 /* Parse a declarator-id */
20196 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20199 cp_parser_parse_tentatively (parser
);
20201 /* If we see an ellipsis, we should be looking at a
20203 if (token
->type
== CPP_ELLIPSIS
)
20205 /* Consume the `...' */
20206 cp_lexer_consume_token (parser
->lexer
);
20208 pack_expansion_p
= true;
20212 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20214 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20215 qualifying_scope
= parser
->scope
;
20220 if (!unqualified_name
&& pack_expansion_p
)
20222 /* Check whether an error occurred. */
20223 okay
= !cp_parser_error_occurred (parser
);
20225 /* We already consumed the ellipsis to mark a
20226 parameter pack, but we have no way to report it,
20227 so abort the tentative parse. We will be exiting
20228 immediately anyway. */
20229 cp_parser_abort_tentative_parse (parser
);
20232 okay
= cp_parser_parse_definitely (parser
);
20235 unqualified_name
= error_mark_node
;
20236 else if (unqualified_name
20237 && (qualifying_scope
20238 || (!identifier_p (unqualified_name
))))
20240 cp_parser_error (parser
, "expected unqualified-id");
20241 unqualified_name
= error_mark_node
;
20245 if (!unqualified_name
)
20247 if (unqualified_name
== error_mark_node
)
20249 declarator
= cp_error_declarator
;
20250 pack_expansion_p
= false;
20251 declarator
->parameter_pack_p
= false;
20255 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20257 if (qualifying_scope
&& at_namespace_scope_p ()
20258 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20260 /* In the declaration of a member of a template class
20261 outside of the class itself, the SCOPE will sometimes
20262 be a TYPENAME_TYPE. For example, given:
20264 template <typename T>
20265 int S<T>::R::i = 3;
20267 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20268 this context, we must resolve S<T>::R to an ordinary
20269 type, rather than a typename type.
20271 The reason we normally avoid resolving TYPENAME_TYPEs
20272 is that a specialization of `S' might render
20273 `S<T>::R' not a type. However, if `S' is
20274 specialized, then this `i' will not be used, so there
20275 is no harm in resolving the types here. */
20278 /* Resolve the TYPENAME_TYPE. */
20279 type
= resolve_typename_type (qualifying_scope
,
20280 /*only_current_p=*/false);
20281 /* If that failed, the declarator is invalid. */
20282 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20284 if (typedef_variant_p (type
))
20285 error_at (declarator_id_start_token
->location
,
20286 "cannot define member of dependent typedef "
20289 error_at (declarator_id_start_token
->location
,
20290 "%<%T::%E%> is not a type",
20291 TYPE_CONTEXT (qualifying_scope
),
20292 TYPE_IDENTIFIER (qualifying_scope
));
20294 qualifying_scope
= type
;
20299 if (unqualified_name
)
20303 if (qualifying_scope
20304 && CLASS_TYPE_P (qualifying_scope
))
20305 class_type
= qualifying_scope
;
20307 class_type
= current_class_type
;
20309 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20311 tree name_type
= TREE_TYPE (unqualified_name
);
20313 if (!class_type
|| !same_type_p (name_type
, class_type
))
20315 /* We do not attempt to print the declarator
20316 here because we do not have enough
20317 information about its original syntactic
20319 cp_parser_error (parser
, "invalid declarator");
20320 declarator
= cp_error_declarator
;
20323 else if (qualifying_scope
20324 && CLASSTYPE_USE_TEMPLATE (name_type
))
20326 error_at (declarator_id_start_token
->location
,
20327 "invalid use of constructor as a template");
20328 inform (declarator_id_start_token
->location
,
20329 "use %<%T::%D%> instead of %<%T::%D%> to "
20330 "name the constructor in a qualified name",
20332 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20333 class_type
, name_type
);
20334 declarator
= cp_error_declarator
;
20337 unqualified_name
= constructor_name (class_type
);
20342 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20343 sfk
= sfk_destructor
;
20344 else if (identifier_p (unqualified_name
)
20345 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20346 sfk
= sfk_conversion
;
20347 else if (/* There's no way to declare a constructor
20348 for an unnamed type, even if the type
20349 got a name for linkage purposes. */
20350 !TYPE_WAS_UNNAMED (class_type
)
20351 /* Handle correctly (c++/19200):
20365 friend void N::S();
20367 && (!friend_p
|| class_type
== qualifying_scope
)
20368 && constructor_name_p (unqualified_name
,
20370 sfk
= sfk_constructor
;
20371 else if (is_overloaded_fn (unqualified_name
)
20372 && DECL_CONSTRUCTOR_P (get_first_fn
20373 (unqualified_name
)))
20374 sfk
= sfk_constructor
;
20376 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20377 *ctor_dtor_or_conv_p
= -1;
20380 declarator
= make_id_declarator (qualifying_scope
,
20383 declarator
->std_attributes
= attrs
;
20384 declarator
->id_loc
= token
->location
;
20385 declarator
->parameter_pack_p
= pack_expansion_p
;
20387 if (pack_expansion_p
)
20388 maybe_warn_variadic_templates ();
20391 handle_declarator
:;
20392 scope
= get_scope_of_declarator (declarator
);
20395 /* Any names that appear after the declarator-id for a
20396 member are looked up in the containing scope. */
20397 if (at_function_scope_p ())
20399 /* But declarations with qualified-ids can't appear in a
20401 cp_parser_error (parser
, "qualified-id in declaration");
20402 declarator
= cp_error_declarator
;
20405 pushed_scope
= push_scope (scope
);
20407 parser
->in_declarator_p
= true;
20408 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20409 || (declarator
&& declarator
->kind
== cdk_id
))
20410 /* Default args are only allowed on function
20412 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20414 parser
->default_arg_ok_p
= false;
20423 /* For an abstract declarator, we might wind up with nothing at this
20424 point. That's an error; the declarator is not optional. */
20426 cp_parser_error (parser
, "expected declarator");
20427 else if (open_paren
)
20429 /* Record overly parenthesized declarator so we can give a
20430 diagnostic about confusing decl/expr disambiguation. */
20431 if (declarator
->kind
== cdk_array
)
20433 /* If the open and close parens are on different lines, this
20434 is probably a formatting thing, so ignore. */
20435 expanded_location open
= expand_location (open_paren
->location
);
20436 expanded_location close
= expand_location (close_paren
->location
);
20437 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20441 declarator
->parenthesized
= open_paren
->location
;
20444 /* If we entered a scope, we must exit it now. */
20446 pop_scope (pushed_scope
);
20448 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20449 parser
->in_declarator_p
= saved_in_declarator_p
;
20454 /* Parse a ptr-operator.
20457 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20458 * cv-qualifier-seq [opt]
20460 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20461 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20466 & cv-qualifier-seq [opt]
20468 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20469 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20470 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20471 filled in with the TYPE containing the member. *CV_QUALS is
20472 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20473 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20474 Note that the tree codes returned by this function have nothing
20475 to do with the types of trees that will be eventually be created
20476 to represent the pointer or reference type being parsed. They are
20477 just constants with suggestive names. */
20478 static enum tree_code
20479 cp_parser_ptr_operator (cp_parser
* parser
,
20481 cp_cv_quals
*cv_quals
,
20484 enum tree_code code
= ERROR_MARK
;
20486 tree attrs
= NULL_TREE
;
20488 /* Assume that it's not a pointer-to-member. */
20490 /* And that there are no cv-qualifiers. */
20491 *cv_quals
= TYPE_UNQUALIFIED
;
20493 /* Peek at the next token. */
20494 token
= cp_lexer_peek_token (parser
->lexer
);
20496 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20497 if (token
->type
== CPP_MULT
)
20498 code
= INDIRECT_REF
;
20499 else if (token
->type
== CPP_AND
)
20501 else if ((cxx_dialect
!= cxx98
) &&
20502 token
->type
== CPP_AND_AND
) /* C++0x only */
20503 code
= NON_LVALUE_EXPR
;
20505 if (code
!= ERROR_MARK
)
20507 /* Consume the `*', `&' or `&&'. */
20508 cp_lexer_consume_token (parser
->lexer
);
20510 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20511 `&', if we are allowing GNU extensions. (The only qualifier
20512 that can legally appear after `&' is `restrict', but that is
20513 enforced during semantic analysis. */
20514 if (code
== INDIRECT_REF
20515 || cp_parser_allow_gnu_extensions_p (parser
))
20516 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20518 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20519 if (attributes
!= NULL
)
20520 *attributes
= attrs
;
20524 /* Try the pointer-to-member case. */
20525 cp_parser_parse_tentatively (parser
);
20526 /* Look for the optional `::' operator. */
20527 cp_parser_global_scope_opt (parser
,
20528 /*current_scope_valid_p=*/false);
20529 /* Look for the nested-name specifier. */
20530 token
= cp_lexer_peek_token (parser
->lexer
);
20531 cp_parser_nested_name_specifier (parser
,
20532 /*typename_keyword_p=*/false,
20533 /*check_dependency_p=*/true,
20535 /*is_declaration=*/false);
20536 /* If we found it, and the next token is a `*', then we are
20537 indeed looking at a pointer-to-member operator. */
20538 if (!cp_parser_error_occurred (parser
)
20539 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20541 /* Indicate that the `*' operator was used. */
20542 code
= INDIRECT_REF
;
20544 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20545 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20546 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20547 error_at (token
->location
, "cannot form pointer to member of "
20548 "non-class %q#T", parser
->scope
);
20551 /* The type of which the member is a member is given by the
20553 *type
= parser
->scope
;
20554 /* The next name will not be qualified. */
20555 parser
->scope
= NULL_TREE
;
20556 parser
->qualifying_scope
= NULL_TREE
;
20557 parser
->object_scope
= NULL_TREE
;
20558 /* Look for optional c++11 attributes. */
20559 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20560 if (attributes
!= NULL
)
20561 *attributes
= attrs
;
20562 /* Look for the optional cv-qualifier-seq. */
20563 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20566 /* If that didn't work we don't have a ptr-operator. */
20567 if (!cp_parser_parse_definitely (parser
))
20568 cp_parser_error (parser
, "expected ptr-operator");
20574 /* Parse an (optional) cv-qualifier-seq.
20577 cv-qualifier cv-qualifier-seq [opt]
20588 Returns a bitmask representing the cv-qualifiers. */
20591 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20593 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20598 cp_cv_quals cv_qualifier
;
20600 /* Peek at the next token. */
20601 token
= cp_lexer_peek_token (parser
->lexer
);
20602 /* See if it's a cv-qualifier. */
20603 switch (token
->keyword
)
20606 cv_qualifier
= TYPE_QUAL_CONST
;
20610 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20614 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20618 cv_qualifier
= TYPE_UNQUALIFIED
;
20625 if (cv_quals
& cv_qualifier
)
20627 gcc_rich_location
richloc (token
->location
);
20628 richloc
.add_fixit_remove ();
20629 error_at (&richloc
, "duplicate cv-qualifier");
20630 cp_lexer_purge_token (parser
->lexer
);
20634 cp_lexer_consume_token (parser
->lexer
);
20635 cv_quals
|= cv_qualifier
;
20642 /* Parse an (optional) ref-qualifier
20648 Returns cp_ref_qualifier representing ref-qualifier. */
20650 static cp_ref_qualifier
20651 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20653 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20655 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20656 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20661 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20662 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20664 switch (token
->type
)
20667 curr_ref_qual
= REF_QUAL_LVALUE
;
20671 curr_ref_qual
= REF_QUAL_RVALUE
;
20675 curr_ref_qual
= REF_QUAL_NONE
;
20679 if (!curr_ref_qual
)
20683 error_at (token
->location
, "multiple ref-qualifiers");
20684 cp_lexer_purge_token (parser
->lexer
);
20688 ref_qual
= curr_ref_qual
;
20689 cp_lexer_consume_token (parser
->lexer
);
20696 /* Parse an optional tx-qualifier.
20700 transaction_safe_dynamic */
20703 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20705 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20706 if (token
->type
== CPP_NAME
)
20708 tree name
= token
->u
.value
;
20709 const char *p
= IDENTIFIER_POINTER (name
);
20710 const int len
= strlen ("transaction_safe");
20711 if (!strncmp (p
, "transaction_safe", len
))
20715 || !strcmp (p
, "_dynamic"))
20717 cp_lexer_consume_token (parser
->lexer
);
20720 error ("%qE requires %<-fgnu-tm%>", name
);
20731 /* Parse an (optional) virt-specifier-seq.
20733 virt-specifier-seq:
20734 virt-specifier virt-specifier-seq [opt]
20740 Returns a bitmask representing the virt-specifiers. */
20742 static cp_virt_specifiers
20743 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20745 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20750 cp_virt_specifiers virt_specifier
;
20752 /* Peek at the next token. */
20753 token
= cp_lexer_peek_token (parser
->lexer
);
20754 /* See if it's a virt-specifier-qualifier. */
20755 if (token
->type
!= CPP_NAME
)
20757 if (id_equal (token
->u
.value
, "override"))
20759 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20760 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20762 else if (id_equal (token
->u
.value
, "final"))
20764 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20765 virt_specifier
= VIRT_SPEC_FINAL
;
20767 else if (id_equal (token
->u
.value
, "__final"))
20769 virt_specifier
= VIRT_SPEC_FINAL
;
20774 if (virt_specifiers
& virt_specifier
)
20776 gcc_rich_location
richloc (token
->location
);
20777 richloc
.add_fixit_remove ();
20778 error_at (&richloc
, "duplicate virt-specifier");
20779 cp_lexer_purge_token (parser
->lexer
);
20783 cp_lexer_consume_token (parser
->lexer
);
20784 virt_specifiers
|= virt_specifier
;
20787 return virt_specifiers
;
20790 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20791 is in scope even though it isn't real. */
20794 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20798 if (current_class_ptr
)
20800 /* We don't clear this between NSDMIs. Is it already what we want? */
20801 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20802 if (DECL_P (current_class_ptr
)
20803 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20804 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20805 && cp_type_quals (type
) == quals
)
20809 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20810 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20811 current_class_ptr
= NULL_TREE
;
20813 = cp_build_fold_indirect_ref (this_parm
);
20814 current_class_ptr
= this_parm
;
20817 /* Return true iff our current scope is a non-static data member
20821 parsing_nsdmi (void)
20823 /* We recognize NSDMI context by the context-less 'this' pointer set up
20824 by the function above. */
20825 if (current_class_ptr
20826 && TREE_CODE (current_class_ptr
) == PARM_DECL
20827 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20832 /* Parse a late-specified return type, if any. This is not a separate
20833 non-terminal, but part of a function declarator, which looks like
20835 -> trailing-type-specifier-seq abstract-declarator(opt)
20837 Returns the type indicated by the type-id.
20839 In addition to this, parse any queued up #pragma omp declare simd
20840 clauses, and #pragma acc routine clauses.
20842 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20846 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20847 tree
& requires_clause
, cp_cv_quals quals
)
20850 tree type
= NULL_TREE
;
20851 bool declare_simd_p
= (parser
->omp_declare_simd
20853 && declarator
->kind
== cdk_id
);
20855 bool oacc_routine_p
= (parser
->oacc_routine
20857 && declarator
->kind
== cdk_id
);
20859 /* Peek at the next token. */
20860 token
= cp_lexer_peek_token (parser
->lexer
);
20861 /* A late-specified return type is indicated by an initial '->'. */
20862 if (token
->type
!= CPP_DEREF
20863 && token
->keyword
!= RID_REQUIRES
20864 && !(token
->type
== CPP_NAME
20865 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20866 && !(declare_simd_p
|| oacc_routine_p
))
20869 tree save_ccp
= current_class_ptr
;
20870 tree save_ccr
= current_class_ref
;
20873 /* DR 1207: 'this' is in scope in the trailing return type. */
20874 inject_this_parameter (current_class_type
, quals
);
20877 if (token
->type
== CPP_DEREF
)
20879 /* Consume the ->. */
20880 cp_lexer_consume_token (parser
->lexer
);
20882 type
= cp_parser_trailing_type_id (parser
);
20885 /* Function declarations may be followed by a trailing
20886 requires-clause. */
20887 requires_clause
= cp_parser_requires_clause_opt (parser
);
20889 if (declare_simd_p
)
20890 declarator
->attributes
20891 = cp_parser_late_parsing_omp_declare_simd (parser
,
20892 declarator
->attributes
);
20893 if (oacc_routine_p
)
20894 declarator
->attributes
20895 = cp_parser_late_parsing_oacc_routine (parser
,
20896 declarator
->attributes
);
20900 current_class_ptr
= save_ccp
;
20901 current_class_ref
= save_ccr
;
20907 /* Parse a declarator-id.
20911 :: [opt] nested-name-specifier [opt] type-name
20913 In the `id-expression' case, the value returned is as for
20914 cp_parser_id_expression if the id-expression was an unqualified-id.
20915 If the id-expression was a qualified-id, then a SCOPE_REF is
20916 returned. The first operand is the scope (either a NAMESPACE_DECL
20917 or TREE_TYPE), but the second is still just a representation of an
20921 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20924 /* The expression must be an id-expression. Assume that qualified
20925 names are the names of types so that:
20928 int S<T>::R::i = 3;
20930 will work; we must treat `S<T>::R' as the name of a type.
20931 Similarly, assume that qualified names are templates, where
20935 int S<T>::R<T>::i = 3;
20938 id
= cp_parser_id_expression (parser
,
20939 /*template_keyword_p=*/false,
20940 /*check_dependency_p=*/false,
20941 /*template_p=*/NULL
,
20942 /*declarator_p=*/true,
20944 if (id
&& BASELINK_P (id
))
20945 id
= BASELINK_FUNCTIONS (id
);
20949 /* Parse a type-id.
20952 type-specifier-seq abstract-declarator [opt]
20954 Returns the TYPE specified. */
20957 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20958 bool is_trailing_return
)
20960 cp_decl_specifier_seq type_specifier_seq
;
20961 cp_declarator
*abstract_declarator
;
20963 /* Parse the type-specifier-seq. */
20964 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20965 is_trailing_return
,
20966 &type_specifier_seq
);
20967 if (is_template_arg
&& type_specifier_seq
.type
20968 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20969 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20970 /* A bare template name as a template argument is a template template
20971 argument, not a placeholder, so fail parsing it as a type argument. */
20973 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20974 cp_parser_simulate_error (parser
);
20975 return error_mark_node
;
20977 if (type_specifier_seq
.type
== error_mark_node
)
20978 return error_mark_node
;
20980 /* There might or might not be an abstract declarator. */
20981 cp_parser_parse_tentatively (parser
);
20982 /* Look for the declarator. */
20983 abstract_declarator
20984 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20985 /*parenthesized_p=*/NULL
,
20986 /*member_p=*/false,
20987 /*friend_p=*/false);
20988 /* Check to see if there really was a declarator. */
20989 if (!cp_parser_parse_definitely (parser
))
20990 abstract_declarator
= NULL
;
20992 if (type_specifier_seq
.type
20993 /* The concepts TS allows 'auto' as a type-id. */
20994 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20995 /* None of the valid uses of 'auto' in C++14 involve the type-id
20996 nonterminal, but it is valid in a trailing-return-type. */
20997 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20998 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21000 /* A type-id with type 'auto' is only ok if the abstract declarator
21001 is a function declarator with a late-specified return type.
21003 A type-id with 'auto' is also valid in a trailing-return-type
21004 in a compound-requirement. */
21005 if (abstract_declarator
21006 && abstract_declarator
->kind
== cdk_function
21007 && abstract_declarator
->u
.function
.late_return_type
)
21009 else if (parser
->in_result_type_constraint_p
)
21013 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21014 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21016 error_at (loc
, "missing template arguments after %qT",
21018 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21022 error_at (loc
, "invalid use of %qT", auto_node
);
21023 return error_mark_node
;
21027 return groktypename (&type_specifier_seq
, abstract_declarator
,
21032 cp_parser_type_id (cp_parser
*parser
)
21034 return cp_parser_type_id_1 (parser
, false, false);
21038 cp_parser_template_type_arg (cp_parser
*parser
)
21041 const char *saved_message
= parser
->type_definition_forbidden_message
;
21042 parser
->type_definition_forbidden_message
21043 = G_("types may not be defined in template arguments");
21044 r
= cp_parser_type_id_1 (parser
, true, false);
21045 parser
->type_definition_forbidden_message
= saved_message
;
21046 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21048 error ("invalid use of %<auto%> in template argument");
21049 r
= error_mark_node
;
21055 cp_parser_trailing_type_id (cp_parser
*parser
)
21057 return cp_parser_type_id_1 (parser
, false, true);
21060 /* Parse a type-specifier-seq.
21062 type-specifier-seq:
21063 type-specifier type-specifier-seq [opt]
21067 type-specifier-seq:
21068 attributes type-specifier-seq [opt]
21070 If IS_DECLARATION is true, we are at the start of a "condition" or
21071 exception-declaration, so we might be followed by a declarator-id.
21073 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21074 i.e. we've just seen "->".
21076 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21079 cp_parser_type_specifier_seq (cp_parser
* parser
,
21080 bool is_declaration
,
21081 bool is_trailing_return
,
21082 cp_decl_specifier_seq
*type_specifier_seq
)
21084 bool seen_type_specifier
= false;
21085 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21086 cp_token
*start_token
= NULL
;
21088 /* Clear the TYPE_SPECIFIER_SEQ. */
21089 clear_decl_specs (type_specifier_seq
);
21091 /* In the context of a trailing return type, enum E { } is an
21092 elaborated-type-specifier followed by a function-body, not an
21094 if (is_trailing_return
)
21095 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21097 /* Parse the type-specifiers and attributes. */
21100 tree type_specifier
;
21101 bool is_cv_qualifier
;
21103 /* Check for attributes first. */
21104 if (cp_next_tokens_can_be_attribute_p (parser
))
21106 type_specifier_seq
->attributes
21107 = attr_chainon (type_specifier_seq
->attributes
,
21108 cp_parser_attributes_opt (parser
));
21112 /* record the token of the beginning of the type specifier seq,
21113 for error reporting purposes*/
21115 start_token
= cp_lexer_peek_token (parser
->lexer
);
21117 /* Look for the type-specifier. */
21118 type_specifier
= cp_parser_type_specifier (parser
,
21120 type_specifier_seq
,
21121 /*is_declaration=*/false,
21124 if (!type_specifier
)
21126 /* If the first type-specifier could not be found, this is not a
21127 type-specifier-seq at all. */
21128 if (!seen_type_specifier
)
21130 /* Set in_declarator_p to avoid skipping to the semicolon. */
21131 int in_decl
= parser
->in_declarator_p
;
21132 parser
->in_declarator_p
= true;
21134 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21135 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21136 cp_parser_error (parser
, "expected type-specifier");
21138 parser
->in_declarator_p
= in_decl
;
21140 type_specifier_seq
->type
= error_mark_node
;
21143 /* If subsequent type-specifiers could not be found, the
21144 type-specifier-seq is complete. */
21148 seen_type_specifier
= true;
21149 /* The standard says that a condition can be:
21151 type-specifier-seq declarator = assignment-expression
21158 we should treat the "S" as a declarator, not as a
21159 type-specifier. The standard doesn't say that explicitly for
21160 type-specifier-seq, but it does say that for
21161 decl-specifier-seq in an ordinary declaration. Perhaps it
21162 would be clearer just to allow a decl-specifier-seq here, and
21163 then add a semantic restriction that if any decl-specifiers
21164 that are not type-specifiers appear, the program is invalid. */
21165 if (is_declaration
&& !is_cv_qualifier
)
21166 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21170 /* Return whether the function currently being declared has an associated
21171 template parameter list. */
21174 function_being_declared_is_template_p (cp_parser
* parser
)
21176 if (!current_template_parms
|| processing_template_parmlist
)
21179 if (parser
->implicit_template_scope
)
21182 if (at_class_scope_p ()
21183 && TYPE_BEING_DEFINED (current_class_type
))
21184 return parser
->num_template_parameter_lists
!= 0;
21186 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21187 (current_class_type
));
21190 /* Parse a parameter-declaration-clause.
21192 parameter-declaration-clause:
21193 parameter-declaration-list [opt] ... [opt]
21194 parameter-declaration-list , ...
21196 Returns a representation for the parameter declarations. A return
21197 value of NULL indicates a parameter-declaration-clause consisting
21198 only of an ellipsis. */
21201 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21208 temp_override
<bool> cleanup
21209 (parser
->auto_is_implicit_function_template_parm_p
);
21211 if (!processing_specialization
21212 && !processing_template_parmlist
21213 && !processing_explicit_instantiation
21214 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21215 actual function or a random abstract declarator. */
21216 && parser
->default_arg_ok_p
)
21217 if (!current_function_decl
21218 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21219 parser
->auto_is_implicit_function_template_parm_p
= true;
21221 /* Peek at the next token. */
21222 token
= cp_lexer_peek_token (parser
->lexer
);
21223 /* Check for trivial parameter-declaration-clauses. */
21224 if (token
->type
== CPP_ELLIPSIS
)
21226 /* Consume the `...' token. */
21227 cp_lexer_consume_token (parser
->lexer
);
21230 else if (token
->type
== CPP_CLOSE_PAREN
)
21231 /* There are no parameters. */
21233 #ifndef NO_IMPLICIT_EXTERN_C
21234 if (in_system_header_at (input_location
)
21235 && current_class_type
== NULL
21236 && current_lang_name
== lang_name_c
)
21240 return void_list_node
;
21242 /* Check for `(void)', too, which is a special case. */
21243 else if (token
->keyword
== RID_VOID
21244 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21245 == CPP_CLOSE_PAREN
))
21247 /* Consume the `void' token. */
21248 cp_lexer_consume_token (parser
->lexer
);
21249 /* There are no parameters. */
21250 return void_list_node
;
21253 /* Parse the parameter-declaration-list. */
21254 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21255 /* If a parse error occurred while parsing the
21256 parameter-declaration-list, then the entire
21257 parameter-declaration-clause is erroneous. */
21261 /* Peek at the next token. */
21262 token
= cp_lexer_peek_token (parser
->lexer
);
21263 /* If it's a `,', the clause should terminate with an ellipsis. */
21264 if (token
->type
== CPP_COMMA
)
21266 /* Consume the `,'. */
21267 cp_lexer_consume_token (parser
->lexer
);
21268 /* Expect an ellipsis. */
21270 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21272 /* It might also be `...' if the optional trailing `,' was
21274 else if (token
->type
== CPP_ELLIPSIS
)
21276 /* Consume the `...' token. */
21277 cp_lexer_consume_token (parser
->lexer
);
21278 /* And remember that we saw it. */
21282 ellipsis_p
= false;
21284 /* Finish the parameter list. */
21286 parameters
= chainon (parameters
, void_list_node
);
21291 /* Parse a parameter-declaration-list.
21293 parameter-declaration-list:
21294 parameter-declaration
21295 parameter-declaration-list , parameter-declaration
21297 Returns a representation of the parameter-declaration-list, as for
21298 cp_parser_parameter_declaration_clause. However, the
21299 `void_list_node' is never appended to the list. Upon return,
21300 *IS_ERROR will be true iff an error occurred. */
21303 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21305 tree parameters
= NULL_TREE
;
21306 tree
*tail
= ¶meters
;
21307 bool saved_in_unbraced_linkage_specification_p
;
21310 /* Assume all will go well. */
21312 /* The special considerations that apply to a function within an
21313 unbraced linkage specifications do not apply to the parameters
21314 to the function. */
21315 saved_in_unbraced_linkage_specification_p
21316 = parser
->in_unbraced_linkage_specification_p
;
21317 parser
->in_unbraced_linkage_specification_p
= false;
21319 /* Look for more parameters. */
21322 cp_parameter_declarator
*parameter
;
21323 tree decl
= error_mark_node
;
21324 bool parenthesized_p
= false;
21326 /* Parse the parameter. */
21328 = cp_parser_parameter_declaration (parser
,
21329 /*template_parm_p=*/false,
21332 /* We don't know yet if the enclosing context is deprecated, so wait
21333 and warn in grokparms if appropriate. */
21334 deprecated_state
= DEPRECATED_SUPPRESS
;
21338 decl
= grokdeclarator (parameter
->declarator
,
21339 ¶meter
->decl_specifiers
,
21341 parameter
->default_argument
!= NULL_TREE
,
21342 ¶meter
->decl_specifiers
.attributes
);
21343 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21344 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21347 deprecated_state
= DEPRECATED_NORMAL
;
21349 /* If a parse error occurred parsing the parameter declaration,
21350 then the entire parameter-declaration-list is erroneous. */
21351 if (decl
== error_mark_node
)
21354 parameters
= error_mark_node
;
21358 if (parameter
->decl_specifiers
.attributes
)
21359 cplus_decl_attributes (&decl
,
21360 parameter
->decl_specifiers
.attributes
,
21362 if (DECL_NAME (decl
))
21363 decl
= pushdecl (decl
);
21365 if (decl
!= error_mark_node
)
21367 retrofit_lang_decl (decl
);
21368 DECL_PARM_INDEX (decl
) = ++index
;
21369 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21372 /* Add the new parameter to the list. */
21373 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21374 tail
= &TREE_CHAIN (*tail
);
21376 /* Peek at the next token. */
21377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21378 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21379 /* These are for Objective-C++ */
21380 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21381 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21382 /* The parameter-declaration-list is complete. */
21384 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21388 /* Peek at the next token. */
21389 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21390 /* If it's an ellipsis, then the list is complete. */
21391 if (token
->type
== CPP_ELLIPSIS
)
21393 /* Otherwise, there must be more parameters. Consume the
21395 cp_lexer_consume_token (parser
->lexer
);
21396 /* When parsing something like:
21398 int i(float f, double d)
21400 we can tell after seeing the declaration for "f" that we
21401 are not looking at an initialization of a variable "i",
21402 but rather at the declaration of a function "i".
21404 Due to the fact that the parsing of template arguments
21405 (as specified to a template-id) requires backtracking we
21406 cannot use this technique when inside a template argument
21408 if (!parser
->in_template_argument_list_p
21409 && !parser
->in_type_id_in_expr_p
21410 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21411 /* However, a parameter-declaration of the form
21412 "float(f)" (which is a valid declaration of a
21413 parameter "f") can also be interpreted as an
21414 expression (the conversion of "f" to "float"). */
21415 && !parenthesized_p
)
21416 cp_parser_commit_to_tentative_parse (parser
);
21420 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21421 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21422 cp_parser_skip_to_closing_parenthesis (parser
,
21423 /*recovering=*/true,
21424 /*or_comma=*/false,
21425 /*consume_paren=*/false);
21430 parser
->in_unbraced_linkage_specification_p
21431 = saved_in_unbraced_linkage_specification_p
;
21433 /* Reset implicit_template_scope if we are about to leave the function
21434 parameter list that introduced it. Note that for out-of-line member
21435 definitions, there will be one or more class scopes before we get to
21436 the template parameter scope. */
21438 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21439 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21441 while (maybe_its
->kind
== sk_class
)
21442 maybe_its
= maybe_its
->level_chain
;
21443 if (maybe_its
== its
)
21445 parser
->implicit_template_parms
= 0;
21446 parser
->implicit_template_scope
= 0;
21453 /* Parse a parameter declaration.
21455 parameter-declaration:
21456 decl-specifier-seq ... [opt] declarator
21457 decl-specifier-seq declarator = assignment-expression
21458 decl-specifier-seq ... [opt] abstract-declarator [opt]
21459 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21461 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21462 declares a template parameter. (In that case, a non-nested `>'
21463 token encountered during the parsing of the assignment-expression
21464 is not interpreted as a greater-than operator.)
21466 Returns a representation of the parameter, or NULL if an error
21467 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21468 true iff the declarator is of the form "(p)". */
21470 static cp_parameter_declarator
*
21471 cp_parser_parameter_declaration (cp_parser
*parser
,
21472 bool template_parm_p
,
21473 bool *parenthesized_p
)
21475 int declares_class_or_enum
;
21476 cp_decl_specifier_seq decl_specifiers
;
21477 cp_declarator
*declarator
;
21478 tree default_argument
;
21479 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21480 const char *saved_message
;
21481 bool template_parameter_pack_p
= false;
21483 /* In a template parameter, `>' is not an operator.
21487 When parsing a default template-argument for a non-type
21488 template-parameter, the first non-nested `>' is taken as the end
21489 of the template parameter-list rather than a greater-than
21492 /* Type definitions may not appear in parameter types. */
21493 saved_message
= parser
->type_definition_forbidden_message
;
21494 parser
->type_definition_forbidden_message
21495 = G_("types may not be defined in parameter types");
21497 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21498 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21499 (current_template_parms
)) : 0);
21501 /* Parse the declaration-specifiers. */
21502 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21503 cp_parser_decl_specifier_seq (parser
,
21504 CP_PARSER_FLAGS_NONE
,
21506 &declares_class_or_enum
);
21508 /* Complain about missing 'typename' or other invalid type names. */
21509 if (!decl_specifiers
.any_type_specifiers_p
21510 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21511 decl_specifiers
.type
= error_mark_node
;
21513 /* If an error occurred, there's no reason to attempt to parse the
21514 rest of the declaration. */
21515 if (cp_parser_error_occurred (parser
))
21517 parser
->type_definition_forbidden_message
= saved_message
;
21521 /* Peek at the next token. */
21522 token
= cp_lexer_peek_token (parser
->lexer
);
21524 /* If the next token is a `)', `,', `=', `>', or `...', then there
21525 is no declarator. However, when variadic templates are enabled,
21526 there may be a declarator following `...'. */
21527 if (token
->type
== CPP_CLOSE_PAREN
21528 || token
->type
== CPP_COMMA
21529 || token
->type
== CPP_EQ
21530 || token
->type
== CPP_GREATER
)
21533 if (parenthesized_p
)
21534 *parenthesized_p
= false;
21536 /* Otherwise, there should be a declarator. */
21539 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21540 parser
->default_arg_ok_p
= false;
21542 /* After seeing a decl-specifier-seq, if the next token is not a
21543 "(", there is no possibility that the code is a valid
21544 expression. Therefore, if parsing tentatively, we commit at
21546 if (!parser
->in_template_argument_list_p
21547 /* In an expression context, having seen:
21551 we cannot be sure whether we are looking at a
21552 function-type (taking a "char" as a parameter) or a cast
21553 of some object of type "char" to "int". */
21554 && !parser
->in_type_id_in_expr_p
21555 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21556 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21557 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21558 cp_parser_commit_to_tentative_parse (parser
);
21559 /* Parse the declarator. */
21560 declarator_token_start
= token
;
21561 declarator
= cp_parser_declarator (parser
,
21562 CP_PARSER_DECLARATOR_EITHER
,
21563 /*ctor_dtor_or_conv_p=*/NULL
,
21565 /*member_p=*/false,
21566 /*friend_p=*/false);
21567 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21568 /* After the declarator, allow more attributes. */
21569 decl_specifiers
.attributes
21570 = attr_chainon (decl_specifiers
.attributes
,
21571 cp_parser_attributes_opt (parser
));
21573 /* If the declarator is a template parameter pack, remember that and
21574 clear the flag in the declarator itself so we don't get errors
21575 from grokdeclarator. */
21576 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21578 declarator
->parameter_pack_p
= false;
21579 template_parameter_pack_p
= true;
21583 /* If the next token is an ellipsis, and we have not seen a declarator
21584 name, and if either the type of the declarator contains parameter
21585 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21586 for, eg, abbreviated integral type names), then we actually have a
21587 parameter pack expansion expression. Otherwise, leave the ellipsis
21588 for a C-style variadic function. */
21589 token
= cp_lexer_peek_token (parser
->lexer
);
21591 /* If a function parameter pack was specified and an implicit template
21592 parameter was introduced during cp_parser_parameter_declaration,
21593 change any implicit parameters introduced into packs. */
21594 if (parser
->implicit_template_parms
21595 && (token
->type
== CPP_ELLIPSIS
21596 || (declarator
&& declarator
->parameter_pack_p
)))
21598 int latest_template_parm_idx
= TREE_VEC_LENGTH
21599 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21601 if (latest_template_parm_idx
!= template_parm_idx
)
21602 decl_specifiers
.type
= convert_generic_types_to_packs
21603 (decl_specifiers
.type
,
21604 template_parm_idx
, latest_template_parm_idx
);
21607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21609 tree type
= decl_specifiers
.type
;
21611 if (type
&& DECL_P (type
))
21612 type
= TREE_TYPE (type
);
21615 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21616 && (template_parm_p
|| uses_parameter_packs (type
)))
21617 || (!type
&& template_parm_p
))
21618 && declarator_can_be_parameter_pack (declarator
))
21620 /* Consume the `...'. */
21621 cp_lexer_consume_token (parser
->lexer
);
21622 maybe_warn_variadic_templates ();
21624 /* Build a pack expansion type */
21625 if (template_parm_p
)
21626 template_parameter_pack_p
= true;
21627 else if (declarator
)
21628 declarator
->parameter_pack_p
= true;
21630 decl_specifiers
.type
= make_pack_expansion (type
);
21634 /* The restriction on defining new types applies only to the type
21635 of the parameter, not to the default argument. */
21636 parser
->type_definition_forbidden_message
= saved_message
;
21638 /* If the next token is `=', then process a default argument. */
21639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21641 tree type
= decl_specifiers
.type
;
21642 token
= cp_lexer_peek_token (parser
->lexer
);
21643 /* If we are defining a class, then the tokens that make up the
21644 default argument must be saved and processed later. */
21645 if (!template_parm_p
&& at_class_scope_p ()
21646 && TYPE_BEING_DEFINED (current_class_type
)
21647 && !LAMBDA_TYPE_P (current_class_type
))
21648 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21650 // A constrained-type-specifier may declare a type template-parameter.
21651 else if (declares_constrained_type_template_parameter (type
))
21653 = cp_parser_default_type_template_argument (parser
);
21655 // A constrained-type-specifier may declare a template-template-parameter.
21656 else if (declares_constrained_template_template_parameter (type
))
21658 = cp_parser_default_template_template_argument (parser
);
21660 /* Outside of a class definition, we can just parse the
21661 assignment-expression. */
21664 = cp_parser_default_argument (parser
, template_parm_p
);
21666 if (!parser
->default_arg_ok_p
)
21668 permerror (token
->location
,
21669 "default arguments are only "
21670 "permitted for function parameters");
21672 else if ((declarator
&& declarator
->parameter_pack_p
)
21673 || template_parameter_pack_p
21674 || (decl_specifiers
.type
21675 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21677 /* Find the name of the parameter pack. */
21678 cp_declarator
*id_declarator
= declarator
;
21679 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21680 id_declarator
= id_declarator
->declarator
;
21682 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21683 error_at (declarator_token_start
->location
,
21685 ? G_("template parameter pack %qD "
21686 "cannot have a default argument")
21687 : G_("parameter pack %qD cannot have "
21688 "a default argument"),
21689 id_declarator
->u
.id
.unqualified_name
);
21691 error_at (declarator_token_start
->location
,
21693 ? G_("template parameter pack cannot have "
21694 "a default argument")
21695 : G_("parameter pack cannot have a "
21696 "default argument"));
21698 default_argument
= NULL_TREE
;
21702 default_argument
= NULL_TREE
;
21704 /* Generate a location for the parameter, ranging from the start of the
21705 initial token to the end of the final token (using input_location for
21706 the latter, set up by cp_lexer_set_source_position_from_token when
21709 If we have a identifier, then use it for the caret location, e.g.
21711 extern int callee (int one, int (*two)(int, int), float three);
21712 ~~~~~~^~~~~~~~~~~~~~
21714 otherwise, reuse the start location for the caret location e.g.:
21716 extern int callee (int one, int (*)(int, int), float three);
21720 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21721 ? declarator
->id_loc
21722 : decl_spec_token_start
->location
);
21723 location_t param_loc
= make_location (caret_loc
,
21724 decl_spec_token_start
->location
,
21727 return make_parameter_declarator (&decl_specifiers
,
21731 template_parameter_pack_p
);
21734 /* Parse a default argument and return it.
21736 TEMPLATE_PARM_P is true if this is a default argument for a
21737 non-type template parameter. */
21739 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21741 tree default_argument
= NULL_TREE
;
21742 bool saved_greater_than_is_operator_p
;
21743 bool saved_local_variables_forbidden_p
;
21744 bool non_constant_p
, is_direct_init
;
21746 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21748 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21749 parser
->greater_than_is_operator_p
= !template_parm_p
;
21750 /* Local variable names (and the `this' keyword) may not
21751 appear in a default argument. */
21752 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21753 parser
->local_variables_forbidden_p
= true;
21754 /* Parse the assignment-expression. */
21755 if (template_parm_p
)
21756 push_deferring_access_checks (dk_no_deferred
);
21757 tree saved_class_ptr
= NULL_TREE
;
21758 tree saved_class_ref
= NULL_TREE
;
21759 /* The "this" pointer is not valid in a default argument. */
21762 saved_class_ptr
= current_class_ptr
;
21763 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21764 saved_class_ref
= current_class_ref
;
21765 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21768 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21769 /* Restore the "this" pointer. */
21772 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21773 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21775 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21776 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21777 if (template_parm_p
)
21778 pop_deferring_access_checks ();
21779 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21780 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21782 return default_argument
;
21785 /* Parse a function-body.
21788 compound_statement */
21791 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21793 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21794 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21798 /* Parse a ctor-initializer-opt followed by a function-body. Return
21799 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21800 is true we are parsing a function-try-block. */
21803 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21804 bool in_function_try_block
)
21807 const bool check_body_p
=
21808 DECL_CONSTRUCTOR_P (current_function_decl
)
21809 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21812 /* Begin the function body. */
21813 body
= begin_function_body ();
21814 /* Parse the optional ctor-initializer. */
21815 cp_parser_ctor_initializer_opt (parser
);
21817 /* If we're parsing a constexpr constructor definition, we need
21818 to check that the constructor body is indeed empty. However,
21819 before we get to cp_parser_function_body lot of junk has been
21820 generated, so we can't just check that we have an empty block.
21821 Rather we take a snapshot of the outermost block, and check whether
21822 cp_parser_function_body changed its state. */
21825 list
= cur_stmt_list
;
21826 if (STATEMENT_LIST_TAIL (list
))
21827 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21829 /* Parse the function-body. */
21830 cp_parser_function_body (parser
, in_function_try_block
);
21832 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21833 /* Finish the function body. */
21834 finish_function_body (body
);
21837 /* Parse an initializer.
21840 = initializer-clause
21841 ( expression-list )
21843 Returns an expression representing the initializer. If no
21844 initializer is present, NULL_TREE is returned.
21846 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21847 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21848 set to TRUE if there is no initializer present. If there is an
21849 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21850 is set to true; otherwise it is set to false. */
21853 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21854 bool* non_constant_p
)
21859 /* Peek at the next token. */
21860 token
= cp_lexer_peek_token (parser
->lexer
);
21862 /* Let our caller know whether or not this initializer was
21864 *is_direct_init
= (token
->type
!= CPP_EQ
);
21865 /* Assume that the initializer is constant. */
21866 *non_constant_p
= false;
21868 if (token
->type
== CPP_EQ
)
21870 /* Consume the `='. */
21871 cp_lexer_consume_token (parser
->lexer
);
21872 /* Parse the initializer-clause. */
21873 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21875 else if (token
->type
== CPP_OPEN_PAREN
)
21877 vec
<tree
, va_gc
> *vec
;
21878 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21880 /*allow_expansion_p=*/true,
21883 return error_mark_node
;
21884 init
= build_tree_list_vec (vec
);
21885 release_tree_vector (vec
);
21887 else if (token
->type
== CPP_OPEN_BRACE
)
21889 cp_lexer_set_source_position (parser
->lexer
);
21890 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21891 init
= cp_parser_braced_list (parser
, non_constant_p
);
21892 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21896 /* Anything else is an error. */
21897 cp_parser_error (parser
, "expected initializer");
21898 init
= error_mark_node
;
21901 if (check_for_bare_parameter_packs (init
))
21902 init
= error_mark_node
;
21907 /* Parse an initializer-clause.
21909 initializer-clause:
21910 assignment-expression
21913 Returns an expression representing the initializer.
21915 If the `assignment-expression' production is used the value
21916 returned is simply a representation for the expression.
21918 Otherwise, calls cp_parser_braced_list. */
21921 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21923 cp_expr initializer
;
21925 /* Assume the expression is constant. */
21926 *non_constant_p
= false;
21928 /* If it is not a `{', then we are looking at an
21929 assignment-expression. */
21930 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21933 = cp_parser_constant_expression (parser
,
21934 /*allow_non_constant_p=*/true,
21938 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21940 return initializer
;
21943 /* Parse a brace-enclosed initializer list.
21946 { initializer-list , [opt] }
21947 { designated-initializer-list , [opt] }
21950 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21951 the elements of the initializer-list (or NULL, if the last
21952 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21953 NULL_TREE. There is no way to detect whether or not the optional
21954 trailing `,' was provided. NON_CONSTANT_P is as for
21955 cp_parser_initializer. */
21958 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21961 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21963 /* Consume the `{' token. */
21964 matching_braces braces
;
21965 braces
.require_open (parser
);
21966 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21967 initializer
= make_node (CONSTRUCTOR
);
21968 /* If it's not a `}', then there is a non-trivial initializer. */
21969 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21971 /* Parse the initializer list. */
21972 CONSTRUCTOR_ELTS (initializer
)
21973 = cp_parser_initializer_list (parser
, non_constant_p
);
21974 /* A trailing `,' token is allowed. */
21975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21976 cp_lexer_consume_token (parser
->lexer
);
21979 *non_constant_p
= false;
21980 /* Now, there should be a trailing `}'. */
21981 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21982 braces
.require_close (parser
);
21983 TREE_TYPE (initializer
) = init_list_type_node
;
21985 cp_expr
result (initializer
);
21986 /* Build a location of the form:
21989 with caret==start at the open brace, finish at the close brace. */
21990 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21991 result
.set_location (combined_loc
);
21995 /* Consume tokens up to, and including, the next non-nested closing `]'.
21996 Returns true iff we found a closing `]'. */
21999 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22001 unsigned square_depth
= 0;
22005 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22007 switch (token
->type
)
22010 case CPP_PRAGMA_EOL
:
22011 /* If we've run out of tokens, then there is no closing `]'. */
22014 case CPP_OPEN_SQUARE
:
22018 case CPP_CLOSE_SQUARE
:
22019 if (!square_depth
--)
22021 cp_lexer_consume_token (parser
->lexer
);
22030 /* Consume the token. */
22031 cp_lexer_consume_token (parser
->lexer
);
22035 /* Return true if we are looking at an array-designator, false otherwise. */
22038 cp_parser_array_designator_p (cp_parser
*parser
)
22040 /* Consume the `['. */
22041 cp_lexer_consume_token (parser
->lexer
);
22043 cp_lexer_save_tokens (parser
->lexer
);
22045 /* Skip tokens until the next token is a closing square bracket.
22046 If we find the closing `]', and the next token is a `=', then
22047 we are looking at an array designator. */
22048 bool array_designator_p
22049 = (cp_parser_skip_to_closing_square_bracket (parser
)
22050 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22052 /* Roll back the tokens we skipped. */
22053 cp_lexer_rollback_tokens (parser
->lexer
);
22055 return array_designator_p
;
22058 /* Parse an initializer-list.
22061 initializer-clause ... [opt]
22062 initializer-list , initializer-clause ... [opt]
22066 designated-initializer-list:
22067 designated-initializer-clause
22068 designated-initializer-list , designated-initializer-clause
22070 designated-initializer-clause:
22071 designator brace-or-equal-initializer
22079 designation initializer-clause ...[opt]
22080 initializer-list , designation initializer-clause ...[opt]
22085 [ constant-expression ] =
22087 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22088 for the initializer. If the INDEX of the elt is non-NULL, it is the
22089 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22090 as for cp_parser_initializer. */
22092 static vec
<constructor_elt
, va_gc
> *
22093 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22095 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22096 bool first_p
= true;
22097 tree first_designator
= NULL_TREE
;
22099 /* Assume all of the expressions are constant. */
22100 *non_constant_p
= false;
22102 /* Parse the rest of the list. */
22108 bool clause_non_constant_p
;
22109 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22111 /* Handle the C++2A syntax, '. id ='. */
22112 if ((cxx_dialect
>= cxx2a
22113 || cp_parser_allow_gnu_extensions_p (parser
))
22114 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22115 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22116 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22117 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22118 == CPP_OPEN_BRACE
)))
22120 if (cxx_dialect
< cxx2a
)
22121 pedwarn (loc
, OPT_Wpedantic
,
22122 "C++ designated initializers only available with "
22123 "-std=c++2a or -std=gnu++2a");
22124 /* Consume the `.'. */
22125 cp_lexer_consume_token (parser
->lexer
);
22126 /* Consume the identifier. */
22127 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22128 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22129 /* Consume the `='. */
22130 cp_lexer_consume_token (parser
->lexer
);
22132 /* Also, if the next token is an identifier and the following one is a
22133 colon, we are looking at the GNU designated-initializer
22135 else if (cp_parser_allow_gnu_extensions_p (parser
)
22136 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22137 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22140 /* Warn the user that they are using an extension. */
22141 pedwarn (loc
, OPT_Wpedantic
,
22142 "ISO C++ does not allow GNU designated initializers");
22143 /* Consume the identifier. */
22144 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22145 /* Consume the `:'. */
22146 cp_lexer_consume_token (parser
->lexer
);
22148 /* Also handle C99 array designators, '[ const ] ='. */
22149 else if (cp_parser_allow_gnu_extensions_p (parser
)
22150 && !c_dialect_objc ()
22151 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22153 /* In C++11, [ could start a lambda-introducer. */
22154 bool non_const
= false;
22156 cp_parser_parse_tentatively (parser
);
22158 if (!cp_parser_array_designator_p (parser
))
22160 cp_parser_simulate_error (parser
);
22161 designator
= NULL_TREE
;
22165 designator
= cp_parser_constant_expression (parser
, true,
22167 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22168 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22171 if (!cp_parser_parse_definitely (parser
))
22172 designator
= NULL_TREE
;
22174 && (!require_potential_rvalue_constant_expression
22176 designator
= NULL_TREE
;
22178 /* Warn the user that they are using an extension. */
22179 pedwarn (loc
, OPT_Wpedantic
,
22180 "ISO C++ does not allow C99 designated initializers");
22183 designator
= NULL_TREE
;
22187 first_designator
= designator
;
22190 else if (cxx_dialect
>= cxx2a
22191 && first_designator
!= error_mark_node
22192 && (!first_designator
!= !designator
))
22194 error_at (loc
, "either all initializer clauses should be designated "
22195 "or none of them should be");
22196 first_designator
= error_mark_node
;
22198 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22199 first_designator
= designator
;
22201 /* Parse the initializer. */
22202 initializer
= cp_parser_initializer_clause (parser
,
22203 &clause_non_constant_p
);
22204 /* If any clause is non-constant, so is the entire initializer. */
22205 if (clause_non_constant_p
)
22206 *non_constant_p
= true;
22208 /* If we have an ellipsis, this is an initializer pack
22210 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22212 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22214 /* Consume the `...'. */
22215 cp_lexer_consume_token (parser
->lexer
);
22217 if (designator
&& cxx_dialect
>= cxx2a
)
22219 "%<...%> not allowed in designated initializer list");
22221 /* Turn the initializer into an initializer expansion. */
22222 initializer
= make_pack_expansion (initializer
);
22225 /* Add it to the vector. */
22226 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22228 /* If the next token is not a comma, we have reached the end of
22230 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22233 /* Peek at the next token. */
22234 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22235 /* If the next token is a `}', then we're still done. An
22236 initializer-clause can have a trailing `,' after the
22237 initializer-list and before the closing `}'. */
22238 if (token
->type
== CPP_CLOSE_BRACE
)
22241 /* Consume the `,' token. */
22242 cp_lexer_consume_token (parser
->lexer
);
22245 /* The same identifier shall not appear in multiple designators
22246 of a designated-initializer-list. */
22247 if (first_designator
)
22250 tree designator
, val
;
22251 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22252 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22254 if (IDENTIFIER_MARKED (designator
))
22256 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22257 "%<.%s%> designator used multiple times in "
22258 "the same initializer list",
22259 IDENTIFIER_POINTER (designator
));
22260 (*v
)[i
].index
= NULL_TREE
;
22263 IDENTIFIER_MARKED (designator
) = 1;
22265 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22266 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22267 IDENTIFIER_MARKED (designator
) = 0;
22273 /* Classes [gram.class] */
22275 /* Parse a class-name.
22281 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22282 to indicate that names looked up in dependent types should be
22283 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22284 keyword has been used to indicate that the name that appears next
22285 is a template. TAG_TYPE indicates the explicit tag given before
22286 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22287 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22288 is the class being defined in a class-head. If ENUM_OK is TRUE,
22289 enum-names are also accepted.
22291 Returns the TYPE_DECL representing the class. */
22294 cp_parser_class_name (cp_parser
*parser
,
22295 bool typename_keyword_p
,
22296 bool template_keyword_p
,
22297 enum tag_types tag_type
,
22298 bool check_dependency_p
,
22300 bool is_declaration
,
22307 tree identifier
= NULL_TREE
;
22309 /* All class-names start with an identifier. */
22310 token
= cp_lexer_peek_token (parser
->lexer
);
22311 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22313 cp_parser_error (parser
, "expected class-name");
22314 return error_mark_node
;
22317 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22318 to a template-id, so we save it here. */
22319 scope
= parser
->scope
;
22320 if (scope
== error_mark_node
)
22321 return error_mark_node
;
22323 /* Any name names a type if we're following the `typename' keyword
22324 in a qualified name where the enclosing scope is type-dependent. */
22325 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22326 && dependent_type_p (scope
));
22327 /* Handle the common case (an identifier, but not a template-id)
22329 if (token
->type
== CPP_NAME
22330 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22332 cp_token
*identifier_token
;
22335 /* Look for the identifier. */
22336 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22337 ambiguous_p
= identifier_token
->error_reported
;
22338 identifier
= cp_parser_identifier (parser
);
22339 /* If the next token isn't an identifier, we are certainly not
22340 looking at a class-name. */
22341 if (identifier
== error_mark_node
)
22342 decl
= error_mark_node
;
22343 /* If we know this is a type-name, there's no need to look it
22345 else if (typename_p
)
22349 tree ambiguous_decls
;
22350 /* If we already know that this lookup is ambiguous, then
22351 we've already issued an error message; there's no reason
22355 cp_parser_simulate_error (parser
);
22356 return error_mark_node
;
22358 /* If the next token is a `::', then the name must be a type
22361 [basic.lookup.qual]
22363 During the lookup for a name preceding the :: scope
22364 resolution operator, object, function, and enumerator
22365 names are ignored. */
22366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22367 tag_type
= scope_type
;
22368 /* Look up the name. */
22369 decl
= cp_parser_lookup_name (parser
, identifier
,
22371 /*is_template=*/false,
22372 /*is_namespace=*/false,
22373 check_dependency_p
,
22375 identifier_token
->location
);
22376 if (ambiguous_decls
)
22378 if (cp_parser_parsing_tentatively (parser
))
22379 cp_parser_simulate_error (parser
);
22380 return error_mark_node
;
22386 /* Try a template-id. */
22387 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22388 check_dependency_p
,
22391 if (decl
== error_mark_node
)
22392 return error_mark_node
;
22395 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22397 /* If this is a typename, create a TYPENAME_TYPE. */
22398 if (typename_p
&& decl
!= error_mark_node
)
22400 decl
= make_typename_type (scope
, decl
, typename_type
,
22401 /*complain=*/tf_error
);
22402 if (decl
!= error_mark_node
)
22403 decl
= TYPE_NAME (decl
);
22406 decl
= strip_using_decl (decl
);
22408 /* Check to see that it is really the name of a class. */
22409 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22410 && identifier_p (TREE_OPERAND (decl
, 0))
22411 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22412 /* Situations like this:
22414 template <typename T> struct A {
22415 typename T::template X<int>::I i;
22418 are problematic. Is `T::template X<int>' a class-name? The
22419 standard does not seem to be definitive, but there is no other
22420 valid interpretation of the following `::'. Therefore, those
22421 names are considered class-names. */
22423 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22424 if (decl
!= error_mark_node
)
22425 decl
= TYPE_NAME (decl
);
22427 else if (TREE_CODE (decl
) != TYPE_DECL
22428 || TREE_TYPE (decl
) == error_mark_node
22429 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22430 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22431 /* In Objective-C 2.0, a classname followed by '.' starts a
22432 dot-syntax expression, and it's not a type-name. */
22433 || (c_dialect_objc ()
22434 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22435 && objc_is_class_name (decl
)))
22436 decl
= error_mark_node
;
22438 if (decl
== error_mark_node
)
22439 cp_parser_error (parser
, "expected class-name");
22440 else if (identifier
&& !parser
->scope
)
22441 maybe_note_name_used_in_class (identifier
, decl
);
22446 /* Parse a class-specifier.
22449 class-head { member-specification [opt] }
22451 Returns the TREE_TYPE representing the class. */
22454 cp_parser_class_specifier_1 (cp_parser
* parser
)
22457 tree attributes
= NULL_TREE
;
22458 bool nested_name_specifier_p
;
22459 unsigned saved_num_template_parameter_lists
;
22460 bool saved_in_function_body
;
22461 unsigned char in_statement
;
22462 bool in_switch_statement_p
;
22463 bool saved_in_unbraced_linkage_specification_p
;
22464 tree old_scope
= NULL_TREE
;
22465 tree scope
= NULL_TREE
;
22466 cp_token
*closing_brace
;
22468 push_deferring_access_checks (dk_no_deferred
);
22470 /* Parse the class-head. */
22471 type
= cp_parser_class_head (parser
,
22472 &nested_name_specifier_p
);
22473 /* If the class-head was a semantic disaster, skip the entire body
22477 cp_parser_skip_to_end_of_block_or_statement (parser
);
22478 pop_deferring_access_checks ();
22479 return error_mark_node
;
22482 /* Look for the `{'. */
22483 matching_braces braces
;
22484 if (!braces
.require_open (parser
))
22486 pop_deferring_access_checks ();
22487 return error_mark_node
;
22490 cp_ensure_no_omp_declare_simd (parser
);
22491 cp_ensure_no_oacc_routine (parser
);
22493 /* Issue an error message if type-definitions are forbidden here. */
22494 cp_parser_check_type_definition (parser
);
22495 /* Remember that we are defining one more class. */
22496 ++parser
->num_classes_being_defined
;
22497 /* Inside the class, surrounding template-parameter-lists do not
22499 saved_num_template_parameter_lists
22500 = parser
->num_template_parameter_lists
;
22501 parser
->num_template_parameter_lists
= 0;
22502 /* We are not in a function body. */
22503 saved_in_function_body
= parser
->in_function_body
;
22504 parser
->in_function_body
= false;
22505 /* Or in a loop. */
22506 in_statement
= parser
->in_statement
;
22507 parser
->in_statement
= 0;
22508 /* Or in a switch. */
22509 in_switch_statement_p
= parser
->in_switch_statement_p
;
22510 parser
->in_switch_statement_p
= false;
22511 /* We are not immediately inside an extern "lang" block. */
22512 saved_in_unbraced_linkage_specification_p
22513 = parser
->in_unbraced_linkage_specification_p
;
22514 parser
->in_unbraced_linkage_specification_p
= false;
22516 // Associate constraints with the type.
22518 type
= associate_classtype_constraints (type
);
22520 /* Start the class. */
22521 if (nested_name_specifier_p
)
22523 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22524 old_scope
= push_inner_scope (scope
);
22526 type
= begin_class_definition (type
);
22528 if (type
== error_mark_node
)
22529 /* If the type is erroneous, skip the entire body of the class. */
22530 cp_parser_skip_to_closing_brace (parser
);
22532 /* Parse the member-specification. */
22533 cp_parser_member_specification_opt (parser
);
22535 /* Look for the trailing `}'. */
22536 closing_brace
= braces
.require_close (parser
);
22537 /* Look for trailing attributes to apply to this class. */
22538 if (cp_parser_allow_gnu_extensions_p (parser
))
22539 attributes
= cp_parser_gnu_attributes_opt (parser
);
22540 if (type
!= error_mark_node
)
22541 type
= finish_struct (type
, attributes
);
22542 if (nested_name_specifier_p
)
22543 pop_inner_scope (old_scope
, scope
);
22545 /* We've finished a type definition. Check for the common syntax
22546 error of forgetting a semicolon after the definition. We need to
22547 be careful, as we can't just check for not-a-semicolon and be done
22548 with it; the user might have typed:
22550 class X { } c = ...;
22551 class X { } *p = ...;
22553 and so forth. Instead, enumerate all the possible tokens that
22554 might follow this production; if we don't see one of them, then
22555 complain and silently insert the semicolon. */
22557 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22558 bool want_semicolon
= true;
22560 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22561 /* Don't try to parse c++11 attributes here. As per the
22562 grammar, that should be a task for
22563 cp_parser_decl_specifier_seq. */
22564 want_semicolon
= false;
22566 switch (token
->type
)
22569 case CPP_SEMICOLON
:
22572 case CPP_OPEN_PAREN
:
22573 case CPP_CLOSE_PAREN
:
22575 want_semicolon
= false;
22578 /* While it's legal for type qualifiers and storage class
22579 specifiers to follow type definitions in the grammar, only
22580 compiler testsuites contain code like that. Assume that if
22581 we see such code, then what we're really seeing is a case
22585 const <type> var = ...;
22590 static <type> func (...) ...
22592 i.e. the qualifier or specifier applies to the next
22593 declaration. To do so, however, we need to look ahead one
22594 more token to see if *that* token is a type specifier.
22596 This code could be improved to handle:
22599 static const <type> var = ...; */
22601 if (keyword_is_decl_specifier (token
->keyword
))
22603 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22605 /* Handling user-defined types here would be nice, but very
22608 = (lookahead
->type
== CPP_KEYWORD
22609 && keyword_begins_type_specifier (lookahead
->keyword
));
22616 /* If we don't have a type, then something is very wrong and we
22617 shouldn't try to do anything clever. Likewise for not seeing the
22619 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22621 /* Locate the closing brace. */
22622 cp_token_position prev
22623 = cp_lexer_previous_token_position (parser
->lexer
);
22624 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22625 location_t loc
= prev_token
->location
;
22627 /* We want to suggest insertion of a ';' immediately *after* the
22628 closing brace, so, if we can, offset the location by 1 column. */
22629 location_t next_loc
= loc
;
22630 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22631 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22633 rich_location
richloc (line_table
, next_loc
);
22635 /* If we successfully offset the location, suggest the fix-it. */
22636 if (next_loc
!= loc
)
22637 richloc
.add_fixit_insert_before (next_loc
, ";");
22639 if (CLASSTYPE_DECLARED_CLASS (type
))
22640 error_at (&richloc
,
22641 "expected %<;%> after class definition");
22642 else if (TREE_CODE (type
) == RECORD_TYPE
)
22643 error_at (&richloc
,
22644 "expected %<;%> after struct definition");
22645 else if (TREE_CODE (type
) == UNION_TYPE
)
22646 error_at (&richloc
,
22647 "expected %<;%> after union definition");
22649 gcc_unreachable ();
22651 /* Unget one token and smash it to look as though we encountered
22652 a semicolon in the input stream. */
22653 cp_lexer_set_token_position (parser
->lexer
, prev
);
22654 token
= cp_lexer_peek_token (parser
->lexer
);
22655 token
->type
= CPP_SEMICOLON
;
22656 token
->keyword
= RID_MAX
;
22660 /* If this class is not itself within the scope of another class,
22661 then we need to parse the bodies of all of the queued function
22662 definitions. Note that the queued functions defined in a class
22663 are not always processed immediately following the
22664 class-specifier for that class. Consider:
22667 struct B { void f() { sizeof (A); } };
22670 If `f' were processed before the processing of `A' were
22671 completed, there would be no way to compute the size of `A'.
22672 Note that the nesting we are interested in here is lexical --
22673 not the semantic nesting given by TYPE_CONTEXT. In particular,
22676 struct A { struct B; };
22677 struct A::B { void f() { } };
22679 there is no need to delay the parsing of `A::B::f'. */
22680 if (--parser
->num_classes_being_defined
== 0)
22683 tree class_type
= NULL_TREE
;
22684 tree pushed_scope
= NULL_TREE
;
22686 cp_default_arg_entry
*e
;
22687 tree save_ccp
, save_ccr
;
22689 if (any_erroneous_template_args_p (type
))
22691 /* Skip default arguments, NSDMIs, etc, in order to improve
22692 error recovery (c++/71169, c++/71832). */
22693 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22694 vec_safe_truncate (unparsed_nsdmis
, 0);
22695 vec_safe_truncate (unparsed_classes
, 0);
22696 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22699 /* In a first pass, parse default arguments to the functions.
22700 Then, in a second pass, parse the bodies of the functions.
22701 This two-phased approach handles cases like:
22709 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22712 /* If there are default arguments that have not yet been processed,
22713 take care of them now. */
22714 if (class_type
!= e
->class_type
)
22717 pop_scope (pushed_scope
);
22718 class_type
= e
->class_type
;
22719 pushed_scope
= push_scope (class_type
);
22721 /* Make sure that any template parameters are in scope. */
22722 maybe_begin_member_template_processing (decl
);
22723 /* Parse the default argument expressions. */
22724 cp_parser_late_parsing_default_args (parser
, decl
);
22725 /* Remove any template parameters from the symbol table. */
22726 maybe_end_member_template_processing ();
22728 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22729 /* Now parse any NSDMIs. */
22730 save_ccp
= current_class_ptr
;
22731 save_ccr
= current_class_ref
;
22732 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22734 if (class_type
!= DECL_CONTEXT (decl
))
22737 pop_scope (pushed_scope
);
22738 class_type
= DECL_CONTEXT (decl
);
22739 pushed_scope
= push_scope (class_type
);
22741 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22742 cp_parser_late_parsing_nsdmi (parser
, decl
);
22744 vec_safe_truncate (unparsed_nsdmis
, 0);
22745 current_class_ptr
= save_ccp
;
22746 current_class_ref
= save_ccr
;
22748 pop_scope (pushed_scope
);
22750 /* Now do some post-NSDMI bookkeeping. */
22751 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22752 after_nsdmi_defaulted_late_checks (class_type
);
22753 vec_safe_truncate (unparsed_classes
, 0);
22754 after_nsdmi_defaulted_late_checks (type
);
22756 /* Now parse the body of the functions. */
22759 /* OpenMP UDRs need to be parsed before all other functions. */
22760 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22761 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22762 cp_parser_late_parsing_for_member (parser
, decl
);
22763 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22764 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22765 cp_parser_late_parsing_for_member (parser
, decl
);
22768 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22769 cp_parser_late_parsing_for_member (parser
, decl
);
22770 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22773 vec_safe_push (unparsed_classes
, type
);
22775 /* Put back any saved access checks. */
22776 pop_deferring_access_checks ();
22778 /* Restore saved state. */
22779 parser
->in_switch_statement_p
= in_switch_statement_p
;
22780 parser
->in_statement
= in_statement
;
22781 parser
->in_function_body
= saved_in_function_body
;
22782 parser
->num_template_parameter_lists
22783 = saved_num_template_parameter_lists
;
22784 parser
->in_unbraced_linkage_specification_p
22785 = saved_in_unbraced_linkage_specification_p
;
22791 cp_parser_class_specifier (cp_parser
* parser
)
22794 timevar_push (TV_PARSE_STRUCT
);
22795 ret
= cp_parser_class_specifier_1 (parser
);
22796 timevar_pop (TV_PARSE_STRUCT
);
22800 /* Parse a class-head.
22803 class-key identifier [opt] base-clause [opt]
22804 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22805 class-key nested-name-specifier [opt] template-id
22808 class-virt-specifier:
22812 class-key attributes identifier [opt] base-clause [opt]
22813 class-key attributes nested-name-specifier identifier base-clause [opt]
22814 class-key attributes nested-name-specifier [opt] template-id
22817 Upon return BASES is initialized to the list of base classes (or
22818 NULL, if there are none) in the same form returned by
22819 cp_parser_base_clause.
22821 Returns the TYPE of the indicated class. Sets
22822 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22823 involving a nested-name-specifier was used, and FALSE otherwise.
22825 Returns error_mark_node if this is not a class-head.
22827 Returns NULL_TREE if the class-head is syntactically valid, but
22828 semantically invalid in a way that means we should skip the entire
22829 body of the class. */
22832 cp_parser_class_head (cp_parser
* parser
,
22833 bool* nested_name_specifier_p
)
22835 tree nested_name_specifier
;
22836 enum tag_types class_key
;
22837 tree id
= NULL_TREE
;
22838 tree type
= NULL_TREE
;
22841 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22842 bool template_id_p
= false;
22843 bool qualified_p
= false;
22844 bool invalid_nested_name_p
= false;
22845 bool invalid_explicit_specialization_p
= false;
22846 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22847 tree pushed_scope
= NULL_TREE
;
22848 unsigned num_templates
;
22849 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22850 /* Assume no nested-name-specifier will be present. */
22851 *nested_name_specifier_p
= false;
22852 /* Assume no template parameter lists will be used in defining the
22855 parser
->colon_corrects_to_scope_p
= false;
22857 /* Look for the class-key. */
22858 class_key
= cp_parser_class_key (parser
);
22859 if (class_key
== none_type
)
22860 return error_mark_node
;
22862 location_t class_head_start_location
= input_location
;
22864 /* Parse the attributes. */
22865 attributes
= cp_parser_attributes_opt (parser
);
22867 /* If the next token is `::', that is invalid -- but sometimes
22868 people do try to write:
22872 Handle this gracefully by accepting the extra qualifier, and then
22873 issuing an error about it later if this really is a
22874 class-head. If it turns out just to be an elaborated type
22875 specifier, remain silent. */
22876 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22877 qualified_p
= true;
22879 push_deferring_access_checks (dk_no_check
);
22881 /* Determine the name of the class. Begin by looking for an
22882 optional nested-name-specifier. */
22883 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22884 nested_name_specifier
22885 = cp_parser_nested_name_specifier_opt (parser
,
22886 /*typename_keyword_p=*/false,
22887 /*check_dependency_p=*/false,
22889 /*is_declaration=*/false);
22890 /* If there was a nested-name-specifier, then there *must* be an
22893 cp_token
*bad_template_keyword
= NULL
;
22895 if (nested_name_specifier
)
22897 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22898 /* Although the grammar says `identifier', it really means
22899 `class-name' or `template-name'. You are only allowed to
22900 define a class that has already been declared with this
22903 The proposed resolution for Core Issue 180 says that wherever
22904 you see `class T::X' you should treat `X' as a type-name.
22906 It is OK to define an inaccessible class; for example:
22908 class A { class B; };
22911 We do not know if we will see a class-name, or a
22912 template-name. We look for a class-name first, in case the
22913 class-name is a template-id; if we looked for the
22914 template-name first we would stop after the template-name. */
22915 cp_parser_parse_tentatively (parser
);
22916 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22917 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22918 type
= cp_parser_class_name (parser
,
22919 /*typename_keyword_p=*/false,
22920 /*template_keyword_p=*/false,
22922 /*check_dependency_p=*/false,
22923 /*class_head_p=*/true,
22924 /*is_declaration=*/false);
22925 /* If that didn't work, ignore the nested-name-specifier. */
22926 if (!cp_parser_parse_definitely (parser
))
22928 invalid_nested_name_p
= true;
22929 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22930 id
= cp_parser_identifier (parser
);
22931 if (id
== error_mark_node
)
22934 /* If we could not find a corresponding TYPE, treat this
22935 declaration like an unqualified declaration. */
22936 if (type
== error_mark_node
)
22937 nested_name_specifier
= NULL_TREE
;
22938 /* Otherwise, count the number of templates used in TYPE and its
22939 containing scopes. */
22944 for (scope
= TREE_TYPE (type
);
22945 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22946 scope
= get_containing_scope (scope
))
22948 && CLASS_TYPE_P (scope
)
22949 && CLASSTYPE_TEMPLATE_INFO (scope
)
22950 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22951 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22952 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22956 /* Otherwise, the identifier is optional. */
22959 /* We don't know whether what comes next is a template-id,
22960 an identifier, or nothing at all. */
22961 cp_parser_parse_tentatively (parser
);
22962 /* Check for a template-id. */
22963 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22964 id
= cp_parser_template_id (parser
,
22965 /*template_keyword_p=*/false,
22966 /*check_dependency_p=*/true,
22968 /*is_declaration=*/true);
22969 /* If that didn't work, it could still be an identifier. */
22970 if (!cp_parser_parse_definitely (parser
))
22972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22974 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22975 id
= cp_parser_identifier (parser
);
22982 template_id_p
= true;
22987 pop_deferring_access_checks ();
22991 cp_parser_check_for_invalid_template_id (parser
, id
,
22993 type_start_token
->location
);
22995 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22997 /* If it's not a `:' or a `{' then we can't really be looking at a
22998 class-head, since a class-head only appears as part of a
22999 class-specifier. We have to detect this situation before calling
23000 xref_tag, since that has irreversible side-effects. */
23001 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23003 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23004 type
= error_mark_node
;
23008 /* At this point, we're going ahead with the class-specifier, even
23009 if some other problem occurs. */
23010 cp_parser_commit_to_tentative_parse (parser
);
23011 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23013 cp_parser_error (parser
,
23014 "cannot specify %<override%> for a class");
23015 type
= error_mark_node
;
23018 /* Issue the error about the overly-qualified name now. */
23021 cp_parser_error (parser
,
23022 "global qualification of class name is invalid");
23023 type
= error_mark_node
;
23026 else if (invalid_nested_name_p
)
23028 cp_parser_error (parser
,
23029 "qualified name does not name a class");
23030 type
= error_mark_node
;
23033 else if (nested_name_specifier
)
23037 if (bad_template_keyword
)
23038 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23039 keyword template shall not appear at the top level. */
23040 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23041 "keyword %<template%> not allowed in class-head-name");
23043 /* Reject typedef-names in class heads. */
23044 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23046 error_at (type_start_token
->location
,
23047 "invalid class name in declaration of %qD",
23053 /* Figure out in what scope the declaration is being placed. */
23054 scope
= current_scope ();
23055 /* If that scope does not contain the scope in which the
23056 class was originally declared, the program is invalid. */
23057 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23059 if (at_namespace_scope_p ())
23060 error_at (type_start_token
->location
,
23061 "declaration of %qD in namespace %qD which does not "
23063 type
, scope
, nested_name_specifier
);
23065 error_at (type_start_token
->location
,
23066 "declaration of %qD in %qD which does not enclose %qD",
23067 type
, scope
, nested_name_specifier
);
23073 A declarator-id shall not be qualified except for the
23074 definition of a ... nested class outside of its class
23075 ... [or] the definition or explicit instantiation of a
23076 class member of a namespace outside of its namespace. */
23077 if (scope
== nested_name_specifier
)
23079 permerror (nested_name_specifier_token_start
->location
,
23080 "extra qualification not allowed");
23081 nested_name_specifier
= NULL_TREE
;
23085 /* An explicit-specialization must be preceded by "template <>". If
23086 it is not, try to recover gracefully. */
23087 if (at_namespace_scope_p ()
23088 && parser
->num_template_parameter_lists
== 0
23089 && !processing_template_parmlist
23092 /* Build a location of this form:
23093 struct typename <ARGS>
23094 ^~~~~~~~~~~~~~~~~~~~~~
23095 with caret==start at the start token, and
23096 finishing at the end of the type. */
23097 location_t reported_loc
23098 = make_location (class_head_start_location
,
23099 class_head_start_location
,
23100 get_finish (type_start_token
->location
));
23101 rich_location
richloc (line_table
, reported_loc
);
23102 richloc
.add_fixit_insert_before (class_head_start_location
,
23104 error_at (&richloc
,
23105 "an explicit specialization must be preceded by"
23106 " %<template <>%>");
23107 invalid_explicit_specialization_p
= true;
23108 /* Take the same action that would have been taken by
23109 cp_parser_explicit_specialization. */
23110 ++parser
->num_template_parameter_lists
;
23111 begin_specialization ();
23113 /* There must be no "return" statements between this point and the
23114 end of this function; set "type "to the correct return value and
23115 use "goto done;" to return. */
23116 /* Make sure that the right number of template parameters were
23118 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23120 type_start_token
->location
,
23121 /*declarator=*/NULL
))
23123 /* If something went wrong, there is no point in even trying to
23124 process the class-definition. */
23129 /* Look up the type. */
23132 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23133 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23134 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23136 error_at (type_start_token
->location
,
23137 "function template %qD redeclared as a class template", id
);
23138 type
= error_mark_node
;
23142 type
= TREE_TYPE (id
);
23143 type
= maybe_process_partial_specialization (type
);
23145 /* Check the scope while we still know whether or not we had a
23146 nested-name-specifier. */
23147 if (type
!= error_mark_node
)
23148 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23150 if (nested_name_specifier
)
23151 pushed_scope
= push_scope (nested_name_specifier
);
23153 else if (nested_name_specifier
)
23159 template <typename T> struct S { struct T };
23160 template <typename T> struct S<T>::T { };
23162 we will get a TYPENAME_TYPE when processing the definition of
23163 `S::T'. We need to resolve it to the actual type before we
23164 try to define it. */
23165 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23167 class_type
= resolve_typename_type (TREE_TYPE (type
),
23168 /*only_current_p=*/false);
23169 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23170 type
= TYPE_NAME (class_type
);
23173 cp_parser_error (parser
, "could not resolve typename type");
23174 type
= error_mark_node
;
23178 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23179 == error_mark_node
)
23185 class_type
= current_class_type
;
23186 /* Enter the scope indicated by the nested-name-specifier. */
23187 pushed_scope
= push_scope (nested_name_specifier
);
23188 /* Get the canonical version of this type. */
23189 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23190 /* Call push_template_decl if it seems like we should be defining a
23191 template either from the template headers or the type we're
23192 defining, so that we diagnose both extra and missing headers. */
23193 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23194 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23195 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23197 type
= push_template_decl (type
);
23198 if (type
== error_mark_node
)
23205 type
= TREE_TYPE (type
);
23206 *nested_name_specifier_p
= true;
23208 else /* The name is not a nested name. */
23210 /* If the class was unnamed, create a dummy name. */
23212 id
= make_anon_name ();
23213 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23214 ? ts_within_enclosing_non_class
23216 type
= xref_tag (class_key
, id
, tag_scope
,
23217 parser
->num_template_parameter_lists
);
23220 /* Indicate whether this class was declared as a `class' or as a
23222 if (TREE_CODE (type
) == RECORD_TYPE
)
23223 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23224 cp_parser_check_class_key (class_key
, type
);
23226 /* If this type was already complete, and we see another definition,
23227 that's an error. */
23228 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23230 error_at (type_start_token
->location
, "redefinition of %q#T",
23232 inform (location_of (type
), "previous definition of %q#T",
23237 else if (type
== error_mark_node
)
23242 /* Apply attributes now, before any use of the class as a template
23243 argument in its base list. */
23244 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23245 fixup_attribute_variants (type
);
23248 /* We will have entered the scope containing the class; the names of
23249 base classes should be looked up in that context. For example:
23251 struct A { struct B {}; struct C; };
23252 struct A::C : B {};
23256 /* Get the list of base-classes, if there is one. */
23257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23259 /* PR59482: enter the class scope so that base-specifiers are looked
23263 bases
= cp_parser_base_clause (parser
);
23264 /* PR59482: get out of the previously pushed class scope so that the
23265 subsequent pops pop the right thing. */
23272 /* If we're really defining a class, process the base classes.
23273 If they're invalid, fail. */
23274 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23275 xref_basetypes (type
, bases
);
23278 /* Leave the scope given by the nested-name-specifier. We will
23279 enter the class scope itself while processing the members. */
23281 pop_scope (pushed_scope
);
23283 if (invalid_explicit_specialization_p
)
23285 end_specialization ();
23286 --parser
->num_template_parameter_lists
;
23290 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23291 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23292 CLASSTYPE_FINAL (type
) = 1;
23294 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23298 /* Parse a class-key.
23305 Returns the kind of class-key specified, or none_type to indicate
23308 static enum tag_types
23309 cp_parser_class_key (cp_parser
* parser
)
23312 enum tag_types tag_type
;
23314 /* Look for the class-key. */
23315 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23319 /* Check to see if the TOKEN is a class-key. */
23320 tag_type
= cp_parser_token_is_class_key (token
);
23322 cp_parser_error (parser
, "expected class-key");
23326 /* Parse a type-parameter-key.
23328 type-parameter-key:
23334 cp_parser_type_parameter_key (cp_parser
* parser
)
23336 /* Look for the type-parameter-key. */
23337 enum tag_types tag_type
= none_type
;
23338 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23339 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23341 cp_lexer_consume_token (parser
->lexer
);
23342 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23343 /* typename is not allowed in a template template parameter
23344 by the standard until C++17. */
23345 pedwarn (token
->location
, OPT_Wpedantic
,
23346 "ISO C++ forbids typename key in template template parameter;"
23347 " use -std=c++17 or -std=gnu++17");
23350 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23355 /* Parse an (optional) member-specification.
23357 member-specification:
23358 member-declaration member-specification [opt]
23359 access-specifier : member-specification [opt] */
23362 cp_parser_member_specification_opt (cp_parser
* parser
)
23369 /* Peek at the next token. */
23370 token
= cp_lexer_peek_token (parser
->lexer
);
23371 /* If it's a `}', or EOF then we've seen all the members. */
23372 if (token
->type
== CPP_CLOSE_BRACE
23373 || token
->type
== CPP_EOF
23374 || token
->type
== CPP_PRAGMA_EOL
)
23377 /* See if this token is a keyword. */
23378 keyword
= token
->keyword
;
23382 case RID_PROTECTED
:
23384 /* Consume the access-specifier. */
23385 cp_lexer_consume_token (parser
->lexer
);
23386 /* Remember which access-specifier is active. */
23387 current_access_specifier
= token
->u
.value
;
23388 /* Look for the `:'. */
23389 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23393 /* Accept #pragmas at class scope. */
23394 if (token
->type
== CPP_PRAGMA
)
23396 cp_parser_pragma (parser
, pragma_member
, NULL
);
23400 /* Otherwise, the next construction must be a
23401 member-declaration. */
23402 cp_parser_member_declaration (parser
);
23407 /* Parse a member-declaration.
23409 member-declaration:
23410 decl-specifier-seq [opt] member-declarator-list [opt] ;
23411 function-definition ; [opt]
23412 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23414 template-declaration
23417 member-declarator-list:
23419 member-declarator-list , member-declarator
23422 declarator pure-specifier [opt]
23423 declarator constant-initializer [opt]
23424 identifier [opt] : constant-expression
23428 member-declaration:
23429 __extension__ member-declaration
23432 declarator attributes [opt] pure-specifier [opt]
23433 declarator attributes [opt] constant-initializer [opt]
23434 identifier [opt] attributes [opt] : constant-expression
23438 member-declaration:
23439 static_assert-declaration */
23442 cp_parser_member_declaration (cp_parser
* parser
)
23444 cp_decl_specifier_seq decl_specifiers
;
23445 tree prefix_attributes
;
23447 int declares_class_or_enum
;
23449 cp_token
*token
= NULL
;
23450 cp_token
*decl_spec_token_start
= NULL
;
23451 cp_token
*initializer_token_start
= NULL
;
23452 int saved_pedantic
;
23453 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23455 /* Check for the `__extension__' keyword. */
23456 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23459 cp_parser_member_declaration (parser
);
23460 /* Restore the old value of the PEDANTIC flag. */
23461 pedantic
= saved_pedantic
;
23466 /* Check for a template-declaration. */
23467 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23469 /* An explicit specialization here is an error condition, and we
23470 expect the specialization handler to detect and report this. */
23471 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23472 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23473 cp_parser_explicit_specialization (parser
);
23475 cp_parser_template_declaration (parser
, /*member_p=*/true);
23479 /* Check for a template introduction. */
23480 else if (cp_parser_template_declaration_after_export (parser
, true))
23483 /* Check for a using-declaration. */
23484 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23486 if (cxx_dialect
< cxx11
)
23488 /* Parse the using-declaration. */
23489 cp_parser_using_declaration (parser
,
23490 /*access_declaration_p=*/false);
23496 bool alias_decl_expected
;
23497 cp_parser_parse_tentatively (parser
);
23498 decl
= cp_parser_alias_declaration (parser
);
23499 /* Note that if we actually see the '=' token after the
23500 identifier, cp_parser_alias_declaration commits the
23501 tentative parse. In that case, we really expect an
23502 alias-declaration. Otherwise, we expect a using
23504 alias_decl_expected
=
23505 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23506 cp_parser_parse_definitely (parser
);
23508 if (alias_decl_expected
)
23509 finish_member_declaration (decl
);
23511 cp_parser_using_declaration (parser
,
23512 /*access_declaration_p=*/false);
23517 /* Check for @defs. */
23518 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23521 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23522 ivar
= ivar_chains
;
23526 ivar
= TREE_CHAIN (member
);
23527 TREE_CHAIN (member
) = NULL_TREE
;
23528 finish_member_declaration (member
);
23533 /* If the next token is `static_assert' we have a static assertion. */
23534 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23536 cp_parser_static_assert (parser
, /*member_p=*/true);
23540 parser
->colon_corrects_to_scope_p
= false;
23542 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23545 /* Parse the decl-specifier-seq. */
23546 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23547 cp_parser_decl_specifier_seq (parser
,
23548 CP_PARSER_FLAGS_OPTIONAL
,
23550 &declares_class_or_enum
);
23551 /* Check for an invalid type-name. */
23552 if (!decl_specifiers
.any_type_specifiers_p
23553 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23555 /* If there is no declarator, then the decl-specifier-seq should
23557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23559 /* If there was no decl-specifier-seq, and the next token is a
23560 `;', then we have something like:
23566 Each member-declaration shall declare at least one member
23567 name of the class. */
23568 if (!decl_specifiers
.any_specifiers_p
)
23570 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23571 if (!in_system_header_at (token
->location
))
23573 gcc_rich_location
richloc (token
->location
);
23574 richloc
.add_fixit_remove ();
23575 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23582 /* See if this declaration is a friend. */
23583 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23584 /* If there were decl-specifiers, check to see if there was
23585 a class-declaration. */
23586 type
= check_tag_decl (&decl_specifiers
,
23587 /*explicit_type_instantiation_p=*/false);
23588 /* Nested classes have already been added to the class, but
23589 a `friend' needs to be explicitly registered. */
23592 /* If the `friend' keyword was present, the friend must
23593 be introduced with a class-key. */
23594 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23595 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23596 "in C++03 a class-key must be used "
23597 "when declaring a friend");
23600 template <typename T> struct A {
23601 friend struct A<T>::B;
23604 A<T>::B will be represented by a TYPENAME_TYPE, and
23605 therefore not recognized by check_tag_decl. */
23608 type
= decl_specifiers
.type
;
23609 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23610 type
= TREE_TYPE (type
);
23612 if (!type
|| !TYPE_P (type
))
23613 error_at (decl_spec_token_start
->location
,
23614 "friend declaration does not name a class or "
23617 make_friend_class (current_class_type
, type
,
23618 /*complain=*/true);
23620 /* If there is no TYPE, an error message will already have
23622 else if (!type
|| type
== error_mark_node
)
23624 /* An anonymous aggregate has to be handled specially; such
23625 a declaration really declares a data member (with a
23626 particular type), as opposed to a nested class. */
23627 else if (ANON_AGGR_TYPE_P (type
))
23630 if (decl_specifiers
.storage_class
!= sc_none
)
23631 error_at (decl_spec_token_start
->location
,
23632 "a storage class on an anonymous aggregate "
23633 "in class scope is not allowed");
23635 /* Remove constructors and such from TYPE, now that we
23636 know it is an anonymous aggregate. */
23637 fixup_anonymous_aggr (type
);
23638 /* And make the corresponding data member. */
23639 decl
= build_decl (decl_spec_token_start
->location
,
23640 FIELD_DECL
, NULL_TREE
, type
);
23641 /* Add it to the class. */
23642 finish_member_declaration (decl
);
23645 cp_parser_check_access_in_redeclaration
23647 decl_spec_token_start
->location
);
23652 bool assume_semicolon
= false;
23654 /* Clear attributes from the decl_specifiers but keep them
23655 around as prefix attributes that apply them to the entity
23657 prefix_attributes
= decl_specifiers
.attributes
;
23658 decl_specifiers
.attributes
= NULL_TREE
;
23660 /* See if these declarations will be friends. */
23661 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23663 /* Keep going until we hit the `;' at the end of the
23665 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23667 tree attributes
= NULL_TREE
;
23668 tree first_attribute
;
23670 bool named_bitfld
= false;
23672 /* Peek at the next token. */
23673 token
= cp_lexer_peek_token (parser
->lexer
);
23675 /* The following code wants to know early if it is a bit-field
23676 or some other declaration. Attributes can appear before
23677 the `:' token. Skip over them without consuming any tokens
23678 to peek if they are followed by `:'. */
23679 if (cp_next_tokens_can_be_attribute_p (parser
)
23680 || (token
->type
== CPP_NAME
23681 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23682 && (named_bitfld
= true)))
23685 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23686 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23689 /* Check for a bitfield declaration. */
23690 if (token
->type
== CPP_COLON
23691 || (token
->type
== CPP_NAME
23692 && token
== cp_lexer_peek_token (parser
->lexer
)
23693 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23694 && (named_bitfld
= true)))
23698 tree late_attributes
= NULL_TREE
;
23701 identifier
= cp_parser_identifier (parser
);
23703 identifier
= NULL_TREE
;
23705 /* Look for attributes that apply to the bitfield. */
23706 attributes
= cp_parser_attributes_opt (parser
);
23708 /* Consume the `:' token. */
23709 cp_lexer_consume_token (parser
->lexer
);
23711 /* Get the width of the bitfield. */
23712 width
= cp_parser_constant_expression (parser
, false, NULL
,
23713 cxx_dialect
>= cxx11
);
23715 /* In C++2A and as extension for C++11 and above we allow
23716 default member initializers for bit-fields. */
23717 initializer
= NULL_TREE
;
23718 if (cxx_dialect
>= cxx11
23719 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23720 || cp_lexer_next_token_is (parser
->lexer
,
23724 = cp_lexer_peek_token (parser
->lexer
)->location
;
23725 if (cxx_dialect
< cxx2a
23726 && !in_system_header_at (loc
)
23727 && identifier
!= NULL_TREE
)
23729 "default member initializers for bit-fields "
23730 "only available with -std=c++2a or "
23733 initializer
= cp_parser_save_nsdmi (parser
);
23734 if (identifier
== NULL_TREE
)
23736 error_at (loc
, "default member initializer for "
23737 "unnamed bit-field");
23738 initializer
= NULL_TREE
;
23743 /* Look for attributes that apply to the bitfield after
23744 the `:' token and width. This is where GCC used to
23745 parse attributes in the past, pedwarn if there is
23746 a std attribute. */
23747 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23748 pedwarn (input_location
, OPT_Wpedantic
,
23749 "ISO C++ allows bit-field attributes only "
23750 "before the %<:%> token");
23752 late_attributes
= cp_parser_attributes_opt (parser
);
23755 attributes
= attr_chainon (attributes
, late_attributes
);
23757 /* Remember which attributes are prefix attributes and
23759 first_attribute
= attributes
;
23760 /* Combine the attributes. */
23761 attributes
= attr_chainon (prefix_attributes
, attributes
);
23763 /* Create the bitfield declaration. */
23764 decl
= grokbitfield (identifier
23765 ? make_id_declarator (NULL_TREE
,
23770 width
, initializer
,
23775 cp_declarator
*declarator
;
23776 tree asm_specification
;
23777 int ctor_dtor_or_conv_p
;
23779 /* Parse the declarator. */
23781 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23782 &ctor_dtor_or_conv_p
,
23783 /*parenthesized_p=*/NULL
,
23787 /* If something went wrong parsing the declarator, make sure
23788 that we at least consume some tokens. */
23789 if (declarator
== cp_error_declarator
)
23791 /* Skip to the end of the statement. */
23792 cp_parser_skip_to_end_of_statement (parser
);
23793 /* If the next token is not a semicolon, that is
23794 probably because we just skipped over the body of
23795 a function. So, we consume a semicolon if
23796 present, but do not issue an error message if it
23798 if (cp_lexer_next_token_is (parser
->lexer
,
23800 cp_lexer_consume_token (parser
->lexer
);
23804 if (declares_class_or_enum
& 2)
23805 cp_parser_check_for_definition_in_return_type
23806 (declarator
, decl_specifiers
.type
,
23807 decl_specifiers
.locations
[ds_type_spec
]);
23809 /* Look for an asm-specification. */
23810 asm_specification
= cp_parser_asm_specification_opt (parser
);
23811 /* Look for attributes that apply to the declaration. */
23812 attributes
= cp_parser_attributes_opt (parser
);
23813 /* Remember which attributes are prefix attributes and
23815 first_attribute
= attributes
;
23816 /* Combine the attributes. */
23817 attributes
= attr_chainon (prefix_attributes
, attributes
);
23819 /* If it's an `=', then we have a constant-initializer or a
23820 pure-specifier. It is not correct to parse the
23821 initializer before registering the member declaration
23822 since the member declaration should be in scope while
23823 its initializer is processed. However, the rest of the
23824 front end does not yet provide an interface that allows
23825 us to handle this correctly. */
23826 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23830 A pure-specifier shall be used only in the declaration of
23831 a virtual function.
23833 A member-declarator can contain a constant-initializer
23834 only if it declares a static member of integral or
23837 Therefore, if the DECLARATOR is for a function, we look
23838 for a pure-specifier; otherwise, we look for a
23839 constant-initializer. When we call `grokfield', it will
23840 perform more stringent semantics checks. */
23841 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23842 if (function_declarator_p (declarator
)
23843 || (decl_specifiers
.type
23844 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23845 && declarator
->kind
== cdk_id
23846 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23847 == FUNCTION_TYPE
)))
23848 initializer
= cp_parser_pure_specifier (parser
);
23849 else if (decl_specifiers
.storage_class
!= sc_static
)
23850 initializer
= cp_parser_save_nsdmi (parser
);
23851 else if (cxx_dialect
>= cxx11
)
23854 /* Don't require a constant rvalue in C++11, since we
23855 might want a reference constant. We'll enforce
23856 constancy later. */
23857 cp_lexer_consume_token (parser
->lexer
);
23858 /* Parse the initializer. */
23859 initializer
= cp_parser_initializer_clause (parser
,
23863 /* Parse the initializer. */
23864 initializer
= cp_parser_constant_initializer (parser
);
23866 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23867 && !function_declarator_p (declarator
))
23870 if (decl_specifiers
.storage_class
!= sc_static
)
23871 initializer
= cp_parser_save_nsdmi (parser
);
23873 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23875 /* Otherwise, there is no initializer. */
23877 initializer
= NULL_TREE
;
23879 /* See if we are probably looking at a function
23880 definition. We are certainly not looking at a
23881 member-declarator. Calling `grokfield' has
23882 side-effects, so we must not do it unless we are sure
23883 that we are looking at a member-declarator. */
23884 if (cp_parser_token_starts_function_definition_p
23885 (cp_lexer_peek_token (parser
->lexer
)))
23887 /* The grammar does not allow a pure-specifier to be
23888 used when a member function is defined. (It is
23889 possible that this fact is an oversight in the
23890 standard, since a pure function may be defined
23891 outside of the class-specifier. */
23892 if (initializer
&& initializer_token_start
)
23893 error_at (initializer_token_start
->location
,
23894 "pure-specifier on function-definition");
23895 decl
= cp_parser_save_member_function_body (parser
,
23899 if (parser
->fully_implicit_function_template_p
)
23900 decl
= finish_fully_implicit_template (parser
, decl
);
23901 /* If the member was not a friend, declare it here. */
23903 finish_member_declaration (decl
);
23904 /* Peek at the next token. */
23905 token
= cp_lexer_peek_token (parser
->lexer
);
23906 /* If the next token is a semicolon, consume it. */
23907 if (token
->type
== CPP_SEMICOLON
)
23909 location_t semicolon_loc
23910 = cp_lexer_consume_token (parser
->lexer
)->location
;
23911 gcc_rich_location
richloc (semicolon_loc
);
23912 richloc
.add_fixit_remove ();
23913 warning_at (&richloc
, OPT_Wextra_semi
,
23914 "extra %<;%> after in-class "
23915 "function definition");
23920 if (declarator
->kind
== cdk_function
)
23921 declarator
->id_loc
= token
->location
;
23922 /* Create the declaration. */
23923 decl
= grokfield (declarator
, &decl_specifiers
,
23924 initializer
, /*init_const_expr_p=*/true,
23925 asm_specification
, attributes
);
23926 if (parser
->fully_implicit_function_template_p
)
23929 finish_fully_implicit_template (parser
, 0);
23931 decl
= finish_fully_implicit_template (parser
, decl
);
23935 cp_finalize_omp_declare_simd (parser
, decl
);
23936 cp_finalize_oacc_routine (parser
, decl
, false);
23938 /* Reset PREFIX_ATTRIBUTES. */
23939 if (attributes
!= error_mark_node
)
23941 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23942 attributes
= TREE_CHAIN (attributes
);
23944 TREE_CHAIN (attributes
) = NULL_TREE
;
23947 /* If there is any qualification still in effect, clear it
23948 now; we will be starting fresh with the next declarator. */
23949 parser
->scope
= NULL_TREE
;
23950 parser
->qualifying_scope
= NULL_TREE
;
23951 parser
->object_scope
= NULL_TREE
;
23952 /* If it's a `,', then there are more declarators. */
23953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23955 cp_lexer_consume_token (parser
->lexer
);
23956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23958 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23959 gcc_rich_location
richloc (token
->location
);
23960 richloc
.add_fixit_remove ();
23961 error_at (&richloc
, "stray %<,%> at end of "
23962 "member declaration");
23965 /* If the next token isn't a `;', then we have a parse error. */
23966 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23969 /* The next token might be a ways away from where the
23970 actual semicolon is missing. Find the previous token
23971 and use that for our error position. */
23972 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23973 gcc_rich_location
richloc (token
->location
);
23974 richloc
.add_fixit_insert_after (";");
23975 error_at (&richloc
, "expected %<;%> at end of "
23976 "member declaration");
23978 /* Assume that the user meant to provide a semicolon. If
23979 we were to cp_parser_skip_to_end_of_statement, we might
23980 skip to a semicolon inside a member function definition
23981 and issue nonsensical error messages. */
23982 assume_semicolon
= true;
23987 /* Add DECL to the list of members. */
23989 /* Explicitly include, eg, NSDMIs, for better error
23990 recovery (c++/58650). */
23991 || !DECL_DECLARES_FUNCTION_P (decl
))
23992 finish_member_declaration (decl
);
23994 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23995 cp_parser_save_default_args (parser
, decl
);
23996 else if (TREE_CODE (decl
) == FIELD_DECL
23997 && DECL_INITIAL (decl
))
23998 /* Add DECL to the queue of NSDMI to be parsed later. */
23999 vec_safe_push (unparsed_nsdmis
, decl
);
24002 if (assume_semicolon
)
24007 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24009 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24012 /* Parse a pure-specifier.
24017 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24018 Otherwise, ERROR_MARK_NODE is returned. */
24021 cp_parser_pure_specifier (cp_parser
* parser
)
24025 /* Look for the `=' token. */
24026 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24027 return error_mark_node
;
24028 /* Look for the `0' token. */
24029 token
= cp_lexer_peek_token (parser
->lexer
);
24031 if (token
->type
== CPP_EOF
24032 || token
->type
== CPP_PRAGMA_EOL
)
24033 return error_mark_node
;
24035 cp_lexer_consume_token (parser
->lexer
);
24037 /* Accept = default or = delete in c++0x mode. */
24038 if (token
->keyword
== RID_DEFAULT
24039 || token
->keyword
== RID_DELETE
)
24041 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24042 return token
->u
.value
;
24045 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24046 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24048 cp_parser_error (parser
,
24049 "invalid pure specifier (only %<= 0%> is allowed)");
24050 cp_parser_skip_to_end_of_statement (parser
);
24051 return error_mark_node
;
24053 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24055 error_at (token
->location
, "templates may not be %<virtual%>");
24056 return error_mark_node
;
24059 return integer_zero_node
;
24062 /* Parse a constant-initializer.
24064 constant-initializer:
24065 = constant-expression
24067 Returns a representation of the constant-expression. */
24070 cp_parser_constant_initializer (cp_parser
* parser
)
24072 /* Look for the `=' token. */
24073 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24074 return error_mark_node
;
24076 /* It is invalid to write:
24078 struct S { static const int i = { 7 }; };
24081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24083 cp_parser_error (parser
,
24084 "a brace-enclosed initializer is not allowed here");
24085 /* Consume the opening brace. */
24086 matching_braces braces
;
24087 braces
.consume_open (parser
);
24088 /* Skip the initializer. */
24089 cp_parser_skip_to_closing_brace (parser
);
24090 /* Look for the trailing `}'. */
24091 braces
.require_close (parser
);
24093 return error_mark_node
;
24096 return cp_parser_constant_expression (parser
);
24099 /* Derived classes [gram.class.derived] */
24101 /* Parse a base-clause.
24104 : base-specifier-list
24106 base-specifier-list:
24107 base-specifier ... [opt]
24108 base-specifier-list , base-specifier ... [opt]
24110 Returns a TREE_LIST representing the base-classes, in the order in
24111 which they were declared. The representation of each node is as
24112 described by cp_parser_base_specifier.
24114 In the case that no bases are specified, this function will return
24115 NULL_TREE, not ERROR_MARK_NODE. */
24118 cp_parser_base_clause (cp_parser
* parser
)
24120 tree bases
= NULL_TREE
;
24122 /* Look for the `:' that begins the list. */
24123 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24125 /* Scan the base-specifier-list. */
24130 bool pack_expansion_p
= false;
24132 /* Look for the base-specifier. */
24133 base
= cp_parser_base_specifier (parser
);
24134 /* Look for the (optional) ellipsis. */
24135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24137 /* Consume the `...'. */
24138 cp_lexer_consume_token (parser
->lexer
);
24140 pack_expansion_p
= true;
24143 /* Add BASE to the front of the list. */
24144 if (base
&& base
!= error_mark_node
)
24146 if (pack_expansion_p
)
24147 /* Make this a pack expansion type. */
24148 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24150 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24152 TREE_CHAIN (base
) = bases
;
24156 /* Peek at the next token. */
24157 token
= cp_lexer_peek_token (parser
->lexer
);
24158 /* If it's not a comma, then the list is complete. */
24159 if (token
->type
!= CPP_COMMA
)
24161 /* Consume the `,'. */
24162 cp_lexer_consume_token (parser
->lexer
);
24165 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24166 base class had a qualified name. However, the next name that
24167 appears is certainly not qualified. */
24168 parser
->scope
= NULL_TREE
;
24169 parser
->qualifying_scope
= NULL_TREE
;
24170 parser
->object_scope
= NULL_TREE
;
24172 return nreverse (bases
);
24175 /* Parse a base-specifier.
24178 :: [opt] nested-name-specifier [opt] class-name
24179 virtual access-specifier [opt] :: [opt] nested-name-specifier
24181 access-specifier virtual [opt] :: [opt] nested-name-specifier
24184 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24185 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24186 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24187 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24190 cp_parser_base_specifier (cp_parser
* parser
)
24194 bool virtual_p
= false;
24195 bool duplicate_virtual_error_issued_p
= false;
24196 bool duplicate_access_error_issued_p
= false;
24197 bool class_scope_p
, template_p
;
24198 tree access
= access_default_node
;
24201 /* Process the optional `virtual' and `access-specifier'. */
24204 /* Peek at the next token. */
24205 token
= cp_lexer_peek_token (parser
->lexer
);
24206 /* Process `virtual'. */
24207 switch (token
->keyword
)
24210 /* If `virtual' appears more than once, issue an error. */
24211 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24213 cp_parser_error (parser
,
24214 "%<virtual%> specified more than once in base-specifier");
24215 duplicate_virtual_error_issued_p
= true;
24220 /* Consume the `virtual' token. */
24221 cp_lexer_consume_token (parser
->lexer
);
24226 case RID_PROTECTED
:
24228 /* If more than one access specifier appears, issue an
24230 if (access
!= access_default_node
24231 && !duplicate_access_error_issued_p
)
24233 cp_parser_error (parser
,
24234 "more than one access specifier in base-specifier");
24235 duplicate_access_error_issued_p
= true;
24238 access
= ridpointers
[(int) token
->keyword
];
24240 /* Consume the access-specifier. */
24241 cp_lexer_consume_token (parser
->lexer
);
24250 /* It is not uncommon to see programs mechanically, erroneously, use
24251 the 'typename' keyword to denote (dependent) qualified types
24252 as base classes. */
24253 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24255 token
= cp_lexer_peek_token (parser
->lexer
);
24256 if (!processing_template_decl
)
24257 error_at (token
->location
,
24258 "keyword %<typename%> not allowed outside of templates");
24260 error_at (token
->location
,
24261 "keyword %<typename%> not allowed in this context "
24262 "(the base class is implicitly a type)");
24263 cp_lexer_consume_token (parser
->lexer
);
24266 /* Look for the optional `::' operator. */
24267 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24268 /* Look for the nested-name-specifier. The simplest way to
24273 The keyword `typename' is not permitted in a base-specifier or
24274 mem-initializer; in these contexts a qualified name that
24275 depends on a template-parameter is implicitly assumed to be a
24278 is to pretend that we have seen the `typename' keyword at this
24280 cp_parser_nested_name_specifier_opt (parser
,
24281 /*typename_keyword_p=*/true,
24282 /*check_dependency_p=*/true,
24284 /*is_declaration=*/true);
24285 /* If the base class is given by a qualified name, assume that names
24286 we see are type names or templates, as appropriate. */
24287 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24288 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24291 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24292 /* DR 950 allows decltype as a base-specifier. */
24293 type
= cp_parser_decltype (parser
);
24296 /* Otherwise, look for the class-name. */
24297 type
= cp_parser_class_name (parser
,
24301 /*check_dependency_p=*/true,
24302 /*class_head_p=*/false,
24303 /*is_declaration=*/true);
24304 type
= TREE_TYPE (type
);
24307 if (type
== error_mark_node
)
24308 return error_mark_node
;
24310 return finish_base_specifier (type
, access
, virtual_p
);
24313 /* Exception handling [gram.exception] */
24315 /* Parse an (optional) noexcept-specification.
24317 noexcept-specification:
24318 noexcept ( constant-expression ) [opt]
24320 If no noexcept-specification is present, returns NULL_TREE.
24321 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24322 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24323 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24324 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24325 in which case a boolean condition is returned instead. */
24328 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24329 bool require_constexpr
,
24330 bool* consumed_expr
,
24334 const char *saved_message
;
24336 /* Peek at the next token. */
24337 token
= cp_lexer_peek_token (parser
->lexer
);
24339 /* Is it a noexcept-specification? */
24340 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24343 cp_lexer_consume_token (parser
->lexer
);
24345 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24347 matching_parens parens
;
24348 parens
.consume_open (parser
);
24350 if (require_constexpr
)
24352 /* Types may not be defined in an exception-specification. */
24353 saved_message
= parser
->type_definition_forbidden_message
;
24354 parser
->type_definition_forbidden_message
24355 = G_("types may not be defined in an exception-specification");
24357 expr
= cp_parser_constant_expression (parser
);
24359 /* Restore the saved message. */
24360 parser
->type_definition_forbidden_message
= saved_message
;
24364 expr
= cp_parser_expression (parser
);
24365 *consumed_expr
= true;
24368 parens
.require_close (parser
);
24372 expr
= boolean_true_node
;
24373 if (!require_constexpr
)
24374 *consumed_expr
= false;
24377 /* We cannot build a noexcept-spec right away because this will check
24378 that expr is a constexpr. */
24380 return build_noexcept_spec (expr
, tf_warning_or_error
);
24388 /* Parse an (optional) exception-specification.
24390 exception-specification:
24391 throw ( type-id-list [opt] )
24393 Returns a TREE_LIST representing the exception-specification. The
24394 TREE_VALUE of each node is a type. */
24397 cp_parser_exception_specification_opt (cp_parser
* parser
)
24401 const char *saved_message
;
24403 /* Peek at the next token. */
24404 token
= cp_lexer_peek_token (parser
->lexer
);
24406 /* Is it a noexcept-specification? */
24407 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24409 if (type_id_list
!= NULL_TREE
)
24410 return type_id_list
;
24412 /* If it's not `throw', then there's no exception-specification. */
24413 if (!cp_parser_is_keyword (token
, RID_THROW
))
24416 location_t loc
= token
->location
;
24418 /* Consume the `throw'. */
24419 cp_lexer_consume_token (parser
->lexer
);
24421 /* Look for the `('. */
24422 matching_parens parens
;
24423 parens
.require_open (parser
);
24425 /* Peek at the next token. */
24426 token
= cp_lexer_peek_token (parser
->lexer
);
24427 /* If it's not a `)', then there is a type-id-list. */
24428 if (token
->type
!= CPP_CLOSE_PAREN
)
24430 /* Types may not be defined in an exception-specification. */
24431 saved_message
= parser
->type_definition_forbidden_message
;
24432 parser
->type_definition_forbidden_message
24433 = G_("types may not be defined in an exception-specification");
24434 /* Parse the type-id-list. */
24435 type_id_list
= cp_parser_type_id_list (parser
);
24436 /* Restore the saved message. */
24437 parser
->type_definition_forbidden_message
= saved_message
;
24439 if (cxx_dialect
>= cxx17
)
24441 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24443 type_id_list
= NULL_TREE
;
24445 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24446 warning_at (loc
, OPT_Wdeprecated
,
24447 "dynamic exception specifications are deprecated in "
24450 /* In C++17, throw() is equivalent to noexcept (true). throw()
24451 is deprecated in C++11 and above as well, but is still widely used,
24452 so don't warn about it yet. */
24453 else if (cxx_dialect
>= cxx17
)
24454 type_id_list
= noexcept_true_spec
;
24456 type_id_list
= empty_except_spec
;
24458 /* Look for the `)'. */
24459 parens
.require_close (parser
);
24461 return type_id_list
;
24464 /* Parse an (optional) type-id-list.
24468 type-id-list , type-id ... [opt]
24470 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24471 in the order that the types were presented. */
24474 cp_parser_type_id_list (cp_parser
* parser
)
24476 tree types
= NULL_TREE
;
24483 token
= cp_lexer_peek_token (parser
->lexer
);
24485 /* Get the next type-id. */
24486 type
= cp_parser_type_id (parser
);
24487 /* Check for invalid 'auto'. */
24488 if (flag_concepts
&& type_uses_auto (type
))
24490 error_at (token
->location
,
24491 "invalid use of %<auto%> in exception-specification");
24492 type
= error_mark_node
;
24494 /* Parse the optional ellipsis. */
24495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24497 /* Consume the `...'. */
24498 cp_lexer_consume_token (parser
->lexer
);
24500 /* Turn the type into a pack expansion expression. */
24501 type
= make_pack_expansion (type
);
24503 /* Add it to the list. */
24504 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24505 /* Peek at the next token. */
24506 token
= cp_lexer_peek_token (parser
->lexer
);
24507 /* If it is not a `,', we are done. */
24508 if (token
->type
!= CPP_COMMA
)
24510 /* Consume the `,'. */
24511 cp_lexer_consume_token (parser
->lexer
);
24514 return nreverse (types
);
24517 /* Parse a try-block.
24520 try compound-statement handler-seq */
24523 cp_parser_try_block (cp_parser
* parser
)
24527 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24528 if (parser
->in_function_body
24529 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24530 error ("%<try%> in %<constexpr%> function");
24532 try_block
= begin_try_block ();
24533 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24534 finish_try_block (try_block
);
24535 cp_parser_handler_seq (parser
);
24536 finish_handler_sequence (try_block
);
24541 /* Parse a function-try-block.
24543 function-try-block:
24544 try ctor-initializer [opt] function-body handler-seq */
24547 cp_parser_function_try_block (cp_parser
* parser
)
24549 tree compound_stmt
;
24552 /* Look for the `try' keyword. */
24553 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24555 /* Let the rest of the front end know where we are. */
24556 try_block
= begin_function_try_block (&compound_stmt
);
24557 /* Parse the function-body. */
24558 cp_parser_ctor_initializer_opt_and_function_body
24559 (parser
, /*in_function_try_block=*/true);
24560 /* We're done with the `try' part. */
24561 finish_function_try_block (try_block
);
24562 /* Parse the handlers. */
24563 cp_parser_handler_seq (parser
);
24564 /* We're done with the handlers. */
24565 finish_function_handler_sequence (try_block
, compound_stmt
);
24568 /* Parse a handler-seq.
24571 handler handler-seq [opt] */
24574 cp_parser_handler_seq (cp_parser
* parser
)
24580 /* Parse the handler. */
24581 cp_parser_handler (parser
);
24582 /* Peek at the next token. */
24583 token
= cp_lexer_peek_token (parser
->lexer
);
24584 /* If it's not `catch' then there are no more handlers. */
24585 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24590 /* Parse a handler.
24593 catch ( exception-declaration ) compound-statement */
24596 cp_parser_handler (cp_parser
* parser
)
24601 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24602 handler
= begin_handler ();
24603 matching_parens parens
;
24604 parens
.require_open (parser
);
24605 declaration
= cp_parser_exception_declaration (parser
);
24606 finish_handler_parms (declaration
, handler
);
24607 parens
.require_close (parser
);
24608 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24609 finish_handler (handler
);
24612 /* Parse an exception-declaration.
24614 exception-declaration:
24615 type-specifier-seq declarator
24616 type-specifier-seq abstract-declarator
24620 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24621 ellipsis variant is used. */
24624 cp_parser_exception_declaration (cp_parser
* parser
)
24626 cp_decl_specifier_seq type_specifiers
;
24627 cp_declarator
*declarator
;
24628 const char *saved_message
;
24630 /* If it's an ellipsis, it's easy to handle. */
24631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24633 /* Consume the `...' token. */
24634 cp_lexer_consume_token (parser
->lexer
);
24638 /* Types may not be defined in exception-declarations. */
24639 saved_message
= parser
->type_definition_forbidden_message
;
24640 parser
->type_definition_forbidden_message
24641 = G_("types may not be defined in exception-declarations");
24643 /* Parse the type-specifier-seq. */
24644 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24645 /*is_trailing_return=*/false,
24647 /* If it's a `)', then there is no declarator. */
24648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24651 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24652 /*ctor_dtor_or_conv_p=*/NULL
,
24653 /*parenthesized_p=*/NULL
,
24654 /*member_p=*/false,
24655 /*friend_p=*/false);
24657 /* Restore the saved message. */
24658 parser
->type_definition_forbidden_message
= saved_message
;
24660 if (!type_specifiers
.any_specifiers_p
)
24661 return error_mark_node
;
24663 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24666 /* Parse a throw-expression.
24669 throw assignment-expression [opt]
24671 Returns a THROW_EXPR representing the throw-expression. */
24674 cp_parser_throw_expression (cp_parser
* parser
)
24679 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24680 token
= cp_lexer_peek_token (parser
->lexer
);
24681 /* Figure out whether or not there is an assignment-expression
24682 following the "throw" keyword. */
24683 if (token
->type
== CPP_COMMA
24684 || token
->type
== CPP_SEMICOLON
24685 || token
->type
== CPP_CLOSE_PAREN
24686 || token
->type
== CPP_CLOSE_SQUARE
24687 || token
->type
== CPP_CLOSE_BRACE
24688 || token
->type
== CPP_COLON
)
24689 expression
= NULL_TREE
;
24691 expression
= cp_parser_assignment_expression (parser
);
24693 return build_throw (expression
);
24696 /* GNU Extensions */
24698 /* Parse an (optional) asm-specification.
24701 asm ( string-literal )
24703 If the asm-specification is present, returns a STRING_CST
24704 corresponding to the string-literal. Otherwise, returns
24708 cp_parser_asm_specification_opt (cp_parser
* parser
)
24711 tree asm_specification
;
24713 /* Peek at the next token. */
24714 token
= cp_lexer_peek_token (parser
->lexer
);
24715 /* If the next token isn't the `asm' keyword, then there's no
24716 asm-specification. */
24717 if (!cp_parser_is_keyword (token
, RID_ASM
))
24720 /* Consume the `asm' token. */
24721 cp_lexer_consume_token (parser
->lexer
);
24722 /* Look for the `('. */
24723 matching_parens parens
;
24724 parens
.require_open (parser
);
24726 /* Look for the string-literal. */
24727 asm_specification
= cp_parser_string_literal (parser
, false, false);
24729 /* Look for the `)'. */
24730 parens
.require_close (parser
);
24732 return asm_specification
;
24735 /* Parse an asm-operand-list.
24739 asm-operand-list , asm-operand
24742 string-literal ( expression )
24743 [ string-literal ] string-literal ( expression )
24745 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24746 each node is the expression. The TREE_PURPOSE is itself a
24747 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24748 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24749 is a STRING_CST for the string literal before the parenthesis. Returns
24750 ERROR_MARK_NODE if any of the operands are invalid. */
24753 cp_parser_asm_operand_list (cp_parser
* parser
)
24755 tree asm_operands
= NULL_TREE
;
24756 bool invalid_operands
= false;
24760 tree string_literal
;
24764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24766 /* Consume the `[' token. */
24767 cp_lexer_consume_token (parser
->lexer
);
24768 /* Read the operand name. */
24769 name
= cp_parser_identifier (parser
);
24770 if (name
!= error_mark_node
)
24771 name
= build_string (IDENTIFIER_LENGTH (name
),
24772 IDENTIFIER_POINTER (name
));
24773 /* Look for the closing `]'. */
24774 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24778 /* Look for the string-literal. */
24779 string_literal
= cp_parser_string_literal (parser
, false, false);
24781 /* Look for the `('. */
24782 matching_parens parens
;
24783 parens
.require_open (parser
);
24784 /* Parse the expression. */
24785 expression
= cp_parser_expression (parser
);
24786 /* Look for the `)'. */
24787 parens
.require_close (parser
);
24789 if (name
== error_mark_node
24790 || string_literal
== error_mark_node
24791 || expression
== error_mark_node
)
24792 invalid_operands
= true;
24794 /* Add this operand to the list. */
24795 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24798 /* If the next token is not a `,', there are no more
24800 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24802 /* Consume the `,'. */
24803 cp_lexer_consume_token (parser
->lexer
);
24806 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24809 /* Parse an asm-clobber-list.
24813 asm-clobber-list , string-literal
24815 Returns a TREE_LIST, indicating the clobbers in the order that they
24816 appeared. The TREE_VALUE of each node is a STRING_CST. */
24819 cp_parser_asm_clobber_list (cp_parser
* parser
)
24821 tree clobbers
= NULL_TREE
;
24825 tree string_literal
;
24827 /* Look for the string literal. */
24828 string_literal
= cp_parser_string_literal (parser
, false, false);
24829 /* Add it to the list. */
24830 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24831 /* If the next token is not a `,', then the list is
24833 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24835 /* Consume the `,' token. */
24836 cp_lexer_consume_token (parser
->lexer
);
24842 /* Parse an asm-label-list.
24846 asm-label-list , identifier
24848 Returns a TREE_LIST, indicating the labels in the order that they
24849 appeared. The TREE_VALUE of each node is a label. */
24852 cp_parser_asm_label_list (cp_parser
* parser
)
24854 tree labels
= NULL_TREE
;
24858 tree identifier
, label
, name
;
24860 /* Look for the identifier. */
24861 identifier
= cp_parser_identifier (parser
);
24862 if (!error_operand_p (identifier
))
24864 label
= lookup_label (identifier
);
24865 if (TREE_CODE (label
) == LABEL_DECL
)
24867 TREE_USED (label
) = 1;
24868 check_goto (label
);
24869 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24870 IDENTIFIER_POINTER (identifier
));
24871 labels
= tree_cons (name
, label
, labels
);
24874 /* If the next token is not a `,', then the list is
24876 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24878 /* Consume the `,' token. */
24879 cp_lexer_consume_token (parser
->lexer
);
24882 return nreverse (labels
);
24885 /* Return TRUE iff the next tokens in the stream are possibly the
24886 beginning of a GNU extension attribute. */
24889 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24891 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24894 /* Return TRUE iff the next tokens in the stream are possibly the
24895 beginning of a standard C++-11 attribute specifier. */
24898 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24900 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24903 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24904 beginning of a standard C++-11 attribute specifier. */
24907 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24909 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24911 return (cxx_dialect
>= cxx11
24912 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24913 || (token
->type
== CPP_OPEN_SQUARE
24914 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24915 && token
->type
== CPP_OPEN_SQUARE
)));
24918 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24919 beginning of a GNU extension attribute. */
24922 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24924 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24926 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24929 /* Return true iff the next tokens can be the beginning of either a
24930 GNU attribute list, or a standard C++11 attribute sequence. */
24933 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24935 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24936 || cp_next_tokens_can_be_std_attribute_p (parser
));
24939 /* Return true iff the next Nth tokens can be the beginning of either
24940 a GNU attribute list, or a standard C++11 attribute sequence. */
24943 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24945 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24946 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24949 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24950 of GNU attributes, or return NULL. */
24953 cp_parser_attributes_opt (cp_parser
*parser
)
24955 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24956 return cp_parser_gnu_attributes_opt (parser
);
24957 return cp_parser_std_attribute_spec_seq (parser
);
24960 /* Parse an (optional) series of attributes.
24963 attributes attribute
24966 __attribute__ (( attribute-list [opt] ))
24968 The return value is as for cp_parser_gnu_attribute_list. */
24971 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24973 tree attributes
= NULL_TREE
;
24975 temp_override
<bool> cleanup
24976 (parser
->auto_is_implicit_function_template_parm_p
, false);
24981 tree attribute_list
;
24984 /* Peek at the next token. */
24985 token
= cp_lexer_peek_token (parser
->lexer
);
24986 /* If it's not `__attribute__', then we're done. */
24987 if (token
->keyword
!= RID_ATTRIBUTE
)
24990 /* Consume the `__attribute__' keyword. */
24991 cp_lexer_consume_token (parser
->lexer
);
24992 /* Look for the two `(' tokens. */
24993 matching_parens outer_parens
;
24994 outer_parens
.require_open (parser
);
24995 matching_parens inner_parens
;
24996 inner_parens
.require_open (parser
);
24998 /* Peek at the next token. */
24999 token
= cp_lexer_peek_token (parser
->lexer
);
25000 if (token
->type
!= CPP_CLOSE_PAREN
)
25001 /* Parse the attribute-list. */
25002 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25004 /* If the next token is a `)', then there is no attribute
25006 attribute_list
= NULL
;
25008 /* Look for the two `)' tokens. */
25009 if (!inner_parens
.require_close (parser
))
25011 if (!outer_parens
.require_close (parser
))
25014 cp_parser_skip_to_end_of_statement (parser
);
25016 /* Add these new attributes to the list. */
25017 attributes
= attr_chainon (attributes
, attribute_list
);
25023 /* Parse a GNU attribute-list.
25027 attribute-list , attribute
25031 identifier ( identifier )
25032 identifier ( identifier , expression-list )
25033 identifier ( expression-list )
25035 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25036 to an attribute. The TREE_PURPOSE of each node is the identifier
25037 indicating which attribute is in use. The TREE_VALUE represents
25038 the arguments, if any. */
25041 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25043 tree attribute_list
= NULL_TREE
;
25044 bool save_translate_strings_p
= parser
->translate_strings_p
;
25046 parser
->translate_strings_p
= false;
25053 /* Look for the identifier. We also allow keywords here; for
25054 example `__attribute__ ((const))' is legal. */
25055 token
= cp_lexer_peek_token (parser
->lexer
);
25056 if (token
->type
== CPP_NAME
25057 || token
->type
== CPP_KEYWORD
)
25059 tree arguments
= NULL_TREE
;
25061 /* Consume the token, but save it since we need it for the
25062 SIMD enabled function parsing. */
25063 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25065 /* Save away the identifier that indicates which attribute
25067 identifier
= (token
->type
== CPP_KEYWORD
)
25068 /* For keywords, use the canonical spelling, not the
25069 parsed identifier. */
25070 ? ridpointers
[(int) token
->keyword
]
25071 : id_token
->u
.value
;
25073 identifier
= canonicalize_attr_name (identifier
);
25074 attribute
= build_tree_list (identifier
, NULL_TREE
);
25076 /* Peek at the next token. */
25077 token
= cp_lexer_peek_token (parser
->lexer
);
25078 /* If it's an `(', then parse the attribute arguments. */
25079 if (token
->type
== CPP_OPEN_PAREN
)
25081 vec
<tree
, va_gc
> *vec
;
25082 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25083 ? id_attr
: normal_attr
);
25084 vec
= cp_parser_parenthesized_expression_list
25085 (parser
, attr_flag
, /*cast_p=*/false,
25086 /*allow_expansion_p=*/false,
25087 /*non_constant_p=*/NULL
);
25089 arguments
= error_mark_node
;
25092 arguments
= build_tree_list_vec (vec
);
25093 release_tree_vector (vec
);
25095 /* Save the arguments away. */
25096 TREE_VALUE (attribute
) = arguments
;
25099 if (arguments
!= error_mark_node
)
25101 /* Add this attribute to the list. */
25102 TREE_CHAIN (attribute
) = attribute_list
;
25103 attribute_list
= attribute
;
25106 token
= cp_lexer_peek_token (parser
->lexer
);
25108 /* Now, look for more attributes. If the next token isn't a
25109 `,', we're done. */
25110 if (token
->type
!= CPP_COMMA
)
25113 /* Consume the comma and keep going. */
25114 cp_lexer_consume_token (parser
->lexer
);
25116 parser
->translate_strings_p
= save_translate_strings_p
;
25118 /* We built up the list in reverse order. */
25119 return nreverse (attribute_list
);
25122 /* Parse a standard C++11 attribute.
25124 The returned representation is a TREE_LIST which TREE_PURPOSE is
25125 the scoped name of the attribute, and the TREE_VALUE is its
25128 Note that the scoped name of the attribute is itself a TREE_LIST
25129 which TREE_PURPOSE is the namespace of the attribute, and
25130 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25131 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25132 and which TREE_PURPOSE is directly the attribute name.
25134 Clients of the attribute code should use get_attribute_namespace
25135 and get_attribute_name to get the actual namespace and name of
25136 attributes, regardless of their being GNU or C++11 attributes.
25139 attribute-token attribute-argument-clause [opt]
25143 attribute-scoped-token
25145 attribute-scoped-token:
25146 attribute-namespace :: identifier
25148 attribute-namespace:
25151 attribute-argument-clause:
25152 ( balanced-token-seq )
25154 balanced-token-seq:
25155 balanced-token [opt]
25156 balanced-token-seq balanced-token
25159 ( balanced-token-seq )
25160 [ balanced-token-seq ]
25161 { balanced-token-seq }. */
25164 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25166 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25169 temp_override
<bool> cleanup
25170 (parser
->auto_is_implicit_function_template_parm_p
, false);
25172 /* First, parse name of the attribute, a.k.a attribute-token. */
25174 token
= cp_lexer_peek_token (parser
->lexer
);
25175 if (token
->type
== CPP_NAME
)
25176 attr_id
= token
->u
.value
;
25177 else if (token
->type
== CPP_KEYWORD
)
25178 attr_id
= ridpointers
[(int) token
->keyword
];
25179 else if (token
->flags
& NAMED_OP
)
25180 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25182 if (attr_id
== NULL_TREE
)
25185 cp_lexer_consume_token (parser
->lexer
);
25187 token
= cp_lexer_peek_token (parser
->lexer
);
25188 if (token
->type
== CPP_SCOPE
)
25190 /* We are seeing a scoped attribute token. */
25192 cp_lexer_consume_token (parser
->lexer
);
25194 error_at (token
->location
, "attribute using prefix used together "
25195 "with scoped attribute token");
25198 token
= cp_lexer_consume_token (parser
->lexer
);
25199 if (token
->type
== CPP_NAME
)
25200 attr_id
= token
->u
.value
;
25201 else if (token
->type
== CPP_KEYWORD
)
25202 attr_id
= ridpointers
[(int) token
->keyword
];
25203 else if (token
->flags
& NAMED_OP
)
25204 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25207 error_at (token
->location
,
25208 "expected an identifier for the attribute name");
25209 return error_mark_node
;
25212 attr_id
= canonicalize_attr_name (attr_id
);
25213 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25215 token
= cp_lexer_peek_token (parser
->lexer
);
25218 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25222 attr_id
= canonicalize_attr_name (attr_id
);
25223 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25225 /* C++11 noreturn attribute is equivalent to GNU's. */
25226 if (is_attribute_p ("noreturn", attr_id
))
25227 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25228 /* C++14 deprecated attribute is equivalent to GNU's. */
25229 else if (is_attribute_p ("deprecated", attr_id
))
25230 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25231 /* C++17 fallthrough attribute is equivalent to GNU's. */
25232 else if (is_attribute_p ("fallthrough", attr_id
))
25233 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25234 /* Transactional Memory TS optimize_for_synchronized attribute is
25235 equivalent to GNU transaction_callable. */
25236 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25237 TREE_PURPOSE (attribute
)
25238 = get_identifier ("transaction_callable");
25239 /* Transactional Memory attributes are GNU attributes. */
25240 else if (tm_attr_to_mask (attr_id
))
25241 TREE_PURPOSE (attribute
) = attr_id
;
25244 /* Now parse the optional argument clause of the attribute. */
25246 if (token
->type
!= CPP_OPEN_PAREN
)
25250 vec
<tree
, va_gc
> *vec
;
25251 int attr_flag
= normal_attr
;
25253 if (attr_ns
== get_identifier ("gnu")
25254 && attribute_takes_identifier_p (attr_id
))
25255 /* A GNU attribute that takes an identifier in parameter. */
25256 attr_flag
= id_attr
;
25258 vec
= cp_parser_parenthesized_expression_list
25259 (parser
, attr_flag
, /*cast_p=*/false,
25260 /*allow_expansion_p=*/true,
25261 /*non_constant_p=*/NULL
);
25263 arguments
= error_mark_node
;
25266 arguments
= build_tree_list_vec (vec
);
25267 release_tree_vector (vec
);
25270 if (arguments
== error_mark_node
)
25271 attribute
= error_mark_node
;
25273 TREE_VALUE (attribute
) = arguments
;
25279 /* Check that the attribute ATTRIBUTE appears at most once in the
25280 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25281 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25282 isn't implemented yet in GCC. */
25285 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25289 tree name
= get_attribute_name (attribute
);
25290 if (is_attribute_p ("noreturn", name
)
25291 && lookup_attribute ("noreturn", attributes
))
25292 error ("attribute %<noreturn%> can appear at most once "
25293 "in an attribute-list");
25294 else if (is_attribute_p ("deprecated", name
)
25295 && lookup_attribute ("deprecated", attributes
))
25296 error ("attribute %<deprecated%> can appear at most once "
25297 "in an attribute-list");
25301 /* Parse a list of standard C++-11 attributes.
25305 attribute-list , attribute[opt]
25307 attribute-list , attribute ...
25311 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25313 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25314 cp_token
*token
= NULL
;
25318 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25319 if (attribute
== error_mark_node
)
25321 if (attribute
!= NULL_TREE
)
25323 cp_parser_check_std_attribute (attributes
, attribute
);
25324 TREE_CHAIN (attribute
) = attributes
;
25325 attributes
= attribute
;
25327 token
= cp_lexer_peek_token (parser
->lexer
);
25328 if (token
->type
== CPP_ELLIPSIS
)
25330 cp_lexer_consume_token (parser
->lexer
);
25331 if (attribute
== NULL_TREE
)
25332 error_at (token
->location
,
25333 "expected attribute before %<...%>");
25336 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25337 if (pack
== error_mark_node
)
25338 return error_mark_node
;
25339 TREE_VALUE (attribute
) = pack
;
25341 token
= cp_lexer_peek_token (parser
->lexer
);
25343 if (token
->type
!= CPP_COMMA
)
25345 cp_lexer_consume_token (parser
->lexer
);
25347 attributes
= nreverse (attributes
);
25351 /* Parse a standard C++-11 attribute specifier.
25353 attribute-specifier:
25354 [ [ attribute-using-prefix [opt] attribute-list ] ]
25355 alignment-specifier
25357 attribute-using-prefix:
25358 using attribute-namespace :
25360 alignment-specifier:
25361 alignas ( type-id ... [opt] )
25362 alignas ( alignment-expression ... [opt] ). */
25365 cp_parser_std_attribute_spec (cp_parser
*parser
)
25367 tree attributes
= NULL_TREE
;
25368 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25370 if (token
->type
== CPP_OPEN_SQUARE
25371 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25373 tree attr_ns
= NULL_TREE
;
25375 cp_lexer_consume_token (parser
->lexer
);
25376 cp_lexer_consume_token (parser
->lexer
);
25378 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25380 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25381 if (token
->type
== CPP_NAME
)
25382 attr_ns
= token
->u
.value
;
25383 else if (token
->type
== CPP_KEYWORD
)
25384 attr_ns
= ridpointers
[(int) token
->keyword
];
25385 else if (token
->flags
& NAMED_OP
)
25386 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25389 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25391 if (cxx_dialect
< cxx17
25392 && !in_system_header_at (input_location
))
25393 pedwarn (input_location
, 0,
25394 "attribute using prefix only available "
25395 "with -std=c++17 or -std=gnu++17");
25397 cp_lexer_consume_token (parser
->lexer
);
25398 cp_lexer_consume_token (parser
->lexer
);
25399 cp_lexer_consume_token (parser
->lexer
);
25402 attr_ns
= NULL_TREE
;
25405 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25407 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25408 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25409 cp_parser_skip_to_end_of_statement (parser
);
25411 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25412 when we are sure that we have actually parsed them. */
25413 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25419 /* Look for an alignment-specifier. */
25421 token
= cp_lexer_peek_token (parser
->lexer
);
25423 if (token
->type
!= CPP_KEYWORD
25424 || token
->keyword
!= RID_ALIGNAS
)
25427 cp_lexer_consume_token (parser
->lexer
);
25428 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25430 matching_parens parens
;
25431 if (!parens
.require_open (parser
))
25432 return error_mark_node
;
25434 cp_parser_parse_tentatively (parser
);
25435 alignas_expr
= cp_parser_type_id (parser
);
25437 if (!cp_parser_parse_definitely (parser
))
25439 alignas_expr
= cp_parser_assignment_expression (parser
);
25440 if (alignas_expr
== error_mark_node
)
25441 cp_parser_skip_to_end_of_statement (parser
);
25442 if (alignas_expr
== NULL_TREE
25443 || alignas_expr
== error_mark_node
)
25444 return alignas_expr
;
25447 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25448 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25450 /* Handle alignas (pack...). */
25451 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25453 cp_lexer_consume_token (parser
->lexer
);
25454 alignas_expr
= make_pack_expansion (alignas_expr
);
25457 /* Something went wrong, so don't build the attribute. */
25458 if (alignas_expr
== error_mark_node
)
25459 return error_mark_node
;
25461 if (!parens
.require_close (parser
))
25462 return error_mark_node
;
25464 /* Build the C++-11 representation of an 'aligned'
25467 build_tree_list (build_tree_list (get_identifier ("gnu"),
25468 get_identifier ("aligned")),
25475 /* Parse a standard C++-11 attribute-specifier-seq.
25477 attribute-specifier-seq:
25478 attribute-specifier-seq [opt] attribute-specifier
25482 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25484 tree attr_specs
= NULL_TREE
;
25485 tree attr_last
= NULL_TREE
;
25489 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25490 if (attr_spec
== NULL_TREE
)
25492 if (attr_spec
== error_mark_node
)
25493 return error_mark_node
;
25496 TREE_CHAIN (attr_last
) = attr_spec
;
25498 attr_specs
= attr_last
= attr_spec
;
25499 attr_last
= tree_last (attr_last
);
25505 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25506 return index of the first token after balanced-token, or N on failure. */
25509 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25512 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25514 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25517 case CPP_PRAGMA_EOL
:
25518 /* Ran out of tokens. */
25520 case CPP_OPEN_PAREN
:
25523 case CPP_OPEN_BRACE
:
25526 case CPP_OPEN_SQUARE
:
25529 case CPP_CLOSE_PAREN
:
25532 case CPP_CLOSE_BRACE
:
25535 case CPP_CLOSE_SQUARE
:
25541 while (nparens
|| nbraces
|| nsquares
);
25545 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25546 return index of the first token after the GNU attribute tokens, or N on
25550 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25554 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25555 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25556 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25559 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25562 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25569 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25570 next token), return index of the first token after the standard C++11
25571 attribute tokens, or N on failure. */
25574 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25578 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25579 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25581 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25584 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25588 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25589 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25591 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25602 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25603 as the next token), return index of the first token after the attribute
25604 tokens, or N on failure. */
25607 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25609 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25610 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25611 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25614 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25615 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25616 current value of the PEDANTIC flag, regardless of whether or not
25617 the `__extension__' keyword is present. The caller is responsible
25618 for restoring the value of the PEDANTIC flag. */
25621 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25623 /* Save the old value of the PEDANTIC flag. */
25624 *saved_pedantic
= pedantic
;
25626 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25628 /* Consume the `__extension__' token. */
25629 cp_lexer_consume_token (parser
->lexer
);
25630 /* We're not being pedantic while the `__extension__' keyword is
25640 /* Parse a label declaration.
25643 __label__ label-declarator-seq ;
25645 label-declarator-seq:
25646 identifier , label-declarator-seq
25650 cp_parser_label_declaration (cp_parser
* parser
)
25652 /* Look for the `__label__' keyword. */
25653 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25659 /* Look for an identifier. */
25660 identifier
= cp_parser_identifier (parser
);
25661 /* If we failed, stop. */
25662 if (identifier
== error_mark_node
)
25664 /* Declare it as a label. */
25665 finish_label_decl (identifier
);
25666 /* If the next token is a `;', stop. */
25667 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25669 /* Look for the `,' separating the label declarations. */
25670 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25673 /* Look for the final `;'. */
25674 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25677 // -------------------------------------------------------------------------- //
25680 // Parse a requires clause.
25682 // requires-clause:
25683 // 'requires' logical-or-expression
25685 // The required logical-or-expression must be a constant expression. Note
25686 // that we don't check that the expression is constepxr here. We defer until
25687 // we analyze constraints and then, we only check atomic constraints.
25689 cp_parser_requires_clause (cp_parser
*parser
)
25691 // Parse the requires clause so that it is not automatically folded.
25692 ++processing_template_decl
;
25693 tree expr
= cp_parser_binary_expression (parser
, false, false,
25694 PREC_NOT_OPERATOR
, NULL
);
25695 if (check_for_bare_parameter_packs (expr
))
25696 expr
= error_mark_node
;
25697 --processing_template_decl
;
25701 // Optionally parse a requires clause:
25703 cp_parser_requires_clause_opt (cp_parser
*parser
)
25705 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25706 if (tok
->keyword
!= RID_REQUIRES
)
25708 if (!flag_concepts
&& tok
->type
== CPP_NAME
25709 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25711 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25712 "%<requires%> only available with -fconcepts");
25713 /* Parse and discard the requires-clause. */
25714 cp_lexer_consume_token (parser
->lexer
);
25715 cp_parser_requires_clause (parser
);
25719 cp_lexer_consume_token (parser
->lexer
);
25720 return cp_parser_requires_clause (parser
);
25724 /*---------------------------------------------------------------------------
25725 Requires expressions
25726 ---------------------------------------------------------------------------*/
25728 /* Parse a requires expression
25730 requirement-expression:
25731 'requires' requirement-parameter-list [opt] requirement-body */
25733 cp_parser_requires_expression (cp_parser
*parser
)
25735 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25736 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25738 /* A requires-expression shall appear only within a concept
25739 definition or a requires-clause.
25741 TODO: Implement this diagnostic correctly. */
25742 if (!processing_template_decl
)
25744 error_at (loc
, "a requires expression cannot appear outside a template");
25745 cp_parser_skip_to_end_of_statement (parser
);
25746 return error_mark_node
;
25751 /* Local parameters are delared as variables within the scope
25752 of the expression. They are not visible past the end of
25753 the expression. Expressions within the requires-expression
25754 are unevaluated. */
25755 struct scope_sentinel
25759 ++cp_unevaluated_operand
;
25760 begin_scope (sk_block
, NULL_TREE
);
25765 pop_bindings_and_leave_scope ();
25766 --cp_unevaluated_operand
;
25770 /* Parse the optional parameter list. */
25771 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25773 parms
= cp_parser_requirement_parameter_list (parser
);
25774 if (parms
== error_mark_node
)
25775 return error_mark_node
;
25780 /* Parse the requirement body. */
25781 reqs
= cp_parser_requirement_body (parser
);
25782 if (reqs
== error_mark_node
)
25783 return error_mark_node
;
25786 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25788 grokparms (parms
, &parms
);
25789 return finish_requires_expr (parms
, reqs
);
25792 /* Parse a parameterized requirement.
25794 requirement-parameter-list:
25795 '(' parameter-declaration-clause ')' */
25797 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25799 matching_parens parens
;
25800 if (!parens
.require_open (parser
))
25801 return error_mark_node
;
25803 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25805 if (!parens
.require_close (parser
))
25806 return error_mark_node
;
25811 /* Parse the body of a requirement.
25814 '{' requirement-list '}' */
25816 cp_parser_requirement_body (cp_parser
*parser
)
25818 matching_braces braces
;
25819 if (!braces
.require_open (parser
))
25820 return error_mark_node
;
25822 tree reqs
= cp_parser_requirement_list (parser
);
25824 if (!braces
.require_close (parser
))
25825 return error_mark_node
;
25830 /* Parse a list of requirements.
25834 requirement-list ';' requirement[opt] */
25836 cp_parser_requirement_list (cp_parser
*parser
)
25838 tree result
= NULL_TREE
;
25841 tree req
= cp_parser_requirement (parser
);
25842 if (req
== error_mark_node
)
25843 return error_mark_node
;
25845 result
= tree_cons (NULL_TREE
, req
, result
);
25847 /* If we see a semi-colon, consume it. */
25848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25849 cp_lexer_consume_token (parser
->lexer
);
25851 /* Stop processing at the end of the list. */
25852 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25856 /* Reverse the order of requirements so they are analyzed in
25857 declaration order. */
25858 return nreverse (result
);
25861 /* Parse a syntactic requirement or type requirement.
25865 compound-requirement
25867 nested-requirement */
25869 cp_parser_requirement (cp_parser
*parser
)
25871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25872 return cp_parser_compound_requirement (parser
);
25873 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25874 return cp_parser_type_requirement (parser
);
25875 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25876 return cp_parser_nested_requirement (parser
);
25878 return cp_parser_simple_requirement (parser
);
25881 /* Parse a simple requirement.
25883 simple-requirement:
25886 cp_parser_simple_requirement (cp_parser
*parser
)
25888 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25889 if (!expr
|| expr
== error_mark_node
)
25890 return error_mark_node
;
25892 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25893 return error_mark_node
;
25895 return finish_simple_requirement (expr
);
25898 /* Parse a type requirement
25901 nested-name-specifier [opt] required-type-name ';'
25903 required-type-name:
25905 'template' [opt] simple-template-id */
25907 cp_parser_type_requirement (cp_parser
*parser
)
25909 cp_lexer_consume_token (parser
->lexer
);
25911 // Save the scope before parsing name specifiers.
25912 tree saved_scope
= parser
->scope
;
25913 tree saved_object_scope
= parser
->object_scope
;
25914 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25915 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25916 cp_parser_nested_name_specifier_opt (parser
,
25917 /*typename_keyword_p=*/true,
25918 /*check_dependency_p=*/false,
25920 /*is_declaration=*/false);
25923 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25925 cp_lexer_consume_token (parser
->lexer
);
25926 type
= cp_parser_template_id (parser
,
25927 /*template_keyword_p=*/true,
25928 /*check_dependency=*/false,
25929 /*tag_type=*/none_type
,
25930 /*is_declaration=*/false);
25931 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25932 /*complain=*/tf_error
);
25935 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25937 if (TREE_CODE (type
) == TYPE_DECL
)
25938 type
= TREE_TYPE (type
);
25940 parser
->scope
= saved_scope
;
25941 parser
->object_scope
= saved_object_scope
;
25942 parser
->qualifying_scope
= saved_qualifying_scope
;
25944 if (type
== error_mark_node
)
25945 cp_parser_skip_to_end_of_statement (parser
);
25947 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25948 return error_mark_node
;
25949 if (type
== error_mark_node
)
25950 return error_mark_node
;
25952 return finish_type_requirement (type
);
25955 /* Parse a compound requirement
25957 compound-requirement:
25958 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25960 cp_parser_compound_requirement (cp_parser
*parser
)
25962 /* Parse an expression enclosed in '{ }'s. */
25963 matching_braces braces
;
25964 if (!braces
.require_open (parser
))
25965 return error_mark_node
;
25967 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25968 if (!expr
|| expr
== error_mark_node
)
25969 return error_mark_node
;
25971 if (!braces
.require_close (parser
))
25972 return error_mark_node
;
25974 /* Parse the optional noexcept. */
25975 bool noexcept_p
= false;
25976 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25978 cp_lexer_consume_token (parser
->lexer
);
25982 /* Parse the optional trailing return type. */
25983 tree type
= NULL_TREE
;
25984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25986 cp_lexer_consume_token (parser
->lexer
);
25987 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25988 parser
->in_result_type_constraint_p
= true;
25989 type
= cp_parser_trailing_type_id (parser
);
25990 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25991 if (type
== error_mark_node
)
25992 return error_mark_node
;
25995 return finish_compound_requirement (expr
, type
, noexcept_p
);
25998 /* Parse a nested requirement. This is the same as a requires clause.
26000 nested-requirement:
26003 cp_parser_nested_requirement (cp_parser
*parser
)
26005 cp_lexer_consume_token (parser
->lexer
);
26006 tree req
= cp_parser_requires_clause (parser
);
26007 if (req
== error_mark_node
)
26008 return error_mark_node
;
26009 return finish_nested_requirement (req
);
26012 /* Support Functions */
26014 /* Return the appropriate prefer_type argument for lookup_name_real based on
26015 tag_type and template_mem_access. */
26018 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26020 /* DR 141: When looking in the current enclosing context for a template-name
26021 after -> or ., only consider class templates. */
26022 if (template_mem_access
)
26026 case none_type
: return 0; // No preference.
26027 case scope_type
: return 1; // Type or namespace.
26028 default: return 2; // Type only.
26032 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26033 NAME should have one of the representations used for an
26034 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26035 is returned. If PARSER->SCOPE is a dependent type, then a
26036 SCOPE_REF is returned.
26038 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26039 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26040 was formed. Abstractly, such entities should not be passed to this
26041 function, because they do not need to be looked up, but it is
26042 simpler to check for this special case here, rather than at the
26045 In cases not explicitly covered above, this function returns a
26046 DECL, OVERLOAD, or baselink representing the result of the lookup.
26047 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26050 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26051 (e.g., "struct") that was used. In that case bindings that do not
26052 refer to types are ignored.
26054 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26057 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26060 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26063 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26064 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26065 NULL_TREE otherwise. */
26068 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26069 enum tag_types tag_type
,
26072 bool check_dependency
,
26073 tree
*ambiguous_decls
,
26074 location_t name_location
)
26077 tree object_type
= parser
->context
->object_type
;
26079 /* Assume that the lookup will be unambiguous. */
26080 if (ambiguous_decls
)
26081 *ambiguous_decls
= NULL_TREE
;
26083 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26084 no longer valid. Note that if we are parsing tentatively, and
26085 the parse fails, OBJECT_TYPE will be automatically restored. */
26086 parser
->context
->object_type
= NULL_TREE
;
26088 if (name
== error_mark_node
)
26089 return error_mark_node
;
26091 /* A template-id has already been resolved; there is no lookup to
26093 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26095 if (BASELINK_P (name
))
26097 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26098 == TEMPLATE_ID_EXPR
);
26102 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26103 it should already have been checked to make sure that the name
26104 used matches the type being destroyed. */
26105 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26109 /* Figure out to which type this destructor applies. */
26111 type
= parser
->scope
;
26112 else if (object_type
)
26113 type
= object_type
;
26115 type
= current_class_type
;
26116 /* If that's not a class type, there is no destructor. */
26117 if (!type
|| !CLASS_TYPE_P (type
))
26118 return error_mark_node
;
26120 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26121 lazily_declare_fn (sfk_destructor
, type
);
26123 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26126 return error_mark_node
;
26129 /* By this point, the NAME should be an ordinary identifier. If
26130 the id-expression was a qualified name, the qualifying scope is
26131 stored in PARSER->SCOPE at this point. */
26132 gcc_assert (identifier_p (name
));
26134 /* Perform the lookup. */
26139 if (parser
->scope
== error_mark_node
)
26140 return error_mark_node
;
26142 /* If the SCOPE is dependent, the lookup must be deferred until
26143 the template is instantiated -- unless we are explicitly
26144 looking up names in uninstantiated templates. Even then, we
26145 cannot look up the name if the scope is not a class type; it
26146 might, for example, be a template type parameter. */
26147 dependent_p
= (TYPE_P (parser
->scope
)
26148 && dependent_scope_p (parser
->scope
));
26149 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26151 /* Defer lookup. */
26152 decl
= error_mark_node
;
26155 tree pushed_scope
= NULL_TREE
;
26157 /* If PARSER->SCOPE is a dependent type, then it must be a
26158 class type, and we must not be checking dependencies;
26159 otherwise, we would have processed this lookup above. So
26160 that PARSER->SCOPE is not considered a dependent base by
26161 lookup_member, we must enter the scope here. */
26163 pushed_scope
= push_scope (parser
->scope
);
26165 /* If the PARSER->SCOPE is a template specialization, it
26166 may be instantiated during name lookup. In that case,
26167 errors may be issued. Even if we rollback the current
26168 tentative parse, those errors are valid. */
26169 decl
= lookup_qualified_name (parser
->scope
, name
,
26170 prefer_type_arg (tag_type
),
26171 /*complain=*/true);
26173 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26174 lookup result and the nested-name-specifier nominates a class C:
26175 * if the name specified after the nested-name-specifier, when
26176 looked up in C, is the injected-class-name of C (Clause 9), or
26177 * if the name specified after the nested-name-specifier is the
26178 same as the identifier or the simple-template-id's template-
26179 name in the last component of the nested-name-specifier,
26180 the name is instead considered to name the constructor of
26181 class C. [ Note: for example, the constructor is not an
26182 acceptable lookup result in an elaborated-type-specifier so
26183 the constructor would not be used in place of the
26184 injected-class-name. --end note ] Such a constructor name
26185 shall be used only in the declarator-id of a declaration that
26186 names a constructor or in a using-declaration. */
26187 if (tag_type
== none_type
26188 && DECL_SELF_REFERENCE_P (decl
)
26189 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26190 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26191 prefer_type_arg (tag_type
),
26192 /*complain=*/true);
26194 /* If we have a single function from a using decl, pull it out. */
26195 if (TREE_CODE (decl
) == OVERLOAD
26196 && !really_overloaded_fn (decl
))
26197 decl
= OVL_FUNCTION (decl
);
26200 pop_scope (pushed_scope
);
26203 /* If the scope is a dependent type and either we deferred lookup or
26204 we did lookup but didn't find the name, rememeber the name. */
26205 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26206 && dependent_type_p (parser
->scope
))
26212 /* The resolution to Core Issue 180 says that `struct
26213 A::B' should be considered a type-name, even if `A'
26215 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26216 /*complain=*/tf_error
);
26217 if (type
!= error_mark_node
)
26218 decl
= TYPE_NAME (type
);
26220 else if (is_template
26221 && (cp_parser_next_token_ends_template_argument_p (parser
)
26222 || cp_lexer_next_token_is (parser
->lexer
,
26224 decl
= make_unbound_class_template (parser
->scope
,
26226 /*complain=*/tf_error
);
26228 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26229 parser
->scope
, name
,
26232 parser
->qualifying_scope
= parser
->scope
;
26233 parser
->object_scope
= NULL_TREE
;
26235 else if (object_type
)
26237 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26238 OBJECT_TYPE is not a class. */
26239 if (CLASS_TYPE_P (object_type
))
26240 /* If the OBJECT_TYPE is a template specialization, it may
26241 be instantiated during name lookup. In that case, errors
26242 may be issued. Even if we rollback the current tentative
26243 parse, those errors are valid. */
26244 decl
= lookup_member (object_type
,
26247 prefer_type_arg (tag_type
),
26248 tf_warning_or_error
);
26253 /* Look it up in the enclosing context. DR 141: When looking for a
26254 template-name after -> or ., only consider class templates. */
26255 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26257 /*block_p=*/true, is_namespace
, 0);
26258 if (object_type
== unknown_type_node
)
26259 /* The object is type-dependent, so we can't look anything up; we used
26260 this to get the DR 141 behavior. */
26261 object_type
= NULL_TREE
;
26262 parser
->object_scope
= object_type
;
26263 parser
->qualifying_scope
= NULL_TREE
;
26267 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26269 /*block_p=*/true, is_namespace
, 0);
26270 parser
->qualifying_scope
= NULL_TREE
;
26271 parser
->object_scope
= NULL_TREE
;
26274 /* If the lookup failed, let our caller know. */
26275 if (!decl
|| decl
== error_mark_node
)
26276 return error_mark_node
;
26278 /* Pull out the template from an injected-class-name (or multiple). */
26280 decl
= maybe_get_template_decl_from_type_decl (decl
);
26282 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26283 if (TREE_CODE (decl
) == TREE_LIST
)
26285 if (ambiguous_decls
)
26286 *ambiguous_decls
= decl
;
26287 /* The error message we have to print is too complicated for
26288 cp_parser_error, so we incorporate its actions directly. */
26289 if (!cp_parser_simulate_error (parser
))
26291 error_at (name_location
, "reference to %qD is ambiguous",
26293 print_candidates (decl
);
26295 return error_mark_node
;
26298 gcc_assert (DECL_P (decl
)
26299 || TREE_CODE (decl
) == OVERLOAD
26300 || TREE_CODE (decl
) == SCOPE_REF
26301 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26302 || BASELINK_P (decl
));
26304 /* If we have resolved the name of a member declaration, check to
26305 see if the declaration is accessible. When the name resolves to
26306 set of overloaded functions, accessibility is checked when
26307 overload resolution is done.
26309 During an explicit instantiation, access is not checked at all,
26310 as per [temp.explicit]. */
26312 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26314 maybe_record_typedef_use (decl
);
26316 return cp_expr (decl
, name_location
);
26319 /* Like cp_parser_lookup_name, but for use in the typical case where
26320 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26321 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26324 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26326 return cp_parser_lookup_name (parser
, name
,
26328 /*is_template=*/false,
26329 /*is_namespace=*/false,
26330 /*check_dependency=*/true,
26331 /*ambiguous_decls=*/NULL
,
26335 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26336 the current context, return the TYPE_DECL. If TAG_NAME_P is
26337 true, the DECL indicates the class being defined in a class-head,
26338 or declared in an elaborated-type-specifier.
26340 Otherwise, return DECL. */
26343 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26345 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26346 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26349 template <typename T> struct B;
26352 template <typename T> struct A::B {};
26354 Similarly, in an elaborated-type-specifier:
26356 namespace N { struct X{}; }
26359 template <typename T> friend struct N::X;
26362 However, if the DECL refers to a class type, and we are in
26363 the scope of the class, then the name lookup automatically
26364 finds the TYPE_DECL created by build_self_reference rather
26365 than a TEMPLATE_DECL. For example, in:
26367 template <class T> struct S {
26371 there is no need to handle such case. */
26373 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26374 return DECL_TEMPLATE_RESULT (decl
);
26379 /* If too many, or too few, template-parameter lists apply to the
26380 declarator, issue an error message. Returns TRUE if all went well,
26381 and FALSE otherwise. */
26384 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26385 cp_declarator
*declarator
,
26386 location_t declarator_location
)
26388 switch (declarator
->kind
)
26392 unsigned num_templates
= 0;
26393 tree scope
= declarator
->u
.id
.qualifying_scope
;
26394 bool template_id_p
= false;
26397 num_templates
= num_template_headers_for_class (scope
);
26398 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26399 == TEMPLATE_ID_EXPR
)
26401 /* If the DECLARATOR has the form `X<y>' then it uses one
26402 additional level of template parameters. */
26404 template_id_p
= true;
26407 return cp_parser_check_template_parameters
26408 (parser
, num_templates
, template_id_p
, declarator_location
,
26415 case cdk_reference
:
26417 return (cp_parser_check_declarator_template_parameters
26418 (parser
, declarator
->declarator
, declarator_location
));
26425 gcc_unreachable ();
26430 /* NUM_TEMPLATES were used in the current declaration. If that is
26431 invalid, return FALSE and issue an error messages. Otherwise,
26432 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26433 declarator and we can print more accurate diagnostics. */
26436 cp_parser_check_template_parameters (cp_parser
* parser
,
26437 unsigned num_templates
,
26438 bool template_id_p
,
26439 location_t location
,
26440 cp_declarator
*declarator
)
26442 /* If there are the same number of template classes and parameter
26443 lists, that's OK. */
26444 if (parser
->num_template_parameter_lists
== num_templates
)
26446 /* If there are more, but only one more, and the name ends in an identifier,
26447 then we are declaring a primary template. That's OK too. */
26449 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26451 /* If there are more template classes than parameter lists, we have
26454 template <class T> void S<T>::R<T>::f (); */
26455 if (parser
->num_template_parameter_lists
< num_templates
)
26457 if (declarator
&& !current_function_decl
)
26458 error_at (location
, "specializing member %<%T::%E%> "
26459 "requires %<template<>%> syntax",
26460 declarator
->u
.id
.qualifying_scope
,
26461 declarator
->u
.id
.unqualified_name
);
26462 else if (declarator
)
26463 error_at (location
, "invalid declaration of %<%T::%E%>",
26464 declarator
->u
.id
.qualifying_scope
,
26465 declarator
->u
.id
.unqualified_name
);
26467 error_at (location
, "too few template-parameter-lists");
26470 /* Otherwise, there are too many template parameter lists. We have
26473 template <class T> template <class U> void S::f(); */
26474 error_at (location
, "too many template-parameter-lists");
26478 /* Parse an optional `::' token indicating that the following name is
26479 from the global namespace. If so, PARSER->SCOPE is set to the
26480 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26481 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26482 Returns the new value of PARSER->SCOPE, if the `::' token is
26483 present, and NULL_TREE otherwise. */
26486 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26490 /* Peek at the next token. */
26491 token
= cp_lexer_peek_token (parser
->lexer
);
26492 /* If we're looking at a `::' token then we're starting from the
26493 global namespace, not our current location. */
26494 if (token
->type
== CPP_SCOPE
)
26496 /* Consume the `::' token. */
26497 cp_lexer_consume_token (parser
->lexer
);
26498 /* Set the SCOPE so that we know where to start the lookup. */
26499 parser
->scope
= global_namespace
;
26500 parser
->qualifying_scope
= global_namespace
;
26501 parser
->object_scope
= NULL_TREE
;
26503 return parser
->scope
;
26505 else if (!current_scope_valid_p
)
26507 parser
->scope
= NULL_TREE
;
26508 parser
->qualifying_scope
= NULL_TREE
;
26509 parser
->object_scope
= NULL_TREE
;
26515 /* Returns TRUE if the upcoming token sequence is the start of a
26516 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26517 declarator is preceded by the `friend' specifier. */
26520 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26522 bool constructor_p
;
26523 bool outside_class_specifier_p
;
26524 tree nested_name_specifier
;
26525 cp_token
*next_token
;
26527 /* The common case is that this is not a constructor declarator, so
26528 try to avoid doing lots of work if at all possible. It's not
26529 valid declare a constructor at function scope. */
26530 if (parser
->in_function_body
)
26532 /* And only certain tokens can begin a constructor declarator. */
26533 next_token
= cp_lexer_peek_token (parser
->lexer
);
26534 if (next_token
->type
!= CPP_NAME
26535 && next_token
->type
!= CPP_SCOPE
26536 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26537 && next_token
->type
!= CPP_TEMPLATE_ID
)
26540 /* Parse tentatively; we are going to roll back all of the tokens
26542 cp_parser_parse_tentatively (parser
);
26543 /* Assume that we are looking at a constructor declarator. */
26544 constructor_p
= true;
26546 /* Look for the optional `::' operator. */
26547 cp_parser_global_scope_opt (parser
,
26548 /*current_scope_valid_p=*/false);
26549 /* Look for the nested-name-specifier. */
26550 nested_name_specifier
26551 = (cp_parser_nested_name_specifier_opt (parser
,
26552 /*typename_keyword_p=*/false,
26553 /*check_dependency_p=*/false,
26555 /*is_declaration=*/false));
26557 outside_class_specifier_p
= (!at_class_scope_p ()
26558 || !TYPE_BEING_DEFINED (current_class_type
)
26561 /* Outside of a class-specifier, there must be a
26562 nested-name-specifier. Except in C++17 mode, where we
26563 might be declaring a guiding declaration. */
26564 if (!nested_name_specifier
&& outside_class_specifier_p
26565 && cxx_dialect
< cxx17
)
26566 constructor_p
= false;
26567 else if (nested_name_specifier
== error_mark_node
)
26568 constructor_p
= false;
26570 /* If we have a class scope, this is easy; DR 147 says that S::S always
26571 names the constructor, and no other qualified name could. */
26572 if (constructor_p
&& nested_name_specifier
26573 && CLASS_TYPE_P (nested_name_specifier
))
26575 tree id
= cp_parser_unqualified_id (parser
,
26576 /*template_keyword_p=*/false,
26577 /*check_dependency_p=*/false,
26578 /*declarator_p=*/true,
26579 /*optional_p=*/false);
26580 if (is_overloaded_fn (id
))
26581 id
= DECL_NAME (get_first_fn (id
));
26582 if (!constructor_name_p (id
, nested_name_specifier
))
26583 constructor_p
= false;
26585 /* If we still think that this might be a constructor-declarator,
26586 look for a class-name. */
26587 else if (constructor_p
)
26591 template <typename T> struct S {
26595 we must recognize that the nested `S' names a class. */
26596 if (cxx_dialect
>= cxx17
)
26597 cp_parser_parse_tentatively (parser
);
26600 type_decl
= cp_parser_class_name (parser
,
26601 /*typename_keyword_p=*/false,
26602 /*template_keyword_p=*/false,
26604 /*check_dependency_p=*/false,
26605 /*class_head_p=*/false,
26606 /*is_declaration=*/false);
26608 if (cxx_dialect
>= cxx17
26609 && !cp_parser_parse_definitely (parser
))
26611 type_decl
= NULL_TREE
;
26612 tree tmpl
= cp_parser_template_name (parser
,
26613 /*template_keyword*/false,
26614 /*check_dependency_p*/false,
26615 /*is_declaration*/false,
26617 /*is_identifier*/NULL
);
26618 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26619 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26620 /* It's a deduction guide, return true. */;
26622 cp_parser_simulate_error (parser
);
26625 /* If there was no class-name, then this is not a constructor.
26626 Otherwise, if we are in a class-specifier and we aren't
26627 handling a friend declaration, check that its type matches
26628 current_class_type (c++/38313). Note: error_mark_node
26629 is left alone for error recovery purposes. */
26630 constructor_p
= (!cp_parser_error_occurred (parser
)
26631 && (outside_class_specifier_p
26632 || type_decl
== NULL_TREE
26633 || type_decl
== error_mark_node
26634 || same_type_p (current_class_type
,
26635 TREE_TYPE (type_decl
))));
26637 /* If we're still considering a constructor, we have to see a `(',
26638 to begin the parameter-declaration-clause, followed by either a
26639 `)', an `...', or a decl-specifier. We need to check for a
26640 type-specifier to avoid being fooled into thinking that:
26644 is a constructor. (It is actually a function named `f' that
26645 takes one parameter (of type `int') and returns a value of type
26648 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26649 constructor_p
= false;
26652 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26653 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26654 /* A parameter declaration begins with a decl-specifier,
26655 which is either the "attribute" keyword, a storage class
26656 specifier, or (usually) a type-specifier. */
26657 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26660 tree pushed_scope
= NULL_TREE
;
26661 unsigned saved_num_template_parameter_lists
;
26663 /* Names appearing in the type-specifier should be looked up
26664 in the scope of the class. */
26665 if (current_class_type
)
26667 else if (type_decl
)
26669 type
= TREE_TYPE (type_decl
);
26670 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26672 type
= resolve_typename_type (type
,
26673 /*only_current_p=*/false);
26674 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26676 cp_parser_abort_tentative_parse (parser
);
26680 pushed_scope
= push_scope (type
);
26683 /* Inside the constructor parameter list, surrounding
26684 template-parameter-lists do not apply. */
26685 saved_num_template_parameter_lists
26686 = parser
->num_template_parameter_lists
;
26687 parser
->num_template_parameter_lists
= 0;
26689 /* Look for the type-specifier. */
26690 cp_parser_type_specifier (parser
,
26691 CP_PARSER_FLAGS_NONE
,
26692 /*decl_specs=*/NULL
,
26693 /*is_declarator=*/true,
26694 /*declares_class_or_enum=*/NULL
,
26695 /*is_cv_qualifier=*/NULL
);
26697 parser
->num_template_parameter_lists
26698 = saved_num_template_parameter_lists
;
26700 /* Leave the scope of the class. */
26702 pop_scope (pushed_scope
);
26704 constructor_p
= !cp_parser_error_occurred (parser
);
26708 /* We did not really want to consume any tokens. */
26709 cp_parser_abort_tentative_parse (parser
);
26711 return constructor_p
;
26714 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26715 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26716 they must be performed once we are in the scope of the function.
26718 Returns the function defined. */
26721 cp_parser_function_definition_from_specifiers_and_declarator
26722 (cp_parser
* parser
,
26723 cp_decl_specifier_seq
*decl_specifiers
,
26725 const cp_declarator
*declarator
)
26730 /* Begin the function-definition. */
26731 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26733 /* The things we're about to see are not directly qualified by any
26734 template headers we've seen thus far. */
26735 reset_specialization ();
26737 /* If there were names looked up in the decl-specifier-seq that we
26738 did not check, check them now. We must wait until we are in the
26739 scope of the function to perform the checks, since the function
26740 might be a friend. */
26741 perform_deferred_access_checks (tf_warning_or_error
);
26745 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26746 parser
->omp_declare_simd
= NULL
;
26747 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26748 parser
->oacc_routine
= NULL
;
26753 /* Skip the entire function. */
26754 cp_parser_skip_to_end_of_block_or_statement (parser
);
26755 fn
= error_mark_node
;
26757 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26759 /* Seen already, skip it. An error message has already been output. */
26760 cp_parser_skip_to_end_of_block_or_statement (parser
);
26761 fn
= current_function_decl
;
26762 current_function_decl
= NULL_TREE
;
26763 /* If this is a function from a class, pop the nested class. */
26764 if (current_class_name
)
26765 pop_nested_class ();
26770 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26771 tv
= TV_PARSE_INLINE
;
26773 tv
= TV_PARSE_FUNC
;
26775 fn
= cp_parser_function_definition_after_declarator (parser
,
26776 /*inline_p=*/false);
26783 /* Parse the part of a function-definition that follows the
26784 declarator. INLINE_P is TRUE iff this function is an inline
26785 function defined within a class-specifier.
26787 Returns the function defined. */
26790 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26794 bool saved_in_unbraced_linkage_specification_p
;
26795 bool saved_in_function_body
;
26796 unsigned saved_num_template_parameter_lists
;
26798 bool fully_implicit_function_template_p
26799 = parser
->fully_implicit_function_template_p
;
26800 parser
->fully_implicit_function_template_p
= false;
26801 tree implicit_template_parms
26802 = parser
->implicit_template_parms
;
26803 parser
->implicit_template_parms
= 0;
26804 cp_binding_level
* implicit_template_scope
26805 = parser
->implicit_template_scope
;
26806 parser
->implicit_template_scope
= 0;
26808 saved_in_function_body
= parser
->in_function_body
;
26809 parser
->in_function_body
= true;
26810 /* If the next token is `return', then the code may be trying to
26811 make use of the "named return value" extension that G++ used to
26813 token
= cp_lexer_peek_token (parser
->lexer
);
26814 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26816 /* Consume the `return' keyword. */
26817 cp_lexer_consume_token (parser
->lexer
);
26818 /* Look for the identifier that indicates what value is to be
26820 cp_parser_identifier (parser
);
26821 /* Issue an error message. */
26822 error_at (token
->location
,
26823 "named return values are no longer supported");
26824 /* Skip tokens until we reach the start of the function body. */
26827 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26828 if (token
->type
== CPP_OPEN_BRACE
26829 || token
->type
== CPP_EOF
26830 || token
->type
== CPP_PRAGMA_EOL
)
26832 cp_lexer_consume_token (parser
->lexer
);
26835 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26836 anything declared inside `f'. */
26837 saved_in_unbraced_linkage_specification_p
26838 = parser
->in_unbraced_linkage_specification_p
;
26839 parser
->in_unbraced_linkage_specification_p
= false;
26840 /* Inside the function, surrounding template-parameter-lists do not
26842 saved_num_template_parameter_lists
26843 = parser
->num_template_parameter_lists
;
26844 parser
->num_template_parameter_lists
= 0;
26846 /* If the next token is `try', `__transaction_atomic', or
26847 `__transaction_relaxed`, then we are looking at either function-try-block
26848 or function-transaction-block. Note that all of these include the
26850 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26851 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26852 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26853 RID_TRANSACTION_RELAXED
))
26854 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26855 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26856 cp_parser_function_try_block (parser
);
26858 cp_parser_ctor_initializer_opt_and_function_body
26859 (parser
, /*in_function_try_block=*/false);
26861 /* Finish the function. */
26862 fn
= finish_function (inline_p
);
26863 /* Generate code for it, if necessary. */
26864 expand_or_defer_fn (fn
);
26865 /* Restore the saved values. */
26866 parser
->in_unbraced_linkage_specification_p
26867 = saved_in_unbraced_linkage_specification_p
;
26868 parser
->num_template_parameter_lists
26869 = saved_num_template_parameter_lists
;
26870 parser
->in_function_body
= saved_in_function_body
;
26872 parser
->fully_implicit_function_template_p
26873 = fully_implicit_function_template_p
;
26874 parser
->implicit_template_parms
26875 = implicit_template_parms
;
26876 parser
->implicit_template_scope
26877 = implicit_template_scope
;
26879 if (parser
->fully_implicit_function_template_p
)
26880 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26885 /* Parse a template-declaration body (following argument list). */
26888 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26889 tree parameter_list
,
26892 tree decl
= NULL_TREE
;
26893 bool friend_p
= false;
26895 /* We just processed one more parameter list. */
26896 ++parser
->num_template_parameter_lists
;
26898 /* Get the deferred access checks from the parameter list. These
26899 will be checked once we know what is being declared, as for a
26900 member template the checks must be performed in the scope of the
26901 class containing the member. */
26902 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26904 /* Tentatively parse for a new template parameter list, which can either be
26905 the template keyword or a template introduction. */
26906 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26908 else if (cxx_dialect
>= cxx11
26909 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26910 decl
= cp_parser_alias_declaration (parser
);
26913 /* There are no access checks when parsing a template, as we do not
26914 know if a specialization will be a friend. */
26915 push_deferring_access_checks (dk_no_check
);
26916 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26917 decl
= cp_parser_single_declaration (parser
,
26920 /*explicit_specialization_p=*/false,
26922 pop_deferring_access_checks ();
26924 /* If this is a member template declaration, let the front
26926 if (member_p
&& !friend_p
&& decl
)
26928 if (TREE_CODE (decl
) == TYPE_DECL
)
26929 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26931 decl
= finish_member_template_decl (decl
);
26933 else if (friend_p
&& decl
26934 && DECL_DECLARES_TYPE_P (decl
))
26935 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26936 /*complain=*/true);
26938 /* We are done with the current parameter list. */
26939 --parser
->num_template_parameter_lists
;
26941 pop_deferring_access_checks ();
26944 finish_template_decl (parameter_list
);
26946 /* Check the template arguments for a literal operator template. */
26948 && DECL_DECLARES_FUNCTION_P (decl
)
26949 && UDLIT_OPER_P (DECL_NAME (decl
)))
26952 if (parameter_list
== NULL_TREE
)
26956 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26957 if (num_parms
== 1)
26959 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26960 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26961 if (TREE_TYPE (parm
) != char_type_node
26962 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26965 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26967 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26968 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26969 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26970 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26971 if (parm
== error_mark_node
26972 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26973 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26981 if (cxx_dialect
>= cxx14
)
26982 error ("literal operator template %qD has invalid parameter list."
26983 " Expected non-type template argument pack <char...>"
26984 " or <typename CharT, CharT...>",
26987 error ("literal operator template %qD has invalid parameter list."
26988 " Expected non-type template argument pack <char...>",
26993 /* Register member declarations. */
26994 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26995 finish_member_declaration (decl
);
26996 /* If DECL is a function template, we must return to parse it later.
26997 (Even though there is no definition, there might be default
26998 arguments that need handling.) */
26999 if (member_p
&& decl
27000 && DECL_DECLARES_FUNCTION_P (decl
))
27001 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27004 /* Parse a template introduction header for a template-declaration. Returns
27005 false if tentative parse fails. */
27008 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27010 cp_parser_parse_tentatively (parser
);
27012 tree saved_scope
= parser
->scope
;
27013 tree saved_object_scope
= parser
->object_scope
;
27014 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27016 /* Look for the optional `::' operator. */
27017 cp_parser_global_scope_opt (parser
,
27018 /*current_scope_valid_p=*/false);
27019 /* Look for the nested-name-specifier. */
27020 cp_parser_nested_name_specifier_opt (parser
,
27021 /*typename_keyword_p=*/false,
27022 /*check_dependency_p=*/true,
27024 /*is_declaration=*/false);
27026 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27027 tree concept_name
= cp_parser_identifier (parser
);
27029 /* Look up the concept for which we will be matching
27030 template parameters. */
27031 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27033 parser
->scope
= saved_scope
;
27034 parser
->object_scope
= saved_object_scope
;
27035 parser
->qualifying_scope
= saved_qualifying_scope
;
27037 if (concept_name
== error_mark_node
)
27038 cp_parser_simulate_error (parser
);
27040 /* Look for opening brace for introduction. */
27041 matching_braces braces
;
27042 braces
.require_open (parser
);
27044 if (!cp_parser_parse_definitely (parser
))
27047 push_deferring_access_checks (dk_deferred
);
27049 /* Build vector of placeholder parameters and grab
27050 matching identifiers. */
27051 tree introduction_list
= cp_parser_introduction_list (parser
);
27053 /* The introduction-list shall not be empty. */
27054 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27057 error ("empty introduction-list");
27061 /* Look for closing brace for introduction. */
27062 if (!braces
.require_close (parser
))
27065 if (tmpl_decl
== error_mark_node
)
27067 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27072 /* Build and associate the constraint. */
27073 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27074 if (parms
&& parms
!= error_mark_node
)
27076 cp_parser_template_declaration_after_parameters (parser
, parms
,
27081 error_at (token
->location
, "no matching concept for template-introduction");
27085 /* Parse a normal template-declaration following the template keyword. */
27088 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27090 tree parameter_list
;
27091 bool need_lang_pop
;
27092 location_t location
= input_location
;
27094 /* Look for the `<' token. */
27095 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27097 if (at_class_scope_p () && current_function_decl
)
27099 /* 14.5.2.2 [temp.mem]
27101 A local class shall not have member templates. */
27102 error_at (location
,
27103 "invalid declaration of member template in local class");
27104 cp_parser_skip_to_end_of_block_or_statement (parser
);
27109 A template ... shall not have C linkage. */
27110 if (current_lang_name
== lang_name_c
)
27112 error_at (location
, "template with C linkage");
27113 maybe_show_extern_c_location ();
27114 /* Give it C++ linkage to avoid confusing other parts of the
27116 push_lang_context (lang_name_cplusplus
);
27117 need_lang_pop
= true;
27120 need_lang_pop
= false;
27122 /* We cannot perform access checks on the template parameter
27123 declarations until we know what is being declared, just as we
27124 cannot check the decl-specifier list. */
27125 push_deferring_access_checks (dk_deferred
);
27127 /* If the next token is `>', then we have an invalid
27128 specialization. Rather than complain about an invalid template
27129 parameter, issue an error message here. */
27130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27132 cp_parser_error (parser
, "invalid explicit specialization");
27133 begin_specialization ();
27134 parameter_list
= NULL_TREE
;
27138 /* Parse the template parameters. */
27139 parameter_list
= cp_parser_template_parameter_list (parser
);
27142 /* Look for the `>'. */
27143 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27145 /* Manage template requirements */
27148 tree reqs
= get_shorthand_constraints (current_template_parms
);
27149 if (tree r
= cp_parser_requires_clause_opt (parser
))
27150 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27151 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27154 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27157 /* For the erroneous case of a template with C linkage, we pushed an
27158 implicit C++ linkage scope; exit that scope now. */
27160 pop_lang_context ();
27163 /* Parse a template-declaration, assuming that the `export' (and
27164 `extern') keywords, if present, has already been scanned. MEMBER_P
27165 is as for cp_parser_template_declaration. */
27168 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27170 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27172 cp_lexer_consume_token (parser
->lexer
);
27173 cp_parser_explicit_template_declaration (parser
, member_p
);
27176 else if (flag_concepts
)
27177 return cp_parser_template_introduction (parser
, member_p
);
27182 /* Perform the deferred access checks from a template-parameter-list.
27183 CHECKS is a TREE_LIST of access checks, as returned by
27184 get_deferred_access_checks. */
27187 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27189 ++processing_template_parmlist
;
27190 perform_access_checks (checks
, tf_warning_or_error
);
27191 --processing_template_parmlist
;
27194 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27195 `function-definition' sequence that follows a template header.
27196 If MEMBER_P is true, this declaration appears in a class scope.
27198 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27199 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27202 cp_parser_single_declaration (cp_parser
* parser
,
27203 vec
<deferred_access_check
, va_gc
> *checks
,
27205 bool explicit_specialization_p
,
27208 int declares_class_or_enum
;
27209 tree decl
= NULL_TREE
;
27210 cp_decl_specifier_seq decl_specifiers
;
27211 bool function_definition_p
= false;
27212 cp_token
*decl_spec_token_start
;
27214 /* This function is only used when processing a template
27216 gcc_assert (innermost_scope_kind () == sk_template_parms
27217 || innermost_scope_kind () == sk_template_spec
);
27219 /* Defer access checks until we know what is being declared. */
27220 push_deferring_access_checks (dk_deferred
);
27222 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27224 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27225 cp_parser_decl_specifier_seq (parser
,
27226 CP_PARSER_FLAGS_OPTIONAL
,
27228 &declares_class_or_enum
);
27230 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27232 /* There are no template typedefs. */
27233 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27235 error_at (decl_spec_token_start
->location
,
27236 "template declaration of %<typedef%>");
27237 decl
= error_mark_node
;
27240 /* Gather up the access checks that occurred the
27241 decl-specifier-seq. */
27242 stop_deferring_access_checks ();
27244 /* Check for the declaration of a template class. */
27245 if (declares_class_or_enum
)
27247 if (cp_parser_declares_only_class_p (parser
)
27248 || (declares_class_or_enum
& 2))
27250 // If this is a declaration, but not a definition, associate
27251 // any constraints with the type declaration. Constraints
27252 // are associated with definitions in cp_parser_class_specifier.
27253 if (declares_class_or_enum
== 1)
27254 associate_classtype_constraints (decl_specifiers
.type
);
27256 decl
= shadow_tag (&decl_specifiers
);
27261 friend template <typename T> struct A<T>::B;
27264 A<T>::B will be represented by a TYPENAME_TYPE, and
27265 therefore not recognized by shadow_tag. */
27266 if (friend_p
&& *friend_p
27268 && decl_specifiers
.type
27269 && TYPE_P (decl_specifiers
.type
))
27270 decl
= decl_specifiers
.type
;
27272 if (decl
&& decl
!= error_mark_node
)
27273 decl
= TYPE_NAME (decl
);
27275 decl
= error_mark_node
;
27277 /* Perform access checks for template parameters. */
27278 cp_parser_perform_template_parameter_access_checks (checks
);
27280 /* Give a helpful diagnostic for
27281 template <class T> struct A { } a;
27282 if we aren't already recovering from an error. */
27283 if (!cp_parser_declares_only_class_p (parser
)
27286 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27287 "a class template declaration must not declare "
27289 cp_parser_skip_to_end_of_block_or_statement (parser
);
27295 /* Complain about missing 'typename' or other invalid type names. */
27296 if (!decl_specifiers
.any_type_specifiers_p
27297 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27299 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27300 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27301 the rest of this declaration. */
27302 decl
= error_mark_node
;
27306 /* If it's not a template class, try for a template function. If
27307 the next token is a `;', then this declaration does not declare
27308 anything. But, if there were errors in the decl-specifiers, then
27309 the error might well have come from an attempted class-specifier.
27310 In that case, there's no need to warn about a missing declarator. */
27312 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27313 || decl_specifiers
.type
!= error_mark_node
))
27315 decl
= cp_parser_init_declarator (parser
,
27318 /*function_definition_allowed_p=*/true,
27320 declares_class_or_enum
,
27321 &function_definition_p
,
27324 /* 7.1.1-1 [dcl.stc]
27326 A storage-class-specifier shall not be specified in an explicit
27327 specialization... */
27329 && explicit_specialization_p
27330 && decl_specifiers
.storage_class
!= sc_none
)
27332 error_at (decl_spec_token_start
->location
,
27333 "explicit template specialization cannot have a storage class");
27334 decl
= error_mark_node
;
27337 if (decl
&& VAR_P (decl
))
27338 check_template_variable (decl
);
27341 /* Look for a trailing `;' after the declaration. */
27342 if (!function_definition_p
27343 && (decl
== error_mark_node
27344 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27345 cp_parser_skip_to_end_of_block_or_statement (parser
);
27348 pop_deferring_access_checks ();
27350 /* Clear any current qualification; whatever comes next is the start
27351 of something new. */
27352 parser
->scope
= NULL_TREE
;
27353 parser
->qualifying_scope
= NULL_TREE
;
27354 parser
->object_scope
= NULL_TREE
;
27359 /* Parse a cast-expression that is not the operand of a unary "&". */
27362 cp_parser_simple_cast_expression (cp_parser
*parser
)
27364 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27365 /*cast_p=*/false, /*decltype*/false, NULL
);
27368 /* Parse a functional cast to TYPE. Returns an expression
27369 representing the cast. */
27372 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27374 vec
<tree
, va_gc
> *vec
;
27375 tree expression_list
;
27379 location_t start_loc
= input_location
;
27382 type
= error_mark_node
;
27384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27386 cp_lexer_set_source_position (parser
->lexer
);
27387 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27388 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27389 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27390 if (TREE_CODE (type
) == TYPE_DECL
)
27391 type
= TREE_TYPE (type
);
27393 cast
= finish_compound_literal (type
, expression_list
,
27394 tf_warning_or_error
, fcl_functional
);
27395 /* Create a location of the form:
27398 with caret == start at the start of the type name,
27399 finishing at the closing brace. */
27400 location_t finish_loc
27401 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27402 location_t combined_loc
= make_location (start_loc
, start_loc
,
27404 cast
.set_location (combined_loc
);
27409 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27411 /*allow_expansion_p=*/true,
27412 /*non_constant_p=*/NULL
);
27414 expression_list
= error_mark_node
;
27417 expression_list
= build_tree_list_vec (vec
);
27418 release_tree_vector (vec
);
27421 cast
= build_functional_cast (type
, expression_list
,
27422 tf_warning_or_error
);
27423 /* [expr.const]/1: In an integral constant expression "only type
27424 conversions to integral or enumeration type can be used". */
27425 if (TREE_CODE (type
) == TYPE_DECL
)
27426 type
= TREE_TYPE (type
);
27427 if (cast
!= error_mark_node
27428 && !cast_valid_in_integral_constant_expression_p (type
)
27429 && cp_parser_non_integral_constant_expression (parser
,
27431 return error_mark_node
;
27433 /* Create a location of the form:
27436 with caret == start at the start of the type name,
27437 finishing at the closing paren. */
27438 location_t finish_loc
27439 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27440 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27441 cast
.set_location (combined_loc
);
27445 /* Save the tokens that make up the body of a member function defined
27446 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27447 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27448 specifiers applied to the declaration. Returns the FUNCTION_DECL
27449 for the member function. */
27452 cp_parser_save_member_function_body (cp_parser
* parser
,
27453 cp_decl_specifier_seq
*decl_specifiers
,
27454 cp_declarator
*declarator
,
27460 bool function_try_block
= false;
27462 /* Create the FUNCTION_DECL. */
27463 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27464 cp_finalize_omp_declare_simd (parser
, fn
);
27465 cp_finalize_oacc_routine (parser
, fn
, true);
27466 /* If something went badly wrong, bail out now. */
27467 if (fn
== error_mark_node
)
27469 /* If there's a function-body, skip it. */
27470 if (cp_parser_token_starts_function_definition_p
27471 (cp_lexer_peek_token (parser
->lexer
)))
27472 cp_parser_skip_to_end_of_block_or_statement (parser
);
27473 return error_mark_node
;
27476 /* Remember it, if there default args to post process. */
27477 cp_parser_save_default_args (parser
, fn
);
27479 /* Save away the tokens that make up the body of the
27481 first
= parser
->lexer
->next_token
;
27483 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27484 cp_lexer_consume_token (parser
->lexer
);
27485 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27486 RID_TRANSACTION_ATOMIC
))
27488 cp_lexer_consume_token (parser
->lexer
);
27489 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27491 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27492 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27493 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27494 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27495 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27497 cp_lexer_consume_token (parser
->lexer
);
27498 cp_lexer_consume_token (parser
->lexer
);
27499 cp_lexer_consume_token (parser
->lexer
);
27500 cp_lexer_consume_token (parser
->lexer
);
27501 cp_lexer_consume_token (parser
->lexer
);
27504 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27505 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27507 cp_lexer_consume_token (parser
->lexer
);
27508 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27513 /* Handle function try blocks. */
27514 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27516 cp_lexer_consume_token (parser
->lexer
);
27517 function_try_block
= true;
27519 /* We can have braced-init-list mem-initializers before the fn body. */
27520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27522 cp_lexer_consume_token (parser
->lexer
);
27523 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27525 /* cache_group will stop after an un-nested { } pair, too. */
27526 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27529 /* variadic mem-inits have ... after the ')'. */
27530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27531 cp_lexer_consume_token (parser
->lexer
);
27534 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27535 /* Handle function try blocks. */
27536 if (function_try_block
)
27537 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27538 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27539 last
= parser
->lexer
->next_token
;
27541 /* Save away the inline definition; we will process it when the
27542 class is complete. */
27543 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27544 DECL_PENDING_INLINE_P (fn
) = 1;
27546 /* We need to know that this was defined in the class, so that
27547 friend templates are handled correctly. */
27548 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27550 /* Add FN to the queue of functions to be parsed later. */
27551 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27556 /* Save the tokens that make up the in-class initializer for a non-static
27557 data member. Returns a DEFAULT_ARG. */
27560 cp_parser_save_nsdmi (cp_parser
* parser
)
27562 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27565 /* Parse a template-argument-list, as well as the trailing ">" (but
27566 not the opening "<"). See cp_parser_template_argument_list for the
27570 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27574 tree saved_qualifying_scope
;
27575 tree saved_object_scope
;
27576 bool saved_greater_than_is_operator_p
;
27577 int saved_unevaluated_operand
;
27578 int saved_inhibit_evaluation_warnings
;
27582 When parsing a template-id, the first non-nested `>' is taken as
27583 the end of the template-argument-list rather than a greater-than
27585 saved_greater_than_is_operator_p
27586 = parser
->greater_than_is_operator_p
;
27587 parser
->greater_than_is_operator_p
= false;
27588 /* Parsing the argument list may modify SCOPE, so we save it
27590 saved_scope
= parser
->scope
;
27591 saved_qualifying_scope
= parser
->qualifying_scope
;
27592 saved_object_scope
= parser
->object_scope
;
27593 /* We need to evaluate the template arguments, even though this
27594 template-id may be nested within a "sizeof". */
27595 saved_unevaluated_operand
= cp_unevaluated_operand
;
27596 cp_unevaluated_operand
= 0;
27597 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27598 c_inhibit_evaluation_warnings
= 0;
27599 /* Parse the template-argument-list itself. */
27600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27601 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27602 arguments
= NULL_TREE
;
27604 arguments
= cp_parser_template_argument_list (parser
);
27605 /* Look for the `>' that ends the template-argument-list. If we find
27606 a '>>' instead, it's probably just a typo. */
27607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27609 if (cxx_dialect
!= cxx98
)
27611 /* In C++0x, a `>>' in a template argument list or cast
27612 expression is considered to be two separate `>'
27613 tokens. So, change the current token to a `>', but don't
27614 consume it: it will be consumed later when the outer
27615 template argument list (or cast expression) is parsed.
27616 Note that this replacement of `>' for `>>' is necessary
27617 even if we are parsing tentatively: in the tentative
27618 case, after calling
27619 cp_parser_enclosed_template_argument_list we will always
27620 throw away all of the template arguments and the first
27621 closing `>', either because the template argument list
27622 was erroneous or because we are replacing those tokens
27623 with a CPP_TEMPLATE_ID token. The second `>' (which will
27624 not have been thrown away) is needed either to close an
27625 outer template argument list or to complete a new-style
27627 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27628 token
->type
= CPP_GREATER
;
27630 else if (!saved_greater_than_is_operator_p
)
27632 /* If we're in a nested template argument list, the '>>' has
27633 to be a typo for '> >'. We emit the error message, but we
27634 continue parsing and we push a '>' as next token, so that
27635 the argument list will be parsed correctly. Note that the
27636 global source location is still on the token before the
27637 '>>', so we need to say explicitly where we want it. */
27638 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27639 gcc_rich_location
richloc (token
->location
);
27640 richloc
.add_fixit_replace ("> >");
27641 error_at (&richloc
, "%<>>%> should be %<> >%> "
27642 "within a nested template argument list");
27644 token
->type
= CPP_GREATER
;
27648 /* If this is not a nested template argument list, the '>>'
27649 is a typo for '>'. Emit an error message and continue.
27650 Same deal about the token location, but here we can get it
27651 right by consuming the '>>' before issuing the diagnostic. */
27652 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27653 error_at (token
->location
,
27654 "spurious %<>>%>, use %<>%> to terminate "
27655 "a template argument list");
27659 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27660 /* The `>' token might be a greater-than operator again now. */
27661 parser
->greater_than_is_operator_p
27662 = saved_greater_than_is_operator_p
;
27663 /* Restore the SAVED_SCOPE. */
27664 parser
->scope
= saved_scope
;
27665 parser
->qualifying_scope
= saved_qualifying_scope
;
27666 parser
->object_scope
= saved_object_scope
;
27667 cp_unevaluated_operand
= saved_unevaluated_operand
;
27668 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27673 /* MEMBER_FUNCTION is a member function, or a friend. If default
27674 arguments, or the body of the function have not yet been parsed,
27678 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27680 timevar_push (TV_PARSE_INMETH
);
27681 /* If this member is a template, get the underlying
27683 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27684 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27686 /* There should not be any class definitions in progress at this
27687 point; the bodies of members are only parsed outside of all class
27689 gcc_assert (parser
->num_classes_being_defined
== 0);
27690 /* While we're parsing the member functions we might encounter more
27691 classes. We want to handle them right away, but we don't want
27692 them getting mixed up with functions that are currently in the
27694 push_unparsed_function_queues (parser
);
27696 /* Make sure that any template parameters are in scope. */
27697 maybe_begin_member_template_processing (member_function
);
27699 /* If the body of the function has not yet been parsed, parse it
27701 if (DECL_PENDING_INLINE_P (member_function
))
27703 tree function_scope
;
27704 cp_token_cache
*tokens
;
27706 /* The function is no longer pending; we are processing it. */
27707 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27708 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27709 DECL_PENDING_INLINE_P (member_function
) = 0;
27711 /* If this is a local class, enter the scope of the containing
27713 function_scope
= current_function_decl
;
27714 if (function_scope
)
27715 push_function_context ();
27717 /* Push the body of the function onto the lexer stack. */
27718 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27720 /* Let the front end know that we going to be defining this
27722 start_preparsed_function (member_function
, NULL_TREE
,
27723 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27725 /* Don't do access checking if it is a templated function. */
27726 if (processing_template_decl
)
27727 push_deferring_access_checks (dk_no_check
);
27729 /* #pragma omp declare reduction needs special parsing. */
27730 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27732 parser
->lexer
->in_pragma
= true;
27733 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27734 finish_function (/*inline_p=*/true);
27735 cp_check_omp_declare_reduction (member_function
);
27738 /* Now, parse the body of the function. */
27739 cp_parser_function_definition_after_declarator (parser
,
27740 /*inline_p=*/true);
27742 if (processing_template_decl
)
27743 pop_deferring_access_checks ();
27745 /* Leave the scope of the containing function. */
27746 if (function_scope
)
27747 pop_function_context ();
27748 cp_parser_pop_lexer (parser
);
27751 /* Remove any template parameters from the symbol table. */
27752 maybe_end_member_template_processing ();
27754 /* Restore the queue. */
27755 pop_unparsed_function_queues (parser
);
27756 timevar_pop (TV_PARSE_INMETH
);
27759 /* If DECL contains any default args, remember it on the unparsed
27760 functions queue. */
27763 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27767 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27769 probe
= TREE_CHAIN (probe
))
27770 if (TREE_PURPOSE (probe
))
27772 cp_default_arg_entry entry
= {current_class_type
, decl
};
27773 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27778 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27779 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27780 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27781 from the parameter-type-list. */
27784 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27785 tree default_arg
, tree parmtype
)
27787 cp_token_cache
*tokens
;
27791 if (default_arg
== error_mark_node
)
27792 return error_mark_node
;
27794 /* Push the saved tokens for the default argument onto the parser's
27796 tokens
= DEFARG_TOKENS (default_arg
);
27797 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27799 start_lambda_scope (decl
);
27801 /* Parse the default argument. */
27802 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27803 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27804 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27806 finish_lambda_scope ();
27808 if (parsed_arg
== error_mark_node
)
27809 cp_parser_skip_to_end_of_statement (parser
);
27811 if (!processing_template_decl
)
27813 /* In a non-template class, check conversions now. In a template,
27814 we'll wait and instantiate these as needed. */
27815 if (TREE_CODE (decl
) == PARM_DECL
)
27816 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27817 tf_warning_or_error
);
27818 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27819 parsed_arg
= error_mark_node
;
27821 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27824 /* If the token stream has not been completely used up, then
27825 there was extra junk after the end of the default
27827 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27829 if (TREE_CODE (decl
) == PARM_DECL
)
27830 cp_parser_error (parser
, "expected %<,%>");
27832 cp_parser_error (parser
, "expected %<;%>");
27835 /* Revert to the main lexer. */
27836 cp_parser_pop_lexer (parser
);
27841 /* FIELD is a non-static data member with an initializer which we saved for
27842 later; parse it now. */
27845 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27849 maybe_begin_member_template_processing (field
);
27851 push_unparsed_function_queues (parser
);
27852 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27853 DECL_INITIAL (field
),
27855 pop_unparsed_function_queues (parser
);
27857 maybe_end_member_template_processing ();
27859 DECL_INITIAL (field
) = def
;
27862 /* FN is a FUNCTION_DECL which may contains a parameter with an
27863 unparsed DEFAULT_ARG. Parse the default args now. This function
27864 assumes that the current scope is the scope in which the default
27865 argument should be processed. */
27868 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27870 bool saved_local_variables_forbidden_p
;
27871 tree parm
, parmdecl
;
27873 /* While we're parsing the default args, we might (due to the
27874 statement expression extension) encounter more classes. We want
27875 to handle them right away, but we don't want them getting mixed
27876 up with default args that are currently in the queue. */
27877 push_unparsed_function_queues (parser
);
27879 /* Local variable names (and the `this' keyword) may not appear
27880 in a default argument. */
27881 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27882 parser
->local_variables_forbidden_p
= true;
27884 push_defarg_context (fn
);
27886 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27887 parmdecl
= DECL_ARGUMENTS (fn
);
27888 parm
&& parm
!= void_list_node
;
27889 parm
= TREE_CHAIN (parm
),
27890 parmdecl
= DECL_CHAIN (parmdecl
))
27892 tree default_arg
= TREE_PURPOSE (parm
);
27894 vec
<tree
, va_gc
> *insts
;
27901 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27902 /* This can happen for a friend declaration for a function
27903 already declared with default arguments. */
27907 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27909 TREE_VALUE (parm
));
27910 TREE_PURPOSE (parm
) = parsed_arg
;
27912 /* Update any instantiations we've already created. */
27913 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27914 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27915 TREE_PURPOSE (copy
) = parsed_arg
;
27918 pop_defarg_context ();
27920 /* Make sure no default arg is missing. */
27921 check_default_args (fn
);
27923 /* Restore the state of local_variables_forbidden_p. */
27924 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27926 /* Restore the queue. */
27927 pop_unparsed_function_queues (parser
);
27930 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27932 sizeof ... ( identifier )
27934 where the 'sizeof' token has already been consumed. */
27937 cp_parser_sizeof_pack (cp_parser
*parser
)
27939 /* Consume the `...'. */
27940 cp_lexer_consume_token (parser
->lexer
);
27941 maybe_warn_variadic_templates ();
27943 matching_parens parens
;
27944 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27946 parens
.consume_open (parser
);
27948 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27949 "%<sizeof...%> argument must be surrounded by parentheses");
27951 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27952 tree name
= cp_parser_identifier (parser
);
27953 if (name
== error_mark_node
)
27954 return error_mark_node
;
27955 /* The name is not qualified. */
27956 parser
->scope
= NULL_TREE
;
27957 parser
->qualifying_scope
= NULL_TREE
;
27958 parser
->object_scope
= NULL_TREE
;
27959 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27960 if (expr
== error_mark_node
)
27961 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27963 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27964 expr
= TREE_TYPE (expr
);
27965 else if (TREE_CODE (expr
) == CONST_DECL
)
27966 expr
= DECL_INITIAL (expr
);
27967 expr
= make_pack_expansion (expr
);
27968 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27971 parens
.require_close (parser
);
27976 /* Parse the operand of `sizeof' (or a similar operator). Returns
27977 either a TYPE or an expression, depending on the form of the
27978 input. The KEYWORD indicates which kind of expression we have
27982 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27984 tree expr
= NULL_TREE
;
27985 const char *saved_message
;
27987 bool saved_integral_constant_expression_p
;
27988 bool saved_non_integral_constant_expression_p
;
27990 /* If it's a `...', then we are computing the length of a parameter
27992 if (keyword
== RID_SIZEOF
27993 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27994 return cp_parser_sizeof_pack (parser
);
27996 /* Types cannot be defined in a `sizeof' expression. Save away the
27998 saved_message
= parser
->type_definition_forbidden_message
;
27999 /* And create the new one. */
28000 tmp
= concat ("types may not be defined in %<",
28001 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28002 "%> expressions", NULL
);
28003 parser
->type_definition_forbidden_message
= tmp
;
28005 /* The restrictions on constant-expressions do not apply inside
28006 sizeof expressions. */
28007 saved_integral_constant_expression_p
28008 = parser
->integral_constant_expression_p
;
28009 saved_non_integral_constant_expression_p
28010 = parser
->non_integral_constant_expression_p
;
28011 parser
->integral_constant_expression_p
= false;
28013 /* Do not actually evaluate the expression. */
28014 ++cp_unevaluated_operand
;
28015 ++c_inhibit_evaluation_warnings
;
28016 /* If it's a `(', then we might be looking at the type-id
28018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28020 tree type
= NULL_TREE
;
28022 /* We can't be sure yet whether we're looking at a type-id or an
28024 cp_parser_parse_tentatively (parser
);
28026 matching_parens parens
;
28027 parens
.consume_open (parser
);
28029 /* Note: as a GNU Extension, compound literals are considered
28030 postfix-expressions as they are in C99, so they are valid
28031 arguments to sizeof. See comment in cp_parser_cast_expression
28033 if (cp_parser_compound_literal_p (parser
))
28034 cp_parser_simulate_error (parser
);
28037 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28038 parser
->in_type_id_in_expr_p
= true;
28039 /* Look for the type-id. */
28040 type
= cp_parser_type_id (parser
);
28041 /* Look for the closing `)'. */
28042 parens
.require_close (parser
);
28043 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28046 /* If all went well, then we're done. */
28047 if (cp_parser_parse_definitely (parser
))
28049 cp_decl_specifier_seq decl_specs
;
28051 /* Build a trivial decl-specifier-seq. */
28052 clear_decl_specs (&decl_specs
);
28053 decl_specs
.type
= type
;
28055 /* Call grokdeclarator to figure out what type this is. */
28056 expr
= grokdeclarator (NULL
,
28060 /*attrlist=*/NULL
);
28064 /* If the type-id production did not work out, then we must be
28065 looking at the unary-expression production. */
28067 expr
= cp_parser_unary_expression (parser
);
28069 /* Go back to evaluating expressions. */
28070 --cp_unevaluated_operand
;
28071 --c_inhibit_evaluation_warnings
;
28073 /* Free the message we created. */
28075 /* And restore the old one. */
28076 parser
->type_definition_forbidden_message
= saved_message
;
28077 parser
->integral_constant_expression_p
28078 = saved_integral_constant_expression_p
;
28079 parser
->non_integral_constant_expression_p
28080 = saved_non_integral_constant_expression_p
;
28085 /* If the current declaration has no declarator, return true. */
28088 cp_parser_declares_only_class_p (cp_parser
*parser
)
28090 /* If the next token is a `;' or a `,' then there is no
28092 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28093 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28096 /* Update the DECL_SPECS to reflect the storage class indicated by
28100 cp_parser_set_storage_class (cp_parser
*parser
,
28101 cp_decl_specifier_seq
*decl_specs
,
28105 cp_storage_class storage_class
;
28107 if (parser
->in_unbraced_linkage_specification_p
)
28109 error_at (token
->location
, "invalid use of %qD in linkage specification",
28110 ridpointers
[keyword
]);
28113 else if (decl_specs
->storage_class
!= sc_none
)
28115 decl_specs
->conflicting_specifiers_p
= true;
28119 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28120 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28121 && decl_specs
->gnu_thread_keyword_p
)
28123 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28124 "%<__thread%> before %qD", ridpointers
[keyword
]);
28130 storage_class
= sc_auto
;
28133 storage_class
= sc_register
;
28136 storage_class
= sc_static
;
28139 storage_class
= sc_extern
;
28142 storage_class
= sc_mutable
;
28145 gcc_unreachable ();
28147 decl_specs
->storage_class
= storage_class
;
28148 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28150 /* A storage class specifier cannot be applied alongside a typedef
28151 specifier. If there is a typedef specifier present then set
28152 conflicting_specifiers_p which will trigger an error later
28153 on in grokdeclarator. */
28154 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28155 decl_specs
->conflicting_specifiers_p
= true;
28158 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28159 is true, the type is a class or enum definition. */
28162 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28165 bool type_definition_p
)
28167 decl_specs
->any_specifiers_p
= true;
28169 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28170 (with, for example, in "typedef int wchar_t;") we remember that
28171 this is what happened. In system headers, we ignore these
28172 declarations so that G++ can work with system headers that are not
28174 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28175 && !type_definition_p
28176 && (type_spec
== boolean_type_node
28177 || type_spec
== char16_type_node
28178 || type_spec
== char32_type_node
28179 || type_spec
== wchar_type_node
)
28180 && (decl_specs
->type
28181 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28182 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28183 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28184 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28186 decl_specs
->redefined_builtin_type
= type_spec
;
28187 set_and_check_decl_spec_loc (decl_specs
,
28188 ds_redefined_builtin_type_spec
,
28190 if (!decl_specs
->type
)
28192 decl_specs
->type
= type_spec
;
28193 decl_specs
->type_definition_p
= false;
28194 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28197 else if (decl_specs
->type
)
28198 decl_specs
->multiple_types_p
= true;
28201 decl_specs
->type
= type_spec
;
28202 decl_specs
->type_definition_p
= type_definition_p
;
28203 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28204 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28208 /* True iff TOKEN is the GNU keyword __thread. */
28211 token_is__thread (cp_token
*token
)
28213 gcc_assert (token
->keyword
== RID_THREAD
);
28214 return id_equal (token
->u
.value
, "__thread");
28217 /* Set the location for a declarator specifier and check if it is
28220 DECL_SPECS is the sequence of declarator specifiers onto which to
28223 DS is the single declarator specifier to set which location is to
28224 be set onto the existing sequence of declarators.
28226 LOCATION is the location for the declarator specifier to
28230 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28231 cp_decl_spec ds
, cp_token
*token
)
28233 gcc_assert (ds
< ds_last
);
28235 if (decl_specs
== NULL
)
28238 source_location location
= token
->location
;
28240 if (decl_specs
->locations
[ds
] == 0)
28242 decl_specs
->locations
[ds
] = location
;
28243 if (ds
== ds_thread
)
28244 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28250 if (decl_specs
->locations
[ds_long_long
] != 0)
28251 error_at (location
,
28252 "%<long long long%> is too long for GCC");
28255 decl_specs
->locations
[ds_long_long
] = location
;
28256 pedwarn_cxx98 (location
,
28258 "ISO C++ 1998 does not support %<long long%>");
28261 else if (ds
== ds_thread
)
28263 bool gnu
= token_is__thread (token
);
28264 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28265 error_at (location
,
28266 "both %<__thread%> and %<thread_local%> specified");
28269 gcc_rich_location
richloc (location
);
28270 richloc
.add_fixit_remove ();
28271 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28276 static const char *const decl_spec_names
[] = {
28293 gcc_rich_location
richloc (location
);
28294 richloc
.add_fixit_remove ();
28295 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28300 /* Return true iff the declarator specifier DS is present in the
28301 sequence of declarator specifiers DECL_SPECS. */
28304 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28307 gcc_assert (ds
< ds_last
);
28309 if (decl_specs
== NULL
)
28312 return decl_specs
->locations
[ds
] != 0;
28315 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28316 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28319 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28321 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28324 /* Issue an error message indicating that TOKEN_DESC was expected.
28325 If KEYWORD is true, it indicated this function is called by
28326 cp_parser_require_keword and the required token can only be
28327 a indicated keyword.
28329 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28330 within any error as the location of an "opening" token matching
28331 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28332 RT_CLOSE_PAREN). */
28335 cp_parser_required_error (cp_parser
*parser
,
28336 required_token token_desc
,
28338 location_t matching_location
)
28340 if (cp_parser_simulate_error (parser
))
28343 const char *gmsgid
= NULL
;
28344 switch (token_desc
)
28347 gmsgid
= G_("expected %<new%>");
28350 gmsgid
= G_("expected %<delete%>");
28353 gmsgid
= G_("expected %<return%>");
28356 gmsgid
= G_("expected %<while%>");
28359 gmsgid
= G_("expected %<extern%>");
28361 case RT_STATIC_ASSERT
:
28362 gmsgid
= G_("expected %<static_assert%>");
28365 gmsgid
= G_("expected %<decltype%>");
28368 gmsgid
= G_("expected %<operator%>");
28371 gmsgid
= G_("expected %<class%>");
28374 gmsgid
= G_("expected %<template%>");
28377 gmsgid
= G_("expected %<namespace%>");
28380 gmsgid
= G_("expected %<using%>");
28383 gmsgid
= G_("expected %<asm%>");
28386 gmsgid
= G_("expected %<try%>");
28389 gmsgid
= G_("expected %<catch%>");
28392 gmsgid
= G_("expected %<throw%>");
28395 gmsgid
= G_("expected %<__label__%>");
28398 gmsgid
= G_("expected %<@try%>");
28400 case RT_AT_SYNCHRONIZED
:
28401 gmsgid
= G_("expected %<@synchronized%>");
28404 gmsgid
= G_("expected %<@throw%>");
28406 case RT_TRANSACTION_ATOMIC
:
28407 gmsgid
= G_("expected %<__transaction_atomic%>");
28409 case RT_TRANSACTION_RELAXED
:
28410 gmsgid
= G_("expected %<__transaction_relaxed%>");
28416 if (!gmsgid
&& !keyword
)
28418 switch (token_desc
)
28421 gmsgid
= G_("expected %<;%>");
28423 case RT_OPEN_PAREN
:
28424 gmsgid
= G_("expected %<(%>");
28426 case RT_CLOSE_BRACE
:
28427 gmsgid
= G_("expected %<}%>");
28429 case RT_OPEN_BRACE
:
28430 gmsgid
= G_("expected %<{%>");
28432 case RT_CLOSE_SQUARE
:
28433 gmsgid
= G_("expected %<]%>");
28435 case RT_OPEN_SQUARE
:
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 %<*%>");
28460 gmsgid
= G_("expected %<~%>");
28463 gmsgid
= G_("expected %<:%>");
28465 case RT_COLON_SCOPE
:
28466 gmsgid
= G_("expected %<:%> or %<::%>");
28468 case RT_CLOSE_PAREN
:
28469 gmsgid
= G_("expected %<)%>");
28471 case RT_COMMA_CLOSE_PAREN
:
28472 gmsgid
= G_("expected %<,%> or %<)%>");
28474 case RT_PRAGMA_EOL
:
28475 gmsgid
= G_("expected end of line");
28478 gmsgid
= G_("expected identifier");
28481 gmsgid
= G_("expected selection-statement");
28484 gmsgid
= G_("expected iteration-statement");
28487 gmsgid
= G_("expected jump-statement");
28490 gmsgid
= G_("expected class-key");
28492 case RT_CLASS_TYPENAME_TEMPLATE
:
28493 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28496 gcc_unreachable ();
28501 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28505 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28506 issue an error message indicating that TOKEN_DESC was expected.
28508 Returns the token consumed, if the token had the appropriate type.
28509 Otherwise, returns NULL.
28511 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28512 within any error as the location of an "opening" token matching
28513 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28514 RT_CLOSE_PAREN). */
28517 cp_parser_require (cp_parser
* parser
,
28518 enum cpp_ttype type
,
28519 required_token token_desc
,
28520 location_t matching_location
)
28522 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28523 return cp_lexer_consume_token (parser
->lexer
);
28526 /* Output the MESSAGE -- unless we're parsing tentatively. */
28527 if (!cp_parser_simulate_error (parser
))
28528 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28529 matching_location
);
28534 /* An error message is produced if the next token is not '>'.
28535 All further tokens are skipped until the desired token is
28536 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28539 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28541 /* Current level of '< ... >'. */
28542 unsigned level
= 0;
28543 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28544 unsigned nesting_depth
= 0;
28546 /* Are we ready, yet? If not, issue error message. */
28547 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28550 /* Skip tokens until the desired token is found. */
28553 /* Peek at the next token. */
28554 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28557 if (!nesting_depth
)
28562 if (cxx_dialect
== cxx98
)
28563 /* C++0x views the `>>' operator as two `>' tokens, but
28566 else if (!nesting_depth
&& level
-- == 0)
28568 /* We've hit a `>>' where the first `>' closes the
28569 template argument list, and the second `>' is
28570 spurious. Just consume the `>>' and stop; we've
28571 already produced at least one error. */
28572 cp_lexer_consume_token (parser
->lexer
);
28575 /* Fall through for C++0x, so we handle the second `>' in
28577 gcc_fallthrough ();
28580 if (!nesting_depth
&& level
-- == 0)
28582 /* We've reached the token we want, consume it and stop. */
28583 cp_lexer_consume_token (parser
->lexer
);
28588 case CPP_OPEN_PAREN
:
28589 case CPP_OPEN_SQUARE
:
28593 case CPP_CLOSE_PAREN
:
28594 case CPP_CLOSE_SQUARE
:
28595 if (nesting_depth
-- == 0)
28600 case CPP_PRAGMA_EOL
:
28601 case CPP_SEMICOLON
:
28602 case CPP_OPEN_BRACE
:
28603 case CPP_CLOSE_BRACE
:
28604 /* The '>' was probably forgotten, don't look further. */
28611 /* Consume this token. */
28612 cp_lexer_consume_token (parser
->lexer
);
28616 /* If the next token is the indicated keyword, consume it. Otherwise,
28617 issue an error message indicating that TOKEN_DESC was expected.
28619 Returns the token consumed, if the token had the appropriate type.
28620 Otherwise, returns NULL. */
28623 cp_parser_require_keyword (cp_parser
* parser
,
28625 required_token token_desc
)
28627 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28629 if (token
&& token
->keyword
!= keyword
)
28631 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28639 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28640 function-definition. */
28643 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28645 return (/* An ordinary function-body begins with an `{'. */
28646 token
->type
== CPP_OPEN_BRACE
28647 /* A ctor-initializer begins with a `:'. */
28648 || token
->type
== CPP_COLON
28649 /* A function-try-block begins with `try'. */
28650 || token
->keyword
== RID_TRY
28651 /* A function-transaction-block begins with `__transaction_atomic'
28652 or `__transaction_relaxed'. */
28653 || token
->keyword
== RID_TRANSACTION_ATOMIC
28654 || token
->keyword
== RID_TRANSACTION_RELAXED
28655 /* The named return value extension begins with `return'. */
28656 || token
->keyword
== RID_RETURN
);
28659 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28663 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28667 token
= cp_lexer_peek_token (parser
->lexer
);
28668 return (token
->type
== CPP_OPEN_BRACE
28669 || (token
->type
== CPP_COLON
28670 && !parser
->colon_doesnt_start_class_def_p
));
28673 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28674 C++0x) ending a template-argument. */
28677 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28681 token
= cp_lexer_peek_token (parser
->lexer
);
28682 return (token
->type
== CPP_COMMA
28683 || token
->type
== CPP_GREATER
28684 || token
->type
== CPP_ELLIPSIS
28685 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28688 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28689 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28692 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28697 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28698 if (token
->type
== CPP_LESS
)
28700 /* Check for the sequence `<::' in the original code. It would be lexed as
28701 `[:', where `[' is a digraph, and there is no whitespace before
28703 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28706 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28707 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28713 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28714 or none_type otherwise. */
28716 static enum tag_types
28717 cp_parser_token_is_class_key (cp_token
* token
)
28719 switch (token
->keyword
)
28724 return record_type
;
28733 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28734 or none_type otherwise or if the token is null. */
28736 static enum tag_types
28737 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28742 switch (token
->keyword
)
28747 return typename_type
;
28754 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28757 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28759 if (type
== error_mark_node
)
28761 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28763 if (permerror (input_location
, "%qs tag used in naming %q#T",
28764 class_key
== union_type
? "union"
28765 : class_key
== record_type
? "struct" : "class",
28767 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28768 "%q#T was previously declared here", type
);
28772 /* Issue an error message if DECL is redeclared with different
28773 access than its original declaration [class.access.spec/3].
28774 This applies to nested classes, nested class templates and
28775 enumerations [class.mem/1]. */
28778 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28781 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28782 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28785 if ((TREE_PRIVATE (decl
)
28786 != (current_access_specifier
== access_private_node
))
28787 || (TREE_PROTECTED (decl
)
28788 != (current_access_specifier
== access_protected_node
)))
28789 error_at (location
, "%qD redeclared with different access", decl
);
28792 /* Look for the `template' keyword, as a syntactic disambiguator.
28793 Return TRUE iff it is present, in which case it will be
28797 cp_parser_optional_template_keyword (cp_parser
*parser
)
28799 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28801 /* In C++98 the `template' keyword can only be used within templates;
28802 outside templates the parser can always figure out what is a
28803 template and what is not. In C++11, per the resolution of DR 468,
28804 `template' is allowed in cases where it is not strictly necessary. */
28805 if (!processing_template_decl
28806 && pedantic
&& cxx_dialect
== cxx98
)
28808 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28809 pedwarn (token
->location
, OPT_Wpedantic
,
28810 "in C++98 %<template%> (as a disambiguator) is only "
28811 "allowed within templates");
28812 /* If this part of the token stream is rescanned, the same
28813 error message would be generated. So, we purge the token
28814 from the stream. */
28815 cp_lexer_purge_token (parser
->lexer
);
28820 /* Consume the `template' keyword. */
28821 cp_lexer_consume_token (parser
->lexer
);
28828 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28829 set PARSER->SCOPE, and perform other related actions. */
28832 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28834 struct tree_check
*check_value
;
28836 /* Get the stored value. */
28837 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28838 /* Set the scope from the stored value. */
28839 parser
->scope
= saved_checks_value (check_value
);
28840 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28841 parser
->object_scope
= NULL_TREE
;
28844 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28845 encounter the end of a block before what we were looking for. */
28848 cp_parser_cache_group (cp_parser
*parser
,
28849 enum cpp_ttype end
,
28854 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28856 /* Abort a parenthesized expression if we encounter a semicolon. */
28857 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28858 && token
->type
== CPP_SEMICOLON
)
28860 /* If we've reached the end of the file, stop. */
28861 if (token
->type
== CPP_EOF
28862 || (end
!= CPP_PRAGMA_EOL
28863 && token
->type
== CPP_PRAGMA_EOL
))
28865 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28866 /* We've hit the end of an enclosing block, so there's been some
28867 kind of syntax error. */
28870 /* Consume the token. */
28871 cp_lexer_consume_token (parser
->lexer
);
28872 /* See if it starts a new group. */
28873 if (token
->type
== CPP_OPEN_BRACE
)
28875 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28876 /* In theory this should probably check end == '}', but
28877 cp_parser_save_member_function_body needs it to exit
28878 after either '}' or ')' when called with ')'. */
28882 else if (token
->type
== CPP_OPEN_PAREN
)
28884 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28885 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28888 else if (token
->type
== CPP_PRAGMA
)
28889 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28890 else if (token
->type
== end
)
28895 /* Like above, for caching a default argument or NSDMI. Both of these are
28896 terminated by a non-nested comma, but it can be unclear whether or not a
28897 comma is nested in a template argument list unless we do more parsing.
28898 In order to handle this ambiguity, when we encounter a ',' after a '<'
28899 we try to parse what follows as a parameter-declaration-list (in the
28900 case of a default argument) or a member-declarator (in the case of an
28901 NSDMI). If that succeeds, then we stop caching. */
28904 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28906 unsigned depth
= 0;
28907 int maybe_template_id
= 0;
28908 cp_token
*first_token
;
28910 tree default_argument
;
28912 /* Add tokens until we have processed the entire default
28913 argument. We add the range [first_token, token). */
28914 first_token
= cp_lexer_peek_token (parser
->lexer
);
28915 if (first_token
->type
== CPP_OPEN_BRACE
)
28917 /* For list-initialization, this is straightforward. */
28918 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28919 token
= cp_lexer_peek_token (parser
->lexer
);
28925 /* Peek at the next token. */
28926 token
= cp_lexer_peek_token (parser
->lexer
);
28927 /* What we do depends on what token we have. */
28928 switch (token
->type
)
28930 /* In valid code, a default argument must be
28931 immediately followed by a `,' `)', or `...'. */
28933 if (depth
== 0 && maybe_template_id
)
28935 /* If we've seen a '<', we might be in a
28936 template-argument-list. Until Core issue 325 is
28937 resolved, we don't know how this situation ought
28938 to be handled, so try to DTRT. We check whether
28939 what comes after the comma is a valid parameter
28940 declaration list. If it is, then the comma ends
28941 the default argument; otherwise the default
28942 argument continues. */
28943 bool error
= false;
28946 /* Set ITALP so cp_parser_parameter_declaration_list
28947 doesn't decide to commit to this parse. */
28948 bool saved_italp
= parser
->in_template_argument_list_p
;
28949 parser
->in_template_argument_list_p
= true;
28951 cp_parser_parse_tentatively (parser
);
28955 /* Parse declarators until we reach a non-comma or
28956 somthing that cannot be an initializer.
28957 Just checking whether we're looking at a single
28958 declarator is insufficient. Consider:
28959 int var = tuple<T,U>::x;
28960 The template parameter 'U' looks exactly like a
28964 int ctor_dtor_or_conv_p
;
28965 cp_lexer_consume_token (parser
->lexer
);
28966 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28967 &ctor_dtor_or_conv_p
,
28968 /*parenthesized_p=*/NULL
,
28970 /*friend_p=*/false);
28971 peek
= cp_lexer_peek_token (parser
->lexer
);
28972 if (cp_parser_error_occurred (parser
))
28975 while (peek
->type
== CPP_COMMA
);
28976 /* If we met an '=' or ';' then the original comma
28977 was the end of the NSDMI. Otherwise assume
28978 we're still in the NSDMI. */
28979 error
= (peek
->type
!= CPP_EQ
28980 && peek
->type
!= CPP_SEMICOLON
);
28984 cp_lexer_consume_token (parser
->lexer
);
28985 begin_scope (sk_function_parms
, NULL_TREE
);
28986 cp_parser_parameter_declaration_list (parser
, &error
);
28987 pop_bindings_and_leave_scope ();
28989 if (!cp_parser_error_occurred (parser
) && !error
)
28991 cp_parser_abort_tentative_parse (parser
);
28993 parser
->in_template_argument_list_p
= saved_italp
;
28997 case CPP_CLOSE_PAREN
:
28999 /* If we run into a non-nested `;', `}', or `]',
29000 then the code is invalid -- but the default
29001 argument is certainly over. */
29002 case CPP_SEMICOLON
:
29003 case CPP_CLOSE_BRACE
:
29004 case CPP_CLOSE_SQUARE
:
29006 /* Handle correctly int n = sizeof ... ( p ); */
29007 && token
->type
!= CPP_ELLIPSIS
)
29009 /* Update DEPTH, if necessary. */
29010 else if (token
->type
== CPP_CLOSE_PAREN
29011 || token
->type
== CPP_CLOSE_BRACE
29012 || token
->type
== CPP_CLOSE_SQUARE
)
29016 case CPP_OPEN_PAREN
:
29017 case CPP_OPEN_SQUARE
:
29018 case CPP_OPEN_BRACE
:
29024 /* This might be the comparison operator, or it might
29025 start a template argument list. */
29026 ++maybe_template_id
;
29030 if (cxx_dialect
== cxx98
)
29032 /* Fall through for C++0x, which treats the `>>'
29033 operator like two `>' tokens in certain
29035 gcc_fallthrough ();
29040 /* This might be an operator, or it might close a
29041 template argument list. But if a previous '<'
29042 started a template argument list, this will have
29043 closed it, so we can't be in one anymore. */
29044 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29045 if (maybe_template_id
< 0)
29046 maybe_template_id
= 0;
29050 /* If we run out of tokens, issue an error message. */
29052 case CPP_PRAGMA_EOL
:
29053 error_at (token
->location
, "file ends in default argument");
29054 return error_mark_node
;
29058 /* In these cases, we should look for template-ids.
29059 For example, if the default argument is
29060 `X<int, double>()', we need to do name lookup to
29061 figure out whether or not `X' is a template; if
29062 so, the `,' does not end the default argument.
29064 That is not yet done. */
29071 /* If we've reached the end, stop. */
29075 /* Add the token to the token block. */
29076 token
= cp_lexer_consume_token (parser
->lexer
);
29079 /* Create a DEFAULT_ARG to represent the unparsed default
29081 default_argument
= make_node (DEFAULT_ARG
);
29082 DEFARG_TOKENS (default_argument
)
29083 = cp_token_cache_new (first_token
, token
);
29084 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29086 return default_argument
;
29089 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29092 defarg_location (tree default_argument
)
29094 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29095 location_t start
= tokens
->first
->location
;
29096 location_t end
= tokens
->last
->location
;
29097 return make_location (start
, start
, end
);
29100 /* Begin parsing tentatively. We always save tokens while parsing
29101 tentatively so that if the tentative parsing fails we can restore the
29105 cp_parser_parse_tentatively (cp_parser
* parser
)
29107 /* Enter a new parsing context. */
29108 parser
->context
= cp_parser_context_new (parser
->context
);
29109 /* Begin saving tokens. */
29110 cp_lexer_save_tokens (parser
->lexer
);
29111 /* In order to avoid repetitive access control error messages,
29112 access checks are queued up until we are no longer parsing
29114 push_deferring_access_checks (dk_deferred
);
29117 /* Commit to the currently active tentative parse. */
29120 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29122 cp_parser_context
*context
;
29125 /* Mark all of the levels as committed. */
29126 lexer
= parser
->lexer
;
29127 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29129 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29131 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29132 while (!cp_lexer_saving_tokens (lexer
))
29133 lexer
= lexer
->next
;
29134 cp_lexer_commit_tokens (lexer
);
29138 /* Commit to the topmost currently active tentative parse.
29140 Note that this function shouldn't be called when there are
29141 irreversible side-effects while in a tentative state. For
29142 example, we shouldn't create a permanent entry in the symbol
29143 table, or issue an error message that might not apply if the
29144 tentative parse is aborted. */
29147 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29149 cp_parser_context
*context
= parser
->context
;
29150 cp_lexer
*lexer
= parser
->lexer
;
29154 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29156 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29158 while (!cp_lexer_saving_tokens (lexer
))
29159 lexer
= lexer
->next
;
29160 cp_lexer_commit_tokens (lexer
);
29164 /* Abort the currently active tentative parse. All consumed tokens
29165 will be rolled back, and no diagnostics will be issued. */
29168 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29170 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29171 || errorcount
> 0);
29172 cp_parser_simulate_error (parser
);
29173 /* Now, pretend that we want to see if the construct was
29174 successfully parsed. */
29175 cp_parser_parse_definitely (parser
);
29178 /* Stop parsing tentatively. If a parse error has occurred, restore the
29179 token stream. Otherwise, commit to the tokens we have consumed.
29180 Returns true if no error occurred; false otherwise. */
29183 cp_parser_parse_definitely (cp_parser
* parser
)
29185 bool error_occurred
;
29186 cp_parser_context
*context
;
29188 /* Remember whether or not an error occurred, since we are about to
29189 destroy that information. */
29190 error_occurred
= cp_parser_error_occurred (parser
);
29191 /* Remove the topmost context from the stack. */
29192 context
= parser
->context
;
29193 parser
->context
= context
->next
;
29194 /* If no parse errors occurred, commit to the tentative parse. */
29195 if (!error_occurred
)
29197 /* Commit to the tokens read tentatively, unless that was
29199 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29200 cp_lexer_commit_tokens (parser
->lexer
);
29202 pop_to_parent_deferring_access_checks ();
29204 /* Otherwise, if errors occurred, roll back our state so that things
29205 are just as they were before we began the tentative parse. */
29208 cp_lexer_rollback_tokens (parser
->lexer
);
29209 pop_deferring_access_checks ();
29211 /* Add the context to the front of the free list. */
29212 context
->next
= cp_parser_context_free_list
;
29213 cp_parser_context_free_list
= context
;
29215 return !error_occurred
;
29218 /* Returns true if we are parsing tentatively and are not committed to
29219 this tentative parse. */
29222 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29224 return (cp_parser_parsing_tentatively (parser
)
29225 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29228 /* Returns nonzero iff an error has occurred during the most recent
29229 tentative parse. */
29232 cp_parser_error_occurred (cp_parser
* parser
)
29234 return (cp_parser_parsing_tentatively (parser
)
29235 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29238 /* Returns nonzero if GNU extensions are allowed. */
29241 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29243 return parser
->allow_gnu_extensions_p
;
29246 /* Objective-C++ Productions */
29249 /* Parse an Objective-C expression, which feeds into a primary-expression
29253 objc-message-expression
29254 objc-string-literal
29255 objc-encode-expression
29256 objc-protocol-expression
29257 objc-selector-expression
29259 Returns a tree representation of the expression. */
29262 cp_parser_objc_expression (cp_parser
* parser
)
29264 /* Try to figure out what kind of declaration is present. */
29265 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29269 case CPP_OPEN_SQUARE
:
29270 return cp_parser_objc_message_expression (parser
);
29272 case CPP_OBJC_STRING
:
29273 kwd
= cp_lexer_consume_token (parser
->lexer
);
29274 return objc_build_string_object (kwd
->u
.value
);
29277 switch (kwd
->keyword
)
29279 case RID_AT_ENCODE
:
29280 return cp_parser_objc_encode_expression (parser
);
29282 case RID_AT_PROTOCOL
:
29283 return cp_parser_objc_protocol_expression (parser
);
29285 case RID_AT_SELECTOR
:
29286 return cp_parser_objc_selector_expression (parser
);
29293 error_at (kwd
->location
,
29294 "misplaced %<@%D%> Objective-C++ construct",
29296 cp_parser_skip_to_end_of_block_or_statement (parser
);
29299 return error_mark_node
;
29302 /* Parse an Objective-C message expression.
29304 objc-message-expression:
29305 [ objc-message-receiver objc-message-args ]
29307 Returns a representation of an Objective-C message. */
29310 cp_parser_objc_message_expression (cp_parser
* parser
)
29312 tree receiver
, messageargs
;
29314 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29315 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29316 receiver
= cp_parser_objc_message_receiver (parser
);
29317 messageargs
= cp_parser_objc_message_args (parser
);
29318 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29319 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29321 tree result
= objc_build_message_expr (receiver
, messageargs
);
29323 /* Construct a location e.g.
29326 ranging from the '[' to the ']', with the caret at the start. */
29327 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29328 protected_set_expr_location (result
, combined_loc
);
29333 /* Parse an objc-message-receiver.
29335 objc-message-receiver:
29337 simple-type-specifier
29339 Returns a representation of the type or expression. */
29342 cp_parser_objc_message_receiver (cp_parser
* parser
)
29346 /* An Objective-C message receiver may be either (1) a type
29347 or (2) an expression. */
29348 cp_parser_parse_tentatively (parser
);
29349 rcv
= cp_parser_expression (parser
);
29351 /* If that worked out, fine. */
29352 if (cp_parser_parse_definitely (parser
))
29355 cp_parser_parse_tentatively (parser
);
29356 rcv
= cp_parser_simple_type_specifier (parser
,
29357 /*decl_specs=*/NULL
,
29358 CP_PARSER_FLAGS_NONE
);
29360 if (cp_parser_parse_definitely (parser
))
29361 return objc_get_class_reference (rcv
);
29363 cp_parser_error (parser
, "objective-c++ message receiver expected");
29364 return error_mark_node
;
29367 /* Parse the arguments and selectors comprising an Objective-C message.
29372 objc-selector-args , objc-comma-args
29374 objc-selector-args:
29375 objc-selector [opt] : assignment-expression
29376 objc-selector-args objc-selector [opt] : assignment-expression
29379 assignment-expression
29380 objc-comma-args , assignment-expression
29382 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29383 selector arguments and TREE_VALUE containing a list of comma
29387 cp_parser_objc_message_args (cp_parser
* parser
)
29389 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29390 bool maybe_unary_selector_p
= true;
29391 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29393 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29395 tree selector
= NULL_TREE
, arg
;
29397 if (token
->type
!= CPP_COLON
)
29398 selector
= cp_parser_objc_selector (parser
);
29400 /* Detect if we have a unary selector. */
29401 if (maybe_unary_selector_p
29402 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29403 return build_tree_list (selector
, NULL_TREE
);
29405 maybe_unary_selector_p
= false;
29406 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29407 arg
= cp_parser_assignment_expression (parser
);
29410 = chainon (sel_args
,
29411 build_tree_list (selector
, arg
));
29413 token
= cp_lexer_peek_token (parser
->lexer
);
29416 /* Handle non-selector arguments, if any. */
29417 while (token
->type
== CPP_COMMA
)
29421 cp_lexer_consume_token (parser
->lexer
);
29422 arg
= cp_parser_assignment_expression (parser
);
29425 = chainon (addl_args
,
29426 build_tree_list (NULL_TREE
, arg
));
29428 token
= cp_lexer_peek_token (parser
->lexer
);
29431 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29433 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29434 return build_tree_list (error_mark_node
, error_mark_node
);
29437 return build_tree_list (sel_args
, addl_args
);
29440 /* Parse an Objective-C encode expression.
29442 objc-encode-expression:
29443 @encode objc-typename
29445 Returns an encoded representation of the type argument. */
29448 cp_parser_objc_encode_expression (cp_parser
* parser
)
29452 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29454 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29455 matching_parens parens
;
29456 parens
.require_open (parser
);
29457 token
= cp_lexer_peek_token (parser
->lexer
);
29458 type
= complete_type (cp_parser_type_id (parser
));
29459 parens
.require_close (parser
);
29463 error_at (token
->location
,
29464 "%<@encode%> must specify a type as an argument");
29465 return error_mark_node
;
29468 /* This happens if we find @encode(T) (where T is a template
29469 typename or something dependent on a template typename) when
29470 parsing a template. In that case, we can't compile it
29471 immediately, but we rather create an AT_ENCODE_EXPR which will
29472 need to be instantiated when the template is used.
29474 if (dependent_type_p (type
))
29476 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29477 TREE_READONLY (value
) = 1;
29482 /* Build a location of the form:
29485 with caret==start at the @ token, finishing at the close paren. */
29486 location_t combined_loc
29487 = make_location (start_loc
, start_loc
,
29488 cp_lexer_previous_token (parser
->lexer
)->location
);
29490 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29493 /* Parse an Objective-C @defs expression. */
29496 cp_parser_objc_defs_expression (cp_parser
*parser
)
29500 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29501 matching_parens parens
;
29502 parens
.require_open (parser
);
29503 name
= cp_parser_identifier (parser
);
29504 parens
.require_close (parser
);
29506 return objc_get_class_ivars (name
);
29509 /* Parse an Objective-C protocol expression.
29511 objc-protocol-expression:
29512 @protocol ( identifier )
29514 Returns a representation of the protocol expression. */
29517 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29520 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29522 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29523 matching_parens parens
;
29524 parens
.require_open (parser
);
29525 proto
= cp_parser_identifier (parser
);
29526 parens
.require_close (parser
);
29528 /* Build a location of the form:
29531 with caret==start at the @ token, finishing at the close paren. */
29532 location_t combined_loc
29533 = make_location (start_loc
, start_loc
,
29534 cp_lexer_previous_token (parser
->lexer
)->location
);
29535 tree result
= objc_build_protocol_expr (proto
);
29536 protected_set_expr_location (result
, combined_loc
);
29540 /* Parse an Objective-C selector expression.
29542 objc-selector-expression:
29543 @selector ( objc-method-signature )
29545 objc-method-signature:
29551 objc-selector-seq objc-selector :
29553 Returns a representation of the method selector. */
29556 cp_parser_objc_selector_expression (cp_parser
* parser
)
29558 tree sel_seq
= NULL_TREE
;
29559 bool maybe_unary_selector_p
= true;
29561 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29563 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29564 matching_parens parens
;
29565 parens
.require_open (parser
);
29566 token
= cp_lexer_peek_token (parser
->lexer
);
29568 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29569 || token
->type
== CPP_SCOPE
)
29571 tree selector
= NULL_TREE
;
29573 if (token
->type
!= CPP_COLON
29574 || token
->type
== CPP_SCOPE
)
29575 selector
= cp_parser_objc_selector (parser
);
29577 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29578 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29580 /* Detect if we have a unary selector. */
29581 if (maybe_unary_selector_p
)
29583 sel_seq
= selector
;
29584 goto finish_selector
;
29588 cp_parser_error (parser
, "expected %<:%>");
29591 maybe_unary_selector_p
= false;
29592 token
= cp_lexer_consume_token (parser
->lexer
);
29594 if (token
->type
== CPP_SCOPE
)
29597 = chainon (sel_seq
,
29598 build_tree_list (selector
, NULL_TREE
));
29600 = chainon (sel_seq
,
29601 build_tree_list (NULL_TREE
, NULL_TREE
));
29605 = chainon (sel_seq
,
29606 build_tree_list (selector
, NULL_TREE
));
29608 token
= cp_lexer_peek_token (parser
->lexer
);
29612 parens
.require_close (parser
);
29615 /* Build a location of the form:
29618 with caret==start at the @ token, finishing at the close paren. */
29619 location_t combined_loc
29620 = make_location (loc
, loc
,
29621 cp_lexer_previous_token (parser
->lexer
)->location
);
29622 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29623 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29624 protected_set_expr_location (result
, combined_loc
);
29628 /* Parse a list of identifiers.
29630 objc-identifier-list:
29632 objc-identifier-list , identifier
29634 Returns a TREE_LIST of identifier nodes. */
29637 cp_parser_objc_identifier_list (cp_parser
* parser
)
29643 identifier
= cp_parser_identifier (parser
);
29644 if (identifier
== error_mark_node
)
29645 return error_mark_node
;
29647 list
= build_tree_list (NULL_TREE
, identifier
);
29648 sep
= cp_lexer_peek_token (parser
->lexer
);
29650 while (sep
->type
== CPP_COMMA
)
29652 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29653 identifier
= cp_parser_identifier (parser
);
29654 if (identifier
== error_mark_node
)
29657 list
= chainon (list
, build_tree_list (NULL_TREE
,
29659 sep
= cp_lexer_peek_token (parser
->lexer
);
29665 /* Parse an Objective-C alias declaration.
29667 objc-alias-declaration:
29668 @compatibility_alias identifier identifier ;
29670 This function registers the alias mapping with the Objective-C front end.
29671 It returns nothing. */
29674 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29678 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29679 alias
= cp_parser_identifier (parser
);
29680 orig
= cp_parser_identifier (parser
);
29681 objc_declare_alias (alias
, orig
);
29682 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29685 /* Parse an Objective-C class forward-declaration.
29687 objc-class-declaration:
29688 @class objc-identifier-list ;
29690 The function registers the forward declarations with the Objective-C
29691 front end. It returns nothing. */
29694 cp_parser_objc_class_declaration (cp_parser
* parser
)
29696 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29701 id
= cp_parser_identifier (parser
);
29702 if (id
== error_mark_node
)
29705 objc_declare_class (id
);
29707 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29708 cp_lexer_consume_token (parser
->lexer
);
29712 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29715 /* Parse a list of Objective-C protocol references.
29717 objc-protocol-refs-opt:
29718 objc-protocol-refs [opt]
29720 objc-protocol-refs:
29721 < objc-identifier-list >
29723 Returns a TREE_LIST of identifiers, if any. */
29726 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29728 tree protorefs
= NULL_TREE
;
29730 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29732 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29733 protorefs
= cp_parser_objc_identifier_list (parser
);
29734 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29740 /* Parse a Objective-C visibility specification. */
29743 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29745 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29747 switch (vis
->keyword
)
29749 case RID_AT_PRIVATE
:
29750 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29752 case RID_AT_PROTECTED
:
29753 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29755 case RID_AT_PUBLIC
:
29756 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29758 case RID_AT_PACKAGE
:
29759 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29765 /* Eat '@private'/'@protected'/'@public'. */
29766 cp_lexer_consume_token (parser
->lexer
);
29769 /* Parse an Objective-C method type. Return 'true' if it is a class
29770 (+) method, and 'false' if it is an instance (-) method. */
29773 cp_parser_objc_method_type (cp_parser
* parser
)
29775 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29781 /* Parse an Objective-C protocol qualifier. */
29784 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29786 tree quals
= NULL_TREE
, node
;
29787 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29789 node
= token
->u
.value
;
29791 while (node
&& identifier_p (node
)
29792 && (node
== ridpointers
[(int) RID_IN
]
29793 || node
== ridpointers
[(int) RID_OUT
]
29794 || node
== ridpointers
[(int) RID_INOUT
]
29795 || node
== ridpointers
[(int) RID_BYCOPY
]
29796 || node
== ridpointers
[(int) RID_BYREF
]
29797 || node
== ridpointers
[(int) RID_ONEWAY
]))
29799 quals
= tree_cons (NULL_TREE
, node
, quals
);
29800 cp_lexer_consume_token (parser
->lexer
);
29801 token
= cp_lexer_peek_token (parser
->lexer
);
29802 node
= token
->u
.value
;
29808 /* Parse an Objective-C typename. */
29811 cp_parser_objc_typename (cp_parser
* parser
)
29813 tree type_name
= NULL_TREE
;
29815 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29817 tree proto_quals
, cp_type
= NULL_TREE
;
29819 matching_parens parens
;
29820 parens
.consume_open (parser
); /* Eat '('. */
29821 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29823 /* An ObjC type name may consist of just protocol qualifiers, in which
29824 case the type shall default to 'id'. */
29825 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29827 cp_type
= cp_parser_type_id (parser
);
29829 /* If the type could not be parsed, an error has already
29830 been produced. For error recovery, behave as if it had
29831 not been specified, which will use the default type
29833 if (cp_type
== error_mark_node
)
29835 cp_type
= NULL_TREE
;
29836 /* We need to skip to the closing parenthesis as
29837 cp_parser_type_id() does not seem to do it for
29839 cp_parser_skip_to_closing_parenthesis (parser
,
29840 /*recovering=*/true,
29841 /*or_comma=*/false,
29842 /*consume_paren=*/false);
29846 parens
.require_close (parser
);
29847 type_name
= build_tree_list (proto_quals
, cp_type
);
29853 /* Check to see if TYPE refers to an Objective-C selector name. */
29856 cp_parser_objc_selector_p (enum cpp_ttype type
)
29858 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29859 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29860 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29861 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29862 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29865 /* Parse an Objective-C selector. */
29868 cp_parser_objc_selector (cp_parser
* parser
)
29870 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29872 if (!cp_parser_objc_selector_p (token
->type
))
29874 error_at (token
->location
, "invalid Objective-C++ selector name");
29875 return error_mark_node
;
29878 /* C++ operator names are allowed to appear in ObjC selectors. */
29879 switch (token
->type
)
29881 case CPP_AND_AND
: return get_identifier ("and");
29882 case CPP_AND_EQ
: return get_identifier ("and_eq");
29883 case CPP_AND
: return get_identifier ("bitand");
29884 case CPP_OR
: return get_identifier ("bitor");
29885 case CPP_COMPL
: return get_identifier ("compl");
29886 case CPP_NOT
: return get_identifier ("not");
29887 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29888 case CPP_OR_OR
: return get_identifier ("or");
29889 case CPP_OR_EQ
: return get_identifier ("or_eq");
29890 case CPP_XOR
: return get_identifier ("xor");
29891 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29892 default: return token
->u
.value
;
29896 /* Parse an Objective-C params list. */
29899 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29901 tree params
= NULL_TREE
;
29902 bool maybe_unary_selector_p
= true;
29903 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29905 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29907 tree selector
= NULL_TREE
, type_name
, identifier
;
29908 tree parm_attr
= NULL_TREE
;
29910 if (token
->keyword
== RID_ATTRIBUTE
)
29913 if (token
->type
!= CPP_COLON
)
29914 selector
= cp_parser_objc_selector (parser
);
29916 /* Detect if we have a unary selector. */
29917 if (maybe_unary_selector_p
29918 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29920 params
= selector
; /* Might be followed by attributes. */
29924 maybe_unary_selector_p
= false;
29925 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29927 /* Something went quite wrong. There should be a colon
29928 here, but there is not. Stop parsing parameters. */
29931 type_name
= cp_parser_objc_typename (parser
);
29932 /* New ObjC allows attributes on parameters too. */
29933 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29934 parm_attr
= cp_parser_attributes_opt (parser
);
29935 identifier
= cp_parser_identifier (parser
);
29939 objc_build_keyword_decl (selector
,
29944 token
= cp_lexer_peek_token (parser
->lexer
);
29947 if (params
== NULL_TREE
)
29949 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29950 return error_mark_node
;
29953 /* We allow tail attributes for the method. */
29954 if (token
->keyword
== RID_ATTRIBUTE
)
29956 *attributes
= cp_parser_attributes_opt (parser
);
29957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29958 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29960 cp_parser_error (parser
,
29961 "method attributes must be specified at the end");
29962 return error_mark_node
;
29965 if (params
== NULL_TREE
)
29967 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29968 return error_mark_node
;
29973 /* Parse the non-keyword Objective-C params. */
29976 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29979 tree params
= make_node (TREE_LIST
);
29980 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29981 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29983 while (token
->type
== CPP_COMMA
)
29985 cp_parameter_declarator
*parmdecl
;
29988 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29989 token
= cp_lexer_peek_token (parser
->lexer
);
29991 if (token
->type
== CPP_ELLIPSIS
)
29993 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29995 token
= cp_lexer_peek_token (parser
->lexer
);
29999 /* TODO: parse attributes for tail parameters. */
30000 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30001 parm
= grokdeclarator (parmdecl
->declarator
,
30002 &parmdecl
->decl_specifiers
,
30003 PARM
, /*initialized=*/0,
30004 /*attrlist=*/NULL
);
30006 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30007 token
= cp_lexer_peek_token (parser
->lexer
);
30010 /* We allow tail attributes for the method. */
30011 if (token
->keyword
== RID_ATTRIBUTE
)
30013 if (*attributes
== NULL_TREE
)
30015 *attributes
= cp_parser_attributes_opt (parser
);
30016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30017 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30021 /* We have an error, but parse the attributes, so that we can
30023 *attributes
= cp_parser_attributes_opt (parser
);
30025 cp_parser_error (parser
,
30026 "method attributes must be specified at the end");
30027 return error_mark_node
;
30033 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30036 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30040 /* If the next token is `extern' and the following token is a string
30041 literal, then we have a linkage specification. */
30042 if (token
->keyword
== RID_EXTERN
30043 && cp_parser_is_pure_string_literal
30044 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30045 cp_parser_linkage_specification (parser
);
30046 /* Handle #pragma, if any. */
30047 else if (token
->type
== CPP_PRAGMA
)
30048 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30049 /* Allow stray semicolons. */
30050 else if (token
->type
== CPP_SEMICOLON
)
30051 cp_lexer_consume_token (parser
->lexer
);
30052 /* Mark methods as optional or required, when building protocols. */
30053 else if (token
->keyword
== RID_AT_OPTIONAL
)
30055 cp_lexer_consume_token (parser
->lexer
);
30056 objc_set_method_opt (true);
30058 else if (token
->keyword
== RID_AT_REQUIRED
)
30060 cp_lexer_consume_token (parser
->lexer
);
30061 objc_set_method_opt (false);
30063 else if (token
->keyword
== RID_NAMESPACE
)
30064 cp_parser_namespace_definition (parser
);
30065 /* Other stray characters must generate errors. */
30066 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30068 cp_lexer_consume_token (parser
->lexer
);
30069 error ("stray %qs between Objective-C++ methods",
30070 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30072 /* Finally, try to parse a block-declaration, or a function-definition. */
30074 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30077 /* Parse a method signature. */
30080 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30082 tree rettype
, kwdparms
, optparms
;
30083 bool ellipsis
= false;
30084 bool is_class_method
;
30086 is_class_method
= cp_parser_objc_method_type (parser
);
30087 rettype
= cp_parser_objc_typename (parser
);
30088 *attributes
= NULL_TREE
;
30089 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30090 if (kwdparms
== error_mark_node
)
30091 return error_mark_node
;
30092 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30093 if (optparms
== error_mark_node
)
30094 return error_mark_node
;
30096 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30100 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30103 cp_lexer_save_tokens (parser
->lexer
);
30104 tattr
= cp_parser_attributes_opt (parser
);
30105 gcc_assert (tattr
) ;
30107 /* If the attributes are followed by a method introducer, this is not allowed.
30108 Dump the attributes and flag the situation. */
30109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30110 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30113 /* Otherwise, the attributes introduce some interstitial code, possibly so
30114 rewind to allow that check. */
30115 cp_lexer_rollback_tokens (parser
->lexer
);
30119 /* Parse an Objective-C method prototype list. */
30122 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30124 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30126 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30128 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30130 tree attributes
, sig
;
30131 bool is_class_method
;
30132 if (token
->type
== CPP_PLUS
)
30133 is_class_method
= true;
30135 is_class_method
= false;
30136 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30137 if (sig
== error_mark_node
)
30139 cp_parser_skip_to_end_of_block_or_statement (parser
);
30140 token
= cp_lexer_peek_token (parser
->lexer
);
30143 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30144 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30146 else if (token
->keyword
== RID_AT_PROPERTY
)
30147 cp_parser_objc_at_property_declaration (parser
);
30148 else if (token
->keyword
== RID_ATTRIBUTE
30149 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30150 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30152 "prefix attributes are ignored for methods");
30154 /* Allow for interspersed non-ObjC++ code. */
30155 cp_parser_objc_interstitial_code (parser
);
30157 token
= cp_lexer_peek_token (parser
->lexer
);
30160 if (token
->type
!= CPP_EOF
)
30161 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30163 cp_parser_error (parser
, "expected %<@end%>");
30165 objc_finish_interface ();
30168 /* Parse an Objective-C method definition list. */
30171 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30173 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30175 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30179 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30182 tree sig
, attribute
;
30183 bool is_class_method
;
30184 if (token
->type
== CPP_PLUS
)
30185 is_class_method
= true;
30187 is_class_method
= false;
30188 push_deferring_access_checks (dk_deferred
);
30189 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30190 if (sig
== error_mark_node
)
30192 cp_parser_skip_to_end_of_block_or_statement (parser
);
30193 token
= cp_lexer_peek_token (parser
->lexer
);
30196 objc_start_method_definition (is_class_method
, sig
, attribute
,
30199 /* For historical reasons, we accept an optional semicolon. */
30200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30201 cp_lexer_consume_token (parser
->lexer
);
30203 ptk
= cp_lexer_peek_token (parser
->lexer
);
30204 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30205 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30207 perform_deferred_access_checks (tf_warning_or_error
);
30208 stop_deferring_access_checks ();
30209 meth
= cp_parser_function_definition_after_declarator (parser
,
30211 pop_deferring_access_checks ();
30212 objc_finish_method_definition (meth
);
30215 /* The following case will be removed once @synthesize is
30216 completely implemented. */
30217 else if (token
->keyword
== RID_AT_PROPERTY
)
30218 cp_parser_objc_at_property_declaration (parser
);
30219 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30220 cp_parser_objc_at_synthesize_declaration (parser
);
30221 else if (token
->keyword
== RID_AT_DYNAMIC
)
30222 cp_parser_objc_at_dynamic_declaration (parser
);
30223 else if (token
->keyword
== RID_ATTRIBUTE
30224 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30225 warning_at (token
->location
, OPT_Wattributes
,
30226 "prefix attributes are ignored for methods");
30228 /* Allow for interspersed non-ObjC++ code. */
30229 cp_parser_objc_interstitial_code (parser
);
30231 token
= cp_lexer_peek_token (parser
->lexer
);
30234 if (token
->type
!= CPP_EOF
)
30235 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30237 cp_parser_error (parser
, "expected %<@end%>");
30239 objc_finish_implementation ();
30242 /* Parse Objective-C ivars. */
30245 cp_parser_objc_class_ivars (cp_parser
* parser
)
30247 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30249 if (token
->type
!= CPP_OPEN_BRACE
)
30250 return; /* No ivars specified. */
30252 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30253 token
= cp_lexer_peek_token (parser
->lexer
);
30255 while (token
->type
!= CPP_CLOSE_BRACE
30256 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30258 cp_decl_specifier_seq declspecs
;
30259 int decl_class_or_enum_p
;
30260 tree prefix_attributes
;
30262 cp_parser_objc_visibility_spec (parser
);
30264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30267 cp_parser_decl_specifier_seq (parser
,
30268 CP_PARSER_FLAGS_OPTIONAL
,
30270 &decl_class_or_enum_p
);
30272 /* auto, register, static, extern, mutable. */
30273 if (declspecs
.storage_class
!= sc_none
)
30275 cp_parser_error (parser
, "invalid type for instance variable");
30276 declspecs
.storage_class
= sc_none
;
30279 /* thread_local. */
30280 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30282 cp_parser_error (parser
, "invalid type for instance variable");
30283 declspecs
.locations
[ds_thread
] = 0;
30287 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30289 cp_parser_error (parser
, "invalid type for instance variable");
30290 declspecs
.locations
[ds_typedef
] = 0;
30293 prefix_attributes
= declspecs
.attributes
;
30294 declspecs
.attributes
= NULL_TREE
;
30296 /* Keep going until we hit the `;' at the end of the
30298 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30300 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30301 cp_declarator
*declarator
= NULL
;
30302 int ctor_dtor_or_conv_p
;
30304 /* Check for a (possibly unnamed) bitfield declaration. */
30305 token
= cp_lexer_peek_token (parser
->lexer
);
30306 if (token
->type
== CPP_COLON
)
30309 if (token
->type
== CPP_NAME
30310 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30313 /* Get the name of the bitfield. */
30314 declarator
= make_id_declarator (NULL_TREE
,
30315 cp_parser_identifier (parser
),
30319 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30320 /* Get the width of the bitfield. */
30322 = cp_parser_constant_expression (parser
);
30326 /* Parse the declarator. */
30328 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30329 &ctor_dtor_or_conv_p
,
30330 /*parenthesized_p=*/NULL
,
30331 /*member_p=*/false,
30332 /*friend_p=*/false);
30335 /* Look for attributes that apply to the ivar. */
30336 attributes
= cp_parser_attributes_opt (parser
);
30337 /* Remember which attributes are prefix attributes and
30339 first_attribute
= attributes
;
30340 /* Combine the attributes. */
30341 attributes
= attr_chainon (prefix_attributes
, attributes
);
30344 /* Create the bitfield declaration. */
30345 decl
= grokbitfield (declarator
, &declspecs
,
30346 width
, NULL_TREE
, attributes
);
30348 decl
= grokfield (declarator
, &declspecs
,
30349 NULL_TREE
, /*init_const_expr_p=*/false,
30350 NULL_TREE
, attributes
);
30352 /* Add the instance variable. */
30353 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30354 objc_add_instance_variable (decl
);
30356 /* Reset PREFIX_ATTRIBUTES. */
30357 if (attributes
!= error_mark_node
)
30359 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30360 attributes
= TREE_CHAIN (attributes
);
30362 TREE_CHAIN (attributes
) = NULL_TREE
;
30365 token
= cp_lexer_peek_token (parser
->lexer
);
30367 if (token
->type
== CPP_COMMA
)
30369 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30375 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30376 token
= cp_lexer_peek_token (parser
->lexer
);
30379 if (token
->keyword
== RID_AT_END
)
30380 cp_parser_error (parser
, "expected %<}%>");
30382 /* Do not consume the RID_AT_END, so it will be read again as terminating
30383 the @interface of @implementation. */
30384 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30385 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30387 /* For historical reasons, we accept an optional semicolon. */
30388 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30389 cp_lexer_consume_token (parser
->lexer
);
30392 /* Parse an Objective-C protocol declaration. */
30395 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30397 tree proto
, protorefs
;
30400 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30401 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30403 tok
= cp_lexer_peek_token (parser
->lexer
);
30404 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30405 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30409 /* See if we have a forward declaration or a definition. */
30410 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30412 /* Try a forward declaration first. */
30413 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30419 id
= cp_parser_identifier (parser
);
30420 if (id
== error_mark_node
)
30423 objc_declare_protocol (id
, attributes
);
30425 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30426 cp_lexer_consume_token (parser
->lexer
);
30430 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30433 /* Ok, we got a full-fledged definition (or at least should). */
30436 proto
= cp_parser_identifier (parser
);
30437 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30438 objc_start_protocol (proto
, protorefs
, attributes
);
30439 cp_parser_objc_method_prototype_list (parser
);
30443 /* Parse an Objective-C superclass or category. */
30446 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30449 tree
*categ
, bool *is_class_extension
)
30451 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30453 *super
= *categ
= NULL_TREE
;
30454 *is_class_extension
= false;
30455 if (next
->type
== CPP_COLON
)
30457 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30458 *super
= cp_parser_identifier (parser
);
30460 else if (next
->type
== CPP_OPEN_PAREN
)
30462 matching_parens parens
;
30463 parens
.consume_open (parser
); /* Eat '('. */
30465 /* If there is no category name, and this is an @interface, we
30466 have a class extension. */
30467 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30469 *categ
= NULL_TREE
;
30470 *is_class_extension
= true;
30473 *categ
= cp_parser_identifier (parser
);
30475 parens
.require_close (parser
);
30479 /* Parse an Objective-C class interface. */
30482 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30484 tree name
, super
, categ
, protos
;
30485 bool is_class_extension
;
30487 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30488 name
= cp_parser_identifier (parser
);
30489 if (name
== error_mark_node
)
30491 /* It's hard to recover because even if valid @interface stuff
30492 is to follow, we can't compile it (or validate it) if we
30493 don't even know which class it refers to. Let's assume this
30494 was a stray '@interface' token in the stream and skip it.
30498 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30499 &is_class_extension
);
30500 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30502 /* We have either a class or a category on our hands. */
30503 if (categ
|| is_class_extension
)
30504 objc_start_category_interface (name
, categ
, protos
, attributes
);
30507 objc_start_class_interface (name
, super
, protos
, attributes
);
30508 /* Handle instance variable declarations, if any. */
30509 cp_parser_objc_class_ivars (parser
);
30510 objc_continue_interface ();
30513 cp_parser_objc_method_prototype_list (parser
);
30516 /* Parse an Objective-C class implementation. */
30519 cp_parser_objc_class_implementation (cp_parser
* parser
)
30521 tree name
, super
, categ
;
30522 bool is_class_extension
;
30524 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30525 name
= cp_parser_identifier (parser
);
30526 if (name
== error_mark_node
)
30528 /* It's hard to recover because even if valid @implementation
30529 stuff is to follow, we can't compile it (or validate it) if
30530 we don't even know which class it refers to. Let's assume
30531 this was a stray '@implementation' token in the stream and
30536 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30537 &is_class_extension
);
30539 /* We have either a class or a category on our hands. */
30541 objc_start_category_implementation (name
, categ
);
30544 objc_start_class_implementation (name
, super
);
30545 /* Handle instance variable declarations, if any. */
30546 cp_parser_objc_class_ivars (parser
);
30547 objc_continue_implementation ();
30550 cp_parser_objc_method_definition_list (parser
);
30553 /* Consume the @end token and finish off the implementation. */
30556 cp_parser_objc_end_implementation (cp_parser
* parser
)
30558 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30559 objc_finish_implementation ();
30562 /* Parse an Objective-C declaration. */
30565 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30567 /* Try to figure out what kind of declaration is present. */
30568 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30571 switch (kwd
->keyword
)
30576 error_at (kwd
->location
, "attributes may not be specified before"
30577 " the %<@%D%> Objective-C++ keyword",
30581 case RID_AT_IMPLEMENTATION
:
30582 warning_at (kwd
->location
, OPT_Wattributes
,
30583 "prefix attributes are ignored before %<@%D%>",
30590 switch (kwd
->keyword
)
30593 cp_parser_objc_alias_declaration (parser
);
30596 cp_parser_objc_class_declaration (parser
);
30598 case RID_AT_PROTOCOL
:
30599 cp_parser_objc_protocol_declaration (parser
, attributes
);
30601 case RID_AT_INTERFACE
:
30602 cp_parser_objc_class_interface (parser
, attributes
);
30604 case RID_AT_IMPLEMENTATION
:
30605 cp_parser_objc_class_implementation (parser
);
30608 cp_parser_objc_end_implementation (parser
);
30611 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30613 cp_parser_skip_to_end_of_block_or_statement (parser
);
30617 /* Parse an Objective-C try-catch-finally statement.
30619 objc-try-catch-finally-stmt:
30620 @try compound-statement objc-catch-clause-seq [opt]
30621 objc-finally-clause [opt]
30623 objc-catch-clause-seq:
30624 objc-catch-clause objc-catch-clause-seq [opt]
30627 @catch ( objc-exception-declaration ) compound-statement
30629 objc-finally-clause:
30630 @finally compound-statement
30632 objc-exception-declaration:
30633 parameter-declaration
30636 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30640 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30641 for C. Keep them in sync. */
30644 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30646 location_t location
;
30649 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30650 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30651 objc_maybe_warn_exceptions (location
);
30652 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30653 node, lest it get absorbed into the surrounding block. */
30654 stmt
= push_stmt_list ();
30655 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30656 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30658 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30660 cp_parameter_declarator
*parm
;
30661 tree parameter_declaration
= error_mark_node
;
30662 bool seen_open_paren
= false;
30663 matching_parens parens
;
30665 cp_lexer_consume_token (parser
->lexer
);
30666 if (parens
.require_open (parser
))
30667 seen_open_paren
= true;
30668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30670 /* We have "@catch (...)" (where the '...' are literally
30671 what is in the code). Skip the '...'.
30672 parameter_declaration is set to NULL_TREE, and
30673 objc_being_catch_clauses() knows that that means
30675 cp_lexer_consume_token (parser
->lexer
);
30676 parameter_declaration
= NULL_TREE
;
30680 /* We have "@catch (NSException *exception)" or something
30681 like that. Parse the parameter declaration. */
30682 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30684 parameter_declaration
= error_mark_node
;
30686 parameter_declaration
= grokdeclarator (parm
->declarator
,
30687 &parm
->decl_specifiers
,
30688 PARM
, /*initialized=*/0,
30689 /*attrlist=*/NULL
);
30691 if (seen_open_paren
)
30692 parens
.require_close (parser
);
30695 /* If there was no open parenthesis, we are recovering from
30696 an error, and we are trying to figure out what mistake
30697 the user has made. */
30699 /* If there is an immediate closing parenthesis, the user
30700 probably forgot the opening one (ie, they typed "@catch
30701 NSException *e)". Parse the closing parenthesis and keep
30703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30704 cp_lexer_consume_token (parser
->lexer
);
30706 /* If these is no immediate closing parenthesis, the user
30707 probably doesn't know that parenthesis are required at
30708 all (ie, they typed "@catch NSException *e"). So, just
30709 forget about the closing parenthesis and keep going. */
30711 objc_begin_catch_clause (parameter_declaration
);
30712 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30713 objc_finish_catch_clause ();
30715 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30717 cp_lexer_consume_token (parser
->lexer
);
30718 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30719 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30720 node, lest it get absorbed into the surrounding block. */
30721 stmt
= push_stmt_list ();
30722 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30723 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30726 return objc_finish_try_stmt ();
30729 /* Parse an Objective-C synchronized statement.
30731 objc-synchronized-stmt:
30732 @synchronized ( expression ) compound-statement
30734 Returns NULL_TREE. */
30737 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30739 location_t location
;
30742 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30744 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30745 objc_maybe_warn_exceptions (location
);
30746 matching_parens parens
;
30747 parens
.require_open (parser
);
30748 lock
= cp_parser_expression (parser
);
30749 parens
.require_close (parser
);
30751 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30752 node, lest it get absorbed into the surrounding block. */
30753 stmt
= push_stmt_list ();
30754 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30756 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30759 /* Parse an Objective-C throw statement.
30762 @throw assignment-expression [opt] ;
30764 Returns a constructed '@throw' statement. */
30767 cp_parser_objc_throw_statement (cp_parser
*parser
)
30769 tree expr
= NULL_TREE
;
30770 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30772 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30774 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30775 expr
= cp_parser_expression (parser
);
30777 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30779 return objc_build_throw_stmt (loc
, expr
);
30782 /* Parse an Objective-C statement. */
30785 cp_parser_objc_statement (cp_parser
* parser
)
30787 /* Try to figure out what kind of declaration is present. */
30788 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30790 switch (kwd
->keyword
)
30793 return cp_parser_objc_try_catch_finally_statement (parser
);
30794 case RID_AT_SYNCHRONIZED
:
30795 return cp_parser_objc_synchronized_statement (parser
);
30797 return cp_parser_objc_throw_statement (parser
);
30799 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30801 cp_parser_skip_to_end_of_block_or_statement (parser
);
30804 return error_mark_node
;
30807 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30808 look ahead to see if an objc keyword follows the attributes. This
30809 is to detect the use of prefix attributes on ObjC @interface and
30813 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30815 cp_lexer_save_tokens (parser
->lexer
);
30816 *attrib
= cp_parser_attributes_opt (parser
);
30817 gcc_assert (*attrib
);
30818 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30820 cp_lexer_commit_tokens (parser
->lexer
);
30823 cp_lexer_rollback_tokens (parser
->lexer
);
30827 /* This routine is a minimal replacement for
30828 c_parser_struct_declaration () used when parsing the list of
30829 types/names or ObjC++ properties. For example, when parsing the
30832 @property (readonly) int a, b, c;
30834 this function is responsible for parsing "int a, int b, int c" and
30835 returning the declarations as CHAIN of DECLs.
30837 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30838 similar parsing. */
30840 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30842 tree decls
= NULL_TREE
;
30843 cp_decl_specifier_seq declspecs
;
30844 int decl_class_or_enum_p
;
30845 tree prefix_attributes
;
30847 cp_parser_decl_specifier_seq (parser
,
30848 CP_PARSER_FLAGS_NONE
,
30850 &decl_class_or_enum_p
);
30852 if (declspecs
.type
== error_mark_node
)
30853 return error_mark_node
;
30855 /* auto, register, static, extern, mutable. */
30856 if (declspecs
.storage_class
!= sc_none
)
30858 cp_parser_error (parser
, "invalid type for property");
30859 declspecs
.storage_class
= sc_none
;
30862 /* thread_local. */
30863 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30865 cp_parser_error (parser
, "invalid type for property");
30866 declspecs
.locations
[ds_thread
] = 0;
30870 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30872 cp_parser_error (parser
, "invalid type for property");
30873 declspecs
.locations
[ds_typedef
] = 0;
30876 prefix_attributes
= declspecs
.attributes
;
30877 declspecs
.attributes
= NULL_TREE
;
30879 /* Keep going until we hit the `;' at the end of the declaration. */
30880 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30882 tree attributes
, first_attribute
, decl
;
30883 cp_declarator
*declarator
;
30886 /* Parse the declarator. */
30887 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30888 NULL
, NULL
, false, false);
30890 /* Look for attributes that apply to the ivar. */
30891 attributes
= cp_parser_attributes_opt (parser
);
30892 /* Remember which attributes are prefix attributes and
30894 first_attribute
= attributes
;
30895 /* Combine the attributes. */
30896 attributes
= attr_chainon (prefix_attributes
, attributes
);
30898 decl
= grokfield (declarator
, &declspecs
,
30899 NULL_TREE
, /*init_const_expr_p=*/false,
30900 NULL_TREE
, attributes
);
30902 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30903 return error_mark_node
;
30905 /* Reset PREFIX_ATTRIBUTES. */
30906 if (attributes
!= error_mark_node
)
30908 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30909 attributes
= TREE_CHAIN (attributes
);
30911 TREE_CHAIN (attributes
) = NULL_TREE
;
30914 DECL_CHAIN (decl
) = decls
;
30917 token
= cp_lexer_peek_token (parser
->lexer
);
30918 if (token
->type
== CPP_COMMA
)
30920 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30929 /* Parse an Objective-C @property declaration. The syntax is:
30931 objc-property-declaration:
30932 '@property' objc-property-attributes[opt] struct-declaration ;
30934 objc-property-attributes:
30935 '(' objc-property-attribute-list ')'
30937 objc-property-attribute-list:
30938 objc-property-attribute
30939 objc-property-attribute-list, objc-property-attribute
30941 objc-property-attribute
30942 'getter' = identifier
30943 'setter' = identifier
30952 @property NSString *name;
30953 @property (readonly) id object;
30954 @property (retain, nonatomic, getter=getTheName) id name;
30955 @property int a, b, c;
30957 PS: This function is identical to
30958 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30960 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30962 /* The following variables hold the attributes of the properties as
30963 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30964 seen. When we see an attribute, we set them to 'true' (if they
30965 are boolean properties) or to the identifier (if they have an
30966 argument, ie, for getter and setter). Note that here we only
30967 parse the list of attributes, check the syntax and accumulate the
30968 attributes that we find. objc_add_property_declaration() will
30969 then process the information. */
30970 bool property_assign
= false;
30971 bool property_copy
= false;
30972 tree property_getter_ident
= NULL_TREE
;
30973 bool property_nonatomic
= false;
30974 bool property_readonly
= false;
30975 bool property_readwrite
= false;
30976 bool property_retain
= false;
30977 tree property_setter_ident
= NULL_TREE
;
30979 /* 'properties' is the list of properties that we read. Usually a
30980 single one, but maybe more (eg, in "@property int a, b, c;" there
30985 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30987 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30989 /* Parse the optional attribute list... */
30990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30993 matching_parens parens
;
30994 parens
.consume_open (parser
);
30998 bool syntax_error
= false;
30999 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31002 if (token
->type
!= CPP_NAME
)
31004 cp_parser_error (parser
, "expected identifier");
31007 keyword
= C_RID_CODE (token
->u
.value
);
31008 cp_lexer_consume_token (parser
->lexer
);
31011 case RID_ASSIGN
: property_assign
= true; break;
31012 case RID_COPY
: property_copy
= true; break;
31013 case RID_NONATOMIC
: property_nonatomic
= true; break;
31014 case RID_READONLY
: property_readonly
= true; break;
31015 case RID_READWRITE
: property_readwrite
= true; break;
31016 case RID_RETAIN
: property_retain
= true; break;
31020 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31022 if (keyword
== RID_GETTER
)
31023 cp_parser_error (parser
,
31024 "missing %<=%> (after %<getter%> attribute)");
31026 cp_parser_error (parser
,
31027 "missing %<=%> (after %<setter%> attribute)");
31028 syntax_error
= true;
31031 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31032 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31034 cp_parser_error (parser
, "expected identifier");
31035 syntax_error
= true;
31038 if (keyword
== RID_SETTER
)
31040 if (property_setter_ident
!= NULL_TREE
)
31042 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31043 cp_lexer_consume_token (parser
->lexer
);
31046 property_setter_ident
= cp_parser_objc_selector (parser
);
31047 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31048 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31050 cp_lexer_consume_token (parser
->lexer
);
31054 if (property_getter_ident
!= NULL_TREE
)
31056 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31057 cp_lexer_consume_token (parser
->lexer
);
31060 property_getter_ident
= cp_parser_objc_selector (parser
);
31064 cp_parser_error (parser
, "unknown property attribute");
31065 syntax_error
= true;
31072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31073 cp_lexer_consume_token (parser
->lexer
);
31078 /* FIXME: "@property (setter, assign);" will generate a spurious
31079 "error: expected ‘)’ before ‘,’ token". This is because
31080 cp_parser_require, unlike the C counterpart, will produce an
31081 error even if we are in error recovery. */
31082 if (!parens
.require_close (parser
))
31084 cp_parser_skip_to_closing_parenthesis (parser
,
31085 /*recovering=*/true,
31086 /*or_comma=*/false,
31087 /*consume_paren=*/true);
31091 /* ... and the property declaration(s). */
31092 properties
= cp_parser_objc_struct_declaration (parser
);
31094 if (properties
== error_mark_node
)
31096 cp_parser_skip_to_end_of_statement (parser
);
31097 /* If the next token is now a `;', consume it. */
31098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31099 cp_lexer_consume_token (parser
->lexer
);
31103 if (properties
== NULL_TREE
)
31104 cp_parser_error (parser
, "expected identifier");
31107 /* Comma-separated properties are chained together in
31108 reverse order; add them one by one. */
31109 properties
= nreverse (properties
);
31111 for (; properties
; properties
= TREE_CHAIN (properties
))
31112 objc_add_property_declaration (loc
, copy_node (properties
),
31113 property_readonly
, property_readwrite
,
31114 property_assign
, property_retain
,
31115 property_copy
, property_nonatomic
,
31116 property_getter_ident
, property_setter_ident
);
31119 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31122 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31124 objc-synthesize-declaration:
31125 @synthesize objc-synthesize-identifier-list ;
31127 objc-synthesize-identifier-list:
31128 objc-synthesize-identifier
31129 objc-synthesize-identifier-list, objc-synthesize-identifier
31131 objc-synthesize-identifier
31133 identifier = identifier
31136 @synthesize MyProperty;
31137 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31139 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31140 for C. Keep them in sync.
31143 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31145 tree list
= NULL_TREE
;
31147 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31149 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31152 tree property
, ivar
;
31153 property
= cp_parser_identifier (parser
);
31154 if (property
== error_mark_node
)
31156 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31161 cp_lexer_consume_token (parser
->lexer
);
31162 ivar
= cp_parser_identifier (parser
);
31163 if (ivar
== error_mark_node
)
31165 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31171 list
= chainon (list
, build_tree_list (ivar
, property
));
31172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31173 cp_lexer_consume_token (parser
->lexer
);
31177 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31178 objc_add_synthesize_declaration (loc
, list
);
31181 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31183 objc-dynamic-declaration:
31184 @dynamic identifier-list ;
31187 @dynamic MyProperty;
31188 @dynamic MyProperty, AnotherProperty;
31190 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31191 for C. Keep them in sync.
31194 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31196 tree list
= NULL_TREE
;
31198 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31200 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31204 property
= cp_parser_identifier (parser
);
31205 if (property
== error_mark_node
)
31207 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31210 list
= chainon (list
, build_tree_list (NULL
, property
));
31211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31212 cp_lexer_consume_token (parser
->lexer
);
31216 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31217 objc_add_dynamic_declaration (loc
, list
);
31221 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31223 /* Returns name of the next clause.
31224 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31225 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31226 returned and the token is consumed. */
31228 static pragma_omp_clause
31229 cp_parser_omp_clause_name (cp_parser
*parser
)
31231 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31233 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31234 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31235 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31236 result
= PRAGMA_OMP_CLAUSE_IF
;
31237 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31238 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31239 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31240 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31241 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31242 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31243 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31244 result
= PRAGMA_OMP_CLAUSE_FOR
;
31245 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31247 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31248 const char *p
= IDENTIFIER_POINTER (id
);
31253 if (!strcmp ("aligned", p
))
31254 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31255 else if (!strcmp ("async", p
))
31256 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31259 if (!strcmp ("collapse", p
))
31260 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31261 else if (!strcmp ("copy", p
))
31262 result
= PRAGMA_OACC_CLAUSE_COPY
;
31263 else if (!strcmp ("copyin", p
))
31264 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31265 else if (!strcmp ("copyout", p
))
31266 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31267 else if (!strcmp ("copyprivate", p
))
31268 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31269 else if (!strcmp ("create", p
))
31270 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31273 if (!strcmp ("defaultmap", p
))
31274 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31275 else if (!strcmp ("depend", p
))
31276 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31277 else if (!strcmp ("device", p
))
31278 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31279 else if (!strcmp ("deviceptr", p
))
31280 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31281 else if (!strcmp ("device_resident", p
))
31282 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31283 else if (!strcmp ("dist_schedule", p
))
31284 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31287 if (!strcmp ("final", p
))
31288 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31289 else if (!strcmp ("firstprivate", p
))
31290 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31291 else if (!strcmp ("from", p
))
31292 result
= PRAGMA_OMP_CLAUSE_FROM
;
31295 if (!strcmp ("gang", p
))
31296 result
= PRAGMA_OACC_CLAUSE_GANG
;
31297 else if (!strcmp ("grainsize", p
))
31298 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31301 if (!strcmp ("hint", p
))
31302 result
= PRAGMA_OMP_CLAUSE_HINT
;
31303 else if (!strcmp ("host", p
))
31304 result
= PRAGMA_OACC_CLAUSE_HOST
;
31307 if (!strcmp ("inbranch", p
))
31308 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31309 else if (!strcmp ("independent", p
))
31310 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31311 else if (!strcmp ("is_device_ptr", p
))
31312 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31315 if (!strcmp ("lastprivate", p
))
31316 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31317 else if (!strcmp ("linear", p
))
31318 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31319 else if (!strcmp ("link", p
))
31320 result
= PRAGMA_OMP_CLAUSE_LINK
;
31323 if (!strcmp ("map", p
))
31324 result
= PRAGMA_OMP_CLAUSE_MAP
;
31325 else if (!strcmp ("mergeable", p
))
31326 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31329 if (!strcmp ("nogroup", p
))
31330 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31331 else if (!strcmp ("notinbranch", p
))
31332 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31333 else if (!strcmp ("nowait", p
))
31334 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31335 else if (!strcmp ("num_gangs", p
))
31336 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31337 else if (!strcmp ("num_tasks", p
))
31338 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31339 else if (!strcmp ("num_teams", p
))
31340 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31341 else if (!strcmp ("num_threads", p
))
31342 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31343 else if (!strcmp ("num_workers", p
))
31344 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31347 if (!strcmp ("ordered", p
))
31348 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31351 if (!strcmp ("parallel", p
))
31352 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31353 else if (!strcmp ("present", p
))
31354 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31355 else if (!strcmp ("present_or_copy", p
)
31356 || !strcmp ("pcopy", p
))
31357 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31358 else if (!strcmp ("present_or_copyin", p
)
31359 || !strcmp ("pcopyin", p
))
31360 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31361 else if (!strcmp ("present_or_copyout", p
)
31362 || !strcmp ("pcopyout", p
))
31363 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31364 else if (!strcmp ("present_or_create", p
)
31365 || !strcmp ("pcreate", p
))
31366 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31367 else if (!strcmp ("priority", p
))
31368 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31369 else if (!strcmp ("proc_bind", p
))
31370 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31373 if (!strcmp ("reduction", p
))
31374 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31377 if (!strcmp ("safelen", p
))
31378 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31379 else if (!strcmp ("schedule", p
))
31380 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31381 else if (!strcmp ("sections", p
))
31382 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31383 else if (!strcmp ("self", p
))
31384 result
= PRAGMA_OACC_CLAUSE_SELF
;
31385 else if (!strcmp ("seq", p
))
31386 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31387 else if (!strcmp ("shared", p
))
31388 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31389 else if (!strcmp ("simd", p
))
31390 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31391 else if (!strcmp ("simdlen", p
))
31392 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31395 if (!strcmp ("taskgroup", p
))
31396 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31397 else if (!strcmp ("thread_limit", p
))
31398 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31399 else if (!strcmp ("threads", p
))
31400 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31401 else if (!strcmp ("tile", p
))
31402 result
= PRAGMA_OACC_CLAUSE_TILE
;
31403 else if (!strcmp ("to", p
))
31404 result
= PRAGMA_OMP_CLAUSE_TO
;
31407 if (!strcmp ("uniform", p
))
31408 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31409 else if (!strcmp ("untied", p
))
31410 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31411 else if (!strcmp ("use_device", p
))
31412 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31413 else if (!strcmp ("use_device_ptr", p
))
31414 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31417 if (!strcmp ("vector", p
))
31418 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31419 else if (!strcmp ("vector_length", p
))
31420 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31423 if (!strcmp ("wait", p
))
31424 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31425 else if (!strcmp ("worker", p
))
31426 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31431 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31432 cp_lexer_consume_token (parser
->lexer
);
31437 /* Validate that a clause of the given type does not already exist. */
31440 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31441 const char *name
, location_t location
)
31445 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31446 if (OMP_CLAUSE_CODE (c
) == code
)
31448 error_at (location
, "too many %qs clauses", name
);
31456 variable-list , identifier
31458 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31459 colon). An opening parenthesis will have been consumed by the caller.
31461 If KIND is nonzero, create the appropriate node and install the decl
31462 in OMP_CLAUSE_DECL and add the node to the head of the list.
31464 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31465 return the list created.
31467 COLON can be NULL if only closing parenthesis should end the list,
31468 or pointer to bool which will receive false if the list is terminated
31469 by closing parenthesis or true if the list is terminated by colon. */
31472 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31473 tree list
, bool *colon
)
31476 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31479 parser
->colon_corrects_to_scope_p
= false;
31486 token
= cp_lexer_peek_token (parser
->lexer
);
31488 && current_class_ptr
31489 && cp_parser_is_keyword (token
, RID_THIS
))
31491 decl
= finish_this_expr ();
31492 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31493 || CONVERT_EXPR_P (decl
))
31494 decl
= TREE_OPERAND (decl
, 0);
31495 cp_lexer_consume_token (parser
->lexer
);
31499 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31500 /*check_dependency_p=*/true,
31501 /*template_p=*/NULL
,
31502 /*declarator_p=*/false,
31503 /*optional_p=*/false);
31504 if (name
== error_mark_node
)
31507 if (identifier_p (name
))
31508 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31511 if (decl
== error_mark_node
)
31512 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31515 if (decl
== error_mark_node
)
31517 else if (kind
!= 0)
31521 case OMP_CLAUSE__CACHE_
:
31522 /* The OpenACC cache directive explicitly only allows "array
31523 elements or subarrays". */
31524 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31526 error_at (token
->location
, "expected %<[%>");
31527 decl
= error_mark_node
;
31531 case OMP_CLAUSE_MAP
:
31532 case OMP_CLAUSE_FROM
:
31533 case OMP_CLAUSE_TO
:
31534 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31537 = cp_lexer_peek_token (parser
->lexer
)->location
;
31538 cp_id_kind idk
= CP_ID_KIND_NONE
;
31539 cp_lexer_consume_token (parser
->lexer
);
31540 decl
= convert_from_reference (decl
);
31542 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31547 case OMP_CLAUSE_DEPEND
:
31548 case OMP_CLAUSE_REDUCTION
:
31549 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31551 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31553 parser
->colon_corrects_to_scope_p
= false;
31554 cp_lexer_consume_token (parser
->lexer
);
31555 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31556 low_bound
= cp_parser_expression (parser
);
31558 parser
->colon_corrects_to_scope_p
31559 = saved_colon_corrects_to_scope_p
;
31560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31561 length
= integer_one_node
;
31564 /* Look for `:'. */
31565 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31567 if (!cp_lexer_next_token_is (parser
->lexer
,
31569 length
= cp_parser_expression (parser
);
31571 /* Look for the closing `]'. */
31572 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31576 decl
= tree_cons (low_bound
, length
, decl
);
31583 tree u
= build_omp_clause (token
->location
, kind
);
31584 OMP_CLAUSE_DECL (u
) = decl
;
31585 OMP_CLAUSE_CHAIN (u
) = list
;
31589 list
= tree_cons (decl
, NULL_TREE
, list
);
31592 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31594 cp_lexer_consume_token (parser
->lexer
);
31598 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31600 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31603 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31607 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31611 /* Try to resync to an unnested comma. Copied from
31612 cp_parser_parenthesized_expression_list. */
31615 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31616 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31617 /*recovering=*/true,
31619 /*consume_paren=*/true);
31627 /* Similarly, but expect leading and trailing parenthesis. This is a very
31628 common case for omp clauses. */
31631 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31633 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31634 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31639 copy ( variable-list )
31640 copyin ( variable-list )
31641 copyout ( variable-list )
31642 create ( variable-list )
31643 delete ( variable-list )
31644 present ( variable-list )
31645 present_or_copy ( variable-list )
31646 pcopy ( variable-list )
31647 present_or_copyin ( variable-list )
31648 pcopyin ( variable-list )
31649 present_or_copyout ( variable-list )
31650 pcopyout ( variable-list )
31651 present_or_create ( variable-list )
31652 pcreate ( variable-list ) */
31655 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31658 enum gomp_map_kind kind
;
31661 case PRAGMA_OACC_CLAUSE_COPY
:
31662 kind
= GOMP_MAP_FORCE_TOFROM
;
31664 case PRAGMA_OACC_CLAUSE_COPYIN
:
31665 kind
= GOMP_MAP_FORCE_TO
;
31667 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31668 kind
= GOMP_MAP_FORCE_FROM
;
31670 case PRAGMA_OACC_CLAUSE_CREATE
:
31671 kind
= GOMP_MAP_FORCE_ALLOC
;
31673 case PRAGMA_OACC_CLAUSE_DELETE
:
31674 kind
= GOMP_MAP_DELETE
;
31676 case PRAGMA_OACC_CLAUSE_DEVICE
:
31677 kind
= GOMP_MAP_FORCE_TO
;
31679 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31680 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31682 case PRAGMA_OACC_CLAUSE_HOST
:
31683 case PRAGMA_OACC_CLAUSE_SELF
:
31684 kind
= GOMP_MAP_FORCE_FROM
;
31686 case PRAGMA_OACC_CLAUSE_LINK
:
31687 kind
= GOMP_MAP_LINK
;
31689 case PRAGMA_OACC_CLAUSE_PRESENT
:
31690 kind
= GOMP_MAP_FORCE_PRESENT
;
31692 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31693 kind
= GOMP_MAP_TOFROM
;
31695 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31696 kind
= GOMP_MAP_TO
;
31698 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31699 kind
= GOMP_MAP_FROM
;
31701 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31702 kind
= GOMP_MAP_ALLOC
;
31705 gcc_unreachable ();
31708 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31710 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31711 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31717 deviceptr ( variable-list ) */
31720 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31722 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31725 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31726 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31727 variable-list must only allow for pointer variables. */
31728 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31729 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31731 tree v
= TREE_PURPOSE (t
);
31732 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31733 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31734 OMP_CLAUSE_DECL (u
) = v
;
31735 OMP_CLAUSE_CHAIN (u
) = list
;
31749 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31750 enum omp_clause_code code
,
31751 tree list
, location_t location
)
31753 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31754 tree c
= build_omp_clause (location
, code
);
31755 OMP_CLAUSE_CHAIN (c
) = list
;
31760 num_gangs ( expression )
31761 num_workers ( expression )
31762 vector_length ( expression ) */
31765 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31766 const char *str
, tree list
)
31768 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31770 matching_parens parens
;
31771 if (!parens
.require_open (parser
))
31774 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31776 if (t
== error_mark_node
31777 || !parens
.require_close (parser
))
31779 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31780 /*or_comma=*/false,
31781 /*consume_paren=*/true);
31785 check_no_duplicate_clause (list
, code
, str
, loc
);
31787 tree c
= build_omp_clause (loc
, code
);
31788 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31789 OMP_CLAUSE_CHAIN (c
) = list
;
31795 gang [( gang-arg-list )]
31796 worker [( [num:] int-expr )]
31797 vector [( [length:] int-expr )]
31799 where gang-arg is one of:
31804 and size-expr may be:
31811 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31812 const char *str
, tree list
)
31814 const char *id
= "num";
31815 cp_lexer
*lexer
= parser
->lexer
;
31816 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31817 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31819 if (kind
== OMP_CLAUSE_VECTOR
)
31822 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31824 matching_parens parens
;
31825 parens
.consume_open (parser
);
31829 cp_token
*next
= cp_lexer_peek_token (lexer
);
31832 /* Gang static argument. */
31833 if (kind
== OMP_CLAUSE_GANG
31834 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31836 cp_lexer_consume_token (lexer
);
31838 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31839 goto cleanup_error
;
31842 if (ops
[idx
] != NULL
)
31844 cp_parser_error (parser
, "too many %<static%> arguments");
31845 goto cleanup_error
;
31848 /* Check for the '*' argument. */
31849 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31850 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31851 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31854 cp_lexer_consume_token (lexer
);
31855 ops
[idx
] = integer_minus_one_node
;
31857 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31859 cp_lexer_consume_token (lexer
);
31865 /* Worker num: argument and vector length: arguments. */
31866 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31867 && id_equal (next
->u
.value
, id
)
31868 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31870 cp_lexer_consume_token (lexer
); /* id */
31871 cp_lexer_consume_token (lexer
); /* ':' */
31874 /* Now collect the actual argument. */
31875 if (ops
[idx
] != NULL_TREE
)
31877 cp_parser_error (parser
, "unexpected argument");
31878 goto cleanup_error
;
31881 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31883 if (expr
== error_mark_node
)
31884 goto cleanup_error
;
31886 mark_exp_read (expr
);
31889 if (kind
== OMP_CLAUSE_GANG
31890 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31892 cp_lexer_consume_token (lexer
);
31899 if (!parens
.require_close (parser
))
31900 goto cleanup_error
;
31903 check_no_duplicate_clause (list
, kind
, str
, loc
);
31905 c
= build_omp_clause (loc
, kind
);
31908 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31910 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31911 OMP_CLAUSE_CHAIN (c
) = list
;
31916 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31921 tile ( size-expr-list ) */
31924 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31926 tree c
, expr
= error_mark_node
;
31927 tree tile
= NULL_TREE
;
31929 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31930 so, but the spec authors never considered such a case and have
31931 differing opinions on what it might mean, including 'not
31933 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31934 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31937 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31942 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31945 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31946 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31947 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31949 cp_lexer_consume_token (parser
->lexer
);
31950 expr
= integer_zero_node
;
31953 expr
= cp_parser_constant_expression (parser
);
31955 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31957 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31959 /* Consume the trailing ')'. */
31960 cp_lexer_consume_token (parser
->lexer
);
31962 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31963 tile
= nreverse (tile
);
31964 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31965 OMP_CLAUSE_CHAIN (c
) = list
;
31970 Parse wait clause or directive parameters. */
31973 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31975 vec
<tree
, va_gc
> *args
;
31978 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31980 /*allow_expansion_p=*/true,
31981 /*non_constant_p=*/NULL
);
31983 if (args
== NULL
|| args
->length () == 0)
31985 cp_parser_error (parser
, "expected integer expression before ')'");
31987 release_tree_vector (args
);
31991 args_tree
= build_tree_list_vec (args
);
31993 release_tree_vector (args
);
31995 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31997 tree targ
= TREE_VALUE (t
);
31999 if (targ
!= error_mark_node
)
32001 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32002 error ("%<wait%> expression must be integral");
32005 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32007 targ
= mark_rvalue_use (targ
);
32008 OMP_CLAUSE_DECL (c
) = targ
;
32009 OMP_CLAUSE_CHAIN (c
) = list
;
32019 wait ( int-expr-list ) */
32022 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32024 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32026 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32029 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32035 collapse ( constant-expression ) */
32038 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32044 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32045 matching_parens parens
;
32046 if (!parens
.require_open (parser
))
32049 num
= cp_parser_constant_expression (parser
);
32051 if (!parens
.require_close (parser
))
32052 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32053 /*or_comma=*/false,
32054 /*consume_paren=*/true);
32056 if (num
== error_mark_node
)
32058 num
= fold_non_dependent_expr (num
);
32059 if (!tree_fits_shwi_p (num
)
32060 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32061 || (n
= tree_to_shwi (num
)) <= 0
32064 error_at (loc
, "collapse argument needs positive constant integer expression");
32068 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32069 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32070 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32071 OMP_CLAUSE_CHAIN (c
) = list
;
32072 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32078 default ( none | shared )
32081 default ( none | present ) */
32084 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32085 location_t location
, bool is_oacc
)
32087 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32090 matching_parens parens
;
32091 if (!parens
.require_open (parser
))
32093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32095 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32096 const char *p
= IDENTIFIER_POINTER (id
);
32101 if (strcmp ("none", p
) != 0)
32103 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32107 if (strcmp ("present", p
) != 0 || !is_oacc
)
32109 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32113 if (strcmp ("shared", p
) != 0 || is_oacc
)
32115 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32122 cp_lexer_consume_token (parser
->lexer
);
32128 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32130 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32133 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32134 || !parens
.require_close (parser
))
32135 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32136 /*or_comma=*/false,
32137 /*consume_paren=*/true);
32139 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32142 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32143 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32144 OMP_CLAUSE_CHAIN (c
) = list
;
32145 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32151 final ( expression ) */
32154 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32158 matching_parens parens
;
32159 if (!parens
.require_open (parser
))
32162 t
= cp_parser_condition (parser
);
32164 if (t
== error_mark_node
32165 || !parens
.require_close (parser
))
32166 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32167 /*or_comma=*/false,
32168 /*consume_paren=*/true);
32170 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32172 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32173 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32174 OMP_CLAUSE_CHAIN (c
) = list
;
32183 if ( directive-name-modifier : expression )
32185 directive-name-modifier:
32186 parallel | task | taskloop | target data | target | target update
32187 | target enter data | target exit data */
32190 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32194 enum tree_code if_modifier
= ERROR_MARK
;
32196 matching_parens parens
;
32197 if (!parens
.require_open (parser
))
32200 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32202 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32203 const char *p
= IDENTIFIER_POINTER (id
);
32206 if (strcmp ("parallel", p
) == 0)
32207 if_modifier
= OMP_PARALLEL
;
32208 else if (strcmp ("task", p
) == 0)
32209 if_modifier
= OMP_TASK
;
32210 else if (strcmp ("taskloop", p
) == 0)
32211 if_modifier
= OMP_TASKLOOP
;
32212 else if (strcmp ("target", p
) == 0)
32214 if_modifier
= OMP_TARGET
;
32215 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32217 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32218 p
= IDENTIFIER_POINTER (id
);
32219 if (strcmp ("data", p
) == 0)
32220 if_modifier
= OMP_TARGET_DATA
;
32221 else if (strcmp ("update", p
) == 0)
32222 if_modifier
= OMP_TARGET_UPDATE
;
32223 else if (strcmp ("enter", p
) == 0)
32224 if_modifier
= OMP_TARGET_ENTER_DATA
;
32225 else if (strcmp ("exit", p
) == 0)
32226 if_modifier
= OMP_TARGET_EXIT_DATA
;
32227 if (if_modifier
!= OMP_TARGET
)
32232 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32233 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32235 if_modifier
= ERROR_MARK
;
32237 if (if_modifier
== OMP_TARGET_ENTER_DATA
32238 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32240 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32242 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32243 p
= IDENTIFIER_POINTER (id
);
32244 if (strcmp ("data", p
) == 0)
32250 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32251 error_at (loc
, "expected %<data%>");
32252 if_modifier
= ERROR_MARK
;
32257 if (if_modifier
!= ERROR_MARK
)
32259 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32262 cp_lexer_consume_token (parser
->lexer
);
32269 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32270 error_at (loc
, "expected %<:%>");
32272 if_modifier
= ERROR_MARK
;
32277 t
= cp_parser_condition (parser
);
32279 if (t
== error_mark_node
32280 || !parens
.require_close (parser
))
32281 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32282 /*or_comma=*/false,
32283 /*consume_paren=*/true);
32285 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32286 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32288 if (if_modifier
!= ERROR_MARK
32289 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32291 const char *p
= NULL
;
32292 switch (if_modifier
)
32294 case OMP_PARALLEL
: p
= "parallel"; break;
32295 case OMP_TASK
: p
= "task"; break;
32296 case OMP_TASKLOOP
: p
= "taskloop"; break;
32297 case OMP_TARGET_DATA
: p
= "target data"; break;
32298 case OMP_TARGET
: p
= "target"; break;
32299 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32300 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32301 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32302 default: gcc_unreachable ();
32304 error_at (location
, "too many %<if%> clauses with %qs modifier",
32308 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32311 error_at (location
, "too many %<if%> clauses");
32313 error_at (location
, "too many %<if%> clauses without modifier");
32316 else if (if_modifier
== ERROR_MARK
32317 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32319 error_at (location
, "if any %<if%> clause has modifier, then all "
32320 "%<if%> clauses have to use modifier");
32325 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32326 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32327 OMP_CLAUSE_IF_EXPR (c
) = t
;
32328 OMP_CLAUSE_CHAIN (c
) = list
;
32337 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32338 tree list
, location_t location
)
32342 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32345 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32346 OMP_CLAUSE_CHAIN (c
) = list
;
32354 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32355 tree list
, location_t location
)
32359 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32361 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32362 OMP_CLAUSE_CHAIN (c
) = list
;
32367 num_threads ( expression ) */
32370 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32371 location_t location
)
32375 matching_parens parens
;
32376 if (!parens
.require_open (parser
))
32379 t
= cp_parser_expression (parser
);
32381 if (t
== error_mark_node
32382 || !parens
.require_close (parser
))
32383 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32384 /*or_comma=*/false,
32385 /*consume_paren=*/true);
32387 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32388 "num_threads", location
);
32390 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32391 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32392 OMP_CLAUSE_CHAIN (c
) = list
;
32398 num_tasks ( expression ) */
32401 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32402 location_t location
)
32406 matching_parens parens
;
32407 if (!parens
.require_open (parser
))
32410 t
= cp_parser_expression (parser
);
32412 if (t
== error_mark_node
32413 || !parens
.require_close (parser
))
32414 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32415 /*or_comma=*/false,
32416 /*consume_paren=*/true);
32418 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32419 "num_tasks", location
);
32421 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32422 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32423 OMP_CLAUSE_CHAIN (c
) = list
;
32429 grainsize ( expression ) */
32432 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32433 location_t location
)
32437 matching_parens parens
;
32438 if (!parens
.require_open (parser
))
32441 t
= cp_parser_expression (parser
);
32443 if (t
== error_mark_node
32444 || !parens
.require_close (parser
))
32445 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32446 /*or_comma=*/false,
32447 /*consume_paren=*/true);
32449 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32450 "grainsize", location
);
32452 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32453 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32454 OMP_CLAUSE_CHAIN (c
) = list
;
32460 priority ( expression ) */
32463 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32464 location_t location
)
32468 matching_parens parens
;
32469 if (!parens
.require_open (parser
))
32472 t
= cp_parser_expression (parser
);
32474 if (t
== error_mark_node
32475 || !parens
.require_close (parser
))
32476 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32477 /*or_comma=*/false,
32478 /*consume_paren=*/true);
32480 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32481 "priority", location
);
32483 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32484 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32485 OMP_CLAUSE_CHAIN (c
) = list
;
32491 hint ( expression ) */
32494 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32495 location_t location
)
32499 matching_parens parens
;
32500 if (!parens
.require_open (parser
))
32503 t
= cp_parser_expression (parser
);
32505 if (t
== error_mark_node
32506 || !parens
.require_close (parser
))
32507 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32508 /*or_comma=*/false,
32509 /*consume_paren=*/true);
32511 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32513 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32514 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32515 OMP_CLAUSE_CHAIN (c
) = list
;
32521 defaultmap ( tofrom : scalar ) */
32524 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32525 location_t location
)
32530 matching_parens parens
;
32531 if (!parens
.require_open (parser
))
32534 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32536 cp_parser_error (parser
, "expected %<tofrom%>");
32539 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32540 p
= IDENTIFIER_POINTER (id
);
32541 if (strcmp (p
, "tofrom") != 0)
32543 cp_parser_error (parser
, "expected %<tofrom%>");
32546 cp_lexer_consume_token (parser
->lexer
);
32547 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32550 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32552 cp_parser_error (parser
, "expected %<scalar%>");
32555 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32556 p
= IDENTIFIER_POINTER (id
);
32557 if (strcmp (p
, "scalar") != 0)
32559 cp_parser_error (parser
, "expected %<scalar%>");
32562 cp_lexer_consume_token (parser
->lexer
);
32563 if (!parens
.require_close (parser
))
32566 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32569 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32570 OMP_CLAUSE_CHAIN (c
) = list
;
32574 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32575 /*or_comma=*/false,
32576 /*consume_paren=*/true);
32584 ordered ( constant-expression ) */
32587 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32588 tree list
, location_t location
)
32590 tree c
, num
= NULL_TREE
;
32593 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32594 "ordered", location
);
32596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32598 matching_parens parens
;
32599 parens
.consume_open (parser
);
32601 num
= cp_parser_constant_expression (parser
);
32603 if (!parens
.require_close (parser
))
32604 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32605 /*or_comma=*/false,
32606 /*consume_paren=*/true);
32608 if (num
== error_mark_node
)
32610 num
= fold_non_dependent_expr (num
);
32611 if (!tree_fits_shwi_p (num
)
32612 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32613 || (n
= tree_to_shwi (num
)) <= 0
32616 error_at (location
,
32617 "ordered argument needs positive constant integer "
32623 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32624 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32625 OMP_CLAUSE_CHAIN (c
) = list
;
32630 reduction ( reduction-operator : variable-list )
32632 reduction-operator:
32633 One of: + * - & ^ | && ||
32637 reduction-operator:
32638 One of: + * - & ^ | && || min max
32642 reduction-operator:
32643 One of: + * - & ^ | && ||
32647 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32649 enum tree_code code
= ERROR_MARK
;
32650 tree nlist
, c
, id
= NULL_TREE
;
32652 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32655 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32657 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32658 case CPP_MULT
: code
= MULT_EXPR
; break;
32659 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32660 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32661 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32662 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32663 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32664 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32668 if (code
!= ERROR_MARK
)
32669 cp_lexer_consume_token (parser
->lexer
);
32672 bool saved_colon_corrects_to_scope_p
;
32673 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32674 parser
->colon_corrects_to_scope_p
= false;
32675 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32676 /*check_dependency_p=*/true,
32677 /*template_p=*/NULL
,
32678 /*declarator_p=*/false,
32679 /*optional_p=*/false);
32680 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32681 if (identifier_p (id
))
32683 const char *p
= IDENTIFIER_POINTER (id
);
32685 if (strcmp (p
, "min") == 0)
32687 else if (strcmp (p
, "max") == 0)
32689 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32691 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32693 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32695 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32696 code
= BIT_AND_EXPR
;
32697 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32698 code
= BIT_IOR_EXPR
;
32699 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32700 code
= BIT_XOR_EXPR
;
32701 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32702 code
= TRUTH_ANDIF_EXPR
;
32703 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32704 code
= TRUTH_ORIF_EXPR
;
32705 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32706 tree scope
= parser
->scope
;
32708 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32709 parser
->scope
= NULL_TREE
;
32710 parser
->qualifying_scope
= NULL_TREE
;
32711 parser
->object_scope
= NULL_TREE
;
32715 error ("invalid reduction-identifier");
32717 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32718 /*or_comma=*/false,
32719 /*consume_paren=*/true);
32724 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32727 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32729 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32731 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32732 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32739 schedule ( schedule-kind )
32740 schedule ( schedule-kind , expression )
32743 static | dynamic | guided | runtime | auto
32746 schedule ( schedule-modifier : schedule-kind )
32747 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32755 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32758 int modifiers
= 0, nmodifiers
= 0;
32760 matching_parens parens
;
32761 if (!parens
.require_open (parser
))
32764 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32766 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32768 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32769 const char *p
= IDENTIFIER_POINTER (id
);
32770 if (strcmp ("simd", p
) == 0)
32771 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32772 else if (strcmp ("monotonic", p
) == 0)
32773 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32774 else if (strcmp ("nonmonotonic", p
) == 0)
32775 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32778 cp_lexer_consume_token (parser
->lexer
);
32779 if (nmodifiers
++ == 0
32780 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32781 cp_lexer_consume_token (parser
->lexer
);
32784 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32791 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32792 const char *p
= IDENTIFIER_POINTER (id
);
32797 if (strcmp ("dynamic", p
) != 0)
32799 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32803 if (strcmp ("guided", p
) != 0)
32805 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32809 if (strcmp ("runtime", p
) != 0)
32811 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32818 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32819 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32820 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32821 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32824 cp_lexer_consume_token (parser
->lexer
);
32826 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32827 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32828 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32829 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32831 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32839 cp_lexer_consume_token (parser
->lexer
);
32841 token
= cp_lexer_peek_token (parser
->lexer
);
32842 t
= cp_parser_assignment_expression (parser
);
32844 if (t
== error_mark_node
)
32846 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32847 error_at (token
->location
, "schedule %<runtime%> does not take "
32848 "a %<chunk_size%> parameter");
32849 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32850 error_at (token
->location
, "schedule %<auto%> does not take "
32851 "a %<chunk_size%> parameter");
32853 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32855 if (!parens
.require_close (parser
))
32858 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32861 OMP_CLAUSE_SCHEDULE_KIND (c
)
32862 = (enum omp_clause_schedule_kind
)
32863 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32865 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32866 OMP_CLAUSE_CHAIN (c
) = list
;
32870 cp_parser_error (parser
, "invalid schedule kind");
32872 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32873 /*or_comma=*/false,
32874 /*consume_paren=*/true);
32882 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32883 tree list
, location_t location
)
32887 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32889 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32890 OMP_CLAUSE_CHAIN (c
) = list
;
32899 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32900 tree list
, location_t location
)
32902 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32903 tree c
= build_omp_clause (location
, code
);
32904 OMP_CLAUSE_CHAIN (c
) = list
;
32915 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32916 enum omp_clause_code code
,
32917 tree list
, location_t location
)
32919 tree c
= build_omp_clause (location
, code
);
32920 OMP_CLAUSE_CHAIN (c
) = list
;
32928 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32929 tree list
, location_t location
)
32931 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32932 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32933 OMP_CLAUSE_CHAIN (c
) = list
;
32942 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32943 enum omp_clause_code code
,
32944 tree list
, location_t location
)
32946 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32947 tree c
= build_omp_clause (location
, code
);
32948 OMP_CLAUSE_CHAIN (c
) = list
;
32953 num_teams ( expression ) */
32956 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32957 location_t location
)
32961 matching_parens parens
;
32962 if (!parens
.require_open (parser
))
32965 t
= cp_parser_expression (parser
);
32967 if (t
== error_mark_node
32968 || !parens
.require_close (parser
))
32969 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32970 /*or_comma=*/false,
32971 /*consume_paren=*/true);
32973 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32974 "num_teams", location
);
32976 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32977 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32978 OMP_CLAUSE_CHAIN (c
) = list
;
32984 thread_limit ( expression ) */
32987 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32988 location_t location
)
32992 matching_parens parens
;
32993 if (!parens
.require_open (parser
))
32996 t
= cp_parser_expression (parser
);
32998 if (t
== error_mark_node
32999 || !parens
.require_close (parser
))
33000 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33001 /*or_comma=*/false,
33002 /*consume_paren=*/true);
33004 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33005 "thread_limit", location
);
33007 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33008 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33009 OMP_CLAUSE_CHAIN (c
) = list
;
33015 aligned ( variable-list )
33016 aligned ( variable-list : constant-expression ) */
33019 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33021 tree nlist
, c
, alignment
= NULL_TREE
;
33024 matching_parens parens
;
33025 if (!parens
.require_open (parser
))
33028 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33033 alignment
= cp_parser_constant_expression (parser
);
33035 if (!parens
.require_close (parser
))
33036 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33037 /*or_comma=*/false,
33038 /*consume_paren=*/true);
33040 if (alignment
== error_mark_node
)
33041 alignment
= NULL_TREE
;
33044 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33045 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33051 linear ( variable-list )
33052 linear ( variable-list : expression )
33055 linear ( modifier ( variable-list ) )
33056 linear ( modifier ( variable-list ) : expression ) */
33059 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33062 tree nlist
, c
, step
= integer_one_node
;
33064 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33066 matching_parens parens
;
33067 if (!parens
.require_open (parser
))
33070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33072 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33073 const char *p
= IDENTIFIER_POINTER (id
);
33075 if (strcmp ("ref", p
) == 0)
33076 kind
= OMP_CLAUSE_LINEAR_REF
;
33077 else if (strcmp ("val", p
) == 0)
33078 kind
= OMP_CLAUSE_LINEAR_VAL
;
33079 else if (strcmp ("uval", p
) == 0)
33080 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33081 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33082 cp_lexer_consume_token (parser
->lexer
);
33084 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33087 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33088 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33092 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33093 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33095 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33096 else if (!parens
.require_close (parser
))
33097 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33098 /*or_comma=*/false,
33099 /*consume_paren=*/true);
33106 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33107 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33109 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33110 cp_parser_parse_tentatively (parser
);
33111 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33112 /*check_dependency_p=*/true,
33113 /*template_p=*/NULL
,
33114 /*declarator_p=*/false,
33115 /*optional_p=*/false);
33116 if (step
!= error_mark_node
)
33117 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33118 if (step
== error_mark_node
)
33121 cp_parser_abort_tentative_parse (parser
);
33123 else if (!cp_parser_parse_definitely (parser
))
33127 step
= cp_parser_expression (parser
);
33129 if (!parens
.require_close (parser
))
33130 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33131 /*or_comma=*/false,
33132 /*consume_paren=*/true);
33134 if (step
== error_mark_node
)
33138 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33140 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33141 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33148 safelen ( constant-expression ) */
33151 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33152 location_t location
)
33156 matching_parens parens
;
33157 if (!parens
.require_open (parser
))
33160 t
= cp_parser_constant_expression (parser
);
33162 if (t
== error_mark_node
33163 || !parens
.require_close (parser
))
33164 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33165 /*or_comma=*/false,
33166 /*consume_paren=*/true);
33168 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33170 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33171 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33172 OMP_CLAUSE_CHAIN (c
) = list
;
33178 simdlen ( constant-expression ) */
33181 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33182 location_t location
)
33186 matching_parens parens
;
33187 if (!parens
.require_open (parser
))
33190 t
= cp_parser_constant_expression (parser
);
33192 if (t
== error_mark_node
33193 || !parens
.require_close (parser
))
33194 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33195 /*or_comma=*/false,
33196 /*consume_paren=*/true);
33198 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33200 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33201 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33202 OMP_CLAUSE_CHAIN (c
) = list
;
33209 identifier [+/- integer]
33210 vec , identifier [+/- integer]
33214 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33219 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33221 cp_parser_error (parser
, "expected identifier");
33225 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33227 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33228 tree t
, identifier
= cp_parser_identifier (parser
);
33229 tree addend
= NULL
;
33231 if (identifier
== error_mark_node
)
33232 t
= error_mark_node
;
33235 t
= cp_parser_lookup_name_simple
33236 (parser
, identifier
,
33237 cp_lexer_peek_token (parser
->lexer
)->location
);
33238 if (t
== error_mark_node
)
33239 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33246 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33248 addend
= integer_zero_node
;
33249 goto add_to_vector
;
33251 cp_lexer_consume_token (parser
->lexer
);
33253 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33255 cp_parser_error (parser
, "expected integer");
33259 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33260 if (TREE_CODE (addend
) != INTEGER_CST
)
33262 cp_parser_error (parser
, "expected integer");
33265 cp_lexer_consume_token (parser
->lexer
);
33268 if (t
!= error_mark_node
)
33270 vec
= tree_cons (addend
, t
, vec
);
33272 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33275 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33278 cp_lexer_consume_token (parser
->lexer
);
33281 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33283 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33284 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33285 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33286 OMP_CLAUSE_CHAIN (u
) = list
;
33293 depend ( depend-kind : variable-list )
33301 depend ( sink : vec ) */
33304 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33307 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33309 matching_parens parens
;
33310 if (!parens
.require_open (parser
))
33313 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33315 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33316 const char *p
= IDENTIFIER_POINTER (id
);
33318 if (strcmp ("in", p
) == 0)
33319 kind
= OMP_CLAUSE_DEPEND_IN
;
33320 else if (strcmp ("inout", p
) == 0)
33321 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33322 else if (strcmp ("out", p
) == 0)
33323 kind
= OMP_CLAUSE_DEPEND_OUT
;
33324 else if (strcmp ("source", p
) == 0)
33325 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33326 else if (strcmp ("sink", p
) == 0)
33327 kind
= OMP_CLAUSE_DEPEND_SINK
;
33334 cp_lexer_consume_token (parser
->lexer
);
33336 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33338 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33339 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33340 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33341 OMP_CLAUSE_CHAIN (c
) = list
;
33342 if (!parens
.require_close (parser
))
33343 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33344 /*or_comma=*/false,
33345 /*consume_paren=*/true);
33349 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33352 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33353 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33356 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33359 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33360 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33365 cp_parser_error (parser
, "invalid depend kind");
33367 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33368 /*or_comma=*/false,
33369 /*consume_paren=*/true);
33374 map ( map-kind : variable-list )
33375 map ( variable-list )
33378 alloc | to | from | tofrom
33382 alloc | to | from | tofrom | release | delete
33384 map ( always [,] map-kind: variable-list ) */
33387 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33390 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33391 bool always
= false;
33393 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33398 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33399 const char *p
= IDENTIFIER_POINTER (id
);
33401 if (strcmp ("always", p
) == 0)
33404 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33406 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33407 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33409 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33413 cp_lexer_consume_token (parser
->lexer
);
33415 cp_lexer_consume_token (parser
->lexer
);
33420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33421 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33423 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33424 const char *p
= IDENTIFIER_POINTER (id
);
33426 if (strcmp ("alloc", p
) == 0)
33427 kind
= GOMP_MAP_ALLOC
;
33428 else if (strcmp ("to", p
) == 0)
33429 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33430 else if (strcmp ("from", p
) == 0)
33431 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33432 else if (strcmp ("tofrom", p
) == 0)
33433 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33434 else if (strcmp ("release", p
) == 0)
33435 kind
= GOMP_MAP_RELEASE
;
33438 cp_parser_error (parser
, "invalid map kind");
33439 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33440 /*or_comma=*/false,
33441 /*consume_paren=*/true);
33444 cp_lexer_consume_token (parser
->lexer
);
33445 cp_lexer_consume_token (parser
->lexer
);
33447 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33448 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33450 kind
= GOMP_MAP_DELETE
;
33451 cp_lexer_consume_token (parser
->lexer
);
33452 cp_lexer_consume_token (parser
->lexer
);
33455 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33458 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33459 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33465 device ( expression ) */
33468 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33469 location_t location
)
33473 matching_parens parens
;
33474 if (!parens
.require_open (parser
))
33477 t
= cp_parser_expression (parser
);
33479 if (t
== error_mark_node
33480 || !parens
.require_close (parser
))
33481 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33482 /*or_comma=*/false,
33483 /*consume_paren=*/true);
33485 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33486 "device", location
);
33488 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33489 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33490 OMP_CLAUSE_CHAIN (c
) = list
;
33496 dist_schedule ( static )
33497 dist_schedule ( static , expression ) */
33500 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33501 location_t location
)
33505 matching_parens parens
;
33506 if (!parens
.require_open (parser
))
33509 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33511 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33513 cp_lexer_consume_token (parser
->lexer
);
33515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33517 cp_lexer_consume_token (parser
->lexer
);
33519 t
= cp_parser_assignment_expression (parser
);
33521 if (t
== error_mark_node
)
33523 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33525 if (!parens
.require_close (parser
))
33528 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33531 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33533 OMP_CLAUSE_CHAIN (c
) = list
;
33537 cp_parser_error (parser
, "invalid dist_schedule kind");
33539 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33540 /*or_comma=*/false,
33541 /*consume_paren=*/true);
33546 proc_bind ( proc-bind-kind )
33549 master | close | spread */
33552 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33553 location_t location
)
33556 enum omp_clause_proc_bind_kind kind
;
33558 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33563 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33564 const char *p
= IDENTIFIER_POINTER (id
);
33566 if (strcmp ("master", p
) == 0)
33567 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33568 else if (strcmp ("close", p
) == 0)
33569 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33570 else if (strcmp ("spread", p
) == 0)
33571 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33578 cp_lexer_consume_token (parser
->lexer
);
33579 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33582 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33583 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33585 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33586 OMP_CLAUSE_CHAIN (c
) = list
;
33590 cp_parser_error (parser
, "invalid depend kind");
33592 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33593 /*or_comma=*/false,
33594 /*consume_paren=*/true);
33599 async [( int-expr )] */
33602 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33605 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33607 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33609 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33611 matching_parens parens
;
33612 parens
.consume_open (parser
);
33614 t
= cp_parser_expression (parser
);
33615 if (t
== error_mark_node
33616 || !parens
.require_close (parser
))
33617 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33618 /*or_comma=*/false,
33619 /*consume_paren=*/true);
33622 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33624 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33625 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33626 OMP_CLAUSE_CHAIN (c
) = list
;
33632 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33633 is a bitmask in MASK. Return the list of clauses found. */
33636 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33637 const char *where
, cp_token
*pragma_tok
,
33638 bool finish_p
= true)
33640 tree clauses
= NULL
;
33643 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33646 pragma_omp_clause c_kind
;
33647 omp_clause_code code
;
33648 const char *c_name
;
33649 tree prev
= clauses
;
33651 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33652 cp_lexer_consume_token (parser
->lexer
);
33654 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33655 c_kind
= cp_parser_omp_clause_name (parser
);
33659 case PRAGMA_OACC_CLAUSE_ASYNC
:
33660 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33663 case PRAGMA_OACC_CLAUSE_AUTO
:
33664 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33668 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33669 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33670 c_name
= "collapse";
33672 case PRAGMA_OACC_CLAUSE_COPY
:
33673 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33676 case PRAGMA_OACC_CLAUSE_COPYIN
:
33677 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33680 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33681 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33682 c_name
= "copyout";
33684 case PRAGMA_OACC_CLAUSE_CREATE
:
33685 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33688 case PRAGMA_OACC_CLAUSE_DELETE
:
33689 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33692 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33693 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33694 c_name
= "default";
33696 case PRAGMA_OACC_CLAUSE_DEVICE
:
33697 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33700 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33701 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33702 c_name
= "deviceptr";
33704 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33705 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33706 c_name
= "device_resident";
33708 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33709 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33711 c_name
= "firstprivate";
33713 case PRAGMA_OACC_CLAUSE_GANG
:
33715 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33718 case PRAGMA_OACC_CLAUSE_HOST
:
33719 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33722 case PRAGMA_OACC_CLAUSE_IF
:
33723 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33726 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33727 clauses
= cp_parser_oacc_simple_clause (parser
,
33728 OMP_CLAUSE_INDEPENDENT
,
33730 c_name
= "independent";
33732 case PRAGMA_OACC_CLAUSE_LINK
:
33733 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33736 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33737 code
= OMP_CLAUSE_NUM_GANGS
;
33738 c_name
= "num_gangs";
33739 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33742 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33743 c_name
= "num_workers";
33744 code
= OMP_CLAUSE_NUM_WORKERS
;
33745 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33748 case PRAGMA_OACC_CLAUSE_PRESENT
:
33749 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33750 c_name
= "present";
33752 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33753 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33754 c_name
= "present_or_copy";
33756 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33757 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33758 c_name
= "present_or_copyin";
33760 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33761 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33762 c_name
= "present_or_copyout";
33764 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33765 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33766 c_name
= "present_or_create";
33768 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33769 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33771 c_name
= "private";
33773 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33774 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33775 c_name
= "reduction";
33777 case PRAGMA_OACC_CLAUSE_SELF
:
33778 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33781 case PRAGMA_OACC_CLAUSE_SEQ
:
33782 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33786 case PRAGMA_OACC_CLAUSE_TILE
:
33787 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33790 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33791 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33793 c_name
= "use_device";
33795 case PRAGMA_OACC_CLAUSE_VECTOR
:
33797 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33800 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33801 c_name
= "vector_length";
33802 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33803 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33806 case PRAGMA_OACC_CLAUSE_WAIT
:
33807 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33810 case PRAGMA_OACC_CLAUSE_WORKER
:
33812 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33816 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33822 if (((mask
>> c_kind
) & 1) == 0)
33824 /* Remove the invalid clause(s) from the list to avoid
33825 confusing the rest of the compiler. */
33827 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33832 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33835 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33840 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33841 is a bitmask in MASK. Return the list of clauses found; the result
33842 of clause default goes in *pdefault. */
33845 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33846 const char *where
, cp_token
*pragma_tok
,
33847 bool finish_p
= true)
33849 tree clauses
= NULL
;
33851 cp_token
*token
= NULL
;
33853 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33855 pragma_omp_clause c_kind
;
33856 const char *c_name
;
33857 tree prev
= clauses
;
33859 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33860 cp_lexer_consume_token (parser
->lexer
);
33862 token
= cp_lexer_peek_token (parser
->lexer
);
33863 c_kind
= cp_parser_omp_clause_name (parser
);
33867 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33868 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33870 c_name
= "collapse";
33872 case PRAGMA_OMP_CLAUSE_COPYIN
:
33873 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33876 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33877 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33879 c_name
= "copyprivate";
33881 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33882 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33883 token
->location
, false);
33884 c_name
= "default";
33886 case PRAGMA_OMP_CLAUSE_FINAL
:
33887 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33890 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33891 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33893 c_name
= "firstprivate";
33895 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33896 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33898 c_name
= "grainsize";
33900 case PRAGMA_OMP_CLAUSE_HINT
:
33901 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33905 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33906 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33908 c_name
= "defaultmap";
33910 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33911 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33913 c_name
= "use_device_ptr";
33915 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33916 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33918 c_name
= "is_device_ptr";
33920 case PRAGMA_OMP_CLAUSE_IF
:
33921 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33925 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33926 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33928 c_name
= "lastprivate";
33930 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33931 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33933 c_name
= "mergeable";
33935 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33936 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33939 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33940 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33942 c_name
= "num_tasks";
33944 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33945 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33947 c_name
= "num_threads";
33949 case PRAGMA_OMP_CLAUSE_ORDERED
:
33950 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33952 c_name
= "ordered";
33954 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33955 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33957 c_name
= "priority";
33959 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33960 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33962 c_name
= "private";
33964 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33965 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33966 c_name
= "reduction";
33968 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33969 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33971 c_name
= "schedule";
33973 case PRAGMA_OMP_CLAUSE_SHARED
:
33974 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33978 case PRAGMA_OMP_CLAUSE_UNTIED
:
33979 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33983 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33984 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33985 clauses
, token
->location
);
33986 c_name
= "inbranch";
33988 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33989 clauses
= cp_parser_omp_clause_branch (parser
,
33990 OMP_CLAUSE_NOTINBRANCH
,
33991 clauses
, token
->location
);
33992 c_name
= "notinbranch";
33994 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33995 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33996 clauses
, token
->location
);
33997 c_name
= "parallel";
34001 error_at (token
->location
, "%qs must be the first clause of %qs",
34006 case PRAGMA_OMP_CLAUSE_FOR
:
34007 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34008 clauses
, token
->location
);
34011 goto clause_not_first
;
34013 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34014 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34015 clauses
, token
->location
);
34016 c_name
= "sections";
34018 goto clause_not_first
;
34020 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34021 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34022 clauses
, token
->location
);
34023 c_name
= "taskgroup";
34025 goto clause_not_first
;
34027 case PRAGMA_OMP_CLAUSE_LINK
:
34028 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34031 case PRAGMA_OMP_CLAUSE_TO
:
34032 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34033 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34036 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34039 case PRAGMA_OMP_CLAUSE_FROM
:
34040 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34043 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34044 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34046 c_name
= "uniform";
34048 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34049 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34051 c_name
= "num_teams";
34053 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34054 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34056 c_name
= "thread_limit";
34058 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34059 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34060 c_name
= "aligned";
34062 case PRAGMA_OMP_CLAUSE_LINEAR
:
34064 bool declare_simd
= false;
34065 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34066 declare_simd
= true;
34067 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34071 case PRAGMA_OMP_CLAUSE_DEPEND
:
34072 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34076 case PRAGMA_OMP_CLAUSE_MAP
:
34077 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34080 case PRAGMA_OMP_CLAUSE_DEVICE
:
34081 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34085 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34086 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34088 c_name
= "dist_schedule";
34090 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34091 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34093 c_name
= "proc_bind";
34095 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34096 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34098 c_name
= "safelen";
34100 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34101 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34103 c_name
= "simdlen";
34105 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34106 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34108 c_name
= "nogroup";
34110 case PRAGMA_OMP_CLAUSE_THREADS
:
34112 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34113 clauses
, token
->location
);
34114 c_name
= "threads";
34116 case PRAGMA_OMP_CLAUSE_SIMD
:
34118 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34119 clauses
, token
->location
);
34123 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34129 if (((mask
>> c_kind
) & 1) == 0)
34131 /* Remove the invalid clause(s) from the list to avoid
34132 confusing the rest of the compiler. */
34134 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34138 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34141 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34142 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34144 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34153 In practice, we're also interested in adding the statement to an
34154 outer node. So it is convenient if we work around the fact that
34155 cp_parser_statement calls add_stmt. */
34158 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34160 unsigned save
= parser
->in_statement
;
34162 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34163 This preserves the "not within loop or switch" style error messages
34164 for nonsense cases like
34170 if (parser
->in_statement
)
34171 parser
->in_statement
= IN_OMP_BLOCK
;
34177 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34179 parser
->in_statement
= save
;
34183 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34185 tree stmt
= begin_omp_structured_block ();
34186 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34188 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34190 cp_parser_end_omp_structured_block (parser
, save
);
34191 return finish_omp_structured_block (stmt
);
34195 # pragma omp atomic new-line
34199 x binop= expr | x++ | ++x | x-- | --x
34201 +, *, -, /, &, ^, |, <<, >>
34203 where x is an lvalue expression with scalar type.
34206 # pragma omp atomic new-line
34209 # pragma omp atomic read new-line
34212 # pragma omp atomic write new-line
34215 # pragma omp atomic update new-line
34218 # pragma omp atomic capture new-line
34221 # pragma omp atomic capture new-line
34229 expression-stmt | x = x binop expr
34231 v = expression-stmt
34233 { v = x; update-stmt; } | { update-stmt; v = x; }
34237 expression-stmt | x = x binop expr | x = expr binop x
34241 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34243 where x and v are lvalue expressions with scalar type. */
34246 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34248 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34249 tree rhs1
= NULL_TREE
, orig_lhs
;
34250 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34251 bool structured_block
= false;
34252 bool seq_cst
= false;
34254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34256 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34257 const char *p
= IDENTIFIER_POINTER (id
);
34259 if (!strcmp (p
, "seq_cst"))
34262 cp_lexer_consume_token (parser
->lexer
);
34263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34264 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34265 cp_lexer_consume_token (parser
->lexer
);
34268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34270 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34271 const char *p
= IDENTIFIER_POINTER (id
);
34273 if (!strcmp (p
, "read"))
34274 code
= OMP_ATOMIC_READ
;
34275 else if (!strcmp (p
, "write"))
34277 else if (!strcmp (p
, "update"))
34279 else if (!strcmp (p
, "capture"))
34280 code
= OMP_ATOMIC_CAPTURE_NEW
;
34284 cp_lexer_consume_token (parser
->lexer
);
34288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34289 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34290 cp_lexer_consume_token (parser
->lexer
);
34292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34294 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34295 const char *p
= IDENTIFIER_POINTER (id
);
34297 if (!strcmp (p
, "seq_cst"))
34300 cp_lexer_consume_token (parser
->lexer
);
34304 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34308 case OMP_ATOMIC_READ
:
34309 case NOP_EXPR
: /* atomic write */
34310 v
= cp_parser_unary_expression (parser
);
34311 if (v
== error_mark_node
)
34313 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34315 if (code
== NOP_EXPR
)
34316 lhs
= cp_parser_expression (parser
);
34318 lhs
= cp_parser_unary_expression (parser
);
34319 if (lhs
== error_mark_node
)
34321 if (code
== NOP_EXPR
)
34323 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34331 case OMP_ATOMIC_CAPTURE_NEW
:
34332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34334 cp_lexer_consume_token (parser
->lexer
);
34335 structured_block
= true;
34339 v
= cp_parser_unary_expression (parser
);
34340 if (v
== error_mark_node
)
34342 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34350 lhs
= cp_parser_unary_expression (parser
);
34352 switch (TREE_CODE (lhs
))
34357 case POSTINCREMENT_EXPR
:
34358 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34359 code
= OMP_ATOMIC_CAPTURE_OLD
;
34361 case PREINCREMENT_EXPR
:
34362 lhs
= TREE_OPERAND (lhs
, 0);
34363 opcode
= PLUS_EXPR
;
34364 rhs
= integer_one_node
;
34367 case POSTDECREMENT_EXPR
:
34368 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34369 code
= OMP_ATOMIC_CAPTURE_OLD
;
34371 case PREDECREMENT_EXPR
:
34372 lhs
= TREE_OPERAND (lhs
, 0);
34373 opcode
= MINUS_EXPR
;
34374 rhs
= integer_one_node
;
34377 case COMPOUND_EXPR
:
34378 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34379 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34380 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34381 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34382 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34383 (TREE_OPERAND (lhs
, 1), 0), 0)))
34385 /* Undo effects of boolean_increment for post {in,de}crement. */
34386 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34389 if (TREE_CODE (lhs
) == MODIFY_EXPR
34390 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34392 /* Undo effects of boolean_increment. */
34393 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34395 /* This is pre or post increment. */
34396 rhs
= TREE_OPERAND (lhs
, 1);
34397 lhs
= TREE_OPERAND (lhs
, 0);
34399 if (code
== OMP_ATOMIC_CAPTURE_NEW
34400 && !structured_block
34401 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34402 code
= OMP_ATOMIC_CAPTURE_OLD
;
34408 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34411 opcode
= MULT_EXPR
;
34414 opcode
= TRUNC_DIV_EXPR
;
34417 opcode
= PLUS_EXPR
;
34420 opcode
= MINUS_EXPR
;
34422 case CPP_LSHIFT_EQ
:
34423 opcode
= LSHIFT_EXPR
;
34425 case CPP_RSHIFT_EQ
:
34426 opcode
= RSHIFT_EXPR
;
34429 opcode
= BIT_AND_EXPR
;
34432 opcode
= BIT_IOR_EXPR
;
34435 opcode
= BIT_XOR_EXPR
;
34438 enum cp_parser_prec oprec
;
34440 cp_lexer_consume_token (parser
->lexer
);
34441 cp_parser_parse_tentatively (parser
);
34442 rhs1
= cp_parser_simple_cast_expression (parser
);
34443 if (rhs1
== error_mark_node
)
34445 cp_parser_abort_tentative_parse (parser
);
34446 cp_parser_simple_cast_expression (parser
);
34449 token
= cp_lexer_peek_token (parser
->lexer
);
34450 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34452 cp_parser_abort_tentative_parse (parser
);
34453 cp_parser_parse_tentatively (parser
);
34454 rhs
= cp_parser_binary_expression (parser
, false, true,
34455 PREC_NOT_OPERATOR
, NULL
);
34456 if (rhs
== error_mark_node
)
34458 cp_parser_abort_tentative_parse (parser
);
34459 cp_parser_binary_expression (parser
, false, true,
34460 PREC_NOT_OPERATOR
, NULL
);
34463 switch (TREE_CODE (rhs
))
34466 case TRUNC_DIV_EXPR
:
34475 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34477 if (cp_parser_parse_definitely (parser
))
34479 opcode
= TREE_CODE (rhs
);
34480 rhs1
= TREE_OPERAND (rhs
, 0);
34481 rhs
= TREE_OPERAND (rhs
, 1);
34491 cp_parser_abort_tentative_parse (parser
);
34492 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34494 rhs
= cp_parser_expression (parser
);
34495 if (rhs
== error_mark_node
)
34501 cp_parser_error (parser
,
34502 "invalid form of %<#pragma omp atomic%>");
34505 if (!cp_parser_parse_definitely (parser
))
34507 switch (token
->type
)
34509 case CPP_SEMICOLON
:
34510 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34512 code
= OMP_ATOMIC_CAPTURE_OLD
;
34517 cp_lexer_consume_token (parser
->lexer
);
34520 else if (structured_block
)
34527 cp_parser_error (parser
,
34528 "invalid form of %<#pragma omp atomic%>");
34531 opcode
= MULT_EXPR
;
34534 opcode
= TRUNC_DIV_EXPR
;
34537 opcode
= PLUS_EXPR
;
34540 opcode
= MINUS_EXPR
;
34543 opcode
= LSHIFT_EXPR
;
34546 opcode
= RSHIFT_EXPR
;
34549 opcode
= BIT_AND_EXPR
;
34552 opcode
= BIT_IOR_EXPR
;
34555 opcode
= BIT_XOR_EXPR
;
34558 cp_parser_error (parser
,
34559 "invalid operator for %<#pragma omp atomic%>");
34562 oprec
= TOKEN_PRECEDENCE (token
);
34563 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34564 if (commutative_tree_code (opcode
))
34565 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34566 cp_lexer_consume_token (parser
->lexer
);
34567 rhs
= cp_parser_binary_expression (parser
, false, false,
34569 if (rhs
== error_mark_node
)
34574 cp_parser_error (parser
,
34575 "invalid operator for %<#pragma omp atomic%>");
34578 cp_lexer_consume_token (parser
->lexer
);
34580 rhs
= cp_parser_expression (parser
);
34581 if (rhs
== error_mark_node
)
34586 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34588 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34590 v
= cp_parser_unary_expression (parser
);
34591 if (v
== error_mark_node
)
34593 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34595 lhs1
= cp_parser_unary_expression (parser
);
34596 if (lhs1
== error_mark_node
)
34599 if (structured_block
)
34601 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34602 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34605 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34606 if (!structured_block
)
34607 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34611 cp_parser_skip_to_end_of_block_or_statement (parser
);
34612 if (structured_block
)
34614 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34615 cp_lexer_consume_token (parser
->lexer
);
34616 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34618 cp_parser_skip_to_end_of_block_or_statement (parser
);
34619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34620 cp_lexer_consume_token (parser
->lexer
);
34627 # pragma omp barrier new-line */
34630 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34632 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34633 finish_omp_barrier ();
34637 # pragma omp critical [(name)] new-line
34641 # pragma omp critical [(name) [hint(expression)]] new-line
34642 structured-block */
34644 #define OMP_CRITICAL_CLAUSE_MASK \
34645 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34648 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34650 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34654 matching_parens parens
;
34655 parens
.consume_open (parser
);
34657 name
= cp_parser_identifier (parser
);
34659 if (name
== error_mark_node
34660 || !parens
.require_close (parser
))
34661 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34662 /*or_comma=*/false,
34663 /*consume_paren=*/true);
34664 if (name
== error_mark_node
)
34667 clauses
= cp_parser_omp_all_clauses (parser
,
34668 OMP_CRITICAL_CLAUSE_MASK
,
34669 "#pragma omp critical", pragma_tok
);
34672 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34674 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34675 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34679 # pragma omp flush flush-vars[opt] new-line
34682 ( variable-list ) */
34685 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34688 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34689 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34691 finish_omp_flush ();
34694 /* Helper function, to parse omp for increment expression. */
34697 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34699 tree cond
= cp_parser_binary_expression (parser
, false, true,
34700 PREC_NOT_OPERATOR
, NULL
);
34701 if (cond
== error_mark_node
34702 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34704 cp_parser_skip_to_end_of_statement (parser
);
34705 return error_mark_node
;
34708 switch (TREE_CODE (cond
))
34716 /* Fall through: OpenMP disallows NE_EXPR. */
34717 gcc_fallthrough ();
34719 return error_mark_node
;
34722 /* If decl is an iterator, preserve LHS and RHS of the relational
34723 expr until finish_omp_for. */
34725 && (type_dependent_expression_p (decl
)
34726 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34729 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34731 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34732 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34733 /*overload=*/NULL
, tf_warning_or_error
);
34736 /* Helper function, to parse omp for increment expression. */
34739 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34747 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34749 op
= (token
->type
== CPP_PLUS_PLUS
34750 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34751 cp_lexer_consume_token (parser
->lexer
);
34752 lhs
= cp_parser_simple_cast_expression (parser
);
34754 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34755 return error_mark_node
;
34756 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34759 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34761 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34762 return error_mark_node
;
34764 token
= cp_lexer_peek_token (parser
->lexer
);
34765 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34767 op
= (token
->type
== CPP_PLUS_PLUS
34768 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34769 cp_lexer_consume_token (parser
->lexer
);
34770 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34773 op
= cp_parser_assignment_operator_opt (parser
);
34774 if (op
== ERROR_MARK
)
34775 return error_mark_node
;
34777 if (op
!= NOP_EXPR
)
34779 rhs
= cp_parser_assignment_expression (parser
);
34780 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34781 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34784 lhs
= cp_parser_binary_expression (parser
, false, false,
34785 PREC_ADDITIVE_EXPRESSION
, NULL
);
34786 token
= cp_lexer_peek_token (parser
->lexer
);
34787 decl_first
= (lhs
== decl
34788 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34791 if (token
->type
!= CPP_PLUS
34792 && token
->type
!= CPP_MINUS
)
34793 return error_mark_node
;
34797 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34798 cp_lexer_consume_token (parser
->lexer
);
34799 rhs
= cp_parser_binary_expression (parser
, false, false,
34800 PREC_ADDITIVE_EXPRESSION
, NULL
);
34801 token
= cp_lexer_peek_token (parser
->lexer
);
34802 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34804 if (lhs
== NULL_TREE
)
34806 if (op
== PLUS_EXPR
)
34809 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34810 tf_warning_or_error
);
34813 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34814 ERROR_MARK
, NULL
, tf_warning_or_error
);
34817 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34822 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34823 || op
== MINUS_EXPR
)
34824 return error_mark_node
;
34825 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34828 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34830 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34833 /* Parse the initialization statement of an OpenMP for loop.
34835 Return true if the resulting construct should have an
34836 OMP_CLAUSE_PRIVATE added to it. */
34839 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34840 tree
&this_pre_body
,
34841 vec
<tree
, va_gc
> *for_block
,
34847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34850 tree add_private_clause
= NULL_TREE
;
34852 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34856 integer-type var = lb
34857 random-access-iterator-type var = lb
34858 pointer-type var = lb
34860 cp_decl_specifier_seq type_specifiers
;
34862 /* First, try to parse as an initialized declaration. See
34863 cp_parser_condition, from whence the bulk of this is copied. */
34865 cp_parser_parse_tentatively (parser
);
34866 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34867 /*is_trailing_return=*/false,
34869 if (cp_parser_parse_definitely (parser
))
34871 /* If parsing a type specifier seq succeeded, then this
34872 MUST be a initialized declaration. */
34873 tree asm_specification
, attributes
;
34874 cp_declarator
*declarator
;
34876 declarator
= cp_parser_declarator (parser
,
34877 CP_PARSER_DECLARATOR_NAMED
,
34878 /*ctor_dtor_or_conv_p=*/NULL
,
34879 /*parenthesized_p=*/NULL
,
34880 /*member_p=*/false,
34881 /*friend_p=*/false);
34882 attributes
= cp_parser_attributes_opt (parser
);
34883 asm_specification
= cp_parser_asm_specification_opt (parser
);
34885 if (declarator
== cp_error_declarator
)
34886 cp_parser_skip_to_end_of_statement (parser
);
34890 tree pushed_scope
, auto_node
;
34892 decl
= start_decl (declarator
, &type_specifiers
,
34893 SD_INITIALIZED
, attributes
,
34894 /*prefix_attributes=*/NULL_TREE
,
34897 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34898 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34900 if (cp_lexer_next_token_is (parser
->lexer
,
34902 error ("parenthesized initialization is not allowed in "
34903 "OpenMP %<for%> loop");
34905 /* Trigger an error. */
34906 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34908 init
= error_mark_node
;
34909 cp_parser_skip_to_end_of_statement (parser
);
34911 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34912 || type_dependent_expression_p (decl
)
34915 bool is_direct_init
, is_non_constant_init
;
34917 init
= cp_parser_initializer (parser
,
34919 &is_non_constant_init
);
34924 = do_auto_deduction (TREE_TYPE (decl
), init
,
34927 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34928 && !type_dependent_expression_p (decl
))
34932 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34934 LOOKUP_ONLYCONVERTING
);
34936 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34938 vec_safe_push (for_block
, this_pre_body
);
34943 init
= pop_stmt_list (this_pre_body
);
34944 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34946 tree_stmt_iterator i
= tsi_start (init
);
34947 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34948 while (!tsi_end_p (i
))
34950 tree t
= tsi_stmt (i
);
34951 if (TREE_CODE (t
) == DECL_EXPR
34952 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34955 vec_safe_push (for_block
, t
);
34960 if (tsi_one_before_end_p (i
))
34962 tree t
= tsi_stmt (i
);
34964 free_stmt_list (init
);
34969 this_pre_body
= NULL_TREE
;
34974 cp_lexer_consume_token (parser
->lexer
);
34975 init
= cp_parser_assignment_expression (parser
);
34978 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34979 init
= error_mark_node
;
34981 cp_finish_decl (decl
, NULL_TREE
,
34982 /*init_const_expr_p=*/false,
34984 LOOKUP_ONLYCONVERTING
);
34988 pop_scope (pushed_scope
);
34994 /* If parsing a type specifier sequence failed, then
34995 this MUST be a simple expression. */
34996 cp_parser_parse_tentatively (parser
);
34997 decl
= cp_parser_primary_expression (parser
, false, false,
34999 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35000 if (!cp_parser_error_occurred (parser
)
35002 && (TREE_CODE (decl
) == COMPONENT_REF
35003 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35005 cp_parser_abort_tentative_parse (parser
);
35006 cp_parser_parse_tentatively (parser
);
35007 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35008 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35009 /*check_dependency_p=*/true,
35010 /*template_p=*/NULL
,
35011 /*declarator_p=*/false,
35012 /*optional_p=*/false);
35013 if (name
!= error_mark_node
35014 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35016 decl
= cp_parser_lookup_name_simple (parser
, name
,
35018 if (TREE_CODE (decl
) == FIELD_DECL
)
35019 add_private_clause
= omp_privatize_field (decl
, false);
35021 cp_parser_abort_tentative_parse (parser
);
35022 cp_parser_parse_tentatively (parser
);
35023 decl
= cp_parser_primary_expression (parser
, false, false,
35026 if (!cp_parser_error_occurred (parser
)
35029 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35033 cp_parser_parse_definitely (parser
);
35034 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35035 rhs
= cp_parser_assignment_expression (parser
);
35037 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35040 tf_warning_or_error
));
35041 if (!add_private_clause
)
35042 add_private_clause
= decl
;
35047 cp_parser_abort_tentative_parse (parser
);
35048 init
= cp_parser_expression (parser
);
35051 if (TREE_CODE (init
) == MODIFY_EXPR
35052 || TREE_CODE (init
) == MODOP_EXPR
)
35053 real_decl
= TREE_OPERAND (init
, 0);
35057 return add_private_clause
;
35060 /* Parse the restricted form of the for statement allowed by OpenMP. */
35063 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35064 tree
*cclauses
, bool *if_p
)
35066 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35067 tree real_decl
, initv
, condv
, incrv
, declv
;
35068 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35069 location_t loc_first
;
35070 bool collapse_err
= false;
35071 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35072 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35073 auto_vec
<tree
, 4> orig_inits
;
35074 bool tiling
= false;
35076 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35077 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35078 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35079 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35082 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35084 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35085 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35088 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35091 if (ordered
&& ordered
< collapse
)
35093 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35094 "%<ordered%> clause parameter is less than %<collapse%>");
35095 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35096 = build_int_cst (NULL_TREE
, collapse
);
35097 ordered
= collapse
;
35101 for (tree
*pc
= &clauses
; *pc
; )
35102 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35104 error_at (OMP_CLAUSE_LOCATION (*pc
),
35105 "%<linear%> clause may not be specified together "
35106 "with %<ordered%> clause with a parameter");
35107 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35110 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35113 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35114 count
= ordered
? ordered
: collapse
;
35116 declv
= make_tree_vec (count
);
35117 initv
= make_tree_vec (count
);
35118 condv
= make_tree_vec (count
);
35119 incrv
= make_tree_vec (count
);
35121 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35123 for (i
= 0; i
< count
; i
++)
35125 int bracecount
= 0;
35126 tree add_private_clause
= NULL_TREE
;
35129 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35132 cp_parser_error (parser
, "for statement expected");
35135 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35137 matching_parens parens
;
35138 if (!parens
.require_open (parser
))
35141 init
= orig_init
= decl
= real_decl
= NULL
;
35142 this_pre_body
= push_stmt_list ();
35145 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35146 init
, orig_init
, decl
, real_decl
);
35148 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35151 this_pre_body
= pop_stmt_list (this_pre_body
);
35155 pre_body
= push_stmt_list ();
35157 add_stmt (this_pre_body
);
35158 pre_body
= pop_stmt_list (pre_body
);
35161 pre_body
= this_pre_body
;
35166 if (cclauses
!= NULL
35167 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35168 && real_decl
!= NULL_TREE
)
35171 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35172 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35173 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35175 error_at (loc
, "iteration variable %qD"
35176 " should not be firstprivate", real_decl
);
35177 *c
= OMP_CLAUSE_CHAIN (*c
);
35179 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35180 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35182 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35184 *c
= OMP_CLAUSE_CHAIN (*c
);
35185 if (code
== OMP_SIMD
)
35187 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35188 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35192 OMP_CLAUSE_CHAIN (l
) = clauses
;
35195 add_private_clause
= NULL_TREE
;
35199 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35200 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35201 add_private_clause
= NULL_TREE
;
35202 c
= &OMP_CLAUSE_CHAIN (*c
);
35206 if (add_private_clause
)
35209 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35211 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35212 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35213 && OMP_CLAUSE_DECL (c
) == decl
)
35215 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35216 && OMP_CLAUSE_DECL (c
) == decl
)
35217 error_at (loc
, "iteration variable %qD "
35218 "should not be firstprivate",
35220 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35221 && OMP_CLAUSE_DECL (c
) == decl
)
35222 error_at (loc
, "iteration variable %qD should not be reduction",
35227 if (code
!= OMP_SIMD
)
35228 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35229 else if (collapse
== 1)
35230 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35232 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35233 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35234 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35237 OMP_CLAUSE_CHAIN (c
) = clauses
;
35239 /* For linear, signal that we need to fill up
35240 the so far unknown linear step. */
35241 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35242 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35248 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35249 cond
= cp_parser_omp_for_cond (parser
, decl
);
35250 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35253 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35255 /* If decl is an iterator, preserve the operator on decl
35256 until finish_omp_for. */
35258 && ((processing_template_decl
35259 && (TREE_TYPE (real_decl
) == NULL_TREE
35260 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35261 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35262 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35264 incr
= cp_parser_expression (parser
);
35265 if (!EXPR_HAS_LOCATION (incr
))
35266 protected_set_expr_location (incr
, input_location
);
35269 if (!parens
.require_close (parser
))
35270 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35271 /*or_comma=*/false,
35272 /*consume_paren=*/true);
35274 TREE_VEC_ELT (declv
, i
) = decl
;
35275 TREE_VEC_ELT (initv
, i
) = init
;
35276 TREE_VEC_ELT (condv
, i
) = cond
;
35277 TREE_VEC_ELT (incrv
, i
) = incr
;
35280 orig_inits
.safe_grow_cleared (i
+ 1);
35281 orig_inits
[i
] = orig_init
;
35284 if (i
== count
- 1)
35287 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35288 in between the collapsed for loops to be still considered perfectly
35289 nested. Hopefully the final version clarifies this.
35290 For now handle (multiple) {'s and empty statements. */
35291 cp_parser_parse_tentatively (parser
);
35294 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35296 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35298 cp_lexer_consume_token (parser
->lexer
);
35301 else if (bracecount
35302 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35303 cp_lexer_consume_token (parser
->lexer
);
35306 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35307 error_at (loc
, "not enough for loops to collapse");
35308 collapse_err
= true;
35309 cp_parser_abort_tentative_parse (parser
);
35317 cp_parser_parse_definitely (parser
);
35318 nbraces
+= bracecount
;
35325 /* Note that we saved the original contents of this flag when we entered
35326 the structured block, and so we don't need to re-save it here. */
35327 parser
->in_statement
= IN_OMP_FOR
;
35329 /* Note that the grammar doesn't call for a structured block here,
35330 though the loop as a whole is a structured block. */
35331 body
= push_stmt_list ();
35332 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35333 body
= pop_stmt_list (body
);
35335 if (declv
== NULL_TREE
)
35338 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35339 body
, pre_body
, &orig_inits
, clauses
);
35343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35345 cp_lexer_consume_token (parser
->lexer
);
35348 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35349 cp_lexer_consume_token (parser
->lexer
);
35354 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35355 "collapsed loops not perfectly nested");
35357 collapse_err
= true;
35358 cp_parser_statement_seq_opt (parser
, NULL
);
35359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35364 while (!for_block
->is_empty ())
35366 tree t
= for_block
->pop ();
35367 if (TREE_CODE (t
) == STATEMENT_LIST
)
35368 add_stmt (pop_stmt_list (t
));
35372 release_tree_vector (for_block
);
35377 /* Helper function for OpenMP parsing, split clauses and call
35378 finish_omp_clauses on each of the set of clauses afterwards. */
35381 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35382 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35385 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35386 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35388 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35392 #pragma omp simd simd-clause[optseq] new-line
35395 #define OMP_SIMD_CLAUSE_MASK \
35396 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35406 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35407 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35410 tree clauses
, sb
, ret
;
35412 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35414 strcat (p_name
, " simd");
35415 mask
|= OMP_SIMD_CLAUSE_MASK
;
35417 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35421 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35422 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35423 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35424 OMP_CLAUSE_ORDERED
);
35425 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35427 error_at (OMP_CLAUSE_LOCATION (c
),
35428 "%<ordered%> clause with parameter may not be specified "
35429 "on %qs construct", p_name
);
35430 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35434 sb
= begin_omp_structured_block ();
35435 save
= cp_parser_begin_omp_structured_block (parser
);
35437 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35439 cp_parser_end_omp_structured_block (parser
, save
);
35440 add_stmt (finish_omp_structured_block (sb
));
35446 #pragma omp for for-clause[optseq] new-line
35450 #pragma omp for simd for-simd-clause[optseq] new-line
35453 #define OMP_FOR_CLAUSE_MASK \
35454 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35465 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35466 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35469 tree clauses
, sb
, ret
;
35471 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35473 strcat (p_name
, " for");
35474 mask
|= OMP_FOR_CLAUSE_MASK
;
35475 /* parallel for{, simd} disallows nowait clause, but for
35476 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35477 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35478 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35479 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35480 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35481 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35485 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35486 const char *p
= IDENTIFIER_POINTER (id
);
35488 if (strcmp (p
, "simd") == 0)
35490 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35491 if (cclauses
== NULL
)
35492 cclauses
= cclauses_buf
;
35494 cp_lexer_consume_token (parser
->lexer
);
35495 if (!flag_openmp
) /* flag_openmp_simd */
35496 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35498 sb
= begin_omp_structured_block ();
35499 save
= cp_parser_begin_omp_structured_block (parser
);
35500 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35502 cp_parser_end_omp_structured_block (parser
, save
);
35503 tree body
= finish_omp_structured_block (sb
);
35506 ret
= make_node (OMP_FOR
);
35507 TREE_TYPE (ret
) = void_type_node
;
35508 OMP_FOR_BODY (ret
) = body
;
35509 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35510 SET_EXPR_LOCATION (ret
, loc
);
35515 if (!flag_openmp
) /* flag_openmp_simd */
35517 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35521 /* Composite distribute parallel for disallows linear clause. */
35522 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35523 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35525 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35529 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35530 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35533 sb
= begin_omp_structured_block ();
35534 save
= cp_parser_begin_omp_structured_block (parser
);
35536 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35538 cp_parser_end_omp_structured_block (parser
, save
);
35539 add_stmt (finish_omp_structured_block (sb
));
35545 # pragma omp master new-line
35546 structured-block */
35549 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35551 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35552 return c_finish_omp_master (input_location
,
35553 cp_parser_omp_structured_block (parser
, if_p
));
35557 # pragma omp ordered new-line
35561 # pragma omp ordered ordered-clauses new-line
35562 structured-block */
35564 #define OMP_ORDERED_CLAUSE_MASK \
35565 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35568 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35569 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35572 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35573 enum pragma_context context
, bool *if_p
)
35575 location_t loc
= pragma_tok
->location
;
35577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35579 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35580 const char *p
= IDENTIFIER_POINTER (id
);
35582 if (strcmp (p
, "depend") == 0)
35584 if (!flag_openmp
) /* flag_openmp_simd */
35586 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35589 if (context
== pragma_stmt
)
35591 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35592 "%<depend%> clause may only be used in compound "
35594 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35598 = cp_parser_omp_all_clauses (parser
,
35599 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35600 "#pragma omp ordered", pragma_tok
);
35601 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35607 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35608 "#pragma omp ordered", pragma_tok
);
35610 if (!flag_openmp
/* flag_openmp_simd */
35611 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35614 c_finish_omp_ordered (loc
, clauses
,
35615 cp_parser_omp_structured_block (parser
, if_p
));
35622 { section-sequence }
35625 section-directive[opt] structured-block
35626 section-sequence section-directive structured-block */
35629 cp_parser_omp_sections_scope (cp_parser
*parser
)
35631 tree stmt
, substmt
;
35632 bool error_suppress
= false;
35635 matching_braces braces
;
35636 if (!braces
.require_open (parser
))
35639 stmt
= push_stmt_list ();
35641 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35642 != PRAGMA_OMP_SECTION
)
35644 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35645 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35646 add_stmt (substmt
);
35651 tok
= cp_lexer_peek_token (parser
->lexer
);
35652 if (tok
->type
== CPP_CLOSE_BRACE
)
35654 if (tok
->type
== CPP_EOF
)
35657 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35659 cp_lexer_consume_token (parser
->lexer
);
35660 cp_parser_require_pragma_eol (parser
, tok
);
35661 error_suppress
= false;
35663 else if (!error_suppress
)
35665 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35666 error_suppress
= true;
35669 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35670 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35671 add_stmt (substmt
);
35673 braces
.require_close (parser
);
35675 substmt
= pop_stmt_list (stmt
);
35677 stmt
= make_node (OMP_SECTIONS
);
35678 TREE_TYPE (stmt
) = void_type_node
;
35679 OMP_SECTIONS_BODY (stmt
) = substmt
;
35686 # pragma omp sections sections-clause[optseq] newline
35689 #define OMP_SECTIONS_CLAUSE_MASK \
35690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35697 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35698 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35701 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35703 strcat (p_name
, " sections");
35704 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35706 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35708 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35712 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35713 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35716 ret
= cp_parser_omp_sections_scope (parser
);
35718 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35724 # pragma omp parallel parallel-clause[optseq] new-line
35726 # pragma omp parallel for parallel-for-clause[optseq] new-line
35728 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35732 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35733 structured-block */
35735 #define OMP_PARALLEL_CLAUSE_MASK \
35736 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35747 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35748 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35751 tree stmt
, clauses
, block
;
35753 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35755 strcat (p_name
, " parallel");
35756 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35757 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35758 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35759 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35760 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35762 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35764 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35765 if (cclauses
== NULL
)
35766 cclauses
= cclauses_buf
;
35768 cp_lexer_consume_token (parser
->lexer
);
35769 if (!flag_openmp
) /* flag_openmp_simd */
35770 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35772 block
= begin_omp_parallel ();
35773 save
= cp_parser_begin_omp_structured_block (parser
);
35774 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35776 cp_parser_end_omp_structured_block (parser
, save
);
35777 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35779 if (ret
== NULL_TREE
)
35781 OMP_PARALLEL_COMBINED (stmt
) = 1;
35784 /* When combined with distribute, parallel has to be followed by for.
35785 #pragma omp target parallel is allowed though. */
35787 && (mask
& (OMP_CLAUSE_MASK_1
35788 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35790 error_at (loc
, "expected %<for%> after %qs", p_name
);
35791 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35794 else if (!flag_openmp
) /* flag_openmp_simd */
35796 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35799 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35801 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35802 const char *p
= IDENTIFIER_POINTER (id
);
35803 if (strcmp (p
, "sections") == 0)
35805 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35806 cclauses
= cclauses_buf
;
35808 cp_lexer_consume_token (parser
->lexer
);
35809 block
= begin_omp_parallel ();
35810 save
= cp_parser_begin_omp_structured_block (parser
);
35811 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35812 cp_parser_end_omp_structured_block (parser
, save
);
35813 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35815 OMP_PARALLEL_COMBINED (stmt
) = 1;
35820 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35824 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35825 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35828 block
= begin_omp_parallel ();
35829 save
= cp_parser_begin_omp_structured_block (parser
);
35830 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35831 cp_parser_end_omp_structured_block (parser
, save
);
35832 stmt
= finish_omp_parallel (clauses
, block
);
35837 # pragma omp single single-clause[optseq] new-line
35838 structured-block */
35840 #define OMP_SINGLE_CLAUSE_MASK \
35841 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35847 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35849 tree stmt
= make_node (OMP_SINGLE
);
35850 TREE_TYPE (stmt
) = void_type_node
;
35852 OMP_SINGLE_CLAUSES (stmt
)
35853 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35854 "#pragma omp single", pragma_tok
);
35855 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35857 return add_stmt (stmt
);
35861 # pragma omp task task-clause[optseq] new-line
35862 structured-block */
35864 #define OMP_TASK_CLAUSE_MASK \
35865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35877 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35879 tree clauses
, block
;
35882 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35883 "#pragma omp task", pragma_tok
);
35884 block
= begin_omp_task ();
35885 save
= cp_parser_begin_omp_structured_block (parser
);
35886 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35887 cp_parser_end_omp_structured_block (parser
, save
);
35888 return finish_omp_task (clauses
, block
);
35892 # pragma omp taskwait new-line */
35895 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35897 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35898 finish_omp_taskwait ();
35902 # pragma omp taskyield new-line */
35905 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35907 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35908 finish_omp_taskyield ();
35912 # pragma omp taskgroup new-line
35913 structured-block */
35916 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35918 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35919 return c_finish_omp_taskgroup (input_location
,
35920 cp_parser_omp_structured_block (parser
,
35926 # pragma omp threadprivate (variable-list) */
35929 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35933 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35934 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35936 finish_omp_threadprivate (vars
);
35940 # pragma omp cancel cancel-clause[optseq] new-line */
35942 #define OMP_CANCEL_CLAUSE_MASK \
35943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35950 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35952 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35953 "#pragma omp cancel", pragma_tok
);
35954 finish_omp_cancel (clauses
);
35958 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35960 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35967 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35968 enum pragma_context context
)
35971 bool point_seen
= false;
35973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35975 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35976 const char *p
= IDENTIFIER_POINTER (id
);
35978 if (strcmp (p
, "point") == 0)
35980 cp_lexer_consume_token (parser
->lexer
);
35986 cp_parser_error (parser
, "expected %<point%>");
35987 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35991 if (context
!= pragma_compound
)
35993 if (context
== pragma_stmt
)
35994 error_at (pragma_tok
->location
,
35995 "%<#pragma %s%> may only be used in compound statements",
35996 "omp cancellation point");
35998 cp_parser_error (parser
, "expected declaration specifiers");
35999 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36003 clauses
= cp_parser_omp_all_clauses (parser
,
36004 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36005 "#pragma omp cancellation point",
36007 finish_omp_cancellation_point (clauses
);
36011 #pragma omp distribute distribute-clause[optseq] new-line
36014 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36022 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36023 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36026 tree clauses
, sb
, ret
;
36028 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36030 strcat (p_name
, " distribute");
36031 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36035 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36036 const char *p
= IDENTIFIER_POINTER (id
);
36038 bool parallel
= false;
36040 if (strcmp (p
, "simd") == 0)
36043 parallel
= strcmp (p
, "parallel") == 0;
36044 if (parallel
|| simd
)
36046 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36047 if (cclauses
== NULL
)
36048 cclauses
= cclauses_buf
;
36049 cp_lexer_consume_token (parser
->lexer
);
36050 if (!flag_openmp
) /* flag_openmp_simd */
36053 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36056 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36059 sb
= begin_omp_structured_block ();
36060 save
= cp_parser_begin_omp_structured_block (parser
);
36062 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36065 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36067 cp_parser_end_omp_structured_block (parser
, save
);
36068 tree body
= finish_omp_structured_block (sb
);
36071 ret
= make_node (OMP_DISTRIBUTE
);
36072 TREE_TYPE (ret
) = void_type_node
;
36073 OMP_FOR_BODY (ret
) = body
;
36074 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36075 SET_EXPR_LOCATION (ret
, loc
);
36080 if (!flag_openmp
) /* flag_openmp_simd */
36082 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36086 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36090 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36091 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36094 sb
= begin_omp_structured_block ();
36095 save
= cp_parser_begin_omp_structured_block (parser
);
36097 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36099 cp_parser_end_omp_structured_block (parser
, save
);
36100 add_stmt (finish_omp_structured_block (sb
));
36106 # pragma omp teams teams-clause[optseq] new-line
36107 structured-block */
36109 #define OMP_TEAMS_CLAUSE_MASK \
36110 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36119 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36120 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36123 tree clauses
, sb
, ret
;
36125 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36127 strcat (p_name
, " teams");
36128 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36132 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36133 const char *p
= IDENTIFIER_POINTER (id
);
36134 if (strcmp (p
, "distribute") == 0)
36136 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36137 if (cclauses
== NULL
)
36138 cclauses
= cclauses_buf
;
36140 cp_lexer_consume_token (parser
->lexer
);
36141 if (!flag_openmp
) /* flag_openmp_simd */
36142 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36144 sb
= begin_omp_structured_block ();
36145 save
= cp_parser_begin_omp_structured_block (parser
);
36146 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36148 cp_parser_end_omp_structured_block (parser
, save
);
36149 tree body
= finish_omp_structured_block (sb
);
36152 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36153 ret
= make_node (OMP_TEAMS
);
36154 TREE_TYPE (ret
) = void_type_node
;
36155 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36156 OMP_TEAMS_BODY (ret
) = body
;
36157 OMP_TEAMS_COMBINED (ret
) = 1;
36158 SET_EXPR_LOCATION (ret
, loc
);
36159 return add_stmt (ret
);
36162 if (!flag_openmp
) /* flag_openmp_simd */
36164 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36168 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36172 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36173 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36176 tree stmt
= make_node (OMP_TEAMS
);
36177 TREE_TYPE (stmt
) = void_type_node
;
36178 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36179 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36180 SET_EXPR_LOCATION (stmt
, loc
);
36182 return add_stmt (stmt
);
36186 # pragma omp target data target-data-clause[optseq] new-line
36187 structured-block */
36189 #define OMP_TARGET_DATA_CLAUSE_MASK \
36190 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36196 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36199 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36200 "#pragma omp target data", pragma_tok
);
36202 for (tree
*pc
= &clauses
; *pc
;)
36204 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36205 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36208 case GOMP_MAP_ALWAYS_TO
:
36209 case GOMP_MAP_FROM
:
36210 case GOMP_MAP_ALWAYS_FROM
:
36211 case GOMP_MAP_TOFROM
:
36212 case GOMP_MAP_ALWAYS_TOFROM
:
36213 case GOMP_MAP_ALLOC
:
36216 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36217 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36218 case GOMP_MAP_ALWAYS_POINTER
:
36222 error_at (OMP_CLAUSE_LOCATION (*pc
),
36223 "%<#pragma omp target data%> with map-type other "
36224 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36225 "on %<map%> clause");
36226 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36229 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36235 error_at (pragma_tok
->location
,
36236 "%<#pragma omp target data%> must contain at least "
36237 "one %<map%> clause");
36241 tree stmt
= make_node (OMP_TARGET_DATA
);
36242 TREE_TYPE (stmt
) = void_type_node
;
36243 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36245 keep_next_level (true);
36246 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36248 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36249 return add_stmt (stmt
);
36253 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36254 structured-block */
36256 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36257 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36264 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36265 enum pragma_context context
)
36267 bool data_seen
= false;
36268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36270 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36271 const char *p
= IDENTIFIER_POINTER (id
);
36273 if (strcmp (p
, "data") == 0)
36275 cp_lexer_consume_token (parser
->lexer
);
36281 cp_parser_error (parser
, "expected %<data%>");
36282 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36286 if (context
== pragma_stmt
)
36288 error_at (pragma_tok
->location
,
36289 "%<#pragma %s%> may only be used in compound statements",
36290 "omp target enter data");
36291 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36296 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36297 "#pragma omp target enter data", pragma_tok
);
36299 for (tree
*pc
= &clauses
; *pc
;)
36301 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36302 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36305 case GOMP_MAP_ALWAYS_TO
:
36306 case GOMP_MAP_ALLOC
:
36309 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36310 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36311 case GOMP_MAP_ALWAYS_POINTER
:
36315 error_at (OMP_CLAUSE_LOCATION (*pc
),
36316 "%<#pragma omp target enter data%> with map-type other "
36317 "than %<to%> or %<alloc%> on %<map%> clause");
36318 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36321 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36327 error_at (pragma_tok
->location
,
36328 "%<#pragma omp target enter data%> must contain at least "
36329 "one %<map%> clause");
36333 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36334 TREE_TYPE (stmt
) = void_type_node
;
36335 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36336 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36337 return add_stmt (stmt
);
36341 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36342 structured-block */
36344 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36352 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36353 enum pragma_context context
)
36355 bool data_seen
= false;
36356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36358 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36359 const char *p
= IDENTIFIER_POINTER (id
);
36361 if (strcmp (p
, "data") == 0)
36363 cp_lexer_consume_token (parser
->lexer
);
36369 cp_parser_error (parser
, "expected %<data%>");
36370 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36374 if (context
== pragma_stmt
)
36376 error_at (pragma_tok
->location
,
36377 "%<#pragma %s%> may only be used in compound statements",
36378 "omp target exit data");
36379 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36384 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36385 "#pragma omp target exit data", pragma_tok
);
36387 for (tree
*pc
= &clauses
; *pc
;)
36389 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36390 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36392 case GOMP_MAP_FROM
:
36393 case GOMP_MAP_ALWAYS_FROM
:
36394 case GOMP_MAP_RELEASE
:
36395 case GOMP_MAP_DELETE
:
36398 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36399 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36400 case GOMP_MAP_ALWAYS_POINTER
:
36404 error_at (OMP_CLAUSE_LOCATION (*pc
),
36405 "%<#pragma omp target exit data%> with map-type other "
36406 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36408 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36411 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36417 error_at (pragma_tok
->location
,
36418 "%<#pragma omp target exit data%> must contain at least "
36419 "one %<map%> clause");
36423 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36424 TREE_TYPE (stmt
) = void_type_node
;
36425 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36426 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36427 return add_stmt (stmt
);
36431 # pragma omp target update target-update-clause[optseq] new-line */
36433 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36434 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36442 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36443 enum pragma_context context
)
36445 if (context
== pragma_stmt
)
36447 error_at (pragma_tok
->location
,
36448 "%<#pragma %s%> may only be used in compound statements",
36449 "omp target update");
36450 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36455 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36456 "#pragma omp target update", pragma_tok
);
36457 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36458 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36460 error_at (pragma_tok
->location
,
36461 "%<#pragma omp target update%> must contain at least one "
36462 "%<from%> or %<to%> clauses");
36466 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36467 TREE_TYPE (stmt
) = void_type_node
;
36468 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36469 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36475 # pragma omp target target-clause[optseq] new-line
36476 structured-block */
36478 #define OMP_TARGET_CLAUSE_MASK \
36479 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36490 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36491 enum pragma_context context
, bool *if_p
)
36493 tree
*pc
= NULL
, stmt
;
36495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36497 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36498 const char *p
= IDENTIFIER_POINTER (id
);
36499 enum tree_code ccode
= ERROR_MARK
;
36501 if (strcmp (p
, "teams") == 0)
36503 else if (strcmp (p
, "parallel") == 0)
36504 ccode
= OMP_PARALLEL
;
36505 else if (strcmp (p
, "simd") == 0)
36507 if (ccode
!= ERROR_MARK
)
36509 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36510 char p_name
[sizeof ("#pragma omp target teams distribute "
36511 "parallel for simd")];
36513 cp_lexer_consume_token (parser
->lexer
);
36514 strcpy (p_name
, "#pragma omp target");
36515 if (!flag_openmp
) /* flag_openmp_simd */
36521 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36522 OMP_TARGET_CLAUSE_MASK
,
36526 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36527 OMP_TARGET_CLAUSE_MASK
,
36531 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36532 OMP_TARGET_CLAUSE_MASK
,
36536 gcc_unreachable ();
36538 return stmt
!= NULL_TREE
;
36540 keep_next_level (true);
36541 tree sb
= begin_omp_structured_block (), ret
;
36542 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36546 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36547 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36551 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36552 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36556 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36557 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36561 gcc_unreachable ();
36563 cp_parser_end_omp_structured_block (parser
, save
);
36564 tree body
= finish_omp_structured_block (sb
);
36565 if (ret
== NULL_TREE
)
36567 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36569 /* For combined target teams, ensure the num_teams and
36570 thread_limit clause expressions are evaluated on the host,
36571 before entering the target construct. */
36573 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36574 c
; c
= OMP_CLAUSE_CHAIN (c
))
36575 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36576 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36577 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36579 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36580 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36581 if (expr
== error_mark_node
)
36583 tree tmp
= TARGET_EXPR_SLOT (expr
);
36585 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36586 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36587 OMP_CLAUSE_FIRSTPRIVATE
);
36588 OMP_CLAUSE_DECL (tc
) = tmp
;
36589 OMP_CLAUSE_CHAIN (tc
)
36590 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36591 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36594 tree stmt
= make_node (OMP_TARGET
);
36595 TREE_TYPE (stmt
) = void_type_node
;
36596 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36597 OMP_TARGET_BODY (stmt
) = body
;
36598 OMP_TARGET_COMBINED (stmt
) = 1;
36599 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36601 pc
= &OMP_TARGET_CLAUSES (stmt
);
36602 goto check_clauses
;
36604 else if (!flag_openmp
) /* flag_openmp_simd */
36606 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36609 else if (strcmp (p
, "data") == 0)
36611 cp_lexer_consume_token (parser
->lexer
);
36612 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36615 else if (strcmp (p
, "enter") == 0)
36617 cp_lexer_consume_token (parser
->lexer
);
36618 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36621 else if (strcmp (p
, "exit") == 0)
36623 cp_lexer_consume_token (parser
->lexer
);
36624 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36627 else if (strcmp (p
, "update") == 0)
36629 cp_lexer_consume_token (parser
->lexer
);
36630 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36633 if (!flag_openmp
) /* flag_openmp_simd */
36635 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36639 stmt
= make_node (OMP_TARGET
);
36640 TREE_TYPE (stmt
) = void_type_node
;
36642 OMP_TARGET_CLAUSES (stmt
)
36643 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36644 "#pragma omp target", pragma_tok
);
36645 pc
= &OMP_TARGET_CLAUSES (stmt
);
36646 keep_next_level (true);
36647 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36649 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36655 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36656 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36659 case GOMP_MAP_ALWAYS_TO
:
36660 case GOMP_MAP_FROM
:
36661 case GOMP_MAP_ALWAYS_FROM
:
36662 case GOMP_MAP_TOFROM
:
36663 case GOMP_MAP_ALWAYS_TOFROM
:
36664 case GOMP_MAP_ALLOC
:
36665 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36666 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36667 case GOMP_MAP_ALWAYS_POINTER
:
36670 error_at (OMP_CLAUSE_LOCATION (*pc
),
36671 "%<#pragma omp target%> with map-type other "
36672 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36673 "on %<map%> clause");
36674 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36677 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36683 # pragma acc cache (variable-list) new-line
36687 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36689 tree stmt
, clauses
;
36691 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36692 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36694 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36696 stmt
= make_node (OACC_CACHE
);
36697 TREE_TYPE (stmt
) = void_type_node
;
36698 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36699 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36706 # pragma acc data oacc-data-clause[optseq] new-line
36707 structured-block */
36709 #define OACC_DATA_CLAUSE_MASK \
36710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36723 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36725 tree stmt
, clauses
, block
;
36728 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36729 "#pragma acc data", pragma_tok
);
36731 block
= begin_omp_parallel ();
36732 save
= cp_parser_begin_omp_structured_block (parser
);
36733 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36734 cp_parser_end_omp_structured_block (parser
, save
);
36735 stmt
= finish_oacc_data (clauses
, block
);
36740 # pragma acc host_data <clauses> new-line
36741 structured-block */
36743 #define OACC_HOST_DATA_CLAUSE_MASK \
36744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36747 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36749 tree stmt
, clauses
, block
;
36752 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36753 "#pragma acc host_data", pragma_tok
);
36755 block
= begin_omp_parallel ();
36756 save
= cp_parser_begin_omp_structured_block (parser
);
36757 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36758 cp_parser_end_omp_structured_block (parser
, save
);
36759 stmt
= finish_oacc_host_data (clauses
, block
);
36764 # pragma acc declare oacc-data-clause[optseq] new-line
36767 #define OACC_DECLARE_CLAUSE_MASK \
36768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36782 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36784 tree clauses
, stmt
;
36785 bool error
= false;
36787 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36788 "#pragma acc declare", pragma_tok
, true);
36791 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36793 error_at (pragma_tok
->location
,
36794 "no valid clauses specified in %<#pragma acc declare%>");
36798 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36800 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36801 tree decl
= OMP_CLAUSE_DECL (t
);
36802 if (!DECL_P (decl
))
36804 error_at (loc
, "array section in %<#pragma acc declare%>");
36808 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36809 switch (OMP_CLAUSE_MAP_KIND (t
))
36811 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36812 case GOMP_MAP_FORCE_ALLOC
:
36813 case GOMP_MAP_FORCE_TO
:
36814 case GOMP_MAP_FORCE_DEVICEPTR
:
36815 case GOMP_MAP_DEVICE_RESIDENT
:
36818 case GOMP_MAP_LINK
:
36819 if (!global_bindings_p ()
36820 && (TREE_STATIC (decl
)
36821 || !DECL_EXTERNAL (decl
)))
36824 "%qD must be a global variable in "
36825 "%<#pragma acc declare link%>",
36833 if (global_bindings_p ())
36835 error_at (loc
, "invalid OpenACC clause at file scope");
36839 if (DECL_EXTERNAL (decl
))
36842 "invalid use of %<extern%> variable %qD "
36843 "in %<#pragma acc declare%>", decl
);
36847 else if (TREE_PUBLIC (decl
))
36850 "invalid use of %<global%> variable %qD "
36851 "in %<#pragma acc declare%>", decl
);
36858 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36859 || lookup_attribute ("omp declare target link",
36860 DECL_ATTRIBUTES (decl
)))
36862 error_at (loc
, "variable %qD used more than once with "
36863 "%<#pragma acc declare%>", decl
);
36872 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36873 id
= get_identifier ("omp declare target link");
36875 id
= get_identifier ("omp declare target");
36877 DECL_ATTRIBUTES (decl
)
36878 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36879 if (global_bindings_p ())
36881 symtab_node
*node
= symtab_node::get (decl
);
36884 node
->offloadable
= 1;
36885 if (ENABLE_OFFLOADING
)
36887 g
->have_offload
= true;
36888 if (is_a
<varpool_node
*> (node
))
36889 vec_safe_push (offload_vars
, decl
);
36896 if (error
|| global_bindings_p ())
36899 stmt
= make_node (OACC_DECLARE
);
36900 TREE_TYPE (stmt
) = void_type_node
;
36901 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36902 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36910 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36914 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36916 LOC is the location of the #pragma token.
36919 #define OACC_ENTER_DATA_CLAUSE_MASK \
36920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36928 #define OACC_EXIT_DATA_CLAUSE_MASK \
36929 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36936 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36939 location_t loc
= pragma_tok
->location
;
36940 tree stmt
, clauses
;
36941 const char *p
= "";
36943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36944 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36946 if (strcmp (p
, "data") != 0)
36948 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36949 enter
? "enter" : "exit");
36950 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36954 cp_lexer_consume_token (parser
->lexer
);
36957 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36958 "#pragma acc enter data", pragma_tok
);
36960 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36961 "#pragma acc exit data", pragma_tok
);
36963 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36965 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36966 enter
? "enter" : "exit");
36970 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36971 TREE_TYPE (stmt
) = void_type_node
;
36972 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36973 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36979 # pragma acc loop oacc-loop-clause[optseq] new-line
36980 structured-block */
36982 #define OACC_LOOP_CLAUSE_MASK \
36983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36995 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36996 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36998 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37000 strcat (p_name
, " loop");
37001 mask
|= OACC_LOOP_CLAUSE_MASK
;
37003 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37007 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37009 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37011 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37014 tree block
= begin_omp_structured_block ();
37015 int save
= cp_parser_begin_omp_structured_block (parser
);
37016 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37017 cp_parser_end_omp_structured_block (parser
, save
);
37018 add_stmt (finish_omp_structured_block (block
));
37024 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37029 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37033 #define OACC_KERNELS_CLAUSE_MASK \
37034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37052 #define OACC_PARALLEL_CLAUSE_MASK \
37053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37075 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37076 char *p_name
, bool *if_p
)
37078 omp_clause_mask mask
;
37079 enum tree_code code
;
37080 switch (cp_parser_pragma_kind (pragma_tok
))
37082 case PRAGMA_OACC_KERNELS
:
37083 strcat (p_name
, " kernels");
37084 mask
= OACC_KERNELS_CLAUSE_MASK
;
37085 code
= OACC_KERNELS
;
37087 case PRAGMA_OACC_PARALLEL
:
37088 strcat (p_name
, " parallel");
37089 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37090 code
= OACC_PARALLEL
;
37093 gcc_unreachable ();
37096 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37099 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37100 if (strcmp (p
, "loop") == 0)
37102 cp_lexer_consume_token (parser
->lexer
);
37103 tree block
= begin_omp_parallel ();
37105 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37107 return finish_omp_construct (code
, block
, clauses
);
37111 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37113 tree block
= begin_omp_parallel ();
37114 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37115 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37116 cp_parser_end_omp_structured_block (parser
, save
);
37117 return finish_omp_construct (code
, block
, clauses
);
37121 # pragma acc update oacc-update-clause[optseq] new-line
37124 #define OACC_UPDATE_CLAUSE_MASK \
37125 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37133 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37135 tree stmt
, clauses
;
37137 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37138 "#pragma acc update", pragma_tok
);
37140 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37142 error_at (pragma_tok
->location
,
37143 "%<#pragma acc update%> must contain at least one "
37144 "%<device%> or %<host%> or %<self%> clause");
37148 stmt
= make_node (OACC_UPDATE
);
37149 TREE_TYPE (stmt
) = void_type_node
;
37150 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37151 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37157 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37159 LOC is the location of the #pragma token.
37162 #define OACC_WAIT_CLAUSE_MASK \
37163 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37166 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37168 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37169 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37171 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37172 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37174 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37175 "#pragma acc wait", pragma_tok
);
37177 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37178 stmt
= finish_expr_stmt (stmt
);
37184 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37186 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37187 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37195 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37196 enum pragma_context context
)
37198 bool first_p
= parser
->omp_declare_simd
== NULL
;
37199 cp_omp_declare_simd_data data
;
37202 data
.error_seen
= false;
37203 data
.fndecl_seen
= false;
37204 data
.tokens
= vNULL
;
37205 data
.clauses
= NULL_TREE
;
37206 /* It is safe to take the address of a local variable; it will only be
37207 used while this scope is live. */
37208 parser
->omp_declare_simd
= &data
;
37211 /* Store away all pragma tokens. */
37212 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37213 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37214 cp_lexer_consume_token (parser
->lexer
);
37215 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37216 parser
->omp_declare_simd
->error_seen
= true;
37217 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37218 struct cp_token_cache
*cp
37219 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37220 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37224 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37225 cp_parser_pragma (parser
, context
, NULL
);
37228 case pragma_external
:
37229 cp_parser_declaration (parser
);
37231 case pragma_member
:
37232 cp_parser_member_declaration (parser
);
37234 case pragma_objc_icode
:
37235 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37238 cp_parser_declaration_statement (parser
);
37241 if (parser
->omp_declare_simd
37242 && !parser
->omp_declare_simd
->error_seen
37243 && !parser
->omp_declare_simd
->fndecl_seen
)
37244 error_at (pragma_tok
->location
,
37245 "%<#pragma omp declare simd%> not immediately followed by "
37246 "function declaration or definition");
37247 data
.tokens
.release ();
37248 parser
->omp_declare_simd
= NULL
;
37252 /* Finalize #pragma omp declare simd clauses after direct declarator has
37253 been parsed, and put that into "omp declare simd" attribute. */
37256 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37258 struct cp_token_cache
*ce
;
37259 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37262 if (!data
->error_seen
&& data
->fndecl_seen
)
37264 error ("%<#pragma omp declare simd%> not immediately followed by "
37265 "a single function declaration or definition");
37266 data
->error_seen
= true;
37268 if (data
->error_seen
)
37271 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37275 cp_parser_push_lexer_for_tokens (parser
, ce
);
37276 parser
->lexer
->in_pragma
= true;
37277 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37278 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37279 cp_lexer_consume_token (parser
->lexer
);
37280 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37281 "#pragma omp declare simd", pragma_tok
);
37282 cp_parser_pop_lexer (parser
);
37284 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37285 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37286 TREE_CHAIN (c
) = attrs
;
37287 if (processing_template_decl
)
37288 ATTR_IS_DEPENDENT (c
) = 1;
37292 data
->fndecl_seen
= true;
37298 # pragma omp declare target new-line
37299 declarations and definitions
37300 # pragma omp end declare target new-line
37303 # pragma omp declare target ( extended-list ) new-line
37305 # pragma omp declare target declare-target-clauses[seq] new-line */
37307 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37308 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37312 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37314 tree clauses
= NULL_TREE
;
37315 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37317 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37318 "#pragma omp declare target", pragma_tok
);
37319 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37321 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37323 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37324 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37328 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37329 scope_chain
->omp_declare_target_attribute
++;
37332 if (scope_chain
->omp_declare_target_attribute
)
37333 error_at (pragma_tok
->location
,
37334 "%<#pragma omp declare target%> with clauses in between "
37335 "%<#pragma omp declare target%> without clauses and "
37336 "%<#pragma omp end declare target%>");
37337 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37339 tree t
= OMP_CLAUSE_DECL (c
), id
;
37340 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37341 tree at2
= lookup_attribute ("omp declare target link",
37342 DECL_ATTRIBUTES (t
));
37343 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37345 id
= get_identifier ("omp declare target link");
37346 std::swap (at1
, at2
);
37349 id
= get_identifier ("omp declare target");
37352 error_at (OMP_CLAUSE_LOCATION (c
),
37353 "%qD specified both in declare target %<link%> and %<to%>"
37359 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37360 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37363 symtab_node
*node
= symtab_node::get (t
);
37366 node
->offloadable
= 1;
37367 if (ENABLE_OFFLOADING
)
37369 g
->have_offload
= true;
37370 if (is_a
<varpool_node
*> (node
))
37371 vec_safe_push (offload_vars
, t
);
37379 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37381 const char *p
= "";
37382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37384 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37385 p
= IDENTIFIER_POINTER (id
);
37387 if (strcmp (p
, "declare") == 0)
37389 cp_lexer_consume_token (parser
->lexer
);
37391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37393 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37394 p
= IDENTIFIER_POINTER (id
);
37396 if (strcmp (p
, "target") == 0)
37397 cp_lexer_consume_token (parser
->lexer
);
37400 cp_parser_error (parser
, "expected %<target%>");
37401 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37407 cp_parser_error (parser
, "expected %<declare%>");
37408 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37411 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37412 if (!scope_chain
->omp_declare_target_attribute
)
37413 error_at (pragma_tok
->location
,
37414 "%<#pragma omp end declare target%> without corresponding "
37415 "%<#pragma omp declare target%>");
37417 scope_chain
->omp_declare_target_attribute
--;
37420 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37421 expression and optional initializer clause of
37422 #pragma omp declare reduction. We store the expression(s) as
37423 either 3, 6 or 7 special statements inside of the artificial function's
37424 body. The first two statements are DECL_EXPRs for the artificial
37425 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37426 expression that uses those variables.
37427 If there was any INITIALIZER clause, this is followed by further statements,
37428 the fourth and fifth statements are DECL_EXPRs for the artificial
37429 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37430 constructor variant (first token after open paren is not omp_priv),
37431 then the sixth statement is a statement with the function call expression
37432 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37433 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37434 to initialize the OMP_PRIV artificial variable and there is seventh
37435 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37438 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37440 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37441 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37442 type
= TREE_TYPE (type
);
37443 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37444 DECL_ARTIFICIAL (omp_out
) = 1;
37445 pushdecl (omp_out
);
37446 add_decl_expr (omp_out
);
37447 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37448 DECL_ARTIFICIAL (omp_in
) = 1;
37450 add_decl_expr (omp_in
);
37452 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37454 keep_next_level (true);
37455 tree block
= begin_omp_structured_block ();
37456 combiner
= cp_parser_expression (parser
);
37457 finish_expr_stmt (combiner
);
37458 block
= finish_omp_structured_block (block
);
37461 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37464 const char *p
= "";
37465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37467 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37468 p
= IDENTIFIER_POINTER (id
);
37471 if (strcmp (p
, "initializer") == 0)
37473 cp_lexer_consume_token (parser
->lexer
);
37474 matching_parens parens
;
37475 if (!parens
.require_open (parser
))
37479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37481 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37482 p
= IDENTIFIER_POINTER (id
);
37485 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37486 DECL_ARTIFICIAL (omp_priv
) = 1;
37487 pushdecl (omp_priv
);
37488 add_decl_expr (omp_priv
);
37489 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37490 DECL_ARTIFICIAL (omp_orig
) = 1;
37491 pushdecl (omp_orig
);
37492 add_decl_expr (omp_orig
);
37494 keep_next_level (true);
37495 block
= begin_omp_structured_block ();
37498 if (strcmp (p
, "omp_priv") == 0)
37500 bool is_direct_init
, is_non_constant_init
;
37502 cp_lexer_consume_token (parser
->lexer
);
37503 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37505 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37506 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37508 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37509 == CPP_CLOSE_PAREN
))
37511 finish_omp_structured_block (block
);
37512 error ("invalid initializer clause");
37515 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37516 &is_non_constant_init
);
37517 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37518 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37522 cp_parser_parse_tentatively (parser
);
37523 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37524 /*check_dependency_p=*/true,
37525 /*template_p=*/NULL
,
37526 /*declarator_p=*/false,
37527 /*optional_p=*/false);
37528 vec
<tree
, va_gc
> *args
;
37529 if (fn_name
== error_mark_node
37530 || cp_parser_error_occurred (parser
)
37531 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37532 || ((args
= cp_parser_parenthesized_expression_list
37533 (parser
, non_attr
, /*cast_p=*/false,
37534 /*allow_expansion_p=*/true,
37535 /*non_constant_p=*/NULL
)),
37536 cp_parser_error_occurred (parser
)))
37538 finish_omp_structured_block (block
);
37539 cp_parser_abort_tentative_parse (parser
);
37540 cp_parser_error (parser
, "expected id-expression (arguments)");
37545 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37546 if (arg
== omp_priv
37547 || (TREE_CODE (arg
) == ADDR_EXPR
37548 && TREE_OPERAND (arg
, 0) == omp_priv
))
37550 cp_parser_abort_tentative_parse (parser
);
37551 if (arg
== NULL_TREE
)
37552 error ("one of the initializer call arguments should be %<omp_priv%>"
37553 " or %<&omp_priv%>");
37554 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37556 finish_expr_stmt (initializer
);
37559 block
= finish_omp_structured_block (block
);
37560 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37564 add_decl_expr (omp_orig
);
37566 if (!parens
.require_close (parser
))
37570 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37571 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37578 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37579 initializer-clause[opt] new-line
37581 initializer-clause:
37582 initializer (omp_priv initializer)
37583 initializer (function-name (argument-list)) */
37586 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37587 enum pragma_context
)
37589 auto_vec
<tree
> types
;
37590 enum tree_code reduc_code
= ERROR_MARK
;
37591 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37593 cp_token
*first_token
;
37594 cp_token_cache
*cp
;
37598 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37599 p
= obstack_alloc (&declarator_obstack
, 0);
37601 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37604 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37607 reduc_code
= PLUS_EXPR
;
37610 reduc_code
= MULT_EXPR
;
37613 reduc_code
= MINUS_EXPR
;
37616 reduc_code
= BIT_AND_EXPR
;
37619 reduc_code
= BIT_XOR_EXPR
;
37622 reduc_code
= BIT_IOR_EXPR
;
37625 reduc_code
= TRUTH_ANDIF_EXPR
;
37628 reduc_code
= TRUTH_ORIF_EXPR
;
37631 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37634 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37635 "%<|%>, %<&&%>, %<||%> or identifier");
37639 if (reduc_code
!= ERROR_MARK
)
37640 cp_lexer_consume_token (parser
->lexer
);
37642 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37643 if (reduc_id
== error_mark_node
)
37646 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37649 /* Types may not be defined in declare reduction type list. */
37650 const char *saved_message
;
37651 saved_message
= parser
->type_definition_forbidden_message
;
37652 parser
->type_definition_forbidden_message
37653 = G_("types may not be defined in declare reduction type list");
37654 bool saved_colon_corrects_to_scope_p
;
37655 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37656 parser
->colon_corrects_to_scope_p
= false;
37657 bool saved_colon_doesnt_start_class_def_p
;
37658 saved_colon_doesnt_start_class_def_p
37659 = parser
->colon_doesnt_start_class_def_p
;
37660 parser
->colon_doesnt_start_class_def_p
= true;
37664 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37665 type
= cp_parser_type_id (parser
);
37666 if (type
== error_mark_node
)
37668 else if (ARITHMETIC_TYPE_P (type
)
37669 && (orig_reduc_id
== NULL_TREE
37670 || (TREE_CODE (type
) != COMPLEX_TYPE
37671 && (id_equal (orig_reduc_id
, "min")
37672 || id_equal (orig_reduc_id
, "max")))))
37673 error_at (loc
, "predeclared arithmetic type %qT in "
37674 "%<#pragma omp declare reduction%>", type
);
37675 else if (TREE_CODE (type
) == FUNCTION_TYPE
37676 || TREE_CODE (type
) == METHOD_TYPE
37677 || TREE_CODE (type
) == ARRAY_TYPE
)
37678 error_at (loc
, "function or array type %qT in "
37679 "%<#pragma omp declare reduction%>", type
);
37680 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37681 error_at (loc
, "reference type %qT in "
37682 "%<#pragma omp declare reduction%>", type
);
37683 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37684 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37685 "%<#pragma omp declare reduction%>", type
);
37687 types
.safe_push (type
);
37689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37690 cp_lexer_consume_token (parser
->lexer
);
37695 /* Restore the saved message. */
37696 parser
->type_definition_forbidden_message
= saved_message
;
37697 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37698 parser
->colon_doesnt_start_class_def_p
37699 = saved_colon_doesnt_start_class_def_p
;
37701 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37702 || types
.is_empty ())
37705 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37709 first_token
= cp_lexer_peek_token (parser
->lexer
);
37712 FOR_EACH_VEC_ELT (types
, i
, type
)
37715 = build_function_type_list (void_type_node
,
37716 cp_build_reference_type (type
, false),
37718 tree this_reduc_id
= reduc_id
;
37719 if (!dependent_type_p (type
))
37720 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37721 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37722 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37723 DECL_ARTIFICIAL (fndecl
) = 1;
37724 DECL_EXTERNAL (fndecl
) = 1;
37725 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37726 DECL_IGNORED_P (fndecl
) = 1;
37727 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37728 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37729 DECL_ATTRIBUTES (fndecl
)
37730 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37731 DECL_ATTRIBUTES (fndecl
));
37732 if (processing_template_decl
)
37733 fndecl
= push_template_decl (fndecl
);
37734 bool block_scope
= false;
37735 tree block
= NULL_TREE
;
37736 if (current_function_decl
)
37738 block_scope
= true;
37739 DECL_CONTEXT (fndecl
) = global_namespace
;
37740 if (!processing_template_decl
)
37743 else if (current_class_type
)
37747 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37748 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37749 cp_lexer_consume_token (parser
->lexer
);
37750 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37752 cp
= cp_token_cache_new (first_token
,
37753 cp_lexer_peek_nth_token (parser
->lexer
,
37756 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37757 finish_member_declaration (fndecl
);
37758 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37759 DECL_PENDING_INLINE_P (fndecl
) = 1;
37760 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37765 DECL_CONTEXT (fndecl
) = current_namespace
;
37769 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37771 block
= begin_omp_structured_block ();
37774 cp_parser_push_lexer_for_tokens (parser
, cp
);
37775 parser
->lexer
->in_pragma
= true;
37777 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37780 finish_function (/*inline_p=*/false);
37782 DECL_CONTEXT (fndecl
) = current_function_decl
;
37784 cp_parser_pop_lexer (parser
);
37788 cp_parser_pop_lexer (parser
);
37790 finish_function (/*inline_p=*/false);
37793 DECL_CONTEXT (fndecl
) = current_function_decl
;
37794 block
= finish_omp_structured_block (block
);
37795 if (TREE_CODE (block
) == BIND_EXPR
)
37796 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37797 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37798 DECL_SAVED_TREE (fndecl
) = block
;
37799 if (processing_template_decl
)
37800 add_decl_expr (fndecl
);
37802 cp_check_omp_declare_reduction (fndecl
);
37803 if (cp
== NULL
&& types
.length () > 1)
37804 cp
= cp_token_cache_new (first_token
,
37805 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37806 if (errs
!= errorcount
)
37810 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37813 /* Free any declarators allocated. */
37814 obstack_free (&declarator_obstack
, p
);
37818 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37819 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37820 initializer-clause[opt] new-line
37821 #pragma omp declare target new-line */
37824 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37825 enum pragma_context context
)
37827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37829 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37830 const char *p
= IDENTIFIER_POINTER (id
);
37832 if (strcmp (p
, "simd") == 0)
37834 cp_lexer_consume_token (parser
->lexer
);
37835 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37839 cp_ensure_no_omp_declare_simd (parser
);
37840 if (strcmp (p
, "reduction") == 0)
37842 cp_lexer_consume_token (parser
->lexer
);
37843 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37847 if (!flag_openmp
) /* flag_openmp_simd */
37849 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37852 if (strcmp (p
, "target") == 0)
37854 cp_lexer_consume_token (parser
->lexer
);
37855 cp_parser_omp_declare_target (parser
, pragma_tok
);
37859 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37861 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37866 #pragma omp taskloop taskloop-clause[optseq] new-line
37869 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37872 #define OMP_TASKLOOP_CLAUSE_MASK \
37873 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37889 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37890 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37893 tree clauses
, sb
, ret
;
37895 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37897 strcat (p_name
, " taskloop");
37898 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37902 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37903 const char *p
= IDENTIFIER_POINTER (id
);
37905 if (strcmp (p
, "simd") == 0)
37907 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37908 if (cclauses
== NULL
)
37909 cclauses
= cclauses_buf
;
37911 cp_lexer_consume_token (parser
->lexer
);
37912 if (!flag_openmp
) /* flag_openmp_simd */
37913 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37915 sb
= begin_omp_structured_block ();
37916 save
= cp_parser_begin_omp_structured_block (parser
);
37917 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37919 cp_parser_end_omp_structured_block (parser
, save
);
37920 tree body
= finish_omp_structured_block (sb
);
37923 ret
= make_node (OMP_TASKLOOP
);
37924 TREE_TYPE (ret
) = void_type_node
;
37925 OMP_FOR_BODY (ret
) = body
;
37926 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37927 SET_EXPR_LOCATION (ret
, loc
);
37932 if (!flag_openmp
) /* flag_openmp_simd */
37934 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37938 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37942 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37943 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37946 sb
= begin_omp_structured_block ();
37947 save
= cp_parser_begin_omp_structured_block (parser
);
37949 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37952 cp_parser_end_omp_structured_block (parser
, save
);
37953 add_stmt (finish_omp_structured_block (sb
));
37960 # pragma acc routine oacc-routine-clause[optseq] new-line
37961 function-definition
37963 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37966 #define OACC_ROUTINE_CLAUSE_MASK \
37967 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37973 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37974 component, which must resolve to a declared namespace-scope
37975 function. The clauses are either processed directly (for a named
37976 function), or defered until the immediatley following declaration
37980 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37981 enum pragma_context context
)
37983 gcc_checking_assert (context
== pragma_external
);
37984 /* The checking for "another pragma following this one" in the "no optional
37985 '( name )'" case makes sure that we dont re-enter. */
37986 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37988 cp_oacc_routine_data data
;
37989 data
.error_seen
= false;
37990 data
.fndecl_seen
= false;
37991 data
.tokens
= vNULL
;
37992 data
.clauses
= NULL_TREE
;
37993 data
.loc
= pragma_tok
->location
;
37994 /* It is safe to take the address of a local variable; it will only be
37995 used while this scope is live. */
37996 parser
->oacc_routine
= &data
;
37998 /* Look for optional '( name )'. */
37999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38001 matching_parens parens
;
38002 parens
.consume_open (parser
); /* '(' */
38004 /* We parse the name as an id-expression. If it resolves to
38005 anything other than a non-overloaded function at namespace
38006 scope, it's an error. */
38007 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38008 tree name
= cp_parser_id_expression (parser
,
38009 /*template_keyword_p=*/false,
38010 /*check_dependency_p=*/false,
38011 /*template_p=*/NULL
,
38012 /*declarator_p=*/false,
38013 /*optional_p=*/false);
38014 tree decl
= (identifier_p (name
)
38015 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38017 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38018 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38020 if (decl
== error_mark_node
38021 || !parens
.require_close (parser
))
38023 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38024 parser
->oacc_routine
= NULL
;
38029 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38030 "#pragma acc routine",
38031 cp_lexer_peek_token (parser
->lexer
));
38033 if (decl
&& is_overloaded_fn (decl
)
38034 && (TREE_CODE (decl
) != FUNCTION_DECL
38035 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38037 error_at (name_loc
,
38038 "%<#pragma acc routine%> names a set of overloads");
38039 parser
->oacc_routine
= NULL
;
38043 /* Perhaps we should use the same rule as declarations in different
38045 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38047 error_at (name_loc
,
38048 "%qD does not refer to a namespace scope function", decl
);
38049 parser
->oacc_routine
= NULL
;
38053 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38055 error_at (name_loc
, "%qD does not refer to a function", decl
);
38056 parser
->oacc_routine
= NULL
;
38060 cp_finalize_oacc_routine (parser
, decl
, false);
38061 parser
->oacc_routine
= NULL
;
38063 else /* No optional '( name )'. */
38065 /* Store away all pragma tokens. */
38066 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38067 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38068 cp_lexer_consume_token (parser
->lexer
);
38069 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38070 parser
->oacc_routine
->error_seen
= true;
38071 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38072 struct cp_token_cache
*cp
38073 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38074 parser
->oacc_routine
->tokens
.safe_push (cp
);
38076 /* Emit a helpful diagnostic if there's another pragma following this
38078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38080 cp_ensure_no_oacc_routine (parser
);
38081 data
.tokens
.release ();
38082 /* ..., and then just keep going. */
38086 /* We only have to consider the pragma_external case here. */
38087 cp_parser_declaration (parser
);
38088 if (parser
->oacc_routine
38089 && !parser
->oacc_routine
->fndecl_seen
)
38090 cp_ensure_no_oacc_routine (parser
);
38092 parser
->oacc_routine
= NULL
;
38093 data
.tokens
.release ();
38097 /* Finalize #pragma acc routine clauses after direct declarator has
38101 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38103 struct cp_token_cache
*ce
;
38104 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38106 if (!data
->error_seen
&& data
->fndecl_seen
)
38108 error_at (data
->loc
,
38109 "%<#pragma acc routine%> not immediately followed by "
38110 "a single function declaration or definition");
38111 data
->error_seen
= true;
38113 if (data
->error_seen
)
38116 gcc_checking_assert (data
->tokens
.length () == 1);
38117 ce
= data
->tokens
[0];
38119 cp_parser_push_lexer_for_tokens (parser
, ce
);
38120 parser
->lexer
->in_pragma
= true;
38121 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38123 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38124 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38125 parser
->oacc_routine
->clauses
38126 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38127 "#pragma acc routine", pragma_tok
);
38128 cp_parser_pop_lexer (parser
);
38129 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38135 /* Apply any saved OpenACC routine clauses to a just-parsed
38139 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38141 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38143 /* Keep going if we're in error reporting mode. */
38144 if (parser
->oacc_routine
->error_seen
38145 || fndecl
== error_mark_node
)
38148 if (parser
->oacc_routine
->fndecl_seen
)
38150 error_at (parser
->oacc_routine
->loc
,
38151 "%<#pragma acc routine%> not immediately followed by"
38152 " a single function declaration or definition");
38153 parser
->oacc_routine
= NULL
;
38156 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38158 cp_ensure_no_oacc_routine (parser
);
38162 if (oacc_get_fn_attrib (fndecl
))
38164 error_at (parser
->oacc_routine
->loc
,
38165 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38166 parser
->oacc_routine
= NULL
;
38170 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38172 error_at (parser
->oacc_routine
->loc
,
38174 ? G_("%<#pragma acc routine%> must be applied before use")
38175 : G_("%<#pragma acc routine%> must be applied before "
38177 parser
->oacc_routine
= NULL
;
38181 /* Process the routine's dimension clauses. */
38182 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38183 oacc_replace_fn_attrib (fndecl
, dims
);
38185 /* Add an "omp declare target" attribute. */
38186 DECL_ATTRIBUTES (fndecl
)
38187 = tree_cons (get_identifier ("omp declare target"),
38188 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38190 /* Don't unset parser->oacc_routine here: we may still need it to
38191 diagnose wrong usage. But, remember that we've used this "#pragma acc
38193 parser
->oacc_routine
->fndecl_seen
= true;
38197 /* Main entry point to OpenMP statement pragmas. */
38200 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38203 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38204 omp_clause_mask
mask (0);
38206 switch (cp_parser_pragma_kind (pragma_tok
))
38208 case PRAGMA_OACC_ATOMIC
:
38209 cp_parser_omp_atomic (parser
, pragma_tok
);
38211 case PRAGMA_OACC_CACHE
:
38212 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38214 case PRAGMA_OACC_DATA
:
38215 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38217 case PRAGMA_OACC_ENTER_DATA
:
38218 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38220 case PRAGMA_OACC_EXIT_DATA
:
38221 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38223 case PRAGMA_OACC_HOST_DATA
:
38224 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38226 case PRAGMA_OACC_KERNELS
:
38227 case PRAGMA_OACC_PARALLEL
:
38228 strcpy (p_name
, "#pragma acc");
38229 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38232 case PRAGMA_OACC_LOOP
:
38233 strcpy (p_name
, "#pragma acc");
38234 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38237 case PRAGMA_OACC_UPDATE
:
38238 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38240 case PRAGMA_OACC_WAIT
:
38241 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38243 case PRAGMA_OMP_ATOMIC
:
38244 cp_parser_omp_atomic (parser
, pragma_tok
);
38246 case PRAGMA_OMP_CRITICAL
:
38247 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38249 case PRAGMA_OMP_DISTRIBUTE
:
38250 strcpy (p_name
, "#pragma omp");
38251 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38254 case PRAGMA_OMP_FOR
:
38255 strcpy (p_name
, "#pragma omp");
38256 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38259 case PRAGMA_OMP_MASTER
:
38260 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38262 case PRAGMA_OMP_PARALLEL
:
38263 strcpy (p_name
, "#pragma omp");
38264 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38267 case PRAGMA_OMP_SECTIONS
:
38268 strcpy (p_name
, "#pragma omp");
38269 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38271 case PRAGMA_OMP_SIMD
:
38272 strcpy (p_name
, "#pragma omp");
38273 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38276 case PRAGMA_OMP_SINGLE
:
38277 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38279 case PRAGMA_OMP_TASK
:
38280 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38282 case PRAGMA_OMP_TASKGROUP
:
38283 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38285 case PRAGMA_OMP_TASKLOOP
:
38286 strcpy (p_name
, "#pragma omp");
38287 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38290 case PRAGMA_OMP_TEAMS
:
38291 strcpy (p_name
, "#pragma omp");
38292 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38296 gcc_unreachable ();
38299 protected_set_expr_location (stmt
, pragma_tok
->location
);
38302 /* Transactional Memory parsing routines. */
38304 /* Parse a transaction attribute.
38310 We use this instead of cp_parser_attributes_opt for transactions to avoid
38311 the pedwarn in C++98 mode. */
38314 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38317 tree attr_name
, attr
= NULL
;
38319 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38320 return cp_parser_attributes_opt (parser
);
38322 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38324 cp_lexer_consume_token (parser
->lexer
);
38325 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38328 token
= cp_lexer_peek_token (parser
->lexer
);
38329 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38331 token
= cp_lexer_consume_token (parser
->lexer
);
38333 attr_name
= (token
->type
== CPP_KEYWORD
38334 /* For keywords, use the canonical spelling,
38335 not the parsed identifier. */
38336 ? ridpointers
[(int) token
->keyword
]
38338 attr
= build_tree_list (attr_name
, NULL_TREE
);
38341 cp_parser_error (parser
, "expected identifier");
38343 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38345 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38349 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38351 transaction-statement:
38352 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38354 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38358 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38360 unsigned char old_in
= parser
->in_transaction
;
38361 unsigned char this_in
= 1, new_in
;
38362 enum rid keyword
= token
->keyword
;
38363 tree stmt
, attrs
, noex
;
38365 cp_lexer_consume_token (parser
->lexer
);
38367 if (keyword
== RID_TRANSACTION_RELAXED
38368 || keyword
== RID_SYNCHRONIZED
)
38369 this_in
|= TM_STMT_ATTR_RELAXED
;
38372 attrs
= cp_parser_txn_attribute_opt (parser
);
38374 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38377 /* Parse a noexcept specification. */
38378 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38379 noex
= boolean_true_node
;
38380 else if (keyword
== RID_ATOMIC_CANCEL
)
38382 /* cancel-and-throw is unimplemented. */
38383 sorry ("atomic_cancel");
38387 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38389 /* Keep track if we're in the lexical scope of an outer transaction. */
38390 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38392 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38394 parser
->in_transaction
= new_in
;
38395 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38396 parser
->in_transaction
= old_in
;
38398 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38403 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38405 transaction-expression:
38406 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38407 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38411 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38413 unsigned char old_in
= parser
->in_transaction
;
38414 unsigned char this_in
= 1;
38418 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38420 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38421 || keyword
== RID_TRANSACTION_RELAXED
);
38425 keyword
== RID_TRANSACTION_RELAXED
38426 ? G_("%<__transaction_relaxed%> without transactional memory "
38428 : G_("%<__transaction_atomic%> without transactional memory "
38429 "support enabled"));
38431 token
= cp_parser_require_keyword (parser
, keyword
,
38432 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38433 : RT_TRANSACTION_RELAXED
));
38434 gcc_assert (token
!= NULL
);
38436 if (keyword
== RID_TRANSACTION_RELAXED
)
38437 this_in
|= TM_STMT_ATTR_RELAXED
;
38439 /* Set this early. This might mean that we allow transaction_cancel in
38440 an expression that we find out later actually has to be a constexpr.
38441 However, we expect that cxx_constant_value will be able to deal with
38442 this; also, if the noexcept has no constexpr, then what we parse next
38443 really is a transaction's body. */
38444 parser
->in_transaction
= this_in
;
38446 /* Parse a noexcept specification. */
38447 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38450 if (!noex
|| !noex_expr
38451 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38453 matching_parens parens
;
38454 parens
.require_open (parser
);
38456 expr
= cp_parser_expression (parser
);
38457 expr
= finish_parenthesized_expr (expr
);
38459 parens
.require_close (parser
);
38463 /* The only expression that is available got parsed for the noexcept
38464 already. noexcept is true then. */
38466 noex
= boolean_true_node
;
38469 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38470 parser
->in_transaction
= old_in
;
38472 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38473 return error_mark_node
;
38475 return (flag_tm
? expr
: error_mark_node
);
38478 /* Parse a function-transaction-block.
38480 function-transaction-block:
38481 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38483 __transaction_atomic txn-attribute[opt] function-try-block
38484 __transaction_relaxed ctor-initializer[opt] function-body
38485 __transaction_relaxed function-try-block
38489 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38491 unsigned char old_in
= parser
->in_transaction
;
38492 unsigned char new_in
= 1;
38493 tree compound_stmt
, stmt
, attrs
;
38496 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38497 || keyword
== RID_TRANSACTION_RELAXED
);
38498 token
= cp_parser_require_keyword (parser
, keyword
,
38499 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38500 : RT_TRANSACTION_RELAXED
));
38501 gcc_assert (token
!= NULL
);
38503 if (keyword
== RID_TRANSACTION_RELAXED
)
38504 new_in
|= TM_STMT_ATTR_RELAXED
;
38507 attrs
= cp_parser_txn_attribute_opt (parser
);
38509 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38512 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38514 parser
->in_transaction
= new_in
;
38516 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38517 cp_parser_function_try_block (parser
);
38519 cp_parser_ctor_initializer_opt_and_function_body
38520 (parser
, /*in_function_try_block=*/false);
38522 parser
->in_transaction
= old_in
;
38524 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38527 /* Parse a __transaction_cancel statement.
38530 __transaction_cancel txn-attribute[opt] ;
38531 __transaction_cancel txn-attribute[opt] throw-expression ;
38533 ??? Cancel and throw is not yet implemented. */
38536 cp_parser_transaction_cancel (cp_parser
*parser
)
38539 bool is_outer
= false;
38542 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38543 RT_TRANSACTION_CANCEL
);
38544 gcc_assert (token
!= NULL
);
38546 attrs
= cp_parser_txn_attribute_opt (parser
);
38548 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38550 /* ??? Parse cancel-and-throw here. */
38552 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38556 error_at (token
->location
, "%<__transaction_cancel%> without "
38557 "transactional memory support enabled");
38558 return error_mark_node
;
38560 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38562 error_at (token
->location
, "%<__transaction_cancel%> within a "
38563 "%<__transaction_relaxed%>");
38564 return error_mark_node
;
38568 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38569 && !is_tm_may_cancel_outer (current_function_decl
))
38571 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38572 "within outer %<__transaction_atomic%>");
38573 error_at (token
->location
,
38574 " or a %<transaction_may_cancel_outer%> function");
38575 return error_mark_node
;
38578 else if (parser
->in_transaction
== 0)
38580 error_at (token
->location
, "%<__transaction_cancel%> not within "
38581 "%<__transaction_atomic%>");
38582 return error_mark_node
;
38585 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38593 static GTY (()) cp_parser
*the_parser
;
38596 /* Special handling for the first token or line in the file. The first
38597 thing in the file might be #pragma GCC pch_preprocess, which loads a
38598 PCH file, which is a GC collection point. So we need to handle this
38599 first pragma without benefit of an existing lexer structure.
38601 Always returns one token to the caller in *FIRST_TOKEN. This is
38602 either the true first token of the file, or the first token after
38603 the initial pragma. */
38606 cp_parser_initial_pragma (cp_token
*first_token
)
38610 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38611 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38614 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38615 if (first_token
->type
== CPP_STRING
)
38617 name
= first_token
->u
.value
;
38619 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38620 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38621 error_at (first_token
->location
,
38622 "junk at end of %<#pragma GCC pch_preprocess%>");
38625 error_at (first_token
->location
, "expected string literal");
38627 /* Skip to the end of the pragma. */
38628 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38629 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38631 /* Now actually load the PCH file. */
38633 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38635 /* Read one more token to return to our caller. We have to do this
38636 after reading the PCH file in, since its pointers have to be
38638 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38641 /* Parse a pragma GCC ivdep. */
38644 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38646 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38650 /* Parse a pragma GCC unroll. */
38652 static unsigned short
38653 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38655 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38656 tree expr
= cp_parser_constant_expression (parser
);
38657 unsigned short unroll
;
38658 expr
= maybe_constant_value (expr
);
38659 HOST_WIDE_INT lunroll
= 0;
38660 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38661 || TREE_CODE (expr
) != INTEGER_CST
38662 || (lunroll
= tree_to_shwi (expr
)) < 0
38663 || lunroll
>= USHRT_MAX
)
38665 error_at (location
, "%<#pragma GCC unroll%> requires an"
38666 " assignment-expression that evaluates to a non-negative"
38667 " integral constant less than %u", USHRT_MAX
);
38672 unroll
= (unsigned short)lunroll
;
38676 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38680 /* Normal parsing of a pragma token. Here we can (and must) use the
38684 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38686 cp_token
*pragma_tok
;
38691 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38692 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38693 parser
->lexer
->in_pragma
= true;
38695 id
= cp_parser_pragma_kind (pragma_tok
);
38696 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38697 cp_ensure_no_omp_declare_simd (parser
);
38700 case PRAGMA_GCC_PCH_PREPROCESS
:
38701 error_at (pragma_tok
->location
,
38702 "%<#pragma GCC pch_preprocess%> must be first");
38705 case PRAGMA_OMP_BARRIER
:
38708 case pragma_compound
:
38709 cp_parser_omp_barrier (parser
, pragma_tok
);
38712 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38713 "used in compound statements", "omp barrier");
38720 case PRAGMA_OMP_FLUSH
:
38723 case pragma_compound
:
38724 cp_parser_omp_flush (parser
, pragma_tok
);
38727 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38728 "used in compound statements", "omp flush");
38735 case PRAGMA_OMP_TASKWAIT
:
38738 case pragma_compound
:
38739 cp_parser_omp_taskwait (parser
, pragma_tok
);
38742 error_at (pragma_tok
->location
,
38743 "%<#pragma %s%> may only be used in compound statements",
38751 case PRAGMA_OMP_TASKYIELD
:
38754 case pragma_compound
:
38755 cp_parser_omp_taskyield (parser
, pragma_tok
);
38758 error_at (pragma_tok
->location
,
38759 "%<#pragma %s%> may only be used in compound statements",
38767 case PRAGMA_OMP_CANCEL
:
38770 case pragma_compound
:
38771 cp_parser_omp_cancel (parser
, pragma_tok
);
38774 error_at (pragma_tok
->location
,
38775 "%<#pragma %s%> may only be used in compound statements",
38783 case PRAGMA_OMP_CANCELLATION_POINT
:
38784 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38787 case PRAGMA_OMP_THREADPRIVATE
:
38788 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38791 case PRAGMA_OMP_DECLARE
:
38792 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38794 case PRAGMA_OACC_DECLARE
:
38795 cp_parser_oacc_declare (parser
, pragma_tok
);
38798 case PRAGMA_OACC_ENTER_DATA
:
38799 if (context
== pragma_stmt
)
38801 error_at (pragma_tok
->location
,
38802 "%<#pragma %s%> may only be used in compound statements",
38806 else if (context
!= pragma_compound
)
38808 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38811 case PRAGMA_OACC_EXIT_DATA
:
38812 if (context
== pragma_stmt
)
38814 error_at (pragma_tok
->location
,
38815 "%<#pragma %s%> may only be used in compound statements",
38819 else if (context
!= pragma_compound
)
38821 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38824 case PRAGMA_OACC_ROUTINE
:
38825 if (context
!= pragma_external
)
38827 error_at (pragma_tok
->location
,
38828 "%<#pragma acc routine%> must be at file scope");
38831 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38834 case PRAGMA_OACC_UPDATE
:
38835 if (context
== pragma_stmt
)
38837 error_at (pragma_tok
->location
,
38838 "%<#pragma %s%> may only be used in compound statements",
38842 else if (context
!= pragma_compound
)
38844 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38847 case PRAGMA_OACC_WAIT
:
38848 if (context
== pragma_stmt
)
38850 error_at (pragma_tok
->location
,
38851 "%<#pragma %s%> may only be used in compound statements",
38855 else if (context
!= pragma_compound
)
38857 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38860 case PRAGMA_OACC_ATOMIC
:
38861 case PRAGMA_OACC_CACHE
:
38862 case PRAGMA_OACC_DATA
:
38863 case PRAGMA_OACC_HOST_DATA
:
38864 case PRAGMA_OACC_KERNELS
:
38865 case PRAGMA_OACC_PARALLEL
:
38866 case PRAGMA_OACC_LOOP
:
38867 case PRAGMA_OMP_ATOMIC
:
38868 case PRAGMA_OMP_CRITICAL
:
38869 case PRAGMA_OMP_DISTRIBUTE
:
38870 case PRAGMA_OMP_FOR
:
38871 case PRAGMA_OMP_MASTER
:
38872 case PRAGMA_OMP_PARALLEL
:
38873 case PRAGMA_OMP_SECTIONS
:
38874 case PRAGMA_OMP_SIMD
:
38875 case PRAGMA_OMP_SINGLE
:
38876 case PRAGMA_OMP_TASK
:
38877 case PRAGMA_OMP_TASKGROUP
:
38878 case PRAGMA_OMP_TASKLOOP
:
38879 case PRAGMA_OMP_TEAMS
:
38880 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38882 stmt
= push_omp_privatization_clauses (false);
38883 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38884 pop_omp_privatization_clauses (stmt
);
38887 case PRAGMA_OMP_ORDERED
:
38888 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38890 stmt
= push_omp_privatization_clauses (false);
38891 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38892 pop_omp_privatization_clauses (stmt
);
38895 case PRAGMA_OMP_TARGET
:
38896 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38898 stmt
= push_omp_privatization_clauses (false);
38899 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38900 pop_omp_privatization_clauses (stmt
);
38903 case PRAGMA_OMP_END_DECLARE_TARGET
:
38904 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38907 case PRAGMA_OMP_SECTION
:
38908 error_at (pragma_tok
->location
,
38909 "%<#pragma omp section%> may only be used in "
38910 "%<#pragma omp sections%> construct");
38915 if (context
== pragma_external
)
38917 error_at (pragma_tok
->location
,
38918 "%<#pragma GCC ivdep%> must be inside a function");
38921 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38922 unsigned short unroll
;
38923 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38924 if (tok
->type
== CPP_PRAGMA
38925 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38927 tok
= cp_lexer_consume_token (parser
->lexer
);
38928 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38929 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38933 if (tok
->type
!= CPP_KEYWORD
38934 || (tok
->keyword
!= RID_FOR
38935 && tok
->keyword
!= RID_WHILE
38936 && tok
->keyword
!= RID_DO
))
38938 cp_parser_error (parser
, "for, while or do statement expected");
38941 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38945 case PRAGMA_UNROLL
:
38947 if (context
== pragma_external
)
38949 error_at (pragma_tok
->location
,
38950 "%<#pragma GCC unroll%> must be inside a function");
38953 const unsigned short unroll
38954 = cp_parser_pragma_unroll (parser
, pragma_tok
);
38956 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38957 if (tok
->type
== CPP_PRAGMA
38958 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
38960 tok
= cp_lexer_consume_token (parser
->lexer
);
38961 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
38962 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38966 if (tok
->type
!= CPP_KEYWORD
38967 || (tok
->keyword
!= RID_FOR
38968 && tok
->keyword
!= RID_WHILE
38969 && tok
->keyword
!= RID_DO
))
38971 cp_parser_error (parser
, "for, while or do statement expected");
38974 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38979 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38980 c_invoke_pragma_handler (id
);
38984 cp_parser_error (parser
, "expected declaration specifiers");
38988 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38992 /* The interface the pragma parsers have to the lexer. */
38995 pragma_lex (tree
*value
, location_t
*loc
)
38997 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38998 enum cpp_ttype ret
= tok
->type
;
39000 *value
= tok
->u
.value
;
39002 *loc
= tok
->location
;
39004 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39006 else if (ret
== CPP_STRING
)
39007 *value
= cp_parser_string_literal (the_parser
, false, false);
39010 if (ret
== CPP_KEYWORD
)
39012 cp_lexer_consume_token (the_parser
->lexer
);
39019 /* External interface. */
39021 /* Parse one entire translation unit. */
39024 c_parse_file (void)
39026 static bool already_called
= false;
39028 if (already_called
)
39029 fatal_error (input_location
,
39030 "inter-module optimizations not implemented for C++");
39031 already_called
= true;
39033 the_parser
= cp_parser_new ();
39034 push_deferring_access_checks (flag_access_control
39035 ? dk_no_deferred
: dk_no_check
);
39036 cp_parser_translation_unit (the_parser
);
39040 /* Create an identifier for a generic parameter type (a synthesized
39041 template parameter implied by `auto' or a concept identifier). */
39043 static GTY(()) int generic_parm_count
;
39045 make_generic_type_name ()
39048 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39049 return get_identifier (buf
);
39052 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39053 (creating a new template parameter list if necessary). Returns the newly
39054 created template type parm. */
39057 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39059 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39061 /* Before committing to modifying any scope, if we're in an
39062 implicit template scope, and we're trying to synthesize a
39063 constrained parameter, try to find a previous parameter with
39064 the same name. This is the same-type rule for abbreviated
39065 function templates.
39067 NOTE: We can generate implicit parameters when tentatively
39068 parsing a nested name specifier, only to reject that parse
39069 later. However, matching the same template-id as part of a
39070 direct-declarator should generate an identical template
39071 parameter, so this rule will merge them. */
39072 if (parser
->implicit_template_scope
&& constr
)
39074 tree t
= parser
->implicit_template_parms
;
39077 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39079 tree d
= TREE_VALUE (t
);
39080 if (TREE_CODE (d
) == PARM_DECL
)
39081 /* Return the TEMPLATE_PARM_INDEX. */
39082 d
= DECL_INITIAL (d
);
39085 t
= TREE_CHAIN (t
);
39089 /* We are either continuing a function template that already contains implicit
39090 template parameters, creating a new fully-implicit function template, or
39091 extending an existing explicit function template with implicit template
39094 cp_binding_level
*const entry_scope
= current_binding_level
;
39096 bool become_template
= false;
39097 cp_binding_level
*parent_scope
= 0;
39099 if (parser
->implicit_template_scope
)
39101 gcc_assert (parser
->implicit_template_parms
);
39103 current_binding_level
= parser
->implicit_template_scope
;
39107 /* Roll back to the existing template parameter scope (in the case of
39108 extending an explicit function template) or introduce a new template
39109 parameter scope ahead of the function parameter scope (or class scope
39110 in the case of out-of-line member definitions). The function scope is
39111 added back after template parameter synthesis below. */
39113 cp_binding_level
*scope
= entry_scope
;
39115 while (scope
->kind
== sk_function_parms
)
39117 parent_scope
= scope
;
39118 scope
= scope
->level_chain
;
39120 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39122 /* If not defining a class, then any class scope is a scope level in
39123 an out-of-line member definition. In this case simply wind back
39124 beyond the first such scope to inject the template parameter list.
39125 Otherwise wind back to the class being defined. The latter can
39126 occur in class member friend declarations such as:
39132 friend void A::foo (auto);
39135 The template parameter list synthesized for the friend declaration
39136 must be injected in the scope of 'B'. This can also occur in
39137 erroneous cases such as:
39143 void B::foo (auto) {}
39146 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39147 but, nevertheless, the template parameter list synthesized for the
39148 declarator should be injected into the scope of 'A' as if the
39149 ill-formed template was specified explicitly. */
39151 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39153 parent_scope
= scope
;
39154 scope
= scope
->level_chain
;
39158 current_binding_level
= scope
;
39160 if (scope
->kind
!= sk_template_parms
39161 || !function_being_declared_is_template_p (parser
))
39163 /* Introduce a new template parameter list for implicit template
39166 become_template
= true;
39168 parser
->implicit_template_scope
39169 = begin_scope (sk_template_parms
, NULL
);
39171 ++processing_template_decl
;
39173 parser
->fully_implicit_function_template_p
= true;
39174 ++parser
->num_template_parameter_lists
;
39178 /* Synthesize implicit template parameters at the end of the explicit
39179 template parameter list. */
39181 gcc_assert (current_template_parms
);
39183 parser
->implicit_template_scope
= scope
;
39185 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39186 parser
->implicit_template_parms
39187 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39191 /* Synthesize a new template parameter and track the current template
39192 parameter chain with implicit_template_parms. */
39194 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39195 tree synth_id
= make_generic_type_name ();
39196 tree synth_tmpl_parm
;
39197 bool non_type
= false;
39199 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39201 = finish_template_type_parm (class_type_node
, synth_id
);
39202 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39204 = finish_constrained_template_template_parm (proto
, synth_id
);
39207 synth_tmpl_parm
= copy_decl (proto
);
39208 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39212 // Attach the constraint to the parm before processing.
39213 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39214 TREE_TYPE (node
) = constr
;
39216 = process_template_parm (parser
->implicit_template_parms
,
39219 /*non_type=*/non_type
,
39220 /*param_pack=*/false);
39222 // Chain the new parameter to the list of implicit parameters.
39223 if (parser
->implicit_template_parms
)
39224 parser
->implicit_template_parms
39225 = TREE_CHAIN (parser
->implicit_template_parms
);
39227 parser
->implicit_template_parms
= new_parm
;
39229 tree new_decl
= get_local_decls ();
39231 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39232 new_decl
= DECL_INITIAL (new_decl
);
39234 /* If creating a fully implicit function template, start the new implicit
39235 template parameter list with this synthesized type, otherwise grow the
39236 current template parameter list. */
39238 if (become_template
)
39240 parent_scope
->level_chain
= current_binding_level
;
39242 tree new_parms
= make_tree_vec (1);
39243 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39244 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39245 new_parms
, current_template_parms
);
39249 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39250 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39251 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39252 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39255 // If the new parameter was constrained, we need to add that to the
39256 // constraints in the template parameter list.
39257 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39259 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39260 reqs
= conjoin_constraints (reqs
, req
);
39261 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39264 current_binding_level
= entry_scope
;
39269 /* Finish the declaration of a fully implicit function template. Such a
39270 template has no explicit template parameter list so has not been through the
39271 normal template head and tail processing. synthesize_implicit_template_parm
39272 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39273 provided if the declaration is a class member such that its template
39274 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39275 form is returned. Otherwise NULL_TREE is returned. */
39278 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39280 gcc_assert (parser
->fully_implicit_function_template_p
);
39282 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39283 && DECL_VIRTUAL_P (member_decl_opt
))
39285 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39286 "implicit templates may not be %<virtual%>");
39287 DECL_VIRTUAL_P (member_decl_opt
) = false;
39290 if (member_decl_opt
)
39291 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39292 end_template_decl ();
39294 parser
->fully_implicit_function_template_p
= false;
39295 parser
->implicit_template_parms
= 0;
39296 parser
->implicit_template_scope
= 0;
39297 --parser
->num_template_parameter_lists
;
39299 return member_decl_opt
;
39302 /* Like finish_fully_implicit_template, but to be used in error
39303 recovery, rearranging scopes so that we restore the state we had
39304 before synthesize_implicit_template_parm inserted the implement
39305 template parms scope. */
39308 abort_fully_implicit_template (cp_parser
*parser
)
39310 cp_binding_level
*return_to_scope
= current_binding_level
;
39312 if (parser
->implicit_template_scope
39313 && return_to_scope
!= parser
->implicit_template_scope
)
39315 cp_binding_level
*child
= return_to_scope
;
39316 for (cp_binding_level
*scope
= child
->level_chain
;
39317 scope
!= parser
->implicit_template_scope
;
39318 scope
= child
->level_chain
)
39320 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39321 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39322 current_binding_level
= parser
->implicit_template_scope
;
39325 return_to_scope
= return_to_scope
->level_chain
;
39327 finish_fully_implicit_template (parser
, NULL
);
39329 gcc_assert (current_binding_level
== return_to_scope
);
39332 /* Helper function for diagnostics that have complained about things
39333 being used with 'extern "C"' linkage.
39335 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39338 maybe_show_extern_c_location (void)
39340 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39341 inform (the_parser
->innermost_linkage_specification_location
,
39342 "%<extern \"C\"%> linkage started here");
39345 #include "gt-cp-parser.h"